-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveSystem.cs
More file actions
314 lines (274 loc) · 10.1 KB
/
SaveSystem.cs
File metadata and controls
314 lines (274 loc) · 10.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
using Godot;
using System.Collections.Generic;
using Saveable.Extensions;
namespace Saveable;
/// <summary>
/// A simple save system.
/// <list type="bullet|number|table">
/// <item>
/// <term>ISaveable</term>
/// <description>An interface to save and load a node</description>
/// </item>
/// <item>
/// <term>NodeSave</term>
/// <description>Store a collections of properties (<see cref="string"/> as key, <see cref="object"/> as value).</description>
/// </item>
/// <item>
/// <term>TreeSave</term>
/// <description>Stores a collections of <see cref="NodeSave"/>.</description>
/// </item>
/// </list>
/// <remarks>This system uses JSON (Newtonsoft.Json) for serialization.</remarks>
/// </summary>
public static class SaveSystem
{
/// <summary>
/// Loads a save file.
/// </summary>
/// <param name="filePath">Where the save file is located</param>
/// <param name="root">A node to search through the tree</param>
/// <param name="loadTree">If the tree should be loaded. If false, only the <see cref="NodeSave"/> will be created.</param>
/// <return>A <see cref="TreeSave"/>, which contains a collection of <see cref="NodeSave"/></return>
public static TreeSave? LoadFile(string filePath, Node root, bool loadTree = true)
{
DirAccess.MakeDirRecursiveAbsolute(filePath.GetBaseDir());
FileAccess file = FileAccess.Open(filePath, FileAccess.ModeFlags.Read);
return LoadFile(file, root, loadTree);
}
/// <summary>
/// Loads a save file.
/// </summary>
/// <param name="filePath">Where the save file is located</param>
/// <param name="root">A node to search through the tree</param>
/// <param name="compressionMode">The compression mode</param>
/// <param name="loadTree">If the tree should be loaded. If false, only the <see cref="NodeSave"/> will be created.</param>
/// <return>A <see cref="TreeSave"/>, which contains a collection of <see cref="NodeSave"/></return>
/// <seealso cref="FileAccess.CompressionMode"/>
public static TreeSave? LoadFile(string filePath, Node root, FileAccess.CompressionMode compressionMode, bool loadTree = true)
{
DirAccess.MakeDirRecursiveAbsolute(filePath.GetBaseDir());
FileAccess file = FileAccess.OpenCompressed(
filePath,
FileAccess.ModeFlags.Read,
compressionMode
);
return LoadFile(file, root, loadTree);
}
/// <summary>
/// Loads a save file.
/// </summary>
/// <param name="filePath">Where the save file is located</param>
/// <param name="key">A key to decrypt the file</param>
/// <param name="root">A node to search through the tree</param>
/// <param name="loadTree">If the tree should be loaded. If false, only the <see cref="NodeSave"/> will be created.</param>
/// <return>A <see cref="TreeSave"/>, which contains a collection of <see cref="NodeSave"/></return>
public static TreeSave? LoadFile(string filePath, byte[] key, Node root, bool loadTree = true)
{
DirAccess.MakeDirRecursiveAbsolute(filePath.GetBaseDir());
FileAccess file = FileAccess.OpenEncrypted(
filePath,
FileAccess.ModeFlags.Read,
key
);
return LoadFile(file, root, loadTree);
}
/// <summary>
/// Loads a save file.
/// </summary>
/// <param name="filePath">Where the save file is located</param>
/// <param name="password">Password to decrypt the file</param>
/// <param name="root">A node to search through the tree</param>
/// <param name="loadTree">If the tree should be loaded. If false, only the <see cref="NodeSave"/> will be created.</param>
/// <return>A <see cref="TreeSave"/>, which contains a collection of <see cref="NodeSave"/></return>
public static TreeSave? LoadFile(string filePath, string password, Node root, bool loadTree = true)
{
DirAccess.MakeDirRecursiveAbsolute(filePath.GetBaseDir());
FileAccess file = FileAccess.OpenEncryptedWithPass(
filePath,
FileAccess.ModeFlags.Read,
password
);
return LoadFile(file, root, loadTree);
}
/// <summary>
/// Saves a save file.
/// </summary>
/// <param name="filePath">Where to save the file to</param>
/// <param name="root">A node to search through the tree</param>
/// <param name="treeSave">A <see cref="TreeSave"/> to save</param>
public static void SaveFile(
string filePath,
Node root,
TreeSave? treeSave = null)
{
DirAccess.MakeDirRecursiveAbsolute(filePath.GetBaseDir());
FileAccess file = FileAccess.Open(filePath, FileAccess.ModeFlags.Write);
SaveFile(file, root, treeSave);
}
/// <summary>
/// Saves a save file.
/// <para>NOTE; This will overwrite existing save file, if tree save is null.</para>
/// </summary>
/// <param name="filePath">Where to save the file to</param>
/// <param name="root">A node to search through the tree</param>
/// <param name="compressionMode">The compression mode</param>
/// <param name="treeSave">A <see cref="TreeSave"/> to save</param>
public static void SaveFile(
string filePath,
Node root,
FileAccess.CompressionMode compressionMode,
TreeSave? treeSave = null)
{
DirAccess.MakeDirRecursiveAbsolute(filePath.GetBaseDir());
FileAccess file = FileAccess.OpenCompressed(
filePath,
FileAccess.ModeFlags.Write,
compressionMode
);
SaveFile(file, root, treeSave);
}
/// <summary>
/// Saves a save file.
/// </summary>
/// <param name="filePath">Where to save the file to</param>
/// <param name="key">A key to decrypt the file</param>
/// <param name="root">A node to search through the tree</param>
/// <param name="treeSave">A <see cref="TreeSave"/> to save</param>
public static void SaveFile(
string filePath,
byte[] key,
Node root,
TreeSave? treeSave = null)
{
DirAccess.MakeDirRecursiveAbsolute(filePath.GetBaseDir());
FileAccess file = FileAccess.OpenEncrypted(
filePath,
FileAccess.ModeFlags.Write,
key
);
SaveFile(file, root, treeSave);
}
/// <summary>
/// Saves a save file.
/// </summary>
/// <param name="filePath">Where to save the file to</param>
/// <param name="password">Password to encrypt the file</param>
/// <param name="root">A node to search through the tree</param>
/// <param name="treeSave">A <see cref="TreeSave"/> to save</param>
public static void SaveFile(
string filePath,
string password,
Node root,
TreeSave? treeSave = null)
{
DirAccess.MakeDirRecursiveAbsolute(filePath.GetBaseDir());
FileAccess file = FileAccess.OpenEncryptedWithPass(
filePath,
FileAccess.ModeFlags.Write,
password
);
SaveFile(file, root, treeSave);
}
private static TreeSave? LoadFile(FileAccess file, Node root, bool loadTree = true)
{
if (file == null)
{
GD.PushWarning("File cannot be loaded!");
return TreeSave.Empty;
}
if (root == null)
{
GD.PushWarning("Cannot load file without a root node!");
return TreeSave.Empty;
}
string json = file.GetAsText();
file.Close();
if (string.IsNullOrEmpty(json))
{
GD.PushWarning("File is empty!");
return TreeSave.Empty;
}
var save = SaveExtension.DeserializeObject<TreeSave>(json);
if (loadTree)
LoadTree(root, save);
#if TOOLS
GD.Print($"File loaded!\n\nFound {save?.NumOfNodes} nodes.\nFound {save?.NumOfProperties} properties.");
#endif
return save;
}
private static void LoadTree(Node root, TreeSave? treeSave)
{
if (treeSave == null)
return;
RunInChildrenRecursive(root, (ISaveable obj) =>
{
if (treeSave.TryGetCollection(obj.UniqueID, out NodeSave? nodeSave))
obj.Load(nodeSave!);
});
}
private static void SaveFile(
FileAccess file,
Node root,
TreeSave? treeSave = null)
{
if (file == null)
{
GD.PushWarning("File cannot be saved!");
return;
}
if (root == null)
{
GD.PushWarning("Cannot save a file without a root node!");
return;
}
TreeSave save = SaveTree(root);
if (treeSave != null)
{
foreach (KeyValuePair<string, NodeSave> collections in treeSave!.Collections)
save.TryAddCollection(collections.Key, collections.Value);
}
string json = SaveExtension.SerializeObject(save);
file.StoreString(json);
#if TOOLS
GD.Print($"\nFile saved! Path located at: {file.GetPath()}.\n\nFound {save.NumOfNodes} nodes.\nFound {save.NumOfProperties} properties.");
#endif
file.Close();
}
private static TreeSave SaveTree(Node root)
{
var tree = new TreeSave();
RunInChildrenRecursive(root, (ISaveable obj) =>
{
var node = new NodeSave();
obj.Save(node);
tree.SetOrAddCollection(obj.UniqueID, node);
});
return tree;
}
private static void RunInChildrenRecursive<T>(
Node parent,
System.Action<T> action)
{
RunInChildrenRecursive(parent, (Node node) =>
{
if (node is not T t)
return;
action.Invoke(t);
});
}
private static void RunInChildrenRecursive(
Node parent,
System.Action<Node> action)
{
RunInChildren(parent);
void RunInChildren(Node parentNode)
{
foreach (Node node in parentNode.GetChildren())
{
if (node.GetChildCount() > 0)
RunInChildren(node);
action.Invoke(node);
}
}
}
}