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;
43 DirectoryInfo directoryInfo =
new DirectoryInfo(
pathTemp);
44 if (directoryInfo.Exists)
46 directoryInfo.Delete(recursive:
true);
53 DirectoryInfo directoryInfo =
new DirectoryInfo(
pathTemp);
54 if (directoryInfo.Exists)
56 DirectoryInfo[] directories = directoryInfo.GetDirectories();
57 for (
int i = 0; i < directories.Length; i++)
59 directories[i].Delete(recursive:
true);
61 FileInfo[] files = directoryInfo.GetFiles();
62 for (
int i = 0; i < files.Length; i++)
79 IO.Compress(path, text);
83 File.WriteAllText(path, text);
85 DirectoryInfo[] directories =
new DirectoryInfo(
pathCurrentSave).GetDirectories();
86 foreach (DirectoryInfo directoryInfo
in directories)
88 if (
int.TryParse(directoryInfo.Name, out var result) && !
EClass.
game.
spatials.
map.ContainsKey(result))
90 IO.DeleteDirectory(directoryInfo.FullName);
91 Debug.Log(
"Deleting unused map:" + directoryInfo.FullName);
104 Debug.Log(
"Start backup:" + index.
id);
105 string id = index.
id;
106 bool cloud = index.
cloud;
109 IO.CreateDirectory(text);
111 List<DirectoryInfo> dirs =
new DirectoryInfo(text).GetDirectories().ToList();
112 dirs.ForeachReverse(delegate(DirectoryInfo i)
114 if (!
int.TryParse(i.Name, out var _))
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);
124 for (
int j = 0; j < count -
NumBackup; j++)
126 IO.DeleteDirectory(dirs[j].FullName);
129 Debug.Log(
"Copying backup:");
130 string newId =
GetNewId(text +
"/",
"", (dirs.Count == 0) ? 1 :
int.Parse(dirs.LastItem().Name));
144 string path = root +
"/game.txt";
145 foreach (KeyValuePair<string, string> fallbackType
in gameIndex.fallbackTypes)
151 gameIndex.cloud =
true;
154 else if (!File.Exists(path))
158 return JsonConvert.DeserializeObject<
Game>(IO.IsCompressed(path) ? IO.Decompress(path) : File.ReadAllText(path),
jsReadGame);
165 path = CorePath.RootSaveCloud +
"/" + id;
167 Debug.Log(
"Prepareing Steam Cloud:" +
id +
": " + path);
168 string text = CorePath.RootSaveCloud +
"/cloud.zip";
169 string text2 = path +
"/cloud.zip";
172 if (File.Exists(text))
176 if (File.Exists(text2))
180 ZipFile.CreateFromDirectory(path, text);
181 if (File.Exists(text2))
185 File.Move(text, text2);
195 Debug.Log(
"LoadGame using cloud save");
196 string text = pathSave +
"/cloud.zip";
197 string text2 = CorePath.RootSaveCloud +
"/cloud.zip";
201 if (!File.Exists(text))
203 EClass.
ui.Say(
"Steam Cloud save not found:" + text);
206 if (File.Exists(text2))
210 File.Move(text, text2);
211 IO.DeleteDirectory(pathSave);
213 Directory.CreateDirectory(pathSave);
214 ZipFile.ExtractToDirectory(text2, pathSave);
215 if (File.Exists(text))
219 File.Move(text2, text);
226 Debug.Log(
"Try restore backup:");
227 if (Directory.Exists(pathSave))
229 Directory.Delete(pathSave);
231 File.Move(text2, text);
241 IO.SaveFile(i.
path +
"/index.txt", i);
244 public static void SaveFile(
string path,
object obj)
254 public static void DeleteGame(
string id,
bool cloud,
bool deleteBackup =
true)
257 if (!Directory.Exists(path))
261 DirectoryInfo directoryInfo =
new DirectoryInfo(path);
262 if (directoryInfo.Exists)
264 directoryInfo.Delete(recursive:
true);
269 if (directoryInfo.Exists)
271 directoryInfo.Delete(recursive:
true);
276 public static List<GameIndex>
GetGameList(
string path,
bool sortByName =
false,
bool includeEmptyFolder =
false)
278 List<GameIndex> list =
new List<GameIndex>();
279 DirectoryInfo directoryInfo =
new DirectoryInfo(path);
280 if (!directoryInfo.Exists)
284 DirectoryInfo[] directories = directoryInfo.GetDirectories();
285 foreach (DirectoryInfo directoryInfo2
in directories)
287 if (File.Exists(directoryInfo2?.ToString() +
"/index.txt"))
291 GameIndex gameIndex = IO.LoadFile<
GameIndex>(directoryInfo2?.ToString() +
"/index.txt");
292 gameIndex.id = directoryInfo2.Name;
293 gameIndex.path = directoryInfo2.FullName;
296 catch (Exception message)
305 if (includeEmptyFolder && Directory.Exists(
CorePath.
PathBackup + directoryInfo2.Name))
308 gameIndex2.id = directoryInfo2.Name;
309 gameIndex2.path = directoryInfo2.FullName;
310 gameIndex2.date = (gameIndex2.real =
new Date());
311 list.Add(gameIndex2);
318 int.TryParse(a.
id, out var result);
319 int.TryParse(b.
id, out var result2);
320 return result2 - result;
332 DirectoryInfo[] directories =
new DirectoryInfo(path).GetDirectories();
333 foreach (DirectoryInfo directoryInfo
in directories)
335 if (!File.Exists(directoryInfo?.ToString() +
"/game.txt"))
337 directoryInfo.Delete(recursive:
true);
342 public static string GetNewId(
string path,
string prefix =
"",
int start = 1)
345 for (
int i = start; i < 999999; i++)
348 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)