import 'dart:io'; import 'package:libac_dart/nbt/NbtIo.dart'; import 'package:libac_dart/nbt/NbtUtils.dart'; import 'package:libac_dart/nbt/SnbtIo.dart'; import 'package:libac_dart/nbt/Stream.dart'; import 'package:libac_dart/nbt/Tag.dart'; import 'package:libac_dart/nbt/impl/CompoundTag.dart'; import 'package:libac_dart/nbt/impl/StringTag.dart'; import 'package:libac_dart/utils/IOTools.dart'; import 'package:libac_dart/utils/uuid/NbtUUID.dart'; import 'package:libac_dart/utils/uuid/UUID.dart'; import 'package:test/expect.dart'; import 'package:test/scaffolding.dart'; void main() { test('read non-compressed helloworld NBT', () async { print("READING : ${Directory.current.path}/test/hello_world.nbt"); CompoundTag tag = await NbtIo.read("${Directory.current.path}/test/hello_world.nbt") as CompoundTag; expect(tag.getKey(), "hello world"); expect(tag.containsKey("name"), true); expect(tag.get("name")!.asString(), "Bananrama"); }); test("write a hello-world NBT", () async { CompoundTag tag = CompoundTag(); tag.setKey("hello world"); tag.put("name", StringTag.valueOf("Bananrama")); var path = "${Directory.current.path}/build/hello_world.nbt"; await NbtIo.write(path, tag); expect(File(path).existsSync(), true); }); test('read non-compressed self-made helloworld NBT', () async { print("READING : ${Directory.current.path}/build/hello_world.nbt"); CompoundTag tag = await NbtIo.read("${Directory.current.path}/build/hello_world.nbt") as CompoundTag; expect(tag.getKey(), "hello world"); expect(tag.containsKey("name"), true); expect(tag.get("name")!.asString(), "Bananrama"); }); test('read compressed bigtest.nbt', () async { var path = "${Directory.current.path}/test/bigtest.nbt"; CompoundTag tag = await NbtIo.read(path) as CompoundTag; expect(tag.getKey(), "Level"); expect(tag.get("shortTest")!.asShort(), 32767); expect(tag.get("doubleTest")!.asDouble(), 0.4931287132182315); expect(tag.get("floatTest")!.asFloat(), 0.49823147); }); test("Generate a UUID v4, save to NBT, and read it back again", () async { var id = UUID.generate(4); CompoundTag tag = CompoundTag(); NbtUtils.writeUUID(tag, "test", NbtUUID.fromUUID(id)); var newID = NbtUtils.readUUID(tag, "test"); expect(id.toString(), newID.toString()); }); test("Read HelloWorld, Output to SNBT", () async { CompoundTag ct = await NbtIo.read("${Directory.current.path}/test/hello_world.nbt") as CompoundTag; StringBuilder sb = StringBuilder(); Tag.writeStringifiedNamedTag(ct, sb, 0); print(sb.toString()); }); test("Read BigTest, Output to SNBT", () async { CompoundTag ct = await NbtIo.read("${Directory.current.path}/test/bigtest.nbt") as CompoundTag; StringBuilder sb = StringBuilder(); Tag.writeStringifiedNamedTag(ct, sb, 0); print(sb.toString()); }); test("Write BigTest to SNBT file", () async { CompoundTag ct = await NbtIo.read("${Directory.current.path}/test/bigtest.nbt") as CompoundTag; String output = "${Directory.current.path}/build/bigtest.snbt"; File file = File(output); SnbtIo.writeToFile(output, ct); // Expect that the file exists PathHelper ph = PathHelper.builder(Directory.current.path) .resolve("build") .resolve("bigtest.snbt"); expect(ph.exists(), true); }); test("Read BigTest from SNBT file", () async { CompoundTag tag = await SnbtIo.readFromFile( "${Directory.current.path}/build/bigtest.snbt") as CompoundTag; expect(tag.containsKey("stringTest"), true); expect(tag.get("doubleTest")!.asDouble(), 0.4931287132182315); }); test("Write NULL UUID to NBT", () async { CompoundTag tag = CompoundTag(); NbtUtils.writeUUID(tag, "test", NbtUUID.fromUUID(UUID.ZERO)); NbtUUID ID = NbtUtils.readUUID(tag, "test"); expect(ID.MSB, 0); expect(ID.LSB, 0); }); test("Convert real-world NBT to SNBT and back to NBT", () async { String OriginFile = "${Directory.current.path}/test/EL-ReducedList.dat"; String OutputSNBT = "${Directory.current.path}/build/el-test.snbt"; String OutputNBT = "${Directory.current.path}/build/el-test.nbt"; CompoundTag tag = await NbtIo.read(OriginFile) as CompoundTag; await SnbtIo.writeToFile(OutputSNBT, tag); expect(File(OutputSNBT).existsSync(), true); tag = await SnbtIo.readFromFile(OutputSNBT) as CompoundTag; await NbtIo.write(OutputNBT, tag); expect(File(OutputNBT).existsSync(), true); }, timeout: Timeout(Duration(hours: 90))); test("Read Sophisticated Backpack data", () async { CompoundTag ct = await NbtIo.read("test/sophisticatedbackpacks.dat") as CompoundTag; // Convert to SNBT String snbtData = SnbtIo.writeToString(ct); // Convert snbt back to NBT CompoundTag testData = await SnbtIo.readFromString(snbtData) as CompoundTag; // Now, convert back to SNBT to validate String snbtTest = SnbtIo.writeToString(testData); if (snbtTest != snbtData) { print(" Converted data : ${snbtTest}"); } expect(snbtTest, snbtData); }, timeout: Timeout(Duration(minutes: 10))); test("Read Sophisticated Backpacks Lore Test", () async { CompoundTag CT = await SnbtIo.readFromFile("test/displayLoreTest.snbt") as CompoundTag; }, timeout: Timeout(Duration(minutes: 10))); test("Test negative numbers from SNBT", () async { String snbt = "{test: -932.0d}"; CompoundTag ct = await SnbtIo.readFromString(snbt) as CompoundTag; expect(ct["test"]!.getTagType(), TagType.Double); expect(ct["test"]!.asDouble(), -932.0); }); }