diff --git a/src/settings.cpp b/src/settings.cpp index 69819ab57d..b15f2426cf 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -187,6 +187,20 @@ size_t OneOfManySettingDesc::ParseSingleValue(const char *str, size_t len, const return (size_t)-1; } +/** + * Find whether a string was a boolean true or a boolean false. + * + * @param str the current value of the setting for which a value needs found. + * @return Either true/false, or nullopt if no boolean value found. + */ +std::optional BoolSettingDesc::ParseSingleValue(const char *str) +{ + if (strcmp(str, "true") == 0 || strcmp(str, "on") == 0 || strcmp(str, "1") == 0) return true; + if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return false; + + return std::nullopt; +} + /** * Find the set-integer value MANYofMANY type in a string * @param many full domain of values the MANYofMANY setting can have @@ -422,8 +436,8 @@ size_t ManyOfManySettingDesc::ParseValue(const char *str) const size_t BoolSettingDesc::ParseValue(const char *str) const { - if (strcmp(str, "true") == 0 || strcmp(str, "on") == 0 || strcmp(str, "1") == 0) return true; - if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return false; + auto r = BoolSettingDesc::ParseSingleValue(str); + if (r.has_value()) return *r; ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE); msg.SetDParamStr(0, str); @@ -1225,8 +1239,9 @@ void LoadFromConfig(bool startup) IniGroup *network = generic_ini.GetGroup("network", false); if (network != nullptr) { IniItem *server_advertise = network->GetItem("server_advertise"); - if (server_advertise != nullptr && server_advertise->value == "true") { - _settings_client.network.server_game_type = SERVER_GAME_TYPE_PUBLIC; + if (server_advertise != nullptr) { + auto old_value = BoolSettingDesc::ParseSingleValue(server_advertise->value->c_str()); + _settings_client.network.server_game_type = old_value.value_or(false) ? SERVER_GAME_TYPE_PUBLIC : SERVER_GAME_TYPE_LOCAL; } } } diff --git a/src/settings_internal.h b/src/settings_internal.h index d614b57ae6..d88a585076 100644 --- a/src/settings_internal.h +++ b/src/settings_internal.h @@ -196,6 +196,8 @@ struct BoolSettingDesc : IntSettingDesc { IntSettingDesc(save, flags, startup, def, 0, 1, 0, str, str_help, str_val, cat, pre_check, post_callback) {} + static std::optional ParseSingleValue(const char *str); + bool IsBoolSetting() const override { return true; } size_t ParseValue(const char *str) const override; std::string FormatValue(const void *object) const override;