This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainScript.cs
169 lines (152 loc) · 7.7 KB
/
MainScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
namespace DOOM1_2WadLoader
{
public class MainScript
{
bool disableTitleNaming = false;
public MainScript(string[] args)
{
IEnumerable<string> wadfiles = Directory.EnumerateFiles(Directory.GetCurrentDirectory(), "*.wad");
string bnetWadsDir = "";
bnetWadsDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Saved Games", "Nightdive Studios", "DOOM", "bnetwads");
bool repairMode = false;
foreach (string arg in args)
{
if (Directory.Exists(arg))
bnetWadsDir = arg;
if (arg == "-n")
disableTitleNaming = true;
if (arg == "-r")
repairMode = true;
}
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("\nFinding bnetwads folder...");
if (!Path.Exists(bnetWadsDir))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("bnetwads folder not found");
Console.ReadLine();
return;
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("bnetwads folder found!");
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("\nStarting repair mode...");
if (repairMode)
{
//big code incoming
IEnumerable<DirectoryInfo> wadFolders = new DirectoryInfo(bnetWadsDir).EnumerateDirectories();
foreach (DirectoryInfo wadfolder in wadFolders)
{
string folderPath = Path.Combine(bnetWadsDir, wadfolder.Name);
string wadFileFoundInDir = Directory.EnumerateFiles(folderPath, "*.wad").FirstOrDefault("");
string indexFileFound = Directory.GetFiles(folderPath, "index.json").FirstOrDefault("");
if (!string.IsNullOrEmpty(wadFileFoundInDir))
{
//if wad file is empty, delete folder
if (new FileInfo(wadFileFoundInDir).Length == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Folder {wadfolder.Name} is not functional. deleting folder...");
Directory.Delete(folderPath, true);
}
else
{
string wadName = Path.GetFileNameWithoutExtension(wadFileFoundInDir);
//check folder's index.json, if missing or empty create one
if (string.IsNullOrEmpty(indexFileFound) || (new FileInfo(indexFileFound).Length == 0))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Folder {wadfolder.Name} is missing index.json");
WriteJsonFileToDir(folderPath, wadName, wadName + ".wad");
}
//handle folders with unequal namings to their wads
if (!string.Equals(wadName, wadfolder.Name))
{
Console.ForegroundColor = ConsoleColor.Red;
string newFolderPath = Path.Combine(bnetWadsDir, wadName);
Directory.CreateDirectory(newFolderPath);
foreach (string fileInFolder in Directory.EnumerateFiles(folderPath))
{
string newFilePath = Path.Combine(newFolderPath, Path.GetFileName(fileInFolder));
if (!File.Exists(newFilePath))
File.Copy(fileInFolder, newFilePath, false);
}
Directory.Delete(folderPath, true);
Console.WriteLine($"Renamed Folder {wadfolder.Name} to {wadName}");
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Folder {wadfolder.Name} is functional.");
}
}
}
else
{
//if a folder has an index.json but no wad file, delete it
if (!string.IsNullOrEmpty(indexFileFound))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Folder {wadfolder.Name} is not functional. deleting folder...");
Directory.Delete(folderPath, true);
}
}
}
Console.ResetColor();
return;
}
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("\nGetting wads in current directory...");
if (wadfiles.Count() == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("No Wad files found in current directory");
Console.ReadLine();
return;
}
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write($"{wadfiles.Count()}");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($" wad files found");
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("\nStarting wad creation process..");
foreach (string item in wadfiles)
{
string wadFileName = Path.GetFileName(item);
string wadName = Path.GetFileNameWithoutExtension(item);
string wadTitle = wadName;
string wadFolder = Path.Combine(bnetWadsDir, wadName);
Directory.CreateDirectory(wadFolder);
WriteJsonFileToDir(wadFolder, wadName, wadFileName);
File.Copy(wadFileName, Path.Combine(wadFolder, wadFileName), true);
File.Delete(wadFileName);
}
Console.ResetColor();
}
private void WriteJsonFileToDir(string dirPath, string wadName, string wadFileName)
{
string wadTitle = wadName;
if (!disableTitleNaming)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write($"\nSpecify Title for");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write($" {wadFileName} ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"(leave blank to use filename instead)");
Console.ForegroundColor = ConsoleColor.White;
string? typedTitle = Console.ReadLine();
wadTitle = string.IsNullOrEmpty(typedTitle) ? wadName : typedTitle;
}
using (StreamWriter outputFile = new StreamWriter(Path.Combine(dirPath, "index.json"), false))
{
outputFile.WriteLine("{");
outputFile.WriteLine($"\t\"id\" : \"{wadName.ToUpper()}\",");
outputFile.WriteLine($"\t\"title\" : \"{wadTitle}\",");
outputFile.WriteLine($"\t\"version\" : \"1\",");
outputFile.WriteLine($"\t\"wad\" : \"{wadFileName}\"");
outputFile.WriteLine("}");
}
}
}
}