Added generic Read methods to NbtFile for any TagContainer type instead of only CompoundTag
This commit is contained in:
parent
6ddd8c8322
commit
bb54ab6aa6
|
@ -25,26 +25,26 @@ namespace SharpNBT.Tests
|
||||||
public void CreateBigTest()
|
public void CreateBigTest()
|
||||||
{
|
{
|
||||||
|
|
||||||
var builder = new TagBuilder("Level")
|
var builder = new TagBuilder("Level")
|
||||||
.BeginCompound("nested compound test")
|
.BeginCompound("nested compound test")
|
||||||
.BeginCompound("egg").AddString("name", "Eggbert").AddFloat("value", 0.5f).EndCompound()
|
.BeginCompound("egg").AddString("name", "Eggbert").AddFloat("value", 0.5f).EndCompound()
|
||||||
.BeginCompound("ham").AddString("name", "Hampus").AddFloat("value", 0.75f).EndCompound()
|
.BeginCompound("ham").AddString("name", "Hampus").AddFloat("value", 0.75f).EndCompound()
|
||||||
.EndCompound()
|
.EndCompound()
|
||||||
.AddInt("iniTest", 2147483647)
|
.AddInt("iniTest", 2147483647)
|
||||||
.AddByte("byteTest", 127)
|
.AddByte("byteTest", 127)
|
||||||
.AddString("stringTest", "HELLO WORLD THIS IS A TEST STRING \xc5\xc4\xd6!")
|
.AddString("stringTest", "HELLO WORLD THIS IS A TEST STRING \xc5\xc4\xd6!")
|
||||||
.BeginList(TagType.Long, "listTest (long)")
|
.BeginList(TagType.Long, "listTest (long)")
|
||||||
.AddLong(11).AddLong(12).AddLong(13).AddLong(14).AddLong(15)
|
.AddLong(11).AddLong(12).AddLong(13).AddLong(14).AddLong(15)
|
||||||
.EndList()
|
.EndList()
|
||||||
.AddDouble("doubleTest", 0.49312871321823148)
|
.AddDouble("doubleTest", 0.49312871321823148)
|
||||||
.AddFloat("floatTest", 0.49823147058486938f)
|
.AddFloat("floatTest", 0.49823147058486938f)
|
||||||
.AddLong("longTest", 9223372036854775807L)
|
.AddLong("longTest", 9223372036854775807L)
|
||||||
.BeginList(TagType.Compound, "listTest (compound)")
|
.BeginList(TagType.Compound, "listTest (compound)")
|
||||||
.BeginCompound().AddLong("created-on", 1264099775885L).AddString("name", "Compound tag #0").EndCompound()
|
.BeginCompound().AddLong("created-on", 1264099775885L).AddString("name", "Compound tag #0").EndCompound()
|
||||||
.BeginCompound().AddLong("created-on", 1264099775885L).AddString("name", "Compound tag #1").EndCompound()
|
.BeginCompound().AddLong("created-on", 1264099775885L).AddString("name", "Compound tag #1").EndCompound()
|
||||||
.EndList()
|
.EndList()
|
||||||
.AddByteArray("byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))", GetByteArray())
|
.AddByteArray("byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))", GetByteArray())
|
||||||
.AddShort("shortTest", 32767);
|
.AddShort("shortTest", 32767);
|
||||||
|
|
||||||
output.WriteLine(builder.Create().PrettyPrinted());
|
output.WriteLine(builder.Create().PrettyPrinted());
|
||||||
}
|
}
|
||||||
|
@ -105,5 +105,16 @@ namespace SharpNBT.Tests
|
||||||
|
|
||||||
output.WriteLine(tb.Create().PrettyPrinted());
|
output.WriteLine(tb.Create().PrettyPrinted());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test3()
|
||||||
|
{
|
||||||
|
var builder = new TagBuilder("My NBT Structure");
|
||||||
|
builder.AddInt("Health", 9000);
|
||||||
|
builder.AddString("PlayerName", "Herobrine");
|
||||||
|
var result = builder.Create();
|
||||||
|
|
||||||
|
output.WriteLine(result.PrettyPrinted());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -13,6 +13,21 @@ namespace SharpNBT
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public static class NbtFile
|
public static class NbtFile
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reads a file at the given <paramref name="path"/> and deserializes the top-level <see cref="Tag"/> contained in the file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">The path to the file to be read.</param>
|
||||||
|
/// <param name="compression">Indicates the compression algorithm used to compress the file.</param>
|
||||||
|
/// <param name="options">Bitwise flags to configure how data should be handled for compatibility between different specifications.</param>
|
||||||
|
/// <typeparam name="T">The type of tag to deserialize.</typeparam>
|
||||||
|
/// <returns>The deserialized <see cref="Tag"/> instance.</returns>
|
||||||
|
public static T Read<T>([NotNull] string path, FormatOptions options, CompressionType compression = CompressionType.AutoDetect) where T : TagContainer
|
||||||
|
{
|
||||||
|
using var reader = new TagReader(GetReadStream(path, compression), options);
|
||||||
|
return reader.ReadTag<T>();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Reads a file at the given <paramref name="path"/> and deserializes the top-level <see cref="CompoundTag"/> contained in the file.
|
/// Reads a file at the given <paramref name="path"/> and deserializes the top-level <see cref="CompoundTag"/> contained in the file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -26,6 +41,19 @@ namespace SharpNBT
|
||||||
return reader.ReadTag<CompoundTag>();
|
return reader.ReadTag<CompoundTag>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously reads a file at the given <paramref name="path"/> and deserializes the top-level <see cref="Tag"/> contained in the file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">The path to the file to be read.</param>
|
||||||
|
/// <param name="compression">Indicates the compression algorithm used to compress the file.</param>
|
||||||
|
/// <param name="options">Bitwise flags to configure how data should be handled for compatibility between different specifications.</param>
|
||||||
|
/// <returns>The deserialized <see cref="Tag"/> instance.</returns>
|
||||||
|
public static async Task<T> ReadAsync<T>([NotNull] string path, FormatOptions options, CompressionType compression = CompressionType.AutoDetect) where T : TagContainer
|
||||||
|
{
|
||||||
|
await using var reader = new TagReader(GetReadStream(path, compression), options);
|
||||||
|
return await reader.ReadTagAsync<T>();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Asynchronously reads a file at the given <paramref name="path"/> and deserializes the top-level <see cref="CompoundTag"/> contained in the file.
|
/// Asynchronously reads a file at the given <paramref name="path"/> and deserializes the top-level <see cref="CompoundTag"/> contained in the file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -54,6 +82,14 @@ namespace SharpNBT
|
||||||
writer.WriteTag(tag);
|
writer.WriteTag(tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="Write(string,SharpNBT.CompoundTag,SharpNBT.FormatOptions,SharpNBT.CompressionType,System.IO.Compression.CompressionLevel)"/>
|
||||||
|
public static void Write([NotNull] string path, [NotNull] ListTag tag, FormatOptions options, CompressionType type = CompressionType.GZip, CompressionLevel level = CompressionLevel.Fastest)
|
||||||
|
{
|
||||||
|
using var stream = File.OpenWrite(path);
|
||||||
|
using var writer = new TagWriter(GetWriteStream(stream, type, level), options);
|
||||||
|
writer.WriteTag(tag);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Asynchronously writes the given <paramref name="tag"/> to a file at the specified <paramref name="path"/>.
|
/// Asynchronously writes the given <paramref name="tag"/> to a file at the specified <paramref name="path"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -69,6 +105,14 @@ namespace SharpNBT
|
||||||
await writer.WriteTagAsync(tag);
|
await writer.WriteTagAsync(tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="WriteAsync(string,SharpNBT.CompoundTag,SharpNBT.FormatOptions,SharpNBT.CompressionType,System.IO.Compression.CompressionLevel)"/>
|
||||||
|
public static async Task WriteAsync([NotNull] string path, [NotNull] ListTag tag, FormatOptions options, CompressionType type = CompressionType.GZip, CompressionLevel level = CompressionLevel.Fastest)
|
||||||
|
{
|
||||||
|
await using var stream = File.OpenWrite(path);
|
||||||
|
await using var writer = new TagWriter(GetWriteStream(stream, type, level), options);
|
||||||
|
await writer.WriteTagAsync(tag);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Opens an existing NBT file for reading, and returns a <see cref="TagReader"/> instance for it.
|
/// Opens an existing NBT file for reading, and returns a <see cref="TagReader"/> instance for it.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
Loading…
Reference in New Issue