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