-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaveAndLoad.cs
100 lines (96 loc) · 3.83 KB
/
SaveAndLoad.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
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace ttc_wtc
{
static class SaveAndLoad
{
public static string[] Saves { get; set; }
public static bool[] EmptySave { get; set; }
public static void Initialise()
{
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream fileStream = new FileStream("saves.dat", FileMode.OpenOrCreate))
{
if (fileStream.Length != 0)
{
string[] saves = (string[])formatter.Deserialize(fileStream);
bool[] emptySave = (bool[])formatter.Deserialize(fileStream);
Saves = saves;
EmptySave = emptySave;
}
else
{
Saves = new string[3] { "None", "None", "None" };
EmptySave = new bool[3] { true, true, true };
}
}
}
public static void Save(Player player, List<Entity> entities, List<Chest> chests, bool endless)
{
List<string> saveMenuItems = new List<string>(Saves);
saveMenuItems.Add("Выйти");
Menu saveMenu = new Menu(saveMenuItems);
int choice = saveMenu.GetChoice();
if (choice != saveMenuItems.Count - 1)
{
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream fileStream = new FileStream("saves.dat", FileMode.OpenOrCreate))
{
Saves[choice] = ("save" + choice + ".dat");
EmptySave[choice] = false;
formatter.Serialize(fileStream, Saves);
formatter.Serialize(fileStream, EmptySave);
}
using (FileStream fileStream = new FileStream(Saves[choice], FileMode.OpenOrCreate))
{
formatter.Serialize(fileStream, endless);
formatter.Serialize(fileStream, CollectedMaps.AllMaps);
formatter.Serialize(fileStream, player);
if (!endless)
{
formatter.Serialize(fileStream, entities);
formatter.Serialize(fileStream, chests);
}
}
}
}
public static bool Load(ref Player player, ref List<Entity> entities, ref List<Chest> chests, out bool? endless)
{
BinaryFormatter formatter = new BinaryFormatter();
List<string> loadMenuItems = new List<string>();
for (int i = 0; i < Saves.Length; i++)
{
if (!EmptySave[i])
{
loadMenuItems.Add(Saves[i]);
}
}
loadMenuItems.Add("Выйти");
Menu loadMenu = new Menu(loadMenuItems);
int choice = loadMenu.GetChoice();
if (choice == loadMenuItems.Count - 1)
{
endless = null;
return false;
}
using (FileStream fileStream = new FileStream(Saves[choice], FileMode.OpenOrCreate))
{
endless = (bool)formatter.Deserialize(fileStream);
CollectedMaps.AllMaps = (List<Map>)formatter.Deserialize(fileStream);
player = (Player)formatter.Deserialize(fileStream);
if (!(bool)endless)
{
entities = (List<Entity>)formatter.Deserialize(fileStream);
chests = (List<Chest>)formatter.Deserialize(fileStream);
}
else
{
entities = null;
chests = null;
}
return true;
}
}
}
}