Merge branch 'main' of https://github.com/MihailRis/VoxelEngine-Cpp
This commit is contained in:
commit
fa6eb6ae22
159
src/coders/binary_json.cpp
Normal file
159
src/coders/binary_json.cpp
Normal file
@ -0,0 +1,159 @@
|
||||
#include "binary_json.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "byte_utils.h"
|
||||
|
||||
using namespace json;
|
||||
using namespace dynamic;
|
||||
|
||||
static void to_binary(ByteBuilder& builder, const Value* value) {
|
||||
switch (value->type) {
|
||||
case valtype::map: {
|
||||
std::vector<ubyte> bytes = to_binary(value->value.map);
|
||||
builder.put(bytes.data(), bytes.size());
|
||||
break;
|
||||
}
|
||||
case valtype::list:
|
||||
builder.put(BJSON_TYPE_LIST);
|
||||
for (auto& element : value->value.list->values) {
|
||||
to_binary(builder, element.get());
|
||||
}
|
||||
builder.put(BJSON_END);
|
||||
break;
|
||||
case valtype::integer: {
|
||||
int64_t val = value->value.integer;
|
||||
if (val >= 0 && val <= 255) {
|
||||
builder.put(BJSON_TYPE_BYTE);
|
||||
builder.put(val);
|
||||
} else if (val >= INT16_MIN && val <= INT16_MAX){
|
||||
builder.put(BJSON_TYPE_INT16);
|
||||
builder.putInt16(val);
|
||||
} else if (val >= INT32_MIN && val <= INT32_MAX) {
|
||||
builder.put(BJSON_TYPE_INT32);
|
||||
builder.putInt32(val);
|
||||
} else {
|
||||
builder.put(BJSON_TYPE_INT64);
|
||||
builder.putInt64(val);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case valtype::number:
|
||||
builder.put(BJSON_TYPE_NUMBER);
|
||||
builder.putFloat64(value->value.decimal);
|
||||
break;
|
||||
case valtype::boolean:
|
||||
builder.put(BJSON_TYPE_FALSE + value->value.boolean);
|
||||
break;
|
||||
case valtype::string:
|
||||
builder.put(BJSON_TYPE_STRING);
|
||||
builder.put(*value->value.str);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static List* array_from_binary(ByteReader& reader);
|
||||
static Map* object_from_binary(ByteReader& reader);
|
||||
|
||||
std::vector<ubyte> json::to_binary(const Map* obj) {
|
||||
ByteBuilder builder;
|
||||
// type byte
|
||||
builder.put(BJSON_TYPE_DOCUMENT);
|
||||
// document size
|
||||
builder.putInt32(0);
|
||||
|
||||
// writing entries
|
||||
for (auto& entry : obj->values) {
|
||||
builder.putCStr(entry.first.c_str());
|
||||
to_binary(builder, entry.second.get());
|
||||
}
|
||||
// terminating byte
|
||||
builder.put(BJSON_END);
|
||||
|
||||
// updating document size
|
||||
builder.setInt32(1, builder.size());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
static Value* value_from_binary(ByteReader& reader) {
|
||||
ubyte typecode = reader.get();
|
||||
valtype type;
|
||||
valvalue val;
|
||||
switch (typecode) {
|
||||
case BJSON_TYPE_DOCUMENT:
|
||||
type = valtype::map;
|
||||
reader.getInt32();
|
||||
val.map = object_from_binary(reader);
|
||||
break;
|
||||
case BJSON_TYPE_LIST:
|
||||
type = valtype::list;
|
||||
val.list = array_from_binary(reader);
|
||||
break;
|
||||
case BJSON_TYPE_BYTE:
|
||||
type = valtype::integer;
|
||||
val.integer = reader.get();
|
||||
break;
|
||||
case BJSON_TYPE_INT16:
|
||||
type = valtype::integer;
|
||||
val.integer = reader.getInt16();
|
||||
break;
|
||||
case BJSON_TYPE_INT32:
|
||||
type = valtype::integer;
|
||||
val.integer = reader.getInt32();
|
||||
break;
|
||||
case BJSON_TYPE_INT64:
|
||||
type = valtype::integer;
|
||||
val.integer = reader.getInt64();
|
||||
break;
|
||||
case BJSON_TYPE_NUMBER:
|
||||
type = valtype::number;
|
||||
val.decimal = reader.getFloat64();
|
||||
break;
|
||||
case BJSON_TYPE_FALSE:
|
||||
case BJSON_TYPE_TRUE:
|
||||
type = valtype::boolean;
|
||||
val.boolean = typecode - BJSON_TYPE_FALSE;
|
||||
break;
|
||||
case BJSON_TYPE_STRING:
|
||||
type = valtype::string;
|
||||
val.str = new std::string(reader.getString());
|
||||
break;
|
||||
default:
|
||||
throw std::runtime_error(
|
||||
"type "+std::to_string(typecode)+" is not supported");
|
||||
}
|
||||
return new Value(type, val);
|
||||
}
|
||||
|
||||
static List* array_from_binary(ByteReader& reader) {
|
||||
auto array = std::make_unique<List>();
|
||||
auto& items = array->values;
|
||||
while (reader.peek() != BJSON_END) {
|
||||
items.push_back(std::unique_ptr<Value>(value_from_binary(reader)));
|
||||
}
|
||||
reader.get();
|
||||
return array.release();
|
||||
}
|
||||
|
||||
static Map* object_from_binary(ByteReader& reader) {
|
||||
auto obj = std::make_unique<Map>();
|
||||
auto& map = obj->values;
|
||||
while (reader.peek() != BJSON_END) {
|
||||
const char* key = reader.getCString();
|
||||
Value* value = value_from_binary(reader);
|
||||
map.insert(std::make_pair(key, value));
|
||||
}
|
||||
reader.get();
|
||||
return obj.release();
|
||||
}
|
||||
|
||||
std::unique_ptr<Map> json::from_binary(const ubyte* src, size_t size) {
|
||||
ByteReader reader(src, size);
|
||||
std::unique_ptr<Value> value (value_from_binary(reader));
|
||||
if (value->type != valtype::map) {
|
||||
throw std::runtime_error("root value is not an object");
|
||||
}
|
||||
std::unique_ptr<Map> obj (value->value.map);
|
||||
value->value.map = nullptr;
|
||||
return obj;
|
||||
}
|
||||
28
src/coders/binary_json.h
Normal file
28
src/coders/binary_json.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef CODERS_BINARY_JSON_H_
|
||||
#define CODERS_BINARY_JSON_H_
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
namespace json {
|
||||
const int BJSON_END = 0x0;
|
||||
const int BJSON_TYPE_DOCUMENT = 0x1;
|
||||
const int BJSON_TYPE_LIST = 0x2;
|
||||
const int BJSON_TYPE_BYTE = 0x3;
|
||||
const int BJSON_TYPE_INT16 = 0x4;
|
||||
const int BJSON_TYPE_INT32 = 0x5;
|
||||
const int BJSON_TYPE_INT64 = 0x6;
|
||||
const int BJSON_TYPE_NUMBER = 0x7;
|
||||
const int BJSON_TYPE_STRING = 0x8;
|
||||
const int BJSON_TYPE_BYTES = 0x9;
|
||||
const int BJSON_TYPE_FALSE = 0xA;
|
||||
const int BJSON_TYPE_TRUE = 0xB;
|
||||
const int BJSON_TYPE_NULL = 0xC;
|
||||
const int BJSON_TYPE_CDOCUMENT = 0x1F;
|
||||
|
||||
extern std::vector<ubyte> to_binary(const dynamic::Map* obj);
|
||||
extern std::unique_ptr<dynamic::Map> from_binary(const ubyte* src, size_t size);
|
||||
}
|
||||
|
||||
#endif // CODERS_BINARY_JSON_H_
|
||||
57
src/coders/binary_json_spec.md
Normal file
57
src/coders/binary_json_spec.md
Normal file
@ -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.
|
||||
212
src/coders/byte_utils.cpp
Normal file
212
src/coders/byte_utils.cpp
Normal file
@ -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;
|
||||
}
|
||||
76
src/coders/byte_utils.h
Normal file
76
src/coders/byte_utils.h
Normal file
@ -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_
|
||||
@ -1,10 +1,9 @@
|
||||
#include "commons.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <math.h>
|
||||
|
||||
using std::string;
|
||||
|
||||
inline int char2int(int c) {
|
||||
if (c >= '0' && c <= '9') {
|
||||
return c - '0';
|
||||
@ -26,23 +25,24 @@ inline double power(double base, int64_t power) {
|
||||
return result;
|
||||
}
|
||||
|
||||
parsing_error::parsing_error(string message,
|
||||
string filename,
|
||||
string source,
|
||||
uint pos,
|
||||
uint line,
|
||||
uint linestart)
|
||||
parsing_error::parsing_error(
|
||||
std::string message,
|
||||
std::string filename,
|
||||
std::string source,
|
||||
uint pos,
|
||||
uint line,
|
||||
uint linestart)
|
||||
: std::runtime_error(message), filename(filename), source(source),
|
||||
pos(pos), line(line), linestart(linestart) {
|
||||
}
|
||||
|
||||
string parsing_error::errorLog() const {
|
||||
std::string parsing_error::errorLog() const {
|
||||
std::stringstream ss;
|
||||
uint linepos = pos - linestart;
|
||||
ss << "parsing error in file '" << filename;
|
||||
ss << "' at " << (line+1) << ":" << linepos << ": " << this->what() << "\n";
|
||||
size_t end = source.find("\n", linestart);
|
||||
if (end == string::npos) {
|
||||
if (end == std::string::npos) {
|
||||
end = source.length();
|
||||
}
|
||||
ss << source.substr(linestart, end-linestart) << "\n";
|
||||
@ -54,7 +54,7 @@ string parsing_error::errorLog() const {
|
||||
|
||||
}
|
||||
|
||||
string escape_string(string s) {
|
||||
std::string escape_string(std::string s) {
|
||||
std::stringstream ss;
|
||||
ss << '"';
|
||||
for (char c : s) {
|
||||
@ -124,7 +124,7 @@ char BasicParser::nextChar() {
|
||||
void BasicParser::expect(char expected) {
|
||||
char c = peek();
|
||||
if (c != expected) {
|
||||
throw error("'"+string({expected})+"' expected");
|
||||
throw error("'"+std::string({expected})+"' expected");
|
||||
}
|
||||
pos++;
|
||||
}
|
||||
@ -153,7 +153,7 @@ char BasicParser::peek() {
|
||||
return source[pos];
|
||||
}
|
||||
|
||||
string BasicParser::parseName() {
|
||||
std::string BasicParser::parseName() {
|
||||
char c = peek();
|
||||
if (!is_identifier_start(c)) {
|
||||
if (c == '"') {
|
||||
@ -262,7 +262,7 @@ bool BasicParser::parseNumber(int sign, number_u& out) {
|
||||
return true;
|
||||
}
|
||||
|
||||
string BasicParser::parseString(char quote, bool closeRequired) {
|
||||
std::string BasicParser::parseString(char quote, bool closeRequired) {
|
||||
std::stringstream ss;
|
||||
while (hasNext()) {
|
||||
char c = source[pos];
|
||||
@ -290,7 +290,8 @@ string BasicParser::parseString(char quote, bool closeRequired) {
|
||||
case '/': ss << '/'; break;
|
||||
case '\n': pos++; continue;
|
||||
default:
|
||||
throw error("'\\" + string({c}) + "' is an illegal escape");
|
||||
throw error("'\\" + std::string({c}) +
|
||||
"' is an illegal escape");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@ -308,4 +309,4 @@ string BasicParser::parseString(char quote, bool closeRequired) {
|
||||
|
||||
parsing_error BasicParser::error(std::string message) {
|
||||
return parsing_error(message, filename, source, pos, line, linestart);
|
||||
}
|
||||
}
|
||||
|
||||
56
src/coders/gzip.cpp
Normal file
56
src/coders/gzip.cpp
Normal file
@ -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;
|
||||
}
|
||||
12
src/coders/gzip.h
Normal file
12
src/coders/gzip.h
Normal file
@ -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_
|
||||
@ -6,17 +6,14 @@
|
||||
#include <memory>
|
||||
|
||||
#include "commons.h"
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
using namespace json;
|
||||
using namespace dynamic;
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
using std::unique_ptr;
|
||||
using std::unordered_map;
|
||||
using std::stringstream;
|
||||
using std::make_pair;
|
||||
|
||||
inline void newline(stringstream& ss, bool nice, uint indent, const string indentstr) {
|
||||
inline void newline(std::stringstream& ss,
|
||||
bool nice, uint indent,
|
||||
const std::string& indentstr) {
|
||||
if (nice) {
|
||||
ss << "\n";
|
||||
for (uint i = 0; i < indent; i++) {
|
||||
@ -27,41 +24,40 @@ inline void newline(stringstream& ss, bool nice, uint indent, const string inden
|
||||
}
|
||||
}
|
||||
|
||||
void stringify(Value* value,
|
||||
stringstream& ss,
|
||||
void stringify(const Value* value,
|
||||
std::stringstream& ss,
|
||||
int indent,
|
||||
string indentstr,
|
||||
const std::string& indentstr,
|
||||
bool nice);
|
||||
|
||||
void stringifyObj(JObject* obj,
|
||||
stringstream& ss,
|
||||
void stringifyObj(const Map* obj,
|
||||
std::stringstream& ss,
|
||||
int indent,
|
||||
string indentstr,
|
||||
const std::string& indentstr,
|
||||
bool nice);
|
||||
|
||||
#include <iostream>
|
||||
void stringify(Value* value,
|
||||
stringstream& ss,
|
||||
void stringify(const Value* value,
|
||||
std::stringstream& ss,
|
||||
int indent,
|
||||
string indentstr,
|
||||
const std::string& indentstr,
|
||||
bool nice) {
|
||||
if (value->type == valtype::object) {
|
||||
stringifyObj(value->value.obj, ss, indent, indentstr, nice);
|
||||
if (value->type == valtype::map) {
|
||||
stringifyObj(value->value.map, ss, indent, indentstr, nice);
|
||||
}
|
||||
else if (value->type == valtype::array) {
|
||||
vector<Value*>& list = value->value.arr->values;
|
||||
if (list.empty()) {
|
||||
else if (value->type == valtype::list) {
|
||||
auto list = value->value.list;
|
||||
if (list->size() == 0) {
|
||||
ss << "[]";
|
||||
return;
|
||||
}
|
||||
ss << '[';
|
||||
for (uint i = 0; i < list.size(); i++) {
|
||||
Value* value = list[i];
|
||||
for (uint i = 0; i < list->size(); i++) {
|
||||
Value* value = list->get(i);
|
||||
if (i > 0 || nice) {
|
||||
newline(ss, nice, indent, indentstr);
|
||||
}
|
||||
stringify(value, ss, indent+1, indentstr, nice);
|
||||
if (i + 1 < list.size()) {
|
||||
if (i + 1 < list->size()) {
|
||||
ss << ',';
|
||||
}
|
||||
}
|
||||
@ -81,23 +77,27 @@ void stringify(Value* value,
|
||||
}
|
||||
}
|
||||
|
||||
void stringifyObj(JObject* obj, stringstream& ss, int indent, string indentstr, bool nice) {
|
||||
if (obj->map.empty()) {
|
||||
void stringifyObj(const Map* obj,
|
||||
std::stringstream& ss,
|
||||
int indent,
|
||||
const std::string& indentstr,
|
||||
bool nice) {
|
||||
if (obj->values.empty()) {
|
||||
ss << "{}";
|
||||
return;
|
||||
}
|
||||
ss << "{";
|
||||
uint index = 0;
|
||||
for (auto entry : obj->map) {
|
||||
for (auto& entry : obj->values) {
|
||||
const std::string& key = entry.first;
|
||||
if (index > 0 || nice) {
|
||||
newline(ss, nice, indent, indentstr);
|
||||
}
|
||||
Value* value = entry.second;
|
||||
Value* value = entry.second.get();
|
||||
ss << escape_string(key) << ": ";
|
||||
stringify(value, ss, indent+1, indentstr, nice);
|
||||
index++;
|
||||
if (index < obj->map.size()) {
|
||||
if (index < obj->values.size()) {
|
||||
ss << ',';
|
||||
}
|
||||
}
|
||||
@ -107,352 +107,20 @@ void stringifyObj(JObject* obj, stringstream& ss, int indent, string indentstr,
|
||||
ss << '}';
|
||||
}
|
||||
|
||||
string json::stringify(JObject* obj, bool nice, string indent) {
|
||||
stringstream ss;
|
||||
std::string json::stringify(
|
||||
const Map* obj,
|
||||
bool nice,
|
||||
const std::string& indent) {
|
||||
std::stringstream ss;
|
||||
stringifyObj(obj, ss, 1, indent, nice);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
||||
JArray::~JArray() {
|
||||
for (auto value : values) {
|
||||
delete value;
|
||||
}
|
||||
Parser::Parser(std::string filename, std::string source)
|
||||
: BasicParser(filename, source) {
|
||||
}
|
||||
|
||||
std::string JArray::str(size_t index) const {
|
||||
const auto& val = values[index];
|
||||
switch (val->type) {
|
||||
case valtype::string: return *val->value.str;
|
||||
case valtype::boolean: return val->value.boolean ? "true" : "false";
|
||||
case valtype::number: return std::to_string(val->value.decimal);
|
||||
case valtype::integer: return std::to_string(val->value.integer);
|
||||
default:
|
||||
throw std::runtime_error("type error");
|
||||
}
|
||||
}
|
||||
|
||||
double JArray::num(size_t index) const {
|
||||
const auto& val = values[index];
|
||||
switch (val->type) {
|
||||
case valtype::number: return val->value.decimal;
|
||||
case valtype::integer: return val->value.integer;
|
||||
case valtype::string: return std::stoll(*val->value.str);
|
||||
case valtype::boolean: return val->value.boolean;
|
||||
default:
|
||||
throw std::runtime_error("type error");
|
||||
}
|
||||
}
|
||||
|
||||
int64_t JArray::integer(size_t index) const {
|
||||
const auto& val = values[index];
|
||||
switch (val->type) {
|
||||
case valtype::number: return val->value.decimal;
|
||||
case valtype::integer: return val->value.integer;
|
||||
case valtype::string: return std::stoll(*val->value.str);
|
||||
case valtype::boolean: return val->value.boolean;
|
||||
default:
|
||||
throw std::runtime_error("type error");
|
||||
}
|
||||
}
|
||||
|
||||
JObject* JArray::obj(size_t index) const {
|
||||
return values[index]->value.obj;
|
||||
}
|
||||
|
||||
JArray* JArray::arr(size_t index) const {
|
||||
return values[index]->value.arr;
|
||||
}
|
||||
|
||||
bool JArray::flag(size_t index) const {
|
||||
return values[index]->value.boolean;
|
||||
}
|
||||
|
||||
JArray& JArray::put(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() {
|
||||
Map* Parser::parse() {
|
||||
char next = peek();
|
||||
if (next != '{') {
|
||||
throw error("'{' expected");
|
||||
@ -460,22 +128,22 @@ JObject* Parser::parse() {
|
||||
return parseObject();
|
||||
}
|
||||
|
||||
JObject* Parser::parseObject() {
|
||||
Map* Parser::parseObject() {
|
||||
expect('{');
|
||||
unique_ptr<JObject> obj(new JObject());
|
||||
unordered_map<string, Value*>& map = obj->map;
|
||||
auto obj = std::make_unique<Map>();
|
||||
auto& map = obj->values;
|
||||
while (peek() != '}') {
|
||||
if (peek() == '#') {
|
||||
skipLine();
|
||||
continue;
|
||||
}
|
||||
string key = parseName();
|
||||
std::string key = parseName();
|
||||
char next = peek();
|
||||
if (next != ':') {
|
||||
throw error("':' expected");
|
||||
}
|
||||
pos++;
|
||||
map.insert(make_pair(key, parseValue()));
|
||||
map.insert(std::make_pair(key, parseValue()));
|
||||
next = peek();
|
||||
if (next == ',') {
|
||||
pos++;
|
||||
@ -489,16 +157,16 @@ JObject* Parser::parseObject() {
|
||||
return obj.release();
|
||||
}
|
||||
|
||||
JArray* Parser::parseArray() {
|
||||
List* Parser::parseList() {
|
||||
expect('[');
|
||||
unique_ptr<JArray> arr(new JArray());
|
||||
vector<Value*>& values = arr->values;
|
||||
auto arr = std::make_unique<List>();
|
||||
auto& values = arr->values;
|
||||
while (peek() != ']') {
|
||||
if (peek() == '#') {
|
||||
skipLine();
|
||||
continue;
|
||||
}
|
||||
values.push_back(parseValue());
|
||||
values.push_back(std::unique_ptr<Value>(parseValue()));
|
||||
|
||||
char next = peek();
|
||||
if (next == ',') {
|
||||
@ -515,7 +183,7 @@ JArray* Parser::parseArray() {
|
||||
|
||||
Value* Parser::parseValue() {
|
||||
char next = peek();
|
||||
valvalue val;
|
||||
dynamic::valvalue val;
|
||||
if (next == '-' || next == '+') {
|
||||
pos++;
|
||||
number_u num;
|
||||
@ -530,7 +198,7 @@ Value* Parser::parseValue() {
|
||||
return new Value(type, val);
|
||||
}
|
||||
if (is_identifier_start(next)) {
|
||||
string literal = parseName();
|
||||
std::string literal = parseName();
|
||||
if (literal == "true") {
|
||||
val.boolean = true;
|
||||
return new Value(valtype::boolean, val);
|
||||
@ -547,12 +215,12 @@ Value* Parser::parseValue() {
|
||||
throw error("invalid literal ");
|
||||
}
|
||||
if (next == '{') {
|
||||
val.obj = parseObject();
|
||||
return new Value(valtype::object, val);
|
||||
val.map = parseObject();
|
||||
return new Value(valtype::map, val);
|
||||
}
|
||||
if (next == '[') {
|
||||
val.arr = parseArray();
|
||||
return new Value(valtype::array, val);
|
||||
val.list = parseList();
|
||||
return new Value(valtype::list, val);
|
||||
}
|
||||
if (is_digit(next)) {
|
||||
number_u num;
|
||||
@ -568,17 +236,17 @@ Value* Parser::parseValue() {
|
||||
}
|
||||
if (next == '"' || next == '\'') {
|
||||
pos++;
|
||||
val.str = new string(parseString(next));
|
||||
val.str = new std::string(parseString(next));
|
||||
return new Value(valtype::string, val);
|
||||
}
|
||||
throw error("unexpected character '"+string({next})+"'");
|
||||
throw error("unexpected character '"+std::string({next})+"'");
|
||||
}
|
||||
|
||||
JObject* json::parse(string filename, string source) {
|
||||
std::unique_ptr<Map> json::parse(std::string filename, std::string 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);
|
||||
}
|
||||
|
||||
@ -8,120 +8,34 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#include "commons.h"
|
||||
#include "../typedefs.h"
|
||||
|
||||
typedef unsigned int uint;
|
||||
#include "binary_json.h"
|
||||
|
||||
namespace dynamic {
|
||||
class Map;
|
||||
class List;
|
||||
class Value;
|
||||
}
|
||||
|
||||
namespace json {
|
||||
class JObject;
|
||||
class JArray;
|
||||
class Value;
|
||||
|
||||
extern std::string stringify(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 {
|
||||
JArray* parseArray();
|
||||
JObject* parseObject();
|
||||
Value* parseValue();
|
||||
dynamic::List* parseList();
|
||||
dynamic::Map* parseObject();
|
||||
dynamic::Value* parseValue();
|
||||
public:
|
||||
Parser(std::string filename, std::string source);
|
||||
|
||||
JObject* parse();
|
||||
dynamic::Map* parse();
|
||||
};
|
||||
|
||||
extern JObject* parse(std::string filename, std::string source);
|
||||
extern JObject* parse(std::string source);
|
||||
extern std::unique_ptr<dynamic::Map> parse(std::string filename, 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_
|
||||
@ -131,7 +131,6 @@ Content::Content(ContentIndices* indices, DrawGroups* drawGroups,
|
||||
}
|
||||
|
||||
Content::~Content() {
|
||||
delete indices;
|
||||
delete drawGroups;
|
||||
}
|
||||
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <unordered_map>
|
||||
#include <set>
|
||||
@ -92,14 +93,18 @@ public:
|
||||
class Content {
|
||||
std::unordered_map<std::string, Block*> blockDefs;
|
||||
std::unordered_map<std::string, ItemDef*> itemDefs;
|
||||
std::unique_ptr<ContentIndices> indices;
|
||||
public:
|
||||
ContentIndices* const indices;
|
||||
DrawGroups* const drawGroups;
|
||||
|
||||
Content(ContentIndices* indices, DrawGroups* drawGroups,
|
||||
std::unordered_map<std::string, Block*> blockDefs,
|
||||
std::unordered_map<std::string, ItemDef*> itemDefs);
|
||||
~Content();
|
||||
|
||||
inline ContentIndices* getIndices() const {
|
||||
return indices.get();
|
||||
}
|
||||
|
||||
Block* findBlock(std::string id) const;
|
||||
Block* requireBlock(std::string id) const;
|
||||
|
||||
@ -8,15 +8,10 @@
|
||||
#include "../coders/json.h"
|
||||
#include "../voxels/Block.h"
|
||||
|
||||
using std::string;
|
||||
using std::unique_ptr;
|
||||
using std::make_unique;
|
||||
using std::filesystem::path;
|
||||
|
||||
#include <iostream>
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
ContentLUT::ContentLUT(size_t blocksCount, const Content* content) {
|
||||
ContentIndices* indices = content->indices;
|
||||
auto* indices = content->getIndices();
|
||||
for (size_t i = 0; i < blocksCount; i++) {
|
||||
blocks.push_back(i);
|
||||
}
|
||||
@ -24,25 +19,25 @@ ContentLUT::ContentLUT(size_t blocksCount, const Content* content) {
|
||||
blockNames.push_back(indices->getBlockDef(i)->name);
|
||||
}
|
||||
|
||||
for (size_t i = indices->countBlockDefs(); i < blocksCount; i++) {
|
||||
for (size_t i = indices->countBlockDefs(); i < blocksCount; i++) {
|
||||
blockNames.push_back("");
|
||||
}
|
||||
}
|
||||
|
||||
ContentLUT* ContentLUT::create(const path& filename,
|
||||
ContentLUT* ContentLUT::create(const fs::path& filename,
|
||||
const Content* content) {
|
||||
unique_ptr<json::JObject> root(files::read_json(filename));
|
||||
json::JArray* blocksarr = root->arr("blocks");
|
||||
auto root = files::read_json(filename);
|
||||
auto blocklist = root->list("blocks");
|
||||
|
||||
auto& indices = content->indices;
|
||||
size_t blocks_c = blocksarr
|
||||
? std::max(blocksarr->size(), indices->countBlockDefs())
|
||||
auto* indices = content->getIndices();
|
||||
size_t blocks_c = blocklist
|
||||
? std::max(blocklist->size(), indices->countBlockDefs())
|
||||
: indices->countBlockDefs();
|
||||
|
||||
auto lut = make_unique<ContentLUT>(blocks_c, content);
|
||||
if (blocksarr) {
|
||||
for (size_t i = 0; i < blocksarr->size(); i++) {
|
||||
string name = blocksarr->str(i);
|
||||
auto lut = std::make_unique<ContentLUT>(blocks_c, content);
|
||||
if (blocklist) {
|
||||
for (size_t i = 0; i < blocklist->size(); i++) {
|
||||
std::string name = blocklist->str(i);
|
||||
Block* def = content->findBlock(name);
|
||||
if (def) {
|
||||
lut->setBlock(i, name, def->rt.id);
|
||||
|
||||
@ -8,6 +8,8 @@
|
||||
#include "../typedefs.h"
|
||||
#include "../constants.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
class Content;
|
||||
|
||||
/* Content indices lookup table or report
|
||||
@ -40,7 +42,7 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
static ContentLUT* create(const std::filesystem::path& filename,
|
||||
static ContentLUT* create(const fs::path& filename,
|
||||
const Content* content);
|
||||
|
||||
inline bool hasContentReorder() const {
|
||||
|
||||
@ -13,6 +13,7 @@
|
||||
#include "../files/files.h"
|
||||
#include "../coders/json.h"
|
||||
#include "../typedefs.h"
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
#include "ContentPack.h"
|
||||
#include "../logic/scripting/scripting.h"
|
||||
@ -23,9 +24,8 @@ ContentLoader::ContentLoader(ContentPack* pack) : pack(pack) {
|
||||
}
|
||||
|
||||
bool ContentLoader::fixPackIndices(fs::path folder,
|
||||
json::JObject* indicesRoot,
|
||||
dynamic::Map* indicesRoot,
|
||||
std::string contentSection) {
|
||||
|
||||
std::vector<std::string> detected;
|
||||
std::vector<std::string> indexed;
|
||||
if (fs::is_directory(folder)) {
|
||||
@ -42,9 +42,9 @@ bool ContentLoader::fixPackIndices(fs::path folder,
|
||||
|
||||
bool modified = false;
|
||||
if (!indicesRoot->has(contentSection)) {
|
||||
indicesRoot->putArray(contentSection);
|
||||
indicesRoot->putList(contentSection);
|
||||
}
|
||||
json::JArray* arr = indicesRoot->arr(contentSection);
|
||||
auto arr = indicesRoot->list(contentSection);
|
||||
if (arr) {
|
||||
for (uint i = 0; i < arr->size(); i++) {
|
||||
std::string name = arr->str(i);
|
||||
@ -72,11 +72,11 @@ void ContentLoader::fixPackIndices() {
|
||||
auto blocksFolder = folder/ContentPack::BLOCKS_FOLDER;
|
||||
auto itemsFolder = folder/ContentPack::ITEMS_FOLDER;
|
||||
|
||||
std::unique_ptr<json::JObject> root;
|
||||
std::unique_ptr<dynamic::Map> root;
|
||||
if (fs::is_regular_file(indexFile)) {
|
||||
root.reset(files::read_json(indexFile));
|
||||
root = std::move(files::read_json(indexFile));
|
||||
} else {
|
||||
root.reset(new json::JObject());
|
||||
root.reset(new dynamic::Map());
|
||||
}
|
||||
|
||||
bool modified = false;
|
||||
@ -87,13 +87,13 @@ void ContentLoader::fixPackIndices() {
|
||||
if (modified){
|
||||
// rewrite modified json
|
||||
std::cout << indexFile << std::endl;
|
||||
files::write_string(indexFile, json::stringify(root.get(), true, " "));
|
||||
files::write_json(indexFile, root.get());
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: add basic validation and logging
|
||||
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
|
||||
if (root->has("texture")) {
|
||||
@ -103,7 +103,7 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) {
|
||||
def->textureFaces[i] = texture;
|
||||
}
|
||||
} else if (root->has("texture-faces")) {
|
||||
json::JArray* texarr = root->arr("texture-faces");
|
||||
auto texarr = root->list("texture-faces");
|
||||
for (uint i = 0; i < 6; i++) {
|
||||
def->textureFaces[i] = texarr->str(i);
|
||||
}
|
||||
@ -117,7 +117,7 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) {
|
||||
else if (model == "custom") {
|
||||
def->model = BlockModel::custom;
|
||||
if (root->has("model-primitives")) {
|
||||
loadCustomBlockModel(def, root->obj("model-primitives"));
|
||||
loadCustomBlockModel(def, root->map("model-primitives"));
|
||||
}
|
||||
else {
|
||||
std::cerr << "ERROR occured while block "
|
||||
@ -145,20 +145,20 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) {
|
||||
}
|
||||
|
||||
// block hitbox AABB [x, y, z, width, height, depth]
|
||||
json::JArray* boxobj = root->arr("hitbox");
|
||||
if (boxobj) {
|
||||
auto boxarr = root->list("hitbox");
|
||||
if (boxarr) {
|
||||
AABB& aabb = def->hitbox;
|
||||
aabb.a = glm::vec3(boxobj->num(0), boxobj->num(1), boxobj->num(2));
|
||||
aabb.b = glm::vec3(boxobj->num(3), boxobj->num(4), boxobj->num(5));
|
||||
aabb.a = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2));
|
||||
aabb.b = glm::vec3(boxarr->num(3), boxarr->num(4), boxarr->num(5));
|
||||
aabb.b += aabb.a;
|
||||
}
|
||||
|
||||
// block light emission [r, g, b] where r,g,b in range [0..15]
|
||||
json::JArray* emissionobj = root->arr("emission");
|
||||
if (emissionobj) {
|
||||
def->emission[0] = emissionobj->num(0);
|
||||
def->emission[1] = emissionobj->num(1);
|
||||
def->emission[2] = emissionobj->num(2);
|
||||
auto emissionarr = root->list("emission");
|
||||
if (emissionarr) {
|
||||
def->emission[0] = emissionarr->num(0);
|
||||
def->emission[1] = emissionarr->num(1);
|
||||
def->emission[2] = emissionarr->num(2);
|
||||
}
|
||||
|
||||
// primitive properties
|
||||
@ -171,29 +171,28 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) {
|
||||
root->flag("hidden", def->hidden);
|
||||
root->flag("sky-light-passing", def->skyLightPassing);
|
||||
root->num("draw-group", def->drawGroup);
|
||||
|
||||
root->str("picking-item", def->pickingItem);
|
||||
}
|
||||
|
||||
void ContentLoader::loadCustomBlockModel(Block* def, json::JObject* primitives) {
|
||||
void ContentLoader::loadCustomBlockModel(Block* def, dynamic::Map* primitives) {
|
||||
if (primitives->has("aabbs")) {
|
||||
json::JArray* modelboxes = primitives->arr("aabbs");
|
||||
auto modelboxes = primitives->list("aabbs");
|
||||
for (uint i = 0; i < modelboxes->size(); i++ ) {
|
||||
/* Parse aabb */
|
||||
json::JArray* boxobj = modelboxes->arr(i);
|
||||
auto boxarr = modelboxes->list(i);
|
||||
AABB modelbox;
|
||||
modelbox.a = glm::vec3(boxobj->num(0), boxobj->num(1), boxobj->num(2));
|
||||
modelbox.b = glm::vec3(boxobj->num(3), boxobj->num(4), boxobj->num(5));
|
||||
modelbox.a = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2));
|
||||
modelbox.b = glm::vec3(boxarr->num(3), boxarr->num(4), boxarr->num(5));
|
||||
modelbox.b += modelbox.a;
|
||||
def->modelBoxes.push_back(modelbox);
|
||||
|
||||
if (boxobj->size() == 7)
|
||||
if (boxarr->size() == 7)
|
||||
for (uint i = 6; i < 12; i++) {
|
||||
def->modelTextures.push_back(boxobj->str(6));
|
||||
def->modelTextures.push_back(boxarr->str(6));
|
||||
}
|
||||
else if (boxobj->size() == 12)
|
||||
else if (boxarr->size() == 12)
|
||||
for (uint i = 6; i < 12; i++) {
|
||||
def->modelTextures.push_back(boxobj->str(i));
|
||||
def->modelTextures.push_back(boxarr->str(i));
|
||||
}
|
||||
else
|
||||
for (uint i = 6; i < 12; i++) {
|
||||
@ -202,10 +201,10 @@ void ContentLoader::loadCustomBlockModel(Block* def, json::JObject* primitives)
|
||||
}
|
||||
}
|
||||
if (primitives->has("tetragons")) {
|
||||
json::JArray* modeltetragons = primitives->arr("tetragons");
|
||||
auto modeltetragons = primitives->list("tetragons");
|
||||
for (uint i = 0; i < modeltetragons->size(); i++) {
|
||||
/* Parse tetragon to points */
|
||||
json::JArray* tgonobj = modeltetragons->arr(i);
|
||||
auto tgonobj = modeltetragons->list(i);
|
||||
glm::vec3 p1(tgonobj->num(0), tgonobj->num(1), tgonobj->num(2)),
|
||||
xw(tgonobj->num(3), tgonobj->num(4), tgonobj->num(5)),
|
||||
yh(tgonobj->num(6), tgonobj->num(7), tgonobj->num(8));
|
||||
@ -220,7 +219,7 @@ void ContentLoader::loadCustomBlockModel(Block* def, json::JObject* primitives)
|
||||
}
|
||||
|
||||
void ContentLoader::loadItem(ItemDef* def, std::string name, fs::path file) {
|
||||
std::unique_ptr<json::JObject> root(files::read_json(file));
|
||||
auto root = files::read_json(file);
|
||||
std::string iconTypeStr = "none";
|
||||
root->str("icon-type", iconTypeStr);
|
||||
if (iconTypeStr == "none") {
|
||||
@ -236,11 +235,11 @@ void ContentLoader::loadItem(ItemDef* def, std::string name, fs::path file) {
|
||||
root->str("placing-block", def->placingBlock);
|
||||
|
||||
// item light emission [r, g, b] where r,g,b in range [0..15]
|
||||
json::JArray* emissionobj = root->arr("emission");
|
||||
if (emissionobj) {
|
||||
def->emission[0] = emissionobj->num(0);
|
||||
def->emission[1] = emissionobj->num(1);
|
||||
def->emission[2] = emissionobj->num(2);
|
||||
auto emissionarr = root->list("emission");
|
||||
if (emissionarr) {
|
||||
def->emission[0] = emissionarr->num(0);
|
||||
def->emission[1] = emissionarr->num(1);
|
||||
def->emission[2] = emissionarr->num(2);
|
||||
}
|
||||
}
|
||||
|
||||
@ -274,9 +273,8 @@ void ContentLoader::load(ContentBuilder* builder) {
|
||||
auto folder = pack->folder;
|
||||
if (!fs::is_regular_file(pack->getContentFile()))
|
||||
return;
|
||||
std::unique_ptr<json::JObject> root (files::read_json(pack->getContentFile()));
|
||||
|
||||
json::JArray* blocksarr = root->arr("blocks");
|
||||
auto root = files::read_json(pack->getContentFile());
|
||||
auto blocksarr = root->list("blocks");
|
||||
if (blocksarr) {
|
||||
for (uint i = 0; i < blocksarr->size(); i++) {
|
||||
std::string name = blocksarr->str(i);
|
||||
@ -297,7 +295,7 @@ void ContentLoader::load(ContentBuilder* builder) {
|
||||
}
|
||||
}
|
||||
|
||||
json::JArray* itemsarr = root->arr("items");
|
||||
auto itemsarr = root->list("items");
|
||||
if (itemsarr) {
|
||||
for (uint i = 0; i < itemsarr->size(); i++) {
|
||||
std::string name = itemsarr->str(i);
|
||||
|
||||
@ -4,30 +4,32 @@
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
class Block;
|
||||
class ItemDef;
|
||||
class ContentPack;
|
||||
class ContentBuilder;
|
||||
|
||||
namespace json {
|
||||
class JObject;
|
||||
namespace dynamic {
|
||||
class Map;
|
||||
}
|
||||
|
||||
class ContentLoader {
|
||||
const ContentPack* pack;
|
||||
|
||||
void loadBlock(Block* def, std::string full, std::string name);
|
||||
void loadCustomBlockModel(Block* def, json::JObject* primitives);
|
||||
void loadCustomBlockModel(Block* def, dynamic::Map* primitives);
|
||||
void loadItem(ItemDef* def, std::string full, std::string name);
|
||||
public:
|
||||
ContentLoader(ContentPack* pack);
|
||||
|
||||
bool fixPackIndices(std::filesystem::path folder,
|
||||
json::JObject* indicesRoot,
|
||||
dynamic::Map* indicesRoot,
|
||||
std::string contentSection);
|
||||
void fixPackIndices();
|
||||
void loadBlock(Block* def, std::string name, std::filesystem::path file);
|
||||
void loadItem(ItemDef* def, std::string name, std::filesystem::path file);
|
||||
void loadBlock(Block* def, std::string name, fs::path file);
|
||||
void loadItem(ItemDef* def, std::string name, fs::path file);
|
||||
void load(ContentBuilder* builder);
|
||||
};
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
#include "../coders/json.h"
|
||||
#include "../files/files.h"
|
||||
#include "../files/engine_paths.h"
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
@ -15,7 +16,7 @@ const fs::path ContentPack::ITEMS_FOLDER = "items";
|
||||
|
||||
contentpack_error::contentpack_error(
|
||||
std::string packId,
|
||||
std::filesystem::path folder,
|
||||
fs::path folder,
|
||||
std::string message)
|
||||
: std::runtime_error(message), packId(packId), folder(folder) {
|
||||
}
|
||||
@ -23,19 +24,19 @@ contentpack_error::contentpack_error(
|
||||
std::string contentpack_error::getPackId() const {
|
||||
return packId;
|
||||
}
|
||||
std::filesystem::path contentpack_error::getFolder() const {
|
||||
fs::path contentpack_error::getFolder() const {
|
||||
return folder;
|
||||
}
|
||||
|
||||
std::filesystem::path ContentPack::getContentFile() const {
|
||||
fs::path ContentPack::getContentFile() const {
|
||||
return folder/fs::path(CONTENT_FILENAME);
|
||||
}
|
||||
|
||||
bool ContentPack::is_pack(std::filesystem::path folder) {
|
||||
bool ContentPack::is_pack(fs::path folder) {
|
||||
return fs::is_regular_file(folder/fs::path(PACKAGE_FILENAME));
|
||||
}
|
||||
|
||||
ContentPack ContentPack::read(std::filesystem::path folder) {
|
||||
ContentPack ContentPack::read(fs::path folder) {
|
||||
auto root = files::read_json(folder/fs::path(PACKAGE_FILENAME));
|
||||
ContentPack pack;
|
||||
root->str("id", pack.id);
|
||||
@ -88,7 +89,7 @@ fs::path ContentPack::findPack(const EnginePaths* paths, fs::path worldDir, std:
|
||||
void ContentPack::readPacks(const EnginePaths* paths,
|
||||
std::vector<ContentPack>& packs,
|
||||
const std::vector<std::string>& packnames,
|
||||
std::filesystem::path worldDir) {
|
||||
fs::path worldDir) {
|
||||
for (const auto& name : packnames) {
|
||||
fs::path packfolder = ContentPack::findPack(paths, worldDir, name);
|
||||
packs.push_back(ContentPack::read(packfolder));
|
||||
|
||||
331
src/data/dynamic.cpp
Normal file
331
src/data/dynamic.cpp
Normal file
@ -0,0 +1,331 @@
|
||||
#include "dynamic.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace dynamic;
|
||||
|
||||
List::~List() {
|
||||
}
|
||||
|
||||
std::string List::str(size_t index) const {
|
||||
const auto& val = values[index];
|
||||
switch (val->type) {
|
||||
case valtype::string: return *val->value.str;
|
||||
case valtype::boolean: return val->value.boolean ? "true" : "false";
|
||||
case valtype::number: return std::to_string(val->value.decimal);
|
||||
case valtype::integer: return std::to_string(val->value.integer);
|
||||
default:
|
||||
throw std::runtime_error("type error");
|
||||
}
|
||||
}
|
||||
|
||||
double List::num(size_t index) const {
|
||||
const auto& val = values[index];
|
||||
switch (val->type) {
|
||||
case valtype::number: return val->value.decimal;
|
||||
case valtype::integer: return val->value.integer;
|
||||
case valtype::string: return std::stoll(*val->value.str);
|
||||
case valtype::boolean: return val->value.boolean;
|
||||
default:
|
||||
throw std::runtime_error("type error");
|
||||
}
|
||||
}
|
||||
|
||||
int64_t List::integer(size_t index) const {
|
||||
const auto& val = values[index];
|
||||
switch (val->type) {
|
||||
case valtype::number: return val->value.decimal;
|
||||
case valtype::integer: return val->value.integer;
|
||||
case valtype::string: return std::stoll(*val->value.str);
|
||||
case valtype::boolean: return val->value.boolean;
|
||||
default:
|
||||
throw std::runtime_error("type error");
|
||||
}
|
||||
}
|
||||
|
||||
Map* List::map(size_t index) const {
|
||||
return values[index]->value.map;
|
||||
}
|
||||
|
||||
List* List::list(size_t index) const {
|
||||
return values[index]->value.list;
|
||||
}
|
||||
|
||||
bool List::flag(size_t index) const {
|
||||
return values[index]->value.boolean;
|
||||
}
|
||||
|
||||
List& List::put(std::string value) {
|
||||
valvalue val;
|
||||
val.str = new std::string(value);
|
||||
values.push_back(std::make_unique<Value>(valtype::string, val));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& List::put(uint value) {
|
||||
return put((int64_t)value);
|
||||
}
|
||||
|
||||
List& List::put(int value) {
|
||||
return put((int64_t)value);
|
||||
}
|
||||
|
||||
List& List::put(int64_t value) {
|
||||
valvalue val;
|
||||
val.integer = value;
|
||||
values.push_back(std::make_unique<Value>(valtype::integer, val));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& List::put(uint64_t value) {
|
||||
return put((int64_t)value);
|
||||
}
|
||||
|
||||
List& List::put(double value) {
|
||||
valvalue val;
|
||||
val.decimal = value;
|
||||
values.push_back(std::make_unique<Value>(valtype::number, val));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& List::put(float value) {
|
||||
return put((double)value);
|
||||
}
|
||||
|
||||
List& List::put(bool value) {
|
||||
valvalue val;
|
||||
val.boolean = value;
|
||||
values.push_back(std::make_unique<Value>(valtype::boolean, val));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& List::put(Map* value) {
|
||||
valvalue val;
|
||||
val.map = value;
|
||||
values.push_back(std::make_unique<Value>(valtype::map, val));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& List::put(List* value) {
|
||||
valvalue val;
|
||||
val.list = value;
|
||||
values.push_back(std::make_unique<Value>(valtype::list, val));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& List::putList() {
|
||||
List* arr = new List();
|
||||
put(arr);
|
||||
return *arr;
|
||||
}
|
||||
|
||||
Map& List::putMap() {
|
||||
Map* map = new Map();
|
||||
put(map);
|
||||
return *map;
|
||||
}
|
||||
|
||||
void List::remove(size_t index) {
|
||||
values.erase(values.begin() + index);
|
||||
}
|
||||
|
||||
Map::~Map() {
|
||||
}
|
||||
|
||||
void Map::str(std::string key, std::string& dst) const {
|
||||
dst = getStr(key, dst);
|
||||
}
|
||||
|
||||
std::string Map::getStr(std::string key, const std::string& def) const {
|
||||
auto found = values.find(key);
|
||||
if (found == values.end())
|
||||
return def;
|
||||
auto& val = found->second;
|
||||
switch (val->type) {
|
||||
case valtype::string: return *val->value.str;
|
||||
case valtype::boolean: return val->value.boolean ? "true" : "false";
|
||||
case valtype::number: return std::to_string(val->value.decimal);
|
||||
case valtype::integer: return std::to_string(val->value.integer);
|
||||
default: throw std::runtime_error("type error");
|
||||
}
|
||||
}
|
||||
|
||||
double Map::getNum(std::string key, double def) const {
|
||||
auto found = values.find(key);
|
||||
if (found == values.end())
|
||||
return def;
|
||||
auto& val = found->second;
|
||||
switch (val->type) {
|
||||
case valtype::number: return val->value.decimal;
|
||||
case valtype::integer: return val->value.integer;
|
||||
case valtype::string: return std::stoull(*val->value.str);
|
||||
case valtype::boolean: return val->value.boolean;
|
||||
default: throw std::runtime_error("type error");
|
||||
}
|
||||
}
|
||||
|
||||
int64_t Map::getInt(std::string key, int64_t def) const {
|
||||
auto found = values.find(key);
|
||||
if (found == values.end())
|
||||
return def;
|
||||
auto& val = found->second;
|
||||
switch (val->type) {
|
||||
case valtype::number: return val->value.decimal;
|
||||
case valtype::integer: return val->value.integer;
|
||||
case valtype::string: return std::stoull(*val->value.str);
|
||||
case valtype::boolean: return val->value.boolean;
|
||||
default: throw std::runtime_error("type error");
|
||||
}
|
||||
}
|
||||
|
||||
bool Map::getBool(std::string key, bool def) const {
|
||||
auto found = values.find(key);
|
||||
if (found != values.end())
|
||||
return found->second->value.boolean;
|
||||
return def;
|
||||
}
|
||||
|
||||
void Map::num(std::string key, double& dst) const {
|
||||
dst = getNum(key, dst);
|
||||
}
|
||||
|
||||
void Map::num(std::string key, float& dst) const {
|
||||
dst = getNum(key, dst);
|
||||
}
|
||||
|
||||
void Map::num(std::string key, ubyte& dst) const {
|
||||
dst = getInt(key, dst);
|
||||
}
|
||||
|
||||
void Map::num(std::string key, int& dst) const {
|
||||
dst = getInt(key, dst);
|
||||
}
|
||||
|
||||
void Map::num(std::string key, int64_t& dst) const {
|
||||
dst = getInt(key, dst);
|
||||
}
|
||||
|
||||
void Map::num(std::string key, uint64_t& dst) const {
|
||||
dst = getInt(key, dst);
|
||||
}
|
||||
|
||||
void Map::num(std::string key, uint& dst) const {
|
||||
dst = getInt(key, dst);
|
||||
}
|
||||
|
||||
Map* Map::map(std::string key) const {
|
||||
auto found = values.find(key);
|
||||
if (found != values.end()) {
|
||||
auto& val = found->second;
|
||||
if (val->type != valtype::map)
|
||||
return nullptr;
|
||||
return val->value.map;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
List* Map::list(std::string key) const {
|
||||
auto found = values.find(key);
|
||||
if (found != values.end())
|
||||
return found->second->value.list;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Map::flag(std::string key, bool& dst) const {
|
||||
auto found = values.find(key);
|
||||
if (found != values.end())
|
||||
dst = found->second->value.boolean;
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, uint value) {
|
||||
return put(key, (int64_t)value);
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, int value) {
|
||||
return put(key, (int64_t)value);
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, int64_t value) {
|
||||
auto found = values.find(key);
|
||||
if (found != values.end()) found->second;
|
||||
valvalue val;
|
||||
val.integer = value;
|
||||
values.insert(std::make_pair(key, new Value(valtype::integer, val)));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, uint64_t value) {
|
||||
return put(key, (int64_t)value);
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, float value) {
|
||||
return put(key, (double)value);
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, double value) {
|
||||
valvalue val;
|
||||
val.decimal = value;
|
||||
values.insert(std::make_pair(key, new Value(valtype::number, val)));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, std::string value){
|
||||
valvalue val;
|
||||
val.str = new std::string(value);
|
||||
values.insert(std::make_pair(key, new Value(valtype::string, val)));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, const char* value) {
|
||||
return put(key, std::string(value));
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, Map* value){
|
||||
valvalue val;
|
||||
val.map = value;
|
||||
values.insert(std::make_pair(key, new Value(valtype::map, val)));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, List* value){
|
||||
valvalue val;
|
||||
val.list = value;
|
||||
values.insert(std::make_pair(key, new Value(valtype::list, val)));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, bool value){
|
||||
valvalue val;
|
||||
val.boolean = value;
|
||||
values.insert(std::make_pair(key, new Value(valtype::boolean, val)));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& Map::putList(std::string key) {
|
||||
List* arr = new List();
|
||||
put(key, arr);
|
||||
return *arr;
|
||||
}
|
||||
|
||||
Map& Map::putMap(std::string key) {
|
||||
Map* obj = new Map();
|
||||
put(key, obj);
|
||||
return *obj;
|
||||
}
|
||||
|
||||
bool Map::has(std::string key) {
|
||||
return values.find(key) != values.end();
|
||||
}
|
||||
|
||||
Value::Value(valtype type, valvalue value) : type(type), value(value) {
|
||||
}
|
||||
|
||||
Value::~Value() {
|
||||
switch (type) {
|
||||
case valtype::map: delete value.map; break;
|
||||
case valtype::list: delete value.list; break;
|
||||
case valtype::string: delete value.str; break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
114
src/data/dynamic.h
Normal file
114
src/data/dynamic.h
Normal file
@ -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_
|
||||
@ -1,7 +1,6 @@
|
||||
#include "WorldFiles.h"
|
||||
|
||||
#include "rle.h"
|
||||
#include "binary_io.h"
|
||||
#include "../window/Camera.h"
|
||||
#include "../content/Content.h"
|
||||
#include "../objects/Player.h"
|
||||
@ -19,6 +18,8 @@
|
||||
#include "../constants.h"
|
||||
#include "../items/ItemDef.h"
|
||||
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <cstdint>
|
||||
@ -445,7 +446,7 @@ void WorldFiles::write(const World* world, const Content* content) {
|
||||
if (generatorTestMode)
|
||||
return;
|
||||
|
||||
writeIndices(content->indices);
|
||||
writeIndices(content->getIndices());
|
||||
writeRegions(regions, regionsFolder, REGION_LAYER_VOXELS);
|
||||
writeRegions(lights, lightsFolder, REGION_LAYER_LIGHTS);
|
||||
}
|
||||
@ -461,40 +462,40 @@ void WorldFiles::writePacks(const World* world) {
|
||||
}
|
||||
|
||||
void WorldFiles::writeIndices(const ContentIndices* indices) {
|
||||
json::JObject root;
|
||||
dynamic::Map root;
|
||||
uint count;
|
||||
json::JArray& blocks = root.putArray("blocks");
|
||||
auto& blocks = root.putList("blocks");
|
||||
count = indices->countBlockDefs();
|
||||
for (uint i = 0; i < count; i++) {
|
||||
const Block* def = indices->getBlockDef(i);
|
||||
blocks.put(def->name);
|
||||
}
|
||||
|
||||
json::JArray& items = root.putArray("items");
|
||||
auto& items = root.putList("items");
|
||||
count = indices->countItemDefs();
|
||||
for (uint i = 0; i < count; i++) {
|
||||
const ItemDef* def = indices->getItemDef(i);
|
||||
items.put(def->name);
|
||||
}
|
||||
|
||||
files::write_string(getIndicesFile(), json::stringify(&root, true, " "));
|
||||
files::write_json(getIndicesFile(), &root);
|
||||
}
|
||||
|
||||
void WorldFiles::writeWorldInfo(const World* world) {
|
||||
json::JObject root;
|
||||
dynamic::Map root;
|
||||
|
||||
json::JObject& versionobj = root.putObj("version");
|
||||
auto& versionobj = root.putMap("version");
|
||||
versionobj.put("major", ENGINE_VERSION_MAJOR);
|
||||
versionobj.put("minor", ENGINE_VERSION_MINOR);
|
||||
|
||||
root.put("name", world->name);
|
||||
root.put("seed", world->seed);
|
||||
|
||||
json::JObject& timeobj = root.putObj("time");
|
||||
auto& timeobj = root.putMap("time");
|
||||
timeobj.put("day-time", world->daytime);
|
||||
timeobj.put("day-time-speed", world->daytimeSpeed);
|
||||
|
||||
files::write_string(getWorldFile(), json::stringify(&root, true, " "));
|
||||
files::write_json(getWorldFile(), &root);
|
||||
}
|
||||
|
||||
bool WorldFiles::readWorldInfo(World* world) {
|
||||
@ -504,11 +505,11 @@ bool WorldFiles::readWorldInfo(World* world) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<json::JObject> root(files::read_json(file));
|
||||
auto root = files::read_json(file);
|
||||
root->str("name", world->name);
|
||||
root->num("seed", world->seed);
|
||||
|
||||
json::JObject* verobj = root->obj("version");
|
||||
auto verobj = root->map("version");
|
||||
if (verobj) {
|
||||
int major=0, minor=-1;
|
||||
verobj->num("major", major);
|
||||
@ -516,7 +517,7 @@ bool WorldFiles::readWorldInfo(World* world) {
|
||||
std::cout << "world version: " << major << "." << minor << std::endl;
|
||||
}
|
||||
|
||||
json::JObject* timeobj = root->obj("time");
|
||||
auto timeobj = root->map("time");
|
||||
if (timeobj) {
|
||||
timeobj->num("day-time", world->daytime);
|
||||
timeobj->num("day-time-speed", world->daytimeSpeed);
|
||||
@ -527,20 +528,20 @@ bool WorldFiles::readWorldInfo(World* world) {
|
||||
|
||||
void WorldFiles::writePlayer(Player* player){
|
||||
glm::vec3 position = player->hitbox->position;
|
||||
json::JObject root;
|
||||
json::JArray& posarr = root.putArray("position");
|
||||
dynamic::Map root;
|
||||
auto& posarr = root.putList("position");
|
||||
posarr.put(position.x);
|
||||
posarr.put(position.y);
|
||||
posarr.put(position.z);
|
||||
|
||||
json::JArray& rotarr = root.putArray("rotation");
|
||||
auto& rotarr = root.putList("rotation");
|
||||
rotarr.put(player->cam.x);
|
||||
rotarr.put(player->cam.y);
|
||||
|
||||
root.put("flight", player->flight);
|
||||
root.put("noclip", player->noclip);
|
||||
|
||||
files::write_string(getPlayerFile(), json::stringify(&root, true, " "));
|
||||
files::write_json(getPlayerFile(), &root);
|
||||
}
|
||||
|
||||
bool WorldFiles::readPlayer(Player* player) {
|
||||
@ -550,15 +551,15 @@ bool WorldFiles::readPlayer(Player* player) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<json::JObject> root(files::read_json(file));
|
||||
json::JArray* posarr = root->arr("position");
|
||||
auto root = files::read_json(file);
|
||||
auto posarr = root->list("position");
|
||||
glm::vec3& position = player->hitbox->position;
|
||||
position.x = posarr->num(0);
|
||||
position.y = posarr->num(1);
|
||||
position.z = posarr->num(2);
|
||||
player->camera->position = position;
|
||||
|
||||
json::JArray* rotarr = root->arr("rotation");
|
||||
auto rotarr = root->list("rotation");
|
||||
player->cam.x = rotarr->num(0);
|
||||
player->cam.y = rotarr->num(1);
|
||||
|
||||
|
||||
@ -34,6 +34,8 @@ class Content;
|
||||
class ContentIndices;
|
||||
class World;
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
class illegal_region_format : public std::runtime_error {
|
||||
public:
|
||||
illegal_region_format(const std::string& message)
|
||||
@ -63,27 +65,24 @@ struct regfile {
|
||||
files::rafile file;
|
||||
int version;
|
||||
|
||||
regfile(std::filesystem::path filename);
|
||||
regfile(fs::path filename);
|
||||
};
|
||||
|
||||
typedef std::unordered_map<glm::ivec2, std::unique_ptr<WorldRegion>> regionsmap;
|
||||
|
||||
class WorldFiles {
|
||||
std::unordered_map<glm::ivec3, std::unique_ptr<regfile>> openRegFiles;
|
||||
|
||||
void writeWorldInfo(const World* world);
|
||||
std::filesystem::path getLightsFolder() const;
|
||||
std::filesystem::path getRegionFilename(int x, int y) const;
|
||||
std::filesystem::path getPlayerFile() const;
|
||||
std::filesystem::path getWorldFile() const;
|
||||
std::filesystem::path getIndicesFile() const;
|
||||
std::filesystem::path getPacksFile() const;
|
||||
fs::path getLightsFolder() const;
|
||||
fs::path getRegionFilename(int x, int y) const;
|
||||
fs::path getPlayerFile() const;
|
||||
fs::path getWorldFile() const;
|
||||
fs::path getIndicesFile() const;
|
||||
fs::path getPacksFile() const;
|
||||
|
||||
WorldRegion* getRegion(regionsmap& regions,
|
||||
int x, int z);
|
||||
|
||||
WorldRegion* getOrCreateRegion(
|
||||
regionsmap& regions,
|
||||
int x, int z);
|
||||
WorldRegion* getRegion(regionsmap& regions, int x, int z);
|
||||
WorldRegion* getOrCreateRegion(regionsmap& regions, int x, int z);
|
||||
|
||||
/* Compress buffer with extrle
|
||||
@param src source buffer
|
||||
@ -94,38 +93,36 @@ class WorldFiles {
|
||||
/* Decompress buffer with extrle
|
||||
@param src compressed buffer
|
||||
@param srclen length of compressed buffer
|
||||
@param dstlen max expected length of source buffer
|
||||
*/
|
||||
@param dstlen max expected length of source buffer */
|
||||
ubyte* decompress(const ubyte* src, size_t srclen, size_t dstlen);
|
||||
|
||||
ubyte* readChunkData(int x, int y,
|
||||
uint32_t& length,
|
||||
std::filesystem::path folder,
|
||||
fs::path folder,
|
||||
int layer);
|
||||
void fetchChunks(WorldRegion* region, int x, int y,
|
||||
std::filesystem::path folder, int layer);
|
||||
fs::path folder, int layer);
|
||||
|
||||
void writeRegions(regionsmap& regions,
|
||||
const std::filesystem::path& folder, int layer);
|
||||
const fs::path& folder, int layer);
|
||||
|
||||
ubyte* getData(regionsmap& regions,
|
||||
const std::filesystem::path& folder,
|
||||
const fs::path& folder,
|
||||
int x, int z, int layer);
|
||||
|
||||
regfile* getRegFile(glm::ivec3 coord,
|
||||
const std::filesystem::path& folder);
|
||||
regfile* getRegFile(glm::ivec3 coord, const fs::path& folder);
|
||||
public:
|
||||
static bool parseRegionFilename(const std::string& name, int& x, int& y);
|
||||
std::filesystem::path getRegionsFolder() const;
|
||||
fs::path getRegionsFolder() const;
|
||||
|
||||
regionsmap regions;
|
||||
regionsmap lights;
|
||||
std::filesystem::path directory;
|
||||
fs::path directory;
|
||||
std::unique_ptr<ubyte[]> compressionBuffer;
|
||||
bool generatorTestMode;
|
||||
bool doWriteLights;
|
||||
|
||||
WorldFiles(std::filesystem::path directory, const DebugSettings& settings);
|
||||
WorldFiles(fs::path directory, const DebugSettings& settings);
|
||||
~WorldFiles();
|
||||
|
||||
void put(Chunk* chunk);
|
||||
@ -142,7 +139,7 @@ public:
|
||||
|
||||
void writeRegion(int x, int y,
|
||||
WorldRegion* entry,
|
||||
std::filesystem::path file,
|
||||
fs::path file,
|
||||
int layer);
|
||||
void writePlayer(Player* player);
|
||||
/* @param world world info to save (nullable) */
|
||||
|
||||
@ -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_
|
||||
@ -7,6 +7,7 @@
|
||||
#include <stdexcept>
|
||||
#include "../coders/json.h"
|
||||
#include "../util/stringutil.h"
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
@ -92,16 +93,41 @@ bool files::write_string(fs::path filename, const std::string content) {
|
||||
return true;
|
||||
}
|
||||
|
||||
json::JObject* files::read_json(fs::path file) {
|
||||
std::string text = files::read_string(file);
|
||||
bool files::write_json(fs::path filename, const dynamic::Map* obj, bool nice) {
|
||||
// -- binary json tests
|
||||
//return write_binary_json(fs::path(filename.u8string()+".bin"), obj);
|
||||
return files::write_string(filename, json::stringify(obj, nice, " "));
|
||||
}
|
||||
|
||||
bool files::write_binary_json(fs::path filename, const 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 {
|
||||
return json::parse(file.string(), text);
|
||||
auto obj = json::parse(filename.string(), text);
|
||||
//write_binary_json(binfile, obj);
|
||||
return obj;
|
||||
} catch (const parsing_error& error) {
|
||||
std::cerr << error.errorLog() << std::endl;
|
||||
throw std::runtime_error("could not to parse "+file.string());
|
||||
throw std::runtime_error("could not to parse "+filename.string());
|
||||
}
|
||||
}
|
||||
|
||||
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::ifstream file(filename);
|
||||
if (!file) {
|
||||
|
||||
@ -3,12 +3,15 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include "../typedefs.h"
|
||||
|
||||
namespace json {
|
||||
class JObject;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace dynamic {
|
||||
class Map;
|
||||
}
|
||||
|
||||
namespace files {
|
||||
@ -25,14 +28,18 @@ namespace files {
|
||||
};
|
||||
|
||||
|
||||
extern bool write_bytes(std::filesystem::path, const char* data, size_t size);
|
||||
extern uint append_bytes(std::filesystem::path, const char* data, size_t size);
|
||||
extern bool read(std::filesystem::path, char* data, size_t size);
|
||||
extern char* read_bytes(std::filesystem::path, size_t& length);
|
||||
extern std::string read_string(std::filesystem::path filename);
|
||||
extern bool write_string(std::filesystem::path filename, const std::string content);
|
||||
extern json::JObject* read_json(std::filesystem::path file);
|
||||
extern std::vector<std::string> read_list(std::filesystem::path file);
|
||||
extern bool write_bytes(fs::path, const char* data, size_t size);
|
||||
extern uint append_bytes(fs::path, const char* data, size_t size);
|
||||
extern bool write_string(fs::path filename, const std::string content);
|
||||
extern bool write_json(fs::path filename, const dynamic::Map* obj, bool nice=true);
|
||||
extern bool write_binary_json(fs::path filename, const dynamic::Map* obj);
|
||||
|
||||
extern bool read(fs::path, char* data, size_t size);
|
||||
extern char* read_bytes(fs::path, size_t& length);
|
||||
extern std::string read_string(fs::path filename);
|
||||
extern 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_ */
|
||||
@ -9,6 +9,8 @@
|
||||
#include "../coders/toml.h"
|
||||
#include "../coders/json.h"
|
||||
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
toml::Wrapper* create_wrapper(EngineSettings& settings) {
|
||||
std::unique_ptr<toml::Wrapper> wrapper (new toml::Wrapper());
|
||||
toml::Section& display = wrapper->add("display");
|
||||
@ -46,28 +48,27 @@ toml::Wrapper* create_wrapper(EngineSettings& settings) {
|
||||
}
|
||||
|
||||
std::string write_controls() {
|
||||
json::JObject* obj = new json::JObject();
|
||||
dynamic::Map obj;
|
||||
for (auto& entry : Events::bindings) {
|
||||
const auto& binding = entry.second;
|
||||
|
||||
json::JObject* jentry = new json::JObject();
|
||||
auto& jentry = obj.putMap(entry.first);
|
||||
switch (binding.type) {
|
||||
case inputtype::keyboard: jentry->put("type", "keyboard"); break;
|
||||
case inputtype::mouse: jentry->put("type", "mouse"); break;
|
||||
case inputtype::keyboard: jentry.put("type", "keyboard"); break;
|
||||
case inputtype::mouse: jentry.put("type", "mouse"); break;
|
||||
default: throw std::runtime_error("unsupported control type");
|
||||
}
|
||||
jentry->put("code", binding.code);
|
||||
obj->put(entry.first, jentry);
|
||||
jentry.put("code", binding.code);
|
||||
}
|
||||
return json::stringify(obj, true, " ");
|
||||
return json::stringify(&obj, true, " ");
|
||||
}
|
||||
|
||||
void load_controls(std::string filename, std::string source) {
|
||||
json::JObject* obj = json::parse(filename, source);
|
||||
auto obj = json::parse(filename, source);
|
||||
for (auto& entry : Events::bindings) {
|
||||
auto& binding = entry.second;
|
||||
|
||||
json::JObject* jentry = obj->obj(entry.first);
|
||||
auto jentry = obj->map(entry.first);
|
||||
if (jentry == nullptr)
|
||||
continue;
|
||||
inputtype type;
|
||||
|
||||
@ -8,12 +8,12 @@
|
||||
#include "../voxels/Block.h"
|
||||
|
||||
ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) {
|
||||
const ContentIndices* contentIds = content->indices;
|
||||
sideregions = new UVRegion[contentIds->countBlockDefs() * 6];
|
||||
auto indices = content->getIndices();
|
||||
sideregions = new UVRegion[indices->countBlockDefs() * 6];
|
||||
Atlas* atlas = assets->getAtlas("blocks");
|
||||
|
||||
for (uint i = 0; i < contentIds->countBlockDefs(); i++) {
|
||||
Block* def = contentIds->getBlockDef(i);
|
||||
for (uint i = 0; i < indices->countBlockDefs(); i++) {
|
||||
Block* def = indices->getBlockDef(i);
|
||||
for (uint side = 0; side < 6; side++) {
|
||||
std::string tex = def->textureFaces[side];
|
||||
if (atlas->has(tex)) {
|
||||
|
||||
@ -22,7 +22,7 @@ InventoryView::InventoryView(
|
||||
LevelFrontend* frontend,
|
||||
std::vector<itemid_t> items)
|
||||
: content(content),
|
||||
indices(content->indices),
|
||||
indices(content->getIndices()),
|
||||
items(items),
|
||||
frontend(frontend),
|
||||
columns(columns) {
|
||||
|
||||
@ -132,7 +132,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
|
||||
1.0f+fog*2.0f, 4);
|
||||
|
||||
const Content* content = level->content;
|
||||
const ContentIndices* contentIds = content->indices;
|
||||
auto indices = content->getIndices();
|
||||
Assets* assets = engine->getAssets();
|
||||
Atlas* atlas = assets->getAtlas("blocks");
|
||||
Shader* shader = assets->getShader("main");
|
||||
@ -170,7 +170,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
|
||||
shader->uniform1i("u_cubemap", 1);
|
||||
{
|
||||
itemid_t id = level->player->getChosenItem();
|
||||
ItemDef* item = contentIds->getItemDef(id);
|
||||
ItemDef* item = indices->getItemDef(id);
|
||||
assert(item != nullptr);
|
||||
float multiplier = 0.5f;
|
||||
shader->uniform3f("u_torchlightColor",
|
||||
@ -189,7 +189,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
|
||||
// Selected block
|
||||
if (PlayerController::selectedBlockId != -1 && hudVisible){
|
||||
blockid_t id = PlayerController::selectedBlockId;
|
||||
Block* block = contentIds->getBlockDef(id);
|
||||
Block* block = indices->getBlockDef(id);
|
||||
assert(block != nullptr);
|
||||
const vec3 pos = PlayerController::selectedBlockPosition;
|
||||
const vec3 point = PlayerController::selectedPointPosition;
|
||||
|
||||
@ -82,7 +82,7 @@ void HudRenderer::createDebugPanel(Engine* engine) {
|
||||
}));
|
||||
panel->add(create_label([=](){
|
||||
auto player = level->player;
|
||||
auto indices = level->content->indices;
|
||||
auto* indices = level->content->getIndices();
|
||||
auto def = indices->getBlockDef(player->selectedVoxel.id);
|
||||
std::wstringstream stream;
|
||||
stream << std::hex << level->player->selectedVoxel.states;
|
||||
@ -171,7 +171,7 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
|
||||
auto level = frontend->getLevel();
|
||||
auto menu = gui->getMenu();
|
||||
auto content = level->content;
|
||||
auto indices = content->indices;
|
||||
auto indices = content->getIndices();
|
||||
|
||||
std::vector<itemid_t> items;
|
||||
for (itemid_t id = 1; id < indices->countItemDefs(); id++) {
|
||||
|
||||
@ -7,22 +7,17 @@
|
||||
#include "../../content/ContentPack.h"
|
||||
#include "../../files/files.h"
|
||||
#include "../../util/stringutil.h"
|
||||
#include "../../data/dynamic.h"
|
||||
|
||||
using std::string;
|
||||
using std::wstring;
|
||||
using std::vector;
|
||||
using std::unique_ptr;
|
||||
using std::unordered_map;
|
||||
using std::filesystem::path;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
unique_ptr<langs::Lang> langs::current;
|
||||
unordered_map<string, langs::LocaleInfo> langs::locales_info;
|
||||
std::unique_ptr<langs::Lang> langs::current;
|
||||
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);
|
||||
if (found == map.end()) {
|
||||
return key;
|
||||
@ -30,11 +25,11 @@ const wstring& langs::Lang::get(const wstring& key) const {
|
||||
return found->second;
|
||||
}
|
||||
|
||||
void langs::Lang::put(const wstring& key, const wstring& text) {
|
||||
void langs::Lang::put(const std::wstring& key, const std::wstring& text) {
|
||||
map[key] = text;
|
||||
}
|
||||
|
||||
const string& langs::Lang::getId() const {
|
||||
const std::string& langs::Lang::getId() const {
|
||||
return locale;
|
||||
}
|
||||
|
||||
@ -50,16 +45,16 @@ class Reader : public BasicParser {
|
||||
}
|
||||
}
|
||||
public:
|
||||
Reader(string file, string source) : BasicParser(file, source) {
|
||||
Reader(std::string file, std::string source) : BasicParser(file, source) {
|
||||
}
|
||||
|
||||
void read(langs::Lang& lang, std::string prefix) {
|
||||
skipWhitespace();
|
||||
while (hasNext()) {
|
||||
string key = parseString('=', true);
|
||||
std::string key = parseString('=', true);
|
||||
util::trim(key);
|
||||
key = prefix + key;
|
||||
string text = parseString('\n', false);
|
||||
std::string text = parseString('\n', false);
|
||||
util::trim(text);
|
||||
lang.put(util::str2wstr_utf8(key),
|
||||
util::str2wstr_utf8(text));
|
||||
@ -68,22 +63,22 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
void langs::loadLocalesInfo(const path& resdir, string& fallback) {
|
||||
path file = resdir/path(langs::TEXTS_FOLDER)/path("langs.json");
|
||||
unique_ptr<json::JObject> root (files::read_json(file));
|
||||
void langs::loadLocalesInfo(const fs::path& resdir, std::string& fallback) {
|
||||
fs::path file = resdir/fs::path(langs::TEXTS_FOLDER)/fs::path("langs.json");
|
||||
auto root = files::read_json(file);
|
||||
|
||||
langs::locales_info.clear();
|
||||
root->str("fallback", fallback);
|
||||
|
||||
auto langs = root->obj("langs");
|
||||
auto langs = root->map("langs");
|
||||
if (langs) {
|
||||
std::cout << "locales ";
|
||||
for (auto& entry : langs->map) {
|
||||
auto langInfo = entry.second;
|
||||
for (auto& entry : langs->values) {
|
||||
auto langInfo = entry.second.get();
|
||||
|
||||
string name;
|
||||
if (langInfo->type == json::valtype::object) {
|
||||
name = langInfo->value.obj->getStr("name", "none");
|
||||
std::string name;
|
||||
if (langInfo->type == dynamic::valtype::map) {
|
||||
name = langInfo->value.map->getStr("name", "none");
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
@ -95,8 +90,8 @@ void langs::loadLocalesInfo(const path& resdir, string& fallback) {
|
||||
}
|
||||
}
|
||||
|
||||
std::string langs::locale_by_envlocale(const std::string& envlocale, const path& resdir){
|
||||
string fallback = FALLBACK_DEFAULT;
|
||||
std::string langs::locale_by_envlocale(const std::string& envlocale, const fs::path& resdir){
|
||||
std::string fallback = FALLBACK_DEFAULT;
|
||||
if (locales_info.size() == 0) {
|
||||
loadLocalesInfo(resdir, fallback);
|
||||
}
|
||||
@ -116,32 +111,33 @@ std::string langs::locale_by_envlocale(const std::string& envlocale, const path&
|
||||
}
|
||||
}
|
||||
|
||||
void langs::load(const path& resdir,
|
||||
const string& locale,
|
||||
const vector<ContentPack>& packs,
|
||||
void langs::load(const fs::path& resdir,
|
||||
const std::string& locale,
|
||||
const std::vector<ContentPack>& packs,
|
||||
Lang& lang) {
|
||||
path filename = path(TEXTS_FOLDER)/path(locale + LANG_FILE_EXT);
|
||||
path core_file = resdir/filename;
|
||||
fs::path filename = fs::path(TEXTS_FOLDER)/fs::path(locale + LANG_FILE_EXT);
|
||||
fs::path core_file = resdir/filename;
|
||||
|
||||
if (fs::is_regular_file(core_file)) {
|
||||
string text = files::read_string(core_file);
|
||||
std::string text = files::read_string(core_file);
|
||||
Reader reader(core_file.string(), text);
|
||||
reader.read(lang, "");
|
||||
}
|
||||
for (auto pack : packs) {
|
||||
path file = pack.folder/filename;
|
||||
fs::path file = pack.folder/filename;
|
||||
if (fs::is_regular_file(file)) {
|
||||
string text = files::read_string(file);
|
||||
std::string text = files::read_string(file);
|
||||
Reader reader(file.string(), text);
|
||||
reader.read(lang, pack.id+":");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void langs::load(const path& resdir,
|
||||
const string& locale,
|
||||
const string& fallback,
|
||||
const vector<ContentPack>& packs) {
|
||||
unique_ptr<Lang> lang (new Lang(locale));
|
||||
void langs::load(const fs::path& resdir,
|
||||
const std::string& locale,
|
||||
const std::string& fallback,
|
||||
const std::vector<ContentPack>& packs) {
|
||||
auto lang = std::make_unique<Lang>(locale);
|
||||
load(resdir, fallback, packs, *lang.get());
|
||||
if (locale != fallback) {
|
||||
load(resdir, locale, packs, *lang.get());
|
||||
@ -149,10 +145,10 @@ void langs::load(const path& resdir,
|
||||
current.reset(lang.release());
|
||||
}
|
||||
|
||||
void langs::setup(const path& resdir,
|
||||
string locale,
|
||||
const vector<ContentPack>& packs) {
|
||||
string fallback = langs::FALLBACK_DEFAULT;
|
||||
void langs::setup(const fs::path& resdir,
|
||||
std::string locale,
|
||||
const std::vector<ContentPack>& packs) {
|
||||
std::string fallback = langs::FALLBACK_DEFAULT;
|
||||
langs::loadLocalesInfo(resdir, fallback);
|
||||
if (langs::locales_info.find(locale) == langs::locales_info.end()) {
|
||||
locale = fallback;
|
||||
@ -160,13 +156,13 @@ void langs::setup(const path& resdir,
|
||||
langs::load(resdir, locale, fallback, packs);
|
||||
}
|
||||
|
||||
const wstring& langs::get(const wstring& key) {
|
||||
const std::wstring& langs::get(const std::wstring& key) {
|
||||
return current->get(key);
|
||||
}
|
||||
|
||||
const wstring& langs::get(const wstring& key, const wstring& context) {
|
||||
wstring ctxkey = context + L"." + key;
|
||||
const wstring& text = current->get(ctxkey);
|
||||
const std::wstring& langs::get(const std::wstring& key, const std::wstring& context) {
|
||||
std::wstring ctxkey = context + L"." + key;
|
||||
const std::wstring& text = current->get(ctxkey);
|
||||
if (&ctxkey != &text) {
|
||||
return text;
|
||||
}
|
||||
|
||||
@ -96,9 +96,8 @@ LevelScreen::LevelScreen(Engine* engine, Level* level)
|
||||
|
||||
LevelScreen::~LevelScreen() {
|
||||
std::cout << "-- writing world" << std::endl;
|
||||
World* world = level->world;
|
||||
auto world = level->getWorld();
|
||||
world->write(level.get());
|
||||
delete world;
|
||||
}
|
||||
|
||||
void LevelScreen::updateHotkeys() {
|
||||
|
||||
@ -34,7 +34,7 @@ BlocksRenderer::BlocksRenderer(size_t capacity,
|
||||
vertexBuffer = new float[capacity];
|
||||
indexBuffer = new int[capacity];
|
||||
voxelsBuffer = new VoxelsVolume(CHUNK_W + 2, CHUNK_H, CHUNK_D + 2);
|
||||
blockDefsCache = content->indices->getBlockDefs();
|
||||
blockDefsCache = content->getIndices()->getBlockDefs();
|
||||
}
|
||||
|
||||
BlocksRenderer::~BlocksRenderer() {
|
||||
@ -44,9 +44,7 @@ BlocksRenderer::~BlocksRenderer() {
|
||||
}
|
||||
|
||||
/* Basic vertex add method */
|
||||
void BlocksRenderer::vertex(const vec3& coord,
|
||||
float u, float v,
|
||||
const vec4& light) {
|
||||
void BlocksRenderer::vertex(const vec3& coord, float u, float v, const vec4& light) {
|
||||
vertexBuffer[vertexOffset++] = coord.x;
|
||||
vertexBuffer[vertexOffset++] = coord.y;
|
||||
vertexBuffer[vertexOffset++] = coord.z;
|
||||
|
||||
@ -15,11 +15,11 @@ using std::shared_ptr;
|
||||
|
||||
Lighting::Lighting(const Content* content, Chunks* chunks)
|
||||
: content(content), chunks(chunks) {
|
||||
const ContentIndices* contentIds = content->indices;
|
||||
solverR = new LightSolver(contentIds, chunks, 0);
|
||||
solverG = new LightSolver(contentIds, chunks, 1);
|
||||
solverB = new LightSolver(contentIds, chunks, 2);
|
||||
solverS = new LightSolver(contentIds, chunks, 3);
|
||||
auto indices = content->getIndices();
|
||||
solverR = new LightSolver(indices, chunks, 0);
|
||||
solverG = new LightSolver(indices, chunks, 1);
|
||||
solverB = new LightSolver(indices, chunks, 2);
|
||||
solverS = new LightSolver(indices, chunks, 3);
|
||||
}
|
||||
|
||||
Lighting::~Lighting(){
|
||||
@ -42,7 +42,7 @@ void Lighting::clear(){
|
||||
}
|
||||
|
||||
void Lighting::prebuildSkyLight(int cx, int cz){
|
||||
const Block* const* blockDefs = content->indices->getBlockDefs();
|
||||
const Block* const* blockDefs = content->getIndices()->getBlockDefs();
|
||||
|
||||
Chunk* chunk = chunks->getChunk(cx, cz);
|
||||
int highestPoint = 0;
|
||||
@ -68,7 +68,7 @@ void Lighting::prebuildSkyLight(int cx, int cz){
|
||||
}
|
||||
|
||||
void Lighting::buildSkyLight(int cx, int cz){
|
||||
const Block* const* blockDefs = content->indices->getBlockDefs();
|
||||
const Block* const* blockDefs = content->getIndices()->getBlockDefs();
|
||||
|
||||
Chunk* chunk = chunks->getChunk(cx, cz);
|
||||
for (int z = 0; z < CHUNK_D; z++){
|
||||
@ -95,7 +95,7 @@ void Lighting::buildSkyLight(int cx, int cz){
|
||||
}
|
||||
|
||||
void Lighting::onChunkLoaded(int cx, int cz){
|
||||
const Block* const* blockDefs = content->indices->getBlockDefs();
|
||||
const Block* const* blockDefs = content->getIndices()->getBlockDefs();
|
||||
const Chunk* chunk = chunks->getChunk(cx, cz);
|
||||
|
||||
for (unsigned int y = 0; y < CHUNK_H; y++){
|
||||
@ -137,7 +137,7 @@ void Lighting::onChunkLoaded(int cx, int cz){
|
||||
}
|
||||
|
||||
void Lighting::onBlockSet(int x, int y, int z, int const id){
|
||||
Block* block = content->indices->getBlockDef(id);
|
||||
Block* block = content->getIndices()->getBlockDef(id);
|
||||
if (id == 0){
|
||||
solverR->remove(x,y,z);
|
||||
solverG->remove(x,y,z);
|
||||
|
||||
@ -69,7 +69,7 @@ void BlocksController::updateBlock(int x, int y, int z) {
|
||||
voxel* vox = chunks->get(x, y, z);
|
||||
if (vox == nullptr)
|
||||
return;
|
||||
const Block* def = level->content->indices->getBlockDef(vox->id);
|
||||
const Block* def = level->content->getIndices()->getBlockDef(vox->id);
|
||||
if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
|
||||
breakBlock(nullptr, def, x, y, z);
|
||||
return;
|
||||
@ -89,7 +89,7 @@ void BlocksController::randomTick(int tickid, int parts) {
|
||||
// timeutil::ScopeLogTimer timer(5000+tickid);
|
||||
const int w = chunks->w;
|
||||
const int d = chunks->d;
|
||||
auto indices = level->content->indices;
|
||||
auto indices = level->content->getIndices();
|
||||
for (uint z = padding; z < d-padding; z++){
|
||||
for (uint x = padding; x < w-padding; x++){
|
||||
int index = z * w + x;
|
||||
|
||||
@ -204,7 +204,7 @@ void PlayerController::updateControls(float delta){
|
||||
}
|
||||
|
||||
void PlayerController::updateInteraction(){
|
||||
auto contentIds = level->content->indices;
|
||||
auto indices = level->content->getIndices();
|
||||
Chunks* chunks = level->chunks;
|
||||
Player* player = level->player;
|
||||
Lighting* lighting = level->lighting;
|
||||
@ -239,8 +239,8 @@ void PlayerController::updateInteraction(){
|
||||
int z = iend.z;
|
||||
uint8_t states = 0;
|
||||
|
||||
ItemDef* item = contentIds->getItemDef(player->getChosenItem());
|
||||
Block* def = contentIds->getBlockDef(item->rt.placingBlock);
|
||||
ItemDef* item = indices->getItemDef(player->getChosenItem());
|
||||
Block* def = indices->getBlockDef(item->rt.placingBlock);
|
||||
if (def && def->rotatable){
|
||||
const std::string& name = def->rotations.name;
|
||||
if (name == "pipe") {
|
||||
@ -270,7 +270,7 @@ void PlayerController::updateInteraction(){
|
||||
}
|
||||
}
|
||||
|
||||
Block* target = contentIds->getBlockDef(vox->id);
|
||||
Block* target = indices->getBlockDef(vox->id);
|
||||
if (lclick && target->breakable){
|
||||
blocksController->breakBlock(player, target, x, y, z);
|
||||
}
|
||||
@ -292,7 +292,7 @@ void PlayerController::updateInteraction(){
|
||||
}
|
||||
vox = chunks->get(x, y, z);
|
||||
blockid_t chosenBlock = def->rt.id;
|
||||
if (vox && (target = contentIds->getBlockDef(vox->id))->replaceable) {
|
||||
if (vox && (target = indices->getBlockDef(vox->id))->replaceable) {
|
||||
if (!level->physics->isBlockInside(x,y,z, player->hitbox.get())
|
||||
|| !def->obstacle){
|
||||
if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
|
||||
@ -310,7 +310,7 @@ void PlayerController::updateInteraction(){
|
||||
}
|
||||
}
|
||||
if (Events::jactive(BIND_PLAYER_PICK)){
|
||||
Block* block = contentIds->getBlockDef(chunks->get(x,y,z)->id);
|
||||
Block* block = indices->getBlockDef(chunks->get(x,y,z)->id);
|
||||
player->setChosenItem(block->rt.pickingItem);
|
||||
}
|
||||
} else {
|
||||
|
||||
@ -14,6 +14,13 @@
|
||||
#include "../../lighting/Lighting.h"
|
||||
#include "../../logic/BlocksController.h"
|
||||
|
||||
inline int lua_pushivec3(lua_State* L, int x, int y, int z) {
|
||||
lua_pushinteger(L, x);
|
||||
lua_pushinteger(L, y);
|
||||
lua_pushinteger(L, z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
inline void luaL_openlib(lua_State* L, const char* name, const luaL_Reg* libfuncs, int nup) {
|
||||
lua_newtable(L);
|
||||
luaL_setfuncs(L, libfuncs, nup);
|
||||
@ -44,11 +51,6 @@ static const luaL_Reg worldlib [] = {
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
int luaopen_world(lua_State* L) {
|
||||
luaL_openlib(L, "world", worldlib, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* == player library ==*/
|
||||
static int l_player_get_pos(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
@ -102,15 +104,11 @@ static const luaL_Reg playerlib [] = {
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
int luaopen_player(lua_State* L) {
|
||||
luaL_openlib(L, "player", playerlib, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* == blocks-related functions == */
|
||||
static int l_block_name(lua_State* L) {
|
||||
int id = lua_tointeger(L, 1);
|
||||
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;
|
||||
}
|
||||
|
||||
@ -124,7 +122,7 @@ static int l_is_solid_at(lua_State* L) {
|
||||
}
|
||||
|
||||
static int l_blocks_count(lua_State* L) {
|
||||
lua_pushinteger(L, scripting::content->indices->countBlockDefs());
|
||||
lua_pushinteger(L, scripting::content->getIndices()->countBlockDefs());
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -158,13 +156,6 @@ static int l_get_block(lua_State* L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int lua_pushivec3(lua_State* L, int x, int y, int z) {
|
||||
lua_pushinteger(L, x);
|
||||
lua_pushinteger(L, y);
|
||||
lua_pushinteger(L, z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
static int l_get_block_x(lua_State* L) {
|
||||
int x = lua_tointeger(L, 1);
|
||||
int y = lua_tointeger(L, 2);
|
||||
@ -173,7 +164,7 @@ static int l_get_block_x(lua_State* L) {
|
||||
if (vox == nullptr) {
|
||||
return lua_pushivec3(L, 1, 0, 0);
|
||||
}
|
||||
const Block* def = scripting::level->content->indices->getBlockDef(vox->id);
|
||||
auto def = scripting::level->content->getIndices()->getBlockDef(vox->id);
|
||||
if (!def->rotatable) {
|
||||
return lua_pushivec3(L, 1, 0, 0);
|
||||
} else {
|
||||
@ -190,7 +181,7 @@ static int l_get_block_y(lua_State* L) {
|
||||
if (vox == nullptr) {
|
||||
return lua_pushivec3(L, 0, 1, 0);
|
||||
}
|
||||
const Block* def = scripting::level->content->indices->getBlockDef(vox->id);
|
||||
auto def = scripting::level->content->getIndices()->getBlockDef(vox->id);
|
||||
if (!def->rotatable) {
|
||||
return lua_pushivec3(L, 0, 1, 0);
|
||||
} else {
|
||||
@ -207,7 +198,7 @@ static int l_get_block_z(lua_State* L) {
|
||||
if (vox == nullptr) {
|
||||
return lua_pushivec3(L, 0, 0, 1);
|
||||
}
|
||||
const Block* def = scripting::level->content->indices->getBlockDef(vox->id);
|
||||
auto def = scripting::level->content->getIndices()->getBlockDef(vox->id);
|
||||
if (!def->rotatable) {
|
||||
return lua_pushivec3(L, 0, 0, 1);
|
||||
} else {
|
||||
@ -275,8 +266,8 @@ static int l_is_replaceable_at(lua_State* L) {
|
||||
lua_setglobal(L, NAME))
|
||||
|
||||
void apilua::create_funcs(lua_State* L) {
|
||||
luaopen_world(L);
|
||||
luaopen_player(L);
|
||||
luaL_openlib(L, "world", worldlib, 0);
|
||||
luaL_openlib(L, "player", playerlib, 0);
|
||||
|
||||
lua_addfunc(L, l_block_index, "block_index");
|
||||
lua_addfunc(L, l_block_name, "block_name");
|
||||
|
||||
@ -26,9 +26,6 @@ Player::Player(glm::vec3 position, float speed) :
|
||||
hitbox(new Hitbox(position, glm::vec3(0.3f,0.9f,0.3f))) {
|
||||
}
|
||||
|
||||
Player::~Player(){
|
||||
}
|
||||
|
||||
void Player::update(
|
||||
Level* level,
|
||||
PlayerInput& input,
|
||||
|
||||
@ -43,7 +43,7 @@ public:
|
||||
glm::vec2 cam = {};
|
||||
|
||||
Player(glm::vec3 position, float speed);
|
||||
~Player();
|
||||
~Player() = default;
|
||||
|
||||
void teleport(glm::vec3 position);
|
||||
void update(Level* level, PlayerInput& input, float delta);
|
||||
|
||||
@ -13,6 +13,8 @@ typedef uint8_t ubyte;
|
||||
typedef uint32_t itemid_t;
|
||||
typedef uint16_t blockid_t;
|
||||
|
||||
typedef uint32_t itemcount_t;
|
||||
|
||||
typedef uint16_t blockstate_t;
|
||||
typedef uint16_t light_t;
|
||||
|
||||
|
||||
@ -15,7 +15,6 @@ Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos){
|
||||
voxels[i].states = 0;
|
||||
}
|
||||
lightmap = new Lightmap();
|
||||
renderData.vertices = nullptr;
|
||||
}
|
||||
|
||||
Chunk::~Chunk(){
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
#include <stdlib.h>
|
||||
#include "../constants.h"
|
||||
|
||||
struct ChunkFlag{
|
||||
struct ChunkFlag {
|
||||
static const int MODIFIED = 0x1;
|
||||
static const int READY = 0x2;
|
||||
static const int LOADED = 0x4;
|
||||
@ -18,11 +18,6 @@ struct voxel;
|
||||
class Lightmap;
|
||||
class ContentLUT;
|
||||
|
||||
struct RenderData {
|
||||
float* vertices;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
class Chunk {
|
||||
public:
|
||||
int x, z;
|
||||
@ -31,7 +26,6 @@ public:
|
||||
Lightmap* lightmap;
|
||||
int flags = 0;
|
||||
int surrounding = 0;
|
||||
RenderData renderData;
|
||||
|
||||
Chunk(int x, int z);
|
||||
~Chunk();
|
||||
@ -43,8 +37,7 @@ public:
|
||||
Chunk* clone() const;
|
||||
|
||||
// flags getters/setters below
|
||||
|
||||
void SETFLAGS(int mask, bool value){
|
||||
inline void setFlags(int mask, bool value){
|
||||
if (value)
|
||||
flags |= mask;
|
||||
else
|
||||
@ -63,17 +56,17 @@ public:
|
||||
|
||||
inline bool isReady() const {return flags & ChunkFlag::READY;}
|
||||
|
||||
inline void setUnsaved(bool newState) {SETFLAGS(ChunkFlag::UNSAVED, newState);}
|
||||
inline void setUnsaved(bool newState) {setFlags(ChunkFlag::UNSAVED, newState);}
|
||||
|
||||
inline void setModified(bool newState) {SETFLAGS(ChunkFlag::MODIFIED, newState);}
|
||||
inline void setModified(bool newState) {setFlags(ChunkFlag::MODIFIED, newState);}
|
||||
|
||||
inline void setLoaded(bool newState) {SETFLAGS(ChunkFlag::LOADED, newState);}
|
||||
inline void setLoaded(bool newState) {setFlags(ChunkFlag::LOADED, newState);}
|
||||
|
||||
inline void setLoadedLights(bool newState) {SETFLAGS(ChunkFlag::LOADED_LIGHTS, newState);}
|
||||
inline void setLoadedLights(bool newState) {setFlags(ChunkFlag::LOADED_LIGHTS, newState);}
|
||||
|
||||
inline void setLighted(bool newState) {SETFLAGS(ChunkFlag::LIGHTED, newState);}
|
||||
inline void setLighted(bool newState) {setFlags(ChunkFlag::LIGHTED, newState);}
|
||||
|
||||
inline void setReady(bool newState) {SETFLAGS(ChunkFlag::READY, newState);}
|
||||
inline void setReady(bool newState) {setFlags(ChunkFlag::READY, newState);}
|
||||
|
||||
ubyte* encode() const;
|
||||
bool decode(ubyte* data);
|
||||
|
||||
@ -16,38 +16,22 @@
|
||||
#include <math.h>
|
||||
#include <limits.h>
|
||||
|
||||
using glm::vec3;
|
||||
using glm::ivec3;
|
||||
using std::shared_ptr;
|
||||
|
||||
Chunks::Chunks(int w, int d,
|
||||
int ox, int oz,
|
||||
WorldFiles* wfile,
|
||||
LevelEvents* events,
|
||||
const Content* content)
|
||||
: content(content),
|
||||
contentIds(content->indices),
|
||||
contentIds(content->getIndices()),
|
||||
chunks(w*d),
|
||||
chunksSecond(w*d),
|
||||
w(w), d(d), ox(ox), oz(oz),
|
||||
worldFiles(wfile),
|
||||
events(events) {
|
||||
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;
|
||||
}
|
||||
|
||||
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){
|
||||
x -= ox * CHUNK_W;
|
||||
z -= oz * CHUNK_D;
|
||||
@ -56,7 +40,7 @@ voxel* Chunks::get(int x, int y, int z){
|
||||
int cz = floordiv(z, CHUNK_D);
|
||||
if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d)
|
||||
return nullptr;
|
||||
shared_ptr<Chunk> chunk = chunks[cz * w + cx]; // chunks is 2D-array
|
||||
std::shared_ptr<Chunk> chunk = chunks[cz * w + cx];
|
||||
if (chunk == nullptr)
|
||||
return nullptr;
|
||||
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);
|
||||
if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d)
|
||||
return 0;
|
||||
shared_ptr<Chunk> chunk = chunks[(cy * d + cz) * w + cx];
|
||||
auto chunk = chunks[(cy * d + cz) * w + cx];
|
||||
if (chunk == nullptr)
|
||||
return 0;
|
||||
int lx = x - cx * CHUNK_W;
|
||||
@ -134,7 +118,7 @@ light_t Chunks::getLight(int x, int y, int z){
|
||||
int cz = floordiv(z, CHUNK_D);
|
||||
if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d)
|
||||
return 0;
|
||||
shared_ptr<Chunk> chunk = chunks[(cy * d + cz) * w + cx];
|
||||
auto chunk = chunks[(cy * d + cz) * w + cx];
|
||||
if (chunk == nullptr)
|
||||
return 0;
|
||||
int lx = x - cx * CHUNK_W;
|
||||
@ -197,12 +181,12 @@ void Chunks::set(int x, int y, int z, int id, uint8_t states){
|
||||
chunk->setModified(true);
|
||||
}
|
||||
|
||||
voxel* Chunks::rayCast(vec3 start,
|
||||
vec3 dir,
|
||||
voxel* Chunks::rayCast(glm::vec3 start,
|
||||
glm::vec3 dir,
|
||||
float maxDist,
|
||||
vec3& end,
|
||||
ivec3& norm,
|
||||
ivec3& iend) {
|
||||
glm::vec3& end,
|
||||
glm::ivec3& norm,
|
||||
glm::ivec3& iend) {
|
||||
float px = start.x;
|
||||
float py = start.y;
|
||||
float pz = start.z;
|
||||
@ -256,7 +240,7 @@ voxel* Chunks::rayCast(vec3 start,
|
||||
scalar_t distance;
|
||||
Ray ray(start, dir);
|
||||
if (ray.intersectAABB(iend, box, maxDist, norm, distance) > RayRelation::None){
|
||||
end = start + (dir * vec3(distance));
|
||||
end = start + (dir * glm::vec3(distance));
|
||||
return voxel;
|
||||
}
|
||||
|
||||
@ -309,7 +293,7 @@ voxel* Chunks::rayCast(vec3 start,
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) {
|
||||
glm::vec3 Chunks::rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDist) {
|
||||
float px = start.x;
|
||||
float py = start.y;
|
||||
float pz = start.z;
|
||||
@ -343,7 +327,7 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) {
|
||||
|
||||
while (t <= maxDist) {
|
||||
voxel* voxel = get(ix, iy, iz);
|
||||
if (voxel == nullptr) { return vec3(px + t * dx, py + t * dy, pz + t * dz); }
|
||||
if (voxel == nullptr) { return glm::vec3(px + t * dx, py + t * dy, pz + t * dz); }
|
||||
|
||||
const Block* def = contentIds->getBlockDef(voxel->id);
|
||||
if (def->obstacle) {
|
||||
@ -352,15 +336,15 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) {
|
||||
? def->rt.hitboxes[voxel->rotation()]
|
||||
: def->hitbox;
|
||||
scalar_t distance;
|
||||
ivec3 norm;
|
||||
glm::ivec3 norm;
|
||||
Ray ray(start, dir);
|
||||
// norm is dummy now, can be inefficient
|
||||
if (ray.intersectAABB(ivec3(ix, iy, iz), box, maxDist, norm, distance) > RayRelation::None) {
|
||||
return start + (dir * vec3(distance));
|
||||
if (ray.intersectAABB(glm::ivec3(ix, iy, iz), box, maxDist, norm, distance) > RayRelation::None) {
|
||||
return start + (dir * glm::vec3(distance));
|
||||
}
|
||||
}
|
||||
else {
|
||||
return vec3(px + t * dx, py + t * dy, pz + t * dz);
|
||||
return glm::vec3(px + t * dx, py + t * dy, pz + t * dz);
|
||||
}
|
||||
}
|
||||
if (txMax < tyMax) {
|
||||
@ -388,7 +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 x = 0; x < w; x++){
|
||||
shared_ptr<Chunk> chunk = chunks[z * w + x];
|
||||
auto chunk = chunks[z * w + x];
|
||||
int nx = x - dx;
|
||||
int nz = z - dz;
|
||||
if (chunk == nullptr)
|
||||
@ -443,20 +427,18 @@ void Chunks::resize(int newW, int newD) {
|
||||
translate(0, delta);
|
||||
}
|
||||
const int newVolume = newW * newD;
|
||||
auto newChunks = new shared_ptr<Chunk>[newVolume] {};
|
||||
auto newChunksSecond = new shared_ptr<Chunk>[newVolume] {};
|
||||
std::vector<std::shared_ptr<Chunk>> newChunks(newVolume);
|
||||
std::vector<std::shared_ptr<Chunk>> newChunksSecond(newVolume);
|
||||
for (int z = 0; z < d && z < newD; z++) {
|
||||
for (int x = 0; x < w && x < newW; x++) {
|
||||
newChunks[z * newW + x] = chunks[z * w + x];
|
||||
}
|
||||
}
|
||||
delete[] chunks;
|
||||
delete[] chunksSecond;
|
||||
w = newW;
|
||||
d = newD;
|
||||
volume = newVolume;
|
||||
chunks = newChunks;
|
||||
chunksSecond = newChunksSecond;
|
||||
w = newW;
|
||||
d = newD;
|
||||
volume = newVolume;
|
||||
chunks = std::move(newChunks);
|
||||
chunksSecond = std::move(newChunksSecond);
|
||||
}
|
||||
|
||||
void Chunks::_setOffset(int x, int z){
|
||||
@ -464,7 +446,7 @@ void Chunks::_setOffset(int x, int z){
|
||||
oz = z;
|
||||
}
|
||||
|
||||
bool Chunks::putChunk(shared_ptr<Chunk> chunk) {
|
||||
bool Chunks::putChunk(std::shared_ptr<Chunk> chunk) {
|
||||
int x = chunk->x;
|
||||
int z = chunk->z;
|
||||
x -= ox;
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
#define VOXELS_CHUNKS_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <glm/glm.hpp>
|
||||
#include "../typedefs.h"
|
||||
@ -21,8 +22,8 @@ class Chunks {
|
||||
const Content* const content;
|
||||
const ContentIndices* const contentIds;
|
||||
public:
|
||||
std::shared_ptr<Chunk>* chunks;
|
||||
std::shared_ptr<Chunk>* chunksSecond;
|
||||
std::vector<std::shared_ptr<Chunk>> chunks;
|
||||
std::vector<std::shared_ptr<Chunk>> chunksSecond;
|
||||
size_t volume;
|
||||
size_t chunksCount;
|
||||
size_t visible;
|
||||
@ -33,7 +34,7 @@ public:
|
||||
|
||||
Chunks(int w, int d, int ox, int oz,
|
||||
WorldFiles* worldFiles, LevelEvents* events, const Content* content);
|
||||
~Chunks();
|
||||
~Chunks() = default;
|
||||
|
||||
bool putChunk(std::shared_ptr<Chunk> chunk);
|
||||
|
||||
|
||||
@ -17,9 +17,6 @@
|
||||
ChunksStorage::ChunksStorage(Level* level) : level(level) {
|
||||
}
|
||||
|
||||
ChunksStorage::~ChunksStorage() {
|
||||
}
|
||||
|
||||
void ChunksStorage::store(std::shared_ptr<Chunk> 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++) {
|
||||
blockid_t id = chunk->voxels[i].id;
|
||||
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) {
|
||||
World* world = level->world;
|
||||
World* world = level->getWorld();
|
||||
WorldFiles* wfile = world->wfile;
|
||||
|
||||
auto chunk = std::make_shared<Chunk>(x, z);
|
||||
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) {
|
||||
chunk->decode(data.get());
|
||||
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) {
|
||||
chunk->lightmap->set(lights);
|
||||
chunk->setLoadedLights(true);
|
||||
@ -74,7 +72,7 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
|
||||
// some magic code
|
||||
void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const {
|
||||
const Content* content = level->content;
|
||||
const ContentIndices* indices = content->indices;
|
||||
auto indices = content->getIndices();
|
||||
voxel* voxels = volume->getVoxels();
|
||||
light_t* lights = volume->getLights();
|
||||
int x = volume->getX();
|
||||
@ -114,7 +112,7 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const std::shared_ptr<Chunk>& chunk = found->second;
|
||||
auto& chunk = found->second;
|
||||
const voxel* cvoxels = chunk->voxels;
|
||||
const light_t* clights = chunk->lightmap->getLights();
|
||||
for (int ly = y; ly < y + h; ly++) {
|
||||
|
||||
@ -18,7 +18,7 @@ class ChunksStorage {
|
||||
std::unordered_map<glm::ivec2, std::shared_ptr<Chunk>> chunksMap;
|
||||
public:
|
||||
ChunksStorage(Level* level);
|
||||
virtual ~ChunksStorage();
|
||||
~ChunksStorage() = default;
|
||||
|
||||
std::shared_ptr<Chunk> get(int x, int z) const;
|
||||
void store(std::shared_ptr<Chunk> chunk);
|
||||
|
||||
@ -49,3 +49,7 @@ void Level::update() {
|
||||
chunks->resize(matrixSize, matrixSize);
|
||||
}
|
||||
}
|
||||
|
||||
World* Level::getWorld() {
|
||||
return world.get();
|
||||
}
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
#ifndef WORLD_LEVEL_H_
|
||||
#define WORLD_LEVEL_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../typedefs.h"
|
||||
#include "../settings.h"
|
||||
|
||||
@ -15,7 +17,7 @@ class ChunksStorage;
|
||||
|
||||
class Level {
|
||||
public:
|
||||
World* world;
|
||||
std::unique_ptr<World> world;
|
||||
const Content* const content;
|
||||
Player* player;
|
||||
Chunks* chunks;
|
||||
@ -34,6 +36,8 @@ public:
|
||||
~Level();
|
||||
|
||||
void update();
|
||||
|
||||
World* getWorld();
|
||||
};
|
||||
|
||||
#endif /* WORLD_LEVEL_H_ */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user