From d59a901ae0bfe0574d2511615789685158f18509 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 30 Aug 2025 20:44:50 +0300 Subject: [PATCH] add user properties methods support items --- src/content/loading/BlockLoader.cpp | 16 ++----------- src/content/loading/ContentLoadingCommons.hpp | 23 +++++++++++++++++++ src/content/loading/ItemLoader.cpp | 4 +++- 3 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 src/content/loading/ContentLoadingCommons.hpp diff --git a/src/content/loading/BlockLoader.cpp b/src/content/loading/BlockLoader.cpp index 65e35a69..bde250a5 100644 --- a/src/content/loading/BlockLoader.cpp +++ b/src/content/loading/BlockLoader.cpp @@ -1,5 +1,6 @@ #define VC_ENABLE_REFLECTION #include "ContentUnitLoader.hpp" +#include "ContentLoadingCommons.hpp" #include "../ContentBuilder.hpp" #include "coders/json.hpp" @@ -87,20 +88,7 @@ template<> void ContentUnitLoader::loadUnit( Block& def, const std::string& name, const io::path& file ) { auto root = io::read_json(file); - if (def.properties == nullptr) { - def.properties = dv::object(); - def.properties["name"] = name; - } - for (auto& [key, value] : root.asObject()) { - auto pos = key.rfind('@'); - if (pos == std::string::npos) { - def.properties[key] = value; - continue; - } - auto field = key.substr(0, pos); - auto suffix = key.substr(pos + 1); - process_method(def.properties, suffix, field, value); - } + process_properties(def, name, root); if (root.has("parent")) { const auto& parentName = root["parent"].asString(); diff --git a/src/content/loading/ContentLoadingCommons.hpp b/src/content/loading/ContentLoadingCommons.hpp new file mode 100644 index 00000000..652b550f --- /dev/null +++ b/src/content/loading/ContentLoadingCommons.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "data/dv.hpp" + +#include + +template +inline void process_properties(T& def, const std::string& name, const dv::value& root) { + if (def.properties == nullptr) { + def.properties = dv::object(); + def.properties["name"] = name; + } + for (auto& [key, value] : root.asObject()) { + auto pos = key.rfind('@'); + if (pos == std::string::npos) { + def.properties[key] = value; + continue; + } + auto field = key.substr(0, pos); + auto suffix = key.substr(pos + 1); + process_method(def.properties, suffix, field, value); + } +} diff --git a/src/content/loading/ItemLoader.cpp b/src/content/loading/ItemLoader.cpp index 1703b778..97ddd334 100644 --- a/src/content/loading/ItemLoader.cpp +++ b/src/content/loading/ItemLoader.cpp @@ -1,5 +1,6 @@ #define VC_ENABLE_REFLECTION #include "ContentUnitLoader.hpp" +#include "ContentLoadingCommons.hpp" #include "../ContentBuilder.hpp" #include "coders/json.hpp" @@ -12,11 +13,12 @@ static debug::Logger logger("item-content-loader"); + template<> void ContentUnitLoader::loadUnit( ItemDef& def, const std::string& name, const io::path& file ) { auto root = io::read_json(file); - def.properties = root; + process_properties(def, name, root); if (root.has("parent")) { const auto& parentName = root["parent"].asString();