Compare commits
2 Commits
5920d1d004
...
4c4366f7e6
Author | SHA1 | Date |
---|---|---|
BodgeMaster | 4c4366f7e6 | |
BodgeMaster | 4ef1d2c44f |
109
src/lib/nbt.cpp
109
src/lib/nbt.cpp
|
@ -550,10 +550,20 @@ namespace NBT {
|
|||
|
||||
Generic::~Generic() {}
|
||||
|
||||
ErrorOrVoid Generic::toRawData([[maybe_unused]] std::vector<uint8_t>* rawData) {
|
||||
ErrorOrVoid Generic::serializeWithoutHeader([[maybe_unused]] std::vector<uint8_t>* rawData) {
|
||||
return ErrorOrVoid(true, ErrorCodes::INVALID_TYPE);
|
||||
}
|
||||
|
||||
ErrorOrVoid Generic::serialize(std::vector<uint8_t>* 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<uint8_t>* rawData) {
|
||||
ErrorOrVoid End::serializeWithoutHeader([[maybe_unused]] std::vector<uint8_t>* rawData) {
|
||||
return ErrorOrVoid();
|
||||
}
|
||||
|
||||
ErrorOrVoid End::serialize(std::vector<uint8_t>* rawData) {
|
||||
rawData->push_back(this->type);
|
||||
return ErrorOrVoid();
|
||||
}
|
||||
|
@ -578,15 +592,8 @@ namespace NBT {
|
|||
this->value = value;
|
||||
}
|
||||
|
||||
ErrorOrVoid Int8::toRawData(std::vector<uint8_t>* rawData) {
|
||||
rawData->push_back(this->type);
|
||||
|
||||
if (Helper::writeString(rawData, this->name).isError) {
|
||||
return ErrorOrVoid(true, ErrorCodes::OVERRUN);
|
||||
}
|
||||
|
||||
ErrorOrVoid Int8::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
|
||||
Helper::writeInt8(rawData, this->value);
|
||||
|
||||
return ErrorOrVoid();
|
||||
}
|
||||
|
||||
|
@ -613,15 +620,8 @@ namespace NBT {
|
|||
this->value = value;
|
||||
}
|
||||
|
||||
ErrorOrVoid Int16::toRawData(std::vector<uint8_t>* rawData) {
|
||||
rawData->push_back(this->type);
|
||||
|
||||
if (Helper::writeString(rawData, this->name).isError) {
|
||||
return ErrorOrVoid(true, ErrorCodes::OVERRUN);
|
||||
}
|
||||
|
||||
ErrorOrVoid Int16::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
|
||||
Helper::writeInt16(rawData, this->value);
|
||||
|
||||
return ErrorOrVoid();
|
||||
}
|
||||
|
||||
|
@ -648,15 +648,8 @@ namespace NBT {
|
|||
this->value = value;
|
||||
}
|
||||
|
||||
ErrorOrVoid Int32::toRawData(std::vector<uint8_t>* rawData) {
|
||||
rawData->push_back(this->type);
|
||||
|
||||
if (Helper::writeString(rawData, this->name).isError) {
|
||||
return ErrorOrVoid(true, ErrorCodes::OVERRUN);
|
||||
}
|
||||
|
||||
ErrorOrVoid Int32::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
|
||||
Helper::writeInt32(rawData, this->value);
|
||||
|
||||
return ErrorOrVoid();
|
||||
}
|
||||
|
||||
|
@ -683,15 +676,8 @@ namespace NBT {
|
|||
this->value = value;
|
||||
}
|
||||
|
||||
ErrorOrVoid Int64::toRawData(std::vector<uint8_t>* rawData) {
|
||||
rawData->push_back(this->type);
|
||||
|
||||
if (Helper::writeString(rawData, this->name).isError) {
|
||||
return ErrorOrVoid(true, ErrorCodes::OVERRUN);
|
||||
}
|
||||
|
||||
ErrorOrVoid Int64::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
|
||||
Helper::writeInt64(rawData, this->value);
|
||||
|
||||
return ErrorOrVoid();
|
||||
}
|
||||
|
||||
|
@ -718,15 +704,8 @@ namespace NBT {
|
|||
this->value = value;
|
||||
}
|
||||
|
||||
ErrorOrVoid Float::toRawData(std::vector<uint8_t>* rawData) {
|
||||
rawData->push_back(this->type);
|
||||
|
||||
if (Helper::writeString(rawData, this->name).isError) {
|
||||
return ErrorOrVoid(true, ErrorCodes::OVERRUN);
|
||||
}
|
||||
|
||||
ErrorOrVoid Float::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
|
||||
Helper::writeFloat(rawData, this->value);
|
||||
|
||||
return ErrorOrVoid();
|
||||
}
|
||||
|
||||
|
@ -753,15 +732,8 @@ namespace NBT {
|
|||
this->value = value;
|
||||
}
|
||||
|
||||
ErrorOrVoid Double::toRawData(std::vector<uint8_t>* rawData) {
|
||||
rawData->push_back(this->type);
|
||||
|
||||
if (Helper::writeString(rawData, this->name).isError) {
|
||||
return ErrorOrVoid(true, ErrorCodes::OVERRUN);
|
||||
}
|
||||
|
||||
ErrorOrVoid Double::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
|
||||
Helper::writeDouble(rawData, this->value);
|
||||
|
||||
return ErrorOrVoid();
|
||||
}
|
||||
|
||||
|
@ -793,15 +765,8 @@ namespace NBT {
|
|||
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);
|
||||
}
|
||||
|
||||
ErrorOrVoid Int8Array::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
|
||||
Helper::writeInt8Array(rawData, this->data);
|
||||
|
||||
return ErrorOrVoid();
|
||||
}
|
||||
|
||||
|
@ -855,13 +820,7 @@ namespace NBT {
|
|||
this->value = value;
|
||||
}
|
||||
|
||||
ErrorOrVoid String::toRawData(std::vector<uint8_t>* rawData) {
|
||||
rawData->push_back(this->type);
|
||||
|
||||
if (Helper::writeString(rawData, this->name).isError) {
|
||||
return ErrorOrVoid(true, ErrorCodes::OVERRUN);
|
||||
}
|
||||
|
||||
ErrorOrVoid String::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
|
||||
return Helper::writeString(rawData, this->value);
|
||||
}
|
||||
|
||||
|
@ -905,9 +864,23 @@ namespace NBT {
|
|||
}
|
||||
}
|
||||
|
||||
ErrorOrVoid List::toRawData(std::vector<uint8_t>* rawData) {
|
||||
#pragma message("TODO: Implement.")
|
||||
return ErrorOrVoid(true, ErrorCodes::UNIMPLEMENTED);
|
||||
ErrorOrVoid List::serializeWithoutHeader(std::vector<uint8_t>* 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; i<this->tags.size(); i++) {
|
||||
ErrorOrVoid result = this->tags.at(i)->serializeWithoutHeader(rawData);
|
||||
if (result.isError) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ErrorOrVoid();
|
||||
}
|
||||
|
||||
ErrorOr<Generic*> List::getElementPointer(uint64_t position) {
|
||||
|
|
|
@ -19,20 +19,20 @@
|
|||
// NBT tags have a type, optionally a name which consists of the name size and the name string, optionally content type, and optionally a payload which can consist of optionally content type, optionally a content size,
|
||||
// and the stored content. The format in which they are stored is as follows: <type><name size><name><payload>. All numbers are stored in big endian representation.
|
||||
// All tag types:
|
||||
// generic representation: Tag(uint8:tag_type, String:name, uint16:name_size, byte[]:content, int32:size)
|
||||
// None (compound end): Tag( 0, "", 0, None, 0) => used to determine the end of a compound tag, only the type gets stored
|
||||
// int8: Tag( 1, String:name, uint16:name_size, int8:content, 1) => a single signed byte, size not stored
|
||||
// int16: Tag( 2, String:name, uint16:name_size, int16:content, 2) => 16 bit signed integer, size not stored
|
||||
// int32: Tag( 3, String:name, uint16:name_size, int32:content, 4) => 32 bit signed integer, size not stored
|
||||
// int64: Tag( 4, String:name, uint16:name_size, int64:content, 8) => 64 bit signed integer, size not stored
|
||||
// float: Tag( 5, String:name, uint16:name_size, float:content, 4) => 32 bit IEEE754 floating point number, size not stored
|
||||
// double: Tag( 6, String:name, uint16:name_size, double:content, 8) => 64 bit IEEE754 floating point number, size not stored
|
||||
// int8[]: Tag( 7, String:name, uint16:name_size, int8[]:content, int32:size) => content stored prefixed with size
|
||||
// String: Tag( 8, String:name, uint16:name_size, String:content, uint16:size) => Java style modified UTF-8 string, content stored prefixed with size
|
||||
// Tag[] (list): Tag<Tag:type>( 9, String:name, uint16:name_size, Tag[]:content, int32:size) => list of tags of the same type with tag type and name information omitted prefixed by (in order) content type and size
|
||||
// Tag[] (compound): Tag(10, String:name, uint16:name_size, Tag[]:content, int32:size) => list of tags, last tag is always an end tag, size not stored
|
||||
// int32[]: Tag(11, String:name, uint16:name_size, int32[]:content,int32:size) => list of 32 bit signed integers prefixed with its size, endianness not verified at this point
|
||||
// int64[]: Tag(12, String:name, uint16:name_size, int64[]:content,int32:size) => list of 64 bit signed integers prefixed with its size, endianness not verified at this point
|
||||
// Generic: Tag(uint8:tag_type, String:name, uint16:name_size, byte[]:content, int32:size)
|
||||
// End: Tag( 0, "", 0, None, 0) => used to determine the end of a compound tag, only the type gets stored
|
||||
// Int8: Tag( 1, String:name, uint16:name_size, int8:content, 1) => a single signed byte, size not stored
|
||||
// Int16: Tag( 2, String:name, uint16:name_size, int16:content, 2) => 16 bit signed integer, size not stored
|
||||
// Int32: Tag( 3, String:name, uint16:name_size, int32:content, 4) => 32 bit signed integer, size not stored
|
||||
// Int64: Tag( 4, String:name, uint16:name_size, int64:content, 8) => 64 bit signed integer, size not stored
|
||||
// Float: Tag( 5, String:name, uint16:name_size, float:content, 4) => 32 bit IEEE754 floating point number, size not stored
|
||||
// Double: Tag( 6, String:name, uint16:name_size, double:content, 8) => 64 bit IEEE754 floating point number, size not stored
|
||||
// Int8Array: Tag( 7, String:name, uint16:name_size, int8[]:content, int32:length) => content stored prefixed with size
|
||||
// String: Tag( 8, String:name, uint16:name_size, String:content, uint16:size) => Java style modified UTF-8 string, content stored prefixed with size
|
||||
// List: Tag<Tag:type>( 9, String:name, uint16:name_size, Tag[]:content, int32:length) => list of tags of the same type with tag type and name information omitted prefixed by (in order) content type and length
|
||||
// Compound: Tag(10, String:name, uint16:name_size, Tag[]:content, int32:length) => list of tags, last tag is always an end tag, size not stored
|
||||
// Int32Array: Tag(11, String:name, uint16:name_size, int32[]:content,int32:length) => list of 32 bit signed integers prefixed with its size
|
||||
// Int64Array: Tag(12, String:name, uint16:name_size, int64[]:content,int32:length) => list of 64 bit signed integers prefixed with its size
|
||||
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
@ -103,7 +103,8 @@ namespace NBT {
|
|||
Generic();
|
||||
virtual ~Generic();
|
||||
|
||||
virtual ErrorOrVoid toRawData(std::vector<uint8_t>* rawData);
|
||||
virtual ErrorOrVoid serialize(std::vector<uint8_t>* rawData);
|
||||
virtual ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData);
|
||||
uint8_t getTagType();
|
||||
};
|
||||
|
||||
|
@ -111,7 +112,10 @@ namespace NBT {
|
|||
public:
|
||||
End();
|
||||
|
||||
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
|
||||
ErrorOrVoid serialize(std::vector<uint8_t>* rawData) override;
|
||||
// This needs a separate serializer because
|
||||
// END tags have a special header.
|
||||
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* 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<uint8_t>* rawData) override;
|
||||
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* 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<uint8_t>* rawData) override;
|
||||
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* 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<uint8_t>* rawData) override;
|
||||
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* 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<uint8_t>* rawData) override;
|
||||
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* 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<uint8_t>* rawData) override;
|
||||
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* 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<uint8_t>* rawData) override;
|
||||
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
|
||||
double getValue();
|
||||
void setValue(double value);
|
||||
};
|
||||
|
@ -194,7 +198,7 @@ namespace NBT {
|
|||
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;
|
||||
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
|
||||
|
||||
std::vector<int8_t> getData();
|
||||
ErrorOr<int8_t> getValue(uint64_t position);
|
||||
|
@ -212,7 +216,7 @@ namespace NBT {
|
|||
String();
|
||||
String(tiny_utf8::string name, tiny_utf8::string value);
|
||||
|
||||
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
|
||||
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
|
||||
tiny_utf8::string getValue();
|
||||
void setValue(tiny_utf8::string value);
|
||||
};
|
||||
|
@ -228,7 +232,7 @@ namespace NBT {
|
|||
|
||||
~List() override;
|
||||
|
||||
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
|
||||
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
|
||||
|
||||
ErrorOr<Generic*> getElementPointer(uint64_t position);
|
||||
ErrorOrVoid setElementPointerAt(uint64_t position, Generic*);
|
||||
|
@ -247,7 +251,7 @@ namespace NBT {
|
|||
|
||||
~Compound() override;
|
||||
|
||||
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
|
||||
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
|
||||
|
||||
ErrorOr<Generic*> getElementPointer(uint64_t position);
|
||||
ErrorOrVoid setElementPointerAt(uint64_t position, Generic*);
|
||||
|
@ -264,7 +268,7 @@ namespace NBT {
|
|||
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;
|
||||
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
|
||||
|
||||
std::vector<int32_t> getData();
|
||||
ErrorOr<int32_t> getValue(uint64_t position);
|
||||
|
@ -283,7 +287,7 @@ namespace NBT {
|
|||
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;
|
||||
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
|
||||
|
||||
std::vector<int64_t> getData();
|
||||
ErrorOr<int64_t> getValue(uint64_t position);
|
||||
|
|
Loading…
Reference in New Issue