Implement ListTag
This commit is contained in:
parent
4ee91e762a
commit
dd133afd68
3 changed files with 88 additions and 50 deletions
|
@ -10,6 +10,7 @@ import 'package:libac_flutter/nbt/impl/TagShort.dart';
|
||||||
|
|
||||||
import 'Stream.dart';
|
import 'Stream.dart';
|
||||||
import 'impl/ByteTag.dart';
|
import 'impl/ByteTag.dart';
|
||||||
|
import 'impl/ListTag.dart';
|
||||||
|
|
||||||
enum TagType {
|
enum TagType {
|
||||||
End(0),
|
End(0),
|
||||||
|
@ -108,6 +109,26 @@ abstract class Tag {
|
||||||
{
|
{
|
||||||
return new IntArrayTag();
|
return new IntArrayTag();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case TagType.List:
|
||||||
|
{
|
||||||
|
return new ListTag();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool equals(dynamic object) {
|
||||||
|
if (object == null || !(object is Tag)) return false;
|
||||||
|
|
||||||
|
Tag tag = object;
|
||||||
|
if (tag.getType() != getType()) return false;
|
||||||
|
|
||||||
|
if (getKey() == null && tag.getKey() != null ||
|
||||||
|
tag.getKey() == null && getKey() != null) return false;
|
||||||
|
|
||||||
|
if (getKey() != null && !(getKey() == tag.getKey())) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
67
lib/nbt/impl/ListTag.dart
Normal file
67
lib/nbt/impl/ListTag.dart
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
import 'package:libac_flutter/nbt/Stream.dart';
|
||||||
|
import 'package:libac_flutter/nbt/Tag.dart';
|
||||||
|
|
||||||
|
class ListTag extends Tag {
|
||||||
|
List<Tag> _value = [];
|
||||||
|
|
||||||
|
ListTag() {
|
||||||
|
_value = List.empty(growable: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void readValue(ByteLayer data) {
|
||||||
|
TagType type = TagType.get(data.readByte());
|
||||||
|
int size = data.readInt();
|
||||||
|
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
Tag tag = Tag.makeTagOfType(type);
|
||||||
|
tag.readValue(data);
|
||||||
|
add(tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void writeValue(ByteLayer data) {
|
||||||
|
TagType type = TagType.End;
|
||||||
|
if (size() > 0) {
|
||||||
|
type = _value[0].getTagType();
|
||||||
|
}
|
||||||
|
|
||||||
|
data.writeByte(type.byte);
|
||||||
|
data.writeInt(size());
|
||||||
|
for (int i = 0; i < size(); i++) {
|
||||||
|
_value[i].writeValue(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void add(Tag tag) {
|
||||||
|
TagType type = TagType.End;
|
||||||
|
if (size() > 0) {
|
||||||
|
type = _value[0].getTagType();
|
||||||
|
}
|
||||||
|
if (type == TagType.End || type == tag.getTagType()) {
|
||||||
|
_value.add(tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove(Tag tag) {
|
||||||
|
_value.remove(tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeAt(int index) {
|
||||||
|
_value.removeAt(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
int indexOf(Tag tag) {
|
||||||
|
return _value.indexOf(tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
TagType getTagType() {
|
||||||
|
return TagType.List;
|
||||||
|
}
|
||||||
|
|
||||||
|
int size() {
|
||||||
|
return _value.length;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,50 +0,0 @@
|
||||||
import 'package:libac_flutter/nbt/Stream.dart';
|
|
||||||
import 'package:libac_flutter/nbt/Tag.dart';
|
|
||||||
|
|
||||||
class TagList extends Tag {
|
|
||||||
late final List<Tag> _value;
|
|
||||||
TagType _internalType = TagType.End;
|
|
||||||
|
|
||||||
TagList() {
|
|
||||||
_value = List.empty(growable: true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void readValue(ByteLayer data) {
|
|
||||||
// TODO: implement readValue
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void writeValue(ByteLayer data) {
|
|
||||||
// TODO: implement writeValue
|
|
||||||
}
|
|
||||||
|
|
||||||
void add(Tag tag) {
|
|
||||||
if (_internalType == TagType.End) {
|
|
||||||
_value.clear();
|
|
||||||
_value.add(tag);
|
|
||||||
_internalType = tag.getTagType();
|
|
||||||
} else {
|
|
||||||
if (_internalType == tag.getTagType()) {
|
|
||||||
_value.add(tag);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void remove(Tag tag) {
|
|
||||||
_value.remove(tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
void removeAt(int index) {
|
|
||||||
_value.removeAt(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
int indexOf(Tag tag) {
|
|
||||||
return _value.indexOf(tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
TagType getTagType() {
|
|
||||||
return TagType.List;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue