newgrf_config.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_config.cpp 19258 2010-02-25 21:18:38Z rubidium $ */
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 "gamelog.h"
00017 #include "network/network_func.h"
00018 #include "gfx_func.h"
00019 
00020 #include "fileio_func.h"
00021 #include "fios.h"
00022 
00023 
00024 GRFConfig *_all_grfs;
00025 GRFConfig *_grfconfig;
00026 GRFConfig *_grfconfig_newgame;
00027 GRFConfig *_grfconfig_static;
00028 
00029 GRFError::GRFError(StringID severity, StringID message) :
00030   message(message),
00031   severity(severity)
00032 {
00033 }
00034 
00035 GRFError::~GRFError()
00036 {
00037   free(this->custom_message);
00038   free(this->data);
00039  }
00040 
00049 void UpdateNewGRFConfigPalette()
00050 {
00051   for (GRFConfig *c = _grfconfig_newgame; c != NULL; c = c->next) c->windows_paletted = (_use_palette == PAL_WINDOWS);
00052   for (GRFConfig *c = _grfconfig_static;  c != NULL; c = c->next) c->windows_paletted = (_use_palette == PAL_WINDOWS);
00053 }
00054 
00055 /* Calculate the MD5 Sum for a GRF */
00056 static bool CalcGRFMD5Sum(GRFConfig *config)
00057 {
00058   FILE *f;
00059   Md5 checksum;
00060   uint8 buffer[1024];
00061   size_t len, size;
00062 
00063   /* open the file */
00064   f = FioFOpenFile(config->filename, "rb", DATA_DIR, &size);
00065   if (f == NULL) return false;
00066 
00067   /* calculate md5sum */
00068   while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
00069     size -= len;
00070     checksum.Append(buffer, len);
00071   }
00072   checksum.Finish(config->md5sum);
00073 
00074   FioFCloseFile(f);
00075 
00076   return true;
00077 }
00078 
00079 
00080 /* Find the GRFID and calculate the md5sum */
00081 bool FillGRFDetails(GRFConfig *config, bool is_static)
00082 {
00083   if (!FioCheckFileExists(config->filename)) {
00084     config->status = GCS_NOT_FOUND;
00085     return false;
00086   }
00087 
00088   /* Find and load the Action 8 information */
00089   LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN);
00090 
00091   /* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
00092   if (config->grfid == 0 || config->grfid == 0xFFFFFFFF || config->IsOpenTTDBaseGRF()) return false;
00093 
00094   if (is_static) {
00095     /* Perform a 'safety scan' for static GRFs */
00096     LoadNewGRFFile(config, 62, GLS_SAFETYSCAN);
00097 
00098     /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
00099     if (HasBit(config->flags, GCF_UNSAFE)) return false;
00100   }
00101 
00102   config->windows_paletted = (_use_palette == PAL_WINDOWS);
00103 
00104   return CalcGRFMD5Sum(config);
00105 }
00106 
00107 
00108 void ClearGRFConfig(GRFConfig **config)
00109 {
00110   /* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
00111   if (!HasBit((*config)->flags, GCF_COPY)) {
00112     free((*config)->filename);
00113     free((*config)->name);
00114     free((*config)->info);
00115     delete (*config)->error;
00116   }
00117   free(*config);
00118   *config = NULL;
00119 }
00120 
00121 
00122 /* Clear a GRF Config list */
00123 void ClearGRFConfigList(GRFConfig **config)
00124 {
00125   GRFConfig *c, *next;
00126   for (c = *config; c != NULL; c = next) {
00127     next = c->next;
00128     ClearGRFConfig(&c);
00129   }
00130   *config = NULL;
00131 }
00132 
00133 
00139 GRFConfig *DuplicateGRFConfig(const GRFConfig *c)
00140 {
00141   GRFConfig *config = MallocT<GRFConfig>(1);
00142   *config = *c;
00143 
00144   if (c->filename != NULL) config->filename = strdup(c->filename);
00145   if (c->name     != NULL) config->name = strdup(c->name);
00146   if (c->info     != NULL) config->info = strdup(c->info);
00147   if (c->error    != NULL) {
00148     config->error = new GRFError(c->error->severity, c->error->message);
00149     config->error->num_params = c->error->num_params;
00150     memcpy(config->error->param_value, c->error->param_value, sizeof(config->error->param_value));
00151     if (c->error->data           != NULL) config->error->data = strdup(c->error->data);
00152     if (c->error->custom_message != NULL) config->error->custom_message = strdup(c->error->custom_message);
00153   }
00154 
00155   ClrBit(config->flags, GCF_COPY);
00156 
00157   return config;
00158 }
00159 
00165 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
00166 {
00167   /* Clear destination as it will be overwritten */
00168   ClearGRFConfigList(dst);
00169   for (; src != NULL; src = src->next) {
00170     GRFConfig *c = DuplicateGRFConfig(src);
00171 
00172     ClrBit(c->flags, GCF_INIT_ONLY);
00173     if (init_only) SetBit(c->flags, GCF_INIT_ONLY);
00174 
00175     *dst = c;
00176     dst = &c->next;
00177   }
00178 
00179   return dst;
00180 }
00181 
00195 static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
00196 {
00197   GRFConfig *prev;
00198   GRFConfig *cur;
00199 
00200   if (list == NULL) return;
00201 
00202   for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) {
00203     if (cur->grfid != list->grfid) continue;
00204 
00205     prev->next = cur->next;
00206     ClearGRFConfig(&cur);
00207     cur = prev; // Just go back one so it continues as normal later on
00208   }
00209 
00210   RemoveDuplicatesFromGRFConfigList(list->next);
00211 }
00212 
00217 void AppendStaticGRFConfigs(GRFConfig **dst)
00218 {
00219   GRFConfig **tail = dst;
00220   while (*tail != NULL) tail = &(*tail)->next;
00221 
00222   CopyGRFConfigList(tail, _grfconfig_static, false);
00223   RemoveDuplicatesFromGRFConfigList(*dst);
00224 }
00225 
00229 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
00230 {
00231   GRFConfig **tail = dst;
00232   while (*tail != NULL) tail = &(*tail)->next;
00233   *tail = el;
00234 
00235   RemoveDuplicatesFromGRFConfigList(*dst);
00236 }
00237 
00238 
00239 /* Reset the current GRF Config to either blank or newgame settings */
00240 void ResetGRFConfig(bool defaults)
00241 {
00242   CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults);
00243   AppendStaticGRFConfigs(&_grfconfig);
00244 }
00245 
00246 
00255 GRFListCompatibility IsGoodGRFConfigList()
00256 {
00257   GRFListCompatibility res = GLC_ALL_GOOD;
00258 
00259   for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
00260     const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
00261     if (f == NULL) {
00262       char buf[256];
00263 
00264       /* If we have not found the exactly matching GRF try to find one with the
00265        * same grfid, as it most likely is compatible */
00266       f = FindGRFConfig(c->grfid);
00267       if (f != NULL) {
00268         md5sumToString(buf, lastof(buf), c->md5sum);
00269         DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->grfid), c->filename, buf);
00270         SetBit(c->flags, GCF_COMPATIBLE);
00271 
00272         /* Non-found has precedence over compatibility load */
00273         if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
00274         GamelogGRFCompatible(f);
00275         goto compatible_grf;
00276       }
00277 
00278       /* No compatible grf was found, mark it as disabled */
00279       md5sumToString(buf, lastof(buf), c->md5sum);
00280       DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->grfid), c->filename, buf);
00281 
00282       GamelogGRFRemove(c->grfid);
00283 
00284       c->status = GCS_NOT_FOUND;
00285       res = GLC_NOT_FOUND;
00286     } else {
00287 compatible_grf:
00288       DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->grfid), f->filename);
00289       /* The filename could be the filename as in the savegame. As we need
00290        * to load the GRF here, we need the correct filename, so overwrite that
00291        * in any case and set the name and info when it is not set already.
00292        * When the GCF_COPY flag is set, it is certain that the filename is
00293        * already a local one, so there is no need to replace it. */
00294       if (!HasBit(c->flags, GCF_COPY)) {
00295         free(c->filename);
00296         c->filename = strdup(f->filename);
00297         memcpy(c->md5sum, f->md5sum, sizeof(c->md5sum));
00298         if (c->name == NULL && f->name != NULL) c->name = strdup(f->name);
00299         if (c->info == NULL && f->info != NULL) c->info = strdup(f->info);
00300         c->error = NULL;
00301       }
00302     }
00303   }
00304 
00305   return res;
00306 }
00307 
00309 class GRFFileScanner : FileScanner {
00310 public:
00311   /* virtual */ bool AddFile(const char *filename, size_t basepath_length);
00312 
00314   static uint DoScan()
00315   {
00316     GRFFileScanner fs;
00317     return fs.Scan(".grf", DATA_DIR);
00318   }
00319 };
00320 
00321 bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length)
00322 {
00323   GRFConfig *c = CallocT<GRFConfig>(1);
00324   c->filename = strdup(filename + basepath_length);
00325 
00326   bool added = true;
00327   if (FillGRFDetails(c, false)) {
00328     if (_all_grfs == NULL) {
00329       _all_grfs = c;
00330     } else {
00331       /* Insert file into list at a position determined by its
00332        * name, so the list is sorted as we go along */
00333       GRFConfig **pd, *d;
00334       bool stop = false;
00335       for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
00336         if (c->grfid == d->grfid && memcmp(c->md5sum, d->md5sum, sizeof(c->md5sum)) == 0) added = false;
00337         /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
00338          *  before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
00339          *  just after the first with the same name. Avoids doubles in the list. */
00340         if (strcasecmp(c->name, d->name) <= 0) {
00341           stop = true;
00342         } else if (stop) {
00343           break;
00344         }
00345       }
00346       if (added) {
00347         c->next = d;
00348         *pd = c;
00349       }
00350     }
00351   } else {
00352     added = false;
00353   }
00354 
00355   if (!added) {
00356     /* File couldn't be opened, or is either not a NewGRF or is a
00357      * 'system' NewGRF or it's already known, so forget about it. */
00358     ClearGRFConfig(&c);
00359   }
00360 
00361   return added;
00362 }
00363 
00370 static int CDECL GRFSorter(GRFConfig * const *p1, GRFConfig * const *p2)
00371 {
00372   const GRFConfig *c1 = *p1;
00373   const GRFConfig *c2 = *p2;
00374 
00375   return strcasecmp(c1->name != NULL ? c1->name : c1->filename,
00376     c2->name != NULL ? c2->name : c2->filename);
00377 }
00378 
00379 /* Scan for all NewGRFs */
00380 void ScanNewGRFFiles()
00381 {
00382   ClearGRFConfigList(&_all_grfs);
00383 
00384   DEBUG(grf, 1, "Scanning for NewGRFs");
00385   uint num = GRFFileScanner::DoScan();
00386 
00387   DEBUG(grf, 1, "Scan complete, found %d files", num);
00388   if (num == 0 || _all_grfs == NULL) return;
00389 
00390   /* Sort the linked list using quicksort.
00391    * For that we first have to make an array, then sort and
00392    * then remake the linked list. */
00393   GRFConfig **to_sort = MallocT<GRFConfig*>(num);
00394 
00395   uint i = 0;
00396   for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) {
00397     to_sort[i] = p;
00398   }
00399   /* Number of files is not necessarily right */
00400   num = i;
00401 
00402   QSortT(to_sort, num, &GRFSorter);
00403 
00404   for (i = 1; i < num; i++) {
00405     to_sort[i - 1]->next = to_sort[i];
00406   }
00407   to_sort[num - 1]->next = NULL;
00408   _all_grfs = to_sort[0];
00409 
00410   free(to_sort);
00411 
00412 #ifdef ENABLE_NETWORK
00413   NetworkAfterNewGRFScan();
00414 #endif
00415 }
00416 
00417 
00418 /* Find a NewGRF in the scanned list, if md5sum is NULL, we don't care about it*/
00419 const GRFConfig *FindGRFConfig(uint32 grfid, const uint8 *md5sum)
00420 {
00421   for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
00422     if (c->grfid == grfid) {
00423       if (md5sum == NULL) return c;
00424 
00425       if (memcmp(md5sum, c->md5sum, sizeof(c->md5sum)) == 0) return c;
00426     }
00427   }
00428 
00429   return NULL;
00430 }
00431 
00432 #ifdef ENABLE_NETWORK
00433 
00435 struct UnknownGRF : public GRFIdentifier {
00436   UnknownGRF *next;
00437   char   name[NETWORK_GRF_NAME_LENGTH];
00438 };
00439 
00457 char *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
00458 {
00459   UnknownGRF *grf;
00460   static UnknownGRF *unknown_grfs = NULL;
00461 
00462   for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
00463     if (grf->grfid == grfid) {
00464       if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
00465     }
00466   }
00467 
00468   if (!create) return NULL;
00469 
00470   grf = CallocT<UnknownGRF>(1);
00471   grf->grfid = grfid;
00472   grf->next  = unknown_grfs;
00473   strecpy(grf->name, UNKNOWN_GRF_NAME_PLACEHOLDER, lastof(grf->name));
00474   memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
00475 
00476   unknown_grfs = grf;
00477   return grf->name;
00478 }
00479 
00480 #endif /* ENABLE_NETWORK */
00481 
00482 
00483 /* Retrieve a NewGRF from the current config by its grfid */
00484 GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask)
00485 {
00486   GRFConfig *c;
00487 
00488   for (c = _grfconfig; c != NULL; c = c->next) {
00489     if ((c->grfid & mask) == (grfid & mask)) return c;
00490   }
00491 
00492   return NULL;
00493 }
00494 
00495 
00496 /* Build a space separated list of parameters, and terminate */
00497 char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
00498 {
00499   uint i;
00500 
00501   /* Return an empty string if there are no parameters */
00502   if (c->num_params == 0) return strecpy(dst, "", last);
00503 
00504   for (i = 0; i < c->num_params; i++) {
00505     if (i > 0) dst = strecpy(dst, " ", last);
00506     dst += seprintf(dst, last, "%d", c->param[i]);
00507   }
00508   return dst;
00509 }
00510 
00512 static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400);
00513 
00518 bool GRFConfig::IsOpenTTDBaseGRF() const
00519 {
00520   return (this->grfid & 0x00FFFFFF) == OPENTTD_GRAPHICS_BASE_GRF_ID;
00521 }

Generated on Sat Jun 5 21:52:07 2010 for OpenTTD by  doxygen 1.6.1