LibAC-dart/lib/nbt/Tag.dart

230 lines
4.3 KiB
Dart

import 'package:libac_flutter/nbt/impl/ByteArrayTag.dart';
import 'package:libac_flutter/nbt/impl/CompoundTag.dart';
import 'package:libac_flutter/nbt/impl/DoubleTag.dart';
import 'package:libac_flutter/nbt/impl/EndTag.dart';
import 'package:libac_flutter/nbt/impl/FloatTag.dart';
import 'package:libac_flutter/nbt/impl/IntArrayTag.dart';
import 'package:libac_flutter/nbt/impl/IntTag.dart';
import 'package:libac_flutter/nbt/impl/LongTag.dart';
import 'package:libac_flutter/nbt/impl/ShortTag.dart';
import 'Stream.dart';
import 'impl/ByteTag.dart';
import 'impl/ListTag.dart';
import 'impl/LongArrayTag.dart';
import 'impl/StringTag.dart';
enum TagType {
End(0),
Byte(1),
Short(2),
Int(3),
Long(4),
Float(5),
Double(6),
ByteArray(7),
String(8),
List(9),
Compound(10),
IntArray(11),
LongArray(12);
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 {
int getType() {
return getTagType().byte;
}
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 EndTag();
} 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);
}
}
bool equals(dynamic object) {
if (object == null || object is! Tag) return false;
Tag tag = object;
if (tag.getType() != getType()) return false;
if (!(getKey() == tag.getKey())) return false;
return true;
}
static Tag makeTagOfType(TagType type) {
switch (type) {
case TagType.Byte:
{
return ByteTag();
}
case TagType.ByteArray:
{
return ByteArrayTag();
}
case TagType.Compound:
{
return CompoundTag();
}
case TagType.Double:
{
return DoubleTag();
}
case TagType.End:
{
return EndTag();
}
case TagType.Short:
{
return ShortTag();
}
case TagType.Int:
{
return IntTag();
}
case TagType.Long:
{
return LongTag();
}
case TagType.Float:
{
return FloatTag();
}
case TagType.IntArray:
{
return IntArrayTag();
}
case TagType.LongArray:
{
return LongArrayTag();
}
case TagType.List:
{
return ListTag();
}
case TagType.String:
{
return StringTag();
}
}
}
int asByte() {
if (this is ByteTag) {
return (this as ByteTag).value;
} else {
return 0;
}
}
List<int> asByteArray() {
if (this is ByteArrayTag) {
return (this as ByteArrayTag).value;
} else {
return [];
}
}
double asDouble() {
if (this is DoubleTag) {
return (this as DoubleTag).value;
} else {
return 0.0;
}
}
double asFloat() {
if (this is FloatTag) {
return (this as FloatTag).value;
} else {
return 0.0;
}
}
List<int> asIntArray() {
if (this is IntArrayTag) {
return (this as IntArrayTag).value;
} else {
return [];
}
}
int asInt() {
if (this is IntTag) {
return (this as IntTag).value;
} else {
return 0;
}
}
List<int> asLongArray() {
if (this is LongArrayTag) {
return (this as LongArrayTag).value;
} else {
return [];
}
}
int asLong() {
if (this is LongTag) {
return (this as LongTag).value;
} else {
return 0;
}
}
int asShort() {
if (this is ShortTag) {
return (this as ShortTag).value;
} else {
return 0;
}
}
String asString() {
if (this is StringTag) {
return (this as StringTag).value;
} else {
return "";
}
}
}