(svn r11391) -Change: print all error messages related to missing/corrupted files in one message including the location where they can get said missing/corrupted files.
authorrubidium
Wed, 07 Nov 2007 23:29:43 +0000
changeset 7841 8f9ab4f7064b
parent 7840 c5c3e59e7fef
child 7842 08babe3cf58a
(svn r11391) -Change: print all error messages related to missing/corrupted files in one message including the location where they can get said missing/corrupted files.
bin/data/dosdummy.grf
src/gfxinit.cpp
src/table/files.h
Binary file bin/data/dosdummy.grf has changed
--- a/src/gfxinit.cpp	Wed Nov 07 22:41:50 2007 +0000
+++ b/src/gfxinit.cpp	Wed Nov 07 23:29:43 2007 +0000
@@ -25,7 +25,7 @@
 };
 
 struct FileList {
-	MD5File basic[4];          ///< grf files that always have to be loaded
+	MD5File basic[2];          ///< grf files that always have to be loaded
 	MD5File landscape[3];      ///< landscape specific grf files
 };
 
@@ -53,7 +53,7 @@
 };
 
 
-static uint LoadGrfFile(const char* filename, uint load_index, int file_index)
+static uint LoadGrfFile(const char *filename, uint load_index, int file_index)
 {
 	uint load_index_org = load_index;
 	uint sprite_id = 0;
@@ -110,17 +110,12 @@
 }
 
 
-/* Check that the supplied MD5 hash matches that stored for the supplied filename */
-static bool CheckMD5Digest(const MD5File file, md5_byte_t *digest, bool warn)
-{
-	if (memcmp(file.hash, digest, sizeof(file.hash)) == 0) return true;
-	if (warn) fprintf(stderr, "MD5 of %s is ****INCORRECT**** - File Corrupt.\n", file.filename);
-	return false;
-}
-
-/* Calculate and check the MD5 hash of the supplied filename.
- * returns true if the checksum is correct */
-static bool FileMD5(const MD5File file, bool warn)
+/**
+ * Calculate and check the MD5 hash of the supplied filename.
+ * @param file filename and expected MD5 hash for the given filename.
+ * @return true if the checksum is correct.
+ */
+static bool FileMD5(const MD5File file)
 {
 	size_t size;
 	FILE *f = FioFOpenFile(file.filename, "rb", DATA_DIR, &size);
@@ -137,53 +132,38 @@
 			md5_append(&filemd5state, buffer, len);
 		}
 
-		if (ferror(f) && warn) ShowInfoF("Error Reading from %s \n", file.filename);
 		FioFCloseFile(f);
 
 		md5_finish(&filemd5state, digest);
-		return CheckMD5Digest(file, digest, warn);
+		return memcmp(file.hash, digest, sizeof(file.hash)) == 0;
 	} else { // file not found
 		return false;
 	}
 }
 
-/* Checks, if either the Windows files exist (TRG1R.GRF) or the DOS files (TRG1.GRF)
- * by comparing the MD5 checksums of the files. _use_dos_palette is set accordingly.
- * If neither are found, Windows palette is assumed.
- *
- * (Note: Also checks sample.cat for corruption) */
-void CheckExternalFiles()
+/**
+ * Determine the palette that has to be used.
+ *  - forced DOS palette via command line -> leave it that way
+ *  - all Windows files present -> Windows palette
+ *  - all DOS files present -> DOS palette
+ *  - no Windows files present and any DOS file present -> DOS palette
+ *  - otherwise -> Windows palette
+ */
+static void DeterminePalette()
 {
-	uint i;
-	/* count of files from this version */
+	if (_use_dos_palette) return;
+
+	/* Count of files from the different versions. */
 	uint dos = 0;
 	uint win = 0;
 
-	for (i = 0; i < 2; i++) if (FileMD5(files_dos.basic[i], true)) dos++;
-	for (i = 0; i < 3; i++) if (FileMD5(files_dos.landscape[i], true)) dos++;
-
-	for (i = 0; i < 2; i++) if (FileMD5(files_win.basic[i], true)) win++;
-	for (i = 0; i < 3; i++) if (FileMD5(files_win.landscape[i], true)) win++;
-
-	if (!FileMD5(sample_cat_win, false) && !FileMD5(sample_cat_dos, false))
-		ShowInfo("Your 'sample.cat' file is corrupted or missing!");
+	for (uint i = 0; i < lengthof(files_dos.basic); i++) if (FioCheckFileExists(files_dos.basic[i].filename)) dos++;
+	for (uint i = 0; i < lengthof(files_dos.landscape); i++) if (FioCheckFileExists(files_dos.landscape[i].filename)) dos++;
 
-	for (i = 0; i < lengthof(files_openttd); i++) {
-		if (!FileMD5(files_openttd[i], false)) {
-			ShowInfoF("Your '%s' file is corrupted or missing!", files_openttd[i].filename);
-		}
-	}
+	for (uint i = 0; i < lengthof(files_win.basic); i++) if (FioCheckFileExists(files_win.basic[i].filename)) win++;
+	for (uint i = 0; i < lengthof(files_win.landscape); i++) if (FioCheckFileExists(files_win.landscape[i].filename)) win++;
 
-	/*
-	 * forced DOS palette via command line -> leave it that way
-	 * all Windows files present -> Windows palette
-	 * all DOS files present -> DOS palette
-	 * no Windows files present and any DOS file present -> DOS palette
-	 * otherwise -> Windows palette
-	 */
-	if (_use_dos_palette) {
-		return;
-	} else if (win == 5) {
+	if (win == 5) {
 		_use_dos_palette = false;
 	} else if (dos == 5 || (win == 0 && dos > 0)) {
 		_use_dos_palette = true;
@@ -192,6 +172,46 @@
 	}
 }
 
+/**
+ * Checks whether the MD5 checksums of the files are correct.
+ *
+ * @note Also checks sample.cat and other required non-NewGRF GRFs for corruption.
+ */
+void CheckExternalFiles()
+{
+	DeterminePalette();
+
+	static const size_t ERROR_MESSAGE_LENGTH = 128;
+	const FileList *files = _use_dos_palette ? &files_dos : &files_win;
+	char error_msg[ERROR_MESSAGE_LENGTH * (lengthof(files->basic) + lengthof(files->landscape) + lengthof(files_openttd) + 1)];
+	error_msg[0] = '\0';
+	char *add_pos = error_msg;
+
+	for (uint i = 0; i < lengthof(files->basic); i++) {
+		if (!FileMD5(files->basic[i])) {
+			add_pos += snprintf(add_pos, ERROR_MESSAGE_LENGTH, "Your '%s' file is corrupted or missing! You can find '%s' on your Transport Tycoon Deluxe CD-ROM.\n", files->basic[i].filename, files->basic[i].filename);
+		}
+	}
+
+	for (uint i = 0; i < lengthof(files->landscape); i++) {
+		if (!FileMD5(files->landscape[i])) {
+			add_pos += snprintf(add_pos, ERROR_MESSAGE_LENGTH, "Your '%s' file is corrupted or missing! You can find '%s' on your Transport Tycoon Deluxe CD-ROM.\n", files->landscape[i].filename, files->landscape[i].filename);
+		}
+	}
+
+	if (!FileMD5(sample_cat_win) && !FileMD5(sample_cat_dos)) {
+		add_pos += snprintf(add_pos, ERROR_MESSAGE_LENGTH, "Your 'sample.cat' file is corrupted or missing! You can find 'sample.cat' on your Transport Tycoon Deluxe CD-ROM.\n");
+	}
+
+	for (uint i = 0; i < lengthof(files_openttd); i++) {
+		if (!FileMD5(files_openttd[i])) {
+			add_pos += snprintf(add_pos, ERROR_MESSAGE_LENGTH, "Your '%s' file is corrupted or missing! The file was part of your installation.\n", files_openttd[i].filename);
+		}
+	}
+
+	if (add_pos != error_msg) ShowInfoF(error_msg);
+}
+
 
 static const SpriteID trg1idx[] = {
 	   0,    1, ///< Mouse cursor, ZZZ
@@ -332,7 +352,7 @@
 
 static void LoadSpriteTables()
 {
-	const FileList* files = _use_dos_palette ? &files_dos : &files_win;
+	const FileList *files = _use_dos_palette ? &files_dos : &files_win;
 	uint load_index;
 	uint i = FIRST_GRF_SLOT;
 
@@ -340,13 +360,20 @@
 	DupSprite(  2, 130); // non-breaking space medium
 	DupSprite(226, 354); // non-breaking space tiny
 	DupSprite(450, 578); // non-breaking space large
-	load_index = 4793;
 
-	for (uint j = 1; files->basic[j].filename != NULL; j++) {
-		load_index += LoadGrfFile(files->basic[j].filename, load_index, i++);
-	}
+	/*
+	 * The second basic file always starts at the given location and does
+	 * contain a different amount of sprites depending on the "type"; DOS
+	 * has a few sprites less. However, we do not care about those missing
+	 * sprites as they are not shown anyway (logos in intro game).
+	 */
+	LoadGrfFile(files->basic[1].filename, 4793, i++);
 
-	/* Load additional sprites for climates other than temperate */
+	/*
+	 * Load additional sprites for climates other than temperate.
+	 * This overwrites some of the temperate sprites, such as foundations
+	 * and the ground sprites.
+	 */
 	if (_opt.landscape != LT_TEMPERATE) {
 		LoadGrfIndexed(
 			files->landscape[_opt.landscape - 1].filename,
@@ -355,7 +382,8 @@
 		);
 	}
 
-	assert(load_index == SPR_SIGNALS_BASE);
+	/* Start loading the extra, non-TTD, base GRFs for the given index. */
+	load_index = SPR_SIGNALS_BASE;
 	load_index += LoadGrfFile("nsignalsw.grf", load_index, i++);
 
 	assert(load_index == SPR_CANALS_BASE);
--- a/src/table/files.h	Wed Nov 07 22:41:50 2007 +0000
+++ b/src/table/files.h	Wed Nov 07 23:29:43 2007 +0000
@@ -26,9 +26,7 @@
 static FileList files_dos = {
 	{
 		{ "TRG1.GRF",      {0x93, 0x11, 0x67, 0x62, 0x80, 0xe5, 0xb1, 0x40, 0x77, 0xa8, 0xee, 0x41, 0xc1, 0xb4, 0x21, 0x92} }, //    0 - 4792 inclusive
-		{ "TRGI.GRF",      {0xda, 0x6a, 0x6c, 0x9d, 0xcc, 0x45, 0x1e, 0xec, 0x88, 0xd7, 0x92, 0x11, 0x43, 0x7b, 0x76, 0xa8} }, // 4793 - 4889 inclusive
-		{ "dosdummy.grf",  {0x07, 0x01, 0xe6, 0xc4, 0x07, 0x6a, 0x5b, 0xc3, 0xf4, 0x9f, 0x01, 0xad, 0x21, 0x6c, 0xa0, 0xc2} }, // 4890 - 4895 inclusive
-		{ NULL, { 0 } }
+		{ "TRGI.GRF",      {0xda, 0x6a, 0x6c, 0x9d, 0xcc, 0x45, 0x1e, 0xec, 0x88, 0xd7, 0x92, 0x11, 0x43, 0x7b, 0x76, 0xa8} }  // 4793 - 4889 inclusive
 	}, {
 		{ "TRGC.GRF",      {0xed, 0x44, 0x66, 0x37, 0xe0, 0x34, 0x10, 0x4c, 0x55, 0x59, 0xb3, 0x2c, 0x18, 0xaf, 0xe7, 0x8d} },
 		{ "TRGH.GRF",      {0xee, 0x66, 0x16, 0xfb, 0x0e, 0x6e, 0xf6, 0xb2, 0x48, 0x92, 0xc5, 0x8c, 0x93, 0xd8, 0x6f, 0xc9} },
@@ -39,9 +37,7 @@
 static FileList files_win = {
 	{
 		{ "TRG1R.GRF",     {0xb0, 0x4c, 0xe5, 0x93, 0xd8, 0xc5, 0x01, 0x6e, 0x07, 0x47, 0x3a, 0x74, 0x3d, 0x7d, 0x33, 0x58} }, //    0 - 4792 inclusive
-		{ "TRGIR.GRF",     {0x0c, 0x24, 0x84, 0xff, 0x6b, 0xe4, 0x9f, 0xc6, 0x3a, 0x83, 0xbe, 0x6a, 0xb5, 0xc3, 0x8f, 0x32} }, // 4793 - 4895 inclusive
-		{ NULL, { 0 } },
-		{ NULL, { 0 } }
+		{ "TRGIR.GRF",     {0x0c, 0x24, 0x84, 0xff, 0x6b, 0xe4, 0x9f, 0xc6, 0x3a, 0x83, 0xbe, 0x6a, 0xb5, 0xc3, 0x8f, 0x32} }  // 4793 - 4895 inclusive
 	}, {
 		{ "TRGCR.GRF",     {0x36, 0x68, 0xf4, 0x10, 0xc7, 0x61, 0xa0, 0x50, 0xb5, 0xe7, 0x09, 0x5a, 0x2b, 0x14, 0x87, 0x9b} },
 		{ "TRGHR.GRF",     {0x06, 0xbf, 0x2b, 0x7a, 0x31, 0x76, 0x6f, 0x04, 0x8b, 0xaa, 0xc2, 0xeb, 0xe4, 0x34, 0x57, 0xb1} },