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   num_valid_params(lengthof(param))
00048 {
00049   if (filename != NULL) this->filename = strdup(filename);
00050   this->name->AddRef();
00051   this->info->AddRef();
00052 }
00053 
00058 GRFConfig::GRFConfig(const GRFConfig &config) :
00059   ZeroedMemoryAllocator(),
00060   ident(config.ident),
00061   name(config.name),
00062   info(config.info),
00063   version(config.version),
00064   min_loadable_version(config.min_loadable_version),
00065   flags(config.flags & ~(1 << GCF_COPY)),
00066   status(config.status),
00067   grf_bugs(config.grf_bugs),
00068   num_params(config.num_params),
00069   num_valid_params(config.num_valid_params),
00070   palette(config.palette),
00071   has_param_defaults(config.has_param_defaults)
00072 {
00073   MemCpyT<uint8>(this->original_md5sum, config.original_md5sum, lengthof(this->original_md5sum));
00074   MemCpyT<uint32>(this->param, config.param, lengthof(this->param));
00075   if (config.filename != NULL) this->filename = strdup(config.filename);
00076   this->name->AddRef();
00077   this->info->AddRef();
00078   if (config.error    != NULL) this->error    = new GRFError(*config.error);
00079   for (uint i = 0; i < config.param_info.Length(); i++) {
00080     if (config.param_info[i] == NULL) {
00081       *this->param_info.Append() = NULL;
00082     } else {
00083       *this->param_info.Append() = new GRFParameterInfo(*config.param_info[i]);
00084     }
00085   }
00086 }
00087 
00089 GRFConfig::~GRFConfig()
00090 {
00091   /* GCF_COPY as in NOT strdupped/alloced the filename */
00092   if (!HasBit(this->flags, GCF_COPY)) {
00093     free(this->filename);
00094     delete this->error;
00095   }
00096   this->name->Release();
00097   this->info->Release();
00098 
00099   for (uint i = 0; i < this->param_info.Length(); i++) delete this->param_info[i];
00100 }
00101 
00107 const char *GRFConfig::GetName() const
00108 {
00109   const char *name = GetGRFStringFromGRFText(this->name->text);
00110   return StrEmpty(name) ? this->filename : name;
00111 }
00112 
00117 const char *GRFConfig::GetDescription() const
00118 {
00119   return GetGRFStringFromGRFText(this->info->text);
00120 }
00121 
00123 void GRFConfig::SetParameterDefaults()
00124 {
00125   this->num_params = 0;
00126   MemSetT<uint32>(this->param, 0, lengthof(this->param));
00127 
00128   if (!this->has_param_defaults) return;
00129 
00130   for (uint i = 0; i < this->param_info.Length(); i++) {
00131     if (this->param_info[i] == NULL) continue;
00132     this->param_info[i]->SetValue(this, this->param_info[i]->def_value);
00133   }
00134 }
00135 
00141 void GRFConfig::SetSuitablePalette()
00142 {
00143   PaletteType pal;
00144   switch (this->palette & GRFP_GRF_MASK) {
00145     case GRFP_GRF_DOS:     pal = PAL_DOS;      break;
00146     case GRFP_GRF_WINDOWS: pal = PAL_WINDOWS;  break;
00147     default:               pal = _settings_client.gui.newgrf_default_palette == 1 ? PAL_WINDOWS : PAL_DOS; break;
00148   }
00149   SB(this->palette, GRFP_USE_BIT, 1, pal == PAL_WINDOWS ? GRFP_USE_WINDOWS : GRFP_USE_DOS);
00150 }
00151 
00152 GRFConfig *_all_grfs;
00153 GRFConfig *_grfconfig;
00154 GRFConfig *_grfconfig_newgame;
00155 GRFConfig *_grfconfig_static;
00156 
00162 GRFError::GRFError(StringID severity, StringID message) :
00163   message(message),
00164   severity(severity)
00165 {
00166 }
00167 
00172 GRFError::GRFError(const GRFError &error) :
00173   ZeroedMemoryAllocator(),
00174   custom_message(error.custom_message),
00175   data(error.data),
00176   message(error.message),
00177   severity(error.severity),
00178   num_params(error.num_params)
00179 {
00180   if (error.custom_message != NULL) this->custom_message = strdup(error.custom_message);
00181   if (error.data           != NULL) this->data           = strdup(error.data);
00182   memcpy(this->param_value, error.param_value, sizeof(this->param_value));
00183 }
00184 
00185 GRFError::~GRFError()
00186 {
00187   free(this->custom_message);
00188   free(this->data);
00189 }
00190 
00195 GRFParameterInfo::GRFParameterInfo(uint nr) :
00196   name(NULL),
00197   desc(NULL),
00198   type(PTYPE_UINT_ENUM),
00199   min_value(0),
00200   max_value(UINT32_MAX),
00201   def_value(0),
00202   param_nr(nr),
00203   first_bit(0),
00204   num_bit(32)
00205 {}
00206 
00212 GRFParameterInfo::GRFParameterInfo(GRFParameterInfo &info) :
00213   name(DuplicateGRFText(info.name)),
00214   desc(DuplicateGRFText(info.desc)),
00215   type(info.type),
00216   min_value(info.min_value),
00217   max_value(info.max_value),
00218   def_value(info.def_value),
00219   param_nr(info.param_nr),
00220   first_bit(info.first_bit),
00221   num_bit(info.num_bit)
00222 {
00223   for (uint i = 0; i < info.value_names.Length(); i++) {
00224     SmallPair<uint32, GRFText *> *data = info.value_names.Get(i);
00225     this->value_names.Insert(data->first, DuplicateGRFText(data->second));
00226   }
00227 }
00228 
00230 GRFParameterInfo::~GRFParameterInfo()
00231 {
00232   CleanUpGRFText(this->name);
00233   CleanUpGRFText(this->desc);
00234   for (uint i = 0; i < this->value_names.Length(); i++) {
00235     SmallPair<uint32, GRFText *> *data = this->value_names.Get(i);
00236     CleanUpGRFText(data->second);
00237   }
00238 }
00239 
00245 uint32 GRFParameterInfo::GetValue(struct GRFConfig *config) const
00246 {
00247   /* GB doesn't work correctly with nbits == 32, so handle that case here. */
00248   if (this->num_bit == 32) return config->param[this->param_nr];
00249   return GB(config->param[this->param_nr], this->first_bit, this->num_bit);
00250 }
00251 
00257 void GRFParameterInfo::SetValue(struct GRFConfig *config, uint32 value)
00258 {
00259   /* SB doesn't work correctly with nbits == 32, so handle that case here. */
00260   if (this->num_bit == 32) {
00261     config->param[this->param_nr] = value;
00262   } else {
00263     SB(config->param[this->param_nr], this->first_bit, this->num_bit, value);
00264   }
00265   config->num_params = max<uint>(config->num_params, this->param_nr + 1);
00266   SetWindowClassesDirty(WC_GAME_OPTIONS); // Is always the newgrf window
00267 }
00268 
00275 bool UpdateNewGRFConfigPalette(int32 p1)
00276 {
00277   for (GRFConfig *c = _grfconfig_newgame; c != NULL; c = c->next) c->SetSuitablePalette();
00278   for (GRFConfig *c = _grfconfig_static;  c != NULL; c = c->next) c->SetSuitablePalette();
00279   for (GRFConfig *c = _all_grfs;          c != NULL; c = c->next) c->SetSuitablePalette();
00280   return true;
00281 }
00282 
00289 static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir)
00290 {
00291   FILE *f;
00292   Md5 checksum;
00293   uint8 buffer[1024];
00294   size_t len, size;
00295 
00296   /* open the file */
00297   f = FioFOpenFile(config->filename, "rb", subdir, &size);
00298   if (f == NULL) return false;
00299 
00300   /* calculate md5sum */
00301   while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
00302     size -= len;
00303     checksum.Append(buffer, len);
00304   }
00305   checksum.Finish(config->ident.md5sum);
00306 
00307   FioFCloseFile(f);
00308 
00309   return true;
00310 }
00311 
00312 
00320 bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir)
00321 {
00322   if (!FioCheckFileExists(config->filename, subdir)) {
00323     config->status = GCS_NOT_FOUND;
00324     return false;
00325   }
00326 
00327   /* Find and load the Action 8 information */
00328   LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN, subdir);
00329   config->SetSuitablePalette();
00330 
00331   /* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
00332   if (config->ident.grfid == 0 || config->ident.grfid == 0xFFFFFFFF || config->IsOpenTTDBaseGRF()) return false;
00333 
00334   if (is_static) {
00335     /* Perform a 'safety scan' for static GRFs */
00336     LoadNewGRFFile(config, 62, GLS_SAFETYSCAN, subdir);
00337 
00338     /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
00339     if (HasBit(config->flags, GCF_UNSAFE)) return false;
00340   }
00341 
00342   return CalcGRFMD5Sum(config, subdir);
00343 }
00344 
00345 
00351 void ClearGRFConfigList(GRFConfig **config)
00352 {
00353   GRFConfig *c, *next;
00354   for (c = *config; c != NULL; c = next) {
00355     next = c->next;
00356     delete c;
00357   }
00358   *config = NULL;
00359 }
00360 
00361 
00369 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
00370 {
00371   /* Clear destination as it will be overwritten */
00372   ClearGRFConfigList(dst);
00373   for (; src != NULL; src = src->next) {
00374     GRFConfig *c = new GRFConfig(*src);
00375 
00376     ClrBit(c->flags, GCF_INIT_ONLY);
00377     if (init_only) SetBit(c->flags, GCF_INIT_ONLY);
00378 
00379     *dst = c;
00380     dst = &c->next;
00381   }
00382 
00383   return dst;
00384 }
00385 
00399 static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
00400 {
00401   GRFConfig *prev;
00402   GRFConfig *cur;
00403 
00404   if (list == NULL) return;
00405 
00406   for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) {
00407     if (cur->ident.grfid != list->ident.grfid) continue;
00408 
00409     prev->next = cur->next;
00410     delete cur;
00411     cur = prev; // Just go back one so it continues as normal later on
00412   }
00413 
00414   RemoveDuplicatesFromGRFConfigList(list->next);
00415 }
00416 
00421 void AppendStaticGRFConfigs(GRFConfig **dst)
00422 {
00423   GRFConfig **tail = dst;
00424   while (*tail != NULL) tail = &(*tail)->next;
00425 
00426   CopyGRFConfigList(tail, _grfconfig_static, false);
00427   RemoveDuplicatesFromGRFConfigList(*dst);
00428 }
00429 
00435 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
00436 {
00437   GRFConfig **tail = dst;
00438   while (*tail != NULL) tail = &(*tail)->next;
00439   *tail = el;
00440 
00441   RemoveDuplicatesFromGRFConfigList(*dst);
00442 }
00443 
00444 
00446 void ResetGRFConfig(bool defaults)
00447 {
00448   CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults);
00449   AppendStaticGRFConfigs(&_grfconfig);
00450 }
00451 
00452 
00464 GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
00465 {
00466   GRFListCompatibility res = GLC_ALL_GOOD;
00467 
00468   for (GRFConfig *c = grfconfig; c != NULL; c = c->next) {
00469     const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
00470     if (f == NULL || HasBit(f->flags, GCF_INVALID)) {
00471       char buf[256];
00472 
00473       /* If we have not found the exactly matching GRF try to find one with the
00474        * same grfid, as it most likely is compatible */
00475       f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, NULL, c->version);
00476       if (f != NULL) {
00477         md5sumToString(buf, lastof(buf), c->ident.md5sum);
00478         DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, buf);
00479         if (!HasBit(c->flags, GCF_COMPATIBLE)) {
00480           /* Preserve original_md5sum after it has been assigned */
00481           SetBit(c->flags, GCF_COMPATIBLE);
00482           memcpy(c->original_md5sum, c->ident.md5sum, sizeof(c->original_md5sum));
00483         }
00484 
00485         /* Non-found has precedence over compatibility load */
00486         if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
00487         goto compatible_grf;
00488       }
00489 
00490       /* No compatible grf was found, mark it as disabled */
00491       md5sumToString(buf, lastof(buf), c->ident.md5sum);
00492       DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->ident.grfid), c->filename, buf);
00493 
00494       c->status = GCS_NOT_FOUND;
00495       res = GLC_NOT_FOUND;
00496     } else {
00497 compatible_grf:
00498       DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->ident.grfid), f->filename);
00499       /* The filename could be the filename as in the savegame. As we need
00500        * to load the GRF here, we need the correct filename, so overwrite that
00501        * in any case and set the name and info when it is not set already.
00502        * When the GCF_COPY flag is set, it is certain that the filename is
00503        * already a local one, so there is no need to replace it. */
00504       if (!HasBit(c->flags, GCF_COPY)) {
00505         free(c->filename);
00506         c->filename = strdup(f->filename);
00507         memcpy(c->ident.md5sum, f->ident.md5sum, sizeof(c->ident.md5sum));
00508         c->name->Release();
00509         c->name = f->name;
00510         c->name->AddRef();
00511         c->info->Release();
00512         c->info = f->name;
00513         c->info->AddRef();
00514         c->error = NULL;
00515         c->version = f->version;
00516         c->min_loadable_version = f->min_loadable_version;
00517         c->num_valid_params = f->num_valid_params;
00518         c->has_param_defaults = f->has_param_defaults;
00519         for (uint i = 0; i < f->param_info.Length(); i++) {
00520           if (f->param_info[i] == NULL) {
00521             *c->param_info.Append() = NULL;
00522           } else {
00523             *c->param_info.Append() = new GRFParameterInfo(*f->param_info[i]);
00524           }
00525         }
00526       }
00527     }
00528   }
00529 
00530   return res;
00531 }
00532 
00534 class GRFFileScanner : FileScanner {
00535   uint next_update; 
00536   uint num_scanned; 
00537 
00538 public:
00539   GRFFileScanner() : next_update(_realtime_tick), num_scanned(0)
00540   {
00541   }
00542 
00543   /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename);
00544 
00546   static uint DoScan()
00547   {
00548     GRFFileScanner fs;
00549     int ret = fs.Scan(".grf", NEWGRF_DIR);
00550     /* The number scanned and the number returned may not be the same;
00551      * duplicate NewGRFs and base sets are ignored in the return value. */
00552     _settings_client.gui.last_newgrf_count = fs.num_scanned;
00553     return ret;
00554   }
00555 };
00556 
00557 bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
00558 {
00559   GRFConfig *c = new GRFConfig(filename + basepath_length);
00560 
00561   bool added = true;
00562   if (FillGRFDetails(c, false)) {
00563     if (_all_grfs == NULL) {
00564       _all_grfs = c;
00565     } else {
00566       /* Insert file into list at a position determined by its
00567        * name, so the list is sorted as we go along */
00568       GRFConfig **pd, *d;
00569       bool stop = false;
00570       for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
00571         if (c->ident.grfid == d->ident.grfid && memcmp(c->ident.md5sum, d->ident.md5sum, sizeof(c->ident.md5sum)) == 0) added = false;
00572         /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
00573          *  before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
00574          *  just after the first with the same name. Avoids doubles in the list. */
00575         if (strcasecmp(c->GetName(), d->GetName()) <= 0) {
00576           stop = true;
00577         } else if (stop) {
00578           break;
00579         }
00580       }
00581       if (added) {
00582         c->next = d;
00583         *pd = c;
00584       }
00585     }
00586   } else {
00587     added = false;
00588   }
00589 
00590   this->num_scanned++;
00591   if (this->next_update <= _realtime_tick) {
00592     _modal_progress_work_mutex->EndCritical();
00593     _modal_progress_paint_mutex->BeginCritical();
00594 
00595     const char *name = NULL;
00596     if (c->name != NULL) name = GetGRFStringFromGRFText(c->name->text);
00597     if (name == NULL) name = c->filename;
00598     UpdateNewGRFScanStatus(this->num_scanned, name);
00599 
00600     _modal_progress_work_mutex->BeginCritical();
00601     _modal_progress_paint_mutex->EndCritical();
00602 
00603     this->next_update = _realtime_tick + 200;
00604   }
00605 
00606   if (!added) {
00607     /* File couldn't be opened, or is either not a NewGRF or is a
00608      * 'system' NewGRF or it's already known, so forget about it. */
00609     delete c;
00610   }
00611 
00612   return added;
00613 }
00614 
00621 static int CDECL GRFSorter(GRFConfig * const *p1, GRFConfig * const *p2)
00622 {
00623   const GRFConfig *c1 = *p1;
00624   const GRFConfig *c2 = *p2;
00625 
00626   return strcasecmp(c1->GetName(), c2->GetName());
00627 }
00628 
00633 void DoScanNewGRFFiles(void *callback)
00634 {
00635   _modal_progress_work_mutex->BeginCritical();
00636 
00637   ClearGRFConfigList(&_all_grfs);
00638   TarScanner::DoScan(TarScanner::NEWGRF);
00639 
00640   DEBUG(grf, 1, "Scanning for NewGRFs");
00641   uint num = GRFFileScanner::DoScan();
00642 
00643   DEBUG(grf, 1, "Scan complete, found %d files", num);
00644   if (num != 0 && _all_grfs != NULL) {
00645     /* Sort the linked list using quicksort.
00646     * For that we first have to make an array, then sort and
00647     * then remake the linked list. */
00648     GRFConfig **to_sort = MallocT<GRFConfig*>(num);
00649 
00650     uint i = 0;
00651     for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) {
00652       to_sort[i] = p;
00653     }
00654     /* Number of files is not necessarily right */
00655     num = i;
00656 
00657     QSortT(to_sort, num, &GRFSorter);
00658 
00659     for (i = 1; i < num; i++) {
00660       to_sort[i - 1]->next = to_sort[i];
00661     }
00662     to_sort[num - 1]->next = NULL;
00663     _all_grfs = to_sort[0];
00664 
00665     free(to_sort);
00666 
00667 #ifdef ENABLE_NETWORK
00668     NetworkAfterNewGRFScan();
00669 #endif
00670   }
00671 
00672   _modal_progress_work_mutex->EndCritical();
00673   _modal_progress_paint_mutex->BeginCritical();
00674 
00675   /* Yes... these are the NewGRF windows */
00676   InvalidateWindowClassesData(WC_SAVELOAD, 0, true);
00677   InvalidateWindowData(WC_GAME_OPTIONS, 0, GOID_NEWGRF_RESCANNED, true);
00678   if (callback != NULL) ((NewGRFScanCallback*)callback)->OnNewGRFsScanned();
00679 
00680   DeleteWindowByClass(WC_MODAL_PROGRESS);
00681   SetModalProgress(false);
00682   MarkWholeScreenDirty();
00683   _modal_progress_paint_mutex->EndCritical();
00684 }
00685 
00690 void ScanNewGRFFiles(NewGRFScanCallback *callback)
00691 {
00692   /* First set the modal progress. This ensures that it will eventually let go of the paint mutex. */
00693   SetModalProgress(true);
00694   /* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */
00695   MarkWholeScreenDirty();
00696 
00697   if (!_video_driver->HasGUI() || !ThreadObject::New(&DoScanNewGRFFiles, callback, NULL)) {
00698     _modal_progress_work_mutex->EndCritical();
00699     _modal_progress_paint_mutex->EndCritical();
00700     DoScanNewGRFFiles(callback);
00701     _modal_progress_paint_mutex->BeginCritical();
00702     _modal_progress_work_mutex->BeginCritical();
00703   } else {
00704     UpdateNewGRFScanStatus(0, NULL);
00705   }
00706 }
00707 
00716 const GRFConfig *FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
00717 {
00718   assert((mode == FGCM_EXACT) != (md5sum == NULL));
00719   const GRFConfig *best = NULL;
00720   for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
00721     /* if md5sum is set, we look for an exact match and continue if not found */
00722     if (!c->ident.HasGrfIdentifier(grfid, md5sum)) continue;
00723     /* return it, if the exact same newgrf is found, or if we do not care about finding "the best" */
00724     if (md5sum != NULL || mode == FGCM_ANY) return c;
00725     /* Skip incompatible stuff, unless explicitly allowed */
00726     if (mode != FGCM_NEWEST && HasBit(c->flags, GCF_INVALID)) continue;
00727     /* check version compatibility */
00728     if (mode == FGCM_COMPATIBLE && (c->version < desired_version || c->min_loadable_version > desired_version)) continue;
00729     /* remember the newest one as "the best" */
00730     if (best == NULL || c->version > best->version) best = c;
00731   }
00732 
00733   return best;
00734 }
00735 
00736 #ifdef ENABLE_NETWORK
00737 
00739 struct UnknownGRF : public GRFIdentifier {
00740   UnknownGRF *next;     
00741   GRFTextWrapper *name; 
00742 };
00743 
00761 GRFTextWrapper *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
00762 {
00763   UnknownGRF *grf;
00764   static UnknownGRF *unknown_grfs = NULL;
00765 
00766   for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
00767     if (grf->grfid == grfid) {
00768       if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
00769     }
00770   }
00771 
00772   if (!create) return NULL;
00773 
00774   grf = CallocT<UnknownGRF>(1);
00775   grf->grfid = grfid;
00776   grf->next  = unknown_grfs;
00777   grf->name = new GRFTextWrapper();
00778   grf->name->AddRef();
00779 
00780   AddGRFTextToList(&grf->name->text, UNKNOWN_GRF_NAME_PLACEHOLDER);
00781   memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
00782 
00783   unknown_grfs = grf;
00784   return grf->name;
00785 }
00786 
00787 #endif /* ENABLE_NETWORK */
00788 
00789 
00796 GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask)
00797 {
00798   GRFConfig *c;
00799 
00800   for (c = _grfconfig; c != NULL; c = c->next) {
00801     if ((c->ident.grfid & mask) == (grfid & mask)) return c;
00802   }
00803 
00804   return NULL;
00805 }
00806 
00807 
00809 char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
00810 {
00811   uint i;
00812 
00813   /* Return an empty string if there are no parameters */
00814   if (c->num_params == 0) return strecpy(dst, "", last);
00815 
00816   for (i = 0; i < c->num_params; i++) {
00817     if (i > 0) dst = strecpy(dst, " ", last);
00818     dst += seprintf(dst, last, "%d", c->param[i]);
00819   }
00820   return dst;
00821 }
00822 
00824 static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400);
00825 
00830 bool GRFConfig::IsOpenTTDBaseGRF() const
00831 {
00832   return (this->ident.grfid & 0x00FFFFFF) == OPENTTD_GRAPHICS_BASE_GRF_ID;
00833 }
00834 
00840 const char *GRFConfig::GetTextfile(TextfileType type) const
00841 {
00842   static const char * const prefixes[] = {
00843     "readme",
00844     "changelog",
00845     "license",
00846   };
00847   assert_compile(lengthof(prefixes) == TFT_END);
00848 
00849   const char *prefix = prefixes[type];
00850 
00851   if (this->filename == NULL) return NULL;
00852 
00853   static char file_path[MAX_PATH];
00854   strecpy(file_path, this->filename, lastof(file_path));
00855 
00856   char *slash = strrchr(file_path, PATHSEPCHAR);
00857   if (slash == NULL) return NULL;
00858 
00859   seprintf(slash + 1, lastof(file_path), "%s_%s.txt", prefix, GetCurrentLanguageIsoCode());
00860   if (FioCheckFileExists(file_path, NEWGRF_DIR)) return file_path;
00861 
00862   seprintf(slash + 1, lastof(file_path), "%s_%.2s.txt", prefix, GetCurrentLanguageIsoCode());
00863   if (FioCheckFileExists(file_path, NEWGRF_DIR)) return file_path;
00864 
00865   seprintf(slash + 1, lastof(file_path), "%s.txt", prefix);
00866   return FioCheckFileExists(file_path, NEWGRF_DIR) ? file_path : NULL;
00867 }