add entities indexing

This commit is contained in:
MihailRis
2024-06-26 18:14:04 +03:00
parent 66a1240fbc
commit f135896683
7 changed files with 50 additions and 50 deletions
+1
View File
@@ -54,6 +54,7 @@ std::unique_ptr<Content> ContentBuilder::build() {
EntityDef& def = *entities.defs[name]; EntityDef& def = *entities.defs[name];
// Generating runtime info // Generating runtime info
def.rt.id = entityDefsIndices.size();
entityDefsIndices.push_back(&def); entityDefsIndices.push_back(&def);
} }
+25 -26
View File
@@ -27,38 +27,36 @@ static debug::Logger logger("content-loader");
ContentLoader::ContentLoader(ContentPack* pack) : pack(pack) { ContentLoader::ContentLoader(ContentPack* pack) : pack(pack) {
} }
static void detect_defs(
const fs::path& folder,
const std::string& prefix,
std::vector<std::string>& 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( bool ContentLoader::fixPackIndices(
const fs::path& folder, const fs::path& folder,
dynamic::Map* indicesRoot, dynamic::Map* indicesRoot,
const std::string& contentSection const std::string& contentSection
) { ) {
std::vector<std::string> detected; std::vector<std::string> detected;
std::vector<std::string> indexed; detect_defs(folder, "", detected);
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);
}
}
}
}
}
std::vector<std::string> indexed;
bool modified = false; bool modified = false;
if (!indicesRoot->has(contentSection)) { if (!indicesRoot->has(contentSection)) {
indicesRoot->putList(contentSection); indicesRoot->putList(contentSection);
@@ -90,6 +88,7 @@ void ContentLoader::fixPackIndices() {
auto indexFile = pack->getContentFile(); auto indexFile = pack->getContentFile();
auto blocksFolder = folder/ContentPack::BLOCKS_FOLDER; auto blocksFolder = folder/ContentPack::BLOCKS_FOLDER;
auto itemsFolder = folder/ContentPack::ITEMS_FOLDER; auto itemsFolder = folder/ContentPack::ITEMS_FOLDER;
auto entitiesFolder = folder/ContentPack::ENTITIES_FOLDER;
dynamic::Map_sptr root; dynamic::Map_sptr root;
if (fs::is_regular_file(indexFile)) { if (fs::is_regular_file(indexFile)) {
@@ -99,9 +98,9 @@ void ContentLoader::fixPackIndices() {
} }
bool modified = false; bool modified = false;
modified |= fixPackIndices(blocksFolder, root.get(), "blocks"); modified |= fixPackIndices(blocksFolder, root.get(), "blocks");
modified |= fixPackIndices(itemsFolder, root.get(), "items"); modified |= fixPackIndices(itemsFolder, root.get(), "items");
modified |= fixPackIndices(entitiesFolder, root.get(), "entities");
if (modified){ if (modified){
// rewrite modified json // rewrite modified json
+3 -2
View File
@@ -26,6 +26,9 @@ class ContentLoader {
void loadCustomBlockModel(Block& def, dynamic::Map* primitives); 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 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: public:
ContentLoader(ContentPack* pack); ContentLoader(ContentPack* pack);
@@ -35,8 +38,6 @@ public:
const std::string& contentSection const std::string& contentSection
); );
void fixPackIndices(); 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); void load(ContentBuilder& builder);
}; };
-4
View File
@@ -11,10 +11,6 @@
namespace fs = std::filesystem; 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<std::string> ContentPack::RESERVED_NAMES = { const std::vector<std::string> ContentPack::RESERVED_NAMES = {
"res", "abs", "local", "core", "user", "world", "none", "null" "res", "abs", "local", "core", "user", "world", "none", "null"
}; };
+5 -4
View File
@@ -46,10 +46,11 @@ struct ContentPack {
fs::path getContentFile() const; fs::path getContentFile() const;
static const std::string PACKAGE_FILENAME; static inline const std::string PACKAGE_FILENAME = "package.json";
static const std::string CONTENT_FILENAME; static inline const std::string CONTENT_FILENAME = "content.json";
static const fs::path BLOCKS_FOLDER; static inline const fs::path BLOCKS_FOLDER = "blocks";
static const fs::path ITEMS_FOLDER; static inline const fs::path ITEMS_FOLDER = "items";
static inline const fs::path ENTITIES_FOLDER = "entities";
static const std::vector<std::string> RESERVED_NAMES; static const std::vector<std::string> RESERVED_NAMES;
static bool is_pack(const fs::path& folder); static bool is_pack(const fs::path& folder);
+12 -14
View File
@@ -11,6 +11,7 @@
#include "../lighting/Lightmap.hpp" #include "../lighting/Lightmap.hpp"
#include "../maths/voxmaths.hpp" #include "../maths/voxmaths.hpp"
#include "../objects/Player.hpp" #include "../objects/Player.hpp"
#include "../objects/EntityDef.hpp"
#include "../physics/Hitbox.hpp" #include "../physics/Hitbox.hpp"
#include "../typedefs.hpp" #include "../typedefs.hpp"
#include "../settings.hpp" #include "../settings.hpp"
@@ -92,22 +93,19 @@ void WorldFiles::writePacks(const std::vector<ContentPack>& packs) {
files::write_string(packsFile, ss.str()); files::write_string(packsFile, ss.str());
} }
template<class T>
static void write_indices(const ContentUnitIndices<T>& 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) { void WorldFiles::writeIndices(const ContentIndices* indices) {
dynamic::Map root; dynamic::Map root;
uint count; write_indices(indices->blocks, root.putList("blocks"));
auto& blocks = root.putList("blocks"); write_indices(indices->items, root.putList("items"));
count = indices->blocks.count(); write_indices(indices->entities, root.putList("entities"));
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);
}
files::write_json(getIndicesFile(), &root); files::write_json(getIndicesFile(), &root);
} }
+4
View File
@@ -8,6 +8,10 @@
struct EntityDef { struct EntityDef {
/// @brief Entity string id (with prefix included) /// @brief Entity string id (with prefix included)
std::string const name; std::string const name;
struct {
entityid_t id;
} rt;
EntityDef(const std::string& name); EntityDef(const std::string& name);
EntityDef(const EntityDef&) = delete; EntityDef(const EntityDef&) = delete;