Skip to content

Commit a834a7c

Browse files
committed
Fix application launch when no config exists
Signed-off-by: Kláben Szabolcs Bence (Tudi20) <[email protected]>
1 parent d27edfe commit a834a7c

File tree

1 file changed

+51
-26
lines changed

1 file changed

+51
-26
lines changed

Universal THCRAP Launcher/Form1.cs

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ private void Form1_Load(object sender, EventArgs e)
6363
const string msgError2 =
6464
"games.js couldn't be found.\nMake sure you run thcrap_configure.exe first!";
6565
if (!File.Exists("games.js")) ErrorAndExit(msgError2);
66+
67+
DeleteOutdatedConfig();
6668

6769
#region Load data from files
6870

@@ -94,23 +96,15 @@ private void Form1_Load(object sender, EventArgs e)
9496
file = File.ReadAllText(ConfigFile);
9597
Configuration1 = JsonConvert.DeserializeObject<Configuration>(file, settings);
9698
}
97-
98-
if (Configuration1.IsDescending.Count == 0)
99-
for (var i = 0; i < 2; i++)
100-
Configuration1.IsDescending.Add("false");
101-
102-
if (Configuration1.OnlyFavourites.Count == 0)
103-
for (var i = 0; i < 2; i++)
104-
Configuration1.OnlyFavourites.Add("false");
99+
105100
//Load favourites
106101
if (File.Exists("favourites.js"))
107102
{
108103
file = File.ReadAllText("favourites.js");
109104
Favourites1 = JsonConvert.DeserializeObject<Favourites>(file);
110105
}
111-
112106
#endregion
113-
107+
114108
#region Fix patch stack list
115109

116110
for (var i = 0; i < _jsFiles.Count; i++)
@@ -138,10 +132,6 @@ private void Form1_Load(object sender, EventArgs e)
138132

139133
#region Display
140134

141-
//Change Form settings
142-
SetDesktopLocation(Configuration1.Window.Location[0], Configuration1.Window.Location[1]);
143-
Size = new Size(Configuration1.Window.Size[0], Configuration1.Window.Size[1]);
144-
145135
//Display patch stacks
146136
foreach (var item in _jsFiles)
147137
listBox1.Items.Add(item);
@@ -151,7 +141,13 @@ private void Form1_Load(object sender, EventArgs e)
151141
_gamesList.Add(item.Key);
152142
listBox2.Items.Add(item.Key);
153143
}
154-
144+
145+
SetDefaultSettings();
146+
147+
//Change Form settings
148+
SetDesktopLocation(Configuration1.Window.Location[0], Configuration1.Window.Location[1]);
149+
Size = new Size(Configuration1.Window.Size[0], Configuration1.Window.Size[1]);
150+
155151
//Display config
156152
checkBox1.Checked = Configuration1.ExitAfterStartup;
157153

@@ -161,8 +157,38 @@ private void Form1_Load(object sender, EventArgs e)
161157

162158
#endregion
163159

164-
#region Set default state for the sort/filter buttons
165160

161+
if (menuStrip1 == null) return;
162+
menuStrip1.Items.OfType<ToolStripMenuItem>().ToList().ForEach(x =>
163+
x.MouseHover += (obj, arg) => ((ToolStripDropDownItem) obj).ShowDropDown());
164+
165+
Debug.WriteLine("Form1 Loaded");
166+
}
167+
168+
private void SetDefaultSettings()
169+
{
170+
//Default Configuration setting
171+
if (Configuration1.LastGame == null) Configuration1.LastGame = _gamesList[0];
172+
if (Configuration1.LastConfig == null) Configuration1.LastConfig = _jsFiles[0];
173+
if (Configuration1.IsDescending == null)
174+
{
175+
string[] a = {"false", "false"};
176+
Configuration1.IsDescending = a;
177+
}
178+
179+
if (Configuration1.OnlyFavourites == null)
180+
{
181+
string[] a = {"false", "false"};
182+
Configuration1.OnlyFavourites = a;
183+
}
184+
185+
if (Configuration1.Window == null)
186+
{
187+
var window = new Window {Size = new[] {Size.Width, Size.Height}, Location = new[] {Location.X, Location.Y}};
188+
Configuration1.Window = window;
189+
}
190+
191+
166192
//Default sort
167193
for (var i = 0; i < 2; i++)
168194
if (Configuration1.IsDescending[i] == "false")
@@ -224,15 +250,10 @@ private void Form1_Load(object sender, EventArgs e)
224250
filterByType_button.BackgroundImage = _gameAndCustom;
225251
for (var i = 0; i < Configuration1.FilterExeType; i++) filterByType_button_Click(null, new EventArgs());
226252

227-
#endregion
253+
254+
}
228255

229-
if (menuStrip1 == null) return;
230-
menuStrip1.Items.OfType<ToolStripMenuItem>().ToList().ForEach(x =>
231-
x.MouseHover += (obj, arg) => ((ToolStripDropDownItem) obj).ShowDropDown());
232256

233-
Debug.WriteLine("Form1 Loaded");
234-
}
235-
236257
private void Form1_Shown(object sender, EventArgs e)
237258
{
238259
ReadConfig();
@@ -245,6 +266,11 @@ private void Form1_Shown(object sender, EventArgs e)
245266

246267
UpdateConfigFile();
247268
}
269+
270+
private void DeleteOutdatedConfig()
271+
{
272+
if(File.Exists("uthcrapl_confis.js")) File.Delete("uthcrapl_confis.js");
273+
}
248274

249275
private void Form1_Resize(object sender, EventArgs e)
250276
{
@@ -327,7 +353,6 @@ private void UpdateConfig()
327353
Configuration1.LastGame = ((string) listBox2.SelectedItem).Replace(" ★", "");
328354

329355
var window = new Window {Size = new[] {Size.Width, Size.Height}, Location = new[] {Location.X, Location.Y}};
330-
331356
Configuration1.Window = window;
332357

333358
Favourites1.Patches.Clear();
@@ -677,8 +702,8 @@ public class Configuration
677702
public bool ExitAfterStartup { get; set; }
678703
public string LastConfig { get; set; }
679704
public string LastGame { get; set; }
680-
public List<string> IsDescending { get; set; }
681-
public List<string> OnlyFavourites { get; set; }
705+
public string[] IsDescending { get; set; }
706+
public string[] OnlyFavourites { get; set; }
682707
public byte FilterExeType { get; set; }
683708
public Window Window { get; set; }
684709
}

0 commit comments

Comments
 (0)