diff options
-rw-r--r-- | src/settings.cpp | 27 | ||||
-rw-r--r-- | src/settings_gui.cpp | 10 | ||||
-rw-r--r-- | src/settings_internal.h | 52 |
3 files changed, 46 insertions, 43 deletions
diff --git a/src/settings.cpp b/src/settings.cpp index a99b196b4..df064499a 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -403,7 +403,7 @@ size_t IntSettingDesc::ParseValue(const char *str) const size_t r = LookupOneOfMany(this->many, str); /* if the first attempt of conversion from string to the appropriate value fails, * look if we have defined a converter from old value to new value. */ - if (r == (size_t)-1 && this->proc_cnvt != nullptr) r = this->proc_cnvt(str); + if (r == (size_t)-1 && this->many_cnvt != nullptr) r = this->many_cnvt(str); if (r != (size_t)-1) return r; // and here goes converted value ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE); @@ -437,7 +437,7 @@ size_t IntSettingDesc::ParseValue(const char *str) const default: NOT_REACHED(); } - return (size_t)this->def; + return this->def; } /** @@ -471,7 +471,7 @@ void IntSettingDesc::Write_ValidateSetting(const void *object, int32 val) const val = Clamp(val, this->min, this->max); } else if (val < this->min || val > (int32)this->max) { /* Reset invalid discrete setting (where different values change gameplay) to its default value */ - val = (int32)(size_t)this->def; + val = this->def; } } break; @@ -485,7 +485,7 @@ void IntSettingDesc::Write_ValidateSetting(const void *object, int32 val) const uval = ClampU(uval, this->min, this->max); } else if (uval < (uint)this->min || uval > this->max) { /* Reset invalid discrete setting to its default value */ - uval = (uint32)(size_t)this->def; + uval = (uint32)this->def; } } WriteValue(ptr, SLE_VAR_U32, (int64)uval); @@ -513,11 +513,11 @@ void StringSettingDesc::Write_ValidateSetting(const void *object, const char *st case SLE_VAR_STR: case SLE_VAR_STRQ: if (str != nullptr) { - if (this->max != 0 && strlen(str) >= this->max) { + if (this->max_length != 0 && strlen(str) >= this->max_length) { /* In case a maximum length is imposed by the setting, the length * includes the '\0' termination for network transfer purposes. * Also ensure the string is valid after chopping of some bytes. */ - std::string stdstr(str, this->max - 1); + std::string stdstr(str, this->max_length - 1); dst->assign(str_validate(stdstr, SVS_NONE)); } else { dst->assign(str); @@ -591,19 +591,19 @@ static void IniLoadSettings(IniFile *ini, const SettingTable &settings_table, co void IntSettingDesc::ParseValue(const IniItem *item, void *object) const { - size_t val = (item == nullptr) ? (size_t)this->def : this->ParseValue(item->value.has_value() ? item->value->c_str() : ""); + size_t val = (item == nullptr) ? this->def : this->ParseValue(item->value.has_value() ? item->value->c_str() : ""); this->Write_ValidateSetting(object, (int32)val); } void StringSettingDesc::ParseValue(const IniItem *item, void *object) const { - const char *str = (item == nullptr) ? (const char *)this->def : item->value.has_value() ? item->value->c_str() : nullptr; + const char *str = (item == nullptr) ? this->def : item->value.has_value() ? item->value->c_str() : nullptr; this->Write_ValidateSetting(object, str); } void ListSettingDesc::ParseValue(const IniItem *item, void *object) const { - const char *str = (item == nullptr) ? (const char *)this->def : item->value.has_value() ? item->value->c_str() : nullptr; + const char *str = (item == nullptr) ? this->def : item->value.has_value() ? item->value->c_str() : nullptr; void *ptr = GetVariableAddress(object, &this->save); if (!LoadIntList(str, ptr, this->save.length, GetVarMemType(this->save.conv))) { ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY); @@ -611,7 +611,7 @@ void ListSettingDesc::ParseValue(const IniItem *item, void *object) const _settings_error_list.push_back(msg); /* Use default */ - LoadIntList((const char*)this->def, ptr, this->save.length, GetVarMemType(this->save.conv)); + LoadIntList(this->def, ptr, this->save.length, GetVarMemType(this->save.conv)); } } @@ -2053,7 +2053,7 @@ void SetDefaultCompanySettings(CompanyID cid) Company *c = Company::Get(cid); for (auto &sd : _company_settings) { const IntSettingDesc *int_setting = sd->AsIntSetting(); - int_setting->Write_ValidateSetting(&c->settings, (int32)(size_t)int_setting->def); + int_setting->Write_ValidateSetting(&c->settings, int_setting->def); } } @@ -2212,11 +2212,12 @@ void IConsoleGetSetting(const char *name, bool force_newgame) if (sd->cmd == SDT_STDSTRING) { const void *ptr = GetVariableAddress(object, &sd->save); IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, reinterpret_cast<const std::string *>(ptr)->c_str()); - } else { + } else if (sd->IsIntSetting()) { char value[20]; sd->FormatValue(value, lastof(value), object); + const IntSettingDesc *int_setting = sd->AsIntSetting(); IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %u)", - name, value, (sd->flags & SGF_0ISDISABLED) ? "(0) " : "", sd->min, sd->max); + name, value, (sd->flags & SGF_0ISDISABLED) ? "(0) " : "", int_setting->min, int_setting->max); } } diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index ef1024278..18beb690a 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -1035,8 +1035,7 @@ void SettingEntry::Init(byte level) /* Sets the given setting entry to its default value */ void SettingEntry::ResetAll() { - int32 default_value = ReadValue(&this->setting->def, this->setting->save.conv); - SetSettingValue(this->setting, default_value); + SetSettingValue(this->setting, this->setting->def); } /** @@ -1092,7 +1091,7 @@ bool SettingEntry::IsVisibleByRestrictionMode(RestrictionMode mode) const /* This entry shall only be visible, if the value deviates from its default value. */ /* Read the default value. */ - filter_value = ReadValue(&sd->def, sd->save.conv); + filter_value = sd->def; } else { assert(mode == RM_CHANGED_AGAINST_NEW); /* This entry shall only be visible, if the value deviates from @@ -2086,8 +2085,7 @@ struct GameSettingsWindow : Window { DrawString(r.left, r.right, y, STR_CONFIG_SETTING_TYPE); y += FONT_HEIGHT_NORMAL; - int32 default_value = ReadValue(&sd->def, sd->save.conv); - this->last_clicked->SetValueDParams(0, default_value); + this->last_clicked->SetValueDParams(0, sd->def); DrawString(r.left, r.right, y, STR_CONFIG_SETTING_DEFAULT_VALUE); y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL; @@ -2324,7 +2322,7 @@ struct GameSettingsWindow : Window { value = (int32)ClampToI32(llvalue); } else { - value = (int32)(size_t)sd->def; + value = sd->def; } SetSettingValue(this->valuewindow_entry->setting, value); diff --git a/src/settings_internal.h b/src/settings_internal.h index 0914f2803..be1dfbd93 100644 --- a/src/settings_internal.h +++ b/src/settings_internal.h @@ -85,27 +85,13 @@ typedef size_t OnConvert(const char *value); ///< callback prototype for convers /** Properties of config file settings. */ struct SettingDesc { - SettingDesc(SaveLoad save, const char *name, const void *def, SettingDescType cmd, SettingGuiFlag flags, - int32 min, uint32 max, int32 interval, const char *many, StringID str, StringID str_help, - StringID str_val, OnChange *proc, OnConvert *proc_cnvt, SettingCategory cat, bool startup) : - name(name), def(def), cmd(cmd), flags(flags), min(min), max(max), interval(interval), many(many), str(str), - str_help(str_help), str_val(str_val), proc(proc), proc_cnvt(proc_cnvt), cat(cat), startup(startup), save(save) {} + SettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup) : + name(name), flags(flags), cmd(cmd), startup(startup), save(save) {} virtual ~SettingDesc() {} const char *name; ///< name of the setting. Used in configuration file and for console - const void *def; ///< default value given when none is present - SettingDescType cmd; ///< various flags for the variable SettingGuiFlag flags; ///< handles how a setting would show up in the GUI (text/currency, etc.) - int32 min; ///< minimum values - uint32 max; ///< maximum values - int32 interval; ///< the interval to use between settings in the 'settings' window. If interval is '0' the interval is dynamically determined - const char *many; ///< ONE/MANY_OF_MANY: string of possible values for this type - StringID str; ///< (translated) string with descriptive text; gui and console - StringID str_help; ///< (Translated) string with help text; gui only. - StringID str_val; ///< (Translated) first string describing the value. - OnChange *proc; ///< callback procedure for when the value is changed - OnConvert *proc_cnvt; ///< callback procedure when loading value mechanism fails - SettingCategory cat; ///< assigned categories of the setting + SettingDescType cmd; ///< various flags for the variable bool startup; ///< setting has to be loaded directly at startup? SaveLoad save; ///< Internal structure (going to savegame, parts to config) @@ -146,12 +132,24 @@ struct SettingDesc { /** Integer type, including boolean, settings. Only these are shown in the settings UI. */ struct IntSettingDesc : SettingDesc { IntSettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup, int32 def, - int32 min, uint32 max, int32 interval, StringID str, StringID str_help, StringID str_val, - SettingCategory cat, OnChange *proc, const char *many = nullptr, OnConvert *many_cnvt = nullptr) : - SettingDesc(save, name, (void*)(size_t)def, cmd, flags, min, max, interval, many, str, str_help, str_val, - proc, many_cnvt, cat, startup) {} + int32 min, uint32 max, int32 interval, StringID str, StringID str_help, StringID str_val, + SettingCategory cat, OnChange *proc, const char *many = nullptr, OnConvert *many_cnvt = nullptr) : + SettingDesc(save, name, flags, cmd, startup), def(def), min(min), max(max), interval(interval), + str(str), str_help(str_help), str_val(str_val), cat(cat), proc(proc), many(many), many_cnvt(many_cnvt) {} virtual ~IntSettingDesc() {} + int32 def; ///< default value given when none is present + int32 min; ///< minimum values + uint32 max; ///< maximum values + int32 interval; ///< the interval to use between settings in the 'settings' window. If interval is '0' the interval is dynamically determined + StringID str; ///< (translated) string with descriptive text; gui and console + StringID str_help; ///< (Translated) string with help text; gui only. + StringID str_val; ///< (Translated) first string describing the value. + SettingCategory cat; ///< assigned categories of the setting + OnChange *proc; ///< callback procedure for when the value is changed + const char *many; ///< ONE/MANY_OF_MANY: string of possible values for this type + OnConvert *many_cnvt; ///< callback procedure when loading value mechanism fails + void ChangeValue(const void *object, int32 newvalue) const; void Write_ValidateSetting(const void *object, int32 value) const; @@ -165,9 +163,13 @@ struct IntSettingDesc : SettingDesc { struct StringSettingDesc : SettingDesc { StringSettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup, const char *def, uint32 max_length, OnChange proc) : - SettingDesc(save, name, def, cmd, flags, 0, max_length, 0, nullptr, 0, 0, 0, proc, nullptr, SC_NONE, startup) {} + SettingDesc(save, name, flags, cmd, startup), def(def), max_length(max_length), proc(proc) {} virtual ~StringSettingDesc() {} + const char *def; ///< default value given when none is present + uint32 max_length; ///< maximum length of the string, 0 means no maximum length + OnChange *proc; ///< callback procedure for when the value is changed + void ChangeValue(const void *object, const char *newval) const; void Write_ValidateSetting(const void *object, const char *str) const; @@ -180,9 +182,11 @@ struct StringSettingDesc : SettingDesc { /** List/array settings. */ struct ListSettingDesc : SettingDesc { ListSettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup, const char *def) : - SettingDesc(save, name, def, cmd, flags, 0, 0, 0, nullptr, 0, 0, 0, proc, nullptr, SC_NONE, startup) {} + SettingDesc(save, name, flags, cmd, startup), def(def) {} virtual ~ListSettingDesc() {} + const char *def; ///< default value given when none is present + void FormatValue(char *buf, const char *last, const void *object) const override; void ParseValue(const IniItem *item, void *object) const override; bool IsSameValue(const IniItem *item, void *object) const override; @@ -191,7 +195,7 @@ struct ListSettingDesc : SettingDesc { /** Placeholder for settings that have been removed, but might still linger in the savegame. */ struct NullSettingDesc : SettingDesc { NullSettingDesc(SaveLoad save) : - SettingDesc(save, "", nullptr, SDT_NULL, SGF_NONE, 0, 0, 0, nullptr, 0, 0, 0, nullptr, nullptr, SC_NONE, false) {} + SettingDesc(save, "", SGF_NONE, SDT_NULL, false) {} virtual ~NullSettingDesc() {} void FormatValue(char *buf, const char *last, const void *object) const override { NOT_REACHED(); } |