OpenTTD Source  12.0-beta2
settings.cpp
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 
24 #include "stdafx.h"
25 #include <charconv>
26 #include "settings_table.h"
27 #include "debug.h"
28 #include "currency.h"
29 #include "network/network.h"
30 #include "network/network_func.h"
31 #include "network/core/config.h"
32 #include "command_func.h"
33 #include "console_func.h"
34 #include "genworld.h"
35 #include "window_func.h"
36 #include "company_func.h"
37 #include "rev.h"
38 #include "error.h"
39 #include "gamelog.h"
40 #include "settings_func.h"
41 #include "ini_type.h"
42 #include "ai/ai_config.hpp"
43 #include "game/game_config.hpp"
44 #include "newgrf_config.h"
45 #include "fios.h"
46 #include "fileio_func.h"
47 
48 #include "table/strings.h"
49 
50 #include "safeguards.h"
51 
56 std::string _config_file;
57 std::string _private_file;
58 std::string _secrets_file;
59 
60 typedef std::list<ErrorMessageData> ErrorList;
62 
73 static auto &GenericSettingTables()
74 {
75  static const SettingTable _generic_setting_tables[] = {
76  _difficulty_settings,
77  _economy_settings,
78  _game_settings,
79  _gui_settings,
80  _linkgraph_settings,
81  _locale_settings,
82  _multimedia_settings,
83  _network_settings,
84  _news_display_settings,
85  _pathfinding_settings,
86  _script_settings,
87  _world_settings,
88  };
89  return _generic_setting_tables;
90 }
91 
95 static auto &PrivateSettingTables()
96 {
97  static const SettingTable _private_setting_tables[] = {
98  _network_private_settings,
99  };
100  return _private_setting_tables;
101 }
102 
106 static auto &SecretSettingTables()
107 {
108  static const SettingTable _secrets_setting_tables[] = {
109  _network_secrets_settings,
110  };
111  return _secrets_setting_tables;
112 }
113 
114 typedef void SettingDescProc(IniFile &ini, const SettingTable &desc, const char *grpname, void *object, bool only_startup);
115 typedef void SettingDescProcList(IniFile &ini, const char *grpname, StringList &list);
116 
117 static bool IsSignedVarMemType(VarType vt)
118 {
119  switch (GetVarMemType(vt)) {
120  case SLE_VAR_I8:
121  case SLE_VAR_I16:
122  case SLE_VAR_I32:
123  case SLE_VAR_I64:
124  return true;
125  }
126  return false;
127 }
128 
132 class ConfigIniFile : public IniFile {
133 private:
134  inline static const char * const list_group_names[] = {
135  "bans",
136  "newgrf",
137  "servers",
138  "server_bind_addresses",
139  nullptr,
140  };
141 
142 public:
143  ConfigIniFile(const std::string &filename) : IniFile(list_group_names)
144  {
145  this->LoadFromDisk(filename, NO_DIRECTORY);
146  }
147 };
148 
156 enum IniFileVersion : uint32 {
159 
161 };
162 
164 
172 size_t OneOfManySettingDesc::ParseSingleValue(const char *str, size_t len, const std::vector<std::string> &many)
173 {
174  /* check if it's an integer */
175  if (isdigit(*str)) return strtoul(str, nullptr, 0);
176 
177  size_t idx = 0;
178  for (auto one : many) {
179  if (one.size() == len && strncmp(one.c_str(), str, len) == 0) return idx;
180  idx++;
181  }
182 
183  return (size_t)-1;
184 }
185 
193 static size_t LookupManyOfMany(const std::vector<std::string> &many, const char *str)
194 {
195  const char *s;
196  size_t r;
197  size_t res = 0;
198 
199  for (;;) {
200  /* skip "whitespace" */
201  while (*str == ' ' || *str == '\t' || *str == '|') str++;
202  if (*str == 0) break;
203 
204  s = str;
205  while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++;
206 
207  r = OneOfManySettingDesc::ParseSingleValue(str, s - str, many);
208  if (r == (size_t)-1) return r;
209 
210  SetBit(res, (uint8)r); // value found, set it
211  if (*s == 0) break;
212  str = s + 1;
213  }
214  return res;
215 }
216 
225 template<typename T>
226 static int ParseIntList(const char *p, T *items, int maxitems)
227 {
228  int n = 0; // number of items read so far
229  bool comma = false; // do we accept comma?
230 
231  while (*p != '\0') {
232  switch (*p) {
233  case ',':
234  /* Do not accept multiple commas between numbers */
235  if (!comma) return -1;
236  comma = false;
237  FALLTHROUGH;
238 
239  case ' ':
240  p++;
241  break;
242 
243  default: {
244  if (n == maxitems) return -1; // we don't accept that many numbers
245  char *end;
246  unsigned long v = strtoul(p, &end, 0);
247  if (p == end) return -1; // invalid character (not a number)
248  if (sizeof(T) < sizeof(v)) v = Clamp<unsigned long>(v, std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
249  items[n++] = v;
250  p = end; // first non-number
251  comma = true; // we accept comma now
252  break;
253  }
254  }
255  }
256 
257  /* If we have read comma but no number after it, fail.
258  * We have read comma when (n != 0) and comma is not allowed */
259  if (n != 0 && !comma) return -1;
260 
261  return n;
262 }
263 
272 static bool LoadIntList(const char *str, void *array, int nelems, VarType type)
273 {
274  unsigned long items[64];
275  int i, nitems;
276 
277  if (str == nullptr) {
278  memset(items, 0, sizeof(items));
279  nitems = nelems;
280  } else {
281  nitems = ParseIntList(str, items, lengthof(items));
282  if (nitems != nelems) return false;
283  }
284 
285  switch (type) {
286  case SLE_VAR_BL:
287  case SLE_VAR_I8:
288  case SLE_VAR_U8:
289  for (i = 0; i != nitems; i++) ((byte*)array)[i] = items[i];
290  break;
291 
292  case SLE_VAR_I16:
293  case SLE_VAR_U16:
294  for (i = 0; i != nitems; i++) ((uint16*)array)[i] = items[i];
295  break;
296 
297  case SLE_VAR_I32:
298  case SLE_VAR_U32:
299  for (i = 0; i != nitems; i++) ((uint32*)array)[i] = items[i];
300  break;
301 
302  default: NOT_REACHED();
303  }
304 
305  return true;
306 }
307 
317 void ListSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
318 {
319  const byte *p = static_cast<const byte *>(GetVariableAddress(object, this->save));
320  int i, v = 0;
321 
322  for (i = 0; i != this->save.length; i++) {
323  switch (GetVarMemType(this->save.conv)) {
324  case SLE_VAR_BL:
325  case SLE_VAR_I8: v = *(const int8 *)p; p += 1; break;
326  case SLE_VAR_U8: v = *(const uint8 *)p; p += 1; break;
327  case SLE_VAR_I16: v = *(const int16 *)p; p += 2; break;
328  case SLE_VAR_U16: v = *(const uint16 *)p; p += 2; break;
329  case SLE_VAR_I32: v = *(const int32 *)p; p += 4; break;
330  case SLE_VAR_U32: v = *(const uint32 *)p; p += 4; break;
331  default: NOT_REACHED();
332  }
333  if (IsSignedVarMemType(this->save.conv)) {
334  buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
335  } else {
336  buf += seprintf(buf, last, (i == 0) ? "%u" : ",%u", v);
337  }
338  }
339 }
340 
341 char *OneOfManySettingDesc::FormatSingleValue(char *buf, const char *last, uint id) const
342 {
343  if (id >= this->many.size()) {
344  return buf + seprintf(buf, last, "%d", id);
345  }
346  return strecpy(buf, this->many[id].c_str(), last);
347 }
348 
349 void OneOfManySettingDesc::FormatValue(char *buf, const char *last, const void *object) const
350 {
351  uint id = (uint)this->Read(object);
352  this->FormatSingleValue(buf, last, id);
353 }
354 
355 void ManyOfManySettingDesc::FormatValue(char *buf, const char *last, const void *object) const
356 {
357  uint bitmask = (uint)this->Read(object);
358  bool first = true;
359  for (uint id : SetBitIterator(bitmask)) {
360  if (!first) buf = strecpy(buf, "|", last);
361  buf = this->FormatSingleValue(buf, last, id);
362  first = false;
363  }
364 }
365 
371 size_t IntSettingDesc::ParseValue(const char *str) const
372 {
373  char *end;
374  size_t val = strtoul(str, &end, 0);
375  if (end == str) {
376  ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
377  msg.SetDParamStr(0, str);
378  msg.SetDParamStr(1, this->GetName());
379  _settings_error_list.push_back(msg);
380  return this->def;
381  }
382  if (*end != '\0') {
383  ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_TRAILING_CHARACTERS);
384  msg.SetDParamStr(0, this->GetName());
385  _settings_error_list.push_back(msg);
386  }
387  return val;
388 }
389 
390 size_t OneOfManySettingDesc::ParseValue(const char *str) const
391 {
392  size_t r = OneOfManySettingDesc::ParseSingleValue(str, strlen(str), this->many);
393  /* if the first attempt of conversion from string to the appropriate value fails,
394  * look if we have defined a converter from old value to new value. */
395  if (r == (size_t)-1 && this->many_cnvt != nullptr) r = this->many_cnvt(str);
396  if (r != (size_t)-1) return r; // and here goes converted value
397 
398  ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
399  msg.SetDParamStr(0, str);
400  msg.SetDParamStr(1, this->GetName());
401  _settings_error_list.push_back(msg);
402  return this->def;
403 }
404 
405 size_t ManyOfManySettingDesc::ParseValue(const char *str) const
406 {
407  size_t r = LookupManyOfMany(this->many, str);
408  if (r != (size_t)-1) return r;
409  ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
410  msg.SetDParamStr(0, str);
411  msg.SetDParamStr(1, this->GetName());
412  _settings_error_list.push_back(msg);
413  return this->def;
414 }
415 
416 size_t BoolSettingDesc::ParseValue(const char *str) const
417 {
418  if (strcmp(str, "true") == 0 || strcmp(str, "on") == 0 || strcmp(str, "1") == 0) return true;
419  if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return false;
420 
421  ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
422  msg.SetDParamStr(0, str);
423  msg.SetDParamStr(1, this->GetName());
424  _settings_error_list.push_back(msg);
425  return this->def;
426 }
427 
434 void IntSettingDesc::MakeValueValidAndWrite(const void *object, int32 val) const
435 {
436  this->MakeValueValid(val);
437  this->Write(object, val);
438 }
439 
449 void IntSettingDesc::MakeValueValid(int32 &val) const
450 {
451  /* We need to take special care of the uint32 type as we receive from the function
452  * a signed integer. While here also bail out on 64-bit settings as those are not
453  * supported. Unsigned 8 and 16-bit variables are safe since they fit into a signed
454  * 32-bit variable
455  * TODO: Support 64-bit settings/variables; requires 64 bit over command protocol! */
456  switch (GetVarMemType(this->save.conv)) {
457  case SLE_VAR_NULL: return;
458  case SLE_VAR_BL:
459  case SLE_VAR_I8:
460  case SLE_VAR_U8:
461  case SLE_VAR_I16:
462  case SLE_VAR_U16:
463  case SLE_VAR_I32: {
464  /* Override the minimum value. No value below this->min, except special value 0 */
465  if (!(this->flags & SF_GUI_0_IS_SPECIAL) || val != 0) {
466  if (!(this->flags & SF_GUI_DROPDOWN)) {
467  /* Clamp value-type setting to its valid range */
468  val = Clamp(val, this->min, this->max);
469  } else if (val < this->min || val > (int32)this->max) {
470  /* Reset invalid discrete setting (where different values change gameplay) to its default value */
471  val = this->def;
472  }
473  }
474  break;
475  }
476  case SLE_VAR_U32: {
477  /* Override the minimum value. No value below this->min, except special value 0 */
478  uint32 uval = (uint32)val;
479  if (!(this->flags & SF_GUI_0_IS_SPECIAL) || uval != 0) {
480  if (!(this->flags & SF_GUI_DROPDOWN)) {
481  /* Clamp value-type setting to its valid range */
482  uval = ClampU(uval, this->min, this->max);
483  } else if (uval < (uint)this->min || uval > this->max) {
484  /* Reset invalid discrete setting to its default value */
485  uval = (uint32)this->def;
486  }
487  }
488  val = (int32)uval;
489  return;
490  }
491  case SLE_VAR_I64:
492  case SLE_VAR_U64:
493  default: NOT_REACHED();
494  }
495 }
496 
502 void IntSettingDesc::Write(const void *object, int32 val) const
503 {
504  void *ptr = GetVariableAddress(object, this->save);
505  WriteValue(ptr, this->save.conv, (int64)val);
506 }
507 
513 int32 IntSettingDesc::Read(const void *object) const
514 {
515  void *ptr = GetVariableAddress(object, this->save);
516  return (int32)ReadValue(ptr, this->save.conv);
517 }
518 
526 void StringSettingDesc::MakeValueValid(std::string &str) const
527 {
528  if (this->max_length == 0 || str.size() < this->max_length) return;
529 
530  /* In case a maximum length is imposed by the setting, the length
531  * includes the '\0' termination for network transfer purposes.
532  * Also ensure the string is valid after chopping of some bytes. */
533  std::string stdstr(str, this->max_length - 1);
534  str.assign(StrMakeValid(stdstr, SVS_NONE));
535 }
536 
542 void StringSettingDesc::Write(const void *object, const std::string &str) const
543 {
544  reinterpret_cast<std::string *>(GetVariableAddress(object, this->save))->assign(str);
545 }
546 
552 const std::string &StringSettingDesc::Read(const void *object) const
553 {
554  return *reinterpret_cast<std::string *>(GetVariableAddress(object, this->save));
555 }
556 
566 static void IniLoadSettings(IniFile &ini, const SettingTable &settings_table, const char *grpname, void *object, bool only_startup)
567 {
568  IniGroup *group;
569  IniGroup *group_def = ini.GetGroup(grpname);
570 
571  for (auto &desc : settings_table) {
572  const SettingDesc *sd = GetSettingDesc(desc);
573  if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
574  if (sd->startup != only_startup) continue;
575 
576  /* For settings.xx.yy load the settings from [xx] yy = ? */
577  std::string s{ sd->GetName() };
578  auto sc = s.find('.');
579  if (sc != std::string::npos) {
580  group = ini.GetGroup(s.substr(0, sc));
581  s = s.substr(sc + 1);
582  } else {
583  group = group_def;
584  }
585 
586  IniItem *item = group->GetItem(s, false);
587  if (item == nullptr && group != group_def) {
588  /* For settings.xx.yy load the settings from [settings] yy = ? in case the previous
589  * did not exist (e.g. loading old config files with a [settings] section */
590  item = group_def->GetItem(s, false);
591  }
592  if (item == nullptr) {
593  /* For settings.xx.zz.yy load the settings from [zz] yy = ? in case the previous
594  * did not exist (e.g. loading old config files with a [yapf] section */
595  sc = s.find('.');
596  if (sc != std::string::npos) item = ini.GetGroup(s.substr(0, sc))->GetItem(s.substr(sc + 1), false);
597  }
598 
599  sd->ParseValue(item, object);
600  }
601 }
602 
603 void IntSettingDesc::ParseValue(const IniItem *item, void *object) const
604 {
605  size_t val = (item == nullptr) ? this->def : this->ParseValue(item->value.has_value() ? item->value->c_str() : "");
606  this->MakeValueValidAndWrite(object, (int32)val);
607 }
608 
609 void StringSettingDesc::ParseValue(const IniItem *item, void *object) const
610 {
611  std::string str = (item == nullptr) ? this->def : item->value.value_or("");
612  this->MakeValueValid(str);
613  this->Write(object, str);
614 }
615 
616 void ListSettingDesc::ParseValue(const IniItem *item, void *object) const
617 {
618  const char *str = (item == nullptr) ? this->def : item->value.has_value() ? item->value->c_str() : nullptr;
619  void *ptr = GetVariableAddress(object, this->save);
620  if (!LoadIntList(str, ptr, this->save.length, GetVarMemType(this->save.conv))) {
621  ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY);
622  msg.SetDParamStr(0, this->GetName());
623  _settings_error_list.push_back(msg);
624 
625  /* Use default */
626  LoadIntList(this->def, ptr, this->save.length, GetVarMemType(this->save.conv));
627  }
628 }
629 
642 static void IniSaveSettings(IniFile &ini, const SettingTable &settings_table, const char *grpname, void *object, bool)
643 {
644  IniGroup *group_def = nullptr, *group;
645  IniItem *item;
646  char buf[512];
647 
648  for (auto &desc : settings_table) {
649  const SettingDesc *sd = GetSettingDesc(desc);
650  /* If the setting is not saved to the configuration
651  * file, just continue with the next setting */
652  if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
653  if (sd->flags & SF_NOT_IN_CONFIG) continue;
654 
655  /* XXX - wtf is this?? (group override?) */
656  std::string s{ sd->GetName() };
657  auto sc = s.find('.');
658  if (sc != std::string::npos) {
659  group = ini.GetGroup(s.substr(0, sc));
660  s = s.substr(sc + 1);
661  } else {
662  if (group_def == nullptr) group_def = ini.GetGroup(grpname);
663  group = group_def;
664  }
665 
666  item = group->GetItem(s, true);
667 
668  if (!item->value.has_value() || !sd->IsSameValue(item, object)) {
669  /* Value has changed, get the new value and put it into a buffer */
670  sd->FormatValue(buf, lastof(buf), object);
671 
672  /* The value is different, that means we have to write it to the ini */
673  item->value.emplace(buf);
674  }
675  }
676 }
677 
678 void IntSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
679 {
680  uint32 i = (uint32)this->Read(object);
681  seprintf(buf, last, IsSignedVarMemType(this->save.conv) ? "%d" : "%u", i);
682 }
683 
684 void BoolSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
685 {
686  bool val = this->Read(object) != 0;
687  strecpy(buf, val ? "true" : "false", last);
688 }
689 
690 bool IntSettingDesc::IsSameValue(const IniItem *item, void *object) const
691 {
692  int32 item_value = (int32)this->ParseValue(item->value->c_str());
693  int32 object_value = this->Read(object);
694  return item_value == object_value;
695 }
696 
697 void StringSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
698 {
699  const std::string &str = this->Read(object);
700  switch (GetVarMemType(this->save.conv)) {
701  case SLE_VAR_STR: strecpy(buf, str.c_str(), last); break;
702 
703  case SLE_VAR_STRQ:
704  if (str.empty()) {
705  buf[0] = '\0';
706  } else {
707  seprintf(buf, last, "\"%s\"", str.c_str());
708  }
709  break;
710 
711  default: NOT_REACHED();
712  }
713 }
714 
715 bool StringSettingDesc::IsSameValue(const IniItem *item, void *object) const
716 {
717  /* The ini parsing removes the quotes, which are needed to retain the spaces in STRQs,
718  * so those values are always different in the parsed ini item than they should be. */
719  if (GetVarMemType(this->save.conv) == SLE_VAR_STRQ) return false;
720 
721  const std::string &str = this->Read(object);
722  return item->value->compare(str) == 0;
723 }
724 
725 bool ListSettingDesc::IsSameValue(const IniItem *item, void *object) const
726 {
727  /* Checking for equality is way more expensive than just writing the value. */
728  return false;
729 }
730 
740 static void IniLoadSettingList(IniFile &ini, const char *grpname, StringList &list)
741 {
742  IniGroup *group = ini.GetGroup(grpname);
743 
744  if (group == nullptr) return;
745 
746  list.clear();
747 
748  for (const IniItem *item = group->item; item != nullptr; item = item->next) {
749  if (!item->name.empty()) list.push_back(item->name);
750  }
751 }
752 
762 static void IniSaveSettingList(IniFile &ini, const char *grpname, StringList &list)
763 {
764  IniGroup *group = ini.GetGroup(grpname);
765 
766  if (group == nullptr) return;
767  group->Clear();
768 
769  for (const auto &iter : list) {
770  group->GetItem(iter, true)->SetValue("");
771  }
772 }
773 
780 void IniLoadWindowSettings(IniFile &ini, const char *grpname, void *desc)
781 {
782  IniLoadSettings(ini, _window_settings, grpname, desc, false);
783 }
784 
791 void IniSaveWindowSettings(IniFile &ini, const char *grpname, void *desc)
792 {
793  IniSaveSettings(ini, _window_settings, grpname, desc, false);
794 }
795 
801 bool SettingDesc::IsEditable(bool do_command) const
802 {
803  if (!do_command && !(this->flags & SF_NO_NETWORK_SYNC) && _networking && !_network_server && !(this->flags & SF_PER_COMPANY)) return false;
804  if ((this->flags & SF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return false;
805  if ((this->flags & SF_NO_NETWORK) && _networking) return false;
806  if ((this->flags & SF_NEWGAME_ONLY) &&
807  (_game_mode == GM_NORMAL ||
808  (_game_mode == GM_EDITOR && !(this->flags & SF_SCENEDIT_TOO)))) return false;
809  if ((this->flags & SF_SCENEDIT_ONLY) && _game_mode != GM_EDITOR) return false;
810  return true;
811 }
812 
818 {
819  if (this->flags & SF_PER_COMPANY) return ST_COMPANY;
820  return (this->flags & SF_NOT_IN_SAVE) ? ST_CLIENT : ST_GAME;
821 }
822 
828 {
829  assert(this->IsIntSetting());
830  return static_cast<const IntSettingDesc *>(this);
831 }
832 
838 {
839  assert(this->IsStringSetting());
840  return static_cast<const StringSettingDesc *>(this);
841 }
842 
843 void PrepareOldDiffCustom();
844 void HandleOldDiffCustom(bool savegame);
845 
846 
848 static void ValidateSettings()
849 {
850  /* Do not allow a custom sea level with the original land generator. */
854  }
855 }
856 
857 static void AILoadConfig(IniFile &ini, const char *grpname)
858 {
859  IniGroup *group = ini.GetGroup(grpname);
860  IniItem *item;
861 
862  /* Clean any configured AI */
863  for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
865  }
866 
867  /* If no group exists, return */
868  if (group == nullptr) return;
869 
871  for (item = group->item; c < MAX_COMPANIES && item != nullptr; c++, item = item->next) {
873 
874  config->Change(item->name.c_str());
875  if (!config->HasScript()) {
876  if (item->name != "none") {
877  Debug(script, 0, "The AI by the name '{}' was no longer found, and removed from the list.", item->name);
878  continue;
879  }
880  }
881  if (item->value.has_value()) config->StringToSettings(*item->value);
882  }
883 }
884 
885 static void GameLoadConfig(IniFile &ini, const char *grpname)
886 {
887  IniGroup *group = ini.GetGroup(grpname);
888  IniItem *item;
889 
890  /* Clean any configured GameScript */
892 
893  /* If no group exists, return */
894  if (group == nullptr) return;
895 
896  item = group->item;
897  if (item == nullptr) return;
898 
900 
901  config->Change(item->name.c_str());
902  if (!config->HasScript()) {
903  if (item->name != "none") {
904  Debug(script, 0, "The GameScript by the name '{}' was no longer found, and removed from the list.", item->name);
905  return;
906  }
907  }
908  if (item->value.has_value()) config->StringToSettings(*item->value);
909 }
910 
916 static int DecodeHexNibble(char c)
917 {
918  if (c >= '0' && c <= '9') return c - '0';
919  if (c >= 'A' && c <= 'F') return c + 10 - 'A';
920  if (c >= 'a' && c <= 'f') return c + 10 - 'a';
921  return -1;
922 }
923 
932 static bool DecodeHexText(const char *pos, uint8 *dest, size_t dest_size)
933 {
934  while (dest_size > 0) {
935  int hi = DecodeHexNibble(pos[0]);
936  int lo = (hi >= 0) ? DecodeHexNibble(pos[1]) : -1;
937  if (lo < 0) return false;
938  *dest++ = (hi << 4) | lo;
939  pos += 2;
940  dest_size--;
941  }
942  return *pos == '|';
943 }
944 
951 static GRFConfig *GRFLoadConfig(IniFile &ini, const char *grpname, bool is_static)
952 {
953  IniGroup *group = ini.GetGroup(grpname);
954  IniItem *item;
955  GRFConfig *first = nullptr;
956  GRFConfig **curr = &first;
957 
958  if (group == nullptr) return nullptr;
959 
960  uint num_grfs = 0;
961  for (item = group->item; item != nullptr; item = item->next) {
962  GRFConfig *c = nullptr;
963 
964  uint8 grfid_buf[4], md5sum[16];
965  const char *filename = item->name.c_str();
966  bool has_grfid = false;
967  bool has_md5sum = false;
968 
969  /* Try reading "<grfid>|" and on success, "<md5sum>|". */
970  has_grfid = DecodeHexText(filename, grfid_buf, lengthof(grfid_buf));
971  if (has_grfid) {
972  filename += 1 + 2 * lengthof(grfid_buf);
973  has_md5sum = DecodeHexText(filename, md5sum, lengthof(md5sum));
974  if (has_md5sum) filename += 1 + 2 * lengthof(md5sum);
975 
976  uint32 grfid = grfid_buf[0] | (grfid_buf[1] << 8) | (grfid_buf[2] << 16) | (grfid_buf[3] << 24);
977  if (has_md5sum) {
978  const GRFConfig *s = FindGRFConfig(grfid, FGCM_EXACT, md5sum);
979  if (s != nullptr) c = new GRFConfig(*s);
980  }
981  if (c == nullptr && !FioCheckFileExists(filename, NEWGRF_DIR)) {
982  const GRFConfig *s = FindGRFConfig(grfid, FGCM_NEWEST_VALID);
983  if (s != nullptr) c = new GRFConfig(*s);
984  }
985  }
986  if (c == nullptr) c = new GRFConfig(filename);
987 
988  /* Parse parameters */
989  if (item->value.has_value() && !item->value->empty()) {
990  int count = ParseIntList(item->value->c_str(), c->param, lengthof(c->param));
991  if (count < 0) {
992  SetDParamStr(0, filename);
993  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY, WL_CRITICAL);
994  count = 0;
995  }
996  c->num_params = count;
997  }
998 
999  /* Check if item is valid */
1000  if (!FillGRFDetails(c, is_static) || HasBit(c->flags, GCF_INVALID)) {
1001  if (c->status == GCS_NOT_FOUND) {
1002  SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_NOT_FOUND);
1003  } else if (HasBit(c->flags, GCF_UNSAFE)) {
1004  SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNSAFE);
1005  } else if (HasBit(c->flags, GCF_SYSTEM)) {
1006  SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_SYSTEM);
1007  } else if (HasBit(c->flags, GCF_INVALID)) {
1008  SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_INCOMPATIBLE);
1009  } else {
1010  SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNKNOWN);
1011  }
1012 
1013  SetDParamStr(0, StrEmpty(filename) ? item->name : filename);
1014  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_GRF, WL_CRITICAL);
1015  delete c;
1016  continue;
1017  }
1018 
1019  /* Check for duplicate GRFID (will also check for duplicate filenames) */
1020  bool duplicate = false;
1021  for (const GRFConfig *gc = first; gc != nullptr; gc = gc->next) {
1022  if (gc->ident.grfid == c->ident.grfid) {
1023  SetDParamStr(0, c->filename);
1024  SetDParamStr(1, gc->filename);
1025  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_DUPLICATE_GRFID, WL_CRITICAL);
1026  duplicate = true;
1027  break;
1028  }
1029  }
1030  if (duplicate) {
1031  delete c;
1032  continue;
1033  }
1034 
1035  if (is_static) {
1036  /* Mark file as static to avoid saving in savegame. */
1037  SetBit(c->flags, GCF_STATIC);
1038  } else if (++num_grfs > NETWORK_MAX_GRF_COUNT) {
1039  /* Check we will not load more non-static NewGRFs than allowed. This could trigger issues for game servers. */
1040  ShowErrorMessage(STR_CONFIG_ERROR, STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED, WL_CRITICAL);
1041  break;
1042  }
1043 
1044  /* Add item to list */
1045  *curr = c;
1046  curr = &c->next;
1047  }
1048 
1049  return first;
1050 }
1051 
1052 static IniFileVersion LoadVersionFromConfig(IniFile &ini)
1053 {
1054  IniGroup *group = ini.GetGroup("version");
1055 
1056  auto version_number = group->GetItem("ini_version", false);
1057  /* Older ini-file versions don't have this key yet. */
1058  if (version_number == nullptr || !version_number->value.has_value()) return IFV_0;
1059 
1060  uint32 version = 0;
1061  std::from_chars(version_number->value->data(), version_number->value->data() + version_number->value->size(), version);
1062 
1063  return static_cast<IniFileVersion>(version);
1064 }
1065 
1066 static void AISaveConfig(IniFile &ini, const char *grpname)
1067 {
1068  IniGroup *group = ini.GetGroup(grpname);
1069 
1070  if (group == nullptr) return;
1071  group->Clear();
1072 
1073  for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
1075  const char *name;
1076  std::string value = config->SettingsToString();
1077 
1078  if (config->HasScript()) {
1079  name = config->GetName();
1080  } else {
1081  name = "none";
1082  }
1083 
1084  IniItem *item = new IniItem(group, name);
1085  item->SetValue(value);
1086  }
1087 }
1088 
1089 static void GameSaveConfig(IniFile &ini, const char *grpname)
1090 {
1091  IniGroup *group = ini.GetGroup(grpname);
1092 
1093  if (group == nullptr) return;
1094  group->Clear();
1095 
1097  const char *name;
1098  std::string value = config->SettingsToString();
1099 
1100  if (config->HasScript()) {
1101  name = config->GetName();
1102  } else {
1103  name = "none";
1104  }
1105 
1106  IniItem *item = new IniItem(group, name);
1107  item->SetValue(value);
1108 }
1109 
1114 static void SaveVersionInConfig(IniFile &ini)
1115 {
1116  IniGroup *group = ini.GetGroup("version");
1117  group->GetItem("version_string", true)->SetValue(_openttd_revision);
1118  group->GetItem("version_number", true)->SetValue(fmt::format("{:08X}", _openttd_newgrf_version));
1119  group->GetItem("ini_version", true)->SetValue(std::to_string(INIFILE_VERSION));
1120 }
1121 
1122 /* Save a GRF configuration to the given group name */
1123 static void GRFSaveConfig(IniFile &ini, const char *grpname, const GRFConfig *list)
1124 {
1125  ini.RemoveGroup(grpname);
1126  IniGroup *group = ini.GetGroup(grpname);
1127  const GRFConfig *c;
1128 
1129  for (c = list; c != nullptr; c = c->next) {
1130  /* Hex grfid (4 bytes in nibbles), "|", hex md5sum (16 bytes in nibbles), "|", file system path. */
1131  char key[4 * 2 + 1 + 16 * 2 + 1 + MAX_PATH];
1132  char params[512];
1133  GRFBuildParamList(params, c, lastof(params));
1134 
1135  char *pos = key + seprintf(key, lastof(key), "%08X|", BSWAP32(c->ident.grfid));
1136  pos = md5sumToString(pos, lastof(key), c->ident.md5sum);
1137  seprintf(pos, lastof(key), "|%s", c->filename);
1138  group->GetItem(key, true)->SetValue(params);
1139  }
1140 }
1141 
1142 /* Common handler for saving/loading variables to the configuration file */
1143 static void HandleSettingDescs(IniFile &generic_ini, IniFile &private_ini, IniFile &secrets_ini, SettingDescProc *proc, SettingDescProcList *proc_list, bool only_startup = false)
1144 {
1145  proc(generic_ini, _misc_settings, "misc", nullptr, only_startup);
1146 #if defined(_WIN32) && !defined(DEDICATED)
1147  proc(generic_ini, _win32_settings, "win32", nullptr, only_startup);
1148 #endif /* _WIN32 */
1149 
1150  /* The name "patches" is a fallback, as every setting should sets its own group. */
1151 
1152  for (auto &table : GenericSettingTables()) {
1153  proc(generic_ini, table, "patches", &_settings_newgame, only_startup);
1154  }
1155  for (auto &table : PrivateSettingTables()) {
1156  proc(private_ini, table, "patches", &_settings_newgame, only_startup);
1157  }
1158  for (auto &table : SecretSettingTables()) {
1159  proc(secrets_ini, table, "patches", &_settings_newgame, only_startup);
1160  }
1161 
1162  proc(generic_ini, _currency_settings, "currency", &_custom_currency, only_startup);
1163  proc(generic_ini, _company_settings, "company", &_settings_client.company, only_startup);
1164 
1165  if (!only_startup) {
1166  proc_list(private_ini, "server_bind_addresses", _network_bind_list);
1167  proc_list(private_ini, "servers", _network_host_list);
1168  proc_list(private_ini, "bans", _network_ban_list);
1169  }
1170 }
1171 
1181 static void RemoveEntriesFromIni(IniFile &ini, const SettingTable &table)
1182 {
1183  for (auto &desc : table) {
1184  const SettingDesc *sd = GetSettingDesc(desc);
1185 
1186  /* For settings.xx.yy load the settings from [xx] yy = ? */
1187  std::string s{ sd->GetName() };
1188  auto sc = s.find('.');
1189  if (sc == std::string::npos) continue;
1190 
1191  IniGroup *group = ini.GetGroup(s.substr(0, sc));
1192  s = s.substr(sc + 1);
1193 
1194  group->RemoveItem(s);
1195  }
1196 }
1197 
1202 void LoadFromConfig(bool startup)
1203 {
1204  ConfigIniFile generic_ini(_config_file);
1205  ConfigIniFile private_ini(_private_file);
1206  ConfigIniFile secrets_ini(_secrets_file);
1207 
1208  if (!startup) ResetCurrencies(false); // Initialize the array of currencies, without preserving the custom one
1209 
1210  IniFileVersion generic_version = LoadVersionFromConfig(generic_ini);
1211 
1212  /* Before the split of private/secrets, we have to look in the generic for these settings. */
1213  if (generic_version < IFV_PRIVATE_SECRETS) {
1214  HandleSettingDescs(generic_ini, generic_ini, generic_ini, IniLoadSettings, IniLoadSettingList, startup);
1215  } else {
1216  HandleSettingDescs(generic_ini, private_ini, secrets_ini, IniLoadSettings, IniLoadSettingList, startup);
1217  }
1218 
1219  /* Load basic settings only during bootstrap, load other settings not during bootstrap */
1220  if (!startup) {
1221  _grfconfig_newgame = GRFLoadConfig(generic_ini, "newgrf", false);
1222  _grfconfig_static = GRFLoadConfig(generic_ini, "newgrf-static", true);
1223  AILoadConfig(generic_ini, "ai_players");
1224  GameLoadConfig(generic_ini, "game_scripts");
1225 
1227  IniLoadSettings(generic_ini, _old_gameopt_settings, "gameopt", &_settings_newgame, false);
1228  HandleOldDiffCustom(false);
1229 
1230  ValidateSettings();
1231 
1232  /* Display scheduled errors */
1233  extern void ScheduleErrorMessage(ErrorList &datas);
1235  if (FindWindowById(WC_ERRMSG, 0) == nullptr) ShowFirstError();
1236  }
1237 }
1238 
1241 {
1242  ConfigIniFile generic_ini(_config_file);
1243  ConfigIniFile private_ini(_private_file);
1244  ConfigIniFile secrets_ini(_secrets_file);
1245 
1246  IniFileVersion generic_version = LoadVersionFromConfig(generic_ini);
1247 
1248  /* If we newly create the private/secrets file, add a dummy group on top
1249  * just so we can add a comment before it (that is how IniFile works).
1250  * This to explain what the file is about. After doing it once, never touch
1251  * it again, as otherwise we might be reverting user changes. */
1252  if (!private_ini.GetGroup("private", false)) private_ini.GetGroup("private")->comment = "; This file possibly contains private information which can identify you as person.\n";
1253  if (!secrets_ini.GetGroup("secrets", false)) secrets_ini.GetGroup("secrets")->comment = "; Do not share this file with others, not even if they claim to be technical support.\n; This file contains saved passwords and other secrets that should remain private to you!\n";
1254 
1255  if (generic_version == IFV_0) {
1256  /* Remove some obsolete groups. These have all been loaded into other groups. */
1257  generic_ini.RemoveGroup("patches");
1258  generic_ini.RemoveGroup("yapf");
1259  generic_ini.RemoveGroup("gameopt");
1260 
1261  /* Remove all settings from the generic ini that are now in the private ini. */
1262  generic_ini.RemoveGroup("server_bind_addresses");
1263  generic_ini.RemoveGroup("servers");
1264  generic_ini.RemoveGroup("bans");
1265  for (auto &table : PrivateSettingTables()) {
1266  RemoveEntriesFromIni(generic_ini, table);
1267  }
1268 
1269  /* Remove all settings from the generic ini that are now in the secrets ini. */
1270  for (auto &table : SecretSettingTables()) {
1271  RemoveEntriesFromIni(generic_ini, table);
1272  }
1273  }
1274 
1275  HandleSettingDescs(generic_ini, private_ini, secrets_ini, IniSaveSettings, IniSaveSettingList);
1276  GRFSaveConfig(generic_ini, "newgrf", _grfconfig_newgame);
1277  GRFSaveConfig(generic_ini, "newgrf-static", _grfconfig_static);
1278  AISaveConfig(generic_ini, "ai_players");
1279  GameSaveConfig(generic_ini, "game_scripts");
1280 
1281  SaveVersionInConfig(generic_ini);
1282  SaveVersionInConfig(private_ini);
1283  SaveVersionInConfig(secrets_ini);
1284 
1285  generic_ini.SaveToDisk(_config_file);
1286  private_ini.SaveToDisk(_private_file);
1287  secrets_ini.SaveToDisk(_secrets_file);
1288 }
1289 
1295 {
1296  StringList list;
1297 
1299  for (IniGroup *group = ini.group; group != nullptr; group = group->next) {
1300  if (group->name.compare(0, 7, "preset-") == 0) {
1301  list.push_back(group->name.substr(7));
1302  }
1303  }
1304 
1305  return list;
1306 }
1307 
1314 GRFConfig *LoadGRFPresetFromConfig(const char *config_name)
1315 {
1316  size_t len = strlen(config_name) + 8;
1317  char *section = (char*)alloca(len);
1318  seprintf(section, section + len - 1, "preset-%s", config_name);
1319 
1321  GRFConfig *config = GRFLoadConfig(ini, section, false);
1322 
1323  return config;
1324 }
1325 
1332 void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
1333 {
1334  size_t len = strlen(config_name) + 8;
1335  char *section = (char*)alloca(len);
1336  seprintf(section, section + len - 1, "preset-%s", config_name);
1337 
1339  GRFSaveConfig(ini, section, config);
1340  ini.SaveToDisk(_config_file);
1341 }
1342 
1347 void DeleteGRFPresetFromConfig(const char *config_name)
1348 {
1349  size_t len = strlen(config_name) + 8;
1350  char *section = (char*)alloca(len);
1351  seprintf(section, section + len - 1, "preset-%s", config_name);
1352 
1354  ini.RemoveGroup(section);
1355  ini.SaveToDisk(_config_file);
1356 }
1357 
1364 void IntSettingDesc::ChangeValue(const void *object, int32 newval) const
1365 {
1366  int32 oldval = this->Read(object);
1367  this->MakeValueValid(newval);
1368  if (this->pre_check != nullptr && !this->pre_check(newval)) return;
1369  if (oldval == newval) return;
1370 
1371  this->Write(object, newval);
1372  if (this->post_callback != nullptr) this->post_callback(newval);
1373 
1374  if (this->flags & SF_NO_NETWORK) {
1376  GamelogSetting(this->GetName(), oldval, newval);
1378  }
1379 
1381 
1382  if (_save_config) SaveToConfig();
1383 }
1384 
1392 static const SettingDesc *GetSettingFromName(const std::string_view name, const SettingTable &settings)
1393 {
1394  /* First check all full names */
1395  for (auto &desc : settings) {
1396  const SettingDesc *sd = GetSettingDesc(desc);
1397  if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
1398  if (sd->GetName() == name) return sd;
1399  }
1400 
1401  /* Then check the shortcut variant of the name. */
1402  std::string short_name_suffix = std::string{ "." }.append(name);
1403  for (auto &desc : settings) {
1404  const SettingDesc *sd = GetSettingDesc(desc);
1405  if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
1406  if (StrEndsWith(sd->GetName(), short_name_suffix)) return sd;
1407  }
1408 
1409  return nullptr;
1410 }
1411 
1417 void GetSaveLoadFromSettingTable(SettingTable settings, std::vector<SaveLoad> &saveloads)
1418 {
1419  for (auto &desc : settings) {
1420  const SettingDesc *sd = GetSettingDesc(desc);
1421  if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
1422  saveloads.push_back(sd->save);
1423  }
1424 }
1425 
1432 static const SettingDesc *GetCompanySettingFromName(std::string_view name)
1433 {
1434  static const std::string_view company_prefix = "company.";
1435  if (StrStartsWith(name, company_prefix)) name.remove_prefix(company_prefix.size());
1436  return GetSettingFromName(name, _company_settings);
1437 }
1438 
1445 const SettingDesc *GetSettingFromName(const std::string_view name)
1446 {
1447  for (auto &table : GenericSettingTables()) {
1448  auto sd = GetSettingFromName(name, table);
1449  if (sd != nullptr) return sd;
1450  }
1451  for (auto &table : PrivateSettingTables()) {
1452  auto sd = GetSettingFromName(name, table);
1453  if (sd != nullptr) return sd;
1454  }
1455  for (auto &table : SecretSettingTables()) {
1456  auto sd = GetSettingFromName(name, table);
1457  if (sd != nullptr) return sd;
1458  }
1459 
1460  return GetCompanySettingFromName(name);
1461 }
1462 
1474 CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
1475 {
1476  if (text.empty()) return CMD_ERROR;
1477  const SettingDesc *sd = GetSettingFromName(text);
1478 
1479  if (sd == nullptr) return CMD_ERROR;
1481  if (!sd->IsIntSetting()) return CMD_ERROR;
1482 
1483  if (!sd->IsEditable(true)) return CMD_ERROR;
1484 
1485  if (flags & DC_EXEC) {
1486  sd->AsIntSetting()->ChangeValue(&GetGameSettings(), p2);
1487  }
1488 
1489  return CommandCost();
1490 }
1491 
1502 CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
1503 {
1504  if (text.empty()) return CMD_ERROR;
1505  const SettingDesc *sd = GetCompanySettingFromName(text.c_str());
1506 
1507  if (sd == nullptr) return CMD_ERROR;
1508  if (!sd->IsIntSetting()) return CMD_ERROR;
1509 
1510  if (flags & DC_EXEC) {
1512  }
1513 
1514  return CommandCost();
1515 }
1516 
1524 bool SetSettingValue(const IntSettingDesc *sd, int32 value, bool force_newgame)
1525 {
1526  const IntSettingDesc *setting = sd->AsIntSetting();
1527  if ((setting->flags & SF_PER_COMPANY) != 0) {
1528  if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
1529  return DoCommandP(0, 0, value, CMD_CHANGE_COMPANY_SETTING, nullptr, setting->GetName());
1530  }
1531 
1532  setting->ChangeValue(&_settings_client.company, value);
1533  return true;
1534  }
1535 
1536  /* If an item is company-based, we do not send it over the network
1537  * (if any) to change. Also *hack*hack* we update the _newgame version
1538  * of settings because changing a company-based setting in a game also
1539  * changes its defaults. At least that is the convention we have chosen */
1540  if (setting->flags & SF_NO_NETWORK_SYNC) {
1541  if (_game_mode != GM_MENU) {
1542  setting->ChangeValue(&_settings_newgame, value);
1543  }
1544  setting->ChangeValue(&GetGameSettings(), value);
1545  return true;
1546  }
1547 
1548  if (force_newgame) {
1549  setting->ChangeValue(&_settings_newgame, value);
1550  return true;
1551  }
1552 
1553  /* send non-company-based settings over the network */
1554  if (!_networking || (_networking && _network_server)) {
1555  return DoCommandP(0, 0, value, CMD_CHANGE_SETTING, nullptr, setting->GetName());
1556  }
1557  return false;
1558 }
1559 
1564 {
1565  Company *c = Company::Get(cid);
1566  for (auto &desc : _company_settings) {
1567  const IntSettingDesc *int_setting = GetSettingDesc(desc)->AsIntSetting();
1568  int_setting->MakeValueValidAndWrite(&c->settings, int_setting->def);
1569  }
1570 }
1571 
1576 {
1577  const void *old_object = &Company::Get(_current_company)->settings;
1578  const void *new_object = &_settings_client.company;
1579  for (auto &desc : _company_settings) {
1580  const SettingDesc *sd = GetSettingDesc(desc);
1581  uint32 old_value = (uint32)sd->AsIntSetting()->Read(new_object);
1582  uint32 new_value = (uint32)sd->AsIntSetting()->Read(old_object);
1583  if (old_value != new_value) NetworkSendCommand(0, 0, new_value, CMD_CHANGE_COMPANY_SETTING, nullptr, sd->GetName(), _local_company);
1584  }
1585 }
1586 
1594 bool SetSettingValue(const StringSettingDesc *sd, std::string value, bool force_newgame)
1595 {
1596  assert(sd->flags & SF_NO_NETWORK_SYNC);
1597 
1598  if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ && value.compare("(null)") == 0) {
1599  value.clear();
1600  }
1601 
1602  const void *object = (_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game;
1603  sd->AsStringSetting()->ChangeValue(object, value);
1604  return true;
1605 }
1606 
1613 void StringSettingDesc::ChangeValue(const void *object, std::string &newval) const
1614 {
1615  this->MakeValueValid(newval);
1616  if (this->pre_check != nullptr && !this->pre_check(newval)) return;
1617 
1618  this->Write(object, newval);
1619  if (this->post_callback != nullptr) this->post_callback(newval);
1620 
1621  if (_save_config) SaveToConfig();
1622 }
1623 
1624 /* Those 2 functions need to be here, else we have to make some stuff non-static
1625  * and besides, it is also better to keep stuff like this at the same place */
1626 void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
1627 {
1628  const SettingDesc *sd = GetSettingFromName(name);
1629  if (sd == nullptr) {
1630  IConsolePrint(CC_ERROR, "'{}' is an unknown setting.", name);
1631  return;
1632  }
1633 
1634  bool success = true;
1635  if (sd->IsStringSetting()) {
1636  success = SetSettingValue(sd->AsStringSetting(), value, force_newgame);
1637  } else if (sd->IsIntSetting()) {
1638  const IntSettingDesc *isd = sd->AsIntSetting();
1639  size_t val = isd->ParseValue(value);
1640  if (!_settings_error_list.empty()) {
1641  IConsolePrint(CC_ERROR, "'{}' is not a valid value for this setting.", value);
1642  _settings_error_list.clear();
1643  return;
1644  }
1645  success = SetSettingValue(isd, (int32)val, force_newgame);
1646  }
1647 
1648  if (!success) {
1649  if (_network_server) {
1650  IConsolePrint(CC_ERROR, "This command/variable is not available during network games.");
1651  } else {
1652  IConsolePrint(CC_ERROR, "This command/variable is only available to a network server.");
1653  }
1654  }
1655 }
1656 
1657 void IConsoleSetSetting(const char *name, int value)
1658 {
1659  const SettingDesc *sd = GetSettingFromName(name);
1660  assert(sd != nullptr);
1661  SetSettingValue(sd->AsIntSetting(), value);
1662 }
1663 
1669 void IConsoleGetSetting(const char *name, bool force_newgame)
1670 {
1671  const SettingDesc *sd = GetSettingFromName(name);
1672  if (sd == nullptr) {
1673  IConsolePrint(CC_ERROR, "'{}' is an unknown setting.", name);
1674  return;
1675  }
1676 
1677  const void *object = (_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game;
1678 
1679  if (sd->IsStringSetting()) {
1680  IConsolePrint(CC_INFO, "Current value for '{}' is '{}'.", sd->GetName(), sd->AsStringSetting()->Read(object));
1681  } else if (sd->IsIntSetting()) {
1682  char value[20];
1683  sd->FormatValue(value, lastof(value), object);
1684  const IntSettingDesc *int_setting = sd->AsIntSetting();
1685  IConsolePrint(CC_INFO, "Current value for '{}' is '{}' (min: {}{}, max: {}).",
1686  sd->GetName(), value, (sd->flags & SF_GUI_0_IS_SPECIAL) ? "(0) " : "", int_setting->min, int_setting->max);
1687  }
1688 }
1689 
1690 static void IConsoleListSettingsTable(const SettingTable &table, const char *prefilter)
1691 {
1692  for (auto &desc : table) {
1693  const SettingDesc *sd = GetSettingDesc(desc);
1694  if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
1695  if (prefilter != nullptr && sd->GetName().find(prefilter) == std::string::npos) continue;
1696  char value[80];
1697  sd->FormatValue(value, lastof(value), &GetGameSettings());
1698  IConsolePrint(CC_DEFAULT, "{} = {}", sd->GetName(), value);
1699  }
1700 }
1701 
1707 void IConsoleListSettings(const char *prefilter)
1708 {
1709  IConsolePrint(CC_HELP, "All settings with their current value:");
1710 
1711  for (auto &table : GenericSettingTables()) {
1712  IConsoleListSettingsTable(table, prefilter);
1713  }
1714  for (auto &table : PrivateSettingTables()) {
1715  IConsoleListSettingsTable(table, prefilter);
1716  }
1717  for (auto &table : SecretSettingTables()) {
1718  IConsoleListSettingsTable(table, prefilter);
1719  }
1720 
1721  IConsolePrint(CC_HELP, "Use 'setting' command to change a value.");
1722 }
GamelogSetting
void GamelogSetting(const std::string &name, int32 oldval, int32 newval)
Logs change in game settings.
Definition: gamelog.cpp:486
IniLoadFile::RemoveGroup
void RemoveGroup(const char *name)
Remove the group with the given name.
Definition: ini_load.cpp:182
ShowFirstError
void ShowFirstError()
Show the first error of the queue.
Definition: error_gui.cpp:348
OneOfManySettingDesc::FormatValue
void FormatValue(char *buf, const char *last, const void *object) const override
Format the value of the setting associated with this object.
Definition: settings.cpp:349
ErrorList
std::list< ErrorMessageData > ErrorList
Define a queue with errors.
Definition: error_gui.cpp:178
SaveLoad::version_to
SaveLoadVersion version_to
Save/load the variable before this savegame version.
Definition: saveload.h:659
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
StringSettingDesc::FormatValue
void FormatValue(char *buf, const char *last, const void *object) const override
Format the value of the setting associated with this object.
Definition: settings.cpp:697
CC_INFO
static const TextColour CC_INFO
Colour for information lines.
Definition: console_type.h:27
IntSettingDesc::MakeValueValid
void MakeValueValid(int32 &value) const
Make the value valid given the limitations of this setting.
Definition: settings.cpp:449
SF_PER_COMPANY
@ SF_PER_COMPANY
This setting can be different for each company (saved in company struct).
Definition: settings_internal.h:27
ClientSettings
All settings that are only important for the local client.
Definition: settings_type.h:592
AIConfig
Definition: ai_config.hpp:16
SF_NOT_IN_SAVE
@ SF_NOT_IN_SAVE
Do not save with savegame, basically client-based.
Definition: settings_internal.h:28
SF_GUI_0_IS_SPECIAL
@ SF_GUI_0_IS_SPECIAL
A value of zero is possible and has a custom string (the one after "strval").
Definition: settings_internal.h:18
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
StringSettingDesc::MakeValueValid
void MakeValueValid(std::string &str) const
Make the value valid given the limitations of this setting.
Definition: settings.cpp:526
RemoveEntriesFromIni
static void RemoveEntriesFromIni(IniFile &ini, const SettingTable &table)
Remove all entries from a settings table from an ini-file.
Definition: settings.cpp:1181
SLE_VAR_STR
@ SLE_VAR_STR
string pointer
Definition: saveload.h:589
command_func.h
FindGRFConfig
const GRFConfig * FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
Find a NewGRF in the scanned list.
Definition: newgrf_config.cpp:736
IniItem::SetValue
void SetValue(const std::string_view value)
Replace the current value with another value.
Definition: ini_load.cpp:41
StringSettingDesc::pre_check
PreChangeCheck * pre_check
Callback to check for the validity of the setting.
Definition: settings_internal.h:271
ErrorMessageData::SetDParamStr
void SetDParamStr(uint n, const char *str)
Set a rawstring parameter.
Definition: error_gui.cpp:161
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:23
IniItem::next
IniItem * next
The next item in this group.
Definition: ini_type.h:26
GRFLoadConfig
static GRFConfig * GRFLoadConfig(IniFile &ini, const char *grpname, bool is_static)
Load a GRF configuration.
Definition: settings.cpp:951
GetSettingFromName
static const SettingDesc * GetSettingFromName(const std::string_view name, const SettingTable &settings)
Given a name of setting, return a setting description from the table.
Definition: settings.cpp:1392
ValidateSettings
static void ValidateSettings()
Checks if any settings are set to incorrect values, and sets them to correct values in that case.
Definition: settings.cpp:848
SetDefaultCompanySettings
void SetDefaultCompanySettings(CompanyID cid)
Set the company settings for a new company to their default values.
Definition: settings.cpp:1563
StringSettingDesc::IsSameValue
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:715
CUSTOM_SEA_LEVEL_MIN_PERCENTAGE
static const uint CUSTOM_SEA_LEVEL_MIN_PERCENTAGE
Minimum percentage a user can specify for custom sea level.
Definition: genworld.h:48
SF_NOT_IN_CONFIG
@ SF_NOT_IN_CONFIG
Do not save to config file.
Definition: settings_internal.h:29
IntSettingDesc::min
int32 min
minimum values
Definition: settings_internal.h:161
IniLoadWindowSettings
void IniLoadWindowSettings(IniFile &ini, const char *grpname, void *desc)
Load a WindowDesc from config.
Definition: settings.cpp:780
currency.h
GRFConfig::num_params
uint8 num_params
Number of used parameters.
Definition: newgrf_config.h:177
ManyOfManySettingDesc::FormatValue
void FormatValue(char *buf, const char *last, const void *object) const override
Format the value of the setting associated with this object.
Definition: settings.cpp:355
ST_GAME
@ ST_GAME
Game setting.
Definition: settings_internal.h:62
_network_server
bool _network_server
network-server is active
Definition: network.cpp:57
IntSettingDesc::MakeValueValidAndWrite
void MakeValueValidAndWrite(const void *object, int32 value) const
Make the value valid and then write it to the setting.
Definition: settings.cpp:434
IniItem
A single "line" in an ini file.
Definition: ini_type.h:25
SettingDesc::save
SaveLoad save
Internal structure (going to savegame, parts to config).
Definition: settings_internal.h:79
SettingDesc::GetType
SettingType GetType() const
Return the type of the setting.
Definition: settings.cpp:817
SaveToConfig
void SaveToConfig()
Save the values to the configuration file.
Definition: settings.cpp:1240
PrepareOldDiffCustom
void PrepareOldDiffCustom()
Prepare for reading and old diff_custom by zero-ing the memory.
Definition: settings_sl.cpp:25
_old_vds
VehicleDefaultSettings _old_vds
Used for loading default vehicles settings from old savegames.
Definition: settings.cpp:55
SettingDesc::IsEditable
bool IsEditable(bool do_command=false) const
Check whether the setting is editable in the current gamemode.
Definition: settings.cpp:801
IniGroup
A group within an ini file.
Definition: ini_type.h:38
ST_CLIENT
@ ST_CLIENT
Client setting.
Definition: settings_internal.h:64
LG_ORIGINAL
@ LG_ORIGINAL
The original landscape generator.
Definition: genworld.h:20
FindWindowById
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1146
ClampU
static uint ClampU(const uint a, const uint min, const uint max)
Clamp an unsigned integer between an interval.
Definition: math_func.hpp:122
HandleOldDiffCustom
void HandleOldDiffCustom(bool savegame)
Reading of the old diff_custom array and transforming it to the new format.
Definition: settings_sl.cpp:36
SettingDesc::FormatValue
virtual void FormatValue(char *buf, const char *last, const void *object) const =0
Format the value of the setting associated with this object.
GameSettings::difficulty
DifficultySettings difficulty
settings related to the difficulty
Definition: settings_type.h:575
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
IniGroup::RemoveItem
void RemoveItem(const std::string &name)
Remove the item with the given name.
Definition: ini_load.cpp:107
SLE_VAR_NULL
@ SLE_VAR_NULL
useful to write zeros in savegame.
Definition: saveload.h:587
IntSettingDesc::ChangeValue
void ChangeValue(const void *object, int32 newvalue) const
Handle changing a value.
Definition: settings.cpp:1364
LookupManyOfMany
static size_t LookupManyOfMany(const std::vector< std::string > &many, const char *str)
Find the set-integer value MANYofMANY type in a string.
Definition: settings.cpp:193
SF_NO_NETWORK_SYNC
@ SF_NO_NETWORK_SYNC
Do not synchronize over network (but it is saved if SF_NOT_IN_SAVE is not set).
Definition: settings_internal.h:30
fileio_func.h
ST_COMPANY
@ ST_COMPANY
Company setting.
Definition: settings_internal.h:63
GCS_NOT_FOUND
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
Definition: newgrf_config.h:37
SaveLoad::length
uint16 length
(Conditional) length of the variable (eg. arrays) (max array size is 65536 elements).
Definition: saveload.h:657
GRFConfig::ident
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
Definition: newgrf_config.h:163
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
_network_bind_list
StringList _network_bind_list
The addresses to bind on.
Definition: network.cpp:65
StringSettingDesc::Read
const std::string & Read(const void *object) const
Read the string from the the actual setting.
Definition: settings.cpp:552
GRFConfig::status
GRFStatus status
NOSAVE: GRFStatus, enum.
Definition: newgrf_config.h:174
StringSettingDesc::post_callback
PostChangeCallback * post_callback
Callback when the setting has been changed.
Definition: settings_internal.h:272
SettingDesc::flags
SettingFlag flags
Handles how a setting would show up in the GUI (text/currency, etc.).
Definition: settings_internal.h:77
SaveLoad::conv
VarType conv
Type of the variable to be saved; this field combines both FileVarType and MemVarType.
Definition: saveload.h:656
_private_file
std::string _private_file
Private configuration file of OpenTTD.
Definition: settings.cpp:57
newgrf_config.h
IFV_PRIVATE_SECRETS
@ IFV_PRIVATE_SECRETS
1 PR#9298 Moving of settings from openttd.cfg to private.cfg / secrets.cfg.
Definition: settings.cpp:158
gamelog.h
fios.h
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
GLAT_SETTING
@ GLAT_SETTING
Setting changed.
Definition: gamelog.h:21
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:348
SettingDesc::IsSameValue
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.
GRFIdentifier::md5sum
uint8 md5sum[16]
MD5 checksum of file to distinguish files with the same GRF ID (eg. newer version of GRF)
Definition: newgrf_config.h:85
IntSettingDesc::pre_check
PreChangeCheck * pre_check
Callback to check for the validity of the setting.
Definition: settings_internal.h:168
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:196
NetworkSendCommand
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const std::string &text, CompanyID company)
Prepare a DoCommand to be send over the network.
Definition: network_command.cpp:136
StringSettingDesc::max_length
uint32 max_length
Maximum length of the string, 0 means no maximum length.
Definition: settings_internal.h:270
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:346
genworld.h
SettingDesc::AsStringSetting
const struct StringSettingDesc * AsStringSetting() const
Get the setting description of this setting as a string setting.
Definition: settings.cpp:837
CC_DEFAULT
static const TextColour CC_DEFAULT
Default colour of the console.
Definition: console_type.h:23
ManyOfManySettingDesc::ParseValue
size_t ParseValue(const char *str) const override
Convert a string representation (external) of an integer-like setting to an integer.
Definition: settings.cpp:405
ConfigIniFile
IniFile to store a configuration.
Definition: settings.cpp:132
GRFIdentifier::grfid
uint32 grfid
GRF ID (defined by Action 0x08)
Definition: newgrf_config.h:84
IniGroup::Clear
void Clear()
Clear all items in the group.
Definition: ini_load.cpp:130
IFV_MAX_VERSION
@ IFV_MAX_VERSION
Highest possible ini-file version.
Definition: settings.cpp:160
ShowErrorMessage
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:383
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:576
GCF_INVALID
@ GCF_INVALID
GRF is unusable with this version of OpenTTD.
Definition: newgrf_config.h:30
GetGameSettings
static GameSettings & GetGameSettings()
Get the settings-object applicable for the current situation: the newgame settings when we're in the ...
Definition: settings_type.h:617
IniSaveSettings
static void IniSaveSettings(IniFile &ini, const SettingTable &settings_table, const char *grpname, void *object, bool)
Save the values of settings to the inifile.
Definition: settings.cpp:642
SettingDesc::ParseValue
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.
StringSettingDesc
String settings.
Definition: settings_internal.h:247
GRFBuildParamList
char * GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
Build a string containing space separated parameter values, and terminate.
Definition: newgrf_config.cpp:775
ListSettingDesc::ParseValue
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:616
COMPANY_FIRST
@ COMPANY_FIRST
First company, same as owner.
Definition: company_type.h:22
SettingType
SettingType
Type of settings for filtering.
Definition: settings_internal.h:61
NETWORK_MAX_GRF_COUNT
static const uint NETWORK_MAX_GRF_COUNT
Maximum number of GRFs that can be sent.
Definition: config.h:95
DecodeHexText
static bool DecodeHexText(const char *pos, uint8 *dest, size_t dest_size)
Parse a sequence of characters (supposedly hex digits) into a sequence of bytes.
Definition: settings.cpp:932
StringSettingDesc::ChangeValue
void ChangeValue(const void *object, std::string &newval) const
Handle changing a string value.
Definition: settings.cpp:1613
GetGRFPresetList
StringList GetGRFPresetList()
Get the list of known NewGrf presets.
Definition: settings.cpp:1294
CommandCost
Common return value for all commands.
Definition: command_type.h:23
SetBitIterator
Iterable ensemble of each set bit in a value.
Definition: bitmath_func.hpp:329
IniFile::IniFile
IniFile(const char *const *list_group_names=nullptr)
Create a new ini file with given group names.
Definition: ini.cpp:37
settings_func.h
SF_NETWORK_ONLY
@ SF_NETWORK_ONLY
This setting only applies to network games.
Definition: settings_internal.h:22
GCF_UNSAFE
@ GCF_UNSAFE
GRF file is unsafe for static usage.
Definition: newgrf_config.h:24
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:155
ParseIntList
static int ParseIntList(const char *p, T *items, int maxitems)
Parse an integerlist string and set each found value.
Definition: settings.cpp:226
DoCommandP
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:541
CC_HELP
static const TextColour CC_HELP
Colour for help lines.
Definition: console_type.h:26
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
SettingDesc::startup
bool startup
Setting has to be loaded directly at startup?.
Definition: settings_internal.h:78
GCF_SYSTEM
@ GCF_SYSTEM
GRF file is an openttd-internal system grf.
Definition: newgrf_config.h:23
IntSettingDesc
Base integer type, including boolean, settings.
Definition: settings_internal.h:136
SettingDesc::GetName
constexpr const std::string & GetName() const
Get the name of this setting.
Definition: settings_internal.h:88
IniItem::value
std::optional< std::string > value
The value of this item.
Definition: ini_type.h:28
GRFConfig::flags
uint8 flags
NOSAVE: GCF_Flags, bitset.
Definition: newgrf_config.h:173
GameConfig
Definition: game_config.hpp:15
IniLoadSettingList
static void IniLoadSettingList(IniFile &ini, const char *grpname, StringList &list)
Loads all items from a 'grpname' section into a list The list parameter can be a nullptr pointer,...
Definition: settings.cpp:740
ScriptConfig::GetName
const char * GetName() const
Get the name of the Script.
Definition: script_config.cpp:172
SF_NEWGAME_ONLY
@ SF_NEWGAME_ONLY
This setting cannot be changed in a game.
Definition: settings_internal.h:24
IniFile::SaveToDisk
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
Definition: ini.cpp:46
AIConfig::GetConfig
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: ai_config.cpp:45
SecretSettingTables
static auto & SecretSettingTables()
List of all the secrets setting tables.
Definition: settings.cpp:106
StringSettingDesc::ParseValue
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:609
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:53
StrStartsWith
bool StrStartsWith(const std::string_view str, const std::string_view prefix)
Check whether the given string starts with the given prefix.
Definition: string.cpp:367
CompanyProperties::settings
CompanySettings settings
settings specific for each company
Definition: company_base.h:104
GamelogStartAction
void GamelogStartAction(GamelogActionType at)
Stores information about new action, but doesn't allocate it Action is allocated only when there is a...
Definition: gamelog.cpp:69
CmdChangeSetting
CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
Network-safe changing of settings (server-only).
Definition: settings.cpp:1474
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
SyncCompanySettings
void SyncCompanySettings()
Sync all company settings in a multiplayer game.
Definition: settings.cpp:1575
safeguards.h
IniLoadSettings
static void IniLoadSettings(IniFile &ini, const SettingTable &settings_table, const char *grpname, void *object, bool only_startup)
Load values from a group of an IniFile structure into the internal representation.
Definition: settings.cpp:566
ScriptConfig::StringToSettings
void StringToSettings(const std::string &value)
Convert a string which is stored in the config file or savegames to custom settings of this Script.
Definition: script_config.cpp:182
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:64
DifficultySettings::quantity_sea_lakes
byte quantity_sea_lakes
the amount of seas/lakes
Definition: settings_type.h:86
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
GameSettings
All settings together for the game.
Definition: settings_type.h:574
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:56
OneOfManySettingDesc::many_cnvt
OnConvert * many_cnvt
callback procedure when loading value mechanism fails
Definition: settings_internal.h:223
IFV_0
@ IFV_0
0 All versions prior to introduction.
Definition: settings.cpp:157
ListSettingDesc::FormatValue
void FormatValue(char *buf, const char *last, const void *object) const override
Convert an integer-array (intlist) to a string representation.
Definition: settings.cpp:317
GetSettingDesc
static constexpr const SettingDesc * GetSettingDesc(const SettingVariant &desc)
Helper to convert the type of the iterated settings description to a pointer to it.
Definition: settings_internal.h:318
DeleteGRFPresetFromConfig
void DeleteGRFPresetFromConfig(const char *config_name)
Delete a NewGRF configuration by preset name.
Definition: settings.cpp:1347
ErrorMessageData
The data of the error message.
Definition: error.h:29
VehicleDefaultSettings
Default settings for vehicles.
Definition: settings_type.h:556
error.h
ResetCurrencies
void ResetCurrencies(bool preserve_custom)
Will fill _currency_specs array with default values from origin_currency_specs Called only from newgr...
Definition: currency.cpp:157
IniSaveSettingList
static void IniSaveSettingList(IniFile &ini, const char *grpname, StringList &list)
Saves all items from a list into the 'grpname' section The list parameter can be a nullptr pointer,...
Definition: settings.cpp:762
stdafx.h
settings_table.h
BSWAP32
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Definition: bitmath_func.hpp:390
IntSettingDesc::Write
void Write(const void *object, int32 value) const
Set the value of a setting.
Definition: settings.cpp:502
NEWGRF_DIR
@ NEWGRF_DIR
Subdirectory for all NewGRFs.
Definition: fileio_type.h:117
_grfconfig_static
GRFConfig * _grfconfig_static
First item in list of static GRF set up.
Definition: newgrf_config.cpp:173
GamelogStopAction
void GamelogStopAction()
Stops logging of any changes.
Definition: gamelog.cpp:78
StrEndsWith
bool StrEndsWith(const std::string_view str, const std::string_view suffix)
Check whether the given string ends with the given suffix.
Definition: string.cpp:380
SlIsObjectCurrentlyValid
static bool SlIsObjectCurrentlyValid(SaveLoadVersion version_from, SaveLoadVersion version_to)
Checks if some version from/to combination falls within the range of the active savegame version.
Definition: saveload.h:1051
LoadGRFPresetFromConfig
GRFConfig * LoadGRFPresetFromConfig(const char *config_name)
Load a NewGRF configuration by preset-name.
Definition: settings.cpp:1314
OneOfManySettingDesc::ParseValue
size_t ParseValue(const char *str) const override
Convert a string representation (external) of an integer-like setting to an integer.
Definition: settings.cpp:390
WriteValue
void WriteValue(void *ptr, VarType conv, int64 val)
Write the value of a setting.
Definition: saveload.cpp:828
SettingDesc::IsStringSetting
virtual bool IsStringSetting() const
Check whether this setting is an string type setting.
Definition: settings_internal.h:103
OneOfManySettingDesc::many
std::vector< std::string > many
possible values for this type
Definition: settings_internal.h:222
_secrets_file
std::string _secrets_file
Secrets configuration file of OpenTTD.
Definition: settings.cpp:58
GenericSettingTables
static auto & GenericSettingTables()
List of all the generic setting tables.
Definition: settings.cpp:73
BoolSettingDesc::ParseValue
size_t ParseValue(const char *str) const override
Convert a string representation (external) of an integer-like setting to an integer.
Definition: settings.cpp:416
IniLoadFile::group
IniGroup * group
the first group in the ini
Definition: ini_type.h:56
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
rev.h
WC_GAME_OPTIONS
@ WC_GAME_OPTIONS
Game options window; Window numbers:
Definition: window_type.h:604
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
IConsoleListSettings
void IConsoleListSettings(const char *prefilter)
List all settings and their value to the console.
Definition: settings.cpp:1707
IniGroup::comment
std::string comment
comment for group
Definition: ini_type.h:44
FioCheckFileExists
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir)
Check whether the given file exists.
Definition: fileio.cpp:108
IniGroup::name
std::string name
name of group
Definition: ini_type.h:43
StrMakeValid
std::string StrMakeValid(const std::string &str, StringValidationSettings settings)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
Definition: string.cpp:281
IniFile
Ini file that supports both loading and saving.
Definition: ini_type.h:89
IntSettingDesc::IsSameValue
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:690
SettingDesc
Properties of config file settings.
Definition: settings_internal.h:72
PrivateSettingTables
static auto & PrivateSettingTables()
List of all the private setting tables.
Definition: settings.cpp:95
GameCreationSettings::land_generator
byte land_generator
the landscape generator
Definition: settings_type.h:309
SetSettingValue
bool SetSettingValue(const IntSettingDesc *sd, int32 value, bool force_newgame)
Top function to save the new value of an element of the Settings struct.
Definition: settings.cpp:1524
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition: fileio_type.h:125
GetVarMemType
static VarType GetVarMemType(VarType type)
Get the NumberType of a setting.
Definition: saveload.h:1063
CmdChangeCompanySetting
CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
Change one of the per-company settings.
Definition: settings.cpp:1502
ScriptConfig::SettingsToString
std::string SettingsToString() const
Convert the custom settings to a string that can be stored in the config file or savegames.
Definition: script_config.cpp:208
IntSettingDesc::str
StringID str
(translated) string with descriptive text; gui and console
Definition: settings_internal.h:164
GRFConfig::next
struct GRFConfig * next
NOSAVE: Next item in the linked list.
Definition: newgrf_config.h:183
GameConfig::GetConfig
static GameConfig * GetConfig(ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: game_config.cpp:18
ScheduleErrorMessage
void ScheduleErrorMessage(const ErrorMessageData &data)
Schedule an error.
Definition: error_gui.cpp:457
IniSaveWindowSettings
void IniSaveWindowSettings(IniFile &ini, const char *grpname, void *desc)
Save a WindowDesc to config.
Definition: settings.cpp:791
DecodeHexNibble
static int DecodeHexNibble(char c)
Convert a character to a hex nibble value, or -1 otherwise.
Definition: settings.cpp:916
ListSettingDesc::def
const char * def
default value given when none is present
Definition: settings_internal.h:293
ScriptConfig::SSS_FORCE_NEWGAME
@ SSS_FORCE_NEWGAME
Get the newgame Script config.
Definition: script_config.hpp:104
SF_SCENEDIT_ONLY
@ SF_SCENEDIT_ONLY
This setting can only be changed in the scenario editor.
Definition: settings_internal.h:26
FGCM_NEWEST_VALID
@ FGCM_NEWEST_VALID
Find newest Grf, ignoring Grfs with GCF_INVALID set.
Definition: newgrf_config.h:202
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:535
SF_GUI_DROPDOWN
@ SF_GUI_DROPDOWN
The value represents a limited number of string-options (internally integer) presented as dropdown.
Definition: settings_internal.h:20
SaveLoad::version_from
SaveLoadVersion version_from
Save/load the variable starting from this savegame version.
Definition: saveload.h:658
IntSettingDesc::Read
int32 Read(const void *object) const
Read the integer from the the actual setting.
Definition: settings.cpp:513
company_func.h
CC_ERROR
static const TextColour CC_ERROR
Colour for error lines.
Definition: console_type.h:24
SLE_VAR_STRQ
@ SLE_VAR_STRQ
string pointer enclosed in quotes
Definition: saveload.h:590
ListSettingDesc::IsSameValue
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:725
SVS_NONE
@ SVS_NONE
Allow nothing and replace nothing.
Definition: string_type.h:49
IntSettingDesc::ParseValue
virtual size_t ParseValue(const char *str) const
Convert a string representation (external) of an integer-like setting to an integer.
Definition: settings.cpp:371
StringSettingDesc::Write
void Write(const void *object, const std::string &str) const
Write a string to the actual setting.
Definition: settings.cpp:542
IConsoleGetSetting
void IConsoleGetSetting(const char *name, bool force_newgame)
Output value of a specific setting to the console.
Definition: settings.cpp:1669
network.h
IntSettingDesc::max
uint32 max
maximum values
Definition: settings_internal.h:162
window_func.h
IniItem::name
std::string name
The name of this item.
Definition: ini_type.h:27
SF_SCENEDIT_TOO
@ SF_SCENEDIT_TOO
This setting can be changed in the scenario editor (only makes sense when SF_NEWGAME_ONLY is set).
Definition: settings_internal.h:25
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
StringSettingDesc::def
std::string def
Default value given when none is present.
Definition: settings_internal.h:269
_network_ban_list
StringList _network_ban_list
The banned clients.
Definition: network.cpp:67
SaveVersionInConfig
static void SaveVersionInConfig(IniFile &ini)
Save the version of OpenTTD to the ini file.
Definition: settings.cpp:1114
CMD_CHANGE_SETTING
@ CMD_CHANGE_SETTING
change a setting
Definition: command_type.h:309
IniGroup::item
IniItem * item
the first item in the group
Definition: ini_type.h:41
GetVariableAddress
static void * GetVariableAddress(const void *object, const SaveLoad &sld)
Get the address of the variable.
Definition: saveload.h:1094
IntSettingDesc::post_callback
PostChangeCallback * post_callback
Callback when the setting has been changed.
Definition: settings_internal.h:169
config.h
GetCompanySettingFromName
static const SettingDesc * GetCompanySettingFromName(std::string_view name)
Given a name of setting, return a company setting description of it.
Definition: settings.cpp:1432
ScriptConfig::Change
void Change(const char *name, int version=-1, bool force_exact_match=false, bool is_random=false)
Set another Script to be loaded in this slot.
Definition: script_config.cpp:19
ReadValue
int64 ReadValue(const void *ptr, VarType conv)
Return a signed-long version of the value of a setting.
Definition: saveload.cpp:804
ClientSettings::company
CompanySettings company
default values for per-company settings
Definition: settings_type.h:595
md5sumToString
char * md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
Convert the md5sum to a hexadecimal string representation.
Definition: string.cpp:553
Pool::PoolItem<&_company_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:326
INIFILE_VERSION
const uint16 INIFILE_VERSION
Current ini-file version of OpenTTD.
Definition: settings.cpp:163
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition: newgrf_config.h:199
IniGroup::GetItem
IniItem * GetItem(const std::string &name, bool create)
Get the item with the given name, and if it doesn't exist and create is true it creates a new item.
Definition: ini_load.cpp:91
SettingDesc::IsIntSetting
virtual bool IsIntSetting() const
Check whether this setting is an integer type setting.
Definition: settings_internal.h:97
_network_host_list
StringList _network_host_list
The servers we know.
Definition: network.cpp:66
GRFConfig::filename
char * filename
Filename - either with or without full path.
Definition: newgrf_config.h:165
console_func.h
BoolSettingDesc::FormatValue
void FormatValue(char *buf, const char *last, const void *object) const override
Format the value of the setting associated with this object.
Definition: settings.cpp:684
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
_config_file
std::string _config_file
Configuration file of OpenTTD.
Definition: settings.cpp:56
WC_ERRMSG
@ WC_ERRMSG
Error message; Window numbers:
Definition: window_type.h:102
CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY
static const uint CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY
Value for custom sea level in difficulty settings.
Definition: genworld.h:47
IniLoadFile::LoadFromDisk
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition: ini_load.cpp:215
Company
Definition: company_base.h:115
game_config.hpp
SetWindowClassesDirty
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition: window.cpp:3146
SF_NO_NETWORK
@ SF_NO_NETWORK
This setting does not apply to network games; it may not be changed during the game.
Definition: settings_internal.h:23
IniFileVersion
IniFileVersion
Ini-file versions.
Definition: settings.cpp:156
FillGRFDetails
bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir)
Find the GRFID of a given grf, and calculate its md5sum.
Definition: newgrf_config.cpp:368
ini_type.h
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:394
_settings_error_list
static ErrorList _settings_error_list
Errors while loading minimal settings.
Definition: settings.cpp:61
LoadFromConfig
void LoadFromConfig(bool startup)
Load the values from the configuration files.
Definition: settings.cpp:1202
CMD_CHANGE_COMPANY_SETTING
@ CMD_CHANGE_COMPANY_SETTING
change a company setting
Definition: command_type.h:310
SaveGRFPresetToConfig
void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
Save a NewGRF configuration with a preset name.
Definition: settings.cpp:1332
network_func.h
GetSaveLoadFromSettingTable
void GetSaveLoadFromSettingTable(SettingTable settings, std::vector< SaveLoad > &saveloads)
Get the SaveLoad for all settings in the settings table.
Definition: settings.cpp:1417
ScriptConfig::HasScript
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
Definition: script_config.cpp:162
IntSettingDesc::def
int32 def
default value given when none is present
Definition: settings_internal.h:160
LoadIntList
static bool LoadIntList(const char *str, void *array, int nelems, VarType type)
Load parsed string-values into an integer-array (intlist)
Definition: settings.cpp:272
SetDParamStr
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:296
_settings_newgame
GameSettings _settings_newgame
Game settings for new games (updated from the intro screen).
Definition: settings.cpp:54
GCF_STATIC
@ GCF_STATIC
GRF file is used statically (can be used in any MP game)
Definition: newgrf_config.h:25
OneOfManySettingDesc::ParseSingleValue
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:172
WL_CRITICAL
@ WL_CRITICAL
Critical errors, the MessageBox is shown in all cases.
Definition: error.h:25
debug.h
GRFConfig::param
uint32 param[0x80]
GRF parameters.
Definition: newgrf_config.h:176
_grfconfig_newgame
GRFConfig * _grfconfig_newgame
First item in list of default GRF set up.
Definition: newgrf_config.cpp:172
SettingDesc::AsIntSetting
const struct IntSettingDesc * AsIntSetting() const
Get the setting description of this setting as an integer setting.
Definition: settings.cpp:827
ai_config.hpp
IniLoadFile::GetGroup
IniGroup * GetGroup(const std::string &name, bool create_new=true)
Get the group with the given name.
Definition: ini_load.cpp:163
IntSettingDesc::FormatValue
void FormatValue(char *buf, const char *last, const void *object) const override
Format the value of the setting associated with this object.
Definition: settings.cpp:678
IniGroup::next
IniGroup * next
the next group within this file
Definition: ini_type.h:39
IConsolePrint
void IConsolePrint(TextColour colour_code, const std::string &string)
Handle the printing of text entered into the console or redirected there by any other means.
Definition: console.cpp:94