public NbtTag Deserialize(BinaryReader br, bool requireName) { string name = null; if (requireName) { name = br.ReadTagString(); } var elementType = br.ReadTagType(); var count = br.ReadInt32().ToggleEndian(); if (count <= 0) { return(new NbtList(elementType, name)); } var elements = new NbtTag[count]; for (var i = 0; i < count; ++i) { elements[i] = NbtTagSerializer.DeserializeTag(br, elementType, false); } return(new NbtList(elements, name)); }
public void Serialize(NbtTag tag, BinaryWriter bw) { var nbtList = (NbtList)tag; if (nbtList.Name != null) { bw.WriteTagValue(nbtList.Name); } foreach (var elem in nbtList._childTags) { NbtTagSerializer.SerializeTag(elem, bw, false); } }
public void Serialize(NbtTag tag, BinaryWriter bw, bool requireName) { var nbtCompound = (NbtCompound)tag; if (requireName) { bw.WriteTagValue(nbtCompound.Name); } foreach (var elem in nbtCompound._childTags) { NbtTagSerializer.SerializeTag(elem.Value, bw); } NbtTagSerializer.SerializeTag(new NbtEnd(), bw); }
public NbtCompound ReadAsNbtTag() { NbtCompound nbt; using (MemoryStream ms = new MemoryStream(_span.ToArray())) { using (var br = new BinaryReader(ms, Encoding.UTF8, false)) { nbt = NbtTagSerializer.DeserializeTag <NbtCompound>(br); } Advance((int)ms.Position); } return(nbt); }
public void Serialize(NbtTag tag, BinaryWriter bw, bool requireName) { var nbtList = (NbtList)tag; if (requireName) { bw.WriteTagValue(nbtList.Name); } bw.WriteTagValue(nbtList.ElementType); bw.Write(nbtList.Count.ToggleEndian()); foreach (var elem in nbtList._childTags) { NbtTagSerializer.SerializeTag(elem, bw, false, false); } }
/// <summary> /// 将内容写入流中 /// </summary> /// <param name="stream">要写入到的流</param> /// <param name="leaveOpen">写入完成后保持流的打开状态</param> public void WriteTo(Stream stream, bool leaveOpen = true) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanWrite) { throw new ArgumentException("流不可写", nameof(stream)); } Contract.EndContractBlock(); using (var bw = new BinaryWriter(stream, Encoding.UTF8, leaveOpen)) { NbtTagSerializer.SerializeTag(RootTag, bw); } }
/// <summary> /// Initializes a new instance of the <see cref="NbtFile"/> class.<para /> /// 从流中初始化 Nbt 数据 /// </summary> /// <param name="stream">要从中初始化数据的流</param> /// <param name="leaveOpen">读取完成后保持流的打开状态</param> public NbtFile(Stream stream, bool leaveOpen = true) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanRead) { throw new ArgumentException("流不可读", nameof(stream)); } Contract.EndContractBlock(); using (var br = new BinaryReader(stream, Encoding.UTF8, leaveOpen)) { RootTag = NbtTagSerializer.DeserializeTag <NbtCompound>(br); } }
/// <summary> /// Initializes a new instance of the <see cref="NbtFile"/> class.<para /> /// 从流中初始化 Nbt 数据 /// </summary> /// <param name="stream">要从中初始化数据的流</param> public NbtFile(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanRead) { throw new ArgumentException("流不可读", nameof(stream)); } Contract.EndContractBlock(); using (var br = new BinaryReader(stream)) { _rootTag = NbtTagSerializer.DeserializeTag <NbtCompound>(br, false); } }
public NbtTag Deserialize(BinaryReader br, bool requireName) { string name = null; if (requireName) { name = br.ReadTagString(); } var elements = new List <NbtTag>(); while (true) { var curElement = NbtTagSerializer.DeserializeTag(br); if (curElement.TagType == NbtTagType.End) { break; } elements.Add(curElement); } return(new NbtCompound(elements, name)); }
public static void WriteAsNbtTag(this BinaryWriter bw, NbtCompound value) { NbtTagSerializer.SerializeTag(value, bw); }
internal static void RegisterSerializer() { NbtTagSerializer.RegisterTag(NbtTagType.Byte, new Serializer()); }
static NbtShort() { NbtTagSerializer.RegisterTag(NbtTagType.Short, new Serializer()); }
static NbtEnd() { NbtTagSerializer.RegisterTag(NbtTagType.End, new Serializer()); }
static NbtDouble() { NbtTagSerializer.RegisterTag(NbtTagType.Double, new Serializer()); }
static NbtList() { NbtTagSerializer.RegisterTag(NbtTagType.List, new Serializer()); }
static NbtCompound() { NbtTagSerializer.RegisterTag(NbtTagType.Compound, new Serializer()); }
static NbtLong() { NbtTagSerializer.RegisterTag(NbtTagType.Long, new Serializer()); }
static NbtByte() { NbtTagSerializer.RegisterTag(NbtTagType.Byte, new Serializer()); }