Initial SNBT implementation

This commit is contained in:
zontreck 2024-05-08 18:52:20 -07:00
parent c10ffc58b8
commit 84b37f8b04
16 changed files with 137 additions and 0 deletions

View file

@ -377,3 +377,22 @@ class ByteLayer {
setBit(position, maskToSet);
}
}
class StringBuilder {
String _buffer = "";
StringBuilder();
void append(String value) {
_buffer += value;
}
void clear() {
_buffer = "";
}
@override
String toString() {
return "${_buffer}";
}
}

View file

@ -82,6 +82,38 @@ abstract class Tag {
}
}
static void writeStringifiedNamedTag(
Tag tag, StringBuilder builder, int indents) {
if (tag.getType() != 0) {
if (builder != "") {
// Write name
if (tag.shouldQuoteName()) {
builder.append("${"".padLeft(indents, "\t")}\"${tag.getKey()}\": ");
} else
builder.append("${"".padLeft(indents, '\t')}${tag.getKey()}: ");
}
tag.writeStringifiedValue(builder, indents + 1);
}
}
bool shouldQuoteName() {
if (getKey() == "") {
return false;
} else {
String letters = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < getKey().length; i++) {
String digit = getKey().substring(i, i + 1);
if (letters.indexOf(digit) == -1) {
return true;
}
}
return false;
}
}
void writeStringifiedValue(StringBuilder builder, int indent);
bool equals(dynamic object) {
if (object == null || object is! Tag) return false;

View file

@ -46,4 +46,9 @@ class ByteArrayTag extends Tag {
print(
"${"".padLeft(indent, '\t')}${Tag.getCanonicalName(getTagType())}: [$array]");
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("[B; ${value.join("B, ")}B]");
}
}

View file

@ -34,4 +34,9 @@ class ByteTag extends Tag {
print(
"${"".padLeft(indent, '\t')}${Tag.getCanonicalName(getTagType())}: $value");
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("${value}b");
}
}

View file

@ -85,4 +85,22 @@ class CompoundTag extends Tag {
void endPrettyPrint(int indent) {
print("${"".padLeft(indent, '\t')}}");
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
Iterator<Tag> it = value.values.iterator;
builder.append("{\n");
bool firstEntry = true;
while (it.moveNext()) {
Tag t = it.current;
if (firstEntry) {
firstEntry = false;
} else {
builder.append(",\n");
}
Tag.writeStringifiedNamedTag(t, builder, indent);
}
builder.append("\n${"".padLeft(indent - 1, '\t')}}\n");
}
}

View file

@ -34,4 +34,9 @@ class DoubleTag extends Tag {
print(
"${"".padLeft(indent, '\t')}${Tag.getCanonicalName(getTagType())}: $value");
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("${value}d");
}
}

View file

@ -19,4 +19,7 @@ class EndTag extends Tag {
void prettyPrint(int indent, bool recurse) {
print("${"".padLeft(indent, '\t')}${Tag.getCanonicalName(getTagType())}");
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {}
}

View file

@ -33,4 +33,9 @@ class FloatTag extends Tag {
print(
"${"".padLeft(indent, '\t')}${Tag.getCanonicalName(getTagType())}: $value");
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("${value}f");
}
}

View file

@ -45,4 +45,9 @@ class IntArrayTag extends Tag {
print(
"${"".padLeft(indent, '\t')}${Tag.getCanonicalName(getTagType())}: [$array]");
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("[I; ${value.join('I, ')}I]");
}
}

View file

@ -33,4 +33,9 @@ class IntTag extends Tag {
print(
"${"".padLeft(indent, '\t')}${Tag.getCanonicalName(getTagType())}: $value");
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("${value}i");
}
}

View file

@ -86,4 +86,9 @@ class ListTag extends Tag {
void endPrettyPrint(int indent) {
print("${"".padLeft(indent, '\t')}]");
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("[\n");
}
}

View file

@ -49,4 +49,9 @@ class LongArrayTag extends Tag {
print(
"${"".padLeft(indent, '\t')}${Tag.getCanonicalName(getTagType())}: [$array]");
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("[L; ${value.join('L, ')}L]");
}
}

View file

@ -33,4 +33,9 @@ class LongTag extends Tag {
print(
"${"".padLeft(indent, '\t')}${Tag.getCanonicalName(getTagType())}: $value");
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("${value}L");
}
}

View file

@ -33,4 +33,9 @@ class ShortTag extends Tag {
print(
"${"".padLeft(indent, '\t')}${Tag.getCanonicalName(getTagType())}: $value");
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("${value}S");
}
}

View file

@ -33,4 +33,9 @@ class StringTag extends Tag {
print(
"${"".padLeft(indent, '\t')}${Tag.getCanonicalName(getTagType())}: $value");
}
@override
void writeStringifiedValue(StringBuilder builder, int indent) {
builder.append("\"${value}\"");
}
}

View file

@ -3,6 +3,8 @@ import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:libac_flutter/nbt/NbtIo.dart';
import 'package:libac_flutter/nbt/NbtUtils.dart';
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
import 'package:libac_flutter/nbt/impl/CompoundTag.dart';
import 'package:libac_flutter/nbt/impl/StringTag.dart';
import 'package:libac_flutter/utils/uuid/UUID.dart';
@ -55,4 +57,12 @@ void main() {
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");
StringBuilder sb = StringBuilder();
Tag.writeStringifiedNamedTag(ct, sb, 0);
print(sb.toString());
});
}