public static void SaveChunk(Chunk chunk) { SaveChunk MyChunkSave = new SaveChunk(chunk); if (MyChunkSave.BlocksDictionary.Count == 0) return; string saveFile = SaveLocation(chunk.world.SaveFileName, chunk.world.worldName); saveFile += FileName(chunk.pos); IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(saveFile, FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(stream, MyChunkSave); stream.Close(); }
public static Chunk GetChunk(Dimension dim, int X, int Z) { LogEntry = $"Getting chunk at {X}/{Z}\n"; GenTime.Restart(); Chunk ch = new Chunk(dim, X, Z); bool AlreadyLoaded = dim.Chunks.TryGetValue((X, Z), out ch); if (AlreadyLoaded) { return(ch); } if (ShouldLoadChunks && File.Exists($"Worlds/{dim.World.Name}/{dim.Name}/ChunkData/{X}.{Z}.gz")) { LogEntry += $"Found chunk file for {X}/{Z}, loading...\n"; int length; byte[] b = new byte[4]; using (FileStream fs = File.OpenRead($"Worlds/{dim.World.Name}/{dim.Name}/ChunkData/{X}.{Z}.gz")) { fs.Position = fs.Length - 4; fs.Read(b, 0, 4); length = BitConverter.ToInt32(b, 0); } byte[] data = new byte[length]; GZipStream chIn = new GZipStream(new FileStream($"Worlds/{dim.World.Name}/{dim.Name}/ChunkData/{X}.{Z}.gz", FileMode.Open), CompressionMode.Decompress); chIn.Read(data, 0, data.Length); chIn.Close(); SaveChunk sch = JsonConvert.DeserializeObject <SaveChunk>(Encoding.ASCII.GetString(data)); foreach (Block bl in sch.Blocks) { ch.Blocks.AddOrUpdate((bl.X, bl.Y, bl.Z), bl, (_, _1) => bl); } LogEntry += $"Loaded chunk {X}/{Z} in: {GenTime.ElapsedTicks / 10000d} ms\n"; } else { LogEntry += $"Generating chunk at {X}/{Z}...\n"; ch = GenChunk(dim, X, Z); } try { LogEntry += $"Adding chunk at {X}/{Z} to world."; dim.Chunks.AddOrUpdate((X, Z), ch, (_, _1) => ch); } catch { } Logger.Log("Terrain Gen", LogEntry); return(ch); }
/// <summary> /// Saves a given <see cref="Chunk"/> if a block in it has been changed /// </summary> /// <param name="chunk"></param> public static void SaveChunk(Chunk chunk) { //* saves the blocks SaveChunk save = new SaveChunk(chunk.blocks); //* if no block was changed return early if (save.blocks.Count == 0) { return; } //* otherwise save the file string saveFile = $"{savePath}/{FileName(chunk.chunkWorldPos)}.dat"; SaveFile(save, saveFile); }
void SaveChunkBytes(SaveChunk chunk) { int i = 0; for (int x = chunk.chunkPosX; x < chunk.chunkPosX + world.chunkSize; x++) { for (int y = 0; y < world.chunkHeight; y++) { for (int z = chunk.chunkPosZ; z < chunk.chunkPosZ + world.chunkSize; z++) { chunk.byteArray[i] = world.data[x, y, z]; i++; } } } }
void SaveBytesToData(SaveChunk data) { int i = 0; for (int x = data.chunkPosX; x < data.chunkPosX + chunkSize; x++) { for (int y = 0; y < chunkHeight; y++) { for (int z = data.chunkPosZ; z < chunkSize + data.chunkPosZ; z++) { this.data[x, y, z] = data.byteArray[i]; i++; } } } }
public SaveChunk LoadChunkData(int x, int z) { string path = Application.persistentDataPath + "/SaveSlot/Chunk[" + x + ";" + z + "].save"; if (File.Exists(path)) { BinaryFormatter formatter = new BinaryFormatter(); FileStream stream = new FileStream(path, FileMode.Open); SaveChunk data = formatter.Deserialize(stream) as SaveChunk; stream.Close(); return(data); } else { return(null); } }
public void SaveChunkData(Chunk chunk) { if (!Directory.Exists(Application.persistentDataPath + "/SaveSlot")) { Directory.CreateDirectory(Application.persistentDataPath + "/SaveSlot"); } Debug.Log("Created"); SaveChunk data = new SaveChunk(chunk); string path = Application.persistentDataPath + "/SaveSlot/Chunk[" + data.chunkIdX + ";" + data.chunkIdZ + "].save"; BinaryFormatter formatter = new BinaryFormatter(); FileStream stream = new FileStream(path, FileMode.Create); SaveChunkBytes(data); formatter.Serialize(stream, data); stream.Close(); }
/** * @brief Chunk 정보를 불러온다. */ public static void LoadChunk(Chunk chunk, string directory = "Assets/MapData") { if (chunk == null) { return; } var parent = chunk.transform.parent; //청크 블럭 생성 parent.GetComponent <VoxelPlanet>().BuildBlocks(chunk); //파일 로드 후 수정 string path = directory + "/" + chunk.transform.parent.parent.name; if (chunk.transform.parent.name == "Terrain") { path += "/Terrain"; } else { path += "/Clouds"; } path += "/" + FileName(chunk.pos); if (!File.Exists(path)) { return; } FileStream stream = new FileStream(path, FileMode.Open); SaveChunk save = (SaveChunk)formatter.Deserialize(stream); foreach (var block in save.blocks) { chunk.blocks[block.Key.x, block.Key.y, block.Key.z] = block.Value; } chunk.update = true; stream.Close(); }
/// <summary> /// Load a <see cref="Chunk"/> /// </summary> /// <param name="chunk"></param> /// <returns></returns> public static bool LoadChunk(Chunk chunk) { //* gets the save file string saveFile = $"{savePath}/{FileName(chunk.chunkWorldPos)}.dat"; //* if the file does not exist return false if (!File.Exists(saveFile)) { return(false); } //* set all of the changed blocks in the chunk SaveChunk save = (SaveChunk)LoadFile(saveFile); foreach (var block in save.blocks) { chunk.blocks[block.Key.x, block.Key.y, block.Key.z] = block.Value; } return(true); }
/** * @brief Chunk 정보를 저장한다. */ public static void SaveChunk(Chunk chunk, string directory = "Assets/MapData") { //block 정보가 없으면 rebuild if (chunk.blocks == null) { chunk.RebuildBlock(); } SaveChunk save = new SaveChunk(chunk); if (save.blocks.Count == 0) { return; } string path = directory; if (chunk.transform.parent.name == "Terrain") { path += "/Terrain"; } else { path += "/Clouds"; } string saveFile = SaveLocation(path); saveFile += FileName(chunk.pos); Debug.Log(saveFile); //바이너리로 저장 IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(saveFile, FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(stream, save); stream.Close(); }
void CheckData() { if (saver.isFileExists() == true) { Debug.Log("1"); SaveGlobal global = saver.LoadGlobalData(); offsetX = global.offsetX; offsetZ = global.offsetZ; player = Instantiate(playerGO, new Vector3(global.position[0], global.position[1], global.position[2]), Quaternion.identity); modify.playerGO = player; saver.playerGO = player; for (int x = 0; x < chunks.GetLength(0); x++) { for (int z = 0; z < chunks.GetLength(2); z++) { SaveChunk chunk = saver.LoadChunkData(x, z); if (chunk != null) { SaveBytesToData(chunk); SaveChunkToArray(chunk.chunkIdX, chunk.chunkIdZ); } } } } else { Debug.Log("2"); player = Instantiate(playerGO, new Vector3(worldX / 2f, 50f, worldZ / 2f), Quaternion.identity); modify.playerGO = player; saver.playerGO = player; offsetX = Random.Range(0, 999999); offsetZ = Random.Range(0, 999999); } }