fix content inheritance issues

This commit is contained in:
MihailRis 2026-11-03 18:46:50 +03:00
parent 8124b550df
commit c9cc301e46
10 changed files with 55 additions and 22 deletions

View file

@ -1,5 +1,8 @@
#include "ContentBuilder.hpp"
#include "core_defs.hpp"
#include "util/stringutil.hpp"
ContentBuilder::~ContentBuilder() = default;
void ContentBuilder::add(std::unique_ptr<ContentPackRuntime> pack) {
@ -19,6 +22,11 @@ std::unique_ptr<Content> 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<uint32_t*>(def.emission);
@ -83,6 +91,11 @@ std::unique_ptr<Content> 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<uint32_t*>(def.emission);
@ -116,8 +129,17 @@ std::unique_ptr<Content> 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();
}

View file

@ -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<T>(id);
return *defs[id];
}
// Only fetch existing definition, return null otherwise.
T* get(const std::string& id) {
auto found = defs.find(id);

View file

@ -180,7 +180,8 @@ void ContentUnitLoader<DefT>::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<Block>(*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;

View file

@ -115,9 +115,8 @@ static void load_variant(
}
template<> void ContentUnitLoader<Block>::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<Block>::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<Block>::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<Variants>();
@ -162,6 +161,13 @@ template<> void ContentUnitLoader<Block>::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);

View file

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

View file

@ -14,9 +14,8 @@
static debug::Logger logger("entity-content-loader");
template<> void ContentUnitLoader<EntityDef>::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")) {

View file

@ -15,9 +15,8 @@ static debug::Logger logger("item-content-loader");
template<> void ContentUnitLoader<ItemDef>::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);

View file

@ -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) {

View file

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

View file

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