public void load(string filename) { try { if (map != null) { GraphicalMap.purge(); GraphicalSociety.purge(); map.world = null; map = null; } filename = saveFolder + filename; string fullFile = File.ReadAllText(filename); //string serializedState = fullFile.Substring(fullFile.IndexOf("\n"), fullFile.Length); int startIndex = fullFile.IndexOf(saveHeader) + saveHeader.Length; int endIndex = fullFile.Length - startIndex; string serializedState = fullFile.Substring(startIndex, endIndex); fsSerializer _serializer = new fsSerializer(); fsData data = fsJsonParser.Parse(serializedState); World.Log("Data parsed"); object deserialized = null; _serializer.TryDeserialize(data, typeof(Map), ref deserialized).AssertSuccessWithoutWarnings(); World.saveLog.takeLine("Finished deserial"); map = (Map)deserialized; map.world = this; staticMap = map; World.self.displayMessages = true; GraphicalMap.map = map; //ui.setToMainMenu(); //GraphicalMap.checkLoaded(); //GraphicalMap.checkData(); //graphicalMap.loadArea(0, 0); map.decompressFromSave(); prefabStore.popMsg("Loaded file: " + filename, true); World.Log("reached end of loading code"); // prefabStore.popMsg("Load may well have succeeded."); } catch (FileLoadException e) { Debug.Log(e); World.log(e.StackTrace); prefabStore.popMsg("Exception: " + e.StackTrace, true); } catch (Exception e2) { Debug.Log(e2); World.log(e2.StackTrace); prefabStore.popMsg("Exception: " + e2.StackTrace, true); } }
public void save(string filename, bool popMsg = true) { Map rescueMap = World.staticMap; if (checkSaveFolder() == false) { prefabStore.popMsg("Unable to locate directory " + saveFolder + ". Saving cannot proceed without folder access. Aborting save.", true); return; } if (!hasWritePermission(saveFolder)) { prefabStore.popMsg("Unable to write to directory " + saveFolder + ". Saving cannot proceed without folder access. Aborting save.", true); World.autosavePeriod = -1; return; } try { World world = this; // world.ui.setToMainMenu(); GraphicalMap.purge(); GraphicalSociety.purge(); map.compressForSave(); world.map.world = null; //foreach (SocialGroup sg in map.socialGroups) //{ // if (sg is Society) // { // Society soc = (Society)sg; // soc.voteSession = null; // } //} fsSerializer _serializer = new fsSerializer(); fsData data; _serializer.TrySerialize(typeof(Map), map, out data).AssertSuccessWithoutWarnings(); // emit the data via JSON string saveString = fsJsonPrinter.CompressedJson(data); World.Log("Save data exit point"); string catSaveString = "Version;" + World.versionNumber + ";" + World.subversionNumber; catSaveString += saveHeader; catSaveString += saveString; if (File.Exists(filename)) { World.Log("Overwriting old save: " + filename); File.Delete(filename); } File.WriteAllLines(filename, new string[] { catSaveString });//Do it all on one line, to avoid faff wrt line endings world.map.world = world; staticMap = map; map.decompressFromSave(); if (popMsg) { world.prefabStore.popMsg("Game saved as: " + filename, true); } //// step 1: parse the JSON data //fsData data = fsJsonParser.Parse(serializedState); //// step 2: deserialize the data //object deserialized = null; //_serializer.TryDeserialize(data, type, ref deserialized).AssertSuccessWithoutWarnings(); } catch (Exception e) { World.log(e.Message); World.log(e.StackTrace); prefabStore.popMsg("Failure to save", true); prefabStore.popMsg("Exception: " + e.StackTrace, true); map = rescueMap; map.world = this; staticMap = map; } }