OpenTTD Source  20241120-master-g6d3adc6169
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 
16 enum SettingFlag : uint16_t {
17  SF_NONE = 0,
19  SF_GUI_DROPDOWN = 1 << 2,
20  SF_GUI_CURRENCY = 1 << 3,
21  SF_NETWORK_ONLY = 1 << 4,
22  SF_NO_NETWORK = 1 << 5,
23  SF_NEWGAME_ONLY = 1 << 6,
24  SF_SCENEDIT_TOO = 1 << 7,
25  SF_SCENEDIT_ONLY = 1 << 8,
26  SF_PER_COMPANY = 1 << 9,
27  SF_NOT_IN_SAVE = 1 << 10,
28  SF_NOT_IN_CONFIG = 1 << 11,
29  SF_NO_NETWORK_SYNC = 1 << 12,
30 };
32 
33 
42  SC_NONE = 0,
43 
44  /* Filters for the list */
45  SC_BASIC_LIST = 1 << 0,
46  SC_ADVANCED_LIST = 1 << 1,
47  SC_EXPERT_LIST = 1 << 2,
48 
49  /* Setting classification */
53 
54  SC_END,
55 };
56 
64 
66 };
67 
68 struct IniItem;
69 
71 struct SettingDesc {
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 
161  typedef bool PreChangeCheck(int32_t &value);
166  typedef void PostChangeCallback(int32_t value);
172  typedef int32_t GetDefaultValueCallback();
173 
174  template <
175  typename Tdef,
176  typename Tmin,
177  typename Tmax,
178  typename Tinterval,
179  std::enable_if_t<std::disjunction_v<std::is_convertible<Tdef, int32_t>, std::is_base_of<StrongTypedefBase, Tdef>>, int> = 0,
180  std::enable_if_t<std::disjunction_v<std::is_convertible<Tmin, int32_t>, std::is_base_of<StrongTypedefBase, Tmin>>, int> = 0,
181  std::enable_if_t<std::disjunction_v<std::is_convertible<Tmax, uint32_t>, std::is_base_of<StrongTypedefBase, Tmax>>, int> = 0,
182  std::enable_if_t<std::disjunction_v<std::is_convertible<Tinterval, int32_t>, std::is_base_of<StrongTypedefBase, Tinterval>>, int> = 0
183  >
185  Tmin min, Tmax max, Tinterval interval, StringID str, StringID str_help, StringID str_val,
187  GetTitleCallback get_title_cb, GetHelpCallback get_help_cb, SetValueDParamsCallback set_value_dparams_cb,
192  get_title_cb(get_title_cb), get_help_cb(get_help_cb), set_value_dparams_cb(set_value_dparams_cb),
194  if constexpr (std::is_base_of_v<StrongTypedefBase, Tdef>) {
195  this->def = def.base();
196  } else {
197  this->def = def;
198  }
199 
200  if constexpr (std::is_base_of_v<StrongTypedefBase, Tmin>) {
201  this->min = min.base();
202  } else {
203  this->min = min;
204  }
205 
206  if constexpr (std::is_base_of_v<StrongTypedefBase, Tmax>) {
207  this->max = max.base();
208  } else {
209  this->max = max;
210  }
211 
212  if constexpr (std::is_base_of_v<StrongTypedefBase, Tinterval>) {
213  this->interval = interval.base();
214  } else {
215  this->interval = interval;
216  }
217  }
218 
219  int32_t def;
220  int32_t min;
221  uint32_t max;
222  int32_t interval;
229  GetTitleCallback *get_title_cb;
230  GetHelpCallback *get_help_cb;
231  SetValueDParamsCallback *set_value_dparams_cb;
233 
234  StringID GetTitle() const;
235  StringID GetHelp() const;
236  void SetValueDParams(uint first_param, int32_t value) const;
237 
242  virtual bool IsBoolSetting() const { return false; }
243  bool IsIntSetting() const override { return true; }
244 
245  void ChangeValue(const void *object, int32_t newvalue) const;
246  void MakeValueValidAndWrite(const void *object, int32_t value) const;
247 
248  virtual size_t ParseValue(const char *str) const;
249  std::string FormatValue(const void *object) const override;
250  void ParseValue(const IniItem *item, void *object) const override;
251  bool IsSameValue(const IniItem *item, void *object) const override;
252  bool IsDefaultValue(void *object) const override;
253  void ResetToDefault(void *object) const override;
254  int32_t Read(const void *object) const;
255 
256 private:
257  void MakeValueValid(int32_t &value) const;
258  void Write(const void *object, int32_t value) const;
259 };
260 
266  GetTitleCallback get_title_cb, GetHelpCallback get_help_cb, SetValueDParamsCallback set_value_dparams_cb,
268  IntSettingDesc(save, flags, startup, def ? 1 : 0, 0, 1, 0, str, str_help, str_val, cat,
269  pre_check, post_callback, get_title_cb, get_help_cb, set_value_dparams_cb, get_def_cb) {}
270 
271  static std::optional<bool> ParseSingleValue(const char *str);
272 
273  bool IsBoolSetting() const override { return true; }
274  size_t ParseValue(const char *str) const override;
275  std::string FormatValue(const void *object) const override;
276 };
277 
280  typedef size_t OnConvert(const char *value);
281 
285  GetTitleCallback get_title_cb, GetHelpCallback get_help_cb, SetValueDParamsCallback set_value_dparams_cb,
286  GetDefaultValueCallback get_def_cb, std::initializer_list<const char *> many, OnConvert *many_cnvt) :
288  pre_check, post_callback, get_title_cb, get_help_cb, set_value_dparams_cb, get_def_cb), many_cnvt(many_cnvt)
289  {
290  for (auto one : many) this->many.push_back(one);
291  }
292 
293  std::vector<std::string> many;
295 
296  static size_t ParseSingleValue(const char *str, size_t len, const std::vector<std::string> &many);
297  std::string FormatSingleValue(uint id) const;
298 
299  size_t ParseValue(const char *str) const override;
300  std::string FormatValue(const void *object) const override;
301 };
302 
308  GetTitleCallback get_title_cb, GetHelpCallback get_help_cb, SetValueDParamsCallback set_value_dparams_cb,
309  GetDefaultValueCallback get_def_cb, std::initializer_list<const char *> many, OnConvert *many_cnvt) :
310  OneOfManySettingDesc(save, flags, startup, def, (1 << many.size()) - 1, str, str_help,
311  str_val, cat, pre_check, post_callback, get_title_cb, get_help_cb, set_value_dparams_cb, get_def_cb, many, many_cnvt) {}
312 
313  size_t ParseValue(const char *str) const override;
314  std::string FormatValue(const void *object) const override;
315 };
316 
327  typedef bool PreChangeCheck(std::string &value);
332  typedef void PostChangeCallback(const std::string &value);
333 
334  StringSettingDesc(const SaveLoad &save, SettingFlag flags, bool startup, const char *def,
336  SettingDesc(save, flags, startup), def(def == nullptr ? "" : def), max_length(max_length),
338 
339  std::string def;
340  uint32_t max_length;
343 
344  bool IsStringSetting() const override { return true; }
345  void ChangeValue(const void *object, std::string &newval) const;
346 
347  std::string FormatValue(const void *object) const override;
348  void ParseValue(const IniItem *item, void *object) const override;
349  bool IsSameValue(const IniItem *item, void *object) const override;
350  bool IsDefaultValue(void *object) const override;
351  void ResetToDefault(void *object) const override;
352  const std::string &Read(const void *object) const;
353 
354 private:
355  void MakeValueValid(std::string &str) const;
356  void Write(const void *object, const std::string &str) const;
357 };
358 
361  ListSettingDesc(const SaveLoad &save, SettingFlag flags, bool startup, const char *def) :
363 
364  const char *def;
365 
366  std::string FormatValue(const void *object) const override;
367  void ParseValue(const IniItem *item, void *object) const override;
368  bool IsSameValue(const IniItem *item, void *object) const override;
369  bool IsDefaultValue(void *object) const override;
370  void ResetToDefault(void *object) const override;
371 };
372 
375  NullSettingDesc(const SaveLoad &save) :
376  SettingDesc(save, SF_NOT_IN_CONFIG, false) {}
377 
378  std::string FormatValue(const void *) const override { NOT_REACHED(); }
379  void ParseValue(const IniItem *, void *) const override { NOT_REACHED(); }
380  bool IsSameValue(const IniItem *, void *) const override { NOT_REACHED(); }
381  bool IsDefaultValue(void *) const override { NOT_REACHED(); }
382  void ResetToDefault(void *) const override { NOT_REACHED(); }
383 };
384 
385 typedef std::variant<IntSettingDesc, BoolSettingDesc, OneOfManySettingDesc, ManyOfManySettingDesc, StringSettingDesc, ListSettingDesc, NullSettingDesc> SettingVariant;
386 
392 static constexpr const SettingDesc *GetSettingDesc(const SettingVariant &desc)
393 {
394  return std::visit([](auto&& arg) -> const SettingDesc * { return &arg; }, desc);
395 }
396 
397 typedef std::span<const SettingVariant> SettingTable;
398 
399 const SettingDesc *GetSettingFromName(const std::string_view name);
400 void GetSaveLoadFromSettingTable(SettingTable settings, std::vector<SaveLoad> &saveloads);
401 bool SetSettingValue(const IntSettingDesc *sd, int32_t value, bool force_newgame = false);
402 bool SetSettingValue(const StringSettingDesc *sd, const std::string value, bool force_newgame = false);
403 
404 #endif /* SETTINGS_INTERNAL_H */
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
Functions/types related to saving and loading games.
static constexpr const SettingDesc * GetSettingDesc(const SettingVariant &desc)
Helper to convert the type of the iterated settings description to a pointer to it.
const SettingDesc * GetSettingFromName(const std::string_view name)
Given a name of any setting, return any setting description of it.
Definition: settings.cpp:1669
void GetSaveLoadFromSettingTable(SettingTable settings, std::vector< SaveLoad > &saveloads)
Get the SaveLoad for all settings in the settings table.
Definition: settings.cpp:1641
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.
Definition: settings.cpp:1744
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.
SettingFlag
@ SF_NOT_IN_CONFIG
Do not save to config file.
@ SF_GUI_DROPDOWN
The value represents a limited number of string-options (internally integer) presented as dropdown.
@ SF_PER_COMPANY
This setting can be different for each company (saved in company struct).
@ SF_NETWORK_ONLY
This setting only applies to network games.
@ SF_NOT_IN_SAVE
Do not save with savegame, basically client-based.
@ SF_NEWGAME_ONLY
This setting cannot be changed in a game.
@ SF_SCENEDIT_ONLY
This setting can only be changed in the scenario editor.
@ SF_NO_NETWORK_SYNC
Do not synchronize over network (but it is saved if SF_NOT_IN_SAVE is not set).
@ SF_NO_NETWORK
This setting does not apply to network games; it may not be changed during the game.
@ SF_GUI_CURRENCY
The number represents money, so when reading value multiply by exchange rate.
@ SF_SCENEDIT_TOO
This setting can be changed in the scenario editor (only makes sense when SF_NEWGAME_ONLY is set).
@ SF_GUI_0_IS_SPECIAL
A value of zero is possible and has a custom string (the one after "strval").
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.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Boolean setting.
std::string FormatValue(const void *object) const override
Format the value of the setting associated with this object.
Definition: settings.cpp:736
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
int32_t GetDefaultValueCallback()
A callback to get the correct default value.
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:497
void ResetToDefault(void *object) const override
Reset the setting to its default value.
Definition: settings.cpp:755
std::string FormatValue(const void *object) const override
Format the value of the setting associated with this object.
Definition: settings.cpp:725
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.
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.
Definition: settings.cpp:1588
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:742
int32_t min
minimum values
PreChangeCheck * pre_check
Callback to check for the validity of the setting.
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:550
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:482
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:561
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:749
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:668
void ResetToDefault(void *object) const override
Reset the setting to its default value.
Definition: settings.cpp:809
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:797
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:803
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:713
std::string name
Name of this field (optional, used for tables).
Definition: saveload.h:714
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:883
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.
constexpr const std::string & GetName() const
Get the name of this setting.
SettingType GetType() const
Return the type of the setting.
Definition: settings.cpp:900
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:920
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:910
SettingFlag flags
Handles how a setting would show up in the GUI (text/currency, etc.).
String settings.
const std::string & Read(const void *object) const
Read the string from the the actual setting.
Definition: settings.cpp:600
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:574
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:776
bool IsDefaultValue(void *object) const override
Check whether the value is the same as the default value.
Definition: settings.cpp:786
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:792
void Write(const void *object, const std::string &str) const
Write a string to the actual setting.
Definition: settings.cpp:590
void ChangeValue(const void *object, std::string &newval) const
Handle changing a string value.
Definition: settings.cpp:1838
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:760
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:661