-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathCheckpointManager.cs
More file actions
205 lines (169 loc) · 8.23 KB
/
Copy pathCheckpointManager.cs
File metadata and controls
205 lines (169 loc) · 8.23 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
using Godot;
using HarmonyLib;
using MegaCrit.Sts2.Core.Nodes;
using MegaCrit.Sts2.Core.Runs;
using MegaCrit.Sts2.Core.Saves;
namespace ModTemplate.ModTemplateCode.Checkpoints;
public static class CheckpointManager
{
public static int CheckpointCount => _checkpoints.Count;
// Floor → (complete run state at that floor, time it was captured)
private static readonly Dictionary<int, (SerializableRun RunSave, DateTime SavedAt)> _checkpoints = new();
private static bool _isLoadingCheckpoint;
private static string CheckpointRoot => Path.Combine(OS.GetUserDataDir(), "mod_checkpoints");
private static string ActiveDir => Path.Combine(CheckpointRoot, "active");
// ── Run lifecycle ─────────────────────────────────────────────────────────
public static void OnRunStart()
{
_checkpoints.Clear();
if (Directory.Exists(ActiveDir))
Directory.Delete(ActiveDir, recursive: true);
Directory.CreateDirectory(ActiveDir);
MainFile.Logger.Info("[Checkpoint] New run started.");
}
public static void OnRunContinue()
{
if (_isLoadingCheckpoint) return;
_checkpoints.Clear();
if (Directory.Exists(ActiveDir))
{
foreach (var dir in Directory.GetDirectories(ActiveDir))
{
var name = Path.GetFileName(dir);
if (!name.StartsWith("floor_") || !int.TryParse(name[6..], out var floor)) continue;
var checkpointFile = Path.Combine(dir, "checkpoint.json");
var metaFile = Path.Combine(dir, "meta.json");
if (!File.Exists(checkpointFile)) continue;
try
{
var result = JsonSerializationUtility.FromJson<SerializableRun>(
File.ReadAllText(checkpointFile));
if (!result.Success || result.SaveData == null) continue;
var runSave = result.SaveData;
var savedAt = File.Exists(metaFile)
? DateTime.Parse(File.ReadAllText(metaFile), null, System.Globalization.DateTimeStyles.RoundtripKind)
: Directory.GetCreationTimeUtc(dir);
_checkpoints[floor] = (runSave, savedAt);
MainFile.Logger.Info($"[Checkpoint] Restored floor {floor} from disk.");
}
catch (Exception ex)
{
MainFile.Logger.Info($"[Checkpoint] Could not restore floor {floor}: {ex.Message}");
}
}
}
MainFile.Logger.Info($"[Checkpoint] Continued run | {_checkpoints.Count} checkpoint(s).");
}
public static void OnRunEnd()
{
_checkpoints.Clear();
if (Directory.Exists(ActiveDir))
{
Directory.Delete(ActiveDir, recursive: true);
MainFile.Logger.Info("[Checkpoint] Run ended, checkpoints cleared.");
}
}
// ── Save ──────────────────────────────────────────────────────────────────
public static void Save(int floor)
{
_ = SaveAsync(floor);
}
private static async Task SaveAsync(int floor)
{
try
{
var saveManager = SaveManager.Instance;
// Wait for any in-flight save so we read the final committed state.
var pending = saveManager.CurrentRunSaveTask;
if (pending != null) await pending;
var readResult = saveManager.LoadRunSave();
if (!readResult.Success || readResult.SaveData == null)
{
MainFile.Logger.Info($"[Checkpoint] Save floor {floor}: LoadRunSave failed (status={readResult.Status}).");
return;
}
var runSave = readResult.SaveData;
bool isNew = !_checkpoints.ContainsKey(floor);
var savedAt = DateTime.UtcNow;
_checkpoints[floor] = (runSave, savedAt);
// Persist to disk so checkpoints survive game restarts.
var dir = Path.Combine(ActiveDir, $"floor_{floor:D2}");
Directory.CreateDirectory(dir);
File.WriteAllText(Path.Combine(dir, "checkpoint.json"),
JsonSerializationUtility.ToJson(runSave));
File.WriteAllText(Path.Combine(dir, "meta.json"),
savedAt.ToString("O")); // ISO 8601 round-trip format
MainFile.Logger.Info($"[Checkpoint] {(isNew ? "Saved" : "Overwrote")} floor {floor} ({_checkpoints.Count} total).");
}
catch (Exception ex)
{
MainFile.Logger.Info($"[Checkpoint] SaveAsync error: {ex}");
}
}
// ── Enumerate ─────────────────────────────────────────────────────────────
public static List<RunCheckpoint> LoadAll() =>
[.. _checkpoints
.Select(kvp => new RunCheckpoint { Floor = kvp.Key, SavedAt = kvp.Value.SavedAt })
.OrderByDescending(s => s.Floor)];
// ── Load ──────────────────────────────────────────────────────────────────
public static void LoadCheckpoint(RunCheckpoint checkpoint)
{
if (!_checkpoints.TryGetValue(checkpoint.Floor, out var entry))
{
MainFile.Logger.Info($"[Checkpoint] Load floor {checkpoint.Floor}: not found in memory.");
return;
}
MainFile.Logger.Info($"[Checkpoint] Starting load for floor {checkpoint.Floor}.");
_ = LoadCheckpointAsync(checkpoint.Floor, entry.RunSave);
}
private static async Task LoadCheckpointAsync(int floor, SerializableRun runSave)
{
try
{
var game = NGame.Instance;
var saveManager = SaveManager.Instance;
var runManager = RunManager.Instance;
if (game == null || saveManager == null || runManager == null)
{
MainFile.Logger.Info("[Checkpoint] LoadAsync: required instances not available.");
return;
}
// Wait for any pending save so we don't race against an in-flight write.
var pending = saveManager.CurrentRunSaveTask;
if (pending != null)
{
MainFile.Logger.Info("[Checkpoint] LoadAsync: waiting for pending save...");
await pending;
}
runManager.ActionExecutor.Cancel();
runManager.ActionQueueSet.Reset();
// Reconstruct run state directly from the captured object — no file I/O needed.
RunState runState = RunState.FromSerializable(runSave);
var transition = game.Transition;
if (transition != null) await transition.FadeOut();
runManager.CleanUp();
// Method name changed capitalisation between game versions.
var setupMethod =
AccessTools.Method(typeof(RunManager), "SetUpSavedSingleplayer", [typeof(RunState), typeof(SerializableRun)]) ??
AccessTools.Method(typeof(RunManager), "SetUpSavedSinglePlayer", [typeof(RunState), typeof(SerializableRun)]);
if (setupMethod != null)
{
var result = setupMethod.Invoke(runManager, [runState, runSave]);
if (result is Task t) await t;
}
else
{
MainFile.Logger.Info("[Checkpoint] LoadAsync: SetUpSavedSingleplayer not found.");
}
_isLoadingCheckpoint = true;
try { await game.LoadRun(runState, runSave.PreFinishedRoom); }
finally { _isLoadingCheckpoint = false; }
if (transition != null) await transition.FadeIn();
MainFile.Logger.Info($"[Checkpoint] Load complete for floor {floor}.");
}
catch (Exception ex)
{
MainFile.Logger.Info($"[Checkpoint] LoadAsync error: {ex}");
}
}
}