2006-12-04 08:30:04 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-03-21 03:06:21 +00:00
|
|
|
/** @file newgfr_config.cpp */
|
|
|
|
|
2006-12-04 08:30:04 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "openttd.h"
|
|
|
|
#include "functions.h"
|
|
|
|
#include "macros.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "variables.h"
|
2006-12-10 11:29:14 +00:00
|
|
|
#include "string.h"
|
2006-12-04 08:30:04 +00:00
|
|
|
#include "saveload.h"
|
|
|
|
#include "md5.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "network/network_data.h"
|
2006-12-04 08:30:04 +00:00
|
|
|
#include "newgrf.h"
|
|
|
|
#include "newgrf_config.h"
|
2007-01-10 18:56:51 +00:00
|
|
|
#include "helpers.hpp"
|
2006-12-04 08:30:04 +00:00
|
|
|
|
|
|
|
#include "fileio.h"
|
|
|
|
#include "fios.h"
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
# include <io.h>
|
|
|
|
#endif /* WIN32 */
|
|
|
|
|
|
|
|
|
|
|
|
GRFConfig *_all_grfs;
|
|
|
|
GRFConfig *_grfconfig;
|
|
|
|
GRFConfig *_grfconfig_newgame;
|
2006-12-12 19:38:41 +00:00
|
|
|
GRFConfig *_grfconfig_static;
|
2006-12-04 08:30:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Calculate the MD5 Sum for a GRF */
|
|
|
|
static bool CalcGRFMD5Sum(GRFConfig *config)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
char filename[MAX_PATH];
|
|
|
|
md5_state_t md5state;
|
|
|
|
md5_byte_t buffer[1024];
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* open the file */
|
2006-12-09 10:56:12 +00:00
|
|
|
snprintf(filename, lengthof(filename), "%s%s", _paths.data_dir, config->filename);
|
2006-12-04 08:30:04 +00:00
|
|
|
f = fopen(filename, "rb");
|
|
|
|
if (f == NULL) return false;
|
|
|
|
|
|
|
|
/* calculate md5sum */
|
|
|
|
md5_init(&md5state);
|
|
|
|
while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0) {
|
|
|
|
md5_append(&md5state, buffer, len);
|
|
|
|
}
|
|
|
|
md5_finish(&md5state, config->md5sum);
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Find the GRFID and calculate the md5sum */
|
2006-12-12 19:38:41 +00:00
|
|
|
bool FillGRFDetails(GRFConfig *config, bool is_static)
|
2006-12-04 08:30:04 +00:00
|
|
|
{
|
|
|
|
if (!FioCheckFileExists(config->filename)) {
|
2007-03-06 19:33:28 +00:00
|
|
|
config->status = GCS_NOT_FOUND;
|
2006-12-04 08:30:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find and load the Action 8 information */
|
|
|
|
/* 62 is the last file slot before sample.cat.
|
|
|
|
* Should perhaps be some "don't care" value */
|
2007-01-03 11:33:54 +00:00
|
|
|
LoadNewGRFFile(config, 62, GLS_FILESCAN);
|
2006-12-04 08:30:04 +00:00
|
|
|
|
|
|
|
/* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
|
|
|
|
if (config->grfid == 0 || config->grfid == 0xFFFFFFFF) return false;
|
|
|
|
|
2007-01-03 11:33:54 +00:00
|
|
|
if (is_static) {
|
|
|
|
/* Perform a 'safety scan' for static GRFs */
|
|
|
|
LoadNewGRFFile(config, 62, GLS_SAFETYSCAN);
|
|
|
|
|
|
|
|
/* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
|
|
|
|
if (HASBIT(config->flags, GCF_UNSAFE)) return false;
|
|
|
|
}
|
|
|
|
|
2006-12-04 08:30:04 +00:00
|
|
|
return CalcGRFMD5Sum(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-20 20:43:52 +00:00
|
|
|
void ClearGRFConfig(GRFConfig **config)
|
2006-12-12 19:38:41 +00:00
|
|
|
{
|
2006-12-18 12:26:55 +00:00
|
|
|
/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
|
2006-12-20 20:43:52 +00:00
|
|
|
if (!HASBIT((*config)->flags, GCF_COPY)) {
|
|
|
|
free((*config)->filename);
|
|
|
|
free((*config)->name);
|
|
|
|
free((*config)->info);
|
2007-02-21 23:18:08 +00:00
|
|
|
free((*config)->error);
|
2006-12-18 12:26:55 +00:00
|
|
|
}
|
2006-12-20 20:43:52 +00:00
|
|
|
free(*config);
|
|
|
|
*config = NULL;
|
2006-12-12 19:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-04 08:30:04 +00:00
|
|
|
/* Clear a GRF Config list */
|
2006-12-20 21:17:33 +00:00
|
|
|
void ClearGRFConfigList(GRFConfig **config)
|
2006-12-04 08:30:04 +00:00
|
|
|
{
|
|
|
|
GRFConfig *c, *next;
|
2006-12-20 21:17:33 +00:00
|
|
|
for (c = *config; c != NULL; c = next) {
|
2006-12-04 08:30:04 +00:00
|
|
|
next = c->next;
|
2006-12-20 20:43:52 +00:00
|
|
|
ClearGRFConfig(&c);
|
2006-12-04 08:30:04 +00:00
|
|
|
}
|
2006-12-20 21:17:33 +00:00
|
|
|
*config = NULL;
|
2006-12-04 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-20 21:17:33 +00:00
|
|
|
/** Copy a GRF Config list
|
|
|
|
* @param dst pointer to destination list
|
2007-02-18 22:37:33 +00:00
|
|
|
* @param src pointer to source list values
|
2006-12-20 21:17:33 +00:00
|
|
|
* @return pointer to the last value added to the destination list */
|
2006-12-20 20:43:52 +00:00
|
|
|
GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src)
|
2006-12-04 08:30:04 +00:00
|
|
|
{
|
2006-12-21 10:09:43 +00:00
|
|
|
/* Clear destination as it will be overwritten */
|
|
|
|
ClearGRFConfigList(dst);
|
2006-12-04 08:30:04 +00:00
|
|
|
for (; src != NULL; src = src->next) {
|
2007-01-11 17:29:39 +00:00
|
|
|
GRFConfig *c = CallocT<GRFConfig>(1);
|
2006-12-04 08:30:04 +00:00
|
|
|
*c = *src;
|
2006-12-23 09:06:37 +00:00
|
|
|
if (src->filename != NULL) c->filename = strdup(src->filename);
|
|
|
|
if (src->name != NULL) c->name = strdup(src->name);
|
|
|
|
if (src->info != NULL) c->info = strdup(src->info);
|
2007-02-24 16:34:37 +00:00
|
|
|
if (src->error != NULL) {
|
|
|
|
c->error = CallocT<GRFError>(1);
|
|
|
|
memcpy(c->error, src->error, sizeof(GRFError));
|
|
|
|
}
|
2006-12-04 08:30:04 +00:00
|
|
|
|
|
|
|
*dst = c;
|
|
|
|
dst = &c->next;
|
|
|
|
}
|
2006-12-12 19:38:41 +00:00
|
|
|
|
|
|
|
return dst;
|
2006-12-04 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
2006-12-27 18:25:17 +00:00
|
|
|
/**
|
|
|
|
* Removes duplicates from lists of GRFConfigs. These duplicates
|
|
|
|
* are introduced when the _grfconfig_static GRFs are appended
|
|
|
|
* to the _grfconfig on a newgame or savegame. As the parameters
|
|
|
|
* of the static GRFs could be different that the parameters of
|
|
|
|
* the ones used non-statically. This can result in desyncs in
|
|
|
|
* multiplayers, so the duplicate static GRFs have to be removed.
|
|
|
|
*
|
|
|
|
* This function _assumes_ that all static GRFs are placed after
|
|
|
|
* the non-static GRFs.
|
|
|
|
*
|
|
|
|
* @param list the list to remove the duplicates from
|
|
|
|
*/
|
|
|
|
static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
|
|
|
|
{
|
|
|
|
GRFConfig *prev;
|
|
|
|
GRFConfig *cur;
|
|
|
|
|
|
|
|
if (list == NULL) return;
|
|
|
|
|
|
|
|
for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) {
|
|
|
|
if (cur->grfid != list->grfid) continue;
|
2007-01-14 22:43:19 +00:00
|
|
|
|
2006-12-27 18:25:17 +00:00
|
|
|
prev->next = cur->next;
|
|
|
|
ClearGRFConfig(&cur);
|
|
|
|
cur = prev; // Just go back one so it continues as normal later on
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoveDuplicatesFromGRFConfigList(list->next);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends the static GRFs to a list of GRFs
|
|
|
|
* @param dst the head of the list to add to
|
|
|
|
*/
|
|
|
|
void AppendStaticGRFConfigs(GRFConfig **dst)
|
|
|
|
{
|
|
|
|
GRFConfig **tail = dst;
|
|
|
|
while (*tail != NULL) tail = &(*tail)->next;
|
|
|
|
|
|
|
|
CopyGRFConfigList(tail, _grfconfig_static);
|
|
|
|
RemoveDuplicatesFromGRFConfigList(*dst);
|
|
|
|
}
|
|
|
|
|
2007-02-18 22:37:33 +00:00
|
|
|
/** Appends an element to a list of GRFs
|
2007-02-22 16:16:44 +00:00
|
|
|
* @param dst the head of the list to add to */
|
|
|
|
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
|
2007-02-18 22:37:33 +00:00
|
|
|
{
|
|
|
|
GRFConfig **tail = dst;
|
|
|
|
while (*tail != NULL) tail = &(*tail)->next;
|
2007-02-22 16:16:44 +00:00
|
|
|
*tail = el;
|
|
|
|
|
2007-02-18 22:37:33 +00:00
|
|
|
RemoveDuplicatesFromGRFConfigList(*dst);
|
|
|
|
}
|
|
|
|
|
2006-12-04 08:30:04 +00:00
|
|
|
|
|
|
|
/* Reset the current GRF Config to either blank or newgame settings */
|
|
|
|
void ResetGRFConfig(bool defaults)
|
|
|
|
{
|
2006-12-12 19:38:41 +00:00
|
|
|
GRFConfig **c = &_grfconfig;
|
|
|
|
|
2006-12-28 16:11:07 +00:00
|
|
|
if (defaults) {
|
|
|
|
c = CopyGRFConfigList(c, _grfconfig_newgame);
|
|
|
|
} else {
|
|
|
|
ClearGRFConfigList(c);
|
|
|
|
}
|
|
|
|
|
2006-12-27 18:25:17 +00:00
|
|
|
AppendStaticGRFConfigs(&_grfconfig);
|
2006-12-04 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-13 17:42:50 +00:00
|
|
|
/** Check if all GRFs in the GRF config from a savegame can be loaded.
|
|
|
|
* @return will return any of the following 3 values:<br>
|
|
|
|
* <ul>
|
2007-03-06 19:33:28 +00:00
|
|
|
* <li> GLC_ALL_GOOD: No problems occured, all GRF files were found and loaded
|
|
|
|
* <li> GLC_COMPATIBLE: For one or more GRF's no exact match was found, but a
|
2007-01-13 17:42:50 +00:00
|
|
|
* compatible GRF with the same grfid was found and used instead
|
2007-03-06 19:33:28 +00:00
|
|
|
* <li> GLC_NOT_FOUND: For one or more GRF's no match was found at all
|
2007-01-13 17:42:50 +00:00
|
|
|
* </ul> */
|
2007-03-07 11:47:46 +00:00
|
|
|
GRFListCompatibility IsGoodGRFConfigList()
|
2006-12-04 08:30:04 +00:00
|
|
|
{
|
2007-03-06 19:33:28 +00:00
|
|
|
GRFListCompatibility res = GLC_ALL_GOOD;
|
2006-12-04 08:30:04 +00:00
|
|
|
|
2007-01-13 17:42:50 +00:00
|
|
|
for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
|
2006-12-04 08:30:04 +00:00
|
|
|
const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
|
|
|
|
if (f == NULL) {
|
2007-01-13 21:28:46 +00:00
|
|
|
char buf[256];
|
2007-01-13 17:42:50 +00:00
|
|
|
|
|
|
|
/* If we have not found the exactly matching GRF try to find one with the
|
|
|
|
* same grfid, as it most likely is compatible */
|
|
|
|
f = FindGRFConfig(c->grfid);
|
|
|
|
if (f != NULL) {
|
|
|
|
md5sumToString(buf, lastof(buf), c->md5sum);
|
|
|
|
DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->grfid), c->filename, buf);
|
|
|
|
SETBIT(c->flags, GCF_COMPATIBLE);
|
|
|
|
|
|
|
|
/* Non-found has precedence over compatibility load */
|
2007-03-06 19:33:28 +00:00
|
|
|
if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
|
2007-01-13 17:42:50 +00:00
|
|
|
goto compatible_grf;
|
|
|
|
}
|
2006-12-04 08:30:04 +00:00
|
|
|
|
2007-01-13 17:42:50 +00:00
|
|
|
/* No compatible grf was found, mark it as disabled */
|
|
|
|
md5sumToString(buf, lastof(buf), c->md5sum);
|
|
|
|
DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->grfid), c->filename, buf);
|
2006-12-04 08:30:04 +00:00
|
|
|
|
2007-03-06 19:33:28 +00:00
|
|
|
c->status = GCS_NOT_FOUND;
|
|
|
|
res = GLC_NOT_FOUND;
|
2006-12-04 08:30:04 +00:00
|
|
|
} else {
|
2007-01-13 17:42:50 +00:00
|
|
|
compatible_grf:
|
|
|
|
DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->grfid), f->filename);
|
2006-12-20 23:44:39 +00:00
|
|
|
/* The filename could be the filename as in the savegame. As we need
|
|
|
|
* to load the GRF here, we need the correct filename, so overwrite that
|
|
|
|
* in any case and set the name and info when it is not set already.
|
|
|
|
* When the GCF_COPY flag is set, it is certain that the filename is
|
|
|
|
* already a local one, so there is no need to replace it. */
|
|
|
|
if (!HASBIT(c->flags, GCF_COPY)) {
|
|
|
|
free(c->filename);
|
|
|
|
c->filename = strdup(f->filename);
|
2007-01-16 21:34:51 +00:00
|
|
|
memcpy(c->md5sum, f->md5sum, sizeof(c->md5sum));
|
2007-03-01 18:28:55 +00:00
|
|
|
if (c->name == NULL && f->name != NULL) c->name = strdup(f->name);
|
|
|
|
if (c->info == NULL && f->info != NULL) c->info = strdup(f->info);
|
2007-02-21 23:18:08 +00:00
|
|
|
c->error = NULL;
|
2006-12-20 23:44:39 +00:00
|
|
|
}
|
2006-12-04 08:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
|
|
|
|
|
|
|
|
/* Scan a path for NewGRFs */
|
|
|
|
static uint ScanPath(const char *path)
|
|
|
|
{
|
|
|
|
uint num = 0;
|
|
|
|
struct stat sb;
|
|
|
|
struct dirent *dirent;
|
|
|
|
DIR *dir;
|
|
|
|
|
2007-02-20 00:09:23 +00:00
|
|
|
if ((dir = ttd_opendir(path)) == NULL) return 0;
|
2006-12-04 08:30:04 +00:00
|
|
|
|
|
|
|
while ((dirent = readdir(dir)) != NULL) {
|
|
|
|
const char *d_name = FS2OTTD(dirent->d_name);
|
|
|
|
char filename[MAX_PATH];
|
|
|
|
|
|
|
|
if (!FiosIsValidFile(path, dirent, &sb)) continue;
|
|
|
|
|
|
|
|
snprintf(filename, lengthof(filename), "%s" PATHSEP "%s", path, d_name);
|
|
|
|
|
|
|
|
if (sb.st_mode & S_IFDIR) {
|
|
|
|
/* Directory */
|
|
|
|
if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
|
|
|
|
num += ScanPath(filename);
|
|
|
|
} else if (sb.st_mode & S_IFREG) {
|
|
|
|
/* File */
|
|
|
|
char *ext = strrchr(filename, '.');
|
2006-12-09 10:56:12 +00:00
|
|
|
char *file = filename + strlen(_paths.data_dir) + 1; // Crop base path
|
2006-12-04 08:30:04 +00:00
|
|
|
|
|
|
|
/* If no extension or extension isn't .grf, skip the file */
|
|
|
|
if (ext == NULL) continue;
|
|
|
|
if (strcasecmp(ext, ".grf") != 0) continue;
|
|
|
|
|
2007-01-11 17:29:39 +00:00
|
|
|
GRFConfig *c = CallocT<GRFConfig>(1);
|
2006-12-04 08:30:04 +00:00
|
|
|
c->filename = strdup(file);
|
|
|
|
|
2006-12-12 19:38:41 +00:00
|
|
|
if (FillGRFDetails(c, false)) {
|
2006-12-04 08:30:04 +00:00
|
|
|
if (_all_grfs == NULL) {
|
|
|
|
_all_grfs = c;
|
|
|
|
} else {
|
|
|
|
/* Insert file into list at a position determined by its
|
|
|
|
* name, so the list is sorted as we go along */
|
|
|
|
GRFConfig **pd, *d;
|
|
|
|
for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
|
|
|
|
if (strcasecmp(c->name, d->name) <= 0) break;
|
|
|
|
}
|
|
|
|
c->next = d;
|
|
|
|
*pd = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
num++;
|
|
|
|
} else {
|
|
|
|
/* File couldn't be opened, or is either not a NewGRF or is a
|
|
|
|
* 'system' NewGRF, so forget about it. */
|
|
|
|
free(c->filename);
|
|
|
|
free(c->name);
|
|
|
|
free(c->info);
|
|
|
|
free(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Scan for all NewGRFs */
|
2007-03-07 11:47:46 +00:00
|
|
|
void ScanNewGRFFiles()
|
2006-12-04 08:30:04 +00:00
|
|
|
{
|
|
|
|
uint num;
|
|
|
|
|
2006-12-20 21:17:33 +00:00
|
|
|
ClearGRFConfigList(&_all_grfs);
|
2006-12-04 08:30:04 +00:00
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(grf, 1, "Scanning for NewGRFs");
|
2006-12-09 10:56:12 +00:00
|
|
|
num = ScanPath(_paths.data_dir);
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(grf, 1, "Scan complete, found %d files", num);
|
2006-12-04 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-13 17:23:02 +00:00
|
|
|
/* Find a NewGRF in the scanned list, if md5sum is NULL, we don't care about it*/
|
|
|
|
const GRFConfig *FindGRFConfig(uint32 grfid, const uint8 *md5sum)
|
2006-12-04 08:30:04 +00:00
|
|
|
{
|
2007-01-13 17:23:02 +00:00
|
|
|
for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
|
2006-12-04 08:30:04 +00:00
|
|
|
if (c->grfid == grfid) {
|
2007-01-13 17:23:02 +00:00
|
|
|
if (md5sum == NULL) return c;
|
|
|
|
|
2006-12-04 08:30:04 +00:00
|
|
|
if (memcmp(md5sum, c->md5sum, sizeof(c->md5sum)) == 0) return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-12-31 11:12:49 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
2006-12-18 12:26:55 +00:00
|
|
|
/** Structure for UnknownGRFs; this is a lightweight variant of GRFConfig */
|
2007-01-21 17:29:38 +00:00
|
|
|
struct UnknownGRF : public GRFIdentifier {
|
2006-12-18 12:26:55 +00:00
|
|
|
UnknownGRF *next;
|
|
|
|
char name[NETWORK_GRF_NAME_LENGTH];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the name of a NewGRF in the list of names for unknown GRFs. An
|
|
|
|
* unknown GRF is a GRF where the .grf is not found during scanning.
|
|
|
|
*
|
|
|
|
* The names are resolved via UDP calls to servers that should know the name,
|
|
|
|
* though the replies may not come. This leaves "<Unknown>" as name, though
|
|
|
|
* that shouldn't matter _very_ much as they need GRF crawler or so to look
|
|
|
|
* up the GRF anyway and that works better with the GRF ID.
|
|
|
|
*
|
|
|
|
* @param grfid the GRF ID part of the 'unique' GRF identifier
|
|
|
|
* @param md5sum the MD5 checksum part of the 'unique' GRF identifier
|
|
|
|
* @param create whether to create a new GRFConfig if the GRFConfig did not
|
|
|
|
* exist in the fake list of GRFConfigs.
|
|
|
|
* @return the GRFConfig with the given GRF ID and MD5 checksum or NULL when
|
|
|
|
* it does not exist and create is false. This value must NEVER be
|
|
|
|
* freed by the caller.
|
|
|
|
*/
|
|
|
|
char *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
|
|
|
|
{
|
|
|
|
UnknownGRF *grf;
|
|
|
|
static UnknownGRF *unknown_grfs = NULL;
|
|
|
|
|
|
|
|
for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
|
|
|
|
if (grf->grfid == grfid) {
|
|
|
|
if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!create) return NULL;
|
|
|
|
|
2007-01-11 17:29:39 +00:00
|
|
|
grf = CallocT<UnknownGRF>(1);
|
2006-12-18 12:26:55 +00:00
|
|
|
grf->grfid = grfid;
|
|
|
|
grf->next = unknown_grfs;
|
|
|
|
ttd_strlcpy(grf->name, UNKNOWN_GRF_NAME_PLACEHOLDER, sizeof(grf->name));
|
|
|
|
memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
|
|
|
|
|
|
|
|
unknown_grfs = grf;
|
|
|
|
return grf->name;
|
|
|
|
}
|
|
|
|
|
2006-12-31 11:12:49 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
|
|
|
|
2006-12-04 08:30:04 +00:00
|
|
|
|
2006-12-04 13:45:20 +00:00
|
|
|
/* Retrieve a NewGRF from the current config by its grfid */
|
2006-12-14 15:14:29 +00:00
|
|
|
GRFConfig *GetGRFConfig(uint32 grfid)
|
2006-12-04 13:45:20 +00:00
|
|
|
{
|
|
|
|
GRFConfig *c;
|
|
|
|
|
|
|
|
for (c = _grfconfig; c != NULL; c = c->next) {
|
|
|
|
if (c->grfid == grfid) return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-10 11:29:14 +00:00
|
|
|
/* Build a space separated list of parameters, and terminate */
|
|
|
|
char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
|
|
|
|
{
|
|
|
|
uint i;
|
|
|
|
|
|
|
|
/* Return an empty string if there are no parameters */
|
|
|
|
if (c->num_params == 0) return strecpy(dst, "", last);
|
|
|
|
|
|
|
|
for (i = 0; i < c->num_params; i++) {
|
|
|
|
if (i > 0) dst = strecpy(dst, " ", last);
|
|
|
|
dst += snprintf(dst, last - dst, "%d", c->param[i]);
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-04 08:30:04 +00:00
|
|
|
static const SaveLoad _grfconfig_desc[] = {
|
|
|
|
SLE_STR(GRFConfig, filename, SLE_STR, 0x40),
|
|
|
|
SLE_VAR(GRFConfig, grfid, SLE_UINT32),
|
|
|
|
SLE_ARR(GRFConfig, md5sum, SLE_UINT8, 16),
|
|
|
|
SLE_ARR(GRFConfig, param, SLE_UINT32, 0x80),
|
|
|
|
SLE_VAR(GRFConfig, num_params, SLE_UINT8),
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void Save_NGRF()
|
2006-12-04 08:30:04 +00:00
|
|
|
{
|
|
|
|
int index = 0;
|
|
|
|
|
2007-02-22 16:16:44 +00:00
|
|
|
for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
|
2006-12-12 19:38:41 +00:00
|
|
|
if (HASBIT(c->flags, GCF_STATIC)) continue;
|
2006-12-04 08:30:04 +00:00
|
|
|
SlSetArrayIndex(index++);
|
|
|
|
SlObject(c, _grfconfig_desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void Load_NGRF()
|
2006-12-04 08:30:04 +00:00
|
|
|
{
|
2007-02-22 16:16:44 +00:00
|
|
|
ClearGRFConfigList(&_grfconfig);
|
2006-12-04 08:30:04 +00:00
|
|
|
while (SlIterateArray() != -1) {
|
2007-02-22 16:16:44 +00:00
|
|
|
GRFConfig *c = CallocT<GRFConfig>(1);
|
|
|
|
SlObject(c, _grfconfig_desc);
|
|
|
|
AppendToGRFConfigList(&_grfconfig, c);
|
2006-12-04 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
2006-12-12 19:38:41 +00:00
|
|
|
/* Append static NewGRF configuration */
|
2006-12-27 18:25:17 +00:00
|
|
|
AppendStaticGRFConfigs(&_grfconfig);
|
2006-12-04 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
extern const ChunkHandler _newgrf_chunk_handlers[] = {
|
2006-12-04 08:30:04 +00:00
|
|
|
{ 'NGRF', Save_NGRF, Load_NGRF, CH_ARRAY | CH_LAST }
|
|
|
|
};
|
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
|
2007-03-21 03:06:21 +00:00
|
|
|
|