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@5339: #include "network_data.h" peter1138@5228: #include "newgrf.h" peter1138@5228: #include "newgrf_config.h" peter1138@5228: peter1138@5228: #include "fileio.h" peter1138@5228: #include "fios.h" peter1138@5228: #include peter1138@5228: #include peter1138@5228: peter1138@5228: #ifdef WIN32 peter1138@5228: # include peter1138@5228: #else peter1138@5228: # include 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@5329: LoadNewGRFFile(config, 62, is_static ? GLS_SAFETYSCAN : GLS_FILESCAN); peter1138@5329: peter1138@5329: /* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */ peter1138@5329: if (HASBIT(config->flags, GCF_UNSAFE)) return false; 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@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); 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@5347: * @param srt 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: { peter1138@5228: GRFConfig *c; peter1138@5228: Darkvater@5351: /* Clear destination as it will be overwritten */ Darkvater@5351: ClearGRFConfigList(dst); peter1138@5228: for (; src != NULL; src = src->next) { peter1138@5228: c = calloc(1, sizeof(*c)); 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); 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; rubidium@5581: assert(HASBIT(cur->flags, GCF_STATIC)); 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: 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: celestar@5592: if (defaults) { celestar@5592: c = CopyGRFConfigList(c, _grfconfig_newgame); celestar@5592: } else { celestar@5592: ClearGRFConfigList(c); celestar@5592: } celestar@5592: rubidium@5581: AppendStaticGRFConfigs(&_grfconfig); peter1138@5228: } peter1138@5228: peter1138@5228: peter1138@5228: /* Check if all GRFs in the GRF Config can be loaded */ peter1138@5228: bool IsGoodGRFConfigList(void) peter1138@5228: { peter1138@5228: bool res = true; peter1138@5228: GRFConfig *c; peter1138@5228: peter1138@5228: for (c = _grfconfig; c != NULL; c = c->next) { peter1138@5228: const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum); peter1138@5228: if (f == NULL) { peter1138@5228: char buf[512], *p = buf; peter1138@5228: uint i; peter1138@5228: peter1138@5228: p += snprintf(p, lastof(buf) - p, "Couldn't find NewGRF %08X (%s) checksum ", BSWAP32(c->grfid), c->filename); peter1138@5228: for (i = 0; i < lengthof(c->md5sum); i++) { peter1138@5228: p += snprintf(p, lastof(buf) - p, "%02X", c->md5sum[i]); peter1138@5228: } peter1138@5228: ShowInfo(buf); peter1138@5228: peter1138@5228: res = false; peter1138@5228: } else { Darkvater@5568: DEBUG(grf, 1, "Loading GRF %08X from '%s'", BSWAP32(c->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); rubidium@5349: if (c->name == NULL) c->name = strdup(f->name); rubidium@5349: if (c->info == NULL) c->info = strdup(f->info); 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: GRFConfig *c; peter1138@5228: peter1138@5228: if ((dir = 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: peter1138@5228: c = calloc(1, sizeof(*c)); 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: peter1138@5228: /* Find a NewGRF in the scanned list */ peter1138@5228: const GRFConfig *FindGRFConfig(uint32 grfid, uint8 *md5sum) peter1138@5228: { peter1138@5228: GRFConfig *c; peter1138@5228: static const uint8 blanksum[sizeof(c->md5sum)] = { 0 }; peter1138@5228: peter1138@5228: for (c = _all_grfs; c != NULL; c = c->next) { peter1138@5228: if (c->grfid == grfid) { peter1138@5228: if (memcmp(blanksum, c->md5sum, sizeof(c->md5sum)) == 0) CalcGRFMD5Sum(c); 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: rubidium@5339: /** Structure for UnknownGRFs; this is a lightweight variant of GRFConfig */ rubidium@5339: typedef struct UnknownGRF UnknownGRF; rubidium@5339: struct UnknownGRF { rubidium@5339: UnknownGRF *next; rubidium@5339: uint32 grfid; rubidium@5339: uint8 md5sum[16]; 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: rubidium@5339: grf = calloc(1, sizeof(*grf)); 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: 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: GRFConfig *c; peter1138@5228: int index = 0; peter1138@5228: peter1138@5228: for (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: { peter1138@5228: GRFConfig *first = NULL; peter1138@5228: GRFConfig **last = &first; peter1138@5228: peter1138@5228: while (SlIterateArray() != -1) { peter1138@5228: GRFConfig *c = calloc(1, sizeof(*c)); peter1138@5228: SlObject(c, _grfconfig_desc); peter1138@5228: peter1138@5228: /* Append our configuration to the list */ peter1138@5228: *last = c; peter1138@5228: last = &c->next; peter1138@5228: } peter1138@5228: peter1138@5329: /* Append static NewGRF configuration */ peter1138@5329: CopyGRFConfigList(last, _grfconfig_static); peter1138@5329: Darkvater@5347: ClearGRFConfigList(&_grfconfig); peter1138@5228: _grfconfig = first; rubidium@5581: AppendStaticGRFConfigs(&_grfconfig); peter1138@5228: } peter1138@5228: peter1138@5228: const ChunkHandler _newgrf_chunk_handlers[] = { peter1138@5228: { 'NGRF', Save_NGRF, Load_NGRF, CH_ARRAY | CH_LAST } peter1138@5228: }; peter1138@5228: Darkvater@5568: