-
Notifications
You must be signed in to change notification settings - Fork 3
/
SavingManager.cs
83 lines (65 loc) · 2.12 KB
/
SavingManager.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace survivalPrototype
{
public static class SavingManager
{
public static string SavingPath;
public static void SaveData<T>(T dataToSave, int Save) where T : SaveableObject
{
if (!Directory.Exists (SavingPath))
Directory.CreateDirectory (SavingPath);
string path = SavingPath + "Save" + Save.ToString() + "/" + dataToSave.GetKey() +".banana";
BinaryFormatter bf = new BinaryFormatter ();
FileStream fs = new FileStream (path, FileMode.Create);
bf.Serialize (fs, dataToSave);
fs.Close ();
}
public static T LoadData<T>(string key, int Save) where T : SaveableObject
{
string path = SavingPath + "Save" + Save.ToString() + "/" + key +".banana";
if (File.Exists (path))
{
FileStream fs = new FileStream (path, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter ();
T objectToReturn = (T)bf.Deserialize (fs);
fs.Close ();
return objectToReturn;
}
return default(T);
}
public static bool HasSaveFile(string key, int Save)
{
return System.IO.File.Exists(SavingPath + "Save" + Save.ToString() + "/" + key +".banana");
}
public static bool HasAnySaveFile(int save)
{
if (Directory.GetFiles (SavingPath + "Save" + save.ToString ()).Length > 0)
{
return true;
}
return false;
}
public static string[] GetObjectsToInstantiate(int save)
{
string[] dirs = Directory.GetFiles (SavingPath + "Save" + save.ToString());
List<string> objectsToInstantiatePath = new List<string> ();
for (int i = 0; i < dirs.Length; i++)
{
FileStream fs = new FileStream (dirs[i], FileMode.Open);
BinaryFormatter bf = new BinaryFormatter ();
SaveableObject saveObject = (SaveableObject)bf.Deserialize (fs);
fs.Close ();
if (saveObject.isInstantiatable())
{
string pathToInstantiateObject = saveObject.GetPrefabPath ();
objectsToInstantiatePath.Add (pathToInstantiateObject);
}
}
return objectsToInstantiatePath.ToArray ();
}
}
}