the big refactor + extracted data classes from coders/json to data/dynamic
This commit is contained in:
+18
-16
@@ -18,6 +18,8 @@
|
||||
#include "../constants.h"
|
||||
#include "../items/ItemDef.h"
|
||||
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <cstdint>
|
||||
@@ -444,7 +446,7 @@ void WorldFiles::write(const World* world, const Content* content) {
|
||||
if (generatorTestMode)
|
||||
return;
|
||||
|
||||
writeIndices(content->indices);
|
||||
writeIndices(content->getIndices());
|
||||
writeRegions(regions, regionsFolder, REGION_LAYER_VOXELS);
|
||||
writeRegions(lights, lightsFolder, REGION_LAYER_LIGHTS);
|
||||
}
|
||||
@@ -460,16 +462,16 @@ void WorldFiles::writePacks(const World* world) {
|
||||
}
|
||||
|
||||
void WorldFiles::writeIndices(const ContentIndices* indices) {
|
||||
json::JObject root;
|
||||
dynamic::Map root;
|
||||
uint count;
|
||||
json::JArray& blocks = root.putArray("blocks");
|
||||
auto& blocks = root.putList("blocks");
|
||||
count = indices->countBlockDefs();
|
||||
for (uint i = 0; i < count; i++) {
|
||||
const Block* def = indices->getBlockDef(i);
|
||||
blocks.put(def->name);
|
||||
}
|
||||
|
||||
json::JArray& items = root.putArray("items");
|
||||
auto& items = root.putList("items");
|
||||
count = indices->countItemDefs();
|
||||
for (uint i = 0; i < count; i++) {
|
||||
const ItemDef* def = indices->getItemDef(i);
|
||||
@@ -480,16 +482,16 @@ void WorldFiles::writeIndices(const ContentIndices* indices) {
|
||||
}
|
||||
|
||||
void WorldFiles::writeWorldInfo(const World* world) {
|
||||
json::JObject root;
|
||||
dynamic::Map root;
|
||||
|
||||
json::JObject& versionobj = root.putObj("version");
|
||||
auto& versionobj = root.putMap("version");
|
||||
versionobj.put("major", ENGINE_VERSION_MAJOR);
|
||||
versionobj.put("minor", ENGINE_VERSION_MINOR);
|
||||
|
||||
root.put("name", world->name);
|
||||
root.put("seed", world->seed);
|
||||
|
||||
json::JObject& timeobj = root.putObj("time");
|
||||
auto& timeobj = root.putMap("time");
|
||||
timeobj.put("day-time", world->daytime);
|
||||
timeobj.put("day-time-speed", world->daytimeSpeed);
|
||||
|
||||
@@ -503,11 +505,11 @@ bool WorldFiles::readWorldInfo(World* world) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<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);
|
||||
@@ -515,7 +517,7 @@ bool WorldFiles::readWorldInfo(World* world) {
|
||||
std::cout << "world version: " << major << "." << minor << std::endl;
|
||||
}
|
||||
|
||||
json::JObject* timeobj = root->obj("time");
|
||||
auto timeobj = root->map("time");
|
||||
if (timeobj) {
|
||||
timeobj->num("day-time", world->daytime);
|
||||
timeobj->num("day-time-speed", world->daytimeSpeed);
|
||||
@@ -526,13 +528,13 @@ bool WorldFiles::readWorldInfo(World* world) {
|
||||
|
||||
void WorldFiles::writePlayer(Player* player){
|
||||
glm::vec3 position = player->hitbox->position;
|
||||
json::JObject root;
|
||||
json::JArray& posarr = root.putArray("position");
|
||||
dynamic::Map root;
|
||||
auto& posarr = root.putList("position");
|
||||
posarr.put(position.x);
|
||||
posarr.put(position.y);
|
||||
posarr.put(position.z);
|
||||
|
||||
json::JArray& rotarr = root.putArray("rotation");
|
||||
auto& rotarr = root.putList("rotation");
|
||||
rotarr.put(player->cam.x);
|
||||
rotarr.put(player->cam.y);
|
||||
|
||||
@@ -549,15 +551,15 @@ bool WorldFiles::readPlayer(Player* player) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<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);
|
||||
|
||||
|
||||
+22
-25
@@ -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) */
|
||||
|
||||
+6
-5
@@ -7,6 +7,7 @@
|
||||
#include <stdexcept>
|
||||
#include "../coders/json.h"
|
||||
#include "../util/stringutil.h"
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
@@ -92,18 +93,18 @@ bool files::write_string(fs::path filename, const std::string content) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool files::write_json(fs::path filename, const json::JObject* obj, bool nice) {
|
||||
bool files::write_json(fs::path filename, const dynamic::Map* obj, bool nice) {
|
||||
// -- binary json tests
|
||||
//return write_binary_json(fs::path(filename.u8string()+".bin"), obj);
|
||||
return files::write_string(filename, json::stringify(obj, nice, " "));
|
||||
}
|
||||
|
||||
bool files::write_binary_json(fs::path filename, const json::JObject* obj) {
|
||||
bool files::write_binary_json(fs::path filename, const dynamic::Map* obj) {
|
||||
std::vector<ubyte> bytes = json::to_binary(obj);
|
||||
return files::write_bytes(filename, (const char*)bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
json::JObject* files::read_json(fs::path filename) {
|
||||
std::unique_ptr<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)){
|
||||
@@ -121,10 +122,10 @@ json::JObject* files::read_json(fs::path filename) {
|
||||
}
|
||||
}
|
||||
|
||||
json::JObject* files::read_binary_json(fs::path file) {
|
||||
std::unique_ptr<dynamic::Map> files::read_binary_json(fs::path file) {
|
||||
size_t size;
|
||||
std::unique_ptr<char[]> bytes (files::read_bytes(file, size));
|
||||
return json::from_binary((const ubyte*)bytes.get(), size);
|
||||
return std::unique_ptr<dynamic::Map>(json::from_binary((const ubyte*)bytes.get(), size));
|
||||
}
|
||||
|
||||
std::vector<std::string> files::read_list(fs::path filename) {
|
||||
|
||||
+7
-6
@@ -3,14 +3,15 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include "../typedefs.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace json {
|
||||
class JObject;
|
||||
namespace dynamic {
|
||||
class Map;
|
||||
}
|
||||
|
||||
namespace files {
|
||||
@@ -30,14 +31,14 @@ namespace files {
|
||||
extern bool write_bytes(fs::path, const char* data, size_t size);
|
||||
extern uint append_bytes(fs::path, const char* data, size_t size);
|
||||
extern bool write_string(fs::path filename, const std::string content);
|
||||
extern bool write_json(fs::path filename, const json::JObject* obj, bool nice=true);
|
||||
extern bool write_binary_json(fs::path filename, const json::JObject* obj);
|
||||
extern bool write_json(fs::path filename, const dynamic::Map* obj, bool nice=true);
|
||||
extern bool write_binary_json(fs::path filename, const dynamic::Map* obj);
|
||||
|
||||
extern bool read(fs::path, char* data, size_t size);
|
||||
extern char* read_bytes(fs::path, size_t& length);
|
||||
extern std::string read_string(fs::path filename);
|
||||
extern json::JObject* read_json(fs::path file);
|
||||
extern json::JObject* read_binary_json(fs::path file);
|
||||
extern std::unique_ptr<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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user