From 434c976cc5e74c5dc9ffbfb85921551f0b858a76 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Wed, 28 Sep 2022 03:43:54 +0200 Subject: [PATCH] lib/nbt: Continue implementing in-memory NBT tag data types This is still all unvalidated work. For all I know it might cause your computer to burst into flames, grow an arm and a leg and an eye, and attack you with a sword. But probably, it will just not work and I will realize that sleepy past me was a fucking idiot (as always). --- src/lib/nbt.cpp | 66 +++++++++++++++++++++++++++++ src/lib/nbt.hpp | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index 85db9ba..9e66fee 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -777,6 +777,72 @@ namespace NBT { this->value = value; this->mutex.unlock(); } + + Int8Array::Int8Array() { + this->type = TagType::INT8_ARRAY; + } + + Int8Array::Int8Array(tiny_utf8::string name, std::vector data) { + this->type = TagType::INT8_ARRAY; + this->name = name; + this->data = data; + } + Int8Array::Int8Array(tiny_utf8::string name, uint64_t length, int8_t data[]){ + this->type = TagType::INT8_ARRAY; + this->name = name; + this->data = std::vector(data, data+length); + } + + ErrorOrVoid Int8Array::toRawData(std::vector* rawData) { + rawData->push_back(this->type); + + if (Helper::writeString(rawData, this->name).isError) { + return ErrorOrVoid(true, ErrorCodes::OVERRUN); + } + + Helper::writeInt8Array(rawData, this->data); + + return ErrorOrVoid(); + } + + std::vector Int8Array::getData() { + return this->data; + } + + ErrorOr Int8Array::getValue(uint64_t position) { + if (this->data.size() <= position) { + return ErrorOr(this->data.at(position)); + } + + return ErrorOr(true, ErrorCodes::OUT_OF_RANGE); + } + + void Int8Array::setData(std::vector newData) { + this->data = newData; + } + + ErrorOrVoid Int8Array::setValue(uint64_t position, int8_t value) { + if (this->data.size() <= position) { + return ErrorOrVoid(true, ErrorCodes::OUT_OF_RANGE); + } + + this->data[position] = value; + + return ErrorOrVoid(); + } + + uint64_t Int8Array::length() { + return this->data.size(); + } + + void Int8Array::addElement(int8_t element) { + this->data.push_back(element); + } + + ErrorOrVoid Int8Array::removeElement(uint64_t position) { + //TODO: implement + //this->data.erase(position); + } } bool validateRawListContents(uint8_t data[], uint64_t dataSize, uint64_t initialPosition, uint64_t* processedDataSize) { diff --git a/src/lib/nbt.hpp b/src/lib/nbt.hpp index 2cca23d..f9a3887 100644 --- a/src/lib/nbt.hpp +++ b/src/lib/nbt.hpp @@ -185,6 +185,114 @@ namespace NBT { double getValue(); void setValue(double value); }; + + class Int8Array: public Generic { + private: + std::vector data; + public: + Int8Array(); + Int8Array(tiny_utf8::string name, std::vector data); + Int8Array(tiny_utf8::string name, uint64_t length, int8_t data[]); + + ErrorOrVoid toRawData(std::vector* rawData) override; + + std::vector getData(); + ErrorOr getValue(uint64_t position); + void setData(std::vector newData); + ErrorOrVoid setValue(uint64_t position, int8_t value); + uint64_t length(); + void addElement(int8_t element); + ErrorOrVoid removeElement(uint64_t position); + }; + + class String: public Generic { + private: + tiny_utf8::string value; + public: + String(); + String(tiny_utf8::string name, tiny_utf8::string value); + + ErrorOrVoid toRawData(std::vector* rawData) override; + tiny_utf8::string getValue(); + void setValue(tiny_utf8::string value); + }; + + class List: public Generic { + private: + std::vector tags; + uint8_t type; + public: + List(); + List(tiny_utf8::string name, uint8_t type); + List(tiny_utf8::string name, std::vector data); + + ~List() override; + + ErrorOrVoid toRawData(std::vector* rawData) override; + + ErrorOr getElementPointer(uint64_t position); + ErrorOrVoid setElementPointerAt(uint64_t position, Generic*); + ErrorOrVoid appendPointer(Generic*); + ErrorOrVoid deleteElement(uint64_t position); + uint64_t length(); + }; + + class Compound: public Generic { + private: + std::vector tags; + public: + Compound(); + Compound(tiny_utf8::string name); + Compound(tiny_utf8::string name, std::vector data); + + ~Compound() override; + + ErrorOrVoid toRawData(std::vector* rawData) override; + + ErrorOr getElementPointer(uint64_t position); + ErrorOrVoid setElementPointerAt(uint64_t position, Generic*); + ErrorOrVoid appendPointer(Generic*); + ErrorOrVoid deleteElement(uint64_t position); + uint64_t length(); + }; + + class Int32Array: public Generic { + private: + std::vector data; + public: + Int32Array(); + Int32Array(tiny_utf8::string name, std::vector data); + Int32Array(tiny_utf8::string name, uint64_t length, int32_t data[]); + + ErrorOrVoid toRawData(std::vector* rawData) override; + + std::vector getData(); + ErrorOr getValue(uint64_t position); + void setData(std::vector newData); + ErrorOrVoid setValue(uint64_t position, int32_t value); + uint64_t length(); + void addElement(int32_t element); + ErrorOrVoid removeElement(uint64_t position); + }; + + class Int64Array: public Generic { + private: + std::vector data; + public: + Int64Array(); + Int64Array(tiny_utf8::string name, std::vector data); + Int64Array(tiny_utf8::string name, uint64_t length, int64_t data[]); + + ErrorOrVoid toRawData(std::vector* rawData) override; + + std::vector getData(); + ErrorOr getValue(uint64_t position); + void setData(std::vector newData); + ErrorOrVoid setValue(uint64_t position, int64_t value); + uint64_t length(); + void addElement(int64_t element); + ErrorOrVoid removeElement(uint64_t position); + }; } bool validateRawNBTData(uint8_t data[], uint64_t dataSize, uint64_t initialPosition=0, uint64_t* processedDataSize=nullptr);