SNBT Serialization now implemented

This commit is contained in:
zontreck 2024-05-08 20:35:08 -07:00
parent 84b37f8b04
commit daf9cb7376
16 changed files with 54 additions and 28 deletions

View file

@ -383,6 +383,8 @@ class StringBuilder {
StringBuilder();
bool get isEmpty => _buffer.isEmpty;
void append(String value) {
_buffer += value;
}

View file

@ -85,7 +85,7 @@ abstract class Tag {
static void writeStringifiedNamedTag(
Tag tag, StringBuilder builder, int indents) {
if (tag.getType() != 0) {
if (builder != "") {
if (!builder.isEmpty) {
// Write name
if (tag.shouldQuoteName()) {
builder.append("${"".padLeft(indents, "\t")}\"${tag.getKey()}\": ");
@ -93,7 +93,7 @@ abstract class Tag {
builder.append("${"".padLeft(indents, '\t')}${tag.getKey()}: ");
}
tag.writeStringifiedValue(builder, indents + 1);
tag.writeStringifiedValue(builder, indents + 1, false);
}
}
@ -112,7 +112,7 @@ abstract class Tag {
}
}
void writeStringifiedValue(StringBuilder builder, int indent);
void writeStringifiedValue(StringBuilder builder, int indent, bool isList);
bool equals(dynamic object) {
if (object == null || object is! Tag) return false;

View file

@ -48,7 +48,8 @@ class ByteArrayTag extends Tag {
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("[B; ${value.join("B, ")}B]");
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {
builder.append(
"${isList ? "".padLeft(indent, '\t') : ""}[B; ${value.join("B, ")}B]");
}
}

View file

@ -36,7 +36,7 @@ class ByteTag extends Tag {
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {
builder.append("${value}b");
}
}

View file

@ -87,9 +87,9 @@ class CompoundTag extends Tag {
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {
Iterator<Tag> it = value.values.iterator;
builder.append("{\n");
builder.append("${isList ? "".padLeft(indent - 1, '\t') : ""}{\n");
bool firstEntry = true;
while (it.moveNext()) {
@ -101,6 +101,6 @@ class CompoundTag extends Tag {
}
Tag.writeStringifiedNamedTag(t, builder, indent);
}
builder.append("\n${"".padLeft(indent - 1, '\t')}}\n");
builder.append("\n${"".padLeft(indent - 1, '\t')}}");
}
}

View file

@ -36,7 +36,7 @@ class DoubleTag extends Tag {
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("${value}d");
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {
builder.append("${isList ? "".padLeft(indent, '\t') : ""}${value}d");
}
}

View file

@ -21,5 +21,5 @@ class EndTag extends Tag {
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {}
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {}
}

View file

@ -35,7 +35,7 @@ class FloatTag extends Tag {
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("${value}f");
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {
builder.append("${isList ? "".padLeft(indent, '\t') : ""}${value}f");
}
}

View file

@ -47,7 +47,8 @@ class IntArrayTag extends Tag {
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("[I; ${value.join('I, ')}I]");
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {
builder.append(
"${isList ? "".padLeft(indent, '\t') : ""}[I; ${value.join('I, ')}I]");
}
}

View file

@ -35,7 +35,7 @@ class IntTag extends Tag {
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("${value}i");
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {
builder.append("${isList ? "".padLeft(indent, '\t') : ""}${value}i");
}
}

View file

@ -88,7 +88,20 @@ class ListTag extends Tag {
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("[\n");
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {
builder.append("${isList ? "".padLeft(indent - 1, '\t') : ""}[\n");
Iterator<Tag> it = value.iterator;
bool firstTag = true;
while (it.moveNext()) {
Tag tag = it.current;
if (firstTag)
firstTag = !firstTag;
else {
builder.append(",\n");
}
tag.writeStringifiedValue(builder, indent + 1, true);
}
builder.append("\n${"".padLeft(indent - 1, '\t')}]");
}
}

View file

@ -51,7 +51,8 @@ class LongArrayTag extends Tag {
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("[L; ${value.join('L, ')}L]");
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {
builder.append(
"${isList ? "".padLeft(indent, '\t') : ""}[L; ${value.join('L, ')}L]");
}
}

View file

@ -35,7 +35,7 @@ class LongTag extends Tag {
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("${value}L");
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {
builder.append("${isList ? "".padLeft(indent, '\t') : ""}${value}L");
}
}

View file

@ -35,7 +35,7 @@ class ShortTag extends Tag {
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("${value}S");
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {
builder.append("${isList ? "".padLeft(indent, '\t') : ""}${value}S");
}
}

View file

@ -35,7 +35,7 @@ class StringTag extends Tag {
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("\"${value}\"");
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {
builder.append("${isList ? "".padLeft(indent, '\t') : ""}\"${value}\"");
}
}

View file

@ -65,4 +65,12 @@ void main() {
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");
StringBuilder sb = StringBuilder();
Tag.writeStringifiedNamedTag(ct, sb, 0);
print(sb.toString());
});
}