From bb54ab6aa63f83e7e91043c75437a0aa9b02041a Mon Sep 17 00:00:00 2001 From: ForeverZer0 Date: Wed, 25 Aug 2021 17:42:37 -0400 Subject: [PATCH] Added generic Read methods to NbtFile for any TagContainer type instead of only CompoundTag --- SharpNBT.Tests/TagBuilderTest.cs | 51 +++++++++++++++++++------------- SharpNBT/NbtFile.cs | 46 +++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 21 deletions(-) diff --git a/SharpNBT.Tests/TagBuilderTest.cs b/SharpNBT.Tests/TagBuilderTest.cs index 94cd204..a17b416 100644 --- a/SharpNBT.Tests/TagBuilderTest.cs +++ b/SharpNBT.Tests/TagBuilderTest.cs @@ -25,26 +25,26 @@ namespace SharpNBT.Tests public void CreateBigTest() { - var builder = new TagBuilder("Level") - .BeginCompound("nested compound test") - .BeginCompound("egg").AddString("name", "Eggbert").AddFloat("value", 0.5f).EndCompound() - .BeginCompound("ham").AddString("name", "Hampus").AddFloat("value", 0.75f).EndCompound() - .EndCompound() - .AddInt("iniTest", 2147483647) - .AddByte("byteTest", 127) - .AddString("stringTest", "HELLO WORLD THIS IS A TEST STRING \xc5\xc4\xd6!") - .BeginList(TagType.Long, "listTest (long)") - .AddLong(11).AddLong(12).AddLong(13).AddLong(14).AddLong(15) - .EndList() - .AddDouble("doubleTest", 0.49312871321823148) - .AddFloat("floatTest", 0.49823147058486938f) - .AddLong("longTest", 9223372036854775807L) - .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 #1").EndCompound() - .EndList() - .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); + var builder = new TagBuilder("Level") + .BeginCompound("nested compound test") + .BeginCompound("egg").AddString("name", "Eggbert").AddFloat("value", 0.5f).EndCompound() + .BeginCompound("ham").AddString("name", "Hampus").AddFloat("value", 0.75f).EndCompound() + .EndCompound() + .AddInt("iniTest", 2147483647) + .AddByte("byteTest", 127) + .AddString("stringTest", "HELLO WORLD THIS IS A TEST STRING \xc5\xc4\xd6!") + .BeginList(TagType.Long, "listTest (long)") + .AddLong(11).AddLong(12).AddLong(13).AddLong(14).AddLong(15) + .EndList() + .AddDouble("doubleTest", 0.49312871321823148) + .AddFloat("floatTest", 0.49823147058486938f) + .AddLong("longTest", 9223372036854775807L) + .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 #1").EndCompound() + .EndList() + .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); output.WriteLine(builder.Create().PrettyPrinted()); } @@ -105,5 +105,16 @@ namespace SharpNBT.Tests 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()); + } } } \ No newline at end of file diff --git a/SharpNBT/NbtFile.cs b/SharpNBT/NbtFile.cs index 8a7af76..747fc52 100644 --- a/SharpNBT/NbtFile.cs +++ b/SharpNBT/NbtFile.cs @@ -13,6 +13,21 @@ namespace SharpNBT [PublicAPI] public static class NbtFile { + + /// + /// Reads a file at the given and deserializes the top-level contained in the file. + /// + /// The path to the file to be read. + /// Indicates the compression algorithm used to compress the file. + /// Bitwise flags to configure how data should be handled for compatibility between different specifications. + /// The type of tag to deserialize. + /// The deserialized instance. + public static T Read([NotNull] string path, FormatOptions options, CompressionType compression = CompressionType.AutoDetect) where T : TagContainer + { + using var reader = new TagReader(GetReadStream(path, compression), options); + return reader.ReadTag(); + } + /// /// Reads a file at the given and deserializes the top-level contained in the file. /// @@ -26,6 +41,19 @@ namespace SharpNBT return reader.ReadTag(); } + /// + /// Asynchronously reads a file at the given and deserializes the top-level contained in the file. + /// + /// The path to the file to be read. + /// Indicates the compression algorithm used to compress the file. + /// Bitwise flags to configure how data should be handled for compatibility between different specifications. + /// The deserialized instance. + public static async Task ReadAsync([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(); + } + /// /// Asynchronously reads a file at the given and deserializes the top-level contained in the file. /// @@ -38,7 +66,7 @@ namespace SharpNBT await using var reader = new TagReader(GetReadStream(path, compression), options); return await reader.ReadTagAsync(); } - + /// /// Writes the given to a file at the specified . /// @@ -54,6 +82,14 @@ namespace SharpNBT writer.WriteTag(tag); } + /// + 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); + } + /// /// Asynchronously writes the given to a file at the specified . /// @@ -68,6 +104,14 @@ namespace SharpNBT await using var writer = new TagWriter(GetWriteStream(stream, type, level), options); await writer.WriteTagAsync(tag); } + + /// + 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); + } /// /// Opens an existing NBT file for reading, and returns a instance for it.