-
Notifications
You must be signed in to change notification settings - Fork 25
/
ConfigurationManager.cs
490 lines (393 loc) · 17.2 KB
/
ConfigurationManager.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Documents;
using CompilePalX.Compilers;
using CompilePalX.Compilers.BSPPack;
using CompilePalX.Compilers.UtilityProcess;
using CompilePalX.Compiling;
using Newtonsoft.Json;
namespace CompilePalX
{
public class Preset : IEquatable<Preset>, ICloneable
{
public string Name { get; set; }
public string? Map { get; set; }
public string? MapRegex { get; set; }
public bool Equals(Preset? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Name == other.Name && MapRegex == other.MapRegex && Map == other.Map;
}
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Preset)obj);
}
public override int GetHashCode()
{
return HashCode.Combine(Name, MapRegex, Map);
}
public object Clone()
{
return this.MemberwiseClone();
}
/// <summary>
/// Returns whether a map can use the preset
/// </summary>
/// <param name="mapName"></param>
/// <returns></returns>
public bool IsValidMap(string mapName)
{
// presets with no map are global
return this.MapRegex == null || Regex.IsMatch(mapName, MapRegex);
}
}
static class ConfigurationManager
{
public static ObservableCollection<CompileProcess> CompileProcesses = new ObservableCollection<CompileProcess>();
public static ObservableCollection<Preset> KnownPresets = new();
public static Preset? CurrentPreset = null;
private static readonly string ParametersFolder = "./Parameters";
private static readonly string PresetsFolder = "./Presets";
public static void AssembleParameters()
{
CompileProcesses.Clear();
CompileProcesses.Add(new BSPPack());
CompileProcesses.Add(new CubemapProcess());
CompileProcesses.Add(new NavProcess());
CompileProcesses.Add(new ShutdownProcess());
CompileProcesses.Add(new UtilityProcess());
CompileProcesses.Add(new CustomProcess());
//collect new metadatas
var metadatas = Directory.GetDirectories(ParametersFolder);
foreach (var metadata in metadatas)
{
string folderName = Path.GetFileName(metadata);
if (CompileProcesses.Any(c => String.Equals(c.Metadata.Name, folderName, StringComparison.CurrentCultureIgnoreCase)))
continue;
var compileProcess = new CompileExecutable(folderName);
CompileProcesses.Add(compileProcess);
}
//collect legacy metadatas
var csvMetaDatas = Directory.GetFiles(ParametersFolder + "\\", "*.meta");
foreach (var metadata in csvMetaDatas)
{
string name = Path.GetFileName(metadata).Replace(".meta", "");
if (CompileProcesses.Any(c => String.Equals(c.Metadata.Name, name, StringComparison.CurrentCultureIgnoreCase)))
continue;
var compileProcess = new CompileExecutable(name);
CompileProcesses.Add(compileProcess);
}
CompileProcesses = new ObservableCollection<CompileProcess>(CompileProcesses.OrderBy(c => c.Metadata.Order));
AssemblePresets();
}
private static void AssemblePresets()
{
if (!Directory.Exists(PresetsFolder))
Directory.CreateDirectory(PresetsFolder);
//get a list of presets from the directories in the preset folder
var presets = Directory.GetDirectories(PresetsFolder);
//clear old lists
KnownPresets.Clear();
foreach (var process in CompileProcesses)
{
process.PresetDictionary.Clear();
}
foreach (string presetPath in presets)
{
string presetName = Path.GetFileName(presetPath);
// try reading preset metadata
string metadataFile = Path.Combine(presetPath, "meta.json");
Preset preset;
if (File.Exists(metadataFile))
preset = JsonConvert.DeserializeObject<Preset>(File.ReadAllText(metadataFile)) ?? new Preset() { Name = presetName };
else
// legacy presets don't have metadata, use folder name as preset name
preset = new Preset() { Name = presetName };
foreach (var process in CompileProcesses)
{
string file = Path.Combine(presetPath, process.PresetFile);
if (File.Exists(file))
{
process.PresetDictionary.Add(preset, new ObservableCollection<ConfigItem>());
//read the list of preset parameters
var lines = File.ReadAllLines(file);
foreach (var line in lines)
{
var item = ParsePresetLine(line);
if (process.ParameterList.Any(c => c.Parameter == item.Parameter))
{
//remove .clone if you are a masochist and wish to enter the object oriented version of hell
var equivalentItem = (ConfigItem)process.ParameterList.FirstOrDefault(c => c.Parameter == item.Parameter).Clone();
equivalentItem.Value = item.Value;
//Copy extra information stored for custom programs
if (item.Parameter == "program")
{
equivalentItem.Value2 = item.Value2;
equivalentItem.WaitForExit= item.WaitForExit;
equivalentItem.Warning = item.Warning;
}
process.PresetDictionary[preset].Add(equivalentItem);
}
}
}
}
CompilePalLogger.LogLine($"Added preset {preset.Name} {(preset.Map != null ? $"for map {preset.Map} " : "")}for processes {string.Join(", ", CompileProcesses)}");
CurrentPreset = preset;
KnownPresets.Add(preset);
}
}
public static void SavePresets()
{
foreach (var knownPreset in KnownPresets)
{
string presetFolder = GetPresetFolder(knownPreset);
foreach (var compileProcess in CompileProcesses)
{
if (compileProcess.PresetDictionary.ContainsKey(knownPreset))
{
var lines = new List<string>();
foreach (var item in compileProcess.PresetDictionary[knownPreset])
{
string line = WritePresetLine(item);
lines.Add(line);
}
string presetPath = Path.Combine(presetFolder, compileProcess.PresetFile);
File.WriteAllLines(presetPath, lines);
}
}
// save preset metadata
string metadataPath = Path.Combine(presetFolder, "meta.json");
string jsonSaveText = JsonConvert.SerializeObject(knownPreset, Formatting.Indented);
File.WriteAllText(metadataPath, jsonSaveText);
}
}
public static void SaveProcesses()
{
foreach (var process in CompileProcesses)
{
string jsonMetadata = Path.Combine("./Parameters", process.Metadata.Name, "meta.json");
File.WriteAllText(jsonMetadata, JsonConvert.SerializeObject(process.Metadata, Formatting.Indented));
}
}
public static Preset NewPreset(Preset preset)
{
string[] defaultProcesses = new string[] { "VBSP", "VVIS", "VRAD", "COPY", "GAME" };
// if map specific, append map to name so you can make map specific presets with the same name as global ones
string folder = GetPresetFolder(preset);
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
foreach (var process in CompileProcesses)
{
if (defaultProcesses.Contains(process.Metadata.Name))
{
string path = Path.ChangeExtension(Path.Combine(folder, process.Metadata.Name), "csv");
File.Create(path).Close();
}
}
// create metadata
string metadataPath = Path.Combine(folder, "meta.json");
string jsonSaveText = JsonConvert.SerializeObject(preset, Formatting.Indented);
File.WriteAllText(metadataPath, jsonSaveText);
}
AssembleParameters();
return preset;
}
public static Preset? ClonePreset(Preset preset)
{
if (CurrentPreset == null)
return null;
// if map specific, append map to name so you can make map specific presets with the same name as global ones
string newFolder = GetPresetFolder(preset);
// if cloned preset is map specific, append map to name
string oldFolder = GetPresetFolder(CurrentPreset);
if (!Directory.Exists(newFolder))
{
SavePresets();
DirectoryCopy(oldFolder, newFolder, true);
// overwrite metadata
string metadataPath = Path.Combine(newFolder, "meta.json");
string jsonSaveText = JsonConvert.SerializeObject(preset, Formatting.Indented);
File.WriteAllText(metadataPath, jsonSaveText);
AssembleParameters();
}
return preset;
}
public static Preset? EditPreset(Preset preset)
{
if (CurrentPreset == null)
{
return null;
}
// "Edit" preset by deleting the current preset and adding a new preset, then make it the currently selected preset
RemovePreset(CurrentPreset);
var newPreset = NewPreset(preset);
CurrentPreset = newPreset;
return newPreset;
}
private static string GetPresetFolder(Preset preset)
{
return preset.Map != null ? Path.Combine(PresetsFolder, $"{preset.Name}_{preset.Map}") : Path.Combine(PresetsFolder, preset.Name);
}
public static void RemovePreset(Preset preset)
{
string folder = GetPresetFolder(preset);
if (Directory.Exists(folder))
{
Directory.Delete(folder, true);
}
AssembleParameters();
}
public static void RemoveProcess(string name)
{
if (CurrentPreset == null)
return;
string presetPath = Path.Combine(GetPresetFolder(CurrentPreset), name.ToLower() + ".csv");
if (File.Exists(presetPath))
{
File.Delete(presetPath);
}
}
public static ObservableCollection<ConfigItem> GetParameters(string processName, bool doRun = false)
{
var list = new ObservableCollection<ConfigItem>();
string jsonParameters = Path.Combine(ParametersFolder, processName, "parameters.json");
if (File.Exists(jsonParameters))
{
ConfigItem[] items = JsonConvert.DeserializeObject<ConfigItem[]>(File.ReadAllText(jsonParameters));
foreach (var configItem in items)
{
list.Add(configItem);
}
// add custom parameter to all runnable steps
if (doRun)
{
list.Add(new ConfigItem()
{
Name = "Command Line Argument",
CanHaveValue = true,
CanBeUsedMoreThanOnce = true,
Description = "Passes value as a command line argument",
});
}
}
else
{
string csvParameters = Path.Combine(ParametersFolder, processName + ".csv");
if (File.Exists(csvParameters))
{
var baselines = File.ReadAllLines(csvParameters);
for (int i = 2; i < baselines.Length; i++)
{
string baseline = baselines[i];
var item = ParseBaseLine(baseline);
list.Add(item);
}
ConfigItem[] items = list.ToArray();
File.WriteAllText(jsonParameters, JsonConvert.SerializeObject(items, Formatting.Indented));
}
else
{
throw new FileNotFoundException("Parameter files could not be found for " + processName);
}
}
return list;
}
private static ConfigItem ParsePresetLine(string line)
{
var item = new ConfigItem();
var pieces = line.Split(',');
if (pieces.Any())
{
// Custom parameter stores name as first value instead of parameter, because it has no parameter
if (pieces[0] == "Command Line Argument")
item.Name = pieces[0];
else
item.Parameter = pieces[0];
if (pieces.Count() >= 2)
item.Value = pieces[1];
//Handle extra information stored for custom programs
if (pieces.Count() >= 3)
item.Value2 = pieces[2];
if (pieces.Length >= 4)
item.ReadOutput = Convert.ToBoolean(pieces[3]);
if (pieces.Length >= 5)
item.WaitForExit= Convert.ToBoolean(pieces[4]);
if (pieces.Length >= 6)
item.Warning = pieces[5];
}
return item;
}
private static string WritePresetLine(ConfigItem item)
{
//Handle extra information stored for custom programs
if (item.Name == "Run Program")
return $"{item.Parameter},{item.Value},{item.Value2},{item.ReadOutput},{item.WaitForExit},{item.Warning}";
else if (item.Name == "Command Line Argument") // Command line arguments have no parameter value
return $"{item.Name},{item.Value}";
return $"{item.Parameter},{item.Value}";
}
private static ConfigItem ParseBaseLine(string line)
{
var item = new ConfigItem();
var pieces = line.Split(';');
if (pieces.Any())
{
item.Name = pieces[0];
if (pieces.Count() >= 2)
item.Parameter = pieces[1];
if (pieces.Count() >= 3)
item.CanHaveValue = bool.Parse(pieces[2]);
if (pieces.Count() >= 4)
item.Description = pieces[3];
if (pieces.Count() >= 5)
item.Warning = pieces[4];
}
return item;
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
}
}