| using System; |
| using System; |
| using System.Collections.Generic; |
| using System.Collections.Generic; |
| using System.IO; |
| using System.IO; |
| using UnityEngine; |
| using UnityEngine; |
| |
| |
| public class World |
| public class World |
| { |
| { |
| public World() |
| public World() |
| { |
| { |
| } |
| } |
| |
| |
| public World(SaveWithBackups save, World.SaveDataError dataError) |
| public World(SaveWithBackups save, World.SaveDataError dataError) |
| { |
| { |
| this.m_fileName = (this.m_name = save.m_name); |
| this.m_fileName = (this.m_name = save.m_name); |
| this.m_dataError = dataError; |
| this.m_dataError = dataError; |
| this.m_fileSource = save.PrimaryFile.m_source; |
| this.m_fileSource = save.PrimaryFile.m_source; |
| } |
| } |
| |
| |
| public World(string name, string seed) |
| public World(string name, string seed) |
| { |
| { |
| this.m_name = name; |
| this.m_name = name; |
| this.m_fileName = name; |
| this.m_fileName = name; |
| this.m_seedName = seed; |
| this.m_seedName = seed; |
| this.m_seed = ((this.m_seedName == "") ? 0 : this.m_seedName.GetStableHashCode()); |
| this.m_seed = ((this.m_seedName == "") ? 0 : this.m_seedName.GetStableHashCode()); |
| this.m_uid = (long)name.GetStableHashCode() + Utils.GenerateUID(); |
| this.m_uid = (long)name.GetStableHashCode() + Utils.GenerateUID(); |
| this.m_worldGenVersion = 2; |
| this.m_worldGenVersion = 2; |
| } |
| } |
| |
| |
| public static string GetWorldSavePath(FileHelpers.FileSource fileSource = FileHelpers.FileSource.Auto) |
| public static string GetWorldSavePath(FileHelpers.FileSource fileSource = FileHelpers.FileSource.Auto) |
| { |
| { |
| return Utils.GetSaveDataPath(fileSource) + ((fileSource == FileHelpers.FileSource.Local) ? "/worlds_local" : "/worlds"); |
| return Utils.GetSaveDataPath(fileSource) + ((fileSource == FileHelpers.FileSource.Local) ? "/worlds_local" : "/worlds"); |
| } |
| } |
| |
| |
| public static void RemoveWorld(string name, FileHelpers.FileSource fileSource) |
| public static void RemoveWorld(string name, FileHelpers.FileSource fileSource) |
| { |
| { |
| SaveWithBackups saveWithBackups; |
| SaveWithBackups saveWithBackups; |
| if (SaveSystem.TryGetSaveByName(name, SaveDataType.World, out saveWithBackups) && !saveWithBackups.IsDeleted) |
| if (SaveSystem.TryGetSaveByName(name, SaveDataType.World, out saveWithBackups) && !saveWithBackups.IsDeleted) |
| { |
| { |
| SaveSystem.Delete(saveWithBackups.PrimaryFile); |
| SaveSystem.Delete(saveWithBackups.PrimaryFile); |
| } |
| } |
| } |
| } |
| |
| |
| public string GetRootPath(FileHelpers.FileSource fileSource) |
| public string GetRootPath(FileHelpers.FileSource fileSource) |
| { |
| { |
| return World.GetWorldSavePath(fileSource) + "/" + this.m_fileName; |
| return World.GetWorldSavePath(fileSource) + "/" + this.m_fileName; |
| } |
| } |
| |
| |
| public string GetDBPath() |
| public string GetDBPath() |
| { |
| { |
| return this.GetDBPath(this.m_fileSource); |
| return this.GetDBPath(this.m_fileSource); |
| } |
| } |
| |
| |
| public string GetDBPath(FileHelpers.FileSource fileSource) |
| public string GetDBPath(FileHelpers.FileSource fileSource) |
| { |
| { |
| return World.GetWorldSavePath(fileSource) + "/" + this.m_fileName + ".db"; |
| return World.GetWorldSavePath(fileSource) + "/" + this.m_fileName + ".db"; |
| } |
| } |
| |
| |
| public static string GetDBPath(string name, FileHelpers.FileSource fileSource = FileHelpers.FileSource.Auto) |
| public static string GetDBPath(string name, FileHelpers.FileSource fileSource = FileHelpers.FileSource.Auto) |
| { |
| { |
| return World.GetWorldSavePath(fileSource) + "/" + name + ".db"; |
| return World.GetWorldSavePath(fileSource) + "/" + name + ".db"; |
| } |
| } |
| |
| |
| public string GetMetaPath() |
| public string GetMetaPath() |
| { |
| { |
| return this.GetMetaPath(this.m_fileSource); |
| return this.GetMetaPath(this.m_fileSource); |
| } |
| } |
| |
| |
| public string GetMetaPath(FileHelpers.FileSource fileSource) |
| public string GetMetaPath(FileHelpers.FileSource fileSource) |
| { |
| { |
| return World.GetWorldSavePath(fileSource) + "/" + this.m_fileName + ".fwl"; |
| return World.GetWorldSavePath(fileSource) + "/" + this.m_fileName + ".fwl"; |
| } |
| } |
| |
| |
| public static string GetMetaPath(string name, FileHelpers.FileSource fileSource = FileHelpers.FileSource.Auto) |
| public static string GetMetaPath(string name, FileHelpers.FileSource fileSource = FileHelpers.FileSource.Auto) |
| { |
| { |
| return World.GetWorldSavePath(fileSource) + "/" + name + ".fwl"; |
| return World.GetWorldSavePath(fileSource) + "/" + name + ".fwl"; |
| } |
| } |
| |
| |
| public static bool HaveWorld(string name) |
| public static bool HaveWorld(string name) |
| { |
| { |
| SaveWithBackups saveWithBackups; |
| SaveWithBackups saveWithBackups; |
| return SaveSystem.TryGetSaveByName(name, SaveDataType.World, out saveWithBackups) && !saveWithBackups.IsDeleted; |
| return SaveSystem.TryGetSaveByName(name, SaveDataType.World, out saveWithBackups) && !saveWithBackups.IsDeleted; |
| } |
| } |
| |
| |
| public static World GetMenuWorld() |
| public static World GetMenuWorld() |
| { |
| { |
| return new World("menu", "") |
| return new World("menu", "") |
| { |
| { |
| m_menu = true |
| m_menu = true |
| }; |
| }; |
| } |
| } |
| |
| |
| public static World GetEditorWorld() |
| public static World GetEditorWorld() |
| { |
| { |
| return new World("editor", ""); |
| return new World("editor", ""); |
| } |
| } |
| |
| |
| public static string GenerateSeed() |
| public static string GenerateSeed() |
| { |
| { |
| string text = ""; |
| string text = ""; |
| for (int i = 0; i < 10; i++) |
| for (int i = 0; i < 10; i++) |
| { |
| { |
| text += "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ023456789"[UnityEngine.Random.Range(0, "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ023456789".Length)].ToString(); |
| text += "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ023456789"[UnityEngine.Random.Range(0, "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ023456789".Length)].ToString(); |
| } |
| } |
| return text; |
| return text; |
| } |
| } |
| |
| |
| public static World GetCreateWorld(string name, FileHelpers.FileSource source) |
| public static World GetCreateWorld(string name, FileHelpers.FileSource source) |
| { |
| { |
| ZLog.Log("Get create world " + name); |
| ZLog.Log("Get create world " + name); |
| SaveWithBackups saveWithBackups; |
| SaveWithBackups saveWithBackups; |
| World world; |
| World world; |
| if (SaveSystem.TryGetSaveByName(name, SaveDataType.World, out saveWithBackups) && !saveWithBackups.IsDeleted) |
| if (SaveSystem.TryGetSaveByName(name, SaveDataType.World, out saveWithBackups) && !saveWithBackups.IsDeleted) |
| { |
| { |
| world = World.LoadWorld(saveWithBackups); |
| world = World.LoadWorld(saveWithBackups); |
| if (world.m_dataError == World.SaveDataError.None) |
| if (world.m_dataError == World.SaveDataError.None) |
| { |
| { |
| return world; |
| return world; |
| } |
| } |
| ZLog.LogError(string.Format("Failed to load world with name \"{0}\", data error {1}.", name, world.m_dataError)); |
| ZLog.LogError(string.Format("Failed to load world with name \"{0}\", data error {1}.", name, world.m_dataError)); |
| } |
| } |
| ZLog.Log(" creating"); |
| ZLog.Log(" creating"); |
| world = new World(name, World.GenerateSeed()); |
| world = new World(name, World.GenerateSeed()); |
| world.m_fileSource = source; |
| world.m_fileSource = source; |
| world.SaveWorldMetaData(DateTime.Now); |
| world.SaveWorldMetaData(DateTime.Now); |
| return world; |
| return world; |
| } |
| } |
| |
| |
| public static World GetDevWorld() |
| public static World GetDevWorld() |
| { |
| { |
| SaveWithBackups saveWithBackups; |
| SaveWithBackups saveWithBackups; |
| World world; |
| World world; |
| if (SaveSystem.TryGetSaveByName(Game.instance.m_devWorldName, SaveDataType.World, out saveWithBackups) && !saveWithBackups.IsDeleted) |
| if (SaveSystem.TryGetSaveByName(Game.instance.m_devWorldName, SaveDataType.World, out saveWithBackups) && !saveWithBackups.IsDeleted) |
| { |
| { |
| world = World.LoadWorld(saveWithBackups); |
| world = World.LoadWorld(saveWithBackups); |
| if (world.m_dataError == World.SaveDataError.None) |
| if (world.m_dataError == World.SaveDataError.None) |
| { |
| { |
| return world; |
| return world; |
| } |
| } |
| ZLog.Log(string.Format("Failed to load dev world, data error {0}. Creating...", world.m_dataError)); |
| ZLog.Log(string.Format("Failed to load dev world, data error {0}. Creating...", world.m_dataError)); |
| } |
| } |
| world = new World(Game.instance.m_devWorldName, Game.instance.m_devWorldSeed); |
| world = new World(Game.instance.m_devWorldName, Game.instance.m_devWorldSeed); |
| world.m_fileSource = FileHelpers.FileSource.Local; |
| world.m_fileSource = FileHelpers.FileSource.Local; |
| world.SaveWorldMetaData(DateTime.Now); |
| world.SaveWorldMetaData(DateTime.Now); |
| return world; |
| return world; |
| } |
| } |
| |
| |
| public void SaveWorldMetaData(DateTime backupTimestamp) |
| public void SaveWorldMetaData(DateTime backupTimestamp) |
| { |
| { |
| bool flag; |
| bool flag; |
| FileWriter fileWriter; |
| FileWriter fileWriter; |
| this.SaveWorldMetaData(backupTimestamp, true, out flag, out fileWriter); |
| this.SaveWorldMetaData(backupTimestamp, true, out flag, out fileWriter); |
| } |
| } |
| |
| |
| public void SaveWorldMetaData(DateTime now, bool considerBackup, out bool cloudSaveFailed, out FileWriter metaWriter) |
| public void SaveWorldMetaData(DateTime now, bool considerBackup, out bool cloudSaveFailed, out FileWriter metaWriter) |
| { |
| { |
| this.GetDBPath(); |
| this.GetDBPath(); |
| SaveSystem.CheckMove(this.m_fileName, SaveDataType.World, ref this.m_fileSource, now, 0UL, false); |
| SaveSystem.CheckMove(this.m_fileName, SaveDataType.World, ref this.m_fileSource, now, 0UL, false); |
| ZPackage zpackage = new ZPackage(); |
| ZPackage zpackage = new ZPackage(); |
| . | zpackage.Write(33); |
| zpackage.Write(34); |
| zpackage.Write(this.m_name); |
| zpackage.Write(this.m_name); |
| zpackage.Write(this.m_seedName); |
| zpackage.Write(this.m_seedName); |
| zpackage.Write(this.m_seed); |
| zpackage.Write(this.m_seed); |
| zpackage.Write(this.m_uid); |
| zpackage.Write(this.m_uid); |
| zpackage.Write(this.m_worldGenVersion); |
| zpackage.Write(this.m_worldGenVersion); |
| zpackage.Write(this.m_needsDB); |
| zpackage.Write(this.m_needsDB); |
| zpackage.Write(this.m_startingGlobalKeys.Count); |
| zpackage.Write(this.m_startingGlobalKeys.Count); |
| for (int i = 0; i < this.m_startingGlobalKeys.Count; i++) |
| for (int i = 0; i < this.m_startingGlobalKeys.Count; i++) |
| { |
| { |
| zpackage.Write(this.m_startingGlobalKeys[i]); |
| zpackage.Write(this.m_startingGlobalKeys[i]); |
| } |
| } |
| if (this.m_fileSource != FileHelpers.FileSource.Cloud) |
| if (this.m_fileSource != FileHelpers.FileSource.Cloud) |
| { |
| { |
| Directory.CreateDirectory(World.GetWorldSavePath(this.m_fileSource)); |
| Directory.CreateDirectory(World.GetWorldSavePath(this.m_fileSource)); |
| } |
| } |
| string metaPath = this.GetMetaPath(); |
| string metaPath = this.GetMetaPath(); |
| string text = metaPath + ".new"; |
| string text = metaPath + ".new"; |
| string text2 = metaPath + ".old"; |
| string text2 = metaPath + ".old"; |
| byte[] array = zpackage.GetArray(); |
| byte[] array = zpackage.GetArray(); |
| bool flag = this.m_fileSource == FileHelpers.FileSource.Cloud; |
| bool flag = this.m_fileSource == FileHelpers.FileSource.Cloud; |
| FileWriter fileWriter = new FileWriter(flag ? metaPath : text, FileHelpers.FileHelperType.Binary, this.m_fileSource); |
| FileWriter fileWriter = new FileWriter(flag ? metaPath : text, FileHelpers.FileHelperType.Binary, this.m_fileSource); |
| fileWriter.m_binary.Write(array.Length); |
| fileWriter.m_binary.Write(array.Length); |
| fileWriter.m_binary.Write(array); |
| fileWriter.m_binary.Write(array); |
| fileWriter.Finish(); |
| fileWriter.Finish(); |
| SaveSystem.InvalidateCache(); |
| SaveSystem.InvalidateCache(); |
| cloudSaveFailed = fileWriter.Status != FileWriter.WriterStatus.CloseSucceeded && this.m_fileSource == FileHelpers.FileSource.Cloud; |
| cloudSaveFailed = fileWriter.Status != FileWriter.WriterStatus.CloseSucceeded && this.m_fileSource == FileHelpers.FileSource.Cloud; |
| if (!cloudSaveFailed) |
| if (!cloudSaveFailed) |
| { |
| { |
| if (!flag) |
| if (!flag) |
| { |
| { |
| FileHelpers.ReplaceOldFile(metaPath, text, text2, this.m_fileSource); |
| FileHelpers.ReplaceOldFile(metaPath, text, text2, this.m_fileSource); |
| SaveSystem.InvalidateCache(); |
| SaveSystem.InvalidateCache(); |
| } |
| } |
| if (considerBackup) |
| if (considerBackup) |
| { |
| { |
| ZNet.ConsiderAutoBackup(this.m_fileName, SaveDataType.World, now); |
| ZNet.ConsiderAutoBackup(this.m_fileName, SaveDataType.World, now); |
| } |
| } |
| } |
| } |
| metaWriter = fileWriter; |
| metaWriter = fileWriter; |
| } |
| } |
| |
| |
| public static World LoadWorld(SaveWithBackups saveFile) |
| public static World LoadWorld(SaveWithBackups saveFile) |
| { |
| { |
| FileReader fileReader = null; |
| FileReader fileReader = null; |
| if (saveFile.IsDeleted) |
| if (saveFile.IsDeleted) |
| { |
| { |
| ZLog.Log("save deleted " + saveFile.m_name); |
| ZLog.Log("save deleted " + saveFile.m_name); |
| return new World(saveFile, World.SaveDataError.LoadError); |
| return new World(saveFile, World.SaveDataError.LoadError); |
| } |
| } |
| FileHelpers.FileSource source = saveFile.PrimaryFile.m_source; |
| FileHelpers.FileSource source = saveFile.PrimaryFile.m_source; |
| string pathPrimary = saveFile.PrimaryFile.PathPrimary; |
| string pathPrimary = saveFile.PrimaryFile.PathPrimary; |
| string text = ((saveFile.PrimaryFile.PathsAssociated.Length != 0) ? saveFile.PrimaryFile.PathsAssociated[0] : null); |
| string text = ((saveFile.PrimaryFile.PathsAssociated.Length != 0) ? saveFile.PrimaryFile.PathsAssociated[0] : null); |
| if (FileHelpers.IsFileCorrupt(pathPrimary, source) || (text != null && FileHelpers.IsFileCorrupt(text, source))) |
| if (FileHelpers.IsFileCorrupt(pathPrimary, source) || (text != null && FileHelpers.IsFileCorrupt(text, source))) |
| { |
| { |
| ZLog.Log(" corrupt save " + saveFile.m_name); |
| ZLog.Log(" corrupt save " + saveFile.m_name); |
| return new World(saveFile, World.SaveDataError.Corrupt); |
| return new World(saveFile, World.SaveDataError.Corrupt); |
| } |
| } |
| try |
| try |
| { |
| { |
| fileReader = new FileReader(pathPrimary, source, FileHelpers.FileHelperType.Binary); |
| fileReader = new FileReader(pathPrimary, source, FileHelpers.FileHelperType.Binary); |
| } |
| } |
| catch (Exception ex) |
| catch (Exception ex) |
| { |
| { |
| if (fileReader != null) |
| if (fileReader != null) |
| { |
| { |
| fileReader.Dispose(); |
| fileReader.Dispose(); |
| } |
| } |
| string text2 = " failed to load "; |
| string text2 = " failed to load "; |
| string name = saveFile.m_name; |
| string name = saveFile.m_name; |
| string text3 = " Exception: "; |
| string text3 = " Exception: "; |
| Exception ex2 = ex; |
| Exception ex2 = ex; |
| ZLog.Log(text2 + name + text3 + ((ex2 != null) ? ex2.ToString() : null)); |
| ZLog.Log(text2 + name + text3 + ((ex2 != null) ? ex2.ToString() : null)); |
| return new World(saveFile, World.SaveDataError.LoadError); |
| return new World(saveFile, World.SaveDataError.LoadError); |
| } |
| } |
| World world; |
| World world; |
| try |
| try |
| { |
| { |
| BinaryReader binary = fileReader.m_binary; |
| BinaryReader binary = fileReader.m_binary; |
| int num = binary.ReadInt32(); |
| int num = binary.ReadInt32(); |
| ZPackage zpackage = new ZPackage(binary.ReadBytes(num)); |
| ZPackage zpackage = new ZPackage(binary.ReadBytes(num)); |
| int num2 = zpackage.ReadInt(); |
| int num2 = zpackage.ReadInt(); |
| if (!global::Version.IsWorldVersionCompatible(num2)) |
| if (!global::Version.IsWorldVersionCompatible(num2)) |
| { |
| { |
| ZLog.Log("incompatible world version " + num2.ToString()); |
| ZLog.Log("incompatible world version " + num2.ToString()); |
| world = new World(saveFile, World.SaveDataError.BadVersion); |
| world = new World(saveFile, World.SaveDataError.BadVersion); |
| } |
| } |
| else |
| else |
| { |
| { |
| World world2 = new World(); |
| World world2 = new World(); |
| world2.m_fileSource = source; |
| world2.m_fileSource = source; |
| world2.m_fileName = saveFile.m_name; |
| world2.m_fileName = saveFile.m_name; |
| world2.m_name = zpackage.ReadString(); |
| world2.m_name = zpackage.ReadString(); |
| world2.m_seedName = zpackage.ReadString(); |
| world2.m_seedName = zpackage.ReadString(); |
| world2.m_seed = zpackage.ReadInt(); |
| world2.m_seed = zpackage.ReadInt(); |
| world2.m_uid = zpackage.ReadLong(); |
| world2.m_uid = zpackage.ReadLong(); |
| world2.m_worldVersion = num2; |
| world2.m_worldVersion = num2; |
| if (num2 >= 26) |
| if (num2 >= 26) |
| { |
| { |
| world2.m_worldGenVersion = zpackage.ReadInt(); |
| world2.m_worldGenVersion = zpackage.ReadInt(); |
| } |
| } |
| world2.m_needsDB = num2 >= 30 && zpackage.ReadBool(); |
| world2.m_needsDB = num2 >= 30 && zpackage.ReadBool(); |
| . | if (num2 != 33) |
| if (num2 != 34) |
| { |
| { |
| world2.m_createBackupBeforeSaving = true; |
| world2.m_createBackupBeforeSaving = true; |
| } |
| } |
| if (world2.CheckDbFile()) |
| if (world2.CheckDbFile()) |
| { |
| { |
| world2.m_dataError = World.SaveDataError.MissingDB; |
| world2.m_dataError = World.SaveDataError.MissingDB; |
| } |
| } |
| if (num2 >= 32) |
| if (num2 >= 32) |
| { |
| { |
| int num3 = zpackage.ReadInt(); |
| int num3 = zpackage.ReadInt(); |
| for (int i = 0; i < num3; i++) |
| for (int i = 0; i < num3; i++) |
| { |
| { |
| world2.m_startingGlobalKeys.Add(zpackage.ReadString()); |
| world2.m_startingGlobalKeys.Add(zpackage.ReadString()); |
| } |
| } |
| } |
| } |
| world = world2; |
| world = world2; |
| } |
| } |
| } |
| } |
| catch |
| catch |
| { |
| { |
| ZLog.LogWarning(" error loading world " + saveFile.m_name); |
| ZLog.LogWarning(" error loading world " + saveFile.m_name); |
| world = new World(saveFile, World.SaveDataError.LoadError); |
| world = new World(saveFile, World.SaveDataError.LoadError); |
| } |
| } |
| finally |
| finally |
| { |
| { |
| if (fileReader != null) |
| if (fileReader != null) |
| { |
| { |
| fileReader.Dispose(); |
| fileReader.Dispose(); |
| } |
| } |
| } |
| } |
| return world; |
| return world; |
| } |
| } |
| |
| |
| private bool CheckDbFile() |
| private bool CheckDbFile() |
| { |
| { |
| return this.m_needsDB && !FileHelpers.Exists(this.GetDBPath(), this.m_fileSource); |
| return this.m_needsDB && !FileHelpers.Exists(this.GetDBPath(), this.m_fileSource); |
| } |
| } |
| |
| |
| public string m_fileName = ""; |
| public string m_fileName = ""; |
| |
| |
| public string m_name = ""; |
| public string m_name = ""; |
| |
| |
| public string m_seedName = ""; |
| public string m_seedName = ""; |
| |
| |
| public int m_seed; |
| public int m_seed; |
| |
| |
| public long m_uid; |
| public long m_uid; |
| |
| |
| public List<string> m_startingGlobalKeys = new List<string>(); |
| public List<string> m_startingGlobalKeys = new List<string>(); |
| |
| |
| public bool m_startingKeysChanged; |
| public bool m_startingKeysChanged; |
| |
| |
| public int m_worldGenVersion; |
| public int m_worldGenVersion; |
| |
| |
| public int m_worldVersion; |
| public int m_worldVersion; |
| |
| |
| public bool m_menu; |
| public bool m_menu; |
| |
| |
| public bool m_needsDB; |
| public bool m_needsDB; |
| |
| |
| public bool m_createBackupBeforeSaving; |
| public bool m_createBackupBeforeSaving; |
| |
| |
| public SaveWithBackups saves; |
| public SaveWithBackups saves; |
| |
| |
| public World.SaveDataError m_dataError; |
| public World.SaveDataError m_dataError; |
| |
| |
| public FileHelpers.FileSource m_fileSource = FileHelpers.FileSource.Local; |
| public FileHelpers.FileSource m_fileSource = FileHelpers.FileSource.Local; |
| |
| |
| public enum SaveDataError |
| public enum SaveDataError |
| { |
| { |
| None, |
| None, |
| BadVersion, |
| BadVersion, |
| LoadError, |
| LoadError, |
| Corrupt, |
| Corrupt, |
| MissingMeta, |
| MissingMeta, |
| MissingDB |
| MissingDB |
| } |
| } |
| } |
| } |
| |
| |