the big refactor + extracted data classes from coders/json to data/dynamic
This commit is contained in:
@@ -131,7 +131,6 @@ Content::Content(ContentIndices* indices, DrawGroups* drawGroups,
|
||||
}
|
||||
|
||||
Content::~Content() {
|
||||
delete indices;
|
||||
delete drawGroups;
|
||||
}
|
||||
|
||||
|
||||
@@ -92,14 +92,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;
|
||||
|
||||
+13
-18
@@ -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;
|
||||
@@ -93,7 +93,7 @@ void ContentLoader::fixPackIndices() {
|
||||
|
||||
// TODO: add basic validation and logging
|
||||
void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) {
|
||||
std::unique_ptr<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));
|
||||
|
||||
Reference in New Issue
Block a user