Content packs indices auto build + WorldGenerator fix

This commit is contained in:
MihailRis
2023-12-29 14:17:26 +03:00
parent d2a40d31a4
commit 76aeb8e4df
8 changed files with 90 additions and 6 deletions
+60 -1
View File
@@ -3,9 +3,11 @@
#include <iostream>
#include <string>
#include <memory>
#include <algorithm>
#include <glm/glm.hpp>
#include "Content.h"
#include "../util/listutil.h"
#include "../voxels/Block.h"
#include "../files/files.h"
#include "../coders/json.h"
@@ -19,6 +21,61 @@ namespace fs = std::filesystem;
ContentLoader::ContentLoader(ContentPack* pack) : pack(pack) {
}
void ContentLoader::fixPackIndices() {
auto folder = pack->folder;
auto indexFile = pack->getContentFile();
auto blocksFolder = folder/ContentPack::BLOCKS_FOLDER;
std::unique_ptr<json::JObject> root;
if (fs::is_regular_file(indexFile)) {
root.reset(files::read_json(indexFile));
} else {
root.reset(new json::JObject());
}
std::vector<std::string> detectedBlocks;
std::vector<std::string> indexedBlocks;
if (fs::is_directory(blocksFolder)) {
for (auto entry : fs::directory_iterator(blocksFolder)) {
fs::path file = entry.path();
if (fs::is_regular_file(file) && file.extension() == ".json") {
std::string name = file.stem();
if (name[0] == '_')
continue;
detectedBlocks.push_back(name);
}
}
}
bool modified = false;
if (!root->has("blocks")) {
root->putArray("blocks");
}
json::JArray* blocksarr = root->arr("blocks");
if (blocksarr) {
for (uint i = 0; i < blocksarr->size(); i++) {
std::string name = blocksarr->str(i);
if (!util::contains(detectedBlocks, name)) {
blocksarr->remove(i);
i--;
modified = true;
continue;
}
indexedBlocks.push_back(name);
}
}
for (auto name : detectedBlocks) {
if (!util::contains(indexedBlocks, name)) {
blocksarr->put(name);
modified = true;
}
}
if (modified){
// rewrite modified json
std::cout << indexFile << std::endl;
files::write_string(indexFile, json::stringify(root.get(), true, " "));
}
}
// TODO: add basic validation and logging
Block* ContentLoader::loadBlock(std::string name, fs::path file) {
std::unique_ptr<json::JObject> root(files::read_json(file));
@@ -95,8 +152,10 @@ Block* ContentLoader::loadBlock(std::string name, fs::path file) {
void ContentLoader::load(ContentBuilder* builder) {
std::cout << "-- loading pack [" << pack->id << "]" << std::endl;
fixPackIndices();
auto folder = pack->folder;
auto root = files::read_json(pack->getContentFile());
std::unique_ptr<json::JObject> root (files::read_json(pack->getContentFile()));
json::JArray* blocksarr = root->arr("blocks");
if (blocksarr) {
+1
View File
@@ -13,6 +13,7 @@ class ContentLoader {
public:
ContentLoader(ContentPack* pack);
void fixPackIndices();
Block* loadBlock(std::string name, std::filesystem::path file);
void load(ContentBuilder* builder);
};
+1
View File
@@ -10,6 +10,7 @@ namespace fs = std::filesystem;
const std::string ContentPack::PACKAGE_FILENAME = "package.json";
const std::string ContentPack::CONTENT_FILENAME = "content.json";
const fs::path ContentPack::BLOCKS_FOLDER = "blocks";
contentpack_error::contentpack_error(
std::string packId,
+2
View File
@@ -30,6 +30,8 @@ struct ContentPack {
static const std::string PACKAGE_FILENAME;
static const std::string CONTENT_FILENAME;
static const std::filesystem::path BLOCKS_FOLDER;
static bool is_pack(std::filesystem::path folder);
static ContentPack read(std::filesystem::path folder);
static void scan(std::filesystem::path folder,