From c9cc301e46266471c3f065872ba3f0933c2d80a4 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 3 Nov 2026 18:46:50 +0300 Subject: [PATCH] fix content inheritance issues --- src/content/ContentBuilder.cpp | 26 +++++++++++++++++++++-- src/content/ContentBuilder.hpp | 9 ++++++-- src/content/ContentLoader.cpp | 12 +++++++---- src/content/loading/BlockLoader.cpp | 14 ++++++++---- src/content/loading/ContentUnitLoader.hpp | 2 +- src/content/loading/EntityLoader.cpp | 3 +-- src/content/loading/ItemLoader.cpp | 3 +-- src/items/ItemDef.cpp | 3 +-- src/voxels/Block.cpp | 3 +-- src/voxels/Block.hpp | 2 +- 10 files changed, 55 insertions(+), 22 deletions(-) diff --git a/src/content/ContentBuilder.cpp b/src/content/ContentBuilder.cpp index 3603e7bf4..4c478f4ef 100644 --- a/src/content/ContentBuilder.cpp +++ b/src/content/ContentBuilder.cpp @@ -1,5 +1,8 @@ #include "ContentBuilder.hpp" +#include "core_defs.hpp" +#include "util/stringutil.hpp" + ContentBuilder::~ContentBuilder() = default; void ContentBuilder::add(std::unique_ptr pack) { @@ -19,6 +22,11 @@ std::unique_ptr ContentBuilder::build() { for (const std::string& name : blocks.names) { Block& def = *blocks.defs[name]; + // Filling auto fields + if (def.caption.empty()) { + def.caption = util::id_to_caption(def.name); + } + // Generating runtime info def.rt.id = blockDefsIndices.size(); def.rt.emissive = *reinterpret_cast(def.emission); @@ -83,6 +91,11 @@ std::unique_ptr ContentBuilder::build() { for (const std::string& name : items.names) { ItemDef& def = *items.defs[name]; + // Filling auto fields + if (def.caption.empty()) { + def.caption = util::id_to_caption(def.name); + } + // Generating runtime info def.rt.id = itemDefsIndices.size(); def.rt.emissive = *reinterpret_cast(def.emission); @@ -116,8 +129,17 @@ std::unique_ptr ContentBuilder::build() { // Now, it's time to resolve foreign keys for (Block* def : blockDefsIndices) { - def->rt.pickingItem = content->items.require(def->pickingItem).rt.id; - def->rt.surfaceReplacement = content->blocks.require(def->surfaceReplacement).rt.id; + def->rt.pickingItem = + content->items + .require( + def->pickingItem.empty() + ? (def->hidden ? CORE_EMPTY + : def->name + BLOCK_ITEM_SUFFIX) + : def->pickingItem + ) + .rt.id; + def->rt.surfaceReplacement = + content->blocks.require(def->surfaceReplacement).rt.id; if (def->properties == nullptr) { def->properties = dv::object(); } diff --git a/src/content/ContentBuilder.hpp b/src/content/ContentBuilder.hpp index b1c4d26c3..a74cdbe76 100644 --- a/src/content/ContentBuilder.hpp +++ b/src/content/ContentBuilder.hpp @@ -38,16 +38,21 @@ public: T& create(const std::string& id, bool* created = nullptr) { auto found = defs.find(id); if (found != defs.end()) { - if (created) *created = false; + if (created) { + *created = false; + } return *found->second; } - if (created) *created = true; + if (created) { + *created = true; + } checkIdentifier(id); allNames[id] = type; names.push_back(id); defs[id] = std::make_unique(id); return *defs[id]; } + // Only fetch existing definition, return null otherwise. T* get(const std::string& id) { auto found = defs.find(id); diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 3c45c4438..bf752531d 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -180,7 +180,8 @@ void ContentUnitLoader::loadUnit( auto configFile = folder / (defsDir + "/" + name + ".json"); if (io::exists(configFile)) { try { - loadUnit(def, full, configFile); + dv::value root = io::read_json(configFile); + loadUnit(def, full, std::move(root)); } catch (const std::runtime_error& err) { throw std::runtime_error( "file " + util::quote(configFile.string()) + ": " + @@ -286,12 +287,15 @@ void ContentLoader::loadContent(const dv::value& root) { }; ContentUnitLoader(*pack, builder.blocks, "blocks", - [this](Block& def) { + [this](const Block& def) { if (!def.hidden) { bool created; - auto& item = builder.items.create(def.name + BLOCK_ITEM_SUFFIX, &created); + auto& item = builder.items.create( + def.name + BLOCK_ITEM_SUFFIX, &created + ); item.generated = true; - item.caption = def.caption; + item.caption = def.caption.empty() ? util::id_to_caption(def.name) + : def.caption; item.iconType = ItemIconType::BLOCK; item.icon = def.name; item.placingBlock = def.name; diff --git a/src/content/loading/BlockLoader.cpp b/src/content/loading/BlockLoader.cpp index 2c29b4471..1c1a1345c 100644 --- a/src/content/loading/BlockLoader.cpp +++ b/src/content/loading/BlockLoader.cpp @@ -115,9 +115,8 @@ static void load_variant( } template<> void ContentUnitLoader::loadUnit( - Block& def, const std::string& name, const io::path& file + Block& def, const std::string& name, dv::value root ) { - auto root = io::read_json(file); process_properties(def, name, root); process_tags(def, root); @@ -126,7 +125,7 @@ template<> void ContentUnitLoader::loadUnit( auto parentDef = builder.get(parentName); if (parentDef == nullptr) { throw std::runtime_error( - "Failed to find parent(" + parentName + ") for " + name + "failed to find parent(" + parentName + ") for " + name ); } parentDef->cloneTo(def); @@ -146,7 +145,7 @@ template<> void ContentUnitLoader::loadUnit( stateBased.at("offset").get(offset); stateBased.at("bits").get(bitsCount); if (offset < 0 || bitsCount <= 0 || offset + bitsCount > 8) { - throw std::runtime_error("Invalid state-based bits configuration"); + throw std::runtime_error("invalid state-based bits configuration"); } def.variants = std::make_unique(); @@ -162,6 +161,13 @@ template<> void ContentUnitLoader::loadUnit( def.variants->variants.push_back(def.defaults); } } + } else if (def.variants) { + auto& variants = def.variants->variants; + variants.at(0) = def.defaults; + + for (int i = 1; i < variants.size(); i++) { + load_variant(variants.at(i), root, name); + } } root.at("material").get(def.material); diff --git a/src/content/loading/ContentUnitLoader.hpp b/src/content/loading/ContentUnitLoader.hpp index 1c0578b9a..51f225a9c 100644 --- a/src/content/loading/ContentUnitLoader.hpp +++ b/src/content/loading/ContentUnitLoader.hpp @@ -25,7 +25,7 @@ public: postFunc(std::move(postFunc)) { } void loadUnit(DefT& def, const std::string& full, const std::string& name); - void loadUnit(DefT& def, const std::string& name, const io::path& file); + void loadUnit(DefT& def, const std::string& name, dv::value root); void loadDefs(const dv::value& root); private: const ContentPack& pack; diff --git a/src/content/loading/EntityLoader.cpp b/src/content/loading/EntityLoader.cpp index 91f571c95..7eab074bf 100644 --- a/src/content/loading/EntityLoader.cpp +++ b/src/content/loading/EntityLoader.cpp @@ -14,9 +14,8 @@ static debug::Logger logger("entity-content-loader"); template<> void ContentUnitLoader::loadUnit( - EntityDef& def, const std::string& name, const io::path& file + EntityDef& def, const std::string& name, dv::value root ) { - auto root = io::read_json(file); process_properties(def, name, root); if (root.has("parent")) { diff --git a/src/content/loading/ItemLoader.cpp b/src/content/loading/ItemLoader.cpp index cba07771c..ec9a65fd2 100644 --- a/src/content/loading/ItemLoader.cpp +++ b/src/content/loading/ItemLoader.cpp @@ -15,9 +15,8 @@ static debug::Logger logger("item-content-loader"); template<> void ContentUnitLoader::loadUnit( - ItemDef& def, const std::string& name, const io::path& file + ItemDef& def, const std::string& name, dv::value root ) { - auto root = io::read_json(file); process_properties(def, name, root); process_tags(def, root); diff --git a/src/items/ItemDef.cpp b/src/items/ItemDef.cpp index bff25e7af..d180c1fc0 100644 --- a/src/items/ItemDef.cpp +++ b/src/items/ItemDef.cpp @@ -3,8 +3,7 @@ #include "util/stringutil.hpp" ItemDef::ItemDef(const std::string& name) - : name(name), - caption(util::id_to_caption(name)), + : name(name), scriptName(name.substr(name.find(':') + 1)) { } void ItemDef::cloneTo(ItemDef& dst) { diff --git a/src/voxels/Block.cpp b/src/voxels/Block.cpp index 017ea36ee..fb3c5888b 100644 --- a/src/voxels/Block.cpp +++ b/src/voxels/Block.cpp @@ -120,8 +120,7 @@ const BlockRotProfile BlockRotProfile::LADDER { 4 }; -Block::Block(const std::string& name) - : name(name), caption(util::id_to_caption(name)) { +Block::Block(const std::string& name) : name(name) { for (int i = 0; i < defaults.textureFaces.size(); i++) { defaults.textureFaces[i] = TEXTURE_NOTFOUND; } diff --git a/src/voxels/Block.hpp b/src/voxels/Block.hpp index ad8d71606..27029f43c 100644 --- a/src/voxels/Block.hpp +++ b/src/voxels/Block.hpp @@ -274,7 +274,7 @@ public: BlockRotProfile rotations = BlockRotProfile::NONE; /// @brief Item will be picked on MMB click on the block - std::string pickingItem = name + BLOCK_ITEM_SUFFIX; + std::string pickingItem; /// @brief Block script name in blocks/ without extension std::string scriptName = name.substr(name.find(':') + 1);