changeset 11266:47b87f7aa945 draft

(svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory. -Change [FS#2692]: make it more likely that scenarios are saved to the directory where openttd.cfg is and not the installation directory (gives problems with e.g. Vista). The working directory still overrides the openttd.cfg directory though, but only if it has a scenario directory.
author rubidium <rubidium@openttd.org>
date Wed, 04 Mar 2009 23:12:15 +0000
parents d56f68646d3e
children 1d456bf484a1
files src/fileio.cpp src/fios.cpp
diffstat 2 files changed, 53 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/fileio.cpp
+++ b/src/fileio.cpp
@@ -941,7 +941,7 @@
 #endif
 
 	static const Subdirectory default_subdirs[] = {
-		SAVE_DIR, AUTOSAVE_DIR
+		SAVE_DIR, AUTOSAVE_DIR, SCENARIO_DIR, HEIGHTMAP_DIR
 	};
 
 	for (uint i = 0; i < lengthof(default_subdirs); i++) {
--- a/src/fios.cpp
+++ b/src/fios.cpp
@@ -256,8 +256,9 @@
 /** Fill the list of the files in a directory, according to some arbitrary rule.
  *  @param mode The mode we are in. Some modes don't allow 'parent'.
  *  @param callback_proc The function that is called where you need to do the filtering.
+ *  @param subdir The directory from where to start (global) searching.
  */
-static void FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc *callback_proc)
+static void FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc *callback_proc, Subdirectory subdir)
 {
 	struct stat sb;
 	struct dirent *dirent;
@@ -310,7 +311,11 @@
 
 	/* Show files */
 	FiosFileScanner scanner(mode, callback_proc);
-	scanner.Scan(NULL, _fios_path, false);
+	if (subdir == NO_DIRECTORY) {
+		scanner.Scan(NULL, _fios_path, false);
+	} else {
+		scanner.Scan(NULL, subdir, true, true);
+	}
 
 	qsort(_fios_items.Get(sort_start), _fios_items.Length() - sort_start, sizeof(FiosItem), compare_FiosItems);
 
@@ -321,6 +326,27 @@
 }
 
 /**
+ * Get the title of a file, which (if exists) is stored in a file named
+ * the same as the data file but with '.title' added to it.
+ * @param file filename to get the title for
+ * @param title the title buffer to fill
+ * @param last the last element in the title buffer
+ */
+static void GetFileTitle(const char *file, char *title, const char *last)
+{
+	char buf[MAX_PATH];
+	strecpy(buf, file, lastof(buf));
+	strecat(buf, ".title", lastof(buf));
+
+	FILE *f = FioFOpenFile(buf, "r");
+	if (f == NULL) return;
+
+	size_t read = fread(title, 1, last - title, f);
+	assert(title + read <= last);
+	title[read] = '\0';
+}
+
+/**
  * Callback for FiosGetFileList. It tells if a file is a savegame or not.
  * @param mode Save/load mode.
  * @param file Name of the file to check.
@@ -338,7 +364,10 @@
 	 * .SS1 Transport Tycoon Deluxe preset game
 	 * .SV1 Transport Tycoon Deluxe (Patch) saved game
 	 * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
-	if (strcasecmp(ext, ".sav") == 0) return FIOS_TYPE_FILE;
+	if (strcasecmp(ext, ".sav") == 0) {
+		GetFileTitle(file, title, last);
+		return FIOS_TYPE_FILE;
+	}
 
 	if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
 		if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 ||
@@ -368,7 +397,7 @@
 
 	_fios_path = fios_save_path;
 
-	FiosGetFileList(mode, &FiosGetSavegameListCallback);
+	FiosGetFileList(mode, &FiosGetSavegameListCallback, NO_DIRECTORY);
 }
 
 /**
@@ -388,7 +417,10 @@
 	 * .SCN OpenTTD style scenario file
 	 * .SV0 Transport Tycoon Deluxe (Patch) scenario
 	 * .SS0 Transport Tycoon Deluxe preset scenario */
-	if (strcasecmp(ext, ".scn") == 0) return FIOS_TYPE_SCENARIO;
+	if (strcasecmp(ext, ".scn") == 0) {
+		GetFileTitle(file, title, last);
+		return FIOS_TYPE_SCENARIO;
+	}
 
 	if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || mode == SLD_NEW_GAME) {
 		if (strcasecmp(ext, ".sv0") == 0 || strcasecmp(ext, ".ss0") == 0 ) {
@@ -418,7 +450,10 @@
 
 	_fios_path = fios_scn_path;
 
-	FiosGetFileList(mode, &FiosGetScenarioListCallback);
+	char base_path[MAX_PATH];
+	FioGetDirectory(base_path, sizeof(base_path), SCENARIO_DIR);
+
+	FiosGetFileList(mode, &FiosGetScenarioListCallback, (mode == SLD_LOAD_SCENARIO && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY);
 }
 
 static FiosType FiosGetHeightmapListCallback(SaveLoadDialogMode mode, const char *file, const char *ext, char *title, const char *last)
@@ -428,13 +463,17 @@
 	 * .BMP BMP Based heightmap files
 	 */
 
+	FiosType type = FIOS_TYPE_INVALID;
+
 #ifdef WITH_PNG
-	if (strcasecmp(ext, ".png") == 0) return FIOS_TYPE_PNG;
+	if (strcasecmp(ext, ".png") == 0) type = FIOS_TYPE_PNG;
 #endif /* WITH_PNG */
 
-	if (strcasecmp(ext, ".bmp") == 0) return FIOS_TYPE_BMP;
+	if (strcasecmp(ext, ".bmp") == 0) type = FIOS_TYPE_BMP;
 
-	return FIOS_TYPE_INVALID;
+	if (type != FIOS_TYPE_INVALID) GetFileTitle(file, title, last);
+
+	return type;
 }
 
 /* Get a list of Heightmaps */
@@ -449,5 +488,8 @@
 
 	_fios_path = fios_hmap_path;
 
-	FiosGetFileList(mode, &FiosGetHeightmapListCallback);
+	char base_path[MAX_PATH];
+	FioGetDirectory(base_path, sizeof(base_path), HEIGHTMAP_DIR);
+
+	FiosGetFileList(mode, &FiosGetHeightmapListCallback, strcmp(base_path, _fios_path) == 0 ? HEIGHTMAP_DIR : NO_DIRECTORY);
 }