Skip to content

Commit

Permalink
Howaner#5: added support for long array tags
Browse files Browse the repository at this point in the history
  • Loading branch information
syldium committed Jun 5, 2022
1 parent 714ac6e commit 7c09053
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ typedef unsigned int uint;
const unsigned int GZIP_BUFFER_SIZE = 128;
const unsigned int WRITE_BUFFER_SIZE = 256;
const unsigned int MIN_TAG = 1;
const unsigned int MAX_TAG = 11;
const unsigned int MAX_TAG = 12;

typedef signed char jbyte;
typedef int16_t jshort;
Expand Down
1 change: 1 addition & 0 deletions src/NBT/NBTHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace NBT {
tags[NbtList] = new NBTTagList();
tags[NbtCompound] = new NBTTagCompound();
tags[NbtIntArray] = new NBTTagIntArray();
tags[NbtLongArray] = new NBTTagLongArray();
return tags;
}();

Expand Down
45 changes: 45 additions & 0 deletions src/NBT/NBTTag.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,51 @@ namespace NBT {
}
};

class NBTTagLongArray : public NBTTagBasic<NBTArray<jlong>> {
public:
NBTTagLongArray() : NBTTagBasic("nbt-array.png", "Long Array") {}

void* Read(ByteBuffer* buffer) const override {
jint length = buffer->ReadInt();
// TODO: Overflow check

long* array = new long[length];
for (jint i = 0; i < length; i++) {
array[i] = buffer->ReadLong();
}

return new NBTArray<jlong>(length, array);
}

void Write(WriteBuffer* buffer, NBTEntry& entry) const override {
NBTArray<jlong>& data = GetData(entry);
buffer->WriteInt(data.length);

for (uint i = 0; i < data.length; i++) {
buffer->WriteLong(data.array[i]);
}
}

void* CreateDefaultData() const override {
return new NBTArray<jlong>(0, new jlong[0]);
}

void SetData(NBTEntry& entry, NBTArray<jlong>& data) const override {
if (entry.value != NULL)
delete static_cast<NBTArray<jlong>*>(entry.value);
entry.value = &data;
}

QVariant GetQtData(NBTEntry& entry) const override {
return QString::number(GetData(entry).length) + QString(" longs");
}

bool SetQtData(NBTEntry&, const QVariant&) const override {
// Not possible
return false;
}
};

class NBTTagList : public NBTTagBasic<NBTList> {
public:
NBTTagList() : NBTTagBasic("nbt-list.png", "List") {}
Expand Down
3 changes: 2 additions & 1 deletion src/NBT/NBTType.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ namespace NBT {
NbtString = 8,
NbtList = 9,
NbtCompound = 10,
NbtIntArray = 11
NbtIntArray = 11,
NbtLongArray = 12
};

enum NBTFileType {
Expand Down

0 comments on commit 7c09053

Please sign in to comment.