Skip to content

Commit ec88cf1

Browse files
committed
Cleanup Code
Signed-off-by: Kláben Szabolcs Bence (Tudi20) <[email protected]>
1 parent a834a7c commit ec88cf1

File tree

1 file changed

+122
-119
lines changed

1 file changed

+122
-119
lines changed

Universal THCRAP Launcher/Form1.cs

Lines changed: 122 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,6 @@ namespace Universal_THCRAP_Launcher
1616
{
1717
public partial class Form1 : Form
1818
{
19-
#region Global variables
20-
private const string ConfigFile = "utl_config.js";
21-
private readonly Image _custom = new Bitmap(Resources.Custom);
22-
private readonly Image _game = new Bitmap(Resources.Game);
23-
24-
private readonly Image _gameAndCustom = new Bitmap(Resources.GameAndCustom);
25-
private readonly List<string> _gamesList = new List<string>();
26-
27-
private readonly Image _sortAscending = new Bitmap(Resources.Sort_Ascending);
28-
private readonly Image _sortDescending = new Bitmap(Resources.Sort_Decending);
29-
30-
private readonly Image _star = new Bitmap(Resources.Star);
31-
private readonly Image _starHollow = new Bitmap(Resources.Star_Hollow);
32-
33-
private List<string> _jsFiles = new List<string>();
34-
35-
private int[] _resizeConstants;
36-
37-
private Configuration Configuration1 { get; set; }
38-
private Favourites Favourites1 { get; set; } = new Favourites(new List<string>(), new List<string>());
39-
#endregion
40-
4119
public Form1()
4220
{
4321
InitializeComponent();
@@ -63,7 +41,7 @@ private void Form1_Load(object sender, EventArgs e)
6341
const string msgError2 =
6442
"games.js couldn't be found.\nMake sure you run thcrap_configure.exe first!";
6543
if (!File.Exists("games.js")) ErrorAndExit(msgError2);
66-
44+
6745
DeleteOutdatedConfig();
6846

6947
#region Load data from files
@@ -96,15 +74,16 @@ private void Form1_Load(object sender, EventArgs e)
9674
file = File.ReadAllText(ConfigFile);
9775
Configuration1 = JsonConvert.DeserializeObject<Configuration>(file, settings);
9876
}
99-
77+
10078
//Load favourites
10179
if (File.Exists("favourites.js"))
10280
{
10381
file = File.ReadAllText("favourites.js");
10482
Favourites1 = JsonConvert.DeserializeObject<Favourites>(file);
10583
}
84+
10685
#endregion
107-
86+
10887
#region Fix patch stack list
10988

11089
for (var i = 0; i < _jsFiles.Count; i++)
@@ -141,14 +120,13 @@ private void Form1_Load(object sender, EventArgs e)
141120
_gamesList.Add(item.Key);
142121
listBox2.Items.Add(item.Key);
143122
}
144-
123+
145124
SetDefaultSettings();
146-
125+
147126
//Change Form settings
148127
SetDesktopLocation(Configuration1.Window.Location[0], Configuration1.Window.Location[1]);
149128
Size = new Size(Configuration1.Window.Size[0], Configuration1.Window.Size[1]);
150-
151-
//Display config
129+
152130
checkBox1.Checked = Configuration1.ExitAfterStartup;
153131

154132
//Update Display favourites
@@ -184,11 +162,12 @@ private void SetDefaultSettings()
184162

185163
if (Configuration1.Window == null)
186164
{
187-
var window = new Window {Size = new[] {Size.Width, Size.Height}, Location = new[] {Location.X, Location.Y}};
165+
var window = new Window
166+
{Size = new[] {Size.Width, Size.Height}, Location = new[] {Location.X, Location.Y}};
188167
Configuration1.Window = window;
189168
}
190-
191-
169+
170+
192171
//Default sort
193172
for (var i = 0; i < 2; i++)
194173
if (Configuration1.IsDescending[i] == "false")
@@ -249,8 +228,6 @@ private void SetDefaultSettings()
249228
//Default exe type button state
250229
filterByType_button.BackgroundImage = _gameAndCustom;
251230
for (var i = 0; i < Configuration1.FilterExeType; i++) filterByType_button_Click(null, new EventArgs());
252-
253-
254231
}
255232

256233

@@ -269,9 +246,9 @@ private void Form1_Shown(object sender, EventArgs e)
269246

270247
private void DeleteOutdatedConfig()
271248
{
272-
if(File.Exists("uthcrapl_confis.js")) File.Delete("uthcrapl_confis.js");
249+
if (File.Exists("uthcrapl_confis.js")) File.Delete("uthcrapl_confis.js");
273250
}
274-
251+
275252
private void Form1_Resize(object sender, EventArgs e)
276253
{
277254
button1.Size = new Size(Size.Width - _resizeConstants[0], button1.Size.Height);
@@ -290,7 +267,7 @@ private void Form1_Resize(object sender, EventArgs e)
290267
filterByType_button.Location = new Point(
291268
star_button2.Location.X + _resizeConstants[6], splitContainer1.Location.Y - _resizeConstants[4]);
292269
}
293-
270+
294271
private static void AddStars(ListBox listBox, IEnumerable<string> list)
295272
{
296273
foreach (var variable in list)
@@ -337,7 +314,7 @@ private void ReadConfig()
337314
if (listBox2.SelectedIndex == -1 && listBox2.Items.Count > 0)
338315
listBox2.SelectedIndex = 0;
339316
}
340-
317+
341318
/// <summary>
342319
/// Updates the configuration and favourites list
343320
/// </summary>
@@ -422,7 +399,107 @@ private void button1_MouseHover(object sender, EventArgs e) =>
422399
private void button1_MouseLeave(object sender, EventArgs e) =>
423400
button1.BackgroundImage = Resources.Shinmera_Banner_5_mini_size;
424401

402+
private void SelectedIndexChanged(object sender, EventArgs e)
403+
{
404+
if (ModifierKeys != Keys.None) return;
405+
var lb = (ListBox) sender;
406+
switch (lb.Name)
407+
{
408+
case "listBox1":
409+
if (lb.SelectedIndex != -1)
410+
Configuration1.LastConfig = lb.SelectedItem.ToString().Replace(" ★", "");
411+
break;
412+
case "listBox2":
413+
if (lb.SelectedIndex != -1)
414+
Configuration1.LastGame = lb.SelectedItem.ToString().Replace(" ★", "");
415+
break;
416+
default:
417+
Debug.WriteLine("Invalid ListBox!");
418+
break;
419+
}
420+
}
421+
422+
private void checkBox1_CheckedChanged(object sender, EventArgs e) =>
423+
Configuration1.ExitAfterStartup = checkBox1.Checked;
424+
425+
private void Form1_KeyUp(object sender, KeyEventArgs e)
426+
{
427+
if (ModifierKeys != Keys.None)
428+
{
429+
listBox1.SelectedItem = Configuration1.LastConfig;
430+
listBox2.SelectedItem = Configuration1.LastGame;
431+
}
432+
433+
switch (e.KeyCode)
434+
{
435+
case Keys.F2 when sender.GetType().FullName != "System.Windows.Forms.ListBox":
436+
case Keys.Enter when sender.GetType().FullName != "System.Windows.Forms.ListBox":
437+
return;
438+
case Keys.Enter:
439+
UpdateConfigFile();
440+
StartThcrap();
441+
break;
442+
case Keys.F2:
443+
var lb = (ListBox) sender;
444+
if (!lb.SelectedItem.ToString().Contains("★"))
445+
{
446+
if (lb.Equals(listBox1))
447+
Favourites1.Patches.Add(lb.Items[lb.SelectedIndex].ToString());
448+
449+
if (lb.Equals(listBox2))
450+
Favourites1.Games.Add(lb.Items[lb.SelectedIndex].ToString());
451+
lb.Items[lb.SelectedIndex] += " ★";
452+
}
453+
else
454+
{
455+
lb.Items[lb.SelectedIndex] = lb.Items[lb.SelectedIndex].ToString().Replace(" ★", "");
456+
}
457+
458+
UpdateConfigFile();
459+
break;
460+
}
461+
}
462+
463+
private static void RestartProgram()
464+
{
465+
Process.Start(Assembly.GetEntryAssembly().Location);
466+
Application.Exit();
467+
}
468+
469+
private static void ShowKeyboardShortcuts()
470+
{
471+
MessageBox.Show(Resources.KeyboardShortcuts,
472+
@"Keyboard Shortcuts", MessageBoxButtons.OK, MessageBoxIcon.Information);
473+
}
474+
475+
private void Form1_FormClosing(object sender, FormClosingEventArgs e) => UpdateConfigFile();
476+
477+
#region Global variables
478+
479+
private const string ConfigFile = "utl_config.js";
480+
private readonly Image _custom = new Bitmap(Resources.Custom);
481+
private readonly Image _game = new Bitmap(Resources.Game);
482+
483+
private readonly Image _gameAndCustom = new Bitmap(Resources.GameAndCustom);
484+
private readonly List<string> _gamesList = new List<string>();
485+
486+
private readonly Image _sortAscending = new Bitmap(Resources.Sort_Ascending);
487+
private readonly Image _sortDescending = new Bitmap(Resources.Sort_Decending);
488+
489+
private readonly Image _star = new Bitmap(Resources.Star);
490+
private readonly Image _starHollow = new Bitmap(Resources.Star_Hollow);
491+
492+
private List<string> _jsFiles = new List<string>();
493+
494+
private int[] _resizeConstants;
495+
496+
private Configuration Configuration1 { get; set; }
497+
private Favourites Favourites1 { get; set; } = new Favourites(new List<string>(), new List<string>());
498+
499+
#endregion
500+
425501
#region Sorting/Filtering Button functions
502+
426503
private void sort_az_button1_Click(object sender, EventArgs e)
427504
{
428505
var isDesc = Configuration1.IsDescending;
@@ -461,7 +538,7 @@ private void sort_az_button2_Click(object sender, EventArgs e)
461538

462539
Configuration1.IsDescending = isDesc;
463540
ReadConfig();
464-
}
541+
}
465542

466543
private void star_button1_Click(object sender, EventArgs e)
467544
{
@@ -520,7 +597,7 @@ private void star_button2_Click(object sender, EventArgs e)
520597
Configuration1.OnlyFavourites = onlyFav;
521598
ReadConfig();
522599
}
523-
600+
524601
private void filterByType_button_Click(object sender, EventArgs e)
525602
{
526603
if (filterByType_button.BackgroundImage.Equals(_gameAndCustom))
@@ -556,85 +633,13 @@ private void filterByType_button_Click(object sender, EventArgs e)
556633
if (sender != null) Configuration1.FilterExeType = 0;
557634
}
558635
}
559-
#endregion
560-
561-
private void SelectedIndexChanged(object sender, EventArgs e)
562-
{
563-
if (ModifierKeys != Keys.None) return;
564-
var lb = (ListBox) sender;
565-
switch (lb.Name)
566-
{
567-
case "listBox1":
568-
if (lb.SelectedIndex != -1)
569-
Configuration1.LastConfig = lb.SelectedItem.ToString().Replace(" ★", "");
570-
break;
571-
case "listBox2":
572-
if (lb.SelectedIndex != -1)
573-
Configuration1.LastGame = lb.SelectedItem.ToString().Replace(" ★", "");
574-
break;
575-
default:
576-
Debug.WriteLine("Invalid ListBox!");
577-
break;
578-
}
579-
}
580636

581-
private void checkBox1_CheckedChanged(object sender, EventArgs e) =>
582-
Configuration1.ExitAfterStartup = checkBox1.Checked;
583-
584-
private void Form1_KeyUp(object sender, KeyEventArgs e)
585-
{
586-
if (ModifierKeys != Keys.None)
587-
{
588-
listBox1.SelectedItem = Configuration1.LastConfig;
589-
listBox2.SelectedItem = Configuration1.LastGame;
590-
}
591-
592-
switch (e.KeyCode)
593-
{
594-
case Keys.F2 when sender.GetType().FullName != "System.Windows.Forms.ListBox":
595-
case Keys.Enter when sender.GetType().FullName != "System.Windows.Forms.ListBox":
596-
return;
597-
case Keys.Enter:
598-
UpdateConfigFile();
599-
StartThcrap();
600-
break;
601-
case Keys.F2:
602-
var lb = (ListBox) sender;
603-
if (!lb.SelectedItem.ToString().Contains("★"))
604-
{
605-
if (lb.Equals(listBox1))
606-
Favourites1.Patches.Add(lb.Items[lb.SelectedIndex].ToString());
607-
608-
if (lb.Equals(listBox2))
609-
Favourites1.Games.Add(lb.Items[lb.SelectedIndex].ToString());
610-
lb.Items[lb.SelectedIndex] += " ★";
611-
}
612-
else
613-
{
614-
lb.Items[lb.SelectedIndex] = lb.Items[lb.SelectedIndex].ToString().Replace(" ★", "");
615-
}
616-
617-
UpdateConfigFile();
618-
break;
619-
}
620-
621-
}
637+
#endregion
622638

623-
private static void RestartProgram()
624-
{
625-
Process.Start(Assembly.GetEntryAssembly().Location);
626-
Application.Exit();
627-
}
628-
629-
private static void ShowKeyboardShortcuts()
630-
{
631-
MessageBox.Show(Resources.KeyboardShortcuts,
632-
@"Keyboard Shortcuts", MessageBoxButtons.OK, MessageBoxIcon.Information);
633-
}
634-
635639
#region Tool Strip functions
640+
636641
private void keyboardShortcutsToolStripMenuItem_Click(object sender, EventArgs e) => ShowKeyboardShortcuts();
637-
642+
638643
private void restartToolStripMenuItem_Click(object sender, EventArgs e) => RestartProgram();
639644

640645
private void exitToolStripMenuItem_Click(object sender, EventArgs e) => Application.Exit();
@@ -689,12 +694,10 @@ private void createShortcutToolStripMenuItem_Click(object sender, EventArgs e)
689694
shortcut.Save();
690695
}
691696

692-
private void openSelectedPatchConfigurationToolStripMenuItem_Click(object sender, EventArgs e) => Process.Start(Directory.GetCurrentDirectory() + @"/" + listBox1.SelectedItem.ToString().Replace(" ★", ""));
697+
private void openSelectedPatchConfigurationToolStripMenuItem_Click(object sender, EventArgs e) =>
698+
Process.Start(Directory.GetCurrentDirectory() + @"/" + listBox1.SelectedItem.ToString().Replace(" ★", ""));
693699

694700
#endregion
695-
696-
private void Form1_FormClosing(object sender, FormClosingEventArgs e) => UpdateConfigFile();
697-
698701
}
699702

700703
public class Configuration

0 commit comments

Comments
 (0)