Codechange: let SettingDesc extend SettingDescBase

This commit is contained in:
rubidium42 2021-05-23 09:51:33 +02:00 committed by rubidium42
parent ac99a38175
commit 425d50372f
5 changed files with 62 additions and 63 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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<const std::string *>(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<const std::string *>(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");

View File

@ -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();

View File

@ -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;