Compare commits

..

No commits in common. "4c4366f7e6741df0c6ff2e23f36359e45a0710d0" and "5920d1d0046369791d63fa69b32a6fa50cbdd5a0" have entirely different histories.

2 changed files with 96 additions and 73 deletions

View File

@ -550,20 +550,10 @@ namespace NBT {
Generic::~Generic() {}
ErrorOrVoid Generic::serializeWithoutHeader([[maybe_unused]] std::vector<uint8_t>* rawData) {
ErrorOrVoid Generic::toRawData([[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;
}
@ -573,11 +563,7 @@ namespace NBT {
this->type = TagType::END;
}
ErrorOrVoid End::serializeWithoutHeader([[maybe_unused]] std::vector<uint8_t>* rawData) {
return ErrorOrVoid();
}
ErrorOrVoid End::serialize(std::vector<uint8_t>* rawData) {
ErrorOrVoid End::toRawData(std::vector<uint8_t>* rawData) {
rawData->push_back(this->type);
return ErrorOrVoid();
}
@ -592,8 +578,15 @@ namespace NBT {
this->value = value;
}
ErrorOrVoid Int8::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
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);
}
Helper::writeInt8(rawData, this->value);
return ErrorOrVoid();
}
@ -620,8 +613,15 @@ namespace NBT {
this->value = value;
}
ErrorOrVoid Int16::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
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);
}
Helper::writeInt16(rawData, this->value);
return ErrorOrVoid();
}
@ -648,8 +648,15 @@ namespace NBT {
this->value = value;
}
ErrorOrVoid Int32::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
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);
}
Helper::writeInt32(rawData, this->value);
return ErrorOrVoid();
}
@ -676,8 +683,15 @@ namespace NBT {
this->value = value;
}
ErrorOrVoid Int64::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
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);
}
Helper::writeInt64(rawData, this->value);
return ErrorOrVoid();
}
@ -704,8 +718,15 @@ namespace NBT {
this->value = value;
}
ErrorOrVoid Float::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
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);
}
Helper::writeFloat(rawData, this->value);
return ErrorOrVoid();
}
@ -732,8 +753,15 @@ namespace NBT {
this->value = value;
}
ErrorOrVoid Double::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
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);
}
Helper::writeDouble(rawData, this->value);
return ErrorOrVoid();
}
@ -765,8 +793,15 @@ namespace NBT {
this->data = std::vector<int8_t>(data, data+length);
}
ErrorOrVoid Int8Array::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
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();
}
@ -820,7 +855,13 @@ namespace NBT {
this->value = value;
}
ErrorOrVoid String::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
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);
}
return Helper::writeString(rawData, this->value);
}
@ -864,23 +905,9 @@ namespace NBT {
}
}
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();
ErrorOrVoid List::toRawData(std::vector<uint8_t>* rawData) {
#pragma message("TODO: Implement.")
return ErrorOrVoid(true, ErrorCodes::UNIMPLEMENTED);
}
ErrorOr<Generic*> List::getElementPointer(uint64_t position) {

View File

@ -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: 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
// 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
#pragma once
#include <cstdint>
@ -103,8 +103,7 @@ namespace NBT {
Generic();
virtual ~Generic();
virtual ErrorOrVoid serialize(std::vector<uint8_t>* rawData);
virtual ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData);
virtual ErrorOrVoid toRawData(std::vector<uint8_t>* rawData);
uint8_t getTagType();
};
@ -112,10 +111,7 @@ namespace NBT {
public:
End();
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;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
};
class Int8: public Generic {
@ -125,7 +121,7 @@ namespace NBT {
Int8();
Int8(tiny_utf8::string name, int8_t value);
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
int8_t getValue();
void setValue(int8_t value);
};
@ -137,7 +133,7 @@ namespace NBT {
Int16();
Int16(tiny_utf8::string name, int16_t value);
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
int16_t getValue();
void setValue(int16_t value);
};
@ -149,7 +145,7 @@ namespace NBT {
Int32();
Int32(tiny_utf8::string name, int32_t value);
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
int32_t getValue();
void setValue(int32_t value);
};
@ -161,7 +157,7 @@ namespace NBT {
Int64();
Int64(tiny_utf8::string name, int64_t value);
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
int64_t getValue();
void setValue(int64_t value);
};
@ -173,7 +169,7 @@ namespace NBT {
Float();
Float(tiny_utf8::string name, float value);
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
float getValue();
void setValue(float value);
};
@ -185,7 +181,7 @@ namespace NBT {
Double();
Double(tiny_utf8::string name, double value);
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
double getValue();
void setValue(double value);
};
@ -198,7 +194,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 serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
std::vector<int8_t> getData();
ErrorOr<int8_t> getValue(uint64_t position);
@ -216,7 +212,7 @@ namespace NBT {
String();
String(tiny_utf8::string name, tiny_utf8::string value);
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
tiny_utf8::string getValue();
void setValue(tiny_utf8::string value);
};
@ -232,7 +228,7 @@ namespace NBT {
~List() override;
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
ErrorOr<Generic*> getElementPointer(uint64_t position);
ErrorOrVoid setElementPointerAt(uint64_t position, Generic*);
@ -251,7 +247,7 @@ namespace NBT {
~Compound() override;
ErrorOrVoid serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
ErrorOr<Generic*> getElementPointer(uint64_t position);
ErrorOrVoid setElementPointerAt(uint64_t position, Generic*);
@ -268,7 +264,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 serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
std::vector<int32_t> getData();
ErrorOr<int32_t> getValue(uint64_t position);
@ -287,7 +283,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 serializeWithoutHeader(std::vector<uint8_t>* rawData) override;
ErrorOrVoid toRawData(std::vector<uint8_t>* rawData) override;
std::vector<int64_t> getData();
ErrorOr<int64_t> getValue(uint64_t position);