newgrf_config.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_config.cpp 23807 2012-01-15 17:33:35Z 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 
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 {
00193   if (error.custom_message != NULL) this->custom_message = strdup(error.custom_message);
00194   if (error.data           != NULL) this->data           = strdup(error.data);
00195   memcpy(this->param_value, error.param_value, sizeof(this->param_value));
00196 }
00197 
00198 GRFError::~GRFError()
00199 {
00200   free(this->custom_message);
00201   free(this->data);
00202 }
00203 
00208 GRFParameterInfo::GRFParameterInfo(uint nr) :
00209   name(NULL),
00210   desc(NULL),
00211   type(PTYPE_UINT_ENUM),
00212   min_value(0),
00213   max_value(UINT32_MAX),
00214   def_value(0),
00215   param_nr(nr),
00216   first_bit(0),
00217   num_bit(32)
00218 {}
00219 
00225 GRFParameterInfo::GRFParameterInfo(GRFParameterInfo &info) :
00226   name(DuplicateGRFText(info.name)),
00227   desc(DuplicateGRFText(info.desc)),
00228   type(info.type),
00229   min_value(info.min_value),
00230   max_value(info.max_value),
00231   def_value(info.def_value),
00232   param_nr(info.param_nr),
00233   first_bit(info.first_bit),
00234   num_bit(info.num_bit)
00235 {
00236   for (uint i = 0; i < info.value_names.Length(); i++) {
00237     SmallPair<uint32, GRFText *> *data = info.value_names.Get(i);
00238     this->value_names.Insert(data->first, DuplicateGRFText(data->second));
00239   }
00240 }
00241 
00243 GRFParameterInfo::~GRFParameterInfo()
00244 {
00245   CleanUpGRFText(this->name);
00246   CleanUpGRFText(this->desc);
00247   for (uint i = 0; i < this->value_names.Length(); i++) {
00248     SmallPair<uint32, GRFText *> *data = this->value_names.Get(i);
00249     CleanUpGRFText(data->second);
00250   }
00251 }
00252 
00258 uint32 GRFParameterInfo::GetValue(struct GRFConfig *config) const
00259 {
00260   /* GB doesn't work correctly with nbits == 32, so handle that case here. */
00261   if (this->num_bit == 32) return config->param[this->param_nr];
00262   return GB(config->param[this->param_nr], this->first_bit, this->num_bit);
00263 }
00264 
00270 void GRFParameterInfo::SetValue(struct GRFConfig *config, uint32 value)
00271 {
00272   /* SB doesn't work correctly with nbits == 32, so handle that case here. */
00273   if (this->num_bit == 32) {
00274     config->param[this->param_nr] = value;
00275   } else {
00276     SB(config->param[this->param_nr], this->first_bit, this->num_bit, value);
00277   }
00278   config->num_params = max<uint>(config->num_params, this->param_nr + 1);
00279   SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
00280 }
00281 
00288 bool UpdateNewGRFConfigPalette(int32 p1)
00289 {
00290   for (GRFConfig *c = _grfconfig_newgame; c != NULL; c = c->next) c->SetSuitablePalette();
00291   for (GRFConfig *c = _grfconfig_static;  c != NULL; c = c->next) c->SetSuitablePalette();
00292   for (GRFConfig *c = _all_grfs;          c != NULL; c = c->next) c->SetSuitablePalette();
00293   return true;
00294 }
00295 
00302 static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir)
00303 {
00304   FILE *f;
00305   Md5 checksum;
00306   uint8 buffer[1024];
00307   size_t len, size;
00308 
00309   /* open the file */
00310   f = FioFOpenFile(config->filename, "rb", subdir, &size);
00311   if (f == NULL) return false;
00312 
00313   /* calculate md5sum */
00314   while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
00315     size -= len;
00316     checksum.Append(buffer, len);
00317   }
00318   checksum.Finish(config->ident.md5sum);
00319 
00320   FioFCloseFile(f);
00321 
00322   return true;
00323 }
00324 
00325 
00333 bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir)
00334 {
00335   if (!FioCheckFileExists(config->filename, subdir)) {
00336     config->status = GCS_NOT_FOUND;
00337     return false;
00338   }
00339 
00340   /* Find and load the Action 8 information */
00341   LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN, subdir);
00342   config->SetSuitablePalette();
00343 
00344   /* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
00345   if (config->ident.grfid == 0 || config->ident.grfid == 0xFFFFFFFF || config->IsOpenTTDBaseGRF()) return false;
00346 
00347   if (is_static) {
00348     /* Perform a 'safety scan' for static GRFs */
00349     LoadNewGRFFile(config, 62, GLS_SAFETYSCAN, subdir);
00350 
00351     /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
00352     if (HasBit(config->flags, GCF_UNSAFE)) return false;
00353   }
00354 
00355   return CalcGRFMD5Sum(config, subdir);
00356 }
00357 
00358 
00364 void ClearGRFConfigList(GRFConfig **config)
00365 {
00366   GRFConfig *c, *next;
00367   for (c = *config; c != NULL; c = next) {
00368     next = c->next;
00369     delete c;
00370   }
00371   *config = NULL;
00372 }
00373 
00374 
00382 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
00383 {
00384   /* Clear destination as it will be overwritten */
00385   ClearGRFConfigList(dst);
00386   for (; src != NULL; src = src->next) {
00387     GRFConfig *c = new GRFConfig(*src);
00388 
00389     ClrBit(c->flags, GCF_INIT_ONLY);
00390     if (init_only) SetBit(c->flags, GCF_INIT_ONLY);
00391 
00392     *dst = c;
00393     dst = &c->next;
00394   }
00395 
00396   return dst;
00397 }
00398 
00412 static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
00413 {
00414   GRFConfig *prev;
00415   GRFConfig *cur;
00416 
00417   if (list == NULL) return;
00418 
00419   for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) {
00420     if (cur->ident.grfid != list->ident.grfid) continue;
00421 
00422     prev->next = cur->next;
00423     delete cur;
00424     cur = prev; // Just go back one so it continues as normal later on
00425   }
00426 
00427   RemoveDuplicatesFromGRFConfigList(list->next);
00428 }
00429 
00434 void AppendStaticGRFConfigs(GRFConfig **dst)
00435 {
00436   GRFConfig **tail = dst;
00437   while (*tail != NULL) tail = &(*tail)->next;
00438 
00439   CopyGRFConfigList(tail, _grfconfig_static, false);
00440   RemoveDuplicatesFromGRFConfigList(*dst);
00441 }
00442 
00448 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
00449 {
00450   GRFConfig **tail = dst;
00451   while (*tail != NULL) tail = &(*tail)->next;
00452   *tail = el;
00453 
00454   RemoveDuplicatesFromGRFConfigList(*dst);
00455 }
00456 
00457 
00459 void ResetGRFConfig(bool defaults)
00460 {
00461   CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults);
00462   AppendStaticGRFConfigs(&_grfconfig);
00463 }
00464 
00465 
00477 GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
00478 {
00479   GRFListCompatibility res = GLC_ALL_GOOD;
00480 
00481   for (GRFConfig *c = grfconfig; c != NULL; c = c->next) {
00482     const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
00483     if (f == NULL || HasBit(f->flags, GCF_INVALID)) {
00484       char buf[256];
00485 
00486       /* If we have not found the exactly matching GRF try to find one with the
00487        * same grfid, as it most likely is compatible */
00488       f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, NULL, c->version);
00489       if (f != NULL) {
00490         md5sumToString(buf, lastof(buf), c->ident.md5sum);
00491         DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, buf);
00492         if (!HasBit(c->flags, GCF_COMPATIBLE)) {
00493           /* Preserve original_md5sum after it has been assigned */
00494           SetBit(c->flags, GCF_COMPATIBLE);
00495           memcpy(c->original_md5sum, c->ident.md5sum, sizeof(c->original_md5sum));
00496         }
00497 
00498         /* Non-found has precedence over compatibility load */
00499         if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
00500         goto compatible_grf;
00501       }
00502 
00503       /* No compatible grf was found, mark it as disabled */
00504       md5sumToString(buf, lastof(buf), c->ident.md5sum);
00505       DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->ident.grfid), c->filename, buf);
00506 
00507       c->status = GCS_NOT_FOUND;
00508       res = GLC_NOT_FOUND;
00509     } else {
00510 compatible_grf:
00511       DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->ident.grfid), f->filename);
00512       /* The filename could be the filename as in the savegame. As we need
00513        * to load the GRF here, we need the correct filename, so overwrite that
00514        * in any case and set the name and info when it is not set already.
00515        * When the GCF_COPY flag is set, it is certain that the filename is
00516        * already a local one, so there is no need to replace it. */
00517       if (!HasBit(c->flags, GCF_COPY)) {
00518         free(c->filename);
00519         c->filename = strdup(f->filename);
00520         memcpy(c->ident.md5sum, f->ident.md5sum, sizeof(c->ident.md5sum));
00521         c->name->Release();
00522         c->name = f->name;
00523         c->name->AddRef();
00524         c->info->Release();
00525         c->info = f->name;
00526         c->info->AddRef();
00527         c->error = NULL;
00528         c->version = f->version;
00529         c->min_loadable_version = f->min_loadable_version;
00530         c->num_valid_params = f->num_valid_params;
00531         c->has_param_defaults = f->has_param_defaults;
00532         for (uint i = 0; i < f->param_info.Length(); i++) {
00533           if (f->param_info[i] == NULL) {
00534             *c->param_info.Append() = NULL;
00535           } else {
00536             *c->param_info.Append() = new GRFParameterInfo(*f->param_info[i]);
00537           }
00538         }
00539       }
00540     }
00541   }
00542 
00543   return res;
00544 }
00545 
00547 class GRFFileScanner : FileScanner {
00548   uint next_update; 
00549   uint num_scanned; 
00550 
00551 public:
00552   GRFFileScanner() : next_update(_realtime_tick), num_scanned(0)
00553   {
00554   }
00555 
00556   /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename);
00557 
00559   static uint DoScan()
00560   {
00561     GRFFileScanner fs;
00562     int ret = fs.Scan(".grf", NEWGRF_DIR);
00563     /* The number scanned and the number returned may not be the same;
00564      * duplicate NewGRFs and base sets are ignored in the return value. */
00565     _settings_client.gui.last_newgrf_count = fs.num_scanned;
00566     return ret;
00567   }
00568 };
00569 
00570 bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
00571 {
00572   GRFConfig *c = new GRFConfig(filename + basepath_length);
00573 
00574   bool added = true;
00575   if (FillGRFDetails(c, false)) {
00576     if (_all_grfs == NULL) {
00577       _all_grfs = c;
00578     } else {
00579       /* Insert file into list at a position determined by its
00580        * name, so the list is sorted as we go along */
00581       GRFConfig **pd, *d;
00582       bool stop = false;
00583       for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
00584         if (c->ident.grfid == d->ident.grfid && memcmp(c->ident.md5sum, d->ident.md5sum, sizeof(c->ident.md5sum)) == 0) added = false;
00585         /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
00586          *  before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
00587          *  just after the first with the same name. Avoids doubles in the list. */
00588         if (strcasecmp(c->GetName(), d->GetName()) <= 0) {
00589           stop = true;
00590         } else if (stop) {
00591           break;
00592         }
00593       }
00594       if (added) {
00595         c->next = d;
00596         *pd = c;
00597       }
00598     }
00599   } else {
00600     added = false;
00601   }
00602 
00603   this->num_scanned++;
00604   if (this->next_update <= _realtime_tick) {
00605     _modal_progress_work_mutex->EndCritical();
00606     _modal_progress_paint_mutex->BeginCritical();
00607 
00608     const char *name = NULL;
00609     if (c->name != NULL) name = GetGRFStringFromGRFText(c->name->text);
00610     if (name == NULL) name = c->filename;
00611     UpdateNewGRFScanStatus(this->num_scanned, name);
00612 
00613     _modal_progress_work_mutex->BeginCritical();
00614     _modal_progress_paint_mutex->EndCritical();
00615 
00616     this->next_update = _realtime_tick + 200;
00617   }
00618 
00619   if (!added) {
00620     /* File couldn't be opened, or is either not a NewGRF or is a
00621      * 'system' NewGRF or it's already known, so forget about it. */
00622     delete c;
00623   }
00624 
00625   return added;
00626 }
00627 
00634 static int CDECL GRFSorter(GRFConfig * const *p1, GRFConfig * const *p2)
00635 {
00636   const GRFConfig *c1 = *p1;
00637   const GRFConfig *c2 = *p2;
00638 
00639   return strcasecmp(c1->GetName(), c2->GetName());
00640 }
00641 
00646 void DoScanNewGRFFiles(void *callback)
00647 {
00648   _modal_progress_work_mutex->BeginCritical();
00649 
00650   ClearGRFConfigList(&_all_grfs);
00651   TarScanner::DoScan(TarScanner::NEWGRF);
00652 
00653   DEBUG(grf, 1, "Scanning for NewGRFs");
00654   uint num = GRFFileScanner::DoScan();
00655 
00656   DEBUG(grf, 1, "Scan complete, found %d files", num);
00657   if (num != 0 && _all_grfs != NULL) {
00658     /* Sort the linked list using quicksort.
00659     * For that we first have to make an array, then sort and
00660     * then remake the linked list. */
00661     GRFConfig **to_sort = MallocT<GRFConfig*>(num);
00662 
00663     uint i = 0;
00664     for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) {
00665       to_sort[i] = p;
00666     }
00667     /* Number of files is not necessarily right */
00668     num = i;
00669 
00670     QSortT(to_sort, num, &GRFSorter);
00671 
00672     for (i = 1; i < num; i++) {
00673       to_sort[i - 1]->next = to_sort[i];
00674     }
00675     to_sort[num - 1]->next = NULL;
00676     _all_grfs = to_sort[0];
00677 
00678     free(to_sort);
00679 
00680 #ifdef ENABLE_NETWORK
00681     NetworkAfterNewGRFScan();
00682 #endif
00683   }
00684 
00685   _modal_progress_work_mutex->EndCritical();
00686   _modal_progress_paint_mutex->BeginCritical();
00687 
00688   /* Yes... these are the NewGRF windows */
00689   InvalidateWindowClassesData(WC_SAVELOAD, 0, true);
00690   InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE, GOID_NEWGRF_RESCANNED, true);
00691   if (callback != NULL) ((NewGRFScanCallback*)callback)->OnNewGRFsScanned();
00692 
00693   DeleteWindowByClass(WC_MODAL_PROGRESS);
00694   SetModalProgress(false);
00695   MarkWholeScreenDirty();
00696   _modal_progress_paint_mutex->EndCritical();
00697 }
00698 
00703 void ScanNewGRFFiles(NewGRFScanCallback *callback)
00704 {
00705   /* First set the modal progress. This ensures that it will eventually let go of the paint mutex. */
00706   SetModalProgress(true);
00707   /* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */
00708   MarkWholeScreenDirty();
00709 
00710   if (!_video_driver->HasGUI() || !ThreadObject::New(&DoScanNewGRFFiles, callback, NULL)) {
00711     _modal_progress_work_mutex->EndCritical();
00712     _modal_progress_paint_mutex->EndCritical();
00713     DoScanNewGRFFiles(callback);
00714     _modal_progress_paint_mutex->BeginCritical();
00715     _modal_progress_work_mutex->BeginCritical();
00716   } else {
00717     UpdateNewGRFScanStatus(0, NULL);
00718   }
00719 }
00720 
00729 const GRFConfig *FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
00730 {
00731   assert((mode == FGCM_EXACT) != (md5sum == NULL));
00732   const GRFConfig *best = NULL;
00733   for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
00734     /* if md5sum is set, we look for an exact match and continue if not found */
00735     if (!c->ident.HasGrfIdentifier(grfid, md5sum)) continue;
00736     /* return it, if the exact same newgrf is found, or if we do not care about finding "the best" */
00737     if (md5sum != NULL || mode == FGCM_ANY) return c;
00738     /* Skip incompatible stuff, unless explicitly allowed */
00739     if (mode != FGCM_NEWEST && HasBit(c->flags, GCF_INVALID)) continue;
00740     /* check version compatibility */
00741     if (mode == FGCM_COMPATIBLE && (c->version < desired_version || c->min_loadable_version > desired_version)) continue;
00742     /* remember the newest one as "the best" */
00743     if (best == NULL || c->version > best->version) best = c;
00744   }
00745 
00746   return best;
00747 }
00748 
00749 #ifdef ENABLE_NETWORK
00750 
00752 struct UnknownGRF : public GRFIdentifier {
00753   UnknownGRF *next;     
00754   GRFTextWrapper *name; 
00755 };
00756 
00774 GRFTextWrapper *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
00775 {
00776   UnknownGRF *grf;
00777   static UnknownGRF *unknown_grfs = NULL;
00778 
00779   for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
00780     if (grf->grfid == grfid) {
00781       if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
00782     }
00783   }
00784 
00785   if (!create) return NULL;
00786 
00787   grf = CallocT<UnknownGRF>(1);
00788   grf->grfid = grfid;
00789   grf->next  = unknown_grfs;
00790   grf->name = new GRFTextWrapper();
00791   grf->name->AddRef();
00792 
00793   AddGRFTextToList(&grf->name->text, UNKNOWN_GRF_NAME_PLACEHOLDER);
00794   memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
00795 
00796   unknown_grfs = grf;
00797   return grf->name;
00798 }
00799 
00800 #endif /* ENABLE_NETWORK */
00801 
00802 
00809 GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask)
00810 {
00811   GRFConfig *c;
00812 
00813   for (c = _grfconfig; c != NULL; c = c->next) {
00814     if ((c->ident.grfid & mask) == (grfid & mask)) return c;
00815   }
00816 
00817   return NULL;
00818 }
00819 
00820 
00822 char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
00823 {
00824   uint i;
00825 
00826   /* Return an empty string if there are no parameters */
00827   if (c->num_params == 0) return strecpy(dst, "", last);
00828 
00829   for (i = 0; i < c->num_params; i++) {
00830     if (i > 0) dst = strecpy(dst, " ", last);
00831     dst += seprintf(dst, last, "%d", c->param[i]);
00832   }
00833   return dst;
00834 }
00835 
00837 static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400);
00838 
00843 bool GRFConfig::IsOpenTTDBaseGRF() const
00844 {
00845   return (this->ident.grfid & 0x00FFFFFF) == OPENTTD_GRAPHICS_BASE_GRF_ID;
00846 }
00847 
00853 const char *GRFConfig::GetTextfile(TextfileType type) const
00854 {
00855   static const char * const prefixes[] = {
00856     "readme",
00857     "changelog",
00858     "license",
00859   };
00860   assert_compile(lengthof(prefixes) == TFT_END);
00861 
00862   const char *prefix = prefixes[type];
00863 
00864   if (this->filename == NULL) return NULL;
00865 
00866   static char file_path[MAX_PATH];
00867   strecpy(file_path, this->filename, lastof(file_path));
00868 
00869   char *slash = strrchr(file_path, PATHSEPCHAR);
00870   if (slash == NULL) return NULL;
00871 
00872   seprintf(slash + 1, lastof(file_path), "%s_%s.txt", prefix, GetCurrentLanguageIsoCode());
00873   if (FioCheckFileExists(file_path, NEWGRF_DIR)) return file_path;
00874 
00875   seprintf(slash + 1, lastof(file_path), "%s_%.2s.txt", prefix, GetCurrentLanguageIsoCode());
00876   if (FioCheckFileExists(file_path, NEWGRF_DIR)) return file_path;
00877 
00878   seprintf(slash + 1, lastof(file_path), "%s.txt", prefix);
00879   return FioCheckFileExists(file_path, NEWGRF_DIR) ? file_path : NULL;
00880 }