forked from m3gagluk/VRCFTVarjoModule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigManager.cs
338 lines (308 loc) · 15.2 KB
/
ConfigManager.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
using Microsoft.Extensions.Logging;
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
namespace VRCFTVarjoModule
{
internal class ConfigManager
{
protected readonly IFormatProvider _formatProvider = new CultureInfo("en-001");
protected ILogger Logger;
protected string path;
protected bool shouldCreateLink;
public int readDelay { get; protected set; }
public OpennessStrategy opennessStrategy { get; protected set; }
// Magic numbers for float lid parsing
public float squeezeThreshold { get; protected set; }
public float widenThreshold { get; protected set; }
public float opennessRange { get; protected set; }
public float maxOpenSpeed { get; protected set; }
public ConfigManager(ILogger loggerInstance) {
Logger = loggerInstance;
InitConfig();
path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\VRCFT Varjo Config.ini";
if (!File.Exists(path))
{
WriteConfig();
}
Reload();
SaveModuleShortcut();
}
#region Config Handlers
/// <summary>
/// Inits internal config state to defaults
/// </summary>
public void InitConfig()
{
shouldCreateLink = true;
readDelay = 10;
opennessStrategy = OpennessStrategy.RestrictedSpeed;
squeezeThreshold = 0.15f;
widenThreshold = 0.9f;
opennessRange = widenThreshold - squeezeThreshold;
maxOpenSpeed = 0.1f;
}
/// <summary>
/// Writes the current configuration to disk
/// </summary>
public void WriteConfig()
{
StreamWriter fs = null;
try
{
fs = new StreamWriter(path, false, Encoding.UTF8);
fs.AutoFlush = false;
fs.WriteLine("[VarjoModule]");
// Implement writing all the configs here! (please add comments using # or ; to the ini file)
fs.WriteLine("");
fs.WriteLine("; Whether or not the module should create a link to the module folder on the desktop");
fs.WriteLine($"CreateLink={shouldCreateLink}");
fs.WriteLine("");
fs.WriteLine("; Double Time is an experimental option which lets the module read from the SDK 2x normal (200Hz)");
fs.WriteLine("; This setting will do nothing but increase CPU time on VB versions older then 4.1");
fs.WriteLine($"DoubleTime={readDelay == 5}");
fs.WriteLine("");
fs.WriteLine("; EyeLidStrat defines how the module calculates the Openness Value");
fs.WriteLine("; There are 5 possible options: Bool, Stepped, RestrictedSpeed, RawFloat & Hybrid");
fs.WriteLine("; Bool => The Eye Lids are either fully open or fully closed (no in-between) based on the individual Eye Status");
fs.WriteLine("; Stepped => The Eye lids openness is stepped based on the individual eye status (Fully Open, 1/3 closed, 2/3 closed, fully closed)");
fs.WriteLine("; RawFloat => The Openness Value given from the Varjo SDK is forwarded with no extra filtering (split up using the thresholds still happens)");
fs.WriteLine("; RestrictedSpeed => (default behaviour) like RawFloat, however the speed at which the eyes can open is limited to MaxOpenSpeed when the eye status is registering as unreliable or invalid");
fs.WriteLine("; Hybrid => Limits openness based on eye status, but allows for further refinement downwards using the openness value (the limits are 1/4 closed, 1/2 closed & 3/4 closed)");
fs.WriteLine($"EyeLidStrat={OpennessStratToString(opennessStrategy)}");
fs.WriteLine("");
fs.WriteLine("; Squeeze and Widen Thershold are only relevant for float-based Eye Lid Strats");
fs.WriteLine("; the Widen Thershold cannot be below the Squeeze Threshold and vice versa");
fs.WriteLine("; Setting Squeeze to 0 or Widen to 1 will disable the parsing for that additional eye lid range");
fs.WriteLine($"SqueezeThreshold={squeezeThreshold.ToString(_formatProvider)}");
fs.WriteLine($"WidenThreshold={widenThreshold.ToString(_formatProvider)}");
fs.WriteLine("");
fs.WriteLine("; MaxOpenSpeed is only relevant for the \"RestrictedSpeed\" Eye Lid Strat");
fs.WriteLine("; setting MaxOpenSpeed to 0 prevents the eye lids from opening up at all for as long as the Eye status reads invalid or unreliable");
fs.WriteLine($"MaxOpenSpeed={maxOpenSpeed.ToString(_formatProvider)}");
fs.Flush();
}
catch
{
Logger.LogWarning("Could not write INI config file!");
}
fs?.Close();
}
/// <summary>
/// Function which generates a Shortcut for the module config on the users desktop
/// </summary>
public void SaveModuleShortcut()
{
// skip all logic if shortcut creation is disabled
if (!shouldCreateLink)
{
Logger.LogInformation($"Link creation disabled, skipping.");
return;
}
try
{
var lnkFilename = $"{Path.GetFileNameWithoutExtension(path)}.lnk";
var shortcutLocation = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
lnkFilename
);
if (Path.Exists(shortcutLocation))
{
Logger.LogInformation($"Shortcut to config already exists, skipping shortcut creation.");
return;
}
var shell = new IWshRuntimeLibrary.WshShell();
var shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.Description = $"Shortcut to {Path.GetFileName(path)}"; // The description of the shortcut
shortcut.TargetPath = path; // The path of the file that will launch when the shortcut is run
shortcut.Save();
Logger.LogInformation($"Successfully created shortcut for {Path.GetFileName(path)}");
return;
}
catch (Exception ex)
{
Logger.LogError($"Failed to create shortcut for file: {path}, reason: {ex.Message}");
return;
}
}
/// <summary>
/// Loads the current configuration from disk
/// </summary>
public void Reload() {
if (File.Exists(path))
{
Logger.LogInformation("Loading config file");
StreamReader fs = null;
try
{
fs = new StreamReader(path, Encoding.UTF8);
bool correctSection = false, finished = false;
// continue reading the file until we either reach the end, or finished the correct parsing group
while (!fs.EndOfStream && !finished)
{
var line = fs.ReadLine();
// Skip comment lines
if (line.StartsWith(";") || line.StartsWith("#")) continue;
// Start parsing key-value pairs only in the VarjoModule section
if (line == "[VarjoModule]")
{
correctSection = true;
continue;
}
// Once the VarjoModule section is over, mark parsing as finished
if (line.StartsWith("[") && correctSection)
{
finished = true;
continue;
}
if (correctSection && line.Contains("=") && !line.Trim().StartsWith("="))
{
// parse the keyname and value
var name = line[..line.IndexOf('=')].Trim();
var value = line.Substring(line.IndexOf("=") + 1).Trim();
switch (name.ToLower())
{
// Implement all valid fields here
case "createlink":
shouldCreateLink = ParseStringToBool(value);
break;
case "doubletime":
readDelay = ParseStringToBool(value) ? 5 : 10;
break;
case "eyelidstrat":
{
var strat = StringToOpennessStrat(value);
if (strat == OpennessStrategy.INVALID)
{
Logger.LogWarning($"{value} is not a valid EyeLidStrat!");
}
else
{
opennessStrategy = strat;
}
break;
}
case "squeezethreshold":
{
if (float.TryParse(value, _formatProvider, out var pval))
{
if (pval > 0.5 || pval > widenThreshold)
{
Logger.LogWarning($"SqueezeThreshold must less then 0.5 and WidenThreshold");
}
else
{
squeezeThreshold = pval;
opennessRange = widenThreshold - squeezeThreshold;
}
}
else Logger.LogWarning($"{value} is not a valid Float!");
break;
}
case "widenthreshold":
{
if (float.TryParse(value, _formatProvider, out var pval))
{
if (pval < 0.5 || pval < squeezeThreshold)
{
Logger.LogWarning($"WidenThreshold must larger then 0.5 and SqueezeThreshold");
}
else
{
widenThreshold = pval;
opennessRange = widenThreshold - squeezeThreshold;
}
}
else Logger.LogWarning($"{value} is not a valid Float!");
break;
}
case "maxopenspeed":
{
if (float.TryParse(value, _formatProvider, out var pval))
{
if (pval < 0 || pval > 1)
{
Logger.LogWarning($"MaxOpenSpeed may not be <0 or >1");
}
maxOpenSpeed = pval;
}
else Logger.LogWarning($"{value} is not a valid Float!");
break;
}
default:
Logger.LogWarning($"Unknown key {name} found with value {value}!");
break;
}
}
}
}
catch
{
Logger.LogWarning("Error while parsing INI config file. Continuing with default config.");
InitConfig();
}
fs?.Close();
}
}
#endregion
#region Parsers
/// <summary>
/// Parses an OpennessStrategy Enum value to it's corresponding string
/// </summary>
/// <param name="strat"></param>
/// <returns></returns>
private static string OpennessStratToString(OpennessStrategy strat)
{
switch (strat)
{
case OpennessStrategy.Bool: return "Bool";
case OpennessStrategy.Stepped: return "Stepped";
case OpennessStrategy.Raw: return "RawFloat";
case OpennessStrategy.RestrictedSpeed: return "RestrictedSpeed";
case OpennessStrategy.Hybrid: return "Hybrid";
}
return "INVALID";
}
/// <summary>
/// Parses a string to it's corresponding OpennessStrategy Enum value
/// </summary>
/// <param name="strat"></param>
/// <returns></returns>
private static OpennessStrategy StringToOpennessStrat(string strat)
{
switch (strat)
{
case "Bool": return OpennessStrategy.Bool;
case "Stepped": return OpennessStrategy.Stepped;
case "RawFloat": return OpennessStrategy.Raw;
case "RestrictedSpeed": return OpennessStrategy.RestrictedSpeed;
case "Hybrid": return OpennessStrategy.Hybrid;
}
return OpennessStrategy.INVALID;
}
/// <summary>
/// Parses a string *correctly* to a boolean (y'know a string of "false" actually parses to false!)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private bool ParseStringToBool(string str)
{
if (str.ToLower() == "false" || str == "0") return false;
else if (str.ToLower() == "true" || str == "1") return true;
Logger.LogWarning($"{str} not a valid Boolean! Using length as fallback (string length >0 = true)");
return str.Length > 0;
}
#endregion
}
public enum OpennessStrategy
{
INVALID=-1,
Bool,
Stepped,
RestrictedSpeed,
Raw,
Hybrid
}
}