(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.)
authorpeter1138
Wed, 03 Jan 2007 11:33:54 +0000
changeset 5502 003b36599ff8
parent 5501 b4e81fd72b3a
child 5503 036e83e9083a
(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.)
src/newgrf.c
src/newgrf_config.c
--- a/src/newgrf.c	Wed Jan 03 09:45:07 2007 +0000
+++ b/src/newgrf.c	Wed Jan 03 11:33:54 2007 +0000
@@ -2451,8 +2451,8 @@
 }
 
 
-/* Action 0x08 (GLS_SAFETYSCAN) */
-static void SafeInfo(byte *buf, int len)
+/* Action 0x08 (GLS_FILESCAN) */
+static void ScanInfo(byte *buf, int len)
 {
 	uint8 version;
 	uint32 grfid;
@@ -2483,12 +2483,6 @@
 
 		if (info_len < len) _cur_grfconfig->info  = TranslateTTDPatchCodes(info);
 	}
-}
-
-/* Action 0x08 (GLS_INFOSCAN) */
-static void ScanInfo(byte *buf, int len)
-{
-	SafeInfo(buf, len);
 
 	/* GLS_INFOSCAN only looks for the action 8, so we can skip the rest of the file */
 	_skip_sprites = -1;
@@ -2908,6 +2902,37 @@
 	}
 }
 
+/* Action 0x0E (GLS_SAFETYSCAN) */
+static void SafeGRFInhibit(byte *buf, int len)
+{
+	/* <0E> <num> <grfids...>
+	 *
+	 * B num           Number of GRFIDs that follow
+	 * D grfids        GRFIDs of the files to deactivate */
+
+	byte num;
+	int i;
+
+	check_length(len, 1, "GRFInhibit");
+	buf++, len--;
+	num = grf_load_byte(&buf); len--;
+	check_length(len, 4 * num, "GRFInhibit");
+
+	for (i = 0; i < num; i++) {
+		uint32 grfid = grf_load_dword(&buf);
+
+		/* GRF is unsafe it if tries to deactivate other GRFs */
+		if (grfid != _cur_grfconfig->grfid) {
+			SETBIT(_cur_grfconfig->flags, GCF_UNSAFE);
+
+			/* Skip remainder of GRF */
+			_skip_sprites = -1;
+
+			return;
+		}
+	}
+}
+
 /* Action 0x0E */
 static void GRFInhibit(byte *buf, int len)
 {
@@ -3169,8 +3194,8 @@
 {
 	SETBIT(_cur_grfconfig->flags, GCF_UNSAFE);
 
-	/* Skip remainder of GRF if GRF ID is set */
-	if (_cur_grfconfig->grfid != 0) _skip_sprites = -1;
+	/* Skip remainder of GRF */
+	_skip_sprites = -1;
 }
 
 
@@ -3522,13 +3547,13 @@
 		/* 0x05 */ { NULL,     NULL,      NULL,            NULL,       GraphicsNew, },
 		/* 0x06 */ { NULL,     NULL,      NULL,            CfgApply,   CfgApply, },
 		/* 0x07 */ { NULL,     NULL,      NULL,            NULL,       SkipIf, },
-		/* 0x08 */ { ScanInfo, SafeInfo,  NULL,            GRFInfo,    GRFInfo, },
+		/* 0x08 */ { ScanInfo, NULL,      NULL,            GRFInfo,    GRFInfo, },
 		/* 0x09 */ { NULL,     NULL,      NULL,            SkipIf,     SkipIf, },
 		/* 0x0A */ { NULL,     NULL,      NULL,            NULL,       SpriteReplace, },
 		/* 0x0B */ { NULL,     NULL,      NULL,            GRFError,   GRFError, },
 		/* 0x0C */ { NULL,     NULL,      NULL,            GRFComment, GRFComment, },
 		/* 0x0D */ { NULL,     GRFUnsafe, NULL,            ParamSet,   ParamSet, },
-		/* 0x0E */ { NULL,     GRFUnsafe, NULL,            GRFInhibit, GRFInhibit, },
+		/* 0x0E */ { NULL,     SafeGRFInhibit, NULL,       GRFInhibit, GRFInhibit, },
 		/* 0x0F */ { NULL,     NULL,      NULL,            NULL,       NULL, },
 		/* 0x10 */ { NULL,     NULL,      DefineGotoLabel, NULL,       NULL, },
 		/* 0x11 */ { NULL,     GRFUnsafe, NULL,            NULL,       GRFSound, },
--- a/src/newgrf_config.c	Wed Jan 03 09:45:07 2007 +0000
+++ b/src/newgrf_config.c	Wed Jan 03 11:33:54 2007 +0000
@@ -70,14 +70,19 @@
 	/* Find and load the Action 8 information */
 	/* 62 is the last file slot before sample.cat.
 	 * Should perhaps be some "don't care" value */
-	LoadNewGRFFile(config, 62, is_static ? GLS_SAFETYSCAN : GLS_FILESCAN);
-
-	/* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
-	if (HASBIT(config->flags, GCF_UNSAFE)) return false;
+	LoadNewGRFFile(config, 62, GLS_FILESCAN);
 
 	/* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
 	if (config->grfid == 0 || config->grfid == 0xFFFFFFFF) return false;
 
+	if (is_static) {
+		/* Perform a 'safety scan' for static GRFs */
+		LoadNewGRFFile(config, 62, GLS_SAFETYSCAN);
+
+		/* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
+		if (HASBIT(config->flags, GCF_UNSAFE)) return false;
+	}
+
 	return CalcGRFMD5Sum(config);
 }