# newgrf_config.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_config.cpp 24318 2012-06-01 15:20:18Z frosch $ */
    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
    00170 void GRFConfig::FinalizeParameterInfo()
    00171 {
    00172   for (GRFParameterInfo **info = this->param_info.Begin(); info != this->param_info.End(); ++info) {
    00173     if (*info == NULL) continue;
    00174     (*info)->Finalize();
    00175   }
    00176 }
    00177
    00178 GRFConfig *_all_grfs;
    00179 GRFConfig *_grfconfig;
    00180 GRFConfig *_grfconfig_newgame;
    00181 GRFConfig *_grfconfig_static;
    00182
    00188 GRFError::GRFError(StringID severity, StringID message) :
    00189   message(message),
    00190   severity(severity)
    00191 {
    00192 }
    00193
    00198 GRFError::GRFError(const GRFError &error) :
    00199   ZeroedMemoryAllocator(),
    00200   custom_message(error.custom_message),
    00201   data(error.data),
    00202   message(error.message),
    00203   severity(error.severity)
    00204 {
    00205   if (error.custom_message != NULL) this->custom_message = strdup(error.custom_message);
    00206   if (error.data           != NULL) this->data           = strdup(error.data);
    00207   memcpy(this->param_value, error.param_value, sizeof(this->param_value));
    00208 }
    00209
    00210 GRFError::~GRFError()
    00211 {
    00212   free(this->custom_message);
    00213   free(this->data);
    00214 }
    00215
    00220 GRFParameterInfo::GRFParameterInfo(uint nr) :
    00221   name(NULL),
    00222   desc(NULL),
    00223   type(PTYPE_UINT_ENUM),
    00224   min_value(0),
    00225   max_value(UINT32_MAX),
    00226   def_value(0),
    00227   param_nr(nr),
    00228   first_bit(0),
    00229   num_bit(32)
    00230 {}
    00231
    00237 GRFParameterInfo::GRFParameterInfo(GRFParameterInfo &info) :
    00238   name(DuplicateGRFText(info.name)),
    00239   desc(DuplicateGRFText(info.desc)),
    00240   type(info.type),
    00241   min_value(info.min_value),
    00242   max_value(info.max_value),
    00243   def_value(info.def_value),
    00244   param_nr(info.param_nr),
    00245   first_bit(info.first_bit),
    00246   num_bit(info.num_bit),
    00247   complete_labels(info.complete_labels)
    00248 {
    00249   for (uint i = 0; i < info.value_names.Length(); i++) {
    00250     SmallPair<uint32, GRFText *> *data = info.value_names.Get(i);
    00251     this->value_names.Insert(data->first, DuplicateGRFText(data->second));
    00252   }
    00253 }
    00254
    00256 GRFParameterInfo::~GRFParameterInfo()
    00257 {
    00258   CleanUpGRFText(this->name);
    00259   CleanUpGRFText(this->desc);
    00260   for (uint i = 0; i < this->value_names.Length(); i++) {
    00261     SmallPair<uint32, GRFText *> *data = this->value_names.Get(i);
    00262     CleanUpGRFText(data->second);
    00263   }
    00264 }
    00265
    00271 uint32 GRFParameterInfo::GetValue(struct GRFConfig *config) const
    00272 {
    00273   /* GB doesn't work correctly with nbits == 32, so handle that case here. */
    00274   if (this->num_bit == 32) return config->param[this->param_nr];
    00275   return GB(config->param[this->param_nr], this->first_bit, this->num_bit);
    00276 }
    00277
    00283 void GRFParameterInfo::SetValue(struct GRFConfig *config, uint32 value)
    00284 {
    00285   /* SB doesn't work correctly with nbits == 32, so handle that case here. */
    00286   if (this->num_bit == 32) {
    00287     config->param[this->param_nr] = value;
    00288   } else {
    00289     SB(config->param[this->param_nr], this->first_bit, this->num_bit, value);
    00290   }
    00291   config->num_params = max<uint>(config->num_params, this->param_nr + 1);
    00292   SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
    00293 }
    00294
    00298 void GRFParameterInfo::Finalize()
    00299 {
    00300   this->complete_labels = true;
    00301   for (uint32 value = this->min_value; value <= this->max_value; value++) {
    00302     if (!this->value_names.Contains(value)) {
    00303       this->complete_labels = false;
    00304       break;
    00305     }
    00306   }
    00307 }
    00308
    00315 bool UpdateNewGRFConfigPalette(int32 p1)
    00316 {
    00317   for (GRFConfig *c = _grfconfig_newgame; c != NULL; c = c->next) c->SetSuitablePalette();
    00318   for (GRFConfig *c = _grfconfig_static;  c != NULL; c = c->next) c->SetSuitablePalette();
    00319   for (GRFConfig *c = _all_grfs;          c != NULL; c = c->next) c->SetSuitablePalette();
    00320   return true;
    00321 }
    00322
    00328 size_t GRFGetSizeOfDataSection(FILE *f)
    00329 {
    00330   extern const byte _grf_cont_v2_sig[];
    00331   static const uint header_len = 14;
    00332
    00333   byte data[header_len];
    00334   if (fread(data, 1, header_len, f) == header_len) {
    00335     if (data[0] == 0 && data[1] == 0 && MemCmpT(data + 2, _grf_cont_v2_sig, 8) == 0) {
    00336       /* Valid container version 2, get data section size. */
    00337       size_t offset = (data[13] << 24) | (data[12] << 16) | (data[11] << 8) | data[10];
    00338       return header_len + offset;
    00339     }
    00340   }
    00341
    00342   return SIZE_MAX;
    00343 }
    00344
    00351 static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir)
    00352 {
    00353   FILE *f;
    00354   Md5 checksum;
    00355   uint8 buffer[1024];
    00356   size_t len, size;
    00357
    00358   /* open the file */
    00359   f = FioFOpenFile(config->filename, "rb", subdir, &size);
    00360   if (f == NULL) return false;
    00361
    00362   size_t start = ftell(f);
    00363   size = min(size, GRFGetSizeOfDataSection(f));
    00364   fseek(f, start, SEEK_SET);
    00365
    00366   /* calculate md5sum */
    00367   while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
    00368     size -= len;
    00369     checksum.Append(buffer, len);
    00370   }
    00371   checksum.Finish(config->ident.md5sum);
    00372
    00373   FioFCloseFile(f);
    00374
    00375   return true;
    00376 }
    00377
    00378
    00386 bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir)
    00387 {
    00388   if (!FioCheckFileExists(config->filename, subdir)) {
    00389     config->status = GCS_NOT_FOUND;
    00390     return false;
    00391   }
    00392
    00393   /* Find and load the Action 8 information */
    00394   LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN, subdir);
    00395   config->SetSuitablePalette();
    00396   config->FinalizeParameterInfo();
    00397
    00398   /* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
    00399   if (config->ident.grfid == 0 || config->ident.grfid == 0xFFFFFFFF || config->IsOpenTTDBaseGRF()) return false;
    00400
    00401   if (is_static) {
    00402     /* Perform a 'safety scan' for static GRFs */
    00403     LoadNewGRFFile(config, 62, GLS_SAFETYSCAN, subdir);
    00404
    00405     /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
    00406     if (HasBit(config->flags, GCF_UNSAFE)) return false;
    00407   }
    00408
    00409   return CalcGRFMD5Sum(config, subdir);
    00410 }
    00411
    00412
    00418 void ClearGRFConfigList(GRFConfig **config)
    00419 {
    00420   GRFConfig *c, *next;
    00421   for (c = *config; c != NULL; c = next) {
    00422     next = c->next;
    00423     delete c;
    00424   }
    00425   *config = NULL;
    00426 }
    00427
    00428
    00436 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
    00437 {
    00438   /* Clear destination as it will be overwritten */
    00439   ClearGRFConfigList(dst);
    00440   for (; src != NULL; src = src->next) {
    00441     GRFConfig *c = new GRFConfig(*src);
    00442
    00443     ClrBit(c->flags, GCF_INIT_ONLY);
    00444     if (init_only) SetBit(c->flags, GCF_INIT_ONLY);
    00445
    00446     *dst = c;
    00447     dst = &c->next;
    00448   }
    00449
    00450   return dst;
    00451 }
    00452
    00466 static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
    00467 {
    00468   GRFConfig *prev;
    00469   GRFConfig *cur;
    00470
    00471   if (list == NULL) return;
    00472
    00473   for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) {
    00474     if (cur->ident.grfid != list->ident.grfid) continue;
    00475
    00476     prev->next = cur->next;
    00477     delete cur;
    00478     cur = prev; // Just go back one so it continues as normal later on
    00479   }
    00480
    00481   RemoveDuplicatesFromGRFConfigList(list->next);
    00482 }
    00483
    00488 void AppendStaticGRFConfigs(GRFConfig **dst)
    00489 {
    00490   GRFConfig **tail = dst;
    00491   while (*tail != NULL) tail = &(*tail)->next;
    00492
    00493   CopyGRFConfigList(tail, _grfconfig_static, false);
    00494   RemoveDuplicatesFromGRFConfigList(*dst);
    00495 }
    00496
    00502 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
    00503 {
    00504   GRFConfig **tail = dst;
    00505   while (*tail != NULL) tail = &(*tail)->next;
    00506   *tail = el;
    00507
    00508   RemoveDuplicatesFromGRFConfigList(*dst);
    00509 }
    00510
    00511
    00513 void ResetGRFConfig(bool defaults)
    00514 {
    00515   CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults);
    00516   AppendStaticGRFConfigs(&_grfconfig);
    00517 }
    00518
    00519
    00531 GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
    00532 {
    00533   GRFListCompatibility res = GLC_ALL_GOOD;
    00534
    00535   for (GRFConfig *c = grfconfig; c != NULL; c = c->next) {
    00536     const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
    00537     if (f == NULL || HasBit(f->flags, GCF_INVALID)) {
    00538       char buf[256];
    00539
    00540       /* If we have not found the exactly matching GRF try to find one with the
    00541        * same grfid, as it most likely is compatible */
    00542       f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, NULL, c->version);
    00543       if (f != NULL) {
    00544         md5sumToString(buf, lastof(buf), c->ident.md5sum);
    00545         DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, buf);
    00546         if (!HasBit(c->flags, GCF_COMPATIBLE)) {
    00547           /* Preserve original_md5sum after it has been assigned */
    00548           SetBit(c->flags, GCF_COMPATIBLE);
    00549           memcpy(c->original_md5sum, c->ident.md5sum, sizeof(c->original_md5sum));
    00550         }
    00551
    00552         /* Non-found has precedence over compatibility load */
    00553         if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
    00554         goto compatible_grf;
    00555       }
    00556
    00557       /* No compatible grf was found, mark it as disabled */
    00558       md5sumToString(buf, lastof(buf), c->ident.md5sum);
    00559       DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->ident.grfid), c->filename, buf);
    00560
    00561       c->status = GCS_NOT_FOUND;
    00562       res = GLC_NOT_FOUND;
    00563     } else {
    00564 compatible_grf:
    00565       DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->ident.grfid), f->filename);
    00566       /* The filename could be the filename as in the savegame. As we need
    00567        * to load the GRF here, we need the correct filename, so overwrite that
    00568        * in any case and set the name and info when it is not set already.
    00569        * When the GCF_COPY flag is set, it is certain that the filename is
    00570        * already a local one, so there is no need to replace it. */
    00571       if (!HasBit(c->flags, GCF_COPY)) {
    00572         free(c->filename);
    00573         c->filename = strdup(f->filename);
    00574         memcpy(c->ident.md5sum, f->ident.md5sum, sizeof(c->ident.md5sum));
    00575         c->name->Release();
    00576         c->name = f->name;
    00577         c->name->AddRef();
    00578         c->info->Release();
    00579         c->info = f->name;
    00580         c->info->AddRef();
    00581         c->error = NULL;
    00582         c->version = f->version;
    00583         c->min_loadable_version = f->min_loadable_version;
    00584         c->num_valid_params = f->num_valid_params;
    00585         c->has_param_defaults = f->has_param_defaults;
    00586         for (uint i = 0; i < f->param_info.Length(); i++) {
    00587           if (f->param_info[i] == NULL) {
    00588             *c->param_info.Append() = NULL;
    00589           } else {
    00590             *c->param_info.Append() = new GRFParameterInfo(*f->param_info[i]);
    00591           }
    00592         }
    00593       }
    00594     }
    00595   }
    00596
    00597   return res;
    00598 }
    00599
    00601 class GRFFileScanner : FileScanner {
    00602   uint next_update;
    00603   uint num_scanned;
    00604
    00605 public:
    00606   GRFFileScanner() : next_update(_realtime_tick), num_scanned(0)
    00607   {
    00608   }
    00609
    00610   /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename);
    00611
    00613   static uint DoScan()
    00614   {
    00615     GRFFileScanner fs;
    00616     int ret = fs.Scan(".grf", NEWGRF_DIR);
    00617     /* The number scanned and the number returned may not be the same;
    00618      * duplicate NewGRFs and base sets are ignored in the return value. */
    00619     _settings_client.gui.last_newgrf_count = fs.num_scanned;
    00620     return ret;
    00621   }
    00622 };
    00623
    00624 bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
    00625 {
    00626   GRFConfig *c = new GRFConfig(filename + basepath_length);
    00627
    00628   bool added = true;
    00629   if (FillGRFDetails(c, false)) {
    00630     if (_all_grfs == NULL) {
    00631       _all_grfs = c;
    00632     } else {
    00633       /* Insert file into list at a position determined by its
    00634        * name, so the list is sorted as we go along */
    00635       GRFConfig **pd, *d;
    00636       bool stop = false;
    00637       for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
    00638         if (c->ident.grfid == d->ident.grfid && memcmp(c->ident.md5sum, d->ident.md5sum, sizeof(c->ident.md5sum)) == 0) added = false;
    00639         /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
    00640          *  before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
    00641          *  just after the first with the same name. Avoids doubles in the list. */
    00642         if (strcasecmp(c->GetName(), d->GetName()) <= 0) {
    00643           stop = true;
    00644         } else if (stop) {
    00645           break;
    00646         }
    00647       }
    00648       if (added) {
    00649         c->next = d;
    00650         *pd = c;
    00651       }
    00652     }
    00653   } else {
    00654     added = false;
    00655   }
    00656
    00657   this->num_scanned++;
    00658   if (this->next_update <= _realtime_tick) {
    00659     _modal_progress_work_mutex->EndCritical();
    00660     _modal_progress_paint_mutex->BeginCritical();
    00661
    00662     const char *name = NULL;
    00663     if (c->name != NULL) name = GetGRFStringFromGRFText(c->name->text);
    00664     if (name == NULL) name = c->filename;
    00665     UpdateNewGRFScanStatus(this->num_scanned, name);
    00666
    00667     _modal_progress_work_mutex->BeginCritical();
    00668     _modal_progress_paint_mutex->EndCritical();
    00669
    00670     this->next_update = _realtime_tick + 200;
    00671   }
    00672
    00673   if (!added) {
    00674     /* File couldn't be opened, or is either not a NewGRF or is a
    00675      * 'system' NewGRF or it's already known, so forget about it. */
    00676     delete c;
    00677   }
    00678
    00679   return added;
    00680 }
    00681
    00688 static int CDECL GRFSorter(GRFConfig * const *p1, GRFConfig * const *p2)
    00689 {
    00690   const GRFConfig *c1 = *p1;
    00691   const GRFConfig *c2 = *p2;
    00692
    00693   return strcasecmp(c1->GetName(), c2->GetName());
    00694 }
    00695
    00700 void DoScanNewGRFFiles(void *callback)
    00701 {
    00702   _modal_progress_work_mutex->BeginCritical();
    00703
    00704   ClearGRFConfigList(&_all_grfs);
    00705   TarScanner::DoScan(TarScanner::NEWGRF);
    00706
    00707   DEBUG(grf, 1, "Scanning for NewGRFs");
    00708   uint num = GRFFileScanner::DoScan();
    00709
    00710   DEBUG(grf, 1, "Scan complete, found %d files", num);
    00711   if (num != 0 && _all_grfs != NULL) {
    00712     /* Sort the linked list using quicksort.
    00713     * For that we first have to make an array, then sort and
    00714     * then remake the linked list. */
    00715     GRFConfig **to_sort = MallocT<GRFConfig*>(num);
    00716
    00717     uint i = 0;
    00718     for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) {
    00719       to_sort[i] = p;
    00720     }
    00721     /* Number of files is not necessarily right */
    00722     num = i;
    00723
    00724     QSortT(to_sort, num, &GRFSorter);
    00725
    00726     for (i = 1; i < num; i++) {
    00727       to_sort[i - 1]->next = to_sort[i];
    00728     }
    00729     to_sort[num - 1]->next = NULL;
    00730     _all_grfs = to_sort[0];
    00731
    00732     free(to_sort);
    00733
    00734 #ifdef ENABLE_NETWORK
    00735     NetworkAfterNewGRFScan();
    00736 #endif
    00737   }
    00738
    00739   _modal_progress_work_mutex->EndCritical();
    00740   _modal_progress_paint_mutex->BeginCritical();
    00741
    00742   /* Yes... these are the NewGRF windows */
    00743   InvalidateWindowClassesData(WC_SAVELOAD, 0, true);
    00744   InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE, GOID_NEWGRF_RESCANNED, true);
    00745   if (callback != NULL) ((NewGRFScanCallback*)callback)->OnNewGRFsScanned();
    00746
    00747   DeleteWindowByClass(WC_MODAL_PROGRESS);
    00748   SetModalProgress(false);
    00749   MarkWholeScreenDirty();
    00750   _modal_progress_paint_mutex->EndCritical();
    00751 }
    00752
    00757 void ScanNewGRFFiles(NewGRFScanCallback *callback)
    00758 {
    00759   /* First set the modal progress. This ensures that it will eventually let go of the paint mutex. */
    00760   SetModalProgress(true);
    00761   /* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */
    00762   MarkWholeScreenDirty();
    00763
    00764   if (!_video_driver->HasGUI() || !ThreadObject::New(&DoScanNewGRFFiles, callback, NULL)) {
    00765     _modal_progress_work_mutex->EndCritical();
    00766     _modal_progress_paint_mutex->EndCritical();
    00767     DoScanNewGRFFiles(callback);
    00768     _modal_progress_paint_mutex->BeginCritical();
    00769     _modal_progress_work_mutex->BeginCritical();
    00770   } else {
    00771     UpdateNewGRFScanStatus(0, NULL);
    00772   }
    00773 }
    00774
    00783 const GRFConfig *FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
    00784 {
    00785   assert((mode == FGCM_EXACT) != (md5sum == NULL));
    00786   const GRFConfig *best = NULL;
    00787   for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
    00788     /* if md5sum is set, we look for an exact match and continue if not found */
    00789     if (!c->ident.HasGrfIdentifier(grfid, md5sum)) continue;
    00790     /* return it, if the exact same newgrf is found, or if we do not care about finding "the best" */
    00791     if (md5sum != NULL || mode == FGCM_ANY) return c;
    00792     /* Skip incompatible stuff, unless explicitly allowed */
    00793     if (mode != FGCM_NEWEST && HasBit(c->flags, GCF_INVALID)) continue;
    00794     /* check version compatibility */
    00795     if (mode == FGCM_COMPATIBLE && (c->version < desired_version || c->min_loadable_version > desired_version)) continue;
    00796     /* remember the newest one as "the best" */
    00797     if (best == NULL || c->version > best->version) best = c;
    00798   }
    00799
    00800   return best;
    00801 }
    00802
    00803 #ifdef ENABLE_NETWORK
    00804
    00806 struct UnknownGRF : public GRFIdentifier {
    00807   UnknownGRF *next;
    00808   GRFTextWrapper *name;
    00809 };
    00810
    00828 GRFTextWrapper *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
    00829 {
    00830   UnknownGRF *grf;
    00831   static UnknownGRF *unknown_grfs = NULL;
    00832
    00833   for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
    00834     if (grf->grfid == grfid) {
    00835       if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
    00836     }
    00837   }
    00838
    00839   if (!create) return NULL;
    00840
    00841   grf = CallocT<UnknownGRF>(1);
    00842   grf->grfid = grfid;
    00843   grf->next  = unknown_grfs;
    00844   grf->name = new GRFTextWrapper();
    00845   grf->name->AddRef();
    00846
    00847   AddGRFTextToList(&grf->name->text, UNKNOWN_GRF_NAME_PLACEHOLDER);
    00848   memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
    00849
    00850   unknown_grfs = grf;
    00851   return grf->name;
    00852 }
    00853
    00854 #endif /* ENABLE_NETWORK */
    00855
    00856
    00863 GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask)
    00864 {
    00865   GRFConfig *c;
    00866
    00867   for (c = _grfconfig; c != NULL; c = c->next) {
    00868     if ((c->ident.grfid & mask) == (grfid & mask)) return c;
    00869   }
    00870
    00871   return NULL;
    00872 }
    00873
    00874
    00876 char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
    00877 {
    00878   uint i;
    00879
    00880   /* Return an empty string if there are no parameters */
    00881   if (c->num_params == 0) return strecpy(dst, "", last);
    00882
    00883   for (i = 0; i < c->num_params; i++) {
    00884     if (i > 0) dst = strecpy(dst, " ", last);
    00885     dst += seprintf(dst, last, "%d", c->param[i]);
    00886   }
    00887   return dst;
    00888 }
    00889
    00891 static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400);
    00892
    00897 bool GRFConfig::IsOpenTTDBaseGRF() const
    00898 {
    00899   return (this->ident.grfid & 0x00FFFFFF) == OPENTTD_GRAPHICS_BASE_GRF_ID;
    00900 }
    00901
    00907 const char *GRFConfig::GetTextfile(TextfileType type) const
    00908 {
    00909   return ::GetTextfile(type, NEWGRF_DIR, this->filename);
    00910 }