Elin Decompiled Documentation EA 23.190 Nightly
Loading...
Searching...
No Matches
GameIO.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.IO.Compression;
5using System.Linq;
6using Newtonsoft.Json;
7using UnityEngine;
8
9public class GameIO : EClass
10{
11 public static JsonSerializerSettings jsReadGame = new JsonSerializerSettings
12 {
13 NullValueHandling = NullValueHandling.Ignore,
14 DefaultValueHandling = DefaultValueHandling.Ignore,
15 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
16 TypeNameHandling = TypeNameHandling.Auto,
17 Error = IO.OnError,
18 SerializationBinder = GameSerializationBinder.Instance
19 };
20
21 public static JsonSerializerSettings jsWriteGame = new JsonSerializerSettings
22 {
23 NullValueHandling = NullValueHandling.Ignore,
24 DefaultValueHandling = DefaultValueHandling.Ignore,
25 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
26 TypeNameHandling = TypeNameHandling.Auto,
28 Error = IO.OnError
29 };
30
31 public static Formatting formatting = Formatting.Indented;
32
33 public static string pathCurrentSave => (EClass.core.game.isCloud ? CorePath.RootSaveCloud : CorePath.RootSave) + Game.id + "/";
34
35 public static string pathTemp => pathCurrentSave + "Temp/";
36
37 public static int NumBackup => (int)MathF.Max(5f, EClass.core.config.game.numBackup);
38
40
41 public static void ResetTemp()
42 {
43 DirectoryInfo directoryInfo = new DirectoryInfo(pathTemp);
44 if (directoryInfo.Exists)
45 {
46 directoryInfo.Delete(recursive: true);
47 }
48 IO.CreateDirectory(pathTemp);
49 }
50
51 public static void ClearTemp()
52 {
53 DirectoryInfo directoryInfo = new DirectoryInfo(pathTemp);
54 if (directoryInfo.Exists)
55 {
56 DirectoryInfo[] directories = directoryInfo.GetDirectories();
57 for (int i = 0; i < directories.Length; i++)
58 {
59 directories[i].Delete(recursive: true);
60 }
61 FileInfo[] files = directoryInfo.GetFiles();
62 for (int i = 0; i < files.Length; i++)
63 {
64 files[i].Delete();
65 }
66 }
67 }
68
69 public static GameIndex SaveGame()
70 {
71 string text = JsonConvert.SerializeObject(EClass.core.game, formatting, jsWriteGame);
72 string path = pathCurrentSave + "game.txt";
73 GameIndex gameIndex = new GameIndex().Create(EClass.core.game);
74 gameIndex.id = Game.id;
75 gameIndex.cloud = EClass.game.isCloud;
76 IO.SaveFile(pathCurrentSave + "index.txt", gameIndex);
77 if (compressSave)
78 {
79 IO.Compress(path, text);
80 }
81 else
82 {
83 File.WriteAllText(path, text);
84 }
85 DirectoryInfo[] directories = new DirectoryInfo(pathCurrentSave).GetDirectories();
86 foreach (DirectoryInfo directoryInfo in directories)
87 {
88 if (int.TryParse(directoryInfo.Name, out var result) && !EClass.game.spatials.map.ContainsKey(result))
89 {
90 IO.DeleteDirectory(directoryInfo.FullName);
91 Debug.Log("Deleting unused map:" + directoryInfo.FullName);
92 }
93 }
94 ClearTemp();
95 if (gameIndex.cloud)
96 {
97 PrepareSteamCloud(gameIndex.id);
98 }
99 return gameIndex;
100 }
101
102 public static void MakeBackup(GameIndex index, string suffix = "")
103 {
104 Debug.Log("Start backup:" + index.id);
105 string id = index.id;
106 bool cloud = index.cloud;
107 IO.CreateDirectory(cloud ? CorePath.PathBackupCloud : CorePath.PathBackup);
108 string text = (cloud ? CorePath.PathBackupCloud : CorePath.PathBackup) + id;
109 IO.CreateDirectory(text);
110 Debug.Log(text);
111 List<DirectoryInfo> dirs = new DirectoryInfo(text).GetDirectories().ToList();
112 dirs.ForeachReverse(delegate(DirectoryInfo i)
113 {
114 if (!int.TryParse(i.Name, out var _))
115 {
116 dirs.Remove(i);
117 }
118 });
119 dirs.Sort((DirectoryInfo a, DirectoryInfo b) => int.Parse(a.Name) - int.Parse(b.Name));
120 int count = dirs.Count;
121 Debug.Log("Deleting excess backup:" + dirs.Count + "/" + NumBackup);
122 if (count > NumBackup)
123 {
124 for (int j = 0; j < count - NumBackup; j++)
125 {
126 IO.DeleteDirectory(dirs[j].FullName);
127 }
128 }
129 Debug.Log("Copying backup:");
130 string newId = GetNewId(text + "/", "", (dirs.Count == 0) ? 1 : int.Parse(dirs.LastItem().Name));
131 IO.CopyDir((cloud ? CorePath.RootSaveCloud : CorePath.RootSave) + id + "/", text + "/" + newId, (string s) => s == "Temp");
132 }
133
134 public static bool CanLoad(string root)
135 {
136 GameIndex gameIndex = IO.LoadFile<GameIndex>(root + "/index.txt");
137 return EClass.core.version.IsSaveCompatible(gameIndex.version);
138 }
139
140 public static Game LoadGame(string id, string root, bool cloud)
141 {
142 Game.id = id;
143 GameIndex gameIndex = IO.LoadFile<GameIndex>(root + "/index.txt");
144 string path = root + "/game.txt";
145 foreach (KeyValuePair<string, string> fallbackType in gameIndex.fallbackTypes)
146 {
147 ModUtil.fallbackTypes[fallbackType.Key] = fallbackType.Value;
148 }
149 if (cloud)
150 {
151 gameIndex.cloud = true;
152 Debug.Log(TryLoadSteamCloud(root));
153 }
154 else if (!File.Exists(path))
155 {
156 Debug.Log(TryLoadSteamCloud(root));
157 }
158 return JsonConvert.DeserializeObject<Game>(IO.IsCompressed(path) ? IO.Decompress(path) : File.ReadAllText(path), jsReadGame);
159 }
160
161 public static void PrepareSteamCloud(string id, string path = "")
162 {
163 if (path.IsEmpty())
164 {
165 path = CorePath.RootSaveCloud + "/" + id;
166 }
167 Debug.Log("Prepareing Steam Cloud:" + id + ": " + path);
168 string text = CorePath.RootSaveCloud + "/cloud.zip";
169 string text2 = path + "/cloud.zip";
170 try
171 {
172 if (File.Exists(text))
173 {
174 File.Delete(text);
175 }
176 if (File.Exists(text2))
177 {
178 File.Delete(text2);
179 }
180 ZipFile.CreateFromDirectory(path, text);
181 if (File.Exists(text2))
182 {
183 File.Delete(text2);
184 }
185 File.Move(text, text2);
186 }
187 catch (Exception ex)
188 {
189 EClass.ui.Say(ex.Message);
190 }
191 }
192
193 public static bool TryLoadSteamCloud(string pathSave)
194 {
195 Debug.Log("LoadGame using cloud save");
196 string text = pathSave + "/cloud.zip";
197 string text2 = CorePath.RootSaveCloud + "/cloud.zip";
198 bool flag = false;
199 try
200 {
201 if (!File.Exists(text))
202 {
203 EClass.ui.Say("Steam Cloud save not found:" + text);
204 return true;
205 }
206 if (File.Exists(text2))
207 {
208 File.Delete(text2);
209 }
210 File.Move(text, text2);
211 IO.DeleteDirectory(pathSave);
212 flag = true;
213 Directory.CreateDirectory(pathSave);
214 ZipFile.ExtractToDirectory(text2, pathSave);
215 if (File.Exists(text))
216 {
217 File.Delete(text);
218 }
219 File.Move(text2, text);
220 }
221 catch (Exception ex)
222 {
223 EClass.ui.Say(ex.Message);
224 if (flag)
225 {
226 Debug.Log("Try restore backup:");
227 if (Directory.Exists(pathSave))
228 {
229 Directory.Delete(pathSave);
230 }
231 File.Move(text2, text);
232 return true;
233 }
234 return false;
235 }
236 return true;
237 }
238
239 public static void UpdateGameIndex(GameIndex i)
240 {
241 IO.SaveFile(i.path + "/index.txt", i);
242 }
243
244 public static void SaveFile(string path, object obj)
245 {
246 IO.SaveFile(path, obj, compressSave, jsWriteGame);
247 }
248
249 public static T LoadFile<T>(string path) where T : new()
250 {
251 return IO.LoadFile<T>(path, compressSave, jsReadGame);
252 }
253
254 public static void DeleteGame(string id, bool cloud, bool deleteBackup = true)
255 {
256 string path = (cloud ? CorePath.RootSaveCloud : CorePath.RootSave) + id;
257 if (!Directory.Exists(path))
258 {
259 return;
260 }
261 DirectoryInfo directoryInfo = new DirectoryInfo(path);
262 if (directoryInfo.Exists)
263 {
264 directoryInfo.Delete(recursive: true);
265 }
266 if (deleteBackup)
267 {
268 directoryInfo = new DirectoryInfo((cloud ? CorePath.PathBackupCloud : CorePath.PathBackup) + id);
269 if (directoryInfo.Exists)
270 {
271 directoryInfo.Delete(recursive: true);
272 }
273 }
274 }
275
276 public static List<GameIndex> GetGameList(string path, bool sortByName = false, bool includeEmptyFolder = false)
277 {
278 List<GameIndex> list = new List<GameIndex>();
279 DirectoryInfo directoryInfo = new DirectoryInfo(path);
280 if (!directoryInfo.Exists)
281 {
282 return list;
283 }
284 DirectoryInfo[] directories = directoryInfo.GetDirectories();
285 foreach (DirectoryInfo directoryInfo2 in directories)
286 {
287 if (File.Exists(directoryInfo2?.ToString() + "/index.txt"))
288 {
289 try
290 {
291 GameIndex gameIndex = IO.LoadFile<GameIndex>(directoryInfo2?.ToString() + "/index.txt");
292 gameIndex.id = directoryInfo2.Name;
293 gameIndex.path = directoryInfo2.FullName;
294 list.Add(gameIndex);
295 }
296 catch (Exception message)
297 {
298 Debug.Log(message);
299 goto IL_0097;
300 }
301 continue;
302 }
303 goto IL_0097;
304 IL_0097:
305 if (includeEmptyFolder && Directory.Exists(CorePath.PathBackup + directoryInfo2.Name))
306 {
307 GameIndex gameIndex2 = new GameIndex();
308 gameIndex2.id = directoryInfo2.Name;
309 gameIndex2.path = directoryInfo2.FullName;
310 gameIndex2.date = (gameIndex2.real = new Date());
311 list.Add(gameIndex2);
312 }
313 }
314 if (sortByName)
315 {
316 list.Sort(delegate(GameIndex a, GameIndex b)
317 {
318 int.TryParse(a.id, out var result);
319 int.TryParse(b.id, out var result2);
320 return result2 - result;
321 });
322 }
323 else
324 {
325 list.Sort((GameIndex a, GameIndex b) => b.real.GetRawReal() - a.real.GetRawReal());
326 }
327 return list;
328 }
329
330 public static void DeleteEmptyGameFolders(string path)
331 {
332 DirectoryInfo[] directories = new DirectoryInfo(path).GetDirectories();
333 foreach (DirectoryInfo directoryInfo in directories)
334 {
335 if (!File.Exists(directoryInfo?.ToString() + "/game.txt"))
336 {
337 directoryInfo.Delete(recursive: true);
338 }
339 }
340 }
341
342 public static string GetNewId(string path, string prefix = "", int start = 1)
343 {
344 string text = "";
345 for (int i = start; i < 999999; i++)
346 {
347 text = prefix + i;
348 if (!Directory.Exists(path + text))
349 {
350 break;
351 }
352 }
353 return text;
354 }
355}
Version version
Definition: BaseCore.cs:17
new GameConfig game
Definition: CoreConfig.cs:602
static string RootSave
Definition: CorePath.cs:206
static string RootSaveCloud
Definition: CorePath.cs:208
static string PathBackup
Definition: CorePath.cs:216
static string PathBackupCloud
Definition: CorePath.cs:218
Game game
Definition: Core.cs:72
CoreConfig config
Definition: Core.cs:70
Definition: Date.cs:4
int GetRawReal(int offsetHours=0)
Definition: Date.cs:317
Definition: EClass.cs:5
static Game game
Definition: EClass.cs:8
static Core core
Definition: EClass.cs:6
static UI ui
Definition: EClass.cs:16
Definition: GameIO.cs:10
static void SaveFile(string path, object obj)
Definition: GameIO.cs:244
static void DeleteGame(string id, bool cloud, bool deleteBackup=true)
Definition: GameIO.cs:254
static T LoadFile< T >(string path)
Definition: GameIO.cs:249
static bool compressSave
Definition: GameIO.cs:39
static int NumBackup
Definition: GameIO.cs:37
static string GetNewId(string path, string prefix="", int start=1)
Definition: GameIO.cs:342
static void DeleteEmptyGameFolders(string path)
Definition: GameIO.cs:330
static string pathCurrentSave
Definition: GameIO.cs:33
static bool CanLoad(string root)
Definition: GameIO.cs:134
static JsonSerializerSettings jsWriteGame
Definition: GameIO.cs:21
static void UpdateGameIndex(GameIndex i)
Definition: GameIO.cs:239
static GameIndex SaveGame()
Definition: GameIO.cs:69
static Formatting formatting
Definition: GameIO.cs:31
static void ClearTemp()
Definition: GameIO.cs:51
static JsonSerializerSettings jsReadGame
Definition: GameIO.cs:11
static Game LoadGame(string id, string root, bool cloud)
Definition: GameIO.cs:140
static void PrepareSteamCloud(string id, string path="")
Definition: GameIO.cs:161
static bool TryLoadSteamCloud(string pathSave)
Definition: GameIO.cs:193
static void ResetTemp()
Definition: GameIO.cs:41
static string pathTemp
Definition: GameIO.cs:35
static List< GameIndex > GetGameList(string path, bool sortByName=false, bool includeEmptyFolder=false)
Definition: GameIO.cs:276
static void MakeBackup(GameIndex index, string suffix="")
Definition: GameIO.cs:102
GameIndex Create(Game game)
Definition: GameIndex.cs:70
string id
Definition: GameIndex.cs:13
bool cloud
Definition: GameIndex.cs:41
Date real
Definition: GameIndex.cs:11
Version version
Definition: GameIndex.cs:21
string path
Definition: GameIndex.cs:48
Definition: Game.cs:8
static string id
Definition: Game.cs:147
SpatialManager spatials
Definition: Game.cs:152
bool isCloud
Definition: Game.cs:242
static Dictionary< string, string > fallbackTypes
Definition: ModUtil.cs:10
static readonly ShouldSerializeContractResolver Instance
GlobalSpatialList map
bool IsSaveCompatible(Version v)
Definition: Version.cs:82