peter1138@5228: /* $Id$ */ peter1138@5228: rubidium@9111: /** @file newgrf_config.cpp Finding NewGRFs and configuring them. */ belugas@6348: peter1138@5228: #include "stdafx.h" peter1138@5228: #include "openttd.h" peter1138@5228: #include "debug.h" peter1138@5228: #include "variables.h" peter1138@5228: #include "saveload.h" peter1138@5228: #include "md5.h" rubidium@5469: #include "network/network_data.h" peter1138@5228: #include "newgrf.h" peter1138@5228: #include "newgrf_config.h" rubidium@8213: #include "core/alloc_func.hpp" rubidium@8214: #include "string_func.h" peter1138@5228: peter1138@5228: #include "fileio.h" peter1138@5228: #include "fios.h" peter1138@5228: #include peter1138@5228: peter1138@5228: peter1138@5228: GRFConfig *_all_grfs; peter1138@5228: GRFConfig *_grfconfig; peter1138@5228: GRFConfig *_grfconfig_newgame; peter1138@5329: GRFConfig *_grfconfig_static; peter1138@5228: peter1138@5228: peter1138@5228: /* Calculate the MD5 Sum for a GRF */ peter1138@5228: static bool CalcGRFMD5Sum(GRFConfig *config) peter1138@5228: { peter1138@5228: FILE *f; skidd13@8133: Md5 checksum; skidd13@8133: uint8 buffer[1024]; truelight@7574: size_t len, size; peter1138@5228: peter1138@5228: /* open the file */ truelight@7574: f = FioFOpenFile(config->filename, "rb", DATA_DIR, &size); peter1138@5228: if (f == NULL) return false; peter1138@5228: peter1138@5228: /* calculate md5sum */ truelight@7574: while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) { truelight@7574: size -= len; skidd13@8133: checksum.Append(buffer, len); peter1138@5228: } skidd13@8133: checksum.Finish(config->md5sum); peter1138@5228: truelight@7592: FioFCloseFile(f); peter1138@5228: peter1138@5228: return true; peter1138@5228: } peter1138@5228: peter1138@5228: peter1138@5228: /* Find the GRFID and calculate the md5sum */ peter1138@5329: bool FillGRFDetails(GRFConfig *config, bool is_static) peter1138@5228: { rubidium@7034: if (!FioCheckFileExists(config->filename)) { maedhros@6229: config->status = GCS_NOT_FOUND; peter1138@5228: return false; peter1138@5228: } peter1138@5228: peter1138@5228: /* Find and load the Action 8 information */ rubidium@7805: LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN); peter1138@5228: peter1138@5228: /* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */ rubidium@7882: if (config->grfid == 0 || config->grfid == 0xFFFFFFFF || config->IsOpenTTDBaseGRF()) return false; peter1138@5228: peter1138@5502: if (is_static) { peter1138@5502: /* Perform a 'safety scan' for static GRFs */ peter1138@5502: LoadNewGRFFile(config, 62, GLS_SAFETYSCAN); peter1138@5502: peter1138@5502: /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */ skidd13@7928: if (HasBit(config->flags, GCF_UNSAFE)) return false; peter1138@5502: } peter1138@5502: peter1138@5228: return CalcGRFMD5Sum(config); peter1138@5228: } peter1138@5228: peter1138@5228: Darkvater@5346: void ClearGRFConfig(GRFConfig **config) peter1138@5329: { rubidium@5339: /* GCF_COPY as in NOT strdupped/alloced the filename, name and info */ skidd13@7928: if (!HasBit((*config)->flags, GCF_COPY)) { Darkvater@5346: free((*config)->filename); Darkvater@5346: free((*config)->name); Darkvater@5346: free((*config)->info); maedhros@6873: maedhros@6873: if ((*config)->error != NULL) { maedhros@6873: free((*config)->error->custom_message); maedhros@6873: free((*config)->error->data); maedhros@6873: free((*config)->error); maedhros@6873: } rubidium@5339: } Darkvater@5346: free(*config); Darkvater@5346: *config = NULL; peter1138@5329: } peter1138@5329: peter1138@5329: peter1138@5228: /* Clear a GRF Config list */ Darkvater@5347: void ClearGRFConfigList(GRFConfig **config) peter1138@5228: { peter1138@5228: GRFConfig *c, *next; Darkvater@5347: for (c = *config; c != NULL; c = next) { peter1138@5228: next = c->next; Darkvater@5346: ClearGRFConfig(&c); peter1138@5228: } Darkvater@5347: *config = NULL; peter1138@5228: } peter1138@5228: peter1138@5228: Darkvater@5347: /** Copy a GRF Config list Darkvater@5347: * @param dst pointer to destination list Darkvater@6073: * @param src pointer to source list values glx@6956: * @param init_only the copied GRF will be processed up to GLS_INIT Darkvater@5347: * @return pointer to the last value added to the destination list */ glx@6956: GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only) peter1138@5228: { Darkvater@5351: /* Clear destination as it will be overwritten */ Darkvater@5351: ClearGRFConfigList(dst); peter1138@5228: for (; src != NULL; src = src->next) { KUDr@5609: GRFConfig *c = CallocT(1); peter1138@5228: *c = *src; rubidium@6424: if (src->filename != NULL) c->filename = strdup(src->filename); rubidium@6424: if (src->name != NULL) c->name = strdup(src->name); rubidium@6424: if (src->info != NULL) c->info = strdup(src->info); rubidium@6424: if (src->error != NULL) { maedhros@6139: c->error = CallocT(1); maedhros@6139: memcpy(c->error, src->error, sizeof(GRFError)); maedhros@6873: if (src->error->data != NULL) c->error->data = strdup(src->error->data); maedhros@6873: if (src->error->custom_message != NULL) c->error->custom_message = strdup(src->error->custom_message); maedhros@6139: } peter1138@5228: skidd13@7929: ClrBit(c->flags, GCF_INIT_ONLY); skidd13@7931: if (init_only) SetBit(c->flags, GCF_INIT_ONLY); glx@6956: peter1138@5228: *dst = c; peter1138@5228: dst = &c->next; peter1138@5228: } peter1138@5329: peter1138@5329: return dst; peter1138@5228: } peter1138@5228: rubidium@5393: /** rubidium@5393: * Removes duplicates from lists of GRFConfigs. These duplicates rubidium@5393: * are introduced when the _grfconfig_static GRFs are appended rubidium@5393: * to the _grfconfig on a newgame or savegame. As the parameters rubidium@5393: * of the static GRFs could be different that the parameters of rubidium@5393: * the ones used non-statically. This can result in desyncs in rubidium@5393: * multiplayers, so the duplicate static GRFs have to be removed. rubidium@5393: * rubidium@5393: * This function _assumes_ that all static GRFs are placed after rubidium@5393: * the non-static GRFs. rubidium@5393: * rubidium@5393: * @param list the list to remove the duplicates from rubidium@5393: */ rubidium@5393: static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list) rubidium@5393: { rubidium@5393: GRFConfig *prev; rubidium@5393: GRFConfig *cur; rubidium@5393: rubidium@5393: if (list == NULL) return; rubidium@5393: rubidium@5393: for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) { rubidium@5393: if (cur->grfid != list->grfid) continue; Darkvater@5675: rubidium@5393: prev->next = cur->next; rubidium@5393: ClearGRFConfig(&cur); rubidium@5393: cur = prev; // Just go back one so it continues as normal later on rubidium@5393: } rubidium@5393: rubidium@5393: RemoveDuplicatesFromGRFConfigList(list->next); rubidium@5393: } rubidium@5393: rubidium@5393: /** rubidium@5393: * Appends the static GRFs to a list of GRFs rubidium@5393: * @param dst the head of the list to add to rubidium@5393: */ rubidium@5393: void AppendStaticGRFConfigs(GRFConfig **dst) rubidium@5393: { rubidium@5393: GRFConfig **tail = dst; rubidium@5393: while (*tail != NULL) tail = &(*tail)->next; rubidium@5393: glx@6956: CopyGRFConfigList(tail, _grfconfig_static, false); rubidium@5393: RemoveDuplicatesFromGRFConfigList(*dst); rubidium@5393: } rubidium@5393: Darkvater@6073: /** Appends an element to a list of GRFs belugas@6481: * @param dst the head of the list to add to belugas@6481: * @param el the new tail to be */ Darkvater@6108: void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el) Darkvater@6073: { Darkvater@6073: GRFConfig **tail = dst; Darkvater@6073: while (*tail != NULL) tail = &(*tail)->next; Darkvater@6108: *tail = el; Darkvater@6108: Darkvater@6073: RemoveDuplicatesFromGRFConfigList(*dst); Darkvater@6073: } Darkvater@6073: peter1138@5228: peter1138@5228: /* Reset the current GRF Config to either blank or newgame settings */ peter1138@5228: void ResetGRFConfig(bool defaults) peter1138@5228: { glx@6956: CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults); rubidium@5393: AppendStaticGRFConfigs(&_grfconfig); peter1138@5228: } peter1138@5228: peter1138@5228: Darkvater@5647: /** Check if all GRFs in the GRF config from a savegame can be loaded. Darkvater@5647: * @return will return any of the following 3 values:
Darkvater@5647: *
    maedhros@6229: *
  • GLC_ALL_GOOD: No problems occured, all GRF files were found and loaded maedhros@6229: *
  • GLC_COMPATIBLE: For one or more GRF's no exact match was found, but a Darkvater@5647: * compatible GRF with the same grfid was found and used instead maedhros@6229: *
  • GLC_NOT_FOUND: For one or more GRF's no match was found at all Darkvater@5647: *
*/ rubidium@6247: GRFListCompatibility IsGoodGRFConfigList() peter1138@5228: { maedhros@6229: GRFListCompatibility res = GLC_ALL_GOOD; peter1138@5228: Darkvater@5647: for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) { peter1138@5228: const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum); peter1138@5228: if (f == NULL) { glx@5652: char buf[256]; peter1138@5228: Darkvater@5647: /* If we have not found the exactly matching GRF try to find one with the Darkvater@5647: * same grfid, as it most likely is compatible */ Darkvater@5647: f = FindGRFConfig(c->grfid); Darkvater@5647: if (f != NULL) { Darkvater@5647: md5sumToString(buf, lastof(buf), c->md5sum); Darkvater@5647: DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->grfid), c->filename, buf); skidd13@7931: SetBit(c->flags, GCF_COMPATIBLE); peter1138@5228: Darkvater@5647: /* Non-found has precedence over compatibility load */ maedhros@6229: if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE; Darkvater@5647: goto compatible_grf; Darkvater@5647: } Darkvater@5647: Darkvater@5647: /* No compatible grf was found, mark it as disabled */ Darkvater@5647: md5sumToString(buf, lastof(buf), c->md5sum); Darkvater@5647: DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->grfid), c->filename, buf); Darkvater@5647: maedhros@6229: c->status = GCS_NOT_FOUND; maedhros@6229: res = GLC_NOT_FOUND; peter1138@5228: } else { Darkvater@5647: compatible_grf: Darkvater@5647: DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->grfid), f->filename); rubidium@5349: /* The filename could be the filename as in the savegame. As we need rubidium@5349: * to load the GRF here, we need the correct filename, so overwrite that rubidium@5349: * in any case and set the name and info when it is not set already. rubidium@5349: * When the GCF_COPY flag is set, it is certain that the filename is rubidium@5349: * already a local one, so there is no need to replace it. */ skidd13@7928: if (!HasBit(c->flags, GCF_COPY)) { rubidium@5349: free(c->filename); rubidium@5349: c->filename = strdup(f->filename); Darkvater@5701: memcpy(c->md5sum, f->md5sum, sizeof(c->md5sum)); rubidium@6183: if (c->name == NULL && f->name != NULL) c->name = strdup(f->name); rubidium@6183: if (c->info == NULL && f->info != NULL) c->info = strdup(f->info); maedhros@6103: c->error = NULL; rubidium@5349: } peter1138@5228: } peter1138@5228: } peter1138@5228: peter1138@5228: return res; peter1138@5228: } peter1138@5228: truelight@7572: static bool ScanPathAddGrf(const char *filename) truelight@7572: { truelight@7572: GRFConfig *c = CallocT(1); truelight@7572: c->filename = strdup(filename); peter1138@5228: truelight@7572: bool added = true; truelight@7572: if (FillGRFDetails(c, false)) { truelight@7572: if (_all_grfs == NULL) { truelight@7572: _all_grfs = c; truelight@7572: } else { truelight@7572: /* Insert file into list at a position determined by its peter1138@8592: * name, so the list is sorted as we go along */ truelight@7572: GRFConfig **pd, *d; truelight@7572: bool stop = false; truelight@7572: for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) { truelight@7572: if (c->grfid == d->grfid && memcmp(c->md5sum, d->md5sum, sizeof(c->md5sum)) == 0) added = false; truelight@7572: /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name, truelight@7573: * before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of truelight@7573: * just after the first with the same name. Avoids doubles in the list. */ peter1138@8592: if (strcasecmp(c->name, d->name) <= 0) { peter1138@8592: stop = true; peter1138@8592: } else if (stop) { peter1138@8592: break; peter1138@8592: } truelight@7572: } truelight@7572: if (added) { truelight@7572: c->next = d; truelight@7572: *pd = c; truelight@7572: } truelight@7572: } truelight@7572: } else { truelight@7572: added = false; truelight@7572: } truelight@7572: truelight@7572: if (!added) { truelight@7572: /* File couldn't be opened, or is either not a NewGRF or is a peter1138@8592: * 'system' NewGRF or it's already known, so forget about it. */ truelight@7572: free(c->filename); truelight@7572: free(c->name); truelight@7572: free(c->info); truelight@7572: free(c); truelight@7572: } truelight@7572: truelight@7572: return added; truelight@7572: } peter1138@5228: peter1138@5228: /* Scan a path for NewGRFs */ glx@9146: static uint ScanPath(const char *path, size_t basepath_length) peter1138@5228: { truelight@7572: extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb); truelight@7572: peter1138@5228: uint num = 0; peter1138@5228: struct stat sb; peter1138@5228: struct dirent *dirent; peter1138@5228: DIR *dir; peter1138@5228: rubidium@6424: if (path == NULL || (dir = ttd_opendir(path)) == NULL) return 0; peter1138@5228: peter1138@5228: while ((dirent = readdir(dir)) != NULL) { peter1138@5228: const char *d_name = FS2OTTD(dirent->d_name); peter1138@5228: char filename[MAX_PATH]; peter1138@5228: peter1138@5228: if (!FiosIsValidFile(path, dirent, &sb)) continue; peter1138@5228: rubidium@6424: snprintf(filename, lengthof(filename), "%s%s", path, d_name); peter1138@5228: peter1138@5228: if (sb.st_mode & S_IFDIR) { peter1138@5228: /* Directory */ peter1138@5228: if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue; rubidium@6424: AppendPathSeparator(filename, lengthof(filename)); rubidium@6834: num += ScanPath(filename, basepath_length); peter1138@5228: } else if (sb.st_mode & S_IFREG) { peter1138@5228: /* File */ peter1138@5228: char *ext = strrchr(filename, '.'); peter1138@5228: peter1138@5228: /* If no extension or extension isn't .grf, skip the file */ peter1138@5228: if (ext == NULL) continue; peter1138@5228: if (strcasecmp(ext, ".grf") != 0) continue; peter1138@5228: truelight@7572: if (ScanPathAddGrf(filename + basepath_length)) num++; peter1138@5228: } peter1138@5228: } peter1138@5228: peter1138@5228: closedir(dir); peter1138@5228: peter1138@5228: return num; peter1138@5228: } peter1138@5228: truelight@7592: static uint ScanTar(TarFileList::iterator tar) truelight@7581: { truelight@7592: uint num = 0; truelight@7592: const char *filename = (*tar).first.c_str(); glx@7586: const char *ext = strrchr(filename, '.'); truelight@7581: truelight@7581: /* If no extension or extension isn't .grf, skip the file */ truelight@7581: if (ext == NULL) return false; truelight@7581: if (strcasecmp(ext, ".grf") != 0) return false; truelight@7581: truelight@7592: if (ScanPathAddGrf(filename)) num++; truelight@7581: truelight@7581: return num; truelight@7581: } peter1138@5228: rubidium@7644: /** rubidium@7644: * Simple sorter for GRFS rubidium@7644: * @param p1 the first GRFConfig * rubidium@7644: * @param p2 the second GRFConfig * rubidium@7644: * @return the same strcmp would return for the name of the NewGRF. rubidium@7644: */ glx@7648: static int CDECL GRFSorter(const void *p1, const void *p2) rubidium@7644: { rubidium@7644: const GRFConfig *c1 = *(const GRFConfig **)p1; rubidium@7644: const GRFConfig *c2 = *(const GRFConfig **)p2; rubidium@7644: rubidium@7644: return strcmp(c1->name != NULL ? c1->name : c1->filename, rubidium@7644: c2->name != NULL ? c2->name : c2->filename); rubidium@7644: } rubidium@7644: peter1138@5228: /* Scan for all NewGRFs */ rubidium@6247: void ScanNewGRFFiles() peter1138@5228: { rubidium@6929: Searchpath sp; rubidium@6929: char path[MAX_PATH]; truelight@7592: TarFileList::iterator tar; rubidium@6929: uint num = 0; peter1138@5228: Darkvater@5347: ClearGRFConfigList(&_all_grfs); peter1138@5228: Darkvater@5380: DEBUG(grf, 1, "Scanning for NewGRFs"); rubidium@6929: FOR_ALL_SEARCHPATHS(sp) { rubidium@6929: FioAppendDirectory(path, MAX_PATH, sp, DATA_DIR); rubidium@6929: num += ScanPath(path, strlen(path)); rubidium@6834: } truelight@7581: FOR_ALL_TARS(tar) { truelight@7581: num += ScanTar(tar); truelight@7581: } rubidium@7644: Darkvater@5380: DEBUG(grf, 1, "Scan complete, found %d files", num); rubidium@7649: if (num == 0 || _all_grfs == NULL) return; rubidium@7644: rubidium@7644: /* Sort the linked list using quicksort. rubidium@7644: * For that we first have to make an array, the qsort and rubidium@7644: * then remake the linked list. */ rubidium@7644: GRFConfig **to_sort = MallocT(num); rubidium@7644: rubidium@7644: uint i = 0; rubidium@7644: for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) { rubidium@7644: to_sort[i] = p; rubidium@7644: } rubidium@7644: /* Number of files is not necessarily right */ rubidium@7644: num = i; rubidium@7644: rubidium@7644: qsort(to_sort, num, sizeof(GRFConfig*), GRFSorter); rubidium@7644: rubidium@7644: for (i = 1; i < num; i++) { rubidium@7644: to_sort[i - 1]->next = to_sort[i]; rubidium@7644: } rubidium@7644: to_sort[num - 1]->next = NULL; rubidium@7644: _all_grfs = to_sort[0]; smatz@9133: smatz@9133: free(to_sort); peter1138@5228: } peter1138@5228: peter1138@5228: Darkvater@5646: /* Find a NewGRF in the scanned list, if md5sum is NULL, we don't care about it*/ Darkvater@5646: const GRFConfig *FindGRFConfig(uint32 grfid, const uint8 *md5sum) peter1138@5228: { Darkvater@5646: for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) { Darkvater@5646: if (c->grfid == grfid) { Darkvater@5646: if (md5sum == NULL) return c; peter1138@5228: peter1138@5228: if (memcmp(md5sum, c->md5sum, sizeof(c->md5sum)) == 0) return c; peter1138@5228: } peter1138@5228: } peter1138@5228: peter1138@5228: return NULL; peter1138@5228: } peter1138@5228: Darkvater@5441: #ifdef ENABLE_NETWORK Darkvater@5441: rubidium@5339: /** Structure for UnknownGRFs; this is a lightweight variant of GRFConfig */ rubidium@5765: struct UnknownGRF : public GRFIdentifier { rubidium@5339: UnknownGRF *next; rubidium@5339: char name[NETWORK_GRF_NAME_LENGTH]; rubidium@5339: }; rubidium@5339: rubidium@5339: /** rubidium@5339: * Finds the name of a NewGRF in the list of names for unknown GRFs. An rubidium@5339: * unknown GRF is a GRF where the .grf is not found during scanning. rubidium@5339: * rubidium@5339: * The names are resolved via UDP calls to servers that should know the name, rubidium@5339: * though the replies may not come. This leaves "" as name, though rubidium@5339: * that shouldn't matter _very_ much as they need GRF crawler or so to look rubidium@5339: * up the GRF anyway and that works better with the GRF ID. rubidium@5339: * rubidium@5339: * @param grfid the GRF ID part of the 'unique' GRF identifier rubidium@5339: * @param md5sum the MD5 checksum part of the 'unique' GRF identifier rubidium@5339: * @param create whether to create a new GRFConfig if the GRFConfig did not rubidium@5339: * exist in the fake list of GRFConfigs. rubidium@5339: * @return the GRFConfig with the given GRF ID and MD5 checksum or NULL when rubidium@5339: * it does not exist and create is false. This value must NEVER be rubidium@5339: * freed by the caller. rubidium@5339: */ rubidium@5339: char *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create) rubidium@5339: { rubidium@5339: UnknownGRF *grf; rubidium@5339: static UnknownGRF *unknown_grfs = NULL; rubidium@5339: rubidium@5339: for (grf = unknown_grfs; grf != NULL; grf = grf->next) { rubidium@5339: if (grf->grfid == grfid) { rubidium@5339: if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name; rubidium@5339: } rubidium@5339: } rubidium@5339: rubidium@5339: if (!create) return NULL; rubidium@5339: KUDr@5609: grf = CallocT(1); rubidium@5339: grf->grfid = grfid; rubidium@5339: grf->next = unknown_grfs; rubidium@5339: ttd_strlcpy(grf->name, UNKNOWN_GRF_NAME_PLACEHOLDER, sizeof(grf->name)); rubidium@5339: memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum)); rubidium@5339: rubidium@5339: unknown_grfs = grf; rubidium@5339: return grf->name; rubidium@5339: } rubidium@5339: Darkvater@5441: #endif /* ENABLE_NETWORK */ Darkvater@5441: peter1138@5228: peter1138@5234: /* Retrieve a NewGRF from the current config by its grfid */ peter1138@5333: GRFConfig *GetGRFConfig(uint32 grfid) peter1138@5234: { peter1138@5234: GRFConfig *c; peter1138@5234: peter1138@5234: for (c = _grfconfig; c != NULL; c = c->next) { peter1138@5234: if (c->grfid == grfid) return c; peter1138@5234: } peter1138@5234: peter1138@5234: return NULL; peter1138@5234: } peter1138@5234: peter1138@5234: peter1138@5308: /* Build a space separated list of parameters, and terminate */ peter1138@5308: char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last) peter1138@5308: { peter1138@5308: uint i; peter1138@5308: peter1138@5308: /* Return an empty string if there are no parameters */ peter1138@5308: if (c->num_params == 0) return strecpy(dst, "", last); peter1138@5308: peter1138@5308: for (i = 0; i < c->num_params; i++) { peter1138@5308: if (i > 0) dst = strecpy(dst, " ", last); peter1138@5308: dst += snprintf(dst, last - dst, "%d", c->param[i]); peter1138@5308: } peter1138@5308: return dst; peter1138@5308: } peter1138@5308: rubidium@7882: /** Base GRF ID for OpenTTD's base graphics GRFs. */ rubidium@7882: static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400); rubidium@7882: rubidium@7882: /** rubidium@7882: * Checks whether this GRF is a OpenTTD base graphic GRF. rubidium@7882: * @return true if and only if it is a base GRF. rubidium@7882: */ rubidium@7882: bool GRFConfig::IsOpenTTDBaseGRF() const rubidium@7882: { rubidium@7882: return (this->grfid & 0x00FFFFFF) == OPENTTD_GRAPHICS_BASE_GRF_ID; rubidium@7882: } rubidium@7882: peter1138@5308: peter1138@5228: static const SaveLoad _grfconfig_desc[] = { peter1138@5228: SLE_STR(GRFConfig, filename, SLE_STR, 0x40), peter1138@5228: SLE_VAR(GRFConfig, grfid, SLE_UINT32), peter1138@5228: SLE_ARR(GRFConfig, md5sum, SLE_UINT8, 16), peter1138@5228: SLE_ARR(GRFConfig, param, SLE_UINT32, 0x80), peter1138@5228: SLE_VAR(GRFConfig, num_params, SLE_UINT8), peter1138@5228: SLE_END() peter1138@5228: }; peter1138@5228: peter1138@5228: rubidium@6247: static void Save_NGRF() peter1138@5228: { peter1138@5228: int index = 0; peter1138@5228: Darkvater@6108: for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) { skidd13@7928: if (HasBit(c->flags, GCF_STATIC)) continue; peter1138@5228: SlSetArrayIndex(index++); peter1138@5228: SlObject(c, _grfconfig_desc); peter1138@5228: } peter1138@5228: } peter1138@5228: peter1138@5228: rubidium@6247: static void Load_NGRF() peter1138@5228: { Darkvater@6108: ClearGRFConfigList(&_grfconfig); peter1138@5228: while (SlIterateArray() != -1) { Darkvater@6108: GRFConfig *c = CallocT(1); Darkvater@6108: SlObject(c, _grfconfig_desc); Darkvater@6108: AppendToGRFConfigList(&_grfconfig, c); peter1138@5228: } peter1138@5228: peter1138@5329: /* Append static NewGRF configuration */ rubidium@5393: AppendStaticGRFConfigs(&_grfconfig); peter1138@5228: } peter1138@5228: rubidium@5587: extern const ChunkHandler _newgrf_chunk_handlers[] = { peter1138@5228: { 'NGRF', Save_NGRF, Load_NGRF, CH_ARRAY | CH_LAST } peter1138@5228: }; peter1138@5228: Darkvater@5380: belugas@6348: