add dv::merge & update EnginePaths::readCombinedObject

This commit is contained in:
MihailRis 2025-02-20 22:08:09 +03:00
parent 12105c2933
commit 84d5b5d1a8
5 changed files with 52 additions and 6 deletions

View File

@ -241,7 +241,7 @@ void ContentLoader::loadGenerator(
load_structures(def, structuresMap, structuresFile.parent(), paths);
auto biomesFile = GENERATORS_DIR / (name + ".files") / BIOMES_FILE;
auto biomesMap = paths.readCombinedObject(biomesFile.string());
auto biomesMap = paths.readCombinedObject(biomesFile.string(), true);
if (biomesMap.empty()) {
throw std::runtime_error(
"generator " + util::quote(def.name) +

View File

@ -14,6 +14,52 @@ namespace dv {
return (*val.object)[key];
}
static void apply_method(value& dst, value&& val, std::string_view method, bool deep) {
if (!val.isList()) {
return;
}
if (dst == nullptr) {
dst = dv::list();
} else if (!dst.isList()) {
return;
}
if (method == "append") {
if (dst == nullptr) {
dst = std::forward<value>(val);
return;
}
for (auto& elem : val) {
dst.add(std::move(elem));
}
}
}
static void merge_elem(value& self, const key_t& key, value&& val, bool deep) {
auto& dst = self[key];
size_t pos = key.rfind('@');
if (pos != std::string::npos) {
auto method = std::string_view(key).substr(pos + 1);
auto& field = self[key.substr(0, pos)];
apply_method(field, std::forward<value>(val), method, deep);
return;
}
if (val.isObject() && dst == nullptr) {
dst = dv::object();
}
if (dst.isObject() && deep) {
dst.merge(std::forward<dv::value>(val), true);
} else {
dst = std::forward<dv::value>(val);
}
}
void value::merge(dv::value&& other, bool deep) {
check_type(other.type, value_type::object);
for (auto& [key, val] : *other.val.object) {
merge_elem(*this, key, std::forward<value>(val), deep);
}
}
value& value::operator=(const objects::Bytes& bytes) {
return setBytes(std::make_shared<objects::Bytes>(bytes));
}

View File

@ -374,6 +374,8 @@ namespace dv {
const value& operator[](const key_t& key) const;
void merge(dv::value&& other, bool deep);
value& operator[](size_t index);
const value& operator[](size_t index) const;

View File

@ -253,7 +253,7 @@ dv::value ResPaths::readCombinedList(const std::string& filename) const {
return list;
}
dv::value ResPaths::readCombinedObject(const std::string& filename) const {
dv::value ResPaths::readCombinedObject(const std::string& filename, bool deep) const {
dv::value object = dv::object();
for (const auto& root : roots) {
auto path = root.path / filename;
@ -267,9 +267,7 @@ dv::value ResPaths::readCombinedObject(const std::string& filename) const {
<< "reading combined object " << root.name << ": "
<< filename << " is not an object (skipped)";
}
for (const auto& [key, element] : value.asObject()) {
object[key] = element;
}
object.merge(std::move(value), deep);
} catch (const std::runtime_error& err) {
logger.warning() << "reading combined object " << root.name << ":"
<< filename << ": " << err.what();

View File

@ -68,7 +68,7 @@ public:
/// @param file *.json file path relative to entry point
dv::value readCombinedList(const std::string& file) const;
dv::value readCombinedObject(const std::string& file) const;
dv::value readCombinedObject(const std::string& file, bool deep=false) const;
const io::path& getMainRoot() const;