From 53878c3e2b6805034ed12490f72493a0f8f8e2c5 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Thu, 15 Sep 2022 06:06:47 +0200 Subject: [PATCH] lib/nbt: Start implementing the in-memory data structure for NBT --- src/lib/error.hpp | 2 ++ src/lib/nbt.cpp | 51 ++++------------------------------------ src/lib/nbt.hpp | 59 +++++++++++++++++++---------------------------- 3 files changed, 31 insertions(+), 81 deletions(-) diff --git a/src/lib/error.hpp b/src/lib/error.hpp index 2cc55ef..58c44eb 100644 --- a/src/lib/error.hpp +++ b/src/lib/error.hpp @@ -89,6 +89,8 @@ namespace ErrorCodes { const uint8_t NOT_YET_KNOWN = 7; + const uint8_t INVALID_TYPE = 8; + const uint8_t UNIMPLEMENTED = 254; const uint8_t UNKNOWN = 255; diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index 4782ad9..17e5352 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -543,56 +543,15 @@ namespace NBT { } } - //Tag constructors - + // generic class that all tag types are derived from template - Tag::Tag(uint8_t tagType, tiny_utf8::string name, uint16_t nameSize, T content, uint32_t size) - : tagType(tagType), name(name), nameSize(nameSize), content(content) ,size(size) - {} + Tag::Tag() { - End::End() : Tag::Tag(0, "", 0, 0, 0) {} - - Byte::Byte(tiny_utf8::string name, uint16_t nameSize, int8_t content) - : Tag::Tag(1, name, nameSize, content, 1) - {} - - Byte::Byte(uint8_t data[]){ - if(validate(data)){ - this->tagType = 1; - - uint8_t nameSizeSlice[] = {data[1], data[2]}; - - ErrorOr readIntResult = Helper::readInt16(nameSizeSlice, 2, 0); - if(!readIntResult.isError){ - this->nameSize = readIntResult.value; - }else{ - throw readIntResult.errorCode; - } - - uint8_t nameSlice[this->nameSize+2]; - for(int i=0; inameSize+2; i++){ - nameSlice[i] = data[i+1]; - } - - ErrorOr readStringResult = Helper::readString(nameSlice, this->nameSize, 0); - if(!readStringResult.isError){ - this->name = readStringResult.value; - }else{ - throw readStringResult.errorCode; - } - - //int8 needs only one byte - this->content = data[this->nameSize+4]; - } } - //more conditions will be added - bool Byte::validate(uint8_t data[]){ - if(data[0] == 0x01){ - return true; - }else{ - return false; - } + template + ErrorOr> Tag::toRawData() { + return ErrorOr>(true, ErrorCodes::INVALID_TYPE); } 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 e1ade91..eae5ed9 100644 --- a/src/lib/nbt.hpp +++ b/src/lib/nbt.hpp @@ -72,45 +72,34 @@ namespace NBT { } namespace TagType { - const uint8_t END = 0; - const uint8_t INT8 = 1; - const uint8_t INT16 = 2; - const uint8_t INT32 = 3; - const uint8_t INT64 = 4; - const uint8_t FLOAT = 5; - const uint8_t DOUBLE = 6; - const uint8_t INT8_ARRAY = 7; - const uint8_t STRING = 8; - const uint8_t LIST = 9; - const uint8_t COMPOUND = 10; - const uint8_t INT32_ARRAY= 11; - const uint8_t INT64_ARRAY= 12; + const uint8_t END = 0; + const uint8_t INT8 = 1; + const uint8_t INT16 = 2; + const uint8_t INT32 = 3; + const uint8_t INT64 = 4; + const uint8_t FLOAT = 5; + const uint8_t DOUBLE = 6; + const uint8_t INT8_ARRAY = 7; + const uint8_t STRING = 8; + const uint8_t LIST = 9; + const uint8_t COMPOUND = 10; + const uint8_t INT32_ARRAY= 11; + const uint8_t INT64_ARRAY= 12; + // This is a workaround that's not part of the spec. + const uint8_t INVALID = 255; + + // This class is used as a placeholder for implementing the end tag. + class End {}; } - //Generic parent class to make declaration easier + // generic class that all tag types are derived from template - class Tag{ - public: - uint8_t tagType; - tiny_utf8::string name; - uint16_t nameSize; - T content; - int32_t size; + struct Tag { + const uint8_t type = TagType::INVALID; + T* containedData; - Tag(){} - Tag(uint8_t tagType, tiny_utf8::string name, uint16_t nameSize, T content, uint32_t size); - }; - - class End: public Tag{ - public: - End(); - }; - - class Byte: public Tag{ - public: - Byte(tiny_utf8::string name, uint16_t nameSize, int8_t content); - Byte(uint8_t data[]); - bool validate(uint8_t data[]); + Tag(); + ErrorOr> toRawData(); }; bool validateRawNBTData(uint8_t data[], uint64_t dataSize, uint64_t initialPosition=0, uint64_t* processedDataSize=nullptr);