Fucking around with this old project!!
This commit is contained in:
3
.idea/misc.xml
generated
3
.idea/misc.xml
generated
@@ -1,4 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CMakePythonSetting">
|
||||
<option name="pythonIntegrationState" value="YES" />
|
||||
</component>
|
||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
145
main.cpp
145
main.cpp
@@ -21,15 +21,6 @@ namespace metabinary {
|
||||
// Handles endianness of integer types
|
||||
// All* integer types are written to NetworkByteOrder (big endian)
|
||||
// and read back into HostByteOrder (lil endian for x86)
|
||||
using byte_buf = std::vector<uint8_t>;
|
||||
static int write_uint8(byte_buf& buf, int index, uint8_t val);
|
||||
static int write_uint16(byte_buf& buf, int index, uint16_t val);
|
||||
static int write_uint32(byte_buf& buf, int index, uint32_t val);
|
||||
static int write_uint64(byte_buf& buf, int index, uint64_t val);
|
||||
static int write_int8(byte_buf& buf, int index, int8_t val);
|
||||
static int write_int16(byte_buf& buf, int index, int16_t val);
|
||||
static int write_int32(byte_buf& buf, int index, int32_t val);
|
||||
static int write_int64(byte_buf& buf, int index, int64_t val);
|
||||
|
||||
|
||||
// Writes an 8-bit unsigned int (1-byte) to the buffer at the given index
|
||||
@@ -195,10 +186,15 @@ namespace metabinary {
|
||||
static std::string read_string(uint8_t* buf, int index) {
|
||||
uint32_t str_len = read_uint32(buf, index);
|
||||
std::vector<uint8_t> bytes(str_len);
|
||||
for (int i = 0; i < str_len; i++)
|
||||
for (int i = 0; i < str_len; ++i)
|
||||
bytes[i] = buf[i+4];
|
||||
return GetStringFromBytes(bytes);
|
||||
}
|
||||
|
||||
static uint32_t read_string_size(uint8_t* buf, int index)
|
||||
{
|
||||
return read_uint32(buf, index);
|
||||
}
|
||||
#pragma endregion
|
||||
#pragma endregion
|
||||
typedef enum {
|
||||
@@ -224,12 +220,20 @@ namespace metabinary {
|
||||
tag(std::string name) {
|
||||
this->name = name;
|
||||
}
|
||||
// Deserialize Constructor
|
||||
tag(uint8_t* buf, int index)
|
||||
{
|
||||
name = read_string(buf, index);
|
||||
}
|
||||
|
||||
virtual int serialize(uint8_t* buf, int startidx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//static tag deserialize(byte* data);
|
||||
// Encodes name length + utf8 string
|
||||
int write_name(uint8_t* buf, int startidx)
|
||||
@@ -252,22 +256,29 @@ namespace metabinary {
|
||||
class end_tag : public tag {
|
||||
public:
|
||||
int serialize(uint8_t *buf, int startidx) override {}
|
||||
|
||||
};
|
||||
class uint8_tag : public tag {
|
||||
private:
|
||||
uint8_t payload;
|
||||
public:
|
||||
uint8_tag() {}
|
||||
|
||||
uint8_tag(std::string name) {}
|
||||
uint8_tag(std::string name, uint8_t data)
|
||||
{
|
||||
|
||||
uint8_tag(std::string name, uint8_t data) {
|
||||
this->name = name;
|
||||
payload = data;
|
||||
}
|
||||
static uint8_t deserialize(uint8_t *buf, int index)
|
||||
{
|
||||
|
||||
uint8_tag(uint8_t *buffer, int startidx) : tag(buffer, startidx)
|
||||
{
|
||||
int name_size = read_string_size(buffer, startidx);
|
||||
startidx+=name_size;
|
||||
|
||||
this->payload = read_uint8(buffer, startidx);
|
||||
}
|
||||
|
||||
int serialize(uint8_t *buf, int startidx) override {
|
||||
int offset = startidx;
|
||||
offset += write_type(buf, offset, metabinary::tag_uint8);
|
||||
@@ -289,6 +300,7 @@ namespace metabinary {
|
||||
this->name = name;
|
||||
payload = data;
|
||||
}
|
||||
uint16_tag(uint8_t* buffer, int startidx) {}
|
||||
int serialize(uint8_t *buf, int startidx) override {
|
||||
int offset = startidx;
|
||||
offset += write_type(buf, offset, metabinary::tag_uint16);
|
||||
@@ -503,21 +515,21 @@ namespace metabinary {
|
||||
private:
|
||||
};
|
||||
class compound_tag : public tag {
|
||||
private:
|
||||
std::vector<tag*> payload;
|
||||
protected:
|
||||
std::vector<tag> payload;
|
||||
public:
|
||||
compound_tag() {}
|
||||
compound_tag(std::string name) {}
|
||||
compound_tag(std::string name, std::vector<tag*> tags)
|
||||
compound_tag(std::string name, std::vector<tag> tags)
|
||||
{
|
||||
this->name = name;
|
||||
payload = tags;
|
||||
}
|
||||
tag* get(std::string name) const {
|
||||
tag get(std::string name) const {
|
||||
|
||||
for (auto& child : payload)
|
||||
{
|
||||
if (child->name == name)
|
||||
if (child.name == name)
|
||||
return child;
|
||||
}
|
||||
}
|
||||
@@ -530,7 +542,7 @@ namespace metabinary {
|
||||
|
||||
// Add Serialized Child Tags
|
||||
for (auto& tag : payload)
|
||||
offset += tag->serialize(buffer, offset);
|
||||
offset += tag.serialize(buffer, offset);
|
||||
|
||||
// Add END Tag
|
||||
buffer[offset] = tag_end;
|
||||
@@ -547,7 +559,7 @@ namespace metabinary {
|
||||
void add_double() {}
|
||||
void add_string(std::string name, std::string value)
|
||||
{
|
||||
this->payload.push_back(new string_tag(name, value));
|
||||
this->payload.push_back(string_tag(name, value));
|
||||
}
|
||||
};
|
||||
class custom_data_tag : public tag { };
|
||||
@@ -555,10 +567,10 @@ namespace metabinary {
|
||||
public:
|
||||
root_tag() : compound_tag() {};
|
||||
root_tag(std::string name) : compound_tag(name) {}
|
||||
root_tag(std::string name, std::vector<tag*> tags) : compound_tag(name, tags) {}
|
||||
root_tag(std::string name, std::vector<tag> tags) : compound_tag(name, tags) {}
|
||||
};
|
||||
|
||||
tag* deserialize(uint8_t* data, int index = 0)
|
||||
tag deserialize(uint8_t* data, int index = 0)
|
||||
{
|
||||
|
||||
tag_type_t tag_typ = static_cast<tag_type_t>(read_uint8(data, index));
|
||||
@@ -566,11 +578,10 @@ namespace metabinary {
|
||||
case tag_end:
|
||||
case tag_compound:
|
||||
case tag_uint8: {
|
||||
return uint8_tag::deserialize(data, index);
|
||||
return uint8_tag(data, index);
|
||||
}
|
||||
case tag_uint16: {
|
||||
uint16_t result = read_uint16(data, index+4+1+name.size());
|
||||
return new uint16_tag(name, result);
|
||||
return uint16_tag(data, index);
|
||||
break;
|
||||
}
|
||||
case tag_uint32:
|
||||
@@ -670,7 +681,7 @@ namespace tests {
|
||||
}
|
||||
void string_roundtrip_test() {
|
||||
std::string begin = "AYYO WHATS UP BABY";
|
||||
uint8_t buf[begin.size()+4];
|
||||
uint8_t buf[begin.size()+sizeof(uint32_t)];
|
||||
metabinary::write_string(buf, 0, begin);
|
||||
std::string result = metabinary::read_string(buf, 0);
|
||||
//std::cout << begin << " : " << result << std::endl;
|
||||
@@ -697,72 +708,72 @@ int main() {
|
||||
using namespace metabinary;
|
||||
|
||||
root_tag test_file1{"Test File",{
|
||||
new uint32_tag("bruh", 42069),
|
||||
uint32_tag("bruh", 42069),
|
||||
}};
|
||||
uint8_t data[69];
|
||||
test_file1.serialize(data, 0);
|
||||
|
||||
root_tag reconstructed = metabinary::deserialize(data);
|
||||
tag reconstructed = metabinary::deserialize(data);
|
||||
|
||||
auto tone = new uint16_tag{"b", 123};
|
||||
auto tone = uint16_tag{"b", 123};
|
||||
|
||||
compound_tag item_meta = {"", {
|
||||
new uint16_tag{"id", 64},
|
||||
new uint16_tag{"quantity", 64},
|
||||
uint16_tag{"id", 64},
|
||||
uint16_tag{"quantity", 64},
|
||||
tone
|
||||
}};
|
||||
item_meta.add_string("custom_name", "BALLIN");
|
||||
|
||||
|
||||
root_tag demo_file { "DEMO METABINARY FILE", {
|
||||
new string_tag{"MAP_NAME", "LEVEL1"},
|
||||
new string_tag{"MAP_AUTHOR", "brogrammer"},
|
||||
new uint64_tag{"MAP_EDIT_TIMESTAMP", 999999},
|
||||
new compound_tag{"SHADERCACHE"},
|
||||
new compound_tag{"ENTITIES",{
|
||||
new compound_tag{"1", {
|
||||
new uint64_tag{"uuid", 42069},
|
||||
new compound_tag{"pos", {
|
||||
new float_tag{"x", 0.25f},
|
||||
new float_tag{"y", 0.25f},
|
||||
new float_tag{"angle", 3.1415f},
|
||||
string_tag{"MAP_NAME", "LEVEL1"},
|
||||
string_tag{"MAP_AUTHOR", "brogrammer"},
|
||||
uint64_tag{"MAP_EDIT_TIMESTAMP", 999999},
|
||||
compound_tag{"SHADERCACHE"},
|
||||
compound_tag{"ENTITIES",{
|
||||
compound_tag{"1", {
|
||||
uint64_tag{"uuid", 42069},
|
||||
compound_tag{"pos", {
|
||||
float_tag{"x", 0.25f},
|
||||
float_tag{"y", 0.25f},
|
||||
float_tag{"angle", 3.1415f},
|
||||
}},
|
||||
}},
|
||||
new compound_tag{"2", {
|
||||
new uint64_tag{"uuid", 42044469},
|
||||
new compound_tag{"pos", {
|
||||
new float_tag{"x", 0.25f},
|
||||
new float_tag{"y", 0.25f},
|
||||
new float_tag{"angle", 3.1415f},
|
||||
compound_tag{"2", {
|
||||
uint64_tag{"uuid", 42044469},
|
||||
compound_tag{"pos", {
|
||||
float_tag{"x", 0.25f},
|
||||
float_tag{"y", 0.25f},
|
||||
float_tag{"angle", 3.1415f},
|
||||
}},
|
||||
}},
|
||||
new compound_tag{"3", {
|
||||
new uint64_tag{"uuid", 66642044469},
|
||||
new compound_tag{"pos", {
|
||||
new float_tag{"x", 0.25f},
|
||||
new float_tag{"y", 0.25f},
|
||||
new float_tag{"angle", 3.1415f},
|
||||
compound_tag{"3", {
|
||||
uint64_tag{"uuid", 66642044469},
|
||||
compound_tag{"pos", {
|
||||
float_tag{"x", 0.25f},
|
||||
float_tag{"y", 0.25f},
|
||||
float_tag{"angle", 3.1415f},
|
||||
}},
|
||||
}},
|
||||
new compound_tag{"4", {
|
||||
new uint64_tag{"uuid", 696969},
|
||||
new compound_tag{"pos", {
|
||||
new float_tag{"x", 0.25f},
|
||||
new float_tag{"y", 0.25f},
|
||||
new float_tag{"angle", 3.1415f},
|
||||
compound_tag{"4", {
|
||||
uint64_tag{"uuid", 696969},
|
||||
compound_tag{"pos", {
|
||||
float_tag{"x", 0.25f},
|
||||
float_tag{"y", 0.25f},
|
||||
float_tag{"angle", 3.1415f},
|
||||
}},
|
||||
}},
|
||||
}},
|
||||
|
||||
new compound_tag{"boyz", {
|
||||
new float_tag{"x", 0.25f},
|
||||
new float_tag{"y", 0.25f},
|
||||
new double_tag{"magic_number", 3.1414951},
|
||||
compound_tag{"boyz", {
|
||||
float_tag{"x", 0.25f},
|
||||
float_tag{"y", 0.25f},
|
||||
double_tag{"magic_number", 3.1414951},
|
||||
}},
|
||||
}};
|
||||
|
||||
tag *t = demo_file.get("SHORTY");
|
||||
std::cout << t->name << std::endl;
|
||||
tag t = demo_file.get("SHORTY");
|
||||
std::cout << t.name << std::endl;
|
||||
uint8_t byte_buff[9999];
|
||||
demo_file.serialize(byte_buff, 0);
|
||||
// copy byte to char buff for writing
|
||||
|
Reference in New Issue
Block a user