src/newgrf_config.cpp
author rubidium
Wed, 17 Dec 2008 23:08:11 +0000
changeset 10434 3659467c844c
parent 10310 ca2eb5811a07
permissions -rw-r--r--
(svn r14687) -Change: log all configure errors to config.log
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     1
/* $Id$ */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8592
diff changeset
     3
/** @file newgrf_config.cpp Finding NewGRFs and configuring them. */
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6248
diff changeset
     4
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     5
#include "stdafx.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     6
#include "openttd.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     7
#include "debug.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     8
#include "variables.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
     9
#include "saveload.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    10
#include "md5.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    11
#include "newgrf.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    12
#include "newgrf_config.h"
8213
7bdd7593eb9b (svn r11776) -Codechange: more header splittings to reduce the dependencies.
rubidium
parents: 8133
diff changeset
    13
#include "core/alloc_func.hpp"
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents: 8213
diff changeset
    14
#include "string_func.h"
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9428
diff changeset
    15
#include "gamelog.h"
9428
1ba05b499957 (svn r13343) -Codechange: reorder/move variable/functions in the network headers so that nothing from the network directory needs to include basically all network headers.
rubidium
parents: 9146
diff changeset
    16
#include "network/network_type.h"
10066
f18e97752eeb (svn r14233) -Feature/Fix [FS#2172]: save the palette of the loaded NewGRFs in the savegame, so joining with a server using Windows palette will make a client with the DOS palette do palette conversion and (thus) not cause a desync due to the different palettes disabling different NewGRFs.
rubidium
parents: 10039
diff changeset
    17
#include "gfx_func.h"
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    18
10039
1f236afd6cd1 (svn r14199) -Codechange: split fileio.h into fileio_type.h and fileio_func.h so not everything that includes saveload.h needs to include everything else too.
rubidium
parents: 10036
diff changeset
    19
#include "fileio_func.h"
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    20
#include "fios.h"
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    21
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    22
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    23
GRFConfig *_all_grfs;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    24
GRFConfig *_grfconfig;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    25
GRFConfig *_grfconfig_newgame;
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
    26
GRFConfig *_grfconfig_static;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    27
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    28
10077
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    29
/**
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    30
 * Update the palettes of the graphics from the config file.
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    31
 * This is needed because the config file gets read and parsed
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    32
 * before the palette is chosen (one can configure the base
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    33
 * graphics set governing the palette in the config after all).
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    34
 * As a result of this we update the settings from the config
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    35
 * once we have determined the palette.
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    36
 */
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    37
void UpdateNewGRFConfigPalette()
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    38
{
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    39
	for (GRFConfig *c = _grfconfig_newgame; c != NULL; c = c->next) c->windows_paletted = (_use_palette == PAL_WINDOWS);
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    40
	for (GRFConfig *c = _grfconfig_static;  c != NULL; c = c->next) c->windows_paletted = (_use_palette == PAL_WINDOWS);
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    41
}
450d92b66f98 (svn r14245) -Fix: NewGRF configs loaded from the config file would always default to the DOS palette instead of the "default" palette.
rubidium
parents: 10066
diff changeset
    42
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    43
/* Calculate the MD5 Sum for a GRF */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    44
static bool CalcGRFMD5Sum(GRFConfig *config)
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    45
{
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    46
	FILE *f;
8133
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8131
diff changeset
    47
	Md5 checksum;
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8131
diff changeset
    48
	uint8 buffer[1024];
7574
40e7c5575a2a (svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight
parents: 7573
diff changeset
    49
	size_t len, size;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    50
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    51
	/* open the file */
7574
40e7c5575a2a (svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight
parents: 7573
diff changeset
    52
	f = FioFOpenFile(config->filename, "rb", DATA_DIR, &size);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    53
	if (f == NULL) return false;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    54
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    55
	/* calculate md5sum */
7574
40e7c5575a2a (svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight
parents: 7573
diff changeset
    56
	while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
40e7c5575a2a (svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight
parents: 7573
diff changeset
    57
		size -= len;
8133
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8131
diff changeset
    58
		checksum.Append(buffer, len);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    59
	}
8133
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8131
diff changeset
    60
	checksum.Finish(config->md5sum);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    61
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7586
diff changeset
    62
	FioFCloseFile(f);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    63
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    64
	return true;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    65
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    66
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    67
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    68
/* Find the GRFID and calculate the md5sum */
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
    69
bool FillGRFDetails(GRFConfig *config, bool is_static)
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    70
{
7034
e2d80a01d9c1 (svn r10298) -Fix [FS#903]: show the subdirectory below the default data directory in this filename in the newgrf list. The directory was removed in r9560 because then it used to full path instead of the path relative to the data directory, but since the inclusion of "search paths" that is not necessary anymore.
rubidium
parents: 6967
diff changeset
    71
	if (!FioCheckFileExists(config->filename)) {
6229
695400602ab0 (svn r9031) -Codechange: Introduce grfconfig->status, and use it for states that are
maedhros
parents: 6183
diff changeset
    72
		config->status = GCS_NOT_FOUND;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    73
		return false;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    74
	}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    75
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    76
	/* Find and load the Action 8 information */
7805
7ab20f94cc80 (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 7649
diff changeset
    77
	LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    78
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    79
	/* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
7882
308cab08d2f3 (svn r11433) -Fix: starting OpenTTD with DOS files made it look weird out of the box.
rubidium
parents: 7805
diff changeset
    80
	if (config->grfid == 0 || config->grfid == 0xFFFFFFFF || config->IsOpenTTDBaseGRF()) return false;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    81
5502
003b36599ff8 (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: 5475
diff changeset
    82
	if (is_static) {
003b36599ff8 (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: 5475
diff changeset
    83
		/* Perform a 'safety scan' for static GRFs */
003b36599ff8 (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: 5475
diff changeset
    84
		LoadNewGRFFile(config, 62, GLS_SAFETYSCAN);
003b36599ff8 (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: 5475
diff changeset
    85
003b36599ff8 (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: 5475
diff changeset
    86
		/* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7882
diff changeset
    87
		if (HasBit(config->flags, GCF_UNSAFE)) return false;
5502
003b36599ff8 (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: 5475
diff changeset
    88
	}
003b36599ff8 (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: 5475
diff changeset
    89
10066
f18e97752eeb (svn r14233) -Feature/Fix [FS#2172]: save the palette of the loaded NewGRFs in the savegame, so joining with a server using Windows palette will make a client with the DOS palette do palette conversion and (thus) not cause a desync due to the different palettes disabling different NewGRFs.
rubidium
parents: 10039
diff changeset
    90
	config->windows_paletted = (_use_palette == PAL_WINDOWS);
f18e97752eeb (svn r14233) -Feature/Fix [FS#2172]: save the palette of the loaded NewGRFs in the savegame, so joining with a server using Windows palette will make a client with the DOS palette do palette conversion and (thus) not cause a desync due to the different palettes disabling different NewGRFs.
rubidium
parents: 10039
diff changeset
    91
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    92
	return CalcGRFMD5Sum(config);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    93
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    94
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
    95
5346
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
    96
void ClearGRFConfig(GRFConfig **config)
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
    97
{
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
    98
	/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7882
diff changeset
    99
	if (!HasBit((*config)->flags, GCF_COPY)) {
5346
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
   100
		free((*config)->filename);
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
   101
		free((*config)->name);
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
   102
		free((*config)->info);
6873
b4d0648edd29 (svn r10114) -Fix: Only load newgrf error messages if the language matches the current
maedhros
parents: 6834
diff changeset
   103
b4d0648edd29 (svn r10114) -Fix: Only load newgrf error messages if the language matches the current
maedhros
parents: 6834
diff changeset
   104
		if ((*config)->error != NULL) {
b4d0648edd29 (svn r10114) -Fix: Only load newgrf error messages if the language matches the current
maedhros
parents: 6834
diff changeset
   105
			free((*config)->error->custom_message);
b4d0648edd29 (svn r10114) -Fix: Only load newgrf error messages if the language matches the current
maedhros
parents: 6834
diff changeset
   106
			free((*config)->error->data);
b4d0648edd29 (svn r10114) -Fix: Only load newgrf error messages if the language matches the current
maedhros
parents: 6834
diff changeset
   107
			free((*config)->error);
b4d0648edd29 (svn r10114) -Fix: Only load newgrf error messages if the language matches the current
maedhros
parents: 6834
diff changeset
   108
		}
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   109
	}
5346
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
   110
	free(*config);
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
   111
	*config = NULL;
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   112
}
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   113
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   114
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   115
/* Clear a GRF Config list */
5347
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   116
void ClearGRFConfigList(GRFConfig **config)
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   117
{
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   118
	GRFConfig *c, *next;
5347
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   119
	for (c = *config; c != NULL; c = next) {
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   120
		next = c->next;
5346
3687c73bc65b (svn r7517) -Codechange: Set pointers to NULL when freeing the newgrf config variables and add the proper const to CopyGRFConfigList
Darkvater
parents: 5339
diff changeset
   121
		ClearGRFConfig(&c);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   122
	}
5347
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   123
	*config = NULL;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   124
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   125
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   126
5347
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   127
/** Copy a GRF Config list
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   128
 * @param dst pointer to destination list
6073
0fb2bb966428 (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: 5765
diff changeset
   129
 * @param src pointer to source list values
6956
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6929
diff changeset
   130
 * @param init_only the copied GRF will be processed up to GLS_INIT
5347
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   131
 * @return pointer to the last value added to the destination list */
6956
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6929
diff changeset
   132
GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   133
{
5351
c83b547490f0 (svn r7522) -Codechange: Have CopyGRFConfigList clear the destination list before assigning
Darkvater
parents: 5349
diff changeset
   134
	/* Clear destination as it will be overwritten */
c83b547490f0 (svn r7522) -Codechange: Have CopyGRFConfigList clear the destination list before assigning
Darkvater
parents: 5349
diff changeset
   135
	ClearGRFConfigList(dst);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   136
	for (; src != NULL; src = src->next) {
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
   137
		GRFConfig *c = CallocT<GRFConfig>(1);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   138
		*c = *src;
6424
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6409
diff changeset
   139
		if (src->filename  != NULL) c->filename  = strdup(src->filename);
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6409
diff changeset
   140
		if (src->name      != NULL) c->name      = strdup(src->name);
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6409
diff changeset
   141
		if (src->info      != NULL) c->info      = strdup(src->info);
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6409
diff changeset
   142
		if (src->error     != NULL) {
6139
cd42df0c1f0f (svn r8881) -Feature: Allow built-in newgrf error messages to be translated, and load custom error messages properly.
maedhros
parents: 6108
diff changeset
   143
			c->error = CallocT<GRFError>(1);
cd42df0c1f0f (svn r8881) -Feature: Allow built-in newgrf error messages to be translated, and load custom error messages properly.
maedhros
parents: 6108
diff changeset
   144
			memcpy(c->error, src->error, sizeof(GRFError));
6873
b4d0648edd29 (svn r10114) -Fix: Only load newgrf error messages if the language matches the current
maedhros
parents: 6834
diff changeset
   145
			if (src->error->data != NULL) c->error->data = strdup(src->error->data);
b4d0648edd29 (svn r10114) -Fix: Only load newgrf error messages if the language matches the current
maedhros
parents: 6834
diff changeset
   146
			if (src->error->custom_message != NULL) c->error->custom_message = strdup(src->error->custom_message);
6139
cd42df0c1f0f (svn r8881) -Feature: Allow built-in newgrf error messages to be translated, and load custom error messages properly.
maedhros
parents: 6108
diff changeset
   147
		}
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   148
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   149
		ClrBit(c->flags, GCF_INIT_ONLY);
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
   150
		if (init_only) SetBit(c->flags, GCF_INIT_ONLY);
6956
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6929
diff changeset
   151
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   152
		*dst = c;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   153
		dst = &c->next;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   154
	}
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   155
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   156
	return dst;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   157
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   158
5393
cf584337ea2c (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: 5380
diff changeset
   159
/**
cf584337ea2c (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: 5380
diff changeset
   160
 * Removes duplicates from lists of GRFConfigs. These duplicates
cf584337ea2c (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: 5380
diff changeset
   161
 * are introduced when the _grfconfig_static GRFs are appended
cf584337ea2c (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: 5380
diff changeset
   162
 * to the _grfconfig on a newgame or savegame. As the parameters
cf584337ea2c (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: 5380
diff changeset
   163
 * of the static GRFs could be different that the parameters of
cf584337ea2c (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: 5380
diff changeset
   164
 * the ones used non-statically. This can result in desyncs in
cf584337ea2c (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: 5380
diff changeset
   165
 * multiplayers, so the duplicate static GRFs have to be removed.
cf584337ea2c (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: 5380
diff changeset
   166
 *
cf584337ea2c (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: 5380
diff changeset
   167
 * This function _assumes_ that all static GRFs are placed after
cf584337ea2c (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: 5380
diff changeset
   168
 * the non-static GRFs.
cf584337ea2c (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: 5380
diff changeset
   169
 *
cf584337ea2c (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: 5380
diff changeset
   170
 * @param list the list to remove the duplicates from
cf584337ea2c (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: 5380
diff changeset
   171
 */
cf584337ea2c (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: 5380
diff changeset
   172
static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
cf584337ea2c (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: 5380
diff changeset
   173
{
cf584337ea2c (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: 5380
diff changeset
   174
	GRFConfig *prev;
cf584337ea2c (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: 5380
diff changeset
   175
	GRFConfig *cur;
cf584337ea2c (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: 5380
diff changeset
   176
cf584337ea2c (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: 5380
diff changeset
   177
	if (list == NULL) return;
cf584337ea2c (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: 5380
diff changeset
   178
cf584337ea2c (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: 5380
diff changeset
   179
	for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) {
cf584337ea2c (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: 5380
diff changeset
   180
		if (cur->grfid != list->grfid) continue;
5675
bbc57cf5cf18 (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: 5652
diff changeset
   181
5393
cf584337ea2c (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: 5380
diff changeset
   182
		prev->next = cur->next;
cf584337ea2c (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: 5380
diff changeset
   183
		ClearGRFConfig(&cur);
cf584337ea2c (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: 5380
diff changeset
   184
		cur = prev; // Just go back one so it continues as normal later on
cf584337ea2c (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: 5380
diff changeset
   185
	}
cf584337ea2c (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: 5380
diff changeset
   186
cf584337ea2c (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: 5380
diff changeset
   187
	RemoveDuplicatesFromGRFConfigList(list->next);
cf584337ea2c (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: 5380
diff changeset
   188
}
cf584337ea2c (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: 5380
diff changeset
   189
cf584337ea2c (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: 5380
diff changeset
   190
/**
cf584337ea2c (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: 5380
diff changeset
   191
 * Appends the static GRFs to a list of GRFs
cf584337ea2c (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: 5380
diff changeset
   192
 * @param dst the head of the list to add to
cf584337ea2c (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: 5380
diff changeset
   193
 */
cf584337ea2c (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: 5380
diff changeset
   194
void AppendStaticGRFConfigs(GRFConfig **dst)
cf584337ea2c (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: 5380
diff changeset
   195
{
cf584337ea2c (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: 5380
diff changeset
   196
	GRFConfig **tail = dst;
cf584337ea2c (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: 5380
diff changeset
   197
	while (*tail != NULL) tail = &(*tail)->next;
cf584337ea2c (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: 5380
diff changeset
   198
6956
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6929
diff changeset
   199
	CopyGRFConfigList(tail, _grfconfig_static, false);
5393
cf584337ea2c (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: 5380
diff changeset
   200
	RemoveDuplicatesFromGRFConfigList(*dst);
cf584337ea2c (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: 5380
diff changeset
   201
}
cf584337ea2c (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: 5380
diff changeset
   202
6073
0fb2bb966428 (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: 5765
diff changeset
   203
/** Appends an element to a list of GRFs
6481
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6425
diff changeset
   204
 * @param dst the head of the list to add to
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6425
diff changeset
   205
 * @param el the new tail to be */
6108
c0929232db31 (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6103
diff changeset
   206
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
6073
0fb2bb966428 (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: 5765
diff changeset
   207
{
0fb2bb966428 (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: 5765
diff changeset
   208
	GRFConfig **tail = dst;
0fb2bb966428 (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: 5765
diff changeset
   209
	while (*tail != NULL) tail = &(*tail)->next;
6108
c0929232db31 (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6103
diff changeset
   210
	*tail = el;
c0929232db31 (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6103
diff changeset
   211
6073
0fb2bb966428 (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: 5765
diff changeset
   212
	RemoveDuplicatesFromGRFConfigList(*dst);
0fb2bb966428 (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: 5765
diff changeset
   213
}
0fb2bb966428 (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: 5765
diff changeset
   214
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   215
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   216
/* Reset the current GRF Config to either blank or newgame settings */
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   217
void ResetGRFConfig(bool defaults)
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   218
{
6956
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6929
diff changeset
   219
	CopyGRFConfigList(&_grfconfig, _grfconfig_newgame, !defaults);
5393
cf584337ea2c (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: 5380
diff changeset
   220
	AppendStaticGRFConfigs(&_grfconfig);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   221
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   222
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   223
5647
9a96d72c23be (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: 5646
diff changeset
   224
/** Check if all GRFs in the GRF config from a savegame can be loaded.
9a96d72c23be (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: 5646
diff changeset
   225
 * @return will return any of the following 3 values:<br>
9a96d72c23be (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: 5646
diff changeset
   226
 * <ul>
6229
695400602ab0 (svn r9031) -Codechange: Introduce grfconfig->status, and use it for states that are
maedhros
parents: 6183
diff changeset
   227
 * <li> GLC_ALL_GOOD: No problems occured, all GRF files were found and loaded
695400602ab0 (svn r9031) -Codechange: Introduce grfconfig->status, and use it for states that are
maedhros
parents: 6183
diff changeset
   228
 * <li> GLC_COMPATIBLE: For one or more GRF's no exact match was found, but a
5647
9a96d72c23be (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: 5646
diff changeset
   229
 *     compatible GRF with the same grfid was found and used instead
6229
695400602ab0 (svn r9031) -Codechange: Introduce grfconfig->status, and use it for states that are
maedhros
parents: 6183
diff changeset
   230
 * <li> GLC_NOT_FOUND: For one or more GRF's no match was found at all
5647
9a96d72c23be (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: 5646
diff changeset
   231
 * </ul> */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6229
diff changeset
   232
GRFListCompatibility IsGoodGRFConfigList()
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   233
{
6229
695400602ab0 (svn r9031) -Codechange: Introduce grfconfig->status, and use it for states that are
maedhros
parents: 6183
diff changeset
   234
	GRFListCompatibility res = GLC_ALL_GOOD;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   235
5647
9a96d72c23be (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: 5646
diff changeset
   236
	for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   237
		const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   238
		if (f == NULL) {
5652
c26cb37e4ef4 (svn r8111) -Fix r8106: silence warnings
glx
parents: 5648
diff changeset
   239
			char buf[256];
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   240
5647
9a96d72c23be (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: 5646
diff changeset
   241
			/* If we have not found the exactly matching GRF try to find one with the
9a96d72c23be (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: 5646
diff changeset
   242
			 * same grfid, as it most likely is compatible */
9a96d72c23be (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: 5646
diff changeset
   243
			f = FindGRFConfig(c->grfid);
9a96d72c23be (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: 5646
diff changeset
   244
			if (f != NULL) {
9a96d72c23be (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: 5646
diff changeset
   245
				md5sumToString(buf, lastof(buf), c->md5sum);
9a96d72c23be (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: 5646
diff changeset
   246
				DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->grfid), c->filename, buf);
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
   247
				SetBit(c->flags, GCF_COMPATIBLE);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   248
5647
9a96d72c23be (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: 5646
diff changeset
   249
				/* Non-found has precedence over compatibility load */
6229
695400602ab0 (svn r9031) -Codechange: Introduce grfconfig->status, and use it for states that are
maedhros
parents: 6183
diff changeset
   250
				if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9428
diff changeset
   251
				GamelogGRFCompatible(f);
5647
9a96d72c23be (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: 5646
diff changeset
   252
				goto compatible_grf;
9a96d72c23be (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: 5646
diff changeset
   253
			}
9a96d72c23be (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: 5646
diff changeset
   254
9a96d72c23be (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: 5646
diff changeset
   255
			/* No compatible grf was found, mark it as disabled */
9a96d72c23be (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: 5646
diff changeset
   256
			md5sumToString(buf, lastof(buf), c->md5sum);
9a96d72c23be (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: 5646
diff changeset
   257
			DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->grfid), c->filename, buf);
9a96d72c23be (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: 5646
diff changeset
   258
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9428
diff changeset
   259
			GamelogGRFRemove(c->grfid);
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9428
diff changeset
   260
6229
695400602ab0 (svn r9031) -Codechange: Introduce grfconfig->status, and use it for states that are
maedhros
parents: 6183
diff changeset
   261
			c->status = GCS_NOT_FOUND;
695400602ab0 (svn r9031) -Codechange: Introduce grfconfig->status, and use it for states that are
maedhros
parents: 6183
diff changeset
   262
			res = GLC_NOT_FOUND;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   263
		} else {
5647
9a96d72c23be (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: 5646
diff changeset
   264
compatible_grf:
9a96d72c23be (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: 5646
diff changeset
   265
			DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->grfid), f->filename);
5349
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   266
			/* The filename could be the filename as in the savegame. As we need
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   267
			 * to load the GRF here, we need the correct filename, so overwrite that
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   268
			 * in any case and set the name and info when it is not set already.
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   269
			 * When the GCF_COPY flag is set, it is certain that the filename is
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   270
			 * already a local one, so there is no need to replace it. */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7882
diff changeset
   271
			if (!HasBit(c->flags, GCF_COPY)) {
5349
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   272
				free(c->filename);
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   273
				c->filename = strdup(f->filename);
5701
fdb25177500c (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: 5675
diff changeset
   274
				memcpy(c->md5sum, f->md5sum, sizeof(c->md5sum));
6183
59fa5aa7ea3a (svn r8955) -Fix: crash on loading savegames with GRFs that do not have their GRF info/name set.
rubidium
parents: 6139
diff changeset
   275
				if (c->name == NULL && f->name != NULL) c->name = strdup(f->name);
59fa5aa7ea3a (svn r8955) -Fix: crash on loading savegames with GRFs that do not have their GRF info/name set.
rubidium
parents: 6139
diff changeset
   276
				if (c->info == NULL && f->info != NULL) c->info = strdup(f->info);
6103
d38163ddfef9 (svn r8838) -Feature: Show newgrf error messages loaded in Action B in the newgrf gui
maedhros
parents: 6088
diff changeset
   277
				c->error = NULL;
5349
5e39937d6247 (svn r7520) -Fix(r7348): memleak due to unconditionally overwriting the filename, name and info of a GRFConfig in IsGoodGRFConfigList.
rubidium
parents: 5347
diff changeset
   278
			}
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   279
		}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   280
	}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   281
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   282
	return res;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   283
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   284
10036
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   285
/** Helper for scanning for files with GRF as extension */
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   286
class GRFFileScanner : FileScanner {
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   287
public:
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   288
	/* virtual */ bool AddFile(const char *filename, size_t basepath_length);
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   289
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   290
	/** Do the scan for GRFs. */
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   291
	static uint DoScan()
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   292
	{
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   293
		GRFFileScanner fs;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   294
		return fs.Scan(".grf", DATA_DIR);
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   295
	}
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   296
};
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   297
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   298
bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length)
7572
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   299
{
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   300
	GRFConfig *c = CallocT<GRFConfig>(1);
10036
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   301
	c->filename = strdup(filename + basepath_length);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   302
7572
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   303
	bool added = true;
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   304
	if (FillGRFDetails(c, false)) {
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   305
		if (_all_grfs == NULL) {
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   306
			_all_grfs = c;
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   307
		} else {
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   308
			/* Insert file into list at a position determined by its
8592
beea2a140a0a (svn r12173) -Cleanup: Minor codestyle fixes.
peter1138
parents: 8214
diff changeset
   309
			 * name, so the list is sorted as we go along */
7572
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   310
			GRFConfig **pd, *d;
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   311
			bool stop = false;
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   312
			for (pd = &_all_grfs; (d = *pd) != NULL; pd = &d->next) {
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   313
				if (c->grfid == d->grfid && memcmp(c->md5sum, d->md5sum, sizeof(c->md5sum)) == 0) added = false;
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   314
				/* Because there can be multiple grfs with the same name, make sure we checked all grfs with the same name,
7573
505e9d05c10a (svn r11098) -Fix r11096: forgot spaces before comments
truelight
parents: 7572
diff changeset
   315
				 *  before inserting the entry. So insert a new grf at the end of all grfs with the same name, instead of
505e9d05c10a (svn r11098) -Fix r11096: forgot spaces before comments
truelight
parents: 7572
diff changeset
   316
				 *  just after the first with the same name. Avoids doubles in the list. */
8592
beea2a140a0a (svn r12173) -Cleanup: Minor codestyle fixes.
peter1138
parents: 8214
diff changeset
   317
				if (strcasecmp(c->name, d->name) <= 0) {
beea2a140a0a (svn r12173) -Cleanup: Minor codestyle fixes.
peter1138
parents: 8214
diff changeset
   318
					stop = true;
beea2a140a0a (svn r12173) -Cleanup: Minor codestyle fixes.
peter1138
parents: 8214
diff changeset
   319
				} else if (stop) {
beea2a140a0a (svn r12173) -Cleanup: Minor codestyle fixes.
peter1138
parents: 8214
diff changeset
   320
					break;
beea2a140a0a (svn r12173) -Cleanup: Minor codestyle fixes.
peter1138
parents: 8214
diff changeset
   321
				}
7572
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   322
			}
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   323
			if (added) {
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   324
				c->next = d;
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   325
				*pd = c;
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   326
			}
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   327
		}
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   328
	} else {
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   329
		added = false;
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   330
	}
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   331
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   332
	if (!added) {
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   333
		/* File couldn't be opened, or is either not a NewGRF or is a
8592
beea2a140a0a (svn r12173) -Cleanup: Minor codestyle fixes.
peter1138
parents: 8214
diff changeset
   334
		 * 'system' NewGRF or it's already known, so forget about it. */
7572
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   335
		free(c->filename);
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   336
		free(c->name);
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   337
		free(c->info);
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   338
		free(c);
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   339
	}
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   340
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   341
	return added;
817d4302228e (svn r11097) -Codechange: for easy future extension of NewGRF Scanning, split up the functions a bit
truelight
parents: 7571
diff changeset
   342
}
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   343
7644
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   344
/**
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   345
 * Simple sorter for GRFS
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   346
 * @param p1 the first GRFConfig *
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   347
 * @param p2 the second GRFConfig *
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   348
 * @return the same strcmp would return for the name of the NewGRF.
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   349
 */
7648
c3f25e9f71e4 (svn r11179) -Fix [FS#1262] (r11175): compilation with MSVC was broken
glx
parents: 7644
diff changeset
   350
static int CDECL GRFSorter(const void *p1, const void *p2)
7644
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   351
{
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   352
	const GRFConfig *c1 = *(const GRFConfig **)p1;
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   353
	const GRFConfig *c2 = *(const GRFConfig **)p2;
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   354
9733
94144b73041b (svn r13865) -Codechange: Use case-insensitive sort for NewGRF list.
peter1138
parents: 9581
diff changeset
   355
	return strcasecmp(c1->name != NULL ? c1->name : c1->filename,
7644
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   356
		c2->name != NULL ? c2->name : c2->filename);
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   357
}
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   358
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   359
/* Scan for all NewGRFs */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6229
diff changeset
   360
void ScanNewGRFFiles()
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   361
{
5347
9b1bc705f2ae (svn r7518) -Codechange: more NULL pointer resets after free.
Darkvater
parents: 5346
diff changeset
   362
	ClearGRFConfigList(&_all_grfs);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   363
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5366
diff changeset
   364
	DEBUG(grf, 1, "Scanning for NewGRFs");
10036
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9733
diff changeset
   365
	uint num = GRFFileScanner::DoScan();
7644
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   366
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5366
diff changeset
   367
	DEBUG(grf, 1, "Scan complete, found %d files", num);
7649
c4e93cf27732 (svn r11180) -Fix (r11175): crash on load when you don't have any NewGRFs. More interesting is: who plays without them ;)
rubidium
parents: 7648
diff changeset
   368
	if (num == 0 || _all_grfs == NULL) return;
7644
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   369
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   370
	/* Sort the linked list using quicksort.
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   371
	 * For that we first have to make an array, the qsort and
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   372
	 * then remake the linked list. */
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   373
	GRFConfig **to_sort = MallocT<GRFConfig*>(num);
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   374
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   375
	uint i = 0;
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   376
	for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) {
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   377
		to_sort[i] = p;
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   378
	}
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   379
	/* Number of files is not necessarily right */
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   380
	num = i;
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   381
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   382
	qsort(to_sort, num, sizeof(GRFConfig*), GRFSorter);
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   383
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   384
	for (i = 1; i < num; i++) {
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   385
		to_sort[i - 1]->next = to_sort[i];
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   386
	}
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   387
	to_sort[num - 1]->next = NULL;
a7ff5c8ae47e (svn r11175) -Codechange: sort the NewGRFs by name, making searching a specific NewGRF a lot easier.
rubidium
parents: 7592
diff changeset
   388
	_all_grfs = to_sort[0];
9133
705318ad20db (svn r12993) -Fix (r11175): list used for sorting GRFs wasn't freed
smatz
parents: 9131
diff changeset
   389
705318ad20db (svn r12993) -Fix (r11175): list used for sorting GRFs wasn't freed
smatz
parents: 9131
diff changeset
   390
	free(to_sort);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   391
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   392
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   393
5646
e94c63712efe (svn r8105) -Codechange: Change FindGRFConfig so that if md5sum parameter is omitted (or NULL)
Darkvater
parents: 5634
diff changeset
   394
/* Find a NewGRF in the scanned list, if md5sum is NULL, we don't care about it*/
e94c63712efe (svn r8105) -Codechange: Change FindGRFConfig so that if md5sum parameter is omitted (or NULL)
Darkvater
parents: 5634
diff changeset
   395
const GRFConfig *FindGRFConfig(uint32 grfid, const uint8 *md5sum)
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   396
{
5646
e94c63712efe (svn r8105) -Codechange: Change FindGRFConfig so that if md5sum parameter is omitted (or NULL)
Darkvater
parents: 5634
diff changeset
   397
	for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
e94c63712efe (svn r8105) -Codechange: Change FindGRFConfig so that if md5sum parameter is omitted (or NULL)
Darkvater
parents: 5634
diff changeset
   398
		if (c->grfid == grfid) {
e94c63712efe (svn r8105) -Codechange: Change FindGRFConfig so that if md5sum parameter is omitted (or NULL)
Darkvater
parents: 5634
diff changeset
   399
			if (md5sum == NULL) return c;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   400
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   401
			if (memcmp(md5sum, c->md5sum, sizeof(c->md5sum)) == 0) return c;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   402
		}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   403
	}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   404
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   405
	return NULL;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   406
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   407
5441
e8bac228a6db (svn r7692) -Fix: OpenTTD didn't compile without network enabled (newgrf sync code)
Darkvater
parents: 5401
diff changeset
   408
#ifdef ENABLE_NETWORK
e8bac228a6db (svn r7692) -Fix: OpenTTD didn't compile without network enabled (newgrf sync code)
Darkvater
parents: 5401
diff changeset
   409
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   410
/** Structure for UnknownGRFs; this is a lightweight variant of GRFConfig */
5765
27dbbe543fcf (svn r8316) -Codechange: move the GRF ID and MD5 checksum from GRFConfig to GRFIdentifier so it can be reused.
rubidium
parents: 5701
diff changeset
   411
struct UnknownGRF : public GRFIdentifier {
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   412
	UnknownGRF *next;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   413
	char   name[NETWORK_GRF_NAME_LENGTH];
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   414
};
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   415
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   416
/**
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   417
 * Finds the name of a NewGRF in the list of names for unknown GRFs. An
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   418
 * unknown GRF is a GRF where the .grf is not found during scanning.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   419
 *
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   420
 * The names are resolved via UDP calls to servers that should know the name,
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   421
 * though the replies may not come. This leaves "<Unknown>" as name, though
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   422
 * that shouldn't matter _very_ much as they need GRF crawler or so to look
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   423
 * up the GRF anyway and that works better with the GRF ID.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   424
 *
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   425
 * @param grfid  the GRF ID part of the 'unique' GRF identifier
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   426
 * @param md5sum the MD5 checksum part of the 'unique' GRF identifier
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   427
 * @param create whether to create a new GRFConfig if the GRFConfig did not
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   428
 *               exist in the fake list of GRFConfigs.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   429
 * @return the GRFConfig with the given GRF ID and MD5 checksum or NULL when
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   430
 *         it does not exist and create is false. This value must NEVER be
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   431
 *         freed by the caller.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   432
 */
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   433
char *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   434
{
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   435
	UnknownGRF *grf;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   436
	static UnknownGRF *unknown_grfs = NULL;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   437
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   438
	for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   439
		if (grf->grfid == grfid) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   440
			if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   441
		}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   442
	}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   443
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   444
	if (!create) return NULL;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   445
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
   446
	grf = CallocT<UnknownGRF>(1);
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   447
	grf->grfid = grfid;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   448
	grf->next  = unknown_grfs;
10310
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10299
diff changeset
   449
	strecpy(grf->name, UNKNOWN_GRF_NAME_PLACEHOLDER, lastof(grf->name));
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   450
	memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   451
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   452
	unknown_grfs = grf;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   453
	return grf->name;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   454
}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5333
diff changeset
   455
5441
e8bac228a6db (svn r7692) -Fix: OpenTTD didn't compile without network enabled (newgrf sync code)
Darkvater
parents: 5401
diff changeset
   456
#endif /* ENABLE_NETWORK */
e8bac228a6db (svn r7692) -Fix: OpenTTD didn't compile without network enabled (newgrf sync code)
Darkvater
parents: 5401
diff changeset
   457
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   458
5234
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   459
/* Retrieve a NewGRF from the current config by its grfid */
5333
dedf1f6de6d3 (svn r7495) -Fix (r7354): [NewGRF] Deactivate the target GRF, not the current GRF.
peter1138
parents: 5329
diff changeset
   460
GRFConfig *GetGRFConfig(uint32 grfid)
5234
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   461
{
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   462
	GRFConfig *c;
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   463
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   464
	for (c = _grfconfig; c != NULL; c = c->next) {
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   465
		if (c->grfid == grfid) return c;
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   466
	}
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   467
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   468
	return NULL;
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   469
}
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   470
980aa61260aa (svn r7354) -Codechange: Remove grffile->flags and use grfconfig->flags exclusively. Update action 7/9 now that we know if GRFs are disabled.
peter1138
parents: 5229
diff changeset
   471
5308
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   472
/* Build a space separated list of parameters, and terminate */
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   473
char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   474
{
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   475
	uint i;
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   476
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   477
	/* Return an empty string if there are no parameters */
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   478
	if (c->num_params == 0) return strecpy(dst, "", last);
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   479
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   480
	for (i = 0; i < c->num_params; i++) {
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   481
		if (i > 0) dst = strecpy(dst, " ", last);
10299
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10077
diff changeset
   482
		dst += seprintf(dst, last, "%d", c->param[i]);
5308
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   483
	}
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   484
	return dst;
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   485
}
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   486
7882
308cab08d2f3 (svn r11433) -Fix: starting OpenTTD with DOS files made it look weird out of the box.
rubidium
parents: 7805
diff changeset
   487
/** Base GRF ID for OpenTTD's base graphics GRFs. */
308cab08d2f3 (svn r11433) -Fix: starting OpenTTD with DOS files made it look weird out of the box.
rubidium
parents: 7805
diff changeset
   488
static const uint32 OPENTTD_GRAPHICS_BASE_GRF_ID = BSWAP32(0xFF4F5400);
308cab08d2f3 (svn r11433) -Fix: starting OpenTTD with DOS files made it look weird out of the box.
rubidium
parents: 7805
diff changeset
   489
308cab08d2f3 (svn r11433) -Fix: starting OpenTTD with DOS files made it look weird out of the box.
rubidium
parents: 7805
diff changeset
   490
/**
308cab08d2f3 (svn r11433) -Fix: starting OpenTTD with DOS files made it look weird out of the box.
rubidium
parents: 7805
diff changeset
   491
 * Checks whether this GRF is a OpenTTD base graphic GRF.
308cab08d2f3 (svn r11433) -Fix: starting OpenTTD with DOS files made it look weird out of the box.
rubidium
parents: 7805
diff changeset
   492
 * @return true if and only if it is a base GRF.
308cab08d2f3 (svn r11433) -Fix: starting OpenTTD with DOS files made it look weird out of the box.
rubidium
parents: 7805
diff changeset
   493
 */
308cab08d2f3 (svn r11433) -Fix: starting OpenTTD with DOS files made it look weird out of the box.
rubidium
parents: 7805
diff changeset
   494
bool GRFConfig::IsOpenTTDBaseGRF() const
308cab08d2f3 (svn r11433) -Fix: starting OpenTTD with DOS files made it look weird out of the box.
rubidium
parents: 7805
diff changeset
   495
{
308cab08d2f3 (svn r11433) -Fix: starting OpenTTD with DOS files made it look weird out of the box.
rubidium
parents: 7805
diff changeset
   496
	return (this->grfid & 0x00FFFFFF) == OPENTTD_GRAPHICS_BASE_GRF_ID;
308cab08d2f3 (svn r11433) -Fix: starting OpenTTD with DOS files made it look weird out of the box.
rubidium
parents: 7805
diff changeset
   497
}
308cab08d2f3 (svn r11433) -Fix: starting OpenTTD with DOS files made it look weird out of the box.
rubidium
parents: 7805
diff changeset
   498
5308
990a4b188233 (svn r7464) -Codechange: move BuildParamList from newgrf_gui to newgrf_config and
peter1138
parents: 5296
diff changeset
   499
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   500
static const SaveLoad _grfconfig_desc[] = {
10066
f18e97752eeb (svn r14233) -Feature/Fix [FS#2172]: save the palette of the loaded NewGRFs in the savegame, so joining with a server using Windows palette will make a client with the DOS palette do palette conversion and (thus) not cause a desync due to the different palettes disabling different NewGRFs.
rubidium
parents: 10039
diff changeset
   501
	    SLE_STR(GRFConfig, filename,         SLE_STR,    0x40),
f18e97752eeb (svn r14233) -Feature/Fix [FS#2172]: save the palette of the loaded NewGRFs in the savegame, so joining with a server using Windows palette will make a client with the DOS palette do palette conversion and (thus) not cause a desync due to the different palettes disabling different NewGRFs.
rubidium
parents: 10039
diff changeset
   502
	    SLE_VAR(GRFConfig, grfid,            SLE_UINT32),
f18e97752eeb (svn r14233) -Feature/Fix [FS#2172]: save the palette of the loaded NewGRFs in the savegame, so joining with a server using Windows palette will make a client with the DOS palette do palette conversion and (thus) not cause a desync due to the different palettes disabling different NewGRFs.
rubidium
parents: 10039
diff changeset
   503
	    SLE_ARR(GRFConfig, md5sum,           SLE_UINT8,  16),
f18e97752eeb (svn r14233) -Feature/Fix [FS#2172]: save the palette of the loaded NewGRFs in the savegame, so joining with a server using Windows palette will make a client with the DOS palette do palette conversion and (thus) not cause a desync due to the different palettes disabling different NewGRFs.
rubidium
parents: 10039
diff changeset
   504
	    SLE_ARR(GRFConfig, param,            SLE_UINT32, 0x80),
f18e97752eeb (svn r14233) -Feature/Fix [FS#2172]: save the palette of the loaded NewGRFs in the savegame, so joining with a server using Windows palette will make a client with the DOS palette do palette conversion and (thus) not cause a desync due to the different palettes disabling different NewGRFs.
rubidium
parents: 10039
diff changeset
   505
	    SLE_VAR(GRFConfig, num_params,       SLE_UINT8),
f18e97752eeb (svn r14233) -Feature/Fix [FS#2172]: save the palette of the loaded NewGRFs in the savegame, so joining with a server using Windows palette will make a client with the DOS palette do palette conversion and (thus) not cause a desync due to the different palettes disabling different NewGRFs.
rubidium
parents: 10039
diff changeset
   506
	SLE_CONDVAR(GRFConfig, windows_paletted, SLE_BOOL,   101, SL_MAX_VERSION),
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   507
	SLE_END()
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   508
};
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   509
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   510
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6229
diff changeset
   511
static void Save_NGRF()
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   512
{
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   513
	int index = 0;
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   514
6108
c0929232db31 (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6103
diff changeset
   515
	for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7882
diff changeset
   516
		if (HasBit(c->flags, GCF_STATIC)) continue;
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   517
		SlSetArrayIndex(index++);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   518
		SlObject(c, _grfconfig_desc);
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   519
	}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   520
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   521
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   522
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6229
diff changeset
   523
static void Load_NGRF()
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   524
{
6108
c0929232db31 (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6103
diff changeset
   525
	ClearGRFConfigList(&_grfconfig);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   526
	while (SlIterateArray() != -1) {
6108
c0929232db31 (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6103
diff changeset
   527
		GRFConfig *c = CallocT<GRFConfig>(1);
c0929232db31 (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6103
diff changeset
   528
		SlObject(c, _grfconfig_desc);
10066
f18e97752eeb (svn r14233) -Feature/Fix [FS#2172]: save the palette of the loaded NewGRFs in the savegame, so joining with a server using Windows palette will make a client with the DOS palette do palette conversion and (thus) not cause a desync due to the different palettes disabling different NewGRFs.
rubidium
parents: 10039
diff changeset
   529
		if (CheckSavegameVersion(101)) c->windows_paletted = (_use_palette == PAL_WINDOWS);
6108
c0929232db31 (svn r8844) -Revert partly (r8820, r8806): Change AppendToGRFConfigList to add the allocated GRFConfig to its list and not copy it.
Darkvater
parents: 6103
diff changeset
   530
		AppendToGRFConfigList(&_grfconfig, c);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   531
	}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   532
5329
9c5e8061ea0f (svn r7490) -Feature: Load a list of NewGRFs from the config (in the [newgrf-static] section) that should always be loaded. These will also be active during the intro screen, and in multiplayer games. Only "network-safe" NewGRFs are permitted, such as fonts and sprite replacement sets.
peter1138
parents: 5308
diff changeset
   533
	/* Append static NewGRF configuration */
5393
cf584337ea2c (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: 5380
diff changeset
   534
	AppendStaticGRFConfigs(&_grfconfig);
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   535
}
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   536
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   537
extern const ChunkHandler _newgrf_chunk_handlers[] = {
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   538
	{ 'NGRF', Save_NGRF, Load_NGRF, CH_ARRAY | CH_LAST }
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   539
};
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents:
diff changeset
   540
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5366
diff changeset
   541
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6248
diff changeset
   542