Merge branch 'main' of https://github.com/MihailRis/VoxelEngine-Cpp
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
@@ -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_
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
# Binary JSON Format Specification
|
||||||
|
|
||||||
|
Format version: 1.0
|
||||||
|
|
||||||
|
This binary data format is developed for use as binary version of JSON in [VoxelEngine-Cpp](https://github.com/MihailRis/VoxelEngine-Cpp) and not compatible with [BSON](https://bsonspec.org/spec.html) due to elements/entries syntax and type codes differences
|
||||||
|
|
||||||
|
## Basic types
|
||||||
|
|
||||||
|
byteorder: little-endian
|
||||||
|
| Name | Size | Definition |
|
||||||
|
| ------- | ------- | ----------------------- |
|
||||||
|
| byte | 1 byte | 8 bit unsigned integer |
|
||||||
|
| int16 | 2 bytes | 16 bit signed integer |
|
||||||
|
| int32 | 4 bytes | 32 bit signed integer |
|
||||||
|
| int64 | 8 bytes | 64 bit unsigned integer |
|
||||||
|
| uint32 | 4 bytes | 32 bit unsigned integer |
|
||||||
|
| float64 | 8 bytes | 64 bit floating point |
|
||||||
|
|
||||||
|
## Syntax (RFC 5234)
|
||||||
|
|
||||||
|
```bnf
|
||||||
|
file = %x01 document / cdocument file content
|
||||||
|
document = uint32 (*entry) %x00 uint32 stores bytes number
|
||||||
|
of the encoded document
|
||||||
|
including the uint32 size
|
||||||
|
entry = cstring value
|
||||||
|
value = %x01 document
|
||||||
|
/ %x02 (*value) %x00 list of values
|
||||||
|
/ %x03 byte 8 bit integer
|
||||||
|
/ %x04 int16 16 bit integer
|
||||||
|
/ %x05 int32 32 bit integer
|
||||||
|
/ %x06 int64 64 bit integer
|
||||||
|
/ %x07 float64 number
|
||||||
|
/ %x08 string utf-8 encoded string
|
||||||
|
/ %x09 uint32 (*byte) bytes array
|
||||||
|
/ %x0A boolean 'false'
|
||||||
|
/ %x0B boolean 'true'
|
||||||
|
/ %x0C null value
|
||||||
|
cdocument = %x1F %x8B (16*byte) gzip-compressed data:
|
||||||
|
%x01 document
|
||||||
|
cstring = (*%x01-FF) %x00
|
||||||
|
string = uint32 (*byte) uint32 stores number of the
|
||||||
|
encoded string bytes
|
||||||
|
|
||||||
|
float64 = 8byte
|
||||||
|
int64 = 8byte
|
||||||
|
uint32 = 4byte
|
||||||
|
int32 = 4byte
|
||||||
|
int16 = 2byte
|
||||||
|
byte = %x00-FF
|
||||||
|
```
|
||||||
|
|
||||||
|
## VoxelEngine format support
|
||||||
|
|
||||||
|
Current implementation does not support types: bytes array, null, compressed document.
|
||||||
|
|
||||||
|
All unsupported types will be implemented in future.
|
||||||
@@ -0,0 +1,212 @@
|
|||||||
|
#include "byte_utils.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <limits>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
void ByteBuilder::put(ubyte b) {
|
||||||
|
buffer.push_back(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteBuilder::putCStr(const char* str) {
|
||||||
|
size_t size = strlen(str)+1;
|
||||||
|
buffer.reserve(buffer.size() + size);
|
||||||
|
for (size_t i = 0; i < size; i++) {
|
||||||
|
buffer.push_back(str[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteBuilder::put(const std::string& s) {
|
||||||
|
size_t len = s.length();
|
||||||
|
putInt32(len);
|
||||||
|
put((const ubyte*)s.data(), len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteBuilder::put(const ubyte* arr, size_t size) {
|
||||||
|
buffer.reserve(buffer.size() + size);
|
||||||
|
for (size_t i = 0; i < size; i++) {
|
||||||
|
buffer.push_back(arr[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteBuilder::putInt16(int16_t val) {
|
||||||
|
buffer.push_back((char) (val >> 0 & 255));
|
||||||
|
buffer.push_back((char) (val >> 8 & 255));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteBuilder::putInt32(int32_t val) {
|
||||||
|
buffer.reserve(buffer.size() + 4);
|
||||||
|
buffer.push_back((char) (val >> 0 & 255));
|
||||||
|
buffer.push_back((char) (val >> 8 & 255));
|
||||||
|
buffer.push_back((char) (val >> 16 & 255));
|
||||||
|
buffer.push_back((char) (val >> 24 & 255));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteBuilder::putInt64(int64_t val) {
|
||||||
|
buffer.reserve(buffer.size() + 8);
|
||||||
|
buffer.push_back((char) (val >> 0 & 255));
|
||||||
|
buffer.push_back((char) (val >> 8 & 255));
|
||||||
|
buffer.push_back((char) (val >> 16 & 255));
|
||||||
|
buffer.push_back((char) (val >> 24 & 255));
|
||||||
|
buffer.push_back((char) (val >> 32 & 255));
|
||||||
|
buffer.push_back((char) (val >> 40 & 255));
|
||||||
|
buffer.push_back((char) (val >> 48 & 255));
|
||||||
|
buffer.push_back((char) (val >> 56 & 255));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteBuilder::putFloat32(float val) {
|
||||||
|
union {
|
||||||
|
int32_t vali32;
|
||||||
|
float valfloat;
|
||||||
|
} value;
|
||||||
|
value.valfloat = val;
|
||||||
|
putInt32(value.vali32);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteBuilder::putFloat64(double val) {
|
||||||
|
union {
|
||||||
|
int64_t vali64;
|
||||||
|
double valfloat;
|
||||||
|
} value;
|
||||||
|
value.valfloat = val;
|
||||||
|
putInt64(value.vali64);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteBuilder::set(size_t position, ubyte val) {
|
||||||
|
buffer[position] = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteBuilder::setInt16(size_t position, int16_t val) {
|
||||||
|
buffer[position++] = val >> 0 & 255;
|
||||||
|
buffer[position] = val >> 8 & 255;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteBuilder::setInt32(size_t position, int32_t val) {
|
||||||
|
buffer[position++] = val >> 0 & 255;
|
||||||
|
buffer[position++] = val >> 8 & 255;
|
||||||
|
buffer[position++] = val >> 16 & 255;
|
||||||
|
buffer[position] = val >> 24 & 255;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteBuilder::setInt64(size_t position, int64_t val) {
|
||||||
|
buffer[position++] = val >> 0 & 255;
|
||||||
|
buffer[position++] = val >> 8 & 255;
|
||||||
|
buffer[position++] = val >> 16 & 255;
|
||||||
|
buffer[position++] = val >> 24 & 255;
|
||||||
|
|
||||||
|
buffer[position++] = val >> 32 & 255;
|
||||||
|
buffer[position++] = val >> 40 & 255;
|
||||||
|
buffer[position++] = val >> 48 & 255;
|
||||||
|
buffer[position] = val >> 56 & 255;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<ubyte> ByteBuilder::build() {
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteReader::ByteReader(const ubyte* data, size_t size)
|
||||||
|
: data(data), size(size), pos(0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteReader::ByteReader(const ubyte* data)
|
||||||
|
: data(data), size(4), pos(0) {
|
||||||
|
size = getInt32();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteReader::checkMagic(const char* data, size_t size) {
|
||||||
|
if (pos + size >= this->size) {
|
||||||
|
throw std::runtime_error("invalid magic number");
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < size; i++) {
|
||||||
|
if (this->data[pos + i] != (ubyte)data[i]){
|
||||||
|
throw std::runtime_error("invalid magic number");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pos += size;
|
||||||
|
}
|
||||||
|
|
||||||
|
ubyte ByteReader::get() {
|
||||||
|
if (pos == size) {
|
||||||
|
throw std::underflow_error("buffer underflow");
|
||||||
|
}
|
||||||
|
return data[pos++];
|
||||||
|
}
|
||||||
|
|
||||||
|
ubyte ByteReader::peek() {
|
||||||
|
if (pos == size) {
|
||||||
|
throw std::underflow_error("buffer underflow");
|
||||||
|
}
|
||||||
|
return data[pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
int16_t ByteReader::getInt16() {
|
||||||
|
if (pos+2 > size) {
|
||||||
|
throw std::underflow_error("unexpected end");
|
||||||
|
}
|
||||||
|
pos += 2;
|
||||||
|
return (data[pos - 1] << 8) |
|
||||||
|
(data[pos - 2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t ByteReader::getInt32() {
|
||||||
|
if (pos+4 > size) {
|
||||||
|
throw std::underflow_error("unexpected end");
|
||||||
|
}
|
||||||
|
pos += 4;
|
||||||
|
return (data[pos - 1] << 24) |
|
||||||
|
(data[pos - 2] << 16) |
|
||||||
|
(data[pos - 3] << 8) |
|
||||||
|
(data[pos - 4]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t ByteReader::getInt64() {
|
||||||
|
if (pos+8 > size) {
|
||||||
|
throw std::underflow_error("unexpected end");
|
||||||
|
}
|
||||||
|
pos += 8;
|
||||||
|
return ((int64_t)data[pos - 1] << 56) |
|
||||||
|
((int64_t)data[pos - 2] << 48) |
|
||||||
|
((int64_t)data[pos - 3] << 40) |
|
||||||
|
((int64_t)data[pos - 4] << 32) |
|
||||||
|
((int64_t)data[pos - 5] << 24) |
|
||||||
|
((int64_t)data[pos - 6] << 16) |
|
||||||
|
((int64_t)data[pos - 7] << 8) |
|
||||||
|
((int64_t)data[pos - 8]);
|
||||||
|
}
|
||||||
|
|
||||||
|
float ByteReader::getFloat32() {
|
||||||
|
union {
|
||||||
|
int32_t vali32;
|
||||||
|
float valfloat;
|
||||||
|
} value;
|
||||||
|
value.vali32 = getInt32();
|
||||||
|
return value.valfloat;
|
||||||
|
}
|
||||||
|
|
||||||
|
double ByteReader::getFloat64() {
|
||||||
|
union {
|
||||||
|
int64_t vali64;
|
||||||
|
double valfloat;
|
||||||
|
} value;
|
||||||
|
value.vali64 = getInt64();
|
||||||
|
return value.valfloat;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* ByteReader::getCString() {
|
||||||
|
const char* cstr = (const char*)(data+pos);
|
||||||
|
pos += strlen(cstr) + 1;
|
||||||
|
return cstr;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ByteReader::getString() {
|
||||||
|
uint32_t length = (uint32_t)getInt32();
|
||||||
|
if (pos+length > size) {
|
||||||
|
throw std::underflow_error("unexpected end");
|
||||||
|
}
|
||||||
|
pos += length;
|
||||||
|
return std::string((const char*)(data+pos-length), length);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ByteReader::hasNext() const {
|
||||||
|
return pos < size;
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
#ifndef CODERS_BYTE_UTILS_H_
|
||||||
|
#define CODERS_BYTE_UTILS_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include "../typedefs.h"
|
||||||
|
|
||||||
|
/* byteorder: little-endian */
|
||||||
|
class ByteBuilder {
|
||||||
|
std::vector<ubyte> buffer;
|
||||||
|
public:
|
||||||
|
/* Write one byte (8 bit unsigned integer) */
|
||||||
|
void put(ubyte b);
|
||||||
|
/* Write c-string (bytes array terminated with '\00') */
|
||||||
|
void putCStr(const char* str);
|
||||||
|
/* Write signed 16 bit integer */
|
||||||
|
void putInt16(int16_t val);
|
||||||
|
/* Write signed 32 bit integer */
|
||||||
|
void putInt32(int32_t val);
|
||||||
|
/* Write signed 64 bit integer */
|
||||||
|
void putInt64(int64_t val);
|
||||||
|
/* Write 32 bit floating-point number */
|
||||||
|
void putFloat32(float val);
|
||||||
|
/* Write 64 bit floating-point number */
|
||||||
|
void putFloat64(double val);
|
||||||
|
|
||||||
|
/* Write string (uint32 length + bytes) */
|
||||||
|
void put(const std::string& s);
|
||||||
|
/* Write sequence of bytes without any header */
|
||||||
|
void put(const ubyte* arr, size_t size);
|
||||||
|
|
||||||
|
void set(size_t position, ubyte val);
|
||||||
|
void setInt16(size_t position, int16_t val);
|
||||||
|
void setInt32(size_t position, int32_t val);
|
||||||
|
void setInt64(size_t position, int64_t val);
|
||||||
|
|
||||||
|
inline size_t size() const {
|
||||||
|
return buffer.size();
|
||||||
|
}
|
||||||
|
inline const ubyte* data() const {
|
||||||
|
return buffer.data();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<ubyte> build();
|
||||||
|
};
|
||||||
|
|
||||||
|
/* byteorder: little-endian */
|
||||||
|
class ByteReader {
|
||||||
|
const ubyte* data;
|
||||||
|
size_t size;
|
||||||
|
size_t pos;
|
||||||
|
public:
|
||||||
|
ByteReader(const ubyte* data, size_t size);
|
||||||
|
ByteReader(const ubyte* data);
|
||||||
|
|
||||||
|
void checkMagic(const char* data, size_t size);
|
||||||
|
/* Read one byte (unsigned 8 bit integer) */
|
||||||
|
ubyte get();
|
||||||
|
/* Read one byte (unsigned 8 bit integer) without pointer move */
|
||||||
|
ubyte peek();
|
||||||
|
/* Read signed 16 bit integer */
|
||||||
|
int16_t getInt16();
|
||||||
|
/* Read signed 32 bit integer */
|
||||||
|
int32_t getInt32();
|
||||||
|
/* Read signed 64 bit integer */
|
||||||
|
int64_t getInt64();
|
||||||
|
/* Read 32 bit floating-point number */
|
||||||
|
float getFloat32();
|
||||||
|
/* Read 64 bit floating-point number */
|
||||||
|
double getFloat64();
|
||||||
|
const char* getCString();
|
||||||
|
std::string getString();
|
||||||
|
bool hasNext() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // CODERS_BYTE_UTILS_H_
|
||||||
+17
-16
@@ -1,10 +1,9 @@
|
|||||||
#include "commons.h"
|
#include "commons.h"
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
using std::string;
|
|
||||||
|
|
||||||
inline int char2int(int c) {
|
inline int char2int(int c) {
|
||||||
if (c >= '0' && c <= '9') {
|
if (c >= '0' && c <= '9') {
|
||||||
return c - '0';
|
return c - '0';
|
||||||
@@ -26,23 +25,24 @@ inline double power(double base, int64_t power) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
parsing_error::parsing_error(string message,
|
parsing_error::parsing_error(
|
||||||
string filename,
|
std::string message,
|
||||||
string source,
|
std::string filename,
|
||||||
uint pos,
|
std::string source,
|
||||||
uint line,
|
uint pos,
|
||||||
uint linestart)
|
uint line,
|
||||||
|
uint linestart)
|
||||||
: std::runtime_error(message), filename(filename), source(source),
|
: std::runtime_error(message), filename(filename), source(source),
|
||||||
pos(pos), line(line), linestart(linestart) {
|
pos(pos), line(line), linestart(linestart) {
|
||||||
}
|
}
|
||||||
|
|
||||||
string parsing_error::errorLog() const {
|
std::string parsing_error::errorLog() const {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
uint linepos = pos - linestart;
|
uint linepos = pos - linestart;
|
||||||
ss << "parsing error in file '" << filename;
|
ss << "parsing error in file '" << filename;
|
||||||
ss << "' at " << (line+1) << ":" << linepos << ": " << this->what() << "\n";
|
ss << "' at " << (line+1) << ":" << linepos << ": " << this->what() << "\n";
|
||||||
size_t end = source.find("\n", linestart);
|
size_t end = source.find("\n", linestart);
|
||||||
if (end == string::npos) {
|
if (end == std::string::npos) {
|
||||||
end = source.length();
|
end = source.length();
|
||||||
}
|
}
|
||||||
ss << source.substr(linestart, end-linestart) << "\n";
|
ss << source.substr(linestart, end-linestart) << "\n";
|
||||||
@@ -54,7 +54,7 @@ string parsing_error::errorLog() const {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string escape_string(string s) {
|
std::string escape_string(std::string s) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << '"';
|
ss << '"';
|
||||||
for (char c : s) {
|
for (char c : s) {
|
||||||
@@ -124,7 +124,7 @@ char BasicParser::nextChar() {
|
|||||||
void BasicParser::expect(char expected) {
|
void BasicParser::expect(char expected) {
|
||||||
char c = peek();
|
char c = peek();
|
||||||
if (c != expected) {
|
if (c != expected) {
|
||||||
throw error("'"+string({expected})+"' expected");
|
throw error("'"+std::string({expected})+"' expected");
|
||||||
}
|
}
|
||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
@@ -153,7 +153,7 @@ char BasicParser::peek() {
|
|||||||
return source[pos];
|
return source[pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
string BasicParser::parseName() {
|
std::string BasicParser::parseName() {
|
||||||
char c = peek();
|
char c = peek();
|
||||||
if (!is_identifier_start(c)) {
|
if (!is_identifier_start(c)) {
|
||||||
if (c == '"') {
|
if (c == '"') {
|
||||||
@@ -262,7 +262,7 @@ bool BasicParser::parseNumber(int sign, number_u& out) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
string BasicParser::parseString(char quote, bool closeRequired) {
|
std::string BasicParser::parseString(char quote, bool closeRequired) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
while (hasNext()) {
|
while (hasNext()) {
|
||||||
char c = source[pos];
|
char c = source[pos];
|
||||||
@@ -290,7 +290,8 @@ string BasicParser::parseString(char quote, bool closeRequired) {
|
|||||||
case '/': ss << '/'; break;
|
case '/': ss << '/'; break;
|
||||||
case '\n': pos++; continue;
|
case '\n': pos++; continue;
|
||||||
default:
|
default:
|
||||||
throw error("'\\" + string({c}) + "' is an illegal escape");
|
throw error("'\\" + std::string({c}) +
|
||||||
|
"' is an illegal escape");
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -308,4 +309,4 @@ string BasicParser::parseString(char quote, bool closeRequired) {
|
|||||||
|
|
||||||
parsing_error BasicParser::error(std::string message) {
|
parsing_error BasicParser::error(std::string message) {
|
||||||
return parsing_error(message, filename, source, pos, line, linestart);
|
return parsing_error(message, filename, source, pos, line, linestart);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
#include "gzip.h"
|
||||||
|
|
||||||
|
#define ZLIB_CONST
|
||||||
|
#include <zlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <memory>
|
||||||
|
#include "byte_utils.h"
|
||||||
|
|
||||||
|
std::vector<ubyte> gzip::compress(const ubyte* src, size_t size) {
|
||||||
|
size_t buffer_size = 23+size*1.01;
|
||||||
|
std::vector<ubyte> buffer;
|
||||||
|
buffer.resize(buffer_size);
|
||||||
|
|
||||||
|
// zlib struct
|
||||||
|
z_stream defstream {};
|
||||||
|
defstream.zalloc = Z_NULL;
|
||||||
|
defstream.zfree = Z_NULL;
|
||||||
|
defstream.opaque = Z_NULL;
|
||||||
|
defstream.avail_in = size;
|
||||||
|
defstream.next_in = src;
|
||||||
|
defstream.avail_out = buffer_size;
|
||||||
|
defstream.next_out = buffer.data();
|
||||||
|
|
||||||
|
// compression
|
||||||
|
deflateInit2(&defstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
|
||||||
|
16 + MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
|
||||||
|
deflate(&defstream, Z_FINISH);
|
||||||
|
deflateEnd(&defstream);
|
||||||
|
|
||||||
|
size_t compressed_size = defstream.next_out - buffer.data();
|
||||||
|
buffer.resize(compressed_size);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<ubyte> gzip::decompress(const ubyte* src, size_t size) {
|
||||||
|
// getting uncompressed data length from gzip footer
|
||||||
|
size_t decompressed_size = *(uint32_t*)(src+size-4);
|
||||||
|
std::vector<ubyte> buffer;
|
||||||
|
buffer.resize(decompressed_size);
|
||||||
|
|
||||||
|
// zlib struct
|
||||||
|
z_stream infstream;
|
||||||
|
infstream.zalloc = Z_NULL;
|
||||||
|
infstream.zfree = Z_NULL;
|
||||||
|
infstream.opaque = Z_NULL;
|
||||||
|
infstream.avail_in = size;
|
||||||
|
infstream.next_in = src;
|
||||||
|
infstream.avail_out = decompressed_size;
|
||||||
|
infstream.next_out = buffer.data();
|
||||||
|
|
||||||
|
inflateInit2(&infstream, 16+MAX_WBITS);
|
||||||
|
inflate(&infstream, Z_NO_FLUSH);
|
||||||
|
inflateEnd(&infstream);
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef CODERS_GZIP_H_
|
||||||
|
#define CODERS_GZIP_H_
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "../typedefs.h"
|
||||||
|
|
||||||
|
namespace gzip {
|
||||||
|
std::vector<ubyte> compress(const ubyte* src, size_t size);
|
||||||
|
std::vector<ubyte> decompress(const ubyte* src, size_t size);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // CODERS_GZIP_H_
|
||||||
+59
-391
@@ -6,17 +6,14 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "commons.h"
|
#include "commons.h"
|
||||||
|
#include "../data/dynamic.h"
|
||||||
|
|
||||||
using namespace json;
|
using namespace json;
|
||||||
|
using namespace dynamic;
|
||||||
|
|
||||||
using std::string;
|
inline void newline(std::stringstream& ss,
|
||||||
using std::vector;
|
bool nice, uint indent,
|
||||||
using std::unique_ptr;
|
const std::string& indentstr) {
|
||||||
using std::unordered_map;
|
|
||||||
using std::stringstream;
|
|
||||||
using std::make_pair;
|
|
||||||
|
|
||||||
inline void newline(stringstream& ss, bool nice, uint indent, const string indentstr) {
|
|
||||||
if (nice) {
|
if (nice) {
|
||||||
ss << "\n";
|
ss << "\n";
|
||||||
for (uint i = 0; i < indent; i++) {
|
for (uint i = 0; i < indent; i++) {
|
||||||
@@ -27,41 +24,40 @@ inline void newline(stringstream& ss, bool nice, uint indent, const string inden
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void stringify(Value* value,
|
void stringify(const Value* value,
|
||||||
stringstream& ss,
|
std::stringstream& ss,
|
||||||
int indent,
|
int indent,
|
||||||
string indentstr,
|
const std::string& indentstr,
|
||||||
bool nice);
|
bool nice);
|
||||||
|
|
||||||
void stringifyObj(JObject* obj,
|
void stringifyObj(const Map* obj,
|
||||||
stringstream& ss,
|
std::stringstream& ss,
|
||||||
int indent,
|
int indent,
|
||||||
string indentstr,
|
const std::string& indentstr,
|
||||||
bool nice);
|
bool nice);
|
||||||
|
|
||||||
#include <iostream>
|
void stringify(const Value* value,
|
||||||
void stringify(Value* value,
|
std::stringstream& ss,
|
||||||
stringstream& ss,
|
|
||||||
int indent,
|
int indent,
|
||||||
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) {
|
||||||
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 << ',';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,23 +77,27 @@ void stringify(Value* value,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void stringifyObj(JObject* obj, stringstream& ss, int indent, string indentstr, bool nice) {
|
void stringifyObj(const Map* obj,
|
||||||
if (obj->map.empty()) {
|
std::stringstream& ss,
|
||||||
|
int indent,
|
||||||
|
const std::string& indentstr,
|
||||||
|
bool nice) {
|
||||||
|
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 << ',';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -107,352 +107,20 @@ void stringifyObj(JObject* obj, stringstream& ss, int indent, string indentstr,
|
|||||||
ss << '}';
|
ss << '}';
|
||||||
}
|
}
|
||||||
|
|
||||||
string json::stringify(JObject* obj, bool nice, string indent) {
|
std::string json::stringify(
|
||||||
stringstream ss;
|
const Map* obj,
|
||||||
|
bool nice,
|
||||||
|
const std::string& indent) {
|
||||||
|
std::stringstream ss;
|
||||||
stringifyObj(obj, ss, 1, indent, nice);
|
stringifyObj(obj, ss, 1, indent, nice);
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Parser::Parser(std::string filename, std::string source)
|
||||||
JArray::~JArray() {
|
: BasicParser(filename, source) {
|
||||||
for (auto value : values) {
|
|
||||||
delete value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string JArray::str(size_t index) const {
|
Map* Parser::parse() {
|
||||||
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(string value) {
|
|
||||||
valvalue val;
|
|
||||||
val.str = new 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(string key, string& dst) const {
|
|
||||||
dst = getStr(key, dst);
|
|
||||||
}
|
|
||||||
|
|
||||||
string JObject::getStr(string key, const 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(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(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(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(string key, uint value) {
|
|
||||||
return put(key, (int64_t)value);
|
|
||||||
}
|
|
||||||
|
|
||||||
JObject& JObject::put(string key, int value) {
|
|
||||||
return put(key, (int64_t)value);
|
|
||||||
}
|
|
||||||
|
|
||||||
JObject& JObject::put(string key, int64_t value) {
|
|
||||||
auto found = map.find(key);
|
|
||||||
if (found != map.end()) found->second;
|
|
||||||
valvalue val;
|
|
||||||
val.integer = value;
|
|
||||||
map.insert(make_pair(key, new Value(valtype::integer, val)));
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
JObject& JObject::put(string key, uint64_t value) {
|
|
||||||
return put(key, (int64_t)value);
|
|
||||||
}
|
|
||||||
|
|
||||||
JObject& JObject::put(string key, float value) {
|
|
||||||
return put(key, (double)value);
|
|
||||||
}
|
|
||||||
|
|
||||||
JObject& JObject::put(string key, double value) {
|
|
||||||
auto found = map.find(key);
|
|
||||||
if (found != map.end()) delete found->second;
|
|
||||||
valvalue val;
|
|
||||||
val.decimal = value;
|
|
||||||
map.insert(make_pair(key, new Value(valtype::number, val)));
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
JObject& JObject::put(string key, string value){
|
|
||||||
auto found = map.find(key);
|
|
||||||
if (found != map.end()) delete found->second;
|
|
||||||
valvalue val;
|
|
||||||
val.str = new string(value);
|
|
||||||
map.insert(make_pair(key, new Value(valtype::string, val)));
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
JObject& JObject::put(std::string key, const char* value) {
|
|
||||||
return put(key, string(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
JObject& JObject::put(string key, JObject* value){
|
|
||||||
auto found = map.find(key);
|
|
||||||
if (found != map.end()) delete found->second;
|
|
||||||
valvalue val;
|
|
||||||
val.obj = value;
|
|
||||||
map.insert(make_pair(key, new Value(valtype::object, val)));
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
JObject& JObject::put(string key, JArray* value){
|
|
||||||
auto found = map.find(key);
|
|
||||||
if (found != map.end()) delete found->second;
|
|
||||||
valvalue val;
|
|
||||||
val.arr = value;
|
|
||||||
map.insert(make_pair(key, new Value(valtype::array, val)));
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
JObject& JObject::put(string key, bool value){
|
|
||||||
auto found = map.find(key);
|
|
||||||
if (found != map.end()) delete found->second;
|
|
||||||
valvalue val;
|
|
||||||
val.boolean = value;
|
|
||||||
map.insert(make_pair(key, new Value(valtype::boolean, val)));
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
JArray& JObject::putArray(string key) {
|
|
||||||
JArray* arr = new JArray();
|
|
||||||
put(key, arr);
|
|
||||||
return *arr;
|
|
||||||
}
|
|
||||||
|
|
||||||
JObject& JObject::putObj(string key) {
|
|
||||||
JObject* obj = new JObject();
|
|
||||||
put(key, obj);
|
|
||||||
return *obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool JObject::has(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(string filename, string source) : BasicParser(filename, source) {
|
|
||||||
}
|
|
||||||
|
|
||||||
JObject* Parser::parse() {
|
|
||||||
char next = peek();
|
char next = peek();
|
||||||
if (next != '{') {
|
if (next != '{') {
|
||||||
throw error("'{' expected");
|
throw error("'{' expected");
|
||||||
@@ -460,22 +128,22 @@ JObject* Parser::parse() {
|
|||||||
return parseObject();
|
return parseObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
JObject* Parser::parseObject() {
|
Map* Parser::parseObject() {
|
||||||
expect('{');
|
expect('{');
|
||||||
unique_ptr<JObject> obj(new JObject());
|
auto obj = std::make_unique<Map>();
|
||||||
unordered_map<string, Value*>& map = obj->map;
|
auto& map = obj->values;
|
||||||
while (peek() != '}') {
|
while (peek() != '}') {
|
||||||
if (peek() == '#') {
|
if (peek() == '#') {
|
||||||
skipLine();
|
skipLine();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
string key = parseName();
|
std::string key = parseName();
|
||||||
char next = peek();
|
char next = peek();
|
||||||
if (next != ':') {
|
if (next != ':') {
|
||||||
throw error("':' expected");
|
throw error("':' expected");
|
||||||
}
|
}
|
||||||
pos++;
|
pos++;
|
||||||
map.insert(make_pair(key, parseValue()));
|
map.insert(std::make_pair(key, parseValue()));
|
||||||
next = peek();
|
next = peek();
|
||||||
if (next == ',') {
|
if (next == ',') {
|
||||||
pos++;
|
pos++;
|
||||||
@@ -489,16 +157,16 @@ JObject* Parser::parseObject() {
|
|||||||
return obj.release();
|
return obj.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
JArray* Parser::parseArray() {
|
List* Parser::parseList() {
|
||||||
expect('[');
|
expect('[');
|
||||||
unique_ptr<JArray> arr(new JArray());
|
auto arr = std::make_unique<List>();
|
||||||
vector<Value*>& 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 == ',') {
|
||||||
@@ -515,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;
|
||||||
@@ -530,7 +198,7 @@ Value* Parser::parseValue() {
|
|||||||
return new Value(type, val);
|
return new Value(type, val);
|
||||||
}
|
}
|
||||||
if (is_identifier_start(next)) {
|
if (is_identifier_start(next)) {
|
||||||
string literal = parseName();
|
std::string literal = parseName();
|
||||||
if (literal == "true") {
|
if (literal == "true") {
|
||||||
val.boolean = true;
|
val.boolean = true;
|
||||||
return new Value(valtype::boolean, val);
|
return new Value(valtype::boolean, val);
|
||||||
@@ -547,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;
|
||||||
@@ -568,17 +236,17 @@ Value* Parser::parseValue() {
|
|||||||
}
|
}
|
||||||
if (next == '"' || next == '\'') {
|
if (next == '"' || next == '\'') {
|
||||||
pos++;
|
pos++;
|
||||||
val.str = new string(parseString(next));
|
val.str = new std::string(parseString(next));
|
||||||
return new Value(valtype::string, val);
|
return new Value(valtype::string, val);
|
||||||
}
|
}
|
||||||
throw error("unexpected character '"+string({next})+"'");
|
throw error("unexpected character '"+std::string({next})+"'");
|
||||||
}
|
}
|
||||||
|
|
||||||
JObject* json::parse(string filename, string source) {
|
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(string source) {
|
std::unique_ptr<Map> json::parse(std::string source) {
|
||||||
return parse("<string>", source);
|
return parse("<string>", source);
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-105
@@ -8,120 +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(JObject* obj, bool nice, std::string indent);
|
|
||||||
|
|
||||||
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_
|
||||||
@@ -131,7 +131,6 @@ Content::Content(ContentIndices* indices, DrawGroups* drawGroups,
|
|||||||
}
|
}
|
||||||
|
|
||||||
Content::~Content() {
|
Content::~Content() {
|
||||||
delete indices;
|
|
||||||
delete drawGroups;
|
delete drawGroups;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <set>
|
#include <set>
|
||||||
@@ -92,14 +93,18 @@ 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,
|
||||||
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);
|
||||||
~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;
|
||||||
|
|||||||
+13
-18
@@ -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);
|
||||||
}
|
}
|
||||||
@@ -24,25 +19,25 @@ ContentLUT::ContentLUT(size_t blocksCount, const Content* content) {
|
|||||||
blockNames.push_back(indices->getBlockDef(i)->name);
|
blockNames.push_back(indices->getBlockDef(i)->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = indices->countBlockDefs(); i < blocksCount; i++) {
|
for (size_t i = indices->countBlockDefs(); i < blocksCount; i++) {
|
||||||
blockNames.push_back("");
|
blockNames.push_back("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -87,13 +87,13 @@ void ContentLoader::fixPackIndices() {
|
|||||||
if (modified){
|
if (modified){
|
||||||
// rewrite modified json
|
// rewrite modified json
|
||||||
std::cout << indexFile << std::endl;
|
std::cout << indexFile << std::endl;
|
||||||
files::write_string(indexFile, json::stringify(root.get(), true, " "));
|
files::write_json(indexFile, root.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
||||||
|
|||||||
@@ -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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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));
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
#ifndef DATA_DYNAMIC_H_
|
||||||
|
#define DATA_DYNAMIC_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
#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_
|
||||||
+21
-20
@@ -1,7 +1,6 @@
|
|||||||
#include "WorldFiles.h"
|
#include "WorldFiles.h"
|
||||||
|
|
||||||
#include "rle.h"
|
#include "rle.h"
|
||||||
#include "binary_io.h"
|
|
||||||
#include "../window/Camera.h"
|
#include "../window/Camera.h"
|
||||||
#include "../content/Content.h"
|
#include "../content/Content.h"
|
||||||
#include "../objects/Player.h"
|
#include "../objects/Player.h"
|
||||||
@@ -19,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>
|
||||||
@@ -445,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);
|
||||||
}
|
}
|
||||||
@@ -461,40 +462,40 @@ 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);
|
||||||
items.put(def->name);
|
items.put(def->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
files::write_string(getIndicesFile(), json::stringify(&root, true, " "));
|
files::write_json(getIndicesFile(), &root);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
files::write_string(getWorldFile(), json::stringify(&root, true, " "));
|
files::write_json(getWorldFile(), &root);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WorldFiles::readWorldInfo(World* world) {
|
bool WorldFiles::readWorldInfo(World* world) {
|
||||||
@@ -504,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);
|
||||||
@@ -516,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);
|
||||||
@@ -527,20 +528,20 @@ 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);
|
||||||
|
|
||||||
root.put("flight", player->flight);
|
root.put("flight", player->flight);
|
||||||
root.put("noclip", player->noclip);
|
root.put("noclip", player->noclip);
|
||||||
|
|
||||||
files::write_string(getPlayerFile(), json::stringify(&root, true, " "));
|
files::write_json(getPlayerFile(), &root);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WorldFiles::readPlayer(Player* player) {
|
bool WorldFiles::readPlayer(Player* player) {
|
||||||
@@ -550,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);
|
||||||
|
|
||||||
|
|||||||
+22
-25
@@ -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) */
|
||||||
|
|||||||
@@ -1,165 +0,0 @@
|
|||||||
#include "binary_io.h"
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <limits>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
void BinaryWriter::put(ubyte b) {
|
|
||||||
buffer.push_back(b);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BinaryWriter::putCStr(const char* str) {
|
|
||||||
size_t size = strlen(str)+1;
|
|
||||||
buffer.reserve(buffer.size() + size);
|
|
||||||
for (size_t i = 0; i < size; i++) {
|
|
||||||
buffer.push_back(str[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BinaryWriter::put(const std::string& s) {
|
|
||||||
size_t len = s.length();
|
|
||||||
if (len > INT16_MAX) {
|
|
||||||
throw std::domain_error("length > INT16_MAX");
|
|
||||||
}
|
|
||||||
putInt16(len);
|
|
||||||
put((const ubyte*)s.data(), len);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BinaryWriter::putShortStr(const std::string& s) {
|
|
||||||
size_t len = s.length();
|
|
||||||
if (len > 255) {
|
|
||||||
throw std::domain_error("length > 255");
|
|
||||||
}
|
|
||||||
put(len);
|
|
||||||
put((const ubyte*)s.data(), len);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BinaryWriter::put(const ubyte* arr, size_t size) {
|
|
||||||
buffer.reserve(buffer.size() + size);
|
|
||||||
for (size_t i = 0; i < size; i++) {
|
|
||||||
buffer.push_back(arr[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BinaryWriter::putInt16(int16_t val) {
|
|
||||||
buffer.push_back((char) (val >> 8 & 255));
|
|
||||||
buffer.push_back((char) (val >> 0 & 255));
|
|
||||||
}
|
|
||||||
|
|
||||||
void BinaryWriter::putInt32(int32_t val) {
|
|
||||||
buffer.reserve(buffer.size() + 4);
|
|
||||||
buffer.push_back((char) (val >> 24 & 255));
|
|
||||||
buffer.push_back((char) (val >> 16 & 255));
|
|
||||||
buffer.push_back((char) (val >> 8 & 255));
|
|
||||||
buffer.push_back((char) (val >> 0 & 255));
|
|
||||||
}
|
|
||||||
|
|
||||||
void BinaryWriter::putInt64(int64_t val) {
|
|
||||||
buffer.reserve(buffer.size() + 8);
|
|
||||||
buffer.push_back((char) (val >> 56 & 255));
|
|
||||||
buffer.push_back((char) (val >> 48 & 255));
|
|
||||||
buffer.push_back((char) (val >> 40 & 255));
|
|
||||||
buffer.push_back((char) (val >> 32 & 255));
|
|
||||||
buffer.push_back((char) (val >> 24 & 255));
|
|
||||||
buffer.push_back((char) (val >> 16 & 255));
|
|
||||||
buffer.push_back((char) (val >> 8 & 255));
|
|
||||||
buffer.push_back((char) (val >> 0 & 255));
|
|
||||||
}
|
|
||||||
|
|
||||||
void BinaryWriter::putFloat32(float val) {
|
|
||||||
union {
|
|
||||||
int32_t vali32;
|
|
||||||
float valfloat;
|
|
||||||
} value;
|
|
||||||
value.valfloat = val;
|
|
||||||
putInt32(value.vali32);
|
|
||||||
}
|
|
||||||
|
|
||||||
BinaryReader::BinaryReader(const ubyte* data, size_t size)
|
|
||||||
: data(data), size(size), pos(0) {
|
|
||||||
}
|
|
||||||
|
|
||||||
void BinaryReader::checkMagic(const char* data, size_t size) {
|
|
||||||
if (pos + size >= this->size) {
|
|
||||||
throw std::runtime_error("invalid magic number");
|
|
||||||
}
|
|
||||||
for (size_t i = 0; i < size; i++) {
|
|
||||||
if (this->data[pos + i] != (ubyte)data[i]){
|
|
||||||
throw std::runtime_error("invalid magic number");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pos += size;
|
|
||||||
}
|
|
||||||
|
|
||||||
ubyte BinaryReader::get() {
|
|
||||||
if (pos == size) {
|
|
||||||
throw std::underflow_error("buffer underflow");
|
|
||||||
}
|
|
||||||
return data[pos++];
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t BinaryReader::getInt16() {
|
|
||||||
if (pos+2 > size) {
|
|
||||||
throw std::underflow_error("unexpected end");
|
|
||||||
}
|
|
||||||
pos += 2;
|
|
||||||
return (data[pos - 2] << 8) |
|
|
||||||
(data[pos - 1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t BinaryReader::getInt32() {
|
|
||||||
if (pos+4 > size) {
|
|
||||||
throw std::underflow_error("unexpected end");
|
|
||||||
}
|
|
||||||
pos += 4;
|
|
||||||
return (data[pos - 4] << 24) |
|
|
||||||
(data[pos - 3] << 16) |
|
|
||||||
(data[pos - 2] << 8) |
|
|
||||||
(data[pos - 1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t BinaryReader::getInt64() {
|
|
||||||
if (pos+8 > size) {
|
|
||||||
throw std::underflow_error("unexpected end");
|
|
||||||
}
|
|
||||||
pos += 8;
|
|
||||||
return ((int64_t)data[pos - 8] << 56) |
|
|
||||||
((int64_t)data[pos - 7] << 48) |
|
|
||||||
((int64_t)data[pos - 6] << 40) |
|
|
||||||
((int64_t)data[pos - 5] << 32) |
|
|
||||||
((int64_t)data[pos - 4] << 24) |
|
|
||||||
((int64_t)data[pos - 3] << 16) |
|
|
||||||
((int64_t)data[pos - 2] << 8) |
|
|
||||||
((int64_t)data[pos - 1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
float BinaryReader::getFloat32() {
|
|
||||||
union {
|
|
||||||
int32_t vali32;
|
|
||||||
float valfloat;
|
|
||||||
} value;
|
|
||||||
value.vali32 = getInt32();
|
|
||||||
return value.valfloat;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string BinaryReader::getString() {
|
|
||||||
uint16_t length = (uint16_t)getInt16();
|
|
||||||
if (pos+length > size) {
|
|
||||||
throw std::underflow_error("unexpected end");
|
|
||||||
}
|
|
||||||
pos += length;
|
|
||||||
return std::string((const char*)(data+pos-length), length);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string BinaryReader::getShortString() {
|
|
||||||
ubyte length = get();
|
|
||||||
if (pos+length > size) {
|
|
||||||
throw std::underflow_error("unexpected end");
|
|
||||||
}
|
|
||||||
pos += length;
|
|
||||||
return std::string((const char*)(data+pos-length), length);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BinaryReader::hasNext() const {
|
|
||||||
return pos < size;
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
#ifndef FILES_BINARY_WRITER_H_
|
|
||||||
#define FILES_BINARY_WRITER_H_
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include "../typedefs.h"
|
|
||||||
|
|
||||||
class BinaryWriter {
|
|
||||||
std::vector<ubyte> buffer;
|
|
||||||
public:
|
|
||||||
void put(ubyte b);
|
|
||||||
void putCStr(const char* str);
|
|
||||||
void putInt16(int16_t val);
|
|
||||||
void putInt32(int32_t val);
|
|
||||||
void putInt64(int64_t val);
|
|
||||||
void putFloat32(float val);
|
|
||||||
void put(const std::string& s);
|
|
||||||
void put(const ubyte* arr, size_t size);
|
|
||||||
void putShortStr(const std::string& s);
|
|
||||||
|
|
||||||
inline size_t size() const {
|
|
||||||
return buffer.size();
|
|
||||||
}
|
|
||||||
inline const ubyte* data() const {
|
|
||||||
return buffer.data();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class BinaryReader {
|
|
||||||
const ubyte* data;
|
|
||||||
size_t size;
|
|
||||||
size_t pos;
|
|
||||||
public:
|
|
||||||
BinaryReader(const ubyte* data, size_t size);
|
|
||||||
|
|
||||||
void checkMagic(const char* data, size_t size);
|
|
||||||
ubyte get();
|
|
||||||
int16_t getInt16();
|
|
||||||
int32_t getInt32();
|
|
||||||
int64_t getInt64();
|
|
||||||
float getFloat32();
|
|
||||||
std::string getString();
|
|
||||||
std::string getShortString();
|
|
||||||
bool hasNext() const;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // FILES_BINARY_WRITER_H_
|
|
||||||
+30
-4
@@ -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,16 +93,41 @@ bool files::write_string(fs::path filename, const std::string content) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
json::JObject* files::read_json(fs::path file) {
|
bool files::write_json(fs::path filename, const dynamic::Map* obj, bool nice) {
|
||||||
std::string text = files::read_string(file);
|
// -- binary json tests
|
||||||
|
//return write_binary_json(fs::path(filename.u8string()+".bin"), obj);
|
||||||
|
return files::write_string(filename, json::stringify(obj, nice, " "));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool files::write_binary_json(fs::path filename, const dynamic::Map* obj) {
|
||||||
|
std::vector<ubyte> bytes = json::to_binary(obj);
|
||||||
|
return files::write_bytes(filename, (const char*)bytes.data(), bytes.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<dynamic::Map> files::read_json(fs::path filename) {
|
||||||
|
// binary json tests
|
||||||
|
// fs::path binfile = fs::path(filename.u8string()+".bin");
|
||||||
|
// if (fs::is_regular_file(binfile)){
|
||||||
|
// return read_binary_json(binfile);
|
||||||
|
// }
|
||||||
|
|
||||||
|
std::string text = files::read_string(filename);
|
||||||
try {
|
try {
|
||||||
return json::parse(file.string(), text);
|
auto obj = json::parse(filename.string(), text);
|
||||||
|
//write_binary_json(binfile, obj);
|
||||||
|
return obj;
|
||||||
} catch (const parsing_error& error) {
|
} catch (const parsing_error& error) {
|
||||||
std::cerr << error.errorLog() << std::endl;
|
std::cerr << error.errorLog() << std::endl;
|
||||||
throw std::runtime_error("could not to parse "+file.string());
|
throw std::runtime_error("could not to parse "+filename.string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<dynamic::Map> files::read_binary_json(fs::path file) {
|
||||||
|
size_t size;
|
||||||
|
std::unique_ptr<char[]> bytes (files::read_bytes(file, 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) {
|
||||||
std::ifstream file(filename);
|
std::ifstream file(filename);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
|
|||||||
+17
-10
@@ -3,12 +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 json {
|
namespace fs = std::filesystem;
|
||||||
class JObject;
|
|
||||||
|
namespace dynamic {
|
||||||
|
class Map;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace files {
|
namespace files {
|
||||||
@@ -25,14 +28,18 @@ namespace files {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
extern bool write_bytes(std::filesystem::path, const char* data, size_t size);
|
extern bool write_bytes(fs::path, const char* data, size_t size);
|
||||||
extern uint append_bytes(std::filesystem::path, const char* data, size_t size);
|
extern uint append_bytes(fs::path, const char* data, size_t size);
|
||||||
extern bool read(std::filesystem::path, char* data, size_t size);
|
extern bool write_string(fs::path filename, const std::string content);
|
||||||
extern char* read_bytes(std::filesystem::path, size_t& length);
|
extern bool write_json(fs::path filename, const dynamic::Map* obj, bool nice=true);
|
||||||
extern std::string read_string(std::filesystem::path filename);
|
extern bool write_binary_json(fs::path filename, const dynamic::Map* obj);
|
||||||
extern bool write_string(std::filesystem::path filename, const std::string content);
|
|
||||||
extern json::JObject* read_json(std::filesystem::path file);
|
extern bool read(fs::path, char* data, size_t size);
|
||||||
extern std::vector<std::string> read_list(std::filesystem::path file);
|
extern char* read_bytes(fs::path, size_t& length);
|
||||||
|
extern std::string read_string(fs::path filename);
|
||||||
|
extern std::unique_ptr<dynamic::Map> read_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);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* FILES_FILES_H_ */
|
#endif /* FILES_FILES_H_ */
|
||||||
@@ -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;
|
||||||
|
|||||||
@@ -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)) {
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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++) {
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,9 +96,8 @@ LevelScreen::LevelScreen(Engine* engine, Level* level)
|
|||||||
|
|
||||||
LevelScreen::~LevelScreen() {
|
LevelScreen::~LevelScreen() {
|
||||||
std::cout << "-- writing world" << std::endl;
|
std::cout << "-- writing world" << std::endl;
|
||||||
World* world = level->world;
|
auto world = level->getWorld();
|
||||||
world->write(level.get());
|
world->write(level.get());
|
||||||
delete world;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LevelScreen::updateHotkeys() {
|
void LevelScreen::updateHotkeys() {
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -14,6 +14,13 @@
|
|||||||
#include "../../lighting/Lighting.h"
|
#include "../../lighting/Lighting.h"
|
||||||
#include "../../logic/BlocksController.h"
|
#include "../../logic/BlocksController.h"
|
||||||
|
|
||||||
|
inline int lua_pushivec3(lua_State* L, int x, int y, int z) {
|
||||||
|
lua_pushinteger(L, x);
|
||||||
|
lua_pushinteger(L, y);
|
||||||
|
lua_pushinteger(L, z);
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
inline void luaL_openlib(lua_State* L, const char* name, const luaL_Reg* libfuncs, int nup) {
|
inline void luaL_openlib(lua_State* L, const char* name, const luaL_Reg* libfuncs, int nup) {
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
luaL_setfuncs(L, libfuncs, nup);
|
luaL_setfuncs(L, libfuncs, nup);
|
||||||
@@ -44,11 +51,6 @@ static const luaL_Reg worldlib [] = {
|
|||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
int luaopen_world(lua_State* L) {
|
|
||||||
luaL_openlib(L, "world", worldlib, 0);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* == player library ==*/
|
/* == player library ==*/
|
||||||
static int l_player_get_pos(lua_State* L) {
|
static int l_player_get_pos(lua_State* L) {
|
||||||
int playerid = lua_tointeger(L, 1);
|
int playerid = lua_tointeger(L, 1);
|
||||||
@@ -102,15 +104,11 @@ static const luaL_Reg playerlib [] = {
|
|||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
int luaopen_player(lua_State* L) {
|
|
||||||
luaL_openlib(L, "player", playerlib, 0);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* == 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,13 +156,6 @@ static int l_get_block(lua_State* L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int lua_pushivec3(lua_State* L, int x, int y, int z) {
|
|
||||||
lua_pushinteger(L, x);
|
|
||||||
lua_pushinteger(L, y);
|
|
||||||
lua_pushinteger(L, z);
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int l_get_block_x(lua_State* L) {
|
static int l_get_block_x(lua_State* L) {
|
||||||
int x = lua_tointeger(L, 1);
|
int x = lua_tointeger(L, 1);
|
||||||
int y = lua_tointeger(L, 2);
|
int y = lua_tointeger(L, 2);
|
||||||
@@ -173,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 {
|
||||||
@@ -190,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 {
|
||||||
@@ -207,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 {
|
||||||
@@ -275,8 +266,8 @@ static int l_is_replaceable_at(lua_State* L) {
|
|||||||
lua_setglobal(L, NAME))
|
lua_setglobal(L, NAME))
|
||||||
|
|
||||||
void apilua::create_funcs(lua_State* L) {
|
void apilua::create_funcs(lua_State* L) {
|
||||||
luaopen_world(L);
|
luaL_openlib(L, "world", worldlib, 0);
|
||||||
luaopen_player(L);
|
luaL_openlib(L, "player", playerlib, 0);
|
||||||
|
|
||||||
lua_addfunc(L, l_block_index, "block_index");
|
lua_addfunc(L, l_block_index, "block_index");
|
||||||
lua_addfunc(L, l_block_name, "block_name");
|
lua_addfunc(L, l_block_name, "block_name");
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos){
|
|||||||
voxels[i].states = 0;
|
voxels[i].states = 0;
|
||||||
}
|
}
|
||||||
lightmap = new Lightmap();
|
lightmap = new Lightmap();
|
||||||
renderData.vertices = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Chunk::~Chunk(){
|
Chunk::~Chunk(){
|
||||||
|
|||||||
+8
-15
@@ -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;
|
||||||
@@ -18,11 +18,6 @@ struct voxel;
|
|||||||
class Lightmap;
|
class Lightmap;
|
||||||
class ContentLUT;
|
class ContentLUT;
|
||||||
|
|
||||||
struct RenderData {
|
|
||||||
float* vertices;
|
|
||||||
size_t size;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Chunk {
|
class Chunk {
|
||||||
public:
|
public:
|
||||||
int x, z;
|
int x, z;
|
||||||
@@ -31,7 +26,6 @@ public:
|
|||||||
Lightmap* lightmap;
|
Lightmap* lightmap;
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
int surrounding = 0;
|
int surrounding = 0;
|
||||||
RenderData renderData;
|
|
||||||
|
|
||||||
Chunk(int x, int z);
|
Chunk(int x, int z);
|
||||||
~Chunk();
|
~Chunk();
|
||||||
@@ -43,8 +37,7 @@ public:
|
|||||||
Chunk* clone() const;
|
Chunk* clone() const;
|
||||||
|
|
||||||
// flags getters/setters below
|
// flags getters/setters below
|
||||||
|
inline void setFlags(int mask, bool value){
|
||||||
void SETFLAGS(int mask, bool value){
|
|
||||||
if (value)
|
if (value)
|
||||||
flags |= mask;
|
flags |= mask;
|
||||||
else
|
else
|
||||||
@@ -63,17 +56,17 @@ public:
|
|||||||
|
|
||||||
inline bool isReady() const {return flags & ChunkFlag::READY;}
|
inline bool isReady() const {return flags & ChunkFlag::READY;}
|
||||||
|
|
||||||
inline void setUnsaved(bool newState) {SETFLAGS(ChunkFlag::UNSAVED, newState);}
|
inline void setUnsaved(bool newState) {setFlags(ChunkFlag::UNSAVED, newState);}
|
||||||
|
|
||||||
inline void setModified(bool newState) {SETFLAGS(ChunkFlag::MODIFIED, newState);}
|
inline void setModified(bool newState) {setFlags(ChunkFlag::MODIFIED, newState);}
|
||||||
|
|
||||||
inline void setLoaded(bool newState) {SETFLAGS(ChunkFlag::LOADED, newState);}
|
inline void setLoaded(bool newState) {setFlags(ChunkFlag::LOADED, newState);}
|
||||||
|
|
||||||
inline void setLoadedLights(bool newState) {SETFLAGS(ChunkFlag::LOADED_LIGHTS, newState);}
|
inline void setLoadedLights(bool newState) {setFlags(ChunkFlag::LOADED_LIGHTS, newState);}
|
||||||
|
|
||||||
inline void setLighted(bool newState) {SETFLAGS(ChunkFlag::LIGHTED, newState);}
|
inline void setLighted(bool newState) {setFlags(ChunkFlag::LIGHTED, newState);}
|
||||||
|
|
||||||
inline void setReady(bool newState) {SETFLAGS(ChunkFlag::READY, newState);}
|
inline void setReady(bool newState) {setFlags(ChunkFlag::READY, newState);}
|
||||||
|
|
||||||
ubyte* encode() const;
|
ubyte* encode() const;
|
||||||
bool decode(ubyte* data);
|
bool decode(ubyte* data);
|
||||||
|
|||||||
+28
-46
@@ -16,38 +16,22 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
using glm::vec3;
|
|
||||||
using glm::ivec3;
|
|
||||||
using std::shared_ptr;
|
|
||||||
|
|
||||||
Chunks::Chunks(int w, int d,
|
Chunks::Chunks(int w, int d,
|
||||||
int ox, int oz,
|
int ox, int oz,
|
||||||
WorldFiles* wfile,
|
WorldFiles* wfile,
|
||||||
LevelEvents* events,
|
LevelEvents* events,
|
||||||
const Content* content)
|
const Content* content)
|
||||||
: content(content),
|
: content(content),
|
||||||
contentIds(content->indices),
|
contentIds(content->getIndices()),
|
||||||
|
chunks(w*d),
|
||||||
|
chunksSecond(w*d),
|
||||||
w(w), d(d), ox(ox), oz(oz),
|
w(w), d(d), ox(ox), oz(oz),
|
||||||
worldFiles(wfile),
|
worldFiles(wfile),
|
||||||
events(events) {
|
events(events) {
|
||||||
volume = (size_t)w*(size_t)d;
|
volume = (size_t)w*(size_t)d;
|
||||||
chunks = new shared_ptr<Chunk>[volume];
|
|
||||||
chunksSecond = new shared_ptr<Chunk>[volume];
|
|
||||||
|
|
||||||
for (size_t i = 0; i < volume; i++){
|
|
||||||
chunks[i] = nullptr;
|
|
||||||
}
|
|
||||||
chunksCount = 0;
|
chunksCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Chunks::~Chunks(){
|
|
||||||
for (size_t i = 0; i < volume; i++){
|
|
||||||
chunks[i] = nullptr;
|
|
||||||
}
|
|
||||||
delete[] chunks;
|
|
||||||
delete[] chunksSecond;
|
|
||||||
}
|
|
||||||
|
|
||||||
voxel* Chunks::get(int x, int y, int z){
|
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;
|
||||||
@@ -56,7 +40,7 @@ voxel* Chunks::get(int x, int y, int z){
|
|||||||
int cz = floordiv(z, CHUNK_D);
|
int cz = floordiv(z, CHUNK_D);
|
||||||
if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d)
|
if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
shared_ptr<Chunk> chunk = chunks[cz * w + cx]; // chunks is 2D-array
|
std::shared_ptr<Chunk> chunk = chunks[cz * w + cx];
|
||||||
if (chunk == nullptr)
|
if (chunk == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
int lx = x - cx * CHUNK_W;
|
int lx = x - cx * CHUNK_W;
|
||||||
@@ -117,7 +101,7 @@ ubyte Chunks::getLight(int x, int y, int z, int channel){
|
|||||||
int cz = floordiv(z, CHUNK_D);
|
int cz = floordiv(z, CHUNK_D);
|
||||||
if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d)
|
if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d)
|
||||||
return 0;
|
return 0;
|
||||||
shared_ptr<Chunk> chunk = chunks[(cy * d + cz) * w + cx];
|
auto chunk = chunks[(cy * d + cz) * w + cx];
|
||||||
if (chunk == nullptr)
|
if (chunk == nullptr)
|
||||||
return 0;
|
return 0;
|
||||||
int lx = x - cx * CHUNK_W;
|
int lx = x - cx * CHUNK_W;
|
||||||
@@ -134,7 +118,7 @@ light_t Chunks::getLight(int x, int y, int z){
|
|||||||
int cz = floordiv(z, CHUNK_D);
|
int cz = floordiv(z, CHUNK_D);
|
||||||
if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d)
|
if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d)
|
||||||
return 0;
|
return 0;
|
||||||
shared_ptr<Chunk> chunk = chunks[(cy * d + cz) * w + cx];
|
auto chunk = chunks[(cy * d + cz) * w + cx];
|
||||||
if (chunk == nullptr)
|
if (chunk == nullptr)
|
||||||
return 0;
|
return 0;
|
||||||
int lx = x - cx * CHUNK_W;
|
int lx = x - cx * CHUNK_W;
|
||||||
@@ -197,12 +181,12 @@ void Chunks::set(int x, int y, int z, int id, uint8_t states){
|
|||||||
chunk->setModified(true);
|
chunk->setModified(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
voxel* Chunks::rayCast(vec3 start,
|
voxel* Chunks::rayCast(glm::vec3 start,
|
||||||
vec3 dir,
|
glm::vec3 dir,
|
||||||
float maxDist,
|
float maxDist,
|
||||||
vec3& end,
|
glm::vec3& end,
|
||||||
ivec3& norm,
|
glm::ivec3& norm,
|
||||||
ivec3& iend) {
|
glm::ivec3& iend) {
|
||||||
float px = start.x;
|
float px = start.x;
|
||||||
float py = start.y;
|
float py = start.y;
|
||||||
float pz = start.z;
|
float pz = start.z;
|
||||||
@@ -256,7 +240,7 @@ voxel* Chunks::rayCast(vec3 start,
|
|||||||
scalar_t distance;
|
scalar_t distance;
|
||||||
Ray ray(start, dir);
|
Ray ray(start, dir);
|
||||||
if (ray.intersectAABB(iend, box, maxDist, norm, distance) > RayRelation::None){
|
if (ray.intersectAABB(iend, box, maxDist, norm, distance) > RayRelation::None){
|
||||||
end = start + (dir * vec3(distance));
|
end = start + (dir * glm::vec3(distance));
|
||||||
return voxel;
|
return voxel;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,7 +293,7 @@ voxel* Chunks::rayCast(vec3 start,
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) {
|
glm::vec3 Chunks::rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDist) {
|
||||||
float px = start.x;
|
float px = start.x;
|
||||||
float py = start.y;
|
float py = start.y;
|
||||||
float pz = start.z;
|
float pz = start.z;
|
||||||
@@ -343,7 +327,7 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) {
|
|||||||
|
|
||||||
while (t <= maxDist) {
|
while (t <= maxDist) {
|
||||||
voxel* voxel = get(ix, iy, iz);
|
voxel* voxel = get(ix, iy, iz);
|
||||||
if (voxel == nullptr) { return vec3(px + t * dx, py + t * dy, pz + t * dz); }
|
if (voxel == nullptr) { return glm::vec3(px + t * dx, py + t * dy, pz + t * dz); }
|
||||||
|
|
||||||
const Block* def = contentIds->getBlockDef(voxel->id);
|
const Block* def = contentIds->getBlockDef(voxel->id);
|
||||||
if (def->obstacle) {
|
if (def->obstacle) {
|
||||||
@@ -352,15 +336,15 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) {
|
|||||||
? def->rt.hitboxes[voxel->rotation()]
|
? def->rt.hitboxes[voxel->rotation()]
|
||||||
: def->hitbox;
|
: def->hitbox;
|
||||||
scalar_t distance;
|
scalar_t distance;
|
||||||
ivec3 norm;
|
glm::ivec3 norm;
|
||||||
Ray ray(start, dir);
|
Ray ray(start, dir);
|
||||||
// norm is dummy now, can be inefficient
|
// norm is dummy now, can be inefficient
|
||||||
if (ray.intersectAABB(ivec3(ix, iy, iz), box, maxDist, norm, distance) > RayRelation::None) {
|
if (ray.intersectAABB(glm::ivec3(ix, iy, iz), box, maxDist, norm, distance) > RayRelation::None) {
|
||||||
return start + (dir * vec3(distance));
|
return start + (dir * glm::vec3(distance));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return vec3(px + t * dx, py + t * dy, pz + t * dz);
|
return glm::vec3(px + t * dx, py + t * dy, pz + t * dz);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (txMax < tyMax) {
|
if (txMax < tyMax) {
|
||||||
@@ -388,7 +372,7 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return vec3(px + maxDist * dx, py + maxDist * dy, pz + maxDist * dz);
|
return glm::vec3(px + maxDist * dx, py + maxDist * dy, pz + maxDist * dz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -408,7 +392,7 @@ void Chunks::translate(int dx, int dz){
|
|||||||
}
|
}
|
||||||
for (int z = 0; z < d; z++){
|
for (int z = 0; z < d; z++){
|
||||||
for (int x = 0; x < w; x++){
|
for (int x = 0; x < w; x++){
|
||||||
shared_ptr<Chunk> chunk = chunks[z * w + x];
|
auto chunk = chunks[z * w + x];
|
||||||
int nx = x - dx;
|
int nx = x - dx;
|
||||||
int nz = z - dz;
|
int nz = z - dz;
|
||||||
if (chunk == nullptr)
|
if (chunk == nullptr)
|
||||||
@@ -443,20 +427,18 @@ void Chunks::resize(int newW, int newD) {
|
|||||||
translate(0, delta);
|
translate(0, delta);
|
||||||
}
|
}
|
||||||
const int newVolume = newW * newD;
|
const int newVolume = newW * newD;
|
||||||
auto newChunks = new shared_ptr<Chunk>[newVolume] {};
|
std::vector<std::shared_ptr<Chunk>> newChunks(newVolume);
|
||||||
auto newChunksSecond = new shared_ptr<Chunk>[newVolume] {};
|
std::vector<std::shared_ptr<Chunk>> newChunksSecond(newVolume);
|
||||||
for (int z = 0; z < d && z < newD; z++) {
|
for (int z = 0; z < d && z < newD; z++) {
|
||||||
for (int x = 0; x < w && x < newW; x++) {
|
for (int x = 0; x < w && x < newW; x++) {
|
||||||
newChunks[z * newW + x] = chunks[z * w + x];
|
newChunks[z * newW + x] = chunks[z * w + x];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete[] chunks;
|
w = newW;
|
||||||
delete[] chunksSecond;
|
d = newD;
|
||||||
w = newW;
|
volume = newVolume;
|
||||||
d = newD;
|
chunks = std::move(newChunks);
|
||||||
volume = newVolume;
|
chunksSecond = std::move(newChunksSecond);
|
||||||
chunks = newChunks;
|
|
||||||
chunksSecond = newChunksSecond;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunks::_setOffset(int x, int z){
|
void Chunks::_setOffset(int x, int z){
|
||||||
@@ -464,7 +446,7 @@ void Chunks::_setOffset(int x, int z){
|
|||||||
oz = z;
|
oz = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Chunks::putChunk(shared_ptr<Chunk> chunk) {
|
bool Chunks::putChunk(std::shared_ptr<Chunk> chunk) {
|
||||||
int x = chunk->x;
|
int x = chunk->x;
|
||||||
int z = chunk->z;
|
int z = chunk->z;
|
||||||
x -= ox;
|
x -= ox;
|
||||||
|
|||||||
+4
-3
@@ -2,6 +2,7 @@
|
|||||||
#define VOXELS_CHUNKS_H_
|
#define VOXELS_CHUNKS_H_
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
@@ -21,8 +22,8 @@ class Chunks {
|
|||||||
const Content* const content;
|
const Content* const content;
|
||||||
const ContentIndices* const contentIds;
|
const ContentIndices* const contentIds;
|
||||||
public:
|
public:
|
||||||
std::shared_ptr<Chunk>* chunks;
|
std::vector<std::shared_ptr<Chunk>> chunks;
|
||||||
std::shared_ptr<Chunk>* chunksSecond;
|
std::vector<std::shared_ptr<Chunk>> chunksSecond;
|
||||||
size_t volume;
|
size_t volume;
|
||||||
size_t chunksCount;
|
size_t chunksCount;
|
||||||
size_t visible;
|
size_t visible;
|
||||||
@@ -33,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);
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
@@ -39,7 +36,7 @@ void ChunksStorage::remove(int x, int z) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void verifyLoadedChunk(ContentIndices* indices, Chunk* chunk) {
|
static void verifyLoadedChunk(ContentIndices* indices, Chunk* chunk) {
|
||||||
for (size_t i = 0; i < CHUNK_VOL; i++) {
|
for (size_t i = 0; i < CHUNK_VOL; i++) {
|
||||||
blockid_t id = chunk->voxels[i].id;
|
blockid_t id = chunk->voxels[i].id;
|
||||||
if (indices->getBlockDef(id) == nullptr) {
|
if (indices->getBlockDef(id) == nullptr) {
|
||||||
@@ -52,18 +49,19 @@ void verifyLoadedChunk(ContentIndices* indices, Chunk* chunk) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
|
std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
|
||||||
World* world = level->world;
|
World* world = level->getWorld();
|
||||||
|
WorldFiles* wfile = world->wfile;
|
||||||
|
|
||||||
auto chunk = std::make_shared<Chunk>(x, z);
|
auto chunk = std::make_shared<Chunk>(x, z);
|
||||||
store(chunk);
|
store(chunk);
|
||||||
std::unique_ptr<ubyte[]> data(world->wfile->getChunk(chunk->x, chunk->z));
|
std::unique_ptr<ubyte[]> data(wfile->getChunk(chunk->x, chunk->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 = world->wfile->getLights(chunk->x, chunk->z);
|
light_t* lights = wfile->getLights(chunk->x, chunk->z);
|
||||||
if (lights) {
|
if (lights) {
|
||||||
chunk->lightmap->set(lights);
|
chunk->lightmap->set(lights);
|
||||||
chunk->setLoadedLights(true);
|
chunk->setLoadedLights(true);
|
||||||
@@ -74,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();
|
||||||
@@ -114,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++) {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -49,3 +49,7 @@ void Level::update() {
|
|||||||
chunks->resize(matrixSize, matrixSize);
|
chunks->resize(matrixSize, matrixSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
World* Level::getWorld() {
|
||||||
|
return world.get();
|
||||||
|
}
|
||||||
|
|||||||
+5
-1
@@ -1,6 +1,8 @@
|
|||||||
#ifndef WORLD_LEVEL_H_
|
#ifndef WORLD_LEVEL_H_
|
||||||
#define WORLD_LEVEL_H_
|
#define WORLD_LEVEL_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
#include "../settings.h"
|
#include "../settings.h"
|
||||||
|
|
||||||
@@ -15,7 +17,7 @@ class ChunksStorage;
|
|||||||
|
|
||||||
class Level {
|
class Level {
|
||||||
public:
|
public:
|
||||||
World* world;
|
std::unique_ptr<World> world;
|
||||||
const Content* const content;
|
const Content* const content;
|
||||||
Player* player;
|
Player* player;
|
||||||
Chunks* chunks;
|
Chunks* chunks;
|
||||||
@@ -34,6 +36,8 @@ public:
|
|||||||
~Level();
|
~Level();
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
|
World* getWorld();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* WORLD_LEVEL_H_ */
|
#endif /* WORLD_LEVEL_H_ */
|
||||||
|
|||||||
Reference in New Issue
Block a user