Finish implementing - and testing SNBT

This commit is contained in:
zontreck 2024-06-06 18:14:41 -07:00
parent a0f372693b
commit dfd536ae1c
10 changed files with 57 additions and 3 deletions

View file

@ -419,6 +419,9 @@ class StringReader {
// Check if there's more to read
bool get canRead => _position < _buffer.length;
// Get the number of chars seeked
int get getSeeked => _lastPostion - _position;
// Read the next character
String next() {
if (canRead) {

View file

@ -44,6 +44,7 @@ enum TagType {
static TagType getStringifiedTagType(StringReader reader) {
reader.startSeek();
TagType ret = TagType.End;
bool isNumber = true;
// Start to determine the next tag type
while (reader.canRead && ret == TagType.End) {
@ -97,6 +98,11 @@ enum TagType {
ret = TagType.Float;
break;
}
case "I":
{
ret = TagType.Int;
break;
}
case "L":
{
ret = TagType.Long;
@ -107,6 +113,26 @@ enum TagType {
ret = TagType.Short;
break;
}
case "\"":
{
ret = TagType.String;
break;
}
case ",":
case "\n":
{
if (reader.getSeeked >= 1) ret = TagType.String;
if (isNumber) ret = TagType.Int;
break;
}
default:
{
if (!reader.isDigit(val)) {
if (isNumber) isNumber = false;
}
break;
}
}
}

View file

@ -56,6 +56,8 @@ class ByteArrayTag extends Tag {
@override
void readStringifiedValue(StringReader reader) {
reader.expect("[");
reader.expect("B");
reader.expect(";");
while (reader.peek() != "]") {
value.add(int.parse(reader.readNumber()));
reader.expect("b");

View file

@ -198,6 +198,8 @@ class CompoundTag extends Tag implements Map<String, Tag> {
while (reader.peek() != "}") {
Tag tag = Tag.readStringifiedNamedTag(reader);
put(tag.getKey(), tag);
if (reader.peek() == ",") reader.next();
}
reader.expect("}");

View file

@ -55,6 +55,8 @@ class IntArrayTag extends Tag {
@override
void readStringifiedValue(StringReader reader) {
reader.expect("[");
reader.expect("I");
reader.expect(";");
while (reader.peek() != "]") {
value.add(int.parse(reader.readNumber()));

View file

@ -43,5 +43,11 @@ class IntTag extends Tag {
void readStringifiedValue(StringReader reader) {
String val = reader.readNumber();
value = int.parse(val);
// Since a type indicator is optional for a int, check for a comma
if (reader.peek() == ",")
return;
else
reader.expect("i");
}
}

View file

@ -108,7 +108,10 @@ class ListTag extends Tag {
void readStringifiedValue(StringReader reader) {
reader.expect("[");
while (reader.peek() != "]") {
value.add(Tag.readStringifiedNamedTag(reader));
TagType type = TagType.getStringifiedTagType(reader);
Tag newTag = Tag.makeTagOfType(type);
newTag.readStringifiedValue(reader);
add(newTag);
if (reader.peek() == ",") reader.next();
}

View file

@ -59,6 +59,8 @@ class LongArrayTag extends Tag {
@override
void readStringifiedValue(StringReader reader) {
reader.expect("[");
reader.expect("L");
reader.expect(";");
while (reader.peek() != "]") {
value.add(int.parse(reader.readNumber()));
reader.expect("l");

View file

@ -15,7 +15,10 @@ class PathHelper {
}
bool exists() {
return File(build()).existsSync();
File fi = File(build());
Directory dir = Directory(build());
return fi.existsSync() || dir.existsSync();
}
PathHelper clone() {

View file

@ -7,6 +7,7 @@ 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';
@ -85,7 +86,10 @@ void main() {
SnbtIo.writeToFile(output, ct);
// Expect that the file exists
expect(file.existsSync(), true);
PathHelper ph = PathHelper.builder(Directory.current.path)
.resolve("build")
.resolve("bigtest.snbt");
expect(ph.exists(), true);
});
test("Read BigTest from SNBT file", () async {
@ -93,6 +97,7 @@ void main() {
"${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 {