//move to world IO? #region LoadWorld public bool LoadWorld(string fileName) { string path = Directory.GetCurrentDirectory() + "/Worlds/" + fileName + ".json"; try { //Get file contents for Parsing string json; using (StreamReader r = new StreamReader(path)) { json = r.ReadToEnd(); } WorldDataSchema worldData = JsonConvert.DeserializeObject <WorldDataSchema>(json); WorldOptions = worldData.WorldConfig; ChunkMap = worldData.Chunks; MapChunksToIndex(); return(true); } catch (Exception ex) { Console.WriteLine(ex); Console.WriteLine("No world with filename: \"" + fileName + "\" exists."); return(false); } }
//move to world IO? #region SaveWorld /** * Saves the current state of the world with a given in the world folder */ public bool SaveWorld(string fileName) { WorldDataSchema worldData = new WorldDataSchema { WorldConfig = WorldOptions, Chunks = ChunkMap }; string serializedWorldData = JsonConvert.SerializeObject(worldData, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); try { string path = Directory.GetCurrentDirectory() + "/Worlds/" + fileName + ".json"; //Create directory. If exist it does nothing Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/Worlds/"); using (StreamWriter s = File.CreateText(path)) { s.Write(serializedWorldData); } return(true); } catch (Exception ex) { Console.WriteLine(ex); return(false); } }