public static void SaveWorldGridChunk(int chunkX, int chunkY, WorldGridChunk chunk) { var chunkPathPersistent = Path.Combine(Util.Util.GetWorldSavePath(), persistentIdentifier + (chunkX < 0 ? "m" + Math.Abs(chunkX) : "" + chunkX) + "_" + (chunkY < 0 ? "m" + Math.Abs(chunkY) : "" + chunkY)); var chunkPath = Path.Combine(Util.Util.GetWorldSavePath(), (chunkX < 0 ? "m" + Math.Abs(chunkX) : "" + chunkX) + "_" + (chunkY < 0 ? "m" + Math.Abs(chunkY) : "" + chunkY)); fileLocks.Enter(chunkPath); try { var savePath = chunk.IsPersistent ? chunkPathPersistent : chunkPath; using (var stream = File.OpenWrite(savePath)) using (var writer = new BsonWriter(stream)) { WorldGridChunkSerializer.Serialize(chunk).WriteTo(writer); } // We delete the other chunk var deletePath = chunk.IsPersistent ? chunkPath : chunkPathPersistent; if (File.Exists(deletePath)) { File.Delete(deletePath); } } finally { fileLocks.Exit(chunkPath); } }
public static WorldGridChunk LoadWorldGridChunk(int chunkX, int chunkY) { var chunkPathPersistent = Path.Combine(Util.Util.GetWorldSavePath(), persistentIdentifier + (chunkX < 0 ? "m" + Math.Abs(chunkX) : "" + chunkX) + "_" + (chunkY < 0 ? "m" + Math.Abs(chunkY) : "" + chunkY)); var chunkPathPersistentExists = File.Exists(chunkPathPersistent); var chunkPath = Path.Combine(Util.Util.GetWorldSavePath(), (chunkX < 0 ? "m" + Math.Abs(chunkX) : "" + chunkX) + "_" + (chunkY < 0 ? "m" + Math.Abs(chunkY) : "" + chunkY)); if (!chunkPathPersistentExists && !File.Exists(chunkPath)) { SimulationGame.WorldGenerator.generateChunk(chunkX * WorldGrid.WorldChunkBlockSize.X, chunkY * WorldGrid.WorldChunkBlockSize.Y); } fileLocks.Enter(chunkPath); try { var loadPath = chunkPathPersistentExists ? chunkPathPersistent : chunkPath; WorldGridChunk worldGridChunk; using (var stream = File.OpenRead(loadPath)) using (var reader = new BsonReader(stream)) { JToken jToken = JToken.ReadFrom(reader); worldGridChunk = WorldGridChunkSerializer.Deserialize((JObject)jToken); } return(worldGridChunk); } finally { fileLocks.Exit(chunkPath); } }