summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordominik <dominik@openttd.org>2005-01-28 08:56:43 +0000
committerdominik <dominik@openttd.org>2005-01-28 08:56:43 +0000
commitecd8952ef973a22f7636e062cbc22fd2d6b5fc8a (patch)
tree5d9b756fe3bff49b8276debff76e632b693e8430
parentdba55a574210406109408d2069c41cc783f9a519 (diff)
downloadopenttd-ecd8952ef973a22f7636e062cbc22fd2d6b5fc8a.tar.xz
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
-rw-r--r--fileio.c36
-rw-r--r--fileio.h1
-rw-r--r--spritecache.c21
3 files changed, 46 insertions, 12 deletions
diff --git a/fileio.c b/fileio.c
index c9fee6ba4..4b6fee1ce 100644
--- a/fileio.c
+++ b/fileio.c
@@ -100,6 +100,42 @@ void FioCloseAll(void)
FioCloseFile(i);
}
+bool FiosCheckFileExists(const char *filename)
+{
+ FILE *f;
+ char buf[MAX_PATH];
+
+ sprintf(buf, "%s%s", _path.data_dir, filename);
+
+ f = fopen(buf, "rb");
+#if !defined(WIN32)
+ if (f == NULL) {
+ char *s;
+ // Make lower case and try again
+ for(s=buf + strlen(_path.data_dir) - 1; *s != 0; s++)
+ *s = tolower(*s);
+ f = fopen(buf, "rb");
+
+#if defined SECOND_DATA_DIR
+ // tries in the 2nd data directory
+ if (f == NULL) {
+ sprintf(buf, "%s%s", _path.second_data_dir, filename);
+ for(s=buf + strlen(_path.second_data_dir) - 1; *s != 0; s++)
+ *s = tolower(*s);
+ f = fopen(buf, "rb");
+ }
+#endif
+ }
+#endif
+
+ if (f == NULL)
+ return false;
+ else {
+ fclose(f);
+ return true;
+ }
+}
+
void FioOpenFile(int slot, const char *filename)
{
FILE *f;
diff --git a/fileio.h b/fileio.h
index c03fc9b01..7b67aa9d7 100644
--- a/fileio.h
+++ b/fileio.h
@@ -11,5 +11,6 @@ void FioCloseAll(void);
void FioOpenFile(int slot, const char *filename);
void FioReadBlock(void *ptr, uint size);
void FioSkipBytes(int n);
+bool FiosCheckFileExists(const char *filename);
#endif /* FILEIO_H */
diff --git a/spritecache.c b/spritecache.c
index 2cec837fc..85a09e940 100644
--- a/spritecache.c
+++ b/spritecache.c
@@ -893,21 +893,18 @@ static void LoadSpriteTables(void)
/* Load newgrf sprites */
-
+ // in each loading stage, (try to) open each file specified in the config and load information from it.
_custom_sprites_base = load_index;
-
- _loading_stage = 0;
- for (j = 0; j != lengthof(_newgrf_files) && _newgrf_files[j]; j++) {
- InitNewGRFFile(_newgrf_files[j], load_index);
- load_index += LoadNewGrfFile(_newgrf_files[j], load_index, i++);
+ for (_loading_stage = 0; _loading_stage < 2; _loading_stage++) {
+ for (j = 0; j != lengthof(_newgrf_files) && _newgrf_files[j]; j++) {
+ if ( !FiosCheckFileExists(_newgrf_files[j]) )
+ continue;
+ if (_loading_stage == 0)
+ InitNewGRFFile(_newgrf_files[j], load_index);
+ load_index += LoadNewGrfFile(_newgrf_files[j], load_index, i++);
+ }
}
- _loading_stage = 1;
- load_index = _custom_sprites_base;
- for (j = 0; j != lengthof(_newgrf_files) && _newgrf_files[j]; j++)
- load_index += LoadNewGrfFile(_newgrf_files[j], load_index, i++);
-
-
// If needed, save the cache to file
HandleCachedSpriteHeaders(_cached_filenames[_opt.landscape], false);
} else {