public byte[] Load(Stream stream, Game game, out int width, out int height, out int length) { GZipHeaderReader gsHeader = new GZipHeaderReader(); while (!gsHeader.ReadHeader(stream)) { } using (DeflateStream gs = new DeflateStream(stream, CompressionMode.Decompress)) { reader = new BinaryReader(gs); if (reader.ReadByte() != (byte)NbtTagType.Compound) { throw new InvalidDataException("Nbt file must start with Tag_Compound"); } this.game = game; map = game.World; file = new NbtFile(reader); NbtTag root = file.ReadTag((byte)NbtTagType.Compound, true); NbtCompound children = (NbtCompound)root.Value; if (children.ContainsKey("Metadata")) { ParseMetadata(children); } NbtCompound spawn = (NbtCompound)children["Spawn"].Value; LocalPlayer p = game.LocalPlayer; p.Spawn.X = (short)spawn["X"].Value; p.Spawn.Y = (short)spawn["Y"].Value; p.Spawn.Z = (short)spawn["Z"].Value; if (spawn.ContainsKey("H")) { p.SpawnRotY = (float)Utils.PackedToDegrees((byte)spawn["H"].Value); } if (spawn.ContainsKey("P")) { p.SpawnHeadX = (float)Utils.PackedToDegrees((byte)spawn["P"].Value); } map.Uuid = new Guid((byte[])children["UUID"].Value); width = (short)children["X"].Value; height = (short)children["Y"].Value; length = (short)children["Z"].Value; // Older versions incorrectly multiplied spawn coords by * 32, so we check for that. if (p.Spawn.X < 0 || p.Spawn.X >= width || p.Spawn.Y < 0 || p.Spawn.Y >= height || p.Spawn.Z < 0 || p.Spawn.Z >= length) { p.Spawn.X /= 32; p.Spawn.Y /= 32; p.Spawn.Z /= 32; } return((byte[])children["BlockArray"].Value); } }
public byte[] Load( Stream stream, Game game, out int width, out int height, out int length ) { GZipHeaderReader gsHeader = new GZipHeaderReader(); while( !gsHeader.ReadHeader( stream ) ) { } using( DeflateStream gs = new DeflateStream( stream, CompressionMode.Decompress ) ) { reader = new BinaryReader( gs ); if( reader.ReadByte() != (byte)NbtTagType.Compound ) throw new InvalidDataException( "Nbt file must start with Tag_Compound" ); this.game = game; map = game.World; file = new NbtFile( reader ); NbtTag root = file.ReadTag( (byte)NbtTagType.Compound, true ); NbtCompound children = (NbtCompound)root.Value; if( children.ContainsKey( "Metadata" ) ) ParseMetadata( children ); NbtCompound spawn = (NbtCompound)children["Spawn"].Value; LocalPlayer p = game.LocalPlayer; p.Spawn.X = (short)spawn["X"].Value; p.Spawn.Y = (short)spawn["Y"].Value; p.Spawn.Z = (short)spawn["Z"].Value; if( spawn.ContainsKey( "H" ) ) p.SpawnYaw = (float)Utils.PackedToDegrees( (byte)spawn["H"].Value ); if( spawn.ContainsKey( "P" ) ) p.SpawnPitch = (float)Utils.PackedToDegrees( (byte)spawn["P"].Value ); map.Uuid = new Guid( (byte[])children["UUID"].Value ); width = (short)children["X"].Value; height = (short)children["Y"].Value; length = (short)children["Z"].Value; // Older versions incorrectly multiplied spawn coords by * 32, so we check for that. if( p.Spawn.X < 0 || p.Spawn.X >= width || p.Spawn.Y < 0 || p.Spawn.Y >= height || p.Spawn.Z < 0 || p.Spawn.Z >= length ) { p.Spawn.X /= 32; p.Spawn.Y /= 32; p.Spawn.Z /= 32; } return (byte[])children["BlockArray"].Value; } }
public bool LoadChunk(ref Chunk chunk, string path) { try { using (FileStream fs = File.OpenRead(path)) { GZipHeaderReader gsheader = new GZipHeaderReader(); while (!gsheader.ReadHeader(fs)) { } using (DeflateStream gs = new DeflateStream(fs, CompressionMode.Decompress)) { BinaryReader reader = new BinaryReader(gs); if (reader.ReadByte() != (byte)NbtTagType.Compound) { throw new InvalidDataException("Nbt file must start with Tag_Compound"); } NbtFile file = new NbtFile(reader); NbtTag root = file.ReadTag((byte)NbtTagType.Compound, true); NbtCompound children = (NbtCompound)root.Value; if (children.ContainsKey("Level")) { NbtCompound levelChildren = (NbtCompound)children["Level"].Value; chunk.blocks = (byte[])levelChildren["Blocks"].Value; int size = 16 * 128 * 16; int halfSize = size / 2; byte[] data = (byte[])levelChildren["Data"].Value; chunk.metadata = new NibbleSlice(data, 0, halfSize); chunk.populated = ((byte)levelChildren["TerrainPopulated"].Value != 0); return(true); } } } } catch (Exception ex) { ErrorHandler.LogError("loading chunk", ex); game.Chat.Add("Failed to load chunk."); return(false); } return(false); }