forked from ChemiKhazi/UnityToolbag
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGameSaveSystem.cs
More file actions
349 lines (300 loc) · 13.3 KB
/
GameSaveSystem.cs
File metadata and controls
349 lines (300 loc) · 13.3 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
using UnityEngine;
using System;
using System.Collections.Generic;
using System.IO;
namespace UnityToolbag
{
/// <summary>
/// A game save system that handles file management and save file caching.
/// </summary>
public static class GameSaveSystem
{
private static GameSaveSystemSettings _settings;
private static string _fileSaveLocation;
/// <summary>
/// Gets a value indicating whether or not the GameSaveSystem has been initialized.
/// </summary>
public static bool isInitialized { get; private set; }
/// <summary>
/// Gets the folder where the game saves are stored;
/// </summary>
public static string saveLocation
{
get
{
ThrowIfNotInitialized();
return _fileSaveLocation;
}
}
/// <summary>
/// Initializes the system with the provided settings.
/// </summary>
/// <param name="settings">The settings to configure the system with.</param>
public static void Initialize(GameSaveSystemSettings settings)
{
if (isInitialized) {
throw new InvalidOperationException("GameSaveSystem is already initialized. Cannot initialize again!");
}
// Validate our input
if (settings == null) {
throw new ArgumentNullException("settings");
}
if (string.IsNullOrEmpty(settings.gameName)) {
throw new ArgumentException("The gameName in the settings must be a non-empty string");
}
if (settings.gameName.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1) {
throw new ArgumentException("The gameName in the settings contains illegal characters");
}
if (settings.useRollingBackups && settings.backupCount <= 0) {
throw new ArgumentException("useRollingBackups is true but backupCount isn't a positive value");
}
// Copy the settings locally so we can retain a new instance (so it can't be changed out from under us)
_settings = settings.Clone();
// Find the base path for where we want to save game saves. Unity's persistentDataPath is generally unacceptable
// to me. In Windows it uses some AppData folder, in OS X it (quite incorrectly) puts saves into a Cache folder,
// and I have no idea what they use on Linux but I'm assuming it's not what I want.
#if UNITY_STANDALONE_WIN
_fileSaveLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "My Games");
#elif UNITY_STANDALONE_OSX
_fileSaveLocation = Path.Combine(Environment.GetEnvironmentVariable("HOME"), "Library/Application Support");
#elif UNITY_STANDALONE_LINUX
string home = Environment.GetEnvironmentVariable("XDG_DATA_HOME");
if (string.IsNullOrEmpty(home)) {
home = Environment.GetEnvironmentVariable("HOME");
}
_fileSaveLocation = Path.Combine(home, ".local/share");
#else
// For any non Mac/Windows/Linux platform, we'll default back to the persistentDataPath since we know it should work.
_fileSaveLocation = Application.persistentDataPath;
#endif
// Do final cleanup on the path if we're on a desktop platform
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
// Company name is optional so we check before appending it
if (!string.IsNullOrEmpty(_settings.companyName)) {
_fileSaveLocation = Path.Combine(_fileSaveLocation, _settings.companyName);
}
_fileSaveLocation = Path.Combine(_fileSaveLocation, _settings.gameName);
_fileSaveLocation = Path.GetFullPath(_fileSaveLocation);
#endif
// Ensure the directory for saves exists.
Directory.CreateDirectory(_fileSaveLocation);
isInitialized = true;
}
/// <summary>
/// Loads a game save.
/// </summary>
/// <typeparam name="TGameSave">The game save class type.</typeparam>
/// <param name="name">The name of the gamesave to load.</param>
/// <param name="forceFromDisk">
/// If <c>true</c> the save will always be read from disk. If <c>false</c> the system will return a cached instance if available.
/// </param>
/// <returns>An <see cref="IFuture<GameSaveLoadResult<TGameSave>>" /> that can be used to track and examine the load results.</returns>
public static IFuture<GameSaveLoadResult<TGameSave>> Load<TGameSave>(string name, bool forceFromDisk = false)
where TGameSave : class, IGameSave, new()
{
ThrowIfNotInitialized();
Future<GameSaveLoadResult<TGameSave>> future = new Future<GameSaveLoadResult<TGameSave>>();
TGameSave save = null;
if (GameSaveCache<TGameSave>.TryGetSave(name, out save) && !forceFromDisk) {
future.Assign(new GameSaveLoadResult<TGameSave>(save, true, false));
}
else {
if (save == null) {
save = new TGameSave();
GameSaveCache<TGameSave>.Set(name, save);
}
else {
save.Reset();
}
future.Process(() =>
{
bool usedBackup = false;
if (_settings.useRollingBackups) {
usedBackup = DoLoadWithBackups(save, name);
}
else {
using (var stream = File.OpenRead(GetGameSavePath(name))) {
save.Load(stream);
}
}
return new GameSaveLoadResult<TGameSave>(save, false, usedBackup);
});
}
return future;
}
/// <summary>
/// Saves a game save.
/// </summary>
/// <typeparam name="TGameSave">The game save class type.</typeparam>
/// <param name="name">The name of the gamesave to load.</param>
/// <param name="save">The game save to save.</param>
/// <returns>An <see cref="IFuture{bool}"/> that can be used to track completion of the save operation.</returns>
public static IFuture<bool> Save<TGameSave>(string name, TGameSave save)
where TGameSave : class, IGameSave, new()
{
ThrowIfNotInitialized();
GameSaveCache<TGameSave>.Set(name, save);
return new Future<bool>().Process(() =>
{
if (_settings.useRollingBackups) {
DoSaveWithBackups(save, name);
}
else {
using (var stream = File.Create(GetGameSavePath(name))) {
save.Save(stream);
}
}
return true;
});
}
private static bool DoLoadWithBackups(IGameSave save, string name)
{
string mainSavePath = GetGameSavePath(name);
// Try loading the regular save. Most times this should work fine.
try {
using (var stream = File.OpenRead(mainSavePath)) {
save.Load(stream);
}
// If Load didn't throw, we're good to go and can return that we
// didn't need to load a backup save.
return false;
}
catch {
save.Reset();
}
// We go through and try loading all of the available backups
var foundGoodSave = false;
var backupIndex = 0;
for (backupIndex = 0; !foundGoodSave && backupIndex < _settings.backupCount; backupIndex++) {
// Try loading the file.
try {
var path = GetBackupSavePath(name, backupIndex);
using (var stream = File.OpenRead(path)) {
save.Load(stream);
}
foundGoodSave = true;
// break so we don't increment the backupIndex again
break;
}
catch {
save.Reset();
}
}
// At this point we either know that A) we loaded a backup successfully or B) all the saves are bad.
// So we need to clean up our saves to get rid of the bad ones.
// We know the main save failed so we can delete that
if (File.Exists(mainSavePath)) {
File.Delete(mainSavePath);
}
// Delete all backups newer than the one we were able to load. This might delete all of them if no saves were good.
for (int i = backupIndex - 1; i >= 0; i--) {
var path = GetBackupSavePath(name, i);
if (File.Exists(path)) {
File.Delete(path);
}
}
if (foundGoodSave) {
// If we did find a save, make that save our main save
Debug.Log("Moving backup " + GetBackupSavePath(name, backupIndex) + " to " + mainSavePath);
MoveFile(GetBackupSavePath(name, backupIndex), mainSavePath);
// Move up the remaining backups
for (int i = backupIndex; i <= _settings.backupCount; i++) {
var path1 = GetBackupSavePath(name, i);
if (File.Exists(path1)) {
File.Delete(path1);
}
if (i < _settings.backupCount) {
var path2 = GetBackupSavePath(name, i + 1);
if (File.Exists(path2)) {
MoveFile(path2, path1);
}
}
}
}
// If we didn't find any good saves, throw an exception so the future will receive the error and
// games can choose how to handle it.
if (!foundGoodSave) {
throw new FileNotFoundException("No game save found with name '" + name + "'");
}
// Return true because if we got here we know we used a backup file
return true;
}
private static void MoveFile(string src, string dst)
{
#if UNITY_WEBPLAYER
File.Copy(src, dst);
File.Delete(src);
#else
File.Move(src, dst);
#endif
}
private static void DoSaveWithBackups(IGameSave save, string name)
{
var mainSavePath = GetGameSavePath(name);
var tempPath = mainSavePath + ".temp";
// Start by attempting the save to a temp file
try {
using (var stream = File.Create(tempPath)) {
save.Save(stream);
}
}
catch {
// Remove the temp file before leaving scope if saving threw an exception
try {
if (File.Exists(tempPath)) {
File.Delete(tempPath);
}
}
catch { }
// Let the exception continue up
throw;
}
// Saving succeeded so we need to move from the temp path to the main path.
// First up we shift down all the backup files
for (int i = _settings.backupCount - 2; i >= 0; i--) {
var path = GetBackupSavePath(name, i);
if (File.Exists(path)) {
var nextPath = GetBackupSavePath(name, i + 1);
if (File.Exists(nextPath)) {
File.Delete(nextPath);
}
MoveFile(path, nextPath);
}
}
// Then move the current main save into the first backup slot
if (File.Exists(mainSavePath)) {
MoveFile(mainSavePath, GetBackupSavePath(name, 0));
}
// Then we can just move the new file into place
MoveFile(tempPath, mainSavePath);
}
private static void ThrowIfNotInitialized()
{
if (!isInitialized) {
throw new InvalidOperationException("You must call Initialize first!");
}
}
private static string GetGameSavePath(string name)
{
return Path.Combine(_fileSaveLocation, name + ".save");
}
private static string GetBackupSavePath(string name, int index)
{
return GetGameSavePath(name) + ".backup" + (index + 1);
}
// Static generic class for caching individual types of game saves.
private static class GameSaveCache<TGameSave>
where TGameSave : class, IGameSave, new()
{
private static readonly Dictionary<string, TGameSave> _cache = new Dictionary<string, TGameSave>();
public static bool TryGetSave(string name, out TGameSave save)
{
return _cache.TryGetValue(name, out save);
}
public static void Set(string name, TGameSave save)
{
_cache[name] = save;
}
}
}
}