From 425d50372fab501acc97981953a6c002c0a750cf Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Sun, 23 May 2021 09:51:33 +0200 Subject: [PATCH 1/3] Codechange: let SettingDesc extend SettingDescBase --- src/saveload/linkgraph_sl.cpp | 2 +- src/script/api/script_gamesettings.cpp | 6 +- src/settings.cpp | 78 +++++++++++++------------- src/settings_gui.cpp | 36 ++++++------ src/settings_internal.h | 3 +- 5 files changed, 62 insertions(+), 63 deletions(-) diff --git a/src/saveload/linkgraph_sl.cpp b/src/saveload/linkgraph_sl.cpp index e46054cf0f..3abf986e0f 100644 --- a/src/saveload/linkgraph_sl.cpp +++ b/src/saveload/linkgraph_sl.cpp @@ -75,7 +75,7 @@ const SaveLoad *GetLinkGraphJobDesc() int setting = 0; const SettingDesc *desc = GetSettingDescription(setting); while (desc->save.cmd != SL_END) { - if (desc->desc.name != nullptr && strncmp(desc->desc.name, prefix, prefixlen) == 0) { + if (desc->name != nullptr && strncmp(desc->name, prefix, prefixlen) == 0) { SaveLoad sl = desc->save; sl.address_proc = proc; saveloads.push_back(sl); diff --git a/src/script/api/script_gamesettings.cpp b/src/script/api/script_gamesettings.cpp index 12435d253f..f96048e153 100644 --- a/src/script/api/script_gamesettings.cpp +++ b/src/script/api/script_gamesettings.cpp @@ -18,7 +18,7 @@ /* static */ bool ScriptGameSettings::IsValid(const char *setting) { const SettingDesc *sd = GetSettingFromName(setting); - return sd != nullptr && sd->desc.cmd != SDT_STDSTRING; + return sd != nullptr && sd->cmd != SDT_STDSTRING; } /* static */ int32 ScriptGameSettings::GetValue(const char *setting) @@ -28,7 +28,7 @@ const SettingDesc *sd = GetSettingFromName(setting); void *ptr = GetVariableAddress(&_settings_game, &sd->save); - if (sd->desc.cmd == SDT_BOOLX) return *(bool*)ptr; + if (sd->cmd == SDT_BOOLX) return *(bool*)ptr; return (int32)ReadValue(ptr, sd->save.conv); } @@ -40,7 +40,7 @@ const SettingDesc *sd = GetSettingFromName(setting); if ((sd->save.conv & SLF_NO_NETWORK_SYNC) != 0) return false; - if (sd->desc.cmd != SDT_BOOLX && sd->desc.cmd != SDT_NUMX) return false; + if (sd->cmd != SDT_BOOLX && sd->cmd != SDT_NUMX) return false; return ScriptObject::DoCommand(0, GetSettingIndex(sd), value, CMD_CHANGE_SETTING); } diff --git a/src/settings.cpp b/src/settings.cpp index 5d4e92b358..2133fd63b4 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -434,7 +434,7 @@ static const void *StringToVal(const SettingDescBase *desc, const char *orig_str */ static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val) { - const SettingDescBase *sdb = &sd->desc; + const SettingDescBase *sdb = sd; if (sdb->cmd != SDT_BOOLX && sdb->cmd != SDT_NUMX && @@ -508,11 +508,11 @@ static void Write_ValidateStdString(void *ptr, const SettingDesc *sd, const char case SLE_VAR_STR: case SLE_VAR_STRQ: if (p != nullptr) { - if (sd->desc.max != 0 && strlen(p) >= sd->desc.max) { + if (sd->max != 0 && strlen(p) >= sd->max) { /* 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 str(p, sd->desc.max - 1); + std::string str(p, sd->max - 1); dst->assign(str_validate(str, SVS_NONE)); } else { dst->assign(p); @@ -541,11 +541,11 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp IniGroup *group_def = ini->GetGroup(grpname); for (; sd->save.cmd != SL_END; sd++) { - const SettingDescBase *sdb = &sd->desc; + const SettingDescBase *sdb = sd; const SaveLoad *sld = &sd->save; if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue; - if (sd->desc.startup != only_startup) continue; + if (sd->startup != only_startup) continue; /* For settings.xx.yy load the settings from [xx] yy = ? */ std::string s{ sdb->name }; @@ -593,8 +593,8 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp /* Use default */ LoadIntList((const char*)sdb->def, ptr, sld->length, GetVarMemType(sld->conv)); - } else if (sd->desc.proc_cnvt != nullptr) { - sd->desc.proc_cnvt((const char*)p); + } else if (sd->proc_cnvt != nullptr) { + sd->proc_cnvt((const char*)p); } break; } @@ -623,7 +623,7 @@ static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grp void *ptr; for (; sd->save.cmd != SL_END; sd++) { - const SettingDescBase *sdb = &sd->desc; + const SettingDescBase *sdb = sd; const SaveLoad *sld = &sd->save; /* If the setting is not saved to the configuration @@ -802,13 +802,13 @@ void IniSaveWindowSettings(IniFile *ini, const char *grpname, void *desc) */ bool SettingDesc::IsEditable(bool do_command) const { - if (!do_command && !(this->save.conv & SLF_NO_NETWORK_SYNC) && _networking && !_network_server && !(this->desc.flags & SGF_PER_COMPANY)) return false; - if ((this->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return false; - if ((this->desc.flags & SGF_NO_NETWORK) && _networking) return false; - if ((this->desc.flags & SGF_NEWGAME_ONLY) && + if (!do_command && !(this->save.conv & SLF_NO_NETWORK_SYNC) && _networking && !_network_server && !(this->flags & SGF_PER_COMPANY)) return false; + if ((this->flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return false; + if ((this->flags & SGF_NO_NETWORK) && _networking) return false; + if ((this->flags & SGF_NEWGAME_ONLY) && (_game_mode == GM_NORMAL || - (_game_mode == GM_EDITOR && !(this->desc.flags & SGF_SCENEDIT_TOO)))) return false; - if ((this->desc.flags & SGF_SCENEDIT_ONLY) && _game_mode != GM_EDITOR) return false; + (_game_mode == GM_EDITOR && !(this->flags & SGF_SCENEDIT_TOO)))) return false; + if ((this->flags & SGF_SCENEDIT_ONLY) && _game_mode != GM_EDITOR) return false; return true; } @@ -818,7 +818,7 @@ bool SettingDesc::IsEditable(bool do_command) const */ SettingType SettingDesc::GetType() const { - if (this->desc.flags & SGF_PER_COMPANY) return ST_COMPANY; + if (this->flags & SGF_PER_COMPANY) return ST_COMPANY; return (this->save.conv & SLF_NOT_IN_SAVE) ? ST_CLIENT : ST_GAME; } @@ -1892,14 +1892,14 @@ CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uin if (oldval == newval) return CommandCost(); - if (sd->desc.proc != nullptr && !sd->desc.proc(newval)) { + if (sd->proc != nullptr && !sd->proc(newval)) { WriteValue(var, sd->save.conv, (int64)oldval); return CommandCost(); } - if (sd->desc.flags & SGF_NO_NETWORK) { + if (sd->flags & SGF_NO_NETWORK) { GamelogStartAction(GLAT_SETTING); - GamelogSetting(sd->desc.name, oldval, newval); + GamelogSetting(sd->name, oldval, newval); GamelogStopAction(); } @@ -1937,7 +1937,7 @@ CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 if (oldval == newval) return CommandCost(); - if (sd->desc.proc != nullptr && !sd->desc.proc(newval)) { + if (sd->proc != nullptr && !sd->proc(newval)) { WriteValue(var, sd->save.conv, (int64)oldval); return CommandCost(); } @@ -1955,7 +1955,7 @@ CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 */ uint GetSettingIndex(const SettingDesc *sd) { - assert((sd->desc.flags & SGF_PER_COMPANY) == 0); + assert((sd->flags & SGF_PER_COMPANY) == 0); return sd - _settings; } @@ -1968,14 +1968,14 @@ uint GetSettingIndex(const SettingDesc *sd) */ bool SetSettingValue(const SettingDesc *sd, int32 value, bool force_newgame) { - if ((sd->desc.flags & SGF_PER_COMPANY) != 0) { + if ((sd->flags & SGF_PER_COMPANY) != 0) { if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) { return DoCommandP(0, sd - _company_settings, value, CMD_CHANGE_COMPANY_SETTING); } void *var = GetVariableAddress(&_settings_client.company, &sd->save); Write_ValidateSetting(var, sd, value); - if (sd->desc.proc != nullptr) sd->desc.proc((int32)ReadValue(var, sd->save.conv)); + if (sd->proc != nullptr) sd->proc((int32)ReadValue(var, sd->save.conv)); return true; } @@ -1991,7 +1991,7 @@ bool SetSettingValue(const SettingDesc *sd, int32 value, bool force_newgame) void *var2 = GetVariableAddress(&_settings_newgame, &sd->save); Write_ValidateSetting(var2, sd, value); } - if (sd->desc.proc != nullptr) sd->desc.proc((int32)ReadValue(var, sd->save.conv)); + if (sd->proc != nullptr) sd->proc((int32)ReadValue(var, sd->save.conv)); SetWindowClassesDirty(WC_GAME_OPTIONS); @@ -2023,7 +2023,7 @@ void SetDefaultCompanySettings(CompanyID cid) const SettingDesc *sd; for (sd = _company_settings; sd->save.cmd != SL_END; sd++) { void *var = GetVariableAddress(&c->settings, &sd->save); - Write_ValidateSetting(var, sd, (int32)(size_t)sd->desc.def); + Write_ValidateSetting(var, sd, (int32)(size_t)sd->def); } } @@ -2051,7 +2051,7 @@ void SyncCompanySettings() uint GetCompanySettingIndex(const char *name) { const SettingDesc *sd = GetSettingFromName(name); - assert(sd != nullptr && (sd->desc.flags & SGF_PER_COMPANY) != 0); + assert(sd != nullptr && (sd->flags & SGF_PER_COMPANY) != 0); return sd - _company_settings; } @@ -2072,7 +2072,7 @@ bool SetSettingValue(const SettingDesc *sd, const char *value, bool force_newgam void *ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save); Write_ValidateStdString(ptr, sd, value); - if (sd->desc.proc != nullptr) sd->desc.proc(0); + if (sd->proc != nullptr) sd->proc(0); if (_save_config) SaveToConfig(); return true; @@ -2090,13 +2090,13 @@ const SettingDesc *GetSettingFromName(const char *name) /* First check all full names */ for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) { if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; - if (strcmp(sd->desc.name, name) == 0) return sd; + if (strcmp(sd->name, name) == 0) return sd; } /* Then check the shortcut variant of the name. */ for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) { if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; - const char *short_name = strchr(sd->desc.name, '.'); + const char *short_name = strchr(sd->name, '.'); if (short_name != nullptr) { short_name++; if (strcmp(short_name, name) == 0) return sd; @@ -2107,7 +2107,7 @@ const SettingDesc *GetSettingFromName(const char *name) /* And finally the company-based settings */ for (const SettingDesc *sd = _company_settings; sd->save.cmd != SL_END; sd++) { if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; - if (strcmp(sd->desc.name, name) == 0) return sd; + if (strcmp(sd->name, name) == 0) return sd; } return nullptr; @@ -2125,7 +2125,7 @@ void IConsoleSetSetting(const char *name, const char *value, bool force_newgame) } bool success; - if (sd->desc.cmd == SDT_STDSTRING) { + if (sd->cmd == SDT_STDSTRING) { success = SetSettingValue(sd, value, force_newgame); } else { uint32 val; @@ -2173,17 +2173,17 @@ void IConsoleGetSetting(const char *name, bool force_newgame) ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save); - if (sd->desc.cmd == SDT_STDSTRING) { + if (sd->cmd == SDT_STDSTRING) { IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, reinterpret_cast(ptr)->c_str()); } else { - if (sd->desc.cmd == SDT_BOOLX) { + if (sd->cmd == SDT_BOOLX) { seprintf(value, lastof(value), (*(const bool*)ptr != 0) ? "on" : "off"); } else { - seprintf(value, lastof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv)); + seprintf(value, lastof(value), sd->min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv)); } IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %u)", - name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max); + name, value, (sd->flags & SGF_0ISDISABLED) ? "(0) " : "", sd->min, sd->max); } } @@ -2198,18 +2198,18 @@ void IConsoleListSettings(const char *prefilter) for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) { if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; - if (prefilter != nullptr && strstr(sd->desc.name, prefilter) == nullptr) continue; + if (prefilter != nullptr && strstr(sd->name, prefilter) == nullptr) continue; char value[80]; const void *ptr = GetVariableAddress(&GetGameSettings(), &sd->save); - if (sd->desc.cmd == SDT_BOOLX) { + if (sd->cmd == SDT_BOOLX) { seprintf(value, lastof(value), (*(const bool *)ptr != 0) ? "on" : "off"); - } else if (sd->desc.cmd == SDT_STDSTRING) { + } else if (sd->cmd == SDT_STDSTRING) { seprintf(value, lastof(value), "%s", reinterpret_cast(ptr)->c_str()); } else { - seprintf(value, lastof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv)); + seprintf(value, lastof(value), sd->min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv)); } - IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value); + IConsolePrintF(CC_DEFAULT, "%s = %s", sd->name, value); } IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value"); diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index b069d27066..edde335601 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -843,7 +843,7 @@ struct SettingEntry : BaseSettingEntry { */ inline StringID GetHelpText() const { - return this->setting->desc.str_help; + return this->setting->str_help; } void SetValueDParams(uint first_param, int32 value) const; @@ -1036,7 +1036,7 @@ void SettingEntry::Init(byte level) /* Sets the given setting entry to its default value */ void SettingEntry::ResetAll() { - int32 default_value = ReadValue(&this->setting->desc.def, this->setting->save.conv); + int32 default_value = ReadValue(&this->setting->def, this->setting->save.conv); SetSettingValue(this->setting, default_value); } @@ -1080,8 +1080,8 @@ bool SettingEntry::IsVisibleByRestrictionMode(RestrictionMode mode) const GameSettings *settings_ptr = &GetGameSettings(); const SettingDesc *sd = this->setting; - if (mode == RM_BASIC) return (this->setting->desc.cat & SC_BASIC_LIST) != 0; - if (mode == RM_ADVANCED) return (this->setting->desc.cat & SC_ADVANCED_LIST) != 0; + if (mode == RM_BASIC) return (this->setting->cat & SC_BASIC_LIST) != 0; + if (mode == RM_ADVANCED) return (this->setting->cat & SC_ADVANCED_LIST) != 0; /* Read the current value. */ const void *var = ResolveVariableAddress(settings_ptr, sd); @@ -1093,7 +1093,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->desc.def, sd->save.conv); + filter_value = ReadValue(&sd->def, sd->save.conv); } else { assert(mode == RM_CHANGED_AGAINST_NEW); /* This entry shall only be visible, if the value deviates from @@ -1127,7 +1127,7 @@ bool SettingEntry::UpdateFilterState(SettingFilter &filter, bool force_visible) /* Process the search text filter for this item. */ filter.string.ResetState(); - const SettingDescBase *sdb = &sd->desc; + const SettingDescBase *sdb = sd; SetDParam(0, STR_EMPTY); filter.string.AddLine(sdb->str); @@ -1153,7 +1153,7 @@ bool SettingEntry::UpdateFilterState(SettingFilter &filter, bool force_visible) static const void *ResolveVariableAddress(const GameSettings *settings_ptr, const SettingDesc *sd) { - if ((sd->desc.flags & SGF_PER_COMPANY) != 0) { + if ((sd->flags & SGF_PER_COMPANY) != 0) { if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) { return GetVariableAddress(&Company::Get(_local_company)->settings, &sd->save); } else { @@ -1171,7 +1171,7 @@ static const void *ResolveVariableAddress(const GameSettings *settings_ptr, cons */ void SettingEntry::SetValueDParams(uint first_param, int32 value) const { - const SettingDescBase *sdb = &this->setting->desc; + const SettingDescBase *sdb = this->setting; if (sdb->cmd == SDT_BOOLX) { SetDParam(first_param++, value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF); } else { @@ -1198,7 +1198,7 @@ void SettingEntry::SetValueDParams(uint first_param, int32 value) const void SettingEntry::DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const { const SettingDesc *sd = this->setting; - const SettingDescBase *sdb = &sd->desc; + const SettingDescBase *sdb = sd; const void *var = ResolveVariableAddress(settings_ptr, sd); int state = this->flags & SEF_BUTTONS_MASK; @@ -2091,7 +2091,7 @@ struct GameSettingsWindow : Window { DrawString(r.left, r.right, y, STR_CONFIG_SETTING_TYPE); y += FONT_HEIGHT_NORMAL; - int32 default_value = ReadValue(&sd->desc.def, sd->save.conv); + int32 default_value = ReadValue(&sd->def, sd->save.conv); this->last_clicked->SetValueDParams(0, default_value); DrawString(r.left, r.right, y, STR_CONFIG_SETTING_DEFAULT_VALUE); y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL; @@ -2195,8 +2195,8 @@ struct GameSettingsWindow : Window { int32 value = (int32)ReadValue(var, sd->save.conv); /* clicked on the icon on the left side. Either scroller, bool on/off or dropdown */ - if (x < SETTING_BUTTON_WIDTH && (sd->desc.flags & SGF_MULTISTRING)) { - const SettingDescBase *sdb = &sd->desc; + if (x < SETTING_BUTTON_WIDTH && (sd->flags & SGF_MULTISTRING)) { + const SettingDescBase *sdb = sd; this->SetDisplayedHelpText(pe); if (this->valuedropdown_entry == pe) { @@ -2234,7 +2234,7 @@ struct GameSettingsWindow : Window { this->SetDirty(); } else if (x < SETTING_BUTTON_WIDTH) { this->SetDisplayedHelpText(pe); - const SettingDescBase *sdb = &sd->desc; + const SettingDescBase *sdb = sd; int32 oldvalue = value; switch (sdb->cmd) { @@ -2291,10 +2291,10 @@ struct GameSettingsWindow : Window { } } else { /* Only open editbox if clicked for the second time, and only for types where it is sensible for. */ - if (this->last_clicked == pe && sd->desc.cmd != SDT_BOOLX && !(sd->desc.flags & SGF_MULTISTRING)) { + if (this->last_clicked == pe && sd->cmd != SDT_BOOLX && !(sd->flags & SGF_MULTISTRING)) { int64 value64 = value; /* Show the correct currency-translated value */ - if (sd->desc.flags & SGF_CURRENCY) value64 *= _currency->rate; + if (sd->flags & SGF_CURRENCY) value64 *= _currency->rate; this->valuewindow_entry = pe; SetDParam(0, value64); @@ -2327,11 +2327,11 @@ struct GameSettingsWindow : Window { long long llvalue = atoll(str); /* Save the correct currency-translated value */ - if (sd->desc.flags & SGF_CURRENCY) llvalue /= _currency->rate; + if (sd->flags & SGF_CURRENCY) llvalue /= _currency->rate; value = (int32)ClampToI32(llvalue); } else { - value = (int32)(size_t)sd->desc.def; + value = (int32)(size_t)sd->def; } SetSettingValue(this->valuewindow_entry->setting, value); @@ -2368,7 +2368,7 @@ struct GameSettingsWindow : Window { /* Deal with drop down boxes on the panel. */ assert(this->valuedropdown_entry != nullptr); const SettingDesc *sd = this->valuedropdown_entry->setting; - assert(sd->desc.flags & SGF_MULTISTRING); + assert(sd->flags & SGF_MULTISTRING); SetSettingValue(sd, index); this->SetDirty(); diff --git a/src/settings_internal.h b/src/settings_internal.h index fc8e0a1e30..f92480a764 100644 --- a/src/settings_internal.h +++ b/src/settings_internal.h @@ -100,8 +100,7 @@ struct SettingDescBase { bool startup; ///< setting has to be loaded directly at startup? }; -struct SettingDesc { - SettingDescBase desc; ///< Settings structure (going to configuration file) +struct SettingDesc : SettingDescBase { SaveLoad save; ///< Internal structure (going to savegame, parts to config) bool IsEditable(bool do_command = false) const; From 3bb6ce8827b71ae1dcfde4a7836fb6c636fb5a67 Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Tue, 18 May 2021 21:01:42 +0200 Subject: [PATCH 2/3] Codechange: use initializer_lists for the settings tables Not using vectors as those require copying from the initializer list and that makes unique_ptrs to the actual SettingDesc objects later impossible. --- src/saveload/linkgraph_sl.cpp | 4 +- src/settings.cpp | 169 ++++++++++++++++++-------------- src/settings_internal.h | 2 + src/table/company_settings.ini | 8 +- src/table/currency_settings.ini | 6 +- src/table/gameopt_settings.ini | 6 +- src/table/misc_settings.ini | 6 +- src/table/settings.h.preamble | 4 - src/table/settings.ini | 6 +- src/table/win32_settings.ini | 6 +- src/table/window_settings.ini | 7 +- 11 files changed, 107 insertions(+), 117 deletions(-) diff --git a/src/saveload/linkgraph_sl.cpp b/src/saveload/linkgraph_sl.cpp index 3abf986e0f..af28902445 100644 --- a/src/saveload/linkgraph_sl.cpp +++ b/src/saveload/linkgraph_sl.cpp @@ -74,7 +74,7 @@ const SaveLoad *GetLinkGraphJobDesc() int setting = 0; const SettingDesc *desc = GetSettingDescription(setting); - while (desc->save.cmd != SL_END) { + while (desc != nullptr) { if (desc->name != nullptr && strncmp(desc->name, prefix, prefixlen) == 0) { SaveLoad sl = desc->save; sl.address_proc = proc; @@ -86,7 +86,7 @@ const SaveLoad *GetLinkGraphJobDesc() int i = 0; do { saveloads.push_back(job_desc[i++]); - } while (saveloads[saveloads.size() - 1].cmd != SL_END); + } while (saveloads.back().cmd != SL_END); } return &saveloads[0]; diff --git a/src/settings.cpp b/src/settings.cpp index 2133fd63b4..746e477da9 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -87,11 +87,33 @@ typedef std::list ErrorList; static ErrorList _settings_error_list; ///< Errors while loading minimal settings. -typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object, bool only_startup); +typedef void SettingDescProc(IniFile *ini, const SettingTable &desc, const char *grpname, void *object, bool only_startup); typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList &list); static bool IsSignedVarMemType(VarType vt); +/** + * Get the setting at the given index into the settings table. + * @param index The index to look for. + * @return The setting at the given index, or nullptr when the index is invalid. + */ +const SettingDesc *GetSettingDescription(uint index) +{ + if (index >= _settings.size()) return nullptr; + return &_settings.begin()[index]; +} + +/** + * Get the setting at the given index into the company settings table. + * @param index The index to look for. + * @return The setting at the given index, or nullptr when the index is invalid. + */ +static const SettingDesc *GetCompanySettingDescription(uint index) +{ + if (index >= _company_settings.size()) return nullptr; + return &_company_settings.begin()[index]; +} + /** * Groups in openttd.cfg that are actually lists. */ @@ -529,23 +551,23 @@ static void Write_ValidateStdString(void *ptr, const SettingDesc *sd, const char /** * Load values from a group of an IniFile structure into the internal representation * @param ini pointer to IniFile structure that holds administrative information - * @param sd pointer to SettingDesc structure whose internally pointed variables will + * @param settings_table table with SettingDesc structures whose internally pointed variables will * be given values * @param grpname the group of the IniFile to search in for the new values * @param object pointer to the object been loaded * @param only_startup load only the startup settings set */ -static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object, bool only_startup) +static void IniLoadSettings(IniFile *ini, const SettingTable &settings_table, const char *grpname, void *object, bool only_startup) { IniGroup *group; IniGroup *group_def = ini->GetGroup(grpname); - for (; sd->save.cmd != SL_END; sd++) { - const SettingDescBase *sdb = sd; - const SaveLoad *sld = &sd->save; + for (auto &sd : settings_table) { + const SettingDescBase *sdb = &sd; + const SaveLoad *sld = &sd.save; if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue; - if (sd->startup != only_startup) continue; + if (sd.startup != only_startup) continue; /* For settings.xx.yy load the settings from [xx] yy = ? */ std::string s{ sdb->name }; @@ -578,11 +600,11 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp case SDT_NUMX: case SDT_ONEOFMANY: case SDT_MANYOFMANY: - Write_ValidateSetting(ptr, sd, (int32)(size_t)p); + Write_ValidateSetting(ptr, &sd, (int32)(size_t)p); break; case SDT_STDSTRING: - Write_ValidateStdString(ptr, sd, (const char *)p); + Write_ValidateStdString(ptr, &sd, (const char *)p); break; case SDT_INTLIST: { @@ -593,8 +615,8 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp /* Use default */ LoadIntList((const char*)sdb->def, ptr, sld->length, GetVarMemType(sld->conv)); - } else if (sd->proc_cnvt != nullptr) { - sd->proc_cnvt((const char*)p); + } else if (sd.proc_cnvt != nullptr) { + sd.proc_cnvt((const char*)p); } break; } @@ -615,16 +637,16 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp * values are reloaded when saving). If settings indeed have changed, we get * these and save them. */ -static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object, bool) +static void IniSaveSettings(IniFile *ini, const SettingTable &settings_table, const char *grpname, void *object, bool) { IniGroup *group_def = nullptr, *group; IniItem *item; char buf[512]; void *ptr; - for (; sd->save.cmd != SL_END; sd++) { - const SettingDescBase *sdb = sd; - const SaveLoad *sld = &sd->save; + for (auto &sd : settings_table) { + const SettingDescBase *sdb = &sd; + const SaveLoad *sld = &sd.save; /* If the setting is not saved to the configuration * file, just continue with the next setting */ @@ -1424,7 +1446,7 @@ static void HandleOldDiffCustom(bool savegame) } for (uint i = 0; i < options_to_load; i++) { - const SettingDesc *sd = &_settings[i]; + const SettingDesc *sd = GetSettingDescription(i); /* Skip deprecated options */ if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; void *var = GetVariableAddress(savegame ? &_settings_game : &_settings_newgame, &sd->save); @@ -1708,9 +1730,9 @@ static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *li /* Common handler for saving/loading variables to the configuration file */ static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list, bool only_startup = false) { - proc(ini, (const SettingDesc*)_misc_settings, "misc", nullptr, only_startup); + proc(ini, _misc_settings, "misc", nullptr, only_startup); #if defined(_WIN32) && !defined(DEDICATED) - proc(ini, (const SettingDesc*)_win32_settings, "win32", nullptr, only_startup); + proc(ini, _win32_settings, "win32", nullptr, only_startup); #endif /* _WIN32 */ proc(ini, _settings, "patches", &_settings_newgame, only_startup); @@ -1855,12 +1877,6 @@ void DeleteGRFPresetFromConfig(const char *config_name) delete ini; } -const SettingDesc *GetSettingDescription(uint index) -{ - if (index >= lengthof(_settings)) return nullptr; - return &_settings[index]; -} - /** * Network-safe changing of settings (server-only). * @param tile unused @@ -1923,8 +1939,8 @@ CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uin */ CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (p1 >= lengthof(_company_settings)) return CMD_ERROR; - const SettingDesc *sd = &_company_settings[p1]; + const SettingDesc *sd = GetCompanySettingDescription(p1); + if (sd == nullptr) return CMD_ERROR; if (flags & DC_EXEC) { void *var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save); @@ -1955,8 +1971,19 @@ CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 */ uint GetSettingIndex(const SettingDesc *sd) { - assert((sd->flags & SGF_PER_COMPANY) == 0); - return sd - _settings; + assert(sd != nullptr && (sd->flags & SGF_PER_COMPANY) == 0); + return sd - _settings.begin(); +} + +/** + * Get the index of the company setting with this description. + * @param sd the setting to get the index for. + * @return the index of the setting to be used for CMD_CHANGE_COMPANY_SETTING. + */ +static uint GetCompanySettingIndex(const SettingDesc *sd) +{ + assert(sd != nullptr && (sd->flags & SGF_PER_COMPANY) != 0); + return sd - _company_settings.begin(); } /** @@ -1970,7 +1997,7 @@ bool SetSettingValue(const SettingDesc *sd, int32 value, bool force_newgame) { if ((sd->flags & SGF_PER_COMPANY) != 0) { if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) { - return DoCommandP(0, sd - _company_settings, value, CMD_CHANGE_COMPANY_SETTING); + return DoCommandP(0, GetCompanySettingIndex(sd), value, CMD_CHANGE_COMPANY_SETTING); } void *var = GetVariableAddress(&_settings_client.company, &sd->save); @@ -2020,10 +2047,9 @@ bool SetSettingValue(const SettingDesc *sd, int32 value, bool force_newgame) void SetDefaultCompanySettings(CompanyID cid) { Company *c = Company::Get(cid); - const SettingDesc *sd; - for (sd = _company_settings; sd->save.cmd != SL_END; sd++) { - void *var = GetVariableAddress(&c->settings, &sd->save); - Write_ValidateSetting(var, sd, (int32)(size_t)sd->def); + for (auto &sd : _company_settings) { + void *var = GetVariableAddress(&c->settings, &sd.save); + Write_ValidateSetting(var, &sd, (int32)(size_t)sd.def); } } @@ -2032,14 +2058,14 @@ void SetDefaultCompanySettings(CompanyID cid) */ void SyncCompanySettings() { - const SettingDesc *sd; uint i = 0; - for (sd = _company_settings; sd->save.cmd != SL_END; sd++, i++) { - const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save); - const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save); - uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv); - uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv); + for (auto &sd : _company_settings) { + const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd.save); + const void *new_var = GetVariableAddress(&_settings_client.company, &sd.save); + uint32 old_value = (uint32)ReadValue(old_var, sd.save.conv); + uint32 new_value = (uint32)ReadValue(new_var, sd.save.conv); if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, nullptr, nullptr, _local_company); + i++; } } @@ -2050,9 +2076,7 @@ void SyncCompanySettings() */ uint GetCompanySettingIndex(const char *name) { - const SettingDesc *sd = GetSettingFromName(name); - assert(sd != nullptr && (sd->flags & SGF_PER_COMPANY) != 0); - return sd - _company_settings; + return GetCompanySettingIndex(GetSettingFromName(name)); } /** @@ -2088,26 +2112,26 @@ bool SetSettingValue(const SettingDesc *sd, const char *value, bool force_newgam const SettingDesc *GetSettingFromName(const char *name) { /* First check all full names */ - for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) { - if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; - if (strcmp(sd->name, name) == 0) return sd; + for (auto &sd : _settings) { + if (!SlIsObjectCurrentlyValid(sd.save.version_from, sd.save.version_to)) continue; + if (strcmp(sd.name, name) == 0) return &sd; } /* Then check the shortcut variant of the name. */ - for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) { - if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; - const char *short_name = strchr(sd->name, '.'); + for (auto &sd : _settings) { + if (!SlIsObjectCurrentlyValid(sd.save.version_from, sd.save.version_to)) continue; + const char *short_name = strchr(sd.name, '.'); if (short_name != nullptr) { short_name++; - if (strcmp(short_name, name) == 0) return sd; + if (strcmp(short_name, name) == 0) return &sd; } } if (strncmp(name, "company.", 8) == 0) name += 8; /* And finally the company-based settings */ - for (const SettingDesc *sd = _company_settings; sd->save.cmd != SL_END; sd++) { - if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; - if (strcmp(sd->name, name) == 0) return sd; + for (auto &sd : _company_settings) { + if (!SlIsObjectCurrentlyValid(sd.save.version_from, sd.save.version_to)) continue; + if (strcmp(sd.name, name) == 0) return &sd; } return nullptr; @@ -2196,20 +2220,20 @@ void IConsoleListSettings(const char *prefilter) { IConsolePrintF(CC_WARNING, "All settings with their current value:"); - for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) { - if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; - if (prefilter != nullptr && strstr(sd->name, prefilter) == nullptr) continue; + for (auto &sd : _settings) { + if (!SlIsObjectCurrentlyValid(sd.save.version_from, sd.save.version_to)) continue; + if (prefilter != nullptr && strstr(sd.name, prefilter) == nullptr) continue; char value[80]; - const void *ptr = GetVariableAddress(&GetGameSettings(), &sd->save); + const void *ptr = GetVariableAddress(&GetGameSettings(), &sd.save); - if (sd->cmd == SDT_BOOLX) { + if (sd.cmd == SDT_BOOLX) { seprintf(value, lastof(value), (*(const bool *)ptr != 0) ? "on" : "off"); - } else if (sd->cmd == SDT_STDSTRING) { + } else if (sd.cmd == SDT_STDSTRING) { seprintf(value, lastof(value), "%s", reinterpret_cast(ptr)->c_str()); } else { - seprintf(value, lastof(value), sd->min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv)); + seprintf(value, lastof(value), sd.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd.save.conv)); } - IConsolePrintF(CC_DEFAULT, "%s = %s", sd->name, value); + IConsolePrintF(CC_DEFAULT, "%s = %s", sd.name, value); } IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value"); @@ -2217,41 +2241,40 @@ void IConsoleListSettings(const char *prefilter) /** * Save and load handler for settings - * @param osd SettingDesc struct containing all information + * @param settings SettingDesc struct containing all information * @param object can be either nullptr in which case we load global variables or * a pointer to a struct which is getting saved */ -static void LoadSettings(const SettingDesc *osd, void *object) +static void LoadSettings(const SettingTable &settings, void *object) { - for (; osd->save.cmd != SL_END; osd++) { - const SaveLoad *sld = &osd->save; + for (auto &osd : settings) { + const SaveLoad *sld = &osd.save; void *ptr = GetVariableAddress(object, sld); if (!SlObjectMember(ptr, sld)) continue; - if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, osd, ReadValue(ptr, sld->conv)); + if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, &osd, ReadValue(ptr, sld->conv)); } } /** * Save and load handler for settings - * @param sd SettingDesc struct containing all information + * @param settings SettingDesc struct containing all information * @param object can be either nullptr in which case we load global variables or * a pointer to a struct which is getting saved */ -static void SaveSettings(const SettingDesc *sd, void *object) +static void SaveSettings(const SettingTable &settings, void *object) { /* We need to write the CH_RIFF header, but unfortunately can't call * SlCalcLength() because we have a different format. So do this manually */ - const SettingDesc *i; size_t length = 0; - for (i = sd; i->save.cmd != SL_END; i++) { - length += SlCalcObjMemberLength(object, &i->save); + for (auto &sd : settings) { + length += SlCalcObjMemberLength(object, &sd.save); } SlSetLength(length); - for (i = sd; i->save.cmd != SL_END; i++) { - void *ptr = GetVariableAddress(object, &i->save); - SlObjectMember(ptr, &i->save); + for (auto &sd : settings) { + void *ptr = GetVariableAddress(object, &sd.save); + SlObjectMember(ptr, &sd.save); } } diff --git a/src/settings_internal.h b/src/settings_internal.h index f92480a764..fca85de0c7 100644 --- a/src/settings_internal.h +++ b/src/settings_internal.h @@ -107,6 +107,8 @@ struct SettingDesc : SettingDescBase { SettingType GetType() const; }; +typedef std::initializer_list SettingTable; + const SettingDesc *GetSettingFromName(const char *name); bool SetSettingValue(const SettingDesc *sd, int32 value, bool force_newgame = false); bool SetSettingValue(const SettingDesc *sd, const char *value, bool force_newgame = false); diff --git a/src/table/company_settings.ini b/src/table/company_settings.ini index eea2371fc6..eecebb40e6 100644 --- a/src/table/company_settings.ini +++ b/src/table/company_settings.ini @@ -12,13 +12,12 @@ static bool UpdateIntervalRoadVeh(int32 p1); static bool UpdateIntervalShips(int32 p1); static bool UpdateIntervalAircraft(int32 p1); -static const SettingDesc _company_settings[] = { +static const SettingTable _company_settings{ [post-amble] }; [templates] SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), -SDT_END = SDT_END() [validation] SDT_VAR = static_assert($max <= MAX_$type, "Maximum value for $base.$var exceeds storage size"); @@ -136,8 +135,3 @@ str = STR_CONFIG_SETTING_SERVINT_AIRCRAFT strhelp = STR_CONFIG_SETTING_SERVINT_AIRCRAFT_HELPTEXT strval = STR_CONFIG_SETTING_SERVINT_VALUE proc = UpdateIntervalAircraft - -[SDT_END] - - -}; diff --git a/src/table/currency_settings.ini b/src/table/currency_settings.ini index d2f12473e7..613f9bfa55 100644 --- a/src/table/currency_settings.ini +++ b/src/table/currency_settings.ini @@ -5,13 +5,12 @@ ; [pre-amble] -static const SettingDesc _currency_settings[] = { +static const SettingTable _currency_settings{ [post-amble] }; [templates] SDT_VAR = SDT_VAR ($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDT_SSTR = SDT_SSTR($base, $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), -SDT_END = SDT_END() [validation] SDT_VAR = static_assert($max <= MAX_$type, "Maximum value for $base.$var exceeds storage size"); @@ -67,6 +66,3 @@ base = CurrencySpec var = suffix type = SLE_STRQ def = "" credits"" - -[SDT_END] - diff --git a/src/table/gameopt_settings.ini b/src/table/gameopt_settings.ini index b0b1798ab9..39caa45c95 100644 --- a/src/table/gameopt_settings.ini +++ b/src/table/gameopt_settings.ini @@ -23,7 +23,7 @@ static const char *_osk_activation = "disabled|double|single|immediately"; static const char *_settings_profiles = "easy|medium|hard"; static const char *_news_display = "off|summarized|full"; -static const SettingDesc _gameopt_settings[] = { +static const SettingTable _gameopt_settings{ /* In version 4 a new difficulty setting has been added to the difficulty settings, * town attitude towards demolishing. Needs special handling because some dimwit thought * it funny to have the GameDifficulty struct be an array while it is a struct of @@ -43,7 +43,6 @@ SDTC_OMANY = SDTC_OMANY( $var, $type, $flags, $guiflags, $def, $ma SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDT_OMANY = SDT_OMANY($base, $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $load, $cat, $extra, $startup), SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), -SDT_END = SDT_END() [validation] SDTG_VAR = static_assert($max <= MAX_$type, "Maximum value for $var exceeds storage size"); @@ -186,6 +185,3 @@ def = 1 max = 1 full = _roadsides cat = SC_BASIC - -[SDT_END] - diff --git a/src/table/misc_settings.ini b/src/table/misc_settings.ini index 2613b4dddb..4e1c3482ec 100644 --- a/src/table/misc_settings.ini +++ b/src/table/misc_settings.ini @@ -16,7 +16,7 @@ extern bool _allow_hidpi_window; #define WITHOUT_COCOA #endif -static const SettingDesc _misc_settings[] = { +static const SettingTable _misc_settings{ [post-amble] }; [templates] @@ -26,7 +26,6 @@ SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, SDTG_SSTR = SDTG_SSTR($name, $type, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), -SDTG_END = SDTG_END() [validation] SDTG_VAR = static_assert($max <= MAX_$type, "Maximum value for $var exceeds storage size"); @@ -353,6 +352,3 @@ ifdef = WITH_COCOA name = ""allow_hidpi"" var = _allow_hidpi_window def = true - -[SDTG_END] - diff --git a/src/table/settings.h.preamble b/src/table/settings.h.preamble index 6410b0360f..f9bb279020 100644 --- a/src/table/settings.h.preamble +++ b/src/table/settings.h.preamble @@ -83,8 +83,6 @@ static size_t ConvertLandscape(const char *value); #define SDTG_NULL(length, from, to)\ {{"", nullptr, SDT_NUMX, SGF_NONE, 0, 0, 0, nullptr, STR_NULL, STR_NULL, STR_NULL, nullptr, nullptr, SC_NONE, false}, SLEG_NULL(length, from, to)} -#define SDTG_END() {{nullptr, nullptr, SDT_NUMX, SGF_NONE, 0, 0, 0, nullptr, STR_NULL, STR_NULL, STR_NULL, nullptr, nullptr, SC_NONE, false}, SLEG_END()} - /* Macros for various objects to go in the configuration file. * This section is for structures where their various members are saved */ #define SDT_GENERAL(name, sdt_cmd, sle_cmd, type, flags, guiflags, base, var, length, def, min, max, interval, full, str, strhelp, strval, proc, load, from, to, cat, extra, startup)\ @@ -127,5 +125,3 @@ static size_t ConvertLandscape(const char *value); #define SDTC_OMANY(var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, cat, extra, startup)\ SDTG_GENERAL(#var, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, _settings_client.var, 1, def, 0, max, 0, full, str, strhelp, strval, proc, from, to, cat, extra, startup) -#define SDT_END() {{nullptr, nullptr, SDT_NUMX, SGF_NONE, 0, 0, 0, nullptr, STR_NULL, STR_NULL, STR_NULL, nullptr, nullptr, SC_NONE, false}, SLE_END()} - diff --git a/src/table/settings.ini b/src/table/settings.ini index eea30e003c..ed07dfb118 100644 --- a/src/table/settings.ini +++ b/src/table/settings.ini @@ -59,7 +59,7 @@ static bool UpdateClientConfigValues(int32 p1); * assigns its own value. If the setting was company-based, that would mean that * vehicles could decide on different moments that they are heading back to a * service depot, causing desyncs on a massive scale. */ -const SettingDesc _settings[] = { +const SettingTable _settings{ [post-amble] }; [templates] @@ -76,7 +76,6 @@ SDT_OMANY = SDT_OMANY($base, $var, $type, $flags, $guiflags, $def, SDT_SSTR = SDT_SSTR($base, $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDT_NULL = SDT_NULL($length, $from, $to), -SDT_END = SDT_END() [validation] SDTG_VAR = static_assert($max <= MAX_$type, "Maximum value for $var exceeds storage size"); @@ -4112,6 +4111,3 @@ str = STR_CONFIG_SETTING_RIGHT_MOUSE_BTN_EMU strhelp = STR_CONFIG_SETTING_RIGHT_MOUSE_BTN_EMU_HELPTEXT strval = STR_CONFIG_SETTING_RIGHT_MOUSE_BTN_EMU_COMMAND cat = SC_BASIC - -[SDT_END] - diff --git a/src/table/win32_settings.ini b/src/table/win32_settings.ini index 963a11df8c..669474cc74 100644 --- a/src/table/win32_settings.ini +++ b/src/table/win32_settings.ini @@ -9,14 +9,13 @@ #if defined(_WIN32) && !defined(DEDICATED) extern bool _window_maximize; -static const SettingDesc _win32_settings[] = { +static const SettingTable _win32_settings{ [post-amble] }; #endif /* _WIN32 */ [templates] SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), -SDTG_END = SDTG_END() [validation] SDTG_VAR = static_assert($max <= MAX_$type, "Maximum value for $var exceeds storage size"); @@ -42,6 +41,3 @@ name = ""window_maximize"" var = _window_maximize def = false cat = SC_BASIC - -[SDTG_END] - diff --git a/src/table/window_settings.ini b/src/table/window_settings.ini index fb252076ce..5e11ea002a 100644 --- a/src/table/window_settings.ini +++ b/src/table/window_settings.ini @@ -6,13 +6,12 @@ [pre-amble] -static const SettingDesc _window_settings[] = { +static const SettingTable _window_settings{ [post-amble] }; [templates] SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), -SDT_END = SDT_END() [validation] SDT_VAR = static_assert($max <= MAX_$type, "Maximum value for $base.$var exceeds storage size"); @@ -52,7 +51,3 @@ type = SLE_INT16 def = 0 min = 0 max = 32000 - -[SDT_END] - -}; From cf6b91f30f2e923b0084255e4082605803d2fd32 Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Sun, 23 May 2021 10:29:38 +0200 Subject: [PATCH 3/3] Codechange: do not use SettingDescBase directly when not needed --- src/settings.cpp | 12 +++++------ src/settings_gui.cpp | 48 +++++++++++++++++++------------------------- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/src/settings.cpp b/src/settings.cpp index 746e477da9..15d90f312b 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -379,7 +379,7 @@ static void MakeManyOfMany(char *buf, const char *last, const char *many, uint32 * @param orig_str input string that will be parsed based on the type of desc * @return return the parsed value of the setting */ -static const void *StringToVal(const SettingDescBase *desc, const char *orig_str) +static const void *StringToVal(const SettingDesc *desc, const char *orig_str) { const char *str = orig_str == nullptr ? "" : orig_str; @@ -456,7 +456,7 @@ static const void *StringToVal(const SettingDescBase *desc, const char *orig_str */ static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val) { - const SettingDescBase *sdb = sd; + const SettingDesc *sdb = sd; if (sdb->cmd != SDT_BOOLX && sdb->cmd != SDT_NUMX && @@ -563,8 +563,8 @@ static void IniLoadSettings(IniFile *ini, const SettingTable &settings_table, co IniGroup *group_def = ini->GetGroup(grpname); for (auto &sd : settings_table) { - const SettingDescBase *sdb = &sd; - const SaveLoad *sld = &sd.save; + const SettingDesc *sdb = &sd; + const SaveLoad *sld = &sd.save; if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue; if (sd.startup != only_startup) continue; @@ -645,8 +645,8 @@ static void IniSaveSettings(IniFile *ini, const SettingTable &settings_table, co void *ptr; for (auto &sd : settings_table) { - const SettingDescBase *sdb = &sd; - const SaveLoad *sld = &sd.save; + const SettingDesc *sdb = &sd; + const SaveLoad *sld = &sd.save; /* If the setting is not saved to the configuration * file, just continue with the next setting */ diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index edde335601..f4570624d7 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -1127,10 +1127,8 @@ bool SettingEntry::UpdateFilterState(SettingFilter &filter, bool force_visible) /* Process the search text filter for this item. */ filter.string.ResetState(); - const SettingDescBase *sdb = sd; - SetDParam(0, STR_EMPTY); - filter.string.AddLine(sdb->str); + filter.string.AddLine(sd->str); filter.string.AddLine(this->GetHelpText()); visible = filter.string.GetState(); @@ -1171,17 +1169,16 @@ static const void *ResolveVariableAddress(const GameSettings *settings_ptr, cons */ void SettingEntry::SetValueDParams(uint first_param, int32 value) const { - const SettingDescBase *sdb = this->setting; - if (sdb->cmd == SDT_BOOLX) { + if (this->setting->cmd == SDT_BOOLX) { SetDParam(first_param++, value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF); } else { - if ((sdb->flags & SGF_MULTISTRING) != 0) { - SetDParam(first_param++, sdb->str_val - sdb->min + value); - } else if ((sdb->flags & SGF_DISPLAY_ABS) != 0) { - SetDParam(first_param++, sdb->str_val + ((value >= 0) ? 1 : 0)); + if ((this->setting->flags & SGF_MULTISTRING) != 0) { + SetDParam(first_param++, this->setting->str_val - this->setting->min + value); + } else if ((this->setting->flags & SGF_DISPLAY_ABS) != 0) { + SetDParam(first_param++, this->setting->str_val + ((value >= 0) ? 1 : 0)); value = abs(value); } else { - SetDParam(first_param++, sdb->str_val + ((value == 0 && (sdb->flags & SGF_0ISDISABLED) != 0) ? 1 : 0)); + SetDParam(first_param++, this->setting->str_val + ((value == 0 && (this->setting->flags & SGF_0ISDISABLED) != 0) ? 1 : 0)); } SetDParam(first_param++, value); } @@ -1198,7 +1195,6 @@ void SettingEntry::SetValueDParams(uint first_param, int32 value) const void SettingEntry::DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const { const SettingDesc *sd = this->setting; - const SettingDescBase *sdb = sd; const void *var = ResolveVariableAddress(settings_ptr, sd); int state = this->flags & SEF_BUTTONS_MASK; @@ -1213,19 +1209,19 @@ void SettingEntry::DrawSetting(GameSettings *settings_ptr, int left, int right, SetDParam(0, highlight ? STR_ORANGE_STRING1_WHITE : STR_ORANGE_STRING1_LTBLUE); int32 value = (int32)ReadValue(var, sd->save.conv); - if (sdb->cmd == SDT_BOOLX) { + if (sd->cmd == SDT_BOOLX) { /* Draw checkbox for boolean-value either on/off */ DrawBoolButton(buttons_left, button_y, value != 0, editable); - } else if ((sdb->flags & SGF_MULTISTRING) != 0) { + } else if ((sd->flags & SGF_MULTISTRING) != 0) { /* Draw [v] button for settings of an enum-type */ DrawDropDownButton(buttons_left, button_y, COLOUR_YELLOW, state != 0, editable); } else { /* Draw [<][>] boxes for settings of an integer-type */ DrawArrowButtons(buttons_left, button_y, COLOUR_YELLOW, state, - editable && value != (sdb->flags & SGF_0ISDISABLED ? 0 : sdb->min), editable && (uint32)value != sdb->max); + editable && value != (sd->flags & SGF_0ISDISABLED ? 0 : sd->min), editable && (uint32)value != sd->max); } this->SetValueDParams(1, value); - DrawString(text_left, text_right, y + (SETTING_HEIGHT - FONT_HEIGHT_NORMAL) / 2, sdb->str, highlight ? TC_WHITE : TC_LIGHT_BLUE); + DrawString(text_left, text_right, y + (SETTING_HEIGHT - FONT_HEIGHT_NORMAL) / 2, sd->str, highlight ? TC_WHITE : TC_LIGHT_BLUE); } /* == SettingsContainer methods == */ @@ -2196,7 +2192,6 @@ struct GameSettingsWindow : Window { /* clicked on the icon on the left side. Either scroller, bool on/off or dropdown */ if (x < SETTING_BUTTON_WIDTH && (sd->flags & SGF_MULTISTRING)) { - const SettingDescBase *sdb = sd; this->SetDisplayedHelpText(pe); if (this->valuedropdown_entry == pe) { @@ -2224,8 +2219,8 @@ struct GameSettingsWindow : Window { this->valuedropdown_entry->SetButtons(SEF_LEFT_DEPRESSED); DropDownList list; - for (int i = sdb->min; i <= (int)sdb->max; i++) { - list.emplace_back(new DropDownListStringItem(sdb->str_val + i - sdb->min, i, false)); + for (int i = sd->min; i <= (int)sd->max; i++) { + list.emplace_back(new DropDownListStringItem(sd->str_val + i - sd->min, i, false)); } ShowDropDownListAt(this, std::move(list), value, -1, wi_rect, COLOUR_ORANGE, true); @@ -2234,10 +2229,9 @@ struct GameSettingsWindow : Window { this->SetDirty(); } else if (x < SETTING_BUTTON_WIDTH) { this->SetDisplayedHelpText(pe); - const SettingDescBase *sdb = sd; int32 oldvalue = value; - switch (sdb->cmd) { + switch (sd->cmd) { case SDT_BOOLX: value ^= 1; break; case SDT_ONEOFMANY: case SDT_NUMX: { @@ -2245,7 +2239,7 @@ struct GameSettingsWindow : Window { * 50-steps you should be able to get from min to max, * unless specified otherwise in the 'interval' variable * of the current setting. */ - uint32 step = (sdb->interval == 0) ? ((sdb->max - sdb->min) / 50) : sdb->interval; + uint32 step = (sd->interval == 0) ? ((sd->max - sd->min) / 50) : sd->interval; if (step == 0) step = 1; /* don't allow too fast scrolling */ @@ -2257,16 +2251,16 @@ struct GameSettingsWindow : Window { /* Increase or decrease the value and clamp it to extremes */ if (x >= SETTING_BUTTON_WIDTH / 2) { value += step; - if (sdb->min < 0) { - assert((int32)sdb->max >= 0); - if (value > (int32)sdb->max) value = (int32)sdb->max; + if (sd->min < 0) { + assert((int32)sd->max >= 0); + if (value > (int32)sd->max) value = (int32)sd->max; } else { - if ((uint32)value > sdb->max) value = (int32)sdb->max; + if ((uint32)value > sd->max) value = (int32)sd->max; } - if (value < sdb->min) value = sdb->min; // skip between "disabled" and minimum + if (value < sd->min) value = sd->min; // skip between "disabled" and minimum } else { value -= step; - if (value < sdb->min) value = (sdb->flags & SGF_0ISDISABLED) ? 0 : sdb->min; + if (value < sd->min) value = (sd->flags & SGF_0ISDISABLED) ? 0 : sd->min; } /* Set up scroller timeout for numeric values */