newgrf_config.c
author rubidium
Sat, 23 Dec 2006 09:06:37 +0000
changeset 5366 e469c6d36582
parent 5351 c83b547490f0
child 5380 8ea58542b6e0
permissions -rw-r--r--
(svn r7551) -Fix (r7523): segmentation fault on showing NewGRF settings of a network game.
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     1
/* $Id$ */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     2
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     3
#include "stdafx.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     4
#include "openttd.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     5
#include "functions.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     6
#include "macros.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     7
#include "debug.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     8
#include "variables.h"
5308
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
     9
#include "string.h"
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    10
#include "saveload.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    11
#include "md5.h"
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
    12
#include "network_data.h"
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    13
#include "newgrf.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    14
#include "newgrf_config.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    15
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    16
#include "fileio.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    17
#include "fios.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    18
#include <sys/types.h>
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    19
#include <sys/stat.h>
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    20
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    21
#ifdef WIN32
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    22
# include <io.h>
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    23
#else
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    24
# include <unistd.h>
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    25
# include <dirent.h>
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    26
#endif /* WIN32 */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    27
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    28
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    29
GRFConfig *_all_grfs;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    30
GRFConfig *_grfconfig;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    31
GRFConfig *_grfconfig_newgame;
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
    32
GRFConfig *_grfconfig_static;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    33
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    34
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    35
/* Calculate the MD5 Sum for a GRF */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    36
static bool CalcGRFMD5Sum(GRFConfig *config)
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    37
{
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    38
	FILE *f;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    39
	char filename[MAX_PATH];
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    40
	md5_state_t md5state;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    41
	md5_byte_t buffer[1024];
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    42
	size_t len;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    43
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    44
	/* open the file */
5296
e7acddfdd8a7 (svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents: 5234
diff changeset
    45
	snprintf(filename, lengthof(filename), "%s%s", _paths.data_dir, config->filename);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    46
	f = fopen(filename, "rb");
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    47
	if (f == NULL) return false;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    48
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    49
	/* calculate md5sum */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    50
	md5_init(&md5state);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    51
	while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    52
		md5_append(&md5state, buffer, len);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    53
	}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    54
	md5_finish(&md5state, config->md5sum);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    55
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    56
	fclose(f);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    57
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    58
	return true;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    59
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    60
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    61
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    62
/* Find the GRFID and calculate the md5sum */
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
    63
bool FillGRFDetails(GRFConfig *config, bool is_static)
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    64
{
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    65
	if (!FioCheckFileExists(config->filename)) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    66
		SETBIT(config->flags, GCF_NOT_FOUND);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    67
		return false;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    68
	}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    69
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    70
	/* Find and load the Action 8 information */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    71
	/* 62 is the last file slot before sample.cat.
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    72
	 * Should perhaps be some "don't care" value */
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
    73
	LoadNewGRFFile(config, 62, is_static ? GLS_SAFETYSCAN : GLS_FILESCAN);
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
    74
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
    75
	/* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
    76
	if (HASBIT(config->flags, GCF_UNSAFE)) return false;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    77
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    78
	/* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    79
	if (config->grfid == 0 || config->grfid == 0xFFFFFFFF) return false;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    80
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    81
	return CalcGRFMD5Sum(config);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    82
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    83
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    84
5346
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
    85
void ClearGRFConfig(GRFConfig **config)
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
    86
{
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
    87
	/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
5346
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
    88
	if (!HASBIT((*config)->flags, GCF_COPY)) {
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
    89
		free((*config)->filename);
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
    90
		free((*config)->name);
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
    91
		free((*config)->info);
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
    92
	}
5346
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
    93
	free(*config);
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
    94
	*config = NULL;
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
    95
}
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
    96
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
    97
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    98
/* Clear a GRF Config list */
5347
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
    99
void ClearGRFConfigList(GRFConfig **config)
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   100
{
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   101
	GRFConfig *c, *next;
5347
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   102
	for (c = *config; c != NULL; c = next) {
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   103
		next = c->next;
5346
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
   104
		ClearGRFConfig(&c);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   105
	}
5347
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   106
	*config = NULL;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   107
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   108
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   109
5347
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   110
/** Copy a GRF Config list
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   111
 * @param dst pointer to destination list
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   112
 * @param srt pointer to source list values
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   113
 * @return pointer to the last value added to the destination list */
5346
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
   114
GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src)
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   115
{
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   116
	GRFConfig *c;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   117
5351
c83b547490f0 (svn r7522) -Codechange: Have CopyGRFConfigList clear the destination list before assigning
Darkvater
parents: 5349
diff changeset
   118
	/* Clear destination as it will be overwritten */
c83b547490f0 (svn r7522) -Codechange: Have CopyGRFConfigList clear the destination list before assigning
Darkvater
parents: 5349
diff changeset
   119
	ClearGRFConfigList(dst);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   120
	for (; src != NULL; src = src->next) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   121
		c = calloc(1, sizeof(*c));
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   122
		*c = *src;
5366
e469c6d36582 (svn r7551) -Fix (r7523): segmentation fault on showing NewGRF settings of a network game.
rubidium
parents: 5351
diff changeset
   123
		if (src->filename != NULL) c->filename = strdup(src->filename);
e469c6d36582 (svn r7551) -Fix (r7523): segmentation fault on showing NewGRF settings of a network game.
rubidium
parents: 5351
diff changeset
   124
		if (src->name     != NULL) c->name     = strdup(src->name);
e469c6d36582 (svn r7551) -Fix (r7523): segmentation fault on showing NewGRF settings of a network game.
rubidium
parents: 5351
diff changeset
   125
		if (src->info     != NULL) c->info     = strdup(src->info);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   126
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   127
		*dst = c;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   128
		dst = &c->next;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   129
	}
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   130
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   131
	return dst;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   132
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   133
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   134
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   135
/* Reset the current GRF Config to either blank or newgame settings */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   136
void ResetGRFConfig(bool defaults)
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   137
{
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   138
	GRFConfig **c = &_grfconfig;
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   139
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   140
	if (defaults) c = CopyGRFConfigList(c, _grfconfig_newgame);
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   141
	CopyGRFConfigList(c, _grfconfig_static);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   142
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   143
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   144
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   145
/* Check if all GRFs in the GRF Config can be loaded */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   146
bool IsGoodGRFConfigList(void)
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   147
{
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   148
	bool res = true;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   149
	GRFConfig *c;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   150
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   151
	for (c = _grfconfig; c != NULL; c = c->next) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   152
		const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   153
		if (f == NULL) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   154
			char buf[512], *p = buf;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   155
			uint i;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   156
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   157
			p += snprintf(p, lastof(buf) - p, "Couldn't find NewGRF %08X (%s) checksum ", BSWAP32(c->grfid), c->filename);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   158
			for (i = 0; i < lengthof(c->md5sum); i++) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   159
				p += snprintf(p, lastof(buf) - p, "%02X", c->md5sum[i]);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   160
			}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   161
			ShowInfo(buf);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   162
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   163
			res = false;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   164
		} else {
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   165
			DEBUG(grf, 1) ("[GRF] Loading GRF %08X from %s", BSWAP32(c->grfid), f->filename);
5349
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   166
			/* The filename could be the filename as in the savegame. As we need
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   167
			 * to load the GRF here, we need the correct filename, so overwrite that
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   168
			 * in any case and set the name and info when it is not set already.
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   169
			 * When the GCF_COPY flag is set, it is certain that the filename is
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   170
			 * already a local one, so there is no need to replace it. */
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   171
			if (!HASBIT(c->flags, GCF_COPY)) {
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   172
				free(c->filename);
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   173
				c->filename = strdup(f->filename);
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   174
				if (c->name == NULL) c->name = strdup(f->name);
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   175
				if (c->info == NULL) c->info = strdup(f->info);
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   176
			}
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   177
		}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   178
	}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   179
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   180
	return res;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   181
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   182
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   183
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   184
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   185
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   186
/* Scan a path for NewGRFs */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   187
static uint ScanPath(const char *path)
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   188
{
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   189
	uint num = 0;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   190
	struct stat sb;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   191
	struct dirent *dirent;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   192
	DIR *dir;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   193
	GRFConfig *c;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   194
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   195
	if ((dir = opendir(path)) == NULL) return 0;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   196
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   197
	while ((dirent = readdir(dir)) != NULL) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   198
		const char *d_name = FS2OTTD(dirent->d_name);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   199
		char filename[MAX_PATH];
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   200
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   201
		if (!FiosIsValidFile(path, dirent, &sb)) continue;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   202
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   203
		snprintf(filename, lengthof(filename), "%s" PATHSEP "%s", path, d_name);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   204
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   205
		if (sb.st_mode & S_IFDIR) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   206
			/* Directory */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   207
			if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   208
			num += ScanPath(filename);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   209
		} else if (sb.st_mode & S_IFREG) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   210
			/* File */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   211
			char *ext = strrchr(filename, '.');
5296
e7acddfdd8a7 (svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents: 5234
diff changeset
   212
			char *file = filename + strlen(_paths.data_dir) + 1; // Crop base path
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   213
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   214
			/* If no extension or extension isn't .grf, skip the file */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   215
			if (ext == NULL) continue;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   216
			if (strcasecmp(ext, ".grf") != 0) continue;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   217
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   218
			c = calloc(1, sizeof(*c));
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   219
			c->filename = strdup(file);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   220
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   221
			if (FillGRFDetails(c, false)) {
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   222
				if (_all_grfs == NULL) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   223
					_all_grfs = c;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   224
				} else {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   225
					/* Insert file into list at a position determined by its
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   226
					 * name, so the list is sorted as we go along */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   227
					GRFConfig **pd, *d;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   228
					for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   229
						if (strcasecmp(c->name, d->name) <= 0) break;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   230
					}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   231
					c->next = d;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   232
					*pd = c;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   233
				}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   234
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   235
				num++;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   236
			} else {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   237
				/* File couldn't be opened, or is either not a NewGRF or is a
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   238
				 * 'system' NewGRF, so forget about it. */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   239
				free(c->filename);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   240
				free(c->name);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   241
				free(c->info);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   242
				free(c);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   243
			}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   244
		}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   245
	}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   246
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   247
	closedir(dir);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   248
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   249
	return num;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   250
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   251
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   252
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   253
/* Scan for all NewGRFs */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   254
void ScanNewGRFFiles(void)
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   255
{
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   256
	uint num;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   257
5347
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   258
	ClearGRFConfigList(&_all_grfs);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   259
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   260
	DEBUG(grf, 1) ("[GRF] Scanning for NewGRFs");
5296
e7acddfdd8a7 (svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents: 5234
diff changeset
   261
	num = ScanPath(_paths.data_dir);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   262
	DEBUG(grf, 1) ("[GRF] Scan complete, found %d files", num);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   263
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   264
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   265
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   266
/* Find a NewGRF in the scanned list */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   267
const GRFConfig *FindGRFConfig(uint32 grfid, uint8 *md5sum)
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   268
{
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   269
	GRFConfig *c;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   270
	static const uint8 blanksum[sizeof(c->md5sum)] = { 0 };
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   271
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   272
	for (c = _all_grfs; c != NULL; c = c->next) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   273
		if (c->grfid == grfid) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   274
			if (memcmp(blanksum, c->md5sum, sizeof(c->md5sum)) == 0) CalcGRFMD5Sum(c);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   275
			if (memcmp(md5sum, c->md5sum, sizeof(c->md5sum)) == 0) return c;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   276
		}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   277
	}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   278
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   279
	return NULL;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   280
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   281
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   282
/** Structure for UnknownGRFs; this is a lightweight variant of GRFConfig */
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   283
typedef struct UnknownGRF UnknownGRF;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   284
struct UnknownGRF {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   285
	UnknownGRF *next;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   286
	uint32 grfid;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   287
	uint8  md5sum[16];
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   288
	char   name[NETWORK_GRF_NAME_LENGTH];
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   289
};
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   290
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   291
/**
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   292
 * Finds the name of a NewGRF in the list of names for unknown GRFs. An
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   293
 * unknown GRF is a GRF where the .grf is not found during scanning.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   294
 *
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   295
 * The names are resolved via UDP calls to servers that should know the name,
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   296
 * though the replies may not come. This leaves "<Unknown>" as name, though
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   297
 * that shouldn't matter _very_ much as they need GRF crawler or so to look
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   298
 * up the GRF anyway and that works better with the GRF ID.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   299
 *
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   300
 * @param grfid  the GRF ID part of the 'unique' GRF identifier
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   301
 * @param md5sum the MD5 checksum part of the 'unique' GRF identifier
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   302
 * @param create whether to create a new GRFConfig if the GRFConfig did not
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   303
 *               exist in the fake list of GRFConfigs.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   304
 * @return the GRFConfig with the given GRF ID and MD5 checksum or NULL when
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   305
 *         it does not exist and create is false. This value must NEVER be
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   306
 *         freed by the caller.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   307
 */
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   308
char *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   309
{
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   310
	UnknownGRF *grf;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   311
	static UnknownGRF *unknown_grfs = NULL;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   312
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   313
	for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   314
		if (grf->grfid == grfid) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   315
			if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   316
		}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   317
	}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   318
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   319
	if (!create) return NULL;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   320
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   321
	grf = calloc(1, sizeof(*grf));
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   322
	grf->grfid = grfid;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   323
	grf->next  = unknown_grfs;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   324
	ttd_strlcpy(grf->name, UNKNOWN_GRF_NAME_PLACEHOLDER, sizeof(grf->name));
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   325
	memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   326
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   327
	unknown_grfs = grf;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   328
	return grf->name;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   329
}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   330
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   331
5234
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   332
/* Retrieve a NewGRF from the current config by its grfid */
5333
dedf1f6de6d3 (svn r7495) -Fix (r7354): [NewGRF] Deactivate the target GRF, not the current GRF.
peter1138
parents: 5329
diff changeset
   333
GRFConfig *GetGRFConfig(uint32 grfid)
5234
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   334
{
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   335
	GRFConfig *c;
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   336
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   337
	for (c = _grfconfig; c != NULL; c = c->next) {
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   338
		if (c->grfid == grfid) return c;
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   339
	}
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   340
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   341
	return NULL;
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   342
}
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   343
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   344
5308
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   345
/* Build a space separated list of parameters, and terminate */
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   346
char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   347
{
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   348
	uint i;
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   349
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   350
	/* Return an empty string if there are no parameters */
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   351
	if (c->num_params == 0) return strecpy(dst, "", last);
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   352
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   353
	for (i = 0; i < c->num_params; i++) {
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   354
		if (i > 0) dst = strecpy(dst, " ", last);
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   355
		dst += snprintf(dst, last - dst, "%d", c->param[i]);
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   356
	}
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   357
	return dst;
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   358
}
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   359
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   360
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   361
static const SaveLoad _grfconfig_desc[] = {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   362
	SLE_STR(GRFConfig, filename,   SLE_STR, 0x40),
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   363
	SLE_VAR(GRFConfig, grfid,      SLE_UINT32),
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   364
	SLE_ARR(GRFConfig, md5sum,     SLE_UINT8, 16),
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   365
	SLE_ARR(GRFConfig, param,      SLE_UINT32, 0x80),
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   366
	SLE_VAR(GRFConfig, num_params, SLE_UINT8),
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   367
	SLE_END()
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   368
};
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   369
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   370
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   371
static void Save_NGRF(void)
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   372
{
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   373
	GRFConfig *c;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   374
	int index = 0;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   375
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   376
	for (c = _grfconfig; c != NULL; c = c->next) {
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   377
		if (HASBIT(c->flags, GCF_STATIC)) continue;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   378
		SlSetArrayIndex(index++);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   379
		SlObject(c, _grfconfig_desc);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   380
	}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   381
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   382
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   383
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   384
static void Load_NGRF(void)
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   385
{
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   386
	GRFConfig *first = NULL;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   387
	GRFConfig **last = &first;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   388
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   389
	while (SlIterateArray() != -1) {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   390
		GRFConfig *c = calloc(1, sizeof(*c));
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   391
		SlObject(c, _grfconfig_desc);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   392
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   393
		/* Append our configuration to the list */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   394
		*last = c;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   395
		last = &c->next;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   396
	}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   397
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   398
	/* Append static NewGRF configuration */
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   399
	CopyGRFConfigList(last, _grfconfig_static);
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   400
5347
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   401
	ClearGRFConfigList(&_grfconfig);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   402
	_grfconfig = first;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   403
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   404
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   405
const ChunkHandler _newgrf_chunk_handlers[] = {
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   406
	{ 'NGRF', Save_NGRF, Load_NGRF, CH_ARRAY | CH_LAST }
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   407
};
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   408