-
Notifications
You must be signed in to change notification settings - Fork 8
/
Save.cs
130 lines (111 loc) · 3.89 KB
/
Save.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
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
using System;
using System.IO;
using System.Xml.Serialization;
using Kingmaker.EntitySystem.Persistence;
namespace BagOfTricks
{
[Serializable]
public class SaveData
{
public string saveVersion = "1.0.0";
public string fileName;
public int lockPicks;
}
public static class SaveTools
{
public static void Serialize(SaveData saveData, string filePath)
{
var serializer = new XmlSerializer(typeof(SaveData));
using (var stream = File.Create(filePath))
{
serializer.Serialize(stream, saveData);
}
}
public static SaveData Deserialize(string filePath)
{
var newSaveData = new SaveData();
if (File.Exists(filePath))
try
{
using (var stream = File.OpenRead(filePath))
{
var serializer = new XmlSerializer(typeof(SaveData));
var save = (SaveData) serializer.Deserialize(stream);
return save;
}
}
catch (Exception exception)
{
Main.modLogger.Log(exception.ToString());
}
return newSaveData;
}
public static void SaveFile(SaveInfo saveInfo)
{
try
{
var name = Strings.RemoveExt(saveInfo.FileName);
var filePath = Storage.modEntryPath + Storage.savesFolder + "\\" + name + ".xml";
Main.saveData.fileName = name;
Main.saveData.lockPicks = Storage.lockPicks;
Common.ModLoggerDebug($"PrepareSave {name}");
if (File.Exists(filePath))
{
Serialize(Main.saveData, filePath);
Common.ModLoggerDebug($"{Storage.modEntryPath + Storage.savesFolder + "\\" + name} overwritten.");
}
else
{
Serialize(Main.saveData, filePath);
Common.ModLoggerDebug($"{Storage.modEntryPath + Storage.savesFolder + "\\" + name} created.");
}
}
catch (Exception e)
{
Main.modLogger.Log(e.ToString());
}
}
public static void LoadFile(SaveInfo saveInfo)
{
try
{
var name = Strings.RemoveExt(saveInfo.FileName);
var filePath = Storage.modEntryPath + Storage.savesFolder + "\\" + name + ".xml";
Common.ModLoggerDebug($"LoadGame {name}");
if (File.Exists(filePath))
{
Main.saveData = Deserialize(filePath);
Storage.lockPicks = Main.saveData.lockPicks;
Common.ModLoggerDebug($"{filePath} loaded.");
}
else
{
Storage.lockPicks = 5;
Main.modLogger.Log($"{filePath} not found!");
}
}
catch (Exception e)
{
Main.modLogger.Log(e.ToString());
}
}
public static void DeleteFile(SaveInfo saveInfo)
{
try
{
var name = Strings.RemoveExt(saveInfo.FileName);
var filePath = Storage.modEntryPath + Storage.savesFolder + "\\" + name + ".xml";
Common.ModLoggerDebug($"DeleteSave {name}");
if (File.Exists(filePath))
{
File.Delete(filePath);
Common.ModLoggerDebug($"{name} deleted.");
}
}
catch (Exception e)
{
Main.modLogger.Log(e.ToString());
}
}
}
}