コード例 #1
0
 internal override void WriteTag(NbtBinaryWriter writeStream)
 {
     writeStream.Write(NbtTagType.Float);
     if (Name == null)
     {
         throw new NbtFormatException("Name is null");
     }
     writeStream.Write(Name);
     writeStream.Write(Value);
 }
コード例 #2
0
        internal override void WriteTag(NbtBinaryWriter writeStream)
        {
            writeStream.Write(NbtTagType.LongArray);
            if (Name == null)
            {
                throw new NbtFormatException("Name is null");
            }

            writeStream.Write(Name);
            WriteData(writeStream);
        }
コード例 #3
0
 internal override void WriteData(NbtBinaryWriter writeStream)
 {
     if (ListType == NbtTagType.Unknown)
     {
         throw new NbtFormatException("NbtList had no elements and an Unknown ListType");
     }
     writeStream.Write(ListType);
     writeStream.Write(tags.Count);
     foreach (NbtTag tag in tags)
     {
         tag.WriteData(writeStream);
     }
 }
コード例 #4
0
ファイル: HealthManager.cs プロジェクト: SharperMC/SharperMC
 public byte[] Export()
 {
     byte[] buffer;
     using (var stream = new MemoryStream())
     {
         var writer = new NbtBinaryWriter(stream, false);
         writer.Write(Health);
         writer.Write(Air);
         writer.Write(FireTick);
         writer.Write(IsOnFire);
         buffer = stream.GetBuffer();
     }
     return(buffer);
 }
コード例 #5
0
ファイル: NbtDouble.cs プロジェクト: sarhatabaot/TrueCraft
        internal override void WriteTag(NbtBinaryWriter writeStream, bool writeName)
        {
            writeStream.Write(NbtTagType.Double);
            if (writeName)
            {
                if (Name == null)
                {
                    throw new NbtFormatException("Name is null");
                }
                writeStream.Write(Name);
            }

            writeStream.Write(Value);
        }
コード例 #6
0
ファイル: ChunkColumn.cs プロジェクト: realTobby/SharpMC
        public byte[] GetMeta()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new NbtBinaryWriter(stream, true))
                {
                    writer.Write(IPAddress.HostToNetworkOrder(X));
                    writer.Write(IPAddress.HostToNetworkOrder(Z));
                    writer.Write((ushort)0xffff);                      // bitmap

                    writer.Flush();
                    writer.Close();
                }
                return(stream.ToArray());
            }
        }
コード例 #7
0
        public byte[] Export()
        {
            byte[] buffer;
            using (MemoryStream stream = new MemoryStream())
            {
                NbtBinaryWriter writer = new NbtBinaryWriter(stream, false);

                Armor.WriteTo(writer);

                Slots.WriteTo(writer);

                ItemHotbar.WriteTo(writer);

                writer.Flush();
                buffer = stream.GetBuffer();
            }
            return(buffer);
        }
コード例 #8
0
        public byte[] GetBytes()
        {
            if (_cache != null)
            {
                return(_cache);
            }

            using (MemoryStream stream = MiNetServer.MemoryStreamManager.GetStream())
            {
                NbtBinaryWriter writer = new NbtBinaryWriter(stream, true);

                writer.Write(blocks);
                writer.Write(metadata.Data);
                _cache = stream.ToArray();
            }

            return(_cache);
        }
コード例 #9
0
        private byte[] GetBytes()
        {
            if (_cache != null)
            {
                return(_cache);
            }

            MemoryStream stream = new MemoryStream();
            {
                NbtBinaryWriter writer = new NbtBinaryWriter(stream, true);

                writer.Write(blocks);
                writer.Write(metadata.Data);
                writer.Write(skylight.Data);
                writer.Write(blocklight.Data);

                //RecalcHeight();
                writer.Write(height);

                for (int i = 0; i < biomeColor.Length; i++)
                {
                    writer.Write(biomeColor[i]);
                }

                foreach (var blockEntity in BlockEntities.Values)
                {
                    NbtFile file = new NbtFile(blockEntity);
                    file.BigEndian = false;
                    writer.Write(file.SaveToBuffer(NbtCompression.None));
                }

                writer.Flush();
                writer.Close();
            }

            var bytes = stream.ToArray();

            stream.Close();

            _cache = bytes;

            return(bytes);
        }
コード例 #10
0
ファイル: ChunkColumn.cs プロジェクト: realTobby/SharpMC
        public byte[] GetChunkData()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new NbtBinaryWriter(stream, true))
                {
                    writer.WriteVarInt((Blocks.Length * 2) + Skylight.Data.Length + Blocklight.Data.Length + BiomeId.Length);

                    for (var i = 0; i < Blocks.Length; i++)
                    {
                        writer.Write((ushort)((Blocks[i] << 4) | Metadata[i]));
                    }

                    writer.Write(Blocklight.Data);
                    writer.Write(Skylight.Data);

                    writer.Write(BiomeId);

                    writer.Flush();
                    writer.Close();
                }
                return(stream.ToArray());
            }
        }
コード例 #11
0
        /// <summary> Saves this NBT file to a stream. Nothing is written to stream if RootTag is <c>null</c>. </summary>
        /// <param name="stream"> Stream to write data to. May not be <c>null</c>. </param>
        /// <param name="compression"> Compression mode to use for saving. May not be AutoDetect. </param>
        /// <returns> Number of bytes written to the stream. </returns>
        /// <exception cref="ArgumentNullException"> <paramref name="stream"/> is <c>null</c>. </exception>
        /// <exception cref="ArgumentException"> If AutoDetect was given as the <paramref name="compression"/> mode. </exception>
        /// <exception cref="ArgumentOutOfRangeException"> If an unrecognized/unsupported value was given for <paramref name="compression"/>. </exception>
        /// <exception cref="InvalidDataException"> If given stream does not support writing. </exception>
        /// <exception cref="NbtFormatException"> If RootTag is null;
        /// or if RootTag is unnamed;
        /// or if one of the NbtCompound tags contained unnamed tags;
        /// or if an NbtList tag had Unknown list type and no elements. </exception>
        public long SaveToStream([NotNull] Stream stream, NbtCompression compression)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            switch (compression)
            {
            case NbtCompression.AutoDetect:
                throw new ArgumentException("AutoDetect is not a valid NbtCompression value for saving.");

            case NbtCompression.ZLib:
            case NbtCompression.GZip:
            case NbtCompression.None:
                break;

            default:
                throw new ArgumentOutOfRangeException("compression");
            }

            if (rootTag.Name == null)
            {
                // This may trigger if root tag has been renamed
                throw new NbtFormatException(
                          "Cannot save NbtFile: Root tag is not named. Its name may be an empty string, but not null.");
            }

            long startOffset = 0;

            if (stream.CanSeek)
            {
                startOffset = stream.Position;
            }
            else
            {
                stream = new ByteCountingStream(stream);
            }

            switch (compression)
            {
            case NbtCompression.ZLib:
                stream.WriteByte(0x78);
                stream.WriteByte(0x01);
                int checksum;
                using (var compressStream = new ZLibStream(stream, CompressionMode.Compress, true)) {
                    var bufferedStream = new BufferedStream(compressStream, WriteBufferSize);
                    RootTag.WriteTag(new NbtBinaryWriter(bufferedStream, BigEndian));
                    bufferedStream.Flush();
                    checksum = compressStream.Checksum;
                }
                byte[] checksumBytes = BitConverter.GetBytes(checksum);
                if (BitConverter.IsLittleEndian)
                {
                    // Adler32 checksum is big-endian
                    Array.Reverse(checksumBytes);
                }
                stream.Write(checksumBytes, 0, checksumBytes.Length);
                break;

            case NbtCompression.GZip:
                using (var compressStream = new GZipStream(stream, CompressionMode.Compress, true)) {
                    // use a buffered stream to avoid GZipping in small increments (which has a lot of overhead)
                    var bufferedStream = new BufferedStream(compressStream, WriteBufferSize);
                    RootTag.WriteTag(new NbtBinaryWriter(bufferedStream, BigEndian));
                    bufferedStream.Flush();
                }
                break;

            case NbtCompression.None:
                var writer = new NbtBinaryWriter(stream, BigEndian);
                RootTag.WriteTag(writer);
                break;

            default:
                throw new ArgumentOutOfRangeException("compression");
            }

            if (stream.CanSeek)
            {
                return(stream.Position - startOffset);
            }
            else
            {
                return(((ByteCountingStream)stream).BytesWritten);
            }
        }
コード例 #12
0
ファイル: NbtTag.cs プロジェクト: savioacp/Obsidian
 // WriteData does not write the tag's ID byte or the name
 internal abstract void WriteData([NotNull] NbtBinaryWriter writeReader);
コード例 #13
0
ファイル: NbtShort.cs プロジェクト: aaronaxvig/fNbt-Core
 internal override void WriteData(NbtBinaryWriter writeStream)
 {
     writeStream.Write(Value);
 }
コード例 #14
0
ファイル: ChunkColumn.cs プロジェクト: oizma/MiNET
        public byte[] GetBytes()
        {
            if (_cache != null)
            {
                return(_cache);
            }

            using (MemoryStream stream = MiNetServer.MemoryStreamManager.GetStream())
            {
                NbtBinaryWriter writer = new NbtBinaryWriter(stream, true);

                int topEmpty = 0;
                for (int ci = 15; ci > 0; ci--)
                {
                    if (chunks[ci].IsAllAir())
                    {
                        topEmpty = ci + 1;
                    }
                    else
                    {
                        break;
                    }
                }

                writer.Write((byte)topEmpty);
                Log.Debug($"Saved sending {16 - topEmpty} chunks");

                for (int ci = 0; ci < topEmpty; ci++)
                {
                    writer.Write((byte)0);
                    writer.Write(chunks[ci].GetBytes());
                }

                //RecalcHeight();
                writer.Write(height);

                //BiomeUtils utils = new BiomeUtils();
                //utils.PrecomputeBiomeColors();

                //InterpolateBiomes();

                writer.Write(biomeId);

                //for (int i = 0; i < biomeId.Length; i++)
                //{
                //	//var biome = biomeId[i];
                //	int color = biomeColor[i];
                //	writer.Write((int) (color & 0x00ffffff) /*| biome << 24*/);
                //}

                short extraSize = 0;
                writer.Write(extraSize);                 // No extra data

                if (BlockEntities.Count == 0)
                {
                    //NbtFile file = new NbtFile(new NbtCompound(string.Empty)) {BigEndian = false, UseVarInt = true};
                    //file.SaveToStream(writer.BaseStream, NbtCompression.None);
                }
                else
                {
                    foreach (NbtCompound blockEntity in BlockEntities.Values.ToArray())
                    {
                        NbtFile file = new NbtFile(blockEntity)
                        {
                            BigEndian = false, UseVarInt = true
                        };
                        file.SaveToStream(writer.BaseStream, NbtCompression.None);
                    }
                }

                _cache = stream.ToArray();
            }

            return(_cache);
        }
コード例 #15
0
 internal override void WriteData(NbtBinaryWriter writeStream)
 {
     writeStream.Write(Value.Length);
     writeStream.Write(Value, 0, Value.Length);
 }
コード例 #16
0
        public byte[] GetBytes()
        {
            if (_cache != null)
            {
                return(_cache);
            }

            MemoryStream stream = new MemoryStream();
            {
                NbtBinaryWriter writer = new NbtBinaryWriter(stream, true);

                writer.Write(blocks);
                writer.Write(metadata.Data);
                writer.Write(skylight.Data);
                writer.Write(blocklight.Data);

                //RecalcHeight();
                writer.Write(height);

                BiomeUtils utils = new BiomeUtils();
                utils.PrecomputeBiomeColors();

                InterpolateBiomes();

                for (int i = 0; i < biomeId.Length; i++)
                {
                    var biome = biomeId[i];
                    int color = biomeColor[i];
                    writer.Write((color & 0x00ffffff) | biome << 24);
                }

                int extraSize = 0;
                writer.Write(extraSize);                 // No extra data

                if (BlockEntities.Count == 0)
                {
                    NbtFile file = new NbtFile(new NbtCompound(string.Empty))
                    {
                        BigEndian = false
                    };
                    file.SaveToStream(writer.BaseStream, NbtCompression.None);
                }
                else
                {
                    foreach (NbtCompound blockEntity in BlockEntities.Values.ToArray())
                    {
                        NbtFile file = new NbtFile(blockEntity)
                        {
                            BigEndian = false
                        };
                        file.SaveToStream(writer.BaseStream, NbtCompression.None);
                    }
                }

                writer.Flush();
                writer.Close();
            }

            var bytes = stream.ToArray();

            stream.Close();

            _cache = bytes;

            return(bytes);
        }
コード例 #17
0
ファイル: ChunkColumn.cs プロジェクト: LiveMC/SharpMC
        public byte[] GetChunkData()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new NbtBinaryWriter(stream, true))
                {
                    writer.WriteVarInt((Blocks.Length*2) + Skylight.Data.Length + Blocklight.Data.Length + BiomeId.Length);

                    for (var i = 0; i < Blocks.Length; i++)
                        writer.Write((ushort) ((Blocks[i] << 4) | Metadata[i]));

                    writer.Write(Blocklight.Data);
                    writer.Write(Skylight.Data);

                    writer.Write(BiomeId);

                    writer.Flush();
                    writer.Close();
                }
                return stream.ToArray();
            }
        }
コード例 #18
0
        private byte[] GetBytes()
        {
            if (_cache != null)
            {
                return(_cache);
            }

            MemoryStream stream = new MemoryStream();
            {
                NbtBinaryWriter writer = new NbtBinaryWriter(stream, true);

                writer.Write(blocks);
                writer.Write(metadata.Data);
                writer.Write(skylight.Data);
                writer.Write(blocklight.Data);

                //RecalcHeight();
                writer.Write(height);

                for (int i = 0; i < biomeColor.Length; i++)
                {
                    writer.Write(biomeColor[i]);
                }

                int extraSize = 0;
                writer.Write(extraSize);                 // No extra data

                if (BlockEntities.Count == 0)
                {
                    NbtFile file = new NbtFile(new NbtCompound(string.Empty))
                    {
                        BigEndian = false
                    };
                    writer.Write(file.SaveToBuffer(NbtCompression.None));
                }
                else
                {
                    //TODO: Not working
                    foreach (NbtCompound blockEntity in BlockEntities.Values)
                    {
                        NbtFile file = new NbtFile(blockEntity)
                        {
                            BigEndian = false
                        };
                        writer.Write(file.SaveToBuffer(NbtCompression.None));
                    }
                }


                writer.Flush();
                writer.Close();
            }

            var bytes = stream.ToArray();

            stream.Close();

            _cache = bytes;

            return(bytes);
        }
コード例 #19
0
ファイル: ChunkColumn.cs プロジェクト: LiveMC/SharpMC
        public byte[] GetMeta()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new NbtBinaryWriter(stream, true))
                {
                    writer.Write(IPAddress.HostToNetworkOrder(X));
                    writer.Write(IPAddress.HostToNetworkOrder(Z));
                    writer.Write((ushort) 0xffff); // bitmap

                    writer.Flush();
                    writer.Close();
                }
                return stream.ToArray();
            }
        }
コード例 #20
0
ファイル: ChunkColumn.cs プロジェクト: kennyvv/MiNET-1.2
        public byte[] GetBytes()
        {
            if (_cache != null)
            {
                return(_cache);
            }

            using (MemoryStream stream = MiNetServer.MemoryStreamManager.GetStream())
            {
                NbtBinaryWriter writer = new NbtBinaryWriter(stream, true);

                int topEmpty = 16;
                for (int ci = 15; ci >= 0; ci--)
                {
                    if (chunks[ci].IsAllAir())
                    {
                        topEmpty = ci;
                    }
                    else
                    {
                        break;
                    }
                }

                writer.Write((byte)topEmpty);

                int sent = 0;
                for (int ci = 0; ci < topEmpty; ci++)
                {
                    writer.Write((byte)0);
                    writer.Write(chunks[ci].GetBytes());
                    sent++;
                }

                //Log.Debug($"Saved sending {16 - sent} chunks");

                //RecalcHeight();

                byte[] ba = new byte[512];
                Buffer.BlockCopy(height, 0, ba, 0, 512);
                writer.Write(ba);
                //Log.Debug($"Heights:\n{Package.HexDump(ba)}");

                //BiomeUtils utils = new BiomeUtils();
                //utils.PrecomputeBiomeColors();

                //InterpolateBiomes();

                writer.Write(biomeId);

                //for (int i = 0; i < biomeId.Length; i++)
                //{
                //	//var biome = biomeId[i];
                //	int color = biomeColor[i];
                //	writer.Write((int) (color & 0x00ffffff) /*| biome << 24*/);
                //}

                //short extraSize = 0;
                //writer.Write(extraSize); // No extra data

                // Count = SignedVarInt (zigzag)
                // Each entry
                // - Hash SignedVarint x << 12, z << 8, y
                // - Block data short

                writer.Write((byte)0);                 // Border blocks - nope

                VarInt.WriteSInt32(stream, 0);         // Block extradata count
                //VarInt.WriteSInt32(stream, 2);
                //VarInt.WriteSInt32(stream, 1 << 12 | 1 << 8 | 4);
                //writer.Write((byte)31);
                //writer.Write((byte)0);

                if (BlockEntities.Count == 0)
                {
                    //NbtFile file = new NbtFile(new NbtCompound(string.Empty)) {BigEndian = false, UseVarInt = true};
                    //file.SaveToStream(writer.BaseStream, NbtCompression.None);
                }
                else
                {
                    foreach (NbtCompound blockEntity in BlockEntities.Values.ToArray())
                    {
                        NbtFile file = new NbtFile(blockEntity)
                        {
                            BigEndian = false, UseVarInt = true
                        };
                        file.SaveToStream(writer.BaseStream, NbtCompression.None);
                    }
                }

                _cache = stream.ToArray();
            }

            return(_cache);
        }
コード例 #21
0
 internal override void WriteData(NbtBinaryWriter writeStream) => writeStream.Write(this.Value);