From 3837cdcf95a2f74299e6853978aa051db5b8127c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 16 Jan 2024 19:05:27 +0300 Subject: [PATCH 1/6] binary json format introduced + specification --- src/coders/binary_json_spec.md | 56 ++++++ src/coders/byte_utils.cpp | 212 +++++++++++++++++++++++ src/coders/byte_utils.h | 76 +++++++++ src/coders/commons.cpp | 33 ++-- src/coders/json.cpp | 290 +++++++++++++++++++++++++------- src/coders/json.h | 4 +- src/content/ContentLoader.cpp | 2 +- src/files/WorldFiles.cpp | 7 +- src/files/binary_io.cpp | 165 ------------------ src/files/binary_io.h | 47 ------ src/files/files.cpp | 33 +++- src/files/files.h | 22 ++- src/logic/scripting/api_lua.cpp | 28 +-- 13 files changed, 649 insertions(+), 326 deletions(-) create mode 100644 src/coders/binary_json_spec.md create mode 100644 src/coders/byte_utils.cpp create mode 100644 src/coders/byte_utils.h delete mode 100644 src/files/binary_io.cpp delete mode 100644 src/files/binary_io.h diff --git a/src/coders/binary_json_spec.md b/src/coders/binary_json_spec.md new file mode 100644 index 00000000..4e694247 --- /dev/null +++ b/src/coders/binary_json_spec.md @@ -0,0 +1,56 @@ +# Binary JSON Format Specification + +Format version: 1.0 + +This binary data format is developed for use as binary version of JSON in [VoxelEngine-Cpp](https://github.com/MihailRis/VoxelEngine-Cpp) and not compatible with [BSON](https://bsonspec.org/spec.html) due to elements/entries syntax and type codes differences + +## Basic types + +byteorder: little-endian +| Name | Size | Definition | +| ------- | ------- | ----------------------- | +| byte | 1 byte | 8 bit unsigned integer | +| int16 | 2 bytes | 16 bit signed integer | +| int32 | 4 bytes | 32 bit signed integer | +| int64 | 8 bytes | 64 bit unsigned integer | +| uint32 | 4 bytes | 32 bit unsigned integer | +| float64 | 8 bytes | 64 bit floating point | + +## Syntax (RFC 5234) + +```bnf +file = %x01 document / cdocument file content +document = uint32 (*entry) %x00 uint32 stores bytes number + of the encoded document + including the uint32 size +entry = cstring value +value = %x01 document + / %x02 (*value) %x00 list of values + / %x03 byte 8 bit integer + / %x04 int16 16 bit integer + / %x05 int32 32 bit integer + / %x06 int64 64 bit integer + / %x07 float64 number + / %x08 string utf-8 encoded string + / %x09 uint32 (*byte) bytes array + / %x0A boolean 'false' + / %x0B boolean 'true' + / %x0C null value +cdocument = %x1F %x8B (16*byte) gzip-compressed document +cstring = (*%x01-FF) %x00 +string = uint32 (*byte) uint32 stores number of the + encoded string bytes + +float64 = 8byte +int64 = 8byte +uint32 = 4byte +int32 = 4byte +int16 = 2byte +byte = %x00-FF +``` + +## VoxelEngine format support + +Current implementation does not support types: bytes array, null, compressed document. + +All unsupported types will be implemented in future. diff --git a/src/coders/byte_utils.cpp b/src/coders/byte_utils.cpp new file mode 100644 index 00000000..5a404ae5 --- /dev/null +++ b/src/coders/byte_utils.cpp @@ -0,0 +1,212 @@ +#include "byte_utils.h" + +#include +#include +#include + +void ByteBuilder::put(ubyte b) { + buffer.push_back(b); +} + +void ByteBuilder::putCStr(const char* str) { + size_t size = strlen(str)+1; + buffer.reserve(buffer.size() + size); + for (size_t i = 0; i < size; i++) { + buffer.push_back(str[i]); + } +} + +void ByteBuilder::put(const std::string& s) { + size_t len = s.length(); + putInt32(len); + put((const ubyte*)s.data(), len); +} + +void ByteBuilder::put(const ubyte* arr, size_t size) { + buffer.reserve(buffer.size() + size); + for (size_t i = 0; i < size; i++) { + buffer.push_back(arr[i]); + } +} + +void ByteBuilder::putInt16(int16_t val) { + buffer.push_back((char) (val >> 0 & 255)); + buffer.push_back((char) (val >> 8 & 255)); +} + +void ByteBuilder::putInt32(int32_t val) { + buffer.reserve(buffer.size() + 4); + buffer.push_back((char) (val >> 0 & 255)); + buffer.push_back((char) (val >> 8 & 255)); + buffer.push_back((char) (val >> 16 & 255)); + buffer.push_back((char) (val >> 24 & 255)); +} + +void ByteBuilder::putInt64(int64_t val) { + buffer.reserve(buffer.size() + 8); + buffer.push_back((char) (val >> 0 & 255)); + buffer.push_back((char) (val >> 8 & 255)); + buffer.push_back((char) (val >> 16 & 255)); + buffer.push_back((char) (val >> 24 & 255)); + buffer.push_back((char) (val >> 32 & 255)); + buffer.push_back((char) (val >> 40 & 255)); + buffer.push_back((char) (val >> 48 & 255)); + buffer.push_back((char) (val >> 56 & 255)); +} + +void ByteBuilder::putFloat32(float val) { + union { + int32_t vali32; + float valfloat; + } value; + value.valfloat = val; + putInt32(value.vali32); +} + +void ByteBuilder::putFloat64(double val) { + union { + int64_t vali64; + double valfloat; + } value; + value.valfloat = val; + putInt64(value.vali64); +} + +void ByteBuilder::set(size_t position, ubyte val) { + buffer[position] = val; +} + +void ByteBuilder::setInt16(size_t position, int16_t val) { + buffer[position++] = val >> 0 & 255; + buffer[position] = val >> 8 & 255; +} + +void ByteBuilder::setInt32(size_t position, int32_t val) { + buffer[position++] = val >> 0 & 255; + buffer[position++] = val >> 8 & 255; + buffer[position++] = val >> 16 & 255; + buffer[position] = val >> 24 & 255; +} + +void ByteBuilder::setInt64(size_t position, int64_t val) { + buffer[position++] = val >> 0 & 255; + buffer[position++] = val >> 8 & 255; + buffer[position++] = val >> 16 & 255; + buffer[position++] = val >> 24 & 255; + + buffer[position++] = val >> 32 & 255; + buffer[position++] = val >> 40 & 255; + buffer[position++] = val >> 48 & 255; + buffer[position] = val >> 56 & 255; +} + +std::vector ByteBuilder::build() { + return buffer; +} + +ByteReader::ByteReader(const ubyte* data, size_t size) + : data(data), size(size), pos(0) { +} + +ByteReader::ByteReader(const ubyte* data) + : data(data), size(4), pos(0) { + size = getInt32(); +} + +void ByteReader::checkMagic(const char* data, size_t size) { + if (pos + size >= this->size) { + throw std::runtime_error("invalid magic number"); + } + for (size_t i = 0; i < size; i++) { + if (this->data[pos + i] != (ubyte)data[i]){ + throw std::runtime_error("invalid magic number"); + } + } + pos += size; +} + +ubyte ByteReader::get() { + if (pos == size) { + throw std::underflow_error("buffer underflow"); + } + return data[pos++]; +} + +ubyte ByteReader::peek() { + if (pos == size) { + throw std::underflow_error("buffer underflow"); + } + return data[pos]; +} + +int16_t ByteReader::getInt16() { + if (pos+2 > size) { + throw std::underflow_error("unexpected end"); + } + pos += 2; + return (data[pos - 1] << 8) | + (data[pos - 2]); +} + +int32_t ByteReader::getInt32() { + if (pos+4 > size) { + throw std::underflow_error("unexpected end"); + } + pos += 4; + return (data[pos - 1] << 24) | + (data[pos - 2] << 16) | + (data[pos - 3] << 8) | + (data[pos - 4]); +} + +int64_t ByteReader::getInt64() { + if (pos+8 > size) { + throw std::underflow_error("unexpected end"); + } + pos += 8; + return ((int64_t)data[pos - 1] << 56) | + ((int64_t)data[pos - 2] << 48) | + ((int64_t)data[pos - 3] << 40) | + ((int64_t)data[pos - 4] << 32) | + ((int64_t)data[pos - 5] << 24) | + ((int64_t)data[pos - 6] << 16) | + ((int64_t)data[pos - 7] << 8) | + ((int64_t)data[pos - 8]); +} + +float ByteReader::getFloat32() { + union { + int32_t vali32; + float valfloat; + } value; + value.vali32 = getInt32(); + return value.valfloat; +} + +double ByteReader::getFloat64() { + union { + int64_t vali64; + double valfloat; + } value; + value.vali64 = getInt64(); + return value.valfloat; +} + +const char* ByteReader::getCString() { + const char* cstr = (const char*)(data+pos); + pos += strlen(cstr) + 1; + return cstr; +} + +std::string ByteReader::getString() { + uint32_t length = (uint32_t)getInt32(); + if (pos+length > size) { + throw std::underflow_error("unexpected end"); + } + pos += length; + return std::string((const char*)(data+pos-length), length); +} + +bool ByteReader::hasNext() const { + return pos < size; +} diff --git a/src/coders/byte_utils.h b/src/coders/byte_utils.h new file mode 100644 index 00000000..7bd2846d --- /dev/null +++ b/src/coders/byte_utils.h @@ -0,0 +1,76 @@ +#ifndef CODERS_BYTE_UTILS_H_ +#define CODERS_BYTE_UTILS_H_ + +#include +#include +#include "../typedefs.h" + +/* byteorder: little-endian */ +class ByteBuilder { + std::vector buffer; +public: + /* Write one byte (8 bit unsigned integer) */ + void put(ubyte b); + /* Write c-string (bytes array terminated with '\00') */ + void putCStr(const char* str); + /* Write signed 16 bit integer */ + void putInt16(int16_t val); + /* Write signed 32 bit integer */ + void putInt32(int32_t val); + /* Write signed 64 bit integer */ + void putInt64(int64_t val); + /* Write 32 bit floating-point number */ + void putFloat32(float val); + /* Write 64 bit floating-point number */ + void putFloat64(double val); + + /* Write string (uint32 length + bytes) */ + void put(const std::string& s); + /* Write sequence of bytes without any header */ + void put(const ubyte* arr, size_t size); + + void set(size_t position, ubyte val); + void setInt16(size_t position, int16_t val); + void setInt32(size_t position, int32_t val); + void setInt64(size_t position, int64_t val); + + inline size_t size() const { + return buffer.size(); + } + inline const ubyte* data() const { + return buffer.data(); + } + + std::vector build(); +}; + +/* byteorder: little-endian */ +class ByteReader { + const ubyte* data; + size_t size; + size_t pos; +public: + ByteReader(const ubyte* data, size_t size); + ByteReader(const ubyte* data); + + void checkMagic(const char* data, size_t size); + /* Read one byte (unsigned 8 bit integer) */ + ubyte get(); + /* Read one byte (unsigned 8 bit integer) without pointer move */ + ubyte peek(); + /* Read signed 16 bit integer */ + int16_t getInt16(); + /* Read signed 32 bit integer */ + int32_t getInt32(); + /* Read signed 64 bit integer */ + int64_t getInt64(); + /* Read 32 bit floating-point number */ + float getFloat32(); + /* Read 64 bit floating-point number */ + double getFloat64(); + const char* getCString(); + std::string getString(); + bool hasNext() const; +}; + +#endif // CODERS_BYTE_UTILS_H_ diff --git a/src/coders/commons.cpp b/src/coders/commons.cpp index ee7a6841..3a504f4b 100644 --- a/src/coders/commons.cpp +++ b/src/coders/commons.cpp @@ -1,10 +1,9 @@ #include "commons.h" #include +#include #include -using std::string; - inline int char2int(int c) { if (c >= '0' && c <= '9') { return c - '0'; @@ -26,23 +25,24 @@ inline double power(double base, int64_t power) { return result; } -parsing_error::parsing_error(string message, - string filename, - string source, - uint pos, - uint line, - uint linestart) +parsing_error::parsing_error( + std::string message, + std::string filename, + std::string source, + uint pos, + uint line, + uint linestart) : std::runtime_error(message), filename(filename), source(source), pos(pos), line(line), linestart(linestart) { } -string parsing_error::errorLog() const { +std::string parsing_error::errorLog() const { std::stringstream ss; uint linepos = pos - linestart; ss << "parsing error in file '" << filename; ss << "' at " << (line+1) << ":" << linepos << ": " << this->what() << "\n"; size_t end = source.find("\n", linestart); - if (end == string::npos) { + if (end == std::string::npos) { end = source.length(); } ss << source.substr(linestart, end-linestart) << "\n"; @@ -54,7 +54,7 @@ string parsing_error::errorLog() const { } -string escape_string(string s) { +std::string escape_string(std::string s) { std::stringstream ss; ss << '"'; for (char c : s) { @@ -124,7 +124,7 @@ char BasicParser::nextChar() { void BasicParser::expect(char expected) { char c = peek(); if (c != expected) { - throw error("'"+string({expected})+"' expected"); + throw error("'"+std::string({expected})+"' expected"); } pos++; } @@ -153,7 +153,7 @@ char BasicParser::peek() { return source[pos]; } -string BasicParser::parseName() { +std::string BasicParser::parseName() { char c = peek(); if (!is_identifier_start(c)) { if (c == '"') { @@ -262,7 +262,7 @@ bool BasicParser::parseNumber(int sign, number_u& out) { return true; } -string BasicParser::parseString(char quote, bool closeRequired) { +std::string BasicParser::parseString(char quote, bool closeRequired) { std::stringstream ss; while (hasNext()) { char c = source[pos]; @@ -290,7 +290,8 @@ string BasicParser::parseString(char quote, bool closeRequired) { case '/': ss << '/'; break; case '\n': pos++; continue; default: - throw error("'\\" + string({c}) + "' is an illegal escape"); + throw error("'\\" + std::string({c}) + + "' is an illegal escape"); } continue; } @@ -308,4 +309,4 @@ string BasicParser::parseString(char quote, bool closeRequired) { parsing_error BasicParser::error(std::string message) { return parsing_error(message, filename, source, pos, line, linestart); -} \ No newline at end of file +} diff --git a/src/coders/json.cpp b/src/coders/json.cpp index a4559771..64e38383 100644 --- a/src/coders/json.cpp +++ b/src/coders/json.cpp @@ -6,17 +6,28 @@ #include #include "commons.h" +#include "byte_utils.h" using namespace json; -using std::string; -using std::vector; -using std::unique_ptr; -using std::unordered_map; -using std::stringstream; -using std::make_pair; +const int BJSON_END = 0x0; +const int BJSON_TYPE_DOCUMENT = 0x1; +const int BJSON_TYPE_LIST = 0x2; +const int BJSON_TYPE_BYTE = 0x3; +const int BJSON_TYPE_INT16 = 0x4; +const int BJSON_TYPE_INT32 = 0x5; +const int BJSON_TYPE_INT64 = 0x6; +const int BJSON_TYPE_NUMBER = 0x7; +const int BJSON_TYPE_STRING = 0x8; +const int BJSON_TYPE_BYTES = 0x9; +const int BJSON_TYPE_FALSE = 0xA; +const int BJSON_TYPE_TRUE = 0xB; +const int BJSON_TYPE_NULL = 0xC; +const int BJSON_TYPE_CDOCUMENT = 0x1F; -inline void newline(stringstream& ss, bool nice, uint indent, const string indentstr) { +inline void newline(std::stringstream& ss, + bool nice, uint indent, + const std::string& indentstr) { if (nice) { ss << "\n"; for (uint i = 0; i < indent; i++) { @@ -27,29 +38,28 @@ inline void newline(stringstream& ss, bool nice, uint indent, const string inden } } -void stringify(Value* value, - stringstream& ss, +void stringify(const Value* value, + std::stringstream& ss, int indent, - string indentstr, + const std::string& indentstr, bool nice); -void stringifyObj(JObject* obj, - stringstream& ss, +void stringifyObj(const JObject* obj, + std::stringstream& ss, int indent, - string indentstr, + const std::string& indentstr, bool nice); -#include -void stringify(Value* value, - stringstream& ss, +void stringify(const Value* value, + std::stringstream& ss, int indent, - string indentstr, + const std::string& indentstr, bool nice) { if (value->type == valtype::object) { stringifyObj(value->value.obj, ss, indent, indentstr, nice); } else if (value->type == valtype::array) { - vector& list = value->value.arr->values; + std::vector& list = value->value.arr->values; if (list.empty()) { ss << "[]"; return; @@ -81,7 +91,11 @@ void stringify(Value* value, } } -void stringifyObj(JObject* obj, stringstream& ss, int indent, string indentstr, bool nice) { +void stringifyObj(const JObject* obj, + std::stringstream& ss, + int indent, + const std::string& indentstr, + bool nice) { if (obj->map.empty()) { ss << "{}"; return; @@ -107,12 +121,165 @@ void stringifyObj(JObject* obj, stringstream& ss, int indent, string indentstr, ss << '}'; } -string json::stringify(JObject* obj, bool nice, string indent) { - stringstream ss; +std::string json::stringify( + const JObject* obj, + bool nice, + const std::string& indent) { + std::stringstream ss; stringifyObj(obj, ss, 1, indent, nice); return ss.str(); } +static void to_binary(ByteBuilder& builder, const Value* value) { + switch (value->type) { + case valtype::object: { + std::vector bytes = to_binary(value->value.obj); + builder.put(bytes.data(), bytes.size()); + break; + } + case valtype::array: + builder.put(BJSON_TYPE_LIST); + for (Value* element : value->value.arr->values) { + to_binary(builder, element); + } + builder.put(BJSON_END); + break; + case valtype::integer: { + int64_t val = value->value.integer; + if (val >= 0 && val <= 255) { + builder.put(BJSON_TYPE_BYTE); + builder.put(val); + } else if (val >= INT16_MIN && val <= INT16_MAX){ + builder.put(BJSON_TYPE_INT16); + builder.putInt16(val); + } else if (val >= INT32_MIN && val <= INT32_MAX) { + builder.put(BJSON_TYPE_INT32); + builder.putInt32(val); + } else { + builder.put(BJSON_TYPE_INT64); + builder.putInt64(val); + } + break; + } + case valtype::number: + builder.put(BJSON_TYPE_NUMBER); + builder.putFloat64(value->value.decimal); + break; + case valtype::boolean: + builder.put(BJSON_TYPE_FALSE + value->value.boolean); + break; + case valtype::string: + builder.put(BJSON_TYPE_STRING); + builder.put(*value->value.str); + break; + } +} + +static JArray* array_from_binary(ByteReader& reader); +static JObject* object_from_binary(ByteReader& reader); + +std::vector json::to_binary(const JObject* obj) { + ByteBuilder builder; + // type byte + builder.put(BJSON_TYPE_DOCUMENT); + // document size + builder.putInt32(0); + + // writing entries + for (auto& entry : obj->map) { + builder.putCStr(entry.first.c_str()); + to_binary(builder, entry.second); + } + // terminating byte + builder.put(BJSON_END); + + // updating document size + builder.setInt32(1, builder.size()); + return builder.build(); +} + +static Value* value_from_binary(ByteReader& reader) { + ubyte typecode = reader.get(); + valtype type; + valvalue val; + switch (typecode) { + case BJSON_TYPE_DOCUMENT: + type = valtype::object; + reader.getInt32(); + val.obj = object_from_binary(reader); + break; + case BJSON_TYPE_LIST: + type = valtype::array; + val.arr = array_from_binary(reader); + break; + case BJSON_TYPE_BYTE: + type = valtype::integer; + val.integer = reader.get(); + break; + case BJSON_TYPE_INT16: + type = valtype::integer; + val.integer = reader.getInt16(); + break; + case BJSON_TYPE_INT32: + type = valtype::integer; + val.integer = reader.getInt32(); + break; + case BJSON_TYPE_INT64: + type = valtype::integer; + val.integer = reader.getInt64(); + break; + case BJSON_TYPE_NUMBER: + type = valtype::number; + val.decimal = reader.getFloat64(); + break; + case BJSON_TYPE_FALSE: + case BJSON_TYPE_TRUE: + type = valtype::boolean; + val.boolean = typecode - BJSON_TYPE_FALSE; + break; + case BJSON_TYPE_STRING: + type = valtype::string; + val.str = new std::string(reader.getString()); + break; + default: + throw std::runtime_error( + "type "+std::to_string(typecode)+" is not supported"); + } + return new Value(type, val); +} + +static JArray* array_from_binary(ByteReader& reader) { + auto array = std::make_unique(); + auto& items = array->values; + while (reader.peek() != BJSON_END) { + items.push_back(value_from_binary(reader)); + } + reader.get(); + return array.release(); +} + +static JObject* object_from_binary(ByteReader& reader) { + auto obj = std::make_unique(); + auto& map = obj->map; + while (reader.peek() != BJSON_END) { + const char* key = reader.getCString(); + Value* value = value_from_binary(reader); + map.insert(std::make_pair(key, value)); + } + reader.get(); + return obj.release(); +} + +JObject* json::from_binary(const ubyte* src, size_t size) { + ByteReader reader(src, size); + std::unique_ptr value (value_from_binary(reader)); + if (value->type != valtype::object) { + throw std::runtime_error("root value is not an object"); + } + JObject* obj = value->value.obj; + value->value.obj = nullptr; + return obj; +} JArray::~JArray() { for (auto value : values) { @@ -168,9 +335,9 @@ bool JArray::flag(size_t index) const { return values[index]->value.boolean; } -JArray& JArray::put(string value) { +JArray& JArray::put(std::string value) { valvalue val; - val.str = new string(value); + val.str = new std::string(value); values.push_back(new Value(valtype::string, val)); return *this; } @@ -248,11 +415,11 @@ JObject::~JObject() { } } -void JObject::str(string key, string& dst) const { +void JObject::str(std::string key, std::string& dst) const { dst = getStr(key, dst); } -string JObject::getStr(string key, const string& def) const { +std::string JObject::getStr(std::string key, const std::string& def) const { auto found = map.find(key); if (found == map.end()) return def; @@ -266,7 +433,7 @@ string JObject::getStr(string key, const string& def) const { } } -double JObject::getNum(string key, double def) const { +double JObject::getNum(std::string key, double def) const { auto found = map.find(key); if (found == map.end()) return def; @@ -280,7 +447,7 @@ double JObject::getNum(string key, double def) const { } } -int64_t JObject::getInteger(string key, int64_t def) const { +int64_t JObject::getInteger(std::string key, int64_t def) const { auto found = map.find(key); if (found == map.end()) return def; @@ -294,7 +461,7 @@ int64_t JObject::getInteger(string key, int64_t def) const { } } -void JObject::num(string key, double& dst) const { +void JObject::num(std::string key, double& dst) const { dst = getNum(key, dst); } @@ -346,93 +513,93 @@ void JObject::flag(std::string key, bool& dst) const { dst = found->second->value.boolean; } -JObject& JObject::put(string key, uint value) { +JObject& JObject::put(std::string key, uint value) { return put(key, (int64_t)value); } -JObject& JObject::put(string key, int value) { +JObject& JObject::put(std::string key, int value) { return put(key, (int64_t)value); } -JObject& JObject::put(string key, int64_t value) { +JObject& JObject::put(std::string key, int64_t value) { auto found = map.find(key); if (found != map.end()) found->second; valvalue val; val.integer = value; - map.insert(make_pair(key, new Value(valtype::integer, val))); + map.insert(std::make_pair(key, new Value(valtype::integer, val))); return *this; } -JObject& JObject::put(string key, uint64_t value) { +JObject& JObject::put(std::string key, uint64_t value) { return put(key, (int64_t)value); } -JObject& JObject::put(string key, float value) { +JObject& JObject::put(std::string key, float value) { return put(key, (double)value); } -JObject& JObject::put(string key, double value) { +JObject& JObject::put(std::string key, double value) { auto found = map.find(key); if (found != map.end()) delete found->second; valvalue val; val.decimal = value; - map.insert(make_pair(key, new Value(valtype::number, val))); + map.insert(std::make_pair(key, new Value(valtype::number, val))); return *this; } -JObject& JObject::put(string key, string value){ +JObject& JObject::put(std::string key, std::string value){ auto found = map.find(key); if (found != map.end()) delete found->second; valvalue val; - val.str = new string(value); - map.insert(make_pair(key, new Value(valtype::string, val))); + val.str = new std::string(value); + map.insert(std::make_pair(key, new Value(valtype::string, val))); return *this; } JObject& JObject::put(std::string key, const char* value) { - return put(key, string(value)); + return put(key, std::string(value)); } -JObject& JObject::put(string key, JObject* value){ +JObject& JObject::put(std::string key, JObject* value){ auto found = map.find(key); if (found != map.end()) delete found->second; valvalue val; val.obj = value; - map.insert(make_pair(key, new Value(valtype::object, val))); + map.insert(std::make_pair(key, new Value(valtype::object, val))); return *this; } -JObject& JObject::put(string key, JArray* value){ +JObject& JObject::put(std::string key, JArray* value){ auto found = map.find(key); if (found != map.end()) delete found->second; valvalue val; val.arr = value; - map.insert(make_pair(key, new Value(valtype::array, val))); + map.insert(std::make_pair(key, new Value(valtype::array, val))); return *this; } -JObject& JObject::put(string key, bool value){ +JObject& JObject::put(std::string key, bool value){ auto found = map.find(key); if (found != map.end()) delete found->second; valvalue val; val.boolean = value; - map.insert(make_pair(key, new Value(valtype::boolean, val))); + map.insert(std::make_pair(key, new Value(valtype::boolean, val))); return *this; } -JArray& JObject::putArray(string key) { +JArray& JObject::putArray(std::string key) { JArray* arr = new JArray(); put(key, arr); return *arr; } -JObject& JObject::putObj(string key) { +JObject& JObject::putObj(std::string key) { JObject* obj = new JObject(); put(key, obj); return *obj; } -bool JObject::has(string key) { +bool JObject::has(std::string key) { return map.find(key) != map.end(); } @@ -449,7 +616,8 @@ Value::~Value() { } } -Parser::Parser(string filename, string source) : BasicParser(filename, source) { +Parser::Parser(std::string filename, std::string source) + : BasicParser(filename, source) { } JObject* Parser::parse() { @@ -462,20 +630,20 @@ JObject* Parser::parse() { JObject* Parser::parseObject() { expect('{'); - unique_ptr obj(new JObject()); - unordered_map& map = obj->map; + auto obj = std::make_unique(); + auto& map = obj->map; while (peek() != '}') { if (peek() == '#') { skipLine(); continue; } - string key = parseName(); + std::string key = parseName(); char next = peek(); if (next != ':') { throw error("':' expected"); } pos++; - map.insert(make_pair(key, parseValue())); + map.insert(std::make_pair(key, parseValue())); next = peek(); if (next == ',') { pos++; @@ -491,8 +659,8 @@ JObject* Parser::parseObject() { JArray* Parser::parseArray() { expect('['); - unique_ptr arr(new JArray()); - vector& values = arr->values; + auto arr = std::make_unique(); + auto& values = arr->values; while (peek() != ']') { if (peek() == '#') { skipLine(); @@ -530,7 +698,7 @@ Value* Parser::parseValue() { return new Value(type, val); } if (is_identifier_start(next)) { - string literal = parseName(); + std::string literal = parseName(); if (literal == "true") { val.boolean = true; return new Value(valtype::boolean, val); @@ -568,17 +736,17 @@ Value* Parser::parseValue() { } if (next == '"' || next == '\'') { pos++; - val.str = new string(parseString(next)); + val.str = new std::string(parseString(next)); return new Value(valtype::string, val); } - throw error("unexpected character '"+string({next})+"'"); + throw error("unexpected character '"+std::string({next})+"'"); } -JObject* json::parse(string filename, string source) { +JObject* json::parse(std::string filename, std::string source) { Parser parser(filename, source); return parser.parse(); } -JObject* json::parse(string source) { +JObject* json::parse(std::string source) { return parse("", source); } diff --git a/src/coders/json.h b/src/coders/json.h index dd170864..c90e3314 100644 --- a/src/coders/json.h +++ b/src/coders/json.h @@ -16,7 +16,9 @@ namespace json { class JArray; class Value; - extern std::string stringify(JObject* obj, bool nice, std::string indent); + extern std::string stringify(const JObject* obj, bool nice, const std::string& indent); + extern std::vector to_binary(const JObject* obj); + extern JObject* from_binary(const ubyte* src, size_t size); enum class valtype { object, array, number, integer, string, boolean diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 2f7d7b22..f0c803c6 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -87,7 +87,7 @@ void ContentLoader::fixPackIndices() { if (modified){ // rewrite modified json std::cout << indexFile << std::endl; - files::write_string(indexFile, json::stringify(root.get(), true, " ")); + files::write_json(indexFile, root.get()); } } diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index 3896a67e..84e857c0 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -1,7 +1,6 @@ #include "WorldFiles.h" #include "rle.h" -#include "binary_io.h" #include "../window/Camera.h" #include "../content/Content.h" #include "../objects/Player.h" @@ -477,7 +476,7 @@ void WorldFiles::writeIndices(const ContentIndices* indices) { items.put(def->name); } - files::write_string(getIndicesFile(), json::stringify(&root, true, " ")); + files::write_json(getIndicesFile(), &root); } void WorldFiles::writeWorldInfo(const World* world) { @@ -494,7 +493,7 @@ void WorldFiles::writeWorldInfo(const World* world) { timeobj.put("day-time", world->daytime); timeobj.put("day-time-speed", world->daytimeSpeed); - files::write_string(getWorldFile(), json::stringify(&root, true, " ")); + files::write_json(getWorldFile(), &root); } bool WorldFiles::readWorldInfo(World* world) { @@ -540,7 +539,7 @@ void WorldFiles::writePlayer(Player* player){ root.put("flight", player->flight); root.put("noclip", player->noclip); - files::write_string(getPlayerFile(), json::stringify(&root, true, " ")); + files::write_json(getPlayerFile(), &root); } bool WorldFiles::readPlayer(Player* player) { diff --git a/src/files/binary_io.cpp b/src/files/binary_io.cpp deleted file mode 100644 index c16083a3..00000000 --- a/src/files/binary_io.cpp +++ /dev/null @@ -1,165 +0,0 @@ -#include "binary_io.h" - -#include -#include -#include - -void BinaryWriter::put(ubyte b) { - buffer.push_back(b); -} - -void BinaryWriter::putCStr(const char* str) { - size_t size = strlen(str)+1; - buffer.reserve(buffer.size() + size); - for (size_t i = 0; i < size; i++) { - buffer.push_back(str[i]); - } -} - -void BinaryWriter::put(const std::string& s) { - size_t len = s.length(); - if (len > INT16_MAX) { - throw std::domain_error("length > INT16_MAX"); - } - putInt16(len); - put((const ubyte*)s.data(), len); -} - -void BinaryWriter::putShortStr(const std::string& s) { - size_t len = s.length(); - if (len > 255) { - throw std::domain_error("length > 255"); - } - put(len); - put((const ubyte*)s.data(), len); -} - -void BinaryWriter::put(const ubyte* arr, size_t size) { - buffer.reserve(buffer.size() + size); - for (size_t i = 0; i < size; i++) { - buffer.push_back(arr[i]); - } -} - -void BinaryWriter::putInt16(int16_t val) { - buffer.push_back((char) (val >> 8 & 255)); - buffer.push_back((char) (val >> 0 & 255)); -} - -void BinaryWriter::putInt32(int32_t val) { - buffer.reserve(buffer.size() + 4); - buffer.push_back((char) (val >> 24 & 255)); - buffer.push_back((char) (val >> 16 & 255)); - buffer.push_back((char) (val >> 8 & 255)); - buffer.push_back((char) (val >> 0 & 255)); -} - -void BinaryWriter::putInt64(int64_t val) { - buffer.reserve(buffer.size() + 8); - buffer.push_back((char) (val >> 56 & 255)); - buffer.push_back((char) (val >> 48 & 255)); - buffer.push_back((char) (val >> 40 & 255)); - buffer.push_back((char) (val >> 32 & 255)); - buffer.push_back((char) (val >> 24 & 255)); - buffer.push_back((char) (val >> 16 & 255)); - buffer.push_back((char) (val >> 8 & 255)); - buffer.push_back((char) (val >> 0 & 255)); -} - -void BinaryWriter::putFloat32(float val) { - union { - int32_t vali32; - float valfloat; - } value; - value.valfloat = val; - putInt32(value.vali32); -} - -BinaryReader::BinaryReader(const ubyte* data, size_t size) - : data(data), size(size), pos(0) { -} - -void BinaryReader::checkMagic(const char* data, size_t size) { - if (pos + size >= this->size) { - throw std::runtime_error("invalid magic number"); - } - for (size_t i = 0; i < size; i++) { - if (this->data[pos + i] != (ubyte)data[i]){ - throw std::runtime_error("invalid magic number"); - } - } - pos += size; -} - -ubyte BinaryReader::get() { - if (pos == size) { - throw std::underflow_error("buffer underflow"); - } - return data[pos++]; -} - -int16_t BinaryReader::getInt16() { - if (pos+2 > size) { - throw std::underflow_error("unexpected end"); - } - pos += 2; - return (data[pos - 2] << 8) | - (data[pos - 1]); -} - -int32_t BinaryReader::getInt32() { - if (pos+4 > size) { - throw std::underflow_error("unexpected end"); - } - pos += 4; - return (data[pos - 4] << 24) | - (data[pos - 3] << 16) | - (data[pos - 2] << 8) | - (data[pos - 1]); -} - -int64_t BinaryReader::getInt64() { - if (pos+8 > size) { - throw std::underflow_error("unexpected end"); - } - pos += 8; - return ((int64_t)data[pos - 8] << 56) | - ((int64_t)data[pos - 7] << 48) | - ((int64_t)data[pos - 6] << 40) | - ((int64_t)data[pos - 5] << 32) | - ((int64_t)data[pos - 4] << 24) | - ((int64_t)data[pos - 3] << 16) | - ((int64_t)data[pos - 2] << 8) | - ((int64_t)data[pos - 1]); -} - -float BinaryReader::getFloat32() { - union { - int32_t vali32; - float valfloat; - } value; - value.vali32 = getInt32(); - return value.valfloat; -} - -std::string BinaryReader::getString() { - uint16_t length = (uint16_t)getInt16(); - if (pos+length > size) { - throw std::underflow_error("unexpected end"); - } - pos += length; - return std::string((const char*)(data+pos-length), length); -} - -std::string BinaryReader::getShortString() { - ubyte length = get(); - if (pos+length > size) { - throw std::underflow_error("unexpected end"); - } - pos += length; - return std::string((const char*)(data+pos-length), length); -} - -bool BinaryReader::hasNext() const { - return pos < size; -} diff --git a/src/files/binary_io.h b/src/files/binary_io.h deleted file mode 100644 index ac7a7299..00000000 --- a/src/files/binary_io.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef FILES_BINARY_WRITER_H_ -#define FILES_BINARY_WRITER_H_ - -#include -#include -#include "../typedefs.h" - -class BinaryWriter { - std::vector buffer; -public: - void put(ubyte b); - void putCStr(const char* str); - void putInt16(int16_t val); - void putInt32(int32_t val); - void putInt64(int64_t val); - void putFloat32(float val); - void put(const std::string& s); - void put(const ubyte* arr, size_t size); - void putShortStr(const std::string& s); - - inline size_t size() const { - return buffer.size(); - } - inline const ubyte* data() const { - return buffer.data(); - } -}; - -class BinaryReader { - const ubyte* data; - size_t size; - size_t pos; -public: - BinaryReader(const ubyte* data, size_t size); - - void checkMagic(const char* data, size_t size); - ubyte get(); - int16_t getInt16(); - int32_t getInt32(); - int64_t getInt64(); - float getFloat32(); - std::string getString(); - std::string getShortString(); - bool hasNext() const; -}; - -#endif // FILES_BINARY_WRITER_H_ \ No newline at end of file diff --git a/src/files/files.cpp b/src/files/files.cpp index 52eb925d..d2161b6c 100644 --- a/src/files/files.cpp +++ b/src/files/files.cpp @@ -92,16 +92,41 @@ bool files::write_string(fs::path filename, const std::string content) { return true; } -json::JObject* files::read_json(fs::path file) { - std::string text = files::read_string(file); +bool files::write_json(fs::path filename, const json::JObject* obj, bool nice) { + // -- binary json tests + //return write_binary_json(fs::path(filename.u8string()+".bin"), obj); + return files::write_string(filename, json::stringify(obj, nice, " ")); +} + +bool files::write_binary_json(fs::path filename, const json::JObject* obj) { + std::vector bytes = json::to_binary(obj); + return files::write_bytes(filename, (const char*)bytes.data(), bytes.size()); +} + +json::JObject* files::read_json(fs::path filename) { + // binary json tests + // fs::path binfile = fs::path(filename.u8string()+".bin"); + // if (fs::is_regular_file(binfile)){ + // return read_binary_json(binfile); + // } + + std::string text = files::read_string(filename); try { - return json::parse(file.string(), text); + auto obj = json::parse(filename.string(), text); + //write_binary_json(binfile, obj); + return obj; } catch (const parsing_error& error) { std::cerr << error.errorLog() << std::endl; - throw std::runtime_error("could not to parse "+file.string()); + throw std::runtime_error("could not to parse "+filename.string()); } } +json::JObject* files::read_binary_json(fs::path file) { + size_t size; + std::unique_ptr bytes (files::read_bytes(file, size)); + return json::from_binary((const ubyte*)bytes.get(), size); +} + std::vector files::read_list(fs::path filename) { std::ifstream file(filename); if (!file) { diff --git a/src/files/files.h b/src/files/files.h index ddffc814..2ad4a879 100644 --- a/src/files/files.h +++ b/src/files/files.h @@ -7,6 +7,8 @@ #include #include "../typedefs.h" +namespace fs = std::filesystem; + namespace json { class JObject; } @@ -25,14 +27,18 @@ namespace files { }; - extern bool write_bytes(std::filesystem::path, const char* data, size_t size); - extern uint append_bytes(std::filesystem::path, const char* data, size_t size); - extern bool read(std::filesystem::path, char* data, size_t size); - extern char* read_bytes(std::filesystem::path, size_t& length); - extern std::string read_string(std::filesystem::path filename); - extern bool write_string(std::filesystem::path filename, const std::string content); - extern json::JObject* read_json(std::filesystem::path file); - extern std::vector read_list(std::filesystem::path file); + extern bool write_bytes(fs::path, const char* data, size_t size); + extern uint append_bytes(fs::path, const char* data, size_t size); + extern bool write_string(fs::path filename, const std::string content); + extern bool write_json(fs::path filename, const json::JObject* obj, bool nice=true); + extern bool write_binary_json(fs::path filename, const json::JObject* obj); + + extern bool read(fs::path, char* data, size_t size); + extern char* read_bytes(fs::path, size_t& length); + extern std::string read_string(fs::path filename); + extern json::JObject* read_json(fs::path file); + extern json::JObject* read_binary_json(fs::path file); + extern std::vector read_list(fs::path file); } #endif /* FILES_FILES_H_ */ \ No newline at end of file diff --git a/src/logic/scripting/api_lua.cpp b/src/logic/scripting/api_lua.cpp index a73c966e..26ade8a6 100644 --- a/src/logic/scripting/api_lua.cpp +++ b/src/logic/scripting/api_lua.cpp @@ -14,6 +14,13 @@ #include "../../lighting/Lighting.h" #include "../../logic/BlocksController.h" +inline int lua_pushivec3(lua_State* L, int x, int y, int z) { + lua_pushinteger(L, x); + lua_pushinteger(L, y); + lua_pushinteger(L, z); + return 3; +} + inline void luaL_openlib(lua_State* L, const char* name, const luaL_Reg* libfuncs, int nup) { lua_newtable(L); luaL_setfuncs(L, libfuncs, nup); @@ -44,11 +51,6 @@ static const luaL_Reg worldlib [] = { {NULL, NULL} }; -int luaopen_world(lua_State* L) { - luaL_openlib(L, "world", worldlib, 0); - return 1; -} - /* == player library ==*/ static int l_player_get_pos(lua_State* L) { int playerid = lua_tointeger(L, 1); @@ -102,11 +104,6 @@ static const luaL_Reg playerlib [] = { {NULL, NULL} }; -int luaopen_player(lua_State* L) { - luaL_openlib(L, "player", playerlib, 0); - return 1; -} - /* == blocks-related functions == */ static int l_block_name(lua_State* L) { int id = lua_tointeger(L, 1); @@ -158,13 +155,6 @@ static int l_get_block(lua_State* L) { return 1; } -inline int lua_pushivec3(lua_State* L, int x, int y, int z) { - lua_pushinteger(L, x); - lua_pushinteger(L, y); - lua_pushinteger(L, z); - return 3; -} - static int l_get_block_x(lua_State* L) { int x = lua_tointeger(L, 1); int y = lua_tointeger(L, 2); @@ -275,8 +265,8 @@ static int l_is_replaceable_at(lua_State* L) { lua_setglobal(L, NAME)) void apilua::create_funcs(lua_State* L) { - luaopen_world(L); - luaopen_player(L); + luaL_openlib(L, "world", worldlib, 0); + luaL_openlib(L, "player", playerlib, 0); lua_addfunc(L, l_block_index, "block_index"); lua_addfunc(L, l_block_name, "block_name"); From b0165b101437a51ca1476c131171bec09490d6af Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 17 Jan 2024 00:52:55 +0300 Subject: [PATCH 2/6] gzip compress/decompress + refactor --- src/coders/gzip.cpp | 56 +++++++++++++++++++++++++++++ src/coders/gzip.h | 12 +++++++ src/frontend/screens.cpp | 3 +- src/voxels/Chunk.cpp | 1 - src/voxels/Chunk.h | 20 ++++------- src/voxels/Chunks.cpp | 69 ++++++++++++++---------------------- src/voxels/Chunks.h | 4 +-- src/voxels/ChunksStorage.cpp | 9 ++--- src/world/Level.cpp | 4 +++ src/world/Level.h | 6 +++- 10 files changed, 119 insertions(+), 65 deletions(-) create mode 100644 src/coders/gzip.cpp create mode 100644 src/coders/gzip.h diff --git a/src/coders/gzip.cpp b/src/coders/gzip.cpp new file mode 100644 index 00000000..b30140c8 --- /dev/null +++ b/src/coders/gzip.cpp @@ -0,0 +1,56 @@ +#include "gzip.h" + +#define ZLIB_CONST +#include +#include +#include +#include "byte_utils.h" + +std::vector gzip::compress(const ubyte* src, size_t size) { + size_t buffer_size = 23+size*1.01; + std::vector buffer; + buffer.resize(buffer_size); + + // zlib struct + z_stream defstream {}; + defstream.zalloc = Z_NULL; + defstream.zfree = Z_NULL; + defstream.opaque = Z_NULL; + defstream.avail_in = size; + defstream.next_in = src; + defstream.avail_out = buffer_size; + defstream.next_out = buffer.data(); + + // compression + deflateInit2(&defstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, + 16 + MAX_WBITS, 8, Z_DEFAULT_STRATEGY); + deflate(&defstream, Z_FINISH); + deflateEnd(&defstream); + + size_t compressed_size = defstream.next_out - buffer.data(); + buffer.resize(compressed_size); + return buffer; +} + +std::vector gzip::decompress(const ubyte* src, size_t size) { + // getting uncompressed data length from gzip footer + size_t decompressed_size = *(uint32_t*)(src+size-4); + std::vector buffer; + buffer.resize(decompressed_size); + + // zlib struct + z_stream infstream; + infstream.zalloc = Z_NULL; + infstream.zfree = Z_NULL; + infstream.opaque = Z_NULL; + infstream.avail_in = size; + infstream.next_in = src; + infstream.avail_out = decompressed_size; + infstream.next_out = buffer.data(); + + inflateInit2(&infstream, 16+MAX_WBITS); + inflate(&infstream, Z_NO_FLUSH); + inflateEnd(&infstream); + + return buffer; +} diff --git a/src/coders/gzip.h b/src/coders/gzip.h new file mode 100644 index 00000000..2bf6cba7 --- /dev/null +++ b/src/coders/gzip.h @@ -0,0 +1,12 @@ +#ifndef CODERS_GZIP_H_ +#define CODERS_GZIP_H_ + +#include +#include "../typedefs.h" + +namespace gzip { + std::vector compress(const ubyte* src, size_t size); + std::vector decompress(const ubyte* src, size_t size); +} + +#endif // CODERS_GZIP_H_ diff --git a/src/frontend/screens.cpp b/src/frontend/screens.cpp index a1271784..2a93f9e7 100644 --- a/src/frontend/screens.cpp +++ b/src/frontend/screens.cpp @@ -96,9 +96,8 @@ LevelScreen::LevelScreen(Engine* engine, Level* level) LevelScreen::~LevelScreen() { std::cout << "-- writing world" << std::endl; - World* world = level->world; + auto world = level->getWorld(); world->write(level.get()); - delete world; } void LevelScreen::updateHotkeys() { diff --git a/src/voxels/Chunk.cpp b/src/voxels/Chunk.cpp index 40777de7..aba45f03 100644 --- a/src/voxels/Chunk.cpp +++ b/src/voxels/Chunk.cpp @@ -15,7 +15,6 @@ Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos){ voxels[i].states = 0; } lightmap = new Lightmap(); - renderData.vertices = nullptr; } Chunk::~Chunk(){ diff --git a/src/voxels/Chunk.h b/src/voxels/Chunk.h index c1f25184..0803c8db 100644 --- a/src/voxels/Chunk.h +++ b/src/voxels/Chunk.h @@ -18,11 +18,6 @@ struct voxel; class Lightmap; class ContentLUT; -struct RenderData { - float* vertices; - size_t size; -}; - class Chunk { public: int x, z; @@ -31,7 +26,6 @@ public: Lightmap* lightmap; int flags = 0; int surrounding = 0; - RenderData renderData; Chunk(int x, int z); ~Chunk(); @@ -44,7 +38,7 @@ public: // flags getters/setters below - void SETFLAGS(int mask, bool value){ + inline void setFlags(int mask, bool value){ if (value) flags |= mask; else @@ -63,17 +57,17 @@ public: inline bool isReady() const {return flags & ChunkFlag::READY;} - inline void setUnsaved(bool newState) {SETFLAGS(ChunkFlag::UNSAVED, newState);} + inline void setUnsaved(bool newState) {setFlags(ChunkFlag::UNSAVED, newState);} - inline void setModified(bool newState) {SETFLAGS(ChunkFlag::MODIFIED, newState);} + inline void setModified(bool newState) {setFlags(ChunkFlag::MODIFIED, newState);} - inline void setLoaded(bool newState) {SETFLAGS(ChunkFlag::LOADED, newState);} + inline void setLoaded(bool newState) {setFlags(ChunkFlag::LOADED, newState);} - inline void setLoadedLights(bool newState) {SETFLAGS(ChunkFlag::LOADED_LIGHTS, newState);} + inline void setLoadedLights(bool newState) {setFlags(ChunkFlag::LOADED_LIGHTS, newState);} - inline void setLighted(bool newState) {SETFLAGS(ChunkFlag::LIGHTED, newState);} + inline void setLighted(bool newState) {setFlags(ChunkFlag::LIGHTED, newState);} - inline void setReady(bool newState) {SETFLAGS(ChunkFlag::READY, newState);} + inline void setReady(bool newState) {setFlags(ChunkFlag::READY, newState);} ubyte* encode() const; bool decode(ubyte* data); diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index ff11ae2b..d774583b 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -16,10 +16,6 @@ #include #include -using glm::vec3; -using glm::ivec3; -using std::shared_ptr; - Chunks::Chunks(int w, int d, int ox, int oz, WorldFiles* wfile, @@ -27,25 +23,16 @@ Chunks::Chunks(int w, int d, const Content* content) : content(content), contentIds(content->indices), + chunks(w*d), + chunksSecond(w*d), w(w), d(d), ox(ox), oz(oz), worldFiles(wfile), events(events) { volume = (size_t)w*(size_t)d; - chunks = new shared_ptr[volume]; - chunksSecond = new shared_ptr[volume]; - - for (size_t i = 0; i < volume; i++){ - chunks[i] = nullptr; - } chunksCount = 0; } Chunks::~Chunks(){ - for (size_t i = 0; i < volume; i++){ - chunks[i] = nullptr; - } - delete[] chunks; - delete[] chunksSecond; } voxel* Chunks::get(int x, int y, int z){ @@ -56,7 +43,7 @@ voxel* Chunks::get(int x, int y, int z){ int cz = floordiv(z, CHUNK_D); if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d) return nullptr; - shared_ptr chunk = chunks[cz * w + cx]; // chunks is 2D-array + std::shared_ptr chunk = chunks[cz * w + cx]; if (chunk == nullptr) return nullptr; int lx = x - cx * CHUNK_W; @@ -117,7 +104,7 @@ ubyte Chunks::getLight(int x, int y, int z, int channel){ int cz = floordiv(z, CHUNK_D); if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d) return 0; - shared_ptr chunk = chunks[(cy * d + cz) * w + cx]; + auto chunk = chunks[(cy * d + cz) * w + cx]; if (chunk == nullptr) return 0; int lx = x - cx * CHUNK_W; @@ -134,7 +121,7 @@ light_t Chunks::getLight(int x, int y, int z){ int cz = floordiv(z, CHUNK_D); if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d) return 0; - shared_ptr chunk = chunks[(cy * d + cz) * w + cx]; + auto chunk = chunks[(cy * d + cz) * w + cx]; if (chunk == nullptr) return 0; int lx = x - cx * CHUNK_W; @@ -197,12 +184,12 @@ void Chunks::set(int x, int y, int z, int id, uint8_t states){ chunk->setModified(true); } -voxel* Chunks::rayCast(vec3 start, - vec3 dir, +voxel* Chunks::rayCast(glm::vec3 start, + glm::vec3 dir, float maxDist, - vec3& end, - ivec3& norm, - ivec3& iend) { + glm::vec3& end, + glm::ivec3& norm, + glm::ivec3& iend) { float px = start.x; float py = start.y; float pz = start.z; @@ -256,7 +243,7 @@ voxel* Chunks::rayCast(vec3 start, scalar_t distance; Ray ray(start, dir); if (ray.intersectAABB(iend, box, maxDist, norm, distance) > RayRelation::None){ - end = start + (dir * vec3(distance)); + end = start + (dir * glm::vec3(distance)); return voxel; } @@ -309,7 +296,7 @@ voxel* Chunks::rayCast(vec3 start, return nullptr; } -vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) { +glm::vec3 Chunks::rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDist) { float px = start.x; float py = start.y; float pz = start.z; @@ -343,7 +330,7 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) { while (t <= maxDist) { voxel* voxel = get(ix, iy, iz); - if (voxel == nullptr) { return vec3(px + t * dx, py + t * dy, pz + t * dz); } + if (voxel == nullptr) { return glm::vec3(px + t * dx, py + t * dy, pz + t * dz); } const Block* def = contentIds->getBlockDef(voxel->id); if (def->obstacle) { @@ -352,15 +339,15 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) { ? def->rt.hitboxes[voxel->rotation()] : def->hitbox; scalar_t distance; - ivec3 norm; + glm::ivec3 norm; Ray ray(start, dir); // norm is dummy now, can be inefficient - if (ray.intersectAABB(ivec3(ix, iy, iz), box, maxDist, norm, distance) > RayRelation::None) { - return start + (dir * vec3(distance)); + if (ray.intersectAABB(glm::ivec3(ix, iy, iz), box, maxDist, norm, distance) > RayRelation::None) { + return start + (dir * glm::vec3(distance)); } } else { - return vec3(px + t * dx, py + t * dy, pz + t * dz); + return glm::vec3(px + t * dx, py + t * dy, pz + t * dz); } } if (txMax < tyMax) { @@ -388,7 +375,7 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) { } } } - return vec3(px + maxDist * dx, py + maxDist * dy, pz + maxDist * dz); + return glm::vec3(px + maxDist * dx, py + maxDist * dy, pz + maxDist * dz); } @@ -408,7 +395,7 @@ void Chunks::translate(int dx, int dz){ } for (int z = 0; z < d; z++){ for (int x = 0; x < w; x++){ - shared_ptr chunk = chunks[z * w + x]; + auto chunk = chunks[z * w + x]; int nx = x - dx; int nz = z - dz; if (chunk == nullptr) @@ -443,20 +430,18 @@ void Chunks::resize(int newW, int newD) { translate(0, delta); } const int newVolume = newW * newD; - auto newChunks = new shared_ptr[newVolume] {}; - auto newChunksSecond = new shared_ptr[newVolume] {}; + std::vector> newChunks(newVolume); + std::vector> newChunksSecond(newVolume); for (int z = 0; z < d && z < newD; z++) { for (int x = 0; x < w && x < newW; x++) { newChunks[z * newW + x] = chunks[z * w + x]; } } - delete[] chunks; - delete[] chunksSecond; - w = newW; - d = newD; - volume = newVolume; - chunks = newChunks; - chunksSecond = newChunksSecond; + w = newW; + d = newD; + volume = newVolume; + chunks = std::move(newChunks); + chunksSecond = std::move(newChunksSecond); } void Chunks::_setOffset(int x, int z){ @@ -464,7 +449,7 @@ void Chunks::_setOffset(int x, int z){ oz = z; } -bool Chunks::putChunk(shared_ptr chunk) { +bool Chunks::putChunk(std::shared_ptr chunk) { int x = chunk->x; int z = chunk->z; x -= ox; diff --git a/src/voxels/Chunks.h b/src/voxels/Chunks.h index f76a9e7c..0e6e1f1a 100644 --- a/src/voxels/Chunks.h +++ b/src/voxels/Chunks.h @@ -21,8 +21,8 @@ class Chunks { const Content* const content; const ContentIndices* const contentIds; public: - std::shared_ptr* chunks; - std::shared_ptr* chunksSecond; + std::vector> chunks; + std::vector> chunksSecond; size_t volume; size_t chunksCount; size_t visible; diff --git a/src/voxels/ChunksStorage.cpp b/src/voxels/ChunksStorage.cpp index 57ba0400..18c4988c 100644 --- a/src/voxels/ChunksStorage.cpp +++ b/src/voxels/ChunksStorage.cpp @@ -39,7 +39,7 @@ void ChunksStorage::remove(int x, int z) { } } -void verifyLoadedChunk(ContentIndices* indices, Chunk* chunk) { +static void verifyLoadedChunk(ContentIndices* indices, Chunk* chunk) { for (size_t i = 0; i < CHUNK_VOL; i++) { blockid_t id = chunk->voxels[i].id; if (indices->getBlockDef(id) == nullptr) { @@ -52,18 +52,19 @@ void verifyLoadedChunk(ContentIndices* indices, Chunk* chunk) { } std::shared_ptr ChunksStorage::create(int x, int z) { - World* world = level->world; + World* world = level->getWorld(); + WorldFiles* wfile = world->wfile; auto chunk = std::make_shared(x, z); store(chunk); - std::unique_ptr data(world->wfile->getChunk(chunk->x, chunk->z)); + std::unique_ptr data(wfile->getChunk(chunk->x, chunk->z)); if (data) { chunk->decode(data.get()); chunk->setLoaded(true); verifyLoadedChunk(level->content->indices, chunk.get()); } - light_t* lights = world->wfile->getLights(chunk->x, chunk->z); + light_t* lights = wfile->getLights(chunk->x, chunk->z); if (lights) { chunk->lightmap->set(lights); chunk->setLoadedLights(true); diff --git a/src/world/Level.cpp b/src/world/Level.cpp index 702163a9..f49d4ad6 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -49,3 +49,7 @@ void Level::update() { chunks->resize(matrixSize, matrixSize); } } + +World* Level::getWorld() { + return world.get(); +} diff --git a/src/world/Level.h b/src/world/Level.h index 434dc179..0e2df5cc 100644 --- a/src/world/Level.h +++ b/src/world/Level.h @@ -1,6 +1,8 @@ #ifndef WORLD_LEVEL_H_ #define WORLD_LEVEL_H_ +#include + #include "../typedefs.h" #include "../settings.h" @@ -15,7 +17,7 @@ class ChunksStorage; class Level { public: - World* world; + std::unique_ptr world; const Content* const content; Player* player; Chunks* chunks; @@ -34,6 +36,8 @@ public: ~Level(); void update(); + + World* getWorld(); }; #endif /* WORLD_LEVEL_H_ */ From 9a518ecc5f45b69bed772031805dd44ceb899c25 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 17 Jan 2024 00:55:49 +0300 Subject: [PATCH 3/6] added missing #include --- src/voxels/Chunks.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/voxels/Chunks.h b/src/voxels/Chunks.h index 0e6e1f1a..06bfb122 100644 --- a/src/voxels/Chunks.h +++ b/src/voxels/Chunks.h @@ -2,6 +2,7 @@ #define VOXELS_CHUNKS_H_ #include +#include #include #include #include "../typedefs.h" From 34d4500e24fe6c1711387863f25933d9009e6677 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 17 Jan 2024 01:24:48 +0300 Subject: [PATCH 4/6] binary_json_spec.md update (cdocument explanation) --- src/coders/binary_json_spec.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/coders/binary_json_spec.md b/src/coders/binary_json_spec.md index 4e694247..e7b6f450 100644 --- a/src/coders/binary_json_spec.md +++ b/src/coders/binary_json_spec.md @@ -36,7 +36,8 @@ value = %x01 document / %x0A boolean 'false' / %x0B boolean 'true' / %x0C null value -cdocument = %x1F %x8B (16*byte) gzip-compressed document +cdocument = %x1F %x8B (16*byte) gzip-compressed data: + %x01 document cstring = (*%x01-FF) %x00 string = uint32 (*byte) uint32 stores number of the encoded string bytes From 377c8e5029aad70310d1b9fd6f46db2e44b4a6eb Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 17 Jan 2024 16:32:53 +0300 Subject: [PATCH 5/6] the big refactor + extracted data classes from coders/json to data/dynamic --- src/coders/binary_json.cpp | 159 +++++++++ src/coders/binary_json.h | 28 ++ src/coders/json.cpp | 564 ++----------------------------- src/coders/json.h | 126 ++----- src/content/Content.cpp | 1 - src/content/Content.h | 6 +- src/content/ContentLUT.cpp | 31 +- src/content/ContentLUT.h | 4 +- src/content/ContentLoader.cpp | 80 +++-- src/content/ContentLoader.h | 14 +- src/content/ContentPack.cpp | 13 +- src/data/dynamic.cpp | 331 ++++++++++++++++++ src/data/dynamic.h | 113 +++++++ src/files/WorldFiles.cpp | 34 +- src/files/WorldFiles.h | 47 ++- src/files/files.cpp | 11 +- src/files/files.h | 13 +- src/files/settings_io.cpp | 19 +- src/frontend/ContentGfxCache.cpp | 8 +- src/frontend/InventoryView.cpp | 2 +- src/frontend/WorldRenderer.cpp | 6 +- src/frontend/hud.cpp | 4 +- src/frontend/locale/langs.cpp | 90 +++-- src/graphics/BlocksRenderer.cpp | 6 +- src/lighting/Lighting.cpp | 18 +- src/logic/BlocksController.cpp | 4 +- src/logic/PlayerController.cpp | 12 +- src/logic/scripting/api_lua.cpp | 11 +- src/objects/Player.cpp | 3 - src/objects/Player.h | 2 +- src/typedefs.h | 2 + src/voxels/Chunk.h | 3 +- src/voxels/Chunks.cpp | 5 +- src/voxels/Chunks.h | 2 +- src/voxels/ChunksStorage.cpp | 9 +- src/voxels/ChunksStorage.h | 2 +- 36 files changed, 908 insertions(+), 875 deletions(-) create mode 100644 src/coders/binary_json.cpp create mode 100644 src/coders/binary_json.h create mode 100644 src/data/dynamic.cpp create mode 100644 src/data/dynamic.h diff --git a/src/coders/binary_json.cpp b/src/coders/binary_json.cpp new file mode 100644 index 00000000..c65012d3 --- /dev/null +++ b/src/coders/binary_json.cpp @@ -0,0 +1,159 @@ +#include "binary_json.h" + +#include + +#include "byte_utils.h" + +using namespace json; +using namespace dynamic; + +static void to_binary(ByteBuilder& builder, const Value* value) { + switch (value->type) { + case valtype::map: { + std::vector bytes = to_binary(value->value.map); + builder.put(bytes.data(), bytes.size()); + break; + } + case valtype::list: + builder.put(BJSON_TYPE_LIST); + for (auto& element : value->value.list->values) { + to_binary(builder, element.get()); + } + builder.put(BJSON_END); + break; + case valtype::integer: { + int64_t val = value->value.integer; + if (val >= 0 && val <= 255) { + builder.put(BJSON_TYPE_BYTE); + builder.put(val); + } else if (val >= INT16_MIN && val <= INT16_MAX){ + builder.put(BJSON_TYPE_INT16); + builder.putInt16(val); + } else if (val >= INT32_MIN && val <= INT32_MAX) { + builder.put(BJSON_TYPE_INT32); + builder.putInt32(val); + } else { + builder.put(BJSON_TYPE_INT64); + builder.putInt64(val); + } + break; + } + case valtype::number: + builder.put(BJSON_TYPE_NUMBER); + builder.putFloat64(value->value.decimal); + break; + case valtype::boolean: + builder.put(BJSON_TYPE_FALSE + value->value.boolean); + break; + case valtype::string: + builder.put(BJSON_TYPE_STRING); + builder.put(*value->value.str); + break; + } +} + +static List* array_from_binary(ByteReader& reader); +static Map* object_from_binary(ByteReader& reader); + +std::vector json::to_binary(const Map* obj) { + ByteBuilder builder; + // type byte + builder.put(BJSON_TYPE_DOCUMENT); + // document size + builder.putInt32(0); + + // writing entries + for (auto& entry : obj->values) { + builder.putCStr(entry.first.c_str()); + to_binary(builder, entry.second.get()); + } + // terminating byte + builder.put(BJSON_END); + + // updating document size + builder.setInt32(1, builder.size()); + return builder.build(); +} + +static Value* value_from_binary(ByteReader& reader) { + ubyte typecode = reader.get(); + valtype type; + valvalue val; + switch (typecode) { + case BJSON_TYPE_DOCUMENT: + type = valtype::map; + reader.getInt32(); + val.map = object_from_binary(reader); + break; + case BJSON_TYPE_LIST: + type = valtype::list; + val.list = array_from_binary(reader); + break; + case BJSON_TYPE_BYTE: + type = valtype::integer; + val.integer = reader.get(); + break; + case BJSON_TYPE_INT16: + type = valtype::integer; + val.integer = reader.getInt16(); + break; + case BJSON_TYPE_INT32: + type = valtype::integer; + val.integer = reader.getInt32(); + break; + case BJSON_TYPE_INT64: + type = valtype::integer; + val.integer = reader.getInt64(); + break; + case BJSON_TYPE_NUMBER: + type = valtype::number; + val.decimal = reader.getFloat64(); + break; + case BJSON_TYPE_FALSE: + case BJSON_TYPE_TRUE: + type = valtype::boolean; + val.boolean = typecode - BJSON_TYPE_FALSE; + break; + case BJSON_TYPE_STRING: + type = valtype::string; + val.str = new std::string(reader.getString()); + break; + default: + throw std::runtime_error( + "type "+std::to_string(typecode)+" is not supported"); + } + return new Value(type, val); +} + +static List* array_from_binary(ByteReader& reader) { + auto array = std::make_unique(); + auto& items = array->values; + while (reader.peek() != BJSON_END) { + items.push_back(std::unique_ptr(value_from_binary(reader))); + } + reader.get(); + return array.release(); +} + +static Map* object_from_binary(ByteReader& reader) { + auto obj = std::make_unique(); + auto& map = obj->values; + while (reader.peek() != BJSON_END) { + const char* key = reader.getCString(); + Value* value = value_from_binary(reader); + map.insert(std::make_pair(key, value)); + } + reader.get(); + return obj.release(); +} + +std::unique_ptr json::from_binary(const ubyte* src, size_t size) { + ByteReader reader(src, size); + std::unique_ptr value (value_from_binary(reader)); + if (value->type != valtype::map) { + throw std::runtime_error("root value is not an object"); + } + std::unique_ptr obj (value->value.map); + value->value.map = nullptr; + return obj; +} diff --git a/src/coders/binary_json.h b/src/coders/binary_json.h new file mode 100644 index 00000000..018f6573 --- /dev/null +++ b/src/coders/binary_json.h @@ -0,0 +1,28 @@ +#ifndef CODERS_BINARY_JSON_H_ +#define CODERS_BINARY_JSON_H_ + +#include +#include +#include "../data/dynamic.h" + +namespace json { + const int BJSON_END = 0x0; + const int BJSON_TYPE_DOCUMENT = 0x1; + const int BJSON_TYPE_LIST = 0x2; + const int BJSON_TYPE_BYTE = 0x3; + const int BJSON_TYPE_INT16 = 0x4; + const int BJSON_TYPE_INT32 = 0x5; + const int BJSON_TYPE_INT64 = 0x6; + const int BJSON_TYPE_NUMBER = 0x7; + const int BJSON_TYPE_STRING = 0x8; + const int BJSON_TYPE_BYTES = 0x9; + const int BJSON_TYPE_FALSE = 0xA; + const int BJSON_TYPE_TRUE = 0xB; + const int BJSON_TYPE_NULL = 0xC; + const int BJSON_TYPE_CDOCUMENT = 0x1F; + + extern std::vector to_binary(const dynamic::Map* obj); + extern std::unique_ptr from_binary(const ubyte* src, size_t size); +} + +#endif // CODERS_BINARY_JSON_H_ diff --git a/src/coders/json.cpp b/src/coders/json.cpp index 64e38383..31440b7d 100644 --- a/src/coders/json.cpp +++ b/src/coders/json.cpp @@ -6,24 +6,10 @@ #include #include "commons.h" -#include "byte_utils.h" +#include "../data/dynamic.h" using namespace json; - -const int BJSON_END = 0x0; -const int BJSON_TYPE_DOCUMENT = 0x1; -const int BJSON_TYPE_LIST = 0x2; -const int BJSON_TYPE_BYTE = 0x3; -const int BJSON_TYPE_INT16 = 0x4; -const int BJSON_TYPE_INT32 = 0x5; -const int BJSON_TYPE_INT64 = 0x6; -const int BJSON_TYPE_NUMBER = 0x7; -const int BJSON_TYPE_STRING = 0x8; -const int BJSON_TYPE_BYTES = 0x9; -const int BJSON_TYPE_FALSE = 0xA; -const int BJSON_TYPE_TRUE = 0xB; -const int BJSON_TYPE_NULL = 0xC; -const int BJSON_TYPE_CDOCUMENT = 0x1F; +using namespace dynamic; inline void newline(std::stringstream& ss, bool nice, uint indent, @@ -44,7 +30,7 @@ void stringify(const Value* value, const std::string& indentstr, bool nice); -void stringifyObj(const JObject* obj, +void stringifyObj(const Map* obj, std::stringstream& ss, int indent, const std::string& indentstr, @@ -55,23 +41,23 @@ void stringify(const Value* value, int indent, const std::string& indentstr, bool nice) { - if (value->type == valtype::object) { - stringifyObj(value->value.obj, ss, indent, indentstr, nice); + if (value->type == valtype::map) { + stringifyObj(value->value.map, ss, indent, indentstr, nice); } - else if (value->type == valtype::array) { - std::vector& list = value->value.arr->values; - if (list.empty()) { + else if (value->type == valtype::list) { + auto list = value->value.list; + if (list->size() == 0) { ss << "[]"; return; } ss << '['; - for (uint i = 0; i < list.size(); i++) { - Value* value = list[i]; + for (uint i = 0; i < list->size(); i++) { + Value* value = list->get(i); if (i > 0 || nice) { newline(ss, nice, indent, indentstr); } stringify(value, ss, indent+1, indentstr, nice); - if (i + 1 < list.size()) { + if (i + 1 < list->size()) { ss << ','; } } @@ -91,27 +77,27 @@ void stringify(const Value* value, } } -void stringifyObj(const JObject* obj, +void stringifyObj(const Map* obj, std::stringstream& ss, int indent, const std::string& indentstr, bool nice) { - if (obj->map.empty()) { + if (obj->values.empty()) { ss << "{}"; return; } ss << "{"; uint index = 0; - for (auto entry : obj->map) { + for (auto& entry : obj->values) { const std::string& key = entry.first; if (index > 0 || nice) { newline(ss, nice, indent, indentstr); } - Value* value = entry.second; + Value* value = entry.second.get(); ss << escape_string(key) << ": "; stringify(value, ss, indent+1, indentstr, nice); index++; - if (index < obj->map.size()) { + if (index < obj->values.size()) { ss << ','; } } @@ -122,7 +108,7 @@ void stringifyObj(const JObject* obj, } std::string json::stringify( - const JObject* obj, + const Map* obj, bool nice, const std::string& indent) { std::stringstream ss; @@ -130,497 +116,11 @@ std::string json::stringify( return ss.str(); } -static void to_binary(ByteBuilder& builder, const Value* value) { - switch (value->type) { - case valtype::object: { - std::vector bytes = to_binary(value->value.obj); - builder.put(bytes.data(), bytes.size()); - break; - } - case valtype::array: - builder.put(BJSON_TYPE_LIST); - for (Value* element : value->value.arr->values) { - to_binary(builder, element); - } - builder.put(BJSON_END); - break; - case valtype::integer: { - int64_t val = value->value.integer; - if (val >= 0 && val <= 255) { - builder.put(BJSON_TYPE_BYTE); - builder.put(val); - } else if (val >= INT16_MIN && val <= INT16_MAX){ - builder.put(BJSON_TYPE_INT16); - builder.putInt16(val); - } else if (val >= INT32_MIN && val <= INT32_MAX) { - builder.put(BJSON_TYPE_INT32); - builder.putInt32(val); - } else { - builder.put(BJSON_TYPE_INT64); - builder.putInt64(val); - } - break; - } - case valtype::number: - builder.put(BJSON_TYPE_NUMBER); - builder.putFloat64(value->value.decimal); - break; - case valtype::boolean: - builder.put(BJSON_TYPE_FALSE + value->value.boolean); - break; - case valtype::string: - builder.put(BJSON_TYPE_STRING); - builder.put(*value->value.str); - break; - } -} - -static JArray* array_from_binary(ByteReader& reader); -static JObject* object_from_binary(ByteReader& reader); - -std::vector json::to_binary(const JObject* obj) { - ByteBuilder builder; - // type byte - builder.put(BJSON_TYPE_DOCUMENT); - // document size - builder.putInt32(0); - - // writing entries - for (auto& entry : obj->map) { - builder.putCStr(entry.first.c_str()); - to_binary(builder, entry.second); - } - // terminating byte - builder.put(BJSON_END); - - // updating document size - builder.setInt32(1, builder.size()); - return builder.build(); -} - -static Value* value_from_binary(ByteReader& reader) { - ubyte typecode = reader.get(); - valtype type; - valvalue val; - switch (typecode) { - case BJSON_TYPE_DOCUMENT: - type = valtype::object; - reader.getInt32(); - val.obj = object_from_binary(reader); - break; - case BJSON_TYPE_LIST: - type = valtype::array; - val.arr = array_from_binary(reader); - break; - case BJSON_TYPE_BYTE: - type = valtype::integer; - val.integer = reader.get(); - break; - case BJSON_TYPE_INT16: - type = valtype::integer; - val.integer = reader.getInt16(); - break; - case BJSON_TYPE_INT32: - type = valtype::integer; - val.integer = reader.getInt32(); - break; - case BJSON_TYPE_INT64: - type = valtype::integer; - val.integer = reader.getInt64(); - break; - case BJSON_TYPE_NUMBER: - type = valtype::number; - val.decimal = reader.getFloat64(); - break; - case BJSON_TYPE_FALSE: - case BJSON_TYPE_TRUE: - type = valtype::boolean; - val.boolean = typecode - BJSON_TYPE_FALSE; - break; - case BJSON_TYPE_STRING: - type = valtype::string; - val.str = new std::string(reader.getString()); - break; - default: - throw std::runtime_error( - "type "+std::to_string(typecode)+" is not supported"); - } - return new Value(type, val); -} - -static JArray* array_from_binary(ByteReader& reader) { - auto array = std::make_unique(); - auto& items = array->values; - while (reader.peek() != BJSON_END) { - items.push_back(value_from_binary(reader)); - } - reader.get(); - return array.release(); -} - -static JObject* object_from_binary(ByteReader& reader) { - auto obj = std::make_unique(); - auto& map = obj->map; - while (reader.peek() != BJSON_END) { - const char* key = reader.getCString(); - Value* value = value_from_binary(reader); - map.insert(std::make_pair(key, value)); - } - reader.get(); - return obj.release(); -} - -JObject* json::from_binary(const ubyte* src, size_t size) { - ByteReader reader(src, size); - std::unique_ptr value (value_from_binary(reader)); - if (value->type != valtype::object) { - throw std::runtime_error("root value is not an object"); - } - JObject* obj = value->value.obj; - value->value.obj = nullptr; - return obj; -} - -JArray::~JArray() { - for (auto value : values) { - delete value; - } -} - -std::string JArray::str(size_t index) const { - const auto& val = values[index]; - switch (val->type) { - case valtype::string: return *val->value.str; - case valtype::boolean: return val->value.boolean ? "true" : "false"; - case valtype::number: return std::to_string(val->value.decimal); - case valtype::integer: return std::to_string(val->value.integer); - default: - throw std::runtime_error("type error"); - } -} - -double JArray::num(size_t index) const { - const auto& val = values[index]; - switch (val->type) { - case valtype::number: return val->value.decimal; - case valtype::integer: return val->value.integer; - case valtype::string: return std::stoll(*val->value.str); - case valtype::boolean: return val->value.boolean; - default: - throw std::runtime_error("type error"); - } -} - -int64_t JArray::integer(size_t index) const { - const auto& val = values[index]; - switch (val->type) { - case valtype::number: return val->value.decimal; - case valtype::integer: return val->value.integer; - case valtype::string: return std::stoll(*val->value.str); - case valtype::boolean: return val->value.boolean; - default: - throw std::runtime_error("type error"); - } -} - -JObject* JArray::obj(size_t index) const { - return values[index]->value.obj; -} - -JArray* JArray::arr(size_t index) const { - return values[index]->value.arr; -} - -bool JArray::flag(size_t index) const { - return values[index]->value.boolean; -} - -JArray& JArray::put(std::string value) { - valvalue val; - val.str = new std::string(value); - values.push_back(new Value(valtype::string, val)); - return *this; -} - -JArray& JArray::put(uint value) { - return put((int64_t)value); -} - -JArray& JArray::put(int value) { - return put((int64_t)value); -} - -JArray& JArray::put(int64_t value) { - valvalue val; - val.integer = value; - values.push_back(new Value(valtype::integer, val)); - return *this; -} - -JArray& JArray::put(uint64_t value) { - return put((int64_t)value); -} - -JArray& JArray::put(double value) { - valvalue val; - val.decimal = value; - values.push_back(new Value(valtype::number, val)); - return *this; -} - -JArray& JArray::put(float value) { - return put((double)value); -} - -JArray& JArray::put(bool value) { - valvalue val; - val.boolean = value; - values.push_back(new Value(valtype::boolean, val)); - return *this; -} - -JArray& JArray::put(JObject* value) { - valvalue val; - val.obj = value; - values.push_back(new Value(valtype::object, val)); - return *this; -} - -JArray& JArray::put(JArray* value) { - valvalue val; - val.arr = value; - values.push_back(new Value(valtype::array, val)); - return *this; -} - -JArray& JArray::putArray() { - JArray* arr = new JArray(); - put(arr); - return *arr; -} - -JObject& JArray::putObj() { - JObject* obj = new JObject(); - put(obj); - return *obj; -} - -void JArray::remove(size_t index) { - values.erase(values.begin() + index); -} - -JObject::~JObject() { - for (auto entry : map) { - delete entry.second; - } -} - -void JObject::str(std::string key, std::string& dst) const { - dst = getStr(key, dst); -} - -std::string JObject::getStr(std::string key, const std::string& def) const { - auto found = map.find(key); - if (found == map.end()) - return def; - auto& val = found->second; - switch (val->type) { - case valtype::string: return *val->value.str; - case valtype::boolean: return val->value.boolean ? "true" : "false"; - case valtype::number: return std::to_string(val->value.decimal); - case valtype::integer: return std::to_string(val->value.integer); - default: throw std::runtime_error("type error"); - } -} - -double JObject::getNum(std::string key, double def) const { - auto found = map.find(key); - if (found == map.end()) - return def; - auto& val = found->second; - switch (val->type) { - case valtype::number: return val->value.decimal; - case valtype::integer: return val->value.integer; - case valtype::string: return std::stoull(*val->value.str); - case valtype::boolean: return val->value.boolean; - default: throw std::runtime_error("type error"); - } -} - -int64_t JObject::getInteger(std::string key, int64_t def) const { - auto found = map.find(key); - if (found == map.end()) - return def; - auto& val = found->second; - switch (val->type) { - case valtype::number: return val->value.decimal; - case valtype::integer: return val->value.integer; - case valtype::string: return std::stoull(*val->value.str); - case valtype::boolean: return val->value.boolean; - default: throw std::runtime_error("type error"); - } -} - -void JObject::num(std::string key, double& dst) const { - dst = getNum(key, dst); -} - -void JObject::num(std::string key, float& dst) const { - dst = getNum(key, dst); -} - -void JObject::num(std::string key, ubyte& dst) const { - dst = getInteger(key, dst); -} - -void JObject::num(std::string key, int& dst) const { - dst = getInteger(key, dst); -} - -void JObject::num(std::string key, int64_t& dst) const { - dst = getInteger(key, dst); -} - -void JObject::num(std::string key, uint64_t& dst) const { - dst = getInteger(key, dst); -} - -void JObject::num(std::string key, uint& dst) const { - dst = getInteger(key, dst); -} - -JObject* JObject::obj(std::string key) const { - auto found = map.find(key); - if (found != map.end()) { - auto& val = found->second; - if (val->type != valtype::object) - return nullptr; - return val->value.obj; - } - return nullptr; -} - -JArray* JObject::arr(std::string key) const { - auto found = map.find(key); - if (found != map.end()) - return found->second->value.arr; - return nullptr; -} - -void JObject::flag(std::string key, bool& dst) const { - auto found = map.find(key); - if (found != map.end()) - dst = found->second->value.boolean; -} - -JObject& JObject::put(std::string key, uint value) { - return put(key, (int64_t)value); -} - -JObject& JObject::put(std::string key, int value) { - return put(key, (int64_t)value); -} - -JObject& JObject::put(std::string key, int64_t value) { - auto found = map.find(key); - if (found != map.end()) found->second; - valvalue val; - val.integer = value; - map.insert(std::make_pair(key, new Value(valtype::integer, val))); - return *this; -} - -JObject& JObject::put(std::string key, uint64_t value) { - return put(key, (int64_t)value); -} - -JObject& JObject::put(std::string key, float value) { - return put(key, (double)value); -} - -JObject& JObject::put(std::string key, double value) { - auto found = map.find(key); - if (found != map.end()) delete found->second; - valvalue val; - val.decimal = value; - map.insert(std::make_pair(key, new Value(valtype::number, val))); - return *this; -} - -JObject& JObject::put(std::string key, std::string value){ - auto found = map.find(key); - if (found != map.end()) delete found->second; - valvalue val; - val.str = new std::string(value); - map.insert(std::make_pair(key, new Value(valtype::string, val))); - return *this; -} - -JObject& JObject::put(std::string key, const char* value) { - return put(key, std::string(value)); -} - -JObject& JObject::put(std::string key, JObject* value){ - auto found = map.find(key); - if (found != map.end()) delete found->second; - valvalue val; - val.obj = value; - map.insert(std::make_pair(key, new Value(valtype::object, val))); - return *this; -} - -JObject& JObject::put(std::string key, JArray* value){ - auto found = map.find(key); - if (found != map.end()) delete found->second; - valvalue val; - val.arr = value; - map.insert(std::make_pair(key, new Value(valtype::array, val))); - return *this; -} - -JObject& JObject::put(std::string key, bool value){ - auto found = map.find(key); - if (found != map.end()) delete found->second; - valvalue val; - val.boolean = value; - map.insert(std::make_pair(key, new Value(valtype::boolean, val))); - return *this; -} - -JArray& JObject::putArray(std::string key) { - JArray* arr = new JArray(); - put(key, arr); - return *arr; -} - -JObject& JObject::putObj(std::string key) { - JObject* obj = new JObject(); - put(key, obj); - return *obj; -} - -bool JObject::has(std::string key) { - return map.find(key) != map.end(); -} - -Value::Value(valtype type, valvalue value) : type(type), value(value) { -} - -Value::~Value() { - switch (type) { - case valtype::object: delete value.obj; break; - case valtype::array: delete value.arr; break; - case valtype::string: delete value.str; break; - default: - break; - } -} - Parser::Parser(std::string filename, std::string source) : BasicParser(filename, source) { } -JObject* Parser::parse() { +Map* Parser::parse() { char next = peek(); if (next != '{') { throw error("'{' expected"); @@ -628,10 +128,10 @@ JObject* Parser::parse() { return parseObject(); } -JObject* Parser::parseObject() { +Map* Parser::parseObject() { expect('{'); - auto obj = std::make_unique(); - auto& map = obj->map; + auto obj = std::make_unique(); + auto& map = obj->values; while (peek() != '}') { if (peek() == '#') { skipLine(); @@ -657,16 +157,16 @@ JObject* Parser::parseObject() { return obj.release(); } -JArray* Parser::parseArray() { +List* Parser::parseList() { expect('['); - auto arr = std::make_unique(); + auto arr = std::make_unique(); auto& values = arr->values; while (peek() != ']') { if (peek() == '#') { skipLine(); continue; } - values.push_back(parseValue()); + values.push_back(std::unique_ptr(parseValue())); char next = peek(); if (next == ',') { @@ -683,7 +183,7 @@ JArray* Parser::parseArray() { Value* Parser::parseValue() { char next = peek(); - valvalue val; + dynamic::valvalue val; if (next == '-' || next == '+') { pos++; number_u num; @@ -715,12 +215,12 @@ Value* Parser::parseValue() { throw error("invalid literal "); } if (next == '{') { - val.obj = parseObject(); - return new Value(valtype::object, val); + val.map = parseObject(); + return new Value(valtype::map, val); } if (next == '[') { - val.arr = parseArray(); - return new Value(valtype::array, val); + val.list = parseList(); + return new Value(valtype::list, val); } if (is_digit(next)) { number_u num; @@ -742,11 +242,11 @@ Value* Parser::parseValue() { throw error("unexpected character '"+std::string({next})+"'"); } -JObject* json::parse(std::string filename, std::string source) { +std::unique_ptr json::parse(std::string filename, std::string source) { Parser parser(filename, source); - return parser.parse(); + return std::unique_ptr(parser.parse()); } -JObject* json::parse(std::string source) { +std::unique_ptr json::parse(std::string source) { return parse("", source); } diff --git a/src/coders/json.h b/src/coders/json.h index c90e3314..3fc917da 100644 --- a/src/coders/json.h +++ b/src/coders/json.h @@ -8,122 +8,34 @@ #include #include "commons.h" +#include "../typedefs.h" -typedef unsigned int uint; +#include "binary_json.h" + +namespace dynamic { + class Map; + class List; + class Value; +} namespace json { - class JObject; - class JArray; - class Value; - - extern std::string stringify(const JObject* obj, bool nice, const std::string& indent); - extern std::vector to_binary(const JObject* obj); - extern JObject* from_binary(const ubyte* src, size_t size); - - enum class valtype { - object, array, number, integer, string, boolean - }; - - union valvalue { - JObject* obj; - JArray* arr; - std::string* str; - double decimal; - int64_t integer; - bool boolean; - }; - - class Value { - public: - valtype type; - valvalue value; - Value(valtype type, valvalue value); - ~Value(); - }; - - class JArray { - public: - std::vector values; - ~JArray(); - - std::string str(size_t index) const; - double num(size_t index) const; - int64_t integer(size_t num) const; - JObject* obj(size_t index) const; - JArray* arr(size_t index) const; - bool flag(size_t index) const; - - inline size_t size() const { - return values.size(); - } - - JArray& put(uint value); - JArray& put(int value); - JArray& put(uint64_t value); - JArray& put(int64_t value); - JArray& put(float value); - JArray& put(double value); - JArray& put(std::string value); - JArray& put(JObject* value); - JArray& put(JArray* value); - JArray& put(bool value); - - JArray& putArray(); - JObject& putObj(); - - void remove(size_t index); - }; - - class JObject { - public: - std::unordered_map map; - ~JObject(); - - std::string getStr(std::string key, const std::string& def) const; - double getNum(std::string key, double def) const; - int64_t getInteger(std::string key, int64_t def) const; - void str(std::string key, std::string& dst) const; - void num(std::string key, int& dst) const; - void num(std::string key, float& dst) const; - void num(std::string key, uint& dst) const; - void num(std::string key, int64_t& dst) const; - void num(std::string key, uint64_t& dst) const; - void num(std::string key, ubyte& dst) const; - void num(std::string key, double& dst) const; - JObject* obj(std::string key) const; - JArray* arr(std::string key) const; - void flag(std::string key, bool& dst) const; - - JObject& put(std::string key, uint value); - JObject& put(std::string key, int value); - JObject& put(std::string key, int64_t value); - JObject& put(std::string key, uint64_t value); - JObject& put(std::string key, float value); - JObject& put(std::string key, double value); - JObject& put(std::string key, const char* value); - JObject& put(std::string key, std::string value); - JObject& put(std::string key, JObject* value); - JObject& put(std::string key, JArray* value); - JObject& put(std::string key, bool value); - - JArray& putArray(std::string key); - JObject& putObj(std::string key); - - bool has(std::string key); - }; - class Parser : public BasicParser { - JArray* parseArray(); - JObject* parseObject(); - Value* parseValue(); + dynamic::List* parseList(); + dynamic::Map* parseObject(); + dynamic::Value* parseValue(); public: Parser(std::string filename, std::string source); - JObject* parse(); + dynamic::Map* parse(); }; - extern JObject* parse(std::string filename, std::string source); - extern JObject* parse(std::string source); + extern std::unique_ptr parse(std::string filename, std::string source); + extern std::unique_ptr parse(std::string source); + + extern std::string stringify( + const dynamic::Map* obj, + bool nice, + const std::string& indent); } #endif // CODERS_JSON_H_ \ No newline at end of file diff --git a/src/content/Content.cpp b/src/content/Content.cpp index d808e48b..aa98de9a 100644 --- a/src/content/Content.cpp +++ b/src/content/Content.cpp @@ -131,7 +131,6 @@ Content::Content(ContentIndices* indices, DrawGroups* drawGroups, } Content::~Content() { - delete indices; delete drawGroups; } diff --git a/src/content/Content.h b/src/content/Content.h index db9aae43..cd36a474 100644 --- a/src/content/Content.h +++ b/src/content/Content.h @@ -92,14 +92,18 @@ public: class Content { std::unordered_map blockDefs; std::unordered_map itemDefs; + std::unique_ptr indices; public: - ContentIndices* const indices; DrawGroups* const drawGroups; Content(ContentIndices* indices, DrawGroups* drawGroups, std::unordered_map blockDefs, std::unordered_map itemDefs); ~Content(); + + inline ContentIndices* getIndices() const { + return indices.get(); + } Block* findBlock(std::string id) const; Block* requireBlock(std::string id) const; diff --git a/src/content/ContentLUT.cpp b/src/content/ContentLUT.cpp index 7a0d6eb8..11e7b5e8 100644 --- a/src/content/ContentLUT.cpp +++ b/src/content/ContentLUT.cpp @@ -8,15 +8,10 @@ #include "../coders/json.h" #include "../voxels/Block.h" -using std::string; -using std::unique_ptr; -using std::make_unique; -using std::filesystem::path; - -#include +#include "../data/dynamic.h" ContentLUT::ContentLUT(size_t blocksCount, const Content* content) { - ContentIndices* indices = content->indices; + auto* indices = content->getIndices(); for (size_t i = 0; i < blocksCount; i++) { blocks.push_back(i); } @@ -24,25 +19,25 @@ ContentLUT::ContentLUT(size_t blocksCount, const Content* content) { blockNames.push_back(indices->getBlockDef(i)->name); } - for (size_t i = indices->countBlockDefs(); i < blocksCount; i++) { + for (size_t i = indices->countBlockDefs(); i < blocksCount; i++) { blockNames.push_back(""); } } -ContentLUT* ContentLUT::create(const path& filename, +ContentLUT* ContentLUT::create(const fs::path& filename, const Content* content) { - unique_ptr root(files::read_json(filename)); - json::JArray* blocksarr = root->arr("blocks"); + auto root = files::read_json(filename); + auto blocklist = root->list("blocks"); - auto& indices = content->indices; - size_t blocks_c = blocksarr - ? std::max(blocksarr->size(), indices->countBlockDefs()) + auto* indices = content->getIndices(); + size_t blocks_c = blocklist + ? std::max(blocklist->size(), indices->countBlockDefs()) : indices->countBlockDefs(); - auto lut = make_unique(blocks_c, content); - if (blocksarr) { - for (size_t i = 0; i < blocksarr->size(); i++) { - string name = blocksarr->str(i); + auto lut = std::make_unique(blocks_c, content); + if (blocklist) { + for (size_t i = 0; i < blocklist->size(); i++) { + std::string name = blocklist->str(i); Block* def = content->findBlock(name); if (def) { lut->setBlock(i, name, def->rt.id); diff --git a/src/content/ContentLUT.h b/src/content/ContentLUT.h index 4da4df78..458d4bfb 100644 --- a/src/content/ContentLUT.h +++ b/src/content/ContentLUT.h @@ -8,6 +8,8 @@ #include "../typedefs.h" #include "../constants.h" +namespace fs = std::filesystem; + class Content; /* Content indices lookup table or report @@ -40,7 +42,7 @@ public: } } - static ContentLUT* create(const std::filesystem::path& filename, + static ContentLUT* create(const fs::path& filename, const Content* content); inline bool hasContentReorder() const { diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index f0c803c6..d0cd7e90 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -13,6 +13,7 @@ #include "../files/files.h" #include "../coders/json.h" #include "../typedefs.h" +#include "../data/dynamic.h" #include "ContentPack.h" #include "../logic/scripting/scripting.h" @@ -23,9 +24,8 @@ ContentLoader::ContentLoader(ContentPack* pack) : pack(pack) { } bool ContentLoader::fixPackIndices(fs::path folder, - json::JObject* indicesRoot, + dynamic::Map* indicesRoot, std::string contentSection) { - std::vector detected; std::vector indexed; if (fs::is_directory(folder)) { @@ -42,9 +42,9 @@ bool ContentLoader::fixPackIndices(fs::path folder, bool modified = false; if (!indicesRoot->has(contentSection)) { - indicesRoot->putArray(contentSection); + indicesRoot->putList(contentSection); } - json::JArray* arr = indicesRoot->arr(contentSection); + auto arr = indicesRoot->list(contentSection); if (arr) { for (uint i = 0; i < arr->size(); i++) { std::string name = arr->str(i); @@ -72,11 +72,11 @@ void ContentLoader::fixPackIndices() { auto blocksFolder = folder/ContentPack::BLOCKS_FOLDER; auto itemsFolder = folder/ContentPack::ITEMS_FOLDER; - std::unique_ptr root; + std::unique_ptr root; if (fs::is_regular_file(indexFile)) { - root.reset(files::read_json(indexFile)); + root = std::move(files::read_json(indexFile)); } else { - root.reset(new json::JObject()); + root.reset(new dynamic::Map()); } bool modified = false; @@ -93,7 +93,7 @@ void ContentLoader::fixPackIndices() { // TODO: add basic validation and logging void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) { - std::unique_ptr root(files::read_json(file)); + auto root = files::read_json(file); // block texturing if (root->has("texture")) { @@ -103,7 +103,7 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) { def->textureFaces[i] = texture; } } else if (root->has("texture-faces")) { - json::JArray* texarr = root->arr("texture-faces"); + auto texarr = root->list("texture-faces"); for (uint i = 0; i < 6; i++) { def->textureFaces[i] = texarr->str(i); } @@ -117,7 +117,7 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) { else if (model == "custom") { def->model = BlockModel::custom; if (root->has("model-primitives")) { - loadCustomBlockModel(def, root->obj("model-primitives")); + loadCustomBlockModel(def, root->map("model-primitives")); } else { std::cerr << "ERROR occured while block " @@ -145,20 +145,20 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) { } // block hitbox AABB [x, y, z, width, height, depth] - json::JArray* boxobj = root->arr("hitbox"); - if (boxobj) { + auto boxarr = root->list("hitbox"); + if (boxarr) { AABB& aabb = def->hitbox; - aabb.a = glm::vec3(boxobj->num(0), boxobj->num(1), boxobj->num(2)); - aabb.b = glm::vec3(boxobj->num(3), boxobj->num(4), boxobj->num(5)); + aabb.a = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2)); + aabb.b = glm::vec3(boxarr->num(3), boxarr->num(4), boxarr->num(5)); aabb.b += aabb.a; } // block light emission [r, g, b] where r,g,b in range [0..15] - json::JArray* emissionobj = root->arr("emission"); - if (emissionobj) { - def->emission[0] = emissionobj->num(0); - def->emission[1] = emissionobj->num(1); - def->emission[2] = emissionobj->num(2); + auto emissionarr = root->list("emission"); + if (emissionarr) { + def->emission[0] = emissionarr->num(0); + def->emission[1] = emissionarr->num(1); + def->emission[2] = emissionarr->num(2); } // primitive properties @@ -171,29 +171,28 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) { root->flag("hidden", def->hidden); root->flag("sky-light-passing", def->skyLightPassing); root->num("draw-group", def->drawGroup); - root->str("picking-item", def->pickingItem); } -void ContentLoader::loadCustomBlockModel(Block* def, json::JObject* primitives) { +void ContentLoader::loadCustomBlockModel(Block* def, dynamic::Map* primitives) { if (primitives->has("aabbs")) { - json::JArray* modelboxes = primitives->arr("aabbs"); + auto modelboxes = primitives->list("aabbs"); for (uint i = 0; i < modelboxes->size(); i++ ) { /* Parse aabb */ - json::JArray* boxobj = modelboxes->arr(i); + auto boxarr = modelboxes->list(i); AABB modelbox; - modelbox.a = glm::vec3(boxobj->num(0), boxobj->num(1), boxobj->num(2)); - modelbox.b = glm::vec3(boxobj->num(3), boxobj->num(4), boxobj->num(5)); + modelbox.a = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2)); + modelbox.b = glm::vec3(boxarr->num(3), boxarr->num(4), boxarr->num(5)); modelbox.b += modelbox.a; def->modelBoxes.push_back(modelbox); - if (boxobj->size() == 7) + if (boxarr->size() == 7) for (uint i = 6; i < 12; i++) { - def->modelTextures.push_back(boxobj->str(6)); + def->modelTextures.push_back(boxarr->str(6)); } - else if (boxobj->size() == 12) + else if (boxarr->size() == 12) for (uint i = 6; i < 12; i++) { - def->modelTextures.push_back(boxobj->str(i)); + def->modelTextures.push_back(boxarr->str(i)); } else for (uint i = 6; i < 12; i++) { @@ -202,10 +201,10 @@ void ContentLoader::loadCustomBlockModel(Block* def, json::JObject* primitives) } } if (primitives->has("tetragons")) { - json::JArray* modeltetragons = primitives->arr("tetragons"); + auto modeltetragons = primitives->list("tetragons"); for (uint i = 0; i < modeltetragons->size(); i++) { /* Parse tetragon to points */ - json::JArray* tgonobj = modeltetragons->arr(i); + auto tgonobj = modeltetragons->list(i); glm::vec3 p1(tgonobj->num(0), tgonobj->num(1), tgonobj->num(2)), xw(tgonobj->num(3), tgonobj->num(4), tgonobj->num(5)), yh(tgonobj->num(6), tgonobj->num(7), tgonobj->num(8)); @@ -220,7 +219,7 @@ void ContentLoader::loadCustomBlockModel(Block* def, json::JObject* primitives) } void ContentLoader::loadItem(ItemDef* def, std::string name, fs::path file) { - std::unique_ptr root(files::read_json(file)); + auto root = files::read_json(file); std::string iconTypeStr = "none"; root->str("icon-type", iconTypeStr); if (iconTypeStr == "none") { @@ -236,11 +235,11 @@ void ContentLoader::loadItem(ItemDef* def, std::string name, fs::path file) { root->str("placing-block", def->placingBlock); // item light emission [r, g, b] where r,g,b in range [0..15] - json::JArray* emissionobj = root->arr("emission"); - if (emissionobj) { - def->emission[0] = emissionobj->num(0); - def->emission[1] = emissionobj->num(1); - def->emission[2] = emissionobj->num(2); + auto emissionarr = root->list("emission"); + if (emissionarr) { + def->emission[0] = emissionarr->num(0); + def->emission[1] = emissionarr->num(1); + def->emission[2] = emissionarr->num(2); } } @@ -274,9 +273,8 @@ void ContentLoader::load(ContentBuilder* builder) { auto folder = pack->folder; if (!fs::is_regular_file(pack->getContentFile())) return; - std::unique_ptr root (files::read_json(pack->getContentFile())); - - json::JArray* blocksarr = root->arr("blocks"); + auto root = files::read_json(pack->getContentFile()); + auto blocksarr = root->list("blocks"); if (blocksarr) { for (uint i = 0; i < blocksarr->size(); i++) { std::string name = blocksarr->str(i); @@ -297,7 +295,7 @@ void ContentLoader::load(ContentBuilder* builder) { } } - json::JArray* itemsarr = root->arr("items"); + auto itemsarr = root->list("items"); if (itemsarr) { for (uint i = 0; i < itemsarr->size(); i++) { std::string name = itemsarr->str(i); diff --git a/src/content/ContentLoader.h b/src/content/ContentLoader.h index acdd4e2f..e8517d53 100644 --- a/src/content/ContentLoader.h +++ b/src/content/ContentLoader.h @@ -4,30 +4,32 @@ #include #include +namespace fs = std::filesystem; + class Block; class ItemDef; class ContentPack; class ContentBuilder; -namespace json { - class JObject; +namespace dynamic { + class Map; } class ContentLoader { const ContentPack* pack; void loadBlock(Block* def, std::string full, std::string name); - void loadCustomBlockModel(Block* def, json::JObject* primitives); + void loadCustomBlockModel(Block* def, dynamic::Map* primitives); void loadItem(ItemDef* def, std::string full, std::string name); public: ContentLoader(ContentPack* pack); bool fixPackIndices(std::filesystem::path folder, - json::JObject* indicesRoot, + dynamic::Map* indicesRoot, std::string contentSection); void fixPackIndices(); - void loadBlock(Block* def, std::string name, std::filesystem::path file); - void loadItem(ItemDef* def, std::string name, std::filesystem::path file); + void loadBlock(Block* def, std::string name, fs::path file); + void loadItem(ItemDef* def, std::string name, fs::path file); void load(ContentBuilder* builder); }; diff --git a/src/content/ContentPack.cpp b/src/content/ContentPack.cpp index ae3ea758..2084d571 100644 --- a/src/content/ContentPack.cpp +++ b/src/content/ContentPack.cpp @@ -5,6 +5,7 @@ #include "../coders/json.h" #include "../files/files.h" #include "../files/engine_paths.h" +#include "../data/dynamic.h" namespace fs = std::filesystem; @@ -15,7 +16,7 @@ const fs::path ContentPack::ITEMS_FOLDER = "items"; contentpack_error::contentpack_error( std::string packId, - std::filesystem::path folder, + fs::path folder, std::string message) : std::runtime_error(message), packId(packId), folder(folder) { } @@ -23,19 +24,19 @@ contentpack_error::contentpack_error( std::string contentpack_error::getPackId() const { return packId; } -std::filesystem::path contentpack_error::getFolder() const { +fs::path contentpack_error::getFolder() const { return folder; } -std::filesystem::path ContentPack::getContentFile() const { +fs::path ContentPack::getContentFile() const { return folder/fs::path(CONTENT_FILENAME); } -bool ContentPack::is_pack(std::filesystem::path folder) { +bool ContentPack::is_pack(fs::path folder) { return fs::is_regular_file(folder/fs::path(PACKAGE_FILENAME)); } -ContentPack ContentPack::read(std::filesystem::path folder) { +ContentPack ContentPack::read(fs::path folder) { auto root = files::read_json(folder/fs::path(PACKAGE_FILENAME)); ContentPack pack; root->str("id", pack.id); @@ -88,7 +89,7 @@ fs::path ContentPack::findPack(const EnginePaths* paths, fs::path worldDir, std: void ContentPack::readPacks(const EnginePaths* paths, std::vector& packs, const std::vector& packnames, - std::filesystem::path worldDir) { + fs::path worldDir) { for (const auto& name : packnames) { fs::path packfolder = ContentPack::findPack(paths, worldDir, name); packs.push_back(ContentPack::read(packfolder)); diff --git a/src/data/dynamic.cpp b/src/data/dynamic.cpp new file mode 100644 index 00000000..30fa53ca --- /dev/null +++ b/src/data/dynamic.cpp @@ -0,0 +1,331 @@ +#include "dynamic.h" + +#include + +using namespace dynamic; + +List::~List() { +} + +std::string List::str(size_t index) const { + const auto& val = values[index]; + switch (val->type) { + case valtype::string: return *val->value.str; + case valtype::boolean: return val->value.boolean ? "true" : "false"; + case valtype::number: return std::to_string(val->value.decimal); + case valtype::integer: return std::to_string(val->value.integer); + default: + throw std::runtime_error("type error"); + } +} + +double List::num(size_t index) const { + const auto& val = values[index]; + switch (val->type) { + case valtype::number: return val->value.decimal; + case valtype::integer: return val->value.integer; + case valtype::string: return std::stoll(*val->value.str); + case valtype::boolean: return val->value.boolean; + default: + throw std::runtime_error("type error"); + } +} + +int64_t List::integer(size_t index) const { + const auto& val = values[index]; + switch (val->type) { + case valtype::number: return val->value.decimal; + case valtype::integer: return val->value.integer; + case valtype::string: return std::stoll(*val->value.str); + case valtype::boolean: return val->value.boolean; + default: + throw std::runtime_error("type error"); + } +} + +Map* List::map(size_t index) const { + return values[index]->value.map; +} + +List* List::list(size_t index) const { + return values[index]->value.list; +} + +bool List::flag(size_t index) const { + return values[index]->value.boolean; +} + +List& List::put(std::string value) { + valvalue val; + val.str = new std::string(value); + values.push_back(std::make_unique(valtype::string, val)); + return *this; +} + +List& List::put(uint value) { + return put((int64_t)value); +} + +List& List::put(int value) { + return put((int64_t)value); +} + +List& List::put(int64_t value) { + valvalue val; + val.integer = value; + values.push_back(std::make_unique(valtype::integer, val)); + return *this; +} + +List& List::put(uint64_t value) { + return put((int64_t)value); +} + +List& List::put(double value) { + valvalue val; + val.decimal = value; + values.push_back(std::make_unique(valtype::number, val)); + return *this; +} + +List& List::put(float value) { + return put((double)value); +} + +List& List::put(bool value) { + valvalue val; + val.boolean = value; + values.push_back(std::make_unique(valtype::boolean, val)); + return *this; +} + +List& List::put(Map* value) { + valvalue val; + val.map = value; + values.push_back(std::make_unique(valtype::map, val)); + return *this; +} + +List& List::put(List* value) { + valvalue val; + val.list = value; + values.push_back(std::make_unique(valtype::list, val)); + return *this; +} + +List& List::putList() { + List* arr = new List(); + put(arr); + return *arr; +} + +Map& List::putMap() { + Map* map = new Map(); + put(map); + return *map; +} + +void List::remove(size_t index) { + values.erase(values.begin() + index); +} + +Map::~Map() { +} + +void Map::str(std::string key, std::string& dst) const { + dst = getStr(key, dst); +} + +std::string Map::getStr(std::string key, const std::string& def) const { + auto found = values.find(key); + if (found == values.end()) + return def; + auto& val = found->second; + switch (val->type) { + case valtype::string: return *val->value.str; + case valtype::boolean: return val->value.boolean ? "true" : "false"; + case valtype::number: return std::to_string(val->value.decimal); + case valtype::integer: return std::to_string(val->value.integer); + default: throw std::runtime_error("type error"); + } +} + +double Map::getNum(std::string key, double def) const { + auto found = values.find(key); + if (found == values.end()) + return def; + auto& val = found->second; + switch (val->type) { + case valtype::number: return val->value.decimal; + case valtype::integer: return val->value.integer; + case valtype::string: return std::stoull(*val->value.str); + case valtype::boolean: return val->value.boolean; + default: throw std::runtime_error("type error"); + } +} + +int64_t Map::getInt(std::string key, int64_t def) const { + auto found = values.find(key); + if (found == values.end()) + return def; + auto& val = found->second; + switch (val->type) { + case valtype::number: return val->value.decimal; + case valtype::integer: return val->value.integer; + case valtype::string: return std::stoull(*val->value.str); + case valtype::boolean: return val->value.boolean; + default: throw std::runtime_error("type error"); + } +} + +bool Map::getBool(std::string key, bool def) const { + auto found = values.find(key); + if (found != values.end()) + return found->second->value.boolean; + return def; +} + +void Map::num(std::string key, double& dst) const { + dst = getNum(key, dst); +} + +void Map::num(std::string key, float& dst) const { + dst = getNum(key, dst); +} + +void Map::num(std::string key, ubyte& dst) const { + dst = getInt(key, dst); +} + +void Map::num(std::string key, int& dst) const { + dst = getInt(key, dst); +} + +void Map::num(std::string key, int64_t& dst) const { + dst = getInt(key, dst); +} + +void Map::num(std::string key, uint64_t& dst) const { + dst = getInt(key, dst); +} + +void Map::num(std::string key, uint& dst) const { + dst = getInt(key, dst); +} + +Map* Map::map(std::string key) const { + auto found = values.find(key); + if (found != values.end()) { + auto& val = found->second; + if (val->type != valtype::map) + return nullptr; + return val->value.map; + } + return nullptr; +} + +List* Map::list(std::string key) const { + auto found = values.find(key); + if (found != values.end()) + return found->second->value.list; + return nullptr; +} + +void Map::flag(std::string key, bool& dst) const { + auto found = values.find(key); + if (found != values.end()) + dst = found->second->value.boolean; +} + +Map& Map::put(std::string key, uint value) { + return put(key, (int64_t)value); +} + +Map& Map::put(std::string key, int value) { + return put(key, (int64_t)value); +} + +Map& Map::put(std::string key, int64_t value) { + auto found = values.find(key); + if (found != values.end()) found->second; + valvalue val; + val.integer = value; + values.insert(std::make_pair(key, new Value(valtype::integer, val))); + return *this; +} + +Map& Map::put(std::string key, uint64_t value) { + return put(key, (int64_t)value); +} + +Map& Map::put(std::string key, float value) { + return put(key, (double)value); +} + +Map& Map::put(std::string key, double value) { + valvalue val; + val.decimal = value; + values.insert(std::make_pair(key, new Value(valtype::number, val))); + return *this; +} + +Map& Map::put(std::string key, std::string value){ + valvalue val; + val.str = new std::string(value); + values.insert(std::make_pair(key, new Value(valtype::string, val))); + return *this; +} + +Map& Map::put(std::string key, const char* value) { + return put(key, std::string(value)); +} + +Map& Map::put(std::string key, Map* value){ + valvalue val; + val.map = value; + values.insert(std::make_pair(key, new Value(valtype::map, val))); + return *this; +} + +Map& Map::put(std::string key, List* value){ + valvalue val; + val.list = value; + values.insert(std::make_pair(key, new Value(valtype::list, val))); + return *this; +} + +Map& Map::put(std::string key, bool value){ + valvalue val; + val.boolean = value; + values.insert(std::make_pair(key, new Value(valtype::boolean, val))); + return *this; +} + +List& Map::putList(std::string key) { + List* arr = new List(); + put(key, arr); + return *arr; +} + +Map& Map::putMap(std::string key) { + Map* obj = new Map(); + put(key, obj); + return *obj; +} + +bool Map::has(std::string key) { + return values.find(key) != values.end(); +} + +Value::Value(valtype type, valvalue value) : type(type), value(value) { +} + +Value::~Value() { + switch (type) { + case valtype::map: delete value.map; break; + case valtype::list: delete value.list; break; + case valtype::string: delete value.str; break; + default: + break; + } +} diff --git a/src/data/dynamic.h b/src/data/dynamic.h new file mode 100644 index 00000000..31150f26 --- /dev/null +++ b/src/data/dynamic.h @@ -0,0 +1,113 @@ +#ifndef DATA_DYNAMIC_H_ +#define DATA_DYNAMIC_H_ + +#include +#include +#include +#include "../typedefs.h" + +namespace dynamic { + class Map; + class List; + class Value; + + enum class valtype { + map, list, number, integer, string, boolean + }; + + union valvalue { + Map* map; + List* list; + std::string* str; + double decimal; + int64_t integer; + bool boolean; + }; + + class Value { + public: + valtype type; + valvalue value; + Value(valtype type, valvalue value); + ~Value(); + }; + + class List { + public: + std::vector> values; + ~List(); + + std::string str(size_t index) const; + double num(size_t index) const; + int64_t integer(size_t num) const; + Map* map(size_t index) const; + List* list(size_t index) const; + bool flag(size_t index) const; + + inline size_t size() const { + return values.size(); + } + + inline Value* get(size_t i) const { + return values.at(i).get(); + } + + List& put(uint value); + List& put(int value); + List& put(uint64_t value); + List& put(int64_t value); + List& put(float value); + List& put(double value); + List& put(std::string value); + List& put(Map* value); + List& put(List* value); + List& put(bool value); + + List& putList(); + Map& putMap(); + + void remove(size_t index); + }; + + class Map { + public: + std::unordered_map> values; + ~Map(); + + std::string getStr(std::string key, const std::string& def) const; + double getNum(std::string key, double def) const; + int64_t getInt(std::string key, int64_t def) const; + bool getBool(std::string key, bool def) const; + + void str(std::string key, std::string& dst) const; + void num(std::string key, int& dst) const; + void num(std::string key, float& dst) const; + void num(std::string key, uint& dst) const; + void num(std::string key, int64_t& dst) const; + void num(std::string key, uint64_t& dst) const; + void num(std::string key, ubyte& dst) const; + void num(std::string key, double& dst) const; + Map* map(std::string key) const; + List* list(std::string key) const; + void flag(std::string key, bool& dst) const; + + Map& put(std::string key, uint value); + Map& put(std::string key, int value); + Map& put(std::string key, int64_t value); + Map& put(std::string key, uint64_t value); + Map& put(std::string key, float value); + Map& put(std::string key, double value); + Map& put(std::string key, const char* value); + Map& put(std::string key, std::string value); + Map& put(std::string key, Map* value); + Map& put(std::string key, List* value); + Map& put(std::string key, bool value); + + List& putList(std::string key); + Map& putMap(std::string key); + + bool has(std::string key); + }; +} + +#endif // DATA_DYNAMIC_H_ diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index 84e857c0..6fd51b84 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -18,6 +18,8 @@ #include "../constants.h" #include "../items/ItemDef.h" +#include "../data/dynamic.h" + #include #include #include @@ -444,7 +446,7 @@ void WorldFiles::write(const World* world, const Content* content) { if (generatorTestMode) return; - writeIndices(content->indices); + writeIndices(content->getIndices()); writeRegions(regions, regionsFolder, REGION_LAYER_VOXELS); writeRegions(lights, lightsFolder, REGION_LAYER_LIGHTS); } @@ -460,16 +462,16 @@ void WorldFiles::writePacks(const World* world) { } void WorldFiles::writeIndices(const ContentIndices* indices) { - json::JObject root; + dynamic::Map root; uint count; - json::JArray& blocks = root.putArray("blocks"); + auto& blocks = root.putList("blocks"); count = indices->countBlockDefs(); for (uint i = 0; i < count; i++) { const Block* def = indices->getBlockDef(i); blocks.put(def->name); } - json::JArray& items = root.putArray("items"); + auto& items = root.putList("items"); count = indices->countItemDefs(); for (uint i = 0; i < count; i++) { const ItemDef* def = indices->getItemDef(i); @@ -480,16 +482,16 @@ void WorldFiles::writeIndices(const ContentIndices* indices) { } void WorldFiles::writeWorldInfo(const World* world) { - json::JObject root; + dynamic::Map root; - json::JObject& versionobj = root.putObj("version"); + auto& versionobj = root.putMap("version"); versionobj.put("major", ENGINE_VERSION_MAJOR); versionobj.put("minor", ENGINE_VERSION_MINOR); root.put("name", world->name); root.put("seed", world->seed); - json::JObject& timeobj = root.putObj("time"); + auto& timeobj = root.putMap("time"); timeobj.put("day-time", world->daytime); timeobj.put("day-time-speed", world->daytimeSpeed); @@ -503,11 +505,11 @@ bool WorldFiles::readWorldInfo(World* world) { return false; } - std::unique_ptr root(files::read_json(file)); + auto root = files::read_json(file); root->str("name", world->name); root->num("seed", world->seed); - json::JObject* verobj = root->obj("version"); + auto verobj = root->map("version"); if (verobj) { int major=0, minor=-1; verobj->num("major", major); @@ -515,7 +517,7 @@ bool WorldFiles::readWorldInfo(World* world) { std::cout << "world version: " << major << "." << minor << std::endl; } - json::JObject* timeobj = root->obj("time"); + auto timeobj = root->map("time"); if (timeobj) { timeobj->num("day-time", world->daytime); timeobj->num("day-time-speed", world->daytimeSpeed); @@ -526,13 +528,13 @@ bool WorldFiles::readWorldInfo(World* world) { void WorldFiles::writePlayer(Player* player){ glm::vec3 position = player->hitbox->position; - json::JObject root; - json::JArray& posarr = root.putArray("position"); + dynamic::Map root; + auto& posarr = root.putList("position"); posarr.put(position.x); posarr.put(position.y); posarr.put(position.z); - json::JArray& rotarr = root.putArray("rotation"); + auto& rotarr = root.putList("rotation"); rotarr.put(player->cam.x); rotarr.put(player->cam.y); @@ -549,15 +551,15 @@ bool WorldFiles::readPlayer(Player* player) { return false; } - std::unique_ptr root(files::read_json(file)); - json::JArray* posarr = root->arr("position"); + auto root = files::read_json(file); + auto posarr = root->list("position"); glm::vec3& position = player->hitbox->position; position.x = posarr->num(0); position.y = posarr->num(1); position.z = posarr->num(2); player->camera->position = position; - json::JArray* rotarr = root->arr("rotation"); + auto rotarr = root->list("rotation"); player->cam.x = rotarr->num(0); player->cam.y = rotarr->num(1); diff --git a/src/files/WorldFiles.h b/src/files/WorldFiles.h index d85b0221..9f44f99c 100644 --- a/src/files/WorldFiles.h +++ b/src/files/WorldFiles.h @@ -34,6 +34,8 @@ class Content; class ContentIndices; class World; +namespace fs = std::filesystem; + class illegal_region_format : public std::runtime_error { public: illegal_region_format(const std::string& message) @@ -63,27 +65,24 @@ struct regfile { files::rafile file; int version; - regfile(std::filesystem::path filename); + regfile(fs::path filename); }; typedef std::unordered_map> regionsmap; + class WorldFiles { std::unordered_map> openRegFiles; void writeWorldInfo(const World* world); - std::filesystem::path getLightsFolder() const; - std::filesystem::path getRegionFilename(int x, int y) const; - std::filesystem::path getPlayerFile() const; - std::filesystem::path getWorldFile() const; - std::filesystem::path getIndicesFile() const; - std::filesystem::path getPacksFile() const; + fs::path getLightsFolder() const; + fs::path getRegionFilename(int x, int y) const; + fs::path getPlayerFile() const; + fs::path getWorldFile() const; + fs::path getIndicesFile() const; + fs::path getPacksFile() const; - WorldRegion* getRegion(regionsmap& regions, - int x, int z); - - WorldRegion* getOrCreateRegion( - regionsmap& regions, - int x, int z); + WorldRegion* getRegion(regionsmap& regions, int x, int z); + WorldRegion* getOrCreateRegion(regionsmap& regions, int x, int z); /* Compress buffer with extrle @param src source buffer @@ -94,38 +93,36 @@ class WorldFiles { /* Decompress buffer with extrle @param src compressed buffer @param srclen length of compressed buffer - @param dstlen max expected length of source buffer - */ + @param dstlen max expected length of source buffer */ ubyte* decompress(const ubyte* src, size_t srclen, size_t dstlen); ubyte* readChunkData(int x, int y, uint32_t& length, - std::filesystem::path folder, + fs::path folder, int layer); void fetchChunks(WorldRegion* region, int x, int y, - std::filesystem::path folder, int layer); + fs::path folder, int layer); void writeRegions(regionsmap& regions, - const std::filesystem::path& folder, int layer); + const fs::path& folder, int layer); ubyte* getData(regionsmap& regions, - const std::filesystem::path& folder, + const fs::path& folder, int x, int z, int layer); - regfile* getRegFile(glm::ivec3 coord, - const std::filesystem::path& folder); + regfile* getRegFile(glm::ivec3 coord, const fs::path& folder); public: static bool parseRegionFilename(const std::string& name, int& x, int& y); - std::filesystem::path getRegionsFolder() const; + fs::path getRegionsFolder() const; regionsmap regions; regionsmap lights; - std::filesystem::path directory; + fs::path directory; std::unique_ptr compressionBuffer; bool generatorTestMode; bool doWriteLights; - WorldFiles(std::filesystem::path directory, const DebugSettings& settings); + WorldFiles(fs::path directory, const DebugSettings& settings); ~WorldFiles(); void put(Chunk* chunk); @@ -142,7 +139,7 @@ public: void writeRegion(int x, int y, WorldRegion* entry, - std::filesystem::path file, + fs::path file, int layer); void writePlayer(Player* player); /* @param world world info to save (nullable) */ diff --git a/src/files/files.cpp b/src/files/files.cpp index d2161b6c..8f459dcc 100644 --- a/src/files/files.cpp +++ b/src/files/files.cpp @@ -7,6 +7,7 @@ #include #include "../coders/json.h" #include "../util/stringutil.h" +#include "../data/dynamic.h" namespace fs = std::filesystem; @@ -92,18 +93,18 @@ bool files::write_string(fs::path filename, const std::string content) { return true; } -bool files::write_json(fs::path filename, const json::JObject* obj, bool nice) { +bool files::write_json(fs::path filename, const dynamic::Map* obj, bool nice) { // -- binary json tests //return write_binary_json(fs::path(filename.u8string()+".bin"), obj); return files::write_string(filename, json::stringify(obj, nice, " ")); } -bool files::write_binary_json(fs::path filename, const json::JObject* obj) { +bool files::write_binary_json(fs::path filename, const dynamic::Map* obj) { std::vector bytes = json::to_binary(obj); return files::write_bytes(filename, (const char*)bytes.data(), bytes.size()); } -json::JObject* files::read_json(fs::path filename) { +std::unique_ptr files::read_json(fs::path filename) { // binary json tests // fs::path binfile = fs::path(filename.u8string()+".bin"); // if (fs::is_regular_file(binfile)){ @@ -121,10 +122,10 @@ json::JObject* files::read_json(fs::path filename) { } } -json::JObject* files::read_binary_json(fs::path file) { +std::unique_ptr files::read_binary_json(fs::path file) { size_t size; std::unique_ptr bytes (files::read_bytes(file, size)); - return json::from_binary((const ubyte*)bytes.get(), size); + return std::unique_ptr(json::from_binary((const ubyte*)bytes.get(), size)); } std::vector files::read_list(fs::path filename) { diff --git a/src/files/files.h b/src/files/files.h index 2ad4a879..4e136fa6 100644 --- a/src/files/files.h +++ b/src/files/files.h @@ -3,14 +3,15 @@ #include #include +#include #include #include #include "../typedefs.h" namespace fs = std::filesystem; -namespace json { - class JObject; +namespace dynamic { + class Map; } namespace files { @@ -30,14 +31,14 @@ namespace files { extern bool write_bytes(fs::path, const char* data, size_t size); extern uint append_bytes(fs::path, const char* data, size_t size); extern bool write_string(fs::path filename, const std::string content); - extern bool write_json(fs::path filename, const json::JObject* obj, bool nice=true); - extern bool write_binary_json(fs::path filename, const json::JObject* obj); + extern bool write_json(fs::path filename, const dynamic::Map* obj, bool nice=true); + extern bool write_binary_json(fs::path filename, const dynamic::Map* obj); extern bool read(fs::path, char* data, size_t size); extern char* read_bytes(fs::path, size_t& length); extern std::string read_string(fs::path filename); - extern json::JObject* read_json(fs::path file); - extern json::JObject* read_binary_json(fs::path file); + extern std::unique_ptr read_json(fs::path file); + extern std::unique_ptr read_binary_json(fs::path file); extern std::vector read_list(fs::path file); } diff --git a/src/files/settings_io.cpp b/src/files/settings_io.cpp index ed19b80f..037282d0 100644 --- a/src/files/settings_io.cpp +++ b/src/files/settings_io.cpp @@ -9,6 +9,8 @@ #include "../coders/toml.h" #include "../coders/json.h" +#include "../data/dynamic.h" + toml::Wrapper* create_wrapper(EngineSettings& settings) { std::unique_ptr wrapper (new toml::Wrapper()); toml::Section& display = wrapper->add("display"); @@ -46,28 +48,27 @@ toml::Wrapper* create_wrapper(EngineSettings& settings) { } std::string write_controls() { - json::JObject* obj = new json::JObject(); + dynamic::Map obj; for (auto& entry : Events::bindings) { const auto& binding = entry.second; - json::JObject* jentry = new json::JObject(); + auto& jentry = obj.putMap(entry.first); switch (binding.type) { - case inputtype::keyboard: jentry->put("type", "keyboard"); break; - case inputtype::mouse: jentry->put("type", "mouse"); break; + case inputtype::keyboard: jentry.put("type", "keyboard"); break; + case inputtype::mouse: jentry.put("type", "mouse"); break; default: throw std::runtime_error("unsupported control type"); } - jentry->put("code", binding.code); - obj->put(entry.first, jentry); + jentry.put("code", binding.code); } - return json::stringify(obj, true, " "); + return json::stringify(&obj, true, " "); } void load_controls(std::string filename, std::string source) { - json::JObject* obj = json::parse(filename, source); + auto obj = json::parse(filename, source); for (auto& entry : Events::bindings) { auto& binding = entry.second; - json::JObject* jentry = obj->obj(entry.first); + auto jentry = obj->map(entry.first); if (jentry == nullptr) continue; inputtype type; diff --git a/src/frontend/ContentGfxCache.cpp b/src/frontend/ContentGfxCache.cpp index 86c8541c..cb9a9ae0 100644 --- a/src/frontend/ContentGfxCache.cpp +++ b/src/frontend/ContentGfxCache.cpp @@ -8,12 +8,12 @@ #include "../voxels/Block.h" ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) { - const ContentIndices* contentIds = content->indices; - sideregions = new UVRegion[contentIds->countBlockDefs() * 6]; + auto indices = content->getIndices(); + sideregions = new UVRegion[indices->countBlockDefs() * 6]; Atlas* atlas = assets->getAtlas("blocks"); - for (uint i = 0; i < contentIds->countBlockDefs(); i++) { - Block* def = contentIds->getBlockDef(i); + for (uint i = 0; i < indices->countBlockDefs(); i++) { + Block* def = indices->getBlockDef(i); for (uint side = 0; side < 6; side++) { std::string tex = def->textureFaces[side]; if (atlas->has(tex)) { diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp index 1c2d2f02..2b898c32 100644 --- a/src/frontend/InventoryView.cpp +++ b/src/frontend/InventoryView.cpp @@ -22,7 +22,7 @@ InventoryView::InventoryView( LevelFrontend* frontend, std::vector items) : content(content), - indices(content->indices), + indices(content->getIndices()), items(items), frontend(frontend), columns(columns) { diff --git a/src/frontend/WorldRenderer.cpp b/src/frontend/WorldRenderer.cpp index a322c10b..0e517951 100644 --- a/src/frontend/WorldRenderer.cpp +++ b/src/frontend/WorldRenderer.cpp @@ -132,7 +132,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible 1.0f+fog*2.0f, 4); const Content* content = level->content; - const ContentIndices* contentIds = content->indices; + auto indices = content->getIndices(); Assets* assets = engine->getAssets(); Atlas* atlas = assets->getAtlas("blocks"); Shader* shader = assets->getShader("main"); @@ -170,7 +170,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible shader->uniform1i("u_cubemap", 1); { itemid_t id = level->player->getChosenItem(); - ItemDef* item = contentIds->getItemDef(id); + ItemDef* item = indices->getItemDef(id); assert(item != nullptr); float multiplier = 0.5f; shader->uniform3f("u_torchlightColor", @@ -189,7 +189,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible // Selected block if (PlayerController::selectedBlockId != -1 && hudVisible){ blockid_t id = PlayerController::selectedBlockId; - Block* block = contentIds->getBlockDef(id); + Block* block = indices->getBlockDef(id); assert(block != nullptr); const vec3 pos = PlayerController::selectedBlockPosition; const vec3 point = PlayerController::selectedPointPosition; diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 54612c0e..aa7fd378 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -82,7 +82,7 @@ void HudRenderer::createDebugPanel(Engine* engine) { })); panel->add(create_label([=](){ auto player = level->player; - auto indices = level->content->indices; + auto* indices = level->content->getIndices(); auto def = indices->getBlockDef(player->selectedVoxel.id); std::wstringstream stream; stream << std::hex << level->player->selectedVoxel.states; @@ -171,7 +171,7 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend) auto level = frontend->getLevel(); auto menu = gui->getMenu(); auto content = level->content; - auto indices = content->indices; + auto indices = content->getIndices(); std::vector items; for (itemid_t id = 1; id < indices->countItemDefs(); id++) { diff --git a/src/frontend/locale/langs.cpp b/src/frontend/locale/langs.cpp index 66b4db75..0074816b 100644 --- a/src/frontend/locale/langs.cpp +++ b/src/frontend/locale/langs.cpp @@ -7,22 +7,17 @@ #include "../../content/ContentPack.h" #include "../../files/files.h" #include "../../util/stringutil.h" +#include "../../data/dynamic.h" -using std::string; -using std::wstring; -using std::vector; -using std::unique_ptr; -using std::unordered_map; -using std::filesystem::path; namespace fs = std::filesystem; -unique_ptr langs::current; -unordered_map langs::locales_info; +std::unique_ptr langs::current; +std::unordered_map langs::locales_info; -langs::Lang::Lang(string locale) : locale(locale) { +langs::Lang::Lang(std::string locale) : locale(locale) { } -const wstring& langs::Lang::get(const wstring& key) const { +const std::wstring& langs::Lang::get(const std::wstring& key) const { auto found = map.find(key); if (found == map.end()) { return key; @@ -30,11 +25,11 @@ const wstring& langs::Lang::get(const wstring& key) const { return found->second; } -void langs::Lang::put(const wstring& key, const wstring& text) { +void langs::Lang::put(const std::wstring& key, const std::wstring& text) { map[key] = text; } -const string& langs::Lang::getId() const { +const std::string& langs::Lang::getId() const { return locale; } @@ -50,16 +45,16 @@ class Reader : public BasicParser { } } public: - Reader(string file, string source) : BasicParser(file, source) { + Reader(std::string file, std::string source) : BasicParser(file, source) { } void read(langs::Lang& lang, std::string prefix) { skipWhitespace(); while (hasNext()) { - string key = parseString('=', true); + std::string key = parseString('=', true); util::trim(key); key = prefix + key; - string text = parseString('\n', false); + std::string text = parseString('\n', false); util::trim(text); lang.put(util::str2wstr_utf8(key), util::str2wstr_utf8(text)); @@ -68,22 +63,22 @@ public: } }; -void langs::loadLocalesInfo(const path& resdir, string& fallback) { - path file = resdir/path(langs::TEXTS_FOLDER)/path("langs.json"); - unique_ptr root (files::read_json(file)); +void langs::loadLocalesInfo(const fs::path& resdir, std::string& fallback) { + fs::path file = resdir/fs::path(langs::TEXTS_FOLDER)/fs::path("langs.json"); + auto root = files::read_json(file); langs::locales_info.clear(); root->str("fallback", fallback); - auto langs = root->obj("langs"); + auto langs = root->map("langs"); if (langs) { std::cout << "locales "; - for (auto& entry : langs->map) { - auto langInfo = entry.second; + for (auto& entry : langs->values) { + auto langInfo = entry.second.get(); - string name; - if (langInfo->type == json::valtype::object) { - name = langInfo->value.obj->getStr("name", "none"); + std::string name; + if (langInfo->type == dynamic::valtype::map) { + name = langInfo->value.map->getStr("name", "none"); } else { continue; } @@ -95,8 +90,8 @@ void langs::loadLocalesInfo(const path& resdir, string& fallback) { } } -std::string langs::locale_by_envlocale(const std::string& envlocale, const path& resdir){ - string fallback = FALLBACK_DEFAULT; +std::string langs::locale_by_envlocale(const std::string& envlocale, const fs::path& resdir){ + std::string fallback = FALLBACK_DEFAULT; if (locales_info.size() == 0) { loadLocalesInfo(resdir, fallback); } @@ -116,32 +111,33 @@ std::string langs::locale_by_envlocale(const std::string& envlocale, const path& } } -void langs::load(const path& resdir, - const string& locale, - const vector& packs, +void langs::load(const fs::path& resdir, + const std::string& locale, + const std::vector& packs, Lang& lang) { - path filename = path(TEXTS_FOLDER)/path(locale + LANG_FILE_EXT); - path core_file = resdir/filename; + fs::path filename = fs::path(TEXTS_FOLDER)/fs::path(locale + LANG_FILE_EXT); + fs::path core_file = resdir/filename; + if (fs::is_regular_file(core_file)) { - string text = files::read_string(core_file); + std::string text = files::read_string(core_file); Reader reader(core_file.string(), text); reader.read(lang, ""); } for (auto pack : packs) { - path file = pack.folder/filename; + fs::path file = pack.folder/filename; if (fs::is_regular_file(file)) { - string text = files::read_string(file); + std::string text = files::read_string(file); Reader reader(file.string(), text); reader.read(lang, pack.id+":"); } } } -void langs::load(const path& resdir, - const string& locale, - const string& fallback, - const vector& packs) { - unique_ptr lang (new Lang(locale)); +void langs::load(const fs::path& resdir, + const std::string& locale, + const std::string& fallback, + const std::vector& packs) { + auto lang = std::make_unique(locale); load(resdir, fallback, packs, *lang.get()); if (locale != fallback) { load(resdir, locale, packs, *lang.get()); @@ -149,10 +145,10 @@ void langs::load(const path& resdir, current.reset(lang.release()); } -void langs::setup(const path& resdir, - string locale, - const vector& packs) { - string fallback = langs::FALLBACK_DEFAULT; +void langs::setup(const fs::path& resdir, + std::string locale, + const std::vector& packs) { + std::string fallback = langs::FALLBACK_DEFAULT; langs::loadLocalesInfo(resdir, fallback); if (langs::locales_info.find(locale) == langs::locales_info.end()) { locale = fallback; @@ -160,13 +156,13 @@ void langs::setup(const path& resdir, langs::load(resdir, locale, fallback, packs); } -const wstring& langs::get(const wstring& key) { +const std::wstring& langs::get(const std::wstring& key) { return current->get(key); } -const wstring& langs::get(const wstring& key, const wstring& context) { - wstring ctxkey = context + L"." + key; - const wstring& text = current->get(ctxkey); +const std::wstring& langs::get(const std::wstring& key, const std::wstring& context) { + std::wstring ctxkey = context + L"." + key; + const std::wstring& text = current->get(ctxkey); if (&ctxkey != &text) { return text; } diff --git a/src/graphics/BlocksRenderer.cpp b/src/graphics/BlocksRenderer.cpp index 8c492a3b..ad8792bd 100644 --- a/src/graphics/BlocksRenderer.cpp +++ b/src/graphics/BlocksRenderer.cpp @@ -34,7 +34,7 @@ BlocksRenderer::BlocksRenderer(size_t capacity, vertexBuffer = new float[capacity]; indexBuffer = new int[capacity]; voxelsBuffer = new VoxelsVolume(CHUNK_W + 2, CHUNK_H, CHUNK_D + 2); - blockDefsCache = content->indices->getBlockDefs(); + blockDefsCache = content->getIndices()->getBlockDefs(); } BlocksRenderer::~BlocksRenderer() { @@ -44,9 +44,7 @@ BlocksRenderer::~BlocksRenderer() { } /* Basic vertex add method */ -void BlocksRenderer::vertex(const vec3& coord, - float u, float v, - const vec4& light) { +void BlocksRenderer::vertex(const vec3& coord, float u, float v, const vec4& light) { vertexBuffer[vertexOffset++] = coord.x; vertexBuffer[vertexOffset++] = coord.y; vertexBuffer[vertexOffset++] = coord.z; diff --git a/src/lighting/Lighting.cpp b/src/lighting/Lighting.cpp index 568bfe9f..25d1a551 100644 --- a/src/lighting/Lighting.cpp +++ b/src/lighting/Lighting.cpp @@ -15,11 +15,11 @@ using std::shared_ptr; Lighting::Lighting(const Content* content, Chunks* chunks) : content(content), chunks(chunks) { - const ContentIndices* contentIds = content->indices; - solverR = new LightSolver(contentIds, chunks, 0); - solverG = new LightSolver(contentIds, chunks, 1); - solverB = new LightSolver(contentIds, chunks, 2); - solverS = new LightSolver(contentIds, chunks, 3); + auto indices = content->getIndices(); + solverR = new LightSolver(indices, chunks, 0); + solverG = new LightSolver(indices, chunks, 1); + solverB = new LightSolver(indices, chunks, 2); + solverS = new LightSolver(indices, chunks, 3); } Lighting::~Lighting(){ @@ -42,7 +42,7 @@ void Lighting::clear(){ } void Lighting::prebuildSkyLight(int cx, int cz){ - const Block* const* blockDefs = content->indices->getBlockDefs(); + const Block* const* blockDefs = content->getIndices()->getBlockDefs(); Chunk* chunk = chunks->getChunk(cx, cz); int highestPoint = 0; @@ -68,7 +68,7 @@ void Lighting::prebuildSkyLight(int cx, int cz){ } void Lighting::buildSkyLight(int cx, int cz){ - const Block* const* blockDefs = content->indices->getBlockDefs(); + const Block* const* blockDefs = content->getIndices()->getBlockDefs(); Chunk* chunk = chunks->getChunk(cx, cz); for (int z = 0; z < CHUNK_D; z++){ @@ -95,7 +95,7 @@ void Lighting::buildSkyLight(int cx, int cz){ } void Lighting::onChunkLoaded(int cx, int cz){ - const Block* const* blockDefs = content->indices->getBlockDefs(); + const Block* const* blockDefs = content->getIndices()->getBlockDefs(); const Chunk* chunk = chunks->getChunk(cx, cz); for (unsigned int y = 0; y < CHUNK_H; y++){ @@ -137,7 +137,7 @@ void Lighting::onChunkLoaded(int cx, int cz){ } void Lighting::onBlockSet(int x, int y, int z, int const id){ - Block* block = content->indices->getBlockDef(id); + Block* block = content->getIndices()->getBlockDef(id); if (id == 0){ solverR->remove(x,y,z); solverG->remove(x,y,z); diff --git a/src/logic/BlocksController.cpp b/src/logic/BlocksController.cpp index a84b7fdf..ab308342 100644 --- a/src/logic/BlocksController.cpp +++ b/src/logic/BlocksController.cpp @@ -69,7 +69,7 @@ void BlocksController::updateBlock(int x, int y, int z) { voxel* vox = chunks->get(x, y, z); if (vox == nullptr) return; - const Block* def = level->content->indices->getBlockDef(vox->id); + const Block* def = level->content->getIndices()->getBlockDef(vox->id); if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) { breakBlock(nullptr, def, x, y, z); return; @@ -89,7 +89,7 @@ void BlocksController::randomTick(int tickid, int parts) { // timeutil::ScopeLogTimer timer(5000+tickid); const int w = chunks->w; const int d = chunks->d; - auto indices = level->content->indices; + auto indices = level->content->getIndices(); for (uint z = padding; z < d-padding; z++){ for (uint x = padding; x < w-padding; x++){ int index = z * w + x; diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index 253f5bda..a0ef0f2f 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -204,7 +204,7 @@ void PlayerController::updateControls(float delta){ } void PlayerController::updateInteraction(){ - auto contentIds = level->content->indices; + auto indices = level->content->getIndices(); Chunks* chunks = level->chunks; Player* player = level->player; Lighting* lighting = level->lighting; @@ -239,8 +239,8 @@ void PlayerController::updateInteraction(){ int z = iend.z; uint8_t states = 0; - ItemDef* item = contentIds->getItemDef(player->getChosenItem()); - Block* def = contentIds->getBlockDef(item->rt.placingBlock); + ItemDef* item = indices->getItemDef(player->getChosenItem()); + Block* def = indices->getBlockDef(item->rt.placingBlock); if (def && def->rotatable){ const std::string& name = def->rotations.name; if (name == "pipe") { @@ -270,7 +270,7 @@ void PlayerController::updateInteraction(){ } } - Block* target = contentIds->getBlockDef(vox->id); + Block* target = indices->getBlockDef(vox->id); if (lclick && target->breakable){ blocksController->breakBlock(player, target, x, y, z); } @@ -292,7 +292,7 @@ void PlayerController::updateInteraction(){ } vox = chunks->get(x, y, z); blockid_t chosenBlock = def->rt.id; - if (vox && (target = contentIds->getBlockDef(vox->id))->replaceable) { + if (vox && (target = indices->getBlockDef(vox->id))->replaceable) { if (!level->physics->isBlockInside(x,y,z, player->hitbox.get()) || !def->obstacle){ if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) { @@ -310,7 +310,7 @@ void PlayerController::updateInteraction(){ } } if (Events::jactive(BIND_PLAYER_PICK)){ - Block* block = contentIds->getBlockDef(chunks->get(x,y,z)->id); + Block* block = indices->getBlockDef(chunks->get(x,y,z)->id); player->setChosenItem(block->rt.pickingItem); } } else { diff --git a/src/logic/scripting/api_lua.cpp b/src/logic/scripting/api_lua.cpp index 26ade8a6..8403604c 100644 --- a/src/logic/scripting/api_lua.cpp +++ b/src/logic/scripting/api_lua.cpp @@ -107,7 +107,8 @@ static const luaL_Reg playerlib [] = { /* == blocks-related functions == */ static int l_block_name(lua_State* L) { int id = lua_tointeger(L, 1); - lua_pushstring(L, scripting::content->indices->getBlockDef(id)->name.c_str()); + auto def = scripting::content->getIndices()->getBlockDef(id); + lua_pushstring(L, def->name.c_str()); return 1; } @@ -121,7 +122,7 @@ static int l_is_solid_at(lua_State* L) { } static int l_blocks_count(lua_State* L) { - lua_pushinteger(L, scripting::content->indices->countBlockDefs()); + lua_pushinteger(L, scripting::content->getIndices()->countBlockDefs()); return 1; } @@ -163,7 +164,7 @@ static int l_get_block_x(lua_State* L) { if (vox == nullptr) { return lua_pushivec3(L, 1, 0, 0); } - const Block* def = scripting::level->content->indices->getBlockDef(vox->id); + auto def = scripting::level->content->getIndices()->getBlockDef(vox->id); if (!def->rotatable) { return lua_pushivec3(L, 1, 0, 0); } else { @@ -180,7 +181,7 @@ static int l_get_block_y(lua_State* L) { if (vox == nullptr) { return lua_pushivec3(L, 0, 1, 0); } - const Block* def = scripting::level->content->indices->getBlockDef(vox->id); + auto def = scripting::level->content->getIndices()->getBlockDef(vox->id); if (!def->rotatable) { return lua_pushivec3(L, 0, 1, 0); } else { @@ -197,7 +198,7 @@ static int l_get_block_z(lua_State* L) { if (vox == nullptr) { return lua_pushivec3(L, 0, 0, 1); } - const Block* def = scripting::level->content->indices->getBlockDef(vox->id); + auto def = scripting::level->content->getIndices()->getBlockDef(vox->id); if (!def->rotatable) { return lua_pushivec3(L, 0, 0, 1); } else { diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index d4af3bf8..d61b667a 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -26,9 +26,6 @@ Player::Player(glm::vec3 position, float speed) : hitbox(new Hitbox(position, glm::vec3(0.3f,0.9f,0.3f))) { } -Player::~Player(){ -} - void Player::update( Level* level, PlayerInput& input, diff --git a/src/objects/Player.h b/src/objects/Player.h index 14aa30a2..46f39aa6 100644 --- a/src/objects/Player.h +++ b/src/objects/Player.h @@ -43,7 +43,7 @@ public: glm::vec2 cam = {}; Player(glm::vec3 position, float speed); - ~Player(); + ~Player() = default; void teleport(glm::vec3 position); void update(Level* level, PlayerInput& input, float delta); diff --git a/src/typedefs.h b/src/typedefs.h index 02bd95fd..7a0fd0f0 100644 --- a/src/typedefs.h +++ b/src/typedefs.h @@ -13,6 +13,8 @@ typedef uint8_t ubyte; typedef uint32_t itemid_t; typedef uint16_t blockid_t; +typedef uint32_t itemcount_t; + typedef uint16_t blockstate_t; typedef uint16_t light_t; diff --git a/src/voxels/Chunk.h b/src/voxels/Chunk.h index 0803c8db..6a07ec5c 100644 --- a/src/voxels/Chunk.h +++ b/src/voxels/Chunk.h @@ -4,7 +4,7 @@ #include #include "../constants.h" -struct ChunkFlag{ +struct ChunkFlag { static const int MODIFIED = 0x1; static const int READY = 0x2; static const int LOADED = 0x4; @@ -37,7 +37,6 @@ public: Chunk* clone() const; // flags getters/setters below - inline void setFlags(int mask, bool value){ if (value) flags |= mask; diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index d774583b..7fb00c6e 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -22,7 +22,7 @@ Chunks::Chunks(int w, int d, LevelEvents* events, const Content* content) : content(content), - contentIds(content->indices), + contentIds(content->getIndices()), chunks(w*d), chunksSecond(w*d), w(w), d(d), ox(ox), oz(oz), @@ -32,9 +32,6 @@ Chunks::Chunks(int w, int d, chunksCount = 0; } -Chunks::~Chunks(){ -} - voxel* Chunks::get(int x, int y, int z){ x -= ox * CHUNK_W; z -= oz * CHUNK_D; diff --git a/src/voxels/Chunks.h b/src/voxels/Chunks.h index 06bfb122..dcecd347 100644 --- a/src/voxels/Chunks.h +++ b/src/voxels/Chunks.h @@ -34,7 +34,7 @@ public: Chunks(int w, int d, int ox, int oz, WorldFiles* worldFiles, LevelEvents* events, const Content* content); - ~Chunks(); + ~Chunks() = default; bool putChunk(std::shared_ptr chunk); diff --git a/src/voxels/ChunksStorage.cpp b/src/voxels/ChunksStorage.cpp index 18c4988c..38f4808a 100644 --- a/src/voxels/ChunksStorage.cpp +++ b/src/voxels/ChunksStorage.cpp @@ -17,9 +17,6 @@ ChunksStorage::ChunksStorage(Level* level) : level(level) { } -ChunksStorage::~ChunksStorage() { -} - void ChunksStorage::store(std::shared_ptr chunk) { chunksMap[glm::ivec2(chunk->x, chunk->z)] = chunk; } @@ -61,7 +58,7 @@ std::shared_ptr ChunksStorage::create(int x, int z) { if (data) { chunk->decode(data.get()); chunk->setLoaded(true); - verifyLoadedChunk(level->content->indices, chunk.get()); + verifyLoadedChunk(level->content->getIndices(), chunk.get()); } light_t* lights = wfile->getLights(chunk->x, chunk->z); @@ -75,7 +72,7 @@ std::shared_ptr ChunksStorage::create(int x, int z) { // some magic code void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const { const Content* content = level->content; - const ContentIndices* indices = content->indices; + auto indices = content->getIndices(); voxel* voxels = volume->getVoxels(); light_t* lights = volume->getLights(); int x = volume->getX(); @@ -115,7 +112,7 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const { } } } else { - const std::shared_ptr& chunk = found->second; + auto& chunk = found->second; const voxel* cvoxels = chunk->voxels; const light_t* clights = chunk->lightmap->getLights(); for (int ly = y; ly < y + h; ly++) { diff --git a/src/voxels/ChunksStorage.h b/src/voxels/ChunksStorage.h index e6549c5a..ad5b0093 100644 --- a/src/voxels/ChunksStorage.h +++ b/src/voxels/ChunksStorage.h @@ -18,7 +18,7 @@ class ChunksStorage { std::unordered_map> chunksMap; public: ChunksStorage(Level* level); - virtual ~ChunksStorage(); + ~ChunksStorage() = default; std::shared_ptr get(int x, int z) const; void store(std::shared_ptr chunk); From e71890b709e80d0cebba28f049bd9f9aaef48ffb Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 17 Jan 2024 16:35:17 +0300 Subject: [PATCH 6/6] includes fix --- src/content/Content.h | 1 + src/data/dynamic.h | 1 + 2 files changed, 2 insertions(+) diff --git a/src/content/Content.h b/src/content/Content.h index cd36a474..bbf25635 100644 --- a/src/content/Content.h +++ b/src/content/Content.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include diff --git a/src/data/dynamic.h b/src/data/dynamic.h index 31150f26..eb4bdf1b 100644 --- a/src/data/dynamic.h +++ b/src/data/dynamic.h @@ -3,6 +3,7 @@ #include #include +#include #include #include "../typedefs.h"