Skip to content

Commit edea88d

Browse files
committed
chore: get rid of horrible goto
1 parent 7ee821f commit edea88d

3 files changed

Lines changed: 143 additions & 104 deletions

File tree

Games/Base.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
public class Base
44
{
5+
public virtual bool RUNNING { get; set; } = false;
56
public virtual string WELCOME_MESSAGE { get; } = "COD Files Cleaner";
67

78
// Games

Games/IW7.cs

Lines changed: 99 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -7,132 +7,128 @@ public class IW7 : Base
77

88
public override async Task Process()
99
{
10-
Main:
11-
Console.WriteLine(WELCOME_MESSAGE);
12-
Console.WriteLine("[1] Clean Game Files");
13-
Console.WriteLine("[2] Revert Cleaning");
14-
var choice = Console.ReadLine();
15-
16-
if (choice == null)
10+
RUNNING = true;
11+
while (RUNNING)
1712
{
18-
Console.WriteLine("Not a Valid Choice!");
19-
goto Main;
20-
}
13+
Helpers.WriteColor(WELCOME_MESSAGE);
14+
Helpers.WriteColor("[1] Clean Game Files");
15+
Helpers.WriteColor("[2] Revert Cleaning");
16+
Helpers.WriteColor("[Q] Quit", ConsoleColor.DarkGray);
2117

22-
if (choice == "1")
23-
{
24-
string[] fastFiles = Directory.GetFiles("./", FASTFILE_EXTENSION);
25-
string[] bikFiles = Directory.GetFiles("./", BIK_EXTENSION);
26-
string[] pakFiles = Directory.GetFiles("./", PAK_EXTENSION);
27-
string[] sablFiles = Directory.GetFiles("./", SABL_EXTENSION);
28-
string[] sabsFiles = Directory.GetFiles("./", SABS_EXTENSION);
18+
var choice = Console.ReadLine()?.ToUpperInvariant();
2919

30-
if (!File.Exists(COMMON_MP_FASTFILE) || !File.Exists(COMMON_FASTFILE))
20+
switch (choice)
3121
{
32-
Console.WriteLine("Your game is already clean!\nIf not, make sure you have put the file in the correct game folder.\n");
33-
goto Main;
22+
case "1":
23+
await CleanGameFiles();
24+
break;
25+
case "2":
26+
await RevertCleaning();
27+
break;
28+
case "Q":
29+
RUNNING = false;
30+
break;
31+
default:
32+
Helpers.WriteColor("Not a Valid Choice!\n", ConsoleColor.Red);
33+
break;
3434
}
35+
}
36+
}
3537

36-
if (!Directory.Exists(ZONE_FOLDER))
37-
{
38-
Console.WriteLine("Creating Zone Folder");
39-
Directory.CreateDirectory(ZONE_FOLDER);
40-
}
38+
private async Task CleanGameFiles()
39+
{
40+
if (!File.Exists(COMMON_MP_FASTFILE) || !File.Exists(COMMON_FASTFILE))
41+
{
42+
Helpers.WriteColor("Your game is already clean!\nIf not, make sure you have put the file in the correct game folder.\n", ConsoleColor.Yellow);
43+
return;
44+
}
4145

42-
if (!Directory.Exists(RAW_FOLDER))
43-
{
44-
Console.WriteLine("Creating Raw Folder");
45-
Directory.CreateDirectory(RAW_FOLDER);
46-
}
46+
Helpers.EnsureDirectoriesExist([ZONE_FOLDER,
47+
RAW_FOLDER,
48+
RAW_VIDEO_FOLDER,
49+
CLEAN_ZONE_FOLDER,
50+
CLEAN_RAW_VIDEO_FOLDER]);
4751

48-
if (!Directory.Exists(RAW_VIDEO_FOLDER))
52+
var fileTypeMap = new Dictionary<string, string>
53+
{
54+
{ FASTFILE_EXTENSION, CLEAN_ZONE_FOLDER },
55+
{ PAK_EXTENSION, CLEAN_ZONE_FOLDER },
56+
{ SABL_EXTENSION, CLEAN_ZONE_FOLDER },
57+
{ SABS_EXTENSION, CLEAN_ZONE_FOLDER },
58+
{ BIK_EXTENSION, CLEAN_RAW_VIDEO_FOLDER }
59+
};
60+
61+
foreach (var entry in fileTypeMap)
62+
{
63+
string[] files = Directory.GetFiles("./", entry.Key);
64+
if (files.Length > 0)
4965
{
50-
Console.WriteLine("Creating Raw Video Folder");
51-
Directory.CreateDirectory(RAW_VIDEO_FOLDER);
66+
Helpers.MoveFileTo(files, entry.Value);
5267
}
68+
}
5369

54-
Helpers.MoveFileTo(fastFiles, CLEAN_ZONE_FOLDER);
55-
Helpers.MoveFileTo(pakFiles, CLEAN_ZONE_FOLDER);
56-
Helpers.MoveFileTo(sablFiles, CLEAN_ZONE_FOLDER);
57-
Helpers.MoveFileTo(sabsFiles, CLEAN_ZONE_FOLDER);
58-
Helpers.MoveFileTo(bikFiles, CLEAN_RAW_VIDEO_FOLDER);
70+
MoveLanguageFolders(isReverting: false);
71+
Helpers.WriteColor("Finished Processing Files\n", ConsoleColor.Green);
72+
}
5973

74+
private void MoveLanguageFolders(bool isReverting)
75+
{
76+
var folderMap = new Dictionary<string, string>
77+
{
78+
{ ENGLISH_FOLDER, ENGLISH_ZONE_FOLDER },
79+
{ ENGLISH_SAFE_FOLDER, ENGLISH_SAFE_ZONE_FOLDER },
80+
{ FRENCH_FOLDER, FRENCH_ZONE_FOLDER },
81+
{ GERMAN_FOLDER, GERMAN_ZONE_FOLDER },
82+
{ RUSSIAN_FOLDER, RUSSIAN_ZONE_FOLDER },
83+
{ POLISH_FOLDER, POLISH_ZONE_FOLDER },
84+
{ JAPAN_FOLDER, JAPAN_ZONE_FOLDER },
85+
{ KOREAN_FOLDER, KOREAN_ZONE_FOLDER },
86+
{ PORTUGUESE_FOLDER, PORTUGUESE_ZONE_FOLDER },
87+
{ SPANISH_FOLDER, SPANISH_ZONE_FOLDER },
88+
{ ITALIAN_FOLDER, ITALIAN_ZONE_FOLDER },
89+
{ SCHINA_FOLDER, SCHINA_ZONE_FOLDER },
90+
{ TCHINA_FOLDER, TCHINA_ZONE_FOLDER }
91+
};
92+
93+
foreach (var entry in folderMap)
94+
{
6095
try
6196
{
62-
Helpers.MoveDirectoryTo(ENGLISH_FOLDER, ENGLISH_ZONE_FOLDER);
63-
Helpers.MoveDirectoryTo(ENGLISH_SAFE_FOLDER, ENGLISH_SAFE_ZONE_FOLDER);
64-
Helpers.MoveDirectoryTo(FRENCH_FOLDER, FRENCH_ZONE_FOLDER);
65-
Helpers.MoveDirectoryTo(GERMAN_FOLDER, GERMAN_ZONE_FOLDER);
66-
Helpers.MoveDirectoryTo(RUSSIAN_FOLDER, RUSSIAN_ZONE_FOLDER);
67-
Helpers.MoveDirectoryTo(POLISH_FOLDER, POLISH_ZONE_FOLDER);
68-
Helpers.MoveDirectoryTo(JAPAN_FOLDER, JAPAN_ZONE_FOLDER);
69-
Helpers.MoveDirectoryTo(KOREAN_FOLDER, KOREAN_ZONE_FOLDER);
70-
Helpers.MoveDirectoryTo(PORTUGUESE_FOLDER, PORTUGUESE_ZONE_FOLDER);
71-
Helpers.MoveDirectoryTo(SPANISH_FOLDER, SPANISH_ZONE_FOLDER);
72-
Helpers.MoveDirectoryTo(ITALIAN_FOLDER, ITALIAN_ZONE_FOLDER);
73-
Helpers.MoveDirectoryTo(SCHINA_FOLDER, SCHINA_ZONE_FOLDER);
74-
Helpers.MoveDirectoryTo(TCHINA_FOLDER, TCHINA_ZONE_FOLDER);
97+
string source = isReverting ? entry.Value : entry.Key;
98+
string dest = isReverting ? entry.Key : entry.Value;
99+
Helpers.MoveDirectoryTo(source, dest);
75100
}
76-
catch (Exception ex)
77-
{
78-
Console.WriteLine(ex.Message);
79-
}
80-
81-
Console.WriteLine("Finished Processing Files\n");
82-
goto Main;
101+
catch (Exception ex) { Console.WriteLine(ex.Message); }
83102
}
103+
}
84104

85-
if (choice == "2")
86-
{
87-
string[] fastFiles = Directory.GetFiles("./", CLEAN_FASTFILE_EXTENSION);
88-
string[] bikFiles = Directory.GetFiles("./", CLEAN_BIK_EXTENSION);
89-
string[] pakFiles = Directory.GetFiles("./", CLEAN_PAK_EXTENSION);
90-
string[] sablFiles = Directory.GetFiles("./", CLEAN_SABL_EXTENSION);
91-
string[] sabsFiles = Directory.GetFiles("./", CLEAN_SABS_EXTENSION);
92-
93-
Helpers.MoveFileTo(fastFiles, "./");
94-
Helpers.MoveFileTo(pakFiles, "./");
95-
Helpers.MoveFileTo(sablFiles, "./");
96-
Helpers.MoveFileTo(sabsFiles, "./");
97-
Helpers.MoveFileTo(bikFiles, "./");
105+
private async Task RevertCleaning()
106+
{
107+
Helpers.WriteColor("Reverting Game Files...", ConsoleColor.Cyan);
98108

99-
try
100-
{
101-
Helpers.MoveDirectoryTo(ENGLISH_ZONE_FOLDER, ENGLISH_FOLDER);
102-
Helpers.MoveDirectoryTo(ENGLISH_SAFE_ZONE_FOLDER, ENGLISH_SAFE_FOLDER);
103-
Helpers.MoveDirectoryTo(FRENCH_ZONE_FOLDER, FRENCH_FOLDER);
104-
Helpers.MoveDirectoryTo(GERMAN_ZONE_FOLDER, GERMAN_FOLDER);
105-
Helpers.MoveDirectoryTo(RUSSIAN_ZONE_FOLDER, RUSSIAN_FOLDER);
106-
Helpers.MoveDirectoryTo(POLISH_ZONE_FOLDER, POLISH_FOLDER);
107-
Helpers.MoveDirectoryTo(JAPAN_ZONE_FOLDER, JAPAN_FOLDER);
108-
Helpers.MoveDirectoryTo(KOREAN_ZONE_FOLDER, KOREAN_FOLDER);
109-
Helpers.MoveDirectoryTo(PORTUGUESE_ZONE_FOLDER, PORTUGUESE_FOLDER);
110-
Helpers.MoveDirectoryTo(SPANISH_ZONE_FOLDER, SPANISH_FOLDER);
111-
Helpers.MoveDirectoryTo(ITALIAN_ZONE_FOLDER, ITALIAN_FOLDER);
112-
Helpers.MoveDirectoryTo(SCHINA_ZONE_FOLDER, SCHINA_FOLDER);
113-
Helpers.MoveDirectoryTo(TCHINA_ZONE_FOLDER, TCHINA_FOLDER);
114-
}
115-
catch (Exception ex)
116-
{
117-
Console.WriteLine(ex.Message);
118-
}
109+
string[] sourceFolders = { CLEAN_ZONE_FOLDER, CLEAN_RAW_VIDEO_FOLDER };
119110

120-
try
121-
{
122-
File.Delete("zone");
123-
File.Delete("raw/video");
124-
File.Delete("raw");
125-
}
126-
catch (Exception ex)
111+
foreach (var folder in sourceFolders)
112+
{
113+
if (Directory.Exists(folder))
127114
{
128-
Console.WriteLine(ex.Message);
115+
var files = Directory.GetFiles(folder, "*.*");
116+
if (files.Length > 0)
117+
{
118+
Helpers.MoveFileTo(files, "./");
119+
}
129120
}
130-
131-
Console.WriteLine("Done Reverting Files\n");
132-
goto Main;
133121
}
134122

135-
goto Main;
123+
MoveLanguageFolders(isReverting: true);
124+
125+
Helpers.TryCleanupDirectory(CLEAN_RAW_VIDEO_FOLDER);
126+
Helpers.TryCleanupDirectory(RAW_VIDEO_FOLDER);
127+
Helpers.TryCleanupDirectory(RAW_FOLDER);
128+
Helpers.TryCleanupDirectory(CLEAN_ZONE_FOLDER);
129+
Helpers.TryCleanupDirectory(ZONE_FOLDER);
130+
131+
Helpers.WriteColor("Done Reverting Files\n", ConsoleColor.Green);
136132
}
137133
}
138134
}

Helpers.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
{
33
public class Helpers
44
{
5+
public static void WriteColor(string message, ConsoleColor color = ConsoleColor.White)
6+
{
7+
Console.ForegroundColor = color;
8+
Console.WriteLine(message);
9+
Console.ResetColor();
10+
}
11+
512
public static void MoveFileTo(string[] files, string to)
613
{
714
foreach (var file in files)
815
{
916
var fileName = Path.GetFileName(file);
10-
Console.WriteLine($"Moving {fileName} to {to}");
17+
WriteColor($"Moving {fileName} to {to}", ConsoleColor.Cyan);
1118
File.Move(file, Path.Combine(to, fileName));
1219
}
1320
}
@@ -19,5 +26,40 @@ public static void MoveDirectoryTo(string directory, string to)
1926
Directory.Move(directory, to);
2027
}
2128
}
29+
30+
public static void EnsureDirectoriesExist(string[] requiredFolders)
31+
{
32+
foreach (var folder in requiredFolders)
33+
{
34+
if (!Directory.Exists(folder))
35+
{
36+
WriteColor($"Creating Folder: {folder}", ConsoleColor.Cyan);
37+
Directory.CreateDirectory(folder);
38+
}
39+
}
40+
}
41+
42+
public static void TryCleanupDirectory(string path)
43+
{
44+
try
45+
{
46+
if (Directory.Exists(path))
47+
{
48+
if (!Directory.EnumerateFileSystemEntries(path).Any())
49+
{
50+
Directory.Delete(path);
51+
WriteColor($"Removed empty directory: {path}", ConsoleColor.Cyan);
52+
}
53+
else
54+
{
55+
WriteColor($"Skipped directory (not empty): {path}", ConsoleColor.Cyan);
56+
}
57+
}
58+
}
59+
catch (Exception ex)
60+
{
61+
Console.WriteLine($"Error cleaning up {path}: {ex.Message}");
62+
}
63+
}
2264
}
2365
}

0 commit comments

Comments
 (0)