-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameMemory.cs
More file actions
381 lines (341 loc) · 17.3 KB
/
Copy pathGameMemory.cs
File metadata and controls
381 lines (341 loc) · 17.3 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
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.Syndicate
{
class GameMemory
{
public enum SplitArea : int
{
None,
SC1_WakeUpCall,
SC1_Tutorial1,
SC2_AssaultOnAspari,
SC2_Tutorial2,
SC3_AspariExtraction,
SC4_A_train_to_catch,
SC5_Escape_from_Los_angeles,
SC6_Eurocorp,
SC6_Tutorial3,
SC7_Voyeur_Central,
SC8_Cayman_Global,
SC9_The_Floating_City,
SC10_Behind_the_scenes,
SC11_Ramon,
SC12_Downzone,
SC13_Betrayed,
SC14_The_wall,
SC15_Kris,
SC16_HumanResources,
SC17_CorporateWar,
SC18_SpireAccess,
SC19_BurningTower,
SC20_Jack_Denham
}
public event EventHandler OnFirstLevelLoading;
public event EventHandler OnPlayerGainedControl;
public event EventHandler OnLoadStarted;
public event EventHandler OnLoadFinished;
public delegate void SplitCompletedEventHandler(object sender, SplitArea type, uint frame);
public event SplitCompletedEventHandler OnSplitCompleted;
private Task _thread;
private CancellationTokenSource _cancelSource;
private SynchronizationContext _uiThread;
private List<int> _ignorePIDs;
private SyndicateSettings _settings;
private DeepPointer _isLoadingPtr;
private DeepPointer _levelNamePtr;
private static class LevelName
{
public const string C1_WakeUpCall = "S01_WAKEUPCALL";
public const string C1_Tutorial1 = "S00_TUTORIAL1";
public const string C2_AssaultOnAspari = "S03_EXECSEARCH";
public const string C2_Tutorial2 = "S00_TUTORIAL2";
public const string C3_AspariExtraction = "S04_EXECSEARCH_A";
public const string C4_A_train_to_catch = "S04_EXECSEARCH_B";
public const string C5_Escape_from_Los_angeles = "S04_EXECSEARCH_C";
public const string C6_Eurocorp = "S05_LILYDRAWL";
public const string C6_Tutorial3 = "S00_TUTORIAL3";
public const string C7_Voyeur_Central = "S06_SURVEYLILY01";
public const string C8_Cayman_Global = "S06_SURVEYLILY02";
public const string C9_The_Floating_City = "S07_LABALLENA_INTRO";
public const string C10_Behind_the_scenes = "S07_LABALLENA";
public const string C11_Ramon = "S08_LABALLENA";
public const string C12_Downzone = "S09_DOWNZONE01";
public const string C13_Betrayed = "S09_DOWNZONE02";
public const string C14_The_wall = "S11_DOWNZONE01";
public const string C15_Kris = "S11_DOWNZONE02";
public const string C16_HumanResources = "S13_HUMANRESOURCES";
public const string C17_CorporateWar = "S15_RISINGFORCE01";
public const string C18_SpireAccess = "S15_RISINGFORCE02";
public const string C19_BurningTower = "S16_RISINGFORCE";
public const string C20_Jack_Denham = "S16_DATACORE";
}
public bool[] splitStates { get; set; }
public void resetSplitStates()
{
for (int i = 0; i <= (int)SplitArea.SC20_Jack_Denham; i++)
{
splitStates[i] = false;
}
}
public GameMemory(SyndicateSettings componentSettings)
{
_settings = componentSettings;
splitStates = new bool[(int)SplitArea.SC20_Jack_Denham + 1];
resetSplitStates();
_isLoadingPtr = new DeepPointer("GameClasses_Win32_x86_Release.dll", 0x00A55288, 0x28, 0x18, 0x18, 0x14, 0x1288); // == 1 if a loadscreen is happening
_levelNamePtr = new DeepPointer("MCCDyn.dll", 0x000E8D68, 0x20, 0x10, 0x20, 0x164);
_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 syndicate.exe...");
Process game;
while ((game = GetGameProcess()) == null)
{
Thread.Sleep(250);
if (_cancelSource.IsCancellationRequested)
{
return;
}
}
Debug.WriteLine("[NoLoads] Got games process!");
uint frameCounter = 0;
bool prevIsLoading = false;
string prevStreamGroupId = String.Empty;
bool loadingStarted = false;
while (!game.HasExited)
{
string streamGroupId = String.Empty;
_levelNamePtr.DerefString(game, ReadStringType.UTF8, 25, out streamGroupId);
if(streamGroupId != null)
{
streamGroupId = streamGroupId.ToUpper();
if (streamGroupId != prevStreamGroupId)
{
//For safety... it may fail to split, but at least it won't kill load remover
if (prevStreamGroupId == LevelName.C1_WakeUpCall && streamGroupId == LevelName.C1_Tutorial1)
{
Split(SplitArea.SC1_WakeUpCall, frameCounter);
}
else if (prevStreamGroupId == LevelName.C1_Tutorial1 && streamGroupId == LevelName.C2_AssaultOnAspari)
{
Split(SplitArea.SC1_Tutorial1, frameCounter);
}
else if (prevStreamGroupId == LevelName.C2_AssaultOnAspari && streamGroupId == LevelName.C2_Tutorial2)
{
Split(SplitArea.SC2_AssaultOnAspari, frameCounter);
}
else if (prevStreamGroupId == LevelName.C2_Tutorial2 && streamGroupId == LevelName.C3_AspariExtraction)
{
Split(SplitArea.SC2_Tutorial2, frameCounter);
}
else if (prevStreamGroupId == LevelName.C3_AspariExtraction && streamGroupId == LevelName.C4_A_train_to_catch)
{
Split(SplitArea.SC3_AspariExtraction, frameCounter);
}
else if (prevStreamGroupId == LevelName.C4_A_train_to_catch && streamGroupId == LevelName.C5_Escape_from_Los_angeles)
{
Split(SplitArea.SC4_A_train_to_catch, frameCounter);
}
else if (prevStreamGroupId == LevelName.C5_Escape_from_Los_angeles && streamGroupId == LevelName.C6_Eurocorp)
{
Split(SplitArea.SC5_Escape_from_Los_angeles, frameCounter);
}
else if (prevStreamGroupId == LevelName.C6_Eurocorp && streamGroupId == LevelName.C6_Tutorial3)
{
Split(SplitArea.SC6_Eurocorp, frameCounter);
}
else if (prevStreamGroupId == LevelName.C6_Tutorial3 && streamGroupId == LevelName.C7_Voyeur_Central)
{
Split(SplitArea.SC6_Tutorial3, frameCounter);
}
else if (prevStreamGroupId == LevelName.C7_Voyeur_Central && streamGroupId == LevelName.C8_Cayman_Global)
{
Split(SplitArea.SC7_Voyeur_Central, frameCounter);
}
else if (prevStreamGroupId == LevelName.C8_Cayman_Global && streamGroupId == LevelName.C9_The_Floating_City)
{
Split(SplitArea.SC8_Cayman_Global, frameCounter);
}
else if (prevStreamGroupId == LevelName.C9_The_Floating_City && streamGroupId == LevelName.C10_Behind_the_scenes)
{
Split(SplitArea.SC9_The_Floating_City, frameCounter);
}
else if (prevStreamGroupId == LevelName.C10_Behind_the_scenes && streamGroupId == LevelName.C11_Ramon)
{
Split(SplitArea.SC10_Behind_the_scenes, frameCounter);
}
else if (prevStreamGroupId == LevelName.C11_Ramon && streamGroupId == LevelName.C12_Downzone)
{
Split(SplitArea.SC11_Ramon, frameCounter);
}
else if (prevStreamGroupId == LevelName.C12_Downzone && streamGroupId == LevelName.C13_Betrayed)
{
Split(SplitArea.SC12_Downzone, frameCounter);
}
else if (prevStreamGroupId == LevelName.C13_Betrayed && streamGroupId == LevelName.C14_The_wall)
{
Split(SplitArea.SC13_Betrayed, frameCounter);
}
else if (prevStreamGroupId == LevelName.C14_The_wall && streamGroupId == LevelName.C15_Kris)
{
Split(SplitArea.SC14_The_wall, frameCounter);
}
else if (prevStreamGroupId == LevelName.C15_Kris && streamGroupId == LevelName.C16_HumanResources)
{
Split(SplitArea.SC15_Kris, frameCounter);
}
else if (prevStreamGroupId == LevelName.C16_HumanResources && streamGroupId == LevelName.C17_CorporateWar)
{
Split(SplitArea.SC16_HumanResources, frameCounter);
}
else if (prevStreamGroupId == LevelName.C17_CorporateWar && streamGroupId == LevelName.C18_SpireAccess)
{
Split(SplitArea.SC17_CorporateWar, frameCounter);
}
else if (prevStreamGroupId == LevelName.C18_SpireAccess && streamGroupId == LevelName.C19_BurningTower)
{
Split(SplitArea.SC18_SpireAccess, frameCounter);
}
else if (prevStreamGroupId == LevelName.C19_BurningTower && streamGroupId == LevelName.C20_Jack_Denham)
{
Split(SplitArea.SC19_BurningTower, frameCounter);
}/*
else if (prevStreamGroupId == LevelName.C20_Jack_Denham)
{
Split(SplitArea.SC20_Jack_Denham, frameCounter);
}*/
}
}
_isLoadingPtr.Deref(game, out int TempIsLoading);
bool isLoading = (TempIsLoading == 1);
if (isLoading != prevIsLoading)
{
if (isLoading)
{
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);
/*
if (streamGroupId == LevelName.C1_WakeUpCall)
{
// reset game timer
_uiThread.Post(d =>
{
if (this.OnFirstLevelLoading != null)
{
this.OnFirstLevelLoading(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);
if (streamGroupId == LevelName.C1_WakeUpCall)
{
// start game timer
_uiThread.Post(d =>
{
if (this.OnPlayerGainedControl != null)
{
this.OnPlayerGainedControl(this, EventArgs.Empty);
}
}, null);
}
}
}
}
Debug.WriteLineIf(streamGroupId != prevStreamGroupId, String.Format("[NoLoads] streamGroupId changed from {0} to {1} - {2}", prevStreamGroupId, streamGroupId, frameCounter));
prevStreamGroupId = streamGroupId;
prevIsLoading = isLoading;
frameCounter++;
Thread.Sleep(15);
if (_cancelSource.IsCancellationRequested)
{
return;
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
Thread.Sleep(1000);
}
}
}
private void Split(SplitArea split, uint frame)
{
Debug.WriteLine(String.Format("[NoLoads] split {0} - {1}", split, frame));
_uiThread.Post(d =>
{
if (this.OnSplitCompleted != null)
{
this.OnSplitCompleted(this, split, frame);
}
}, null);
}
Process GetGameProcess()
{
Process game = Process.GetProcesses().FirstOrDefault(p => p.ProcessName.ToLower() == "syndicate" && !p.HasExited && !_ignorePIDs.Contains(p.Id));
if (game == null)
{
return null;
}
return game;
}
}
}