mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-13 02:52:37 +00:00
Codechange: use BoolSettingDesc to parse settings in conversion code
This commit is contained in:
parent
06b2d9b1cc
commit
ece50d5a1d
@ -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<bool> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<bool> 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;
|
||||
|
Loading…
Reference in New Issue
Block a user