Skip to content

Commit dd3eabd

Browse files
author
Tudi20
committed
Added a few comments
Reordered some stuff Remade the select last selected one Added an error if nothing selected
1 parent 80c11d6 commit dd3eabd

File tree

2 files changed

+147
-59
lines changed

2 files changed

+147
-59
lines changed

Universal THCRAP Launcher/Form1.Designer.cs

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Universal THCRAP Launcher/Form1.cs

Lines changed: 144 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Drawing;
55
using System.IO;
66
using System.Linq;
7+
using System.Threading;
78
using System.Windows.Forms;
89
using Newtonsoft.Json;
910
using Universal_THCRAP_Launcher.Properties;
@@ -35,89 +36,86 @@ void ErrorAndExit(string errorMessage)
3536

3637
private void Form1_Load(object sender, EventArgs e)
3738
{
38-
39+
//Give error if not next to thcrap_loader.exe
3940
const string msgError1 =
4041
"thcrap_loader.exe couldn't be found.\nMake sure you put the application next to it!";
4142
bool fileExists = !File.Exists("thcrap_loader.exe");
4243
if (fileExists)
4344
ErrorAndExit(msgError1);
4445

46+
//Give error if no games.js file
4547
const string msgError2 =
4648
"games.js couldn't be found.\nMake sure you run thcrap_configure.exe first!";
4749
if (!File.Exists("games.js")) ErrorAndExit(msgError2);
4850

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
5352

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();
5755

56+
//Give error if there are no .js files apart from games.js and config.js
5857
const string msgError3 =
5958
"No config files could be found.\nMake sure you run thcrap_configure.exe first!";
6059
if (_jsFiles.Count == 0) ErrorAndExit(msgError3);
6160

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");
7163
Dictionary<string, string> games = JsonConvert.DeserializeObject<Dictionary<string, string>>(file);
7264

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
9066
if (File.Exists("config.js"))
9167
{
9268
file = File.ReadAllText("config.js");
9369
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-
}
10070
}
10171

72+
//Load favourites
10273
if (File.Exists("favourites.js"))
10374
{
10475
file = File.ReadAllText("favourites.js");
10576
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+
}
11178

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);
11797
}
98+
99+
//Display config
100+
checkBox1.Checked = Configuration1.UthcraplExitAfterStartup;
118101

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
120117

118+
//Create const for resizing
121119
_resizeConsts = new int[8];
122120
_resizeConsts[0] = Size.Width - button1.Width;
123121
_resizeConsts[1] = Size.Width - splitContainer1.Width;
@@ -128,9 +126,11 @@ private void Form1_Load(object sender, EventArgs e)
128126
_resizeConsts[6] = sort_az_button2.Location.X - listBox1.Size.Width;
129127
_resizeConsts[7] = star_button2.Location.X - sort_az_button2.Location.X;
130128

129+
#endregion
130+
131+
//Default sort
131132
SortListBoxItems(ref listBox1);
132133
SortListBoxItems(ref listBox2);
133-
134134
Debug.WriteLine("Form1 Loaded");
135135
}
136136

@@ -152,12 +152,21 @@ private void SortListBoxItemsDesc(ref ListBox lb)
152152
lb.Items.AddRange(items.OrderByDescending(i => i).ToArray());
153153
}
154154

155-
private void UpdateConfigFile()
155+
/// <summary>
156+
/// Updates the configuration and favourites list
157+
/// </summary>
158+
private void UpdateConfig()
156159
{
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+
161170

162171
Favourites1.Patches.Clear();
163172
Favourites1.Games.Clear();
@@ -175,15 +184,56 @@ private void UpdateConfigFile()
175184
string v = s.Replace(" ★", "");
176185
Favourites1.Games.Add(v);
177186
}
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);
178219

179220
output = JsonConvert.SerializeObject(Favourites1,Formatting.Indented);
180221
File.WriteAllText("favourites.js", output);
181222

182223
Debug.WriteLine("Config file Updated!");
183224
}
184225

226+
/// <summary>
227+
/// Starts thcrap with the selected patch stack and executable
228+
/// </summary>
185229
private void StartThcrap()
186230
{
231+
if (listBox1.SelectedIndex == -1 || listBox2.SelectedIndex == -1)
232+
{
233+
const string error = "No run configuration (patch stack) or game (executable) selected!\nPlease select one!";
234+
MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
235+
return;
236+
}
187237
string s = "";
188238
s += listBox1.SelectedItem;
189239
s += " ";
@@ -199,6 +249,9 @@ private void StartThcrap()
199249

200250
private void button1_Click(object sender, EventArgs e) => StartThcrap();
201251

252+
/// <summary>
253+
/// Handles starting thcrap with enter and favouriting when pressing f
254+
/// </summary>
202255
private new void KeyPress(object sender, KeyPressEventArgs e)
203256
{
204257
if (e.KeyChar == (char) Keys.Enter)
@@ -259,7 +312,7 @@ private void sort_az_button1_Click(object sender, EventArgs e)
259312
SortListBoxItemsDesc(ref listBox1);
260313
sort_az_button1.BackgroundImage = _sortAscending;
261314
}
262-
Refresh();
315+
ReadConfig();
263316
}
264317

265318
private void sort_az_button2_Click(object sender, EventArgs e)
@@ -274,7 +327,7 @@ private void sort_az_button2_Click(object sender, EventArgs e)
274327
SortListBoxItemsDesc(ref listBox2);
275328
sort_az_button2.BackgroundImage = _sortAscending;
276329
}
277-
Refresh();
330+
ReadConfig();
278331
}
279332

280333
private readonly Image _star = new Bitmap(Resources.Star);
@@ -304,7 +357,9 @@ private void star_button1_Click(object sender, EventArgs e)
304357
{
305358
int index = listBox1.FindStringExact(VARIABLE);
306359
listBox1.Items[index] += " ★";
360+
307361
}
362+
ReadConfig();
308363
}
309364
}
310365

@@ -333,6 +388,36 @@ private void star_button2_Click(object sender, EventArgs e)
333388
int index = listBox2.FindStringExact(VARIABLE);
334389
listBox2.Items[index] += " ★";
335390
}
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;
336421
}
337422
}
338423
}

0 commit comments

Comments
 (0)