forked from ewersp/SaveManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StandaloneSaveManager.cs
105 lines (94 loc) · 3.29 KB
/
StandaloneSaveManager.cs
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
using UnityEngine;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// The game save manager for all standalone platforms (Windows, Mac and Linux). Capable of
/// binary serialization and deserialization of generic data objects.
///
/// This manager supports adding new data to your [Serializable] classes, even if you've already
/// saved it to disk. For example, adding new persisted data to a save file post-launch.
///
/// Example Usage:
/// [Serializable]
/// public class SaveData {
/// public int ID;
/// public string Name;
/// // Etc... (safe to add new fields here, even if the file has already been serialized)
/// }
///
/// StandaloneSaveManager manager = new StandaloneSaveManager();
/// MySaveData data = manager.BinaryDeserialize<SaveData>("save.dat");
/// data.Name = "Hello, World.";
/// saveManager.BinarySerialize("save.dat", data);
/// </summary>
public class StandaloneSaveManager : MonoBehaviour, IGameSave {
/// <summary>
/// Binary serialize any generic data object.
/// </summary>
/// <param name="filename">The filename to save this data to.</param>
/// <param name="data">The data to serialize.</param>
public void BinarySerialize(string filename, object data) {
FileStream file = File.Open(GetFullPath(filename), FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
try {
bf.Serialize(file, data);
} catch (SerializationException e) {
Debug.Log("BinarySerialize failed: " + e.Message);
} finally {
file.Close();
}
}
/// <summary>
/// Binary deserialize any generic data object.
/// </summary>
/// <typeparam name="T">The class type of the deserialized data.</typeparam>
/// <param name="filename">The filename of the data to deserialize.</param>
/// <returns>The deserialized data if it existed, otherwise a default object.</returns>
public T BinaryDeserialize<T>(string filename) where T : new() {
T result = new T();
// If the file doesn't exist, return a default object
if (!FileExists(filename)) {
return result;
}
FileStream file = File.Open(GetFullPath(filename), FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
try {
result = (T)bf.Deserialize(file);
} catch (SerializationException e) {
Debug.Log("BinaryDeserialize failed: " + e.Message);
} finally {
file.Close();
}
return result;
}
/// <summary>
/// Checks if a given file exists.
/// </summary>
/// <param name="filename">The name of the file to check.</param>
/// <returns>True if the file exists, false otherwise.</returns>
public bool FileExists(string filename) {
return File.Exists(GetFullPath(filename));
}
/// <summary>
/// Delete a given file.
/// </summary>
/// <param name="filename">The name of the file to delete.</param>
public void Delete(string filename) {
File.Delete(GetFullPath(filename));
}
/// <summary>
/// Gets the full save path, including the filename.
/// </summary>
/// <param name="filename"></param>
/// <returns>The full save path.</returns>
public string GetFullPath(string filename) {
return Path.Combine(Application.persistentDataPath, filename);
}
/// <summary>
/// Persists all changes to the hardware
/// </summary>
public void WriteToDisk() {
// No action needed, automatic writes happen in BinarySerialize
}
}