-
Notifications
You must be signed in to change notification settings - Fork 25
/
ConfigurationManager.cs
367 lines (290 loc) · 12.5 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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
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
{
static class ConfigurationManager
{
public static ObservableCollection<CompileProcess> CompileProcesses = new ObservableCollection<CompileProcess>();
public static ObservableCollection<string> KnownPresets = new ObservableCollection<string>();
public static string CurrentPreset = "Fast";
private static readonly string ParametersFolder = CompilePalPath.Directory + "Parameters";
private static readonly string PresetsFolder = CompilePalPath.Directory + "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 preset = Path.GetFileName(presetPath);
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.Warning = item.Warning;
}
process.PresetDictionary[preset].Add(equivalentItem);
}
}
}
}
CompilePalLogger.LogLine("Added preset {0} for processes {1}", preset, string.Join(", ", CompileProcesses));
CurrentPreset = preset;
KnownPresets.Add(preset);
}
}
public static void SavePresets()
{
foreach (var knownPreset in KnownPresets)
{
string presetFolder = Path.Combine(PresetsFolder, 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);
}
}
}
}
public static void SaveProcesses()
{
foreach (var process in CompileProcesses)
{
string jsonMetadata = Path.Combine(CompilePalPath.Directory + "Parameters", process.Metadata.Name, "meta.json");
File.WriteAllText(jsonMetadata, JsonConvert.SerializeObject(process.Metadata, Formatting.Indented));
}
}
public static void NewPreset(string name)
{
string[] defaultProcesses = new string[] { "VBSP", "VVIS", "VRAD", "COPY", "GAME" };
string folder = Path.Combine(PresetsFolder, name);
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();
}
}
}
AssembleParameters();
}
public static void ClonePreset(string name)
{
string newFolder = Path.Combine(PresetsFolder, name);
string oldFolder = Path.Combine(PresetsFolder, CurrentPreset);
if (!Directory.Exists(newFolder))
{
SavePresets();
DirectoryCopy(oldFolder, newFolder, true);
AssembleParameters();
}
}
public static void RemovePreset(string name)
{
string folder = Path.Combine(PresetsFolder, name);
if (Directory.Exists(folder))
{
Directory.Delete(folder, true);
}
AssembleParameters();
}
public static void RemoveProcess(string name)
{
string presetPath = Path.Combine(PresetsFolder, CurrentPreset, name.ToLower() + ".csv");
if (File.Exists(presetPath))
{
File.Delete(presetPath);
}
}
public static ObservableCollection<ConfigItem> GetParameters(string processName)
{
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);
}
}
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())
{
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.Warning = pieces[4];
}
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.Warning}";
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);
}
}
}
}
}