Exemplo n.º 1
0
        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;
            }
        }