OpenTTD Source 20250205-master-gfd85ab1e2c
settings_internal.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef SETTINGS_INTERNAL_H
11#define SETTINGS_INTERNAL_H
12
13#include <variant>
14#include "saveload/saveload.h"
15
32
41enum SettingCategory : uint8_t {
42 SC_NONE = 0,
43
44 /* Filters for the list */
45 SC_BASIC_LIST = 1 << 0,
47 SC_EXPERT_LIST = 1 << 2,
48
49 /* Setting classification */
53
54 SC_END,
55};
56
67
68struct IniItem;
69
74 virtual ~SettingDesc() = default;
75
77 bool startup;
79
80 bool IsEditable(bool do_command = false) const;
81 SettingType GetType() const;
82
87 constexpr const std::string &GetName() const
88 {
89 return this->save.name;
90 }
91
96 virtual bool IsIntSetting() const { return false; }
97
102 virtual bool IsStringSetting() const { return false; }
103
104 const struct IntSettingDesc *AsIntSetting() const;
105 const struct StringSettingDesc *AsStringSetting() const;
106
113 virtual std::string FormatValue(const void *object) const = 0;
114
120 virtual void ParseValue(const IniItem *item, void *object) const = 0;
121
131 virtual bool IsSameValue(const IniItem *item, void *object) const = 0;
132
139 virtual bool IsDefaultValue(void *object) const = 0;
140
144 virtual void ResetToDefault(void *object) const = 0;
145};
146
149 typedef StringID GetTitleCallback(const IntSettingDesc &sd);
150 typedef StringID GetHelpCallback(const IntSettingDesc &sd);
151 typedef void SetValueDParamsCallback(const IntSettingDesc &sd, uint first_param, int32_t value);
152 typedef int32_t GetDefaultValueCallback(const IntSettingDesc &sd);
153 typedef std::tuple<int32_t, uint32_t> GetRangeCallback(const IntSettingDesc &sd);
154
163 typedef bool PreChangeCheck(int32_t &value);
168 typedef void PostChangeCallback(int32_t value);
169
170 template <ConvertibleThroughBaseOrTo<int32_t> Tdef, ConvertibleThroughBaseOrTo<int32_t> Tmin, ConvertibleThroughBaseOrTo<uint32_t> Tmax, ConvertibleThroughBaseOrTo<int32_t> Tinterval>
172 Tmin min, Tmax max, Tinterval interval, StringID str, StringID str_help, StringID str_val,
174 GetTitleCallback get_title_cb, GetHelpCallback get_help_cb, SetValueDParamsCallback set_value_dparams_cb,
175 GetDefaultValueCallback get_def_cb, GetRangeCallback get_range_cb) :
179 get_title_cb(get_title_cb), get_help_cb(get_help_cb), set_value_dparams_cb(set_value_dparams_cb),
180 get_def_cb(get_def_cb), get_range_cb(get_range_cb) {
181 if constexpr (ConvertibleThroughBase<Tdef>) {
182 this->def = def.base();
183 } else {
184 this->def = def;
185 }
186
187 if constexpr (ConvertibleThroughBase<Tmin>) {
188 this->min = min.base();
189 } else {
190 this->min = min;
191 }
192
193 if constexpr (ConvertibleThroughBase<Tmax>) {
194 this->max = max.base();
195 } else {
196 this->max = max;
197 }
198
199 if constexpr (ConvertibleThroughBase<Tinterval>) {
200 this->interval = interval.base();
201 } else {
202 this->interval = interval;
203 }
204 }
205
206 int32_t def;
207 int32_t min;
208 uint32_t max;
209 int32_t interval;
216 GetTitleCallback *get_title_cb;
217 GetHelpCallback *get_help_cb;
218 SetValueDParamsCallback *set_value_dparams_cb;
219 GetDefaultValueCallback *get_def_cb;
220 GetRangeCallback *get_range_cb;
221
222 StringID GetTitle() const;
223 StringID GetHelp() const;
224 void SetValueDParams(uint first_param, int32_t value) const;
225 int32_t GetDefaultValue() const;
226 std::tuple<int32_t, uint32_t> GetRange() const;
227
232 virtual bool IsBoolSetting() const { return false; }
233 bool IsIntSetting() const override { return true; }
234
235 void ChangeValue(const void *object, int32_t newvalue) const;
236 void MakeValueValidAndWrite(const void *object, int32_t value) const;
237
238 virtual size_t ParseValue(const char *str) const;
239 std::string FormatValue(const void *object) const override;
240 void ParseValue(const IniItem *item, void *object) const override;
241 bool IsSameValue(const IniItem *item, void *object) const override;
242 bool IsDefaultValue(void *object) const override;
243 void ResetToDefault(void *object) const override;
244 int32_t Read(const void *object) const;
245
246private:
247 void MakeValueValid(int32_t &value) const;
248 void Write(const void *object, int32_t value) const;
249};
250
256 GetTitleCallback get_title_cb, GetHelpCallback get_help_cb, SetValueDParamsCallback set_value_dparams_cb,
257 GetDefaultValueCallback get_def_cb) :
258 IntSettingDesc(save, flags, startup, def ? 1 : 0, 0, 1, 0, str, str_help, str_val, cat,
259 pre_check, post_callback, get_title_cb, get_help_cb, set_value_dparams_cb, get_def_cb, nullptr) {}
260
261 static std::optional<bool> ParseSingleValue(const char *str);
262
263 bool IsBoolSetting() const override { return true; }
264 size_t ParseValue(const char *str) const override;
265 std::string FormatValue(const void *object) const override;
266};
267
270 typedef size_t OnConvert(const char *value);
271
275 GetTitleCallback get_title_cb, GetHelpCallback get_help_cb, SetValueDParamsCallback set_value_dparams_cb,
276 GetDefaultValueCallback get_def_cb, std::initializer_list<const char *> many, OnConvert *many_cnvt) :
278 pre_check, post_callback, get_title_cb, get_help_cb, set_value_dparams_cb, get_def_cb, nullptr), many_cnvt(many_cnvt)
279 {
280 for (auto one : many) this->many.push_back(one);
281 }
282
283 std::vector<std::string> many;
285
286 static size_t ParseSingleValue(const char *str, size_t len, const std::vector<std::string> &many);
287 std::string FormatSingleValue(uint id) const;
288
289 size_t ParseValue(const char *str) const override;
290 std::string FormatValue(const void *object) const override;
291};
292
298 GetTitleCallback get_title_cb, GetHelpCallback get_help_cb, SetValueDParamsCallback set_value_dparams_cb,
299 GetDefaultValueCallback get_def_cb, std::initializer_list<const char *> many, OnConvert *many_cnvt) :
300 OneOfManySettingDesc(save, flags, startup, def, (1 << many.size()) - 1, str, str_help,
301 str_val, cat, pre_check, post_callback, get_title_cb, get_help_cb, set_value_dparams_cb, get_def_cb, many, many_cnvt) {}
302
303 size_t ParseValue(const char *str) const override;
304 std::string FormatValue(const void *object) const override;
305};
306
317 typedef bool PreChangeCheck(std::string &value);
322 typedef void PostChangeCallback(const std::string &value);
323
324 StringSettingDesc(const SaveLoad &save, SettingFlags flags, bool startup, const char *def,
326 SettingDesc(save, flags, startup), def(def == nullptr ? "" : def), max_length(max_length),
328
329 std::string def;
330 uint32_t max_length;
333
334 bool IsStringSetting() const override { return true; }
335 void ChangeValue(const void *object, std::string &newval) const;
336
337 std::string FormatValue(const void *object) const override;
338 void ParseValue(const IniItem *item, void *object) const override;
339 bool IsSameValue(const IniItem *item, void *object) const override;
340 bool IsDefaultValue(void *object) const override;
341 void ResetToDefault(void *object) const override;
342 const std::string &Read(const void *object) const;
343
344private:
345 void MakeValueValid(std::string &str) const;
346 void Write(const void *object, const std::string &str) const;
347};
348
351 ListSettingDesc(const SaveLoad &save, SettingFlags flags, bool startup, const char *def) :
353
354 const char *def;
355
356 std::string FormatValue(const void *object) const override;
357 void ParseValue(const IniItem *item, void *object) const override;
358 bool IsSameValue(const IniItem *item, void *object) const override;
359 bool IsDefaultValue(void *object) const override;
360 void ResetToDefault(void *object) const override;
361};
362
367
368 std::string FormatValue(const void *) const override { NOT_REACHED(); }
369 void ParseValue(const IniItem *, void *) const override { NOT_REACHED(); }
370 bool IsSameValue(const IniItem *, void *) const override { NOT_REACHED(); }
371 bool IsDefaultValue(void *) const override { NOT_REACHED(); }
372 void ResetToDefault(void *) const override { NOT_REACHED(); }
373};
374
375typedef std::variant<IntSettingDesc, BoolSettingDesc, OneOfManySettingDesc, ManyOfManySettingDesc, StringSettingDesc, ListSettingDesc, NullSettingDesc> SettingVariant;
376
382static constexpr const SettingDesc *GetSettingDesc(const SettingVariant &desc)
383{
384 return std::visit([](auto&& arg) -> const SettingDesc * { return &arg; }, desc);
385}
386
387typedef std::span<const SettingVariant> SettingTable;
388
389const SettingDesc *GetSettingFromName(const std::string_view name);
390void GetSaveLoadFromSettingTable(SettingTable settings, std::vector<SaveLoad> &saveloads);
391bool SetSettingValue(const IntSettingDesc *sd, int32_t value, bool force_newgame = false);
392bool SetSettingValue(const StringSettingDesc *sd, const std::string value, bool force_newgame = false);
393
394std::vector<const SettingDesc *> GetFilteredSettingCollection(std::function<bool(const SettingDesc &desc)> func);
395
396#endif /* SETTINGS_INTERNAL_H */
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...
fluid_settings_t * settings
FluidSynth settings handle.
Functions/types related to saving and loading games.
const SettingDesc * GetSettingFromName(const std::string_view name)
Given a name of any setting, return any setting description of it.
SettingFlag
@ NotInSave
Do not save with savegame, basically client-based.
@ GuiCurrency
The number represents money, so when reading value multiply by exchange rate.
@ Sandbox
This setting is a sandbox setting.
@ SceneditOnly
This setting can only be changed in the scenario editor.
@ PerCompany
This setting can be different for each company (saved in company struct).
@ NewgameOnly
This setting cannot be changed in a game.
@ GuiZeroIsSpecial
A value of zero is possible and has a custom string (the one after "strval").
@ NoNetwork
This setting does not apply to network games; it may not be changed during the game.
@ NotInConfig
Do not save to config file.
@ GuiDropdown
The value represents a limited number of string-options (internally integer) presented as dropdown.
@ SceneditToo
This setting can be changed in the scenario editor (only makes sense when SettingFlag::NewgameOnly is...
@ NoNetworkSync
Do not synchronize over network (but it is saved if SettingFlag::NotInSave is not set).
@ NetworkOnly
This setting only applies to network games.
std::vector< const SettingDesc * > GetFilteredSettingCollection(std::function< bool(const SettingDesc &desc)> func)
Get a collection of settings matching a custom filter.
SettingType
Type of settings for filtering.
@ ST_CLIENT
Client setting.
@ ST_ALL
Used in setting filter to match all types.
@ ST_GAME
Game setting.
@ ST_COMPANY
Company setting.
void GetSaveLoadFromSettingTable(SettingTable settings, std::vector< SaveLoad > &saveloads)
Get the SaveLoad for all settings in the settings table.
bool SetSettingValue(const IntSettingDesc *sd, int32_t value, bool force_newgame=false)
Top function to save the new value of an element of the Settings struct.
SettingCategory
A SettingCategory defines a grouping of the settings.
@ SC_ADVANCED
Advanced settings are part of advanced and expert list.
@ SC_EXPERT
Expert settings can only be seen in the expert list.
@ SC_ADVANCED_LIST
Settings displayed in the list of advanced settings.
@ SC_EXPERT_LIST
Settings displayed in the list of expert settings.
@ SC_BASIC_LIST
Settings displayed in the list of basic settings.
@ SC_BASIC
Basic settings are part of all lists.
static constexpr const SettingDesc * GetSettingDesc(const SettingVariant &desc)
Helper to convert the type of the iterated settings description to a pointer to it.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Boolean setting.
std::string FormatValue(const void *object) const override
Format the value of the setting associated with this object.
Definition settings.cpp:756
bool IsBoolSetting() const override
Check whether this setting is a boolean type setting.
static std::optional< bool > ParseSingleValue(const char *str)
Find whether a string was a boolean true or a boolean false.
Definition settings.cpp:205
size_t ParseValue(const char *str) const override
Convert a string representation (external) of an integer-like setting to an integer.
Definition settings.cpp:424
A single "line" in an ini file.
Definition ini_type.h:23
Base integer type, including boolean, settings.
void SetValueDParams(uint first_param, int32_t value) const
Set the DParams for drawing the value of the setting.
Definition settings.cpp:460
StringID str_help
(Translated) string with help text; gui only.
StringID str_val
(Translated) first string describing the value.
void MakeValueValid(int32_t &value) const
Make the value valid given the limitations of this setting.
Definition settings.cpp:516
void ResetToDefault(void *object) const override
Reset the setting to its default value.
Definition settings.cpp:775
std::string FormatValue(const void *object) const override
Format the value of the setting associated with this object.
Definition settings.cpp:745
int32_t def
default value given when none is present
SettingCategory cat
assigned categories of the setting
bool IsIntSetting() const override
Check whether this setting is an integer type setting.
std::tuple< int32_t, uint32_t > GetRange() const
Get the min/max range for the setting.
Definition settings.cpp:490
StringID GetTitle() const
Get the title of the setting.
Definition settings.cpp:441
void ChangeValue(const void *object, int32_t newvalue) const
Handle changing a value.
uint32_t max
maximum values
void PostChangeCallback(int32_t value)
A callback to denote that a setting has been changed.
GetDefaultValueCallback * get_def_cb
Callback to set the correct default value.
bool IsSameValue(const IniItem *item, void *object) const override
Check whether the value in the Ini item is the same as is saved in this setting in the object.
Definition settings.cpp:762
int32_t min
minimum values
PreChangeCheck * pre_check
Callback to check for the validity of the setting.
int32_t GetDefaultValue() const
Get the default value of the setting.
Definition settings.cpp:481
bool PreChangeCheck(int32_t &value)
A check to be performed before the setting gets changed.
void Write(const void *object, int32_t value) const
Set the value of a setting.
Definition settings.cpp:570
StringID GetHelp() const
Get the help text of the setting.
Definition settings.cpp:450
virtual bool IsBoolSetting() const
Check whether this setting is a boolean type setting.
PostChangeCallback * post_callback
Callback when the setting has been changed.
StringID str
(translated) string with descriptive text; gui and console
void MakeValueValidAndWrite(const void *object, int32_t value) const
Make the value valid and then write it to the setting.
Definition settings.cpp:501
virtual size_t ParseValue(const char *str) const
Convert a string representation (external) of an integer-like setting to an integer.
Definition settings.cpp:379
int32_t Read(const void *object) const
Read the integer from the the actual setting.
Definition settings.cpp:581
int32_t interval
the interval to use between settings in the 'settings' window. If interval is '0' the interval is dyn...
bool IsDefaultValue(void *object) const override
Check whether the value is the same as the default value.
Definition settings.cpp:769
List/array settings.
void ParseValue(const IniItem *item, void *object) const override
Parse/read the value from the Ini item into the setting associated with this object.
Definition settings.cpp:688
void ResetToDefault(void *object) const override
Reset the setting to its default value.
Definition settings.cpp:829
bool IsSameValue(const IniItem *item, void *object) const override
Check whether the value in the Ini item is the same as is saved in this setting in the object.
Definition settings.cpp:817
std::string FormatValue(const void *object) const override
Convert an integer-array (intlist) to a string representation.
Definition settings.cpp:322
const char * def
default value given when none is present
bool IsDefaultValue(void *object) const override
Check whether the value is the same as the default value.
Definition settings.cpp:823
Many of many setting.
std::string FormatValue(const void *object) const override
Format the value of the setting associated with this object.
Definition settings.cpp:359
size_t ParseValue(const char *str) const override
Convert a string representation (external) of an integer-like setting to an integer.
Definition settings.cpp:413
Placeholder for settings that have been removed, but might still linger in the savegame.
bool IsSameValue(const IniItem *, void *) const override
Check whether the value in the Ini item is the same as is saved in this setting in the object.
void ParseValue(const IniItem *, void *) const override
Parse/read the value from the Ini item into the setting associated with this object.
std::string FormatValue(const void *) const override
Format the value of the setting associated with this object.
bool IsDefaultValue(void *) const override
Check whether the value is the same as the default value.
void ResetToDefault(void *) const override
Reset the setting to its default value.
One of many setting.
OnConvert * many_cnvt
callback procedure when loading value mechanism fails
size_t ParseValue(const char *str) const override
Convert a string representation (external) of an integer-like setting to an integer.
Definition settings.cpp:398
static size_t ParseSingleValue(const char *str, size_t len, const std::vector< std::string > &many)
Find the index value of a ONEofMANY type in a string separated by |.
Definition settings.cpp:185
std::string FormatValue(const void *object) const override
Format the value of the setting associated with this object.
Definition settings.cpp:353
size_t OnConvert(const char *value)
callback prototype for conversion error
std::vector< std::string > many
possible values for this type
SaveLoad type struct.
Definition saveload.h:718
std::string name
Name of this field (optional, used for tables).
Definition saveload.h:719
Properties of config file settings.
virtual void ParseValue(const IniItem *item, void *object) const =0
Parse/read the value from the Ini item into the setting associated with this object.
bool IsEditable(bool do_command=false) const
Check whether the setting is editable in the current gamemode.
Definition settings.cpp:903
SettingFlags flags
Handles how a setting would show up in the GUI (text/currency, etc.).
virtual bool IsStringSetting() const
Check whether this setting is an string type setting.
virtual void ResetToDefault(void *object) const =0
Reset the setting to its default value.
SettingType GetType() const
Return the type of the setting.
Definition settings.cpp:920
constexpr const std::string & GetName() const
Get the name of this setting.
bool startup
Setting has to be loaded directly at startup?.
virtual std::string FormatValue(const void *object) const =0
Format the value of the setting associated with this object.
const struct StringSettingDesc * AsStringSetting() const
Get the setting description of this setting as a string setting.
Definition settings.cpp:940
virtual bool IsSameValue(const IniItem *item, void *object) const =0
Check whether the value in the Ini item is the same as is saved in this setting in the object.
virtual bool IsDefaultValue(void *object) const =0
Check whether the value is the same as the default value.
SaveLoad save
Internal structure (going to savegame, parts to config).
virtual bool IsIntSetting() const
Check whether this setting is an integer type setting.
const struct IntSettingDesc * AsIntSetting() const
Get the setting description of this setting as an integer setting.
Definition settings.cpp:930
String settings.
const std::string & Read(const void *object) const
Read the string from the the actual setting.
Definition settings.cpp:620
void PostChangeCallback(const std::string &value)
A callback to denote that a setting has been changed.
std::string def
Default value given when none is present.
uint32_t max_length
Maximum length of the string, 0 means no maximum length.
PreChangeCheck * pre_check
Callback to check for the validity of the setting.
void MakeValueValid(std::string &str) const
Make the value valid given the limitations of this setting.
Definition settings.cpp:594
bool IsSameValue(const IniItem *item, void *object) const override
Check whether the value in the Ini item is the same as is saved in this setting in the object.
Definition settings.cpp:796
bool IsDefaultValue(void *object) const override
Check whether the value is the same as the default value.
Definition settings.cpp:806
PostChangeCallback * post_callback
Callback when the setting has been changed.
void ResetToDefault(void *object) const override
Reset the setting to its default value.
Definition settings.cpp:812
void Write(const void *object, const std::string &str) const
Write a string to the actual setting.
Definition settings.cpp:610
void ChangeValue(const void *object, std::string &newval) const
Handle changing a string value.
bool PreChangeCheck(std::string &value)
A check to be performed before the setting gets changed.
std::string FormatValue(const void *object) const override
Format the value of the setting associated with this object.
Definition settings.cpp:780
bool IsStringSetting() const override
Check whether this setting is an string type setting.
void ParseValue(const IniItem *item, void *object) const override
Parse/read the value from the Ini item into the setting associated with this object.
Definition settings.cpp:681