peter1138@5228: /* $Id$ */ peter1138@5228: peter1138@5228: #include "stdafx.h" peter1138@5228: #include "openttd.h" peter1138@5228: #include "functions.h" peter1138@5228: #include "macros.h" peter1138@5228: #include "debug.h" peter1138@5228: #include "variables.h" peter1138@5308: #include "string.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@5838: #include "helpers.hpp" 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; peter1138@5228: char filename[MAX_PATH]; peter1138@5228: md5_state_t md5state; peter1138@5228: md5_byte_t buffer[1024]; peter1138@5228: size_t len; peter1138@5228: peter1138@5228: /* open the file */ Darkvater@5296: snprintf(filename, lengthof(filename), "%s%s", _paths.data_dir, config->filename); peter1138@5228: f = fopen(filename, "rb"); peter1138@5228: if (f == NULL) return false; peter1138@5228: peter1138@5228: /* calculate md5sum */ peter1138@5228: md5_init(&md5state); peter1138@5228: while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0) { peter1138@5228: md5_append(&md5state, buffer, len); peter1138@5228: } peter1138@5228: md5_finish(&md5state, config->md5sum); peter1138@5228: peter1138@5228: fclose(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: { peter1138@5228: if (!FioCheckFileExists(config->filename)) { peter1138@5228: SETBIT(config->flags, GCF_NOT_FOUND); peter1138@5228: return false; peter1138@5228: } peter1138@5228: peter1138@5228: /* Find and load the Action 8 information */ peter1138@5228: /* 62 is the last file slot before sample.cat. peter1138@5228: * Should perhaps be some "don't care" value */ peter1138@5753: LoadNewGRFFile(config, 62, GLS_FILESCAN); peter1138@5228: peter1138@5228: /* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */ peter1138@5228: if (config->grfid == 0 || config->grfid == 0xFFFFFFFF) 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 */ peter1138@5753: 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 */ Darkvater@5346: if (!HASBIT((*config)->flags, GCF_COPY)) { Darkvater@5346: free((*config)->filename); Darkvater@5346: free((*config)->name); Darkvater@5346: free((*config)->info); maedhros@6429: free((*config)->error); 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 Darkvater@5347: * @return pointer to the last value added to the destination list */ Darkvater@5346: GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src) 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@5554: if (src->filename != NULL) c->filename = strdup(src->filename); rubidium@5554: if (src->name != NULL) c->name = strdup(src->name); rubidium@5554: if (src->info != NULL) c->info = strdup(src->info); maedhros@6465: if (src->error != NULL) { maedhros@6465: c->error = CallocT(1); maedhros@6465: memcpy(c->error, src->error, sizeof(GRFError)); maedhros@6465: } peter1138@5228: 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: rubidium@5581: CopyGRFConfigList(tail, _grfconfig_static); rubidium@5581: RemoveDuplicatesFromGRFConfigList(*dst); rubidium@5581: } rubidium@5581: Darkvater@6399: /** Appends an element to a list of GRFs Darkvater@6434: * @param dst the head of the list to add to */ 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: { peter1138@5329: GRFConfig **c = &_grfconfig; peter1138@5329: peter1138@5652: if (defaults) { peter1138@5652: c = CopyGRFConfigList(c, _grfconfig_newgame); peter1138@5652: } else { peter1138@5652: ClearGRFConfigList(c); peter1138@5652: } peter1138@5652: 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: *
    Darkvater@5898: *
  • GCF_ACTIVATED: No problems occured, all GRF files were found and loaded Darkvater@5898: *
  • GCF_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 Darkvater@5898: *
  • GCF_NOT_FOUND: For one or more GRF's no match was found at all Darkvater@5898: *
*/ Darkvater@5898: GCF_Flags IsGoodGRFConfigList(void) peter1138@5228: { Darkvater@5898: GCF_Flags res = GCF_ACTIVATED; 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); Darkvater@5898: SETBIT(c->flags, GCF_COMPATIBLE); peter1138@5228: Darkvater@5898: /* Non-found has precedence over compatibility load */ Darkvater@5898: if (res != GCF_NOT_FOUND) res = GCF_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: Darkvater@5898: SETBIT(c->flags, GCF_NOT_FOUND); Darkvater@5898: res = GCF_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. */ rubidium@5349: 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: peter1138@5228: peter1138@5228: extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb); peter1138@5228: peter1138@5228: /* Scan a path for NewGRFs */ peter1138@5228: static uint ScanPath(const char *path) peter1138@5228: { peter1138@5228: uint num = 0; peter1138@5228: struct stat sb; peter1138@5228: struct dirent *dirent; peter1138@5228: DIR *dir; peter1138@5228: Darkvater@6412: if ((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: peter1138@5228: snprintf(filename, lengthof(filename), "%s" PATHSEP "%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; peter1138@5228: num += ScanPath(filename); peter1138@5228: } else if (sb.st_mode & S_IFREG) { peter1138@5228: /* File */ peter1138@5228: char *ext = strrchr(filename, '.'); Darkvater@5296: char *file = filename + strlen(_paths.data_dir) + 1; // Crop base path 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: KUDr@5860: GRFConfig *c = CallocT(1); peter1138@5228: c->filename = strdup(file); peter1138@5228: peter1138@5329: if (FillGRFDetails(c, false)) { peter1138@5228: if (_all_grfs == NULL) { peter1138@5228: _all_grfs = c; peter1138@5228: } else { peter1138@5228: /* Insert file into list at a position determined by its peter1138@5228: * name, so the list is sorted as we go along */ peter1138@5228: GRFConfig **pd, *d; peter1138@5228: for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) { peter1138@5228: if (strcasecmp(c->name, d->name) <= 0) break; peter1138@5228: } peter1138@5228: c->next = d; peter1138@5228: *pd = c; peter1138@5228: } peter1138@5228: peter1138@5228: num++; peter1138@5228: } else { peter1138@5228: /* File couldn't be opened, or is either not a NewGRF or is a peter1138@5228: * 'system' NewGRF, so forget about it. */ peter1138@5228: free(c->filename); peter1138@5228: free(c->name); peter1138@5228: free(c->info); peter1138@5228: free(c); peter1138@5228: } peter1138@5228: } peter1138@5228: } peter1138@5228: peter1138@5228: closedir(dir); peter1138@5228: peter1138@5228: return num; peter1138@5228: } peter1138@5228: peter1138@5228: peter1138@5228: /* Scan for all NewGRFs */ peter1138@5228: void ScanNewGRFFiles(void) peter1138@5228: { peter1138@5228: uint num; peter1138@5228: Darkvater@5347: ClearGRFConfigList(&_all_grfs); peter1138@5228: Darkvater@5568: DEBUG(grf, 1, "Scanning for NewGRFs"); Darkvater@5296: num = ScanPath(_paths.data_dir); Darkvater@5568: DEBUG(grf, 1, "Scan complete, found %d files", num); 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@5339: typedef struct UnknownGRF UnknownGRF; 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: 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: peter1138@5228: static void Save_NGRF(void) peter1138@5228: { peter1138@5228: int index = 0; peter1138@5228: Darkvater@6434: for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) { peter1138@5329: 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: peter1138@5228: static void Load_NGRF(void) 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: