summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-05-18 20:57:44 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-05-27 18:49:43 +0200
commit8ffb4122df903393bceca49a25ac81cc42cca3ff (patch)
tree0e1d390cc284f83e8cde908f33e6d901e2533d0f /src
parentdb54e208256958f721bf74bf058b45039be5b488 (diff)
downloadopenttd-8ffb4122df903393bceca49a25ac81cc42cca3ff.tar.xz
Codechange: just pass the SettingDesc to SetSettingValue and remove distinction between (non)company
Diffstat (limited to 'src')
-rw-r--r--src/network/network_gui.cpp8
-rw-r--r--src/news_gui.cpp3
-rw-r--r--src/script/api/script_gamesettings.cpp11
-rw-r--r--src/settings.cpp82
-rw-r--r--src/settings_gui.cpp25
-rw-r--r--src/settings_internal.h8
6 files changed, 54 insertions, 83 deletions
diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp
index f0c28530a..e37843db8 100644
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -2199,9 +2199,7 @@ public:
case WID_CL_SERVER_NAME_EDIT: {
if (!_network_server) break;
- uint index;
- GetSettingFromName("network.server_name", &index);
- SetSettingValue(index, StrEmpty(str) ? "Unnamed Server" : str);
+ SetSettingValue(GetSettingFromName("network.server_name"), StrEmpty(str) ? "Unnamed Server" : str);
this->InvalidateData();
break;
}
@@ -2210,9 +2208,7 @@ public:
std::string client_name(str);
if (!NetworkValidateClientName(client_name)) break;
- uint index;
- GetSettingFromName("network.client_name", &index);
- SetSettingValue(index, client_name.c_str());
+ SetSettingValue(GetSettingFromName("network.client_name"), client_name.c_str());
this->InvalidateData();
break;
}
diff --git a/src/news_gui.cpp b/src/news_gui.cpp
index e3fb60033..58d62dde8 100644
--- a/src/news_gui.cpp
+++ b/src/news_gui.cpp
@@ -253,8 +253,7 @@ static_assert(lengthof(_news_type_data) == NT_END);
*/
NewsDisplay NewsTypeData::GetDisplay() const
{
- uint index;
- const SettingDesc *sd = GetSettingFromName(this->name, &index);
+ const SettingDesc *sd = GetSettingFromName(this->name);
assert(sd != nullptr);
void *ptr = GetVariableAddress(nullptr, &sd->save);
return (NewsDisplay)ReadValue(ptr, sd->save.conv);
diff --git a/src/script/api/script_gamesettings.cpp b/src/script/api/script_gamesettings.cpp
index 2717d4ddd..12435d253 100644
--- a/src/script/api/script_gamesettings.cpp
+++ b/src/script/api/script_gamesettings.cpp
@@ -17,8 +17,7 @@
/* static */ bool ScriptGameSettings::IsValid(const char *setting)
{
- uint i;
- const SettingDesc *sd = GetSettingFromName(setting, &i);
+ const SettingDesc *sd = GetSettingFromName(setting);
return sd != nullptr && sd->desc.cmd != SDT_STDSTRING;
}
@@ -26,8 +25,7 @@
{
if (!IsValid(setting)) return -1;
- uint index;
- const SettingDesc *sd = GetSettingFromName(setting, &index);
+ const SettingDesc *sd = GetSettingFromName(setting);
void *ptr = GetVariableAddress(&_settings_game, &sd->save);
if (sd->desc.cmd == SDT_BOOLX) return *(bool*)ptr;
@@ -39,13 +37,12 @@
{
if (!IsValid(setting)) return false;
- uint index;
- const SettingDesc *sd = GetSettingFromName(setting, &index);
+ 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;
- return ScriptObject::DoCommand(0, index, value, CMD_CHANGE_SETTING);
+ return ScriptObject::DoCommand(0, GetSettingIndex(sd), value, CMD_CHANGE_SETTING);
}
/* static */ bool ScriptGameSettings::IsDisabledVehicleType(ScriptVehicle::VehicleType vehicle_type)
diff --git a/src/settings.cpp b/src/settings.cpp
index 6882179eb..5d4e92b35 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -1949,15 +1949,36 @@ CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32
}
/**
+ * Get the index of the 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_SETTING.
+ */
+uint GetSettingIndex(const SettingDesc *sd)
+{
+ assert((sd->desc.flags & SGF_PER_COMPANY) == 0);
+ return sd - _settings;
+}
+
+/**
* Top function to save the new value of an element of the Settings struct
* @param index offset in the SettingDesc array of the Settings struct which
* identifies the setting member we want to change
* @param value new value of the setting
* @param force_newgame force the newgame settings
*/
-bool SetSettingValue(uint index, int32 value, bool force_newgame)
+bool SetSettingValue(const SettingDesc *sd, int32 value, bool force_newgame)
{
- const SettingDesc *sd = &_settings[index];
+ if ((sd->desc.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));
+ return true;
+ }
+
/* If an item is company-based, we do not send it over the network
* (if any) to change. Also *hack*hack* we update the _newgame version
* of settings because changing a company-based setting in a game also
@@ -1988,30 +2009,12 @@ bool SetSettingValue(uint index, int32 value, bool force_newgame)
/* send non-company-based settings over the network */
if (!_networking || (_networking && _network_server)) {
- return DoCommandP(0, index, value, CMD_CHANGE_SETTING);
+ return DoCommandP(0, GetSettingIndex(sd), value, CMD_CHANGE_SETTING);
}
return false;
}
/**
- * Top function to save the new value of an element of the Settings struct
- * @param index offset in the SettingDesc array of the CompanySettings struct
- * which identifies the setting member we want to change
- * @param value new value of the setting
- */
-void SetCompanySetting(uint index, int32 value)
-{
- const SettingDesc *sd = &_company_settings[index];
- if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
- DoCommandP(0, index, value, CMD_CHANGE_COMPANY_SETTING);
- } else {
- 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));
- }
-}
-
-/**
* Set the company settings for a new company to their default values.
*/
void SetDefaultCompanySettings(CompanyID cid)
@@ -2047,23 +2050,20 @@ void SyncCompanySettings()
*/
uint GetCompanySettingIndex(const char *name)
{
- uint i;
- const SettingDesc *sd = GetSettingFromName(name, &i);
- (void)sd; // Unused without asserts
+ const SettingDesc *sd = GetSettingFromName(name);
assert(sd != nullptr && (sd->desc.flags & SGF_PER_COMPANY) != 0);
- return i;
+ return sd - _company_settings;
}
/**
* Set a setting value with a string.
- * @param index the settings index.
+ * @param sd the setting to change.
* @param value the value to write
* @param force_newgame force the newgame settings
* @note Strings WILL NOT be synced over the network
*/
-bool SetSettingValue(uint index, const char *value, bool force_newgame)
+bool SetSettingValue(const SettingDesc *sd, const char *value, bool force_newgame)
{
- const SettingDesc *sd = &_settings[index];
assert(sd->save.conv & SLF_NO_NETWORK_SYNC);
if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ && strcmp(value, "(null)") == 0) {
@@ -2085,18 +2085,16 @@ bool SetSettingValue(uint index, const char *value, bool force_newgame)
* @return Pointer to the setting description of setting \a name if it can be found,
* \c nullptr indicates failure to obtain the description
*/
-const SettingDesc *GetSettingFromName(const char *name, uint *i)
+const SettingDesc *GetSettingFromName(const char *name)
{
- const SettingDesc *sd;
-
/* First check all full names */
- for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
+ 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;
}
/* Then check the shortcut variant of the name. */
- for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
+ 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, '.');
if (short_name != nullptr) {
@@ -2107,7 +2105,7 @@ const SettingDesc *GetSettingFromName(const char *name, uint *i)
if (strncmp(name, "company.", 8) == 0) name += 8;
/* And finally the company-based settings */
- for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) {
+ 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;
}
@@ -2119,8 +2117,7 @@ const SettingDesc *GetSettingFromName(const char *name, uint *i)
* and besides, it is also better to keep stuff like this at the same place */
void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
{
- uint index;
- const SettingDesc *sd = GetSettingFromName(name, &index);
+ const SettingDesc *sd = GetSettingFromName(name);
if (sd == nullptr) {
IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
@@ -2129,7 +2126,7 @@ void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
bool success;
if (sd->desc.cmd == SDT_STDSTRING) {
- success = SetSettingValue(index, value, force_newgame);
+ success = SetSettingValue(sd, value, force_newgame);
} else {
uint32 val;
extern bool GetArgumentInteger(uint32 *value, const char *arg);
@@ -2139,7 +2136,7 @@ void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
return;
}
- success = SetSettingValue(index, val, force_newgame);
+ success = SetSettingValue(sd, val, force_newgame);
}
if (!success) {
@@ -2153,11 +2150,9 @@ void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
void IConsoleSetSetting(const char *name, int value)
{
- uint index;
- const SettingDesc *sd = GetSettingFromName(name, &index);
- (void)sd; // Unused without asserts
+ const SettingDesc *sd = GetSettingFromName(name);
assert(sd != nullptr);
- SetSettingValue(index, value);
+ SetSettingValue(sd, value);
}
/**
@@ -2168,8 +2163,7 @@ void IConsoleSetSetting(const char *name, int value)
void IConsoleGetSetting(const char *name, bool force_newgame)
{
char value[20];
- uint index;
- const SettingDesc *sd = GetSettingFromName(name, &index);
+ const SettingDesc *sd = GetSettingFromName(name);
const void *ptr;
if (sd == nullptr) {
diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp
index 664cfbd21..b069d2706 100644
--- a/src/settings_gui.cpp
+++ b/src/settings_gui.cpp
@@ -826,7 +826,6 @@ protected:
struct SettingEntry : BaseSettingEntry {
const char *name; ///< Name of the setting
const SettingDesc *setting; ///< Setting description of the setting
- uint index; ///< Index of the setting in the settings table
SettingEntry(const char *name);
@@ -1021,7 +1020,6 @@ SettingEntry::SettingEntry(const char *name)
{
this->name = name;
this->setting = nullptr;
- this->index = 0;
}
/**
@@ -1031,7 +1029,7 @@ SettingEntry::SettingEntry(const char *name)
void SettingEntry::Init(byte level)
{
BaseSettingEntry::Init(level);
- this->setting = GetSettingFromName(this->name, &this->index);
+ this->setting = GetSettingFromName(this->name);
assert(this->setting != nullptr);
}
@@ -1039,7 +1037,7 @@ void SettingEntry::Init(byte level)
void SettingEntry::ResetAll()
{
int32 default_value = ReadValue(&this->setting->desc.def, this->setting->save.conv);
- SetSettingValue(this->index, default_value);
+ SetSettingValue(this->setting, default_value);
}
/**
@@ -2288,11 +2286,7 @@ struct GameSettingsWindow : Window {
}
if (value != oldvalue) {
- if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
- SetCompanySetting(pe->index, value);
- } else {
- SetSettingValue(pe->index, value);
- }
+ SetSettingValue(sd, value);
this->SetDirty();
}
} else {
@@ -2340,11 +2334,7 @@ struct GameSettingsWindow : Window {
value = (int32)(size_t)sd->desc.def;
}
- if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
- SetCompanySetting(this->valuewindow_entry->index, value);
- } else {
- SetSettingValue(this->valuewindow_entry->index, value);
- }
+ SetSettingValue(this->valuewindow_entry->setting, value);
this->SetDirty();
}
@@ -2380,12 +2370,7 @@ struct GameSettingsWindow : Window {
const SettingDesc *sd = this->valuedropdown_entry->setting;
assert(sd->desc.flags & SGF_MULTISTRING);
- if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
- SetCompanySetting(this->valuedropdown_entry->index, index);
- } else {
- SetSettingValue(this->valuedropdown_entry->index, index);
- }
-
+ SetSettingValue(sd, index);
this->SetDirty();
}
break;
diff --git a/src/settings_internal.h b/src/settings_internal.h
index d5f237f3c..225bb7195 100644
--- a/src/settings_internal.h
+++ b/src/settings_internal.h
@@ -123,9 +123,9 @@ struct SettingDesc {
* offset in a certain struct */
typedef SettingDesc SettingDescGlobVarList;
-const SettingDesc *GetSettingFromName(const char *name, uint *i);
-bool SetSettingValue(uint index, int32 value, bool force_newgame = false);
-bool SetSettingValue(uint index, const char *value, bool force_newgame = false);
-void SetCompanySetting(uint index, int32 value);
+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);
+uint GetSettingIndex(const SettingDesc *sd);
#endif /* SETTINGS_INTERNAL_H */