Exemple #1
0
        public NbtTag ReadAsTag()
        {
            switch (state)
            {
            case NbtParseState.Error:
                throw new InvalidReaderStateException(ErroneousStateError);

            case NbtParseState.AtStreamEnd:
                throw new EndOfStreamException();

            case NbtParseState.AtStreamBeginning:
            case NbtParseState.AtCompoundEnd:
                ReadToFollowing();
                break;
            }

            // get this tag
            NbtTag parent;

            if (TagType == NbtTagType.Compound)
            {
                parent = new NbtCompound(TagName);
            }
            else if (TagType == NbtTagType.List)
            {
                parent = new NbtList(TagName, ListType);
            }
            else if (atValue)
            {
                NbtTag result = ReadValueAsTag();
                ReadToFollowing();
                // if we're at a value tag, there are no child tags to read
                return(result);
            }
            else
            {
                // end tags cannot be read-as-tags (there is no corresponding NbtTag object)
                throw new InvalidOperationException(NoValueToReadError);
            }

            int startingDepth = Depth;
            int parentDepth   = Depth;

            do
            {
                ReadToFollowing();
                // Going up the file tree, or end of document: wrap up
                while (Depth <= parentDepth && parent.Parent != null)
                {
                    parent = parent.Parent;
                    parentDepth--;
                }
                if (Depth <= startingDepth)
                {
                    break;
                }

                NbtTag thisTag;
                if (TagType == NbtTagType.Compound)
                {
                    thisTag = new NbtCompound(TagName);
                    AddToParent(thisTag, parent);
                    parent      = thisTag;
                    parentDepth = Depth;
                }
                else if (TagType == NbtTagType.List)
                {
                    thisTag = new NbtList(TagName, ListType);
                    AddToParent(thisTag, parent);
                    parent      = thisTag;
                    parentDepth = Depth;
                }
                else if (TagType != NbtTagType.End)
                {
                    thisTag = ReadValueAsTag();
                    AddToParent(thisTag, parent);
                }
            } while (true);

            return(parent);
        }
Exemple #2
0
 /// <summary> Creates an empty NbtFile.
 /// RootTag will be set to an empty <c>NbtCompound</c> with a blank name (""). </summary>
 public NbtFile()
 {
     BigEndian  = BigEndianByDefault;
     BufferSize = DefaultBufferSize;
     rootTag    = new NbtCompound("");
 }