-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSaveStateManager.cs
More file actions
411 lines (353 loc) · 14.1 KB
/
Copy pathSaveStateManager.cs
File metadata and controls
411 lines (353 loc) · 14.1 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MiniDebug.Util;
using Modding;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using USceneManager = UnityEngine.SceneManagement.SceneManager;
namespace MiniDebug
{
public class SaveStateManager : MonoBehaviour
{
private const int STATE_COUNT = 10;
private MenuAction _currentMenu = MenuAction.None;
private List<string> allStates = new(), curSelection = new();
private string query = "";
private int selector;
private string lastSaveState = "quicksave";
private static PlayerData PD
{
get => PlayerData.instance;
set => PlayerData.instance = value;
}
private static HeroController HC => HeroController.instance;
private static GameManager GM => GameManager.instance;
private static readonly MiniDebug MD = MiniDebug.Instance;
public void LoadStateNames()
{
allStates.Clear();
curSelection.Clear();
query = "";
selector = 0;
allStates.AddRange(Directory.GetFiles($"{Application.persistentDataPath}/MiniDebug Savestates/", "*.json", SearchOption.TopDirectoryOnly)
.Select(Path.GetFileNameWithoutExtension));
allStates.Sort();
curSelection = allStates;
}
public void LoadSaveState(bool duped)
{
_currentMenu = _currentMenu == MenuAction.None
? duped
? MenuAction.LoadStateDuped
: MenuAction.LoadState
: MenuAction.None;
if (_currentMenu == MenuAction.None)
return;
if (GM.IsGamePaused())
{
MD.AcceptingInput = false;
LoadStateNames();
try
{
InputHandler ih = UIManager.instance.GetField<UIManager, InputHandler>("ih");
ih.acceptingInput = false;
EventSystem.current.sendNavigationEvents = false;
}
catch (Exception e)
{
MiniDebugMod.Instance.Log("Unable to disable UI input\n" + e.Message);
}
}
else if (!string.IsNullOrEmpty(lastSaveState))
{
StartCoroutine(LoadState(_currentMenu == MenuAction.LoadStateDuped, lastSaveState));
_currentMenu = MenuAction.None;
}
else
{
_currentMenu = MenuAction.None;
}
}
private void Update()
{
if (_currentMenu == MenuAction.None || GM.GetSceneNameString() == Constants.MENU_SCENE)
{
return;
}
if (!GM.IsGamePaused())
{
MD.AcceptingInput = true;
_currentMenu = MenuAction.None;
return;
}
if (Input.GetKeyDown(KeyCode.Return))
{
if (curSelection.Count == 0)
return;
lastSaveState = curSelection[selector];
bool duped = _currentMenu == MenuAction.LoadStateDuped;
CancelMenuInput();
StartCoroutine(LoadState(duped, lastSaveState));
}
else if (Input.GetKeyDown(KeyCode.Escape))
{
CancelMenuInput();
}
else if (Input.GetKeyDown(KeyCode.Backspace))
{
if (query.Length == 0)
return;
query = query.Substring(0, query.Length - 1);
UpdateSelection();
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
selector = selector == 0 ? curSelection.Count - 1 : selector - 1;
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
selector = selector == curSelection.Count - 1 ? 0 : selector + 1;
}
else if (Input.inputString.Length > 0)
{
query += Input.inputString;
UpdateSelection();
}
}
private void OnGUI()
{
if (_currentMenu == MenuAction.None)
{
return;
}
GUIHelper.Config cfg = GUIHelper.SaveConfig();
bool wrap = GUI.skin.label.wordWrap;
var clipping = GUI.skin.label.clipping;
GUI.skin.label.wordWrap = false;
GUI.skin.label.clipping = TextClipping.Overflow;
GUI.Label(new Rect(0f, 300f, 200f, 200f), "Select a Save to Load From:");
GUI.Label(new Rect(0f, 350f, 200f, 200f), ">" + query);
int dispOffset = Math.Min(selector, Math.Max(0, curSelection.Count - STATE_COUNT));
List<string> shownStates = curSelection.Count <= STATE_COUNT
? curSelection
: curSelection.GetRange(dispOffset, STATE_COUNT);
for (int i = 0; i < shownStates.Count; i++)
{
GUI.Label(new Rect(0, 375 + 25 * i, 200, 200), $"{(i == selector - dispOffset ? '*' : ' ')}{shownStates[i]}");
}
GUIHelper.RestoreConfig(cfg);
GUI.skin.label.wordWrap = wrap;
GUI.skin.label.clipping = clipping;
}
public void SaveState(bool detailed)
{
try
{
string loc;
if (GM.IsGamePaused())
{
loc = $"{GM.GetSceneNameString()}__{DateTimeString()}";
if (File.Exists(loc + ".json"))
{
for (int i = 0; ; i++)
{
if (!File.Exists($"{loc}__{i}.json"))
{
loc = $"{loc}__{i}";
break;
}
}
}
}
else
{
loc = $"quicksave";
}
List<EnemyPosition> enemyPositions = new();
List<string> breakables = new();
if (detailed)
{
HashSet<GameObject> processed = new();
foreach (var go in FindObjectsOfType<Collider2D>().Select(c2d => c2d.gameObject))
{
if (go.LocateMyFSM("health_manager_enemy") && !processed.Contains(go))
{
processed.Add(go);
enemyPositions.Add(new EnemyPosition
{
Name = go.name,
Pos = go.transform.position
});
}
else if (!processed.Contains(go))
{
var fsm = go.LocateMyFSM("FSM");
if (fsm != null && fsm.FsmEvents.Any(e => e.Name == "BREAKABLE DEACTIVE")
&& fsm.FsmVariables.BoolVariables.First(v => v.Name == "Activated").Value)
{
processed.Add(go);
breakables.Add(go.name + go.transform.position);
}
}
}
}
SaveData data = new SaveData
{
SavePos = HC.gameObject.transform.position,
Rooms = (from i in Enumerable.Range(0, UnityEngine.SceneManagement.SceneManager.sceneCount) select UnityEngine.SceneManagement.SceneManager.GetSceneAt(i).name).ToArray<string>(),
Data = new SaveGameData(PD, SceneData.instance),
HazardRespawn = PD.hazardRespawnLocation,
EnemyPositions = EnemyPosition.serializeList(enemyPositions),
BrokenBreakables = breakables
};
data.Data.BeforeSave();
File.WriteAllText(
$"{Application.persistentDataPath}/MiniDebug Savestates/{loc}.json",
JsonUtility.ToJson(data, true)
);
lastSaveState = loc;
}
catch (Exception e)
{
MiniDebugMod.Instance.Log("Failed to save state\n" + e.Message);
}
}
private IEnumerator LoadState(bool duped, string state)
{
SaveData save;
string path = $"{Application.persistentDataPath}/MiniDebug Savestates/{state}.json";
if (File.Exists(path))
{
save = JsonUtility.FromJson<SaveData>(File.ReadAllText(path));
}
else
{
MiniDebugMod.Instance.Log($"Unable to load savestate {state}, file does not exist");
yield break;
}
save.Data.AfterLoad();
GM.ChangeToScene("Room_Sly_Storeroom", "", 0f);
yield return new WaitUntil(() => GM.GetSceneNameString() == "Room_Sly_Storeroom");
GM.ResetSemiPersistentItems();
yield return null;
PD = save.Data.playerData;
PD.hazardRespawnLocation = save.HazardRespawn;
GM.playerData = PD;
HC.playerData = PD;
HC.geoCounter.playerData = PD;
HC.GetField<HeroController, HeroAnimationController>("animCtrl").SetField("pd", PD);
SceneData.instance = GM.sceneData = save.Data.sceneData;
HC.transform.position = save.SavePos;
GM.cameraCtrl.SetField("isGameplayScene", true);
HC.TakeHealth(1);
HC.AddHealth(1);
HC.geoCounter.geoTextMesh.text = PD.geo.ToString();
HC.SetMPCharge(save.Data.playerData.MPCharge);
if (save.Data.playerData.MPCharge == 0)
{
GameCameras.instance.soulOrbFSM.SendEvent("MP LOSE");
}
yield return null;
GM.ChangeToScene(save.Rooms[0], "", 0.4f);
yield return new WaitUntil(() => GM.GetSceneNameString() == save.Rooms[0]);
if (duped)
{
for (int i = 1; i < save.Rooms.Length; i++)
{
USceneManager.LoadScene(save.Rooms[i], LoadSceneMode.Additive);
yield return null; // wait for LoadScene to complete
}
}
GM.cameraCtrl.SetMode(CameraController.CameraMode.FOLLOWING);
List<EnemyPosition> enemyPositions = EnemyPosition.deserializeList(save.EnemyPositions);
if (enemyPositions.Count > 0)
{
var gos = FindObjectsOfType<Collider2D>()
.Select(c2d => c2d.gameObject)
.Where(go => FSMUtility.LocateFSM(go, "health_manager_enemy"))
.GroupBy(go => go.name)
.ToDictionary(x => x.Key, x => x.ToList());
foreach (var epos in enemyPositions)
{
if (!gos.ContainsKey(epos.Name) || gos[epos.Name].Count == 0)
{
MiniDebugMod.Instance.Log(
$"[WARNING] Couldn't find enemy \"{epos.Name}\" after loading savestate");
continue;
}
var go = gos[epos.Name][0];
go.transform.position = epos.Pos;
gos[epos.Name].RemoveAt(0);
}
}
if (save.BrokenBreakables.Count > 0)
{
var breakables = new HashSet<string>(save.BrokenBreakables);
foreach (var go in FindObjectsOfType<Collider2D>()
.Select(c2d => c2d.gameObject)
.Where(go =>
{
var fsm = go.LocateMyFSM("FSM");
return fsm != null && fsm.FsmEvents.Any(e => e.Name == "BREAKABLE DEACTIVE");
}))
{
if (breakables.Contains(go.name + go.transform.position))
{
go.LocateMyFSM("FSM").SendEvent("BREAK");
}
}
}
}
private string DateTimeString()
{
DateTime now = DateTime.Now;
return $"{now.Year}-{now.Month}-{now.Day}_{now.Hour}-{now.Minute}-{now.Second}";
}
private void UpdateSelection()
{
curSelection = allStates.FindAll(s => s.Contains(query));
selector = 0;
}
private void CancelMenuInput()
{
MD.AcceptingInput = true;
UIManager.instance.UIClosePauseMenu();
GameCameras.instance.ResumeCameraShake();
GM.actorSnapshotUnpaused.TransitionTo(0f);
GM.isPaused = false;
GM.ui.AudioGoToGameplay(.2f);
HC.UnPause();
Time.timeScale = 1f;
_currentMenu = MenuAction.None;
try
{
InputHandler ih = UIManager.instance.GetField<UIManager, InputHandler>("ih");
ih.StartUIInput();
}
catch (Exception e)
{
MiniDebugMod.Instance.Log("Unable to restart UI input\n" + e.Message);
}
}
[Serializable]
public class SaveData
{
public Vector3 SavePos;
public string[] Rooms;
public SaveGameData Data;
public Vector3 HazardRespawn;
public string EnemyPositions;
public List<string> BrokenBreakables;
}
private enum MenuAction
{
None,
LoadState,
LoadStateDuped
}
}
}