BinaryReader wrapper that takes care of reading primitives from an NBT stream, while taking care of endianness, string encoding, and skipping.
Inheritance: System.IO.BinaryReader
Example #1
0
 internal override bool ReadTag(NbtBinaryReader readStream)
 {
     if (readStream.Selector != null && !readStream.Selector(this)) {
         readStream.ReadInt64();
         return false;
     }
     Value = readStream.ReadInt64();
     return true;
 }
Example #2
0
        internal override bool ReadTag(NbtBinaryReader readStream)
        {
            int length = readStream.ReadInt32();

            if (length < 0)
            {
                throw new NbtFormatException("Negative length given in TAG_Byte_Array");
            }

            if (readStream.Selector != null && !readStream.Selector(this))
            {
                readStream.Skip(length);
                return(false);
            }
            Value = readStream.ReadBytes(length);
            return(true);
        }
Example #3
0
        static string GetRootNameInternal([NotNull] Stream stream, bool bigEndian)
        {
            Debug.Assert(stream != null);
            int firstByte = stream.ReadByte();

            if (firstByte < 0)
            {
                throw new EndOfStreamException();
            }
            else if (firstByte != (int)NbtTagType.Compound)
            {
                throw new NbtFormatException("Given NBT stream does not start with a TAG_Compound");
            }
            var reader = new NbtBinaryReader(stream, bigEndian);

            return(reader.ReadString());
        }
Example #4
0
        internal override void ReadTag(NbtBinaryReader readStream)
        {
            ListType = readStream.ReadTagType();

            int length = readStream.ReadInt32();

            if (length < 0)
            {
                throw new NbtFormatException("Negative list size given.");
            }

            for (int i = 0; i < length; i++)
            {
                NbtTag newTag = NbtTag.Construct(ListType);
                newTag.ReadTag(readStream);
                Tags.Add(newTag);
            }
        }
Example #5
0
        /// <summary> Initializes a new instance of the NbtReader class. </summary>
        /// <param name="stream"> Stream to read from. </param>
        /// <param name="bigEndian"> Whether NBT data is in Big-Endian encoding. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="stream"/> is <c>null</c>. </exception>
        /// <exception cref="ArgumentException"> <paramref name="stream"/> is not readable. </exception>
        public NbtReader([NotNull] Stream stream, bool bigEndian)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            SkipEndTags    = true;
            CacheTagValues = false;
            ParentTagType  = NbtTagType.Unknown;
            TagType        = NbtTagType.Unknown;

            canSeekStream = stream.CanSeek;
            if (canSeekStream)
            {
                streamStartOffset = stream.Position;
            }

            reader = new NbtBinaryReader(stream, bigEndian);
        }
Example #6
0
        internal override bool ReadTag(NbtBinaryReader readStream)
        {
            int length = readStream.ReadInt32();

            if (length < 0)
            {
                throw new NbtFormatException("Negative length given in TAG_Int_Array");
            }

            if (readStream.Selector != null && !readStream.Selector(this))
            {
                readStream.Skip(length * sizeof(int));
                return(false);
            }

            Value = new int[length];
            for (int i = 0; i < length; i++)
            {
                Value[i] = readStream.ReadInt32();
            }
            return(true);
        }
Example #7
0
        void LoadFromStreamInternal([NotNull] Stream stream, [CanBeNull] TagSelector tagSelector)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            // Make sure the first byte in this file is the tag for a TAG_Compound
            if (stream.ReadByte() != (int)NbtTagType.Compound)
            {
                throw new NbtFormatException("Given NBT stream does not start with a TAG_Compound");
            }
            NbtBinaryReader reader = new NbtBinaryReader(stream, BigEndian)
            {
                Selector = tagSelector
            };

            var rootCompound = new NbtCompound(reader.ReadString());

            rootCompound.ReadTag(reader);
            RootTag = rootCompound;
        }
Example #8
0
        void LoadFromStreamInternal(Stream stream, TagSelector tagSelector)
        {
            // Make sure the first byte in this file is the tag for a TAG_Compound
            int firstByte = stream.ReadByte();

            if (firstByte < 0)
            {
                throw new EndOfStreamException();
            }
            if (firstByte != (int)NbtTagType.Compound)
            {
                throw new NbtFormatException("Given NBT stream does not start with a TAG_Compound");
            }
            var reader = new NbtBinaryReader(stream, BigEndian)
            {
                Selector = tagSelector
            };

            var rootCompound = new NbtCompound(reader.ReadString());

            rootCompound.ReadTag(reader);
            RootTag = rootCompound;
        }
Example #9
0
 internal abstract void ReadTag(NbtBinaryReader reader);
Example #10
0
 internal override void SkipTag(NbtBinaryReader readStream)
 {
     readStream.SkipString();
 }
Example #11
0
 internal override void SkipTag(NbtBinaryReader readStream)
 {
     int length = readStream.ReadInt32();
     if (length < 0) {
         throw new NbtFormatException("Negative length given in TAG_Int_Array");
     }
     readStream.Skip(length*sizeof(int));
 }
Example #12
0
 internal override void SkipTag(NbtBinaryReader readStream)
 {
     readStream.ReadInt64();
 }
Example #13
0
        internal static NbtTag ReadUnknownTag(NbtBinaryReader readStream)
        {
            NbtTagType nextTag = readStream.ReadTagType();
            NbtTag     newTag;

            switch (nextTag)
            {
            case NbtTagType.End:
                throw new EndOfStreamException();

            //case NbtTagType.Byte:
            //    newTag = new NbtByte();
            //    break;

            //case NbtTagType.Short:
            //    newTag = new NbtShort();
            //    break;

            //case NbtTagType.Int:
            //    newTag = new NbtInt();
            //    break;

            //case NbtTagType.Long:
            //    newTag = new NbtLong();
            //    break;

            //case NbtTagType.Float:
            //    newTag = new NbtFloat();
            //    break;

            //case NbtTagType.Double:
            //    newTag = new NbtDouble();
            //    break;

            //case NbtTagType.ByteArray:
            //    newTag = new NbtByteArray();
            //    break;

            //case NbtTagType.String:
            //    newTag = new NbtString();
            //    break;

            case NbtTagType.List:
                newTag = new NbtList();
                break;

            case NbtTagType.Compound:
                newTag = new NbtCompound();
                break;

            //case NbtTagType.IntArray:
            //    newTag = new NbtIntArray();
            //    break;

            //case NbtTagType.LongArray:
            //    newTag = new NbtLongArray();
            //    break;

            default:
                throw new NbtFormatException("Unsupported tag type found in NBT_Tag: " + nextTag);
            }

            newTag.Name = readStream.ReadString();
            if (newTag.ReadTag(readStream))
            {
                return(newTag);
            }

            throw new NbtFormatException("Given NBT stream does not start with a proper TAG");
        }
Example #14
0
        void LoadFromStreamInternal([NotNull] Stream stream, [CanBeNull] TagSelector tagSelector)
        {
            // Make sure the first byte in this file is the tag for a TAG_Compound
            if (stream.ReadByte() != (int)NbtTagType.Compound) {
                throw new NbtFormatException("Given NBT stream does not start with a TAG_Compound");
            }
            var reader = new NbtBinaryReader(stream, BigEndian) {
                Selector = tagSelector
            };

            var rootCompound = new NbtCompound(reader.ReadString());
            rootCompound.ReadTag(reader);
            RootTag = rootCompound;
        }
Example #15
0
        internal override void SkipTag(NbtBinaryReader readStream)
        {
            // read list type, and make sure it's defined
            ListType = readStream.ReadTagType();

            int length = readStream.ReadInt32();

            if (length < 0)
            {
                throw new NbtFormatException("Negative list size given.");
            }

            switch (ListType)
            {
            case NbtTagType.Byte:
                readStream.Skip(length);
                break;

            case NbtTagType.Short:
                readStream.Skip(length * sizeof(short));
                break;

            case NbtTagType.Int:
                readStream.Skip(length * sizeof(int));
                break;

            case NbtTagType.Long:
                readStream.Skip(length * sizeof(long));
                break;

            case NbtTagType.Float:
                readStream.Skip(length * sizeof(float));
                break;

            case NbtTagType.Double:
                readStream.Skip(length * sizeof(double));
                break;

            default:
                for (int i = 0; i < length; i++)
                {
                    switch (listType)
                    {
                    case NbtTagType.ByteArray:
                        new NbtByteArray().SkipTag(readStream);
                        break;

                    case NbtTagType.String:
                        readStream.SkipString();
                        break;

                    case NbtTagType.List:
                        new NbtList().SkipTag(readStream);
                        break;

                    case NbtTagType.Compound:
                        new NbtCompound().SkipTag(readStream);
                        break;

                    case NbtTagType.IntArray:
                        new NbtIntArray().SkipTag(readStream);
                        break;
                    }
                }
                break;
            }
        }
Example #16
0
        internal override bool ReadTag(NbtBinaryReader readStream)
        {
            if (readStream.Selector != null && !readStream.Selector(this))
            {
                SkipTag(readStream);
                return(false);
            }

            ListType = readStream.ReadTagType();

            int length = readStream.ReadInt32();

            if (length < 0)
            {
                throw new NbtFormatException("Negative list size given.");
            }

            for (int i = 0; i < length; i++)
            {
                NbtTag newTag;
                switch (ListType)
                {
                case NbtTagType.Byte:
                    newTag = new NbtByte();
                    break;

                case NbtTagType.Short:
                    newTag = new NbtShort();
                    break;

                case NbtTagType.Int:
                    newTag = new NbtInt();
                    break;

                case NbtTagType.Long:
                    newTag = new NbtLong();
                    break;

                case NbtTagType.Float:
                    newTag = new NbtFloat();
                    break;

                case NbtTagType.Double:
                    newTag = new NbtDouble();
                    break;

                case NbtTagType.ByteArray:
                    newTag = new NbtByteArray();
                    break;

                case NbtTagType.String:
                    newTag = new NbtString();
                    break;

                case NbtTagType.List:
                    newTag = new NbtList();
                    break;

                case NbtTagType.Compound:
                    newTag = new NbtCompound();
                    break;

                case NbtTagType.IntArray:
                    newTag = new NbtIntArray();
                    break;

                default:
                    // should never happen, since ListType is checked beforehand
                    throw new NbtFormatException("Unsupported tag type found in a list: " + ListType);
                }
                newTag.Parent = this;
                if (newTag.ReadTag(readStream))
                {
                    tags.Add(newTag);
                }
            }
            return(true);
        }
Example #17
0
        internal override void SkipTag(NbtBinaryReader readStream)
        {
            while (true)
            {
                NbtTagType nextTag = readStream.ReadTagType();
                NbtTag     newTag;
                switch (nextTag)
                {
                case NbtTagType.End:
                    return;

                case NbtTagType.Byte:
                    newTag = new NbtByte();
                    break;

                case NbtTagType.Short:
                    newTag = new NbtShort();
                    break;

                case NbtTagType.Int:
                    newTag = new NbtInt();
                    break;

                case NbtTagType.Long:
                    newTag = new NbtLong();
                    break;

                case NbtTagType.Float:
                    newTag = new NbtFloat();
                    break;

                case NbtTagType.Double:
                    newTag = new NbtDouble();
                    break;

                case NbtTagType.ByteArray:
                    newTag = new NbtByteArray();
                    break;

                case NbtTagType.String:
                    newTag = new NbtString();
                    break;

                case NbtTagType.List:
                    newTag = new NbtList();
                    break;

                case NbtTagType.Compound:
                    newTag = new NbtCompound();
                    break;

                case NbtTagType.IntArray:
                    newTag = new NbtIntArray();
                    break;

                case NbtTagType.LongArray:
                    newTag = new NbtLongArray();
                    break;

                default:
                    throw new NbtFormatException("Unsupported tag type found in NBT_Compound: " + nextTag);
                }
                readStream.SkipString();
                newTag.SkipTag(readStream);
            }
        }
Example #18
0
        internal override bool ReadTag(NbtBinaryReader readStream)
        {
            if (Parent != null && readStream.Selector != null && !readStream.Selector(this))
            {
                SkipTag(readStream);
                return(false);
            }

            while (true)
            {
                NbtTagType nextTag = readStream.ReadTagType();
                NbtTag     newTag;
                switch (nextTag)
                {
                case NbtTagType.End:
                    return(true);

                case NbtTagType.Byte:
                    newTag = new NbtByte();
                    break;

                case NbtTagType.Short:
                    newTag = new NbtShort();
                    break;

                case NbtTagType.Int:
                    newTag = new NbtInt();
                    break;

                case NbtTagType.Long:
                    newTag = new NbtLong();
                    break;

                case NbtTagType.Float:
                    newTag = new NbtFloat();
                    break;

                case NbtTagType.Double:
                    newTag = new NbtDouble();
                    break;

                case NbtTagType.ByteArray:
                    newTag = new NbtByteArray();
                    break;

                case NbtTagType.String:
                    newTag = new NbtString();
                    break;

                case NbtTagType.List:
                    newTag = new NbtList();
                    break;

                case NbtTagType.Compound:
                    newTag = new NbtCompound();
                    break;

                case NbtTagType.IntArray:
                    newTag = new NbtIntArray();
                    break;

                case NbtTagType.LongArray:
                    newTag = new NbtLongArray();
                    break;

                default:
                    throw new NbtFormatException("Unsupported tag type found in NBT_Compound: " + nextTag);
                }
                newTag.Parent = this;
                newTag.Name   = readStream.ReadString();
                if (newTag.ReadTag(readStream))
                {
                    // ReSharper disable AssignNullToNotNullAttribute
                    // newTag.Name is never null
                    tags.Add(newTag.Name, newTag);
                    // ReSharper restore AssignNullToNotNullAttribute
                }
            }
        }
Example #19
0
 internal override void SkipTag( NbtBinaryReader readStream )
 {
     readStream.SkipString();
 }
Example #20
0
        static string GetRootNameInternal([NotNull] Stream stream, bool bigEndian)
        {
            Debug.Assert(stream != null);
            int firstByte = stream.ReadByte();
            if (firstByte < 0) {
                throw new EndOfStreamException();
            } else if (firstByte != (int)NbtTagType.Compound) {
                throw new NbtFormatException("Given NBT stream does not start with a TAG_Compound");
            }
            var reader = new NbtBinaryReader(stream, bigEndian);

            return reader.ReadString();
        }
Example #21
0
        internal override bool ReadTag( NbtBinaryReader readStream )
        {
            int length = readStream.ReadInt32();
            if( length < 0 ) {
                throw new NbtFormatException( "Negative length given in TAG_Byte_Array" );
            }

            if( readStream.Selector != null && !readStream.Selector( this ) ) {
                readStream.Skip( length );
                return false;
            }
            Value = readStream.ReadBytes( length );
            return true;
        }
Example #22
0
 internal override void SkipTag(NbtBinaryReader readStream)
 {
     readStream.ReadDouble();
 }
Example #23
0
        static string GetRootNameInternal([NotNull] Stream stream, bool bigEndian)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");
            if (stream.ReadByte() != (int)NbtTagType.Compound) {
                throw new NbtFormatException("Given NBT stream does not start with a TAG_Compound");
            }
            var reader = new NbtBinaryReader(stream, bigEndian);

            return reader.ReadString();
        }
Example #24
0
 internal override void SkipTag(NbtBinaryReader readStream)
 {
     readStream.ReadInt64();
 }
Example #25
0
 internal abstract void SkipTag( NbtBinaryReader readStream );
Example #26
0
 internal abstract bool ReadTag([NotNull] NbtBinaryReader readStream);
Example #27
0
 internal override void ReadTag(NbtBinaryReader reader)
 {
     Value = reader.ReadString();
 }
Example #28
0
 internal override void ReadTag(NbtBinaryReader reader)
 {
     Value = reader.ReadInt16();
 }
Example #29
0
 internal override void SkipTag(NbtBinaryReader readStream)
 {
     readStream.ReadDouble();
 }
Example #30
0
 internal override void ReadTag(NbtBinaryReader readStream)
 {
     Value = readStream.ReadByte();
 }
Example #31
0
 internal abstract bool ReadTag( NbtBinaryReader readStream );
Example #32
0
 internal abstract bool ReadTag(NbtBinaryReader readStream);
Example #33
0
 internal abstract void SkipTag(NbtBinaryReader readStream);
Example #34
0
        internal override bool ReadTag(NbtBinaryReader readStream)
        {
            int length = readStream.ReadInt32();
            if (length < 0) {
                throw new NbtFormatException("Negative length given in TAG_Int_Array");
            }

            if (readStream.Selector != null && !readStream.Selector(this)) {
                readStream.Skip(length*sizeof(int));
                return false;
            }

            Value = new int[length];
            for (int i = 0; i < length; i++) {
                Value[i] = readStream.ReadInt32();
            }
            return true;
        }
Example #35
0
 internal abstract void SkipTag([NotNull] NbtBinaryReader readStream);
Example #36
0
 internal override void ReadTag(NbtBinaryReader reader)
 {
     Value = reader.ReadDouble();
 }