Abstracted DisposeAsync to TagIO class

This commit is contained in:
ForeverZer0 2021-08-24 22:24:28 -04:00
parent c1f6f3d721
commit b5005ffdcc
7 changed files with 14 additions and 52 deletions

View File

@ -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

View File

@ -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));
}
}
}

View File

@ -223,11 +223,6 @@ namespace SharpNBT
public TagBuilder AddByteArray([NotNull] IEnumerable<sbyte> values) => AddByteArray(null, values.ToArray());
/// <summary>
/// Adds a new <see cref="IntArrayTag"/> with the specified <paramref name="values"/> to the tree at the current depth.
/// </summary>
@ -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;
}

View File

@ -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.
/// </summary>
public abstract void Dispose();
/// <summary>
/// Asynchronously releases the unmanaged resources used by the <see cref="TagIO"/> instance.
/// </summary>
public abstract ValueTask DisposeAsync();
}
}

View File

@ -13,7 +13,6 @@ namespace SharpNBT
/// <seealso cref="TagReader.TagRead"/>
public delegate void TagReadCallback(TagReader reader, TagType type, Tag tag);
/// <summary>
/// Provides methods for reading NBT data from a stream.
/// </summary>
@ -34,7 +33,7 @@ namespace SharpNBT
private readonly bool leaveOpen;
/// <summary>
/// Creates a new instance of the <see cref="TagReader"/> class from the given uncompressed <paramref name="stream"/>.
/// Creates a new instance of the <see cref="TagReader"/> class from the given <paramref name="stream"/>.
/// </summary>
/// <param name="stream">A <see cref="Stream"/> instance that the reader will be reading from.</param>
/// <param name="options">Bitwise flags to configure how data should be handled for compatibility between different specifications.</param>
@ -385,18 +384,7 @@ namespace SharpNBT
return TagType.End;
}
}
/// <summary>
/// Reads a length-prefixed UTF-8 string from the stream.
/// </summary>
@ -449,7 +437,7 @@ namespace SharpNBT
/// <summary>
/// Asynchronously releases the unmanaged resources used by the <see cref="TagReader"/>.
/// </summary>
public async ValueTask DisposeAsync()
public override async ValueTask DisposeAsync()
{
if (!leaveOpen)
await BaseStream.DisposeAsync();

View File

@ -297,7 +297,7 @@ namespace SharpNBT
/// <summary>
/// Asynchronously releases the unmanaged resources used by the <see cref="TagReader"/>.
/// </summary>
public async ValueTask DisposeAsync()
public override async ValueTask DisposeAsync()
{
await BaseStream.FlushAsync();
if (!leaveOpen)

View File

@ -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());
}
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
/// <param name="other">An object to compare with this object.</param>