From 8e60ecc022335239c000152992d75abebeb9149b Mon Sep 17 00:00:00 2001 From: Michael Steenbeek Date: Sat, 3 Oct 2020 19:41:52 +0200 Subject: [PATCH] Do not output error if the sourceGame parameter is omitted (#13079) Since >90% of objects in the wild are custom objects, it make sense if this parameter can be safely omitted. Only warn if this field contains garbage. --- src/openrct2/object/ObjectFactory.cpp | 48 ++++++++++++++++++--------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/src/openrct2/object/ObjectFactory.cpp b/src/openrct2/object/ObjectFactory.cpp index 82ba9fd88a..15138c417d 100644 --- a/src/openrct2/object/ObjectFactory.cpp +++ b/src/openrct2/object/ObjectFactory.cpp @@ -408,6 +408,38 @@ namespace ObjectFactory return result; } + static void ExtractSourceGames(const std::string& id, json_t& jRoot, Object* result) + { + auto sourceGames = jRoot["sourceGame"]; + if (sourceGames.is_array() || sourceGames.is_string()) + { + std::vector sourceGameVector; + for (const auto& jSourceGame : sourceGames) + { + sourceGameVector.push_back(ParseSourceGame(Json::GetString(jSourceGame))); + } + if (!sourceGameVector.empty()) + { + result->SetSourceGames(sourceGameVector); + } + else + { + log_error("Object %s has an incorrect sourceGame parameter.", id.c_str()); + result->SetSourceGames({ OBJECT_SOURCE_CUSTOM }); + } + } + // >90% of objects are custom, so allow omitting the parameter without displaying an error. + else if (sourceGames.is_null()) + { + result->SetSourceGames({ OBJECT_SOURCE_CUSTOM }); + } + else + { + log_error("Object %s has an incorrect sourceGame parameter.", id.c_str()); + result->SetSourceGames({ OBJECT_SOURCE_CUSTOM }); + } + } + Object* CreateObjectFromJson(IObjectRepository& objectRepository, json_t& jRoot, const IFileDataRetriever* fileRetriever) { Guard::Assert(jRoot.is_object(), "ObjectFactory::CreateObjectFromJson expects parameter jRoot to be object"); @@ -454,21 +486,7 @@ namespace ObjectFactory } result->SetAuthors(std::move(authorVector)); - auto sourceGames = jRoot["sourceGame"]; - if (sourceGames.is_array() || sourceGames.is_string()) - { - std::vector sourceGameVector; - for (const auto& jSourceGame : sourceGames) - { - sourceGameVector.push_back(ParseSourceGame(Json::GetString(jSourceGame))); - } - result->SetSourceGames(sourceGameVector); - } - else - { - log_error("Object %s has an incorrect sourceGame parameter.", id.c_str()); - result->SetSourceGames({ OBJECT_SOURCE_CUSTOM }); - } + ExtractSourceGames(id, jRoot, result); } return result; }