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 '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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue