-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathVASComponent.cs
480 lines (413 loc) · 16.1 KB
/
VASComponent.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using LiveSplit.Model;
using LiveSplit.UI;
using LiveSplit.UI.Components;
using LiveSplit.VAS.Models;
using LiveSplit.VAS.Models.Delta;
using LiveSplit.VAS.UI;
using LiveSplit.VAS.VASL;
namespace LiveSplit.VAS
{
public class VASComponent : LogicComponent
{
public static readonly Version Version = typeof(VASComponent).Assembly.GetName().Version;
public override string ComponentName => "Video Auto Splitter";
private LiveSplitState State;
private ComponentUI ComponentUI;
private string _ProfilePath = string.Empty;
public string ProfilePath
{
get
{
return _ProfilePath;
}
set
{
if (_ProfilePath != value)
{
// This is so the event logger for ProfileCleanup doesn't run at start.
if (!string.IsNullOrEmpty(_ProfilePath))
{
ProfileCleanup();
}
try
{
_ProfilePath = value;
ProfileChanged?.Invoke(this, GameProfile); // Invokes getter
}
catch (Exception e)
{
Log.Error(e, "Profile path failed to set.");
_ProfilePath = string.Empty;
ProfileCleanup();
}
}
}
}
private GameProfile _GameProfile = null;
public GameProfile GameProfile
{
get
{
if (_GameProfile == null)
{
try
{
if (!string.IsNullOrWhiteSpace(_ProfilePath))
{
Log.Info("Loading new game profile: " + ProfilePath);
_GameProfile = GameProfile.FromPath(ProfilePath);
// Todo: Unset GameVersion if the existing one isn't in the new profile. Maybe.
if (File.Exists(ProfilePath))
{
FSWatcher.Path = Path.GetDirectoryName(ProfilePath);
FSWatcher.Filter = Path.GetFileName(ProfilePath + "*");
}
else
{
FSWatcher.Path = ProfilePath;
FSWatcher.Filter = null;
}
FSWatcher.EnableRaisingEvents = true;
Log.Info("Game profile successfully loaded!");
// Fixes a recursion problem that decided to show up.
_Script = new VASLScript(_GameProfile.RawScript, GameVersion);
// Todo: Log this separately. Just don't want to atm.
VASLSettings settings = Script.RunStartup(State);
SetVASLSettings(settings);
}
}
catch (Exception e)
{
Log.Error(e, "Error loading Game profile:");
ProfileCleanup();
}
}
return _GameProfile;
}
}
private VASLScript _Script = null;
public VASLScript Script
{
get
{
if (_Script == null)
{
try
{
var gp = GameProfile; // Invoke getter
Log.Info("Loading VASL script within profile...");
_Script = new VASLScript(gp.RawScript, GameVersion);
Log.Info("VASL script successfully loaded!");
TryStartScanner();
}
catch (Exception e)
{
Log.Error(e, "Error loading VASL script:");
ProfileCleanup();
}
}
return _Script;
}
}
private string _GameVersion = string.Empty;
public string GameVersion
{
get
{
return _GameVersion;
}
internal set
{
if (_GameVersion != value)
{
_GameVersion = value;
GameVersionChanged?.Invoke(this, _GameVersion);
_Script = null; // Testing
}
}
}
// To expand upon
private string _VideoDevice = string.Empty;
public string VideoDevice {
get
{
return _VideoDevice;
}
internal set
{
if (_VideoDevice != value)
{
_VideoDevice = value;
VideoDeviceChanged?.Invoke(this, _VideoDevice);
TryStartScanner();
}
}
}
public IDictionary<string, Geometry> CropGeometries { get; internal set; }
public IDictionary<string, bool> BasicSettingsState { get; internal set; }
public IDictionary<string, dynamic> CustomSettingsState { get; internal set; }
// Temporary. Remove later.
public Geometry CropGeometry { get { return Scanner.CropGeometry; } set { Scanner.CropGeometry = value; } }
public Scanner Scanner { get; internal set; }
private FileSystemWatcher FSWatcher;
public event EventHandler<GameProfile> ProfileChanged;
public event EventHandler<string> VideoDeviceChanged;
public event EventHandler<string> GameVersionChanged;
//public event EventHandler<Geometry> CropGeometryChanged;
public VASComponent(LiveSplitState state)
{
Log.Info("Establishing Video Auto Splitter, standby...");
State = state;
ComponentUI = new ComponentUI(this);
CropGeometries = new Dictionary<string, Geometry>();
BasicSettingsState = new Dictionary<string, bool>();
CustomSettingsState = new Dictionary<string, dynamic>();
Scanner = new Scanner(this);
FSWatcher = new FileSystemWatcher();
FSWatcher.Changed += (sender, args) => {
ProfileCleanup();
ProfileChanged?.Invoke(this, GameProfile);
};
Scanner.NewResult += (sender, dm) => RunScript(sender, dm);
}
public override Control GetSettingsControl(LayoutMode mode) => ComponentUI;
#region Save Settings
public override XmlNode GetSettings(XmlDocument document)
{
XmlElement settingsNode = document.CreateElement("Settings");
settingsNode.AppendChild(SettingsHelper.ToElement(document, "Version", VASComponent.Version));
settingsNode.AppendChild(SettingsHelper.ToElement(document, "ProfilePath", ProfilePath));
settingsNode.AppendChild(SettingsHelper.ToElement(document, "VideoDevice", VideoDevice));
settingsNode.AppendChild(SettingsHelper.ToElement(document, "GameVersion", GameVersion));
settingsNode.AppendChild(SettingsHelper.ToElement(document, "CropGeometry", CropGeometry));
AppendBasicSettingsToXml(document, settingsNode);
AppendCustomSettingsToXml(document, settingsNode);
return settingsNode;
}
private void AppendBasicSettingsToXml(XmlDocument document, XmlNode settingsNode)
{
var basicSettings = ComponentUI?.SettingsUI?.BasicSettings;
if (basicSettings != null && basicSettings.Count > 0)
{
foreach (var setting in basicSettings)
{
var key = setting.Key;
if (BasicSettingsState.ContainsKey(key.ToLower()))
{
var value = BasicSettingsState[key.ToLower()];
settingsNode.AppendChild(SettingsHelper.ToElement(document, key, value));
}
}
}
}
private void AppendCustomSettingsToXml(XmlDocument document, XmlNode parent)
{
XmlElement vaslParent = document.CreateElement("CustomSettings");
foreach (var state in CustomSettingsState)
{
XmlElement element = SettingsHelper.ToElement(document, "Setting", state.Value);
XmlAttribute id = SettingsHelper.ToAttribute(document, "ID", state.Key);
element.Attributes.Append(id);
vaslParent.AppendChild(element);
}
parent.AppendChild(vaslParent);
}
#endregion Save Settings
#region Load Settings
public override void SetSettings(XmlNode settings)
{
var element = (XmlElement)settings;
if (!element.IsEmpty)
{
ParseStandardSettingsFromXml(element);
ParseBasicSettingsFromXml(element);
ParseCustomScriptSettingsFromXml(element);
}
//UpdateProfile(null, null);
}
private void ParseStandardSettingsFromXml(XmlElement element)
{
ProfilePath = SettingsHelper.ParseString(element["ProfilePath"], string.Empty);
GameVersion = SettingsHelper.ParseString(element["GameVersion"], string.Empty);
// Hacky? Probably. Geometry should have proper XML support.
var geo = Geometry.FromString(element["CropGeometry"].InnerText);
if (geo.HasSize) CropGeometry = geo;
VideoDevice = SettingsHelper.ParseString(element["VideoDevice"], string.Empty);
}
private void ParseBasicSettingsFromXml(XmlElement element)
{
var basicSettings = ComponentUI?.SettingsUI?.BasicSettings;
if (basicSettings != null && basicSettings.Count > 0)
{
foreach (var setting in basicSettings)
{
if (element[setting.Key] != null)
{
var value = bool.Parse(element[setting.Key].InnerText);
if (setting.Value.Enabled)
{
setting.Value.Checked = value;
}
BasicSettingsState[setting.Key.ToLower()] = value;
}
}
}
}
private void ParseCustomScriptSettingsFromXml(XmlElement data)
{
XmlElement customSettingsNode = data["CustomSettings"];
if (customSettingsNode != null && customSettingsNode.HasChildNodes)
{
foreach (XmlElement element in customSettingsNode.ChildNodes)
{
if (element.Name != "Setting")
{
continue;
}
string id = element.Attributes["id"]?.Value;
//string type = element.Attributes["type"].Value;
string type = "string"; // Temporary until coercion by the script is done.
if (id != null)
{
dynamic value;
switch (type)
{
case "bool":
value = SettingsHelper.ParseBool(element);
break;
case "float":
case "double":
value = SettingsHelper.ParseDouble(element);
break;
case "byte":
case "sbyte":
case "short":
case "ushort":
case "int":
case "uint":
case "long":
case "ulong":
value = SettingsHelper.ParseInt(element);
break;
case "char":
case "string":
value = SettingsHelper.ParseString(element);
break;
case "timespan":
value = SettingsHelper.ParseTimeSpan(element);
break;
default:
throw new NotSupportedException("Data type is either incorrect or unsupported.");
}
CustomSettingsState[id] = value;
}
}
}
//SettingUI.UpdateNodesValues(CustomSettingsState, null);
}
#endregion Load Settings
#region VASL Settings
private void InitVASLSettings(VASLSettings settings, bool scriptLoaded)
{
if (!scriptLoaded)
{
BasicSettingsState.Clear();
CustomSettingsState.Clear();
}
else
{
var values = new Dictionary<string, dynamic>();
foreach (var setting in settings.OrderedSettings)
{
var value = setting.Value;
if (CustomSettingsState.ContainsKey(setting.Id))
value = CustomSettingsState[setting.Id];
setting.Value = value;
values.Add(setting.Id, value);
}
CustomSettingsState = values;
}
ComponentUI.InitVASLSettings(settings, scriptLoaded);
}
public void SetVASLSettings(VASLSettings settings)
{
InitVASLSettings(settings, true);
}
public void ResetVASLSettings()
{
InitVASLSettings(new VASLSettings(), false);
}
#endregion VASL Settings
public bool IsScriptLoaded()
{
return _Script != null;
}
internal void RunScript(object sender, DeltaOutput d)
{
try
{
Script.Update(State, d);
}
catch (Exception e)
{
Log.Error(e, "VASL Script failed to process frame.");
}
}
private void TryStartScanner()
{
try
{
if (!string.IsNullOrEmpty(VideoDevice))
{
Scanner.AsyncStart();
}
}
catch (Exception e)
{
Log.Error(e, "Scanner failed to initialize");
ProfileCleanup();
}
}
private void ProfileCleanup()
{
Log.Info("Cleaning up profile...");
try
{
//FSWatcher.EnableRaisingEvents = false;
if (_Script != null)
{
Script.RunShutdown(State);
}
}
catch (Exception e)
{
Log.Error(e, "Unable to run shutdown on script, skipping.");
}
finally
{
Scanner.Stop();
_GameProfile = null;
_Script = null;
ResetVASLSettings();
}
Log.Info("Profile cleanup finished.");
}
public override void Dispose()
{
Log.Info("Disposing...");
_ProfilePath = null;
ProfileCleanup();
Scanner.Dispose();
FSWatcher?.Dispose();
ComponentUI.Dispose();
Log.Info("Closing...");
Log.Flush();
}
public override void Update(IInvalidator i, LiveSplitState s, float w, float h, LayoutMode m) { }
}
}