Finish implementing CompoundTag
This commit is contained in:
parent
a27248daf9
commit
70f6e16d17
4 changed files with 108 additions and 6 deletions
|
@ -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 'Stream.dart';
|
||||||
|
import 'impl/ByteTag.dart';
|
||||||
|
|
||||||
enum TagType {
|
enum TagType {
|
||||||
End(0),
|
End(0),
|
||||||
|
@ -17,6 +26,16 @@ enum TagType {
|
||||||
|
|
||||||
final int byte;
|
final int byte;
|
||||||
const TagType(this.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 {
|
abstract class Tag {
|
||||||
|
@ -27,4 +46,58 @@ abstract class Tag {
|
||||||
TagType getTagType();
|
TagType getTagType();
|
||||||
void writeValue(ByteLayer data);
|
void writeValue(ByteLayer data);
|
||||||
void readValue(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,17 @@ import 'package:libac_flutter/nbt/Tag.dart';
|
||||||
|
|
||||||
class ByteArrayTag extends Tag {
|
class ByteArrayTag extends Tag {
|
||||||
List<int> _value = [];
|
List<int> _value = [];
|
||||||
ByteArrayTag(List<int> value) {
|
|
||||||
|
ByteArrayTag();
|
||||||
|
|
||||||
|
ByteArrayTag._(List<int> value) {
|
||||||
_value.addAll(value);
|
_value.addAll(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ByteArrayTag valueOf(List<int> value) {
|
||||||
|
return ByteArrayTag._(value);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void readValue(ByteLayer data) {
|
void readValue(ByteLayer data) {
|
||||||
int len = data.readInt();
|
int len = data.readInt();
|
||||||
|
|
|
@ -4,10 +4,16 @@ import 'package:libac_flutter/nbt/Tag.dart';
|
||||||
class ByteTag extends Tag {
|
class ByteTag extends Tag {
|
||||||
int _value = 0;
|
int _value = 0;
|
||||||
|
|
||||||
ByteTag(int value) {
|
ByteTag._(int value) {
|
||||||
_value = value;
|
_value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ByteTag();
|
||||||
|
|
||||||
|
static ByteTag valueOf(int value) {
|
||||||
|
return ByteTag._(value);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void readValue(ByteLayer data) {
|
void readValue(ByteLayer data) {
|
||||||
_value = data.readByte();
|
_value = data.readByte();
|
||||||
|
|
|
@ -1,21 +1,37 @@
|
||||||
import 'package:libac_flutter/nbt/Stream.dart';
|
import 'package:libac_flutter/nbt/Stream.dart';
|
||||||
import 'package:libac_flutter/nbt/Tag.dart';
|
import 'package:libac_flutter/nbt/Tag.dart';
|
||||||
|
|
||||||
class TagCompound extends Tag {
|
class CompoundTag extends Tag {
|
||||||
late final Map<String, Tag> _value;
|
late final Map<String, Tag> _value;
|
||||||
|
|
||||||
TagCompound() {
|
CompoundTag() {
|
||||||
_value = new Map();
|
_value = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void readValue(ByteLayer data) {
|
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
|
@override
|
||||||
void writeValue(ByteLayer data) {
|
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) {
|
void put(String name, Tag tag) {
|
Loading…
Add table
Add a link
Reference in a new issue