Initial SNBT implementation
This commit is contained in:
parent
c10ffc58b8
commit
84b37f8b04
16 changed files with 137 additions and 0 deletions
|
@ -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}";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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]");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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]");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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]");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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}\"");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue