-
Notifications
You must be signed in to change notification settings - Fork 0
/
TOEMComponent.cs
149 lines (125 loc) · 3.96 KB
/
TOEMComponent.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
using LiveSplit.Model;
using LiveSplit.TOEM.Game;
using LiveSplit.TOEM.Memory;
using LiveSplit.UI;
using LiveSplit.UI.Components;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
namespace LiveSplit.TOEM
{
public class TOEMComponent : IComponent
{
public string ComponentName { get { return "TOEM Autosplitter"; } }
public float HorizontalWidth { get { return 0; } }
public float MinimumHeight { get { return 0; } }
public float VerticalHeight { get { return 0; } }
public float MinimumWidth { get { return 0; } }
public float PaddingTop { get { return 0; } }
public float PaddingBottom { get { return 0; } }
public float PaddingLeft { get { return 0; } }
public float PaddingRight { get { return 0; } }
public IDictionary<string, Action> ContextMenuControls { get { return null; } }
private Thread _updateLoop;
private bool _updateLoopRunning;
private ProcessCapture _processCapture;
private MemoryInterface _memoryInterface;
private Speedrun _speedrun;
public TOEMComponent(LiveSplitState state, bool shown = false)
{
_processCapture = new ProcessCapture("TOEM");
_processCapture.ProcessHooked += InitializeHook;
_processCapture.ProcessLost += DisposeHook;
Debug.WriteLine("LiveSplitState = " + state);
_speedrun = new Speedrun(state);
_updateLoopRunning = true;
_updateLoop = new Thread(UpdateLoopMain);
_updateLoop.IsBackground = true;
_updateLoop.Start();
}
//
// Component Logic
//
private void UpdateLoopMain()
{
while (_updateLoopRunning)
{
try
{
PerformUpdate();
}
catch (Exception ex)
{
// TODO: Perform logging
}
Thread.Sleep(5);
}
}
private void PerformUpdate()
{
// Attempt to hook game, if not already hooked
if (!_processCapture.EnsureProcessHook()) return;
UpdateHook();
}
private void InitializeHook(object sender, EventArgs args)
{
try
{
_memoryInterface = new MemoryInterface(_processCapture.HookedProcess);
_speedrun.ReInitialize(_memoryInterface);
}
catch (Exception)
{
// TODO: Log exception
_processCapture.Unhook();
}
}
private void UpdateHook()
{
try
{
_speedrun.Update();
}
catch (Exception ex)
{
// TODO: Log exception
Console.WriteLine(ex);
}
}
private void DisposeHook(object sender, EventArgs args)
{
_speedrun.UnInitialize();
_memoryInterface = null;
}
//
// IComponent implementation
//
public void Dispose()
{
}
public void DrawHorizontal(Graphics g, LiveSplitState state, float height, Region clipRegion)
{
}
public void DrawVertical(Graphics g, LiveSplitState state, float width, Region clipRegion)
{
}
public XmlNode GetSettings(XmlDocument document)
{
return document.CreateElement("Settings");
}
public Control GetSettingsControl(LayoutMode mode)
{
return null;
}
public void SetSettings(XmlNode settings)
{
}
public void Update(IInvalidator invalidator, LiveSplitState state, float width, float height, LayoutMode mode)
{
}
}
}