update ContentLoader part 1

This commit is contained in:
MihailRis
2024-06-26 23:36:01 +03:00
parent f135896683
commit 8d41f6af11
5 changed files with 98 additions and 54 deletions
+78 -41
View File
@@ -24,7 +24,15 @@ namespace fs = std::filesystem;
static debug::Logger logger("content-loader"); static debug::Logger logger("content-loader");
ContentLoader::ContentLoader(ContentPack* pack) : pack(pack) { ContentLoader::ContentLoader(ContentPack* pack, ContentBuilder& builder)
: pack(pack), builder(builder)
{
auto runtime = std::make_unique<ContentPackRuntime>(
*pack, scripting::create_pack_environment(*pack)
);
stats = &runtime->getStatsWriteable();
env = runtime->getEnvironment();
builder.add(std::move(runtime));
} }
static void detect_defs( static void detect_defs(
@@ -172,7 +180,7 @@ void ContentLoader::loadBlock(Block& def, const std::string& name, const fs::pat
def.hitboxes[i].b = glm::vec3(box->num(3), box->num(4), box->num(5)); def.hitboxes[i].b = glm::vec3(box->num(3), box->num(4), box->num(5));
def.hitboxes[i].b += def.hitboxes[i].a; def.hitboxes[i].b += def.hitboxes[i].a;
} }
} else if (auto boxarr = root->list("hitbox")){ } else if ((boxarr = root->list("hitbox"))){
AABB aabb; AABB aabb;
aabb.a = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2)); 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 = glm::vec3(boxarr->num(3), boxarr->num(4), boxarr->num(5));
@@ -301,25 +309,47 @@ void ContentLoader::loadItem(ItemDef& def, const std::string& name, const fs::pa
} }
} }
void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs::path& file) {
auto root = files::read_json(file);
}
void ContentLoader::loadEntity(EntityDef& def, const std::string& full, const std::string& name) {
auto folder = pack->folder;
auto configFile = folder/fs::path("entities/"+name+".json");
if (fs::exists(configFile)) loadEntity(def, full, configFile);
}
void ContentLoader::loadBlock(Block& def, const std::string& full, const std::string& name) { void ContentLoader::loadBlock(Block& def, const std::string& full, const std::string& name) {
auto folder = pack->folder; auto folder = pack->folder;
auto configFile = folder/fs::path("blocks/"+name+".json");
fs::path configFile = folder/fs::path("blocks/"+name+".json");
if (fs::exists(configFile)) loadBlock(def, full, configFile); if (fs::exists(configFile)) loadBlock(def, full, configFile);
fs::path scriptfile = folder/fs::path("scripts/"+def.scriptName+".lua"); auto scriptfile = folder/fs::path("scripts/"+def.scriptName+".lua");
if (fs::is_regular_file(scriptfile)) { if (fs::is_regular_file(scriptfile)) {
scripting::load_block_script(env, full, scriptfile, def.rt.funcsset); scripting::load_block_script(env, full, scriptfile, def.rt.funcsset);
} }
if (!def.hidden) {
auto& item = builder.items.create(full+BLOCK_ITEM_SUFFIX);
item.generated = true;
item.caption = def.caption;
item.iconType = item_icon_type::block;
item.icon = full;
item.placingBlock = full;
for (uint j = 0; j < 4; j++) {
item.emission[j] = def.emission[j];
}
stats->totalItems++;
}
} }
void ContentLoader::loadItem(ItemDef& def, const std::string& full, const std::string& name) { void ContentLoader::loadItem(ItemDef& def, const std::string& full, const std::string& name) {
auto folder = pack->folder; auto folder = pack->folder;
auto configFile = folder/fs::path("items/"+name+".json");
fs::path configFile = folder/fs::path("items/"+name+".json");
if (fs::exists(configFile)) loadItem(def, full, configFile); if (fs::exists(configFile)) loadItem(def, full, configFile);
fs::path scriptfile = folder/fs::path("scripts/"+def.scriptName+".lua"); auto scriptfile = folder/fs::path("scripts/"+def.scriptName+".lua");
if (fs::is_regular_file(scriptfile)) { if (fs::is_regular_file(scriptfile)) {
scripting::load_item_script(env, full, scriptfile, def.rt.funcsset); scripting::load_item_script(env, full, scriptfile, def.rt.funcsset);
} }
@@ -332,15 +362,27 @@ void ContentLoader::loadBlockMaterial(BlockMaterial& def, const fs::path& file)
root->str("break-sound", def.breakSound); root->str("break-sound", def.breakSound);
} }
void ContentLoader::load(ContentBuilder& builder) { template<class T, void(ContentLoader::*loadfunc)(T&, const std::string&, const std::string&)>
logger.info() << "loading pack [" << pack->id << "]"; static void load_defs(ContentLoader* cl, dynamic::List* arr, const ContentPack* pack, ContentUnitBuilder<T>& builder, size_t& counter) {
if (arr == nullptr) {
return;
}
for (size_t i = 0; i < arr->size(); i++) {
std::string name = arr->str(i);
auto colon = name.find(':');
std::string full = colon == std::string::npos ? pack->id + ":" + name : name;
if (colon != std::string::npos) name[colon] = '/';
auto& def = builder.create(full);
if (colon != std::string::npos) {
def.scriptName = name.substr(0, colon) + '/' + def.scriptName;
}
cl->*loadfunc(def, full, name);
counter++;
}
}
auto runtime = std::make_unique<ContentPackRuntime>( void ContentLoader::load() {
*pack, scripting::create_pack_environment(*pack) logger.info() << "loading pack [" << pack->id << "]";
);
env = runtime->getEnvironment();
ContentPackStats& stats = runtime->getStatsWriteable();
builder.add(std::move(runtime));
fixPackIndices(); fixPackIndices();
@@ -355,38 +397,21 @@ void ContentLoader::load(ContentBuilder& builder) {
return; return;
auto root = files::read_json(pack->getContentFile()); auto root = files::read_json(pack->getContentFile());
auto blocksarr = root->list("blocks");
if (blocksarr) { if (auto blocksarr = root->list("blocks")) {
for (uint i = 0; i < blocksarr->size(); i++) { for (size_t i = 0; i < blocksarr->size(); i++) {
std::string name = blocksarr->str(i); std::string name = blocksarr->str(i);
auto colon = name.find(':'); auto colon = name.find(':');
std::string full = colon == std::string::npos ? pack->id + ":" + name : name; std::string full = colon == std::string::npos ? pack->id + ":" + name : name;
if (colon != std::string::npos) name[colon] = '/'; if (colon != std::string::npos) name[colon] = '/';
auto& def = builder.blocks.create(full); auto& def = builder.blocks.create(full);
if (colon != std::string::npos) { if (colon != std::string::npos) def.scriptName = name.substr(0, colon) + '/' + def.scriptName;
def.scriptName = name.substr(0, colon) + '/' + def.scriptName;
}
loadBlock(def, full, name); loadBlock(def, full, name);
stats.totalBlocks++; stats->totalBlocks++;
if (!def.hidden) {
auto& item = builder.items.create(full+BLOCK_ITEM_SUFFIX);
item.generated = true;
item.caption = def.caption;
item.iconType = item_icon_type::block;
item.icon = full;
item.placingBlock = full;
for (uint j = 0; j < 4; j++) {
item.emission[j] = def.emission[j];
}
stats.totalItems++;
}
} }
} }
if (auto itemsarr = root->list("items")) {
auto itemsarr = root->list("items"); for (size_t i = 0; i < itemsarr->size(); i++) {
if (itemsarr) {
for (uint i = 0; i < itemsarr->size(); i++) {
std::string name = itemsarr->str(i); std::string name = itemsarr->str(i);
auto colon = name.find(':'); auto colon = name.find(':');
std::string full = colon == std::string::npos ? pack->id + ":" + name : name; std::string full = colon == std::string::npos ? pack->id + ":" + name : name;
@@ -394,7 +419,19 @@ void ContentLoader::load(ContentBuilder& builder) {
auto& def = builder.items.create(full); auto& def = builder.items.create(full);
if (colon != std::string::npos) def.scriptName = name.substr(0, colon) + '/' + def.scriptName; if (colon != std::string::npos) def.scriptName = name.substr(0, colon) + '/' + def.scriptName;
loadItem(def, full, name); loadItem(def, full, name);
stats.totalItems++; stats->totalItems++;
}
}
if (auto entitiesarr = root->list("entities")) {
for (size_t i = 0; i < entitiesarr->size(); i++) {
std::string name = entitiesarr->str(i);
auto colon = name.find(':');
std::string full = colon == std::string::npos ? pack->id + ":" + name : name;
if (colon != std::string::npos) name[colon] = '/';
auto& def = builder.entities.create(full);
if (colon != std::string::npos) def.scriptName = name.substr(0, colon) + '/' + def.scriptName;
loadEntity(def, full, name);
stats->totalEntities++;
} }
} }
+14 -7
View File
@@ -4,6 +4,7 @@
#include "../typedefs.hpp" #include "../typedefs.hpp"
#include <string> #include <string>
#include <memory>
#include <filesystem> #include <filesystem>
namespace fs = std::filesystem; namespace fs = std::filesystem;
@@ -11,8 +12,10 @@ namespace fs = std::filesystem;
class Block; class Block;
struct BlockMaterial; struct BlockMaterial;
struct ItemDef; struct ItemDef;
struct EntityDef;
struct ContentPack; struct ContentPack;
class ContentBuilder; class ContentBuilder;
struct ContentPackStats;
namespace dynamic { namespace dynamic {
class Map; class Map;
@@ -21,16 +24,20 @@ namespace dynamic {
class ContentLoader { class ContentLoader {
const ContentPack* pack; const ContentPack* pack;
scriptenv env; scriptenv env;
ContentBuilder& builder;
ContentPackStats* stats;
void loadBlock(Block& def, const std::string& full, const std::string& name); void loadBlock(Block& def, const std::string& full, const std::string& name);
void loadCustomBlockModel(Block& def, dynamic::Map* primitives);
void loadItem(ItemDef& def, const std::string& full, const std::string& name); void loadItem(ItemDef& def, const std::string& full, const std::string& name);
void loadBlockMaterial(BlockMaterial& def, const fs::path& file); void loadEntity(EntityDef& def, const std::string& full, const std::string& name);
void loadBlock(Block& def, const std::string& name, const fs::path& file); static void loadCustomBlockModel(Block& def, dynamic::Map* primitives);
void loadItem(ItemDef& def, const std::string& name, const fs::path& file); static void loadBlockMaterial(BlockMaterial& def, const fs::path& file);
static void loadBlock(Block& def, const std::string& name, const fs::path& file);
static void loadItem(ItemDef& def, const std::string& name, const fs::path& file);
static void loadEntity(EntityDef& def, const std::string& name, const fs::path& file);
public: public:
ContentLoader(ContentPack* pack); ContentLoader(ContentPack* pack, ContentBuilder& builder);
bool fixPackIndices( bool fixPackIndices(
const fs::path& folder, const fs::path& folder,
@@ -38,7 +45,7 @@ public:
const std::string& contentSection const std::string& contentSection
); );
void fixPackIndices(); void fixPackIndices();
void load(ContentBuilder& builder); void load();
}; };
#endif // CONTENT_CONTENT_LOADER_HPP_ #endif // CONTENT_CONTENT_LOADER_HPP_
+2 -1
View File
@@ -73,9 +73,10 @@ struct ContentPack {
struct ContentPackStats { struct ContentPackStats {
size_t totalBlocks; size_t totalBlocks;
size_t totalItems; size_t totalItems;
size_t totalEntities;
inline bool hasSavingContent() const { inline bool hasSavingContent() const {
return totalBlocks + totalItems > 0; return totalBlocks + totalItems + totalEntities > 0;
} }
}; };
+1 -4
View File
@@ -289,10 +289,7 @@ void Engine::loadContent() {
std::vector<PathsRoot> resRoots; std::vector<PathsRoot> resRoots;
for (auto& pack : contentPacks) { for (auto& pack : contentPacks) {
resRoots.push_back({pack.id, pack.folder}); resRoots.push_back({pack.id, pack.folder});
ContentLoader(&pack, contentBuilder).load();
ContentLoader loader(&pack);
loader.load(contentBuilder);
load_configs(pack.folder); load_configs(pack.folder);
} }
load_configs(paths->getResources()); load_configs(paths->getResources());
+3 -1
View File
@@ -9,11 +9,13 @@ struct EntityDef {
/// @brief Entity string id (with prefix included) /// @brief Entity string id (with prefix included)
std::string const name; std::string const name;
std::string scriptName = name.substr(name.find(':')+1);
struct { struct {
entityid_t id; entityid_t id;
} rt; } rt;
EntityDef(const std::string& name); EntityDef(const std::string& name) : name(name) {}
EntityDef(const EntityDef&) = delete; EntityDef(const EntityDef&) = delete;
}; };