1
0
Fork 0

(svn r27647) -Codechange: Introduce file operations, and use it to replace most of SaveLoadDialogMode

release/1.7
alberth 2016-09-04 12:56:23 +00:00
parent 2d7201781b
commit 75ad6d0100
6 changed files with 80 additions and 51 deletions

View File

@ -67,7 +67,7 @@ public:
void ValidateFileList(bool force_reload = false) void ValidateFileList(bool force_reload = false)
{ {
if (force_reload || !this->file_list_valid) { if (force_reload || !this->file_list_valid) {
this->BuildFileList(SLD_LOAD_GAME); this->BuildFileList(FT_SAVEGAME, FOP_LOAD);
this->file_list_valid = true; this->file_list_valid = true;
} }
} }

View File

@ -24,6 +24,12 @@ enum AbstractFileType {
FT_INVALID = 7, ///< Invalid or unknown file type. FT_INVALID = 7, ///< Invalid or unknown file type.
}; };
/** Operation performed on the file. */
enum FileOperation {
FOP_LOAD, ///< File is being loaded.
FOP_SAVE, ///< File is being saved.
};
/** Elements of a file system that are recognized. */ /** Elements of a file system that are recognized. */
enum FiosType { enum FiosType {
FIOS_TYPE_DRIVE, FIOS_TYPE_DRIVE,

View File

@ -69,22 +69,33 @@ FileList::~FileList()
} }
/** /**
* Construct a file list containing file appropriate for the specified \a mode. * Construct a file list with the given kind of files, for the stated purpose.
* @param mode Kind of files required in the list. * @param abstract_filetype Kind of files to collect.
* @param fop Purpose of the collection, either #FOP_LOAD or #FOP_SAVE.
*/ */
void FileList::BuildFileList(SaveLoadDialogMode mode) void FileList::BuildFileList(AbstractFileType abstract_filetype, FileOperation fop)
{ {
this->Clear(); this->Clear();
switch (mode) { assert(fop == FOP_LOAD || FOP_SAVE);
case SLD_LOAD_SCENARIO: switch (abstract_filetype) {
case SLD_SAVE_SCENARIO: case FT_NONE:
FiosGetScenarioList(mode, *this); break; break;
case SLD_SAVE_HEIGHTMAP:
case SLD_LOAD_HEIGHTMAP:
FiosGetHeightmapList(mode, *this); break;
default: FiosGetSavegameList(mode, *this); break; case FT_SAVEGAME:
FiosGetSavegameList(fop, *this);
break;
case FT_SCENARIO:
FiosGetScenarioList(fop, *this);
break;
case FT_HEIGHTMAP:
FiosGetHeightmapList(fop, *this);
break;
default:
NOT_REACHED();
} }
} }
@ -263,24 +274,24 @@ bool FiosDelete(const char *name)
return unlink(filename) == 0; return unlink(filename) == 0;
} }
typedef FiosType fios_getlist_callback_proc(SaveLoadDialogMode mode, const char *filename, const char *ext, char *title, const char *last); typedef FiosType fios_getlist_callback_proc(FileOperation fop, const char *filename, const char *ext, char *title, const char *last);
/** /**
* Scanner to scan for a particular type of FIOS file. * Scanner to scan for a particular type of FIOS file.
*/ */
class FiosFileScanner : public FileScanner { class FiosFileScanner : public FileScanner {
SaveLoadDialogMode mode; ///< The mode we want to search for FileOperation fop; ///< The kind of file we are looking for.
fios_getlist_callback_proc *callback_proc; ///< Callback to check whether the file may be added fios_getlist_callback_proc *callback_proc; ///< Callback to check whether the file may be added
FileList &file_list; ///< Destination of the found files. FileList &file_list; ///< Destination of the found files.
public: public:
/** /**
* Create the scanner * Create the scanner
* @param mode The mode we are in. Some modes don't allow 'parent'. * @param fop Purpose of collecting the list.
* @param callback_proc The function that is called where you need to do the filtering. * @param callback_proc The function that is called where you need to do the filtering.
* @param file_list Destination of the found files. * @param file_list Destination of the found files.
*/ */
FiosFileScanner(SaveLoadDialogMode mode, fios_getlist_callback_proc *callback_proc, FileList &file_list) : FiosFileScanner(FileOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list) :
mode(mode), callback_proc(callback_proc), file_list(file_list) fop(fop), callback_proc(callback_proc), file_list(file_list)
{} {}
/* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename); /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename);
@ -300,7 +311,7 @@ bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, cons
char fios_title[64]; char fios_title[64];
fios_title[0] = '\0'; // reset the title; fios_title[0] = '\0'; // reset the title;
FiosType type = this->callback_proc(this->mode, filename, ext, fios_title, lastof(fios_title)); FiosType type = this->callback_proc(this->fop, filename, ext, fios_title, lastof(fios_title));
if (type == FIOS_TYPE_INVALID) return false; if (type == FIOS_TYPE_INVALID) return false;
for (const FiosItem *fios = file_list.Begin(); fios != file_list.End(); fios++) { for (const FiosItem *fios = file_list.Begin(); fios != file_list.End(); fios++) {
@ -338,12 +349,12 @@ bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, cons
/** /**
* Fill the list of the files in a directory, according to some arbitrary rule. * 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 fop Purpose of collecting the list.
* @param callback_proc The function that is called where you need to do the filtering. * @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. * @param subdir The directory from where to start (global) searching.
* @param file_list Destination of the found files. * @param file_list Destination of the found files.
*/ */
static void FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc *callback_proc, Subdirectory subdir, FileList &file_list) static void FiosGetFileList(FileOperation fop, fios_getlist_callback_proc *callback_proc, Subdirectory subdir, FileList &file_list)
{ {
struct stat sb; struct stat sb;
struct dirent *dirent; struct dirent *dirent;
@ -395,7 +406,7 @@ static void FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc
sort_start = file_list.Length(); sort_start = file_list.Length();
/* Show files */ /* Show files */
FiosFileScanner scanner(mode, callback_proc, file_list); FiosFileScanner scanner(fop, callback_proc, file_list);
if (subdir == NO_DIRECTORY) { if (subdir == NO_DIRECTORY) {
scanner.Scan(NULL, _fios_path, false); scanner.Scan(NULL, _fios_path, false);
} else { } else {
@ -436,7 +447,7 @@ static void GetFileTitle(const char *file, char *title, const char *last, Subdir
/** /**
* Callback for FiosGetFileList. It tells if a file is a savegame or not. * Callback for FiosGetFileList. It tells if a file is a savegame or not.
* @param mode Save/load mode. * @param fop Purpose of collecting the list.
* @param file Name of the file to check. * @param file Name of the file to check.
* @param ext A pointer to the extension identifier inside file * @param ext A pointer to the extension identifier inside file
* @param title Buffer if a callback wants to lookup the title of the file; NULL to skip the lookup * @param title Buffer if a callback wants to lookup the title of the file; NULL to skip the lookup
@ -445,7 +456,7 @@ static void GetFileTitle(const char *file, char *title, const char *last, Subdir
* @see FiosGetFileList * @see FiosGetFileList
* @see FiosGetSavegameList * @see FiosGetSavegameList
*/ */
FiosType FiosGetSavegameListCallback(SaveLoadDialogMode mode, const char *file, const char *ext, char *title, const char *last) FiosType FiosGetSavegameListCallback(FileOperation fop, const char *file, const char *ext, char *title, const char *last)
{ {
/* Show savegame files /* Show savegame files
* .SAV OpenTTD saved game * .SAV OpenTTD saved game
@ -461,7 +472,7 @@ FiosType FiosGetSavegameListCallback(SaveLoadDialogMode mode, const char *file,
return FIOS_TYPE_FILE; return FIOS_TYPE_FILE;
} }
if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { if (fop == FOP_LOAD) {
if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 || if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 ||
strcasecmp(ext, ".sv2") == 0) { strcasecmp(ext, ".sv2") == 0) {
if (title != NULL) GetOldSaveGameName(file, title, last); if (title != NULL) GetOldSaveGameName(file, title, last);
@ -474,11 +485,11 @@ FiosType FiosGetSavegameListCallback(SaveLoadDialogMode mode, const char *file,
/** /**
* Get a list of savegames. * Get a list of savegames.
* @param mode Save/load mode. * @param fop Purpose of collecting the list.
* @param file_list Destination of the found files. * @param file_list Destination of the found files.
* @see FiosGetFileList * @see FiosGetFileList
*/ */
void FiosGetSavegameList(SaveLoadDialogMode mode, FileList &file_list) void FiosGetSavegameList(FileOperation fop, FileList &file_list)
{ {
static char *fios_save_path = NULL; static char *fios_save_path = NULL;
static char *fios_save_path_last = NULL; static char *fios_save_path_last = NULL;
@ -492,12 +503,12 @@ void FiosGetSavegameList(SaveLoadDialogMode mode, FileList &file_list)
_fios_path = fios_save_path; _fios_path = fios_save_path;
_fios_path_last = fios_save_path_last; _fios_path_last = fios_save_path_last;
FiosGetFileList(mode, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list); FiosGetFileList(fop, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
} }
/** /**
* Callback for FiosGetFileList. It tells if a file is a scenario or not. * Callback for FiosGetFileList. It tells if a file is a scenario or not.
* @param mode Save/load mode. * @param fop Purpose of collecting the list.
* @param file Name of the file to check. * @param file Name of the file to check.
* @param ext A pointer to the extension identifier inside file * @param ext A pointer to the extension identifier inside file
* @param title Buffer if a callback wants to lookup the title of the file * @param title Buffer if a callback wants to lookup the title of the file
@ -506,7 +517,7 @@ void FiosGetSavegameList(SaveLoadDialogMode mode, FileList &file_list)
* @see FiosGetFileList * @see FiosGetFileList
* @see FiosGetScenarioList * @see FiosGetScenarioList
*/ */
static FiosType FiosGetScenarioListCallback(SaveLoadDialogMode mode, const char *file, const char *ext, char *title, const char *last) static FiosType FiosGetScenarioListCallback(FileOperation fop, const char *file, const char *ext, char *title, const char *last)
{ {
/* Show scenario files /* Show scenario files
* .SCN OpenTTD style scenario file * .SCN OpenTTD style scenario file
@ -517,7 +528,7 @@ static FiosType FiosGetScenarioListCallback(SaveLoadDialogMode mode, const char
return FIOS_TYPE_SCENARIO; return FIOS_TYPE_SCENARIO;
} }
if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { if (fop == FOP_LOAD) {
if (strcasecmp(ext, ".sv0") == 0 || strcasecmp(ext, ".ss0") == 0 ) { if (strcasecmp(ext, ".sv0") == 0 || strcasecmp(ext, ".ss0") == 0 ) {
GetOldSaveGameName(file, title, last); GetOldSaveGameName(file, title, last);
return FIOS_TYPE_OLD_SCENARIO; return FIOS_TYPE_OLD_SCENARIO;
@ -529,11 +540,11 @@ static FiosType FiosGetScenarioListCallback(SaveLoadDialogMode mode, const char
/** /**
* Get a list of scenarios. * Get a list of scenarios.
* @param mode Save/load mode. * @param fop Purpose of collecting the list.
* @param file_list Destination of the found files. * @param file_list Destination of the found files.
* @see FiosGetFileList * @see FiosGetFileList
*/ */
void FiosGetScenarioList(SaveLoadDialogMode mode, FileList &file_list) void FiosGetScenarioList(FileOperation fop, FileList &file_list)
{ {
static char *fios_scn_path = NULL; static char *fios_scn_path = NULL;
static char *fios_scn_path_last = NULL; static char *fios_scn_path_last = NULL;
@ -551,11 +562,11 @@ void FiosGetScenarioList(SaveLoadDialogMode mode, FileList &file_list)
char base_path[MAX_PATH]; char base_path[MAX_PATH];
FioGetDirectory(base_path, lastof(base_path), SCENARIO_DIR); FioGetDirectory(base_path, lastof(base_path), SCENARIO_DIR);
Subdirectory subdir = (mode == SLD_LOAD_SCENARIO && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY; Subdirectory subdir = (fop == FOP_LOAD && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY;
FiosGetFileList(mode, &FiosGetScenarioListCallback, subdir, file_list); FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list);
} }
static FiosType FiosGetHeightmapListCallback(SaveLoadDialogMode mode, const char *file, const char *ext, char *title, const char *last) static FiosType FiosGetHeightmapListCallback(FileOperation fop, const char *file, const char *ext, char *title, const char *last)
{ {
/* Show heightmap files /* Show heightmap files
* .PNG PNG Based heightmap files * .PNG PNG Based heightmap files
@ -601,10 +612,10 @@ static FiosType FiosGetHeightmapListCallback(SaveLoadDialogMode mode, const char
/** /**
* Get a list of heightmaps. * Get a list of heightmaps.
* @param mode Save/load mode. * @param fop Purpose of collecting the list.
* @param file_list Destination of the found files. * @param file_list Destination of the found files.
*/ */
void FiosGetHeightmapList(SaveLoadDialogMode mode, FileList &file_list) void FiosGetHeightmapList(FileOperation fop, FileList &file_list)
{ {
static char *fios_hmap_path = NULL; static char *fios_hmap_path = NULL;
static char *fios_hmap_path_last = NULL; static char *fios_hmap_path_last = NULL;
@ -622,7 +633,7 @@ void FiosGetHeightmapList(SaveLoadDialogMode mode, FileList &file_list)
FioGetDirectory(base_path, lastof(base_path), HEIGHTMAP_DIR); FioGetDirectory(base_path, lastof(base_path), HEIGHTMAP_DIR);
Subdirectory subdir = strcmp(base_path, _fios_path) == 0 ? HEIGHTMAP_DIR : NO_DIRECTORY; Subdirectory subdir = strcmp(base_path, _fios_path) == 0 ? HEIGHTMAP_DIR : NO_DIRECTORY;
FiosGetFileList(mode, &FiosGetHeightmapListCallback, subdir, file_list); FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list);
} }
/** /**

View File

@ -200,7 +200,7 @@ public:
this->files.Compact(); this->files.Compact();
} }
void BuildFileList(SaveLoadDialogMode mode); void BuildFileList(AbstractFileType abstract_filetype, FileOperation fop);
const FiosItem *FindItem(const char *file); const FiosItem *FindItem(const char *file);
SmallVector<FiosItem, 32> files; ///< The list of files. SmallVector<FiosItem, 32> files; ///< The list of files.
@ -220,9 +220,9 @@ extern SortingBits _savegame_sort_order;
void ShowSaveLoadDialog(SaveLoadDialogMode mode); void ShowSaveLoadDialog(SaveLoadDialogMode mode);
void FiosGetSavegameList(SaveLoadDialogMode mode, FileList &file_list); void FiosGetSavegameList(FileOperation fop, FileList &file_list);
void FiosGetScenarioList(SaveLoadDialogMode mode, FileList &file_list); void FiosGetScenarioList(FileOperation fop, FileList &file_list);
void FiosGetHeightmapList(SaveLoadDialogMode mode, FileList &file_list); void FiosGetHeightmapList(FileOperation fop, FileList &file_list);
const char *FiosBrowseTo(const FiosItem *item); const char *FiosBrowseTo(const FiosItem *item);
@ -231,7 +231,7 @@ bool FiosDelete(const char *name);
void FiosMakeHeightmapName(char *buf, const char *name, const char *last); void FiosMakeHeightmapName(char *buf, const char *name, const char *last);
void FiosMakeSavegameName(char *buf, const char *name, const char *last); void FiosMakeSavegameName(char *buf, const char *name, const char *last);
FiosType FiosGetSavegameListCallback(SaveLoadDialogMode mode, const char *file, const char *ext, char *title, const char *last); FiosType FiosGetSavegameListCallback(FileOperation fop, const char *file, const char *ext, char *title, const char *last);
int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b); int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b);

View File

@ -669,7 +669,19 @@ public:
if (!gui_scope) break; if (!gui_scope) break;
_fios_path_changed = true; _fios_path_changed = true;
this->fios_items.BuildFileList(_saveload_mode);
AbstractFileType abstract_filetype;
FileOperation fop;
switch (_saveload_mode) {
case SLD_LOAD_GAME: abstract_filetype = FT_SAVEGAME; fop = FOP_LOAD; break;
case SLD_LOAD_SCENARIO: abstract_filetype = FT_SCENARIO; fop = FOP_LOAD; break;
case SLD_SAVE_GAME: abstract_filetype = FT_SAVEGAME; fop = FOP_SAVE; break;
case SLD_SAVE_SCENARIO: abstract_filetype = FT_SCENARIO; fop = FOP_SAVE; break;
case SLD_LOAD_HEIGHTMAP: abstract_filetype = FT_HEIGHTMAP; fop = FOP_LOAD; break;
case SLD_SAVE_HEIGHTMAP: abstract_filetype = FT_HEIGHTMAP; fop = FOP_SAVE; break;
default: NOT_REACHED();
}
this->fios_items.BuildFileList(abstract_filetype, fop);
this->vscroll->SetCount(this->fios_items.Length()); this->vscroll->SetCount(this->fios_items.Length());
this->selected = NULL; this->selected = NULL;
_load_check_data.Clear(); _load_check_data.Clear();

View File

@ -625,7 +625,7 @@ int openttd_main(int argc, char *argv[])
/* if the file doesn't exist or it is not a valid savegame, let the saveload code show an error */ /* if the file doesn't exist or it is not a valid savegame, let the saveload code show an error */
const char *t = strrchr(_file_to_saveload.name, '.'); const char *t = strrchr(_file_to_saveload.name, '.');
if (t != NULL) { if (t != NULL) {
FiosType ft = FiosGetSavegameListCallback(SLD_LOAD_GAME, _file_to_saveload.name, t, NULL, NULL); FiosType ft = FiosGetSavegameListCallback(FOP_LOAD, _file_to_saveload.name, t, NULL, NULL);
if (ft != FIOS_TYPE_INVALID) _file_to_saveload.SetMode(ft); if (ft != FIOS_TYPE_INVALID) _file_to_saveload.SetMode(ft);
} }
@ -647,7 +647,7 @@ int openttd_main(int argc, char *argv[])
char title[80]; char title[80];
title[0] = '\0'; title[0] = '\0';
FiosGetSavegameListCallback(SLD_LOAD_GAME, mgo.opt, strrchr(mgo.opt, '.'), title, lastof(title)); FiosGetSavegameListCallback(FOP_LOAD, mgo.opt, strrchr(mgo.opt, '.'), title, lastof(title));
_load_check_data.Clear(); _load_check_data.Clear();
SaveOrLoadResult res = SaveOrLoad(mgo.opt, SL_LOAD_CHECK, SAVE_DIR, false); SaveOrLoadResult res = SaveOrLoad(mgo.opt, SL_LOAD_CHECK, SAVE_DIR, false);