diff --git a/src/content/ContentBuilder.cpp b/src/content/ContentBuilder.cpp index adbe6425..723eabe1 100644 --- a/src/content/ContentBuilder.cpp +++ b/src/content/ContentBuilder.cpp @@ -54,6 +54,7 @@ std::unique_ptr ContentBuilder::build() { EntityDef& def = *entities.defs[name]; // Generating runtime info + def.rt.id = entityDefsIndices.size(); entityDefsIndices.push_back(&def); } diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 265c93b7..799ef94a 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -27,38 +27,36 @@ static debug::Logger logger("content-loader"); ContentLoader::ContentLoader(ContentPack* pack) : pack(pack) { } +static void detect_defs( + const fs::path& folder, + const std::string& prefix, + std::vector& detected +) { + if (fs::is_directory(folder)) { + for (const auto& entry : fs::directory_iterator(folder)) { + const fs::path& file = entry.path(); + std::string name = file.stem().string(); + if (name[0] == '_') { + continue; + } + if (fs::is_regular_file(file) && file.extension() == ".json") { + detected.push_back(prefix.empty() ? name : prefix + ":" + name); + } else if (fs::is_directory(file)) { + detect_defs(file, name, detected); + } + } + } +} + bool ContentLoader::fixPackIndices( const fs::path& folder, dynamic::Map* indicesRoot, const std::string& contentSection ) { std::vector detected; - std::vector indexed; - if (fs::is_directory(folder)) { - for (const auto& entry : fs::directory_iterator(folder)) { - const fs::path& file = entry.path(); - if (fs::is_regular_file(file) && file.extension() == ".json") { - std::string name = file.stem().string(); - if (name[0] == '_') - continue; - detected.push_back(name); - } else if (fs::is_directory(file)) { - std::string space = file.stem().string(); - if (space[0] == '_') - continue; - for (const auto& entry : fs::directory_iterator(file)) { - const fs::path& file = entry.path(); - if (fs::is_regular_file(file) && file.extension() == ".json") { - std::string name = file.stem().string(); - if (name[0] == '_') - continue; - detected.push_back(space + ':' + name); - } - } - } - } - } + detect_defs(folder, "", detected); + std::vector indexed; bool modified = false; if (!indicesRoot->has(contentSection)) { indicesRoot->putList(contentSection); @@ -90,6 +88,7 @@ void ContentLoader::fixPackIndices() { auto indexFile = pack->getContentFile(); auto blocksFolder = folder/ContentPack::BLOCKS_FOLDER; auto itemsFolder = folder/ContentPack::ITEMS_FOLDER; + auto entitiesFolder = folder/ContentPack::ENTITIES_FOLDER; dynamic::Map_sptr root; if (fs::is_regular_file(indexFile)) { @@ -99,9 +98,9 @@ void ContentLoader::fixPackIndices() { } bool modified = false; - modified |= fixPackIndices(blocksFolder, root.get(), "blocks"); modified |= fixPackIndices(itemsFolder, root.get(), "items"); + modified |= fixPackIndices(entitiesFolder, root.get(), "entities"); if (modified){ // rewrite modified json diff --git a/src/content/ContentLoader.hpp b/src/content/ContentLoader.hpp index 7a5068eb..6c04b6db 100644 --- a/src/content/ContentLoader.hpp +++ b/src/content/ContentLoader.hpp @@ -26,6 +26,9 @@ class ContentLoader { void loadCustomBlockModel(Block& def, dynamic::Map* primitives); void loadItem(ItemDef& def, const std::string& full, const std::string& name); void loadBlockMaterial(BlockMaterial& def, const fs::path& file); + + void loadBlock(Block& def, const std::string& name, const fs::path& file); + void loadItem(ItemDef& def, const std::string& name, const fs::path& file); public: ContentLoader(ContentPack* pack); @@ -35,8 +38,6 @@ public: const std::string& contentSection ); void fixPackIndices(); - void loadBlock(Block& def, const std::string& name, const fs::path& file); - void loadItem(ItemDef& def, const std::string& name, const fs::path& file); void load(ContentBuilder& builder); }; diff --git a/src/content/ContentPack.cpp b/src/content/ContentPack.cpp index e3a5f0f1..8388e30c 100644 --- a/src/content/ContentPack.cpp +++ b/src/content/ContentPack.cpp @@ -11,10 +11,6 @@ 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"; -const fs::path ContentPack::ITEMS_FOLDER = "items"; const std::vector ContentPack::RESERVED_NAMES = { "res", "abs", "local", "core", "user", "world", "none", "null" }; diff --git a/src/content/ContentPack.hpp b/src/content/ContentPack.hpp index ca0aff2e..dfb62033 100644 --- a/src/content/ContentPack.hpp +++ b/src/content/ContentPack.hpp @@ -46,10 +46,11 @@ struct ContentPack { fs::path getContentFile() const; - static const std::string PACKAGE_FILENAME; - static const std::string CONTENT_FILENAME; - static const fs::path BLOCKS_FOLDER; - static const fs::path ITEMS_FOLDER; + static inline const std::string PACKAGE_FILENAME = "package.json"; + static inline const std::string CONTENT_FILENAME = "content.json"; + static inline const fs::path BLOCKS_FOLDER = "blocks"; + static inline const fs::path ITEMS_FOLDER = "items"; + static inline const fs::path ENTITIES_FOLDER = "entities"; static const std::vector RESERVED_NAMES; static bool is_pack(const fs::path& folder); diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index 82b70881..b497b69a 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -11,6 +11,7 @@ #include "../lighting/Lightmap.hpp" #include "../maths/voxmaths.hpp" #include "../objects/Player.hpp" +#include "../objects/EntityDef.hpp" #include "../physics/Hitbox.hpp" #include "../typedefs.hpp" #include "../settings.hpp" @@ -92,22 +93,19 @@ void WorldFiles::writePacks(const std::vector& packs) { files::write_string(packsFile, ss.str()); } +template +static void write_indices(const ContentUnitIndices& indices, dynamic::List& list) { + size_t count = indices.count(); + for (size_t i = 0; i < count; i++) { + list.put(indices.get(i)->name); + } +} + void WorldFiles::writeIndices(const ContentIndices* indices) { dynamic::Map root; - uint count; - auto& blocks = root.putList("blocks"); - count = indices->blocks.count(); - for (uint i = 0; i < count; i++) { - const Block* def = indices->blocks.get(i); - blocks.put(def->name); - } - - auto& items = root.putList("items"); - count = indices->items.count(); - for (uint i = 0; i < count; i++) { - items.put(indices->items.get(i)->name); - } - + write_indices(indices->blocks, root.putList("blocks")); + write_indices(indices->items, root.putList("items")); + write_indices(indices->entities, root.putList("entities")); files::write_json(getIndicesFile(), &root); } diff --git a/src/objects/EntityDef.hpp b/src/objects/EntityDef.hpp index 06510099..70f8653b 100644 --- a/src/objects/EntityDef.hpp +++ b/src/objects/EntityDef.hpp @@ -8,6 +8,10 @@ struct EntityDef { /// @brief Entity string id (with prefix included) std::string const name; + + struct { + entityid_t id; + } rt; EntityDef(const std::string& name); EntityDef(const EntityDef&) = delete;