Darkvater@4220: /* $Id$ */ Darkvater@4220: Darkvater@4220: /** @file fios.c Darkvater@4220: * This file contains functions for building file lists for the save/load dialogs. Darkvater@4220: */ Darkvater@4220: Darkvater@4220: #include "stdafx.h" Darkvater@4220: #include "openttd.h" Darkvater@4220: #include "hal.h" Darkvater@4220: #include "string.h" Darkvater@4220: #include "variables.h" Darkvater@4220: #include "functions.h" truelight@4300: #include "heightmap.h" Darkvater@4220: #include "table/strings.h" Darkvater@4221: #include "fios.h" Darkvater@4220: #include Darkvater@4220: #include Darkvater@4220: Darkvater@4220: #ifdef WIN32 Darkvater@4220: # include Darkvater@4220: #else Darkvater@4220: # include Darkvater@4220: # include Darkvater@4220: #endif /* WIN32 */ Darkvater@4220: Darkvater@4223: /* Variables to display file lists */ Darkvater@4223: int _fios_num; Darkvater@4223: Darkvater@4223: static char *_fios_path; Darkvater@4223: static FiosItem *_fios_items; Darkvater@4223: static int _fios_count, _fios_alloc; Darkvater@4220: Darkvater@4221: /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */ Darkvater@4221: extern bool FiosIsRoot(const char *path); Darkvater@4221: extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb); Darkvater@4221: extern void FiosGetDrives(void); Darkvater@4222: extern bool FiosGetDiskFreeSpace(const char *path, uint32 *tot); Darkvater@4221: Darkvater@4221: /* get the name of an oldstyle savegame */ Darkvater@4221: extern void GetOldSaveGameName(char *title, const char *path, const char *file); Darkvater@4221: Darkvater@4220: /** Darkvater@4220: * Allocate a new FiosItem. Darkvater@4220: * @return A pointer to the newly allocated FiosItem. Darkvater@4220: */ Darkvater@4220: FiosItem *FiosAlloc(void) Darkvater@4220: { Darkvater@4220: if (_fios_count == _fios_alloc) { Darkvater@4220: _fios_alloc += 256; Darkvater@4220: _fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem)); Darkvater@4220: } Darkvater@4220: return &_fios_items[_fios_count++]; Darkvater@4220: } Darkvater@4220: Darkvater@4220: /** Darkvater@4220: * Compare two FiosItem's. Used with qsort when sorting the file list. Darkvater@4220: * @param a A pointer to the first FiosItem to compare. Darkvater@4220: * @param a A pointer to the second FiosItem to compare. Darkvater@4220: * @return -1, 0 or 1, depending on how the two items should be sorted. Darkvater@4220: */ Darkvater@4220: int CDECL compare_FiosItems(const void *a, const void *b) Darkvater@4220: { Darkvater@4220: const FiosItem *da = (const FiosItem *)a; Darkvater@4220: const FiosItem *db = (const FiosItem *)b; Darkvater@4220: int r; Darkvater@4220: Darkvater@4220: if (_savegame_sort_order & SORT_BY_NAME) { Darkvater@4220: r = strcasecmp(da->title, db->title); Darkvater@4220: } else { Darkvater@4220: r = da->mtime < db->mtime ? -1 : 1; Darkvater@4220: } Darkvater@4220: Darkvater@4220: if (_savegame_sort_order & SORT_DESCENDING) r = -r; Darkvater@4220: return r; Darkvater@4220: } Darkvater@4220: Darkvater@4220: /** Darkvater@4220: * Free the list of savegames Darkvater@4220: */ Darkvater@4220: void FiosFreeSavegameList(void) Darkvater@4220: { Darkvater@4220: free(_fios_items); Darkvater@4220: _fios_items = NULL; Darkvater@4220: _fios_alloc = _fios_count = 0; Darkvater@4220: } Darkvater@4220: Darkvater@4222: /** Darkvater@4222: * Get descriptive texts. Returns the path and free space Darkvater@4222: * left on the device Darkvater@4222: * @param path string describing the path Darkvater@4222: * @param total_free total free space in megabytes, optional (can be NULL) Darkvater@4222: * @return StringID describing the path (free space or failure) Darkvater@4222: */ Darkvater@4222: StringID FiosGetDescText(const char **path, uint32 *total_free) Darkvater@4222: { Darkvater@4222: *path = _fios_path; Darkvater@4222: return FiosGetDiskFreeSpace(*path, total_free) ? STR_4005_BYTES_FREE : STR_4006_UNABLE_TO_READ_DRIVE; Darkvater@4222: } Darkvater@4222: Darkvater@4222: /* Browse to a new path based on the passed FiosItem struct Darkvater@4222: * @param *item FiosItem object telling us what to do Darkvater@4222: * @return a string if we have given a file as a target, otherwise NULL */ Darkvater@4222: char *FiosBrowseTo(const FiosItem *item) Darkvater@4222: { Darkvater@4222: char *s; Darkvater@4222: char *path = _fios_path; Darkvater@4222: Darkvater@4222: switch (item->type) { Darkvater@4222: #if defined(WIN32) || defined(__OS2__) Darkvater@4222: case FIOS_TYPE_DRIVE: sprintf(path, "%c:" PATHSEP, item->title[0]); break; Darkvater@4222: #endif Darkvater@4222: Darkvater@4222: case FIOS_TYPE_PARENT: Darkvater@4222: /* Check for possible NULL ptr (not required for UNIXes, but AmigaOS-alikes) */ Darkvater@4222: if ((s = strrchr(path, PATHSEPCHAR)) != NULL) { Darkvater@4222: s[1] = '\0'; // go up a directory Darkvater@4222: if (!FiosIsRoot(path)) s[0] = '\0'; Darkvater@4222: } Darkvater@4222: #if defined(__MORPHOS__) || defined(__AMIGAOS__) Darkvater@4222: /* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */ Darkvater@4222: else if ((s = strrchr(path, ':')) != NULL) s[1] = '\0'; Darkvater@4222: #endif Darkvater@4222: break; Darkvater@4222: Darkvater@4222: case FIOS_TYPE_DIR: Darkvater@4222: if (!FiosIsRoot(path)) strcat(path, PATHSEP); Darkvater@4222: strcat(path, item->name); Darkvater@4222: break; Darkvater@4222: Darkvater@4222: case FIOS_TYPE_DIRECT: Darkvater@4222: sprintf(path, "%s" PATHSEP, item->name); Darkvater@4222: s = strrchr(path, PATHSEPCHAR); Darkvater@4222: if (s != NULL && s[1] == '\0') s[0] = '\0'; // strip trailing slash Darkvater@4222: break; Darkvater@4222: Darkvater@4222: case FIOS_TYPE_FILE: Darkvater@4222: case FIOS_TYPE_OLDFILE: Darkvater@4222: case FIOS_TYPE_SCENARIO: truelight@4300: case FIOS_TYPE_OLD_SCENARIO: truelight@4300: case FIOS_TYPE_PNG: truelight@4300: case FIOS_TYPE_BMP: truelight@4300: { Darkvater@4222: static char str_buffr[512]; Darkvater@4222: Darkvater@4222: #if defined(__MORPHOS__) || defined(__AMIGAOS__) Darkvater@4222: /* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */ Darkvater@4222: if (FiosIsRoot(path)) { Darkvater@4222: snprintf(str_buffr, lengthof(str_buffr), "%s:%s", path, item->name); Darkvater@4222: } else // XXX - only next line! Darkvater@4222: #endif Darkvater@4222: snprintf(str_buffr, lengthof(str_buffr), "%s" PATHSEP "%s", path, item->name); Darkvater@4222: Darkvater@4222: return str_buffr; Darkvater@4222: } Darkvater@4222: } Darkvater@4222: Darkvater@4222: return NULL; Darkvater@4222: } Darkvater@4222: Darkvater@4220: void FiosMakeSavegameName(char *buf, const char *name, size_t size) Darkvater@4220: { Darkvater@4220: const char *extension, *period; Darkvater@4220: Darkvater@4220: extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav"; Darkvater@4220: Darkvater@4220: /* Don't append the extension if it is already there */ Darkvater@4220: period = strrchr(name, '.'); Darkvater@4220: if (period != NULL && strcasecmp(period, extension) == 0) extension = ""; Darkvater@4220: Darkvater@4220: snprintf(buf, size, "%s" PATHSEP "%s%s", _fios_path, name, extension); Darkvater@4220: } Darkvater@4220: Darkvater@5167: #if defined(WIN32) || defined(WIN64) Darkvater@5167: # define unlink _wunlink Darkvater@5167: #endif Darkvater@5167: Darkvater@4220: bool FiosDelete(const char *name) Darkvater@4220: { Darkvater@4220: char filename[512]; Darkvater@4220: Darkvater@4220: FiosMakeSavegameName(filename, name, lengthof(filename)); Darkvater@5167: return unlink(OTTD2FS(filename)) == 0; Darkvater@4220: } Darkvater@4220: Darkvater@4220: bool FileExists(const char *filename) Darkvater@4220: { Darkvater@4220: return access(filename, 0) == 0; Darkvater@4220: } Darkvater@4221: Darkvater@4221: typedef byte fios_getlist_callback_proc(int mode, const char *filename, const char *ext, char *title); Darkvater@4221: Darkvater@4221: /** Create a list of the files in a directory, according to some arbitrary rule. Darkvater@4221: * @param num Will be filled with the amount of items. Darkvater@4221: * @param mode The mode we are in. Some modes don't allow 'parent'. Darkvater@4221: * @param callback The function that is called where you need to do the filtering. Darkvater@4221: * @return Return the list of files. */ Darkvater@4223: static FiosItem *FiosGetFileList(int mode, fios_getlist_callback_proc *callback_proc) Darkvater@4221: { Darkvater@4221: struct stat sb; Darkvater@4221: struct dirent *dirent; Darkvater@4221: DIR *dir; Darkvater@4221: FiosItem *fios; Darkvater@4221: int sort_start; Darkvater@4221: Darkvater@4221: /* A parent directory link exists if we are not in the root directory */ Darkvater@4221: if (!FiosIsRoot(_fios_path) && mode != SLD_NEW_GAME) { Darkvater@4221: fios = FiosAlloc(); Darkvater@4221: fios->type = FIOS_TYPE_PARENT; Darkvater@4221: fios->mtime = 0; Darkvater@4221: ttd_strlcpy(fios->name, "..", lengthof(fios->name)); Darkvater@4221: ttd_strlcpy(fios->title, ".. (Parent directory)", lengthof(fios->title)); Darkvater@4221: } Darkvater@4221: Darkvater@4221: /* Show subdirectories */ Darkvater@4221: if (mode != SLD_NEW_GAME && (dir = opendir(_fios_path)) != NULL) { Darkvater@4221: while ((dirent = readdir(dir)) != NULL) { Darkvater@5167: const char *d_name = FS2OTTD(dirent->d_name); Darkvater@5167: Darkvater@4221: /* found file must be directory, but not '.' or '..' */ Darkvater@4221: if (FiosIsValidFile(_fios_path, dirent, &sb) && (sb.st_mode & S_IFDIR) && Darkvater@5167: strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) { Darkvater@4221: fios = FiosAlloc(); Darkvater@4221: fios->type = FIOS_TYPE_DIR; Darkvater@4221: fios->mtime = 0; Darkvater@5167: ttd_strlcpy(fios->name, d_name, lengthof(fios->name)); Darkvater@5167: snprintf(fios->title, lengthof(fios->title), "%s" PATHSEP " (Directory)", d_name); Darkvater@4221: str_validate(fios->title); Darkvater@4221: } Darkvater@4221: } Darkvater@4221: closedir(dir); Darkvater@4221: } Darkvater@4221: Darkvater@4221: /* Sort the subdirs always by name, ascending, remember user-sorting order */ Darkvater@4221: { Darkvater@4221: byte order = _savegame_sort_order; Darkvater@4221: _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING; Darkvater@4221: qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems); Darkvater@4221: _savegame_sort_order = order; Darkvater@4221: } Darkvater@4221: Darkvater@4221: /* This is where to start sorting for the filenames */ Darkvater@4221: sort_start = _fios_count; Darkvater@4221: Darkvater@4221: /* Show files */ Darkvater@4221: dir = opendir(_fios_path); Darkvater@4221: if (dir != NULL) { Darkvater@4221: while ((dirent = readdir(dir)) != NULL) { Darkvater@4221: char fios_title[64]; Darkvater@4221: char *t; Darkvater@5167: char *d_name = (char*)FS2OTTD(dirent->d_name); Darkvater@4221: byte type; Darkvater@4221: Darkvater@4221: if (!FiosIsValidFile(_fios_path, dirent, &sb) || !(sb.st_mode & S_IFREG)) continue; Darkvater@4221: Darkvater@4221: /* File has no extension, skip it */ Darkvater@5167: if ((t = strrchr(d_name, '.')) == NULL) continue; Darkvater@4221: fios_title[0] = '\0'; // reset the title; Darkvater@4221: Darkvater@5167: type = callback_proc(mode, d_name, t, fios_title); Darkvater@4221: if (type != FIOS_TYPE_INVALID) { Darkvater@4221: fios = FiosAlloc(); Darkvater@4221: fios->mtime = sb.st_mtime; Darkvater@4221: fios->type = type; Darkvater@5167: ttd_strlcpy(fios->name, d_name, lengthof(fios->name)); Darkvater@4221: Darkvater@4221: /* Some callbacks want to lookup the title of the file. Allow that. Darkvater@4221: * If we just copy the title from the filename, strip the extension */ Darkvater@5167: t = (fios_title[0] == '\0') ? *t = '\0', d_name : fios_title; Darkvater@5167: ttd_strlcpy(fios->title, t, lengthof(fios->title)); Darkvater@4221: str_validate(fios->title); Darkvater@4221: } Darkvater@4221: } Darkvater@4221: closedir(dir); Darkvater@4221: } Darkvater@4221: Darkvater@4221: qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); Darkvater@4221: Darkvater@4221: /* Show drives */ Darkvater@4221: if (mode != SLD_NEW_GAME) FiosGetDrives(); Darkvater@4221: Darkvater@4223: _fios_num = _fios_count; Darkvater@4221: return _fios_items; Darkvater@4221: } Darkvater@4221: Darkvater@4221: /** Darkvater@4221: * Callback for FiosGetFileList. It tells if a file is a savegame or not. Darkvater@4221: * @param mode Save/load mode. Darkvater@4221: * @param file Name of the file to check. Darkvater@4221: * @param ext A pointer to the extension identifier inside file Darkvater@4221: * @param title Buffer if a callback wants to lookup the title of the file Darkvater@4221: * @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a savegame Darkvater@4221: * @see FiosGetFileList Darkvater@4221: * @see FiosGetSavegameList Darkvater@4221: */ Darkvater@4221: static byte FiosGetSavegameListCallback(int mode, const char *file, const char *ext, char *title) Darkvater@4221: { Darkvater@4221: /* Show savegame files Darkvater@4221: * .SAV OpenTTD saved game Darkvater@4221: * .SS1 Transport Tycoon Deluxe preset game Darkvater@4221: * .SV1 Transport Tycoon Deluxe (Patch) saved game Darkvater@4221: * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */ Darkvater@4221: if (strcasecmp(ext, ".sav") == 0) return FIOS_TYPE_FILE; Darkvater@4221: Darkvater@4221: if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { Darkvater@4221: if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 || Darkvater@4221: strcasecmp(ext, ".sv2") == 0) { Darkvater@4221: GetOldSaveGameName(title, _fios_path, file); Darkvater@4221: return FIOS_TYPE_OLDFILE; Darkvater@4221: } Darkvater@4221: } Darkvater@4221: Darkvater@4221: return FIOS_TYPE_INVALID; Darkvater@4221: } Darkvater@4221: Darkvater@4221: /** Darkvater@4221: * Get a list of savegames. Darkvater@4221: * @param mode Save/load mode. Darkvater@4221: * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog. Darkvater@4221: * @see FiosGetFileList Darkvater@4221: */ Darkvater@4223: FiosItem *FiosGetSavegameList(int mode) Darkvater@4221: { Darkvater@4221: static char *_fios_save_path = NULL; Darkvater@4221: Darkvater@4221: if (_fios_save_path == NULL) { Darkvater@4221: _fios_save_path = malloc(MAX_PATH); Darkvater@5296: ttd_strlcpy(_fios_save_path, _paths.save_dir, MAX_PATH); Darkvater@4221: } Darkvater@4221: Darkvater@4221: _fios_path = _fios_save_path; Darkvater@4221: Darkvater@4223: return FiosGetFileList(mode, &FiosGetSavegameListCallback); Darkvater@4221: } Darkvater@4221: Darkvater@4221: /** Darkvater@4221: * Callback for FiosGetFileList. It tells if a file is a scenario or not. Darkvater@4221: * @param mode Save/load mode. Darkvater@4221: * @param file Name of the file to check. Darkvater@4221: * @param ext A pointer to the extension identifier inside file Darkvater@4221: * @param title Buffer if a callback wants to lookup the title of the file Darkvater@4221: * @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a scenario Darkvater@4221: * @see FiosGetFileList Darkvater@4221: * @see FiosGetScenarioList Darkvater@4221: */ Darkvater@4221: static byte FiosGetScenarioListCallback(int mode, const char *file, const char *ext, char *title) Darkvater@4221: { Darkvater@4221: /* Show scenario files Darkvater@4221: * .SCN OpenTTD style scenario file Darkvater@4221: * .SV0 Transport Tycoon Deluxe (Patch) scenario Darkvater@4221: * .SS0 Transport Tycoon Deluxe preset scenario */ Darkvater@4221: if (strcasecmp(ext, ".scn") == 0) return FIOS_TYPE_SCENARIO; Darkvater@4221: Darkvater@4221: if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || mode == SLD_NEW_GAME) { Darkvater@4221: if (strcasecmp(ext, ".sv0") == 0 || strcasecmp(ext, ".ss0") == 0 ) { Darkvater@4221: GetOldSaveGameName(title, _fios_path, file); Darkvater@4221: return FIOS_TYPE_OLD_SCENARIO; Darkvater@4221: } Darkvater@4221: } Darkvater@4221: Darkvater@4221: return FIOS_TYPE_INVALID; Darkvater@4221: } Darkvater@4221: Darkvater@4221: /** Darkvater@4221: * Get a list of scenarios. Darkvater@4221: * @param mode Save/load mode. Darkvater@4221: * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog. Darkvater@4221: * @see FiosGetFileList Darkvater@4221: */ Darkvater@4223: FiosItem *FiosGetScenarioList(int mode) Darkvater@4221: { truelight@4300: static char *_fios_scn_path = NULL; Darkvater@4221: truelight@4300: if (_fios_scn_path == NULL) { truelight@4300: _fios_scn_path = malloc(MAX_PATH); Darkvater@5296: ttd_strlcpy(_fios_scn_path, _paths.scenario_dir, MAX_PATH); Darkvater@4221: } Darkvater@4221: truelight@4300: _fios_path = _fios_scn_path; Darkvater@4221: Darkvater@4223: return FiosGetFileList(mode, &FiosGetScenarioListCallback); Darkvater@4221: } truelight@4300: truelight@4300: static byte FiosGetHeightmapListCallback(int mode, const char *file, const char *ext, char *title) truelight@4300: { truelight@4300: /* Show heightmap files truelight@4300: * .PNG PNG Based heightmap files truelight@4300: * .BMP BMP Based heightmap files truelight@4300: */ truelight@4300: truelight@4300: #ifdef WITH_PNG truelight@4300: if (strcasecmp(ext, ".png") == 0) return FIOS_TYPE_PNG; truelight@4300: #endif /* WITH_PNG */ truelight@4300: truelight@4300: if (strcasecmp(ext, ".bmp") == 0) return FIOS_TYPE_BMP; truelight@4300: truelight@4300: return FIOS_TYPE_INVALID; truelight@4300: } truelight@4300: truelight@4300: // Get a list of Heightmaps truelight@4300: FiosItem *FiosGetHeightmapList(int mode) truelight@4300: { truelight@4300: static char *_fios_hmap_path = NULL; truelight@4300: truelight@4300: if (_fios_hmap_path == NULL) { truelight@4300: _fios_hmap_path = malloc(MAX_PATH); Darkvater@5296: strcpy(_fios_hmap_path, _paths.heightmap_dir); truelight@4300: } truelight@4300: truelight@4300: _fios_path = _fios_hmap_path; truelight@4300: truelight@4300: return FiosGetFileList(mode, &FiosGetHeightmapListCallback); truelight@4300: }