-
Notifications
You must be signed in to change notification settings - Fork 2
/
Config.cs
171 lines (148 loc) · 6.1 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
159
160
161
162
163
164
165
166
167
168
169
170
171
using Newtonsoft.Json;
using System.Diagnostics;
using System.Reflection;
using System.Text.RegularExpressions;
namespace GTerm
{
internal class JsonConfig
{
public string[]? ExclusionPatterns { get; set; }
public bool ArchiveLogs { get; set; }
public bool MonitorGmod { get; set; }
public bool StartAsGmod { get; set; }
public bool? API { get; set; }
public string? APISecret { get; set; }
public int? APIPort { get; set; }
}
internal class Config
{
internal List<Regex> ExclusionPatterns { get; set; } = [];
internal bool ArchiveLogs { get; set; } = true;
internal bool MonitorGmod { get; set; } = true;
internal bool StartAsGmod { get; set; } = false;
internal bool API { get; set; } = false;
internal string? APISecret { get; set; }
internal int APIPort { get; set; }
internal Config() { }
internal Config(string[] args)
{
JsonConfig cfg = new();
string? appPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule?.FileName);
if (appPath != null)
{
string configPath = Path.Combine(appPath, "Config.json");
if (File.Exists(configPath))
{
string json = File.ReadAllText(configPath);
JsonConfig? extractedCfg = JsonConvert.DeserializeObject<JsonConfig>(json);
if (extractedCfg != null)
{
cfg = extractedCfg;
}
}
}
if (args.Length > 0)
{
Dictionary<string, List<string>> options = ParseCLIArgs(args);
ProcessOptions(options, ref cfg);
}
this.ProcessConfig(cfg);
}
private void ProcessConfig(JsonConfig cfg)
{
this.ArchiveLogs = cfg.ArchiveLogs;
this.MonitorGmod = cfg.MonitorGmod;
this.StartAsGmod = cfg.StartAsGmod;
this.API = cfg.API ?? false;
this.APIPort = cfg.APIPort ?? 27512;
this.APISecret = cfg.APISecret;
if (cfg.ExclusionPatterns != null)
{
foreach (string pattern in cfg.ExclusionPatterns)
{
this.ExclusionPatterns.Add(new Regex(pattern, RegexOptions.Compiled));
}
}
LocalLogger.WriteLine("Config params:");
LocalLogger.WriteLine("Logs archiving: " + this.ArchiveLogs);
LocalLogger.WriteLine("Gmod monitoring: " + this.MonitorGmod);
LocalLogger.WriteLine("Start as Gmod: " + this.StartAsGmod);
LocalLogger.WriteLine("Exclusion Patterns: \n", string.Join("\n", this.ExclusionPatterns.Select(r => r.ToString())));
}
private static Dictionary<string, List<string>> ParseCLIArgs(string[] args)
{
args = args.Select(arg => arg.Trim().ToLower()).ToArray();
List<string>? knownOptionParams = null;
Dictionary<string, List<string>> options = [];
string? curOption = null;
List<string> curOptionParams = [];
foreach (string arg in args)
{
if (arg.StartsWith("--"))
{
if (curOption != null)
{
if (options.TryGetValue(curOption, out knownOptionParams))
{
knownOptionParams.AddRange(curOptionParams);
curOptionParams.Clear();
}
else
{
options.Add(curOption, curOptionParams);
curOptionParams.Clear();
}
}
if (arg.Length > 3)
curOption = arg.Substring(2);
}
else
{
curOptionParams.Add(arg);
}
}
// for the last option
if (curOption != null)
{
if (options.TryGetValue(curOption, out knownOptionParams))
knownOptionParams.AddRange(curOptionParams);
else
options.Add(curOption, curOptionParams);
}
return options;
}
private static void ProcessOptions(Dictionary<string, List<string>> options, ref JsonConfig curCfg)
{
Type baseCfgType = typeof(JsonConfig);
PropertyInfo[] props = baseCfgType.GetProperties();
foreach (KeyValuePair<string, List<string>> option in options)
{
PropertyInfo? prop = props.FirstOrDefault(p => p.Name.Equals(option.Key, StringComparison.CurrentCultureIgnoreCase));
if (prop == null) continue;
switch (prop.PropertyType)
{
case Type t when t == typeof(bool):
bool value = true;
if (option.Value.Count > 0 && int.TryParse(string.Join(' ', option.Value), out int parsedValue))
value = parsedValue > 0;
prop.SetValue(curCfg, value);
break;
case Type t when t == typeof(string[]):
prop.SetValue(curCfg, option.Value.ToArray());
break;
case Type t when t == typeof(int):
int number = 0;
if (option.Value.Count > 0 && int.TryParse(string.Join(' ', option.Value), out int parsedNumber))
number = parsedNumber;
prop.SetValue(curCfg, number);
break;
case Type t when t == typeof(string):
prop.SetValue(curCfg, string.Join(' ', option.Value));
break;
default:
break;
}
}
}
}
}