forked from Marlamin/wow.tools.local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsManager.cs
306 lines (282 loc) · 11.3 KB
/
SettingsManager.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
namespace wow.tools.local
{
public static class SettingsManager
{
public static string definitionDir;
public static string listfileURL;
public static string tactKeyURL;
public static string? wowFolder;
public static string dbcFolder;
public static string manifestFolder;
public static string wowProduct;
public static string region;
public static CASCLib.LocaleFlags locale;
public static bool showAllFiles = false;
public static string extractionDir;
public static bool preferHighResTextures = false;
// supported command line flags and switches
// flag syntax: -flag value || -flag=value or --switch
private const string _wowFolderFlag = "-wowFolder";
private const string _wowProductFlag = "-product";
private const string _dbdFolderFlag = "-dbdFolder";
private const string _dbcFolderFlag = "-dbcFolder";
private const string _manifestFolderFlag = "-manifestFolder";
private const string _listfileURLFlag = "-listfileURL";
private const string _tactKeyURLFlag = "-tactKeyURL";
private const string _regionFlag = "-region";
private const string _localeFlag = "-locale";
private const string _showAllFilesFlag = "-showAllFiles";
private const string _extractionDirFlag = "-extractionDir";
private const string _preferHighResTexturesFlag = "-preferHighResTextures";
// calling the double-hyphen args 'switch' instead of 'flag' because they don't have values
private const string _debugSwitch = "--debug";
// to add a new flag, add a new const string above
// then add a new case to the switch statement in either HandleFlag or HandleSwitch with the functionality you want
// TODO:
// make it easier to associate flags with values and add new flags
// flags are currently case-sensitive, probably not best practice. easy to fix, i'm just being lazy
static SettingsManager()
{
try
{
LoadSettings();
}
catch(Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("An error happened during config.json reading, make sure it is valid. Application will be unable to load correctly.");
Console.WriteLine("Error message: " + e.Message);
Console.ResetColor();
}
}
public static void LoadSettings()
{
IConfigurationRoot config;
var cwd = Directory.GetCurrentDirectory();
var appDir = AppDomain.CurrentDomain.BaseDirectory;
try
{
config = new ConfigurationBuilder().SetBasePath(cwd).AddJsonFile("config.json", optional: false, reloadOnChange: false).Build();
}
catch(Exception)
{ // if we can't find config.json in the cwd, try the app's directory instead. This is to support launching from the command line, which might not be started in the app's directory
// this could cause soft errors in case of misconfiguration, but too bad
config = new ConfigurationBuilder().SetBasePath(appDir).AddJsonFile("config.json", optional: false, reloadOnChange: false).Build();
Environment.CurrentDirectory = appDir;
}
definitionDir = config.GetSection("config")["definitionDir"];
listfileURL = config.GetSection("config")["listfileURL"];
tactKeyURL = config.GetSection("config")["tactKeyURL"];
if (config.GetSection("config")["region"] != null)
{
region = config.GetSection("config")["region"];
}
else
{
region = "eu";
}
if (config.GetSection("config")["locale"] != null)
{
SetLocale(config.GetSection("config")["locale"]);
}
else
{
locale = CASCLib.LocaleFlags.enUS;
}
if (config.GetSection("config")["manifestFolder"] != null)
{
manifestFolder = config.GetSection("config")["manifestFolder"];
}
else
{
manifestFolder = "manifests";
}
dbcFolder = config.GetSection("config")["dbcFolder"];
wowFolder = config.GetSection("config")["wowFolder"];
wowProduct = config.GetSection("config")["wowProduct"];
showAllFiles = bool.Parse(config.GetSection("config")["showAllFiles"]);
extractionDir = config.GetSection("config")["extractionDir"];
preferHighResTextures = bool.Parse(config.GetSection("config")["preferHighResTextures"]);
}
private static void SetLocale(string locValue)
{
if (locValue == null)
{
locale = CASCLib.LocaleFlags.enUS;
return;
}
switch (locValue)
{
case "deDE":
locale = CASCLib.LocaleFlags.deDE;
break;
case "enUS":
locale = CASCLib.LocaleFlags.enUS;
break;
case "enGB":
locale = CASCLib.LocaleFlags.enGB;
break;
case "ruRU":
locale = CASCLib.LocaleFlags.ruRU;
break;
case "zhCN":
locale = CASCLib.LocaleFlags.zhCN;
break;
case "zhTW":
locale = CASCLib.LocaleFlags.zhTW;
break;
case "enTW":
locale = CASCLib.LocaleFlags.enTW;
break;
case "esES":
locale = CASCLib.LocaleFlags.esES;
break;
case "esMX":
locale = CASCLib.LocaleFlags.esMX;
break;
case "frFR":
locale = CASCLib.LocaleFlags.frFR;
break;
case "itIT":
locale = CASCLib.LocaleFlags.itIT;
break;
case "koKR":
locale = CASCLib.LocaleFlags.koKR;
break;
case "ptBR":
locale = CASCLib.LocaleFlags.ptBR;
break;
case "ptPT":
locale = CASCLib.LocaleFlags.ptPT;
break;
default:
Console.WriteLine("Locale " + locValue + " not found, defaulting to enUS");
locale = CASCLib.LocaleFlags.enUS;
break;
}
}
private static void HandleFlag(string flag, string? value)
{
if (!string.IsNullOrEmpty(value))
{
switch(flag)
{
case (_wowFolderFlag):
wowFolder = value;
break;
case (_wowProductFlag):
wowProduct = value;
break;
case (_dbdFolderFlag):
definitionDir = value;
break;
case (_dbcFolderFlag):
dbcFolder = value;
break;
case (_manifestFolderFlag):
manifestFolder = value;
break;
case (_listfileURLFlag):
listfileURL = value;
break;
case (_tactKeyURLFlag):
tactKeyURL = value;
break;
case (_regionFlag):
region = value;
break;
case (_localeFlag):
SetLocale(value);
break;
case (_showAllFilesFlag):
showAllFiles = bool.Parse(value);
break;
case (_extractionDirFlag):
extractionDir = value;
break;
case (_preferHighResTexturesFlag):
preferHighResTextures = bool.Parse(value);
break;
}
}
}
private static void HandleSwitch(string flag)
{
if (!string.IsNullOrEmpty(flag))
{
switch (flag)
{
case (_debugSwitch):
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
break;
}
}
}
private static void ProcessFlags(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
string _arg = args[i];
string _flag = _arg;
string? _value = null;
int _nextIndex = i + 1;
// handle switches first, cause they're special
if (_arg.StartsWith("--"))
{
HandleSwitch(_arg);
continue;
}
// check if the value is specified with an equals sign, then break it up and set _value
// TODO: currently eats any args containing an equal sign, which could cause us to eat flags that aren't meant for us. too bad
if (_arg.Contains('='))
{
string[] _argSplit = _arg.Split('=');
_flag = _argSplit[0];
_value = _argSplit[1];
HandleFlag(_flag, _value);
args[i] = "";
continue;
}
// check that the next index in args is valid
if (_nextIndex < args.Length)
{
// if flag was NOT specified with an equals sign, set it to the next index
if (_value == null)
{
_value = args[_nextIndex];
i++; // then skip the next item
}
HandleFlag(_flag, _value);
continue;
}
}
}
public static void ParseCommandLineArguments(string[] args)
{
ProcessFlags(args);
// since this runs after LoadSettings() AND the command line arguments are parsed, I'm comfortable throwing this folder check here.
ValidateWowFolder();
}
private static void ValidateWowFolder()
{
if (string.IsNullOrEmpty(wowFolder))
{
wowFolder = null;
}
else
{
if (!Directory.Exists(wowFolder))
{
throw new DirectoryNotFoundException("Could not find folder " + wowFolder);
}
else
{
if (!File.Exists(Path.Combine(wowFolder, ".build.info")))
{
throw new FileNotFoundException("Unable to find .build.info in WoW directory. Make sure you selected the root WoW directory and not a subfolder.");
}
}
}
}
}
}