forked from SpectralPlatypus/TackleboxDebug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveState.cs
More file actions
201 lines (167 loc) · 6.83 KB
/
Copy pathSaveState.cs
File metadata and controls
201 lines (167 loc) · 6.83 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
using System.Collections;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.Rendering;
namespace TackleboxDbg
{
class SaveStateManager
{
private readonly string saveStatesPath = Application.persistentDataPath + "/SaveStates";
private MainMenu MainMenu => Manager.GetMainMenu();
private PlayerMachine Player => Manager.GetPlayerMachine();
private SaveManager SaveManager => Manager.GetSaveManager();
private PlayerCamera PlayerCamera => Manager.GetPlayerCamera();
public bool IsInGame =>
MainMenu._currentState == MainMenu._inGameState ||
MainMenu._currentState == MainMenu._gameplayState;
public int CurrentIndex;
private string[] SaveStateFiles =>
[.. Directory.GetFiles(saveStatesPath).Where(x => x.EndsWith(".json"))];
public string[] SaveStateNames =>
[.. SaveStateFiles.Select(Path.GetFileNameWithoutExtension)];
private SaveStateData CurrentData => new()
{
playerPos = Player._position,
lookDir = PlayerCamera._lookDirection,
facingDir = Player._currentFacingDirection,
health = Player._currentHealth,
safeMortarIsland = GetMortarIslandMook()._currentState == MortarDesertMook._states.Dead,
timeOfDay = FiletRenderPipeline.GetConfig()._currentDayNightConfig._timeOfDay,
SaveData = SaveManager._currentSaveData
};
private MortarDesertMook GetMortarIslandMook()
{
return GameObject.FindObjectsByType<MortarDesertMook>(FindObjectsInactive.Include, FindObjectsSortMode.None)
.Where(x => !x._canRespawn)
.First();
}
public SaveStateManager()
{
Directory.CreateDirectory(saveStatesPath);
}
public void SaveCurrentDataToNewFile()
{
if (!IsInGame) return;
int i = 1;
string name = "SaveState";
while(File.Exists($"{saveStatesPath}/{name}.json"))
{
i++;
name = "SaveState " + i.ToString();
}
CurrentData.WriteToFile($"{saveStatesPath}/{name}.json");
SetCurrentIndexByName(name);
}
public void SaveCurrentDataToQuickSlot()
{
string path = saveStatesPath + "/(quickslot).json";
CurrentData.WriteToFile(path);
SetCurrentIndexByName("(quickslot)");
}
public void LoadCurrent()
{
string path = Directory.GetFiles(saveStatesPath).ElementAt(CurrentIndex);
if (IsInGame)
Manager._instance.StartCoroutine(LoadSaveStateRoutine(path));
}
public void DeleteCurrent()
{
File.Delete(SaveStateFiles[CurrentIndex]);
if(CurrentIndex == SaveStateFiles.Length) CurrentIndex--;
}
public void OpenSaveStateFolder()
{
Process.Start(saveStatesPath);
}
public void LoadCurrentNonSaveData()
{
SaveStateData data = new(SaveStateFiles[CurrentIndex]);
Manager._instance.StartCoroutine(LoadStateDataRoutine(data));
}
private void SetCurrentIndexByName(string name)
{
int i = Array.IndexOf(SaveStateNames, name);
if(i != -1) CurrentIndex = i;
}
private IEnumerator LoadSaveStateRoutine(string path)
{
if (!File.Exists(path)) yield break;
SaveStateData data = new(path);
SaveManager.PrepareGameState(data.SaveData);
MainMenu.LoadGameScene();
yield return new WaitUntil(() => IsInGame);
// The game doesn't load the checkpoint rods properly, so I'm doing it manually here.
ZipRing[] zipList = GameObject.FindObjectsByType<ZipRing>(FindObjectsInactive.Include, FindObjectsSortMode.None);
Dictionary<Guid, object> varRefs = data.SaveData._variableReferences;
foreach (ZipRing zip in zipList)
{
BoolVariableReference actRef = zip._activatedReference;
if (actRef is null || !varRefs.ContainsKey(actRef._guid)) continue;
if ((bool)varRefs[actRef._guid]) zip.Activate(true);
else zip.Deactivate(true);
}
yield return LoadStateDataRoutine(data, true);
}
private IEnumerator LoadStateDataRoutine(SaveStateData data, bool waitToSetHealth = false)
{
if(data.playerPos != Vector3.zero)
Player.Teleport(data.playerPos);
if(data.lookDir != Vector3.zero)
PlayerCamera.SetLookDirection(data.lookDir);
if(data.facingDir != Vector3.zero)
{
Player._targetFacingDirection = data.facingDir;
Player._currentFacingDirection = data.facingDir;
}
if(0 <= data.timeOfDay && data.timeOfDay <= 1)
FiletRenderPipeline.GetConfig()._currentDayNightConfig._timeOfDay = data.timeOfDay;
MortarDesertMook mook = GetMortarIslandMook();
if(data.safeMortarIsland) mook.Kill(Vector3.zero);
else mook._headAgent.Respawn();
if(waitToSetHealth) yield return new WaitUntil(() => Player._currentHealth == 3);
if (0 < data.health && data.health < 3) Player.SetCurrentHealth(data.health);
}
}
[Serializable]
public class SaveStateData
{
public Vector3 playerPos;
public Vector3 lookDir;
public Vector3 facingDir;
public int health;
public bool safeMortarIsland;
public float timeOfDay = -1;
[SerializeField]
private string SaveDataString;
public SaveData SaveData
{
get
{
SaveData data = new();
JsonUtility.FromJsonOverwrite(SaveDataString, data);
// May want to use a slot that is either in use or not in use, depending on how we want to implement it.
// Just going to make a temporary save for now.
data.name = "tempSave";
return data;
}
set
{
SaveDataString = JsonUtility.ToJson(value);
}
}
public SaveStateData() { }
/// <summary>
/// Creates a new SaveStateData from an already existing file.
/// </summary>
/// <param name="path">The path of the .json file to read from.</param>
public SaveStateData(string path)
{
if (!File.Exists(path)) return;
JsonUtility.FromJsonOverwrite(File.ReadAllText(path), this);
}
public void WriteToFile(string path)
{
File.WriteAllText(path, JsonUtility.ToJson(this, true));
}
}
}