public static void SaveWorld() { // Get list of coords of all chunks that have been generated List <Tuple <int, int> > chunkCoords = new List <Tuple <int, int> >(WorldData.chunks.Keys); Debug.Log("Total chunks: " + chunkCoords.Count); // Generate ChunkData objects for each chunk ChunkData[] worldChunkData = new ChunkData[chunkCoords.Count]; for (int i = 0; i < chunkCoords.Count; i++) { int x = chunkCoords[i].Item1; int y = chunkCoords[i].Item2; worldChunkData[i] = new ChunkData(x, y, WorldData.chunks[x, y]); } // Generate wrapper for JSON conversion GenericArrayWrapper <ChunkData> worldChunkDataWrapper = new GenericArrayWrapper <ChunkData>(worldChunkData); // Generat compressed string from JSON byte[] save = CompressString(JsonUtility.ToJson(worldChunkDataWrapper)); string saveName = "WORLDSAVE" + DateTime.UtcNow.ToString("d") + ".bingus"; // Save file File.WriteAllBytes(path + saveName, save); }
public static ChunkData[] LoadWorld(string path) { if (!File.Exists(path)) { throw new Exception("File not found at path " + path); } byte[] file = File.ReadAllBytes(path); using (var inputStream = new MemoryStream(file)) using (var gZipStream = new GZipStream(inputStream, CompressionMode.Decompress)) using (var outputStream = new MemoryStream()) { gZipStream.CopyTo(outputStream); var outputBytes = outputStream.ToArray(); string decompressed = System.Text.Encoding.UTF8.GetString(outputBytes); GenericArrayWrapper <ChunkData> worldChunkDataWrapper = JsonUtility.FromJson <GenericArrayWrapper <ChunkData> >(decompressed); return(worldChunkDataWrapper.data); } }