diff --git a/src/crashlog.cpp b/src/crashlog.cpp index 11ec63f9a3..4be2605652 100644 --- a/src/crashlog.cpp +++ b/src/crashlog.cpp @@ -136,7 +136,7 @@ void CrashLog::FillCrashLog() if (!this->TryExecute("companies", [&game]() { SurveyCompanies(game["companies"]); return true; })) { game["companies"] = "crashed while gathering information"; } - if (!this->TryExecute("settings", [&game]() { SurveySettings(game["settings"]); return true; })) { + if (!this->TryExecute("settings", [&game]() { SurveySettings(game["settings_changed"], true); return true; })) { game["settings"] = "crashed while gathering information"; } if (!this->TryExecute("grfs", [&game]() { SurveyGrfs(game["grfs"]); return true; })) { diff --git a/src/network/network_survey.cpp b/src/network/network_survey.cpp index dab745d078..96f9c964a1 100644 --- a/src/network/network_survey.cpp +++ b/src/network/network_survey.cpp @@ -66,7 +66,7 @@ std::string NetworkSurveyHandler::CreatePayload(Reason reason, bool for_preview) auto &game = survey["game"]; SurveyTimers(game["timers"]); SurveyCompanies(game["companies"]); - SurveySettings(game["settings"]); + SurveySettings(game["settings"], false); SurveyGrfs(game["grfs"]); SurveyGameScript(game["game_script"]); } diff --git a/src/settings.cpp b/src/settings.cpp index cfd6dedba0..42c2fe0d2d 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -716,6 +716,12 @@ bool IntSettingDesc::IsSameValue(const IniItem *item, void *object) const return item_value == object_value; } +bool IntSettingDesc::IsDefaultValue(void *object) const +{ + int32_t object_value = this->Read(object); + return this->def == object_value; +} + std::string StringSettingDesc::FormatValue(const void *object) const { const std::string &str = this->Read(object); @@ -742,12 +748,24 @@ bool StringSettingDesc::IsSameValue(const IniItem *item, void *object) const return item->value->compare(str) == 0; } +bool StringSettingDesc::IsDefaultValue(void *object) const +{ + const std::string &str = this->Read(object); + return this->def == str; +} + bool ListSettingDesc::IsSameValue(const IniItem *item, void *object) const { /* Checking for equality is way more expensive than just writing the value. */ return false; } +bool ListSettingDesc::IsDefaultValue(void *object) const +{ + /* Defaults of lists are often complicated, and hard to compare. */ + return false; +} + /** * Loads all items from a 'grpname' section into a list * The list parameter can be a nullptr pointer, in this case nothing will be diff --git a/src/settings_internal.h b/src/settings_internal.h index 5bee8ba84b..9c791cb0c5 100644 --- a/src/settings_internal.h +++ b/src/settings_internal.h @@ -130,6 +130,14 @@ struct SettingDesc { * @return True if the value is definitely the same (might be false when the same). */ virtual bool IsSameValue(const IniItem *item, void *object) const = 0; + + /** + * Check whether the value is the same as the default value. + * + * @param object The object the setting is in. + * @return true iff the value is the default value. + */ + virtual bool IsDefaultValue(void *object) const = 0; }; /** Base integer type, including boolean, settings. Only these are shown in the settings UI. */ @@ -215,6 +223,7 @@ struct IntSettingDesc : SettingDesc { std::string FormatValue(const void *object) const override; void ParseValue(const IniItem *item, void *object) const override; bool IsSameValue(const IniItem *item, void *object) const override; + bool IsDefaultValue(void *object) const override; int32_t Read(const void *object) const; private: @@ -307,6 +316,7 @@ struct StringSettingDesc : SettingDesc { std::string FormatValue(const void *object) const override; void ParseValue(const IniItem *item, void *object) const override; bool IsSameValue(const IniItem *item, void *object) const override; + bool IsDefaultValue(void *object) const override; const std::string &Read(const void *object) const; private: @@ -324,6 +334,7 @@ struct ListSettingDesc : SettingDesc { std::string FormatValue(const void *object) const override; void ParseValue(const IniItem *item, void *object) const override; bool IsSameValue(const IniItem *item, void *object) const override; + bool IsDefaultValue(void *object) const override; }; /** Placeholder for settings that have been removed, but might still linger in the savegame. */ @@ -334,6 +345,7 @@ struct NullSettingDesc : SettingDesc { std::string FormatValue(const void *object) const override { NOT_REACHED(); } void ParseValue(const IniItem *item, void *object) const override { NOT_REACHED(); } bool IsSameValue(const IniItem *item, void *object) const override { NOT_REACHED(); } + bool IsDefaultValue(void *object) const override { NOT_REACHED(); } }; typedef std::variant SettingVariant; diff --git a/src/survey.cpp b/src/survey.cpp index 51b1830450..f9d0ac3ca8 100644 --- a/src/survey.cpp +++ b/src/survey.cpp @@ -122,8 +122,9 @@ static auto &GenericSettingTables() * @param survey The JSON object. * @param table The settings table to convert. * @param object The object to get the settings from. + * @param skip_if_default If true, skip any settings that are on their default value. */ -static void SurveySettingsTable(nlohmann::json &survey, const SettingTable &table, void *object) +static void SurveySettingsTable(nlohmann::json &survey, const SettingTable &table, void *object, bool skip_if_default) { for (auto &desc : table) { const SettingDesc *sd = GetSettingDesc(desc); @@ -131,6 +132,7 @@ static void SurveySettingsTable(nlohmann::json &survey, const SettingTable &tabl if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; auto name = sd->GetName(); + if (skip_if_default && sd->IsDefaultValue(object)) continue; survey[name] = sd->FormatValue(object); } } @@ -140,17 +142,17 @@ static void SurveySettingsTable(nlohmann::json &survey, const SettingTable &tabl * * @param survey The JSON object. */ -void SurveySettings(nlohmann::json &survey) +void SurveySettings(nlohmann::json &survey, bool skip_if_default) { - SurveySettingsTable(survey, _misc_settings, nullptr); + SurveySettingsTable(survey, _misc_settings, nullptr, skip_if_default); #if defined(_WIN32) && !defined(DEDICATED) - SurveySettingsTable(survey, _win32_settings, nullptr); + SurveySettingsTable(survey, _win32_settings, nullptr, skip_if_default); #endif for (auto &table : GenericSettingTables()) { - SurveySettingsTable(survey, table, &_settings_game); + SurveySettingsTable(survey, table, &_settings_game, skip_if_default); } - SurveySettingsTable(survey, _currency_settings, &_custom_currency); - SurveySettingsTable(survey, _company_settings, &_settings_client.company); + SurveySettingsTable(survey, _currency_settings, &_custom_currency, skip_if_default); + SurveySettingsTable(survey, _company_settings, &_settings_client.company, skip_if_default); } /** diff --git a/src/survey.h b/src/survey.h index b64c062657..aae16d4bf4 100644 --- a/src/survey.h +++ b/src/survey.h @@ -22,7 +22,7 @@ void SurveyGameScript(nlohmann::json &survey); void SurveyGrfs(nlohmann::json &survey); void SurveyLibraries(nlohmann::json &survey); void SurveyOpenTTD(nlohmann::json &survey); -void SurveySettings(nlohmann::json &survey); +void SurveySettings(nlohmann::json &survey, bool skip_if_default); void SurveyTimers(nlohmann::json &survey); /* Defined in os//survey_.cpp. */