/// <summary> /// Deserialisiert die Chunksäule aus dem angegebenen Stream. /// </summary> /// <param name="stream">Quellstream</param> /// <param name="definitionManager">Der verwendete DefinitionManager</param> /// <param name="columnIndex">Die Position der Säule</param> /// <param name="planetId">Der Index des Planeten</param> public void Deserialize(Stream stream, IDefinitionManager definitionManager, int planetId, Index2 columnIndex) { using (BinaryReader br = new BinaryReader(stream)) { bool longIndex = br.ReadByte() > 0; // Phase 1 (Column Meta: Heightmap, populated, chunkcount) Chunks = new Chunk[br.ReadByte()]; // Chunk Count Planet = planetId; Index = columnIndex; Populated = br.ReadBoolean(); // Populated for (int y = 0; y < Chunk.CHUNKSIZE_Y; y++) // Heightmap { for (int x = 0; x < Chunk.CHUNKSIZE_X; x++) { Heights[x, y] = br.ReadUInt16(); } } int[] counter = new int[Chunks.Length]; for (int i = 0; i < Chunks.Length; i++) // ChangeCounter { counter[i] = br.ReadInt32(); } // Phase 2 (Block Definitionen) List <IBlockDefinition> types = new List <IBlockDefinition>(); Dictionary <ushort, ushort> map = new Dictionary <ushort, ushort>(); int typecount = longIndex ? br.ReadUInt16() : br.ReadByte(); for (int i = 0; i < typecount; i++) { string typeName = br.ReadString(); IBlockDefinition[] definitions = definitionManager.GetBlockDefinitions().ToArray(); var blockDefinition = definitions.FirstOrDefault(d => d.GetType().FullName == typeName); types.Add(blockDefinition); map.Add((ushort)types.Count, (ushort)(Array.IndexOf(definitions, blockDefinition) + 1)); } // Phase 3 (Chunk Infos) for (int c = 0; c < Chunks.Length; c++) { IChunk chunk = Chunks[c] = new Chunk(new Index3(columnIndex, c), planetId); for (int i = 0; i < chunk.Blocks.Length; i++) { ushort typeIndex = longIndex ? br.ReadUInt16() : br.ReadByte(); chunk.MetaData[i] = 0; if (typeIndex > 0) { chunk.Blocks[i] = map[typeIndex]; var definition = definitionManager.GetBlockDefinitionByIndex(map[typeIndex]); if (definition.HasMetaData) { chunk.MetaData[i] = br.ReadInt32(); } } } chunk.ChangeCounter = counter[c]; } } }
/// <summary> /// Serialisiert die Chunksäule in den angegebenen Stream. /// </summary> /// <param name="stream">Zielstream</param> /// <param name="definitionManager">Der verwendete DefinitionManager</param> public void Serialize(Stream stream, IDefinitionManager definitionManager) { using (BinaryWriter bw = new BinaryWriter(stream)) { // Definitionen sammeln List<IBlockDefinition> definitions = new List<IBlockDefinition>(); for (int c = 0; c < Chunks.Length; c++) { IChunk chunk = Chunks[c]; for (int i = 0; i < chunk.Blocks.Length; i++) { if (chunk.Blocks[i] != 0) { IBlockDefinition definition = definitionManager.GetBlockDefinitionByIndex(chunk.Blocks[i]); if (!definitions.Contains(definition)) definitions.Add(definition); } } } bool longIndex = definitions.Count > 254; bw.Write((byte)((longIndex) ? 1 : 0)); // Schreibe Phase 1 (Column Meta: Heightmap, populated, chunkcount) bw.Write((byte)Chunks.Length); // Chunk Count bw.Write(Populated); // Populated for (int y = 0; y < Chunk.CHUNKSIZE_Y; y++) // Heightmap for (int x = 0; x < Chunk.CHUNKSIZE_X; x++) bw.Write((ushort)Heights[x, y]); for (int i = 0; i < Chunks.Length; i++) // Change Counter bw.Write(Chunks[i].ChangeCounter); // Schreibe Phase 2 (Block Definitionen) if (longIndex) bw.Write((ushort)definitions.Count); else bw.Write((byte)definitions.Count); foreach (var definition in definitions) bw.Write(definition.GetType().FullName); // Schreibe Phase 3 (Chunk Infos) for (int c = 0; c < Chunks.Length; c++) { IChunk chunk = Chunks[c]; for (int i = 0; i < chunk.Blocks.Length; i++) { if (chunk.Blocks[i] == 0) { // Definition Index (Air) if (longIndex) bw.Write((ushort)0); else bw.Write((byte)0); } else { // Definition Index IBlockDefinition definition = definitionManager.GetBlockDefinitionByIndex(chunk.Blocks[i]); if (longIndex) bw.Write((ushort)(definitions.IndexOf(definition) + 1)); else bw.Write((byte)(definitions.IndexOf(definition) + 1)); // Meta Data if (definition.HasMetaData) bw.Write(chunk.MetaData[i]); } } } } }
/// <summary> /// Serialisiert die Chunksäule in den angegebenen Stream. /// </summary> /// <param name="stream">Zielstream</param> /// <param name="definitionManager">Der verwendete DefinitionManager</param> public void Serialize(Stream stream, IDefinitionManager definitionManager) { using (BinaryWriter bw = new BinaryWriter(stream)) { // Definitionen sammeln List <IBlockDefinition> definitions = new List <IBlockDefinition>(); for (int c = 0; c < Chunks.Length; c++) { IChunk chunk = Chunks[c]; for (int i = 0; i < chunk.Blocks.Length; i++) { if (chunk.Blocks[i] != 0) { IBlockDefinition definition = definitionManager.GetBlockDefinitionByIndex(chunk.Blocks[i]); if (!definitions.Contains(definition)) { definitions.Add(definition); } } } } bool longIndex = definitions.Count > 254; bw.Write((byte)((longIndex) ? 1 : 0)); // Schreibe Phase 1 (Column Meta: Heightmap, populated, chunkcount) bw.Write((byte)Chunks.Length); // Chunk Count bw.Write(Populated); // Populated for (int y = 0; y < Chunk.CHUNKSIZE_Y; y++) // Heightmap { for (int x = 0; x < Chunk.CHUNKSIZE_X; x++) { bw.Write((ushort)Heights[x, y]); } } for (int i = 0; i < Chunks.Length; i++) // Change Counter { bw.Write(Chunks[i].ChangeCounter); } // Schreibe Phase 2 (Block Definitionen) if (longIndex) { bw.Write((ushort)definitions.Count); } else { bw.Write((byte)definitions.Count); } foreach (var definition in definitions) { bw.Write(definition.GetType().FullName); } // Schreibe Phase 3 (Chunk Infos) for (int c = 0; c < Chunks.Length; c++) { IChunk chunk = Chunks[c]; for (int i = 0; i < chunk.Blocks.Length; i++) { if (chunk.Blocks[i] == 0) { // Definition Index (Air) if (longIndex) { bw.Write((ushort)0); } else { bw.Write((byte)0); } } else { // Definition Index IBlockDefinition definition = definitionManager.GetBlockDefinitionByIndex(chunk.Blocks[i]); if (longIndex) { bw.Write((ushort)(definitions.IndexOf(definition) + 1)); } else { bw.Write((byte)(definitions.IndexOf(definition) + 1)); } // Meta Data if (definition.HasMetaData) { bw.Write(chunk.MetaData[i]); } } } } } }
/// <summary> /// Deserialisiert die Chunksäule aus dem angegebenen Stream. /// </summary> /// <param name="stream">Quellstream</param> /// <param name="definitionManager">Der verwendete DefinitionManager</param> /// <param name="columnIndex">Die Position der Säule</param> /// <param name="planetId">Der Index des Planeten</param> public void Deserialize(Stream stream, IDefinitionManager definitionManager, int planetId, Index2 columnIndex) { using (BinaryReader br = new BinaryReader(stream)) { bool longIndex = br.ReadByte() > 0; // Phase 1 (Column Meta: Heightmap, populated, chunkcount) Chunks = new Chunk[br.ReadByte()]; // Chunk Count Planet = planetId; Index = columnIndex; Populated = br.ReadBoolean(); // Populated for (int y = 0; y < Chunk.CHUNKSIZE_Y; y++) // Heightmap for (int x = 0; x < Chunk.CHUNKSIZE_X; x++) Heights[x, y] = br.ReadUInt16(); int[] counter = new int[Chunks.Length]; for (int i = 0; i < Chunks.Length; i++) // ChangeCounter counter[i] = br.ReadInt32(); // Phase 2 (Block Definitionen) List<IBlockDefinition> types = new List<IBlockDefinition>(); Dictionary<ushort, ushort> map = new Dictionary<ushort, ushort>(); int typecount = longIndex ? br.ReadUInt16() : br.ReadByte(); for (int i = 0; i < typecount; i++) { string typeName = br.ReadString(); IBlockDefinition[] definitions = definitionManager.GetBlockDefinitions().ToArray(); var blockDefinition = definitions.FirstOrDefault(d => d.GetType().FullName == typeName); types.Add(blockDefinition); map.Add((ushort)types.Count, (ushort)(Array.IndexOf(definitions, blockDefinition) + 1)); } // Phase 3 (Chunk Infos) for (int c = 0; c < Chunks.Length; c++) { IChunk chunk = Chunks[c] = new Chunk(new Index3(columnIndex, c), planetId); for (int i = 0; i < chunk.Blocks.Length; i++) { ushort typeIndex = longIndex ? br.ReadUInt16() : br.ReadByte(); chunk.MetaData[i] = 0; if (typeIndex > 0) { chunk.Blocks[i] = map[typeIndex]; var definition = definitionManager.GetBlockDefinitionByIndex(map[typeIndex]); if (definition.HasMetaData) chunk.MetaData[i] = br.ReadInt32(); } } chunk.ChangeCounter = counter[c]; } } }