Abstracted DisposeAsync to TagIO class
This commit is contained in:
parent
c1f6f3d721
commit
b5005ffdcc
|
@ -19,8 +19,6 @@ A CLS-compliant implementation of the Named Binary Tag (NBT) specifications (Jav
|
||||||
* Xamarin.Mac 5.16
|
* Xamarin.Mac 5.16
|
||||||
* Xamarin.Android 10.0
|
* Xamarin.Android 10.0
|
||||||
* Unity 2021.2.0b6
|
* Unity 2021.2.0b6
|
||||||
* **No Dependencies:** No reliance on third-party libraries.
|
|
||||||
* **Format Conversion:** Can convert NBT to/from JSON and XML formats.
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
|
|
@ -6,17 +6,18 @@ namespace SharpNBT.Tests
|
||||||
public class ConversionTest
|
public class ConversionTest
|
||||||
{
|
{
|
||||||
private readonly ITestOutputHelper output;
|
private readonly ITestOutputHelper output;
|
||||||
|
private readonly CompoundTag tag;
|
||||||
|
|
||||||
public ConversionTest(ITestOutputHelper output)
|
public ConversionTest(ITestOutputHelper output)
|
||||||
{
|
{
|
||||||
this.output = output;
|
this.output = output;
|
||||||
|
tag = TestHelper.GetTag("bigtest.nbt", CompressionType.GZip);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void JsonOutput1()
|
public void JsonOutput1()
|
||||||
{
|
{
|
||||||
var bigtest = TestHelper.GetTag("bigtest.nbt", CompressionType.GZip);
|
output.WriteLine(tag.ToJsonString(true));
|
||||||
output.WriteLine(bigtest.ToJsonString(true));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -223,11 +223,6 @@ namespace SharpNBT
|
||||||
public TagBuilder AddByteArray([NotNull] IEnumerable<sbyte> values) => AddByteArray(null, values.ToArray());
|
public TagBuilder AddByteArray([NotNull] IEnumerable<sbyte> values) => AddByteArray(null, values.ToArray());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a new <see cref="IntArrayTag"/> with the specified <paramref name="values"/> to the tree at the current depth.
|
/// Adds a new <see cref="IntArrayTag"/> with the specified <paramref name="values"/> to the tree at the current depth.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -297,19 +292,6 @@ namespace SharpNBT
|
||||||
public TagBuilder AddTag([NotNull] Tag tag)
|
public TagBuilder AddTag([NotNull] Tag tag)
|
||||||
{
|
{
|
||||||
var top = tree.Peek();
|
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)));
|
top.Add(tag ?? throw new ArgumentNullException(nameof(tag)));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
namespace SharpNBT
|
namespace SharpNBT
|
||||||
|
@ -46,6 +47,11 @@ namespace SharpNBT
|
||||||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract void Dispose();
|
public abstract void Dispose();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously releases the unmanaged resources used by the <see cref="TagIO"/> instance.
|
||||||
|
/// </summary>
|
||||||
|
public abstract ValueTask DisposeAsync();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -13,7 +13,6 @@ namespace SharpNBT
|
||||||
/// <seealso cref="TagReader.TagRead"/>
|
/// <seealso cref="TagReader.TagRead"/>
|
||||||
public delegate void TagReadCallback(TagReader reader, TagType type, Tag tag);
|
public delegate void TagReadCallback(TagReader reader, TagType type, Tag tag);
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides methods for reading NBT data from a stream.
|
/// Provides methods for reading NBT data from a stream.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -34,7 +33,7 @@ namespace SharpNBT
|
||||||
private readonly bool leaveOpen;
|
private readonly bool leaveOpen;
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <param name="stream">A <see cref="Stream"/> instance that the reader will be reading from.</param>
|
/// <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>
|
/// <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;
|
return TagType.End;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Reads a length-prefixed UTF-8 string from the stream.
|
/// Reads a length-prefixed UTF-8 string from the stream.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -449,7 +437,7 @@ namespace SharpNBT
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Asynchronously releases the unmanaged resources used by the <see cref="TagReader"/>.
|
/// Asynchronously releases the unmanaged resources used by the <see cref="TagReader"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async ValueTask DisposeAsync()
|
public override async ValueTask DisposeAsync()
|
||||||
{
|
{
|
||||||
if (!leaveOpen)
|
if (!leaveOpen)
|
||||||
await BaseStream.DisposeAsync();
|
await BaseStream.DisposeAsync();
|
||||||
|
|
|
@ -297,7 +297,7 @@ namespace SharpNBT
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Asynchronously releases the unmanaged resources used by the <see cref="TagReader"/>.
|
/// Asynchronously releases the unmanaged resources used by the <see cref="TagReader"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async ValueTask DisposeAsync()
|
public override async ValueTask DisposeAsync()
|
||||||
{
|
{
|
||||||
await BaseStream.FlushAsync();
|
await BaseStream.FlushAsync();
|
||||||
if (!leaveOpen)
|
if (!leaveOpen)
|
||||||
|
|
|
@ -22,6 +22,7 @@ namespace SharpNBT
|
||||||
{
|
{
|
||||||
typeof(TagType),
|
typeof(TagType),
|
||||||
typeof(Tag<>),
|
typeof(Tag<>),
|
||||||
|
typeof(Tag[]),
|
||||||
typeof(EnumerableTag<>),
|
typeof(EnumerableTag<>),
|
||||||
typeof(TagContainer),
|
typeof(TagContainer),
|
||||||
typeof(ByteTag),
|
typeof(ByteTag),
|
||||||
|
@ -114,20 +115,6 @@ namespace SharpNBT
|
||||||
stream.Flush();
|
stream.Flush();
|
||||||
return Encoding.UTF8.GetString(stream.ToArray());
|
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>
|
/// <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>
|
/// <param name="other">An object to compare with this object.</param>
|
||||||
|
|
Loading…
Reference in New Issue