/// <summary> Initializes a new instance of the NbtWriter class. </summary> /// <param name="stream"> Stream to write to. </param> /// <param name="rootTagName"> Name to give to the root tag (written immediately). </param> /// <param name="bigEndian"> Whether NBT data should be in Big-Endian encoding. </param> /// <exception cref="ArgumentNullException"> <paramref name="stream"/> or <paramref name="rootTagName"/> is <c>null</c>. </exception> /// <exception cref="ArgumentException"> <paramref name="stream"/> is not writable. </exception> public NbtWriter([NotNull] Stream stream, [NotNull] String rootTagName, bool bigEndian) { if (rootTagName == null) throw new ArgumentNullException("rootTagName"); writer = new NbtBinaryWriter(stream, bigEndian); writer.Write((byte)NbtTagType.Compound); writer.Write(rootTagName); parentType = NbtTagType.Compound; }
internal SerializationInfo(NbtTagType tagType, string tagName, Action<object, object, object[]> setMethod, Func<object, object[], object> getMethod, NbtTagType? elementType) { m_tagType = tagType; m_tagName = tagName; m_setMethod = setMethod; m_getMethod = getMethod; m_elementType = elementType; }
public NbtTag Read(NbtTagType type) { int length; switch (type) { case NbtTagType.End: return null; case NbtTagType.Byte: return NbtTag.Create(_reader.ReadByte()); case NbtTagType.Short: return NbtTag.Create(_reader.ReadInt16()); case NbtTagType.Int: return NbtTag.Create(_reader.ReadInt32()); case NbtTagType.Long: return NbtTag.Create(_reader.ReadInt64()); case NbtTagType.Float: return NbtTag.Create(_reader.ReadSingle()); case NbtTagType.Double: return NbtTag.Create(_reader.ReadDouble()); case NbtTagType.ByteArray: length = _reader.ReadInt32(); return NbtTag.Create(_reader.ReadBytes(length)); case NbtTagType.String: length = _reader.ReadInt16(); byte[] array = _reader.ReadBytes(length); return NbtTag.Create(Encoding.UTF8.GetString(array)); case NbtTagType.List: NbtTagType listType = (NbtTagType)_reader.ReadByte(); if (listType < NbtTagType.Byte || listType > NbtTagType.Compound) throw new FormatException("'"+(int)type+"' is not a valid ListType."); int count = _reader.ReadInt32(); NbtTag list = NbtTag.CreateList(listType); for (int i = 0; i < count; i++) list.Add(Read(listType)); return list; case NbtTagType.Compound: NbtTag compound = NbtTag.CreateCompound(); while (true) { string name; NbtTag item = Read(out name); if (item == null) return compound; compound.Add(name, item); } case NbtTagType.IntArray: length = _reader.ReadInt32(); int[] intArray = new int[length]; for (int i = 0; i < length; i++) intArray[i] = _reader.ReadInt32(); return NbtTag.Create(intArray); default: throw new FormatException("'"+(int)type+"' is not a valid TagType."); } }
// when a multiline list contains this type, should it keep all the values on one line anyway? private static bool ShouldCompressListOf(NbtTagType type) { switch (type) { case NbtTagType.String: case NbtTagType.List: case NbtTagType.Compound: case NbtTagType.IntArray: case NbtTagType.ByteArray: case NbtTagType.LongArray: return(false); default: return(true); } }
/// <summary> Adds an tag to this NbtList. </summary> /// <param name="newTag"> The tag to add to this NbtList. </param> /// <exception cref="ArgumentNullException"> <paramref name="newTag"/> is null. </exception> /// <exception cref="ArgumentException"> If newTag does not match ListType. </exception> public void Add([NotNull] NbtTag newTag) { if (newTag == null) { throw new ArgumentNullException("newTag"); } if (listType == NbtTagType.Unknown) { listType = newTag.TagType; } else if (newTag.TagType != listType) { throw new ArgumentException("Items must be of type " + listType); } tags.Add(newTag); }
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); } }
public static bool IsNumericType(NbtTagType type) { switch (type) { case NbtTagType.Byte: case NbtTagType.Short: case NbtTagType.Int: case NbtTagType.Long: case NbtTagType.Float: case NbtTagType.Double: return(true); default: return(false); } }
public static string GetCanonicalTagName(NbtTagType type) { switch (type) { case NbtTagType.Byte: return("TAG_Byte"); case NbtTagType.ByteArray: return("TAG_Byte_Array"); case NbtTagType.Compound: return("TAG_Compound"); case NbtTagType.Double: return("TAG_Double"); case NbtTagType.End: return("TAG_End"); case NbtTagType.Float: return("TAG_Float"); case NbtTagType.Int: return("TAG_Int"); case NbtTagType.IntArray: return("TAG_Int_Array"); case NbtTagType.LongArray: return("TAG_Long_Array"); case NbtTagType.List: return("TAG_List"); case NbtTagType.Long: return("TAG_Long"); case NbtTagType.Short: return("TAG_Short"); case NbtTagType.String: return("TAG_String"); default: return(null); } }
public void Serializing1() { // check the basics of saving/loading const NbtTagType expectedListType = NbtTagType.Int; const int elements = 10; // construct nbt file var writtenFile = new NbtFile(new NbtCompound("ListTypeTest")); var writtenList = new NbtList("Entities", null, expectedListType); for (int i = 0; i < elements; i++) { writtenList.Add(new NbtInt(i)); } NbtCompound rootTag = (NbtCompound)writtenFile.RootTag; rootTag.Add(writtenList); // test saving byte[] data = writtenFile.SaveToBuffer(NbtCompression.None); // test loading var readFile = new NbtFile(); long bytesRead = readFile.LoadFromBuffer(data, 0, data.Length, NbtCompression.None); Assert.AreEqual(bytesRead, data.Length); // check contents of loaded file Assert.NotNull(readFile.RootTag); Assert.IsInstanceOf <NbtList>(readFile.RootTag["Entities"]); var readList = (NbtList)readFile.RootTag["Entities"]; Assert.AreEqual(writtenList.ListType, readList.ListType); Assert.AreEqual(readList.Count, writtenList.Count); // check .ToArray CollectionAssert.AreEquivalent(readList, readList.ToArray()); CollectionAssert.AreEquivalent(readList, readList.ToArray <NbtInt>()); // check contents of loaded list for (int i = 0; i < elements; i++) { Assert.AreEqual(readList.Get <NbtInt>(i).Value, writtenList.Get <NbtInt>(i).Value); } }
private static bool TryGetNbtTagType(Type type, out NbtTagType tagType, out NbtTagType? elementType) { elementType = null; if (type == typeof(byte)) tagType = NbtTagType.Byte; else if (type == typeof(short)) tagType = NbtTagType.Short; else if (type == typeof(int)) tagType = NbtTagType.Int; else if (type == typeof(long)) tagType = NbtTagType.Long; else if (type == typeof(float)) tagType = NbtTagType.Float; else if (type == typeof(double)) tagType = NbtTagType.Double; else if (type == typeof(byte[]) || typeof(IEnumerable<byte>).IsAssignableFrom(type)) tagType = NbtTagType.ByteArray; else if (type == typeof(string)) tagType = NbtTagType.String; else if (type == typeof(int[]) || typeof(IEnumerable<int>).IsAssignableFrom(type)) tagType = NbtTagType.IntArray; else if (type.IsArray) { NbtTagType t; TryGetNbtTagType(type.GetElementType(), out t, out elementType); elementType = t; tagType = NbtTagType.List; } else if (typeof(IEnumerable).IsAssignableFrom(type) && type.IsGenericType) { NbtTagType t; TryGetNbtTagType(type.GetGenericArguments().First(), out t, out elementType); elementType = t; tagType = NbtTagType.List; } else { tagType = NbtTagType.Compound; } return true; }
/// <summary> Begins an unnamed list tag. </summary> /// <param name="elementType"> Type of elements of this list. </param> /// <param name="size"> Number of elements in this list. Must not be negative. </param> /// <exception cref="NbtFormatException"> No more tags can be written -OR- /// a named list tag was expected -OR- a tag of a different type was expected -OR- /// the size of a parent list has been exceeded. </exception> /// <exception cref="ArgumentOutOfRangeException"> <paramref name="size"/> is negative -OR- /// <paramref name="elementType"/> is not a valid NbtTagType. </exception> public void BeginList(NbtTagType elementType, int size) { if (size < 0) { throw new ArgumentOutOfRangeException("size", "List size may not be negative."); } if (elementType < NbtTagType.Byte || elementType > NbtTagType.IntArray) { throw new ArgumentOutOfRangeException("elementType"); } EnforceConstraints(null, NbtTagType.List); GoDown(NbtTagType.List); listType = elementType; listSize = size; writer.Write((byte)elementType); writer.Write(size); }
public override NbtTag this[int tagIndex] { get { return(tags[tagIndex]); } set { if (value == null) { throw new ArgumentNullException("value"); } if (listType == NbtTagType.Unknown) { listType = value.TagType; } else if (value.TagType != listType) { throw new ArgumentException("Items must be of type " + listType); } tags[tagIndex] = value; } }
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); } }
private NbtTag ParseValue(NbtTagType tagType) { switch (tagType) { case NbtTagType.Byte: return(ParseByteValue()); case NbtTagType.Short: return(ParseShortValue()); case NbtTagType.Int: return(ParseIntValue()); case NbtTagType.Long: return(ParseLongValue()); case NbtTagType.Float: return(ParseFloatValue()); case NbtTagType.Double: return(ParseDoubleValue()); case NbtTagType.String: return(ParseStringValue()); case NbtTagType.List: return(ParseListValue()); case NbtTagType.Compound: return(ParseCompoundValue()); case NbtTagType.ByteArray: return(ParseByteArrayValue()); case NbtTagType.IntArray: return(ParseIntArrayValue()); case NbtTagType.LongArray: return(ParseLongArrayValue()); default: throw new NotImplementedException($"Tag type {tagType} is not yet implemented."); } }
public NbtCompound ReadNbtCompound() { NbtTagType t = (NbtTagType)ReadByte(); if (t != NbtTagType.Compound) { return(null); } Position--; NbtFile file = new NbtFile() { BigEndian = true, UseVarInt = false }; file.LoadFromStream(this, NbtCompression.None); return((NbtCompound)file.RootTag); }
internal void SerializeObject(NbtTagType tagType, string name, object value, NbtTagType? elementType, bool writeHeader) { switch (tagType) { case NbtTagType.Byte: m_nbtWriter.Write(name, Convert.ToByte(value), writeHeader); break; case NbtTagType.ByteArray: SerializeByteArray(name, value, writeHeader); break; case NbtTagType.Compound: SerializeCompound(name, value, writeHeader); break; case NbtTagType.Double: m_nbtWriter.Write(name, Convert.ToDouble(value), writeHeader); break; case NbtTagType.End: throw new ArgumentException("tagType End cannot be serialized.", "tagType"); case NbtTagType.Float: m_nbtWriter.Write(name, Convert.ToSingle(value), writeHeader); break; case NbtTagType.Int: m_nbtWriter.Write(name, Convert.ToInt32(value), writeHeader); break; case NbtTagType.IntArray: SerializeIntArray(name, value, writeHeader); break; case NbtTagType.List: SerializeList(name, elementType.Value, value, writeHeader); break; case NbtTagType.Long: m_nbtWriter.Write(name, Convert.ToInt64(value), writeHeader); break; case NbtTagType.Short: m_nbtWriter.Write(name, Convert.ToInt16(value), writeHeader); break; case NbtTagType.String: m_nbtWriter.Write(name, Convert.ToString(value), writeHeader); break; default: break; } }
private INbtTag ReadArray(string name, NbtTagType type) { var length = this.ReadInt32(); switch (type) { case NbtTagType.ByteArray: { var buffer = new byte[length]; this.BaseStream.Read(buffer); return(new NbtArray <byte>(name, buffer)); } case NbtTagType.IntArray: { var array = new NbtArray <int>(name, length); for (int i = 0; i < array.Count; i++) { array[i] = this.ReadInt32(); } return(array); } case NbtTagType.LongArray: { var array = new NbtArray <long>(name, length); for (int i = 0; i < array.Count; i++) { array[i] = this.ReadInt64(); } return(array); } default: throw new InvalidOperationException(); } }
/// <summary> Creates an NbtList with the given name and contents, and an explicitly specified ListType. </summary> /// <param name="tagName"> Name to assign to this tag. May be <c>null</c>. </param> /// <param name="tags"> /// Collection of tags to insert into the list. /// All tags are expected to be of the same type (matching givenListType). May be empty or <c>null</c>. /// </param> /// <param name="givenListType"> Name to assign to this tag. May be Unknown (to infer type from the first element of tags). </param> /// <exception cref="ArgumentOutOfRangeException"> <paramref name="givenListType" /> is not a recognized tag type. </exception> /// <exception cref="ArgumentException"> /// If given tags do not match <paramref name="givenListType" />, or are of mixed /// types. /// </exception> public NbtList([CanBeNull] string tagName, [CanBeNull] IEnumerable <NbtTag> tags, NbtTagType givenListType) { Name = tagName; this.tags = new List <NbtTag>(); listType = givenListType; if (!Enum.IsDefined(typeof(NbtTagType), givenListType)) { throw new ArgumentOutOfRangeException("givenListType"); } if (tags == null) { return; } foreach (var tag in tags) { Add(tag); } }
void GoDown(NbtTagType thisType) { if (nodes == null) { nodes = new Stack <NbtWriterNode>(); } var newNode = new NbtWriterNode { ParentType = parentType, ListType = listType, ListSize = listSize, ListIndex = listIndex }; nodes.Push(newNode); parentType = thisType; listType = NbtTagType.Unknown; listSize = 0; listIndex = 0; }
/// <summary> Begins an unnamed list tag. </summary> /// <param name="tagName"> Name to give to this compound tag. May not be null. </param> /// <param name="elementType"> Type of elements of this list. </param> /// <param name="size"> Number of elements in this list. Must not be negative. </param> /// <exception cref="NbtFormatException"> No more tags can be written -OR- /// an unnamed list tag was expected -OR- a tag of a different type was expected. </exception> /// <exception cref="ArgumentOutOfRangeException"> <paramref name="size"/> is negative -OR- /// <paramref name="elementType"/> is not a valid NbtTagType. </exception> public void BeginList([NotNull] string tagName, NbtTagType elementType, int size) { if (size < 0) { throw new ArgumentOutOfRangeException(nameof(size), "List size may not be negative."); } if (elementType < NbtTagType.Byte || elementType > NbtTagType.LongArray) { throw new ArgumentOutOfRangeException(nameof(elementType)); } EnforceConstraints(tagName, NbtTagType.List); GoDown(NbtTagType.List); listType = elementType; listSize = size; writer.Write((byte)NbtTagType.List); writer.Write(tagName); writer.Write((byte)elementType); writer.Write(size); }
/// <summary> Adds a tag to this NbtList. </summary> /// <param name="newTag"> The tag to add to this NbtList. </param> /// <exception cref="ArgumentNullException"> <paramref name="newTag"/> is <c>null</c>. </exception> /// <exception cref="ArgumentException"> If <paramref name="newTag"/> does not match ListType. </exception> public void Add([NotNull] NbtTag newTag) { if (newTag == null) { throw new ArgumentNullException("newTag"); } if (listType == NbtTagType.Unknown) { listType = newTag.TagType; } else if (newTag.TagType != listType) { throw new ArgumentException("Items in this list must be of type " + listType + ". Given type: " + newTag.TagType); } else if (newTag.Parent != null) { throw new ArgumentException("A tag may only be added to one compound/list at a time."); } tags.Add(newTag); newTag.Parent = this; }
/// <summary> Inserts an item to this NbtList at the specified index. </summary> /// <param name="tagIndex"> The zero-based index at which newTag should be inserted. </param> /// <param name="newTag"> The tag to insert into this NbtList. </param> /// <exception cref="ArgumentOutOfRangeException"> <paramref name="tagIndex"/> is not a valid index in this NbtList. </exception> /// <exception cref="ArgumentNullException"> <paramref name="newTag"/> is <c>null</c>. </exception> public void Insert(int tagIndex, [NotNull] NbtTag newTag) { if (newTag == null) { throw new ArgumentNullException("newTag"); } if (listType != NbtTagType.Unknown && newTag.TagType != listType) { throw new ArgumentException("Items must be of type " + listType); } else if (newTag.Parent != null) { throw new ArgumentException("A tag may only be added to one compound/list at a time."); } tags.Insert(tagIndex, newTag); if (listType == NbtTagType.Unknown) { listType = newTag.TagType; } newTag.Parent = this; }
private static bool TagTypeMatchesType(NbtTagType tagType, Type type) { switch (tagType) { case NbtTagType.UInt8: return(type == typeof(byte)); case NbtTagType.Int16: return(type == typeof(short)); case NbtTagType.Int32: return(type == typeof(int)); case NbtTagType.Int64: return(type == typeof(long)); case NbtTagType.Float32: return(type == typeof(float)); case NbtTagType.Float64: return(type == typeof(double)); case NbtTagType.UInt8Array: return(type == typeof(byte[])); case NbtTagType.Utf8String: return(type == typeof(string)); case NbtTagType.Compound: return(typeof(INbtStream).IsAssignableFrom(type)); case NbtTagType.Int32Array: return(type == typeof(int[])); case NbtTagType.Int64Array: return(type == typeof(long[])); } return(false); }
public void Serializing() { string fileName = Path.Combine(TempDir, "NbtListType.nbt"); const NbtTagType expectedListType = NbtTagType.Int; const int elements = 10; // construct nbt file NbtFile writtenFile = new NbtFile(new NbtCompound("ListTypeTest")); NbtList writtenList = new NbtList("Entities", null, expectedListType); for (int i = 0; i < elements; i++) { writtenList.Add(new NbtInt(i)); } writtenFile.RootTag.Add(writtenList); // test saving writtenFile.SaveToFile(fileName, NbtCompression.GZip); // test loading NbtFile readFile = new NbtFile(fileName); // check contents of loaded file Assert.NotNull(readFile.RootTag); Assert.IsInstanceOf <NbtList>(readFile.RootTag["Entities"]); NbtList readList = (NbtList)readFile.RootTag["Entities"]; Assert.AreEqual(readList.ListType, writtenList.ListType); Assert.AreEqual(readList.Count, writtenList.Count); // check .ToArray CollectionAssert.AreEquivalent(readList, readList.ToArray()); CollectionAssert.AreEquivalent(readList, readList.ToArray <NbtInt>()); // check contents of loaded list for (int i = 0; i < elements; i++) { Assert.AreEqual(readList.Get <NbtInt>(i).Value, writtenList.Get <NbtInt>(i).Value); } }
private void GoDown(NbtTagType thisType) { if (_nodes is null) { _nodes = new Stack <NbtWriterNode>(); } var newNode = new NbtWriterNode { ParentType = _parentType, ListType = _listType, ListSize = _listSize, ListIndex = _listIndex }; _nodes.Push(newNode); _parentType = thisType; _listType = NbtTagType.Unknown; _listSize = 0; _listIndex = 0; }
/// <summary>于指定的索引处添加子 Tag</summary> /// <param name="index">指定的索引</param> /// <param name="tag">要添加的 Tag</param> /// <exception cref="ArgumentException"><paramref name="tag"/> 不符合要求</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 不在合法范围内</exception> /// <exception cref="ArgumentNullException"><paramref name="tag"/> 为 null</exception> public void Add(int index, NbtTag tag) { if (index < 0 || index > _childTags.Count) { throw new ArgumentOutOfRangeException(nameof(index)); } if (tag == null) { throw new ArgumentNullException(nameof(tag)); } if (tag.Name != null) { throw new ArgumentException("子 Tag 不能具有名称", nameof(tag)); } Contract.EndContractBlock(); if (tag.TagType != ElementType) { if (ElementType == NbtTagType.End && tag.TagType != NbtTagType.End) { ElementType = tag.TagType; } else { throw new ArgumentException( $"{nameof(tag)} 具有不同的类型(需要 {ElementType},但获得的是{tag.TagType})或者具有类型 {NbtTagType.End}", nameof(tag)); } } // TODO: 这个检查是否必要? Contract.Assert(!_childTags.Contains(tag)); _childTags.Insert(index, tag); tag.Parent = this; }
public static NbtTag CreateTag(IconSource source, NbtTagType type, NbtContainerTag parent, bool bypass_window = false) { bool has_name = parent is NbtCompound; bool has_value = NbtUtil.IsValueType(type); var tag = NbtUtil.CreateTag(type); if (bypass_window) { tag.Name = NbtUtil.GetAutomaticName(tag, parent); return(tag); } else if (has_name || has_value) { var window = new EditTagWindow(source, tag, parent, has_name, has_value, EditPurpose.Create); return(window.ShowDialog() == DialogResult.OK ? tag : null); } else { return(tag); // no customization required, example: adding a compound to a list } }
private static void SavePayload(ref BinaryWriter writer, NbtTagType type, object payload) { switch (type) { case NbtTagType.End: writer.Write((byte)0); break; case NbtTagType.Byte: writer.Write((byte)payload); break; case NbtTagType.Short: writer.Write(EndiannessConverter.ToInt16((short)payload)); break; case NbtTagType.Int: writer.Write(EndiannessConverter.ToInt32((int)payload)); break; case NbtTagType.Long: writer.Write(EndiannessConverter.ToInt64((long)payload)); break; case NbtTagType.Float: writer.Write(EndiannessConverter.ToSingle((float)payload)); break; case NbtTagType.Double: writer.Write(EndiannessConverter.ToDouble((double)payload)); break; case NbtTagType.ByteArray: { var pload = (int[])payload; writer.Write(EndiannessConverter.ToInt32(pload.Length)); foreach (var t in pload) { writer.Write(t); } } break; case NbtTagType.String: { var pload = (string)payload; writer.Write(EndiannessConverter.ToInt16((short)pload.Length)); var oString = Encoding.UTF8.GetBytes(pload); foreach (var t in oString) { writer.Write(t); } } break; case NbtTagType.List: { var pload = (List<NbtTag>)payload; writer.Write((byte)pload[0].Type); writer.Write(EndiannessConverter.ToInt32(pload.Count)); foreach (var tag in pload) { SavePayload(ref writer, tag.Type, tag.Payload); } } break; case NbtTagType.Compound: { foreach (var tag in (Dictionary<string, NbtTag>)payload) { writer.Write((byte)tag.Value.Type); SavePayload(ref writer, NbtTagType.String, tag.Key); SavePayload(ref writer, tag.Value.Type, tag.Value.Payload); } SavePayload(ref writer, NbtTagType.End, null); } break; case NbtTagType.IntArray: break; } }
/// <summary> /// Initializes a new instance of an <see cref="NbtTag"/>. /// </summary> /// <param name="name">The name.</param> /// <param name="type">The type.</param> internal NbtTag(string name, NbtTagType type) { m_name = name; m_type = type; }
protected NbtTagAttribute(NbtTagType type, string name) { m_type = type; m_name = name; m_hasName = !string.IsNullOrEmpty(name); }
public void Do_SerializeObject(NbtTagType tagType, string name, object value, NbtTagType? elementType, bool writeHeader, byte[] expected) { // Arrange MemoryStream stream = new MemoryStream(); NbtWriter writer = new NbtWriter(stream); InstanceProxy contextProxy = InstanceProxy.For("Konves.Nbt", "Konves.Nbt.Serialization.SerializationContext", writer); // Act contextProxy.Invoke("SerializeObject", tagType, name, value, elementType, writeHeader); byte[] result = stream.ToArray(); // Assert CollectionAssert.AreEqual(expected, result); }
public NbtTagList(NbtTagType type) { if (type < NbtTagType.Byte || type > NbtTagType.Compound) throw new ArgumentException("'"+(int)type+"' is not a valid ListType.", "type"); _type = type; }
internal static void WriteTagValue(this BinaryWriter bw, NbtTagType tagType) { bw.Write((byte)tagType); }
/// <summary> /// Creates a new NBT tag. /// </summary> /// <param name="name">The name of the tag.</param> /// <param name="type">The type of the tag.</param> /// <param name="payload">The payload of the tag.</param> public NbtTag(string name, NbtTagType type, object payload) : this() { Payload = payload; Name = name; Type = type; var error = false; switch (type) { case NbtTagType.End: if (payload.GetType() != typeof (byte) && (byte)payload != 0) error = true; break; case NbtTagType.Byte: if (payload.GetType() != typeof (byte)) error = true; break; case NbtTagType.Short: if (payload.GetType() != typeof (short)) error = true; break; case NbtTagType.Int: if (payload.GetType() != typeof (int)) error = true; break; case NbtTagType.Long: if (payload.GetType() != typeof (long)) error = true; break; case NbtTagType.Float: if (payload.GetType() != typeof (float)) error = true; break; case NbtTagType.Double: if (payload.GetType() != typeof (double)) error = true; break; case NbtTagType.ByteArray: if (payload.GetType() != typeof (byte[])) error = true; break; case NbtTagType.String: if (payload.GetType() != typeof (string)) error = true; break; case NbtTagType.List: if (payload.GetType() != typeof (List<NbtTag>)) error = true; break; case NbtTagType.Compound: if (payload.GetType() != typeof (Dictionary<string, NbtTag>)) error = true; break; case NbtTagType.IntArray: if (payload.GetType() != typeof (int[])) error = true; break; } if (error) throw new InvalidOperationException("NBT type differs from NBT payload."); }
void EnforceConstraints([CanBeNull] String name, NbtTagType desiredType) { if (IsDone) { throw new NbtFormatException("Cannot write any more tags: root tag has been closed."); } if (parentType == NbtTagType.List) { if (name != null) { throw new NbtFormatException("Expecting an unnamed tag."); } else if (listType != desiredType) { throw new NbtFormatException("Unexpected tag type (expected: " + listType + ", given: " + desiredType); } else if (listIndex >= listSize) { throw new NbtFormatException("Given list size exceeded."); } listIndex++; } else if (name == null) { throw new NbtFormatException("Expecting a named tag."); } }
private void Do_TryGetNbtTagType(Type type, NbtTagType expectedTagType, NbtTagType? expectedElementType) { // Arrange TypeProxy proxy = TypeProxy.For("Konves.Nbt", "Konves.Nbt.Serialization.SerializationInfo"); object[] parameters = new object[] { type, null, null }; // Act proxy.Invoke("TryGetNbtTagType", parameters); // Assert Assert.AreEqual(expectedTagType, (NbtTagType)parameters[1]); Assert.AreEqual(expectedElementType, (NbtTagType?)parameters[2]); }
internal void Write(string name, NbtTagType elementType, NbtTag[] value, bool writeHeader) { if (writeHeader) WriteTagHeader(NbtTagType.List, name); Write((byte)elementType); if (value == null) { Write((int)0); } else { Write((int)value.Length); foreach (var element in value) Write(element, false); } }
public void Write( NbtTagType v ) { writer.Write( (byte)v ); }
/// <summary> /// Writes out a tag containing the specified name and array of values. /// </summary> /// <param name="name">The tag name.</param> /// <param name="elementType">Type of the element in <paramref name="value"/>.</param> /// <param name="value">The value.</param> /// <exception cref="System.ArgumentException"><paramref name="value"/> contains elements of a type other than that specified by <paramref name="elementType"/>.</exception> /// <exception cref="System.ObjectDisposedException">The stream is closed.</exception> /// <exception cref="System.IO.IOException">An I/O error occured.</exception> public void Write(string name, NbtTagType elementType, NbtTag[] value) { Write(name, elementType, value, true); }
public void Write(NbtTagType v) { writer.Write((byte)v); }
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"); }
public void Write(NbtTagType value) { Write((byte) value); }
void GoDown(NbtTagType thisType) { if (nodes == null) { nodes = new Stack<NbtWriterNode>(); } var newNode = new NbtWriterNode { ParentType = parentType, ListType = listType, ListSize = listSize, ListIndex = listIndex }; nodes.Push(newNode); parentType = thisType; listType = NbtTagType.Unknown; listSize = 0; listIndex = 0; }
void GoUp() { if (nodes == null || nodes.Count == 0) { IsDone = true; } else { NbtWriterNode oldNode = nodes.Pop(); parentType = oldNode.ParentType; listType = oldNode.ListType; listSize = oldNode.ListSize; listIndex = oldNode.ListIndex; } }
/// <summary> /// Initializes a new instance of the <see cref="NbtList"/> class.<para /> /// 默认构造方法 /// </summary> /// <param name="elementType">指定该 <see cref="NbtList"/> 的元素类型</param> /// <param name="name">该 Tag 的名称</param> public NbtList(NbtTagType elementType, string name = null) : base(name) { ElementType = elementType; _childTags = new List <NbtTag>(); }
public void Write(NbtTagType value) { Write((byte)value); }
internal static bool ReadTag(this NbtTag tag, NbtBinaryReader readStream) { while (true) { NbtTag nbtTag; do { NbtTagType nbtTagType = readStream.ReadTagType(); switch (nbtTagType) { case NbtTagType.End: return(true); case NbtTagType.Byte: nbtTag = (NbtTag) new NbtByte(); break; case NbtTagType.Short: nbtTag = (NbtTag) new NbtShort(); break; case NbtTagType.Int: nbtTag = (NbtTag) new NbtInt(); break; case NbtTagType.Long: nbtTag = (NbtTag) new NbtLong(); break; case NbtTagType.Float: nbtTag = (NbtTag) new NbtFloat(); break; case NbtTagType.Double: nbtTag = (NbtTag) new NbtDouble(); break; case NbtTagType.ByteArray: nbtTag = (NbtTag) new NbtByteArray(); break; case NbtTagType.String: nbtTag = (NbtTag) new NbtString(); break; case NbtTagType.List: nbtTag = (NbtTag) new NbtList(); break; case NbtTagType.Compound: nbtTag = (NbtTag) new NbtCompound(); break; case NbtTagType.IntArray: nbtTag = (NbtTag) new NbtIntArray(); break; case NbtTagType.LongArray: nbtTag = (NbtTag) new NbtLongArray(); break; case NbtTagType.Unknown: return(true); break; default: throw new FormatException("Unsupported tag type found in NBT_Compound: " + (object)nbtTagType); } //nbtTag.Parent = (NbtTag) this; nbtTag.Name = readStream.ReadString(); }while (!nbtTag.ReadTag(readStream)); switch (tag.TagType) { case NbtTagType.Compound: NbtCompound compound = (NbtCompound)tag; compound.Add(nbtTag); break; case NbtTagType.List: NbtList list = (NbtList)tag; list.Add(nbtTag); break; } } }
/// <summary> Begins an unnamed list tag. </summary> /// <param name="tagName"> Name to give to this compound tag. May not be null. </param> /// <param name="elementType"> Type of elements of this list. </param> /// <param name="size"> Number of elements in this list. Must not be negative. </param> /// <exception cref="NbtFormatException"> No more tags can be written -OR- /// an unnamed list tag was expected -OR- a tag of a different type was expected. </exception> /// <exception cref="ArgumentOutOfRangeException"> <paramref name="size"/> is negative -OR- /// <paramref name="elementType"/> is not a valid NbtTagType. </exception> public void BeginList([NotNull] String tagName, NbtTagType elementType, int size) { if (size < 0) { throw new ArgumentOutOfRangeException("size", "List size may not be negative."); } if (elementType < NbtTagType.Byte || elementType > NbtTagType.IntArray) { throw new ArgumentOutOfRangeException("elementType"); } EnforceConstraints(tagName, NbtTagType.List); GoDown(NbtTagType.List); listType = elementType; listSize = size; writer.Write((byte)NbtTagType.List); writer.Write(tagName); writer.Write((byte)elementType); writer.Write(size); }
void WriteTag( NbtTagType v ) { writer.Write( (byte)v ); }
protected NbtTagAttribute(NbtTagType type) : this(type, null) { }
public void Write(NbtTagType value) { BaseStream.WriteByte((byte)value); }
public NbtListAttribute(NbtTagType elementType) : base(NbtTagType.List) { m_elementType = elementType; }
public static string GetCanonicalTagName( NbtTagType type ) { switch( type ) { case NbtTagType.Byte: return "TAG_Byte"; case NbtTagType.ByteArray: return "TAG_Byte_Array"; case NbtTagType.Compound: return "TAG_Compound"; case NbtTagType.Double: return "TAG_Double"; case NbtTagType.End: return "TAG_End"; case NbtTagType.Float: return "TAG_Float"; case NbtTagType.Int: return "TAG_Int"; case NbtTagType.IntArray: return "TAG_Int_Array"; case NbtTagType.List: return "TAG_List"; case NbtTagType.Long: return "TAG_Long"; case NbtTagType.Short: return "TAG_Short"; case NbtTagType.String: return "TAG_String"; default: return null; } }
public NbtListAttribute(string name, NbtTagType elementType) : base(NbtTagType.List, name) { m_elementType = elementType; }
public bool MoveNext() { if (source_.Position >= source_.Length) { return(false); } long before = source_.Position; try { NbtTagType nextTagType = (NbtTagType)reader_.ReadByte(); if (nextTagType == NbtTagType.EndTag) { currentTag_ = EndTag.Instance; return(source_.Position < source_.Length); } ushort nextTagNameLength = reader_.ReadUInt16(); string nextTagName = ""; if (nextTagNameLength > 0) { byte[] utf8TagName = reader_.ReadBytes(nextTagNameLength); nextTagName = Encoding.UTF8.GetString(utf8TagName); } switch (nextTagType) { case NbtTagType.Float32: case NbtTagType.Float64: case NbtTagType.Int16: case NbtTagType.Int32: case NbtTagType.Int64: case NbtTagType.UInt8: case NbtTagType.Utf8String: currentTag_ = SimpleTag.FromReader(nextTagType, nextTagName, reader_); return(true); case NbtTagType.Int32Array: case NbtTagType.Int64Array: case NbtTagType.UInt8Array: currentTag_ = NumberArrayTag.FromReader(nextTagType, nextTagName, reader_); return(true); case NbtTagType.List: currentTag_ = ListTag.FromReaderAndStream(nextTagName, reader_, this); return(true); case NbtTagType.Compound: currentTag_ = NbtCompoundTag.FromReader(nextTagName, this); return(true); default: Debug.WriteLine($"Unrecognized NBT tag {((int)nextTagType):x2} at position 0x{before:x}"); throw new Exception($"Unrecognized NBT tag {((int)nextTagType):x2} at position 0x{before:x}"); } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); source_.Seek(before, SeekOrigin.Begin); return(false); } }
internal void WriteTagHeader(NbtTagType tagType, string name) { Write((byte)tagType); Write((short)name.Length); if (!string.IsNullOrEmpty(name)) Write(name); }
/// <summary> /// Creates a new <see cref="NbtElementAttribute"/> instance. /// </summary> /// <param name="type">NBT tag type.</param> /// <param name="name">NBT tag name.</param> public NbtElementAttribute(NbtTagType type, string name) { Type = type; Name = name; }
internal void SerializeList(string name, NbtTagType elementType, object value, bool writeHeader) { object[] objects = value as object[]; if (objects == null) { IEnumerable data = (value as IEnumerable); if(data != null) objects = data.Cast<object>().ToArray(); } if (objects != null) { if (writeHeader) m_nbtWriter.WriteTagHeader(NbtTagType.List, name); m_nbtWriter.Write((byte)elementType); m_nbtWriter.Write((int)objects.Length); foreach (object obj in objects) SerializeObject(elementType, null, obj, null, false); // TODO: This may cause a bug with nested lists: eg. SomeType[][] } }