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).
Soda
BodgeMaster 2022-09-28 03:43:54 +02:00
parent 47fd1f8970
commit 434c976cc5
2 changed files with 174 additions and 0 deletions

View File

@ -777,6 +777,72 @@ namespace NBT {
this->value = value; this->value = value;
this->mutex.unlock(); this->mutex.unlock();
} }
Int8Array::Int8Array() {
this->type = TagType::INT8_ARRAY;
}
Int8Array::Int8Array(tiny_utf8::string name, std::vector<int8_t> 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<int8_t>(data, data+length);
}
ErrorOrVoid Int8Array::toRawData(std::vector<uint8_t>* 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<int8_t> Int8Array::getData() {
return this->data;
}
ErrorOr<int8_t> Int8Array::getValue(uint64_t position) {
if (this->data.size() <= position) {
return ErrorOr<int8_t>(this->data.at(position));
}
return ErrorOr<int8_t>(true, ErrorCodes::OUT_OF_RANGE);
}
void Int8Array::setData(std::vector<int8_t> 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) { bool validateRawListContents(uint8_t data[], uint64_t dataSize, uint64_t initialPosition, uint64_t* processedDataSize) {

View File

@ -185,6 +185,114 @@ namespace NBT {
double getValue(); double getValue();
void setValue(double value); void setValue(double value);
}; };
class Int8Array: public Generic {
private:
std::vector<int8_t> data;
public:
Int8Array();
Int8Array(tiny_utf8::string name, std::vector<int8_t> data);
Int8Array(tiny_utf8::string name, uint64_t length, int8_t data[]);
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
std::vector<int8_t> getData();
ErrorOr<int8_t> getValue(uint64_t position);
void setData(std::vector<int8_t> 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<uint8_t>* rawData) override;
tiny_utf8::string getValue();
void setValue(tiny_utf8::string value);
};
class List: public Generic {
private:
std::vector<Generic*> tags;
uint8_t type;
public:
List();
List(tiny_utf8::string name, uint8_t type);
List(tiny_utf8::string name, std::vector<Generic*> data);
~List() override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
ErrorOr<Generic*> 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<Generic*> tags;
public:
Compound();
Compound(tiny_utf8::string name);
Compound(tiny_utf8::string name, std::vector<Generic*> data);
~Compound() override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
ErrorOr<Generic*> 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<int32_t> data;
public:
Int32Array();
Int32Array(tiny_utf8::string name, std::vector<int32_t> data);
Int32Array(tiny_utf8::string name, uint64_t length, int32_t data[]);
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
std::vector<int32_t> getData();
ErrorOr<int32_t> getValue(uint64_t position);
void setData(std::vector<int32_t> 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<int64_t> data;
public:
Int64Array();
Int64Array(tiny_utf8::string name, std::vector<int64_t> data);
Int64Array(tiny_utf8::string name, uint64_t length, int64_t data[]);
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
std::vector<int64_t> getData();
ErrorOr<int64_t> getValue(uint64_t position);
void setData(std::vector<int64_t> 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); bool validateRawNBTData(uint8_t data[], uint64_t dataSize, uint64_t initialPosition=0, uint64_t* processedDataSize=nullptr);