From b5005ffdccdbae2118c793c3c51b8c6e138201a5 Mon Sep 17 00:00:00 2001 From: ForeverZer0 Date: Tue, 24 Aug 2021 22:24:28 -0400 Subject: [PATCH] Abstracted DisposeAsync to TagIO class --- README.md | 2 -- SharpNBT.Tests/ConversionTest.cs | 5 +++-- SharpNBT/TagBuilder.cs | 18 ------------------ SharpNBT/TagIO.cs | 8 +++++++- SharpNBT/TagReader.cs | 16 ++-------------- SharpNBT/TagWriter.cs | 2 +- SharpNBT/Tags/Tag.cs | 15 +-------------- 7 files changed, 14 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 38efca5..f0dfbd6 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,6 @@ A CLS-compliant implementation of the Named Binary Tag (NBT) specifications (Jav * Xamarin.Mac 5.16 * Xamarin.Android 10.0 * Unity 2021.2.0b6 -* **No Dependencies:** No reliance on third-party libraries. -* **Format Conversion:** Can convert NBT to/from JSON and XML formats. ## Usage diff --git a/SharpNBT.Tests/ConversionTest.cs b/SharpNBT.Tests/ConversionTest.cs index 5d47860..e0510f3 100644 --- a/SharpNBT.Tests/ConversionTest.cs +++ b/SharpNBT.Tests/ConversionTest.cs @@ -6,17 +6,18 @@ namespace SharpNBT.Tests public class ConversionTest { private readonly ITestOutputHelper output; + private readonly CompoundTag tag; public ConversionTest(ITestOutputHelper output) { this.output = output; + tag = TestHelper.GetTag("bigtest.nbt", CompressionType.GZip); } [Fact] public void JsonOutput1() { - var bigtest = TestHelper.GetTag("bigtest.nbt", CompressionType.GZip); - output.WriteLine(bigtest.ToJsonString(true)); + output.WriteLine(tag.ToJsonString(true)); } } } \ No newline at end of file diff --git a/SharpNBT/TagBuilder.cs b/SharpNBT/TagBuilder.cs index cc56d31..195d4bc 100644 --- a/SharpNBT/TagBuilder.cs +++ b/SharpNBT/TagBuilder.cs @@ -223,11 +223,6 @@ namespace SharpNBT public TagBuilder AddByteArray([NotNull] IEnumerable values) => AddByteArray(null, values.ToArray()); - - - - - /// /// Adds a new with the specified to the tree at the current depth. /// @@ -297,19 +292,6 @@ namespace SharpNBT public TagBuilder AddTag([NotNull] Tag tag) { var top = tree.Peek(); - if (top is ListTag list) - { - if (list.ChildType != tag.Type) - throw new ArgumentException("Tag type does not match the child type of parent ListTag.", nameof(tag)); - if (tag.Name != null) - throw new ArgumentException("Child tags within a ListTag must not be named."); - } - else if (top is CompoundTag) - { - if (tag.Name == null) - throw new FormatException("Child tags within a CompoundTag must have a named."); - } - top.Add(tag ?? throw new ArgumentNullException(nameof(tag))); return this; } diff --git a/SharpNBT/TagIO.cs b/SharpNBT/TagIO.cs index 89e4eb1..20249af 100644 --- a/SharpNBT/TagIO.cs +++ b/SharpNBT/TagIO.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using JetBrains.Annotations; namespace SharpNBT @@ -46,6 +47,11 @@ namespace SharpNBT /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public abstract void Dispose(); - + + /// + /// Asynchronously releases the unmanaged resources used by the instance. + /// + public abstract ValueTask DisposeAsync(); + } } \ No newline at end of file diff --git a/SharpNBT/TagReader.cs b/SharpNBT/TagReader.cs index a02a701..44a5b2f 100644 --- a/SharpNBT/TagReader.cs +++ b/SharpNBT/TagReader.cs @@ -13,7 +13,6 @@ namespace SharpNBT /// public delegate void TagReadCallback(TagReader reader, TagType type, Tag tag); - /// /// Provides methods for reading NBT data from a stream. /// @@ -34,7 +33,7 @@ namespace SharpNBT private readonly bool leaveOpen; /// - /// Creates a new instance of the class from the given uncompressed . + /// Creates a new instance of the class from the given . /// /// A instance that the reader will be reading from. /// Bitwise flags to configure how data should be handled for compatibility between different specifications. @@ -385,18 +384,7 @@ namespace SharpNBT return TagType.End; } } - - - - - - - - - - - /// /// Reads a length-prefixed UTF-8 string from the stream. /// @@ -449,7 +437,7 @@ namespace SharpNBT /// /// Asynchronously releases the unmanaged resources used by the . /// - public async ValueTask DisposeAsync() + public override async ValueTask DisposeAsync() { if (!leaveOpen) await BaseStream.DisposeAsync(); diff --git a/SharpNBT/TagWriter.cs b/SharpNBT/TagWriter.cs index b586048..9568af6 100644 --- a/SharpNBT/TagWriter.cs +++ b/SharpNBT/TagWriter.cs @@ -297,7 +297,7 @@ namespace SharpNBT /// /// Asynchronously releases the unmanaged resources used by the . /// - public async ValueTask DisposeAsync() + public override async ValueTask DisposeAsync() { await BaseStream.FlushAsync(); if (!leaveOpen) diff --git a/SharpNBT/Tags/Tag.cs b/SharpNBT/Tags/Tag.cs index 3577842..3bc371f 100644 --- a/SharpNBT/Tags/Tag.cs +++ b/SharpNBT/Tags/Tag.cs @@ -22,6 +22,7 @@ namespace SharpNBT { typeof(TagType), typeof(Tag<>), + typeof(Tag[]), typeof(EnumerableTag<>), typeof(TagContainer), typeof(ByteTag), @@ -114,20 +115,6 @@ namespace SharpNBT stream.Flush(); return Encoding.UTF8.GetString(stream.ToArray()); } - - public string ToXmlString() - { - var settings = new DataContractSerializerSettings - { - KnownTypes = GetKnownTypes() - }; - var serializer = new DataContractSerializer(typeof(Tag), settings); - using var stream = new MemoryStream(); - - serializer.WriteObject(stream, this); - stream.Flush(); - return Encoding.UTF8.GetString(stream.ToArray()); - } /// Indicates whether the current object is equal to another object of the same type. /// An object to compare with this object.