-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameMemory.cs
More file actions
277 lines (235 loc) · 9.7 KB
/
Copy pathGameMemory.cs
File metadata and controls
277 lines (235 loc) · 9.7 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
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.DXTF
{
#region Game Data
public enum GameState
{
Startup,
FadeStaticOut,
FadeKeyArtIn,
FadeKeyArtOut,
ObbDownload,
LoadLocalization,
PreMenu,
Menu,
FadeBlackToGameLoad,
FadeInGameLoad,
FadeOutGameLoad,
WaitingOnLoad,
Game,
Paused,
FadeBlackToMenuLoad,
FadeInMenuLoad,
FramePadding,
LoadMenuFromGame,
SetupMenuFromGame,
FadeOutMenuLoad
}
#endregion
class GameMemory
{
public event EventHandler OnLoadStarted;
public event EventHandler OnLoadFinished;
public event EventHandler OnLevelChanged;
public event EventHandler OnFirstLevelLoad;
public event EventHandler OnFirstLevelAutostart;
private Task _thread;
private CancellationTokenSource _cancelSource;
private SynchronizationContext _uiThread;
private List<int> _ignorePIDs;
private DeepPointer _MGameState;
private DeepPointer _MJustLoadedLevel;
private DeepPointer _MLevelToLoadPtr;
private enum ExpectedDllSizes
{
DXTF = 11948032,
}
public void resetSplitStates()
{
}
public GameMemory(DXTFSettings componentSettings)
{
resetSplitStates();
_MJustLoadedLevel = new DeepPointer(0x009E2A64, 0x50, 0x8, 0x50, 0x164, 0x13);
_MGameState = new DeepPointer(0x009E2A64, 0x50, 0x8, 0x50, 0x164, 0x0, 0x58);
_MLevelToLoadPtr = new DeepPointer(0x009E2A64, 0x50, 0x8, 0x50, 0x164, 0x8, 0xC);
_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 DeusEx_steam.exe...");
uint frameCounter = 0;
Process game;
while ((game = GetGameProcess()) == null)
{
Thread.Sleep(250);
if (_cancelSource.IsCancellationRequested)
{
return;
}
}
Debug.WriteLine("[NoLoads] Got games process!");
GameState prevGameState = GameState.Startup;
bool prevIsJustLoadedLevel = false;
bool loadingStarted = false;
string prevLevelName = "";
while (!game.HasExited)
{
bool isJustLoadedLevel = _MJustLoadedLevel.Deref<bool>(game);
GameState gameState = _MGameState.Deref<GameState>(game);
string levelName = _MLevelToLoadPtr.DerefString(game, ReadStringType.UTF16, 40, "");
if (gameState != prevGameState)
{
switch (gameState)
{
case GameState.FadeInGameLoad:
case GameState.WaitingOnLoad:
case GameState.FadeOutGameLoad:
{
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);
}
break;
default:
{
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);
}
}
break;
}
Debug.WriteLine($"Game state changed from {prevGameState} to {gameState}");
}
if (isJustLoadedLevel != prevIsJustLoadedLevel)
{
if(!isJustLoadedLevel)
{
if (loadingStarted)
{
loadingStarted = false;
// unpause game timer
_uiThread.Post(d =>
{
if (this.OnLoadFinished != null)
{
this.OnLoadFinished(this, EventArgs.Empty);
}
}, null);
}
if (levelName == "CostaRicaSafehouse")
{
//Autostart invoke event
_uiThread.Post(d =>
{
if (this.OnFirstLevelLoad != null)
{
this.OnFirstLevelLoad(this, EventArgs.Empty);
}
}, null);
}
}
}
if (levelName != prevLevelName && prevLevelName != "" && levelName != "")
{
if (levelName == "CostaRicaSafehouse" && prevLevelName == "Menu")
{
//Autorestart
_uiThread.Post(d =>
{
if (this.OnFirstLevelAutostart != null)
{
this.OnFirstLevelAutostart(this, EventArgs.Empty);
}
}, null);
}
Debug.WriteLine($"Level changed: {prevLevelName} -> {levelName}");
}
prevGameState = gameState;
prevLevelName = levelName;
prevIsJustLoadedLevel = isJustLoadedLevel;
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() == "deusex_steam" && !p.HasExited && !_ignorePIDs.Contains(p.Id));
if (game == null)
{
return null;
}
if (game.MainModuleWow64Safe().ModuleMemorySize != (int)ExpectedDllSizes.DXTF )
{
_ignorePIDs.Add(game.Id);
_uiThread.Send(d => MessageBox.Show("Unexpected game version. Deus Ex The Fall (1.1) is required.", "LiveSplit.DXTF",
MessageBoxButtons.OK, MessageBoxIcon.Error), null);
return null;
}
return game;
}
}
}