# 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
    00024 #include "fileio_func.h"
    00025 #include "fios.h"
    00026
    00028 GRFTextWrapper::GRFTextWrapper() :
    00029   text(NULL)
    00030 {
    00031 }
    00032
    00034 GRFTextWrapper::~GRFTextWrapper()
    00035 {
    00036   CleanUpGRFText(this->text);
    00037 }
    00038
    00044 GRFConfig::GRFConfig(const char *filename) :
    00045   name(new GRFTextWrapper()),
    00046   info(new GRFTextWrapper()),
    00047   url(new GRFTextWrapper()),
    00048   num_valid_params(lengthof(param))
    00049 {
    00050   if (filename != NULL) this->filename = strdup(filename);
    00051   this->name->AddRef();
    00052   this->info->AddRef();
    00053   this->url->AddRef();
    00054 }
    00055
    00060 GRFConfig::GRFConfig(const GRFConfig &config) :
    00061   ZeroedMemoryAllocator(),
    00062   ident(config.ident),
    00063   name(config.name),
    00064   info(config.info),
    00065   url(config.url),
    00066   version(config.version),
    00067   min_loadable_version(config.min_loadable_version),
    00068   flags(config.flags & ~(1 << GCF_COPY)),
    00069   status(config.status),
    00070   grf_bugs(config.grf_bugs),
    00071   num_params(config.num_params),
    00072   num_valid_params(config.num_valid_params),
    00073   palette(config.palette),
    00074   has_param_defaults(config.has_param_defaults)
    00075 {
    00076   MemCpyT<uint8>(this->original_md5sum, config.original_md5sum, lengthof(this->original_md5sum));
    00077   MemCpyT<uint32>(this->param, config.param, lengthof(this->param));
    00078   if (config.filename != NULL) this->filename = strdup(config.filename);
    00079   this->name->AddRef();
    00080   this->info->AddRef();
    00081   this->url->AddRef();
    00082   if (config.error    != NULL) this->error    = new GRFError(*config.error);
    00083   for (uint i = 0; i < config.param_info.Length(); i++) {
    00084     if (config.param_info[i] == NULL) {
    00085       *this->param_info.Append() = NULL;
    00086     } else {
    00087       *this->param_info.Append() = new GRFParameterInfo(*config.param_info[i]);
    00088     }
    00089   }
    00090 }
    00091
    00093 GRFConfig::~GRFConfig()
    00094 {
    00095   /* GCF_COPY as in NOT strdupped/alloced the filename */
    00096   if (!HasBit(this->flags, GCF_COPY)) {
    00097     free(this->filename);
    00098     delete this->error;
    00099   }
    00100   this->name->Release();
    00101   this->info->Release();
    00102   this->url->Release();
    00103
    00104   for (uint i = 0; i < this->param_info.Length(); i++) delete this->param_info[i];
    00105 }
    00106
    00112 const char *GRFConfig::GetName() const
    00113 {
    00114   const char *name = GetGRFStringFromGRFText(this->name->text);
    00115   return StrEmpty(name) ? this->filename : name;
    00116 }
    00117
    00122 const char *GRFConfig::GetDescription() const
    00123 {
    00124   return GetGRFStringFromGRFText(this->info->text);
    00125 }
    00126
    00131 const char *GRFConfig::GetURL() const
    00132 {
    00133   return GetGRFStringFromGRFText(this->url->text);
    00134 }
    00135
    00137 void GRFConfig::SetParameterDefaults()
    00138 {
    00139   this->num_params = 0;
    00140   MemSetT<uint32>(this->param, 0, lengthof(this->param));
    00141
    00142   if (!this->has_param_defaults) return;
    00143
    00144   for (uint i = 0; i < this->param_info.Length(); i++) {
    00145     if (this->param_info[i] == NULL) continue;
    00146     this->param_info[i]->SetValue(this, this->param_info[i]->def_value);
    00147   }
    00148 }
    00149
    00155 void GRFConfig::SetSuitablePalette()
    00156 {
    00157   PaletteType pal;
    00158   switch (this->palette & GRFP_GRF_MASK) {
    00159     case GRFP_GRF_DOS:     pal = PAL_DOS;      break;
    00160     case GRFP_GRF_WINDOWS: pal = PAL_WINDOWS;  break;
    00161     default:               pal = _settings_client.gui.newgrf_default_palette == 1 ? PAL_WINDOWS : PAL_DOS; break;
    00162   }
    00163   SB(this->palette, GRFP_USE_BIT, 1, pal == PAL_WINDOWS ? GRFP_USE_WINDOWS : GRFP_USE_DOS);
    00164 }
    00165
    00166 GRFConfig *_all_grfs;
    00167 GRFConfig *_grfconfig;
    00168 GRFConfig *_grfconfig_newgame;
    00169 GRFConfig *_grfconfig_static;
    00170
    00176 GRFError::GRFError(StringID severity, StringID message) :
    00177   message(message),
    00178   severity(severity)
    00179 {
    00180 }
    00181
    00186 GRFError::GRFError(const GRFError &error) :
    00187   ZeroedMemoryAllocator(),
    00188   custom_message(error.custom_message),
    00189   data(error.data),
    00190   message(error.message),
    00191   severity(error.severity),
    00192   num_params(error.num_params)
    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
    00303 static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir)
    00304 {
    00305   FILE *f;
    00306   Md5 checksum;
    00307   uint8 buffer[1024];
    00308   size_t len, size;
    00309
    00310   /* open the file */
    00311   f = FioFOpenFile(config->filename, "rb", subdir, &size);
    00312   if (f == NULL) return false;
    00313
    00314   /* calculate md5sum */
    00315   while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
    00316     size -= len;
    00317     checksum.Append(buffer, len);
    00318   }
    00319   checksum.Finish(config->ident.md5sum);
    00320
    00321   FioFCloseFile(f);
    00322
    00323   return true;
    00324 }
    00325
    00326
    00334 bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir)
    00335 {
    00336   if (!FioCheckFileExists(config->filename, subdir)) {
    00337     config->status = GCS_NOT_FOUND;
    00338     return false;
    00339   }
    00340
    00341   /* Find and load the Action 8 information */
    00342   LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN, subdir);
    00343   config->SetSuitablePalette();
    00344
    00345   /* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
    00346   if (config->ident.grfid == 0 || config->ident.grfid == 0xFFFFFFFF || config->IsOpenTTDBaseGRF()) return false;
    00347
    00348   if (is_static) {
    00349     /* Perform a 'safety scan' for static GRFs */
    00350     LoadNewGRFFile(config, 62, GLS_SAFETYSCAN, subdir);
    00351
    00352     /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
    00353     if (HasBit(config->flags, GCF_UNSAFE)) return false;
    00354   }
    00355
    00356   return CalcGRFMD5Sum(config, subdir);
    00357 }
    00358
    00359
    00365 void ClearGRFConfigList(GRFConfig **config)
    00366 {
    00367   GRFConfig *c, *next;
    00368   for (c = *config; c != NULL; c = next) {
    00369     next = c->next;
    00370     delete c;
    00371   }
    00372   *config = NULL;
    00373 }
    00374
    00375
    00383 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
    00384 {
    00385   /* Clear destination as it will be overwritten */
    00386   ClearGRFConfigList(dst);
    00387   for (; src != NULL; src = src->next) {
    00388     GRFConfig *c = new GRFConfig(*src);
    00389
    00390     ClrBit(c->flags, GCF_INIT_ONLY);
    00391     if (init_only) SetBit(c->flags, GCF_INIT_ONLY);
    00392
    00393     *dst = c;
    00394     dst = &c->next;
    00395   }
    00396
    00397   return dst;
    00398 }
    00399
    00413 static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
    00414 {
    00415   GRFConfig *prev;
    00416   GRFConfig *cur;
    00417
    00418   if (list == NULL) return;
    00419
    00420   for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) {
    00421     if (cur->ident.grfid != list->ident.grfid) continue;
    00422
    00423     prev->next = cur->next;
    00424     delete cur;
    00425     cur = prev; // Just go back one so it continues as normal later on
    00426   }
    00427
    00428   RemoveDuplicatesFromGRFConfigList(list->next);
    00429 }
    00430
    00435 void AppendStaticGRFConfigs(GRFConfig **dst)
    00436 {
    00437   GRFConfig **tail = dst;
    00438   while (*tail != NULL) tail = &(*tail)->next;
    00439
    00440   CopyGRFConfigList(tail, _grfconfig_static, false);
    00441   RemoveDuplicatesFromGRFConfigList(*dst);
    00442 }
    00443
    00449 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
    00450 {
    00451   GRFConfig **tail = dst;
    00452   while (*tail != NULL) tail = &(*tail)->next;
    00453   *tail = el;
    00454
    00455   RemoveDuplicatesFromGRFConfigList(*dst);
    00456 }
    00457
    00458
    00460 void ResetGRFConfig(bool defaults)
    00461 {
    00462   CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults);
    00463   AppendStaticGRFConfigs(&_grfconfig);
    00464 }
    00465
    00466
    00478 GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
    00479 {
    00480   GRFListCompatibility res = GLC_ALL_GOOD;
    00481
    00482   for (GRFConfig *c = grfconfig; c != NULL; c = c->next) {
    00483     const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
    00484     if (f == NULL || HasBit(f->flags, GCF_INVALID)) {
    00485       char buf[256];
    00486
    00487       /* If we have not found the exactly matching GRF try to find one with the
    00488        * same grfid, as it most likely is compatible */
    00489       f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, NULL, c->version);
    00490       if (f != NULL) {
    00491         md5sumToString(buf, lastof(buf), c->ident.md5sum);
    00492         DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, buf);
    00493         if (!HasBit(c->flags, GCF_COMPATIBLE)) {
    00494           /* Preserve original_md5sum after it has been assigned */
    00495           SetBit(c->flags, GCF_COMPATIBLE);
    00496           memcpy(c->original_md5sum, c->ident.md5sum, sizeof(c->original_md5sum));
    00497         }
    00498
    00499         /* Non-found has precedence over compatibility load */
    00500         if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
    00501         goto compatible_grf;
    00502       }
    00503
    00504       /* No compatible grf was found, mark it as disabled */
    00505       md5sumToString(buf, lastof(buf), c->ident.md5sum);
    00506       DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->ident.grfid), c->filename, buf);
    00507
    00508       c->status = GCS_NOT_FOUND;
    00509       res = GLC_NOT_FOUND;
    00510     } else {
    00511 compatible_grf:
    00512       DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->ident.grfid), f->filename);
    00513       /* The filename could be the filename as in the savegame. As we need
    00514        * to load the GRF here, we need the correct filename, so overwrite that
    00515        * in any case and set the name and info when it is not set already.
    00516        * When the GCF_COPY flag is set, it is certain that the filename is
    00517        * already a local one, so there is no need to replace it. */
    00518       if (!HasBit(c->flags, GCF_COPY)) {
    00519         free(c->filename);
    00520         c->filename = strdup(f->filename);
    00521         memcpy(c->ident.md5sum, f->ident.md5sum, sizeof(c->ident.md5sum));
    00522         c->name->Release();
    00523         c->name = f->name;
    00524         c->name->AddRef();
    00525         c->info->Release();
    00526         c->info = f->name;
    00527         c->info->AddRef();
    00528         c->error = NULL;
    00529         c->version = f->version;
    00530         c->min_loadable_version = f->min_loadable_version;
    00531         c->num_valid_params = f->num_valid_params;
    00532         c->has_param_defaults = f->has_param_defaults;
    00533         for (uint i = 0; i < f->param_info.Length(); i++) {
    00534           if (f->param_info[i] == NULL) {
    00535             *c->param_info.Append() = NULL;
    00536           } else {
    00537             *c->param_info.Append() = new GRFParameterInfo(*f->param_info[i]);
    00538           }
    00539         }
    00540       }
    00541     }
    00542   }
    00543
    00544   return res;
    00545 }
    00546
    00548 class GRFFileScanner : FileScanner {
    00549   uint next_update;
    00550   uint num_scanned;
    00551
    00552 public:
    00553   GRFFileScanner() : next_update(_realtime_tick), num_scanned(0)
    00554   {
    00555   }
    00556
    00557   /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename);
    00558
    00560   static uint DoScan()
    00561   {
    00562     GRFFileScanner fs;
    00563     int ret = fs.Scan(".grf", NEWGRF_DIR);
    00564     /* The number scanned and the number returned may not be the same;
    00565      * duplicate NewGRFs and base sets are ignored in the return value. */
    00566     _settings_client.gui.last_newgrf_count = fs.num_scanned;
    00567     return ret;
    00568   }
    00569 };
    00570
    00571 bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
    00572 {
    00573   GRFConfig *c = new GRFConfig(filename + basepath_length);
    00574
    00575   bool added = true;
    00576   if (FillGRFDetails(c, false)) {
    00577     if (_all_grfs == NULL) {
    00578       _all_grfs = c;
    00579     } else {
    00580       /* Insert file into list at a position determined by its
    00581        * name, so the list is sorted as we go along */
    00582       GRFConfig **pd, *d;
    00583       bool stop = false;
    00584       for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
    00585         if (c->ident.grfid == d->ident.grfid && memcmp(c->ident.md5sum, d->ident.md5sum, sizeof(c->ident.md5sum)) == 0) added = false;
    00586         /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
    00587          *  before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
    00588          *  just after the first with the same name. Avoids doubles in the list. */
    00589         if (strcasecmp(c->GetName(), d->GetName()) <= 0) {
    00590           stop = true;
    00591         } else if (stop) {
    00592           break;
    00593         }
    00594       }
    00595       if (added) {
    00596         c->next = d;
    00597         *pd = c;
    00598       }
    00599     }
    00600   } else {
    00601     added = false;
    00602   }
    00603
    00604   this->num_scanned++;
    00605   if (this->next_update <= _realtime_tick) {
    00606     _modal_progress_work_mutex->EndCritical();
    00607     _modal_progress_paint_mutex->BeginCritical();
    00608
    00609     const char *name = NULL;
    00610     if (c->name != NULL) name = GetGRFStringFromGRFText(c->name->text);
    00611     if (name == NULL) name = c->filename;
    00612     UpdateNewGRFScanStatus(this->num_scanned, name);
    00613
    00614     _modal_progress_work_mutex->BeginCritical();
    00615     _modal_progress_paint_mutex->EndCritical();
    00616
    00617     this->next_update = _realtime_tick + 200;
    00618   }
    00619
    00620   if (!added) {
    00621     /* File couldn't be opened, or is either not a NewGRF or is a
    00622      * 'system' NewGRF or it's already known, so forget about it. */
    00623     delete c;
    00624   }
    00625
    00626   return added;
    00627 }
    00628
    00635 static int CDECL GRFSorter(GRFConfig * const *p1, GRFConfig * const *p2)
    00636 {
    00637   const GRFConfig *c1 = *p1;
    00638   const GRFConfig *c2 = *p2;
    00639
    00640   return strcasecmp(c1->GetName(), c2->GetName());
    00641 }
    00642
    00647 void DoScanNewGRFFiles(void *callback)
    00648 {
    00649   _modal_progress_work_mutex->BeginCritical();
    00650
    00651   ClearGRFConfigList(&_all_grfs);
    00652   TarScanner::DoScan(TarScanner::NEWGRF);
    00653
    00654   DEBUG(grf, 1, "Scanning for NewGRFs");
    00655   uint num = GRFFileScanner::DoScan();
    00656
    00657   DEBUG(grf, 1, "Scan complete, found %d files", num);
    00658   if (num != 0 && _all_grfs != NULL) {
    00659     /* Sort the linked list using quicksort.
    00660     * For that we first have to make an array, then sort and
    00661     * then remake the linked list. */
    00662     GRFConfig **to_sort = MallocT<GRFConfig*>(num);
    00663
    00664     uint i = 0;
    00665     for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) {
    00666       to_sort[i] = p;
    00667     }
    00668     /* Number of files is not necessarily right */
    00669     num = i;
    00670
    00671     QSortT(to_sort, num, &GRFSorter);
    00672
    00673     for (i = 1; i < num; i++) {
    00674       to_sort[i - 1]->next = to_sort[i];
    00675     }
    00676     to_sort[num - 1]->next = NULL;
    00677     _all_grfs = to_sort[0];
    00678
    00679     free(to_sort);
    00680
    00681 #ifdef ENABLE_NETWORK
    00682     NetworkAfterNewGRFScan();
    00683 #endif
    00684   }
    00685
    00686   _modal_progress_work_mutex->EndCritical();
    00687   _modal_progress_paint_mutex->BeginCritical();
    00688
    00689   /* Yes... these are the NewGRF windows */
    00690   InvalidateWindowClassesData(WC_SAVELOAD, 0, true);
    00691   InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE, GOID_NEWGRF_RESCANNED, true);
    00692   if (callback != NULL) ((NewGRFScanCallback*)callback)->OnNewGRFsScanned();
    00693
    00694   DeleteWindowByClass(WC_MODAL_PROGRESS);
    00695   SetModalProgress(false);
    00696   MarkWholeScreenDirty();
    00697   _modal_progress_paint_mutex->EndCritical();
    00698 }
    00699
    00704 void ScanNewGRFFiles(NewGRFScanCallback *callback)
    00705 {
    00706   /* First set the modal progress. This ensures that it will eventually let go of the paint mutex. */
    00707   SetModalProgress(true);
    00708   /* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */
    00709   MarkWholeScreenDirty();
    00710
    00711   if (!_video_driver->HasGUI() || !ThreadObject::New(&DoScanNewGRFFiles, callback, NULL)) {
    00712     _modal_progress_work_mutex->EndCritical();
    00713     _modal_progress_paint_mutex->EndCritical();
    00714     DoScanNewGRFFiles(callback);
    00715     _modal_progress_paint_mutex->BeginCritical();
    00716     _modal_progress_work_mutex->BeginCritical();
    00717   } else {
    00718     UpdateNewGRFScanStatus(0, NULL);
    00719   }
    00720 }
    00721
    00730 const GRFConfig *FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
    00731 {
    00732   assert((mode == FGCM_EXACT) != (md5sum == NULL));
    00733   const GRFConfig *best = NULL;
    00734   for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
    00735     /* if md5sum is set, we look for an exact match and continue if not found */
    00736     if (!c->ident.HasGrfIdentifier(grfid, md5sum)) continue;
    00737     /* return it, if the exact same newgrf is found, or if we do not care about finding "the best" */
    00738     if (md5sum != NULL || mode == FGCM_ANY) return c;
    00739     /* Skip incompatible stuff, unless explicitly allowed */
    00740     if (mode != FGCM_NEWEST && HasBit(c->flags, GCF_INVALID)) continue;
    00741     /* check version compatibility */
    00742     if (mode == FGCM_COMPATIBLE && (c->version < desired_version || c->min_loadable_version > desired_version)) continue;
    00743     /* remember the newest one as "the best" */
    00744     if (best == NULL || c->version > best->version) best = c;
    00745   }
    00746
    00747   return best;
    00748 }
    00749
    00750 #ifdef ENABLE_NETWORK
    00751
    00753 struct UnknownGRF : public GRFIdentifier {
    00754   UnknownGRF *next;
    00755   GRFTextWrapper *name;
    00756 };
    00757
    00775 GRFTextWrapper *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
    00776 {
    00777   UnknownGRF *grf;
    00778   static UnknownGRF *unknown_grfs = NULL;
    00779
    00780   for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
    00781     if (grf->grfid == grfid) {
    00782       if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
    00783     }
    00784   }
    00785
    00786   if (!create) return NULL;
    00787
    00788   grf = CallocT<UnknownGRF>(1);
    00789   grf->grfid = grfid;
    00790   grf->next  = unknown_grfs;
    00791   grf->name = new GRFTextWrapper();
    00792   grf->name->AddRef();
    00793
    00794   AddGRFTextToList(&grf->name->text, UNKNOWN_GRF_NAME_PLACEHOLDER);
    00795   memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
    00796
    00797   unknown_grfs = grf;
    00798   return grf->name;
    00799 }
    00800
    00801 #endif /* ENABLE_NETWORK */
    00802
    00803
    00810 GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask)
    00811 {
    00812   GRFConfig *c;
    00813
    00814   for (c = _grfconfig; c != NULL; c = c->next) {
    00815     if ((c->ident.grfid & mask) == (grfid & mask)) return c;
    00816   }
    00817
    00818   return NULL;
    00819 }
    00820
    00821
    00823 char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
    00824 {
    00825   uint i;
    00826
    00827   /* Return an empty string if there are no parameters */
    00828   if (c->num_params == 0) return strecpy(dst, "", last);
    00829
    00830   for (i = 0; i < c->num_params; i++) {
    00831     if (i > 0) dst = strecpy(dst, " ", last);
    00832     dst += seprintf(dst, last, "%d", c->param[i]);
    00833   }
    00834   return dst;
    00835 }
    00836
    00838 static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400);
    00839
    00844 bool GRFConfig::IsOpenTTDBaseGRF() const
    00845 {
    00846   return (this->ident.grfid & 0x00FFFFFF) == OPENTTD_GRAPHICS_BASE_GRF_ID;
    00847 }
    00848
    00854 const char *GRFConfig::GetTextfile(TextfileType type) const
    00855 {
    00856   static const char * const prefixes[] = {
    00857     "readme",
    00858     "changelog",
    00859     "license",
    00860   };
    00861   assert_compile(lengthof(prefixes) == TFT_END);
    00862
    00863   const char *prefix = prefixes[type];
    00864
    00865   if (this->filename == NULL) return NULL;
    00866
    00867   static char file_path[MAX_PATH];
    00868   strecpy(file_path, this->filename, lastof(file_path));
    00869
    00870   char *slash = strrchr(file_path, PATHSEPCHAR);
    00871   if (slash == NULL) return NULL;
    00872
    00873   seprintf(slash + 1, lastof(file_path), "%s_%s.txt", prefix, GetCurrentLanguageIsoCode());
    00874   if (FioCheckFileExists(file_path, NEWGRF_DIR)) return file_path;
    00875
    00876   seprintf(slash + 1, lastof(file_path), "%s_%.2s.txt", prefix, GetCurrentLanguageIsoCode());
    00877   if (FioCheckFileExists(file_path, NEWGRF_DIR)) return file_path;
    00878
    00879   seprintf(slash + 1, lastof(file_path), "%s.txt", prefix);
    00880   return FioCheckFileExists(file_path, NEWGRF_DIR) ? file_path : NULL;
    00881 }