-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathConfig.cs
158 lines (137 loc) · 5.57 KB
/
Config.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace SMWPatcher
{
public class Config
{
public string WorkingDirectory;
public string OutputPath;
public string TempPath;
public string CleanPath;
public string PackagePath;
public string AsarPath;
public string UberASMPath;
public string GPSPath;
public string PixiPath;
public string AddMusicKPath;
public string LunarMagicPath;
public string FlipsPath;
public string InitialPatch;
public string LevelsPath;
public string Map16Path;
public string SharedPalettePath;
public string GlobalDataPath;
public string TitleMovesPath;
public List<string> Patches = new List<string>();
public string TestLevel;
public string TestLevelDest;
public string RetroArchPath;
public string RetroArchCore;
public string Snes9xPath;
public string MesenSPath;
#region load
static public Config Load(out string error)
{
error = "";
try
{
var data = new List<string>();
foreach (var file in Directory.EnumerateFiles(Directory.GetCurrentDirectory(), "config*.txt", SearchOption.TopDirectoryOnly))
data.Add(File.ReadAllText(file));
return Load(data);
}
catch (Exception e)
{
error = e.Message;
return null;
}
}
static private Config Load(List<string> data)
{
Config config = new Config();
HashSet<string> flags = new HashSet<string>();
Dictionary<string, string> vars = new Dictionary<string, string>();
Dictionary<string, List<string>> lists = new Dictionary<string, List<string>>();
foreach (var d in data)
Parse(d, flags, vars, lists);
vars.TryGetValue("dir", out config.WorkingDirectory);
vars.TryGetValue("output", out config.OutputPath);
vars.TryGetValue("temp", out config.TempPath);
vars.TryGetValue("clean", out config.CleanPath);
vars.TryGetValue("package", out config.PackagePath);
vars.TryGetValue("asar_path", out config.AsarPath);
vars.TryGetValue("uberasm_path", out config.UberASMPath);
vars.TryGetValue("gps_path", out config.GPSPath);
vars.TryGetValue("pixi_path", out config.PixiPath);
vars.TryGetValue("addmusick_path", out config.AddMusicKPath);
vars.TryGetValue("lm_path", out config.LunarMagicPath);
vars.TryGetValue("flips_path", out config.FlipsPath);
vars.TryGetValue("levels", out config.LevelsPath);
vars.TryGetValue("map16", out config.Map16Path);
vars.TryGetValue("shared_palette", out config.SharedPalettePath);
vars.TryGetValue("global_data", out config.GlobalDataPath);
vars.TryGetValue("title_moves", out config.TitleMovesPath);
vars.TryGetValue("initial_patch", out config.InitialPatch);
lists.TryGetValue("patches", out config.Patches);
vars.TryGetValue("test_level", out config.TestLevel);
vars.TryGetValue("test_level_dest", out config.TestLevelDest);
vars.TryGetValue("retroarch_path", out config.RetroArchPath);
vars.TryGetValue("retroarch_core", out config.RetroArchCore);
vars.TryGetValue("snes9x_path", out config.Snes9xPath);
vars.TryGetValue("mesens_path", out config.MesenSPath);
return config;
}
static private void Parse(string data, HashSet<string> flags, Dictionary<string, string> vars, Dictionary<string, List<string>> lists)
{
var lines = data.Split('\n');
for (int i = 0; i < lines.Length; i++)
{
string str = lines[i];
string peek = null;
if (i < lines.Length - 1)
peek = lines[i + 1];
if (str.StartsWith("--"))
{
// comment
}
else if (str.Contains('='))
{
// var
var sp = str.Split('=');
if (sp.Length != 2)
throw new Exception("Malformed assignment");
var key = sp[0].Trim();
if (vars.ContainsKey(key))
throw new Exception($"Duplicate config key: '{key}'");
vars.Add(key, sp[1].Trim());
}
else if (peek != null && peek.Trim() == "[")
{
// list
var list = new List<string>();
lists.Add(str.Trim(), list);
i += 2;
while (true)
{
if (i >= lines.Length)
throw new Exception("Malformed list");
str = lines[i];
if (str.Trim() == "]")
break;
else if (!String.IsNullOrWhiteSpace(str))
list.Add(str.Trim());
i++;
}
}
else if (!string.IsNullOrWhiteSpace(str))
{
// flag
flags.Add(str.Trim());
}
}
}
#endregion
}
}