Merge pull request #472 from MihailRis/update-combined-object-files
Improve combined object files
This commit is contained in:
commit
1fa20b15ad
@ -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) +
|
||||
|
||||
@ -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));
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user