-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryReader.cs
More file actions
394 lines (321 loc) · 12.5 KB
/
MemoryReader.cs
File metadata and controls
394 lines (321 loc) · 12.5 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace ACCMetry
{
//The contracts
public delegate void PhysicsHandler(object sender, PhysicsEventArgs e);
public delegate void GraphicsHandler(object sender, GraphicsEventArgs e);
public delegate void StaticDataHandler(object sender, StaticDataEventArgs e);
/// <summary>
/// Custom exception for when shared memory isn't available.
/// Caused by the game not being in a running session most likely, as no data will be streamed to memory.
/// </summary>
public class MemoryNotActiveException : Exception
{
public MemoryNotActiveException() : base("Memory not active")
{
}
}
//Status flags for memory
enum MEMORY_STATUS { DISCONNECTED, CONNECTING, CONNECTED }
//------------------------------------------------------------------------------------------------------------------
public class MemoryReader
{
//Define MMF
MemoryMappedFile physicsDataMMF;
MemoryMappedFile graphicsDataMMF;
MemoryMappedFile staticDataMMF;
//Events that are called when timers are active and data is being processed.
public event StaticDataHandler StaticDataUpdated;
public event GraphicsHandler GraphicsDataUpdated;
public event PhysicsHandler PhysicsDataUpdated;
Timer physicsTimer;
Timer graphicsTimer;
Timer staticDataTimer;
//Set inital memory status to DISCONNECTED
private MEMORY_STATUS memStatus = MEMORY_STATUS.DISCONNECTED;
//Memory timer
private Timer memoryTimer;
/// <summary>
/// Class Constructor
/// </summary>
public MemoryReader()
{
//Initialize and set the memory timer
memoryTimer = new Timer(2000);
memoryTimer.AutoReset = true;
//As timer elapse, call method to connect to memory and repeat until a connection is made.
memoryTimer.Elapsed += memoryTimer_Elapsed;
physicsTimer = new Timer();
physicsTimer.AutoReset = true;
physicsTimer.Elapsed += physicsTimer_Elapsed;
PhysicsInterval = 10;
graphicsTimer = new Timer();
graphicsTimer.AutoReset = true;
graphicsTimer.Elapsed += graphicsTimer_Elapsed;
GraphicsInterval = 1000;
staticDataTimer = new Timer();
staticDataTimer.AutoReset = true;
staticDataTimer.Elapsed += staticInfoTimer_Elapsed;
StaticDataInterval = 1000;
}
/// <summary>
/// Stop the timers and dispose of the shared memory handles
/// </summary>
public void Stop()
{
memStatus = MEMORY_STATUS.DISCONNECTED;
memoryTimer.Stop();
// Stop the timers
physicsTimer.Stop();
graphicsTimer.Stop();
staticDataTimer.Stop();
}
/// <summary>
/// Starts the whole connection process by starting the memoryTimer which will trigger connection attempts to memory.
/// </summary>
public void Connect()
{
memoryTimer.Start();
}
/// <summary>
/// This method is called as the memorytimer have turned one whole interval
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void memoryTimer_Elapsed(object sender, ElapsedEventArgs e)
{
ConnectToMemory();
}
private void physicsTimer_Elapsed(object sender, ElapsedEventArgs e)
{
ProcessPhysicsData();
}
private void graphicsTimer_Elapsed(object sender, ElapsedEventArgs e)
{
ProcessGraphicsData();
}
private void staticInfoTimer_Elapsed(object sender, ElapsedEventArgs e)
{
ProcessStaticData();
}
/// <summary>
/// Assigns MMF files with the path to shared memory location and calls for processing to begin if files are found.
/// </summary>
/// <returns>True if a connection is made, false if no file is found.</returns>
private bool ConnectToMemory()
{
try
{
//Starting attempt to connect to memory
memStatus = MEMORY_STATUS.CONNECTING;
//Open MMF paths
staticDataMMF = MemoryMappedFile.OpenExisting("Local\\acpmf_static");
graphicsDataMMF = MemoryMappedFile.OpenExisting("Local\\acpmf_graphics");
physicsDataMMF = MemoryMappedFile.OpenExisting("Local\\acpmf_physics");
//Start processing data from memory files
graphicsTimer.Start();
ProcessGraphicsData();
physicsTimer.Start();
ProcessPhysicsData();
staticDataTimer.Start();
ProcessStaticData();
//Connected successfully
memStatus = MEMORY_STATUS.CONNECTED;
//Stop resetting connection-timer
memoryTimer.Stop();
return true;
}
catch (FileNotFoundException)
{
staticDataTimer.Stop();
graphicsTimer.Stop();
physicsTimer.Stop();
return false;
}
}
/// <summary>
/// Interval for physics updates in milliseconds
/// </summary>
public double PhysicsInterval
{
get
{
return physicsTimer.Interval;
}
set
{
physicsTimer.Interval = value;
}
}
/// <summary>
/// Interval for graphics updates in milliseconds
/// </summary>
public double GraphicsInterval
{
get
{
return graphicsTimer.Interval;
}
set
{
graphicsTimer.Interval = value;
}
}
/// <summary>
/// Interval for static info updates in milliseconds
/// </summary>
public double StaticDataInterval
{
get
{
return staticDataTimer.Interval;
}
set
{
staticDataTimer.Interval = value;
}
}
/// <summary>
/// Event triggers that notifies the subscribers and passes the data as "e"
/// </summary>
/// <param name="e">The collected data</param>
public virtual void OnStaticDataUpdated(StaticDataEventArgs e)
{
//Check if there are any subscribers to the event
if (StaticDataUpdated != null)
//Pass on the dataobject in "e" to subscribers.
StaticDataUpdated(this, e);
}
public virtual void OnGraphicsUpdated(GraphicsEventArgs e)
{
if (GraphicsDataUpdated != null)
GraphicsDataUpdated(this, e);
}
public virtual void OnPhysicsUpdated(PhysicsEventArgs e)
{
if (PhysicsDataUpdated != null)
PhysicsDataUpdated(this, e);
}
/// <summary>
/// Methods writing read byte data into respective dataobject
/// </summary>
private void ProcessPhysicsData()
{
if (memStatus == MEMORY_STATUS.DISCONNECTED)
return;
try
{
PhysicsData physicsData = ReadPhysicsData();
OnPhysicsUpdated(new PhysicsEventArgs(physicsData));
}
catch (MemoryNotActiveException) { }
}
private void ProcessGraphicsData()
{
if (memStatus == MEMORY_STATUS.DISCONNECTED)
return;
try
{
GraphicsData graphicsData = ReadGraphicsData();
OnGraphicsUpdated(new GraphicsEventArgs(graphicsData));
}
catch (MemoryNotActiveException) { }
}
private void ProcessStaticData()
{
if (memStatus == MEMORY_STATUS.DISCONNECTED)
return;
try
{
StaticData staticData = ReadStaticData();
//Data has been read/gathered. Notify the subscribers of this event and pass
//the data from "staticData"
OnStaticDataUpdated(new StaticDataEventArgs(staticData));
}
catch (MemoryNotActiveException) { }
}
/// <summary>
/// Static dataobject return method that opens a stream to the mapped memory file and decodes the byte data.
/// </summary>
/// <returns>Allocated memory data</returns>
public StaticData ReadStaticData()
{
if (memStatus == MEMORY_STATUS.DISCONNECTED || staticDataMMF == null)
throw new MemoryNotActiveException();
//Open the stream to the mapped memory file
using (var stream = staticDataMMF.CreateViewStream())
{
using (var reader = new BinaryReader(stream))
{
//Size variable for the dataobject
var size = Marshal.SizeOf(typeof(StaticData));
//Read byte stream to variable
var bytes = reader.ReadBytes(size);
var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
//Takes the unmanaged data and applies it the the data structure of StaticData struct.
var data = (StaticData)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(StaticData));
handle.Free();
return data;
}
}
}
/// <summary>
/// Graphics dataobject return method that opens a stream to the mapped memory file and decodes the byte data.
/// </summary>
/// <returns>Allocated memory data</returns>
public GraphicsData ReadGraphicsData()
{
if (memStatus == MEMORY_STATUS.DISCONNECTED || graphicsDataMMF == null)
throw new MemoryNotActiveException();
using (var stream = graphicsDataMMF.CreateViewStream())
{
using (var reader = new BinaryReader(stream))
{
//Size variable for the dataobject
var size = Marshal.SizeOf(typeof(GraphicsData));
//
var bytes = reader.ReadBytes(size);
//
var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
//
var data = (GraphicsData)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(GraphicsData));
handle.Free();
return data;
}
}
}
/// <summary>
/// Physics dataobject return method that opens a stream to the mapped memory file and decodes the byte data.
/// </summary>
/// <returns>Allocated memory data</returns>
public PhysicsData ReadPhysicsData()
{
if (memStatus == MEMORY_STATUS.DISCONNECTED || physicsDataMMF == null)
throw new MemoryNotActiveException();
using (var stream = physicsDataMMF.CreateViewStream())
{
using (var reader = new BinaryReader(stream))
{
//Size variable for the dataobject
var size = Marshal.SizeOf(typeof(PhysicsData));
//
var bytes = reader.ReadBytes(size);
//
var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
//
var data = (PhysicsData)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(PhysicsData));
handle.Free();
return data;
}
}
}
}
}