diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index 393a92e..97a1a9d 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -550,10 +550,20 @@ namespace NBT { Generic::~Generic() {} - ErrorOrVoid Generic::toRawData([[maybe_unused]] std::vector* rawData) { + ErrorOrVoid Generic::serializeWithoutHeader([[maybe_unused]] std::vector* rawData) { return ErrorOrVoid(true, ErrorCodes::INVALID_TYPE); } + ErrorOrVoid Generic::serialize(std::vector* rawData) { + rawData->push_back(this->type); + + if (Helper::writeString(rawData, this->name).isError) { + return ErrorOrVoid(true, ErrorCodes::OVERRUN); + } + + return this->serializeWithoutHeader(rawData); + } + uint8_t Generic::getTagType(){ return this->type; } @@ -563,7 +573,11 @@ namespace NBT { this->type = TagType::END; } - ErrorOrVoid End::toRawData(std::vector* rawData) { + ErrorOrVoid End::serializeWithoutHeader([[maybe_unused]] std::vector* rawData) { + return ErrorOrVoid(); + } + + ErrorOrVoid End::serialize(std::vector* rawData) { rawData->push_back(this->type); return ErrorOrVoid(); } @@ -578,15 +592,8 @@ namespace NBT { this->value = value; } - ErrorOrVoid Int8::toRawData(std::vector* rawData) { - rawData->push_back(this->type); - - if (Helper::writeString(rawData, this->name).isError) { - return ErrorOrVoid(true, ErrorCodes::OVERRUN); - } - + ErrorOrVoid Int8::serializeWithoutHeader(std::vector* rawData) { Helper::writeInt8(rawData, this->value); - return ErrorOrVoid(); } @@ -613,15 +620,8 @@ namespace NBT { this->value = value; } - ErrorOrVoid Int16::toRawData(std::vector* rawData) { - rawData->push_back(this->type); - - if (Helper::writeString(rawData, this->name).isError) { - return ErrorOrVoid(true, ErrorCodes::OVERRUN); - } - + ErrorOrVoid Int16::serializeWithoutHeader(std::vector* rawData) { Helper::writeInt16(rawData, this->value); - return ErrorOrVoid(); } @@ -648,15 +648,8 @@ namespace NBT { this->value = value; } - ErrorOrVoid Int32::toRawData(std::vector* rawData) { - rawData->push_back(this->type); - - if (Helper::writeString(rawData, this->name).isError) { - return ErrorOrVoid(true, ErrorCodes::OVERRUN); - } - + ErrorOrVoid Int32::serializeWithoutHeader(std::vector* rawData) { Helper::writeInt32(rawData, this->value); - return ErrorOrVoid(); } @@ -683,15 +676,8 @@ namespace NBT { this->value = value; } - ErrorOrVoid Int64::toRawData(std::vector* rawData) { - rawData->push_back(this->type); - - if (Helper::writeString(rawData, this->name).isError) { - return ErrorOrVoid(true, ErrorCodes::OVERRUN); - } - + ErrorOrVoid Int64::serializeWithoutHeader(std::vector* rawData) { Helper::writeInt64(rawData, this->value); - return ErrorOrVoid(); } @@ -718,15 +704,8 @@ namespace NBT { this->value = value; } - ErrorOrVoid Float::toRawData(std::vector* rawData) { - rawData->push_back(this->type); - - if (Helper::writeString(rawData, this->name).isError) { - return ErrorOrVoid(true, ErrorCodes::OVERRUN); - } - + ErrorOrVoid Float::serializeWithoutHeader(std::vector* rawData) { Helper::writeFloat(rawData, this->value); - return ErrorOrVoid(); } @@ -753,15 +732,8 @@ namespace NBT { this->value = value; } - ErrorOrVoid Double::toRawData(std::vector* rawData) { - rawData->push_back(this->type); - - if (Helper::writeString(rawData, this->name).isError) { - return ErrorOrVoid(true, ErrorCodes::OVERRUN); - } - + ErrorOrVoid Double::serializeWithoutHeader(std::vector* rawData) { Helper::writeDouble(rawData, this->value); - return ErrorOrVoid(); } @@ -793,15 +765,8 @@ namespace NBT { 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); - } - + ErrorOrVoid Int8Array::serializeWithoutHeader(std::vector* rawData) { Helper::writeInt8Array(rawData, this->data); - return ErrorOrVoid(); } @@ -855,13 +820,7 @@ namespace NBT { this->value = value; } - ErrorOrVoid String::toRawData(std::vector* rawData) { - rawData->push_back(this->type); - - if (Helper::writeString(rawData, this->name).isError) { - return ErrorOrVoid(true, ErrorCodes::OVERRUN); - } - + ErrorOrVoid String::serializeWithoutHeader(std::vector* rawData) { return Helper::writeString(rawData, this->value); } @@ -905,9 +864,23 @@ namespace NBT { } } - ErrorOrVoid List::toRawData(std::vector* rawData) { - #pragma message("TODO: Implement.") - return ErrorOrVoid(true, ErrorCodes::UNIMPLEMENTED); + ErrorOrVoid List::serializeWithoutHeader(std::vector* rawData) { + rawData->push_back(this->containedType); + + // 32 bit signed integer max + if (this->tags.size() > 0x7FFFFFFF) { + return ErrorOrVoid(true, ErrorCodes::OVERRUN); + } + Helper::writeInt32(rawData, this->tags.size()); + + for (int32_t i=0; itags.size(); i++) { + ErrorOrVoid result = this->tags.at(i)->serializeWithoutHeader(rawData); + if (result.isError) { + return result; + } + } + + return ErrorOrVoid(); } ErrorOr List::getElementPointer(uint64_t position) { diff --git a/src/lib/nbt.hpp b/src/lib/nbt.hpp index 0635d44..9fd4dab 100644 --- a/src/lib/nbt.hpp +++ b/src/lib/nbt.hpp @@ -103,7 +103,8 @@ namespace NBT { Generic(); virtual ~Generic(); - virtual ErrorOrVoid toRawData(std::vector* rawData); + virtual ErrorOrVoid serialize(std::vector* rawData); + virtual ErrorOrVoid serializeWithoutHeader(std::vector* rawData); uint8_t getTagType(); }; @@ -111,7 +112,10 @@ namespace NBT { public: End(); - ErrorOrVoid toRawData(std::vector* rawData) override; + ErrorOrVoid serialize(std::vector* rawData) override; + // This needs a separate serializer because + // END tags have a special header. + ErrorOrVoid serializeWithoutHeader(std::vector* rawData) override; }; class Int8: public Generic { @@ -121,7 +125,7 @@ namespace NBT { Int8(); Int8(tiny_utf8::string name, int8_t value); - ErrorOrVoid toRawData(std::vector* rawData) override; + ErrorOrVoid serializeWithoutHeader(std::vector* rawData) override; int8_t getValue(); void setValue(int8_t value); }; @@ -133,7 +137,7 @@ namespace NBT { Int16(); Int16(tiny_utf8::string name, int16_t value); - ErrorOrVoid toRawData(std::vector* rawData) override; + ErrorOrVoid serializeWithoutHeader(std::vector* rawData) override; int16_t getValue(); void setValue(int16_t value); }; @@ -145,7 +149,7 @@ namespace NBT { Int32(); Int32(tiny_utf8::string name, int32_t value); - ErrorOrVoid toRawData(std::vector* rawData) override; + ErrorOrVoid serializeWithoutHeader(std::vector* rawData) override; int32_t getValue(); void setValue(int32_t value); }; @@ -157,7 +161,7 @@ namespace NBT { Int64(); Int64(tiny_utf8::string name, int64_t value); - ErrorOrVoid toRawData(std::vector* rawData) override; + ErrorOrVoid serializeWithoutHeader(std::vector* rawData) override; int64_t getValue(); void setValue(int64_t value); }; @@ -169,7 +173,7 @@ namespace NBT { Float(); Float(tiny_utf8::string name, float value); - ErrorOrVoid toRawData(std::vector* rawData) override; + ErrorOrVoid serializeWithoutHeader(std::vector* rawData) override; float getValue(); void setValue(float value); }; @@ -181,7 +185,7 @@ namespace NBT { Double(); Double(tiny_utf8::string name, double value); - ErrorOrVoid toRawData(std::vector* rawData) override; + ErrorOrVoid serializeWithoutHeader(std::vector* rawData) override; double getValue(); void setValue(double value); }; @@ -194,7 +198,7 @@ namespace NBT { 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; + ErrorOrVoid serializeWithoutHeader(std::vector* rawData) override; std::vector getData(); ErrorOr getValue(uint64_t position); @@ -212,7 +216,7 @@ namespace NBT { String(); String(tiny_utf8::string name, tiny_utf8::string value); - ErrorOrVoid toRawData(std::vector* rawData) override; + ErrorOrVoid serializeWithoutHeader(std::vector* rawData) override; tiny_utf8::string getValue(); void setValue(tiny_utf8::string value); }; @@ -228,7 +232,7 @@ namespace NBT { ~List() override; - ErrorOrVoid toRawData(std::vector* rawData) override; + ErrorOrVoid serializeWithoutHeader(std::vector* rawData) override; ErrorOr getElementPointer(uint64_t position); ErrorOrVoid setElementPointerAt(uint64_t position, Generic*); @@ -247,7 +251,7 @@ namespace NBT { ~Compound() override; - ErrorOrVoid toRawData(std::vector* rawData) override; + ErrorOrVoid serializeWithoutHeader(std::vector* rawData) override; ErrorOr getElementPointer(uint64_t position); ErrorOrVoid setElementPointerAt(uint64_t position, Generic*); @@ -264,7 +268,7 @@ namespace NBT { 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; + ErrorOrVoid serializeWithoutHeader(std::vector* rawData) override; std::vector getData(); ErrorOr getValue(uint64_t position); @@ -283,7 +287,7 @@ namespace NBT { 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; + ErrorOrVoid serializeWithoutHeader(std::vector* rawData) override; std::vector getData(); ErrorOr getValue(uint64_t position);