peter1138@5228: /* $Id$ */ peter1138@5228: belugas@6905: /** @file newgrf_config.cpp */ belugas@6674: 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@5720: #include "network/network_data.h" peter1138@5228: #include "newgrf.h" peter1138@5228: #include "newgrf_config.h" rubidium@8709: #include "core/alloc_func.hpp" rubidium@8710: #include "string_func.h" peter1138@5228: peter1138@5228: #include "fileio.h" peter1138@5228: #include "fios.h" peter1138@5228: #include peter1138@5228: peter1138@5228: #ifdef WIN32 peter1138@5228: # include peter1138@5228: #endif /* WIN32 */ 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@8629: Md5 checksum; skidd13@8629: uint8 buffer[1024]; truelight@8070: size_t len, size; peter1138@5228: peter1138@5228: /* open the file */ truelight@8070: f = FioFOpenFile(config->filename, "rb", DATA_DIR, &size); peter1138@5228: if (f == NULL) return false; peter1138@5228: peter1138@5228: /* calculate md5sum */ truelight@8070: while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) { truelight@8070: size -= len; skidd13@8629: checksum.Append(buffer, len); peter1138@5228: } skidd13@8629: checksum.Finish(config->md5sum); peter1138@5228: truelight@8088: 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@7530: if (!FioCheckFileExists(config->filename)) { maedhros@6555: config->status = GCS_NOT_FOUND; peter1138@5228: return false; peter1138@5228: } peter1138@5228: peter1138@5228: /* Find and load the Action 8 information */ rubidium@8301: LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN); peter1138@5228: peter1138@5228: /* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */ rubidium@8378: if (config->grfid == 0 || config->grfid == 0xFFFFFFFF || config->IsOpenTTDBaseGRF()) return false; peter1138@5228: peter1138@5753: if (is_static) { peter1138@5753: /* Perform a 'safety scan' for static GRFs */ peter1138@5753: LoadNewGRFFile(config, 62, GLS_SAFETYSCAN); peter1138@5753: peter1138@5753: /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */ skidd13@8424: if (HasBit(config->flags, GCF_UNSAFE)) return false; peter1138@5753: } peter1138@5753: 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@8424: if (!HasBit((*config)->flags, GCF_COPY)) { Darkvater@5346: free((*config)->filename); Darkvater@5346: free((*config)->name); Darkvater@5346: free((*config)->info); maedhros@7369: maedhros@7369: if ((*config)->error != NULL) { maedhros@7369: free((*config)->error->custom_message); maedhros@7369: free((*config)->error->data); maedhros@7369: free((*config)->error); maedhros@7369: } 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@6399: * @param src pointer to source list values glx@7452: * @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@7452: 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@5860: GRFConfig *c = CallocT(1); peter1138@5228: *c = *src; rubidium@6920: if (src->filename != NULL) c->filename = strdup(src->filename); rubidium@6920: if (src->name != NULL) c->name = strdup(src->name); rubidium@6920: if (src->info != NULL) c->info = strdup(src->info); rubidium@6920: if (src->error != NULL) { maedhros@6465: c->error = CallocT(1); maedhros@6465: memcpy(c->error, src->error, sizeof(GRFError)); maedhros@7369: if (src->error->data != NULL) c->error->data = strdup(src->error->data); maedhros@7369: if (src->error->custom_message != NULL) c->error->custom_message = strdup(src->error->custom_message); maedhros@6465: } peter1138@5228: skidd13@8425: ClrBit(c->flags, GCF_INIT_ONLY); skidd13@8427: if (init_only) SetBit(c->flags, GCF_INIT_ONLY); glx@7452: peter1138@5228: *dst = c; peter1138@5228: dst = &c->next; peter1138@5228: } peter1138@5329: peter1138@5329: return dst; peter1138@5228: } peter1138@5228: rubidium@5581: /** rubidium@5581: * Removes duplicates from lists of GRFConfigs. These duplicates rubidium@5581: * are introduced when the _grfconfig_static GRFs are appended rubidium@5581: * to the _grfconfig on a newgame or savegame. As the parameters rubidium@5581: * of the static GRFs could be different that the parameters of rubidium@5581: * the ones used non-statically. This can result in desyncs in rubidium@5581: * multiplayers, so the duplicate static GRFs have to be removed. rubidium@5581: * rubidium@5581: * This function _assumes_ that all static GRFs are placed after rubidium@5581: * the non-static GRFs. rubidium@5581: * rubidium@5581: * @param list the list to remove the duplicates from rubidium@5581: */ rubidium@5581: static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list) rubidium@5581: { rubidium@5581: GRFConfig *prev; rubidium@5581: GRFConfig *cur; rubidium@5581: rubidium@5581: if (list == NULL) return; rubidium@5581: rubidium@5581: for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) { rubidium@5581: if (cur->grfid != list->grfid) continue; Darkvater@5926: rubidium@5581: prev->next = cur->next; rubidium@5581: ClearGRFConfig(&cur); rubidium@5581: cur = prev; // Just go back one so it continues as normal later on rubidium@5581: } rubidium@5581: rubidium@5581: RemoveDuplicatesFromGRFConfigList(list->next); rubidium@5581: } rubidium@5581: rubidium@5581: /** rubidium@5581: * Appends the static GRFs to a list of GRFs rubidium@5581: * @param dst the head of the list to add to rubidium@5581: */ rubidium@5581: void AppendStaticGRFConfigs(GRFConfig **dst) rubidium@5581: { rubidium@5581: GRFConfig **tail = dst; rubidium@5581: while (*tail != NULL) tail = &(*tail)->next; rubidium@5581: glx@7452: CopyGRFConfigList(tail, _grfconfig_static, false); rubidium@5581: RemoveDuplicatesFromGRFConfigList(*dst); rubidium@5581: } rubidium@5581: Darkvater@6399: /** Appends an element to a list of GRFs belugas@6977: * @param dst the head of the list to add to belugas@6977: * @param el the new tail to be */ Darkvater@6434: void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el) Darkvater@6399: { Darkvater@6399: GRFConfig **tail = dst; Darkvater@6399: while (*tail != NULL) tail = &(*tail)->next; Darkvater@6434: *tail = el; Darkvater@6434: Darkvater@6399: RemoveDuplicatesFromGRFConfigList(*dst); Darkvater@6399: } Darkvater@6399: peter1138@5228: peter1138@5228: /* Reset the current GRF Config to either blank or newgame settings */ peter1138@5228: void ResetGRFConfig(bool defaults) peter1138@5228: { glx@7452: CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults); rubidium@5581: AppendStaticGRFConfigs(&_grfconfig); peter1138@5228: } peter1138@5228: peter1138@5228: Darkvater@5898: /** Check if all GRFs in the GRF config from a savegame can be loaded. Darkvater@5898: * @return will return any of the following 3 values:
Darkvater@5898: *
    maedhros@6555: *
  • GLC_ALL_GOOD: No problems occured, all GRF files were found and loaded maedhros@6555: *
  • GLC_COMPATIBLE: For one or more GRF's no exact match was found, but a Darkvater@5898: * compatible GRF with the same grfid was found and used instead maedhros@6555: *
  • GLC_NOT_FOUND: For one or more GRF's no match was found at all Darkvater@5898: *
*/ rubidium@6573: GRFListCompatibility IsGoodGRFConfigList() peter1138@5228: { maedhros@6555: GRFListCompatibility res = GLC_ALL_GOOD; peter1138@5228: Darkvater@5898: 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@5903: char buf[256]; peter1138@5228: Darkvater@5898: /* If we have not found the exactly matching GRF try to find one with the Darkvater@5898: * same grfid, as it most likely is compatible */ Darkvater@5898: f = FindGRFConfig(c->grfid); Darkvater@5898: if (f != NULL) { Darkvater@5898: md5sumToString(buf, lastof(buf), c->md5sum); Darkvater@5898: DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->grfid), c->filename, buf); skidd13@8427: SetBit(c->flags, GCF_COMPATIBLE); peter1138@5228: Darkvater@5898: /* Non-found has precedence over compatibility load */ maedhros@6555: if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE; Darkvater@5898: goto compatible_grf; Darkvater@5898: } Darkvater@5898: Darkvater@5898: /* No compatible grf was found, mark it as disabled */ Darkvater@5898: md5sumToString(buf, lastof(buf), c->md5sum); Darkvater@5898: DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->grfid), c->filename, buf); Darkvater@5898: maedhros@6555: c->status = GCS_NOT_FOUND; maedhros@6555: res = GLC_NOT_FOUND; peter1138@5228: } else { Darkvater@5898: compatible_grf: Darkvater@5898: 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@8424: if (!HasBit(c->flags, GCF_COPY)) { rubidium@5349: free(c->filename); rubidium@5349: c->filename = strdup(f->filename); Darkvater@5952: memcpy(c->md5sum, f->md5sum, sizeof(c->md5sum)); rubidium@6509: if (c->name == NULL && f->name != NULL) c->name = strdup(f->name); rubidium@6509: if (c->info == NULL && f->info != NULL) c->info = strdup(f->info); maedhros@6429: c->error = NULL; rubidium@5349: } peter1138@5228: } peter1138@5228: } peter1138@5228: peter1138@5228: return res; peter1138@5228: } peter1138@5228: truelight@8068: static bool ScanPathAddGrf(const char *filename) truelight@8068: { truelight@8068: GRFConfig *c = CallocT(1); truelight@8068: c->filename = strdup(filename); peter1138@5228: truelight@8068: bool added = true; truelight@8068: if (FillGRFDetails(c, false)) { truelight@8068: if (_all_grfs == NULL) { truelight@8068: _all_grfs = c; truelight@8068: } else { truelight@8068: /* Insert file into list at a position determined by its peter1138@9088: * name, so the list is sorted as we go along */ truelight@8068: GRFConfig **pd, *d; truelight@8068: bool stop = false; truelight@8068: for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) { truelight@8068: if (c->grfid == d->grfid && memcmp(c->md5sum, d->md5sum, sizeof(c->md5sum)) == 0) added = false; truelight@8068: /* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name, truelight@8069: * before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of truelight@8069: * just after the first with the same name. Avoids doubles in the list. */ peter1138@9088: if (strcasecmp(c->name, d->name) <= 0) { peter1138@9088: stop = true; peter1138@9088: } else if (stop) { peter1138@9088: break; peter1138@9088: } truelight@8068: } truelight@8068: if (added) { truelight@8068: c->next = d; truelight@8068: *pd = c; truelight@8068: } truelight@8068: } truelight@8068: } else { truelight@8068: added = false; truelight@8068: } truelight@8068: truelight@8068: if (!added) { truelight@8068: /* File couldn't be opened, or is either not a NewGRF or is a peter1138@9088: * 'system' NewGRF or it's already known, so forget about it. */ truelight@8068: free(c->filename); truelight@8068: free(c->name); truelight@8068: free(c->info); truelight@8068: free(c); truelight@8068: } truelight@8068: truelight@8068: return added; truelight@8068: } peter1138@5228: peter1138@5228: /* Scan a path for NewGRFs */ rubidium@7330: static uint ScanPath(const char *path, int basepath_length) peter1138@5228: { truelight@8068: extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb); truelight@8068: peter1138@5228: uint num = 0; peter1138@5228: struct stat sb; peter1138@5228: struct dirent *dirent; peter1138@5228: DIR *dir; peter1138@5228: rubidium@6920: 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@6920: 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@6920: AppendPathSeparator(filename, lengthof(filename)); rubidium@7330: 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@8068: 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@8088: static uint ScanTar(TarFileList::iterator tar) truelight@8077: { truelight@8088: uint num = 0; truelight@8088: const char *filename = (*tar).first.c_str(); glx@8082: const char *ext = strrchr(filename, '.'); truelight@8077: truelight@8077: /* If no extension or extension isn't .grf, skip the file */ truelight@8077: if (ext == NULL) return false; truelight@8077: if (strcasecmp(ext, ".grf") != 0) return false; truelight@8077: truelight@8088: if (ScanPathAddGrf(filename)) num++; truelight@8077: truelight@8077: return num; truelight@8077: } peter1138@5228: rubidium@8140: /** rubidium@8140: * Simple sorter for GRFS rubidium@8140: * @param p1 the first GRFConfig * rubidium@8140: * @param p2 the second GRFConfig * rubidium@8140: * @return the same strcmp would return for the name of the NewGRF. rubidium@8140: */ glx@8144: static int CDECL GRFSorter(const void *p1, const void *p2) rubidium@8140: { rubidium@8140: const GRFConfig *c1 = *(const GRFConfig **)p1; rubidium@8140: const GRFConfig *c2 = *(const GRFConfig **)p2; rubidium@8140: rubidium@8140: return strcmp(c1->name != NULL ? c1->name : c1->filename, rubidium@8140: c2->name != NULL ? c2->name : c2->filename); rubidium@8140: } rubidium@8140: peter1138@5228: /* Scan for all NewGRFs */ rubidium@6573: void ScanNewGRFFiles() peter1138@5228: { rubidium@7425: Searchpath sp; rubidium@7425: char path[MAX_PATH]; truelight@8088: TarFileList::iterator tar; rubidium@7425: uint num = 0; peter1138@5228: Darkvater@5347: ClearGRFConfigList(&_all_grfs); peter1138@5228: Darkvater@5568: DEBUG(grf, 1, "Scanning for NewGRFs"); rubidium@7425: FOR_ALL_SEARCHPATHS(sp) { rubidium@7425: FioAppendDirectory(path, MAX_PATH, sp, DATA_DIR); rubidium@7425: num += ScanPath(path, strlen(path)); rubidium@7330: } truelight@8077: FOR_ALL_TARS(tar) { truelight@8077: num += ScanTar(tar); truelight@8077: } rubidium@8140: Darkvater@5568: DEBUG(grf, 1, "Scan complete, found %d files", num); rubidium@8145: if (num == 0 || _all_grfs == NULL) return; rubidium@8140: rubidium@8140: /* Sort the linked list using quicksort. rubidium@8140: * For that we first have to make an array, the qsort and rubidium@8140: * then remake the linked list. */ rubidium@8140: GRFConfig **to_sort = MallocT(num); rubidium@8140: if (to_sort == NULL) return; // No memory, then don't sort rubidium@8140: rubidium@8140: uint i = 0; rubidium@8140: for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) { rubidium@8140: to_sort[i] = p; rubidium@8140: } rubidium@8140: /* Number of files is not necessarily right */ rubidium@8140: num = i; rubidium@8140: rubidium@8140: qsort(to_sort, num, sizeof(GRFConfig*), GRFSorter); rubidium@8140: rubidium@8140: for (i = 1; i < num; i++) { rubidium@8140: to_sort[i - 1]->next = to_sort[i]; rubidium@8140: } rubidium@8140: to_sort[num - 1]->next = NULL; rubidium@8140: _all_grfs = to_sort[0]; peter1138@5228: } peter1138@5228: peter1138@5228: Darkvater@5897: /* Find a NewGRF in the scanned list, if md5sum is NULL, we don't care about it*/ Darkvater@5897: const GRFConfig *FindGRFConfig(uint32 grfid, const uint8 *md5sum) peter1138@5228: { Darkvater@5897: for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) { Darkvater@5897: if (c->grfid == grfid) { Darkvater@5897: 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@5692: #ifdef ENABLE_NETWORK Darkvater@5692: rubidium@5339: /** Structure for UnknownGRFs; this is a lightweight variant of GRFConfig */ rubidium@6016: 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@5860: 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@5692: #endif /* ENABLE_NETWORK */ Darkvater@5692: 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@8378: /** Base GRF ID for OpenTTD's base graphics GRFs. */ rubidium@8378: static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400); rubidium@8378: rubidium@8378: /** rubidium@8378: * Checks whether this GRF is a OpenTTD base graphic GRF. rubidium@8378: * @return true if and only if it is a base GRF. rubidium@8378: */ rubidium@8378: bool GRFConfig::IsOpenTTDBaseGRF() const rubidium@8378: { rubidium@8378: return (this->grfid & 0x00FFFFFF) == OPENTTD_GRAPHICS_BASE_GRF_ID; rubidium@8378: } rubidium@8378: 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@6573: static void Save_NGRF() peter1138@5228: { peter1138@5228: int index = 0; peter1138@5228: Darkvater@6434: for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) { skidd13@8424: 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@6573: static void Load_NGRF() peter1138@5228: { Darkvater@6434: ClearGRFConfigList(&_grfconfig); peter1138@5228: while (SlIterateArray() != -1) { Darkvater@6434: GRFConfig *c = CallocT(1); Darkvater@6434: SlObject(c, _grfconfig_desc); Darkvater@6434: AppendToGRFConfigList(&_grfconfig, c); peter1138@5228: } peter1138@5228: peter1138@5329: /* Append static NewGRF configuration */ rubidium@5581: AppendStaticGRFConfigs(&_grfconfig); peter1138@5228: } peter1138@5228: rubidium@5838: extern const ChunkHandler _newgrf_chunk_handlers[] = { peter1138@5228: { 'NGRF', Save_NGRF, Load_NGRF, CH_ARRAY | CH_LAST } peter1138@5228: }; peter1138@5228: Darkvater@5568: belugas@6674: