4
4
using System . Drawing ;
5
5
using System . IO ;
6
6
using System . Linq ;
7
+ using System . Threading ;
7
8
using System . Windows . Forms ;
8
9
using Newtonsoft . Json ;
9
10
using Universal_THCRAP_Launcher . Properties ;
@@ -35,89 +36,86 @@ void ErrorAndExit(string errorMessage)
35
36
36
37
private void Form1_Load ( object sender , EventArgs e )
37
38
{
38
-
39
+ //Give error if not next to thcrap_loader.exe
39
40
const string msgError1 =
40
41
"thcrap_loader.exe couldn't be found.\n Make sure you put the application next to it!" ;
41
42
bool fileExists = ! File . Exists ( "thcrap_loader.exe" ) ;
42
43
if ( fileExists )
43
44
ErrorAndExit ( msgError1 ) ;
44
45
46
+ //Give error if no games.js file
45
47
const string msgError2 =
46
48
"games.js couldn't be found.\n Make sure you run thcrap_configure.exe first!" ;
47
49
if ( ! File . Exists ( "games.js" ) ) ErrorAndExit ( msgError2 ) ;
48
50
49
- _jsFiles = Directory . GetFiles ( Directory . GetCurrentDirectory ( ) , "*.js" ) . ToList ( ) ;
50
-
51
- for ( int i = 0 ; i < _jsFiles . Count ; i ++ )
52
- _jsFiles [ i ] = _jsFiles [ i ] . Replace ( Directory . GetCurrentDirectory ( ) + "\\ " , "" ) ;
51
+ #region Load data from files
53
52
54
- _jsFiles . Remove ( "games.js" ) ;
55
- _jsFiles . Remove ( "config.js" ) ;
56
- _jsFiles . Remove ( "favourites.js" ) ;
53
+ //Load patch stacks
54
+ _jsFiles = Directory . GetFiles ( Directory . GetCurrentDirectory ( ) , "*.js" ) . ToList ( ) ;
57
55
56
+ //Give error if there are no .js files apart from games.js and config.js
58
57
const string msgError3 =
59
58
"No config files could be found.\n Make sure you run thcrap_configure.exe first!" ;
60
59
if ( _jsFiles . Count == 0 ) ErrorAndExit ( msgError3 ) ;
61
60
62
- foreach ( var item in _jsFiles )
63
- listBox1 . Items . Add ( item ) ;
64
-
65
- FileStream fs = new FileStream ( "games.js" , FileMode . Open ) ;
66
- StreamReader sr = new StreamReader ( fs ) ;
67
- string file = sr . ReadToEnd ( ) ;
68
- sr . Close ( ) ;
69
- fs . Close ( ) ;
70
-
61
+ //Load executables
62
+ string file = File . ReadAllText ( "games.js" ) ;
71
63
Dictionary < string , string > games = JsonConvert . DeserializeObject < Dictionary < string , string > > ( file ) ;
72
64
73
-
74
- foreach ( var item in games )
75
- {
76
- _gamesList . Add ( item . Key ) ;
77
- listBox2 . Items . Add ( item . Key ) ;
78
- }
79
-
80
- listBox1 . SelectedIndex = 0 ;
81
- listBox2 . SelectedIndex = 0 ;
82
-
83
- //MessageBox.Show((string)listBox1.SelectedItem);
84
-
85
- Configuration1 . UthcraplLastConfig = ( string ) listBox1 . SelectedItem ;
86
- Configuration1 . UthcraplLastGame = ( string ) listBox2 . SelectedItem ;
87
-
88
- //MessageBox.Show(Configuration1.UthcraplLastConfig);
89
-
65
+ //Load config
90
66
if ( File . Exists ( "config.js" ) )
91
67
{
92
68
file = File . ReadAllText ( "config.js" ) ;
93
69
Configuration1 = JsonConvert . DeserializeObject < Configuration > ( file ) ;
94
- checkBox1 . Checked = Configuration1 . UthcraplExitAfterStartup ;
95
- if ( Configuration1 . UthcraplLastConfig != null || Configuration1 . UthcraplLastGame != null )
96
- {
97
- listBox1 . SelectedIndex = listBox1 . FindString ( Configuration1 . UthcraplLastConfig ) ;
98
- listBox2 . SelectedIndex = listBox2 . FindString ( Configuration1 . UthcraplLastGame ) ;
99
- }
100
70
}
101
71
72
+ //Load favourites
102
73
if ( File . Exists ( "favourites.js" ) )
103
74
{
104
75
file = File . ReadAllText ( "favourites.js" ) ;
105
76
Favourites1 = JsonConvert . DeserializeObject < Favourites > ( file ) ;
106
- foreach ( var VARIABLE in Favourites1 . Patches )
107
- {
108
- int index = listBox1 . FindStringExact ( VARIABLE ) ;
109
- listBox1 . Items [ index ] += " ★" ;
110
- }
77
+ }
111
78
112
- foreach ( var VARIABLE in Favourites1 . Games )
113
- {
114
- int index = listBox2 . FindStringExact ( VARIABLE ) ;
115
- listBox2 . Items [ index ] += " ★" ;
116
- }
79
+ #endregion
80
+ #region Fix patch stack list
81
+ for ( int i = 0 ; i < _jsFiles . Count ; i ++ )
82
+ _jsFiles [ i ] = _jsFiles [ i ] . Replace ( Directory . GetCurrentDirectory ( ) + "\\ " , "" ) ;
83
+ _jsFiles . Remove ( "games.js" ) ;
84
+ _jsFiles . Remove ( "config.js" ) ;
85
+ _jsFiles . Remove ( "favourites.js" ) ;
86
+ #endregion
87
+
88
+ #region Display
89
+ //Display patch stacks
90
+ foreach ( var item in _jsFiles )
91
+ listBox1 . Items . Add ( item ) ;
92
+ //Display executables
93
+ foreach ( var item in games )
94
+ {
95
+ _gamesList . Add ( item . Key ) ;
96
+ listBox2 . Items . Add ( item . Key ) ;
117
97
}
98
+
99
+ //Display config
100
+ checkBox1 . Checked = Configuration1 . UthcraplExitAfterStartup ;
118
101
119
- UpdateConfigFile ( ) ;
102
+
103
+
104
+ //Update Display favourites
105
+ foreach ( var VARIABLE in Favourites1 . Patches )
106
+ {
107
+ int index = listBox1 . FindStringExact ( VARIABLE ) ;
108
+ listBox1 . Items [ index ] += " ★" ;
109
+ }
110
+ foreach ( var VARIABLE in Favourites1 . Games )
111
+ {
112
+ int index = listBox2 . FindStringExact ( VARIABLE ) ;
113
+ listBox2 . Items [ index ] += " ★" ;
114
+ }
115
+ #endregion
116
+ #region Set stuff
120
117
118
+ //Create const for resizing
121
119
_resizeConsts = new int [ 8 ] ;
122
120
_resizeConsts [ 0 ] = Size . Width - button1 . Width ;
123
121
_resizeConsts [ 1 ] = Size . Width - splitContainer1 . Width ;
@@ -128,9 +126,11 @@ private void Form1_Load(object sender, EventArgs e)
128
126
_resizeConsts [ 6 ] = sort_az_button2 . Location . X - listBox1 . Size . Width ;
129
127
_resizeConsts [ 7 ] = star_button2 . Location . X - sort_az_button2 . Location . X ;
130
128
129
+ #endregion
130
+
131
+ //Default sort
131
132
SortListBoxItems ( ref listBox1 ) ;
132
133
SortListBoxItems ( ref listBox2 ) ;
133
-
134
134
Debug . WriteLine ( "Form1 Loaded" ) ;
135
135
}
136
136
@@ -152,12 +152,21 @@ private void SortListBoxItemsDesc(ref ListBox lb)
152
152
lb . Items . AddRange ( items . OrderByDescending ( i => i ) . ToArray ( ) ) ;
153
153
}
154
154
155
- private void UpdateConfigFile ( )
155
+ /// <summary>
156
+ /// Updates the configuration and favourites list
157
+ /// </summary>
158
+ private void UpdateConfig ( )
156
159
{
157
- Configuration1 . UthcraplLastConfig = ( string ) listBox1 . SelectedItem ;
158
- Configuration1 . UthcraplLastGame = ( string ) listBox2 . SelectedItem ;
159
- string output = JsonConvert . SerializeObject ( Configuration1 , Formatting . Indented ) ;
160
- File . WriteAllText ( "config.js" , output ) ;
160
+ if ( listBox1 . SelectedIndex == - 1 && listBox1 . Items . Count > 0 )
161
+ listBox1 . SelectedIndex = 0 ;
162
+ if ( listBox1 . SelectedIndex != - 1 )
163
+ Configuration1 . UthcraplLastConfig = ( ( string ) listBox1 . SelectedItem ) . Replace ( " ★" , "" ) ;
164
+ if ( listBox2 . SelectedIndex == - 1 && listBox2 . Items . Count > 0 )
165
+ listBox2 . SelectedIndex = 0 ;
166
+ if ( listBox2 . SelectedIndex != - 1 )
167
+ Configuration1 . UthcraplLastGame = ( ( string ) listBox2 . SelectedItem ) . Replace ( " ★" , "" ) ;
168
+
169
+
161
170
162
171
Favourites1 . Patches . Clear ( ) ;
163
172
Favourites1 . Games . Clear ( ) ;
@@ -175,15 +184,56 @@ private void UpdateConfigFile()
175
184
string v = s . Replace ( " ★" , "" ) ;
176
185
Favourites1 . Games . Add ( v ) ;
177
186
}
187
+ }
188
+
189
+ /// <summary>
190
+ /// Selects the items based on the configuration
191
+ /// </summary>
192
+ private void ReadConfig ( )
193
+ {
194
+ string s = Configuration1 . UthcraplLastConfig ;
195
+ if ( Favourites1 . Patches . Contains ( s ) )
196
+ s += " ★" ;
197
+ listBox1 . SelectedIndex = listBox1 . FindStringExact ( s ) ;
198
+ s = Configuration1 . UthcraplLastGame ;
199
+
200
+ if ( Favourites1 . Games . Contains ( s ) )
201
+ s += " ★" ;
202
+
203
+ listBox2 . SelectedIndex = listBox2 . FindStringExact ( s ) ;
204
+
205
+ if ( listBox1 . SelectedIndex == - 1 )
206
+ listBox1 . SelectedIndex = 0 ;
207
+ if ( listBox2 . SelectedIndex == - 1 )
208
+ listBox2 . SelectedIndex = 0 ;
209
+ }
210
+
211
+ /// <summary>
212
+ /// Writes the configuration and favourites to file
213
+ /// </summary>
214
+ private void UpdateConfigFile ( )
215
+ {
216
+ UpdateConfig ( ) ;
217
+ string output = JsonConvert . SerializeObject ( Configuration1 , Formatting . Indented ) ;
218
+ File . WriteAllText ( "config.js" , output ) ;
178
219
179
220
output = JsonConvert . SerializeObject ( Favourites1 , Formatting . Indented ) ;
180
221
File . WriteAllText ( "favourites.js" , output ) ;
181
222
182
223
Debug . WriteLine ( "Config file Updated!" ) ;
183
224
}
184
225
226
+ /// <summary>
227
+ /// Starts thcrap with the selected patch stack and executable
228
+ /// </summary>
185
229
private void StartThcrap ( )
186
230
{
231
+ if ( listBox1 . SelectedIndex == - 1 || listBox2 . SelectedIndex == - 1 )
232
+ {
233
+ const string error = "No run configuration (patch stack) or game (executable) selected!\n Please select one!" ;
234
+ MessageBox . Show ( error , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
235
+ return ;
236
+ }
187
237
string s = "" ;
188
238
s += listBox1 . SelectedItem ;
189
239
s += " " ;
@@ -199,6 +249,9 @@ private void StartThcrap()
199
249
200
250
private void button1_Click ( object sender , EventArgs e ) => StartThcrap ( ) ;
201
251
252
+ /// <summary>
253
+ /// Handles starting thcrap with enter and favouriting when pressing f
254
+ /// </summary>
202
255
private new void KeyPress ( object sender , KeyPressEventArgs e )
203
256
{
204
257
if ( e . KeyChar == ( char ) Keys . Enter )
@@ -259,7 +312,7 @@ private void sort_az_button1_Click(object sender, EventArgs e)
259
312
SortListBoxItemsDesc ( ref listBox1 ) ;
260
313
sort_az_button1 . BackgroundImage = _sortAscending ;
261
314
}
262
- Refresh ( ) ;
315
+ ReadConfig ( ) ;
263
316
}
264
317
265
318
private void sort_az_button2_Click ( object sender , EventArgs e )
@@ -274,7 +327,7 @@ private void sort_az_button2_Click(object sender, EventArgs e)
274
327
SortListBoxItemsDesc ( ref listBox2 ) ;
275
328
sort_az_button2 . BackgroundImage = _sortAscending ;
276
329
}
277
- Refresh ( ) ;
330
+ ReadConfig ( ) ;
278
331
}
279
332
280
333
private readonly Image _star = new Bitmap ( Resources . Star ) ;
@@ -304,7 +357,9 @@ private void star_button1_Click(object sender, EventArgs e)
304
357
{
305
358
int index = listBox1 . FindStringExact ( VARIABLE ) ;
306
359
listBox1 . Items [ index ] += " ★" ;
360
+
307
361
}
362
+ ReadConfig ( ) ;
308
363
}
309
364
}
310
365
@@ -333,6 +388,36 @@ private void star_button2_Click(object sender, EventArgs e)
333
388
int index = listBox2 . FindStringExact ( VARIABLE ) ;
334
389
listBox2 . Items [ index ] += " ★" ;
335
390
}
391
+ ReadConfig ( ) ;
392
+ }
393
+ }
394
+
395
+ private void Form1_Shown ( object sender , EventArgs e )
396
+ {
397
+ ReadConfig ( ) ;
398
+ //Set default selection index
399
+ if ( listBox1 . SelectedIndex == - 1 )
400
+ listBox1 . SelectedIndex = 0 ;
401
+
402
+ if ( listBox2 . SelectedIndex == - 1 )
403
+ listBox2 . SelectedIndex = 0 ;
404
+
405
+ UpdateConfigFile ( ) ;
406
+ }
407
+
408
+ private void SelectedIndexChanged ( object sender , EventArgs e )
409
+ {
410
+ ListBox lb = ( ListBox ) sender ;
411
+ switch ( lb . Name )
412
+ {
413
+ case "listBox1" :
414
+ if ( lb . SelectedIndex != - 1 )
415
+ Configuration1 . UthcraplLastConfig = lb . SelectedItem . ToString ( ) . Replace ( " ★" , "" ) ;
416
+ break ;
417
+ case "listBox2" :
418
+ if ( lb . SelectedIndex != - 1 )
419
+ Configuration1 . UthcraplLastGame = lb . SelectedItem . ToString ( ) . Replace ( " ★" , "" ) ;
420
+ break ;
336
421
}
337
422
}
338
423
}
0 commit comments