-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLiveSplitLogic.cs
350 lines (303 loc) · 10.1 KB
/
LiveSplitLogic.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
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
using LiveSplit.BugFables.UI;
using System;
using System.IO;
namespace LiveSplit.BugFables
{
public class LiveSplitLogic
{
private enum EndTimeState
{
NotArrivedYet,
ArrivedInRoom,
SongLevelUpStarting,
SongLevelIsPlaying,
SongIsFading
}
private const int newGameEventId = 8;
private EndTimeState currentEndTimeState = EndTimeState.NotArrivedYet;
private GameMemory gameMemory;
// Defaults to true so that the ShouldStart logic won't trigger if LiveSplit
// is opened whtn a file is loaded
private bool oldNewGameStarted = true;
private byte[] oldEnemyEncounter = null;
private string pathLogFile = "";
private bool lastReadWasBad = false;
private Split[] splits;
private SettingsUserControl settings;
public LiveSplitLogic(GameMemory gameMemory, SettingsUserControl settings)
{
this.gameMemory = gameMemory;
this.settings = settings;
this.pathLogFile = Directory.GetCurrentDirectory() + @"\Components\LiveSplit.BugFables-log.txt";
InitSplits();
LogToFile("STARTED");
}
private void LogToFile(string message)
{
try
{
if (!File.Exists(pathLogFile))
{
var fs = File.Create(pathLogFile);
fs.Close();
File.SetCreationTimeUtc(pathLogFile, DateTime.UtcNow);
}
else
{
if (DateTime.UtcNow - File.GetCreationTimeUtc(pathLogFile) > TimeSpan.FromDays(30))
{
File.Delete(pathLogFile);
var fs = File.Create(pathLogFile);
fs.Close();
File.SetCreationTimeUtc(pathLogFile, DateTime.UtcNow);
}
}
string currentTimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
using StreamWriter file = new StreamWriter(pathLogFile, append: true);
file.WriteLine("[" + currentTimeStamp + "] " + message);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private void InitSplits()
{
splits = settings.GetSplitsGlitchless();
}
public bool ShouldStart()
{
byte[] flags;
bool inEvent = false;
int lastEvent = -1;
try
{
if (!gameMemory.ReadFlags(out flags))
{
if (!lastReadWasBad)
LogToFile("ShouldStart: Couldn't read the flags");
lastReadWasBad = true;
return false;
}
if (!gameMemory.ReadInEvent(out inEvent))
{
if (!lastReadWasBad)
LogToFile("ShouldStart: Couldn't read inevent");
lastReadWasBad = true;
return false;
}
if (!gameMemory.ReadLastEvent(out lastEvent))
{
if (!lastReadWasBad)
LogToFile("ShouldStart: Couldn't read lastevent");
lastReadWasBad = true;
return false;
}
}
catch (Exception ex)
{
if (!lastReadWasBad)
LogToFile("ShouldStart: Unhandled exception while reading memory: " + ex.Message);
lastReadWasBad = true;
return false;
}
if (lastReadWasBad)
LogToFile("ShouldStart: Reestablished memory reads");
lastReadWasBad = false;
bool newNewGameStarted = BitConverter.ToBoolean(flags, (int)GameEnums.Flag.NewGameStarted);
bool shouldStart = (newNewGameStarted && !oldNewGameStarted);
if (newNewGameStarted != oldNewGameStarted)
LogToFile("ShouldStart: NewGameStarted was " + oldNewGameStarted + " and now " + newNewGameStarted);
oldNewGameStarted = newNewGameStarted;
// Having the new game started flag being toggled to true isn't enough: we need to make sure the value was
// changed as part of being in the new game event because it's possible it was set as part of the save load event.
// It also improves the reliability since this check can be destructive if a false positive happen
bool willStart = shouldStart && inEvent && lastEvent == newGameEventId;
if (willStart)
{
LogToFile("ShouldStart: Decided to start");
LogToFile($"ShouldStart: Mode: {settings.Mode}");
}
return willStart;
}
public bool ShouldSplit(int currentSplitIndex, int currentRunSplitsCount)
{
if (settings.Mode != SettingsUserControl.AutoSplitterMode.StartEndOnly &&
currentSplitIndex != currentRunSplitsCount - 1)
{
return ShouldMidSplit(currentSplitIndex);
}
return ShouldEnd();
}
private bool ShouldMidSplit(int currentSplitIndex)
{
if (splits.Length - 1 < currentSplitIndex)
return false;
int currentRoomId;
byte[] flags;
byte[] enemyEncounter;
long battlePtr;
try
{
if (!gameMemory.ReadCurrentRoomId(out currentRoomId))
return false;
if (!gameMemory.ReadFlags(out flags))
return false;
if (!gameMemory.ReadEnemyEncounter(out enemyEncounter))
return false;
if (!gameMemory.ReadBattlePtr(out battlePtr))
return false;
}
catch (Exception)
{
return false;
}
if (oldEnemyEncounter == null)
{
oldEnemyEncounter = new byte[GameMemory.nbrBytesEnemyEncounter];
enemyEncounter.CopyTo(oldEnemyEncounter, 0);
}
Split split = splits[currentSplitIndex];
if (!MidSplitRoomCheck(split, currentRoomId))
{
enemyEncounter.CopyTo(oldEnemyEncounter, 0);
return false;
}
if (!MidSplitFlagsCheck(split, flags))
{
enemyEncounter.CopyTo(oldEnemyEncounter, 0);
return false;
}
if (!MidSplitEnemyDefeatedCheck(split, enemyEncounter, battlePtr))
return false;
enemyEncounter.CopyTo(oldEnemyEncounter, 0);
LogToFile($"ShouldMidSplit: Decided to split on {split.name} in {split.group}");
return true;
}
private bool MidSplitRoomCheck(Split split, int currentRoomId)
{
return (split.requiredRoom == GameEnums.Room.UNASSIGNED ||
currentRoomId == (int)split.requiredRoom);
}
private bool MidSplitFlagsCheck(Split split, byte[] flags)
{
if (split.requiredFlags != null && split.requiredFlags.Length != 0)
{
bool allFlagsTrue = true;
foreach (var requiredFlag in split.requiredFlags)
{
if (!BitConverter.ToBoolean(flags, (int)requiredFlag))
{
allFlagsTrue = false;
break;
}
}
return allFlagsTrue;
}
return true;
}
private bool MidSplitEnemyDefeatedCheck(Split split, byte[] enemyEncounter, long battlePtr)
{
if (split.requiredEnemiesDefeated != null && split.requiredEnemiesDefeated.Length != 0)
{
if (battlePtr != 0)
return false;
bool allEnemiesDefeated = true;
foreach (var requiredEnemy in split.requiredEnemiesDefeated)
{
int newNbrDefeated = gameMemory.GetNbrDefeatedForEnemyId(enemyEncounter, requiredEnemy);
int oldNbrDefeated = gameMemory.GetNbrDefeatedForEnemyId(oldEnemyEncounter, requiredEnemy);
if (newNbrDefeated <= oldNbrDefeated)
{
allEnemiesDefeated = false;
break;
}
}
return allEnemiesDefeated;
}
return true;
}
private bool ShouldEnd()
{
int currentSong;
long musicCoroutine;
int currentRoomId;
try
{
if (!gameMemory.ReadFirstMusicId(out currentSong))
{
if (!lastReadWasBad)
LogToFile("ShouldEnd: Couldn't read the first music id");
lastReadWasBad = true;
return false;
}
if (!gameMemory.ReadMusicCoroutineInProgress(out musicCoroutine))
{
if (!lastReadWasBad)
LogToFile("ShouldEnd: Couldn't read the music coroutine");
lastReadWasBad = true;
return false;
}
if (!gameMemory.ReadCurrentRoomId(out currentRoomId))
{
if (!lastReadWasBad)
LogToFile("ShouldEnd: Couldn't read the current room id");
lastReadWasBad = true;
return false;
}
}
catch (Exception ex)
{
if (!lastReadWasBad)
LogToFile("ShouldEnd: Unhandled exception while reading memory: " + ex.Message);
lastReadWasBad = true;
return false;
}
if (lastReadWasBad)
LogToFile("ShouldEnd: Reestablished memory reads");
lastReadWasBad = false;
if (currentEndTimeState == EndTimeState.NotArrivedYet)
{
if (currentRoomId == (int)GameEnums.Room.BugariaEndThrone)
{
currentEndTimeState = EndTimeState.ArrivedInRoom;
LogToFile("ShouldEnd: Just arrived in the room");
}
}
else
{
bool newMusicCouroutineInProgress = (musicCoroutine != 0);
if (currentEndTimeState == EndTimeState.ArrivedInRoom && currentSong == (int)GameEnums.Song.LevelUp)
{
currentEndTimeState = EndTimeState.SongLevelUpStarting;
LogToFile("ShouldEnd: The level up song is starting");
}
else if (currentEndTimeState == EndTimeState.SongLevelUpStarting && !newMusicCouroutineInProgress)
{
currentEndTimeState = EndTimeState.SongLevelIsPlaying;
LogToFile("ShouldEnd: The level up song is playing");
}
else if (currentEndTimeState == EndTimeState.SongLevelIsPlaying && newMusicCouroutineInProgress)
{
currentEndTimeState = EndTimeState.SongIsFading;
LogToFile("ShouldEnd: The level up song is fading");
}
else if (currentEndTimeState == EndTimeState.SongIsFading && !newMusicCouroutineInProgress)
{
currentEndTimeState = EndTimeState.NotArrivedYet;
LogToFile("ShouldEnd: The level up song is done fading, decided to end");
return true;
}
}
return false;
}
public void ResetLogic()
{
oldEnemyEncounter = null;
currentEndTimeState = EndTimeState.NotArrivedYet;
oldNewGameStarted = true;
InitSplits();
LogToFile("LOGIC RESET");
}
}
}