-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
settings.cpp
2834 lines (2438 loc) · 84.5 KB
/
settings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/**
* @file settings.cpp
* All actions handling saving and loading of the settings/configuration goes on in this file.
* The file consists of three parts:
* <ol>
* <li>Parsing the configuration file (openttd.cfg). This is achieved with the ini_ functions which
* handle various types, such as normal 'key = value' pairs, lists and value combinations of
* lists, strings, integers, 'bit'-masks and element selections.
* <li>Handle reading and writing to the setting-structures from inside the game either from
* the console for example or through the gui with CMD_ functions.
* <li>Handle saving/loading of the PATS chunk inside the savegame.
* </ol>
* @see SettingDesc
* @see SaveLoad
*/
#include "stdafx.h"
#include "currency.h"
#include "screenshot.h"
#include "network/network.h"
#include "network/network_func.h"
#include "settings_internal.h"
#include "command_func.h"
#include "console_func.h"
#include "pathfinder/pathfinder_type.h"
#include "genworld.h"
#include "train.h"
#include "news_func.h"
#include "window_func.h"
#include "sound_func.h"
#include "company_func.h"
#include "rev.h"
#if defined(WITH_FREETYPE) || defined(_WIN32)
#include "fontcache.h"
#endif
#include "textbuf_gui.h"
#include "rail_gui.h"
#include "elrail_func.h"
#include "error.h"
#include "town.h"
#include "video/video_driver.hpp"
#include "sound/sound_driver.hpp"
#include "music/music_driver.hpp"
#include "blitter/factory.hpp"
#include "base_media_base.h"
#include "gamelog.h"
#include "settings_func.h"
#include "ini_type.h"
#include "ai/ai_config.hpp"
#include "ai/ai.hpp"
#include "game/game_config.hpp"
#include "game/game.hpp"
#include "ship.h"
#include "smallmap_gui.h"
#include "roadveh.h"
#include "fios.h"
#include "strings_func.h"
#include "string_func.h"
#include "debug.h"
#include "zoning.h"
#include "void_map.h"
#include "station_base.h"
#include "infrastructure_func.h"
#if defined(WITH_FREETYPE) || defined(_WIN32)
#define HAS_TRUETYPE_FONT
#endif
#include "table/strings.h"
#include "table/settings.h"
#include <algorithm>
#include <vector>
#include "safeguards.h"
ClientSettings _settings_client;
GameSettings _settings_game; ///< Game settings of a running game or the scenario editor.
GameSettings _settings_newgame; ///< Game settings for new games (updated from the intro screen).
VehicleDefaultSettings _old_vds; ///< Used for loading default vehicles settings from old savegames
char *_config_file; ///< Configuration file of OpenTTD
typedef std::list<ErrorMessageData> ErrorList;
static ErrorList _settings_error_list; ///< Errors while loading minimal settings.
typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object);
typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList &list);
static bool IsSignedVarMemType(VarType vt);
/**
* Groups in openttd.cfg that are actually lists.
*/
static const char * const _list_group_names[] = {
"bans",
"newgrf",
"servers",
"server_bind_addresses",
nullptr
};
/**
* Find the index value of a ONEofMANY type in a string separated by |
* @param many full domain of values the ONEofMANY setting can have
* @param one the current value of the setting for which a value needs found
* @param onelen force calculation of the *one parameter
* @return the integer index of the full-list, or -1 if not found
*/
static size_t LookupOneOfMany(const char *many, const char *one, size_t onelen = 0)
{
const char *s;
size_t idx;
if (onelen == 0) onelen = strlen(one);
/* check if it's an integer */
if (*one >= '0' && *one <= '9') return strtoul(one, nullptr, 0);
idx = 0;
for (;;) {
/* find end of item */
s = many;
while (*s != '|' && *s != 0) s++;
if ((size_t)(s - many) == onelen && !memcmp(one, many, onelen)) return idx;
if (*s == 0) return (size_t)-1;
many = s + 1;
idx++;
}
}
/**
* Find the set-integer value MANYofMANY type in a string
* @param many full domain of values the MANYofMANY setting can have
* @param str the current string value of the setting, each individual
* of separated by a whitespace,tab or | character
* @return the 'fully' set integer, or -1 if a set is not found
*/
static size_t LookupManyOfMany(const char *many, const char *str)
{
const char *s;
size_t r;
size_t res = 0;
for (;;) {
/* skip "whitespace" */
while (*str == ' ' || *str == '\t' || *str == '|') str++;
if (*str == 0) break;
s = str;
while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++;
r = LookupOneOfMany(many, str, s - str);
if (r == (size_t)-1) return r;
SetBit(res, (uint8)r); // value found, set it
if (*s == 0) break;
str = s + 1;
}
return res;
}
/**
* Parse an integerlist string and set each found value
* @param p the string to be parsed. Each element in the list is separated by a
* comma or a space character
* @param items pointer to the integerlist-array that will be filled with values
* @param maxitems the maximum number of elements the integerlist-array has
* @return returns the number of items found, or -1 on an error
*/
static int ParseIntList(const char *p, int *items, int maxitems)
{
int n = 0; // number of items read so far
bool comma = false; // do we accept comma?
while (*p != '\0') {
switch (*p) {
case ',':
/* Do not accept multiple commas between numbers */
if (!comma) return -1;
comma = false;
FALLTHROUGH;
case ' ':
p++;
break;
default: {
if (n == maxitems) return -1; // we don't accept that many numbers
char *end;
long v = strtol(p, &end, 0);
if (p == end) return -1; // invalid character (not a number)
if (sizeof(int) < sizeof(long)) v = ClampToI32(v);
items[n++] = v;
p = end; // first non-number
comma = true; // we accept comma now
break;
}
}
}
/* If we have read comma but no number after it, fail.
* We have read comma when (n != 0) and comma is not allowed */
if (n != 0 && !comma) return -1;
return n;
}
/**
* Load parsed string-values into an integer-array (intlist)
* @param str the string that contains the values (and will be parsed)
* @param array pointer to the integer-arrays that will be filled
* @param nelems the number of elements the array holds. Maximum is 64 elements
* @param type the type of elements the array holds (eg INT8, UINT16, etc.)
* @return return true on success and false on error
*/
static bool LoadIntList(const char *str, void *array, int nelems, VarType type)
{
int items[64];
int i, nitems;
if (str == nullptr) {
memset(items, 0, sizeof(items));
nitems = nelems;
} else {
nitems = ParseIntList(str, items, lengthof(items));
if (nitems != nelems) return false;
}
switch (type) {
case SLE_VAR_BL:
case SLE_VAR_I8:
case SLE_VAR_U8:
for (i = 0; i != nitems; i++) ((byte*)array)[i] = items[i];
break;
case SLE_VAR_I16:
case SLE_VAR_U16:
for (i = 0; i != nitems; i++) ((uint16*)array)[i] = items[i];
break;
case SLE_VAR_I32:
case SLE_VAR_U32:
for (i = 0; i != nitems; i++) ((uint32*)array)[i] = items[i];
break;
default: NOT_REACHED();
}
return true;
}
/**
* Convert an integer-array (intlist) to a string representation. Each value
* is separated by a comma or a space character
* @param buf output buffer where the string-representation will be stored
* @param last last item to write to in the output buffer
* @param array pointer to the integer-arrays that is read from
* @param nelems the number of elements the array holds.
* @param type the type of elements the array holds (eg INT8, UINT16, etc.)
*/
static void MakeIntList(char *buf, const char *last, const void *array, int nelems, VarType type)
{
int i, v = 0;
const byte *p = (const byte *)array;
for (i = 0; i != nelems; i++) {
switch (type) {
case SLE_VAR_BL:
case SLE_VAR_I8: v = *(const int8 *)p; p += 1; break;
case SLE_VAR_U8: v = *(const uint8 *)p; p += 1; break;
case SLE_VAR_I16: v = *(const int16 *)p; p += 2; break;
case SLE_VAR_U16: v = *(const uint16 *)p; p += 2; break;
case SLE_VAR_I32: v = *(const int32 *)p; p += 4; break;
case SLE_VAR_U32: v = *(const uint32 *)p; p += 4; break;
default: NOT_REACHED();
}
buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
}
}
/**
* Convert a ONEofMANY structure to a string representation.
* @param buf output buffer where the string-representation will be stored
* @param last last item to write to in the output buffer
* @param many the full-domain string of possible values
* @param id the value of the variable and whose string-representation must be found
*/
static void MakeOneOfMany(char *buf, const char *last, const char *many, int id)
{
int orig_id = id;
/* Look for the id'th element */
while (--id >= 0) {
for (; *many != '|'; many++) {
if (*many == '\0') { // not found
seprintf(buf, last, "%d", orig_id);
return;
}
}
many++; // pass the |-character
}
/* copy string until next item (|) or the end of the list if this is the last one */
while (*many != '\0' && *many != '|' && buf < last) *buf++ = *many++;
*buf = '\0';
}
/**
* Convert a MANYofMANY structure to a string representation.
* @param buf output buffer where the string-representation will be stored
* @param last last item to write to in the output buffer
* @param many the full-domain string of possible values
* @param x the value of the variable and whose string-representation must
* be found in the bitmasked many string
*/
static void MakeManyOfMany(char *buf, const char *last, const char *many, uint32 x)
{
const char *start;
int i = 0;
bool init = true;
for (; x != 0; x >>= 1, i++) {
start = many;
while (*many != 0 && *many != '|') many++; // advance to the next element
if (HasBit(x, 0)) { // item found, copy it
if (!init) buf += seprintf(buf, last, "|");
init = false;
if (start == many) {
buf += seprintf(buf, last, "%d", i);
} else {
memcpy(buf, start, many - start);
buf += many - start;
}
}
if (*many == '|') many++;
}
*buf = '\0';
}
/**
* Convert a string representation (external) of a setting to the internal rep.
* @param desc SettingDesc struct that holds all information about the variable
* @param orig_str input string that will be parsed based on the type of desc
* @return return the parsed value of the setting
*/
static const void *StringToVal(const SettingDescBase *desc, const char *orig_str)
{
const char *str = orig_str == nullptr ? "" : orig_str;
switch (desc->cmd) {
case SDT_NUMX: {
char *end;
size_t val = strtoul(str, &end, 0);
if (end == str) {
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
msg.SetDParamStr(0, str);
msg.SetDParamStr(1, desc->name);
_settings_error_list.push_back(msg);
return desc->def;
}
if (*end != '\0') {
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_TRAILING_CHARACTERS);
msg.SetDParamStr(0, desc->name);
_settings_error_list.push_back(msg);
}
return (void*)val;
}
case SDT_ONEOFMANY: {
size_t r = LookupOneOfMany(desc->many, str);
/* if the first attempt of conversion from string to the appropriate value fails,
* look if we have defined a converter from old value to new value. */
if (r == (size_t)-1 && desc->proc_cnvt != nullptr) r = desc->proc_cnvt(str);
if (r != (size_t)-1) return (void*)r; // and here goes converted value
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
msg.SetDParamStr(0, str);
msg.SetDParamStr(1, desc->name);
_settings_error_list.push_back(msg);
return desc->def;
}
case SDT_MANYOFMANY: {
size_t r = LookupManyOfMany(desc->many, str);
if (r != (size_t)-1) return (void*)r;
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
msg.SetDParamStr(0, str);
msg.SetDParamStr(1, desc->name);
_settings_error_list.push_back(msg);
return desc->def;
}
case SDT_BOOLX: {
if (strcmp(str, "true") == 0 || strcmp(str, "on") == 0 || strcmp(str, "1") == 0) return (void*)true;
if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return (void*)false;
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
msg.SetDParamStr(0, str);
msg.SetDParamStr(1, desc->name);
_settings_error_list.push_back(msg);
return desc->def;
}
case SDT_STRING: return orig_str;
case SDT_INTLIST: return str;
default: break;
}
return nullptr;
}
static bool ValidateEnumSetting(const SettingDescBase *sdb, int32 val)
{
for (const SettingDescEnumEntry *enumlist = sdb->enumlist; enumlist != nullptr && enumlist->str != STR_NULL; enumlist++) {
if (enumlist->val == val) {
return true;
}
}
return false;
}
/**
* Set the value of a setting and if needed clamp the value to
* the preset minimum and maximum.
* @param ptr the variable itself
* @param sd pointer to the 'information'-database of the variable
* @param val signed long version of the new value
* @pre SettingDesc is of type SDT_BOOLX, SDT_NUMX,
* SDT_ONEOFMANY or SDT_MANYOFMANY. Other types are not supported as of now
*/
static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val)
{
const SettingDescBase *sdb = &sd->desc;
if (sdb->cmd != SDT_BOOLX &&
sdb->cmd != SDT_NUMX &&
sdb->cmd != SDT_ONEOFMANY &&
sdb->cmd != SDT_MANYOFMANY) {
return;
}
/* We cannot know the maximum value of a bitset variable, so just have faith */
if (sdb->cmd != SDT_MANYOFMANY) {
/* We need to take special care of the uint32 type as we receive from the function
* a signed integer. While here also bail out on 64-bit settings as those are not
* supported. Unsigned 8 and 16-bit variables are safe since they fit into a signed
* 32-bit variable
* TODO: Support 64-bit settings/variables */
switch (GetVarMemType(sd->save.conv)) {
case SLE_VAR_NULL: return;
case SLE_VAR_BL:
case SLE_VAR_I8:
case SLE_VAR_U8:
case SLE_VAR_I16:
case SLE_VAR_U16:
case SLE_VAR_I32: {
/* Override the minimum value. No value below sdb->min, except special value 0 */
if (!(sdb->flags & SGF_0ISDISABLED) || val != 0) {
if (sdb->flags & SGF_ENUM) {
if (!ValidateEnumSetting(sdb, val)) val = (int32)(size_t)sdb->def;
} else if (!(sdb->flags & SGF_MULTISTRING)) {
/* Clamp value-type setting to its valid range */
val = Clamp(val, sdb->min, sdb->max);
} else if (val < sdb->min || val > (int32)sdb->max) {
/* Reset invalid discrete setting (where different values change gameplay) to its default value */
val = (int32)(size_t)sdb->def;
}
}
break;
}
case SLE_VAR_U32: {
/* Override the minimum value. No value below sdb->min, except special value 0 */
uint32 uval = (uint32)val;
if (!(sdb->flags & SGF_0ISDISABLED) || uval != 0) {
if (sdb->flags & SGF_ENUM) {
if (!ValidateEnumSetting(sdb, val)) uval = (uint32)(size_t)sdb->def;
} else if (!(sdb->flags & SGF_MULTISTRING)) {
/* Clamp value-type setting to its valid range */
uval = ClampU(uval, sdb->min, sdb->max);
} else if (uval < (uint)sdb->min || uval > sdb->max) {
/* Reset invalid discrete setting to its default value */
uval = (uint32)(size_t)sdb->def;
}
}
WriteValue(ptr, SLE_VAR_U32, (int64)uval);
return;
}
case SLE_VAR_I64:
case SLE_VAR_U64:
default: NOT_REACHED();
}
}
WriteValue(ptr, sd->save.conv, (int64)val);
}
/**
* Load values from a group of an IniFile structure into the internal representation
* @param ini pointer to IniFile structure that holds administrative information
* @param sd pointer to SettingDesc structure whose internally pointed variables will
* be given values
* @param grpname the group of the IniFile to search in for the new values
* @param object pointer to the object been loaded
*/
static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
{
IniGroup *group;
IniGroup *group_def = ini->GetGroup(grpname);
IniItem *item;
const void *p;
void *ptr;
const char *s;
for (; sd->save.cmd != SL_END; sd++) {
const SettingDescBase *sdb = &sd->desc;
const SaveLoad *sld = &sd->save;
if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to, sld->ext_feature_test)) continue;
if (sdb->flags & SGF_NO_NEWGAME) {
item = nullptr;
} else {
/* For settings.xx.yy load the settings from [xx] yy = ? */
s = strchr(sdb->name, '.');
if (s != nullptr) {
group = ini->GetGroup(sdb->name, s - sdb->name);
s++;
} else {
s = sdb->name;
group = group_def;
}
item = group->GetItem(s, false);
if (item == nullptr && group != group_def) {
/* For settings.xx.yy load the settings from [settingss] yy = ? in case the previous
* did not exist (e.g. loading old config files with a [settings] section */
item = group_def->GetItem(s, false);
}
if (item == nullptr) {
/* For settings.xx.zz.yy load the settings from [zz] yy = ? in case the previous
* did not exist (e.g. loading old config files with a [yapf] section */
const char *sc = strchr(s, '.');
if (sc != nullptr) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
}
}
p = (item == nullptr) ? sdb->def : StringToVal(sdb, item->value);
ptr = GetVariableAddress(object, sld);
switch (sdb->cmd) {
case SDT_BOOLX: // All four are various types of (integer) numbers
case SDT_NUMX:
case SDT_ONEOFMANY:
case SDT_MANYOFMANY:
Write_ValidateSetting(ptr, sd, (int32)(size_t)p);
break;
case SDT_STRING:
switch (GetVarMemType(sld->conv)) {
case SLE_VAR_STRB:
case SLE_VAR_STRBQ:
if (p != nullptr) strecpy((char*)ptr, (const char*)p, (char*)ptr + sld->length - 1);
break;
case SLE_VAR_STR:
case SLE_VAR_STRQ:
free(*(char**)ptr);
*(char**)ptr = p == nullptr ? nullptr : stredup((const char*)p);
break;
case SLE_VAR_CHAR: if (p != nullptr) *(char *)ptr = *(const char *)p; break;
default: NOT_REACHED();
}
break;
case SDT_INTLIST: {
if (!LoadIntList((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY);
msg.SetDParamStr(0, sdb->name);
_settings_error_list.push_back(msg);
/* Use default */
LoadIntList((const char*)sdb->def, ptr, sld->length, GetVarMemType(sld->conv));
} else if (sd->desc.proc_cnvt != nullptr) {
sd->desc.proc_cnvt((const char*)p);
}
break;
}
default: NOT_REACHED();
}
}
}
/**
* Save the values of settings to the inifile.
* @param ini pointer to IniFile structure
* @param sd read-only SettingDesc structure which contains the unmodified,
* loaded values of the configuration file and various information about it
* @param grpname holds the name of the group (eg. [network]) where these will be saved
* @param object pointer to the object been saved
* The function works as follows: for each item in the SettingDesc structure we
* have a look if the value has changed since we started the game (the original
* values are reloaded when saving). If settings indeed have changed, we get
* these and save them.
*/
static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
{
IniGroup *group_def = nullptr, *group;
IniItem *item;
char buf[512];
const char *s;
void *ptr;
for (; sd->save.cmd != SL_END; sd++) {
const SettingDescBase *sdb = &sd->desc;
const SaveLoad *sld = &sd->save;
/* If the setting is not saved to the configuration
* file, just continue with the next setting */
if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to, sld->ext_feature_test)) continue;
if (sld->conv & SLF_NOT_IN_CONFIG) continue;
if (sdb->flags & SGF_NO_NEWGAME) continue;
/* XXX - wtf is this?? (group override?) */
s = strchr(sdb->name, '.');
if (s != nullptr) {
group = ini->GetGroup(sdb->name, s - sdb->name);
s++;
} else {
if (group_def == nullptr) group_def = ini->GetGroup(grpname);
s = sdb->name;
group = group_def;
}
item = group->GetItem(s, true);
ptr = GetVariableAddress(object, sld);
if (item->value != nullptr) {
/* check if the value is the same as the old value */
const void *p = StringToVal(sdb, item->value);
/* The main type of a variable/setting is in bytes 8-15
* The subtype (what kind of numbers do we have there) is in 0-7 */
switch (sdb->cmd) {
case SDT_BOOLX:
case SDT_NUMX:
case SDT_ONEOFMANY:
case SDT_MANYOFMANY:
switch (GetVarMemType(sld->conv)) {
case SLE_VAR_BL:
if (*(bool*)ptr == (p != nullptr)) continue;
break;
case SLE_VAR_I8:
case SLE_VAR_U8:
if (*(byte*)ptr == (byte)(size_t)p) continue;
break;
case SLE_VAR_I16:
case SLE_VAR_U16:
if (*(uint16*)ptr == (uint16)(size_t)p) continue;
break;
case SLE_VAR_I32:
case SLE_VAR_U32:
if (*(uint32*)ptr == (uint32)(size_t)p) continue;
break;
default: NOT_REACHED();
}
break;
default: break; // Assume the other types are always changed
}
}
/* Value has changed, get the new value and put it into a buffer */
switch (sdb->cmd) {
case SDT_BOOLX:
case SDT_NUMX:
case SDT_ONEOFMANY:
case SDT_MANYOFMANY: {
uint32 i = (uint32)ReadValue(ptr, sld->conv);
switch (sdb->cmd) {
case SDT_BOOLX: strecpy(buf, (i != 0) ? "true" : "false", lastof(buf)); break;
case SDT_NUMX: seprintf(buf, lastof(buf), IsSignedVarMemType(sld->conv) ? "%d" : "%u", i); break;
case SDT_ONEOFMANY: MakeOneOfMany(buf, lastof(buf), sdb->many, i); break;
case SDT_MANYOFMANY: MakeManyOfMany(buf, lastof(buf), sdb->many, i); break;
default: NOT_REACHED();
}
break;
}
case SDT_STRING:
switch (GetVarMemType(sld->conv)) {
case SLE_VAR_STRB: strecpy(buf, (char*)ptr, lastof(buf)); break;
case SLE_VAR_STRBQ:seprintf(buf, lastof(buf), "\"%s\"", (char*)ptr); break;
case SLE_VAR_STR: strecpy(buf, *(char**)ptr, lastof(buf)); break;
case SLE_VAR_STRQ:
if (*(char**)ptr == nullptr) {
buf[0] = '\0';
} else {
seprintf(buf, lastof(buf), "\"%s\"", *(char**)ptr);
}
break;
case SLE_VAR_CHAR: buf[0] = *(char*)ptr; buf[1] = '\0'; break;
default: NOT_REACHED();
}
break;
case SDT_INTLIST:
MakeIntList(buf, lastof(buf), ptr, sld->length, GetVarMemType(sld->conv));
break;
default: NOT_REACHED();
}
/* The value is different, that means we have to write it to the ini */
free(item->value);
item->value = stredup(buf);
}
}
/**
* Loads all items from a 'grpname' section into a list
* The list parameter can be a nullptr pointer, in this case nothing will be
* saved and a callback function should be defined that will take over the
* list-handling and store the data itself somewhere.
* @param ini IniFile handle to the ini file with the source data
* @param grpname character string identifying the section-header of the ini file that will be parsed
* @param list new list with entries of the given section
*/
static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList &list)
{
IniGroup *group = ini->GetGroup(grpname);
if (group == nullptr) return;
list.clear();
for (const IniItem *item = group->item; item != nullptr; item = item->next) {
if (item->name != nullptr) list.emplace_back(item->name);
}
}
/**
* Saves all items from a list into the 'grpname' section
* The list parameter can be a nullptr pointer, in this case a callback function
* should be defined that will provide the source data to be saved.
* @param ini IniFile handle to the ini file where the destination data is saved
* @param grpname character string identifying the section-header of the ini file
* @param list pointer to an string(pointer) array that will be used as the
* source to be saved into the relevant ini section
*/
static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList &list)
{
IniGroup *group = ini->GetGroup(grpname);
if (group == nullptr) return;
group->Clear();
for (const auto &iter : list) {
group->GetItem(iter.c_str(), true)->SetValue("");
}
}
/**
* Load a WindowDesc from config.
* @param ini IniFile handle to the ini file with the source data
* @param grpname character string identifying the section-header of the ini file that will be parsed
* @param desc Destination WindowDesc
*/
void IniLoadWindowSettings(IniFile *ini, const char *grpname, void *desc)
{
IniLoadSettings(ini, _window_settings, grpname, desc);
}
/**
* Save a WindowDesc to config.
* @param ini IniFile handle to the ini file where the destination data is saved
* @param grpname character string identifying the section-header of the ini file
* @param desc Source WindowDesc
*/
void IniSaveWindowSettings(IniFile *ini, const char *grpname, void *desc)
{
IniSaveSettings(ini, _window_settings, grpname, desc);
}
/**
* Check whether the setting is editable in the current gamemode.
* @param do_command true if this is about checking a command from the server.
* @return true if editable.
*/
bool SettingDesc::IsEditable(bool do_command) const
{
if (!do_command && !(this->save.conv & SLF_NO_NETWORK_SYNC) && _networking && !(_network_server || _network_settings_access) && !(this->desc.flags & SGF_PER_COMPANY)) return false;
if ((this->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return false;
if ((this->desc.flags & SGF_NO_NETWORK) && _networking) return false;
if ((this->desc.flags & SGF_NEWGAME_ONLY) &&
(_game_mode == GM_NORMAL ||
(_game_mode == GM_EDITOR && !(this->desc.flags & SGF_SCENEDIT_TOO)))) return false;
return true;
}
/**
* Return the type of the setting.
* @return type of setting
*/
SettingType SettingDesc::GetType() const
{
if (this->desc.flags & SGF_PER_COMPANY) return ST_COMPANY;
return (this->save.conv & SLF_NOT_IN_SAVE) ? ST_CLIENT : ST_GAME;
}
/* Begin - Callback Functions for the various settings. */
/** Reposition the main toolbar as the setting changed. */
static bool v_PositionMainToolbar(int32 p1)
{
if (_game_mode != GM_MENU) PositionMainToolbar(nullptr);
return true;
}
/** Reposition the statusbar as the setting changed. */
static bool v_PositionStatusbar(int32 p1)
{
if (_game_mode != GM_MENU) {
PositionStatusbar(nullptr);
PositionNewsMessage(nullptr);
PositionNetworkChatWindow(nullptr);
}
return true;
}
static bool PopulationInLabelActive(int32 p1)
{
UpdateAllTownVirtCoords();
return true;
}
static bool RedrawScreen(int32 p1)
{
MarkWholeScreenDirty();
return true;
}
/**
* Redraw the smallmap after a colour scheme change.
* @param p1 Callback parameter.
* @return Always true.
*/
static bool RedrawSmallmap(int32 p1)
{
BuildLandLegend();
BuildOwnerLegend();
SetWindowClassesDirty(WC_SMALLMAP);
return true;
}
static bool InvalidateDetailsWindow(int32 p1)
{
SetWindowClassesDirty(WC_VEHICLE_DETAILS);
return true;
}
static bool StationSpreadChanged(int32 p1)
{
InvalidateWindowData(WC_SELECT_STATION, 0);
InvalidateWindowData(WC_BUILD_STATION, 0);
return true;
}
static bool InvalidateBuildIndustryWindow(int32 p1)
{
InvalidateWindowData(WC_BUILD_INDUSTRY, 0);
return true;
}
static bool CloseSignalGUI(int32 p1)
{
if (p1 == 0) {
DeleteWindowByClass(WC_BUILD_SIGNAL);
}
return true;
}
static bool InvalidateTownViewWindow(int32 p1)
{
InvalidateWindowClassesData(WC_TOWN_VIEW, p1);
return true;
}
static bool DeleteSelectStationWindow(int32 p1)
{
DeleteWindowById(WC_SELECT_STATION, 0);
return true;
}
static bool UpdateConsists(int32 p1)
{
for (Train *t : Train::Iterate()) {
/* Update the consist of all trains so the maximum speed is set correctly. */
if (t->IsFrontEngine() || t->IsFreeWagon()) t->ConsistChanged(CCF_TRACK);
}
InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
return true;
}
/* Check service intervals of vehicles, p1 is value of % or day based servicing */
static bool CheckInterval(int32 p1)
{
bool update_vehicles;
VehicleDefaultSettings *vds;
if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
vds = &_settings_client.company.vehicle;
update_vehicles = false;
} else {
vds = &Company::Get(_current_company)->settings.vehicle;
update_vehicles = true;
}
if (p1 != 0) {
vds->servint_trains = 50;
vds->servint_roadveh = 50;
vds->servint_aircraft = 50;
vds->servint_ships = 50;
} else {
vds->servint_trains = 150;
vds->servint_roadveh = 150;
vds->servint_aircraft = 100;
vds->servint_ships = 360;
}
if (update_vehicles) {
const Company *c = Company::Get(_current_company);
for (Vehicle *v : Vehicle::Iterate()) {
if (v->owner == _current_company && v->IsPrimaryVehicle() && !v->ServiceIntervalIsCustom()) {
v->SetServiceInterval(CompanyServiceInterval(c, v->type));
v->SetServiceIntervalIsPercent(p1 != 0);
}
}
}
InvalidateDetailsWindow(0);
return true;
}
static bool UpdateInterval(VehicleType type, int32 p1)
{
bool update_vehicles;
VehicleDefaultSettings *vds;
if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
vds = &_settings_client.company.vehicle;
update_vehicles = false;
} else {
vds = &Company::Get(_current_company)->settings.vehicle;
update_vehicles = true;
}
/* Test if the interval is valid */
uint16 interval = GetServiceIntervalClamped(p1, vds->servint_ispercent);
if (interval != p1) return false;
if (update_vehicles) {
for (Vehicle *v : Vehicle::Iterate()) {
if (v->owner == _current_company && v->type == type && v->IsPrimaryVehicle() && !v->ServiceIntervalIsCustom()) {
v->SetServiceInterval(p1);
}
}
}
InvalidateDetailsWindow(0);
return true;
}
static bool UpdateIntervalTrains(int32 p1)
{
return UpdateInterval(VEH_TRAIN, p1);
}
static bool UpdateIntervalRoadVeh(int32 p1)
{
return UpdateInterval(VEH_ROAD, p1);