persist car: add safety so that old json files work fine (#485)

* persist car: add safety so that old json files work fine

* remove unused include
This commit is contained in:
Quentin E. / iDeath 2022-10-20 00:50:24 +02:00 committed by GitHub
parent 7d67f85924
commit 0e7d3a73b6

View File

@ -14,7 +14,9 @@ namespace big
bool is_invincible;
};
static void to_json(nlohmann::json& j, const model_attachment& attachment) {
static void to_json(nlohmann::json& j, const model_attachment& attachment)
{
j = nlohmann::json{ {"model_hash", attachment.model_hash},
{"position_x", attachment.position.x}, {"position_y", attachment.position.y}, {"position_z", attachment.position.z},
@ -25,12 +27,34 @@ namespace big
};
};
static void from_json(const nlohmann::json& j, model_attachment& attachment) {
j.at("model_hash").get_to(attachment.model_hash);
j.at("position_x").get_to(attachment.position.x); j.at("position_y").get_to(attachment.position.y); j.at("position_z").get_to(attachment.position.z);
j.at("rotation_x").get_to(attachment.rotation.x); j.at("rotation_y").get_to(attachment.rotation.y); j.at("rotation_z").get_to(attachment.rotation.z);
j.at("has_collision").get_to(attachment.has_collision);
j.at("is_visible").get_to(attachment.is_visible);
j.at("is_invincible").get_to(attachment.is_invincible);
template <typename ValueType>
static void set_from_key_or_default(const nlohmann::json& j, const char* key, ValueType& value, ValueType default_value = {})
{
if (j.contains(key))
{
j.at(key).get_to(value);
}
else
{
value = default_value;
}
}
};
static void from_json(const nlohmann::json& j, model_attachment& attachment)
{
set_from_key_or_default(j, "model_hash", attachment.model_hash);
set_from_key_or_default(j, "position_x", attachment.position.x);
set_from_key_or_default(j, "position_y", attachment.position.y);
set_from_key_or_default(j, "position_z", attachment.position.z);
set_from_key_or_default(j, "rotation_x", attachment.rotation.x);
set_from_key_or_default(j, "rotation_y", attachment.rotation.y);
set_from_key_or_default(j, "rotation_z", attachment.rotation.z);
set_from_key_or_default(j, "has_collision", attachment.has_collision);
set_from_key_or_default(j, "is_visible", attachment.is_visible, true);
set_from_key_or_default(j, "is_invincible", attachment.is_invincible);
}
};