static string GetRootNameInternal([NotNull] Stream stream, bool bigEndian) { if (stream == null) { throw new ArgumentNullException("stream"); } NbtBinaryReader reader = new NbtBinaryReader(stream, bigEndian); if (reader.ReadTagType() != NbtTagType.Compound) { throw new NbtFormatException("Given NBT stream does not start with a TAG_Compound"); } return(reader.ReadString()); }
internal override void ReadTag(NbtBinaryReader readStream) { while (true) { NbtTagType nextTag = readStream.ReadTagType(); if (nextTag == NbtTagType.End) { return; } NbtTag newTag = Construct(nextTag); newTag.Name = readStream.ReadString(); newTag.ReadTag(readStream); tags.Add(newTag.Name, newTag); } }
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); } }
/// <summary> Reads the next tag from the stream. </summary> /// <returns> true if the next tag was read successfully; false if there are no more tags to read. </returns> /// <exception cref="NbtFormatException"> If an error occurred while parsing data in NBT format. </exception> /// <exception cref="InvalidReaderStateException"> If NbtReader cannot recover from a previous parsing error. </exception> public bool ReadToFollowing() { switch (state) { case NbtParseState.AtStreamBeginning: // set state to error in case reader.ReadTagType throws. state = NbtParseState.Error; // read first tag, make sure it's a compound if (reader.ReadTagType() != NbtTagType.Compound) { throw new NbtFormatException("Given NBT stream does not start with a TAG_Compound"); } Depth = 1; TagType = NbtTagType.Compound; // Read root name. Advance to the first inside tag. ReadTagHeader(true); RootName = TagName; return(true); case NbtParseState.AtCompoundBeginning: GoDown(); state = NbtParseState.InCompound; goto case NbtParseState.InCompound; case NbtParseState.InCompound: state = NbtParseState.Error; if (atValue) { SkipValue(); } // Read next tag, check if we've hit the end if (canSeekStream) { TagStartOffset = (int)(reader.BaseStream.Position - streamStartOffset); } // set state to error in case reader.ReadTagType throws. TagType = reader.ReadTagType(); state = NbtParseState.InCompound; if (TagType == NbtTagType.End) { TagName = null; TagsRead++; state = NbtParseState.AtCompoundEnd; if (SkipEndTags) { TagsRead--; goto case NbtParseState.AtCompoundEnd; } else { return(true); } } else { ReadTagHeader(true); return(true); } case NbtParseState.AtListBeginning: GoDown(); ListIndex = -1; TagType = ListType; state = NbtParseState.InList; goto case NbtParseState.InList; case NbtParseState.InList: state = NbtParseState.Error; if (atValue) { SkipValue(); } ListIndex++; if (ListIndex >= ParentTagLength) { GoUp(); if (ParentTagType == NbtTagType.List) { state = NbtParseState.InList; TagType = NbtTagType.List; goto case NbtParseState.InList; } else if (ParentTagType == NbtTagType.Compound) { state = NbtParseState.InCompound; goto case NbtParseState.InCompound; } else { // This should not happen unless NbtReader is bugged throw new NbtFormatException(InvalidParentTagError); } } else { if (canSeekStream) { TagStartOffset = (int)(reader.BaseStream.Position - streamStartOffset); } state = NbtParseState.InList; ReadTagHeader(false); } return(true); case NbtParseState.AtCompoundEnd: GoUp(); if (ParentTagType == NbtTagType.List) { state = NbtParseState.InList; TagType = NbtTagType.Compound; goto case NbtParseState.InList; } else if (ParentTagType == NbtTagType.Compound) { state = NbtParseState.InCompound; goto case NbtParseState.InCompound; } else if (ParentTagType == NbtTagType.Unknown) { state = NbtParseState.AtStreamEnd; return(false); } else { // This should not happen unless NbtReader is bugged state = NbtParseState.Error; throw new NbtFormatException(InvalidParentTagError); } case NbtParseState.AtStreamEnd: // nothing left to read! return(false); default: // Parsing error, or unexpected state. throw new InvalidReaderStateException(ErroneousStateError); } }
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"); }
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; } }
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); }
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); } }
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 } } }
static string GetRootNameInternal( [NotNull] Stream stream, bool bigEndian ) { if( stream == null ) throw new ArgumentNullException( "stream" ); NbtBinaryReader reader = new NbtBinaryReader( stream, bigEndian ); if( reader.ReadTagType() != NbtTagType.Compound ) { throw new NbtFormatException( "Given NBT stream does not start with a TAG_Compound" ); } return reader.ReadString(); }