Improved prefab loading a bit.

This commit is contained in:
2024-12-05 19:27:37 -06:00
parent 223bbed232
commit a4774e6189
10 changed files with 113 additions and 71 deletions

View File

@ -76,4 +76,27 @@ struct Color JSON::color(const json &j) {
assertUnreachable("Invalid JSON type for color");
return COLOR_WHITE;
}
void JSON::mergeObjectsRecursive(json &a, const json &b) {
assertTrue(a.type() == json::value_t::object, "a is not an object");
assertTrue(b.type() == json::value_t::object, "b is not an object");
for(auto &item : b.items()) {
auto &key = item.key();
auto &value = item.value();
if(a.contains(key)) {
if(
a[key].type() == json::value_t::object &&
value.type() == json::value_t::object
) {
mergeObjectsRecursive(a[key], value);
} else {
a[key] = json(value);
}
} else {
a[key] = json(value);
}
}
}

View File

@ -25,5 +25,20 @@ namespace Dawn {
* @return Parsed color.
*/
static struct Color color(const json &j);
/**
* Merges all objects in the given JSON object into the first object,
* recursively.
*
* e.g. if; &a is { a: { b: 1, c: 2 }, d: 3 }
* &b is { a: { b: 4, d: 5 }, e: 4 }
*
* The output will become;
* { a: { b: 4, c: 2, d: 5 }, e: 4 }
*
* @param a JSON object to merge into.
* @param b JSON object to merge from.
*/
static void mergeObjectsRecursive(json &a, const json &b);
};
}