2using System.Collections.Generic;
4using System.IO.Compression;
11 public static JsonSerializerSettings
jsReadGame =
new JsonSerializerSettings
13 NullValueHandling = NullValueHandling.Ignore,
14 DefaultValueHandling = DefaultValueHandling.Ignore,
15 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
16 TypeNameHandling = TypeNameHandling.Auto,
18 SerializationBinder = GameSerializationBinder.Instance
21 public static JsonSerializerSettings
jsWriteGame =
new JsonSerializerSettings
23 NullValueHandling = NullValueHandling.Ignore,
24 DefaultValueHandling = DefaultValueHandling.Ignore,
25 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
26 TypeNameHandling = TypeNameHandling.Auto,
31 public static Formatting
formatting = Formatting.Indented;
53 DirectoryInfo directoryInfo =
new DirectoryInfo(
pathTemp);
54 if (directoryInfo.Exists)
56 directoryInfo.Delete(recursive:
true);
63 DirectoryInfo directoryInfo =
new DirectoryInfo(
pathTemp);
64 if (directoryInfo.Exists)
66 DirectoryInfo[] directories = directoryInfo.GetDirectories();
67 for (
int i = 0; i < directories.Length; i++)
69 directories[i].Delete(recursive:
true);
71 FileInfo[] files = directoryInfo.GetFiles();
72 for (
int i = 0; i < files.Length; i++)
89 IO.Compress(path, text);
93 File.WriteAllText(path, text);
95 DirectoryInfo[] directories =
new DirectoryInfo(
pathCurrentSave).GetDirectories();
96 foreach (DirectoryInfo directoryInfo
in directories)
98 if (
int.TryParse(directoryInfo.Name, out var result) && !
EClass.
game.
spatials.
map.ContainsKey(result))
100 IO.DeleteDirectory(directoryInfo.FullName);
101 Debug.Log(
"Deleting unused map:" + directoryInfo.FullName);
114 Debug.Log(
"Start backup:" + index.
id);
115 string id = index.
id;
116 bool cloud = index.
cloud;
119 IO.CreateDirectory(text);
121 List<DirectoryInfo> dirs =
new DirectoryInfo(text).GetDirectories().ToList();
122 dirs.ForeachReverse(delegate(DirectoryInfo i)
124 if (!
int.TryParse(i.Name, out var _))
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);
134 for (
int j = 0; j < count -
NumBackup; j++)
136 IO.DeleteDirectory(dirs[j].FullName);
139 Debug.Log(
"Copying backup:");
140 string newId =
GetNewId(text +
"/",
"", (dirs.Count == 0) ? 1 :
int.Parse(dirs.LastItem().Name));
154 string path = root +
"/game.txt";
155 foreach (KeyValuePair<string, string> fallbackType
in gameIndex.fallbackTypes)
161 gameIndex.cloud =
true;
164 else if (!File.Exists(path))
168 return JsonConvert.DeserializeObject<
Game>(IO.IsCompressed(path) ? IO.Decompress(path) : File.ReadAllText(path),
jsReadGame);
175 path = CorePath.RootSaveCloud +
"/" + id;
177 Debug.Log(
"Prepareing Steam Cloud:" +
id +
": " + path);
178 string text = CorePath.RootSaveCloud +
"/cloud.zip";
179 string text2 = path +
"/cloud.zip";
182 if (File.Exists(text))
186 if (File.Exists(text2))
190 ZipFile.CreateFromDirectory(path, text);
191 if (File.Exists(text2))
195 File.Move(text, text2);
205 Debug.Log(
"LoadGame using cloud save");
206 string text = pathSave +
"/cloud.zip";
207 string text2 = CorePath.RootSaveCloud +
"/cloud.zip";
211 if (!File.Exists(text))
213 EClass.
ui.Say(
"Steam Cloud save not found:" + text);
216 if (File.Exists(text2))
220 File.Move(text, text2);
221 IO.DeleteDirectory(pathSave);
223 Directory.CreateDirectory(pathSave);
224 ZipFile.ExtractToDirectory(text2, pathSave);
225 if (File.Exists(text))
229 File.Move(text2, text);
236 Debug.Log(
"Try restore backup:");
237 if (Directory.Exists(pathSave))
239 Directory.Delete(pathSave);
241 File.Move(text2, text);
251 IO.SaveFile(i.
path +
"/index.txt", i);
254 public static void SaveFile(
string path,
object obj)
264 public static void DeleteGame(
string id,
bool cloud,
bool deleteBackup =
true)
267 if (!Directory.Exists(path))
271 DirectoryInfo directoryInfo =
new DirectoryInfo(path);
272 if (directoryInfo.Exists)
274 directoryInfo.Delete(recursive:
true);
279 if (directoryInfo.Exists)
281 directoryInfo.Delete(recursive:
true);
286 public static List<GameIndex>
GetGameList(
string path,
bool sortByName =
false,
bool includeEmptyFolder =
false)
288 List<GameIndex> list =
new List<GameIndex>();
289 DirectoryInfo directoryInfo =
new DirectoryInfo(path);
290 if (!directoryInfo.Exists)
294 DirectoryInfo[] directories = directoryInfo.GetDirectories();
295 foreach (DirectoryInfo directoryInfo2
in directories)
297 if (File.Exists(directoryInfo2?.ToString() +
"/index.txt"))
301 GameIndex gameIndex = IO.LoadFile<
GameIndex>(directoryInfo2?.ToString() +
"/index.txt");
302 gameIndex.id = directoryInfo2.Name;
303 gameIndex.path = directoryInfo2.FullName;
306 catch (Exception message)
315 if (includeEmptyFolder && Directory.Exists(
CorePath.
PathBackup + directoryInfo2.Name))
318 gameIndex2.id = directoryInfo2.Name;
319 gameIndex2.path = directoryInfo2.FullName;
320 gameIndex2.date = (gameIndex2.real =
new Date());
321 list.Add(gameIndex2);
328 int.TryParse(a.
id, out var result);
329 int.TryParse(b.
id, out var result2);
330 return result2 - result;
342 DirectoryInfo[] directories =
new DirectoryInfo(path).GetDirectories();
343 foreach (DirectoryInfo directoryInfo
in directories)
345 if (!File.Exists(directoryInfo?.ToString() +
"/game.txt"))
347 directoryInfo.Delete(recursive:
true);
352 public static string GetNewId(
string path,
string prefix =
"",
int start = 1)
355 for (
int i = start; i < 999999; i++)
358 if (!Directory.Exists(path + text))
static string RootSaveCloud
static string PathBackupCloud
int GetRawReal(int offsetHours=0)
static void SaveFile(string path, object obj)
static void DeleteGame(string id, bool cloud, bool deleteBackup=true)
static T LoadFile< T >(string path)
static string GetNewId(string path, string prefix="", int start=1)
static void DeleteEmptyGameFolders(string path)
static string pathCurrentSave
static bool CanLoad(string root)
static JsonSerializerSettings jsWriteGame
static void UpdateGameIndex(GameIndex i)
static GameIndex SaveGame()
static Formatting formatting
static JsonSerializerSettings jsReadGame
static Game LoadGame(string id, string root, bool cloud)
static void PrepareSteamCloud(string id, string path="")
static bool TryLoadSteamCloud(string pathSave)
static List< GameIndex > GetGameList(string path, bool sortByName=false, bool includeEmptyFolder=false)
static void MakeBackup(GameIndex index, string suffix="")
GameIndex Create(Game game)
static Dictionary< string, string > fallbackTypes
static readonly ShouldSerializeContractResolver Instance
bool IsSaveCompatible(Version v)