the big refactor + extracted data classes from coders/json to data/dynamic

This commit is contained in:
MihailRis 2024-01-17 16:32:53 +03:00
parent 34d4500e24
commit 377c8e5029
36 changed files with 908 additions and 875 deletions

159
src/coders/binary_json.cpp Normal file
View File

@ -0,0 +1,159 @@
#include "binary_json.h"
#include <stdexcept>
#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<ubyte> 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<ubyte> 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<List>();
auto& items = array->values;
while (reader.peek() != BJSON_END) {
items.push_back(std::unique_ptr<Value>(value_from_binary(reader)));
}
reader.get();
return array.release();
}
static Map* object_from_binary(ByteReader& reader) {
auto obj = std::make_unique<Map>();
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<Map> json::from_binary(const ubyte* src, size_t size) {
ByteReader reader(src, size);
std::unique_ptr<Value> value (value_from_binary(reader));
if (value->type != valtype::map) {
throw std::runtime_error("root value is not an object");
}
std::unique_ptr<Map> obj (value->value.map);
value->value.map = nullptr;
return obj;
}

28
src/coders/binary_json.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef CODERS_BINARY_JSON_H_
#define CODERS_BINARY_JSON_H_
#include <vector>
#include <memory>
#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<ubyte> to_binary(const dynamic::Map* obj);
extern std::unique_ptr<dynamic::Map> from_binary(const ubyte* src, size_t size);
}
#endif // CODERS_BINARY_JSON_H_

View File

@ -6,24 +6,10 @@
#include <memory> #include <memory>
#include "commons.h" #include "commons.h"
#include "byte_utils.h" #include "../data/dynamic.h"
using namespace json; using namespace json;
using namespace dynamic;
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(std::stringstream& ss, inline void newline(std::stringstream& ss,
bool nice, uint indent, bool nice, uint indent,
@ -44,7 +30,7 @@ void stringify(const Value* value,
const std::string& indentstr, const std::string& indentstr,
bool nice); bool nice);
void stringifyObj(const JObject* obj, void stringifyObj(const Map* obj,
std::stringstream& ss, std::stringstream& ss,
int indent, int indent,
const std::string& indentstr, const std::string& indentstr,
@ -55,23 +41,23 @@ void stringify(const Value* value,
int indent, int indent,
const std::string& indentstr, const std::string& indentstr,
bool nice) { bool nice) {
if (value->type == valtype::object) { if (value->type == valtype::map) {
stringifyObj(value->value.obj, ss, indent, indentstr, nice); stringifyObj(value->value.map, ss, indent, indentstr, nice);
} }
else if (value->type == valtype::array) { else if (value->type == valtype::list) {
std::vector<Value*>& list = value->value.arr->values; auto list = value->value.list;
if (list.empty()) { if (list->size() == 0) {
ss << "[]"; ss << "[]";
return; return;
} }
ss << '['; ss << '[';
for (uint i = 0; i < list.size(); i++) { for (uint i = 0; i < list->size(); i++) {
Value* value = list[i]; Value* value = list->get(i);
if (i > 0 || nice) { if (i > 0 || nice) {
newline(ss, nice, indent, indentstr); newline(ss, nice, indent, indentstr);
} }
stringify(value, ss, indent+1, indentstr, nice); stringify(value, ss, indent+1, indentstr, nice);
if (i + 1 < list.size()) { if (i + 1 < list->size()) {
ss << ','; ss << ',';
} }
} }
@ -91,27 +77,27 @@ void stringify(const Value* value,
} }
} }
void stringifyObj(const JObject* obj, void stringifyObj(const Map* obj,
std::stringstream& ss, std::stringstream& ss,
int indent, int indent,
const std::string& indentstr, const std::string& indentstr,
bool nice) { bool nice) {
if (obj->map.empty()) { if (obj->values.empty()) {
ss << "{}"; ss << "{}";
return; return;
} }
ss << "{"; ss << "{";
uint index = 0; uint index = 0;
for (auto entry : obj->map) { for (auto& entry : obj->values) {
const std::string& key = entry.first; const std::string& key = entry.first;
if (index > 0 || nice) { if (index > 0 || nice) {
newline(ss, nice, indent, indentstr); newline(ss, nice, indent, indentstr);
} }
Value* value = entry.second; Value* value = entry.second.get();
ss << escape_string(key) << ": "; ss << escape_string(key) << ": ";
stringify(value, ss, indent+1, indentstr, nice); stringify(value, ss, indent+1, indentstr, nice);
index++; index++;
if (index < obj->map.size()) { if (index < obj->values.size()) {
ss << ','; ss << ',';
} }
} }
@ -122,7 +108,7 @@ void stringifyObj(const JObject* obj,
} }
std::string json::stringify( std::string json::stringify(
const JObject* obj, const Map* obj,
bool nice, bool nice,
const std::string& indent) { const std::string& indent) {
std::stringstream ss; std::stringstream ss;
@ -130,497 +116,11 @@ std::string json::stringify(
return ss.str(); return ss.str();
} }
static void to_binary(ByteBuilder& builder, const Value* value) {
switch (value->type) {
case valtype::object: {
std::vector<ubyte> 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<ubyte> 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<JArray>();
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<JObject>();
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 (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) Parser::Parser(std::string filename, std::string source)
: BasicParser(filename, source) { : BasicParser(filename, source) {
} }
JObject* Parser::parse() { Map* Parser::parse() {
char next = peek(); char next = peek();
if (next != '{') { if (next != '{') {
throw error("'{' expected"); throw error("'{' expected");
@ -628,10 +128,10 @@ JObject* Parser::parse() {
return parseObject(); return parseObject();
} }
JObject* Parser::parseObject() { Map* Parser::parseObject() {
expect('{'); expect('{');
auto obj = std::make_unique<JObject>(); auto obj = std::make_unique<Map>();
auto& map = obj->map; auto& map = obj->values;
while (peek() != '}') { while (peek() != '}') {
if (peek() == '#') { if (peek() == '#') {
skipLine(); skipLine();
@ -657,16 +157,16 @@ JObject* Parser::parseObject() {
return obj.release(); return obj.release();
} }
JArray* Parser::parseArray() { List* Parser::parseList() {
expect('['); expect('[');
auto arr = std::make_unique<JArray>(); auto arr = std::make_unique<List>();
auto& values = arr->values; auto& values = arr->values;
while (peek() != ']') { while (peek() != ']') {
if (peek() == '#') { if (peek() == '#') {
skipLine(); skipLine();
continue; continue;
} }
values.push_back(parseValue()); values.push_back(std::unique_ptr<Value>(parseValue()));
char next = peek(); char next = peek();
if (next == ',') { if (next == ',') {
@ -683,7 +183,7 @@ JArray* Parser::parseArray() {
Value* Parser::parseValue() { Value* Parser::parseValue() {
char next = peek(); char next = peek();
valvalue val; dynamic::valvalue val;
if (next == '-' || next == '+') { if (next == '-' || next == '+') {
pos++; pos++;
number_u num; number_u num;
@ -715,12 +215,12 @@ Value* Parser::parseValue() {
throw error("invalid literal "); throw error("invalid literal ");
} }
if (next == '{') { if (next == '{') {
val.obj = parseObject(); val.map = parseObject();
return new Value(valtype::object, val); return new Value(valtype::map, val);
} }
if (next == '[') { if (next == '[') {
val.arr = parseArray(); val.list = parseList();
return new Value(valtype::array, val); return new Value(valtype::list, val);
} }
if (is_digit(next)) { if (is_digit(next)) {
number_u num; number_u num;
@ -742,11 +242,11 @@ Value* Parser::parseValue() {
throw error("unexpected character '"+std::string({next})+"'"); throw error("unexpected character '"+std::string({next})+"'");
} }
JObject* json::parse(std::string filename, std::string source) { std::unique_ptr<Map> json::parse(std::string filename, std::string source) {
Parser parser(filename, source); Parser parser(filename, source);
return parser.parse(); return std::unique_ptr<Map>(parser.parse());
} }
JObject* json::parse(std::string source) { std::unique_ptr<Map> json::parse(std::string source) {
return parse("<string>", source); return parse("<string>", source);
} }

View File

@ -8,122 +8,34 @@
#include <unordered_map> #include <unordered_map>
#include "commons.h" #include "commons.h"
#include "../typedefs.h"
typedef unsigned int uint; #include "binary_json.h"
namespace dynamic {
class Map;
class List;
class Value;
}
namespace json { namespace json {
class JObject;
class JArray;
class Value;
extern std::string stringify(const JObject* obj, bool nice, const std::string& indent);
extern std::vector<ubyte> 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<Value*> 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<std::string, Value*> 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 { class Parser : public BasicParser {
JArray* parseArray(); dynamic::List* parseList();
JObject* parseObject(); dynamic::Map* parseObject();
Value* parseValue(); dynamic::Value* parseValue();
public: public:
Parser(std::string filename, std::string source); Parser(std::string filename, std::string source);
JObject* parse(); dynamic::Map* parse();
}; };
extern JObject* parse(std::string filename, std::string source); extern std::unique_ptr<dynamic::Map> parse(std::string filename, std::string source);
extern JObject* parse(std::string source); extern std::unique_ptr<dynamic::Map> parse(std::string source);
extern std::string stringify(
const dynamic::Map* obj,
bool nice,
const std::string& indent);
} }
#endif // CODERS_JSON_H_ #endif // CODERS_JSON_H_

View File

@ -131,7 +131,6 @@ Content::Content(ContentIndices* indices, DrawGroups* drawGroups,
} }
Content::~Content() { Content::~Content() {
delete indices;
delete drawGroups; delete drawGroups;
} }

View File

@ -92,8 +92,8 @@ public:
class Content { class Content {
std::unordered_map<std::string, Block*> blockDefs; std::unordered_map<std::string, Block*> blockDefs;
std::unordered_map<std::string, ItemDef*> itemDefs; std::unordered_map<std::string, ItemDef*> itemDefs;
std::unique_ptr<ContentIndices> indices;
public: public:
ContentIndices* const indices;
DrawGroups* const drawGroups; DrawGroups* const drawGroups;
Content(ContentIndices* indices, DrawGroups* drawGroups, Content(ContentIndices* indices, DrawGroups* drawGroups,
@ -101,6 +101,10 @@ public:
std::unordered_map<std::string, ItemDef*> itemDefs); std::unordered_map<std::string, ItemDef*> itemDefs);
~Content(); ~Content();
inline ContentIndices* getIndices() const {
return indices.get();
}
Block* findBlock(std::string id) const; Block* findBlock(std::string id) const;
Block* requireBlock(std::string id) const; Block* requireBlock(std::string id) const;

View File

@ -8,15 +8,10 @@
#include "../coders/json.h" #include "../coders/json.h"
#include "../voxels/Block.h" #include "../voxels/Block.h"
using std::string; #include "../data/dynamic.h"
using std::unique_ptr;
using std::make_unique;
using std::filesystem::path;
#include <iostream>
ContentLUT::ContentLUT(size_t blocksCount, const Content* content) { ContentLUT::ContentLUT(size_t blocksCount, const Content* content) {
ContentIndices* indices = content->indices; auto* indices = content->getIndices();
for (size_t i = 0; i < blocksCount; i++) { for (size_t i = 0; i < blocksCount; i++) {
blocks.push_back(i); blocks.push_back(i);
} }
@ -29,20 +24,20 @@ ContentLUT::ContentLUT(size_t blocksCount, const Content* content) {
} }
} }
ContentLUT* ContentLUT::create(const path& filename, ContentLUT* ContentLUT::create(const fs::path& filename,
const Content* content) { const Content* content) {
unique_ptr<json::JObject> root(files::read_json(filename)); auto root = files::read_json(filename);
json::JArray* blocksarr = root->arr("blocks"); auto blocklist = root->list("blocks");
auto& indices = content->indices; auto* indices = content->getIndices();
size_t blocks_c = blocksarr size_t blocks_c = blocklist
? std::max(blocksarr->size(), indices->countBlockDefs()) ? std::max(blocklist->size(), indices->countBlockDefs())
: indices->countBlockDefs(); : indices->countBlockDefs();
auto lut = make_unique<ContentLUT>(blocks_c, content); auto lut = std::make_unique<ContentLUT>(blocks_c, content);
if (blocksarr) { if (blocklist) {
for (size_t i = 0; i < blocksarr->size(); i++) { for (size_t i = 0; i < blocklist->size(); i++) {
string name = blocksarr->str(i); std::string name = blocklist->str(i);
Block* def = content->findBlock(name); Block* def = content->findBlock(name);
if (def) { if (def) {
lut->setBlock(i, name, def->rt.id); lut->setBlock(i, name, def->rt.id);

View File

@ -8,6 +8,8 @@
#include "../typedefs.h" #include "../typedefs.h"
#include "../constants.h" #include "../constants.h"
namespace fs = std::filesystem;
class Content; class Content;
/* Content indices lookup table or report /* 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); const Content* content);
inline bool hasContentReorder() const { inline bool hasContentReorder() const {

View File

@ -13,6 +13,7 @@
#include "../files/files.h" #include "../files/files.h"
#include "../coders/json.h" #include "../coders/json.h"
#include "../typedefs.h" #include "../typedefs.h"
#include "../data/dynamic.h"
#include "ContentPack.h" #include "ContentPack.h"
#include "../logic/scripting/scripting.h" #include "../logic/scripting/scripting.h"
@ -23,9 +24,8 @@ ContentLoader::ContentLoader(ContentPack* pack) : pack(pack) {
} }
bool ContentLoader::fixPackIndices(fs::path folder, bool ContentLoader::fixPackIndices(fs::path folder,
json::JObject* indicesRoot, dynamic::Map* indicesRoot,
std::string contentSection) { std::string contentSection) {
std::vector<std::string> detected; std::vector<std::string> detected;
std::vector<std::string> indexed; std::vector<std::string> indexed;
if (fs::is_directory(folder)) { if (fs::is_directory(folder)) {
@ -42,9 +42,9 @@ bool ContentLoader::fixPackIndices(fs::path folder,
bool modified = false; bool modified = false;
if (!indicesRoot->has(contentSection)) { if (!indicesRoot->has(contentSection)) {
indicesRoot->putArray(contentSection); indicesRoot->putList(contentSection);
} }
json::JArray* arr = indicesRoot->arr(contentSection); auto arr = indicesRoot->list(contentSection);
if (arr) { if (arr) {
for (uint i = 0; i < arr->size(); i++) { for (uint i = 0; i < arr->size(); i++) {
std::string name = arr->str(i); std::string name = arr->str(i);
@ -72,11 +72,11 @@ void ContentLoader::fixPackIndices() {
auto blocksFolder = folder/ContentPack::BLOCKS_FOLDER; auto blocksFolder = folder/ContentPack::BLOCKS_FOLDER;
auto itemsFolder = folder/ContentPack::ITEMS_FOLDER; auto itemsFolder = folder/ContentPack::ITEMS_FOLDER;
std::unique_ptr<json::JObject> root; std::unique_ptr<dynamic::Map> root;
if (fs::is_regular_file(indexFile)) { if (fs::is_regular_file(indexFile)) {
root.reset(files::read_json(indexFile)); root = std::move(files::read_json(indexFile));
} else { } else {
root.reset(new json::JObject()); root.reset(new dynamic::Map());
} }
bool modified = false; bool modified = false;
@ -93,7 +93,7 @@ void ContentLoader::fixPackIndices() {
// TODO: add basic validation and logging // TODO: add basic validation and logging
void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) { void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) {
std::unique_ptr<json::JObject> root(files::read_json(file)); auto root = files::read_json(file);
// block texturing // block texturing
if (root->has("texture")) { if (root->has("texture")) {
@ -103,7 +103,7 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) {
def->textureFaces[i] = texture; def->textureFaces[i] = texture;
} }
} else if (root->has("texture-faces")) { } 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++) { for (uint i = 0; i < 6; i++) {
def->textureFaces[i] = texarr->str(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") { else if (model == "custom") {
def->model = BlockModel::custom; def->model = BlockModel::custom;
if (root->has("model-primitives")) { if (root->has("model-primitives")) {
loadCustomBlockModel(def, root->obj("model-primitives")); loadCustomBlockModel(def, root->map("model-primitives"));
} }
else { else {
std::cerr << "ERROR occured while block " 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] // block hitbox AABB [x, y, z, width, height, depth]
json::JArray* boxobj = root->arr("hitbox"); auto boxarr = root->list("hitbox");
if (boxobj) { if (boxarr) {
AABB& aabb = def->hitbox; AABB& aabb = def->hitbox;
aabb.a = glm::vec3(boxobj->num(0), boxobj->num(1), boxobj->num(2)); aabb.a = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2));
aabb.b = glm::vec3(boxobj->num(3), boxobj->num(4), boxobj->num(5)); aabb.b = glm::vec3(boxarr->num(3), boxarr->num(4), boxarr->num(5));
aabb.b += aabb.a; aabb.b += aabb.a;
} }
// block light emission [r, g, b] where r,g,b in range [0..15] // block light emission [r, g, b] where r,g,b in range [0..15]
json::JArray* emissionobj = root->arr("emission"); auto emissionarr = root->list("emission");
if (emissionobj) { if (emissionarr) {
def->emission[0] = emissionobj->num(0); def->emission[0] = emissionarr->num(0);
def->emission[1] = emissionobj->num(1); def->emission[1] = emissionarr->num(1);
def->emission[2] = emissionobj->num(2); def->emission[2] = emissionarr->num(2);
} }
// primitive properties // primitive properties
@ -171,29 +171,28 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) {
root->flag("hidden", def->hidden); root->flag("hidden", def->hidden);
root->flag("sky-light-passing", def->skyLightPassing); root->flag("sky-light-passing", def->skyLightPassing);
root->num("draw-group", def->drawGroup); root->num("draw-group", def->drawGroup);
root->str("picking-item", def->pickingItem); 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")) { if (primitives->has("aabbs")) {
json::JArray* modelboxes = primitives->arr("aabbs"); auto modelboxes = primitives->list("aabbs");
for (uint i = 0; i < modelboxes->size(); i++ ) { for (uint i = 0; i < modelboxes->size(); i++ ) {
/* Parse aabb */ /* Parse aabb */
json::JArray* boxobj = modelboxes->arr(i); auto boxarr = modelboxes->list(i);
AABB modelbox; AABB modelbox;
modelbox.a = glm::vec3(boxobj->num(0), boxobj->num(1), boxobj->num(2)); modelbox.a = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2));
modelbox.b = glm::vec3(boxobj->num(3), boxobj->num(4), boxobj->num(5)); modelbox.b = glm::vec3(boxarr->num(3), boxarr->num(4), boxarr->num(5));
modelbox.b += modelbox.a; modelbox.b += modelbox.a;
def->modelBoxes.push_back(modelbox); def->modelBoxes.push_back(modelbox);
if (boxobj->size() == 7) if (boxarr->size() == 7)
for (uint i = 6; i < 12; i++) { 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++) { for (uint i = 6; i < 12; i++) {
def->modelTextures.push_back(boxobj->str(i)); def->modelTextures.push_back(boxarr->str(i));
} }
else else
for (uint i = 6; i < 12; i++) { for (uint i = 6; i < 12; i++) {
@ -202,10 +201,10 @@ void ContentLoader::loadCustomBlockModel(Block* def, json::JObject* primitives)
} }
} }
if (primitives->has("tetragons")) { if (primitives->has("tetragons")) {
json::JArray* modeltetragons = primitives->arr("tetragons"); auto modeltetragons = primitives->list("tetragons");
for (uint i = 0; i < modeltetragons->size(); i++) { for (uint i = 0; i < modeltetragons->size(); i++) {
/* Parse tetragon to points */ /* 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)), glm::vec3 p1(tgonobj->num(0), tgonobj->num(1), tgonobj->num(2)),
xw(tgonobj->num(3), tgonobj->num(4), tgonobj->num(5)), xw(tgonobj->num(3), tgonobj->num(4), tgonobj->num(5)),
yh(tgonobj->num(6), tgonobj->num(7), tgonobj->num(8)); 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) { void ContentLoader::loadItem(ItemDef* def, std::string name, fs::path file) {
std::unique_ptr<json::JObject> root(files::read_json(file)); auto root = files::read_json(file);
std::string iconTypeStr = "none"; std::string iconTypeStr = "none";
root->str("icon-type", iconTypeStr); root->str("icon-type", iconTypeStr);
if (iconTypeStr == "none") { if (iconTypeStr == "none") {
@ -236,11 +235,11 @@ void ContentLoader::loadItem(ItemDef* def, std::string name, fs::path file) {
root->str("placing-block", def->placingBlock); root->str("placing-block", def->placingBlock);
// item light emission [r, g, b] where r,g,b in range [0..15] // item light emission [r, g, b] where r,g,b in range [0..15]
json::JArray* emissionobj = root->arr("emission"); auto emissionarr = root->list("emission");
if (emissionobj) { if (emissionarr) {
def->emission[0] = emissionobj->num(0); def->emission[0] = emissionarr->num(0);
def->emission[1] = emissionobj->num(1); def->emission[1] = emissionarr->num(1);
def->emission[2] = emissionobj->num(2); def->emission[2] = emissionarr->num(2);
} }
} }
@ -274,9 +273,8 @@ void ContentLoader::load(ContentBuilder* builder) {
auto folder = pack->folder; auto folder = pack->folder;
if (!fs::is_regular_file(pack->getContentFile())) if (!fs::is_regular_file(pack->getContentFile()))
return; return;
std::unique_ptr<json::JObject> root (files::read_json(pack->getContentFile())); auto root = files::read_json(pack->getContentFile());
auto blocksarr = root->list("blocks");
json::JArray* blocksarr = root->arr("blocks");
if (blocksarr) { if (blocksarr) {
for (uint i = 0; i < blocksarr->size(); i++) { for (uint i = 0; i < blocksarr->size(); i++) {
std::string name = blocksarr->str(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) { if (itemsarr) {
for (uint i = 0; i < itemsarr->size(); i++) { for (uint i = 0; i < itemsarr->size(); i++) {
std::string name = itemsarr->str(i); std::string name = itemsarr->str(i);

View File

@ -4,30 +4,32 @@
#include <string> #include <string>
#include <filesystem> #include <filesystem>
namespace fs = std::filesystem;
class Block; class Block;
class ItemDef; class ItemDef;
class ContentPack; class ContentPack;
class ContentBuilder; class ContentBuilder;
namespace json { namespace dynamic {
class JObject; class Map;
} }
class ContentLoader { class ContentLoader {
const ContentPack* pack; const ContentPack* pack;
void loadBlock(Block* def, std::string full, std::string name); 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); void loadItem(ItemDef* def, std::string full, std::string name);
public: public:
ContentLoader(ContentPack* pack); ContentLoader(ContentPack* pack);
bool fixPackIndices(std::filesystem::path folder, bool fixPackIndices(std::filesystem::path folder,
json::JObject* indicesRoot, dynamic::Map* indicesRoot,
std::string contentSection); std::string contentSection);
void fixPackIndices(); void fixPackIndices();
void loadBlock(Block* 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, std::filesystem::path file); void loadItem(ItemDef* def, std::string name, fs::path file);
void load(ContentBuilder* builder); void load(ContentBuilder* builder);
}; };

View File

@ -5,6 +5,7 @@
#include "../coders/json.h" #include "../coders/json.h"
#include "../files/files.h" #include "../files/files.h"
#include "../files/engine_paths.h" #include "../files/engine_paths.h"
#include "../data/dynamic.h"
namespace fs = std::filesystem; namespace fs = std::filesystem;
@ -15,7 +16,7 @@ const fs::path ContentPack::ITEMS_FOLDER = "items";
contentpack_error::contentpack_error( contentpack_error::contentpack_error(
std::string packId, std::string packId,
std::filesystem::path folder, fs::path folder,
std::string message) std::string message)
: std::runtime_error(message), packId(packId), folder(folder) { : std::runtime_error(message), packId(packId), folder(folder) {
} }
@ -23,19 +24,19 @@ contentpack_error::contentpack_error(
std::string contentpack_error::getPackId() const { std::string contentpack_error::getPackId() const {
return packId; return packId;
} }
std::filesystem::path contentpack_error::getFolder() const { fs::path contentpack_error::getFolder() const {
return folder; return folder;
} }
std::filesystem::path ContentPack::getContentFile() const { fs::path ContentPack::getContentFile() const {
return folder/fs::path(CONTENT_FILENAME); 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)); 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)); auto root = files::read_json(folder/fs::path(PACKAGE_FILENAME));
ContentPack pack; ContentPack pack;
root->str("id", pack.id); 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, void ContentPack::readPacks(const EnginePaths* paths,
std::vector<ContentPack>& packs, std::vector<ContentPack>& packs,
const std::vector<std::string>& packnames, const std::vector<std::string>& packnames,
std::filesystem::path worldDir) { fs::path worldDir) {
for (const auto& name : packnames) { for (const auto& name : packnames) {
fs::path packfolder = ContentPack::findPack(paths, worldDir, name); fs::path packfolder = ContentPack::findPack(paths, worldDir, name);
packs.push_back(ContentPack::read(packfolder)); packs.push_back(ContentPack::read(packfolder));

331
src/data/dynamic.cpp Normal file
View File

@ -0,0 +1,331 @@
#include "dynamic.h"
#include <stdexcept>
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<Value>(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<Value>(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<Value>(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<Value>(valtype::boolean, val));
return *this;
}
List& List::put(Map* value) {
valvalue val;
val.map = value;
values.push_back(std::make_unique<Value>(valtype::map, val));
return *this;
}
List& List::put(List* value) {
valvalue val;
val.list = value;
values.push_back(std::make_unique<Value>(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;
}
}

113
src/data/dynamic.h Normal file
View File

@ -0,0 +1,113 @@
#ifndef DATA_DYNAMIC_H_
#define DATA_DYNAMIC_H_
#include <string>
#include <vector>
#include <unordered_map>
#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<std::unique_ptr<Value>> 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<std::string, std::unique_ptr<Value>> 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_

View File

@ -18,6 +18,8 @@
#include "../constants.h" #include "../constants.h"
#include "../items/ItemDef.h" #include "../items/ItemDef.h"
#include "../data/dynamic.h"
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include <cstdint> #include <cstdint>
@ -444,7 +446,7 @@ void WorldFiles::write(const World* world, const Content* content) {
if (generatorTestMode) if (generatorTestMode)
return; return;
writeIndices(content->indices); writeIndices(content->getIndices());
writeRegions(regions, regionsFolder, REGION_LAYER_VOXELS); writeRegions(regions, regionsFolder, REGION_LAYER_VOXELS);
writeRegions(lights, lightsFolder, REGION_LAYER_LIGHTS); writeRegions(lights, lightsFolder, REGION_LAYER_LIGHTS);
} }
@ -460,16 +462,16 @@ void WorldFiles::writePacks(const World* world) {
} }
void WorldFiles::writeIndices(const ContentIndices* indices) { void WorldFiles::writeIndices(const ContentIndices* indices) {
json::JObject root; dynamic::Map root;
uint count; uint count;
json::JArray& blocks = root.putArray("blocks"); auto& blocks = root.putList("blocks");
count = indices->countBlockDefs(); count = indices->countBlockDefs();
for (uint i = 0; i < count; i++) { for (uint i = 0; i < count; i++) {
const Block* def = indices->getBlockDef(i); const Block* def = indices->getBlockDef(i);
blocks.put(def->name); blocks.put(def->name);
} }
json::JArray& items = root.putArray("items"); auto& items = root.putList("items");
count = indices->countItemDefs(); count = indices->countItemDefs();
for (uint i = 0; i < count; i++) { for (uint i = 0; i < count; i++) {
const ItemDef* def = indices->getItemDef(i); const ItemDef* def = indices->getItemDef(i);
@ -480,16 +482,16 @@ void WorldFiles::writeIndices(const ContentIndices* indices) {
} }
void WorldFiles::writeWorldInfo(const World* world) { 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("major", ENGINE_VERSION_MAJOR);
versionobj.put("minor", ENGINE_VERSION_MINOR); versionobj.put("minor", ENGINE_VERSION_MINOR);
root.put("name", world->name); root.put("name", world->name);
root.put("seed", world->seed); 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", world->daytime);
timeobj.put("day-time-speed", world->daytimeSpeed); timeobj.put("day-time-speed", world->daytimeSpeed);
@ -503,11 +505,11 @@ bool WorldFiles::readWorldInfo(World* world) {
return false; return false;
} }
std::unique_ptr<json::JObject> root(files::read_json(file)); auto root = files::read_json(file);
root->str("name", world->name); root->str("name", world->name);
root->num("seed", world->seed); root->num("seed", world->seed);
json::JObject* verobj = root->obj("version"); auto verobj = root->map("version");
if (verobj) { if (verobj) {
int major=0, minor=-1; int major=0, minor=-1;
verobj->num("major", major); verobj->num("major", major);
@ -515,7 +517,7 @@ bool WorldFiles::readWorldInfo(World* world) {
std::cout << "world version: " << major << "." << minor << std::endl; std::cout << "world version: " << major << "." << minor << std::endl;
} }
json::JObject* timeobj = root->obj("time"); auto timeobj = root->map("time");
if (timeobj) { if (timeobj) {
timeobj->num("day-time", world->daytime); timeobj->num("day-time", world->daytime);
timeobj->num("day-time-speed", world->daytimeSpeed); timeobj->num("day-time-speed", world->daytimeSpeed);
@ -526,13 +528,13 @@ bool WorldFiles::readWorldInfo(World* world) {
void WorldFiles::writePlayer(Player* player){ void WorldFiles::writePlayer(Player* player){
glm::vec3 position = player->hitbox->position; glm::vec3 position = player->hitbox->position;
json::JObject root; dynamic::Map root;
json::JArray& posarr = root.putArray("position"); auto& posarr = root.putList("position");
posarr.put(position.x); posarr.put(position.x);
posarr.put(position.y); posarr.put(position.y);
posarr.put(position.z); posarr.put(position.z);
json::JArray& rotarr = root.putArray("rotation"); auto& rotarr = root.putList("rotation");
rotarr.put(player->cam.x); rotarr.put(player->cam.x);
rotarr.put(player->cam.y); rotarr.put(player->cam.y);
@ -549,15 +551,15 @@ bool WorldFiles::readPlayer(Player* player) {
return false; return false;
} }
std::unique_ptr<json::JObject> root(files::read_json(file)); auto root = files::read_json(file);
json::JArray* posarr = root->arr("position"); auto posarr = root->list("position");
glm::vec3& position = player->hitbox->position; glm::vec3& position = player->hitbox->position;
position.x = posarr->num(0); position.x = posarr->num(0);
position.y = posarr->num(1); position.y = posarr->num(1);
position.z = posarr->num(2); position.z = posarr->num(2);
player->camera->position = position; player->camera->position = position;
json::JArray* rotarr = root->arr("rotation"); auto rotarr = root->list("rotation");
player->cam.x = rotarr->num(0); player->cam.x = rotarr->num(0);
player->cam.y = rotarr->num(1); player->cam.y = rotarr->num(1);

View File

@ -34,6 +34,8 @@ class Content;
class ContentIndices; class ContentIndices;
class World; class World;
namespace fs = std::filesystem;
class illegal_region_format : public std::runtime_error { class illegal_region_format : public std::runtime_error {
public: public:
illegal_region_format(const std::string& message) illegal_region_format(const std::string& message)
@ -63,27 +65,24 @@ struct regfile {
files::rafile file; files::rafile file;
int version; int version;
regfile(std::filesystem::path filename); regfile(fs::path filename);
}; };
typedef std::unordered_map<glm::ivec2, std::unique_ptr<WorldRegion>> regionsmap; typedef std::unordered_map<glm::ivec2, std::unique_ptr<WorldRegion>> regionsmap;
class WorldFiles { class WorldFiles {
std::unordered_map<glm::ivec3, std::unique_ptr<regfile>> openRegFiles; std::unordered_map<glm::ivec3, std::unique_ptr<regfile>> openRegFiles;
void writeWorldInfo(const World* world); void writeWorldInfo(const World* world);
std::filesystem::path getLightsFolder() const; fs::path getLightsFolder() const;
std::filesystem::path getRegionFilename(int x, int y) const; fs::path getRegionFilename(int x, int y) const;
std::filesystem::path getPlayerFile() const; fs::path getPlayerFile() const;
std::filesystem::path getWorldFile() const; fs::path getWorldFile() const;
std::filesystem::path getIndicesFile() const; fs::path getIndicesFile() const;
std::filesystem::path getPacksFile() const; fs::path getPacksFile() const;
WorldRegion* getRegion(regionsmap& regions, WorldRegion* getRegion(regionsmap& regions, int x, int z);
int x, int z); WorldRegion* getOrCreateRegion(regionsmap& regions, int x, int z);
WorldRegion* getOrCreateRegion(
regionsmap& regions,
int x, int z);
/* Compress buffer with extrle /* Compress buffer with extrle
@param src source buffer @param src source buffer
@ -94,38 +93,36 @@ class WorldFiles {
/* Decompress buffer with extrle /* Decompress buffer with extrle
@param src compressed buffer @param src compressed buffer
@param srclen length of 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* decompress(const ubyte* src, size_t srclen, size_t dstlen);
ubyte* readChunkData(int x, int y, ubyte* readChunkData(int x, int y,
uint32_t& length, uint32_t& length,
std::filesystem::path folder, fs::path folder,
int layer); int layer);
void fetchChunks(WorldRegion* region, int x, int y, void fetchChunks(WorldRegion* region, int x, int y,
std::filesystem::path folder, int layer); fs::path folder, int layer);
void writeRegions(regionsmap& regions, void writeRegions(regionsmap& regions,
const std::filesystem::path& folder, int layer); const fs::path& folder, int layer);
ubyte* getData(regionsmap& regions, ubyte* getData(regionsmap& regions,
const std::filesystem::path& folder, const fs::path& folder,
int x, int z, int layer); int x, int z, int layer);
regfile* getRegFile(glm::ivec3 coord, regfile* getRegFile(glm::ivec3 coord, const fs::path& folder);
const std::filesystem::path& folder);
public: public:
static bool parseRegionFilename(const std::string& name, int& x, int& y); static bool parseRegionFilename(const std::string& name, int& x, int& y);
std::filesystem::path getRegionsFolder() const; fs::path getRegionsFolder() const;
regionsmap regions; regionsmap regions;
regionsmap lights; regionsmap lights;
std::filesystem::path directory; fs::path directory;
std::unique_ptr<ubyte[]> compressionBuffer; std::unique_ptr<ubyte[]> compressionBuffer;
bool generatorTestMode; bool generatorTestMode;
bool doWriteLights; bool doWriteLights;
WorldFiles(std::filesystem::path directory, const DebugSettings& settings); WorldFiles(fs::path directory, const DebugSettings& settings);
~WorldFiles(); ~WorldFiles();
void put(Chunk* chunk); void put(Chunk* chunk);
@ -142,7 +139,7 @@ public:
void writeRegion(int x, int y, void writeRegion(int x, int y,
WorldRegion* entry, WorldRegion* entry,
std::filesystem::path file, fs::path file,
int layer); int layer);
void writePlayer(Player* player); void writePlayer(Player* player);
/* @param world world info to save (nullable) */ /* @param world world info to save (nullable) */

View File

@ -7,6 +7,7 @@
#include <stdexcept> #include <stdexcept>
#include "../coders/json.h" #include "../coders/json.h"
#include "../util/stringutil.h" #include "../util/stringutil.h"
#include "../data/dynamic.h"
namespace fs = std::filesystem; namespace fs = std::filesystem;
@ -92,18 +93,18 @@ bool files::write_string(fs::path filename, const std::string content) {
return true; 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 // -- binary json tests
//return write_binary_json(fs::path(filename.u8string()+".bin"), obj); //return write_binary_json(fs::path(filename.u8string()+".bin"), obj);
return files::write_string(filename, json::stringify(obj, nice, " ")); 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<ubyte> bytes = json::to_binary(obj); std::vector<ubyte> bytes = json::to_binary(obj);
return files::write_bytes(filename, (const char*)bytes.data(), bytes.size()); return files::write_bytes(filename, (const char*)bytes.data(), bytes.size());
} }
json::JObject* files::read_json(fs::path filename) { std::unique_ptr<dynamic::Map> files::read_json(fs::path filename) {
// binary json tests // binary json tests
// fs::path binfile = fs::path(filename.u8string()+".bin"); // fs::path binfile = fs::path(filename.u8string()+".bin");
// if (fs::is_regular_file(binfile)){ // 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<dynamic::Map> files::read_binary_json(fs::path file) {
size_t size; size_t size;
std::unique_ptr<char[]> bytes (files::read_bytes(file, size)); std::unique_ptr<char[]> bytes (files::read_bytes(file, size));
return json::from_binary((const ubyte*)bytes.get(), size); return std::unique_ptr<dynamic::Map>(json::from_binary((const ubyte*)bytes.get(), size));
} }
std::vector<std::string> files::read_list(fs::path filename) { std::vector<std::string> files::read_list(fs::path filename) {

View File

@ -3,14 +3,15 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
#include <fstream> #include <fstream>
#include <filesystem> #include <filesystem>
#include "../typedefs.h" #include "../typedefs.h"
namespace fs = std::filesystem; namespace fs = std::filesystem;
namespace json { namespace dynamic {
class JObject; class Map;
} }
namespace files { namespace files {
@ -30,14 +31,14 @@ namespace files {
extern bool write_bytes(fs::path, const char* data, size_t size); 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 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_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_json(fs::path filename, const dynamic::Map* obj, bool nice=true);
extern bool write_binary_json(fs::path filename, const json::JObject* obj); extern bool write_binary_json(fs::path filename, const dynamic::Map* obj);
extern bool read(fs::path, char* data, size_t size); extern bool read(fs::path, char* data, size_t size);
extern char* read_bytes(fs::path, size_t& length); extern char* read_bytes(fs::path, size_t& length);
extern std::string read_string(fs::path filename); extern std::string read_string(fs::path filename);
extern json::JObject* read_json(fs::path file); extern std::unique_ptr<dynamic::Map> read_json(fs::path file);
extern json::JObject* read_binary_json(fs::path file); extern std::unique_ptr<dynamic::Map> read_binary_json(fs::path file);
extern std::vector<std::string> read_list(fs::path file); extern std::vector<std::string> read_list(fs::path file);
} }

View File

@ -9,6 +9,8 @@
#include "../coders/toml.h" #include "../coders/toml.h"
#include "../coders/json.h" #include "../coders/json.h"
#include "../data/dynamic.h"
toml::Wrapper* create_wrapper(EngineSettings& settings) { toml::Wrapper* create_wrapper(EngineSettings& settings) {
std::unique_ptr<toml::Wrapper> wrapper (new toml::Wrapper()); std::unique_ptr<toml::Wrapper> wrapper (new toml::Wrapper());
toml::Section& display = wrapper->add("display"); toml::Section& display = wrapper->add("display");
@ -46,28 +48,27 @@ toml::Wrapper* create_wrapper(EngineSettings& settings) {
} }
std::string write_controls() { std::string write_controls() {
json::JObject* obj = new json::JObject(); dynamic::Map obj;
for (auto& entry : Events::bindings) { for (auto& entry : Events::bindings) {
const auto& binding = entry.second; const auto& binding = entry.second;
json::JObject* jentry = new json::JObject(); auto& jentry = obj.putMap(entry.first);
switch (binding.type) { switch (binding.type) {
case inputtype::keyboard: jentry->put("type", "keyboard"); break; case inputtype::keyboard: jentry.put("type", "keyboard"); break;
case inputtype::mouse: jentry->put("type", "mouse"); break; case inputtype::mouse: jentry.put("type", "mouse"); break;
default: throw std::runtime_error("unsupported control type"); default: throw std::runtime_error("unsupported control type");
} }
jentry->put("code", binding.code); jentry.put("code", binding.code);
obj->put(entry.first, jentry);
} }
return json::stringify(obj, true, " "); return json::stringify(&obj, true, " ");
} }
void load_controls(std::string filename, std::string source) { 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) { for (auto& entry : Events::bindings) {
auto& binding = entry.second; auto& binding = entry.second;
json::JObject* jentry = obj->obj(entry.first); auto jentry = obj->map(entry.first);
if (jentry == nullptr) if (jentry == nullptr)
continue; continue;
inputtype type; inputtype type;

View File

@ -8,12 +8,12 @@
#include "../voxels/Block.h" #include "../voxels/Block.h"
ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) { ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) {
const ContentIndices* contentIds = content->indices; auto indices = content->getIndices();
sideregions = new UVRegion[contentIds->countBlockDefs() * 6]; sideregions = new UVRegion[indices->countBlockDefs() * 6];
Atlas* atlas = assets->getAtlas("blocks"); Atlas* atlas = assets->getAtlas("blocks");
for (uint i = 0; i < contentIds->countBlockDefs(); i++) { for (uint i = 0; i < indices->countBlockDefs(); i++) {
Block* def = contentIds->getBlockDef(i); Block* def = indices->getBlockDef(i);
for (uint side = 0; side < 6; side++) { for (uint side = 0; side < 6; side++) {
std::string tex = def->textureFaces[side]; std::string tex = def->textureFaces[side];
if (atlas->has(tex)) { if (atlas->has(tex)) {

View File

@ -22,7 +22,7 @@ InventoryView::InventoryView(
LevelFrontend* frontend, LevelFrontend* frontend,
std::vector<itemid_t> items) std::vector<itemid_t> items)
: content(content), : content(content),
indices(content->indices), indices(content->getIndices()),
items(items), items(items),
frontend(frontend), frontend(frontend),
columns(columns) { columns(columns) {

View File

@ -132,7 +132,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
1.0f+fog*2.0f, 4); 1.0f+fog*2.0f, 4);
const Content* content = level->content; const Content* content = level->content;
const ContentIndices* contentIds = content->indices; auto indices = content->getIndices();
Assets* assets = engine->getAssets(); Assets* assets = engine->getAssets();
Atlas* atlas = assets->getAtlas("blocks"); Atlas* atlas = assets->getAtlas("blocks");
Shader* shader = assets->getShader("main"); Shader* shader = assets->getShader("main");
@ -170,7 +170,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
shader->uniform1i("u_cubemap", 1); shader->uniform1i("u_cubemap", 1);
{ {
itemid_t id = level->player->getChosenItem(); itemid_t id = level->player->getChosenItem();
ItemDef* item = contentIds->getItemDef(id); ItemDef* item = indices->getItemDef(id);
assert(item != nullptr); assert(item != nullptr);
float multiplier = 0.5f; float multiplier = 0.5f;
shader->uniform3f("u_torchlightColor", shader->uniform3f("u_torchlightColor",
@ -189,7 +189,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
// Selected block // Selected block
if (PlayerController::selectedBlockId != -1 && hudVisible){ if (PlayerController::selectedBlockId != -1 && hudVisible){
blockid_t id = PlayerController::selectedBlockId; blockid_t id = PlayerController::selectedBlockId;
Block* block = contentIds->getBlockDef(id); Block* block = indices->getBlockDef(id);
assert(block != nullptr); assert(block != nullptr);
const vec3 pos = PlayerController::selectedBlockPosition; const vec3 pos = PlayerController::selectedBlockPosition;
const vec3 point = PlayerController::selectedPointPosition; const vec3 point = PlayerController::selectedPointPosition;

View File

@ -82,7 +82,7 @@ void HudRenderer::createDebugPanel(Engine* engine) {
})); }));
panel->add(create_label([=](){ panel->add(create_label([=](){
auto player = level->player; auto player = level->player;
auto indices = level->content->indices; auto* indices = level->content->getIndices();
auto def = indices->getBlockDef(player->selectedVoxel.id); auto def = indices->getBlockDef(player->selectedVoxel.id);
std::wstringstream stream; std::wstringstream stream;
stream << std::hex << level->player->selectedVoxel.states; stream << std::hex << level->player->selectedVoxel.states;
@ -171,7 +171,7 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
auto level = frontend->getLevel(); auto level = frontend->getLevel();
auto menu = gui->getMenu(); auto menu = gui->getMenu();
auto content = level->content; auto content = level->content;
auto indices = content->indices; auto indices = content->getIndices();
std::vector<itemid_t> items; std::vector<itemid_t> items;
for (itemid_t id = 1; id < indices->countItemDefs(); id++) { for (itemid_t id = 1; id < indices->countItemDefs(); id++) {

View File

@ -7,22 +7,17 @@
#include "../../content/ContentPack.h" #include "../../content/ContentPack.h"
#include "../../files/files.h" #include "../../files/files.h"
#include "../../util/stringutil.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; namespace fs = std::filesystem;
unique_ptr<langs::Lang> langs::current; std::unique_ptr<langs::Lang> langs::current;
unordered_map<string, langs::LocaleInfo> langs::locales_info; std::unordered_map<std::string, langs::LocaleInfo> 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); auto found = map.find(key);
if (found == map.end()) { if (found == map.end()) {
return key; return key;
@ -30,11 +25,11 @@ const wstring& langs::Lang::get(const wstring& key) const {
return found->second; 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; map[key] = text;
} }
const string& langs::Lang::getId() const { const std::string& langs::Lang::getId() const {
return locale; return locale;
} }
@ -50,16 +45,16 @@ class Reader : public BasicParser {
} }
} }
public: 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) { void read(langs::Lang& lang, std::string prefix) {
skipWhitespace(); skipWhitespace();
while (hasNext()) { while (hasNext()) {
string key = parseString('=', true); std::string key = parseString('=', true);
util::trim(key); util::trim(key);
key = prefix + key; key = prefix + key;
string text = parseString('\n', false); std::string text = parseString('\n', false);
util::trim(text); util::trim(text);
lang.put(util::str2wstr_utf8(key), lang.put(util::str2wstr_utf8(key),
util::str2wstr_utf8(text)); util::str2wstr_utf8(text));
@ -68,22 +63,22 @@ public:
} }
}; };
void langs::loadLocalesInfo(const path& resdir, string& fallback) { void langs::loadLocalesInfo(const fs::path& resdir, std::string& fallback) {
path file = resdir/path(langs::TEXTS_FOLDER)/path("langs.json"); fs::path file = resdir/fs::path(langs::TEXTS_FOLDER)/fs::path("langs.json");
unique_ptr<json::JObject> root (files::read_json(file)); auto root = files::read_json(file);
langs::locales_info.clear(); langs::locales_info.clear();
root->str("fallback", fallback); root->str("fallback", fallback);
auto langs = root->obj("langs"); auto langs = root->map("langs");
if (langs) { if (langs) {
std::cout << "locales "; std::cout << "locales ";
for (auto& entry : langs->map) { for (auto& entry : langs->values) {
auto langInfo = entry.second; auto langInfo = entry.second.get();
string name; std::string name;
if (langInfo->type == json::valtype::object) { if (langInfo->type == dynamic::valtype::map) {
name = langInfo->value.obj->getStr("name", "none"); name = langInfo->value.map->getStr("name", "none");
} else { } else {
continue; 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){ std::string langs::locale_by_envlocale(const std::string& envlocale, const fs::path& resdir){
string fallback = FALLBACK_DEFAULT; std::string fallback = FALLBACK_DEFAULT;
if (locales_info.size() == 0) { if (locales_info.size() == 0) {
loadLocalesInfo(resdir, fallback); 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, void langs::load(const fs::path& resdir,
const string& locale, const std::string& locale,
const vector<ContentPack>& packs, const std::vector<ContentPack>& packs,
Lang& lang) { Lang& lang) {
path filename = path(TEXTS_FOLDER)/path(locale + LANG_FILE_EXT); fs::path filename = fs::path(TEXTS_FOLDER)/fs::path(locale + LANG_FILE_EXT);
path core_file = resdir/filename; fs::path core_file = resdir/filename;
if (fs::is_regular_file(core_file)) { 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 reader(core_file.string(), text);
reader.read(lang, ""); reader.read(lang, "");
} }
for (auto pack : packs) { for (auto pack : packs) {
path file = pack.folder/filename; fs::path file = pack.folder/filename;
if (fs::is_regular_file(file)) { 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 reader(file.string(), text);
reader.read(lang, pack.id+":"); reader.read(lang, pack.id+":");
} }
} }
} }
void langs::load(const path& resdir, void langs::load(const fs::path& resdir,
const string& locale, const std::string& locale,
const string& fallback, const std::string& fallback,
const vector<ContentPack>& packs) { const std::vector<ContentPack>& packs) {
unique_ptr<Lang> lang (new Lang(locale)); auto lang = std::make_unique<Lang>(locale);
load(resdir, fallback, packs, *lang.get()); load(resdir, fallback, packs, *lang.get());
if (locale != fallback) { if (locale != fallback) {
load(resdir, locale, packs, *lang.get()); load(resdir, locale, packs, *lang.get());
@ -149,10 +145,10 @@ void langs::load(const path& resdir,
current.reset(lang.release()); current.reset(lang.release());
} }
void langs::setup(const path& resdir, void langs::setup(const fs::path& resdir,
string locale, std::string locale,
const vector<ContentPack>& packs) { const std::vector<ContentPack>& packs) {
string fallback = langs::FALLBACK_DEFAULT; std::string fallback = langs::FALLBACK_DEFAULT;
langs::loadLocalesInfo(resdir, fallback); langs::loadLocalesInfo(resdir, fallback);
if (langs::locales_info.find(locale) == langs::locales_info.end()) { if (langs::locales_info.find(locale) == langs::locales_info.end()) {
locale = fallback; locale = fallback;
@ -160,13 +156,13 @@ void langs::setup(const path& resdir,
langs::load(resdir, locale, fallback, packs); 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); return current->get(key);
} }
const wstring& langs::get(const wstring& key, const wstring& context) { const std::wstring& langs::get(const std::wstring& key, const std::wstring& context) {
wstring ctxkey = context + L"." + key; std::wstring ctxkey = context + L"." + key;
const wstring& text = current->get(ctxkey); const std::wstring& text = current->get(ctxkey);
if (&ctxkey != &text) { if (&ctxkey != &text) {
return text; return text;
} }

View File

@ -34,7 +34,7 @@ BlocksRenderer::BlocksRenderer(size_t capacity,
vertexBuffer = new float[capacity]; vertexBuffer = new float[capacity];
indexBuffer = new int[capacity]; indexBuffer = new int[capacity];
voxelsBuffer = new VoxelsVolume(CHUNK_W + 2, CHUNK_H, CHUNK_D + 2); voxelsBuffer = new VoxelsVolume(CHUNK_W + 2, CHUNK_H, CHUNK_D + 2);
blockDefsCache = content->indices->getBlockDefs(); blockDefsCache = content->getIndices()->getBlockDefs();
} }
BlocksRenderer::~BlocksRenderer() { BlocksRenderer::~BlocksRenderer() {
@ -44,9 +44,7 @@ BlocksRenderer::~BlocksRenderer() {
} }
/* Basic vertex add method */ /* Basic vertex add method */
void BlocksRenderer::vertex(const vec3& coord, void BlocksRenderer::vertex(const vec3& coord, float u, float v, const vec4& light) {
float u, float v,
const vec4& light) {
vertexBuffer[vertexOffset++] = coord.x; vertexBuffer[vertexOffset++] = coord.x;
vertexBuffer[vertexOffset++] = coord.y; vertexBuffer[vertexOffset++] = coord.y;
vertexBuffer[vertexOffset++] = coord.z; vertexBuffer[vertexOffset++] = coord.z;

View File

@ -15,11 +15,11 @@ using std::shared_ptr;
Lighting::Lighting(const Content* content, Chunks* chunks) Lighting::Lighting(const Content* content, Chunks* chunks)
: content(content), chunks(chunks) { : content(content), chunks(chunks) {
const ContentIndices* contentIds = content->indices; auto indices = content->getIndices();
solverR = new LightSolver(contentIds, chunks, 0); solverR = new LightSolver(indices, chunks, 0);
solverG = new LightSolver(contentIds, chunks, 1); solverG = new LightSolver(indices, chunks, 1);
solverB = new LightSolver(contentIds, chunks, 2); solverB = new LightSolver(indices, chunks, 2);
solverS = new LightSolver(contentIds, chunks, 3); solverS = new LightSolver(indices, chunks, 3);
} }
Lighting::~Lighting(){ Lighting::~Lighting(){
@ -42,7 +42,7 @@ void Lighting::clear(){
} }
void Lighting::prebuildSkyLight(int cx, int cz){ 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); Chunk* chunk = chunks->getChunk(cx, cz);
int highestPoint = 0; int highestPoint = 0;
@ -68,7 +68,7 @@ void Lighting::prebuildSkyLight(int cx, int cz){
} }
void Lighting::buildSkyLight(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); Chunk* chunk = chunks->getChunk(cx, cz);
for (int z = 0; z < CHUNK_D; z++){ 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){ 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); const Chunk* chunk = chunks->getChunk(cx, cz);
for (unsigned int y = 0; y < CHUNK_H; y++){ 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){ 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){ if (id == 0){
solverR->remove(x,y,z); solverR->remove(x,y,z);
solverG->remove(x,y,z); solverG->remove(x,y,z);

View File

@ -69,7 +69,7 @@ void BlocksController::updateBlock(int x, int y, int z) {
voxel* vox = chunks->get(x, y, z); voxel* vox = chunks->get(x, y, z);
if (vox == nullptr) if (vox == nullptr)
return; 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)) { if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
breakBlock(nullptr, def, x, y, z); breakBlock(nullptr, def, x, y, z);
return; return;
@ -89,7 +89,7 @@ void BlocksController::randomTick(int tickid, int parts) {
// timeutil::ScopeLogTimer timer(5000+tickid); // timeutil::ScopeLogTimer timer(5000+tickid);
const int w = chunks->w; const int w = chunks->w;
const int d = chunks->d; 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 z = padding; z < d-padding; z++){
for (uint x = padding; x < w-padding; x++){ for (uint x = padding; x < w-padding; x++){
int index = z * w + x; int index = z * w + x;

View File

@ -204,7 +204,7 @@ void PlayerController::updateControls(float delta){
} }
void PlayerController::updateInteraction(){ void PlayerController::updateInteraction(){
auto contentIds = level->content->indices; auto indices = level->content->getIndices();
Chunks* chunks = level->chunks; Chunks* chunks = level->chunks;
Player* player = level->player; Player* player = level->player;
Lighting* lighting = level->lighting; Lighting* lighting = level->lighting;
@ -239,8 +239,8 @@ void PlayerController::updateInteraction(){
int z = iend.z; int z = iend.z;
uint8_t states = 0; uint8_t states = 0;
ItemDef* item = contentIds->getItemDef(player->getChosenItem()); ItemDef* item = indices->getItemDef(player->getChosenItem());
Block* def = contentIds->getBlockDef(item->rt.placingBlock); Block* def = indices->getBlockDef(item->rt.placingBlock);
if (def && def->rotatable){ if (def && def->rotatable){
const std::string& name = def->rotations.name; const std::string& name = def->rotations.name;
if (name == "pipe") { 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){ if (lclick && target->breakable){
blocksController->breakBlock(player, target, x, y, z); blocksController->breakBlock(player, target, x, y, z);
} }
@ -292,7 +292,7 @@ void PlayerController::updateInteraction(){
} }
vox = chunks->get(x, y, z); vox = chunks->get(x, y, z);
blockid_t chosenBlock = def->rt.id; 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()) if (!level->physics->isBlockInside(x,y,z, player->hitbox.get())
|| !def->obstacle){ || !def->obstacle){
if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) { if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
@ -310,7 +310,7 @@ void PlayerController::updateInteraction(){
} }
} }
if (Events::jactive(BIND_PLAYER_PICK)){ 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); player->setChosenItem(block->rt.pickingItem);
} }
} else { } else {

View File

@ -107,7 +107,8 @@ static const luaL_Reg playerlib [] = {
/* == blocks-related functions == */ /* == blocks-related functions == */
static int l_block_name(lua_State* L) { static int l_block_name(lua_State* L) {
int id = lua_tointeger(L, 1); 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; return 1;
} }
@ -121,7 +122,7 @@ static int l_is_solid_at(lua_State* L) {
} }
static int l_blocks_count(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; return 1;
} }
@ -163,7 +164,7 @@ static int l_get_block_x(lua_State* L) {
if (vox == nullptr) { if (vox == nullptr) {
return lua_pushivec3(L, 1, 0, 0); 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) { if (!def->rotatable) {
return lua_pushivec3(L, 1, 0, 0); return lua_pushivec3(L, 1, 0, 0);
} else { } else {
@ -180,7 +181,7 @@ static int l_get_block_y(lua_State* L) {
if (vox == nullptr) { if (vox == nullptr) {
return lua_pushivec3(L, 0, 1, 0); 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) { if (!def->rotatable) {
return lua_pushivec3(L, 0, 1, 0); return lua_pushivec3(L, 0, 1, 0);
} else { } else {
@ -197,7 +198,7 @@ static int l_get_block_z(lua_State* L) {
if (vox == nullptr) { if (vox == nullptr) {
return lua_pushivec3(L, 0, 0, 1); 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) { if (!def->rotatable) {
return lua_pushivec3(L, 0, 0, 1); return lua_pushivec3(L, 0, 0, 1);
} else { } else {

View File

@ -26,9 +26,6 @@ Player::Player(glm::vec3 position, float speed) :
hitbox(new Hitbox(position, glm::vec3(0.3f,0.9f,0.3f))) { hitbox(new Hitbox(position, glm::vec3(0.3f,0.9f,0.3f))) {
} }
Player::~Player(){
}
void Player::update( void Player::update(
Level* level, Level* level,
PlayerInput& input, PlayerInput& input,

View File

@ -43,7 +43,7 @@ public:
glm::vec2 cam = {}; glm::vec2 cam = {};
Player(glm::vec3 position, float speed); Player(glm::vec3 position, float speed);
~Player(); ~Player() = default;
void teleport(glm::vec3 position); void teleport(glm::vec3 position);
void update(Level* level, PlayerInput& input, float delta); void update(Level* level, PlayerInput& input, float delta);

View File

@ -13,6 +13,8 @@ typedef uint8_t ubyte;
typedef uint32_t itemid_t; typedef uint32_t itemid_t;
typedef uint16_t blockid_t; typedef uint16_t blockid_t;
typedef uint32_t itemcount_t;
typedef uint16_t blockstate_t; typedef uint16_t blockstate_t;
typedef uint16_t light_t; typedef uint16_t light_t;

View File

@ -4,7 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "../constants.h" #include "../constants.h"
struct ChunkFlag{ struct ChunkFlag {
static const int MODIFIED = 0x1; static const int MODIFIED = 0x1;
static const int READY = 0x2; static const int READY = 0x2;
static const int LOADED = 0x4; static const int LOADED = 0x4;
@ -37,7 +37,6 @@ public:
Chunk* clone() const; Chunk* clone() const;
// flags getters/setters below // flags getters/setters below
inline void setFlags(int mask, bool value){ inline void setFlags(int mask, bool value){
if (value) if (value)
flags |= mask; flags |= mask;

View File

@ -22,7 +22,7 @@ Chunks::Chunks(int w, int d,
LevelEvents* events, LevelEvents* events,
const Content* content) const Content* content)
: content(content), : content(content),
contentIds(content->indices), contentIds(content->getIndices()),
chunks(w*d), chunks(w*d),
chunksSecond(w*d), chunksSecond(w*d),
w(w), d(d), ox(ox), oz(oz), w(w), d(d), ox(ox), oz(oz),
@ -32,9 +32,6 @@ Chunks::Chunks(int w, int d,
chunksCount = 0; chunksCount = 0;
} }
Chunks::~Chunks(){
}
voxel* Chunks::get(int x, int y, int z){ voxel* Chunks::get(int x, int y, int z){
x -= ox * CHUNK_W; x -= ox * CHUNK_W;
z -= oz * CHUNK_D; z -= oz * CHUNK_D;

View File

@ -34,7 +34,7 @@ public:
Chunks(int w, int d, int ox, int oz, Chunks(int w, int d, int ox, int oz,
WorldFiles* worldFiles, LevelEvents* events, const Content* content); WorldFiles* worldFiles, LevelEvents* events, const Content* content);
~Chunks(); ~Chunks() = default;
bool putChunk(std::shared_ptr<Chunk> chunk); bool putChunk(std::shared_ptr<Chunk> chunk);

View File

@ -17,9 +17,6 @@
ChunksStorage::ChunksStorage(Level* level) : level(level) { ChunksStorage::ChunksStorage(Level* level) : level(level) {
} }
ChunksStorage::~ChunksStorage() {
}
void ChunksStorage::store(std::shared_ptr<Chunk> chunk) { void ChunksStorage::store(std::shared_ptr<Chunk> chunk) {
chunksMap[glm::ivec2(chunk->x, chunk->z)] = chunk; chunksMap[glm::ivec2(chunk->x, chunk->z)] = chunk;
} }
@ -61,7 +58,7 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
if (data) { if (data) {
chunk->decode(data.get()); chunk->decode(data.get());
chunk->setLoaded(true); chunk->setLoaded(true);
verifyLoadedChunk(level->content->indices, chunk.get()); verifyLoadedChunk(level->content->getIndices(), chunk.get());
} }
light_t* lights = wfile->getLights(chunk->x, chunk->z); light_t* lights = wfile->getLights(chunk->x, chunk->z);
@ -75,7 +72,7 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
// some magic code // some magic code
void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const { void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const {
const Content* content = level->content; const Content* content = level->content;
const ContentIndices* indices = content->indices; auto indices = content->getIndices();
voxel* voxels = volume->getVoxels(); voxel* voxels = volume->getVoxels();
light_t* lights = volume->getLights(); light_t* lights = volume->getLights();
int x = volume->getX(); int x = volume->getX();
@ -115,7 +112,7 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const {
} }
} }
} else { } else {
const std::shared_ptr<Chunk>& chunk = found->second; auto& chunk = found->second;
const voxel* cvoxels = chunk->voxels; const voxel* cvoxels = chunk->voxels;
const light_t* clights = chunk->lightmap->getLights(); const light_t* clights = chunk->lightmap->getLights();
for (int ly = y; ly < y + h; ly++) { for (int ly = y; ly < y + h; ly++) {

View File

@ -18,7 +18,7 @@ class ChunksStorage {
std::unordered_map<glm::ivec2, std::shared_ptr<Chunk>> chunksMap; std::unordered_map<glm::ivec2, std::shared_ptr<Chunk>> chunksMap;
public: public:
ChunksStorage(Level* level); ChunksStorage(Level* level);
virtual ~ChunksStorage(); ~ChunksStorage() = default;
std::shared_ptr<Chunk> get(int x, int z) const; std::shared_ptr<Chunk> get(int x, int z) const;
void store(std::shared_ptr<Chunk> chunk); void store(std::shared_ptr<Chunk> chunk);