# newgrf_config.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
    00012 #include "stdafx.h"
    00013 #include "debug.h"
    00014 #include "3rdparty/md5/md5.h"
    00015 #include "newgrf.h"
    00016 #include "network/network_func.h"
    00017 #include "gfx_func.h"
    00018 #include "newgrf_text.h"
    00019 #include "window_func.h"
    00020 #include "progress.h"
    00021 #include "video/video_driver.hpp"
    00022 #include "strings_func.h"
    00023 #include "textfile_gui.h"
    00024
    00025 #include "fileio_func.h"
    00026 #include "fios.h"
    00027
    00029 GRFTextWrapper::GRFTextWrapper() :
    00030   text(NULL)
    00031 {
    00032 }
    00033
    00035 GRFTextWrapper::~GRFTextWrapper()
    00036 {
    00037   CleanUpGRFText(this->text);
    00038 }
    00039
    00045 GRFConfig::GRFConfig(const char *filename) :
    00046   name(new GRFTextWrapper()),
    00047   info(new GRFTextWrapper()),
    00048   url(new GRFTextWrapper()),
    00049   num_valid_params(lengthof(param))
    00050 {
    00051   if (filename != NULL) this->filename = strdup(filename);
    00052   this->name->AddRef();
    00053   this->info->AddRef();
    00054   this->url->AddRef();
    00055 }
    00056
    00061 GRFConfig::GRFConfig(const GRFConfig &config) :
    00062   ZeroedMemoryAllocator(),
    00063   ident(config.ident),
    00064   name(config.name),
    00065   info(config.info),
    00066   url(config.url),
    00067   version(config.version),
    00068   min_loadable_version(config.min_loadable_version),
    00069   flags(config.flags & ~(1 << GCF_COPY)),
    00070   status(config.status),
    00071   grf_bugs(config.grf_bugs),
    00072   num_params(config.num_params),
    00073   num_valid_params(config.num_valid_params),
    00074   palette(config.palette),
    00075   has_param_defaults(config.has_param_defaults)
    00076 {
    00077   MemCpyT<uint8>(this->original_md5sum, config.original_md5sum, lengthof(this->original_md5sum));
    00078   MemCpyT<uint32>(this->param, config.param, lengthof(this->param));
    00079   if (config.filename != NULL) this->filename = strdup(config.filename);
    00080   this->name->AddRef();
    00081   this->info->AddRef();
    00082   this->url->AddRef();
    00083   if (config.error    != NULL) this->error    = new GRFError(*config.error);
    00084   for (uint i = 0; i < config.param_info.Length(); i++) {
    00085     if (config.param_info[i] == NULL) {
    00086       *this->param_info.Append() = NULL;
    00087     } else {
    00088       *this->param_info.Append() = new GRFParameterInfo(*config.param_info[i]);
    00089     }
    00090   }
    00091 }
    00092
    00094 GRFConfig::~GRFConfig()
    00095 {
    00096   /* GCF_COPY as in NOT strdupped/alloced the filename */
    00097   if (!HasBit(this->flags, GCF_COPY)) {
    00098     free(this->filename);
    00099     delete this->error;
    00100   }
    00101   this->name->Release();
    00102   this->info->Release();
    00103   this->url->Release();
    00104
    00105   for (uint i = 0; i < this->param_info.Length(); i++) delete this->param_info[i];
    00106 }
    00107
    00113 const char *GRFConfig::GetName() const
    00114 {
    00115   const char *name = GetGRFStringFromGRFText(this->name->text);
    00116   return StrEmpty(name) ? this->filename : name;
    00117 }
    00118
    00123 const char *GRFConfig::GetDescription() const
    00124 {
    00125   return GetGRFStringFromGRFText(this->info->text);
    00126 }
    00127
    00132 const char *GRFConfig::GetURL() const
    00133 {
    00134   return GetGRFStringFromGRFText(this->url->text);
    00135 }
    00136
    00138 void GRFConfig::SetParameterDefaults()
    00139 {
    00140   this->num_params = 0;
    00141   MemSetT<uint32>(this->param, 0, lengthof(this->param));
    00142
    00143   if (!this->has_param_defaults) return;
    00144
    00145   for (uint i = 0; i < this->param_info.Length(); i++) {
    00146     if (this->param_info[i] == NULL) continue;
    00147     this->param_info[i]->SetValue(this, this->param_info[i]->def_value);
    00148   }
    00149 }
    00150
    00156 void GRFConfig::SetSuitablePalette()
    00157 {
    00158   PaletteType pal;
    00159   switch (this->palette & GRFP_GRF_MASK) {
    00160     case GRFP_GRF_DOS:     pal = PAL_DOS;      break;
    00161     case GRFP_GRF_WINDOWS: pal = PAL_WINDOWS;  break;
    00162     default:               pal = _settings_client.gui.newgrf_default_palette == 1 ? PAL_WINDOWS : PAL_DOS; break;
    00163   }
    00164   SB(this->palette, GRFP_USE_BIT, 1, pal == PAL_WINDOWS ? GRFP_USE_WINDOWS : GRFP_USE_DOS);
    00165 }
    00166
    00167 GRFConfig *_all_grfs;
    00168 GRFConfig *_grfconfig;
    00169 GRFConfig *_grfconfig_newgame;
    00170 GRFConfig *_grfconfig_static;
    00171
    00177 GRFError::GRFError(StringID severity, StringID message) :
    00178   message(message),
    00179   severity(severity)
    00180 {
    00181 }
    00182
    00187 GRFError::GRFError(const GRFError &error) :
    00188   ZeroedMemoryAllocator(),
    00189   custom_message(error.custom_message),
    00190   data(error.data),
    00191   message(error.message),
    00192   severity(error.severity)
    00193 {
    00194   if (error.custom_message != NULL) this->custom_message = strdup(error.custom_message);
    00195   if (error.data           != NULL) this->data           = strdup(error.data);
    00196   memcpy(this->param_value, error.param_value, sizeof(this->param_value));
    00197 }
    00198
    00199 GRFError::~GRFError()
    00200 {
    00201   free(this->custom_message);
    00202   free(this->data);
    00203 }
    00204
    00209 GRFParameterInfo::GRFParameterInfo(uint nr) :
    00210   name(NULL),
    00211   desc(NULL),
    00212   type(PTYPE_UINT_ENUM),
    00213   min_value(0),
    00214   max_value(UINT32_MAX),
    00215   def_value(0),
    00216   param_nr(nr),
    00217   first_bit(0),
    00218   num_bit(32)
    00219 {}
    00220
    00226 GRFParameterInfo::GRFParameterInfo(GRFParameterInfo &info) :
    00227   name(DuplicateGRFText(info.name)),
    00228   desc(DuplicateGRFText(info.desc)),
    00229   type(info.type),
    00230   min_value(info.min_value),
    00231   max_value(info.max_value),
    00232   def_value(info.def_value),
    00233   param_nr(info.param_nr),
    00234   first_bit(info.first_bit),
    00235   num_bit(info.num_bit)
    00236 {
    00237   for (uint i = 0; i < info.value_names.Length(); i++) {
    00238     SmallPair<uint32, GRFText *> *data = info.value_names.Get(i);
    00239     this->value_names.Insert(data->first, DuplicateGRFText(data->second));
    00240   }
    00241 }
    00242
    00244 GRFParameterInfo::~GRFParameterInfo()
    00245 {
    00246   CleanUpGRFText(this->name);
    00247   CleanUpGRFText(this->desc);
    00248   for (uint i = 0; i < this->value_names.Length(); i++) {
    00249     SmallPair<uint32, GRFText *> *data = this->value_names.Get(i);
    00250     CleanUpGRFText(data->second);
    00251   }
    00252 }
    00253
    00259 uint32 GRFParameterInfo::GetValue(struct GRFConfig *config) const
    00260 {
    00261   /* GB doesn't work correctly with nbits == 32, so handle that case here. */
    00262   if (this->num_bit == 32) return config->param[this->param_nr];
    00263   return GB(config->param[this->param_nr], this->first_bit, this->num_bit);
    00264 }
    00265
    00271 void GRFParameterInfo::SetValue(struct GRFConfig *config, uint32 value)
    00272 {
    00273   /* SB doesn't work correctly with nbits == 32, so handle that case here. */
    00274   if (this->num_bit == 32) {
    00275     config->param[this->param_nr] = value;
    00276   } else {
    00277     SB(config->param[this->param_nr], this->first_bit, this->num_bit, value);
    00278   }
    00279   config->num_params = max<uint>(config->num_params, this->param_nr + 1);
    00280   SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
    00281 }
    00282
    00289 bool UpdateNewGRFConfigPalette(int32 p1)
    00290 {
    00291   for (GRFConfig *c = _grfconfig_newgame; c != NULL; c = c->next) c->SetSuitablePalette();
    00292   for (GRFConfig *c = _grfconfig_static;  c != NULL; c = c->next) c->SetSuitablePalette();
    00293   for (GRFConfig *c = _all_grfs;          c != NULL; c = c->next) c->SetSuitablePalette();
    00294   return true;
    00295 }
    00296
    00302 size_t GRFGetSizeOfDataSection(FILE *f)
    00303 {
    00304   extern const byte _grf_cont_v2_sig[];
    00305   static const uint header_len = 14;
    00306
    00307   byte data[header_len];
    00308   if (fread(data, 1, header_len, f) == header_len) {
    00309     if (data[0] == 0 && data[1] == 0 && MemCmpT(data + 2, _grf_cont_v2_sig, 8) == 0) {
    00310       /* Valid container version 2, get data section size. */
    00311       size_t offset = (data[13] << 24) | (data[12] << 16) | (data[11] << 8) | data[10];
    00312       return header_len + offset;
    00313     }
    00314   }
    00315
    00316   return SIZE_MAX;
    00317 }
    00318
    00325 static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir)
    00326 {
    00327   FILE *f;
    00328   Md5 checksum;
    00329   uint8 buffer[1024];
    00330   size_t len, size;
    00331
    00332   /* open the file */
    00333   f = FioFOpenFile(config->filename, "rb", subdir, &size);
    00334   if (f == NULL) return false;
    00335
    00336   size_t start = ftell(f);
    00337   size = min(size, GRFGetSizeOfDataSection(f));
    00338   fseek(f, start, SEEK_SET);
    00339
    00340   /* calculate md5sum */
    00341   while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
    00342     size -= len;
    00343     checksum.Append(buffer, len);
    00344   }
    00345   checksum.Finish(config->ident.md5sum);
    00346
    00347   FioFCloseFile(f);
    00348
    00349   return true;
    00350 }
    00351
    00352
    00360 bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir)
    00361 {
    00362   if (!FioCheckFileExists(config->filename, subdir)) {
    00363     config->status = GCS_NOT_FOUND;
    00364     return false;
    00365   }
    00366
    00367   /* Find and load the Action 8 information */
    00368   LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN, subdir);
    00369   config->SetSuitablePalette();
    00370
    00371   /* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
    00372   if (config->ident.grfid == 0 || config->ident.grfid == 0xFFFFFFFF || config->IsOpenTTDBaseGRF()) return false;
    00373
    00374   if (is_static) {
    00375     /* Perform a 'safety scan' for static GRFs */
    00376     LoadNewGRFFile(config, 62, GLS_SAFETYSCAN, subdir);
    00377
    00378     /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
    00379     if (HasBit(config->flags, GCF_UNSAFE)) return false;
    00380   }
    00381
    00382   return CalcGRFMD5Sum(config, subdir);
    00383 }
    00384
    00385
    00391 void ClearGRFConfigList(GRFConfig **config)
    00392 {
    00393   GRFConfig *c, *next;
    00394   for (c = *config; c != NULL; c = next) {
    00395     next = c->next;
    00396     delete c;
    00397   }
    00398   *config = NULL;
    00399 }
    00400
    00401
    00409 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
    00410 {
    00411   /* Clear destination as it will be overwritten */
    00412   ClearGRFConfigList(dst);
    00413   for (; src != NULL; src = src->next) {
    00414     GRFConfig *c = new GRFConfig(*src);
    00415
    00416     ClrBit(c->flags, GCF_INIT_ONLY);
    00417     if (init_only) SetBit(c->flags, GCF_INIT_ONLY);
    00418
    00419     *dst = c;
    00420     dst = &c->next;
    00421   }
    00422
    00423   return dst;
    00424 }
    00425
    00439 static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
    00440 {
    00441   GRFConfig *prev;
    00442   GRFConfig *cur;
    00443
    00444   if (list == NULL) return;
    00445
    00446   for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) {
    00447     if (cur->ident.grfid != list->ident.grfid) continue;
    00448
    00449     prev->next = cur->next;
    00450     delete cur;
    00451     cur = prev; // Just go back one so it continues as normal later on
    00452   }
    00453
    00454   RemoveDuplicatesFromGRFConfigList(list->next);
    00455 }
    00456
    00461 void AppendStaticGRFConfigs(GRFConfig **dst)
    00462 {
    00463   GRFConfig **tail = dst;
    00464   while (*tail != NULL) tail = &(*tail)->next;
    00465
    00466   CopyGRFConfigList(tail, _grfconfig_static, false);
    00467   RemoveDuplicatesFromGRFConfigList(*dst);
    00468 }
    00469
    00475 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
    00476 {
    00477   GRFConfig **tail = dst;
    00478   while (*tail != NULL) tail = &(*tail)->next;
    00479   *tail = el;
    00480
    00481   RemoveDuplicatesFromGRFConfigList(*dst);
    00482 }
    00483
    00484
    00486 void ResetGRFConfig(bool defaults)
    00487 {
    00488   CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults);
    00489   AppendStaticGRFConfigs(&_grfconfig);
    00490 }
    00491
    00492
    00504 GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
    00505 {
    00506   GRFListCompatibility res = GLC_ALL_GOOD;
    00507
    00508   for (GRFConfig *c = grfconfig; c != NULL; c = c->next) {
    00509     const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
    00510     if (f == NULL || HasBit(f->flags, GCF_INVALID)) {
    00511       char buf[256];
    00512
    00513       /* If we have not found the exactly matching GRF try to find one with the
    00514        * same grfid, as it most likely is compatible */
    00515       f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, NULL, c->version);
    00516       if (f != NULL) {
    00517         md5sumToString(buf, lastof(buf), c->ident.md5sum);
    00518         DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, buf);
    00519         if (!HasBit(c->flags, GCF_COMPATIBLE)) {
    00520           /* Preserve original_md5sum after it has been assigned */
    00521           SetBit(c->flags, GCF_COMPATIBLE);
    00522           memcpy(c->original_md5sum, c->ident.md5sum, sizeof(c->original_md5sum));
    00523         }
    00524
    00525         /* Non-found has precedence over compatibility load */
    00526         if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
    00527         goto compatible_grf;
    00528       }
    00529
    00530       /* No compatible grf was found, mark it as disabled */
    00531       md5sumToString(buf, lastof(buf), c->ident.md5sum);
    00532       DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->ident.grfid), c->filename, buf);
    00533
    00534       c->status = GCS_NOT_FOUND;
    00535       res = GLC_NOT_FOUND;
    00536     } else {
    00537 compatible_grf:
    00538       DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->ident.grfid), f->filename);
    00539       /* The filename could be the filename as in the savegame. As we need
    00540        * to load the GRF here, we need the correct filename, so overwrite that
    00541        * in any case and set the name and info when it is not set already.
    00542        * When the GCF_COPY flag is set, it is certain that the filename is
    00543        * already a local one, so there is no need to replace it. */
    00544       if (!HasBit(c->flags, GCF_COPY)) {
    00545         free(c->filename);
    00546         c->filename = strdup(f->filename);
    00547         memcpy(c->ident.md5sum, f->ident.md5sum, sizeof(c->ident.md5sum));
    00548         c->name->Release();
    00549         c->name = f->name;
    00550         c->name->AddRef();
    00551         c->info->Release();
    00552         c->info = f->name;
    00553         c->info->AddRef();
    00554         c->error = NULL;
    00555         c->version = f->version;
    00556         c->min_loadable_version = f->min_loadable_version;
    00557         c->num_valid_params = f->num_valid_params;
    00558         c->has_param_defaults = f->has_param_defaults;
    00559         for (uint i = 0; i < f->param_info.Length(); i++) {
    00560           if (f->param_info[i] == NULL) {
    00561             *c->param_info.Append() = NULL;
    00562           } else {
    00563             *c->param_info.Append() = new GRFParameterInfo(*f->param_info[i]);
    00564           }
    00565         }
    00566       }
    00567     }
    00568   }
    00569
    00570   return res;
    00571 }
    00572
    00574 class GRFFileScanner : FileScanner {
    00575   uint next_update;
    00576   uint num_scanned;
    00577
    00578 public:
    00579   GRFFileScanner() : next_update(_realtime_tick), num_scanned(0)
    00580   {
    00581   }
    00582
    00583   /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename);
    00584
    00586   static uint DoScan()
    00587   {
    00588     GRFFileScanner fs;
    00589     int ret = fs.Scan(".grf", NEWGRF_DIR);
    00590     /* The number scanned and the number returned may not be the same;
    00591      * duplicate NewGRFs and base sets are ignored in the return value. */
    00592     _settings_client.gui.last_newgrf_count = fs.num_scanned;
    00593     return ret;
    00594   }
    00595 };
    00596
    00597 bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
    00598 {
    00599   GRFConfig *c = new GRFConfig(filename + basepath_length);
    00600
    00601   bool added = true;
    00602   if (FillGRFDetails(c, false)) {
    00603     if (_all_grfs == NULL) {
    00604       _all_grfs = c;
    00605     } else {
    00606       /* Insert file into list at a position determined by its
    00607        * name, so the list is sorted as we go along */
    00608       GRFConfig **pd, *d;
    00609       bool stop = false;
    00610       for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
    00611         if (c->ident.grfid == d->ident.grfid && memcmp(c->ident.md5sum, d->ident.md5sum, sizeof(c->ident.md5sum)) == 0) added = false;
    00612         /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
    00613          *  before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
    00614          *  just after the first with the same name. Avoids doubles in the list. */
    00615         if (strcasecmp(c->GetName(), d->GetName()) <= 0) {
    00616           stop = true;
    00617         } else if (stop) {
    00618           break;
    00619         }
    00620       }
    00621       if (added) {
    00622         c->next = d;
    00623         *pd = c;
    00624       }
    00625     }
    00626   } else {
    00627     added = false;
    00628   }
    00629
    00630   this->num_scanned++;
    00631   if (this->next_update <= _realtime_tick) {
    00632     _modal_progress_work_mutex->EndCritical();
    00633     _modal_progress_paint_mutex->BeginCritical();
    00634
    00635     const char *name = NULL;
    00636     if (c->name != NULL) name = GetGRFStringFromGRFText(c->name->text);
    00637     if (name == NULL) name = c->filename;
    00638     UpdateNewGRFScanStatus(this->num_scanned, name);
    00639
    00640     _modal_progress_work_mutex->BeginCritical();
    00641     _modal_progress_paint_mutex->EndCritical();
    00642
    00643     this->next_update = _realtime_tick + 200;
    00644   }
    00645
    00646   if (!added) {
    00647     /* File couldn't be opened, or is either not a NewGRF or is a
    00648      * 'system' NewGRF or it's already known, so forget about it. */
    00649     delete c;
    00650   }
    00651
    00652   return added;
    00653 }
    00654
    00661 static int CDECL GRFSorter(GRFConfig * const *p1, GRFConfig * const *p2)
    00662 {
    00663   const GRFConfig *c1 = *p1;
    00664   const GRFConfig *c2 = *p2;
    00665
    00666   return strcasecmp(c1->GetName(), c2->GetName());
    00667 }
    00668
    00673 void DoScanNewGRFFiles(void *callback)
    00674 {
    00675   _modal_progress_work_mutex->BeginCritical();
    00676
    00677   ClearGRFConfigList(&_all_grfs);
    00678   TarScanner::DoScan(TarScanner::NEWGRF);
    00679
    00680   DEBUG(grf, 1, "Scanning for NewGRFs");
    00681   uint num = GRFFileScanner::DoScan();
    00682
    00683   DEBUG(grf, 1, "Scan complete, found %d files", num);
    00684   if (num != 0 && _all_grfs != NULL) {
    00685     /* Sort the linked list using quicksort.
    00686     * For that we first have to make an array, then sort and
    00687     * then remake the linked list. */
    00688     GRFConfig **to_sort = MallocT<GRFConfig*>(num);
    00689
    00690     uint i = 0;
    00691     for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) {
    00692       to_sort[i] = p;
    00693     }
    00694     /* Number of files is not necessarily right */
    00695     num = i;
    00696
    00697     QSortT(to_sort, num, &GRFSorter);
    00698
    00699     for (i = 1; i < num; i++) {
    00700       to_sort[i - 1]->next = to_sort[i];
    00701     }
    00702     to_sort[num - 1]->next = NULL;
    00703     _all_grfs = to_sort[0];
    00704
    00705     free(to_sort);
    00706
    00707 #ifdef ENABLE_NETWORK
    00708     NetworkAfterNewGRFScan();
    00709 #endif
    00710   }
    00711
    00712   _modal_progress_work_mutex->EndCritical();
    00713   _modal_progress_paint_mutex->BeginCritical();
    00714
    00715   /* Yes... these are the NewGRF windows */
    00716   InvalidateWindowClassesData(WC_SAVELOAD, 0, true);
    00717   InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE, GOID_NEWGRF_RESCANNED, true);
    00718   if (callback != NULL) ((NewGRFScanCallback*)callback)->OnNewGRFsScanned();
    00719
    00720   DeleteWindowByClass(WC_MODAL_PROGRESS);
    00721   SetModalProgress(false);
    00722   MarkWholeScreenDirty();
    00723   _modal_progress_paint_mutex->EndCritical();
    00724 }
    00725
    00730 void ScanNewGRFFiles(NewGRFScanCallback *callback)
    00731 {
    00732   /* First set the modal progress. This ensures that it will eventually let go of the paint mutex. */
    00733   SetModalProgress(true);
    00734   /* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */
    00735   MarkWholeScreenDirty();
    00736
    00737   if (!_video_driver->HasGUI() || !ThreadObject::New(&DoScanNewGRFFiles, callback, NULL)) {
    00738     _modal_progress_work_mutex->EndCritical();
    00739     _modal_progress_paint_mutex->EndCritical();
    00740     DoScanNewGRFFiles(callback);
    00741     _modal_progress_paint_mutex->BeginCritical();
    00742     _modal_progress_work_mutex->BeginCritical();
    00743   } else {
    00744     UpdateNewGRFScanStatus(0, NULL);
    00745   }
    00746 }
    00747
    00756 const GRFConfig *FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
    00757 {
    00758   assert((mode == FGCM_EXACT) != (md5sum == NULL));
    00759   const GRFConfig *best = NULL;
    00760   for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
    00761     /* if md5sum is set, we look for an exact match and continue if not found */
    00762     if (!c->ident.HasGrfIdentifier(grfid, md5sum)) continue;
    00763     /* return it, if the exact same newgrf is found, or if we do not care about finding "the best" */
    00764     if (md5sum != NULL || mode == FGCM_ANY) return c;
    00765     /* Skip incompatible stuff, unless explicitly allowed */
    00766     if (mode != FGCM_NEWEST && HasBit(c->flags, GCF_INVALID)) continue;
    00767     /* check version compatibility */
    00768     if (mode == FGCM_COMPATIBLE && (c->version < desired_version || c->min_loadable_version > desired_version)) continue;
    00769     /* remember the newest one as "the best" */
    00770     if (best == NULL || c->version > best->version) best = c;
    00771   }
    00772
    00773   return best;
    00774 }
    00775
    00776 #ifdef ENABLE_NETWORK
    00777
    00779 struct UnknownGRF : public GRFIdentifier {
    00780   UnknownGRF *next;
    00781   GRFTextWrapper *name;
    00782 };
    00783
    00801 GRFTextWrapper *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
    00802 {
    00803   UnknownGRF *grf;
    00804   static UnknownGRF *unknown_grfs = NULL;
    00805
    00806   for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
    00807     if (grf->grfid == grfid) {
    00808       if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
    00809     }
    00810   }
    00811
    00812   if (!create) return NULL;
    00813
    00814   grf = CallocT<UnknownGRF>(1);
    00815   grf->grfid = grfid;
    00816   grf->next  = unknown_grfs;
    00817   grf->name = new GRFTextWrapper();
    00818   grf->name->AddRef();
    00819
    00820   AddGRFTextToList(&grf->name->text, UNKNOWN_GRF_NAME_PLACEHOLDER);
    00821   memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
    00822
    00823   unknown_grfs = grf;
    00824   return grf->name;
    00825 }
    00826
    00827 #endif /* ENABLE_NETWORK */
    00828
    00829
    00836 GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask)
    00837 {
    00838   GRFConfig *c;
    00839
    00840   for (c = _grfconfig; c != NULL; c = c->next) {
    00841     if ((c->ident.grfid & mask) == (grfid & mask)) return c;
    00842   }
    00843
    00844   return NULL;
    00845 }
    00846
    00847
    00849 char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
    00850 {
    00851   uint i;
    00852
    00853   /* Return an empty string if there are no parameters */
    00854   if (c->num_params == 0) return strecpy(dst, "", last);
    00855
    00856   for (i = 0; i < c->num_params; i++) {
    00857     if (i > 0) dst = strecpy(dst, " ", last);
    00858     dst += seprintf(dst, last, "%d", c->param[i]);
    00859   }
    00860   return dst;
    00861 }
    00862
    00864 static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400);
    00865
    00870 bool GRFConfig::IsOpenTTDBaseGRF() const
    00871 {
    00872   return (this->ident.grfid & 0x00FFFFFF) == OPENTTD_GRAPHICS_BASE_GRF_ID;
    00873 }
    00874
    00880 const char *GRFConfig::GetTextfile(TextfileType type) const
    00881 {
    00882   return ::GetTextfile(type, NEWGRF_DIR, this->filename);
    00883 }