public void WriteChunk(Chunk chunk, string path) { try { using (FileStream fs = new FileStream(path, FileMode.CreateNew, FileAccess.Write)) { using (GZipStream wrapper = new GZipStream(fs, CompressionMode.Compress)) { BinaryWriter writer = new BinaryWriter(wrapper); NbtFile nbt = new NbtFile(writer); nbt.Write(NbtTagType.Compound); nbt.Write(""); nbt.Write(NbtTagType.Compound); nbt.Write("Level"); nbt.Write(NbtTagType.Int8); nbt.Write("TerrainPopulated"); nbt.WriteUInt8((byte)1); nbt.Write(NbtTagType.Int8Array); nbt.Write("Blocks"); nbt.WriteInt32(chunk.blocks.Length); nbt.WriteBytes(chunk.blocks); nbt.Write(NbtTagType.Int8Array); nbt.Write("Data"); nbt.WriteInt32(chunk.metadata.Data.Length); nbt.WriteBytes(chunk.metadata.Data); nbt.Write(NbtTagType.End); nbt.Write(NbtTagType.End); } } } catch (Exception ex) { ErrorHandler.LogError("saving chunk", ex); game.Chat.Add("Failed to save chunk."); } }
unsafe void WriteBlockDefinitionCompound(byte id) { nbt.Write(NbtTagType.Compound, "Block" + id); bool sprite = BlockInfo.Draw[id] == DrawType.Sprite; nbt.Write(NbtTagType.Int8, "ID"); nbt.WriteUInt8(id); nbt.Write(NbtTagType.String, "Name"); nbt.Write(BlockInfo.Name[id]); nbt.Write(NbtTagType.Int8, "CollideType"); nbt.WriteUInt8((byte)BlockInfo.Collide[id]); float speed = BlockInfo.SpeedMultiplier[id]; nbt.Write(NbtTagType.Real32, "Speed"); nbt.WriteInt32(*((int *)&speed)); nbt.Write(NbtTagType.Int8Array, "Textures"); nbt.WriteInt32(6); nbt.WriteUInt8(BlockInfo.GetTextureLoc(id, Side.Top)); nbt.WriteUInt8(BlockInfo.GetTextureLoc(id, Side.Bottom)); nbt.WriteUInt8(BlockInfo.GetTextureLoc(id, Side.Left)); nbt.WriteUInt8(BlockInfo.GetTextureLoc(id, Side.Right)); nbt.WriteUInt8(BlockInfo.GetTextureLoc(id, Side.Front)); nbt.WriteUInt8(BlockInfo.GetTextureLoc(id, Side.Back)); nbt.Write(NbtTagType.Int8, "TransmitsLight"); nbt.WriteUInt8(BlockInfo.BlocksLight[id] ? 0 : 1); nbt.Write(NbtTagType.Int8, "WalkSound"); nbt.WriteUInt8(BlockInfo.DigSounds[id]); nbt.Write(NbtTagType.Int8, "FullBright"); nbt.WriteUInt8(BlockInfo.FullBright[id] ? 1 : 0); nbt.Write(NbtTagType.Int8, "Shape"); int shape = sprite ? 0 : (int)(BlockInfo.MaxBB[id].Y * 16); nbt.WriteUInt8(shape); nbt.Write(NbtTagType.Int8, "BlockDraw"); byte draw = sprite ? BlockInfo.SpriteOffset[id] : BlockInfo.Draw[id]; nbt.WriteUInt8(draw); PackedCol col = BlockInfo.FogCol[id]; nbt.Write(NbtTagType.Int8Array, "Fog"); nbt.WriteInt32(4); byte fog = (byte)(128 * BlockInfo.FogDensity[id] - 1); nbt.WriteUInt8(BlockInfo.FogDensity[id] == 0 ? (byte)0 : fog); nbt.WriteUInt8(col.R); nbt.WriteUInt8(col.G); nbt.WriteUInt8(col.B); Vector3 min = BlockInfo.MinBB[id], max = BlockInfo.MaxBB[id]; nbt.Write(NbtTagType.Int8Array, "Coords"); nbt.WriteInt32(6); nbt.WriteUInt8((byte)(min.X * 16)); nbt.WriteUInt8((byte)(min.Y * 16)); nbt.WriteUInt8((byte)(min.Z * 16)); nbt.WriteUInt8((byte)(max.X * 16)); nbt.WriteUInt8((byte)(max.Y * 16)); nbt.WriteUInt8((byte)(max.Z * 16)); nbt.Write(NbtTagType.End); }
public void Save(Stream stream, Game game) { using (GZipStream wrapper = new GZipStream(stream, CompressionMode.Compress)) { BinaryWriter writer = new BinaryWriter(wrapper); NbtFile nbt = new NbtFile(writer); World map = game.World; nbt.Write(NbtTagType.Compound); nbt.Write("Schematic"); nbt.Write(NbtTagType.String); nbt.Write("Materials"); nbt.Write("Classic"); nbt.Write(NbtTagType.Int16); nbt.Write("Width"); nbt.WriteInt16((short)map.Width); nbt.Write(NbtTagType.Int16); nbt.Write("Height"); nbt.WriteInt16((short)map.Height); nbt.Write(NbtTagType.Int16); nbt.Write("Length"); nbt.WriteInt16((short)map.Length); WriteBlocks(nbt, map.blocks1); WriteBlockData(nbt, map.blocks1.Length); nbt.Write(NbtTagType.List); nbt.Write("Entities"); nbt.Write(NbtTagType.Compound); nbt.WriteInt32(0); nbt.Write(NbtTagType.List); nbt.Write("TileEntities"); nbt.Write(NbtTagType.Compound); nbt.WriteInt32(0); nbt.Write(NbtTagType.End); } }
unsafe void WriteBlockDefinitionCompound(byte id) { BlockInfo info = game.BlockInfo; nbt.Write(NbtTagType.Compound); nbt.Write("Block" + id); nbt.Write(NbtTagType.Int8); nbt.Write("ID"); nbt.WriteUInt8(id); nbt.Write(NbtTagType.String); nbt.Write("Name"); nbt.Write(info.Name[id]); nbt.Write(NbtTagType.Int8); nbt.Write("CollideType"); nbt.WriteUInt8((byte)info.Collide[id]); float speed = info.SpeedMultiplier[id]; nbt.Write(NbtTagType.Real32); nbt.Write("Speed"); nbt.WriteInt32(*((int *)&speed)); nbt.Write(NbtTagType.Int8Array); nbt.Write("Textures"); nbt.WriteInt32(6); nbt.WriteUInt8(info.GetTextureLoc(id, Side.Top)); nbt.WriteUInt8(info.GetTextureLoc(id, Side.Bottom)); nbt.WriteUInt8(info.GetTextureLoc(id, Side.Left)); nbt.WriteUInt8(info.GetTextureLoc(id, Side.Right)); nbt.WriteUInt8(info.GetTextureLoc(id, Side.Front)); nbt.WriteUInt8(info.GetTextureLoc(id, Side.Back)); nbt.Write(NbtTagType.Int8); nbt.Write("TransmitsLight"); nbt.WriteUInt8(info.BlocksLight[id] ? 0 : 1); nbt.Write(NbtTagType.Int8); nbt.Write("WalkSound"); nbt.WriteUInt8((byte)info.DigSounds[id]); nbt.Write(NbtTagType.Int8); nbt.Write("FullBright"); nbt.WriteUInt8(info.FullBright[id] ? 1 : 0); nbt.Write(NbtTagType.Int8); nbt.Write("Shape"); nbt.WriteUInt8(GetShape(info, id)); nbt.Write(NbtTagType.Int8); nbt.Write("BlockDraw"); nbt.WriteUInt8(GetDraw(info, id)); FastColour col = info.FogColour[id]; nbt.Write(NbtTagType.Int8Array); nbt.Write("Fog"); nbt.WriteInt32(4); byte fog = (byte)(128 * info.FogDensity[id] - 1); nbt.WriteUInt8(info.FogDensity[id] == 0 ? (byte)0 : fog); nbt.WriteUInt8(col.R); nbt.WriteUInt8(col.G); nbt.WriteUInt8(col.B); Vector3 min = info.MinBB[id], max = info.MaxBB[id]; nbt.Write(NbtTagType.Int8Array); nbt.Write("Coords"); nbt.WriteInt32(6); nbt.WriteUInt8((byte)(min.X * 16)); nbt.WriteUInt8((byte)(min.Y * 16)); nbt.WriteUInt8((byte)(min.Z * 16)); nbt.WriteUInt8((byte)(max.X * 16)); nbt.WriteUInt8((byte)(max.Y * 16)); nbt.WriteUInt8((byte)(max.Z * 16)); nbt.Write(NbtTagType.End); }
public void Save(Stream stream, Game game) { using (GZipStream wrapper = new GZipStream(stream, CompressionMode.Compress)) { writer = new BinaryWriter(wrapper); nbt = new NbtFile(writer); this.game = game; map = game.World; nbt.Write(NbtTagType.Compound); nbt.Write("ClassicWorld"); nbt.Write(NbtTagType.Int8); nbt.Write("FormatVersion"); nbt.WriteInt8(1); nbt.Write(NbtTagType.Int8Array); nbt.Write("UUID"); nbt.WriteInt32(16); nbt.WriteBytes(map.Uuid.ToByteArray()); nbt.Write(NbtTagType.Int16); nbt.Write("X"); nbt.WriteInt16((short)map.Width); nbt.Write(NbtTagType.Int16); nbt.Write("Y"); nbt.WriteInt16((short)map.Height); nbt.Write(NbtTagType.Int16); nbt.Write("Z"); nbt.WriteInt16((short)map.Length); WriteSpawnCompoundTag(); nbt.Write(NbtTagType.Int8Array); nbt.Write("BlockArray"); nbt.WriteInt32(map.blocks.Length); #if USE16_BIT nbt.WriteBytes(Utils.UInt16sToUInt8s(map.blocks)); #else nbt.WriteBytes(map.blocks); #endif WriteMetadata(); nbt.Write(NbtTagType.End); } }
void WriteBlocks( NbtFile nbt, byte[] blocks ) { const int chunkSize = 64 * 1024 * 32; nbt.Write( NbtTagType.Int8Array ); nbt.Write( "Blocks" ); nbt.WriteInt32( blocks.Length ); for( int i = 0; i < blocks.Length; i += chunkSize ) { int count = Math.Min( chunkSize, blocks.Length - i ); nbt.WriteBytes( blocks, i, count ); } }
void WriteBlocks(NbtFile nbt, byte[] blocks) { const int chunkSize = 64 * 1024 * 32; nbt.Write(NbtTagType.Int8Array, "Blocks"); nbt.WriteInt32(blocks.Length); for (int i = 0; i < blocks.Length; i += chunkSize) { int count = Math.Min(chunkSize, blocks.Length - i); nbt.WriteBytes(blocks, i, count); } }
void WriteBlockData( NbtFile nbt, byte[] blocks ) { const int chunkSize = 64 * 1024; byte[] chunk = new byte[chunkSize]; nbt.Write( NbtTagType.Int8Array ); nbt.Write( "Data" ); nbt.WriteInt32( blocks.Length ); for( int i = 0; i < blocks.Length; i += chunkSize ) { // All 0 so we can skip this. int count = Math.Min( chunkSize, blocks.Length - i ); nbt.WriteBytes( chunk, 0, count ); } }
public void Save( Stream stream, Game game ) { using( GZipStream wrapper = new GZipStream( stream, CompressionMode.Compress ) ) { writer = new BinaryWriter( wrapper ); nbt = new NbtFile( writer ); this.game = game; map = game.World; nbt.Write( NbtTagType.Compound ); nbt.Write( "ClassicWorld" ); nbt.Write( NbtTagType.Int8 ); nbt.Write( "FormatVersion" ); nbt.WriteInt8( 1 ); nbt.Write( NbtTagType.Int8Array ); nbt.Write( "UUID" ); nbt.WriteInt32( 16 ); nbt.WriteBytes( map.Uuid.ToByteArray() ); nbt.Write( NbtTagType.Int16 ); nbt.Write( "X" ); nbt.WriteInt16( (short)map.Width ); nbt.Write( NbtTagType.Int16 ); nbt.Write( "Y" ); nbt.WriteInt16( (short)map.Height ); nbt.Write( NbtTagType.Int16 ); nbt.Write( "Z" ); nbt.WriteInt16( (short)map.Length ); WriteSpawnCompoundTag(); nbt.Write( NbtTagType.Int8Array ); nbt.Write( "BlockArray" ); nbt.WriteInt32( map.blocks.Length ); nbt.WriteBytes( map.blocks ); WriteMetadata(); nbt.Write( NbtTagType.End ); } }
public void Save(Stream stream, Game game) { using (GZipStream s = new GZipStream(stream, CompressionMode.Compress)) { writer = new BinaryWriter(s); nbt = new NbtFile(writer); this.game = game; map = game.World; nbt.Write(NbtTagType.Compound, "ClassicWorld"); nbt.Write(NbtTagType.Int8, "FormatVersion"); nbt.WriteUInt8(1); nbt.Write(NbtTagType.Int8Array, "UUID"); nbt.WriteInt32(16); nbt.WriteBytes(map.Uuid.ToByteArray()); nbt.Write(NbtTagType.Int16, "X"); nbt.WriteInt16((short)map.Width); nbt.Write(NbtTagType.Int16, "Y"); nbt.WriteInt16((short)map.Height); nbt.Write(NbtTagType.Int16, "Z"); nbt.WriteInt16((short)map.Length); WriteSpawnCompoundTag(); nbt.Write(NbtTagType.Int8Array, "BlockArray"); nbt.WriteInt32(map.blocks.Length); nbt.WriteBytes(map.blocks); WriteMetadata(); nbt.Write(NbtTagType.End); } }
public void Save( Stream stream, Game game ) { using( GZipStream wrapper = new GZipStream( stream, CompressionMode.Compress ) ) { BinaryWriter writer = new BinaryWriter( wrapper ); NbtFile nbt = new NbtFile( writer ); World map = game.World; nbt.Write( NbtTagType.Compound ); nbt.Write( "Schematic" ); nbt.Write( NbtTagType.String ); nbt.Write( "Materials" ); nbt.Write( "Classic" ); nbt.Write( NbtTagType.Int16 ); nbt.Write( "Width" ); nbt.WriteInt16( (short)map.Width ); nbt.Write( NbtTagType.Int16 ); nbt.Write( "Height" ); nbt.WriteInt16( (short)map.Height ); nbt.Write( NbtTagType.Int16 ); nbt.Write( "Length" ); nbt.WriteInt16( (short)map.Length ); WriteBlocks( nbt, map.blocks ); WriteBlockData( nbt, map.blocks ); nbt.Write( NbtTagType.List ); nbt.Write( "Entities" ); nbt.Write( NbtTagType.Compound ); nbt.WriteInt32( 0 ); nbt.Write( NbtTagType.List ); nbt.Write( "TileEntities" ); nbt.Write( NbtTagType.Compound ); nbt.WriteInt32( 0 ); nbt.Write( NbtTagType.End ); } }
void WriteBlockData(NbtFile nbt, int blocksLength) { const int chunkSize = 64 * 1024; byte[] chunk = new byte[chunkSize]; nbt.Write(NbtTagType.Int8Array, "Data"); nbt.WriteInt32(blocksLength); for (int i = 0; i < blocksLength; i += chunkSize) { // All 0 so we can skip this. int count = Math.Min(chunkSize, blocksLength - i); nbt.WriteBytes(chunk, 0, count); } }