-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameMemory.cs
163 lines (135 loc) · 5.17 KB
/
GameMemory.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using LiveSplit.ComponentUtil;
namespace LiveSplit.AVP2010
{
class GameMemory
{
public event EventHandler OnLoadStarted;
public event EventHandler OnLoadFinished;
private Task _thread;
private CancellationTokenSource _cancelSource;
private SynchronizationContext _uiThread;
private List<int> _ignorePIDs;
private DeepPointer _IsLoading;
public void resetSplitStates()
{
}
public GameMemory(AVP2010Settings componentSettings)
{
_IsLoading = new DeepPointer(0x000FB610, 0x60);
resetSplitStates();
_ignorePIDs = new List<int>();
}
public void StartMonitoring()
{
if (_thread != null && _thread.Status == TaskStatus.Running)
{
throw new InvalidOperationException();
}
if (!(SynchronizationContext.Current is WindowsFormsSynchronizationContext))
{
throw new InvalidOperationException("SynchronizationContext.Current is not a UI thread.");
}
_uiThread = SynchronizationContext.Current;
_cancelSource = new CancellationTokenSource();
_thread = Task.Factory.StartNew(MemoryReadThread);
}
public void Stop()
{
if (_cancelSource == null || _thread == null || _thread.Status != TaskStatus.Running)
{
return;
}
_cancelSource.Cancel();
_thread.Wait();
}
void MemoryReadThread()
{
Debug.WriteLine("[NoLoads] MemoryReadThread");
while (!_cancelSource.IsCancellationRequested)
{
try
{
Debug.WriteLine("[NoLoads] Waiting for AvP_DX11.exe...");
uint frameCounter = 0;
Process game;
while ((game = GetGameProcess()) == null)
{
Thread.Sleep(250);
if (_cancelSource.IsCancellationRequested)
{
return;
}
}
Debug.WriteLine("[NoLoads] Got games process!");
bool isActuallyLoading;
bool prevIsActuallyLoading = false;
bool loadingStarted = false;
while (!game.HasExited)
{
_IsLoading.Deref(game, out isActuallyLoading);
if (isActuallyLoading != prevIsActuallyLoading)
{
if (isActuallyLoading)
{
Debug.WriteLine(String.Format("[NoLoads] Load Start - {0}", frameCounter));
loadingStarted = true;
// pause game timer
_uiThread.Post(d =>
{
if (this.OnLoadStarted != null)
{
this.OnLoadStarted(this, EventArgs.Empty);
}
}, null);
}
else
{
Debug.WriteLine(String.Format("[NoLoads] Load End - {0}", frameCounter));
if (loadingStarted)
{
loadingStarted = false;
// unpause game timer
_uiThread.Post(d =>
{
if (this.OnLoadFinished != null)
{
this.OnLoadFinished(this, EventArgs.Empty);
}
}, null);
}
}
}
prevIsActuallyLoading = isActuallyLoading;
frameCounter++;
Thread.Sleep(15);
if (_cancelSource.IsCancellationRequested)
{
return;
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
Thread.Sleep(1000);
}
}
}
Process GetGameProcess()
{
Process game = Process.GetProcesses().FirstOrDefault(p => p.ProcessName.ToLower() == "avp_dx11" && !p.HasExited && !_ignorePIDs.Contains(p.Id));
if (game == null)
{
return null;
}
return game;
}
}
}