Changeset - r6229:a69564e4eb2e
[Not reviewed]
master
0 7 0
maedhros - 17 years ago 2007-03-06 19:33:28
maedhros@openttd.org
(svn r9031) -Codechange: Introduce grfconfig->status, and use it for states that are
mutually exclusive. At the same time, add an INITIALISED state which makes it
possible to check if a grf is not yet active but will be later on during the
GLS_ACTIVATION loading stage.
7 files changed with 62 insertions and 49 deletions:
0 comments (0 inline, 0 general)
src/network/network_udp.cpp
Show inline comments
 
@@ -285,14 +285,14 @@ DEF_UDP_RECEIVE_COMMAND(Client, PACKET_U
 
		const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT];
 
		const GRFConfig *c;
 
		uint in_request_count = 0;
 
		struct sockaddr_in out_addr;
 

	
 
		for (c = item->info.grfconfig; c != NULL; c = c->next) {
 
			if (HASBIT(c->flags, GCF_NOT_FOUND)) item->info.compatible = false;
 
			if (!HASBIT(c->flags, GCF_NOT_FOUND) || strcmp(c->name, UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue;
 
			if (c->status == GCS_NOT_FOUND) item->info.compatible = false;
 
			if (c->status == GCS_NOT_FOUND || strcmp(c->name, UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue;
 
			in_request[in_request_count] = c;
 
			in_request_count++;
 
		}
 

	
 
		if (in_request_count > 0) {
 
			/* There are 'unknown' GRFs, now send a request for them */
 
@@ -389,13 +389,13 @@ void ClientNetworkUDPSocketHandler::Hand
 
	const GRFConfig *f = FindGRFConfig(config->grfid, config->md5sum);
 
	if (f == NULL) {
 
		/* Don't know the GRF, so mark game incompatible and the (possibly)
 
		 * already resolved name for this GRF (another server has sent the
 
		 * name of the GRF already */
 
		config->name     = FindUnknownGRFName(config->grfid, config->md5sum, true);
 
		SETBIT(config->flags, GCF_NOT_FOUND);
 
		config->status   = GCS_NOT_FOUND;
 
	} else {
 
		config->filename = f->filename;
 
		config->name     = f->name;
 
		config->info     = f->info;
 
	}
 
	SETBIT(config->flags, GCF_COPY);
src/newgrf.cpp
Show inline comments
 
@@ -2532,30 +2532,30 @@ static void SkipIf(byte *buf, int len)
 
			return;
 
		}
 

	
 
		switch (condtype) {
 
			/* Tests 6 to 10 are only for param 0x88, GRFID checks */
 
			case 6: /* Is GRFID active? */
 
				result = HASBIT(c->flags, GCF_ACTIVATED);
 
				result = c->status == GCS_ACTIVATED;
 
				break;
 

	
 
			case 7: /* Is GRFID non-active? */
 
				result = !HASBIT(c->flags, GCF_ACTIVATED);
 
				result = c->status != GCS_ACTIVATED;
 
				break;
 

	
 
			case 8: /* GRFID is not but will be active? */
 
				result = !HASBIT(c->flags, GCF_ACTIVATED) && !HASBIT(c->flags, GCF_DISABLED);
 
				result = c->status == GCS_INITIALISED;
 
				break;
 

	
 
			case 9: /* GRFID is or will be active? */
 
				result = !HASBIT(c->flags, GCF_NOT_FOUND) && !HASBIT(c->flags, GCF_DISABLED);
 
				result = c->status == GCS_ACTIVATED || c->status == GCS_INITIALISED;
 
				break;
 

	
 
			case 10: /* GRFID is not nor will be active */
 
				/* This is the only condtype that doesn't get ignored if the GRFID is not found */
 
				result = c == NULL || HASBIT(c->flags, GCF_DISABLED) || HASBIT(c->flags, GCF_NOT_FOUND);
 
				result = c == NULL || c->flags == GCS_DISABLED || c->status == GCS_NOT_FOUND;
 
				break;
 

	
 
			default: grfmsg(1, "Unsupported GRF test %d. Ignoring", condtype); return;
 
		}
 
	} else {
 
		/* Parameter or variable tests */
 
@@ -2613,13 +2613,13 @@ static void SkipIf(byte *buf, int len)
 
		/* Zero means there are no sprites to skip, so
 
		 * we use -1 to indicate that all further
 
		 * sprites should be skipped. */
 
		_skip_sprites = -1;
 

	
 
		/* If an action 8 hasn't been encountered yet, disable the grf. */
 
		if (!HASBIT(_cur_grfconfig->flags, GCF_ACTIVATED)) SETBIT(_cur_grfconfig->flags, GCF_DISABLED);
 
		if (_cur_grfconfig->status != GCS_ACTIVATED) _cur_grfconfig->status = GCS_DISABLED;
 
	}
 
}
 

	
 

	
 
/* Action 0x08 (GLS_FILESCAN) */
 
static void ScanInfo(byte *buf, int len)
 
@@ -2670,13 +2670,13 @@ static void GRFInfo(byte *buf, int len)
 
	version = grf_load_byte(&buf);
 
	grfid = grf_load_dword(&buf);
 
	name = grf_load_string(&buf, len - 6);
 

	
 
	_cur_grffile->grfid = grfid;
 
	_cur_grffile->grf_version = version;
 
	SETBIT(_cur_grfconfig->flags, GCF_ACTIVATED);
 
	_cur_grfconfig->status = _cur_stage < GLS_ACTIVATION ? GCS_INITIALISED : GCS_ACTIVATED;
 

	
 
	/* Do swap the GRFID for displaying purposes since people expect that */
 
	DEBUG(grf, 1, "Loaded GRFv%d set %08lX - %s", version, BSWAP32(grfid), name);
 
}
 

	
 
/* Action 0x0A */
 
@@ -2776,14 +2776,13 @@ static void GRFLoadError(byte *buf, int 
 
	if (severity >= lengthof(sevstr)) {
 
		grfmsg(7, "GRFLoadError: Invalid severity id %d. Setting to 2 (non-fatal error).", severity);
 
		severity = 2;
 
	} else if (severity == 3) {
 
		/* This is a fatal error, so make sure the GRF is deactivated and no
 
		 * more of it gets loaded. */
 
		SETBIT(_cur_grfconfig->flags, GCF_DISABLED);
 
		CLRBIT(_cur_grfconfig->flags, GCF_ACTIVATED);
 
		_cur_grfconfig->status = GCS_DISABLED;
 

	
 
		_skip_sprites = -1;
 
	}
 

	
 
	if (message_id >= lengthof(msgstr) && message_id != 0xFF) {
 
		grfmsg(7, "GRFLoadError: Invalid message id.");
 
@@ -2978,14 +2977,13 @@ static void ParamSet(byte *buf, int len)
 
								src1 = start;
 
							} else {
 
								/* Unable to allocate */
 
								if (op != 4 && op != 5) {
 
									/* Deactivate GRF */
 
									grfmsg(0, "GRM: Unable to allocate %d vehicles, deactivating", count);
 
									SETBIT(_cur_grfconfig->flags, GCF_DISABLED);
 
									CLRBIT(_cur_grfconfig->flags, GCF_ACTIVATED);
 
									_cur_grfconfig->status = GCS_DISABLED;
 

	
 
									_skip_sprites = -1;
 
									return;
 
								}
 

	
 
								grfmsg(1, "GRM: Unable to allocate %d vehicles", count);
 
@@ -2997,14 +2995,13 @@ static void ParamSet(byte *buf, int len)
 
						case 0x08: /* General sprites */
 
							switch (op) {
 
								case 0:
 
									/* Check if the allocated sprites will fit below the original sprite limit */
 
									if (_cur_spriteid + count >= 16384) {
 
										grfmsg(0, "GRM: Unable to allocate %d sprites; try changing NewGRF order", count);
 
										SETBIT(_cur_grfconfig->flags, GCF_DISABLED);
 
										CLRBIT(_cur_grfconfig->flags, GCF_ACTIVATED);
 
										_cur_grfconfig->status = GCS_DISABLED;
 

	
 
										_skip_sprites = -1;
 
										return;
 
									}
 

	
 
									/* 'Reserve' space at the current sprite ID */
 
@@ -3215,14 +3212,13 @@ static void GRFInhibit(byte *buf, int le
 
		uint32 grfid = grf_load_dword(&buf);
 
		GRFConfig *file = GetGRFConfig(grfid);
 

	
 
		/* Unset activation flag */
 
		if (file != NULL && file != _cur_grfconfig) {
 
			grfmsg(2, "GRFInhibit: Deactivating file '%s'", file->filename);
 
			SETBIT(file->flags, GCF_DISABLED);
 
			CLRBIT(file->flags, GCF_ACTIVATED);
 
			file->status = GCS_DISABLED;
 
		}
 
	}
 
}
 

	
 
/* Action 0x10 */
 
static void DefineGotoLabel(byte *buf, int len)
 
@@ -3947,17 +3943,15 @@ void LoadNewGRFFile(GRFConfig *config, u
 
	 * During activation, only actions 0, 1, 2, 3, 4, 5, 7, 8, 9, 0A and 0B are
 
	 * carried out.  All others are ignored, because they only need to be
 
	 * processed once at initialization.  */
 
	if (stage != GLS_FILESCAN && stage != GLS_SAFETYSCAN && stage != GLS_LABELSCAN) {
 
		_cur_grffile = GetFileByFilename(filename);
 
		if (_cur_grffile == NULL) error("File '%s' lost in cache.\n", filename);
 
		if (stage == GLS_ACTIVATION && !HASBIT(config->flags, GCF_ACTIVATED)) return;
 
		if (stage == GLS_ACTIVATION && config->status != GCS_INITIALISED) return;
 
	}
 

	
 
	if (stage == GLS_ACTIVATION) CLRBIT(config->flags, GCF_ACTIVATED);
 

	
 
	FioOpenFile(file_index, filename);
 
	_file_index = file_index; // XXX
 

	
 
	_cur_grfconfig = config;
 

	
 
	DEBUG(grf, 2, "Reading NewGRF-file '%s'", filename);
 
@@ -4034,13 +4028,13 @@ void LoadNewGRF(uint load_index, uint fi
 
		uint slot = file_index;
 
		GRFConfig *c;
 

	
 
		_cur_stage = stage;
 
		_cur_spriteid = load_index;
 
		for (c = _grfconfig; c != NULL; c = c->next) {
 
			if (HASBIT(c->flags, GCF_DISABLED) || HASBIT(c->flags, GCF_NOT_FOUND)) continue;
 
			if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
 

	
 
			// TODO usererror()
 
			if (!FioCheckFileExists(c->filename)) error("NewGRF file is missing '%s'", c->filename);
 

	
 
			if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
 
			LoadNewGRFFile(c, slot++, stage);
src/newgrf_config.cpp
Show inline comments
 
@@ -57,13 +57,13 @@ static bool CalcGRFMD5Sum(GRFConfig *con
 

	
 

	
 
/* Find the GRFID and calculate the md5sum */
 
bool FillGRFDetails(GRFConfig *config, bool is_static)
 
{
 
	if (!FioCheckFileExists(config->filename)) {
 
		SETBIT(config->flags, GCF_NOT_FOUND);
 
		config->status = GCS_NOT_FOUND;
 
		return false;
 
	}
 

	
 
	/* Find and load the Action 8 information */
 
	/* 62 is the last file slot before sample.cat.
 
	 * Should perhaps be some "don't care" value */
 
@@ -207,20 +207,20 @@ void ResetGRFConfig(bool defaults)
 
}
 

	
 

	
 
/** Check if all GRFs in the GRF config from a savegame can be loaded.
 
 * @return will return any of the following 3 values:<br>
 
 * <ul>
 
 * <li> GCF_ACTIVATED: No problems occured, all GRF files were found and loaded
 
 * <li> GCF_COMPATIBLE: For one or more GRF's no exact match was found, but a
 
 * <li> GLC_ALL_GOOD: No problems occured, all GRF files were found and loaded
 
 * <li> GLC_COMPATIBLE: For one or more GRF's no exact match was found, but a
 
 *     compatible GRF with the same grfid was found and used instead
 
 * <li> GCF_NOT_FOUND: For one or more GRF's no match was found at all
 
 * <li> GLC_NOT_FOUND: For one or more GRF's no match was found at all
 
 * </ul> */
 
GCF_Flags IsGoodGRFConfigList(void)
 
GRFListCompatibility IsGoodGRFConfigList(void)
 
{
 
	GCF_Flags res = GCF_ACTIVATED;
 
	GRFListCompatibility res = GLC_ALL_GOOD;
 

	
 
	for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
 
		const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
 
		if (f == NULL) {
 
			char buf[256];
 

	
 
@@ -230,22 +230,22 @@ GCF_Flags IsGoodGRFConfigList(void)
 
			if (f != NULL) {
 
				md5sumToString(buf, lastof(buf), c->md5sum);
 
				DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->grfid), c->filename, buf);
 
				SETBIT(c->flags, GCF_COMPATIBLE);
 

	
 
				/* Non-found has precedence over compatibility load */
 
				if (res != GCF_NOT_FOUND) res = GCF_COMPATIBLE;
 
				if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
 
				goto compatible_grf;
 
			}
 

	
 
			/* No compatible grf was found, mark it as disabled */
 
			md5sumToString(buf, lastof(buf), c->md5sum);
 
			DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->grfid), c->filename, buf);
 

	
 
			SETBIT(c->flags, GCF_NOT_FOUND);
 
			res = GCF_NOT_FOUND;
 
			c->status = GCS_NOT_FOUND;
 
			res = GLC_NOT_FOUND;
 
		} else {
 
compatible_grf:
 
			DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->grfid), f->filename);
 
			/* The filename could be the filename as in the savegame. As we need
 
			 * to load the GRF here, we need the correct filename, so overwrite that
 
			 * in any case and set the name and info when it is not set already.
src/newgrf_config.h
Show inline comments
 
@@ -4,22 +4,33 @@
 
#define NEWGRF_CONFIG_H
 

	
 
#include "openttd.h"
 

	
 
/* GRF config bit flags */
 
typedef enum {
 
	GCF_DISABLED,  ///< GRF file is disabled
 
	GCF_NOT_FOUND, ///< GRF file was not found in the local cache
 
	GCF_ACTIVATED, ///< GRF file is active
 
	GCF_SYSTEM,    ///< GRF file is an openttd-internal system grf
 
	GCF_UNSAFE,    ///< GRF file is unsafe for static usage
 
	GCF_STATIC,    ///< GRF file is used statically (can be used in any MP game)
 
	GCF_COMPATIBLE,///< GRF file does not exactly match the requested GRF (different MD5SUM), but grfid matches)
 
	GCF_COPY,      ///< The data is copied from a grf in _all_grfs
 
} GCF_Flags;
 

	
 
typedef enum {
 
	GCS_UNKNOWN,      ///< The status of this grf file is unknown
 
	GCS_DISABLED,     ///< GRF file is disabled
 
	GCS_NOT_FOUND,    ///< GRF file was not found in the local cache
 
	GCS_INITIALISED,  ///< GRF file has been initialised
 
	GCS_ACTIVATED     ///< GRF file has been activated
 
} GRFStatus;
 

	
 
typedef enum {
 
	GLC_ALL_GOOD,
 
	GLC_COMPATIBLE,
 
	GLC_NOT_FOUND
 
} GRFListCompatibility;
 

	
 
typedef struct GRFIdentifier {
 
	uint32 grfid;
 
	uint8 md5sum[16];
 
} GRF;
 

	
 
typedef struct GRFError {
 
@@ -34,12 +45,13 @@ typedef struct GRFConfig : public GRFIde
 
	char *filename;
 
	char *name;
 
	char *info;
 
	GRFError *error;
 

	
 
	uint8 flags;
 
	GRFStatus status;
 
	uint32 param[0x80];
 
	uint8 num_params;
 

	
 
	struct GRFConfig *next;
 
} GRFConfig;
 

	
 
@@ -61,13 +73,13 @@ GRFConfig *GetGRFConfig(uint32 grfid);
 
GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src);
 
void AppendStaticGRFConfigs(GRFConfig **dst);
 
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el);
 
void ClearGRFConfig(GRFConfig **config);
 
void ClearGRFConfigList(GRFConfig **config);
 
void ResetGRFConfig(bool defaults);
 
GCF_Flags IsGoodGRFConfigList(void);
 
GRFListCompatibility IsGoodGRFConfigList(void);
 
bool FillGRFDetails(GRFConfig *config, bool is_static);
 
char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last);
 

	
 
/* In newgrf_gui.c */
 
void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config);
 

	
src/newgrf_gui.cpp
Show inline comments
 
@@ -88,14 +88,14 @@ static void ShowNewGRFInfo(const GRFConf
 
			SetDParam(0, STR_01A9_NONE);
 
		}
 
		y += DrawStringMultiLine(x, y, STR_NEWGRF_PARAMETER, w);
 
	}
 

	
 
	/* Show flags */
 
	if (HASBIT(c->flags, GCF_NOT_FOUND))  y += DrawStringMultiLine(x, y, STR_NEWGRF_NOT_FOUND, w);
 
	if (HASBIT(c->flags, GCF_DISABLED))   y += DrawStringMultiLine(x, y, STR_NEWGRF_DISABLED, w);
 
	if (c->status == GCS_NOT_FOUND)        y += DrawStringMultiLine(x, y, STR_NEWGRF_NOT_FOUND, w);
 
	if (c->status == GCS_DISABLED)         y += DrawStringMultiLine(x, y, STR_NEWGRF_DISABLED, w);
 
	if (HASBIT(c->flags, GCF_COMPATIBLE)) y += DrawStringMultiLine(x, y, STR_NEWGRF_COMPATIBLE_LOADED, w);
 

	
 
	/* Draw GRF info if it exists */
 
	if (c->info != NULL && !StrEmpty(c->info)) {
 
		SetDParamStr(0, c->info);
 
		y += DrawStringMultiLine(x, y, STR_02BD, w);
 
@@ -332,22 +332,29 @@ static void NewGRFWndProc(Window *w, Win
 
			for (c = *WP(w, newgrf_d).list, i = 0; c != NULL; c = c->next, i++) {
 
				if (i >= w->vscroll.pos && i < w->vscroll.pos + w->vscroll.cap) {
 
					const char *text = (c->name != NULL && !StrEmpty(c->name)) ? c->name : c->filename;
 
					SpriteID pal;
 

	
 
					/* Pick a colour */
 
					if (HASBIT(c->flags, GCF_NOT_FOUND) || HASBIT(c->flags, GCF_DISABLED)) {
 
						pal = PALETTE_TO_RED;
 
					} else if (HASBIT(c->flags, GCF_STATIC)) {
 
					switch (c->status) {
 
						case GCS_NOT_FOUND:
 
						case GCS_DISABLED:
 
							pal = PALETTE_TO_RED;
 
							break;
 
						case GCS_ACTIVATED:
 
							pal = PALETTE_TO_GREEN;
 
							break;
 
						default:
 
							pal = PALETTE_TO_BLUE;
 
							break;
 
					}
 

	
 
					if (HASBIT(c->flags, GCF_STATIC)) {
 
						pal = PALETTE_TO_GREY;
 
					} else if (HASBIT(c->flags, GCF_COMPATIBLE)) {
 
						pal = PALETTE_TO_ORANGE;
 
					} else if (HASBIT(c->flags, GCF_ACTIVATED)) {
 
						pal = PALETTE_TO_GREEN;
 
					} else {
 
						pal = PALETTE_TO_BLUE;
 
					}
 

	
 
					DrawSprite(SPR_SQUARE, pal, 5, y + 2);
 
					if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, 20, y + 2);
 
					DoDrawString(text, c->error != NULL ? 35 : 25, y + 3, WP(w, newgrf_d).sel == c ? 0xC : 0x10);
 
					y += 14;
src/openttd.cpp
Show inline comments
 
@@ -1206,18 +1206,18 @@ bool AfterLoadGame(void)
 
	}
 

	
 
	// convert road side to my format.
 
	if (_opt.road_side) _opt.road_side = 1;
 

	
 
	/* Check if all NewGRFs are present, we are very strict in MP mode */
 
	GCF_Flags gcf_res = IsGoodGRFConfigList();
 
	if (_networking && gcf_res != GCF_ACTIVATED) return false;
 
	GRFListCompatibility gcf_res = IsGoodGRFConfigList();
 
	if (_networking && gcf_res != GLC_ALL_GOOD) return false;
 

	
 
	switch (gcf_res) {
 
		case GCF_COMPATIBLE: _switch_mode_errorstr = STR_NEWGRF_COMPATIBLE_LOAD_WARNING; break;
 
		case GCF_NOT_FOUND: _switch_mode_errorstr = STR_NEWGRF_DISABLED_WARNING; break;
 
		case GLC_COMPATIBLE: _switch_mode_errorstr = STR_NEWGRF_COMPATIBLE_LOAD_WARNING; break;
 
		case GLC_NOT_FOUND: _switch_mode_errorstr = STR_NEWGRF_DISABLED_WARNING; break;
 
		default: break;
 
	}
 

	
 
	/* Update current year
 
	 * must be done before loading sprites as some newgrfs check it */
 
	SetDate(_date);
src/settings.cpp
Show inline comments
 
@@ -1527,13 +1527,13 @@ static GRFConfig *GRFLoadConfig(IniFile 
 
		}
 

	
 
		/* Check if item is valid */
 
		if (!FillGRFDetails(c, is_static)) {
 
			const char *msg;
 

	
 
			if (HASBIT(c->flags, GCF_NOT_FOUND)) {
 
			if (c->status == GCS_NOT_FOUND) {
 
				msg = "not found";
 
			} else if (HASBIT(c->flags, GCF_UNSAFE)) {
 
				msg = "unsafe for static use";
 
			} else if (HASBIT(c->flags, GCF_SYSTEM)) {
 
				msg = "system NewGRF";
 
			} else {
0 comments (0 inline, 0 general)