public override void WriteStartTag(string name, TagType type) { TagContainerState currentState; currentState = _state.StartTag(type); if (string.IsNullOrEmpty(name)) { name = "tag"; } if (XmlConvert.EncodeName(name) == name) { _writer.WriteStartElement(name); } else { _writer.WriteStartElement("tag"); _writer.WriteAttributeString("name", name); } if (type != TagType.End && (currentState == null || currentState.ContainerType != TagType.List)) { _writer.WriteAttributeString("type", type.ToString()); } }
public override void WriteStartTag(string name, TagType type) { TagContainerState currentState; currentState = _state.StartTag(type); if (type != TagType.End && (currentState == null || currentState.ContainerType != TagType.List)) { this.WriteValue((byte)type); this.WriteValue(name); } }
public bool Read() { switch (_readState) { case ReadState.NotInitialized: int value; value = _stream.ReadByte(); if (value != (int)TagType.Compound) { throw new InvalidDataException("Stream does not contain a NBT compound."); } _type = TagType.Compound; _readState = ReadState.Tag; _state.Start(); _state.StartTag(_type); break; case ReadState.TagEnd: case ReadState.TagType: this.SetType(_stream.ReadByte()); break; case ReadState.Tag: case ReadState.TagValue: this.MoveToNextElement(); break; case ReadState.End: _state.SetComplete(); break; default: throw new InvalidOperationException($"Unexpected read state '{_readState}'."); } return(_readState != ReadState.End); }
private Tag ReadTag(TagType defaultTagType) { Tag result; TagType type; string name; TagContainerState state; type = this.ReadTagType(defaultTagType); state = _state.StartTag(type); if (type != TagType.End && (state == null || state.ContainerType != TagType.List)) { name = _reader.GetAttribute("name"); if (string.IsNullOrEmpty(name)) { name = this.ReadTagName(); } } else { name = string.Empty; } result = null; switch (type) { case TagType.Byte: result = TagFactory.CreateTag(name, this.ReadByte()); break; case TagType.Short: result = TagFactory.CreateTag(name, this.ReadShort()); break; case TagType.Int: result = TagFactory.CreateTag(name, this.ReadInt()); break; case TagType.Long: result = TagFactory.CreateTag(name, this.ReadLong()); break; case TagType.Float: result = TagFactory.CreateTag(name, this.ReadFloat()); break; case TagType.Double: result = TagFactory.CreateTag(name, this.ReadDouble()); break; case TagType.ByteArray: result = TagFactory.CreateTag(name, this.ReadByteArray()); break; case TagType.String: result = TagFactory.CreateTag(name, this.ReadString()); break; case TagType.List: result = TagFactory.CreateTag(name, this.ReadList()); break; case TagType.Compound: result = TagFactory.CreateTag(name, this.ReadCompound()); break; case TagType.IntArray: result = TagFactory.CreateTag(name, this.ReadIntArray()); break; // Can't be hit as ReadTagType will throw // an exception for unsupported types // default: // throw new InvalidDataException($"Unrecognized tag type: {type}"); } _state.EndTag(); return(result); }
public override TagCollection ReadList() { TagCollection tags; int length; TagType listType; listType = (TagType)this.ReadByte(); if (listType < TagType.Byte || listType > TagType.IntArray) { throw new InvalidDataException($"Unexpected list type '{listType}' found."); } tags = new TagCollection(listType); length = this.ReadInt(); for (int i = 0; i < length; i++) { Tag tag; tag = null; _state.StartTag(listType); switch (listType) { case TagType.Byte: tag = TagFactory.CreateTag(this.ReadByte()); break; case TagType.ByteArray: tag = TagFactory.CreateTag(this.ReadByteArray()); break; case TagType.Compound: tag = TagFactory.CreateTag(this.ReadCompound()); break; case TagType.Double: tag = TagFactory.CreateTag(this.ReadDouble()); break; case TagType.Float: tag = TagFactory.CreateTag(this.ReadFloat()); break; case TagType.Int: tag = TagFactory.CreateTag(this.ReadInt()); break; case TagType.IntArray: tag = TagFactory.CreateTag(this.ReadIntArray()); break; case TagType.List: tag = TagFactory.CreateTag(this.ReadList()); break; case TagType.Long: tag = TagFactory.CreateTag(this.ReadLong()); break; case TagType.Short: tag = TagFactory.CreateTag(this.ReadShort()); break; case TagType.String: tag = TagFactory.CreateTag(this.ReadString()); break; // Can never be hit due to the type check above //default: // throw new InvalidDataException("Invalid list type."); } _state.EndTag(); tags.Add(tag); } return(tags); }