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

View File

@ -54,6 +54,7 @@ std::unique_ptr<Content> ContentBuilder::build() {
EntityDef& def = *entities.defs[name];
// Generating runtime info
def.rt.id = entityDefsIndices.size();
entityDefsIndices.push_back(&def);
}

View File

@ -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<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(
const fs::path& folder,
dynamic::Map* indicesRoot,
const std::string& contentSection
) {
std::vector<std::string> detected;
std::vector<std::string> 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<std::string> 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

View File

@ -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);
};

View File

@ -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<std::string> ContentPack::RESERVED_NAMES = {
"res", "abs", "local", "core", "user", "world", "none", "null"
};

View File

@ -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<std::string> RESERVED_NAMES;
static bool is_pack(const fs::path& folder);

View File

@ -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<ContentPack>& packs) {
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) {
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);
}

View File

@ -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;