-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPS4SaveManager
104 lines (90 loc) · 3.14 KB
/
PS4SaveManager
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
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
/// <summary>
/// This game save manager is Technical Requirements Checklist (TRC) compliant for PS4 development.
///
/// Instead of writing to disk files directly (ie: StandaloneSaveManager) we instead serialize
/// our data to a string and use Unity's PlayerPrefs API. This solution is not specific to PS4 and could
/// be used in various situations -- it just so happens that Unity's PS4 integration prefers PlayerPrefs usage.
/// </summary>
public class PS4SaveManager : MonoBehaviour, IGameSave {
/// <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();
// Make sure the data exists
string dataString = PlayerPrefs.GetString(filename, string.Empty);
if (dataString == string.Empty) {
return result;
}
// Convert the string into a stream
BinaryFormatter bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream(Convert.FromBase64String(dataString));
try {
// Deserialize the data
result = (T)bf.Deserialize(stream);
} catch (SerializationException e) {
Debug.Log("BinaryDeserialize failed: " + e.Message);
throw;
} finally {
stream.Close();
}
return result;
}
/// <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) {
BinaryFormatter bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
try {
// Serialize the data
bf.Serialize(stream, data);
// Write to player prefs
string dataString = Convert.ToBase64String(stream.ToArray());
PlayerPrefs.SetString(filename, dataString);
} catch (SerializationException e) {
Debug.Log("BinarySerialize failed: " + e.Message);
} finally {
stream.Close();
}
}
/// <summary>
/// Delete a given file.
/// </summary>
/// <param name="filename">The name of the file to delete.</param>
public void Delete(string filename) {
PlayerPrefs.SetString(filename, string.Empty);
}
/// <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 PlayerPrefs.GetString(filename, string.Empty) != string.Empty;
}
/// <summary>
/// Gets the full save path, including the filename.
/// </summary>
/// <param name="filename"></param>
/// <returns>The full save path.</returns>
public string GetPath(string filename) {
return string.Empty;
}
/// <summary>
/// Persists all changes to the hardware
/// </summary>
public void WriteToDisk() {
PlayerPrefs.Save();
}
}