Finish implementing CompoundTag

This commit is contained in:
zontreck 2024-05-04 21:24:42 -07:00
parent a27248daf9
commit 70f6e16d17
4 changed files with 108 additions and 6 deletions

View file

@ -1,4 +1,13 @@
import 'package:libac_flutter/nbt/impl/ByteArrayTag.dart';
import 'package:libac_flutter/nbt/impl/CompoundTag.dart';
import 'package:libac_flutter/nbt/impl/TagEnd.dart';
import 'package:libac_flutter/nbt/impl/TagFloat.dart';
import 'package:libac_flutter/nbt/impl/TagInt.dart';
import 'package:libac_flutter/nbt/impl/TagLong.dart';
import 'package:libac_flutter/nbt/impl/TagShort.dart';
import 'Stream.dart';
import 'impl/ByteTag.dart';
enum TagType {
End(0),
@ -17,6 +26,16 @@ enum TagType {
final int byte;
const TagType(this.byte);
static TagType get(int byte) {
for (TagType type in values) {
if (type.byte == byte) {
return type;
}
}
return TagType.End;
}
}
abstract class Tag {
@ -27,4 +46,58 @@ abstract class Tag {
TagType getTagType();
void writeValue(ByteLayer data);
void readValue(ByteLayer data);
String? _key;
String getKey() {
return (_key == null ? "" : _key!);
}
static Tag readNamedTag(ByteLayer data) {
var type = data.readByte();
if (type == 0) {
return new TagEnd();
} else {
Tag tag = makeTagOfType(TagType.get(type));
tag._key = data.readString();
tag.readValue(data);
return tag;
}
}
static void writeNamedTag(Tag tag, ByteLayer data) {
data.writeByte(tag.getType());
if (tag.getType() != 0) {
data.writeString(tag.getKey());
tag.writeValue(data);
}
}
static Tag makeTagOfType(TagType type) {
switch (type) {
case TagType.Byte:
{
return new ByteTag();
}
case TagType.ByteArray:
{
return new ByteArrayTag();
}
case TagType.Compound:
{
return new CompoundTag();
}
case TagType.End:
return new TagEnd();
case TagType.Short:
return new TagShort(value: 0);
case TagType.Int:
return new TagInt(value: 0);
case TagType.Long:
return new TagLong(value: 0);
case TagType.Float:
return new TagFloat(value: 0);
}
}
}

View file

@ -3,10 +3,17 @@ import 'package:libac_flutter/nbt/Tag.dart';
class ByteArrayTag extends Tag {
List<int> _value = [];
ByteArrayTag(List<int> value) {
ByteArrayTag();
ByteArrayTag._(List<int> value) {
_value.addAll(value);
}
static ByteArrayTag valueOf(List<int> value) {
return ByteArrayTag._(value);
}
@override
void readValue(ByteLayer data) {
int len = data.readInt();

View file

@ -4,10 +4,16 @@ import 'package:libac_flutter/nbt/Tag.dart';
class ByteTag extends Tag {
int _value = 0;
ByteTag(int value) {
ByteTag._(int value) {
_value = value;
}
ByteTag();
static ByteTag valueOf(int value) {
return ByteTag._(value);
}
@override
void readValue(ByteLayer data) {
_value = data.readByte();

View file

@ -1,21 +1,37 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class TagCompound extends Tag {
class CompoundTag extends Tag {
late final Map<String, Tag> _value;
TagCompound() {
CompoundTag() {
_value = new Map();
}
@override
void readValue(ByteLayer data) {
// TODO: implement readValue
_value.clear();
while (true) {
Tag tag = Tag.readNamedTag(data);
if (tag.getType() == 0) {
return;
}
put(tag.getKey(), tag);
}
}
@override
void writeValue(ByteLayer data) {
// TODO: implement writeValue
Iterator<Tag> it = _value.values.iterator;
while (it.moveNext()) {
Tag tag = it.current;
Tag.writeNamedTag(tag, data);
}
data.writeByte(TagType.End.byte);
}
void put(String name, Tag tag) {