# settings.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00026 #include "stdafx.h"
    00027 #include "currency.h"
    00028 #include "screenshot.h"
    00029 #include "network/network.h"
    00030 #include "network/network_func.h"
    00031 #include "settings_internal.h"
    00032 #include "command_func.h"
    00033 #include "console_func.h"
    00034 #include "pathfinder/pathfinder_type.h"
    00035 #include "genworld.h"
    00036 #include "train.h"
    00037 #include "news_func.h"
    00038 #include "window_func.h"
    00039 #include "sound_func.h"
    00040 #include "company_func.h"
    00041 #include "rev.h"
    00042 #ifdef WITH_FREETYPE
    00043 #include "fontcache.h"
    00044 #endif
    00045 #include "textbuf_gui.h"
    00046 #include "rail_gui.h"
    00047 #include "elrail_func.h"
    00048 #include "error.h"
    00049 #include "town.h"
    00050 #include "video/video_driver.hpp"
    00051 #include "sound/sound_driver.hpp"
    00052 #include "music/music_driver.hpp"
    00053 #include "blitter/factory.hpp"
    00054 #include "base_media_base.h"
    00055 #include "gamelog.h"
    00056 #include "settings_func.h"
    00057 #include "ini_type.h"
    00058 #include "ai/ai_config.hpp"
    00059 #include "ai/ai.hpp"
    00060 #include "game/game_config.hpp"
    00061 #include "game/game.hpp"
    00062 #include "ship.h"
    00063 #include "smallmap_gui.h"
    00064 #include "roadveh.h"
    00065 #include "fios.h"
    00066 #include "strings_func.h"
    00067
    00068 #include "void_map.h"
    00069 #include "station_base.h"
    00070
    00071 #include "table/strings.h"
    00072 #include "table/settings.h"
    00073
    00074 ClientSettings _settings_client;
    00075 GameSettings _settings_game;
    00076 GameSettings _settings_newgame;
    00077 VehicleDefaultSettings _old_vds;
    00078 char *_config_file;
    00079
    00080 typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object);
    00081 typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList *list);
    00082
    00083 static bool IsSignedVarMemType(VarType vt);
    00084
    00088 static const char * const _list_group_names[] = {
    00089   "bans",
    00090   "newgrf",
    00091   "servers",
    00092   "server_bind_addresses",
    00093   NULL
    00094 };
    00095
    00103 static size_t LookupOneOfMany(const char *many, const char *one, size_t onelen = 0)
    00104 {
    00105   const char *s;
    00106   size_t idx;
    00107
    00108   if (onelen == 0) onelen = strlen(one);
    00109
    00110   /* check if it's an integer */
    00111   if (*one >= '0' && *one <= '9') return strtoul(one, NULL, 0);
    00112
    00113   idx = 0;
    00114   for (;;) {
    00115     /* find end of item */
    00116     s = many;
    00117     while (*s != '|' && *s != 0) s++;
    00118     if ((size_t)(s - many) == onelen && !memcmp(one, many, onelen)) return idx;
    00119     if (*s == 0) return (size_t)-1;
    00120     many = s + 1;
    00121     idx++;
    00122   }
    00123 }
    00124
    00132 static size_t LookupManyOfMany(const char *many, const char *str)
    00133 {
    00134   const char *s;
    00135   size_t r;
    00136   size_t res = 0;
    00137
    00138   for (;;) {
    00139     /* skip "whitespace" */
    00140     while (*str == ' ' || *str == '\t' || *str == '|') str++;
    00141     if (*str == 0) break;
    00142
    00143     s = str;
    00144     while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++;
    00145
    00146     r = LookupOneOfMany(many, str, s - str);
    00147     if (r == (size_t)-1) return r;
    00148
    00149     SetBit(res, (uint8)r); // value found, set it
    00150     if (*s == 0) break;
    00151     str = s + 1;
    00152   }
    00153   return res;
    00154 }
    00155
    00164 static int ParseIntList(const char *p, int *items, int maxitems)
    00165 {
    00166   int n = 0; // number of items read so far
    00167   bool comma = false; // do we accept comma?
    00168
    00169   while (*p != '\0') {
    00170     switch (*p) {
    00171       case ',':
    00172         /* Do not accept multiple commas between numbers */
    00173         if (!comma) return -1;
    00174         comma = false;
    00175         /* FALL THROUGH */
    00176       case ' ':
    00177         p++;
    00178         break;
    00179
    00180       default: {
    00181         if (n == maxitems) return -1; // we don't accept that many numbers
    00182         char *end;
    00183         long v = strtol(p, &end, 0);
    00184         if (p == end) return -1; // invalid character (not a number)
    00185         if (sizeof(int) < sizeof(long)) v = ClampToI32(v);
    00186         items[n++] = v;
    00187         p = end; // first non-number
    00188         comma = true; // we accept comma now
    00189         break;
    00190       }
    00191     }
    00192   }
    00193
    00194   /* If we have read comma but no number after it, fail.
    00195    * We have read comma when (n != 0) and comma is not allowed */
    00196   if (n != 0 && !comma) return -1;
    00197
    00198   return n;
    00199 }
    00200
    00209 static bool LoadIntList(const char *str, void *array, int nelems, VarType type)
    00210 {
    00211   int items[64];
    00212   int i, nitems;
    00213
    00214   if (str == NULL) {
    00215     memset(items, 0, sizeof(items));
    00216     nitems = nelems;
    00217   } else {
    00218     nitems = ParseIntList(str, items, lengthof(items));
    00219     if (nitems != nelems) return false;
    00220   }
    00221
    00222   switch (type) {
    00223   case SLE_VAR_BL:
    00224   case SLE_VAR_I8:
    00225   case SLE_VAR_U8:
    00226     for (i = 0; i != nitems; i++) ((byte*)array)[i] = items[i];
    00227     break;
    00228   case SLE_VAR_I16:
    00229   case SLE_VAR_U16:
    00230     for (i = 0; i != nitems; i++) ((uint16*)array)[i] = items[i];
    00231     break;
    00232   case SLE_VAR_I32:
    00233   case SLE_VAR_U32:
    00234     for (i = 0; i != nitems; i++) ((uint32*)array)[i] = items[i];
    00235     break;
    00236   default: NOT_REACHED();
    00237   }
    00238
    00239   return true;
    00240 }
    00241
    00251 static void MakeIntList(char *buf, const char *last, const void *array, int nelems, VarType type)
    00252 {
    00253   int i, v = 0;
    00254   const byte *p = (const byte *)array;
    00255
    00256   for (i = 0; i != nelems; i++) {
    00257     switch (type) {
    00258     case SLE_VAR_BL:
    00259     case SLE_VAR_I8:  v = *(const   int8 *)p; p += 1; break;
    00260     case SLE_VAR_U8:  v = *(const  uint8 *)p; p += 1; break;
    00261     case SLE_VAR_I16: v = *(const  int16 *)p; p += 2; break;
    00262     case SLE_VAR_U16: v = *(const uint16 *)p; p += 2; break;
    00263     case SLE_VAR_I32: v = *(const  int32 *)p; p += 4; break;
    00264     case SLE_VAR_U32: v = *(const uint32 *)p; p += 4; break;
    00265     default: NOT_REACHED();
    00266     }
    00267     buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
    00268   }
    00269 }
    00270
    00278 static void MakeOneOfMany(char *buf, const char *last, const char *many, int id)
    00279 {
    00280   int orig_id = id;
    00281
    00282   /* Look for the id'th element */
    00283   while (--id >= 0) {
    00284     for (; *many != '|'; many++) {
    00285       if (*many == '\0') { // not found
    00286         seprintf(buf, last, "%d", orig_id);
    00287         return;
    00288       }
    00289     }
    00290     many++; // pass the |-character
    00291   }
    00292
    00293   /* copy string until next item (|) or the end of the list if this is the last one */
    00294   while (*many != '\0' && *many != '|' && buf < last) *buf++ = *many++;
    00295   *buf = '\0';
    00296 }
    00297
    00306 static void MakeManyOfMany(char *buf, const char *last, const char *many, uint32 x)
    00307 {
    00308   const char *start;
    00309   int i = 0;
    00310   bool init = true;
    00311
    00312   for (; x != 0; x >>= 1, i++) {
    00313     start = many;
    00314     while (*many != 0 && *many != '|') many++; // advance to the next element
    00315
    00316     if (HasBit(x, 0)) { // item found, copy it
    00317       if (!init) buf += seprintf(buf, last, "|");
    00318       init = false;
    00319       if (start == many) {
    00320         buf += seprintf(buf, last, "%d", i);
    00321       } else {
    00322         memcpy(buf, start, many - start);
    00323         buf += many - start;
    00324       }
    00325     }
    00326
    00327     if (*many == '|') many++;
    00328   }
    00329
    00330   *buf = '\0';
    00331 }
    00332
    00339 static const void *StringToVal(const SettingDescBase *desc, const char *orig_str)
    00340 {
    00341   const char *str = orig_str == NULL ? "" : orig_str;
    00342   switch (desc->cmd) {
    00343   case SDT_NUMX: {
    00344     char *end;
    00345     size_t val = strtoul(str, &end, 0);
    00346     if (*end != '\0') {
    00347       SetDParamStr(0, desc->name);
    00348       ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_TRAILING_CHARACTERS, WL_CRITICAL);
    00349     }
    00350     return (void*)val;
    00351   }
    00352   case SDT_ONEOFMANY: {
    00353     size_t r = LookupOneOfMany(desc->many, str);
    00354     /* if the first attempt of conversion from string to the appropriate value fails,
    00355      * look if we have defined a converter from old value to new value. */
    00356     if (r == (size_t)-1 && desc->proc_cnvt != NULL) r = desc->proc_cnvt(str);
    00357     if (r != (size_t)-1) return (void*)r; // and here goes converted value
    00358
    00359     SetDParamStr(0, str);
    00360     SetDParamStr(1, desc->name);
    00361     ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE, WL_CRITICAL);
    00362     return 0;
    00363   }
    00364   case SDT_MANYOFMANY: {
    00365     size_t r = LookupManyOfMany(desc->many, str);
    00366     if (r != (size_t)-1) return (void*)r;
    00367     SetDParamStr(0, str);
    00368     SetDParamStr(1, desc->name);
    00369     ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE, WL_CRITICAL);
    00370     return NULL;
    00371   }
    00372   case SDT_BOOLX:
    00373     if (strcmp(str, "true")  == 0 || strcmp(str, "on")  == 0 || strcmp(str, "1") == 0) return (void*)true;
    00374     if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return (void*)false;
    00375
    00376     SetDParamStr(0, str);
    00377     SetDParamStr(1, desc->name);
    00378     ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE, WL_CRITICAL);
    00379     break;
    00380
    00381   case SDT_STRING: return orig_str;
    00382   case SDT_INTLIST: return str;
    00383   default: break;
    00384   }
    00385
    00386   return NULL;
    00387 }
    00388
    00398 static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val)
    00399 {
    00400   const SettingDescBase *sdb = &sd->desc;
    00401
    00402   if (sdb->cmd != SDT_BOOLX &&
    00403       sdb->cmd != SDT_NUMX &&
    00404       sdb->cmd != SDT_ONEOFMANY &&
    00405       sdb->cmd != SDT_MANYOFMANY) {
    00406     return;
    00407   }
    00408
    00409   /* We cannot know the maximum value of a bitset variable, so just have faith */
    00410   if (sdb->cmd != SDT_MANYOFMANY) {
    00411     /* We need to take special care of the uint32 type as we receive from the function
    00412      * a signed integer. While here also bail out on 64-bit settings as those are not
    00413      * supported. Unsigned 8 and 16-bit variables are safe since they fit into a signed
    00414      * 32-bit variable
    00415      * TODO: Support 64-bit settings/variables */
    00416     switch (GetVarMemType(sd->save.conv)) {
    00417       case SLE_VAR_NULL: return;
    00418       case SLE_VAR_BL:
    00419       case SLE_VAR_I8:
    00420       case SLE_VAR_U8:
    00421       case SLE_VAR_I16:
    00422       case SLE_VAR_U16:
    00423       case SLE_VAR_I32: {
    00424         /* Override the minimum value. No value below sdb->min, except special value 0 */
    00425         if (!(sdb->flags & SGF_0ISDISABLED) || val != 0) val = Clamp(val, sdb->min, sdb->max);
    00426         break;
    00427       }
    00428       case SLE_VAR_U32: {
    00429         /* Override the minimum value. No value below sdb->min, except special value 0 */
    00430         uint min = ((sdb->flags & SGF_0ISDISABLED) && (uint)val <= (uint)sdb->min) ? 0 : sdb->min;
    00431         WriteValue(ptr, SLE_VAR_U32, (int64)ClampU(val, min, sdb->max));
    00432         return;
    00433       }
    00434       case SLE_VAR_I64:
    00435       case SLE_VAR_U64:
    00436       default: NOT_REACHED();
    00437     }
    00438   }
    00439
    00440   WriteValue(ptr, sd->save.conv, (int64)val);
    00441 }
    00442
    00451 static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
    00452 {
    00453   IniGroup *group;
    00454   IniGroup *group_def = ini->GetGroup(grpname);
    00455   IniItem *item;
    00456   const void *p;
    00457   void *ptr;
    00458   const char *s;
    00459
    00460   for (; sd->save.cmd != SL_END; sd++) {
    00461     const SettingDescBase *sdb = &sd->desc;
    00462     const SaveLoad        *sld = &sd->save;
    00463
    00464     if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
    00465
    00466     /* For settings.xx.yy load the settings from [xx] yy = ? */
    00467     s = strchr(sdb->name, '.');
    00468     if (s != NULL) {
    00469       group = ini->GetGroup(sdb->name, s - sdb->name);
    00470       s++;
    00471     } else {
    00472       s = sdb->name;
    00473       group = group_def;
    00474     }
    00475
    00476     item = group->GetItem(s, false);
    00477     if (item == NULL && group != group_def) {
    00478       /* For settings.xx.yy load the settings from [settingss] yy = ? in case the previous
    00479        * did not exist (e.g. loading old config files with a [settings] section */
    00480       item = group_def->GetItem(s, false);
    00481     }
    00482     if (item == NULL) {
    00483       /* For settings.xx.zz.yy load the settings from [zz] yy = ? in case the previous
    00484        * did not exist (e.g. loading old config files with a [yapf] section */
    00485       const char *sc = strchr(s, '.');
    00486       if (sc != NULL) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
    00487     }
    00488
    00489     p = (item == NULL) ? sdb->def : StringToVal(sdb, item->value);
    00490     ptr = GetVariableAddress(object, sld);
    00491
    00492     switch (sdb->cmd) {
    00493     case SDT_BOOLX: // All four are various types of (integer) numbers
    00494     case SDT_NUMX:
    00495     case SDT_ONEOFMANY:
    00496     case SDT_MANYOFMANY:
    00497       Write_ValidateSetting(ptr, sd, (int32)(size_t)p); break;
    00498
    00499     case SDT_STRING:
    00500       switch (GetVarMemType(sld->conv)) {
    00501         case SLE_VAR_STRB:
    00502         case SLE_VAR_STRBQ:
    00503           if (p != NULL) ttd_strlcpy((char*)ptr, (const char*)p, sld->length);
    00504           break;
    00505         case SLE_VAR_STR:
    00506         case SLE_VAR_STRQ:
    00507           free(*(char**)ptr);
    00508           *(char**)ptr = p == NULL ? NULL : strdup((const char*)p);
    00509           break;
    00510         case SLE_VAR_CHAR: if (p != NULL) *(char *)ptr = *(const char *)p; break;
    00511         default: NOT_REACHED();
    00512       }
    00513       break;
    00514
    00515     case SDT_INTLIST: {
    00516       if (!LoadIntList((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
    00517         SetDParamStr(0, sdb->name);
    00518         ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY, WL_CRITICAL);
    00519       } else if (sd->desc.proc_cnvt != NULL) {
    00520         sd->desc.proc_cnvt((const char*)p);
    00521       }
    00522       break;
    00523     }
    00524     default: NOT_REACHED();
    00525     }
    00526   }
    00527 }
    00528
    00541 static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
    00542 {
    00543   IniGroup *group_def = NULL, *group;
    00544   IniItem *item;
    00545   char buf[512];
    00546   const char *s;
    00547   void *ptr;
    00548
    00549   for (; sd->save.cmd != SL_END; sd++) {
    00550     const SettingDescBase *sdb = &sd->desc;
    00551     const SaveLoad        *sld = &sd->save;
    00552
    00553     /* If the setting is not saved to the configuration
    00554      * file, just continue with the next setting */
    00555     if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
    00556     if (sld->conv & SLF_NOT_IN_CONFIG) continue;
    00557
    00558     /* XXX - wtf is this?? (group override?) */
    00559     s = strchr(sdb->name, '.');
    00560     if (s != NULL) {
    00561       group = ini->GetGroup(sdb->name, s - sdb->name);
    00562       s++;
    00563     } else {
    00564       if (group_def == NULL) group_def = ini->GetGroup(grpname);
    00565       s = sdb->name;
    00566       group = group_def;
    00567     }
    00568
    00569     item = group->GetItem(s, true);
    00570     ptr = GetVariableAddress(object, sld);
    00571
    00572     if (item->value != NULL) {
    00573       /* check if the value is the same as the old value */
    00574       const void *p = StringToVal(sdb, item->value);
    00575
    00576       /* The main type of a variable/setting is in bytes 8-15
    00577        * The subtype (what kind of numbers do we have there) is in 0-7 */
    00578       switch (sdb->cmd) {
    00579       case SDT_BOOLX:
    00580       case SDT_NUMX:
    00581       case SDT_ONEOFMANY:
    00582       case SDT_MANYOFMANY:
    00583         switch (GetVarMemType(sld->conv)) {
    00584         case SLE_VAR_BL:
    00585           if (*(bool*)ptr == (p != NULL)) continue;
    00586           break;
    00587         case SLE_VAR_I8:
    00588         case SLE_VAR_U8:
    00589           if (*(byte*)ptr == (byte)(size_t)p) continue;
    00590           break;
    00591         case SLE_VAR_I16:
    00592         case SLE_VAR_U16:
    00593           if (*(uint16*)ptr == (uint16)(size_t)p) continue;
    00594           break;
    00595         case SLE_VAR_I32:
    00596         case SLE_VAR_U32:
    00597           if (*(uint32*)ptr == (uint32)(size_t)p) continue;
    00598           break;
    00599         default: NOT_REACHED();
    00600         }
    00601         break;
    00602       default: break; // Assume the other types are always changed
    00603       }
    00604     }
    00605
    00606     /* Value has changed, get the new value and put it into a buffer */
    00607     switch (sdb->cmd) {
    00608     case SDT_BOOLX:
    00609     case SDT_NUMX:
    00610     case SDT_ONEOFMANY:
    00611     case SDT_MANYOFMANY: {
    00612       uint32 i = (uint32)ReadValue(ptr, sld->conv);
    00613
    00614       switch (sdb->cmd) {
    00615       case SDT_BOOLX:      strecpy(buf, (i != 0) ? "true" : "false", lastof(buf)); break;
    00616       case SDT_NUMX:       seprintf(buf, lastof(buf), IsSignedVarMemType(sld->conv) ? "%d" : "%u", i); break;
    00617       case SDT_ONEOFMANY:  MakeOneOfMany(buf, lastof(buf), sdb->many, i); break;
    00618       case SDT_MANYOFMANY: MakeManyOfMany(buf, lastof(buf), sdb->many, i); break;
    00619       default: NOT_REACHED();
    00620       }
    00621       break;
    00622     }
    00623
    00624     case SDT_STRING:
    00625       switch (GetVarMemType(sld->conv)) {
    00626       case SLE_VAR_STRB: strecpy(buf, (char*)ptr, lastof(buf)); break;
    00627       case SLE_VAR_STRBQ:seprintf(buf, lastof(buf), "\"%s\"", (char*)ptr); break;
    00628       case SLE_VAR_STR:  strecpy(buf, *(char**)ptr, lastof(buf)); break;
    00629       case SLE_VAR_STRQ:
    00630         if (*(char**)ptr == NULL) {
    00631           buf[0] = '\0';
    00632         } else {
    00633           seprintf(buf, lastof(buf), "\"%s\"", *(char**)ptr);
    00634         }
    00635         break;
    00636       case SLE_VAR_CHAR: buf[0] = *(char*)ptr; buf[1] = '\0'; break;
    00637       default: NOT_REACHED();
    00638       }
    00639       break;
    00640
    00641     case SDT_INTLIST:
    00642       MakeIntList(buf, lastof(buf), ptr, sld->length, GetVarMemType(sld->conv));
    00643       break;
    00644     default: NOT_REACHED();
    00645     }
    00646
    00647     /* The value is different, that means we have to write it to the ini */
    00648     free(item->value);
    00649     item->value = strdup(buf);
    00650   }
    00651 }
    00652
    00662 static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList *list)
    00663 {
    00664   IniGroup *group = ini->GetGroup(grpname);
    00665
    00666   if (group == NULL || list == NULL) return;
    00667
    00668   list->Clear();
    00669
    00670   for (const IniItem *item = group->item; item != NULL; item = item->next) {
    00671     if (item->name != NULL) *list->Append() = strdup(item->name);
    00672   }
    00673 }
    00674
    00684 static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList *list)
    00685 {
    00686   IniGroup *group = ini->GetGroup(grpname);
    00687
    00688   if (group == NULL || list == NULL) return;
    00689   group->Clear();
    00690
    00691   for (char **iter = list->Begin(); iter != list->End(); iter++) {
    00692     group->GetItem(*iter, true)->SetValue("");
    00693   }
    00694 }
    00695
    00696 /* Begin - Callback Functions for the various settings. */
    00697
    00699 static bool v_PositionMainToolbar(int32 p1)
    00700 {
    00701   if (_game_mode != GM_MENU) PositionMainToolbar(NULL);
    00702   return true;
    00703 }
    00704
    00706 static bool v_PositionStatusbar(int32 p1)
    00707 {
    00708   if (_game_mode != GM_MENU) {
    00709     PositionStatusbar(NULL);
    00710     PositionNewsMessage(NULL);
    00711     PositionNetworkChatWindow(NULL);
    00712   }
    00713   return true;
    00714 }
    00715
    00716 static bool PopulationInLabelActive(int32 p1)
    00717 {
    00718   UpdateAllTownVirtCoords();
    00719   return true;
    00720 }
    00721
    00722 static bool RedrawScreen(int32 p1)
    00723 {
    00724   MarkWholeScreenDirty();
    00725   return true;
    00726 }
    00727
    00733 static bool RedrawSmallmap(int32 p1)
    00734 {
    00735   BuildLandLegend();
    00736   BuildOwnerLegend();
    00737   SetWindowClassesDirty(WC_SMALLMAP);
    00738   return true;
    00739 }
    00740
    00741 static bool InvalidateDetailsWindow(int32 p1)
    00742 {
    00743   SetWindowClassesDirty(WC_VEHICLE_DETAILS);
    00744   return true;
    00745 }
    00746
    00747 static bool InvalidateStationBuildWindow(int32 p1)
    00748 {
    00749   SetWindowDirty(WC_BUILD_STATION, 0);
    00750   return true;
    00751 }
    00752
    00753 static bool InvalidateBuildIndustryWindow(int32 p1)
    00754 {
    00755   InvalidateWindowData(WC_BUILD_INDUSTRY, 0);
    00756   return true;
    00757 }
    00758
    00759 static bool CloseSignalGUI(int32 p1)
    00760 {
    00761   if (p1 == 0) {
    00762     DeleteWindowByClass(WC_BUILD_SIGNAL);
    00763   }
    00764   return true;
    00765 }
    00766
    00767 static bool InvalidateTownViewWindow(int32 p1)
    00768 {
    00769   InvalidateWindowClassesData(WC_TOWN_VIEW, p1);
    00770   return true;
    00771 }
    00772
    00773 static bool DeleteSelectStationWindow(int32 p1)
    00774 {
    00775   DeleteWindowById(WC_SELECT_STATION, 0);
    00776   return true;
    00777 }
    00778
    00779 static bool UpdateConsists(int32 p1)
    00780 {
    00781   Train *t;
    00782   FOR_ALL_TRAINS(t) {
    00783     /* Update the consist of all trains so the maximum speed is set correctly. */
    00784     if (t->IsFrontEngine() || t->IsFreeWagon()) t->ConsistChanged(true);
    00785   }
    00786   InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
    00787   return true;
    00788 }
    00789
    00790 /* Check service intervals of vehicles, p1 is value of % or day based servicing */
    00791 static bool CheckInterval(int32 p1)
    00792 {
    00793   VehicleDefaultSettings *vds;
    00794   if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
    00795     vds = &_settings_client.company.vehicle;
    00796   } else {
    00797     vds = &Company::Get(_current_company)->settings.vehicle;
    00798   }
    00799
    00800   if (p1 != 0) {
    00801     vds->servint_trains   = 50;
    00802     vds->servint_roadveh  = 50;
    00803     vds->servint_aircraft = 50;
    00804     vds->servint_ships    = 50;
    00805   } else {
    00806     vds->servint_trains   = 150;
    00807     vds->servint_roadveh  = 150;
    00808     vds->servint_aircraft = 100;
    00809     vds->servint_ships    = 360;
    00810   }
    00811
    00812   InvalidateDetailsWindow(0);
    00813
    00814   return true;
    00815 }
    00816
    00817 static bool TrainAccelerationModelChanged(int32 p1)
    00818 {
    00819   Train *t;
    00820   FOR_ALL_TRAINS(t) {
    00821     if (t->IsFrontEngine()) {
    00822       t->tcache.cached_max_curve_speed = t->GetCurveSpeedLimit();
    00823       t->UpdateAcceleration();
    00824     }
    00825   }
    00826
    00827   /* These windows show acceleration values only when realistic acceleration is on. They must be redrawn after a setting change. */
    00828   SetWindowClassesDirty(WC_ENGINE_PREVIEW);
    00829   InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
    00830   SetWindowClassesDirty(WC_VEHICLE_DETAILS);
    00831
    00832   return true;
    00833 }
    00834
    00840 static bool TrainSlopeSteepnessChanged(int32 p1)
    00841 {
    00842   Train *t;
    00843   FOR_ALL_TRAINS(t) {
    00844     if (t->IsFrontEngine()) t->CargoChanged();
    00845   }
    00846
    00847   return true;
    00848 }
    00849
    00855 static bool RoadVehAccelerationModelChanged(int32 p1)
    00856 {
    00857   if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
    00858     RoadVehicle *rv;
    00859     FOR_ALL_ROADVEHICLES(rv) {
    00860       if (rv->IsFrontEngine()) {
    00861         rv->CargoChanged();
    00862       }
    00863     }
    00864   }
    00865
    00866   /* These windows show acceleration values only when realistic acceleration is on. They must be redrawn after a setting change. */
    00867   SetWindowClassesDirty(WC_ENGINE_PREVIEW);
    00868   InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
    00869   SetWindowClassesDirty(WC_VEHICLE_DETAILS);
    00870
    00871   return true;
    00872 }
    00873
    00879 static bool RoadVehSlopeSteepnessChanged(int32 p1)
    00880 {
    00881   RoadVehicle *rv;
    00882   FOR_ALL_ROADVEHICLES(rv) {
    00883     if (rv->IsFrontEngine()) rv->CargoChanged();
    00884   }
    00885
    00886   return true;
    00887 }
    00888
    00889 static bool DragSignalsDensityChanged(int32)
    00890 {
    00891   InvalidateWindowData(WC_BUILD_SIGNAL, 0);
    00892
    00893   return true;
    00894 }
    00895
    00896 static bool TownFoundingChanged(int32 p1)
    00897 {
    00898   if (_game_mode != GM_EDITOR && _settings_game.economy.found_town == TF_FORBIDDEN) {
    00899     DeleteWindowById(WC_FOUND_TOWN, 0);
    00900     return true;
    00901   }
    00902   InvalidateWindowData(WC_FOUND_TOWN, 0);
    00903   return true;
    00904 }
    00905
    00906 static bool InvalidateVehTimetableWindow(int32 p1)
    00907 {
    00908   InvalidateWindowClassesData(WC_VEHICLE_TIMETABLE, -2);
    00909   return true;
    00910 }
    00911
    00912 static bool ZoomMinMaxChanged(int32 p1)
    00913 {
    00914   extern void ConstrainAllViewportsZoom();
    00915   ConstrainAllViewportsZoom();
    00916   GfxClearSpriteCache();
    00917   return true;
    00918 }
    00919
    00927 static bool InvalidateNewGRFChangeWindows(int32 p1)
    00928 {
    00929   InvalidateWindowClassesData(WC_SAVELOAD);
    00930   DeleteWindowByClass(WC_GAME_OPTIONS);
    00931   ReInitAllWindows();
    00932   return true;
    00933 }
    00934
    00935 static bool InvalidateCompanyLiveryWindow(int32 p1)
    00936 {
    00937   InvalidateWindowClassesData(WC_COMPANY_COLOUR);
    00938   return RedrawScreen(p1);
    00939 }
    00940
    00941 static bool InvalidateIndustryViewWindow(int32 p1)
    00942 {
    00943   InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
    00944   return true;
    00945 }
    00946
    00947 static bool InvalidateAISettingsWindow(int32 p1)
    00948 {
    00949   InvalidateWindowClassesData(WC_AI_SETTINGS);
    00950   return true;
    00951 }
    00952
    00958 static bool RedrawTownAuthority(int32 p1)
    00959 {
    00960   SetWindowClassesDirty(WC_TOWN_AUTHORITY);
    00961   return true;
    00962 }
    00963
    00969 static bool InvalidateCompanyInfrastructureWindow(int32 p1)
    00970 {
    00971   InvalidateWindowClassesData(WC_COMPANY_INFRASTRUCTURE);
    00972   return true;
    00973 }
    00974
    00975 /*
    00976  * A: competitors
    00977  * B: competitor start time. Deprecated since savegame version 110.
    00978  * C: town count (3 = high, 0 = very low)
    00979  * D: industry count (4 = high, 0 = none)
    00980  * E: inital loan (in GBP)
    00981  * F: interest rate
    00982  * G: running costs (0 = low, 2 = high)
    00983  * H: construction speed of competitors (0 = very slow, 4 = very fast)
    00984  * I: competitor intelligence. Deprecated since savegame version 110.
    00985  * J: breakdowns (0 = off, 2 = normal)
    00986  * K: subsidy multiplier (0 = 1.5, 3 = 4.0)
    00987  * L: construction cost (0-2)
    00988  * M: terrain type (0 = very flat, 3 = mountainous)
    00989  * N: amount of water (0 = very low, 3 = high)
    00990  * O: economy (0 = steady, 1 = fluctuating)
    00991  * P: Train reversing (0 = end of line + stations, 1 = end of line)
    00992  * Q: disasters
    00993  * R: area restructuring (0 = permissive, 2 = hostile)
    00994  * S: the difficulty level
    00995  */
    00996 static const DifficultySettings _default_game_diff[3] = { /*
    00997    A, C, D,      E, F, G, H, J, K, L, M, N, O, P, Q, R, S*/
    00998   {2, 2, 4, 300000, 2, 0, 2, 1, 2, 0, 1, 0, 0, 0, 0, 0, 0},
    00999   {4, 2, 3, 150000, 3, 1, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
    01000   {7, 3, 3, 100000, 4, 1, 3, 2, 0, 2, 3, 2, 1, 1, 1, 2, 2},
    01001 };
    01002
    01003 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt)
    01004 {
    01005   assert(mode <= 3);
    01006
    01007   if (mode != 3) {
    01008     *gm_opt = _default_game_diff[mode];
    01009   } else {
    01010     gm_opt->diff_level = 3;
    01011   }
    01012 }
    01013
    01015 static void ValidateSettings()
    01016 {
    01017   /* Force the difficulty levels to correct values if they are invalid. */
    01018   if (_settings_newgame.difficulty.diff_level != 3) {
    01019     SetDifficultyLevel(_settings_newgame.difficulty.diff_level, &_settings_newgame.difficulty);
    01020   }
    01021
    01022   /* Do not allow a custom sea level with the original land generator. */
    01023   if (_settings_newgame.game_creation.land_generator == 0 &&
    01024       _settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
    01025     _settings_newgame.difficulty.quantity_sea_lakes = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE;
    01026   }
    01027 }
    01028
    01029 static bool DifficultyReset(int32 level)
    01030 {
    01031   /* In game / in the scenario editor you can set the difficulty level only to custom. This is
    01032    * needed by the AI Gui code that sets the difficulty level when you change any AI settings. */
    01033   if (_game_mode != GM_MENU && level != 3) return false;
    01034   SetDifficultyLevel(level, &GetGameSettings().difficulty);
    01035   return true;
    01036 }
    01037
    01038 static bool DifficultyChange(int32)
    01039 {
    01040   if (_game_mode == GM_MENU) {
    01041     if (_settings_newgame.difficulty.diff_level != 3) {
    01042       ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
    01043       _settings_newgame.difficulty.diff_level = 3;
    01044     }
    01045     SetWindowClassesDirty(WC_SELECT_GAME);
    01046   } else {
    01047     _settings_game.difficulty.diff_level = 3;
    01048   }
    01049
    01050   /* If we are a network-client, update the difficult setting (if it is open).
    01051    * Use this instead of just dirtying the window because we need to load in
    01052    * the new difficulty settings */
    01053   if (_networking) InvalidateWindowClassesData(WC_GAME_OPTIONS, GOID_DIFFICULTY_CHANGED);
    01054
    01055   return true;
    01056 }
    01057
    01058 static bool DifficultyNoiseChange(int32 i)
    01059 {
    01060   if (_game_mode == GM_NORMAL) {
    01061     UpdateAirportsNoise();
    01062     if (_settings_game.economy.station_noise_level) {
    01063       InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
    01064     }
    01065   }
    01066
    01067   return DifficultyChange(i);
    01068 }
    01069
    01070 static bool MaxNoAIsChange(int32 i)
    01071 {
    01072   if (GetGameSettings().difficulty.max_no_competitors != 0 &&
    01073       AI::GetInfoList()->size() == 0 &&
    01074       (!_networking || _network_server)) {
    01075     ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, WL_CRITICAL);
    01076   }
    01077
    01078   return DifficultyChange(i);
    01079 }
    01080
    01086 static bool CheckRoadSide(int p1)
    01087 {
    01088   extern bool RoadVehiclesAreBuilt();
    01089   return _game_mode == GM_MENU || !RoadVehiclesAreBuilt();
    01090 }
    01091
    01099 static size_t ConvertLandscape(const char *value)
    01100 {
    01101   /* try with the old values */
    01102   return LookupOneOfMany("normal|hilly|desert|candy", value);
    01103 }
    01104
    01105 static bool CheckFreeformEdges(int32 p1)
    01106 {
    01107   if (_game_mode == GM_MENU) return true;
    01108   if (p1 != 0) {
    01109     Ship *s;
    01110     FOR_ALL_SHIPS(s) {
    01111       /* Check if there is a ship on the northern border. */
    01112       if (TileX(s->tile) == 0 || TileY(s->tile) == 0) {
    01113         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
    01114         return false;
    01115       }
    01116     }
    01117     BaseStation *st;
    01118     FOR_ALL_BASE_STATIONS(st) {
    01119       /* Check if there is a non-deleted buoy on the northern border. */
    01120       if (st->IsInUse() && (TileX(st->xy) == 0 || TileY(st->xy) == 0)) {
    01121         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
    01122         return false;
    01123       }
    01124     }
    01125     for (uint i = 0; i < MapSizeX(); i++) MakeVoid(TileXY(i, 0));
    01126     for (uint i = 0; i < MapSizeY(); i++) MakeVoid(TileXY(0, i));
    01127   } else {
    01128     for (uint i = 0; i < MapMaxX(); i++) {
    01129       if (TileHeight(TileXY(i, 1)) != 0) {
    01130         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
    01131         return false;
    01132       }
    01133     }
    01134     for (uint i = 1; i < MapMaxX(); i++) {
    01135       if (!IsTileType(TileXY(i, MapMaxY() - 1), MP_WATER) || TileHeight(TileXY(1, MapMaxY())) != 0) {
    01136         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
    01137         return false;
    01138       }
    01139     }
    01140     for (uint i = 0; i < MapMaxY(); i++) {
    01141       if (TileHeight(TileXY(1, i)) != 0) {
    01142         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
    01143         return false;
    01144       }
    01145     }
    01146     for (uint i = 1; i < MapMaxY(); i++) {
    01147       if (!IsTileType(TileXY(MapMaxX() - 1, i), MP_WATER) || TileHeight(TileXY(MapMaxX(), i)) != 0) {
    01148         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
    01149         return false;
    01150       }
    01151     }
    01152     /* Make tiles at the border water again. */
    01153     for (uint i = 0; i < MapMaxX(); i++) {
    01154       SetTileHeight(TileXY(i, 0), 0);
    01155       SetTileType(TileXY(i, 0), MP_WATER);
    01156     }
    01157     for (uint i = 0; i < MapMaxY(); i++) {
    01158       SetTileHeight(TileXY(0, i), 0);
    01159       SetTileType(TileXY(0, i), MP_WATER);
    01160     }
    01161   }
    01162   MarkWholeScreenDirty();
    01163   return true;
    01164 }
    01165
    01170 static bool ChangeDynamicEngines(int32 p1)
    01171 {
    01172   if (_game_mode == GM_MENU) return true;
    01173
    01174   if (!EngineOverrideManager::ResetToCurrentNewGRFConfig()) {
    01175     ShowErrorMessage(STR_CONFIG_SETTING_DYNAMIC_ENGINES_EXISTING_VEHICLES, INVALID_STRING_ID, WL_ERROR);
    01176     return false;
    01177   }
    01178
    01179   return true;
    01180 }
    01181
    01182 static bool StationCatchmentChanged(int32 p1)
    01183 {
    01184   Station::RecomputeIndustriesNearForAll();
    01185   return true;
    01186 }
    01187
    01188
    01189 #ifdef ENABLE_NETWORK
    01190
    01191 static bool UpdateClientName(int32 p1)
    01192 {
    01193   NetworkUpdateClientName();
    01194   return true;
    01195 }
    01196
    01197 static bool UpdateServerPassword(int32 p1)
    01198 {
    01199   if (strcmp(_settings_client.network.server_password, "*") == 0) {
    01200     _settings_client.network.server_password[0] = '\0';
    01201   }
    01202
    01203   return true;
    01204 }
    01205
    01206 static bool UpdateRconPassword(int32 p1)
    01207 {
    01208   if (strcmp(_settings_client.network.rcon_password, "*") == 0) {
    01209     _settings_client.network.rcon_password[0] = '\0';
    01210   }
    01211
    01212   return true;
    01213 }
    01214
    01215 static bool UpdateClientConfigValues(int32 p1)
    01216 {
    01217   if (_network_server) NetworkServerSendConfigUpdate();
    01218
    01219   return true;
    01220 }
    01221
    01222 #endif /* ENABLE_NETWORK */
    01223
    01224
    01225 /* End - Callback Functions */
    01226
    01230 static void PrepareOldDiffCustom()
    01231 {
    01232   memset(_old_diff_custom, 0, sizeof(_old_diff_custom));
    01233 }
    01234
    01241 static void HandleOldDiffCustom(bool savegame)
    01242 {
    01243   uint options_to_load = GAME_DIFFICULTY_NUM - ((savegame && IsSavegameVersionBefore(4)) ? 1 : 0);
    01244
    01245   if (!savegame) {
    01246     /* If we did read to old_diff_custom, then at least one value must be non 0. */
    01247     bool old_diff_custom_used = false;
    01248     for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
    01249       old_diff_custom_used = (_old_diff_custom[i] != 0);
    01250     }
    01251
    01252     if (!old_diff_custom_used) return;
    01253   }
    01254
    01255   for (uint i = 0; i < options_to_load; i++) {
    01256     const SettingDesc *sd = &_settings[i];
    01257     /* Skip deprecated options */
    01258     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
    01259     void *var = GetVariableAddress(savegame ? &_settings_game : &_settings_newgame, &sd->save);
    01260     Write_ValidateSetting(var, sd, (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i]));
    01261   }
    01262 }
    01263
    01270 static bool ConvertOldNewsSetting(const char *name, const char *value)
    01271 {
    01272   if (strcasecmp(name, "openclose") == 0) {
    01273     /* openclose has been split in "open" and "close".
    01274      * So the job is now to decrypt the value of the old news config
    01275      * and give it to the two newly introduced ones*/
    01276
    01277     NewsDisplay display = ND_OFF; // default
    01278     if (strcasecmp(value, "full") == 0) {
    01279       display = ND_FULL;
    01280     } else if (strcasecmp(value, "summarized") == 0) {
    01281       display = ND_SUMMARY;
    01282     }
    01283     /* tranfert of values */
    01284     _news_type_data[NT_INDUSTRY_OPEN].display = display;
    01285     _news_type_data[NT_INDUSTRY_CLOSE].display = display;
    01286     return true;
    01287   }
    01288   return false;
    01289 }
    01290
    01296 static void NewsDisplayLoadConfig(IniFile *ini, const char *grpname)
    01297 {
    01298   IniGroup *group = ini->GetGroup(grpname);
    01299   IniItem *item;
    01300
    01301   /* If no group exists, return */
    01302   if (group == NULL) return;
    01303
    01304   for (item = group->item; item != NULL; item = item->next) {
    01305     int news_item = -1;
    01306     for (int i = 0; i < NT_END; i++) {
    01307       if (strcasecmp(item->name, _news_type_data[i].name) == 0) {
    01308         news_item = i;
    01309         break;
    01310       }
    01311     }
    01312
    01313     /* the config been read is not within current aceptable config */
    01314     if (news_item == -1) {
    01315       /* if the conversion function cannot process it, advice by a debug warning*/
    01316       if (!ConvertOldNewsSetting(item->name, item->value)) {
    01317         DEBUG(misc, 0, "Invalid display option: %s", item->name);
    01318       }
    01319       /* in all cases, there is nothing left to do */
    01320       continue;
    01321     }
    01322
    01323     if (StrEmpty(item->value)) {
    01324       DEBUG(misc, 0, "Empty display value for newstype %s", item->name);
    01325       continue;
    01326     } else if (strcasecmp(item->value, "full") == 0) {
    01327       _news_type_data[news_item].display = ND_FULL;
    01328     } else if (strcasecmp(item->value, "off") == 0) {
    01329       _news_type_data[news_item].display = ND_OFF;
    01330     } else if (strcasecmp(item->value, "summarized") == 0) {
    01331       _news_type_data[news_item].display = ND_SUMMARY;
    01332     } else {
    01333       DEBUG(misc, 0, "Invalid display value for newstype %s: %s", item->name, item->value);
    01334       continue;
    01335     }
    01336   }
    01337 }
    01338
    01339 static void AILoadConfig(IniFile *ini, const char *grpname)
    01340 {
    01341   IniGroup *group = ini->GetGroup(grpname);
    01342   IniItem *item;
    01343
    01344   /* Clean any configured AI */
    01345   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
    01346     AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME)->Change(NULL);
    01347   }
    01348
    01349   /* If no group exists, return */
    01350   if (group == NULL) return;
    01351
    01352   CompanyID c = COMPANY_FIRST;
    01353   for (item = group->item; c < MAX_COMPANIES && item != NULL; c++, item = item->next) {
    01354     AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
    01355
    01356     config->Change(item->name);
    01357     if (!config->HasScript()) {
    01358       if (strcmp(item->name, "none") != 0) {
    01359         DEBUG(script, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name);
    01360         continue;
    01361       }
    01362     }
    01363     if (item->value != NULL) config->StringToSettings(item->value);
    01364   }
    01365 }
    01366
    01367 static void GameLoadConfig(IniFile *ini, const char *grpname)
    01368 {
    01369   IniGroup *group = ini->GetGroup(grpname);
    01370   IniItem *item;
    01371
    01372   /* Clean any configured GameScript */
    01373   GameConfig::GetConfig(GameConfig::SSS_FORCE_NEWGAME)->Change(NULL);
    01374
    01375   /* If no group exists, return */
    01376   if (group == NULL) return;
    01377
    01378   item = group->item;
    01379   if (item == NULL) return;
    01380
    01381   GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
    01382
    01383   config->Change(item->name);
    01384   if (!config->HasScript()) {
    01385     if (strcmp(item->name, "none") != 0) {
    01386       DEBUG(script, 0, "The GameScript by the name '%s' was no longer found, and removed from the list.", item->name);
    01387       return;
    01388     }
    01389   }
    01390   if (item->value != NULL) config->StringToSettings(item->value);
    01391 }
    01392
    01399 static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_static)
    01400 {
    01401   IniGroup *group = ini->GetGroup(grpname);
    01402   IniItem *item;
    01403   GRFConfig *first = NULL;
    01404   GRFConfig **curr = &first;
    01405
    01406   if (group == NULL) return NULL;
    01407
    01408   for (item = group->item; item != NULL; item = item->next) {
    01409     GRFConfig *c = new GRFConfig(item->name);
    01410
    01411     /* Parse parameters */
    01412     if (!StrEmpty(item->value)) {
    01413       c->num_params = ParseIntList(item->value, (int*)c->param, lengthof(c->param));
    01414       if (c->num_params == (byte)-1) {
    01415         SetDParamStr(0, item->name);
    01416         ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY, WL_CRITICAL);
    01417         c->num_params = 0;
    01418       }
    01419     }
    01420
    01421     /* Check if item is valid */
    01422     if (!FillGRFDetails(c, is_static) || HasBit(c->flags, GCF_INVALID)) {
    01423       if (c->status == GCS_NOT_FOUND) {
    01424         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_NOT_FOUND);
    01425       } else if (HasBit(c->flags, GCF_UNSAFE)) {
    01426         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNSAFE);
    01427       } else if (HasBit(c->flags, GCF_SYSTEM)) {
    01428         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_SYSTEM);
    01429       } else if (HasBit(c->flags, GCF_INVALID)) {
    01430         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_INCOMPATIBLE);
    01431       } else {
    01432         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNKNOWN);
    01433       }
    01434
    01435       SetDParamStr(0, item->name);
    01436       ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_GRF, WL_CRITICAL);
    01437       delete c;
    01438       continue;
    01439     }
    01440
    01441     /* Check for duplicate GRFID (will also check for duplicate filenames) */
    01442     bool duplicate = false;
    01443     for (const GRFConfig *gc = first; gc != NULL; gc = gc->next) {
    01444       if (gc->ident.grfid == c->ident.grfid) {
    01445         SetDParamStr(0, item->name);
    01446         SetDParamStr(1, gc->filename);
    01447         ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_DUPLICATE_GRFID, WL_CRITICAL);
    01448         duplicate = true;
    01449         break;
    01450       }
    01451     }
    01452     if (duplicate) {
    01453       delete c;
    01454       continue;
    01455     }
    01456
    01457     /* Mark file as static to avoid saving in savegame. */
    01458     if (is_static) SetBit(c->flags, GCF_STATIC);
    01459
    01460     /* Add item to list */
    01461     *curr = c;
    01462     curr = &c->next;
    01463   }
    01464
    01465   return first;
    01466 }
    01467
    01473 static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname)
    01474 {
    01475   IniGroup *group = ini->GetGroup(grpname);
    01476
    01477   for (int i = 0; i < NT_END; i++) {
    01478     const char *value;
    01479     int v = _news_type_data[i].display;
    01480
    01481     value = (v == ND_OFF ? "off" : (v == ND_SUMMARY ? "summarized" : "full"));
    01482
    01483     group->GetItem(_news_type_data[i].name, true)->SetValue(value);
    01484   }
    01485 }
    01486
    01487 static void AISaveConfig(IniFile *ini, const char *grpname)
    01488 {
    01489   IniGroup *group = ini->GetGroup(grpname);
    01490
    01491   if (group == NULL) return;
    01492   group->Clear();
    01493
    01494   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
    01495     AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
    01496     const char *name;
    01497     char value[1024];
    01498     config->SettingsToString(value, lengthof(value));
    01499
    01500     if (config->HasScript()) {
    01501       name = config->GetName();
    01502     } else {
    01503       name = "none";
    01504     }
    01505
    01506     IniItem *item = new IniItem(group, name, strlen(name));
    01507     item->SetValue(value);
    01508   }
    01509 }
    01510
    01511 static void GameSaveConfig(IniFile *ini, const char *grpname)
    01512 {
    01513   IniGroup *group = ini->GetGroup(grpname);
    01514
    01515   if (group == NULL) return;
    01516   group->Clear();
    01517
    01518   GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
    01519   const char *name;
    01520   char value[1024];
    01521   config->SettingsToString(value, lengthof(value));
    01522
    01523   if (config->HasScript()) {
    01524     name = config->GetName();
    01525   } else {
    01526     name = "none";
    01527   }
    01528
    01529   IniItem *item = new IniItem(group, name, strlen(name));
    01530   item->SetValue(value);
    01531 }
    01532
    01537 static void SaveVersionInConfig(IniFile *ini)
    01538 {
    01539   IniGroup *group = ini->GetGroup("version");
    01540
    01541   char version[9];
    01542   snprintf(version, lengthof(version), "%08X", _openttd_newgrf_version);
    01543
    01544   const char * const versions[][2] = {
    01545     { "version_string", _openttd_revision },
    01546     { "version_number", version }
    01547   };
    01548
    01549   for (uint i = 0; i < lengthof(versions); i++) {
    01550     group->GetItem(versions[i][0], true)->SetValue(versions[i][1]);
    01551   }
    01552 }
    01553
    01554 /* Save a GRF configuration to the given group name */
    01555 static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *list)
    01556 {
    01557   ini->RemoveGroup(grpname);
    01558   IniGroup *group = ini->GetGroup(grpname);
    01559   const GRFConfig *c;
    01560
    01561   for (c = list; c != NULL; c = c->next) {
    01562     char params[512];
    01563     GRFBuildParamList(params, c, lastof(params));
    01564
    01565     group->GetItem(c->filename, true)->SetValue(params);
    01566   }
    01567 }
    01568
    01569 /* Common handler for saving/loading variables to the configuration file */
    01570 static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list, bool basic_settings = true, bool other_settings = true)
    01571 {
    01572   if (basic_settings) {
    01573     proc(ini, (const SettingDesc*)_misc_settings,    "misc",  NULL);
    01574 #if defined(WIN32) && !defined(DEDICATED)
    01575     proc(ini, (const SettingDesc*)_win32_settings,   "win32", NULL);
    01576 #endif /* WIN32 */
    01577   }
    01578
    01579   if (other_settings) {
    01580     proc(ini, _settings,         "patches",  &_settings_newgame);
    01581     proc(ini, _currency_settings,"currency", &_custom_currency);
    01582     proc(ini, _company_settings, "company",  &_settings_client.company);
    01583
    01584 #ifdef ENABLE_NETWORK
    01585     proc_list(ini, "server_bind_addresses", &_network_bind_list);
    01586     proc_list(ini, "servers", &_network_host_list);
    01587     proc_list(ini, "bans",    &_network_ban_list);
    01588 #endif /* ENABLE_NETWORK */
    01589   }
    01590 }
    01591
    01592 static IniFile *IniLoadConfig()
    01593 {
    01594   IniFile *ini = new IniFile(_list_group_names);
    01595   ini->LoadFromDisk(_config_file, BASE_DIR);
    01596   return ini;
    01597 }
    01598
    01603 void LoadFromConfig(bool minimal)
    01604 {
    01605   IniFile *ini = IniLoadConfig();
    01606   if (!minimal) ResetCurrencies(false); // Initialize the array of curencies, without preserving the custom one
    01607
    01608   /* Load basic settings only during bootstrap, load other settings not during bootstrap */
    01609   HandleSettingDescs(ini, IniLoadSettings, IniLoadSettingList, minimal, !minimal);
    01610
    01611   if (!minimal) {
    01612     _grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
    01613     _grfconfig_static  = GRFLoadConfig(ini, "newgrf-static", true);
    01614     NewsDisplayLoadConfig(ini, "news_display");
    01615     AILoadConfig(ini, "ai_players");
    01616     GameLoadConfig(ini, "game_scripts");
    01617
    01618     PrepareOldDiffCustom();
    01619     IniLoadSettings(ini, _gameopt_settings, "gameopt", &_settings_newgame);
    01620     HandleOldDiffCustom(false);
    01621
    01622     ValidateSettings();
    01623   }
    01624
    01625   delete ini;
    01626 }
    01627
    01629 void SaveToConfig()
    01630 {
    01631   IniFile *ini = IniLoadConfig();
    01632
    01633   /* Remove some obsolete groups. These have all been loaded into other groups. */
    01634   ini->RemoveGroup("patches");
    01635   ini->RemoveGroup("yapf");
    01636   ini->RemoveGroup("gameopt");
    01637
    01638   HandleSettingDescs(ini, IniSaveSettings, IniSaveSettingList);
    01639   GRFSaveConfig(ini, "newgrf", _grfconfig_newgame);
    01640   GRFSaveConfig(ini, "newgrf-static", _grfconfig_static);
    01641   NewsDisplaySaveConfig(ini, "news_display");
    01642   AISaveConfig(ini, "ai_players");
    01643   GameSaveConfig(ini, "game_scripts");
    01644   SaveVersionInConfig(ini);
    01645   ini->SaveToDisk(_config_file);
    01646   delete ini;
    01647 }
    01648
    01653 void GetGRFPresetList(GRFPresetList *list)
    01654 {
    01655   list->Clear();
    01656
    01657   IniFile *ini = IniLoadConfig();
    01658   IniGroup *group;
    01659   for (group = ini->group; group != NULL; group = group->next) {
    01660     if (strncmp(group->name, "preset-", 7) == 0) {
    01661       *list->Append() = strdup(group->name + 7);
    01662     }
    01663   }
    01664
    01665   delete ini;
    01666 }
    01667
    01674 GRFConfig *LoadGRFPresetFromConfig(const char *config_name)
    01675 {
    01676   char *section = (char*)alloca(strlen(config_name) + 8);
    01677   sprintf(section, "preset-%s", config_name);
    01678
    01679   IniFile *ini = IniLoadConfig();
    01680   GRFConfig *config = GRFLoadConfig(ini, section, false);
    01681   delete ini;
    01682
    01683   return config;
    01684 }
    01685
    01692 void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
    01693 {
    01694   char *section = (char*)alloca(strlen(config_name) + 8);
    01695   sprintf(section, "preset-%s", config_name);
    01696
    01697   IniFile *ini = IniLoadConfig();
    01698   GRFSaveConfig(ini, section, config);
    01699   ini->SaveToDisk(_config_file);
    01700   delete ini;
    01701 }
    01702
    01707 void DeleteGRFPresetFromConfig(const char *config_name)
    01708 {
    01709   char *section = (char*)alloca(strlen(config_name) + 8);
    01710   sprintf(section, "preset-%s", config_name);
    01711
    01712   IniFile *ini = IniLoadConfig();
    01713   ini->RemoveGroup(section);
    01714   ini->SaveToDisk(_config_file);
    01715   delete ini;
    01716 }
    01717
    01718 const SettingDesc *GetSettingDescription(uint index)
    01719 {
    01720   if (index >= lengthof(_settings)) return NULL;
    01721   return &_settings[index];
    01722 }
    01723
    01735 CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01736 {
    01737   const SettingDesc *sd = GetSettingDescription(p1);
    01738
    01739   if (sd == NULL) return CMD_ERROR;
    01740   if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
    01741
    01742   if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return CMD_ERROR;
    01743   if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return CMD_ERROR;
    01744   if ((sd->desc.flags & SGF_NEWGAME_ONLY) &&
    01745       (_game_mode == GM_NORMAL ||
    01746       (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0))) {
    01747     return CMD_ERROR;
    01748   }
    01749
    01750   if (flags & DC_EXEC) {
    01751     void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
    01752
    01753     int32 oldval = (int32)ReadValue(var, sd->save.conv);
    01754     int32 newval = (int32)p2;
    01755
    01756     Write_ValidateSetting(var, sd, newval);
    01757     newval = (int32)ReadValue(var, sd->save.conv);
    01758
    01759     if (oldval == newval) return CommandCost();
    01760
    01761     if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
    01762       WriteValue(var, sd->save.conv, (int64)oldval);
    01763       return CommandCost();
    01764     }
    01765
    01766     if (sd->desc.flags & SGF_NO_NETWORK) {
    01767       GamelogStartAction(GLAT_SETTING);
    01768       GamelogSetting(sd->desc.name, oldval, newval);
    01769       GamelogStopAction();
    01770     }
    01771
    01772     SetWindowClassesDirty(WC_GAME_OPTIONS);
    01773   }
    01774
    01775   return CommandCost();
    01776 }
    01777
    01788 CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01789 {
    01790   if (p1 >= lengthof(_company_settings)) return CMD_ERROR;
    01791   const SettingDesc *sd = &_company_settings[p1];
    01792
    01793   if (flags & DC_EXEC) {
    01794     void *var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
    01795
    01796     int32 oldval = (int32)ReadValue(var, sd->save.conv);
    01797     int32 newval = (int32)p2;
    01798
    01799     Write_ValidateSetting(var, sd, newval);
    01800     newval = (int32)ReadValue(var, sd->save.conv);
    01801
    01802     if (oldval == newval) return CommandCost();
    01803
    01804     if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
    01805       WriteValue(var, sd->save.conv, (int64)oldval);
    01806       return CommandCost();
    01807     }
    01808
    01809     SetWindowClassesDirty(WC_GAME_OPTIONS);
    01810   }
    01811
    01812   return CommandCost();
    01813 }
    01814
    01822 bool SetSettingValue(uint index, int32 value, bool force_newgame)
    01823 {
    01824   const SettingDesc *sd = &_settings[index];
    01825   /* If an item is company-based, we do not send it over the network
    01826    * (if any) to change. Also *hack*hack* we update the _newgame version
    01827    * of settings because changing a company-based setting in a game also
    01828    * changes its defaults. At least that is the convention we have chosen */
    01829   if (sd->save.conv & SLF_NO_NETWORK_SYNC) {
    01830     void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
    01831     Write_ValidateSetting(var, sd, value);
    01832
    01833     if (_game_mode != GM_MENU) {
    01834       void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
    01835       Write_ValidateSetting(var2, sd, value);
    01836     }
    01837     if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
    01838
    01839     SetWindowClassesDirty(WC_GAME_OPTIONS);
    01840
    01841     return true;
    01842   }
    01843
    01844   if (force_newgame) {
    01845     void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
    01846     Write_ValidateSetting(var2, sd, value);
    01847     return true;
    01848   }
    01849
    01850   /* send non-company-based settings over the network */
    01851   if (!_networking || (_networking && _network_server)) {
    01852     return DoCommandP(0, index, value, CMD_CHANGE_SETTING);
    01853   }
    01854   return false;
    01855 }
    01856
    01863 void SetCompanySetting(uint index, int32 value)
    01864 {
    01865   const SettingDesc *sd = &_company_settings[index];
    01866   if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
    01867     DoCommandP(0, index, value, CMD_CHANGE_COMPANY_SETTING);
    01868   } else {
    01869     void *var = GetVariableAddress(&_settings_client.company, &sd->save);
    01870     Write_ValidateSetting(var, sd, value);
    01871     if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
    01872   }
    01873 }
    01874
    01878 void SetDefaultCompanySettings(CompanyID cid)
    01879 {
    01880   Company *c = Company::Get(cid);
    01881   const SettingDesc *sd;
    01882   for (sd = _company_settings; sd->save.cmd != SL_END; sd++) {
    01883     void *var = GetVariableAddress(&c->settings, &sd->save);
    01884     Write_ValidateSetting(var, sd, (int32)(size_t)sd->desc.def);
    01885   }
    01886 }
    01887
    01888 #if defined(ENABLE_NETWORK)
    01889
    01892 void SyncCompanySettings()
    01893 {
    01894   const SettingDesc *sd;
    01895   uint i = 0;
    01896   for (sd = _company_settings; sd->save.cmd != SL_END; sd++, i++) {
    01897     const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
    01898     const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save);
    01899     uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv);
    01900     uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv);
    01901     if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, NULL, NULL, _local_company);
    01902   }
    01903 }
    01904 #endif /* ENABLE_NETWORK */
    01905
    01911 uint GetCompanySettingIndex(const char *name)
    01912 {
    01913   uint i;
    01914   const SettingDesc *sd = GetSettingFromName(name, &i);
    01915   assert(sd != NULL && (sd->desc.flags & SGF_PER_COMPANY) != 0);
    01916   return i;
    01917 }
    01918
    01926 bool SetSettingValue(uint index, const char *value, bool force_newgame)
    01927 {
    01928   const SettingDesc *sd = &_settings[index];
    01929   assert(sd->save.conv & SLF_NO_NETWORK_SYNC);
    01930
    01931   if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) {
    01932     char **var = (char**)GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
    01933     free(*var);
    01934     *var = strcmp(value, "(null)") == 0 ? NULL : strdup(value);
    01935   } else {
    01936     char *var = (char*)GetVariableAddress(NULL, &sd->save);
    01937     ttd_strlcpy(var, value, sd->save.length);
    01938   }
    01939   if (sd->desc.proc != NULL) sd->desc.proc(0);
    01940
    01941   return true;
    01942 }
    01943
    01951 const SettingDesc *GetSettingFromName(const char *name, uint *i)
    01952 {
    01953   const SettingDesc *sd;
    01954
    01955   /* First check all full names */
    01956   for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
    01957     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
    01958     if (strcmp(sd->desc.name, name) == 0) return sd;
    01959   }
    01960
    01961   /* Then check the shortcut variant of the name. */
    01962   for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
    01963     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
    01964     const char *short_name = strchr(sd->desc.name, '.');
    01965     if (short_name != NULL) {
    01966       short_name++;
    01967       if (strcmp(short_name, name) == 0) return sd;
    01968     }
    01969   }
    01970
    01971   if (strncmp(name, "company.", 8) == 0) name += 8;
    01972   /* And finally the company-based settings */
    01973   for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) {
    01974     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
    01975     if (strcmp(sd->desc.name, name) == 0) return sd;
    01976   }
    01977
    01978   return NULL;
    01979 }
    01980
    01981 /* Those 2 functions need to be here, else we have to make some stuff non-static
    01982  * and besides, it is also better to keep stuff like this at the same place */
    01983 void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
    01984 {
    01985   uint index;
    01986   const SettingDesc *sd = GetSettingFromName(name, &index);
    01987
    01988   if (sd == NULL) {
    01989     IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
    01990     return;
    01991   }
    01992
    01993   bool success;
    01994   if (sd->desc.cmd == SDT_STRING) {
    01995     success = SetSettingValue(index, value, force_newgame);
    01996   } else {
    01997     uint32 val;
    01998     extern bool GetArgumentInteger(uint32 *value, const char *arg);
    01999     success = GetArgumentInteger(&val, value);
    02000     if (!success) {
    02001       IConsolePrintF(CC_ERROR, "'%s' is not an integer.", value);
    02002       return;
    02003     }
    02004
    02005     success = SetSettingValue(index, val, force_newgame);
    02006   }
    02007
    02008   if (!success) {
    02009     if (_network_server) {
    02010       IConsoleError("This command/variable is not available during network games.");
    02011     } else {
    02012       IConsoleError("This command/variable is only available to a network server.");
    02013     }
    02014   }
    02015 }
    02016
    02017 void IConsoleSetSetting(const char *name, int value)
    02018 {
    02019   uint index;
    02020   const SettingDesc *sd = GetSettingFromName(name, &index);
    02021   assert(sd != NULL);
    02022   SetSettingValue(index, value);
    02023 }
    02024
    02030 void IConsoleGetSetting(const char *name, bool force_newgame)
    02031 {
    02032   char value[20];
    02033   uint index;
    02034   const SettingDesc *sd = GetSettingFromName(name, &index);
    02035   const void *ptr;
    02036
    02037   if (sd == NULL) {
    02038     IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
    02039     return;
    02040   }
    02041
    02042   ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
    02043
    02044   if (sd->desc.cmd == SDT_STRING) {
    02045     IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char * const *)ptr : (const char *)ptr);
    02046   } else {
    02047     if (sd->desc.cmd == SDT_BOOLX) {
    02048       snprintf(value, sizeof(value), (*(const bool*)ptr != 0) ? "on" : "off");
    02049     } else {
    02050       snprintf(value, sizeof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
    02051     }
    02052
    02053     IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %u)",
    02054       name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max);
    02055   }
    02056 }
    02057
    02063 void IConsoleListSettings(const char *prefilter)
    02064 {
    02065   IConsolePrintF(CC_WARNING, "All settings with their current value:");
    02066
    02067   for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) {
    02068     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
    02069     if (prefilter != NULL && strstr(sd->desc.name, prefilter) == NULL) continue;
    02070     char value[80];
    02071     const void *ptr = GetVariableAddress(&GetGameSettings(), &sd->save);
    02072
    02073     if (sd->desc.cmd == SDT_BOOLX) {
    02074       snprintf(value, lengthof(value), (*(const bool *)ptr != 0) ? "on" : "off");
    02075     } else if (sd->desc.cmd == SDT_STRING) {
    02076       snprintf(value, sizeof(value), "%s", (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char * const *)ptr : (const char *)ptr);
    02077     } else {
    02078       snprintf(value, lengthof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
    02079     }
    02080     IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value);
    02081   }
    02082
    02083   IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value");
    02084 }
    02085
    02092 static void LoadSettings(const SettingDesc *osd, void *object)
    02093 {
    02094   for (; osd->save.cmd != SL_END; osd++) {
    02095     const SaveLoad *sld = &osd->save;
    02096     void *ptr = GetVariableAddress(object, sld);
    02097
    02098     if (!SlObjectMember(ptr, sld)) continue;
    02099     if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, osd, ReadValue(ptr, sld->conv));
    02100   }
    02101 }
    02102
    02109 static void SaveSettings(const SettingDesc *sd, void *object)
    02110 {
    02111   /* We need to write the CH_RIFF header, but unfortunately can't call
    02112    * SlCalcLength() because we have a different format. So do this manually */
    02113   const SettingDesc *i;
    02114   size_t length = 0;
    02115   for (i = sd; i->save.cmd != SL_END; i++) {
    02116     length += SlCalcObjMemberLength(object, &i->save);
    02117   }
    02118   SlSetLength(length);
    02119
    02120   for (i = sd; i->save.cmd != SL_END; i++) {
    02121     void *ptr = GetVariableAddress(object, &i->save);
    02122     SlObjectMember(ptr, &i->save);
    02123   }
    02124 }
    02125
    02126 static void Load_OPTS()
    02127 {
    02128   /* Copy over default setting since some might not get loaded in
    02129    * a networking environment. This ensures for example that the local
    02130    * autosave-frequency stays when joining a network-server */
    02131   PrepareOldDiffCustom();
    02132   LoadSettings(_gameopt_settings, &_settings_game);
    02133   HandleOldDiffCustom(true);
    02134 }
    02135
    02136 static void Load_PATS()
    02137 {
    02138   /* Copy over default setting since some might not get loaded in
    02139    * a networking environment. This ensures for example that the local
    02140    * signal_side stays when joining a network-server */
    02141   LoadSettings(_settings, &_settings_game);
    02142 }
    02143
    02144 static void Check_PATS()
    02145 {
    02146   LoadSettings(_settings, &_load_check_data.settings);
    02147 }
    02148
    02149 static void Save_PATS()
    02150 {
    02151   SaveSettings(_settings, &_settings_game);
    02152 }
    02153
    02154 void CheckConfig()
    02155 {
    02156   /*
    02157    * Increase old default values for pf_maxdepth and pf_maxlength
    02158    * to support big networks.
    02159    */
    02160   if (_settings_newgame.pf.opf.pf_maxdepth == 16 && _settings_newgame.pf.opf.pf_maxlength == 512) {
    02161     _settings_newgame.pf.opf.pf_maxdepth = 48;
    02162     _settings_newgame.pf.opf.pf_maxlength = 4096;
    02163   }
    02164 }
    02165
    02166 extern const ChunkHandler _setting_chunk_handlers[] = {
    02167   { 'OPTS', NULL,      Load_OPTS, NULL, NULL,       CH_RIFF},
    02168   { 'PATS', Save_PATS, Load_PATS, NULL, Check_PATS, CH_RIFF | CH_LAST},
    02169 };
    02170
    02171 static bool IsSignedVarMemType(VarType vt)
    02172 {
    02173   switch (GetVarMemType(vt)) {
    02174     case SLE_VAR_I8:
    02175     case SLE_VAR_I16:
    02176     case SLE_VAR_I32:
    02177     case SLE_VAR_I64:
    02178       return true;
    02179   }
    02180   return false;
    02181 }