lib/nbt: Start implementing the in-memory data structure for NBT
parent
ad291ee77d
commit
53878c3e2b
|
@ -89,6 +89,8 @@ namespace ErrorCodes {
|
||||||
|
|
||||||
const uint8_t NOT_YET_KNOWN = 7;
|
const uint8_t NOT_YET_KNOWN = 7;
|
||||||
|
|
||||||
|
const uint8_t INVALID_TYPE = 8;
|
||||||
|
|
||||||
const uint8_t UNIMPLEMENTED = 254;
|
const uint8_t UNIMPLEMENTED = 254;
|
||||||
|
|
||||||
const uint8_t UNKNOWN = 255;
|
const uint8_t UNKNOWN = 255;
|
||||||
|
|
|
@ -543,56 +543,15 @@ namespace NBT {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Tag constructors
|
// generic class that all tag types are derived from
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
Tag<T>::Tag(uint8_t tagType, tiny_utf8::string name, uint16_t nameSize, T content, uint32_t size)
|
Tag<T>::Tag() {
|
||||||
: tagType(tagType), name(name), nameSize(nameSize), content(content) ,size(size)
|
|
||||||
{}
|
|
||||||
|
|
||||||
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<int16_t> 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; i<this->nameSize+2; i++){
|
|
||||||
nameSlice[i] = data[i+1];
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<tiny_utf8::string> 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
|
template <typename T>
|
||||||
bool Byte::validate(uint8_t data[]){
|
ErrorOr<std::vector<uint8_t>> Tag<T>::toRawData() {
|
||||||
if(data[0] == 0x01){
|
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::INVALID_TYPE);
|
||||||
return true;
|
|
||||||
}else{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
|
|
@ -72,45 +72,34 @@ namespace NBT {
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace TagType {
|
namespace TagType {
|
||||||
const uint8_t END = 0;
|
const uint8_t END = 0;
|
||||||
const uint8_t INT8 = 1;
|
const uint8_t INT8 = 1;
|
||||||
const uint8_t INT16 = 2;
|
const uint8_t INT16 = 2;
|
||||||
const uint8_t INT32 = 3;
|
const uint8_t INT32 = 3;
|
||||||
const uint8_t INT64 = 4;
|
const uint8_t INT64 = 4;
|
||||||
const uint8_t FLOAT = 5;
|
const uint8_t FLOAT = 5;
|
||||||
const uint8_t DOUBLE = 6;
|
const uint8_t DOUBLE = 6;
|
||||||
const uint8_t INT8_ARRAY = 7;
|
const uint8_t INT8_ARRAY = 7;
|
||||||
const uint8_t STRING = 8;
|
const uint8_t STRING = 8;
|
||||||
const uint8_t LIST = 9;
|
const uint8_t LIST = 9;
|
||||||
const uint8_t COMPOUND = 10;
|
const uint8_t COMPOUND = 10;
|
||||||
const uint8_t INT32_ARRAY= 11;
|
const uint8_t INT32_ARRAY= 11;
|
||||||
const uint8_t INT64_ARRAY= 12;
|
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 <typename T>
|
template <typename T>
|
||||||
class Tag{
|
struct Tag {
|
||||||
public:
|
const uint8_t type = TagType::INVALID;
|
||||||
uint8_t tagType;
|
T* containedData;
|
||||||
tiny_utf8::string name;
|
|
||||||
uint16_t nameSize;
|
|
||||||
T content;
|
|
||||||
int32_t size;
|
|
||||||
|
|
||||||
Tag(){}
|
Tag();
|
||||||
Tag(uint8_t tagType, tiny_utf8::string name, uint16_t nameSize, T content, uint32_t size);
|
ErrorOr<std::vector<uint8_t>> toRawData();
|
||||||
};
|
|
||||||
|
|
||||||
class End: public Tag<uint8_t>{
|
|
||||||
public:
|
|
||||||
End();
|
|
||||||
};
|
|
||||||
|
|
||||||
class Byte: public Tag<int8_t>{
|
|
||||||
public:
|
|
||||||
Byte(tiny_utf8::string name, uint16_t nameSize, int8_t content);
|
|
||||||
Byte(uint8_t data[]);
|
|
||||||
bool validate(uint8_t data[]);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
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);
|
||||||
|
|
Loading…
Reference in New Issue