forked from ZacharyTalis/alyx-multiplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryWatcher.cs
291 lines (244 loc) · 7.89 KB
/
MemoryWatcher.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
#pragma warning disable 1591
// Note: Please be careful when modifying this because it could break existing components!
namespace LiveSplit.ComponentUtil
{
public class MemoryWatcherList : List<MemoryWatcher>
{
public delegate void MemoryWatcherDataChangedEventHandler(MemoryWatcher watcher);
public event MemoryWatcherDataChangedEventHandler OnWatcherDataChanged;
public MemoryWatcher this[string name]
{
get { return this.First(w => w.Name == name); }
}
public void UpdateAll(Process process)
{
if (OnWatcherDataChanged != null)
{
var changedList = new List<MemoryWatcher>();
foreach (var watcher in this)
{
bool changed = watcher.Update(process);
if (changed)
changedList.Add(watcher);
}
// only report changes when all of the other watches are updated too
foreach (var watcher in changedList)
{
OnWatcherDataChanged(watcher);
}
}
else
{
foreach (var watcher in this)
{
watcher.Update(process);
}
}
}
public void ResetAll()
{
foreach (var watcher in this)
{
watcher.Reset();
}
}
}
public abstract class MemoryWatcher
{
public enum ReadFailAction
{
DontUpdate,
SetZeroOrNull,
}
public string Name { get; set; }
public bool Enabled { get; set; }
public object Current { get; set; }
public object Old { get; set; }
public bool Changed { get; protected set; }
public TimeSpan? UpdateInterval { get; set; }
public ReadFailAction FailAction { get; set; }
protected bool InitialUpdate { get; set; }
protected DateTime? LastUpdateTime { get; set; }
protected DeepPointer DeepPtr { get; set; }
protected IntPtr Address { get; set; }
protected AddressType AddrType { get; }
protected enum AddressType { DeepPointer, Absolute }
protected MemoryWatcher(DeepPointer pointer)
{
DeepPtr = pointer;
AddrType = AddressType.DeepPointer;
Enabled = true;
FailAction = ReadFailAction.DontUpdate;
}
protected MemoryWatcher(IntPtr address)
{
Address = address;
AddrType = AddressType.Absolute;
Enabled = true;
FailAction = ReadFailAction.DontUpdate;
}
/// <summary>
/// Updates the watcher and returns true if the value has changed.
/// </summary>
public abstract bool Update(Process process);
public abstract void Reset();
protected bool CheckInterval()
{
if (UpdateInterval.HasValue)
{
if (LastUpdateTime.HasValue)
{
if (DateTime.Now - LastUpdateTime.Value < UpdateInterval.Value)
return false;
}
LastUpdateTime = DateTime.Now;
}
return true;
}
}
public class StringWatcher : MemoryWatcher
{
public new string Current
{
get { return (string)base.Current; }
set { base.Current = value; }
}
public new string Old
{
get { return (string)base.Old; }
set { base.Old = value; }
}
public delegate void StringChangedEventHandler(string old, string current);
public event StringChangedEventHandler OnChanged;
private ReadStringType _stringType;
private int _numBytes;
public StringWatcher(DeepPointer pointer, ReadStringType type, int numBytes)
: base(pointer)
{
_stringType = type;
_numBytes = numBytes;
}
public StringWatcher(DeepPointer pointer, int numBytes)
: this(pointer, ReadStringType.AutoDetect, numBytes) { }
public StringWatcher(IntPtr address, ReadStringType type, int numBytes)
: base(address)
{
_stringType = type;
_numBytes = numBytes;
}
public StringWatcher(IntPtr address, int numBytes)
: this(address, ReadStringType.AutoDetect, numBytes) { }
public override bool Update(Process process)
{
Changed = false;
if (!Enabled)
return false;
if (!CheckInterval())
return false;
string str;
bool success;
if (AddrType == AddressType.DeepPointer)
success = DeepPtr.DerefString(process, _stringType, _numBytes, out str);
else
success = process.ReadString(Address, _stringType, _numBytes, out str);
if (success)
{
base.Old = base.Current;
base.Current = str;
}
else
{
if (FailAction == ReadFailAction.DontUpdate)
return false;
base.Old = base.Current;
base.Current = str;
}
if (!InitialUpdate)
{
InitialUpdate = true;
return false;
}
if (!Current.Equals(Old))
{
OnChanged?.Invoke(Old, Current);
Changed = true;
return true;
}
return false;
}
public override void Reset()
{
base.Current = null;
base.Old = null;
InitialUpdate = false;
}
}
public class MemoryWatcher<T> : MemoryWatcher where T : struct
{
public new T Current
{
get { return (T)(base.Current ?? default(T)); }
set { base.Current = value; }
}
public new T Old
{
get { return (T)(base.Old ?? default(T)); }
set { base.Old = value; }
}
public delegate void DataChangedEventHandler(T old, T current);
public event DataChangedEventHandler OnChanged;
public MemoryWatcher(DeepPointer pointer)
: base(pointer) { }
public MemoryWatcher(IntPtr address)
: base(address) { }
public override bool Update(Process process)
{
Changed = false;
if (!Enabled)
return false;
if (!CheckInterval())
return false;
base.Old = Current;
T val;
bool success;
if (AddrType == AddressType.DeepPointer)
success = DeepPtr.Deref(process, out val);
else
success = process.ReadValue(Address, out val);
if (success)
{
base.Old = base.Current;
base.Current = val;
}
else
{
if (FailAction == ReadFailAction.DontUpdate)
return false;
base.Old = base.Current;
base.Current = val;
}
if (!InitialUpdate)
{
InitialUpdate = true;
return false;
}
if (!Current.Equals(Old))
{
OnChanged?.Invoke(Old, Current);
Changed = true;
return true;
}
return false;
}
public override void Reset()
{
base.Current = default(T);
base.Old = default(T);
InitialUpdate = false;
}
}
}