src/newgrf_config.cpp
author celestar
Tue, 19 Jun 2007 07:21:01 +0000
branchgamebalance
changeset 9913 e79cd19772dd
parent 9912 1ac8aac92385
permissions -rw-r--r--
(svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     1
/* $Id$ */
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     2
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
     3
/** @file newgrf_config.cpp */
9906
6f41b8713b65 (svn r9674) [gamebalance] -Sync: r9322:9420 from trunk
celestar
parents: 9895
diff changeset
     4
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     5
#include "stdafx.h"
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     6
#include "openttd.h"
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     7
#include "functions.h"
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     8
#include "macros.h"
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     9
#include "debug.h"
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    10
#include "variables.h"
5308
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
    11
#include "string.h"
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    12
#include "saveload.h"
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    13
#include "md5.h"
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5692
diff changeset
    14
#include "network/network_data.h"
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    15
#include "newgrf.h"
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    16
#include "newgrf_config.h"
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    17
#include "helpers.hpp"
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    18
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    19
#include "fileio.h"
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    20
#include "fios.h"
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    21
#include <sys/stat.h>
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    22
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    23
#ifdef WIN32
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    24
# include <io.h>
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    25
#endif /* WIN32 */
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    26
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    27
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    28
GRFConfig *_all_grfs;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    29
GRFConfig *_grfconfig;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    30
GRFConfig *_grfconfig_newgame;
5329
2b117d8652f0 (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
    31
GRFConfig *_grfconfig_static;
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    32
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    33
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    34
/* Calculate the MD5 Sum for a GRF */
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    35
static bool CalcGRFMD5Sum(GRFConfig *config)
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    36
{
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    37
	FILE *f;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    38
	md5_state_t md5state;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    39
	md5_byte_t buffer[1024];
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    40
	size_t len;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    41
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    42
	/* open the file */
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9909
diff changeset
    43
	f = FioFOpenFile(config->full_path);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    44
	if (f == NULL) return false;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    45
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    46
	/* calculate md5sum */
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    47
	md5_init(&md5state);
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    48
	while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0) {
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    49
		md5_append(&md5state, buffer, len);
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    50
	}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    51
	md5_finish(&md5state, config->md5sum);
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    52
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    53
	fclose(f);
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    54
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    55
	return true;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    56
}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    57
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    58
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    59
/* Find the GRFID and calculate the md5sum */
5329
2b117d8652f0 (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
    60
bool FillGRFDetails(GRFConfig *config, bool is_static)
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    61
{
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9909
diff changeset
    62
	if (!FioCheckFileExists(config->full_path)) {
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6509
diff changeset
    63
		config->status = GCS_NOT_FOUND;
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    64
		return false;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    65
	}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    66
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
    67
	if (config->filename == NULL) {
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9909
diff changeset
    68
		const char *t = strrchr(config->full_path, PATHSEPCHAR);
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9909
diff changeset
    69
		config->filename = strdup(t != NULL ? t + 1 : config->full_path);
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
    70
	}
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
    71
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    72
	/* Find and load the Action 8 information */
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    73
	/* 62 is the last file slot before sample.cat.
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    74
	 * Should perhaps be some "don't care" value */
5753
788f8d55eb54 (svn r7795) -Codechange: [NewGRF] When safety checking, allow an Action E that force activates the GRF. Unfortunately this requires knowing the GRF ID in advance (before the Action 8) so the static GRFs are now scanned twice on start up, once for the GRF ID, and then for the safety check. (This fix allows unifont.grf to be used.)
peter1138
parents: 5726
diff changeset
    75
	LoadNewGRFFile(config, 62, GLS_FILESCAN);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    76
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    77
	/* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    78
	if (config->grfid == 0 || config->grfid == 0xFFFFFFFF) return false;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    79
5753
788f8d55eb54 (svn r7795) -Codechange: [NewGRF] When safety checking, allow an Action E that force activates the GRF. Unfortunately this requires knowing the GRF ID in advance (before the Action 8) so the static GRFs are now scanned twice on start up, once for the GRF ID, and then for the safety check. (This fix allows unifont.grf to be used.)
peter1138
parents: 5726
diff changeset
    80
	if (is_static) {
788f8d55eb54 (svn r7795) -Codechange: [NewGRF] When safety checking, allow an Action E that force activates the GRF. Unfortunately this requires knowing the GRF ID in advance (before the Action 8) so the static GRFs are now scanned twice on start up, once for the GRF ID, and then for the safety check. (This fix allows unifont.grf to be used.)
peter1138
parents: 5726
diff changeset
    81
		/* Perform a 'safety scan' for static GRFs */
788f8d55eb54 (svn r7795) -Codechange: [NewGRF] When safety checking, allow an Action E that force activates the GRF. Unfortunately this requires knowing the GRF ID in advance (before the Action 8) so the static GRFs are now scanned twice on start up, once for the GRF ID, and then for the safety check. (This fix allows unifont.grf to be used.)
peter1138
parents: 5726
diff changeset
    82
		LoadNewGRFFile(config, 62, GLS_SAFETYSCAN);
788f8d55eb54 (svn r7795) -Codechange: [NewGRF] When safety checking, allow an Action E that force activates the GRF. Unfortunately this requires knowing the GRF ID in advance (before the Action 8) so the static GRFs are now scanned twice on start up, once for the GRF ID, and then for the safety check. (This fix allows unifont.grf to be used.)
peter1138
parents: 5726
diff changeset
    83
788f8d55eb54 (svn r7795) -Codechange: [NewGRF] When safety checking, allow an Action E that force activates the GRF. Unfortunately this requires knowing the GRF ID in advance (before the Action 8) so the static GRFs are now scanned twice on start up, once for the GRF ID, and then for the safety check. (This fix allows unifont.grf to be used.)
peter1138
parents: 5726
diff changeset
    84
		/* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
788f8d55eb54 (svn r7795) -Codechange: [NewGRF] When safety checking, allow an Action E that force activates the GRF. Unfortunately this requires knowing the GRF ID in advance (before the Action 8) so the static GRFs are now scanned twice on start up, once for the GRF ID, and then for the safety check. (This fix allows unifont.grf to be used.)
peter1138
parents: 5726
diff changeset
    85
		if (HASBIT(config->flags, GCF_UNSAFE)) return false;
788f8d55eb54 (svn r7795) -Codechange: [NewGRF] When safety checking, allow an Action E that force activates the GRF. Unfortunately this requires knowing the GRF ID in advance (before the Action 8) so the static GRFs are now scanned twice on start up, once for the GRF ID, and then for the safety check. (This fix allows unifont.grf to be used.)
peter1138
parents: 5726
diff changeset
    86
	}
788f8d55eb54 (svn r7795) -Codechange: [NewGRF] When safety checking, allow an Action E that force activates the GRF. Unfortunately this requires knowing the GRF ID in advance (before the Action 8) so the static GRFs are now scanned twice on start up, once for the GRF ID, and then for the safety check. (This fix allows unifont.grf to be used.)
peter1138
parents: 5726
diff changeset
    87
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    88
	return CalcGRFMD5Sum(config);
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    89
}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    90
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    91
5346
0d7cc9cefd2b (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
    92
void ClearGRFConfig(GRFConfig **config)
5329
2b117d8652f0 (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
    93
{
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
    94
	/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
5346
0d7cc9cefd2b (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
    95
	if (!HASBIT((*config)->flags, GCF_COPY)) {
0d7cc9cefd2b (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
    96
		free((*config)->filename);
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
    97
		free((*config)->full_path);
5346
0d7cc9cefd2b (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
    98
		free((*config)->name);
0d7cc9cefd2b (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
    99
		free((*config)->info);
9913
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   100
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   101
		if ((*config)->error != NULL) {
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   102
			free((*config)->error->custom_message);
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   103
			free((*config)->error->data);
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   104
			free((*config)->error);
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   105
		}
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   106
	}
5346
0d7cc9cefd2b (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
   107
	free(*config);
0d7cc9cefd2b (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
   108
	*config = NULL;
5329
2b117d8652f0 (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
   109
}
2b117d8652f0 (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
   110
2b117d8652f0 (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
   111
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   112
/* Clear a GRF Config list */
5347
18836f961808 (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   113
void ClearGRFConfigList(GRFConfig **config)
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   114
{
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   115
	GRFConfig *c, *next;
5347
18836f961808 (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   116
	for (c = *config; c != NULL; c = next) {
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   117
		next = c->next;
5346
0d7cc9cefd2b (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
   118
		ClearGRFConfig(&c);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   119
	}
5347
18836f961808 (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   120
	*config = NULL;
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   121
}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   122
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   123
5347
18836f961808 (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   124
/** Copy a GRF Config list
18836f961808 (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   125
 * @param dst pointer to destination list
6399
d704bf2ec4ee (svn r8806) -Codechange (r7582): Remove a duplicate append of static GRF's when loading the game and hide the intrinsics of adding elements to the GRFConfig list by using an AppendToGRFConfigList function.
Darkvater
parents: 6016
diff changeset
   126
 * @param src pointer to source list values
5347
18836f961808 (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   127
 * @return pointer to the last value added to the destination list */
5346
0d7cc9cefd2b (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
   128
GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src)
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   129
{
5351
0980b66406af (svn r7522) -Codechange: Have CopyGRFConfigList clear the destination list before assigning
Darkvater
parents: 5349
diff changeset
   130
	/* Clear destination as it will be overwritten */
0980b66406af (svn r7522) -Codechange: Have CopyGRFConfigList clear the destination list before assigning
Darkvater
parents: 5349
diff changeset
   131
	ClearGRFConfigList(dst);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   132
	for (; src != NULL; src = src->next) {
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5838
diff changeset
   133
		GRFConfig *c = CallocT<GRFConfig>(1);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   134
		*c = *src;
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   135
		if (src->filename  != NULL) c->filename  = strdup(src->filename);
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   136
		if (src->full_path != NULL) c->full_path = strdup(src->full_path);
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   137
		if (src->name      != NULL) c->name      = strdup(src->name);
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   138
		if (src->info      != NULL) c->info      = strdup(src->info);
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   139
		if (src->error     != NULL) {
6465
dddb39b41ee0 (svn r8881) -Feature: Allow built-in newgrf error messages to be translated, and load custom error messages properly.
maedhros
parents: 6434
diff changeset
   140
			c->error = CallocT<GRFError>(1);
dddb39b41ee0 (svn r8881) -Feature: Allow built-in newgrf error messages to be translated, and load custom error messages properly.
maedhros
parents: 6434
diff changeset
   141
			memcpy(c->error, src->error, sizeof(GRFError));
9913
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   142
			if (src->error->data != NULL) c->error->data = strdup(src->error->data);
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   143
			if (src->error->custom_message != NULL) c->error->custom_message = strdup(src->error->custom_message);
6465
dddb39b41ee0 (svn r8881) -Feature: Allow built-in newgrf error messages to be translated, and load custom error messages properly.
maedhros
parents: 6434
diff changeset
   144
		}
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   145
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   146
		*dst = c;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   147
		dst = &c->next;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   148
	}
5329
2b117d8652f0 (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
   149
2b117d8652f0 (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
   150
	return dst;
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   151
}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   152
5581
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   153
/**
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   154
 * Removes duplicates from lists of GRFConfigs. These duplicates
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   155
 * are introduced when the _grfconfig_static GRFs are appended
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   156
 * to the _grfconfig on a newgame or savegame. As the parameters
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   157
 * of the static GRFs could be different that the parameters of
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   158
 * the ones used non-statically. This can result in desyncs in
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   159
 * multiplayers, so the duplicate static GRFs have to be removed.
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   160
 *
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   161
 * This function _assumes_ that all static GRFs are placed after
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   162
 * the non-static GRFs.
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   163
 *
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   164
 * @param list the list to remove the duplicates from
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   165
 */
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   166
static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   167
{
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   168
	GRFConfig *prev;
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   169
	GRFConfig *cur;
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   170
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   171
	if (list == NULL) return;
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   172
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   173
	for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) {
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   174
		if (cur->grfid != list->grfid) continue;
5926
97834f38dab2 (svn r8135) -Fix (r7582): Don't assert on duplicate non-static grfs when removing duplicates as the PEBKAC effect can result in the user adding the same grf's in the config file.
Darkvater
parents: 5903
diff changeset
   175
5581
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   176
		prev->next = cur->next;
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   177
		ClearGRFConfig(&cur);
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   178
		cur = prev; // Just go back one so it continues as normal later on
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   179
	}
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   180
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   181
	RemoveDuplicatesFromGRFConfigList(list->next);
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   182
}
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   183
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   184
/**
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   185
 * Appends the static GRFs to a list of GRFs
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   186
 * @param dst the head of the list to add to
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   187
 */
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   188
void AppendStaticGRFConfigs(GRFConfig **dst)
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   189
{
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   190
	GRFConfig **tail = dst;
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   191
	while (*tail != NULL) tail = &(*tail)->next;
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   192
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   193
	CopyGRFConfigList(tail, _grfconfig_static);
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   194
	RemoveDuplicatesFromGRFConfigList(*dst);
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   195
}
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   196
6399
d704bf2ec4ee (svn r8806) -Codechange (r7582): Remove a duplicate append of static GRF's when loading the game and hide the intrinsics of adding elements to the GRFConfig list by using an AppendToGRFConfigList function.
Darkvater
parents: 6016
diff changeset
   197
/** Appends an element to a list of GRFs
9909
dce9a6923bb7 (svn r10133) [gamebalance] -Sync: r9620:9670 from trunk
celestar
parents: 9908
diff changeset
   198
 * @param dst the head of the list to add to
dce9a6923bb7 (svn r10133) [gamebalance] -Sync: r9620:9670 from trunk
celestar
parents: 9908
diff changeset
   199
 * @param el the new tail to be */
6434
d12a3ab5acbc (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6429
diff changeset
   200
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
6399
d704bf2ec4ee (svn r8806) -Codechange (r7582): Remove a duplicate append of static GRF's when loading the game and hide the intrinsics of adding elements to the GRFConfig list by using an AppendToGRFConfigList function.
Darkvater
parents: 6016
diff changeset
   201
{
d704bf2ec4ee (svn r8806) -Codechange (r7582): Remove a duplicate append of static GRF's when loading the game and hide the intrinsics of adding elements to the GRFConfig list by using an AppendToGRFConfigList function.
Darkvater
parents: 6016
diff changeset
   202
	GRFConfig **tail = dst;
d704bf2ec4ee (svn r8806) -Codechange (r7582): Remove a duplicate append of static GRF's when loading the game and hide the intrinsics of adding elements to the GRFConfig list by using an AppendToGRFConfigList function.
Darkvater
parents: 6016
diff changeset
   203
	while (*tail != NULL) tail = &(*tail)->next;
6434
d12a3ab5acbc (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6429
diff changeset
   204
	*tail = el;
d12a3ab5acbc (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6429
diff changeset
   205
6399
d704bf2ec4ee (svn r8806) -Codechange (r7582): Remove a duplicate append of static GRF's when loading the game and hide the intrinsics of adding elements to the GRFConfig list by using an AppendToGRFConfigList function.
Darkvater
parents: 6016
diff changeset
   206
	RemoveDuplicatesFromGRFConfigList(*dst);
d704bf2ec4ee (svn r8806) -Codechange (r7582): Remove a duplicate append of static GRF's when loading the game and hide the intrinsics of adding elements to the GRFConfig list by using an AppendToGRFConfigList function.
Darkvater
parents: 6016
diff changeset
   207
}
d704bf2ec4ee (svn r8806) -Codechange (r7582): Remove a duplicate append of static GRF's when loading the game and hide the intrinsics of adding elements to the GRFConfig list by using an AppendToGRFConfigList function.
Darkvater
parents: 6016
diff changeset
   208
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   209
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   210
/* Reset the current GRF Config to either blank or newgame settings */
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   211
void ResetGRFConfig(bool defaults)
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   212
{
5329
2b117d8652f0 (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
   213
	GRFConfig **c = &_grfconfig;
2b117d8652f0 (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
   214
5652
bc39ff3dc48d (svn r7594) -Fix (r7522): GRF config list wasn't cleared when no GRFs should be used.
peter1138
parents: 5581
diff changeset
   215
	if (defaults) {
bc39ff3dc48d (svn r7594) -Fix (r7522): GRF config list wasn't cleared when no GRFs should be used.
peter1138
parents: 5581
diff changeset
   216
		c = CopyGRFConfigList(c, _grfconfig_newgame);
bc39ff3dc48d (svn r7594) -Fix (r7522): GRF config list wasn't cleared when no GRFs should be used.
peter1138
parents: 5581
diff changeset
   217
	} else {
bc39ff3dc48d (svn r7594) -Fix (r7522): GRF config list wasn't cleared when no GRFs should be used.
peter1138
parents: 5581
diff changeset
   218
		ClearGRFConfigList(c);
bc39ff3dc48d (svn r7594) -Fix (r7522): GRF config list wasn't cleared when no GRFs should be used.
peter1138
parents: 5581
diff changeset
   219
	}
bc39ff3dc48d (svn r7594) -Fix (r7522): GRF config list wasn't cleared when no GRFs should be used.
peter1138
parents: 5581
diff changeset
   220
5581
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   221
	AppendStaticGRFConfigs(&_grfconfig);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   222
}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   223
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   224
5898
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   225
/** Check if all GRFs in the GRF config from a savegame can be loaded.
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   226
 * @return will return any of the following 3 values:<br>
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   227
 * <ul>
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6509
diff changeset
   228
 * <li> GLC_ALL_GOOD: No problems occured, all GRF files were found and loaded
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6509
diff changeset
   229
 * <li> GLC_COMPATIBLE: For one or more GRF's no exact match was found, but a
5898
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   230
 *     compatible GRF with the same grfid was found and used instead
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6509
diff changeset
   231
 * <li> GLC_NOT_FOUND: For one or more GRF's no match was found at all
5898
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   232
 * </ul> */
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6509
diff changeset
   233
GRFListCompatibility IsGoodGRFConfigList()
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   234
{
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6509
diff changeset
   235
	GRFListCompatibility res = GLC_ALL_GOOD;
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   236
5898
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   237
	for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   238
		const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   239
		if (f == NULL) {
5903
cd2d0ef3a3b9 (svn r8111) -Fix r8106: silence warnings
glx
parents: 5899
diff changeset
   240
			char buf[256];
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   241
5898
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   242
			/* If we have not found the exactly matching GRF try to find one with the
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   243
			 * same grfid, as it most likely is compatible */
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   244
			f = FindGRFConfig(c->grfid);
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   245
			if (f != NULL) {
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   246
				md5sumToString(buf, lastof(buf), c->md5sum);
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   247
				DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->grfid), c->filename, buf);
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   248
				SETBIT(c->flags, GCF_COMPATIBLE);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   249
5898
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   250
				/* Non-found has precedence over compatibility load */
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6509
diff changeset
   251
				if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
5898
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   252
				goto compatible_grf;
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   253
			}
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   254
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   255
			/* No compatible grf was found, mark it as disabled */
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   256
			md5sumToString(buf, lastof(buf), c->md5sum);
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   257
			DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->grfid), c->filename, buf);
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   258
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6509
diff changeset
   259
			c->status = GCS_NOT_FOUND;
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6509
diff changeset
   260
			res = GLC_NOT_FOUND;
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   261
		} else {
5898
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   262
compatible_grf:
2ee7d23291fa (svn r8106) -Feature/Fix: Add the ability to load savegames when you don't have the exact GRF files in your list. GRF files that are found based on GRFID (but not on matching md5sum) are used instead of disabling them. This does not affect MP games, there you still need an exact match.
Darkvater
parents: 5897
diff changeset
   263
			DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->grfid), f->filename);
5349
6a220d58c0ce (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   264
			/* The filename could be the filename as in the savegame. As we need
6a220d58c0ce (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   265
			 * to load the GRF here, we need the correct filename, so overwrite that
6a220d58c0ce (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   266
			 * in any case and set the name and info when it is not set already.
6a220d58c0ce (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   267
			 * When the GCF_COPY flag is set, it is certain that the filename is
6a220d58c0ce (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   268
			 * already a local one, so there is no need to replace it. */
6a220d58c0ce (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   269
			if (!HASBIT(c->flags, GCF_COPY)) {
6a220d58c0ce (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   270
				free(c->filename);
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   271
				free(c->full_path);
5349
6a220d58c0ce (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   272
				c->filename = strdup(f->filename);
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   273
				c->full_path = strdup(f->full_path);
5952
701521003c56 (svn r8165) -Revert r8107 and show the MD5SUM of the *REPLACEMENT* grf file, NOT the original one. Reason for this is that this md5sum is saved, and otherwise a wrong md5sum would be stored in a savegame.
Darkvater
parents: 5926
diff changeset
   274
				memcpy(c->md5sum, f->md5sum, sizeof(c->md5sum));
6509
5978ce669c8e (svn r8955) -Fix: crash on loading savegames with GRFs that do not have their GRF info/name set.
rubidium
parents: 6465
diff changeset
   275
				if (c->name == NULL && f->name != NULL) c->name = strdup(f->name);
5978ce669c8e (svn r8955) -Fix: crash on loading savegames with GRFs that do not have their GRF info/name set.
rubidium
parents: 6465
diff changeset
   276
				if (c->info == NULL && f->info != NULL) c->info = strdup(f->info);
6429
02c062eb2c74 (svn r8838) -Feature: Show newgrf error messages loaded in Action B in the newgrf gui
maedhros
parents: 6414
diff changeset
   277
				c->error = NULL;
5349
6a220d58c0ce (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   278
			}
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   279
		}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   280
	}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   281
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   282
	return res;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   283
}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   284
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   285
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   286
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   287
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   288
/* Scan a path for NewGRFs */
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9909
diff changeset
   289
static uint ScanPath(const char *path, int basepath_length)
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   290
{
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   291
	uint num = 0;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   292
	struct stat sb;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   293
	struct dirent *dirent;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   294
	DIR *dir;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   295
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   296
	if (path == NULL || (dir = ttd_opendir(path)) == NULL) return 0;
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   297
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   298
	while ((dirent = readdir(dir)) != NULL) {
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   299
		const char *d_name = FS2OTTD(dirent->d_name);
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   300
		char filename[MAX_PATH];
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   301
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   302
		if (!FiosIsValidFile(path, dirent, &sb)) continue;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   303
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   304
		snprintf(filename, lengthof(filename), "%s%s", path, d_name);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   305
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   306
		if (sb.st_mode & S_IFDIR) {
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   307
			/* Directory */
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   308
			if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   309
			AppendPathSeparator(filename, lengthof(filename));
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9909
diff changeset
   310
			num += ScanPath(filename, basepath_length);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   311
		} else if (sb.st_mode & S_IFREG) {
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   312
			/* File */
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   313
			char *ext = strrchr(filename, '.');
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   314
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   315
			/* If no extension or extension isn't .grf, skip the file */
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   316
			if (ext == NULL) continue;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   317
			if (strcasecmp(ext, ".grf") != 0) continue;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   318
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5838
diff changeset
   319
			GRFConfig *c = CallocT<GRFConfig>(1);
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9909
diff changeset
   320
			c->full_path = strdup(filename + basepath_length);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   321
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   322
			bool added = true;
5329
2b117d8652f0 (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
   323
			if (FillGRFDetails(c, false)) {
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   324
				if (_all_grfs == NULL) {
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   325
					_all_grfs = c;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   326
				} else {
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   327
					/* Insert file into list at a position determined by its
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   328
					 * name, so the list is sorted as we go along */
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   329
					GRFConfig **pd, *d;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   330
					for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   331
						if (c->grfid == d->grfid && memcmp(c->md5sum, d->md5sum, sizeof(c->md5sum)) == 0) added = false;
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   332
						if (strcasecmp(c->name, d->name) <= 0) break;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   333
					}
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   334
					if (added) {
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   335
						c->next = d;
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   336
						*pd = c;
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   337
					}
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   338
				}
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   339
			} else {
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   340
				added = false;
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   341
			}
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   342
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   343
			if (!added) {
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   344
				/* File couldn't be opened, or is either not a NewGRF or is a
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   345
				 * 'system' NewGRF or it's already known, so forget about it. */
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   346
				free(c->filename);
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   347
				free(c->full_path);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   348
				free(c->name);
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   349
				free(c->info);
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   350
				free(c);
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   351
			} else {
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9906
diff changeset
   352
				num++;
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   353
			}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   354
		}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   355
	}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   356
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   357
	closedir(dir);
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   358
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   359
	return num;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   360
}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   361
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   362
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   363
/* Scan for all NewGRFs */
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6509
diff changeset
   364
void ScanNewGRFFiles()
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   365
{
9913
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   366
	Searchpath sp;
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   367
	char path[MAX_PATH];
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   368
	uint num = 0;
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   369
5347
18836f961808 (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   370
	ClearGRFConfigList(&_all_grfs);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   371
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5554
diff changeset
   372
	DEBUG(grf, 1, "Scanning for NewGRFs");
9913
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   373
	FOR_ALL_SEARCHPATHS(sp) {
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   374
		FioAppendDirectory(path, MAX_PATH, sp, DATA_DIR);
e79cd19772dd (svn r10213) [gamebalance] -Sync: r10100:10200 from trunk
celestar
parents: 9912
diff changeset
   375
		num += ScanPath(path, strlen(path));
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9909
diff changeset
   376
	}
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5554
diff changeset
   377
	DEBUG(grf, 1, "Scan complete, found %d files", num);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   378
}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   379
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   380
5897
016df903f3bb (svn r8105) -Codechange: Change FindGRFConfig so that if md5sum parameter is omitted (or NULL)
Darkvater
parents: 5885
diff changeset
   381
/* Find a NewGRF in the scanned list, if md5sum is NULL, we don't care about it*/
016df903f3bb (svn r8105) -Codechange: Change FindGRFConfig so that if md5sum parameter is omitted (or NULL)
Darkvater
parents: 5885
diff changeset
   382
const GRFConfig *FindGRFConfig(uint32 grfid, const uint8 *md5sum)
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   383
{
5897
016df903f3bb (svn r8105) -Codechange: Change FindGRFConfig so that if md5sum parameter is omitted (or NULL)
Darkvater
parents: 5885
diff changeset
   384
	for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
016df903f3bb (svn r8105) -Codechange: Change FindGRFConfig so that if md5sum parameter is omitted (or NULL)
Darkvater
parents: 5885
diff changeset
   385
		if (c->grfid == grfid) {
016df903f3bb (svn r8105) -Codechange: Change FindGRFConfig so that if md5sum parameter is omitted (or NULL)
Darkvater
parents: 5885
diff changeset
   386
			if (md5sum == NULL) return c;
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   387
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   388
			if (memcmp(md5sum, c->md5sum, sizeof(c->md5sum)) == 0) return c;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   389
		}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   390
	}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   391
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   392
	return NULL;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   393
}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   394
5692
b83442e14fd4 (svn r7692) -Fix: OpenTTD didn't compile without network enabled (newgrf sync code)
Darkvater
parents: 5652
diff changeset
   395
#ifdef ENABLE_NETWORK
b83442e14fd4 (svn r7692) -Fix: OpenTTD didn't compile without network enabled (newgrf sync code)
Darkvater
parents: 5652
diff changeset
   396
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   397
/** Structure for UnknownGRFs; this is a lightweight variant of GRFConfig */
6016
9d7b851ffe1c (svn r8316) -Codechange: move the GRF ID and MD5 checksum from GRFConfig to GRFIdentifier so it can be reused.
rubidium
parents: 5952
diff changeset
   398
struct UnknownGRF : public GRFIdentifier {
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   399
	UnknownGRF *next;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   400
	char   name[NETWORK_GRF_NAME_LENGTH];
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   401
};
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   402
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   403
/**
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   404
 * Finds the name of a NewGRF in the list of names for unknown GRFs. An
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   405
 * unknown GRF is a GRF where the .grf is not found during scanning.
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   406
 *
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   407
 * The names are resolved via UDP calls to servers that should know the name,
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   408
 * though the replies may not come. This leaves "<Unknown>" as name, though
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   409
 * that shouldn't matter _very_ much as they need GRF crawler or so to look
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   410
 * up the GRF anyway and that works better with the GRF ID.
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   411
 *
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   412
 * @param grfid  the GRF ID part of the 'unique' GRF identifier
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   413
 * @param md5sum the MD5 checksum part of the 'unique' GRF identifier
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   414
 * @param create whether to create a new GRFConfig if the GRFConfig did not
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   415
 *               exist in the fake list of GRFConfigs.
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   416
 * @return the GRFConfig with the given GRF ID and MD5 checksum or NULL when
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   417
 *         it does not exist and create is false. This value must NEVER be
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   418
 *         freed by the caller.
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   419
 */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   420
char *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   421
{
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   422
	UnknownGRF *grf;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   423
	static UnknownGRF *unknown_grfs = NULL;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   424
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   425
	for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   426
		if (grf->grfid == grfid) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   427
			if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   428
		}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   429
	}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   430
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   431
	if (!create) return NULL;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   432
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5838
diff changeset
   433
	grf = CallocT<UnknownGRF>(1);
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   434
	grf->grfid = grfid;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   435
	grf->next  = unknown_grfs;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   436
	ttd_strlcpy(grf->name, UNKNOWN_GRF_NAME_PLACEHOLDER, sizeof(grf->name));
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   437
	memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   438
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   439
	unknown_grfs = grf;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   440
	return grf->name;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   441
}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   442
5692
b83442e14fd4 (svn r7692) -Fix: OpenTTD didn't compile without network enabled (newgrf sync code)
Darkvater
parents: 5652
diff changeset
   443
#endif /* ENABLE_NETWORK */
b83442e14fd4 (svn r7692) -Fix: OpenTTD didn't compile without network enabled (newgrf sync code)
Darkvater
parents: 5652
diff changeset
   444
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   445
5234
91b7def5e4b2 (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
   446
/* Retrieve a NewGRF from the current config by its grfid */
5333
8e7d68b06399 (svn r7495) -Fix (r7354): [NewGRF] Deactivate the target GRF, not the current GRF.
peter1138
parents: 5329
diff changeset
   447
GRFConfig *GetGRFConfig(uint32 grfid)
5234
91b7def5e4b2 (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
   448
{
91b7def5e4b2 (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
   449
	GRFConfig *c;
91b7def5e4b2 (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
   450
91b7def5e4b2 (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
   451
	for (c = _grfconfig; c != NULL; c = c->next) {
91b7def5e4b2 (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
   452
		if (c->grfid == grfid) return c;
91b7def5e4b2 (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
   453
	}
91b7def5e4b2 (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
   454
91b7def5e4b2 (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
   455
	return NULL;
91b7def5e4b2 (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
   456
}
91b7def5e4b2 (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
   457
91b7def5e4b2 (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
   458
5308
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   459
/* Build a space separated list of parameters, and terminate */
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   460
char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   461
{
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   462
	uint i;
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   463
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   464
	/* Return an empty string if there are no parameters */
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   465
	if (c->num_params == 0) return strecpy(dst, "", last);
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   466
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   467
	for (i = 0; i < c->num_params; i++) {
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   468
		if (i > 0) dst = strecpy(dst, " ", last);
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   469
		dst += snprintf(dst, last - dst, "%d", c->param[i]);
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   470
	}
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   471
	return dst;
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   472
}
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   473
bad31e174cc6 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   474
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   475
static const SaveLoad _grfconfig_desc[] = {
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   476
	SLE_STR(GRFConfig, filename,   SLE_STR, 0x40),
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   477
	SLE_VAR(GRFConfig, grfid,      SLE_UINT32),
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   478
	SLE_ARR(GRFConfig, md5sum,     SLE_UINT8, 16),
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   479
	SLE_ARR(GRFConfig, param,      SLE_UINT32, 0x80),
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   480
	SLE_VAR(GRFConfig, num_params, SLE_UINT8),
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   481
	SLE_END()
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   482
};
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   483
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   484
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6509
diff changeset
   485
static void Save_NGRF()
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   486
{
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   487
	int index = 0;
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   488
6434
d12a3ab5acbc (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6429
diff changeset
   489
	for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
5329
2b117d8652f0 (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
   490
		if (HASBIT(c->flags, GCF_STATIC)) continue;
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   491
		SlSetArrayIndex(index++);
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   492
		SlObject(c, _grfconfig_desc);
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   493
	}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   494
}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   495
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   496
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6509
diff changeset
   497
static void Load_NGRF()
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   498
{
6434
d12a3ab5acbc (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6429
diff changeset
   499
	ClearGRFConfigList(&_grfconfig);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   500
	while (SlIterateArray() != -1) {
6434
d12a3ab5acbc (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6429
diff changeset
   501
		GRFConfig *c = CallocT<GRFConfig>(1);
d12a3ab5acbc (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6429
diff changeset
   502
		SlObject(c, _grfconfig_desc);
d12a3ab5acbc (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6429
diff changeset
   503
		AppendToGRFConfigList(&_grfconfig, c);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   504
	}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   505
5329
2b117d8652f0 (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
   506
	/* Append static NewGRF configuration */
5581
d0407b8e20ce (svn r7582) -Fix (r7490): appending static GRFs to the list of to-be loaded GRF for a game could result in duplicate GRFs in that list, which can cause a segmentation fault while loading the GRFs.
rubidium
parents: 5568
diff changeset
   507
	AppendStaticGRFConfigs(&_grfconfig);
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   508
}
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   509
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   510
extern const ChunkHandler _newgrf_chunk_handlers[] = {
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   511
	{ 'NGRF', Save_NGRF, Load_NGRF, CH_ARRAY | CH_LAST }
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   512
};
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   513
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5554
diff changeset
   514
9906
6f41b8713b65 (svn r9674) [gamebalance] -Sync: r9322:9420 from trunk
celestar
parents: 9895
diff changeset
   515