newgrf_config.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_config.cpp 15299 2009-01-31 20:16:06Z smatz $ */
00002 
00005 #include "stdafx.h"
00006 #include "debug.h"
00007 #include "md5.h"
00008 #include "newgrf.h"
00009 #include "string_func.h"
00010 #include "gamelog.h"
00011 #include "network/network_type.h"
00012 #include "gfx_func.h"
00013 
00014 #include "fileio_func.h"
00015 #include "fios.h"
00016 
00017 
00018 GRFConfig *_all_grfs;
00019 GRFConfig *_grfconfig;
00020 GRFConfig *_grfconfig_newgame;
00021 GRFConfig *_grfconfig_static;
00022 
00023 
00032 void UpdateNewGRFConfigPalette()
00033 {
00034   for (GRFConfig *c = _grfconfig_newgame; c != NULL; c = c->next) c->windows_paletted = (_use_palette == PAL_WINDOWS);
00035   for (GRFConfig *c = _grfconfig_static;  c != NULL; c = c->next) c->windows_paletted = (_use_palette == PAL_WINDOWS);
00036 }
00037 
00038 /* Calculate the MD5 Sum for a GRF */
00039 static bool CalcGRFMD5Sum(GRFConfig *config)
00040 {
00041   FILE *f;
00042   Md5 checksum;
00043   uint8 buffer[1024];
00044   size_t len, size;
00045 
00046   /* open the file */
00047   f = FioFOpenFile(config->filename, "rb", DATA_DIR, &size);
00048   if (f == NULL) return false;
00049 
00050   /* calculate md5sum */
00051   while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
00052     size -= len;
00053     checksum.Append(buffer, len);
00054   }
00055   checksum.Finish(config->md5sum);
00056 
00057   FioFCloseFile(f);
00058 
00059   return true;
00060 }
00061 
00062 
00063 /* Find the GRFID and calculate the md5sum */
00064 bool FillGRFDetails(GRFConfig *config, bool is_static)
00065 {
00066   if (!FioCheckFileExists(config->filename)) {
00067     config->status = GCS_NOT_FOUND;
00068     return false;
00069   }
00070 
00071   /* Find and load the Action 8 information */
00072   LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN);
00073 
00074   /* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
00075   if (config->grfid == 0 || config->grfid == 0xFFFFFFFF || config->IsOpenTTDBaseGRF()) return false;
00076 
00077   if (is_static) {
00078     /* Perform a 'safety scan' for static GRFs */
00079     LoadNewGRFFile(config, 62, GLS_SAFETYSCAN);
00080 
00081     /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
00082     if (HasBit(config->flags, GCF_UNSAFE)) return false;
00083   }
00084 
00085   config->windows_paletted = (_use_palette == PAL_WINDOWS);
00086 
00087   return CalcGRFMD5Sum(config);
00088 }
00089 
00090 
00091 void ClearGRFConfig(GRFConfig **config)
00092 {
00093   /* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
00094   if (!HasBit((*config)->flags, GCF_COPY)) {
00095     free((*config)->filename);
00096     free((*config)->name);
00097     free((*config)->info);
00098 
00099     if ((*config)->error != NULL) {
00100       free((*config)->error->custom_message);
00101       free((*config)->error->data);
00102       free((*config)->error);
00103     }
00104   }
00105   free(*config);
00106   *config = NULL;
00107 }
00108 
00109 
00110 /* Clear a GRF Config list */
00111 void ClearGRFConfigList(GRFConfig **config)
00112 {
00113   GRFConfig *c, *next;
00114   for (c = *config; c != NULL; c = next) {
00115     next = c->next;
00116     ClearGRFConfig(&c);
00117   }
00118   *config = NULL;
00119 }
00120 
00121 
00127 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
00128 {
00129   /* Clear destination as it will be overwritten */
00130   ClearGRFConfigList(dst);
00131   for (; src != NULL; src = src->next) {
00132     GRFConfig *c = CallocT<GRFConfig>(1);
00133     *c = *src;
00134     if (src->filename  != NULL) c->filename  = strdup(src->filename);
00135     if (src->name      != NULL) c->name      = strdup(src->name);
00136     if (src->info      != NULL) c->info      = strdup(src->info);
00137     if (src->error     != NULL) {
00138       c->error = CallocT<GRFError>(1);
00139       memcpy(c->error, src->error, sizeof(GRFError));
00140       if (src->error->data != NULL) c->error->data = strdup(src->error->data);
00141       if (src->error->custom_message != NULL) c->error->custom_message = strdup(src->error->custom_message);
00142     }
00143 
00144     ClrBit(c->flags, GCF_INIT_ONLY);
00145     if (init_only) SetBit(c->flags, GCF_INIT_ONLY);
00146 
00147     *dst = c;
00148     dst = &c->next;
00149   }
00150 
00151   return dst;
00152 }
00153 
00167 static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
00168 {
00169   GRFConfig *prev;
00170   GRFConfig *cur;
00171 
00172   if (list == NULL) return;
00173 
00174   for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) {
00175     if (cur->grfid != list->grfid) continue;
00176 
00177     prev->next = cur->next;
00178     ClearGRFConfig(&cur);
00179     cur = prev; // Just go back one so it continues as normal later on
00180   }
00181 
00182   RemoveDuplicatesFromGRFConfigList(list->next);
00183 }
00184 
00189 void AppendStaticGRFConfigs(GRFConfig **dst)
00190 {
00191   GRFConfig **tail = dst;
00192   while (*tail != NULL) tail = &(*tail)->next;
00193 
00194   CopyGRFConfigList(tail, _grfconfig_static, false);
00195   RemoveDuplicatesFromGRFConfigList(*dst);
00196 }
00197 
00201 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
00202 {
00203   GRFConfig **tail = dst;
00204   while (*tail != NULL) tail = &(*tail)->next;
00205   *tail = el;
00206 
00207   RemoveDuplicatesFromGRFConfigList(*dst);
00208 }
00209 
00210 
00211 /* Reset the current GRF Config to either blank or newgame settings */
00212 void ResetGRFConfig(bool defaults)
00213 {
00214   CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults);
00215   AppendStaticGRFConfigs(&_grfconfig);
00216 }
00217 
00218 
00227 GRFListCompatibility IsGoodGRFConfigList()
00228 {
00229   GRFListCompatibility res = GLC_ALL_GOOD;
00230 
00231   for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
00232     const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
00233     if (f == NULL) {
00234       char buf[256];
00235 
00236       /* If we have not found the exactly matching GRF try to find one with the
00237        * same grfid, as it most likely is compatible */
00238       f = FindGRFConfig(c->grfid);
00239       if (f != NULL) {
00240         md5sumToString(buf, lastof(buf), c->md5sum);
00241         DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->grfid), c->filename, buf);
00242         SetBit(c->flags, GCF_COMPATIBLE);
00243 
00244         /* Non-found has precedence over compatibility load */
00245         if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
00246         GamelogGRFCompatible(f);
00247         goto compatible_grf;
00248       }
00249 
00250       /* No compatible grf was found, mark it as disabled */
00251       md5sumToString(buf, lastof(buf), c->md5sum);
00252       DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->grfid), c->filename, buf);
00253 
00254       GamelogGRFRemove(c->grfid);
00255 
00256       c->status = GCS_NOT_FOUND;
00257       res = GLC_NOT_FOUND;
00258     } else {
00259 compatible_grf:
00260       DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->grfid), f->filename);
00261       /* The filename could be the filename as in the savegame. As we need
00262        * to load the GRF here, we need the correct filename, so overwrite that
00263        * in any case and set the name and info when it is not set already.
00264        * When the GCF_COPY flag is set, it is certain that the filename is
00265        * already a local one, so there is no need to replace it. */
00266       if (!HasBit(c->flags, GCF_COPY)) {
00267         free(c->filename);
00268         c->filename = strdup(f->filename);
00269         memcpy(c->md5sum, f->md5sum, sizeof(c->md5sum));
00270         if (c->name == NULL && f->name != NULL) c->name = strdup(f->name);
00271         if (c->info == NULL && f->info != NULL) c->info = strdup(f->info);
00272         c->error = NULL;
00273       }
00274     }
00275   }
00276 
00277   return res;
00278 }
00279 
00281 class GRFFileScanner : FileScanner {
00282 public:
00283   /* virtual */ bool AddFile(const char *filename, size_t basepath_length);
00284 
00286   static uint DoScan()
00287   {
00288     GRFFileScanner fs;
00289     return fs.Scan(".grf", DATA_DIR);
00290   }
00291 };
00292 
00293 bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length)
00294 {
00295   GRFConfig *c = CallocT<GRFConfig>(1);
00296   c->filename = strdup(filename + basepath_length);
00297 
00298   bool added = true;
00299   if (FillGRFDetails(c, false)) {
00300     if (_all_grfs == NULL) {
00301       _all_grfs = c;
00302     } else {
00303       /* Insert file into list at a position determined by its
00304        * name, so the list is sorted as we go along */
00305       GRFConfig **pd, *d;
00306       bool stop = false;
00307       for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
00308         if (c->grfid == d->grfid && memcmp(c->md5sum, d->md5sum, sizeof(c->md5sum)) == 0) added = false;
00309         /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
00310          *  before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
00311          *  just after the first with the same name. Avoids doubles in the list. */
00312         if (strcasecmp(c->name, d->name) <= 0) {
00313           stop = true;
00314         } else if (stop) {
00315           break;
00316         }
00317       }
00318       if (added) {
00319         c->next = d;
00320         *pd = c;
00321       }
00322     }
00323   } else {
00324     added = false;
00325   }
00326 
00327   if (!added) {
00328     /* File couldn't be opened, or is either not a NewGRF or is a
00329      * 'system' NewGRF or it's already known, so forget about it. */
00330     free(c->filename);
00331     free(c->name);
00332     free(c->info);
00333     free(c);
00334   }
00335 
00336   return added;
00337 }
00338 
00345 static int CDECL GRFSorter(const void *p1, const void *p2)
00346 {
00347   const GRFConfig *c1 = *(const GRFConfig **)p1;
00348   const GRFConfig *c2 = *(const GRFConfig **)p2;
00349 
00350   return strcasecmp(c1->name != NULL ? c1->name : c1->filename,
00351     c2->name != NULL ? c2->name : c2->filename);
00352 }
00353 
00354 /* Scan for all NewGRFs */
00355 void ScanNewGRFFiles()
00356 {
00357   ClearGRFConfigList(&_all_grfs);
00358 
00359   DEBUG(grf, 1, "Scanning for NewGRFs");
00360   uint num = GRFFileScanner::DoScan();
00361 
00362   DEBUG(grf, 1, "Scan complete, found %d files", num);
00363   if (num == 0 || _all_grfs == NULL) return;
00364 
00365   /* Sort the linked list using quicksort.
00366    * For that we first have to make an array, the qsort and
00367    * then remake the linked list. */
00368   GRFConfig **to_sort = MallocT<GRFConfig*>(num);
00369 
00370   uint i = 0;
00371   for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) {
00372     to_sort[i] = p;
00373   }
00374   /* Number of files is not necessarily right */
00375   num = i;
00376 
00377   qsort(to_sort, num, sizeof(GRFConfig*), GRFSorter);
00378 
00379   for (i = 1; i < num; i++) {
00380     to_sort[i - 1]->next = to_sort[i];
00381   }
00382   to_sort[num - 1]->next = NULL;
00383   _all_grfs = to_sort[0];
00384 
00385   free(to_sort);
00386 }
00387 
00388 
00389 /* Find a NewGRF in the scanned list, if md5sum is NULL, we don't care about it*/
00390 const GRFConfig *FindGRFConfig(uint32 grfid, const uint8 *md5sum)
00391 {
00392   for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
00393     if (c->grfid == grfid) {
00394       if (md5sum == NULL) return c;
00395 
00396       if (memcmp(md5sum, c->md5sum, sizeof(c->md5sum)) == 0) return c;
00397     }
00398   }
00399 
00400   return NULL;
00401 }
00402 
00403 #ifdef ENABLE_NETWORK
00404 
00406 struct UnknownGRF : public GRFIdentifier {
00407   UnknownGRF *next;
00408   char   name[NETWORK_GRF_NAME_LENGTH];
00409 };
00410 
00428 char *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
00429 {
00430   UnknownGRF *grf;
00431   static UnknownGRF *unknown_grfs = NULL;
00432 
00433   for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
00434     if (grf->grfid == grfid) {
00435       if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
00436     }
00437   }
00438 
00439   if (!create) return NULL;
00440 
00441   grf = CallocT<UnknownGRF>(1);
00442   grf->grfid = grfid;
00443   grf->next  = unknown_grfs;
00444   strecpy(grf->name, UNKNOWN_GRF_NAME_PLACEHOLDER, lastof(grf->name));
00445   memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
00446 
00447   unknown_grfs = grf;
00448   return grf->name;
00449 }
00450 
00451 #endif /* ENABLE_NETWORK */
00452 
00453 
00454 /* Retrieve a NewGRF from the current config by its grfid */
00455 GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask)
00456 {
00457   GRFConfig *c;
00458 
00459   for (c = _grfconfig; c != NULL; c = c->next) {
00460     if ((c->grfid & mask) == (grfid & mask)) return c;
00461   }
00462 
00463   return NULL;
00464 }
00465 
00466 
00467 /* Build a space separated list of parameters, and terminate */
00468 char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
00469 {
00470   uint i;
00471 
00472   /* Return an empty string if there are no parameters */
00473   if (c->num_params == 0) return strecpy(dst, "", last);
00474 
00475   for (i = 0; i < c->num_params; i++) {
00476     if (i > 0) dst = strecpy(dst, " ", last);
00477     dst += seprintf(dst, last, "%d", c->param[i]);
00478   }
00479   return dst;
00480 }
00481 
00483 static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400);
00484 
00489 bool GRFConfig::IsOpenTTDBaseGRF() const
00490 {
00491   return (this->grfid & 0x00FFFFFF) == OPENTTD_GRAPHICS_BASE_GRF_ID;
00492 }

Generated on Sun Sep 13 08:19:17 2009 for OpenTTD by  doxygen 1.5.6