From 55c7c21c5d6f6b72408cb67ed7e663216226105f Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:44:42 +0000 Subject: [PATCH 01/28] (svn r27633) -Codechange: Extract _saveload_mode use from BuildFileList --- src/console_cmds.cpp | 4 ++-- src/fios.h | 2 +- src/fios_gui.cpp | 12 ++++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 8d9e04113d..8df5b15cde 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -324,7 +324,7 @@ DEF_CONSOLE_CMD(ConSaveConfig) static const FiosItem *GetFiosItem(const char *file) { _saveload_mode = SLD_LOAD_GAME; - BuildFileList(); + BuildFileList(_saveload_mode); for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) { if (strcmp(file, item->name) == 0) return item; @@ -415,7 +415,7 @@ DEF_CONSOLE_CMD(ConListFiles) return true; } - BuildFileList(); + BuildFileList(_saveload_mode); for (uint i = 0; i < _fios_items.Length(); i++) { IConsolePrintF(CC_DEFAULT, "%d) %s", i, _fios_items[i].title); diff --git a/src/fios.h b/src/fios.h index b26fe00925..b9bf390bb8 100644 --- a/src/fios.h +++ b/src/fios.h @@ -179,7 +179,7 @@ int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b); extern const TextColour _fios_colours[]; -void BuildFileList(); +void BuildFileList(SaveLoadDialogMode mode); void SetFiosType(const byte fiostype); #endif /* FIOS_H */ diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 9316deb658..401cb82545 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -193,20 +193,20 @@ const TextColour _fios_colours[] = { TC_ORANGE, TC_LIGHT_BROWN, TC_ORANGE, TC_ORANGE, TC_YELLOW }; -void BuildFileList() +void BuildFileList(SaveLoadDialogMode mode) { _fios_path_changed = true; FiosFreeSavegameList(); - switch (_saveload_mode) { + switch (mode) { case SLD_LOAD_SCENARIO: case SLD_SAVE_SCENARIO: - FiosGetScenarioList(_saveload_mode); break; + FiosGetScenarioList(mode); break; case SLD_SAVE_HEIGHTMAP: case SLD_LOAD_HEIGHTMAP: - FiosGetHeightmapList(_saveload_mode); break; + FiosGetHeightmapList(mode); break; - default: FiosGetSavegameList(_saveload_mode); break; + default: FiosGetSavegameList(mode); break; } /* Invalidate saveload window */ @@ -683,7 +683,7 @@ public: this->selected = NULL; _load_check_data.Clear(); if (!gui_scope) break; - BuildFileList(); + BuildFileList(_saveload_mode); /* FALL THROUGH */ case 1: /* Selection changes */ From df2221ba7eb72a2dfd3b1babe8b97fe99e2aad7d Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:45:11 +0000 Subject: [PATCH 02/28] (svn r27634) -Codechange: Improve name of the SmallFiosItem struct. --- src/fios.cpp | 2 +- src/fios.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/fios.cpp b/src/fios.cpp index 00ed5a484c..6ba913c09c 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -32,7 +32,7 @@ SmallVector _fios_items; static char *_fios_path; static const char *_fios_path_last; -SmallFiosItem _file_to_saveload; +FileToSaveLoad _file_to_saveload; ///< File to save or load in the openttd loop. SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING; /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */ diff --git a/src/fios.h b/src/fios.h index b9bf390bb8..f25d5a7e2b 100644 --- a/src/fios.h +++ b/src/fios.h @@ -138,7 +138,7 @@ struct FiosItem { }; /** Deals with the type of the savegame, independent of extension */ -struct SmallFiosItem { +struct FileToSaveLoad { int mode; ///< savegame/scenario type (old, new) FileType filetype; ///< what type of file are we dealing with char name[MAX_PATH]; ///< name @@ -155,7 +155,7 @@ DECLARE_ENUM_AS_BIT_SET(SortingBits) /* Variables to display file lists */ extern SmallVector _fios_items; -extern SmallFiosItem _file_to_saveload; +extern FileToSaveLoad _file_to_saveload; extern SaveLoadDialogMode _saveload_mode; extern SortingBits _savegame_sort_order; From a35e970ab034d1f96b3f438eabd750c25a3451d0 Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:45:40 +0000 Subject: [PATCH 03/28] (svn r27635) -Codechange: Move FileType and FileToSaveLoad structure definitions. --- src/fios.cpp | 1 - src/fios.h | 17 ----------------- src/genworld_gui.cpp | 1 + src/landscape.cpp | 1 + src/saveload/saveload.cpp | 1 + src/saveload/saveload.h | 18 ++++++++++++++++++ 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/fios.cpp b/src/fios.cpp index 6ba913c09c..631c88bf62 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -32,7 +32,6 @@ SmallVector _fios_items; static char *_fios_path; static const char *_fios_path_last; -FileToSaveLoad _file_to_saveload; ///< File to save or load in the openttd loop. SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING; /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */ diff --git a/src/fios.h b/src/fios.h index f25d5a7e2b..6ef7783b62 100644 --- a/src/fios.h +++ b/src/fios.h @@ -107,14 +107,6 @@ enum SaveLoadDialogMode { SLD_SAVE_HEIGHTMAP, ///< Save a heightmap. }; -/** The different types of files that the system knows about. */ -enum FileType { - FT_NONE, ///< nothing to do - FT_SAVEGAME, ///< old or new savegame - FT_SCENARIO, ///< old or new scenario - FT_HEIGHTMAP, ///< heightmap file -}; - enum FiosType { FIOS_TYPE_DRIVE, FIOS_TYPE_PARENT, @@ -137,14 +129,6 @@ struct FiosItem { char name[MAX_PATH]; }; -/** Deals with the type of the savegame, independent of extension */ -struct FileToSaveLoad { - int mode; ///< savegame/scenario type (old, new) - FileType filetype; ///< what type of file are we dealing with - char name[MAX_PATH]; ///< name - char title[255]; ///< internal name of the game -}; - enum SortingBits { SORT_ASCENDING = 0, SORT_DESCENDING = 1, @@ -155,7 +139,6 @@ DECLARE_ENUM_AS_BIT_SET(SortingBits) /* Variables to display file lists */ extern SmallVector _fios_items; -extern FileToSaveLoad _file_to_saveload; extern SaveLoadDialogMode _saveload_mode; extern SortingBits _savegame_sort_order; diff --git a/src/genworld_gui.cpp b/src/genworld_gui.cpp index 741fd75d1c..61e114311b 100644 --- a/src/genworld_gui.cpp +++ b/src/genworld_gui.cpp @@ -26,6 +26,7 @@ #include "town.h" #include "core/geometry_func.hpp" #include "core/random_func.hpp" +#include "saveload/saveload.h" #include "progress.h" #include "error.h" diff --git a/src/landscape.cpp b/src/landscape.cpp index d1c73fd42b..d2b1b98cd5 100644 --- a/src/landscape.cpp +++ b/src/landscape.cpp @@ -31,6 +31,7 @@ #include "object_base.h" #include "company_func.h" #include "pathfinder/npf/aystar.h" +#include "saveload/saveload.h" #include #include diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 3d6f3c4949..03c0cb8c84 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -267,6 +267,7 @@ extern const uint16 SAVEGAME_VERSION = 195; ///< Current savegame version of OpenTTD. SavegameType _savegame_type; ///< type of savegame we are loading +FileToSaveLoad _file_to_saveload; ///< File to save or load in the openttd loop. uint32 _ttdp_version; ///< version of TTDP savegame (if applicable) uint16 _sl_version; ///< the major savegame version identifier diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 72c51fa69d..c4472237c2 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -33,6 +33,22 @@ enum SaveOrLoadMode { SL_LOAD_CHECK = 5, ///< Load for game preview. }; +/** The different types of files that the system knows about. */ +enum FileType { + FT_NONE, ///< nothing to do + FT_SAVEGAME, ///< old or new savegame + FT_SCENARIO, ///< old or new scenario + FT_HEIGHTMAP, ///< heightmap file +}; + +/** Deals with the type of the savegame, independent of extension */ +struct FileToSaveLoad { + SaveOrLoadMode mode; ///< savegame/scenario type (old, new) + FileType filetype; ///< what type of file are we dealing with + char name[MAX_PATH]; ///< name + char title[255]; ///< internal name of the game +}; + /** Types of save games. */ enum SavegameType { SGT_TTD, ///< TTD savegame (can be detected incorrectly) @@ -43,6 +59,8 @@ enum SavegameType { SGT_INVALID = 0xFF, ///< broken savegame (used internally) }; +extern FileToSaveLoad _file_to_saveload; + void GenerateDefaultSaveName(char *buf, const char *last); void SetSaveLoadError(uint16 str); const char *GetSaveLoadErrorString(); From 757bc0a97bef39196a5278656850680e4f67cc4b Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:46:07 +0000 Subject: [PATCH 04/28] (svn r27636) -Codechange: Rename FileType to AbstractFileType. --- src/fileio_type.h | 8 ++++++++ src/fios_gui.cpp | 2 +- src/saveload/saveload.h | 16 ++++------------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/fileio_type.h b/src/fileio_type.h index 87cb2d1a82..fc50484f20 100644 --- a/src/fileio_type.h +++ b/src/fileio_type.h @@ -14,6 +14,14 @@ #include "core/enum_type.hpp" +/** The different abstract types of files that the system knows about. */ +enum AbstractFileType { + FT_NONE, ///< nothing to do + FT_SAVEGAME, ///< old or new savegame + FT_SCENARIO, ///< old or new scenario + FT_HEIGHTMAP, ///< heightmap file +}; + /** * The different kinds of subdirectories OpenTTD uses */ diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 401cb82545..31f0b3d51e 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -738,7 +738,7 @@ static WindowDesc _save_dialog_desc( * These values are used to convert the file/operations mode into a corresponding file type. * So each entry, as expressed by the related comment, is based on the enum */ -static const FileType _file_modetotype[] = { +static const AbstractFileType _file_modetotype[] = { FT_SAVEGAME, // used for SLD_LOAD_GAME FT_SCENARIO, // used for SLD_LOAD_SCENARIO FT_SAVEGAME, // used for SLD_SAVE_GAME diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index c4472237c2..450fe58fe0 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -33,20 +33,12 @@ enum SaveOrLoadMode { SL_LOAD_CHECK = 5, ///< Load for game preview. }; -/** The different types of files that the system knows about. */ -enum FileType { - FT_NONE, ///< nothing to do - FT_SAVEGAME, ///< old or new savegame - FT_SCENARIO, ///< old or new scenario - FT_HEIGHTMAP, ///< heightmap file -}; - /** Deals with the type of the savegame, independent of extension */ struct FileToSaveLoad { - SaveOrLoadMode mode; ///< savegame/scenario type (old, new) - FileType filetype; ///< what type of file are we dealing with - char name[MAX_PATH]; ///< name - char title[255]; ///< internal name of the game + SaveOrLoadMode mode; ///< savegame/scenario type (old, new) + AbstractFileType filetype; ///< what type of file are we dealing with + char name[MAX_PATH]; ///< name + char title[255]; ///< internal name of the game }; /** Types of save games. */ From b5eb50da7b0e702eb74176deea6fa46d120cd718 Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:46:29 +0000 Subject: [PATCH 05/28] (svn r27637) -Codechange: Don't use _saveload_mode for scenario loading detection. --- src/saveload/signs_sl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/saveload/signs_sl.cpp b/src/saveload/signs_sl.cpp index d5bacd509c..077d34501d 100644 --- a/src/saveload/signs_sl.cpp +++ b/src/saveload/signs_sl.cpp @@ -60,7 +60,7 @@ static void Load_SIGN() } /* Signs placed in scenario editor shall now be OWNER_DEITY */ - if (IsSavegameVersionBefore(171) && si->owner == OWNER_NONE && _saveload_mode == SLD_LOAD_SCENARIO) { + if (IsSavegameVersionBefore(171) && si->owner == OWNER_NONE && _file_to_saveload.filetype == FT_SCENARIO) { si->owner = OWNER_DEITY; } } From e78627f2f10b2b742ae990d5d007309b5c70f3af Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:47:07 +0000 Subject: [PATCH 06/28] (svn r27638) -Codechange: Move FiosType enum, move and rename SetFiosType function. --- src/console_cmds.cpp | 2 +- src/fileio_type.h | 15 +++++++++++++++ src/fios.h | 15 --------------- src/fios_gui.cpp | 33 ++------------------------------- src/openttd.cpp | 2 +- src/saveload/saveload.cpp | 33 +++++++++++++++++++++++++++++++++ src/saveload/saveload.h | 2 ++ 7 files changed, 54 insertions(+), 48 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 8df5b15cde..67f3f60988 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -366,7 +366,7 @@ DEF_CONSOLE_CMD(ConLoad) switch (item->type) { case FIOS_TYPE_FILE: case FIOS_TYPE_OLDFILE: { _switch_mode = SM_LOAD_GAME; - SetFiosType(item->type); + _file_to_saveload.SetMode(item->type); strecpy(_file_to_saveload.name, FiosBrowseTo(item), lastof(_file_to_saveload.name)); strecpy(_file_to_saveload.title, item->title, lastof(_file_to_saveload.title)); diff --git a/src/fileio_type.h b/src/fileio_type.h index fc50484f20..390b28f24d 100644 --- a/src/fileio_type.h +++ b/src/fileio_type.h @@ -22,6 +22,21 @@ enum AbstractFileType { FT_HEIGHTMAP, ///< heightmap file }; +/** Elements of a file system that are recognized. */ +enum FiosType { + FIOS_TYPE_DRIVE, + FIOS_TYPE_PARENT, + FIOS_TYPE_DIR, + FIOS_TYPE_FILE, + FIOS_TYPE_OLDFILE, + FIOS_TYPE_SCENARIO, + FIOS_TYPE_OLD_SCENARIO, + FIOS_TYPE_DIRECT, + FIOS_TYPE_PNG, + FIOS_TYPE_BMP, + FIOS_TYPE_INVALID = 255, +}; + /** * The different kinds of subdirectories OpenTTD uses */ diff --git a/src/fios.h b/src/fios.h index 6ef7783b62..4ccd1f192b 100644 --- a/src/fios.h +++ b/src/fios.h @@ -107,20 +107,6 @@ enum SaveLoadDialogMode { SLD_SAVE_HEIGHTMAP, ///< Save a heightmap. }; -enum FiosType { - FIOS_TYPE_DRIVE, - FIOS_TYPE_PARENT, - FIOS_TYPE_DIR, - FIOS_TYPE_FILE, - FIOS_TYPE_OLDFILE, - FIOS_TYPE_SCENARIO, - FIOS_TYPE_OLD_SCENARIO, - FIOS_TYPE_DIRECT, - FIOS_TYPE_PNG, - FIOS_TYPE_BMP, - FIOS_TYPE_INVALID = 255, -}; - /** Deals with finding savegames */ struct FiosItem { FiosType type; @@ -163,6 +149,5 @@ int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b); extern const TextColour _fios_colours[]; void BuildFileList(SaveLoadDialogMode mode); -void SetFiosType(const byte fiostype); #endif /* FIOS_H */ diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 31f0b3d51e..50a6711c6b 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -527,7 +527,7 @@ public: case WID_SL_LOAD_BUTTON: if (this->selected != NULL && !_load_check_data.HasErrors()) { const char *name = FiosBrowseTo(this->selected); - SetFiosType(this->selected->type); + _file_to_saveload.SetMode(this->selected->type); strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name)); strecpy(_file_to_saveload.title, this->selected->title, lastof(_file_to_saveload.title)); @@ -588,7 +588,7 @@ public: if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) { this->OnClick(pt, WID_SL_LOAD_BUTTON, 1); } else if (_saveload_mode == SLD_LOAD_HEIGHTMAP) { - SetFiosType(file->type); + _file_to_saveload.SetMode(file->type); strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name)); strecpy(_file_to_saveload.title, file->title, lastof(_file_to_saveload.title)); @@ -772,32 +772,3 @@ void ShowSaveLoadDialog(SaveLoadDialogMode mode) new SaveLoadWindow(sld, mode); } - -void SetFiosType(const byte fiostype) -{ - switch (fiostype) { - case FIOS_TYPE_FILE: - case FIOS_TYPE_SCENARIO: - _file_to_saveload.mode = SL_LOAD; - break; - - case FIOS_TYPE_OLDFILE: - case FIOS_TYPE_OLD_SCENARIO: - _file_to_saveload.mode = SL_OLD_LOAD; - break; - -#ifdef WITH_PNG - case FIOS_TYPE_PNG: - _file_to_saveload.mode = SL_PNG; - break; -#endif /* WITH_PNG */ - - case FIOS_TYPE_BMP: - _file_to_saveload.mode = SL_BMP; - break; - - default: - _file_to_saveload.mode = SL_INVALID; - break; - } -} diff --git a/src/openttd.cpp b/src/openttd.cpp index c149ebbd4d..b5f654df67 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -626,7 +626,7 @@ int openttd_main(int argc, char *argv[]) const char *t = strrchr(_file_to_saveload.name, '.'); if (t != NULL) { FiosType ft = FiosGetSavegameListCallback(SLD_LOAD_GAME, _file_to_saveload.name, t, NULL, NULL); - if (ft != FIOS_TYPE_INVALID) SetFiosType(ft); + if (ft != FIOS_TYPE_INVALID) _file_to_saveload.SetMode(ft); } break; diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 03c0cb8c84..007b22f012 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -2892,6 +2892,39 @@ void GenerateDefaultSaveName(char *buf, const char *last) SanitizeFilename(buf); } +/** + * Set the mode of the file to save or load based on the type of file entry at the file system. + * @param ft Type of file entry of the file system. + */ +void FileToSaveLoad::SetMode(FiosType ft) +{ + switch (ft) { + case FIOS_TYPE_FILE: + case FIOS_TYPE_SCENARIO: + this->mode = SL_LOAD; + break; + + case FIOS_TYPE_OLDFILE: + case FIOS_TYPE_OLD_SCENARIO: + this->mode = SL_OLD_LOAD; + break; + +#ifdef WITH_PNG + case FIOS_TYPE_PNG: + this->mode = SL_PNG; + break; +#endif /* WITH_PNG */ + + case FIOS_TYPE_BMP: + this->mode = SL_BMP; + break; + + default: + this->mode = SL_INVALID; + break; + } +} + #if 0 /** * Function to get the type of the savegame by looking at the file header. diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 450fe58fe0..43b9d5d3ce 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -39,6 +39,8 @@ struct FileToSaveLoad { AbstractFileType filetype; ///< what type of file are we dealing with char name[MAX_PATH]; ///< name char title[255]; ///< internal name of the game + + void SetMode(FiosType ft); }; /** Types of save games. */ From b6c29ae4eb8fb307c30677705431bdec307ff53b Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:47:39 +0000 Subject: [PATCH 07/28] (svn r27639) -Codechange: Also always set the abstract FileToSaveLoad::filetype when setting a mode. --- src/fileio_type.h | 2 ++ src/saveload/saveload.cpp | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/fileio_type.h b/src/fileio_type.h index 390b28f24d..3aee5a0e76 100644 --- a/src/fileio_type.h +++ b/src/fileio_type.h @@ -20,6 +20,8 @@ enum AbstractFileType { FT_SAVEGAME, ///< old or new savegame FT_SCENARIO, ///< old or new scenario FT_HEIGHTMAP, ///< heightmap file + + FT_INVALID = 7, ///< Invalid or unknown file type. }; /** Elements of a file system that are recognized. */ diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 007b22f012..a0d15938a4 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -2893,7 +2893,7 @@ void GenerateDefaultSaveName(char *buf, const char *last) } /** - * Set the mode of the file to save or load based on the type of file entry at the file system. + * Set the mode and file type of the file to save or load based on the type of file entry at the file system. * @param ft Type of file entry of the file system. */ void FileToSaveLoad::SetMode(FiosType ft) @@ -2923,6 +2923,31 @@ void FileToSaveLoad::SetMode(FiosType ft) this->mode = SL_INVALID; break; } + + switch (ft) { + case FIOS_TYPE_OLDFILE: + case FIOS_TYPE_FILE: + this->filetype = FT_SAVEGAME; + break; + + case FIOS_TYPE_OLD_SCENARIO: + case FIOS_TYPE_SCENARIO: + this->filetype = FT_SCENARIO; + break; + +#ifdef WITH_PNG + case FIOS_TYPE_PNG: + /* FALL THROUGH */ +#endif /* WITH_PNG */ + + case FIOS_TYPE_BMP: + this->filetype = FT_HEIGHTMAP; + break; + + default: + this->filetype = FT_INVALID; + break; + } } #if 0 From c530f89761476d683893fcc5c1513089a754d773 Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:48:28 +0000 Subject: [PATCH 08/28] (svn r27640) -Codechange: Remove another use of _saveload_mode in the loading code. --- src/openttd.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openttd.cpp b/src/openttd.cpp index b5f654df67..594b6fa37e 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1097,7 +1097,7 @@ void SwitchToMode(SwitchMode new_mode) SetDParamStr(0, GetSaveLoadErrorString()); ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR); } else { - if (_saveload_mode == SLD_LOAD_SCENARIO) { + if (_file_to_saveload.filetype == FT_SCENARIO) { /* Reset engine pool to simplify changing engine NewGRFs in scenario editor. */ EngineOverrideManager::ResetToCurrentNewGRFConfig(); } From a38cdeccc041acc005e075f4d1a861b33ffd5f83 Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:50:22 +0000 Subject: [PATCH 09/28] (svn r27641) -Codechange: Fold the _fios_items file list vector into its own class. --- src/console_cmds.cpp | 10 ++--- src/core/smallvec_type.hpp | 2 + src/fios.cpp | 10 ++--- src/fios.h | 91 +++++++++++++++++++++++++++++++++++++- src/fios_gui.cpp | 4 +- 5 files changed, 102 insertions(+), 15 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 67f3f60988..7db151d77a 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -378,7 +378,7 @@ DEF_CONSOLE_CMD(ConLoad) IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file); } - FiosFreeSavegameList(); + _fios_items.Clear(); return true; } @@ -402,7 +402,7 @@ DEF_CONSOLE_CMD(ConRemove) IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file); } - FiosFreeSavegameList(); + _fios_items.Clear(); return true; } @@ -421,7 +421,7 @@ DEF_CONSOLE_CMD(ConListFiles) IConsolePrintF(CC_DEFAULT, "%d) %s", i, _fios_items[i].title); } - FiosFreeSavegameList(); + _fios_items.Clear(); return true; } @@ -448,7 +448,7 @@ DEF_CONSOLE_CMD(ConChangeDirectory) IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file); } - FiosFreeSavegameList(); + _fios_items.Clear(); return true; } @@ -463,7 +463,7 @@ DEF_CONSOLE_CMD(ConPrintWorkingDirectory) /* XXX - Workaround for broken file handling */ FiosGetSavegameList(SLD_LOAD_GAME); - FiosFreeSavegameList(); + _fios_items.Clear(); FiosGetDescText(&path, NULL); IConsolePrint(CC_DEFAULT, path); diff --git a/src/core/smallvec_type.hpp b/src/core/smallvec_type.hpp index 62de176a54..8676265d26 100644 --- a/src/core/smallvec_type.hpp +++ b/src/core/smallvec_type.hpp @@ -256,6 +256,8 @@ public: /** * Get the number of items in the list. + * + * @return The number of items in the list. */ inline uint Length() const { diff --git a/src/fios.cpp b/src/fios.cpp index 631c88bf62..e929596f92 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -29,7 +29,7 @@ #include "safeguards.h" /* Variables to display file lists */ -SmallVector _fios_items; +FileList _fios_items; static char *_fios_path; static const char *_fios_path_last; SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING; @@ -64,11 +64,9 @@ int CDECL CompareFiosItems(const FiosItem *da, const FiosItem *db) return r; } -/** Free the list of savegames. */ -void FiosFreeSavegameList() +FileList::~FileList() { - _fios_items.Clear(); - _fios_items.Compact(); + this->Clear(); } /** @@ -336,7 +334,7 @@ static void FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc { SortingBits order = _savegame_sort_order; _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING; - QSortT(_fios_items.Begin(), _fios_items.Length(), CompareFiosItems); + QSortT(_fios_items.files.Begin(), _fios_items.files.Length(), CompareFiosItems); _savegame_sort_order = order; } diff --git a/src/fios.h b/src/fios.h index 4ccd1f192b..0a6d3f95d0 100644 --- a/src/fios.h +++ b/src/fios.h @@ -115,6 +115,94 @@ struct FiosItem { char name[MAX_PATH]; }; +/** List of file information. */ +class FileList { +public: + ~FileList(); + + /** + * Construct a new entry in the file list. + * @return Pointer to the new items to be initialized. + */ + inline FiosItem *Append() + { + return this->files.Append(); + } + + /** + * Get the number of files in the list. + * @return The number of files stored in the list. + */ + inline uint Length() const + { + return this->files.Length(); + } + + /** + * Get a pointer to the first file information. + * @return Address of the first file information. + */ + inline const FiosItem *Begin() const + { + return this->files.Begin(); + } + + /** + * Get a pointer behind the last file information. + * @return Address behind the last file information. + */ + inline const FiosItem *End() const + { + return this->files.End(); + } + + /** + * Get a pointer to the indicated file information. File information must exist. + * @return Address of the indicated existing file information. + */ + inline const FiosItem *Get(uint index) const + { + return this->files.Get(index); + } + + /** + * Get a pointer to the indicated file information. File information must exist. + * @return Address of the indicated existing file information. + */ + inline FiosItem *Get(uint index) + { + return this->files.Get(index); + } + + inline const FiosItem &operator[](uint index) const + { + return this->files[index]; + } + + /** + * Get a reference to the indicated file information. File information must exist. + * @return The requested file information. + */ + inline FiosItem &operator[](uint index) + { + return this->files[index]; + } + + /** Remove all items from the list. */ + inline void Clear() + { + this->files.Clear(); + } + + /** Compact the list down to the smallest block size boundary. */ + inline void Compact() + { + this->files.Compact(); + } + + SmallVector files; ///< The list of files. +}; + enum SortingBits { SORT_ASCENDING = 0, SORT_DESCENDING = 1, @@ -124,7 +212,7 @@ enum SortingBits { DECLARE_ENUM_AS_BIT_SET(SortingBits) /* Variables to display file lists */ -extern SmallVector _fios_items; +extern FileList _fios_items; extern SaveLoadDialogMode _saveload_mode; extern SortingBits _savegame_sort_order; @@ -134,7 +222,6 @@ void FiosGetSavegameList(SaveLoadDialogMode mode); void FiosGetScenarioList(SaveLoadDialogMode mode); void FiosGetHeightmapList(SaveLoadDialogMode mode); -void FiosFreeSavegameList(); const char *FiosBrowseTo(const FiosItem *item); StringID FiosGetDescText(const char **path, uint64 *total_free); diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 50a6711c6b..b01954c557 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -196,7 +196,7 @@ const TextColour _fios_colours[] = { void BuildFileList(SaveLoadDialogMode mode) { _fios_path_changed = true; - FiosFreeSavegameList(); + _fios_items.Clear(); switch (mode) { case SLD_LOAD_SCENARIO: @@ -327,7 +327,7 @@ public: if (!_networking && _game_mode != GM_EDITOR && _game_mode != GM_MENU) { DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE); } - FiosFreeSavegameList(); + _fios_items.Clear(); } virtual void DrawWidget(const Rect &r, int widget) const From 93baf192a089e839684beaeee41abe3779aa9c0c Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:54:03 +0000 Subject: [PATCH 10/28] (svn r27642) -Codechange: FiosGet* file query functions take a destination file list. --- src/console_cmds.cpp | 2 +- src/fios.cpp | 47 +++++++++++++++++++++++++------------------- src/fios.h | 6 +++--- src/fios_gui.cpp | 6 +++--- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 7db151d77a..ae6e5458e1 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -462,7 +462,7 @@ DEF_CONSOLE_CMD(ConPrintWorkingDirectory) } /* XXX - Workaround for broken file handling */ - FiosGetSavegameList(SLD_LOAD_GAME); + FiosGetSavegameList(SLD_LOAD_GAME, _fios_items); _fios_items.Clear(); FiosGetDescText(&path, NULL); diff --git a/src/fios.cpp b/src/fios.cpp index e929596f92..529890810c 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -220,15 +220,16 @@ typedef FiosType fios_getlist_callback_proc(SaveLoadDialogMode mode, const char class FiosFileScanner : public FileScanner { SaveLoadDialogMode mode; ///< The mode we want to search for fios_getlist_callback_proc *callback_proc; ///< Callback to check whether the file may be added + FileList &file_list; ///< Destination of the found files. public: /** * Create the scanner * @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 file_list Destination of the found files. */ - FiosFileScanner(SaveLoadDialogMode mode, fios_getlist_callback_proc *callback_proc) : - mode(mode), - callback_proc(callback_proc) + FiosFileScanner(SaveLoadDialogMode mode, fios_getlist_callback_proc *callback_proc, FileList &file_list) : + mode(mode), callback_proc(callback_proc), file_list(file_list) {} /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename); @@ -251,11 +252,11 @@ bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, cons FiosType type = this->callback_proc(this->mode, filename, ext, fios_title, lastof(fios_title)); if (type == FIOS_TYPE_INVALID) return false; - for (const FiosItem *fios = _fios_items.Begin(); fios != _fios_items.End(); fios++) { + for (const FiosItem *fios = file_list.Begin(); fios != file_list.End(); fios++) { if (strcmp(fios->name, filename) == 0) return false; } - FiosItem *fios = _fios_items.Append(); + FiosItem *fios = file_list.Append(); #ifdef WIN32 struct _stat sb; if (_tstat(OTTD2FS(filename), &sb) == 0) { @@ -289,8 +290,9 @@ bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, cons * @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. + * @param file_list Destination of the found files. */ -static void FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc *callback_proc, Subdirectory subdir) +static void FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc *callback_proc, Subdirectory subdir, FileList &file_list) { struct stat sb; struct dirent *dirent; @@ -299,11 +301,11 @@ static void FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc int sort_start; char d_name[sizeof(fios->name)]; - _fios_items.Clear(); + file_list.Clear(); /* A parent directory link exists if we are not in the root directory */ if (!FiosIsRoot(_fios_path)) { - fios = _fios_items.Append(); + fios = file_list.Append(); fios->type = FIOS_TYPE_PARENT; fios->mtime = 0; strecpy(fios->name, "..", lastof(fios->name)); @@ -319,7 +321,7 @@ static void FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc if (FiosIsValidFile(_fios_path, dirent, &sb) && S_ISDIR(sb.st_mode) && (!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) && strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) { - fios = _fios_items.Append(); + fios = file_list.Append(); fios->type = FIOS_TYPE_DIR; fios->mtime = 0; strecpy(fios->name, d_name, lastof(fios->name)); @@ -334,27 +336,27 @@ static void FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc { SortingBits order = _savegame_sort_order; _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING; - QSortT(_fios_items.files.Begin(), _fios_items.files.Length(), CompareFiosItems); + QSortT(file_list.files.Begin(), file_list.files.Length(), CompareFiosItems); _savegame_sort_order = order; } /* This is where to start sorting for the filenames */ - sort_start = _fios_items.Length(); + sort_start = file_list.Length(); /* Show files */ - FiosFileScanner scanner(mode, callback_proc); + FiosFileScanner scanner(mode, callback_proc, file_list); if (subdir == NO_DIRECTORY) { scanner.Scan(NULL, _fios_path, false); } else { scanner.Scan(NULL, subdir, true, true); } - QSortT(_fios_items.Get(sort_start), _fios_items.Length() - sort_start, CompareFiosItems); + QSortT(file_list.Get(sort_start), file_list.Length() - sort_start, CompareFiosItems); /* Show drives */ FiosGetDrives(); - _fios_items.Compact(); + file_list.Compact(); } /** @@ -422,9 +424,10 @@ FiosType FiosGetSavegameListCallback(SaveLoadDialogMode mode, const char *file, /** * Get a list of savegames. * @param mode Save/load mode. + * @param file_list Destination of the found files. * @see FiosGetFileList */ -void FiosGetSavegameList(SaveLoadDialogMode mode) +void FiosGetSavegameList(SaveLoadDialogMode mode, FileList &file_list) { static char *fios_save_path = NULL; static char *fios_save_path_last = NULL; @@ -438,7 +441,7 @@ void FiosGetSavegameList(SaveLoadDialogMode mode) _fios_path = fios_save_path; _fios_path_last = fios_save_path_last; - FiosGetFileList(mode, &FiosGetSavegameListCallback, NO_DIRECTORY); + FiosGetFileList(mode, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list); } /** @@ -476,9 +479,10 @@ static FiosType FiosGetScenarioListCallback(SaveLoadDialogMode mode, const char /** * Get a list of scenarios. * @param mode Save/load mode. + * @param file_list Destination of the found files. * @see FiosGetFileList */ -void FiosGetScenarioList(SaveLoadDialogMode mode) +void FiosGetScenarioList(SaveLoadDialogMode mode, FileList &file_list) { static char *fios_scn_path = NULL; static char *fios_scn_path_last = NULL; @@ -496,7 +500,8 @@ void FiosGetScenarioList(SaveLoadDialogMode mode) char base_path[MAX_PATH]; FioGetDirectory(base_path, lastof(base_path), SCENARIO_DIR); - FiosGetFileList(mode, &FiosGetScenarioListCallback, (mode == SLD_LOAD_SCENARIO && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY); + Subdirectory subdir = (mode == SLD_LOAD_SCENARIO && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY; + FiosGetFileList(mode, &FiosGetScenarioListCallback, subdir, file_list); } static FiosType FiosGetHeightmapListCallback(SaveLoadDialogMode mode, const char *file, const char *ext, char *title, const char *last) @@ -546,8 +551,9 @@ static FiosType FiosGetHeightmapListCallback(SaveLoadDialogMode mode, const char /** * Get a list of heightmaps. * @param mode Save/load mode. + * @param file_list Destination of the found files. */ -void FiosGetHeightmapList(SaveLoadDialogMode mode) +void FiosGetHeightmapList(SaveLoadDialogMode mode, FileList &file_list) { static char *fios_hmap_path = NULL; static char *fios_hmap_path_last = NULL; @@ -564,7 +570,8 @@ void FiosGetHeightmapList(SaveLoadDialogMode mode) char base_path[MAX_PATH]; FioGetDirectory(base_path, lastof(base_path), HEIGHTMAP_DIR); - FiosGetFileList(mode, &FiosGetHeightmapListCallback, 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); } /** diff --git a/src/fios.h b/src/fios.h index 0a6d3f95d0..888e3c2362 100644 --- a/src/fios.h +++ b/src/fios.h @@ -218,9 +218,9 @@ extern SortingBits _savegame_sort_order; void ShowSaveLoadDialog(SaveLoadDialogMode mode); -void FiosGetSavegameList(SaveLoadDialogMode mode); -void FiosGetScenarioList(SaveLoadDialogMode mode); -void FiosGetHeightmapList(SaveLoadDialogMode mode); +void FiosGetSavegameList(SaveLoadDialogMode mode, FileList &file_list); +void FiosGetScenarioList(SaveLoadDialogMode mode, FileList &file_list); +void FiosGetHeightmapList(SaveLoadDialogMode mode, FileList &file_list); const char *FiosBrowseTo(const FiosItem *item); diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index b01954c557..fdd38da6ca 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -201,12 +201,12 @@ void BuildFileList(SaveLoadDialogMode mode) switch (mode) { case SLD_LOAD_SCENARIO: case SLD_SAVE_SCENARIO: - FiosGetScenarioList(mode); break; + FiosGetScenarioList(mode, _fios_items); break; case SLD_SAVE_HEIGHTMAP: case SLD_LOAD_HEIGHTMAP: - FiosGetHeightmapList(mode); break; + FiosGetHeightmapList(mode, _fios_items); break; - default: FiosGetSavegameList(mode); break; + default: FiosGetSavegameList(mode, _fios_items); break; } /* Invalidate saveload window */ From f618c0d0a581b4d3893d73ddc48d3d621198ec4b Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:54:30 +0000 Subject: [PATCH 11/28] (svn r27643) -Codechange: FiosGetDrives function also takes a destination file list. --- src/fios.cpp | 4 ++-- src/os/os2/os2.cpp | 4 ++-- src/os/unix/unix.cpp | 3 ++- src/os/windows/win32.cpp | 6 +++--- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/fios.cpp b/src/fios.cpp index 529890810c..271f270762 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -38,7 +38,7 @@ SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING; extern bool FiosIsRoot(const char *path); extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb); extern bool FiosIsHiddenFile(const struct dirent *ent); -extern void FiosGetDrives(); +extern void FiosGetDrives(FileList &file_list); extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot); /* get the name of an oldstyle savegame */ @@ -354,7 +354,7 @@ static void FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc QSortT(file_list.Get(sort_start), file_list.Length() - sort_start, CompareFiosItems); /* Show drives */ - FiosGetDrives(); + FiosGetDrives(file_list); file_list.Compact(); } diff --git a/src/os/os2/os2.cpp b/src/os/os2/os2.cpp index 386cc4dbb7..7b34f528a6 100644 --- a/src/os/os2/os2.cpp +++ b/src/os/os2/os2.cpp @@ -45,7 +45,7 @@ bool FiosIsRoot(const char *file) return file[3] == '\0'; } -void FiosGetDrives() +void FiosGetDrives(FileList &file_list) { uint disk, disk2, save, total; @@ -75,7 +75,7 @@ void FiosGetDrives() #endif if (disk == disk2) { - FiosItem *fios = _fios_items.Append(); + FiosItem *fios = file_list.Append(); fios->type = FIOS_TYPE_DRIVE; fios->mtime = 0; #ifndef __INNOTEK_LIBC__ diff --git a/src/os/unix/unix.cpp b/src/os/unix/unix.cpp index 24dedb2ee1..d7c2304ce5 100644 --- a/src/os/unix/unix.cpp +++ b/src/os/unix/unix.cpp @@ -16,6 +16,7 @@ #include "../../core/random_func.hpp" #include "../../debug.h" #include "../../string_func.h" +#include "../../fios.h" #include @@ -77,7 +78,7 @@ bool FiosIsRoot(const char *path) #endif } -void FiosGetDrives() +void FiosGetDrives(FileList &file_list) { return; } diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index 041468a139..344d243143 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -208,11 +208,11 @@ bool FiosIsRoot(const char *file) return file[3] == '\0'; // C:\... } -void FiosGetDrives() +void FiosGetDrives(FileList &file_list) { #if defined(WINCE) /* WinCE only knows one drive: / */ - FiosItem *fios = _fios_items.Append(); + FiosItem *fios = file_list.Append(); fios->type = FIOS_TYPE_DRIVE; fios->mtime = 0; seprintf(fios->name, lastof(fios->name), PATHSEP ""); @@ -223,7 +223,7 @@ void FiosGetDrives() GetLogicalDriveStrings(lengthof(drives), drives); for (s = drives; *s != '\0';) { - FiosItem *fios = _fios_items.Append(); + FiosItem *fios = file_list.Append(); fios->type = FIOS_TYPE_DRIVE; fios->mtime = 0; seprintf(fios->name, lastof(fios->name), "%c:", s[0] & 0xFF); From 8a9e9f838bf18d419f26f1a2b47a37c4a6485751 Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:54:52 +0000 Subject: [PATCH 12/28] (svn r27644) -Codechange: Split GetFiosItem into BuildFileList and FindItem, and move both to FileList. --- src/console_cmds.cpp | 48 ++++++---------------------------------- src/fios.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++ src/fios.h | 5 +++-- src/fios_gui.cpp | 25 ++++----------------- 4 files changed, 66 insertions(+), 64 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index ae6e5458e1..28c540dacc 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -315,42 +315,6 @@ DEF_CONSOLE_CMD(ConSaveConfig) return true; } -/** - * Get savegame file informations. - * @param file The savegame filename to return information about. Can be the actual name - * or a numbered entry into the filename list. - * @return FiosItem The information on the file. - */ -static const FiosItem *GetFiosItem(const char *file) -{ - _saveload_mode = SLD_LOAD_GAME; - BuildFileList(_saveload_mode); - - for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) { - if (strcmp(file, item->name) == 0) return item; - if (strcmp(file, item->title) == 0) return item; - } - - /* If no name matches, try to parse it as number */ - char *endptr; - int i = strtol(file, &endptr, 10); - if (file == endptr || *endptr != '\0') i = -1; - - if (IsInsideMM(i, 0, _fios_items.Length())) return _fios_items.Get(i); - - /* As a last effort assume it is an OpenTTD savegame and - * that the ".sav" part was not given. */ - char long_file[MAX_PATH]; - seprintf(long_file, lastof(long_file), "%s.sav", file); - for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) { - if (strcmp(long_file, item->name) == 0) return item; - if (strcmp(long_file, item->title) == 0) return item; - } - - return NULL; -} - - DEF_CONSOLE_CMD(ConLoad) { if (argc == 0) { @@ -361,7 +325,8 @@ DEF_CONSOLE_CMD(ConLoad) if (argc != 2) return false; const char *file = argv[1]; - const FiosItem *item = GetFiosItem(file); + _fios_items.BuildFileList(SLD_LOAD_GAME); + const FiosItem *item = _fios_items.FindItem(file); if (item != NULL) { switch (item->type) { case FIOS_TYPE_FILE: case FIOS_TYPE_OLDFILE: { @@ -393,7 +358,8 @@ DEF_CONSOLE_CMD(ConRemove) if (argc != 2) return false; const char *file = argv[1]; - const FiosItem *item = GetFiosItem(file); + _fios_items.BuildFileList(SLD_LOAD_GAME); + const FiosItem *item = _fios_items.FindItem(file); if (item != NULL) { if (!FiosDelete(item->name)) { IConsolePrintF(CC_ERROR, "%s: Failed to delete file", file); @@ -415,8 +381,7 @@ DEF_CONSOLE_CMD(ConListFiles) return true; } - BuildFileList(_saveload_mode); - + _fios_items.BuildFileList(_saveload_mode); for (uint i = 0; i < _fios_items.Length(); i++) { IConsolePrintF(CC_DEFAULT, "%d) %s", i, _fios_items[i].title); } @@ -436,7 +401,8 @@ DEF_CONSOLE_CMD(ConChangeDirectory) if (argc != 2) return false; const char *file = argv[1]; - const FiosItem *item = GetFiosItem(file); + _fios_items.BuildFileList(SLD_LOAD_GAME); + const FiosItem *item = _fios_items.FindItem(file); if (item != NULL) { switch (item->type) { case FIOS_TYPE_DIR: case FIOS_TYPE_DRIVE: case FIOS_TYPE_PARENT: diff --git a/src/fios.cpp b/src/fios.cpp index 271f270762..c670ee3391 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -69,6 +69,58 @@ FileList::~FileList() this->Clear(); } +/** + * Construct a file list containing file appropriate for the specified \a mode. + * @param mode Kind of files required in the list. + */ +void FileList::BuildFileList(SaveLoadDialogMode mode) +{ + this->Clear(); + + switch (mode) { + case SLD_LOAD_SCENARIO: + case SLD_SAVE_SCENARIO: + FiosGetScenarioList(mode, *this); break; + case SLD_SAVE_HEIGHTMAP: + case SLD_LOAD_HEIGHTMAP: + FiosGetHeightmapList(mode, *this); break; + + default: FiosGetSavegameList(mode, *this); break; + } +} + +/** + * Find file information of a file by its name from the file list. + * @param file The filename to return information about. Can be the actual name + * or a numbered entry into the filename list. + * @return The information on the file, or \c NULL if the file is not available. + */ +const FiosItem *FileList::FindItem(const char *file) +{ + for (const FiosItem *item = this->Begin(); item != this->End(); item++) { + if (strcmp(file, item->name) == 0) return item; + if (strcmp(file, item->title) == 0) return item; + } + + /* If no name matches, try to parse it as number */ + char *endptr; + int i = strtol(file, &endptr, 10); + if (file == endptr || *endptr != '\0') i = -1; + + if (IsInsideMM(i, 0, this->Length())) return this->Get(i); + + /* As a last effort assume it is an OpenTTD savegame and + * that the ".sav" part was not given. */ + char long_file[MAX_PATH]; + seprintf(long_file, lastof(long_file), "%s.sav", file); + for (const FiosItem *item = this->Begin(); item != this->End(); item++) { + if (strcmp(long_file, item->name) == 0) return item; + if (strcmp(long_file, item->title) == 0) return item; + } + + return NULL; +} + /** * Get descriptive texts. Returns the path and free space * left on the device diff --git a/src/fios.h b/src/fios.h index 888e3c2362..c1da5720f0 100644 --- a/src/fios.h +++ b/src/fios.h @@ -200,6 +200,9 @@ public: this->files.Compact(); } + void BuildFileList(SaveLoadDialogMode mode); + const FiosItem *FindItem(const char *file); + SmallVector files; ///< The list of files. }; @@ -235,6 +238,4 @@ int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b); extern const TextColour _fios_colours[]; -void BuildFileList(SaveLoadDialogMode mode); - #endif /* FIOS_H */ diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index fdd38da6ca..179a397efb 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -193,26 +193,6 @@ const TextColour _fios_colours[] = { TC_ORANGE, TC_LIGHT_BROWN, TC_ORANGE, TC_ORANGE, TC_YELLOW }; -void BuildFileList(SaveLoadDialogMode mode) -{ - _fios_path_changed = true; - _fios_items.Clear(); - - switch (mode) { - case SLD_LOAD_SCENARIO: - case SLD_SAVE_SCENARIO: - FiosGetScenarioList(mode, _fios_items); break; - case SLD_SAVE_HEIGHTMAP: - case SLD_LOAD_HEIGHTMAP: - FiosGetHeightmapList(mode, _fios_items); break; - - default: FiosGetSavegameList(mode, _fios_items); break; - } - - /* Invalidate saveload window */ - InvalidateWindowData(WC_SAVELOAD, 0, 2, true); -} - static void MakeSortedSaveGameList() { uint sort_start = 0; @@ -683,7 +663,10 @@ public: this->selected = NULL; _load_check_data.Clear(); if (!gui_scope) break; - BuildFileList(_saveload_mode); + + _fios_path_changed = true; + _fios_items.BuildFileList(_saveload_mode); + InvalidateWindowData(WC_SAVELOAD, 0, 2, true); /* FALL THROUGH */ case 1: /* Selection changes */ From 76cd96e709018157434db350d0cee88801a8c107 Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:55:21 +0000 Subject: [PATCH 13/28] (svn r27645) -Add: Give console commands their own file list storage. --- src/console_cmds.cpp | 60 +++++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 28c540dacc..7d2a99bf8e 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -45,6 +45,38 @@ /* scriptfile handling */ static bool _script_running; ///< Script is running (used to abort execution when #ConReturn is encountered). +/** File list storage for the console, for caching the last 'ls' command. */ +class ConsoleFileList : public FileList { +public: + ConsoleFileList() : FileList() + { + this->file_list_valid = false; + } + + /** Declare the file storage cache as being invalid, also clears all stored files. */ + void InvalidateFileList() + { + this->Clear(); + this->file_list_valid = false; + } + + /** + * (Re-)validate the file storage cache. Only makes a change if the storage was invalid, or if \a force_reload. + * @param Always reload the file storage cache. + */ + void ValidateFileList(bool force_reload = false) + { + if (force_reload || !this->file_list_valid) { + this->BuildFileList(SLD_LOAD_GAME); + this->file_list_valid = true; + } + } + + bool file_list_valid; ///< If set, the file list is valid. +}; + +static ConsoleFileList _console_file_list; ///< File storage cache for the console. + /* console command defines */ #define DEF_CONSOLE_CMD(function) static bool function(byte argc, char *argv[]) #define DEF_CONSOLE_HOOK(function) static ConsoleHookResult function(bool echo) @@ -325,8 +357,8 @@ DEF_CONSOLE_CMD(ConLoad) if (argc != 2) return false; const char *file = argv[1]; - _fios_items.BuildFileList(SLD_LOAD_GAME); - const FiosItem *item = _fios_items.FindItem(file); + _console_file_list.ValidateFileList(); + const FiosItem *item = _console_file_list.FindItem(file); if (item != NULL) { switch (item->type) { case FIOS_TYPE_FILE: case FIOS_TYPE_OLDFILE: { @@ -343,7 +375,6 @@ DEF_CONSOLE_CMD(ConLoad) IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file); } - _fios_items.Clear(); return true; } @@ -358,8 +389,8 @@ DEF_CONSOLE_CMD(ConRemove) if (argc != 2) return false; const char *file = argv[1]; - _fios_items.BuildFileList(SLD_LOAD_GAME); - const FiosItem *item = _fios_items.FindItem(file); + _console_file_list.ValidateFileList(); + const FiosItem *item = _console_file_list.FindItem(file); if (item != NULL) { if (!FiosDelete(item->name)) { IConsolePrintF(CC_ERROR, "%s: Failed to delete file", file); @@ -368,7 +399,7 @@ DEF_CONSOLE_CMD(ConRemove) IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file); } - _fios_items.Clear(); + _console_file_list.InvalidateFileList(); return true; } @@ -381,12 +412,11 @@ DEF_CONSOLE_CMD(ConListFiles) return true; } - _fios_items.BuildFileList(_saveload_mode); - for (uint i = 0; i < _fios_items.Length(); i++) { - IConsolePrintF(CC_DEFAULT, "%d) %s", i, _fios_items[i].title); + _console_file_list.ValidateFileList(true); + for (uint i = 0; i < _console_file_list.Length(); i++) { + IConsolePrintF(CC_DEFAULT, "%d) %s", i, _console_file_list[i].title); } - _fios_items.Clear(); return true; } @@ -401,8 +431,8 @@ DEF_CONSOLE_CMD(ConChangeDirectory) if (argc != 2) return false; const char *file = argv[1]; - _fios_items.BuildFileList(SLD_LOAD_GAME); - const FiosItem *item = _fios_items.FindItem(file); + _console_file_list.ValidateFileList(true); + const FiosItem *item = _console_file_list.FindItem(file); if (item != NULL) { switch (item->type) { case FIOS_TYPE_DIR: case FIOS_TYPE_DRIVE: case FIOS_TYPE_PARENT: @@ -414,7 +444,7 @@ DEF_CONSOLE_CMD(ConChangeDirectory) IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file); } - _fios_items.Clear(); + _console_file_list.InvalidateFileList(); return true; } @@ -428,8 +458,8 @@ DEF_CONSOLE_CMD(ConPrintWorkingDirectory) } /* XXX - Workaround for broken file handling */ - FiosGetSavegameList(SLD_LOAD_GAME, _fios_items); - _fios_items.Clear(); + _console_file_list.ValidateFileList(true); + _console_file_list.InvalidateFileList(); FiosGetDescText(&path, NULL); IConsolePrint(CC_DEFAULT, path); From ac72a6b1ede84941966f06de81fe00549bd9e97f Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:55:54 +0000 Subject: [PATCH 14/28] (svn r27646) -Codechange: Move _fios_items variable into the SaveLoadWindow class. --- src/fios.cpp | 1 - src/fios.h | 1 - src/fios_gui.cpp | 38 +++++++++++++++++++------------------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/fios.cpp b/src/fios.cpp index c670ee3391..af89ef0491 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -29,7 +29,6 @@ #include "safeguards.h" /* Variables to display file lists */ -FileList _fios_items; static char *_fios_path; static const char *_fios_path_last; SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING; diff --git a/src/fios.h b/src/fios.h index c1da5720f0..37d562ca1a 100644 --- a/src/fios.h +++ b/src/fios.h @@ -215,7 +215,6 @@ enum SortingBits { DECLARE_ENUM_AS_BIT_SET(SortingBits) /* Variables to display file lists */ -extern FileList _fios_items; extern SaveLoadDialogMode _saveload_mode; extern SortingBits _savegame_sort_order; diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 179a397efb..c9d612f4c2 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -193,7 +193,11 @@ const TextColour _fios_colours[] = { TC_ORANGE, TC_LIGHT_BROWN, TC_ORANGE, TC_ORANGE, TC_YELLOW }; -static void MakeSortedSaveGameList() +/** + * Sort the collected list save games prior to displaying it in the save/load gui. + * @param [inout] file_list List of save game files found in the directory. + */ +static void SortSaveGameList(FileList &file_list) { uint sort_start = 0; uint sort_end = 0; @@ -202,7 +206,7 @@ static void MakeSortedSaveGameList() * Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE) * Only sort savegames/scenarios, not directories */ - for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) { + for (const FiosItem *item = file_list.Begin(); item != file_list.End(); item++) { switch (item->type) { case FIOS_TYPE_DIR: sort_start++; break; case FIOS_TYPE_PARENT: sort_start++; break; @@ -211,15 +215,16 @@ static void MakeSortedSaveGameList() } } - uint s_amount = _fios_items.Length() - sort_start - sort_end; - QSortT(_fios_items.Get(sort_start), s_amount, CompareFiosItems); + uint s_amount = file_list.Length() - sort_start - sort_end; + QSortT(file_list.Get(sort_start), s_amount, CompareFiosItems); } struct SaveLoadWindow : public Window { private: QueryString filename_editbox; ///< Filename editbox. + FileList fios_items; ///< Save game list. FiosItem o_dir; - const FiosItem *selected; + const FiosItem *selected; ///< Selected game in #fios_items, or \c NULL. Scrollbar *vscroll; public: @@ -307,7 +312,6 @@ public: if (!_networking && _game_mode != GM_EDITOR && _game_mode != GM_MENU) { DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE); } - _fios_items.Clear(); } virtual void DrawWidget(const Rect &r, int widget) const @@ -340,8 +344,8 @@ public: GfxFillRect(r.left + 1, r.top + 1, r.right, r.bottom, PC_BLACK); uint y = r.top + WD_FRAMERECT_TOP; - for (uint pos = this->vscroll->GetPosition(); pos < _fios_items.Length(); pos++) { - const FiosItem *item = _fios_items.Get(pos); + for (uint pos = this->vscroll->GetPosition(); pos < this->fios_items.Length(); pos++) { + const FiosItem *item = this->fios_items.Get(pos); if (item == this->selected) { GfxFillRect(r.left + 1, y, r.right, y + this->resize.step_height, PC_DARK_BLUE); @@ -475,10 +479,10 @@ public: { if (_savegame_sort_dirty) { _savegame_sort_dirty = false; - MakeSortedSaveGameList(); + SortSaveGameList(this->fios_items); } - this->vscroll->SetCount(_fios_items.Length()); + this->vscroll->SetCount(this->fios_items.Length()); this->DrawWidgets(); } @@ -543,7 +547,7 @@ public: int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SL_DRIVES_DIRECTORIES_LIST, WD_FRAMERECT_TOP); if (y == INT_MAX) return; - const FiosItem *file = _fios_items.Get(y); + const FiosItem *file = this->fios_items.Get(y); const char *name = FiosBrowseTo(file); if (name != NULL) { @@ -665,8 +669,10 @@ public: if (!gui_scope) break; _fios_path_changed = true; - _fios_items.BuildFileList(_saveload_mode); - InvalidateWindowData(WC_SAVELOAD, 0, 2, true); + this->fios_items.BuildFileList(_saveload_mode); + this->vscroll->SetCount(this->fios_items.Length()); + this->selected = NULL; + _load_check_data.Clear(); /* FALL THROUGH */ case 1: /* Selection changes */ @@ -683,12 +689,6 @@ public: !_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility == GLC_ALL_GOOD); } break; - case 2: - /* _fios_items changed */ - this->vscroll->SetCount(_fios_items.Length()); - this->selected = NULL; - _load_check_data.Clear(); - break; } } }; From ec2b2fad58d36954a18722497875d3991e60f8a0 Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:56:23 +0000 Subject: [PATCH 15/28] (svn r27647) -Codechange: Introduce file operations, and use it to replace most of SaveLoadDialogMode --- src/console_cmds.cpp | 2 +- src/fileio_type.h | 6 +++ src/fios.cpp | 95 ++++++++++++++++++++++++-------------------- src/fios.h | 10 ++--- src/fios_gui.cpp | 14 ++++++- src/openttd.cpp | 4 +- 6 files changed, 80 insertions(+), 51 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 7d2a99bf8e..6284287c04 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -67,7 +67,7 @@ public: void ValidateFileList(bool force_reload = false) { if (force_reload || !this->file_list_valid) { - this->BuildFileList(SLD_LOAD_GAME); + this->BuildFileList(FT_SAVEGAME, FOP_LOAD); this->file_list_valid = true; } } diff --git a/src/fileio_type.h b/src/fileio_type.h index 3aee5a0e76..20df8cd05d 100644 --- a/src/fileio_type.h +++ b/src/fileio_type.h @@ -24,6 +24,12 @@ enum AbstractFileType { 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. */ enum FiosType { FIOS_TYPE_DRIVE, diff --git a/src/fios.cpp b/src/fios.cpp index af89ef0491..987542acc9 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -69,22 +69,33 @@ FileList::~FileList() } /** - * Construct a file list containing file appropriate for the specified \a mode. - * @param mode Kind of files required in the list. + * Construct a file list with the given kind of files, for the stated purpose. + * @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(); - switch (mode) { - case SLD_LOAD_SCENARIO: - case SLD_SAVE_SCENARIO: - FiosGetScenarioList(mode, *this); break; - case SLD_SAVE_HEIGHTMAP: - case SLD_LOAD_HEIGHTMAP: - FiosGetHeightmapList(mode, *this); break; + assert(fop == FOP_LOAD || FOP_SAVE); + switch (abstract_filetype) { + case FT_NONE: + 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; } -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. */ 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 FileList &file_list; ///< Destination of the found files. public: /** * Create the scanner - * @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 file_list Destination of the found files. + * @param fop Purpose of collecting the list. + * @param callback_proc The function that is called where you need to do the filtering. + * @param file_list Destination of the found files. */ - FiosFileScanner(SaveLoadDialogMode mode, fios_getlist_callback_proc *callback_proc, FileList &file_list) : - mode(mode), callback_proc(callback_proc), file_list(file_list) + FiosFileScanner(FileOperation fop, fios_getlist_callback_proc *callback_proc, FileList &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); @@ -300,7 +311,7 @@ bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, cons char fios_title[64]; 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; 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. - * @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. - * @param file_list Destination of the found files. + * @param fop Purpose of collecting the list. + * @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 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 dirent *dirent; @@ -395,7 +406,7 @@ static void FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc sort_start = file_list.Length(); /* Show files */ - FiosFileScanner scanner(mode, callback_proc, file_list); + FiosFileScanner scanner(fop, callback_proc, file_list); if (subdir == NO_DIRECTORY) { scanner.Scan(NULL, _fios_path, false); } 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. - * @param mode Save/load mode. + * @param fop Purpose of collecting the list. * @param file Name of the file to check. * @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 @@ -445,7 +456,7 @@ static void GetFileTitle(const char *file, char *title, const char *last, Subdir * @see FiosGetFileList * @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 * .SAV OpenTTD saved game @@ -461,7 +472,7 @@ FiosType FiosGetSavegameListCallback(SaveLoadDialogMode mode, const char *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 || strcasecmp(ext, ".sv2") == 0) { if (title != NULL) GetOldSaveGameName(file, title, last); @@ -474,11 +485,11 @@ FiosType FiosGetSavegameListCallback(SaveLoadDialogMode mode, const char *file, /** * 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. * @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_last = NULL; @@ -492,12 +503,12 @@ void FiosGetSavegameList(SaveLoadDialogMode mode, FileList &file_list) _fios_path = fios_save_path; _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. - * @param mode Save/load mode. + * @param fop Purpose of collecting the list. * @param file Name of the file to check. * @param ext A pointer to the extension identifier inside 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 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 * .SCN OpenTTD style scenario file @@ -517,7 +528,7 @@ static FiosType FiosGetScenarioListCallback(SaveLoadDialogMode mode, const char 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 ) { GetOldSaveGameName(file, title, last); return FIOS_TYPE_OLD_SCENARIO; @@ -529,11 +540,11 @@ static FiosType FiosGetScenarioListCallback(SaveLoadDialogMode mode, const char /** * 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. * @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_last = NULL; @@ -551,11 +562,11 @@ void FiosGetScenarioList(SaveLoadDialogMode mode, FileList &file_list) char base_path[MAX_PATH]; FioGetDirectory(base_path, lastof(base_path), SCENARIO_DIR); - Subdirectory subdir = (mode == SLD_LOAD_SCENARIO && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY; - FiosGetFileList(mode, &FiosGetScenarioListCallback, subdir, file_list); + Subdirectory subdir = (fop == FOP_LOAD && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY; + 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 * .PNG PNG Based heightmap files @@ -601,10 +612,10 @@ static FiosType FiosGetHeightmapListCallback(SaveLoadDialogMode mode, const char /** * 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. */ -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_last = NULL; @@ -622,7 +633,7 @@ void FiosGetHeightmapList(SaveLoadDialogMode mode, FileList &file_list) FioGetDirectory(base_path, lastof(base_path), HEIGHTMAP_DIR); Subdirectory subdir = strcmp(base_path, _fios_path) == 0 ? HEIGHTMAP_DIR : NO_DIRECTORY; - FiosGetFileList(mode, &FiosGetHeightmapListCallback, subdir, file_list); + FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list); } /** diff --git a/src/fios.h b/src/fios.h index 37d562ca1a..1c61492af4 100644 --- a/src/fios.h +++ b/src/fios.h @@ -200,7 +200,7 @@ public: this->files.Compact(); } - void BuildFileList(SaveLoadDialogMode mode); + void BuildFileList(AbstractFileType abstract_filetype, FileOperation fop); const FiosItem *FindItem(const char *file); SmallVector files; ///< The list of files. @@ -220,9 +220,9 @@ extern SortingBits _savegame_sort_order; void ShowSaveLoadDialog(SaveLoadDialogMode mode); -void FiosGetSavegameList(SaveLoadDialogMode mode, FileList &file_list); -void FiosGetScenarioList(SaveLoadDialogMode mode, FileList &file_list); -void FiosGetHeightmapList(SaveLoadDialogMode mode, FileList &file_list); +void FiosGetSavegameList(FileOperation fop, FileList &file_list); +void FiosGetScenarioList(FileOperation fop, FileList &file_list); +void FiosGetHeightmapList(FileOperation fop, FileList &file_list); 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 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); diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index c9d612f4c2..e217ab6e11 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -669,7 +669,19 @@ public: if (!gui_scope) break; _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->selected = NULL; _load_check_data.Clear(); diff --git a/src/openttd.cpp b/src/openttd.cpp index 594b6fa37e..bd8ac42532 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -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 */ const char *t = strrchr(_file_to_saveload.name, '.'); 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); } @@ -647,7 +647,7 @@ int openttd_main(int argc, char *argv[]) char title[80]; 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(); SaveOrLoadResult res = SaveOrLoad(mgo.opt, SL_LOAD_CHECK, SAVE_DIR, false); From 3b848277736ace422da3a743d3c1374466abc7e3 Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:56:56 +0000 Subject: [PATCH 16/28] (svn r27648) -Codechange: Remove remaining _saveload_mode usage. --- src/fios.h | 13 +-- src/fios_gui.cpp | 212 ++++++++++++++++++------------------ src/intro_gui.cpp | 6 +- src/network/network_gui.cpp | 6 +- src/toolbar_gui.cpp | 14 +-- 5 files changed, 123 insertions(+), 128 deletions(-) diff --git a/src/fios.h b/src/fios.h index 1c61492af4..86bcf3e3a0 100644 --- a/src/fios.h +++ b/src/fios.h @@ -97,16 +97,6 @@ enum FileSlots { MAX_FILE_SLOTS = 64 }; -/** Mode of the file dialogue window. */ -enum SaveLoadDialogMode { - SLD_LOAD_GAME, ///< Load a game. - SLD_LOAD_SCENARIO, ///< Load a scenario. - SLD_SAVE_GAME, ///< Save a game. - SLD_SAVE_SCENARIO, ///< Save a scenario. - SLD_LOAD_HEIGHTMAP, ///< Load a heightmap. - SLD_SAVE_HEIGHTMAP, ///< Save a heightmap. -}; - /** Deals with finding savegames */ struct FiosItem { FiosType type; @@ -215,10 +205,9 @@ enum SortingBits { DECLARE_ENUM_AS_BIT_SET(SortingBits) /* Variables to display file lists */ -extern SaveLoadDialogMode _saveload_mode; extern SortingBits _savegame_sort_order; -void ShowSaveLoadDialog(SaveLoadDialogMode mode); +void ShowSaveLoadDialog(AbstractFileType abstract_filetype, FileOperation fop); void FiosGetSavegameList(FileOperation fop, FileList &file_list); void FiosGetScenarioList(FileOperation fop, FileList &file_list); diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index e217ab6e11..e902941d65 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -36,7 +36,6 @@ #include "safeguards.h" -SaveLoadDialogMode _saveload_mode; LoadCheckData _load_check_data; ///< Data loaded from save during SL_LOAD_CHECK. static bool _fios_path_changed; @@ -222,6 +221,8 @@ static void SortSaveGameList(FileList &file_list) struct SaveLoadWindow : public Window { private: QueryString filename_editbox; ///< Filename editbox. + AbstractFileType abstract_filetype; /// Type of file to select. + FileOperation fop; ///< File operation to perform. FileList fios_items; ///< Save game list. FiosItem o_dir; const FiosItem *selected; ///< Selected game in #fios_items, or \c NULL. @@ -235,35 +236,56 @@ public: this->filename_editbox.text.UpdateSize(); } - SaveLoadWindow(WindowDesc *desc, SaveLoadDialogMode mode) : Window(desc), filename_editbox(64) + SaveLoadWindow(WindowDesc *desc, AbstractFileType abstract_filetype, FileOperation fop) + : Window(desc), filename_editbox(64), abstract_filetype(abstract_filetype), fop(fop) { - static const StringID saveload_captions[] = { - STR_SAVELOAD_LOAD_CAPTION, - STR_SAVELOAD_LOAD_SCENARIO, - STR_SAVELOAD_SAVE_CAPTION, - STR_SAVELOAD_SAVE_SCENARIO, - STR_SAVELOAD_LOAD_HEIGHTMAP, - STR_SAVELOAD_SAVE_HEIGHTMAP, - }; - assert((uint)mode < lengthof(saveload_captions)); + assert(this->fop == FOP_SAVE || this->fop == FOP_LOAD); - /* Use an array to define what will be the current file type being handled - * by current file mode */ - switch (mode) { - case SLD_SAVE_GAME: this->GenerateFileName(); break; - case SLD_SAVE_HEIGHTMAP: - case SLD_SAVE_SCENARIO: this->filename_editbox.text.Assign("UNNAMED"); break; - default: break; + /* For saving, construct an initial file name. */ + if (this->fop == FOP_SAVE) { + switch (this->abstract_filetype) { + case FT_SAVEGAME: + this->GenerateFileName(); + break; + + case FT_SCENARIO: + case FT_HEIGHTMAP: + this->filename_editbox.text.Assign("UNNAMED"); + break; + + default: + NOT_REACHED(); + } } - this->querystrings[WID_SL_SAVE_OSK_TITLE] = &this->filename_editbox; this->filename_editbox.ok_button = WID_SL_SAVE_GAME; this->CreateNestedTree(true); - if (mode == SLD_LOAD_GAME) this->GetWidget(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL); - this->GetWidget(WID_SL_CAPTION)->widget_data = saveload_captions[mode]; - this->vscroll = this->GetScrollbar(WID_SL_SCROLLBAR); + if (this->fop == FOP_LOAD && this->abstract_filetype == FT_SAVEGAME) { + this->GetWidget(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL); + } + /* Select caption string of the window. */ + StringID caption_string; + switch (this->abstract_filetype) { + case FT_SAVEGAME: + caption_string = (this->fop == FOP_SAVE) ? STR_SAVELOAD_SAVE_CAPTION : STR_SAVELOAD_LOAD_CAPTION; + break; + + case FT_SCENARIO: + caption_string = (this->fop == FOP_SAVE) ? STR_SAVELOAD_SAVE_SCENARIO : STR_SAVELOAD_LOAD_SCENARIO; + break; + + case FT_HEIGHTMAP: + caption_string = (this->fop == FOP_SAVE) ? STR_SAVELOAD_SAVE_HEIGHTMAP : STR_SAVELOAD_LOAD_HEIGHTMAP; + break; + + default: + NOT_REACHED(); + } + this->GetWidget(WID_SL_CAPTION)->widget_data = caption_string; + + this->vscroll = this->GetScrollbar(WID_SL_SCROLLBAR); this->FinishInitNested(0); this->LowerWidget(WID_SL_DRIVES_DIRECTORIES_LIST); @@ -279,20 +301,18 @@ public: ResetObjectToPlace(); + /* Select the initial directory. */ o_dir.type = FIOS_TYPE_DIRECT; - switch (_saveload_mode) { - case SLD_SAVE_GAME: - case SLD_LOAD_GAME: + switch (this->abstract_filetype) { + case FT_SAVEGAME: FioGetDirectory(o_dir.name, lastof(o_dir.name), SAVE_DIR); break; - case SLD_SAVE_SCENARIO: - case SLD_LOAD_SCENARIO: + case FT_SCENARIO: FioGetDirectory(o_dir.name, lastof(o_dir.name), SCENARIO_DIR); break; - case SLD_SAVE_HEIGHTMAP: - case SLD_LOAD_HEIGHTMAP: + case FT_HEIGHTMAP: FioGetDirectory(o_dir.name, lastof(o_dir.name), HEIGHTMAP_DIR); break; @@ -301,9 +321,7 @@ public: } /* Focus the edit box by default in the save windows */ - if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) { - this->SetFocusedWidget(WID_SL_SAVE_OSK_TITLE); - } + if (this->fop == FOP_SAVE) this->SetFocusedWidget(WID_SL_SAVE_OSK_TITLE); } virtual ~SaveLoadWindow() @@ -405,7 +423,7 @@ public: if (y > y_max) break; /* Hide current date for scenarios */ - if (_saveload_mode != SLD_LOAD_SCENARIO && _saveload_mode != SLD_SAVE_SCENARIO) { + if (this->abstract_filetype != FT_SCENARIO) { /* Current date */ SetDParam(0, _load_check_data.current_date); DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CURRENT_DATE); @@ -413,7 +431,7 @@ public: } /* Hide the NewGRF stuff when saving. We also hide the button. */ - if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) { + if (this->fop == FOP_LOAD && (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO)) { y += WD_PAR_VSEP_NORMAL; if (y > y_max) break; @@ -426,7 +444,7 @@ public: if (y > y_max) break; /* Hide the company stuff for scenarios */ - if (_saveload_mode != SLD_LOAD_SCENARIO && _saveload_mode != SLD_SAVE_SCENARIO) { + if (this->abstract_filetype != FT_SCENARIO) { y += FONT_HEIGHT_NORMAL; if (y > y_max) break; @@ -516,9 +534,10 @@ public: strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name)); strecpy(_file_to_saveload.title, this->selected->title, lastof(_file_to_saveload.title)); - if (_saveload_mode == SLD_LOAD_HEIGHTMAP) { + if (this->abstract_filetype == FT_HEIGHTMAP) { delete this; ShowHeightmapLoad(); + } else if (!_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs()) { _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_SCENARIO : SM_LOAD_GAME; ClearErrorMessages(); @@ -562,22 +581,25 @@ public: this->InvalidateData(1); } - if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) { + if (this->fop == FOP_SAVE) { /* Copy clicked name to editbox */ this->filename_editbox.text.Assign(file->title); this->SetWidgetDirty(WID_SL_SAVE_OSK_TITLE); } } else if (!_load_check_data.HasErrors()) { this->selected = file; - if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) { - this->OnClick(pt, WID_SL_LOAD_BUTTON, 1); - } else if (_saveload_mode == SLD_LOAD_HEIGHTMAP) { - _file_to_saveload.SetMode(file->type); - strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name)); - strecpy(_file_to_saveload.title, file->title, lastof(_file_to_saveload.title)); + if (this->fop == FOP_LOAD) { + if (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO) { + this->OnClick(pt, WID_SL_LOAD_BUTTON, 1); + } else { + assert(this->abstract_filetype == FT_HEIGHTMAP); + _file_to_saveload.SetMode(file->type); + strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name)); + strecpy(_file_to_saveload.title, file->title, lastof(_file_to_saveload.title)); - delete this; - ShowHeightmapLoad(); + delete this; + ShowHeightmapLoad(); + } } } } else { @@ -592,10 +614,11 @@ public: ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR); } else { #if defined(ENABLE_NETWORK) - switch (_saveload_mode) { + assert(this->fop == FOP_LOAD); + switch (this->abstract_filetype) { default: NOT_REACHED(); - case SLD_LOAD_SCENARIO: ShowNetworkContentListWindow(NULL, CONTENT_TYPE_SCENARIO); break; - case SLD_LOAD_HEIGHTMAP: ShowNetworkContentListWindow(NULL, CONTENT_TYPE_HEIGHTMAP); break; + case FT_SCENARIO: ShowNetworkContentListWindow(NULL, CONTENT_TYPE_SCENARIO); break; + case FT_HEIGHTMAP: ShowNetworkContentListWindow(NULL, CONTENT_TYPE_HEIGHTMAP); break; } #endif } @@ -623,9 +646,8 @@ public: virtual void OnTimeout() { - /* This test protects against using widgets 11 and 12 which are only available - * in those saveload modes. */ - if (!(_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP)) return; + /* Widgets WID_SL_DELETE_SELECTION and WID_SL_SAVE_GAME only exist when saving to a file. */ + if (this->fop != FOP_SAVE) return; if (this->IsWidgetLowered(WID_SL_DELETE_SELECTION)) { // Delete button clicked if (!FiosDelete(this->filename_editbox.text.buf)) { @@ -633,10 +655,10 @@ public: } else { this->InvalidateData(); /* Reset file name to current date on successful delete */ - if (_saveload_mode == SLD_SAVE_GAME) GenerateFileName(); + if (this->abstract_filetype == FT_SAVEGAME) GenerateFileName(); } } else if (this->IsWidgetLowered(WID_SL_SAVE_GAME)) { // Save button clicked - if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) { + if (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO) { _switch_mode = SM_SAVE_GAME; FiosMakeSavegameName(_file_to_saveload.name, this->filename_editbox.text.buf, lastof(_file_to_saveload.name)); } else { @@ -669,19 +691,7 @@ public: if (!gui_scope) break; _fios_path_changed = true; - - 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->fios_items.BuildFileList(this->abstract_filetype, this->fop); this->vscroll->SetCount(this->fios_items.Length()); this->selected = NULL; _load_check_data.Clear(); @@ -689,16 +699,29 @@ public: case 1: /* Selection changes */ if (!gui_scope) break; - if (_saveload_mode == SLD_LOAD_HEIGHTMAP) { - this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON, this->selected == NULL || _load_check_data.HasErrors()); - } - if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) { - this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON, - this->selected == NULL || _load_check_data.HasErrors() || !(!_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs())); - this->SetWidgetDisabledState(WID_SL_NEWGRF_INFO, - !_load_check_data.HasNewGrfs()); - this->SetWidgetDisabledState(WID_SL_MISSING_NEWGRFS, - !_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility == GLC_ALL_GOOD); + + if (this->fop != FOP_LOAD) break; + + switch (this->abstract_filetype) { + case FT_HEIGHTMAP: + this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON, this->selected == NULL || _load_check_data.HasErrors()); + break; + + case FT_SAVEGAME: + case FT_SCENARIO: { + bool disabled = this->selected == NULL || _load_check_data.HasErrors(); + if (!_settings_client.gui.UserIsAllowedToChangeNewGRFs()) { + disabled |= _load_check_data.HasNewGrfs() && _load_check_data.grf_compatibility == GLC_NOT_FOUND; + } + this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON, disabled); + this->SetWidgetDisabledState(WID_SL_NEWGRF_INFO, !_load_check_data.HasNewGrfs()); + this->SetWidgetDisabledState(WID_SL_MISSING_NEWGRFS, + !_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility == GLC_ALL_GOOD); + break; + } + + default: + NOT_REACHED(); } break; } @@ -729,41 +752,24 @@ static WindowDesc _save_dialog_desc( _nested_save_dialog_widgets, lengthof(_nested_save_dialog_widgets) ); -/** - * These values are used to convert the file/operations mode into a corresponding file type. - * So each entry, as expressed by the related comment, is based on the enum - */ -static const AbstractFileType _file_modetotype[] = { - FT_SAVEGAME, // used for SLD_LOAD_GAME - FT_SCENARIO, // used for SLD_LOAD_SCENARIO - FT_SAVEGAME, // used for SLD_SAVE_GAME - FT_SCENARIO, // used for SLD_SAVE_SCENARIO - FT_HEIGHTMAP, // used for SLD_LOAD_HEIGHTMAP - FT_HEIGHTMAP, // used for SLD_SAVE_HEIGHTMAP -}; - /** * Launch save/load dialog in the given mode. - * @param mode Save/load mode. + * @param abstract_filetype Kind of file to handle. + * @param fop File operation to perform (load or save). */ -void ShowSaveLoadDialog(SaveLoadDialogMode mode) +void ShowSaveLoadDialog(AbstractFileType abstract_filetype, FileOperation fop) { DeleteWindowById(WC_SAVELOAD, 0); WindowDesc *sld; - switch (mode) { - case SLD_SAVE_GAME: - case SLD_SAVE_SCENARIO: - case SLD_SAVE_HEIGHTMAP: - sld = &_save_dialog_desc; break; - case SLD_LOAD_HEIGHTMAP: - sld = &_load_heightmap_dialog_desc; break; - default: - sld = &_load_dialog_desc; break; + if (fop == FOP_SAVE) { + sld = &_save_dialog_desc; + } else { + /* Dialogue for loading a file. */ + sld = (abstract_filetype == FT_HEIGHTMAP) ? &_load_heightmap_dialog_desc : &_load_dialog_desc; } - _saveload_mode = mode; - _file_to_saveload.filetype = _file_modetotype[mode]; + _file_to_saveload.filetype = abstract_filetype; - new SaveLoadWindow(sld, mode); + new SaveLoadWindow(sld, abstract_filetype, fop); } diff --git a/src/intro_gui.cpp b/src/intro_gui.cpp index de8b379392..b6ac73cb74 100644 --- a/src/intro_gui.cpp +++ b/src/intro_gui.cpp @@ -111,9 +111,9 @@ struct SelectGameWindow : public Window { } break; - case WID_SGI_LOAD_GAME: ShowSaveLoadDialog(SLD_LOAD_GAME); break; - case WID_SGI_PLAY_SCENARIO: ShowSaveLoadDialog(SLD_LOAD_SCENARIO); break; - case WID_SGI_PLAY_HEIGHTMAP: ShowSaveLoadDialog(SLD_LOAD_HEIGHTMAP); break; + case WID_SGI_LOAD_GAME: ShowSaveLoadDialog(FT_SAVEGAME, FOP_LOAD); break; + case WID_SGI_PLAY_SCENARIO: ShowSaveLoadDialog(FT_SCENARIO, FOP_LOAD); break; + case WID_SGI_PLAY_HEIGHTMAP: ShowSaveLoadDialog(FT_HEIGHTMAP,FOP_LOAD); break; case WID_SGI_EDIT_SCENARIO: StartScenarioEditor(); break; case WID_SGI_PLAY_NETWORK: diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index 7c04ad4e02..c3aef5b0fd 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -1187,17 +1187,17 @@ struct NetworkStartServerWindow : public Window { case WID_NSS_LOAD_GAME: _is_network_server = true; - ShowSaveLoadDialog(SLD_LOAD_GAME); + ShowSaveLoadDialog(FT_SAVEGAME, FOP_LOAD); break; case WID_NSS_PLAY_SCENARIO: _is_network_server = true; - ShowSaveLoadDialog(SLD_LOAD_SCENARIO); + ShowSaveLoadDialog(FT_SCENARIO, FOP_LOAD); break; case WID_NSS_PLAY_HEIGHTMAP: _is_network_server = true; - ShowSaveLoadDialog(SLD_LOAD_HEIGHTMAP); + ShowSaveLoadDialog(FT_HEIGHTMAP,FOP_LOAD); break; } } diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index 40c75ad710..55b49fd748 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -427,17 +427,17 @@ static CallBackFunction MenuClickSaveLoad(int index = 0) { if (_game_mode == GM_EDITOR) { switch (index) { - case SLEME_SAVE_SCENARIO: ShowSaveLoadDialog(SLD_SAVE_SCENARIO); break; - case SLEME_LOAD_SCENARIO: ShowSaveLoadDialog(SLD_LOAD_SCENARIO); break; - case SLEME_SAVE_HEIGHTMAP: ShowSaveLoadDialog(SLD_SAVE_HEIGHTMAP); break; - case SLEME_LOAD_HEIGHTMAP: ShowSaveLoadDialog(SLD_LOAD_HEIGHTMAP); break; + case SLEME_SAVE_SCENARIO: ShowSaveLoadDialog(FT_SCENARIO, FOP_SAVE); break; + case SLEME_LOAD_SCENARIO: ShowSaveLoadDialog(FT_SCENARIO, FOP_LOAD); break; + case SLEME_SAVE_HEIGHTMAP: ShowSaveLoadDialog(FT_HEIGHTMAP,FOP_SAVE); break; + case SLEME_LOAD_HEIGHTMAP: ShowSaveLoadDialog(FT_HEIGHTMAP,FOP_LOAD); break; case SLEME_EXIT_TOINTRO: AskExitToGameMenu(); break; case SLEME_EXIT_GAME: HandleExitGameRequest(); break; } } else { switch (index) { - case SLNME_SAVE_GAME: ShowSaveLoadDialog(SLD_SAVE_GAME); break; - case SLNME_LOAD_GAME: ShowSaveLoadDialog(SLD_LOAD_GAME); break; + case SLNME_SAVE_GAME: ShowSaveLoadDialog(FT_SAVEGAME, FOP_SAVE); break; + case SLNME_LOAD_GAME: ShowSaveLoadDialog(FT_SAVEGAME, FOP_LOAD); break; case SLNME_EXIT_TOINTRO: AskExitToGameMenu(); break; case SLNME_EXIT_GAME: HandleExitGameRequest(); break; } @@ -1712,7 +1712,7 @@ struct MainToolbarWindow : Window { case MTHK_FASTFORWARD: ToolbarFastForwardClick(this); break; case MTHK_SETTINGS: ShowGameOptions(); break; case MTHK_SAVEGAME: MenuClickSaveLoad(); break; - case MTHK_LOADGAME: ShowSaveLoadDialog(SLD_LOAD_GAME); break; + case MTHK_LOADGAME: ShowSaveLoadDialog(FT_SAVEGAME, FOP_LOAD); break; case MTHK_SMALLMAP: ShowSmallMap(); break; case MTHK_TOWNDIRECTORY: ShowTownDirectory(); break; case MTHK_SUBSIDIES: ShowSubsidiesList(); break; From c3bed437ee7ce0a41236951a569883486f4d2fe2 Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:57:20 +0000 Subject: [PATCH 17/28] (svn r27649) -Codechange: Introduce detailed file type enum, rebuild FiosType with it. --- src/console_cmds.cpp | 16 ++++---- src/fileio_type.h | 80 +++++++++++++++++++++++++++++++++------ src/fios.h | 2 - src/fios_gui.cpp | 20 +++++++--- src/saveload/saveload.cpp | 26 +------------ 5 files changed, 91 insertions(+), 53 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 6284287c04..a486ea2822 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -360,16 +360,14 @@ DEF_CONSOLE_CMD(ConLoad) _console_file_list.ValidateFileList(); const FiosItem *item = _console_file_list.FindItem(file); if (item != NULL) { - switch (item->type) { - case FIOS_TYPE_FILE: case FIOS_TYPE_OLDFILE: { - _switch_mode = SM_LOAD_GAME; - _file_to_saveload.SetMode(item->type); + if (GetAbstractFileType(item->type) == FT_SAVEGAME) { + _switch_mode = SM_LOAD_GAME; + _file_to_saveload.SetMode(item->type); - strecpy(_file_to_saveload.name, FiosBrowseTo(item), lastof(_file_to_saveload.name)); - strecpy(_file_to_saveload.title, item->title, lastof(_file_to_saveload.title)); - break; - } - default: IConsolePrintF(CC_ERROR, "%s: Not a savegame.", file); + strecpy(_file_to_saveload.name, FiosBrowseTo(item), lastof(_file_to_saveload.name)); + strecpy(_file_to_saveload.title, item->title, lastof(_file_to_saveload.title)); + } else { + IConsolePrintF(CC_ERROR, "%s: Not a savegame.", file); } } else { IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file); diff --git a/src/fileio_type.h b/src/fileio_type.h index 20df8cd05d..4fae7b3920 100644 --- a/src/fileio_type.h +++ b/src/fileio_type.h @@ -22,6 +22,27 @@ enum AbstractFileType { FT_HEIGHTMAP, ///< heightmap file FT_INVALID = 7, ///< Invalid or unknown file type. + FT_NUMBITS = 3, ///< Number of bits required for storing a #AbstractFileType value. + FT_MASK = (1 << FT_NUMBITS) - 1, ///< Bitmask for extracting an abstract file type. +}; + +/** Kinds of files in each #AbstractFileType. */ +enum DetailedFileType { + /* Save game and scenario files. */ + DFT_OLD_GAME_FILE, ///< Old save game or scenario file. + DFT_GAME_FILE, ///< Save game or scenario file. + + /* Heightmap files. */ + DFT_HEIGHTMAP_BMP, ///< BMP file. + DFT_HEIGHTMAP_PNG, ///< PNG file. + + /* fios 'files' */ + DFT_FIOS_DRIVE, ///< A drive (letter) entry. + DFT_FIOS_PARENT, ///< A parent directory entry. + DFT_FIOS_DIR, ///< A directory entry. + DFT_FIOS_DIRECT, ///< Direct filename. + + DFT_INVALID = 255, ///< Unknown or invalid file. }; /** Operation performed on the file. */ @@ -30,21 +51,56 @@ enum FileOperation { FOP_SAVE, ///< File is being saved. }; -/** Elements of a file system that are recognized. */ +/** + * Construct an enum value for #FiosType as a combination of an abstract and a detailed file type. + * @param abstract Abstract file type (one of #AbstractFileType). + * @param detailed Detailed file type (one of #DetailedFileType). + */ +#define MAKE_FIOS_TYPE(abstract, detailed) ((abstract) | ((detailed) << FT_NUMBITS)) + +/** + * Elements of a file system that are recognized. + * Values are a combination of #AbstractFileType and #DetailedFileType. + * @see GetAbstractFileType GetDetailedFileType + */ enum FiosType { - FIOS_TYPE_DRIVE, - FIOS_TYPE_PARENT, - FIOS_TYPE_DIR, - FIOS_TYPE_FILE, - FIOS_TYPE_OLDFILE, - FIOS_TYPE_SCENARIO, - FIOS_TYPE_OLD_SCENARIO, - FIOS_TYPE_DIRECT, - FIOS_TYPE_PNG, - FIOS_TYPE_BMP, - FIOS_TYPE_INVALID = 255, + FIOS_TYPE_DRIVE = MAKE_FIOS_TYPE(FT_NONE, DFT_FIOS_DRIVE), + FIOS_TYPE_PARENT = MAKE_FIOS_TYPE(FT_NONE, DFT_FIOS_PARENT), + FIOS_TYPE_DIR = MAKE_FIOS_TYPE(FT_NONE, DFT_FIOS_DIR), + FIOS_TYPE_DIRECT = MAKE_FIOS_TYPE(FT_NONE, DFT_FIOS_DIRECT), + + FIOS_TYPE_FILE = MAKE_FIOS_TYPE(FT_SAVEGAME, DFT_GAME_FILE), + FIOS_TYPE_OLDFILE = MAKE_FIOS_TYPE(FT_SAVEGAME, DFT_OLD_GAME_FILE), + FIOS_TYPE_SCENARIO = MAKE_FIOS_TYPE(FT_SCENARIO, DFT_GAME_FILE), + FIOS_TYPE_OLD_SCENARIO = MAKE_FIOS_TYPE(FT_SCENARIO, DFT_OLD_GAME_FILE), + FIOS_TYPE_PNG = MAKE_FIOS_TYPE(FT_HEIGHTMAP, DFT_HEIGHTMAP_PNG), + FIOS_TYPE_BMP = MAKE_FIOS_TYPE(FT_HEIGHTMAP, DFT_HEIGHTMAP_BMP), + + FIOS_TYPE_INVALID = MAKE_FIOS_TYPE(FT_INVALID, DFT_INVALID), }; +#undef MAKE_FIOS_TYPE + +/** + * Extract the abstract file type from a #FiosType. + * @param fios_type Type to query. + * @return The Abstract file type of the \a fios_type. + */ +inline AbstractFileType GetAbstractFileType(FiosType fios_type) +{ + return static_cast(fios_type & FT_MASK); +} + +/** + * Extract the detailed file type from a #FiosType. + * @param fios_type Type to query. + * @return The Detailed file type of the \a fios_type. + */ +inline DetailedFileType GetDetailedFileType(FiosType fios_type) +{ + return static_cast(fios_type >> FT_NUMBITS); +} + /** * The different kinds of subdirectories OpenTTD uses */ diff --git a/src/fios.h b/src/fios.h index 86bcf3e3a0..80654812b2 100644 --- a/src/fios.h +++ b/src/fios.h @@ -224,6 +224,4 @@ FiosType FiosGetSavegameListCallback(FileOperation fop, const char *file, const int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b); -extern const TextColour _fios_colours[]; - #endif /* FIOS_H */ diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index e902941d65..a4e495b491 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -186,12 +186,19 @@ static const NWidgetPart _nested_save_dialog_widgets[] = { EndContainer(), }; -/** Colours for fios types, indexed by #FiosType. */ -const TextColour _fios_colours[] = { - TC_LIGHT_BLUE, TC_DARK_GREEN, TC_DARK_GREEN, TC_ORANGE, TC_LIGHT_BROWN, - TC_ORANGE, TC_LIGHT_BROWN, TC_ORANGE, TC_ORANGE, TC_YELLOW +/** Text colours of #DetailedFileType fios entries in the window. */ +static const TextColour _fios_colours[] = { + TC_LIGHT_BROWN, // DFT_OLD_GAME_FILE + TC_ORANGE, // DFT_GAME_FILE + TC_YELLOW, // DFT_HEIGHTMAP_BMP + TC_ORANGE, // DFT_HEIGHTMAP_PNG + TC_LIGHT_BLUE, // DFT_FIOS_DRIVE + TC_DARK_GREEN, // DFT_FIOS_PARENT + TC_DARK_GREEN, // DFT_FIOS_DIR + TC_ORANGE, // DFT_FIOS_DIRECT }; + /** * Sort the collected list save games prior to displaying it in the save/load gui. * @param [inout] file_list List of save game files found in the directory. @@ -368,7 +375,7 @@ public: if (item == this->selected) { GfxFillRect(r.left + 1, y, r.right, y + this->resize.step_height, PC_DARK_BLUE); } - DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, item->title, _fios_colours[item->type]); + DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, item->title, _fios_colours[GetDetailedFileType(item->type)]); y += this->resize.step_height; if (y >= this->vscroll->GetCapacity() * this->resize.step_height + r.top + WD_FRAMERECT_TOP) break; } @@ -575,7 +582,8 @@ public: this->selected = file; _load_check_data.Clear(); - if (file->type == FIOS_TYPE_FILE || file->type == FIOS_TYPE_SCENARIO) { + if (GetDetailedFileType(file->type) == DFT_GAME_FILE) { + /* Other detailed file types cannot be checked before. */ SaveOrLoad(name, SL_LOAD_CHECK, NO_DIRECTORY, false); } diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index a0d15938a4..b5fdda27a1 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -2924,30 +2924,8 @@ void FileToSaveLoad::SetMode(FiosType ft) break; } - switch (ft) { - case FIOS_TYPE_OLDFILE: - case FIOS_TYPE_FILE: - this->filetype = FT_SAVEGAME; - break; - - case FIOS_TYPE_OLD_SCENARIO: - case FIOS_TYPE_SCENARIO: - this->filetype = FT_SCENARIO; - break; - -#ifdef WITH_PNG - case FIOS_TYPE_PNG: - /* FALL THROUGH */ -#endif /* WITH_PNG */ - - case FIOS_TYPE_BMP: - this->filetype = FT_HEIGHTMAP; - break; - - default: - this->filetype = FT_INVALID; - break; - } + this->filetype = GetAbstractFileType(ft); + if (this->filetype == FT_NONE) this->filetype = FT_INVALID; } #if 0 From a959c0c9653c484d4925dbeda6ab50bc082d01fe Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:57:43 +0000 Subject: [PATCH 18/28] (svn r27650) -Codechange: Replace SaveOrLoadMode by FileOperation and DetailedFileType. --- src/console_cmds.cpp | 2 +- src/crashlog.cpp | 2 +- src/fileio_type.h | 7 ++- src/fios_gui.cpp | 4 +- src/genworld.cpp | 2 +- src/genworld_gui.cpp | 2 +- src/heightmap.cpp | 35 ++++++++----- src/heightmap.h | 6 ++- src/landscape.cpp | 2 +- src/network/network_client.cpp | 4 +- src/openttd.cpp | 28 ++++++----- src/saveload/afterload.cpp | 2 +- src/saveload/saveload.cpp | 90 ++++++++++++++++++---------------- src/saveload/saveload.h | 23 +++------ src/saveload/signs_sl.cpp | 2 +- src/video/dedicated_v.cpp | 4 +- 16 files changed, 114 insertions(+), 101 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index a486ea2822..c26f17f29e 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -318,7 +318,7 @@ DEF_CONSOLE_CMD(ConSave) char *filename = str_fmt("%s.sav", argv[1]); IConsolePrint(CC_DEFAULT, "Saving map..."); - if (SaveOrLoad(filename, SL_SAVE, SAVE_DIR) != SL_OK) { + if (SaveOrLoad(filename, FOP_SAVE, DFT_GAME_FILE, SAVE_DIR) != SL_OK) { IConsolePrint(CC_ERROR, "Saving map failed"); } else { IConsolePrintF(CC_DEFAULT, "Map successfully saved to %s", filename); diff --git a/src/crashlog.cpp b/src/crashlog.cpp index 4000cfb03e..83beb94ff8 100644 --- a/src/crashlog.cpp +++ b/src/crashlog.cpp @@ -388,7 +388,7 @@ bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const seprintf(filename, filename_last, "%scrash.sav", _personal_dir); /* Don't do a threaded saveload. */ - return SaveOrLoad(filename, SL_SAVE, NO_DIRECTORY, false) == SL_OK; + return SaveOrLoad(filename, FOP_SAVE, DFT_GAME_FILE, NO_DIRECTORY, false) == SL_OK; } catch (...) { return false; } diff --git a/src/fileio_type.h b/src/fileio_type.h index 4fae7b3920..0c4602bbaf 100644 --- a/src/fileio_type.h +++ b/src/fileio_type.h @@ -47,8 +47,11 @@ enum DetailedFileType { /** Operation performed on the file. */ enum FileOperation { - FOP_LOAD, ///< File is being loaded. - FOP_SAVE, ///< File is being saved. + FOP_CHECK, ///< Load file for checking and/or preview. + FOP_LOAD, ///< File is being loaded. + FOP_SAVE, ///< File is being saved. + + FOP_INVALID, ///< Unknown file operation. }; /** diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index a4e495b491..0c09f96ab0 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -584,7 +584,7 @@ public: if (GetDetailedFileType(file->type) == DFT_GAME_FILE) { /* Other detailed file types cannot be checked before. */ - SaveOrLoad(name, SL_LOAD_CHECK, NO_DIRECTORY, false); + SaveOrLoad(name, FOP_CHECK, DFT_GAME_FILE, NO_DIRECTORY, false); } this->InvalidateData(1); @@ -777,7 +777,7 @@ void ShowSaveLoadDialog(AbstractFileType abstract_filetype, FileOperation fop) sld = (abstract_filetype == FT_HEIGHTMAP) ? &_load_heightmap_dialog_desc : &_load_dialog_desc; } - _file_to_saveload.filetype = abstract_filetype; + _file_to_saveload.abstract_ftype = abstract_filetype; new SaveLoadWindow(sld, abstract_filetype, fop); } diff --git a/src/genworld.cpp b/src/genworld.cpp index 2b2dfb5fd7..95e28fcdf0 100644 --- a/src/genworld.cpp +++ b/src/genworld.cpp @@ -204,7 +204,7 @@ static void _GenerateWorld(void *) if (_debug_desync_level > 0) { char name[MAX_PATH]; seprintf(name, lastof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date); - SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false); + SaveOrLoad(name, FOP_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR, false); } } catch (...) { BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP, true); diff --git a/src/genworld_gui.cpp b/src/genworld_gui.cpp index 61e114311b..359709e361 100644 --- a/src/genworld_gui.cpp +++ b/src/genworld_gui.cpp @@ -833,7 +833,7 @@ static void _ShowGenerateLandscape(GenerateLandscapeWindowMode mode) if (mode == GLWM_HEIGHTMAP) { /* If the function returns negative, it means there was a problem loading the heightmap */ - if (!GetHeightmapDimensions(_file_to_saveload.name, &x, &y)) return; + if (!GetHeightmapDimensions(_file_to_saveload.detail_ftype, _file_to_saveload.name, &x, &y)) return; } WindowDesc *desc = (mode == GLWM_HEIGHTMAP) ? &_heightmap_load_desc : &_generate_landscape_desc; diff --git a/src/heightmap.cpp b/src/heightmap.cpp index ec31257287..630dc69d7e 100644 --- a/src/heightmap.cpp +++ b/src/heightmap.cpp @@ -102,7 +102,7 @@ static void ReadHeightmapPNGImageData(byte *map, png_structp png_ptr, png_infop * If map == NULL only the size of the PNG is read, otherwise a map * with grayscale pixels is allocated and assigned to *map. */ -static bool ReadHeightmapPNG(char *filename, uint *x, uint *y, byte **map) +static bool ReadHeightmapPNG(const char *filename, uint *x, uint *y, byte **map) { FILE *fp; png_structp png_ptr = NULL; @@ -232,7 +232,7 @@ static void ReadHeightmapBMPImageData(byte *map, BmpInfo *info, BmpData *data) * If map == NULL only the size of the BMP is read, otherwise a map * with grayscale pixels is allocated and assigned to *map. */ -static bool ReadHeightmapBMP(char *filename, uint *x, uint *y, byte **map) +static bool ReadHeightmapBMP(const char *filename, uint *x, uint *y, byte **map) { FILE *f; BmpInfo info; @@ -444,45 +444,56 @@ void FixSlopes() } /** - * Reads the heightmap with the correct file reader + * Reads the heightmap with the correct file reader. + * @param dft Type of image file. + * @param filename Name of the file to load. + * @param [out] x Length of the image. + * @param [out] y Height of the image. + * @param [inout] map If not \c NULL, destination to store the loaded block of image data. + * @return Whether loading was successful. */ -static bool ReadHeightMap(char *filename, uint *x, uint *y, byte **map) +static bool ReadHeightMap(DetailedFileType dft, const char *filename, uint *x, uint *y, byte **map) { - switch (_file_to_saveload.mode) { - default: NOT_REACHED(); + switch (dft) { + default: + NOT_REACHED(); + #ifdef WITH_PNG - case SL_PNG: + case DFT_HEIGHTMAP_PNG: return ReadHeightmapPNG(filename, x, y, map); #endif /* WITH_PNG */ - case SL_BMP: + + case DFT_HEIGHTMAP_BMP: return ReadHeightmapBMP(filename, x, y, map); } } /** * Get the dimensions of a heightmap. + * @param dft Type of image file. * @param filename to query * @param x dimension x * @param y dimension y * @return Returns false if loading of the image failed. */ -bool GetHeightmapDimensions(char *filename, uint *x, uint *y) +bool GetHeightmapDimensions(DetailedFileType dft, const char *filename, uint *x, uint *y) { - return ReadHeightMap(filename, x, y, NULL); + return ReadHeightMap(dft, filename, x, y, NULL); } /** * Load a heightmap from file and change the map in his current dimensions * to a landscape representing the heightmap. * It converts pixels to height. The brighter, the higher. + * @param dft Type of image file. * @param filename of the heightmap file to be imported */ -void LoadHeightmap(char *filename) +void LoadHeightmap(DetailedFileType dft, const char *filename) { uint x, y; byte *map = NULL; - if (!ReadHeightMap(filename, &x, &y, &map)) { + if (!ReadHeightMap(dft, filename, &x, &y, &map)) { free(map); return; } diff --git a/src/heightmap.h b/src/heightmap.h index 08ae200ece..67349df483 100644 --- a/src/heightmap.h +++ b/src/heightmap.h @@ -12,6 +12,8 @@ #ifndef HEIGHTMAP_H #define HEIGHTMAP_H +#include "fileio_type.h" + /** * Order of these enums has to be the same as in lang/english.txt * Otherwise you will get inconsistent behaviour. @@ -21,8 +23,8 @@ enum HeightmapRotation { HM_CLOCKWISE, ///< Rotate the map clockwise 45 degrees }; -bool GetHeightmapDimensions(char *filename, uint *x, uint *y); -void LoadHeightmap(char *filename); +bool GetHeightmapDimensions(DetailedFileType dft, const char *filename, uint *x, uint *y); +void LoadHeightmap(DetailedFileType dft, const char *filename); void FlatEmptyWorld(byte tile_height); void FixSlopes(); diff --git a/src/landscape.cpp b/src/landscape.cpp index d2b1b98cd5..185e84a80b 100644 --- a/src/landscape.cpp +++ b/src/landscape.cpp @@ -1222,7 +1222,7 @@ void GenerateLandscape(byte mode) if (mode == GWM_HEIGHTMAP) { SetGeneratingWorldProgress(GWP_LANDSCAPE, steps + GLS_HEIGHTMAP); - LoadHeightmap(_file_to_saveload.name); + LoadHeightmap(_file_to_saveload.detail_ftype, _file_to_saveload.name); IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); } else if (_settings_game.game_creation.land_generator == LG_TERRAGENESIS) { SetGeneratingWorldProgress(GWP_LANDSCAPE, steps + GLS_TERRAGENESIS); diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index d4d294146e..a33e5c6814 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -520,7 +520,7 @@ bool ClientNetworkGameSocketHandler::IsConnected() * DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p ************/ -extern bool SafeLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL); +extern bool SafeLoad(const char *filename, FileOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL); NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_FULL(Packet *p) { @@ -836,7 +836,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet /* The map is done downloading, load it */ ClearErrorMessages(); - bool load_success = SafeLoad(NULL, SL_LOAD, GM_NORMAL, NO_DIRECTORY, lf); + bool load_success = SafeLoad(NULL, FOP_LOAD, DFT_GAME_FILE, GM_NORMAL, NO_DIRECTORY, lf); /* Long savegame loads shouldn't affect the lag calculation! */ this->last_packet = _realtime_tick; diff --git a/src/openttd.cpp b/src/openttd.cpp index bd8ac42532..4464e80168 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -328,7 +328,7 @@ static void LoadIntroGame(bool load_newgrfs = true) SetupColoursAndInitialWindow(); /* Load the default opening screen savegame */ - if (SaveOrLoad("opntitle.dat", SL_LOAD, BASESET_DIR) != SL_OK) { + if (SaveOrLoad("opntitle.dat", FOP_LOAD, DFT_GAME_FILE, BASESET_DIR) != SL_OK) { GenerateWorld(GWM_EMPTY, 64, 64); // if failed loading, make empty world. WaitTillGeneratedWorld(); SetLocalCompany(COMPANY_SPECTATOR); @@ -619,8 +619,9 @@ int openttd_main(int argc, char *argv[]) case 'g': if (mgo.opt != NULL) { strecpy(_file_to_saveload.name, mgo.opt, lastof(_file_to_saveload.name)); - _switch_mode = (_switch_mode == SM_EDITOR || _switch_mode == SM_LOAD_SCENARIO ? SM_LOAD_SCENARIO : SM_LOAD_GAME); - _file_to_saveload.mode = SL_LOAD; + bool is_scenario = _switch_mode == SM_EDITOR || _switch_mode == SM_LOAD_SCENARIO; + _switch_mode = is_scenario ? SM_LOAD_SCENARIO : SM_LOAD_GAME; + _file_to_saveload.SetMode(FOP_LOAD, is_scenario ? FT_SCENARIO : FT_SAVEGAME, DFT_GAME_FILE); /* 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, '.'); @@ -650,7 +651,7 @@ int openttd_main(int argc, char *argv[]) FiosGetSavegameListCallback(FOP_LOAD, mgo.opt, strrchr(mgo.opt, '.'), title, lastof(title)); _load_check_data.Clear(); - SaveOrLoadResult res = SaveOrLoad(mgo.opt, SL_LOAD_CHECK, SAVE_DIR, false); + SaveOrLoadResult res = SaveOrLoad(mgo.opt, FOP_CHECK, DFT_GAME_FILE, SAVE_DIR, false); if (res != SL_OK || _load_check_data.HasErrors()) { fprintf(stderr, "Failed to open savegame\n"); if (_load_check_data.HasErrors()) { @@ -997,14 +998,15 @@ static void MakeNewEditorWorld() * @param subdir default directory to look for filename, set to 0 if not needed * @param lf Load filter to use, if NULL: use filename + subdir. */ -bool SafeLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL) +bool SafeLoad(const char *filename, FileOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL) { - assert(mode == SL_LOAD || (lf == NULL && mode == SL_OLD_LOAD)); + assert(fop == FOP_LOAD); + assert(dft == DFT_GAME_FILE || (lf == NULL && dft == DFT_OLD_GAME_FILE)); GameMode ogm = _game_mode; _game_mode = newgm; - switch (lf == NULL ? SaveOrLoad(filename, mode, subdir) : LoadWithFilter(lf)) { + switch (lf == NULL ? SaveOrLoad(filename, fop, dft, subdir) : LoadWithFilter(lf)) { case SL_OK: return true; case SL_REINIT: @@ -1093,11 +1095,11 @@ void SwitchToMode(SwitchMode new_mode) ResetGRFConfig(true); ResetWindowSystem(); - if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, NO_DIRECTORY)) { + if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.file_op, _file_to_saveload.detail_ftype, GM_NORMAL, NO_DIRECTORY)) { SetDParamStr(0, GetSaveLoadErrorString()); ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR); } else { - if (_file_to_saveload.filetype == FT_SCENARIO) { + if (_file_to_saveload.abstract_ftype == FT_SCENARIO) { /* Reset engine pool to simplify changing engine NewGRFs in scenario editor. */ EngineOverrideManager::ResetToCurrentNewGRFConfig(); } @@ -1134,7 +1136,7 @@ void SwitchToMode(SwitchMode new_mode) break; case SM_LOAD_SCENARIO: { // Load scenario from scenario editor - if (SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_EDITOR, NO_DIRECTORY)) { + if (SafeLoad(_file_to_saveload.name, _file_to_saveload.file_op, _file_to_saveload.detail_ftype, GM_EDITOR, NO_DIRECTORY)) { SetLocalCompany(OWNER_NONE); _settings_newgame.game_creation.starting_year = _cur_year; /* Cancel the saveload pausing */ @@ -1156,7 +1158,7 @@ void SwitchToMode(SwitchMode new_mode) case SM_SAVE_GAME: // Save game. /* Make network saved games on pause compatible to singleplayer */ - if (SaveOrLoad(_file_to_saveload.name, SL_SAVE, NO_DIRECTORY) != SL_OK) { + if (SaveOrLoad(_file_to_saveload.name, FOP_SAVE, DFT_GAME_FILE, NO_DIRECTORY) != SL_OK) { SetDParamStr(0, GetSaveLoadErrorString()); ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR); } else { @@ -1367,7 +1369,7 @@ void StateGameLoop() /* Save the desync savegame if needed. */ char name[MAX_PATH]; seprintf(name, lastof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date); - SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false); + SaveOrLoad(name, FOP_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR, false); } CheckCaches(); @@ -1424,7 +1426,7 @@ static void DoAutosave() } DEBUG(sl, 2, "Autosaving to '%s'", buf); - if (SaveOrLoad(buf, SL_SAVE, AUTOSAVE_DIR) != SL_OK) { + if (SaveOrLoad(buf, FOP_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR) != SL_OK) { ShowErrorMessage(STR_ERROR_AUTOSAVE_FAILED, INVALID_STRING_ID, WL_ERROR); } } diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 9690481154..de3f7ccff9 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -248,7 +248,7 @@ static void InitializeWindowsAndCaches() /* For each company, verify (while loading a scenario) that the inauguration date is the current year and set it * accordingly if it is not the case. No need to set it on companies that are not been used already, * thus the MIN_YEAR (which is really nothing more than Zero, initialized value) test */ - if (_file_to_saveload.filetype == FT_SCENARIO && c->inaugurated_year != MIN_YEAR) { + if (_file_to_saveload.abstract_ftype == FT_SCENARIO && c->inaugurated_year != MIN_YEAR) { c->inaugurated_year = _cur_year; } } diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index b5fdda27a1..4dd8a4c7b6 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -2778,10 +2778,10 @@ SaveOrLoadResult LoadWithFilter(LoadFilter *reader) * @param threaded True when threaded saving is allowed * @return Return the result of the action. #SL_OK, #SL_ERROR, or #SL_REINIT ("unload" the game) */ -SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, bool threaded) +SaveOrLoadResult SaveOrLoad(const char *filename, FileOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded) { /* An instance of saving is already active, so don't go saving again */ - if (_sl.saveinprogress && mode == SL_SAVE && threaded) { + if (_sl.saveinprogress && fop == FOP_SAVE && dft == DFT_GAME_FILE && threaded) { /* if not an autosave, but a user action, show error message */ if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR); return SL_OK; @@ -2790,7 +2790,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo try { /* Load a TTDLX or TTDPatch game */ - if (mode == SL_OLD_LOAD) { + if (fop == FOP_LOAD && dft == DFT_OLD_GAME_FILE) { InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them @@ -2811,25 +2811,35 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo return SL_OK; } - switch (mode) { - case SL_LOAD_CHECK: _sl.action = SLA_LOAD_CHECK; break; - case SL_LOAD: _sl.action = SLA_LOAD; break; - case SL_SAVE: _sl.action = SLA_SAVE; break; + assert(dft == DFT_GAME_FILE); + switch (dft) { + case FOP_CHECK: + _sl.action = SLA_LOAD_CHECK; + break; + + case FOP_LOAD: + _sl.action = SLA_LOAD; + break; + + case FOP_SAVE: + _sl.action = SLA_SAVE; + break; + default: NOT_REACHED(); } - FILE *fh = (mode == SL_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb); + FILE *fh = (fop == FOP_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb); /* Make it a little easier to load savegames from the console */ - if (fh == NULL && mode != SL_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR); - if (fh == NULL && mode != SL_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR); - if (fh == NULL && mode != SL_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR); + if (fh == NULL && fop != FOP_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR); + if (fh == NULL && fop != FOP_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR); + if (fh == NULL && fop != FOP_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR); if (fh == NULL) { - SlError(mode == SL_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE); + SlError(fop == FOP_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE); } - if (mode == SL_SAVE) { // SAVE game + if (fop == FOP_SAVE) { // SAVE game DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename); if (_network_server || !_settings_client.gui.threaded_saves) threaded = false; @@ -2837,24 +2847,25 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo } /* LOAD game */ - assert(mode == SL_LOAD || mode == SL_LOAD_CHECK); + assert(fop == FOP_LOAD || fop == FOP_CHECK); DEBUG(desync, 1, "load: %s", filename); - return DoLoad(new FileReader(fh), mode == SL_LOAD_CHECK); + return DoLoad(new FileReader(fh), fop == FOP_CHECK); } catch (...) { + /* This code may be executed both for old and new save games. */ ClearSaveLoadState(); /* Skip the "colour" character */ - if (mode != SL_LOAD_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3); + if (fop != FOP_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3); /* A saver/loader exception!! reinitialize all variables to prevent crash! */ - return (mode == SL_LOAD || mode == SL_OLD_LOAD) ? SL_REINIT : SL_ERROR; + return (fop == FOP_LOAD) ? SL_REINIT : SL_ERROR; } } /** Do a save when exiting the game (_settings_client.gui.autosave_on_exit) */ void DoExitSave() { - SaveOrLoad("exit.sav", SL_SAVE, AUTOSAVE_DIR); + SaveOrLoad("exit.sav", FOP_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR); } /** @@ -2898,34 +2909,27 @@ void GenerateDefaultSaveName(char *buf, const char *last) */ void FileToSaveLoad::SetMode(FiosType ft) { - switch (ft) { - case FIOS_TYPE_FILE: - case FIOS_TYPE_SCENARIO: - this->mode = SL_LOAD; - break; + this->SetMode(FOP_LOAD, GetAbstractFileType(ft), GetDetailedFileType(ft)); +} - case FIOS_TYPE_OLDFILE: - case FIOS_TYPE_OLD_SCENARIO: - this->mode = SL_OLD_LOAD; - break; - -#ifdef WITH_PNG - case FIOS_TYPE_PNG: - this->mode = SL_PNG; - break; -#endif /* WITH_PNG */ - - case FIOS_TYPE_BMP: - this->mode = SL_BMP; - break; - - default: - this->mode = SL_INVALID; - break; +/** + * Set the mode and file type of the file to save or load. + * @param fop File operation being performed. + * @param aft Abstract file type. + * @param dft Detailed file type. + */ +void FileToSaveLoad::SetMode(FileOperation fop, AbstractFileType aft, DetailedFileType dft) +{ + if (aft == FT_INVALID || aft == FT_NONE) { + this->file_op = FOP_INVALID; + this->detail_ftype = DFT_INVALID; + this->abstract_ftype = FT_INVALID; + return; } - this->filetype = GetAbstractFileType(ft); - if (this->filetype == FT_NONE) this->filetype = FT_INVALID; + this->file_op = fop; + this->detail_ftype = dft; + this->abstract_ftype = aft; } #if 0 diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 43b9d5d3ce..7b0ae8f487 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -22,25 +22,16 @@ enum SaveOrLoadResult { SL_REINIT = 2, ///< error that was caught in the middle of updating game state, need to clear it. (can only happen during load) }; -/** Save or load mode. @see SaveOrLoad */ -enum SaveOrLoadMode { - SL_INVALID = -1, ///< Invalid mode. - SL_LOAD = 0, ///< Load game. - SL_SAVE = 1, ///< Save game. - SL_OLD_LOAD = 2, ///< Load old game. - SL_PNG = 3, ///< Load PNG file (height map). - SL_BMP = 4, ///< Load BMP file (height map). - SL_LOAD_CHECK = 5, ///< Load for game preview. -}; - /** Deals with the type of the savegame, independent of extension */ struct FileToSaveLoad { - SaveOrLoadMode mode; ///< savegame/scenario type (old, new) - AbstractFileType filetype; ///< what type of file are we dealing with - char name[MAX_PATH]; ///< name - char title[255]; ///< internal name of the game + FileOperation file_op; ///< File operation to perform. + DetailedFileType detail_ftype; ///< Concrete file type (PNG, BMP, old save, etc). + AbstractFileType abstract_ftype; ///< Abstract type of file (scenario, heightmap, etc). + char name[MAX_PATH]; ///< Name of the file. + char title[255]; ///< Internal name of the game. void SetMode(FiosType ft); + void SetMode(FileOperation fop, AbstractFileType aft, DetailedFileType dft); }; /** Types of save games. */ @@ -58,7 +49,7 @@ extern FileToSaveLoad _file_to_saveload; void GenerateDefaultSaveName(char *buf, const char *last); void SetSaveLoadError(uint16 str); const char *GetSaveLoadErrorString(); -SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, bool threaded = true); +SaveOrLoadResult SaveOrLoad(const char *filename, FileOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded = true); void WaitTillSaved(); void ProcessAsyncSaveFinish(); void DoExitSave(); diff --git a/src/saveload/signs_sl.cpp b/src/saveload/signs_sl.cpp index 077d34501d..132ac181e4 100644 --- a/src/saveload/signs_sl.cpp +++ b/src/saveload/signs_sl.cpp @@ -60,7 +60,7 @@ static void Load_SIGN() } /* Signs placed in scenario editor shall now be OWNER_DEITY */ - if (IsSavegameVersionBefore(171) && si->owner == OWNER_NONE && _file_to_saveload.filetype == FT_SCENARIO) { + if (IsSavegameVersionBefore(171) && si->owner == OWNER_NONE && _file_to_saveload.abstract_ftype == FT_SCENARIO) { si->owner = OWNER_DEITY; } } diff --git a/src/video/dedicated_v.cpp b/src/video/dedicated_v.cpp index bfe853e9a0..95d40cd000 100644 --- a/src/video/dedicated_v.cpp +++ b/src/video/dedicated_v.cpp @@ -142,7 +142,7 @@ static void *_dedicated_video_mem; /* Whether a fork has been done. */ bool _dedicated_forks; -extern bool SafeLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL); +extern bool SafeLoad(const char *filename, FileOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL); static FVideoDriver_Dedicated iFVideoDriver_Dedicated; @@ -286,7 +286,7 @@ void VideoDriver_Dedicated::MainLoop() _switch_mode = SM_NONE; /* First we need to test if the savegame can be loaded, else we will end up playing the * intro game... */ - if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, BASE_DIR)) { + if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.file_op, _file_to_saveload.detail_ftype, GM_NORMAL, BASE_DIR)) { /* Loading failed, pop out.. */ DEBUG(net, 0, "Loading requested map failed, aborting"); _networking = false; From e735bbebcecc33f55b75f0c7fc543ad023901db3 Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:58:04 +0000 Subject: [PATCH 19/28] (svn r27651) -Codechange: Introduce methods for setting the name and title of _file_to_saveload. --- src/console_cmds.cpp | 5 ++--- src/fios_gui.cpp | 9 ++++----- src/openttd.cpp | 2 +- src/saveload/saveload.cpp | 18 ++++++++++++++++++ src/saveload/saveload.h | 2 ++ 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index c26f17f29e..863929b94c 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -363,9 +363,8 @@ DEF_CONSOLE_CMD(ConLoad) if (GetAbstractFileType(item->type) == FT_SAVEGAME) { _switch_mode = SM_LOAD_GAME; _file_to_saveload.SetMode(item->type); - - strecpy(_file_to_saveload.name, FiosBrowseTo(item), lastof(_file_to_saveload.name)); - strecpy(_file_to_saveload.title, item->title, lastof(_file_to_saveload.title)); + _file_to_saveload.SetName(FiosBrowseTo(item)); + _file_to_saveload.SetTitle(item->title); } else { IConsolePrintF(CC_ERROR, "%s: Not a savegame.", file); } diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 0c09f96ab0..a0f0f1b296 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -537,9 +537,8 @@ public: if (this->selected != NULL && !_load_check_data.HasErrors()) { const char *name = FiosBrowseTo(this->selected); _file_to_saveload.SetMode(this->selected->type); - - strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name)); - strecpy(_file_to_saveload.title, this->selected->title, lastof(_file_to_saveload.title)); + _file_to_saveload.SetName(name); + _file_to_saveload.SetTitle(this->selected->title); if (this->abstract_filetype == FT_HEIGHTMAP) { delete this; @@ -602,8 +601,8 @@ public: } else { assert(this->abstract_filetype == FT_HEIGHTMAP); _file_to_saveload.SetMode(file->type); - strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name)); - strecpy(_file_to_saveload.title, file->title, lastof(_file_to_saveload.title)); + _file_to_saveload.SetName(name); + _file_to_saveload.SetTitle(file->title); delete this; ShowHeightmapLoad(); diff --git a/src/openttd.cpp b/src/openttd.cpp index 4464e80168..f00ce74c37 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -618,7 +618,7 @@ int openttd_main(int argc, char *argv[]) case 'e': _switch_mode = (_switch_mode == SM_LOAD_GAME || _switch_mode == SM_LOAD_SCENARIO ? SM_LOAD_SCENARIO : SM_EDITOR); break; case 'g': if (mgo.opt != NULL) { - strecpy(_file_to_saveload.name, mgo.opt, lastof(_file_to_saveload.name)); + _file_to_saveload.SetName(mgo.opt); bool is_scenario = _switch_mode == SM_EDITOR || _switch_mode == SM_LOAD_SCENARIO; _switch_mode = is_scenario ? SM_LOAD_SCENARIO : SM_LOAD_GAME; _file_to_saveload.SetMode(FOP_LOAD, is_scenario ? FT_SCENARIO : FT_SAVEGAME, DFT_GAME_FILE); diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 4dd8a4c7b6..b78171e600 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -2932,6 +2932,24 @@ void FileToSaveLoad::SetMode(FileOperation fop, AbstractFileType aft, DetailedFi this->abstract_ftype = aft; } +/** + * Set the name of the file. + * @param name Name of the file. + */ +void FileToSaveLoad::SetName(const char *name) +{ + strecpy(this->name, name, lastof(this->name)); +} + +/** + * Set the title of the file. + * @param title Title of the file. + */ +void FileToSaveLoad::SetTitle(const char *title) +{ + strecpy(this->title, title, lastof(this->title)); +} + #if 0 /** * Function to get the type of the savegame by looking at the file header. diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 7b0ae8f487..b25e91e2cd 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -32,6 +32,8 @@ struct FileToSaveLoad { void SetMode(FiosType ft); void SetMode(FileOperation fop, AbstractFileType aft, DetailedFileType dft); + void SetName(const char *name); + void SetTitle(const char *title); }; /** Types of save games. */ From 0b8869930fb91824e425d9eebcdf351eeab0bcee Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 15:10:41 +0000 Subject: [PATCH 20/28] (svn r27652) -Fix(r27650): Use the file operation being performed to set the _sl.action variable. --- src/saveload/saveload.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index b78171e600..8da0d03984 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -2812,7 +2812,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, FileOperation fop, DetailedFil } assert(dft == DFT_GAME_FILE); - switch (dft) { + switch (fop) { case FOP_CHECK: _sl.action = SLA_LOAD_CHECK; break; From f286ba8db2c8f57346ba4cd273a00ad171619b92 Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 16:06:50 +0000 Subject: [PATCH 21/28] (svn r27653) -Fix(r27647): Rename FileOperation enum and values to SaveLoadOperation to avoid nameclash with windows compiler toolkit. --- src/console_cmds.cpp | 4 ++-- src/crashlog.cpp | 2 +- src/fileio_type.h | 10 ++++----- src/fios.cpp | 32 +++++++++++++-------------- src/fios.h | 12 +++++----- src/fios_gui.cpp | 36 +++++++++++++++--------------- src/genworld.cpp | 2 +- src/intro_gui.cpp | 6 ++--- src/network/network_client.cpp | 4 ++-- src/network/network_gui.cpp | 6 ++--- src/openttd.cpp | 20 ++++++++--------- src/saveload/saveload.cpp | 40 +++++++++++++++++----------------- src/saveload/saveload.h | 6 ++--- src/toolbar_gui.cpp | 14 ++++++------ src/video/dedicated_v.cpp | 2 +- 15 files changed, 98 insertions(+), 98 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 863929b94c..9cfc8e8f9d 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -67,7 +67,7 @@ public: void ValidateFileList(bool force_reload = false) { if (force_reload || !this->file_list_valid) { - this->BuildFileList(FT_SAVEGAME, FOP_LOAD); + this->BuildFileList(FT_SAVEGAME, SLO_LOAD); this->file_list_valid = true; } } @@ -318,7 +318,7 @@ DEF_CONSOLE_CMD(ConSave) char *filename = str_fmt("%s.sav", argv[1]); IConsolePrint(CC_DEFAULT, "Saving map..."); - if (SaveOrLoad(filename, FOP_SAVE, DFT_GAME_FILE, SAVE_DIR) != SL_OK) { + if (SaveOrLoad(filename, SLO_SAVE, DFT_GAME_FILE, SAVE_DIR) != SL_OK) { IConsolePrint(CC_ERROR, "Saving map failed"); } else { IConsolePrintF(CC_DEFAULT, "Map successfully saved to %s", filename); diff --git a/src/crashlog.cpp b/src/crashlog.cpp index 83beb94ff8..53a85e442c 100644 --- a/src/crashlog.cpp +++ b/src/crashlog.cpp @@ -388,7 +388,7 @@ bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const seprintf(filename, filename_last, "%scrash.sav", _personal_dir); /* Don't do a threaded saveload. */ - return SaveOrLoad(filename, FOP_SAVE, DFT_GAME_FILE, NO_DIRECTORY, false) == SL_OK; + return SaveOrLoad(filename, SLO_SAVE, DFT_GAME_FILE, NO_DIRECTORY, false) == SL_OK; } catch (...) { return false; } diff --git a/src/fileio_type.h b/src/fileio_type.h index 0c4602bbaf..fd3ac84554 100644 --- a/src/fileio_type.h +++ b/src/fileio_type.h @@ -46,12 +46,12 @@ enum DetailedFileType { }; /** Operation performed on the file. */ -enum FileOperation { - FOP_CHECK, ///< Load file for checking and/or preview. - FOP_LOAD, ///< File is being loaded. - FOP_SAVE, ///< File is being saved. +enum SaveLoadOperation { + SLO_CHECK, ///< Load file for checking and/or preview. + SLO_LOAD, ///< File is being loaded. + SLO_SAVE, ///< File is being saved. - FOP_INVALID, ///< Unknown file operation. + SLO_INVALID, ///< Unknown file operation. }; /** diff --git a/src/fios.cpp b/src/fios.cpp index 987542acc9..8ed01152f0 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -71,13 +71,13 @@ FileList::~FileList() /** * Construct a file list with the given kind of files, for the stated purpose. * @param abstract_filetype Kind of files to collect. - * @param fop Purpose of the collection, either #FOP_LOAD or #FOP_SAVE. + * @param fop Purpose of the collection, either #SLO_LOAD or #SLO_SAVE. */ -void FileList::BuildFileList(AbstractFileType abstract_filetype, FileOperation fop) +void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop) { this->Clear(); - assert(fop == FOP_LOAD || FOP_SAVE); + assert(fop == SLO_LOAD || SLO_SAVE); switch (abstract_filetype) { case FT_NONE: break; @@ -274,13 +274,13 @@ bool FiosDelete(const char *name) return unlink(filename) == 0; } -typedef FiosType fios_getlist_callback_proc(FileOperation fop, const char *filename, const char *ext, char *title, const char *last); +typedef FiosType fios_getlist_callback_proc(SaveLoadOperation fop, const char *filename, const char *ext, char *title, const char *last); /** * Scanner to scan for a particular type of FIOS file. */ class FiosFileScanner : public FileScanner { - FileOperation fop; ///< The kind of file we are looking for. + SaveLoadOperation fop; ///< The kind of file we are looking for. fios_getlist_callback_proc *callback_proc; ///< Callback to check whether the file may be added FileList &file_list; ///< Destination of the found files. public: @@ -290,7 +290,7 @@ public: * @param callback_proc The function that is called where you need to do the filtering. * @param file_list Destination of the found files. */ - FiosFileScanner(FileOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list) : + FiosFileScanner(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list) : fop(fop), callback_proc(callback_proc), file_list(file_list) {} @@ -354,7 +354,7 @@ bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, cons * @param subdir The directory from where to start (global) searching. * @param file_list Destination of the found files. */ -static void FiosGetFileList(FileOperation fop, fios_getlist_callback_proc *callback_proc, Subdirectory subdir, FileList &file_list) +static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, Subdirectory subdir, FileList &file_list) { struct stat sb; struct dirent *dirent; @@ -456,7 +456,7 @@ static void GetFileTitle(const char *file, char *title, const char *last, Subdir * @see FiosGetFileList * @see FiosGetSavegameList */ -FiosType FiosGetSavegameListCallback(FileOperation fop, const char *file, const char *ext, char *title, const char *last) +FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last) { /* Show savegame files * .SAV OpenTTD saved game @@ -472,7 +472,7 @@ FiosType FiosGetSavegameListCallback(FileOperation fop, const char *file, const return FIOS_TYPE_FILE; } - if (fop == FOP_LOAD) { + if (fop == SLO_LOAD) { if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 || strcasecmp(ext, ".sv2") == 0) { if (title != NULL) GetOldSaveGameName(file, title, last); @@ -489,7 +489,7 @@ FiosType FiosGetSavegameListCallback(FileOperation fop, const char *file, const * @param file_list Destination of the found files. * @see FiosGetFileList */ -void FiosGetSavegameList(FileOperation fop, FileList &file_list) +void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list) { static char *fios_save_path = NULL; static char *fios_save_path_last = NULL; @@ -517,7 +517,7 @@ void FiosGetSavegameList(FileOperation fop, FileList &file_list) * @see FiosGetFileList * @see FiosGetScenarioList */ -static FiosType FiosGetScenarioListCallback(FileOperation fop, const char *file, const char *ext, char *title, const char *last) +static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last) { /* Show scenario files * .SCN OpenTTD style scenario file @@ -528,7 +528,7 @@ static FiosType FiosGetScenarioListCallback(FileOperation fop, const char *file, return FIOS_TYPE_SCENARIO; } - if (fop == FOP_LOAD) { + if (fop == SLO_LOAD) { if (strcasecmp(ext, ".sv0") == 0 || strcasecmp(ext, ".ss0") == 0 ) { GetOldSaveGameName(file, title, last); return FIOS_TYPE_OLD_SCENARIO; @@ -544,7 +544,7 @@ static FiosType FiosGetScenarioListCallback(FileOperation fop, const char *file, * @param file_list Destination of the found files. * @see FiosGetFileList */ -void FiosGetScenarioList(FileOperation fop, FileList &file_list) +void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list) { static char *fios_scn_path = NULL; static char *fios_scn_path_last = NULL; @@ -562,11 +562,11 @@ void FiosGetScenarioList(FileOperation fop, FileList &file_list) char base_path[MAX_PATH]; FioGetDirectory(base_path, lastof(base_path), SCENARIO_DIR); - Subdirectory subdir = (fop == FOP_LOAD && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY; + Subdirectory subdir = (fop == SLO_LOAD && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY; FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list); } -static FiosType FiosGetHeightmapListCallback(FileOperation fop, const char *file, const char *ext, char *title, const char *last) +static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last) { /* Show heightmap files * .PNG PNG Based heightmap files @@ -615,7 +615,7 @@ static FiosType FiosGetHeightmapListCallback(FileOperation fop, const char *file * @param fop Purpose of collecting the list. * @param file_list Destination of the found files. */ -void FiosGetHeightmapList(FileOperation fop, FileList &file_list) +void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list) { static char *fios_hmap_path = NULL; static char *fios_hmap_path_last = NULL; diff --git a/src/fios.h b/src/fios.h index 80654812b2..51e0c275a6 100644 --- a/src/fios.h +++ b/src/fios.h @@ -190,7 +190,7 @@ public: this->files.Compact(); } - void BuildFileList(AbstractFileType abstract_filetype, FileOperation fop); + void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop); const FiosItem *FindItem(const char *file); SmallVector files; ///< The list of files. @@ -207,11 +207,11 @@ DECLARE_ENUM_AS_BIT_SET(SortingBits) /* Variables to display file lists */ extern SortingBits _savegame_sort_order; -void ShowSaveLoadDialog(AbstractFileType abstract_filetype, FileOperation fop); +void ShowSaveLoadDialog(AbstractFileType abstract_filetype, SaveLoadOperation fop); -void FiosGetSavegameList(FileOperation fop, FileList &file_list); -void FiosGetScenarioList(FileOperation fop, FileList &file_list); -void FiosGetHeightmapList(FileOperation fop, FileList &file_list); +void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list); +void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list); +void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list); const char *FiosBrowseTo(const FiosItem *item); @@ -220,7 +220,7 @@ bool FiosDelete(const char *name); void FiosMakeHeightmapName(char *buf, const char *name, const char *last); void FiosMakeSavegameName(char *buf, const char *name, const char *last); -FiosType FiosGetSavegameListCallback(FileOperation fop, const char *file, const char *ext, char *title, const char *last); +FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last); int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b); diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index a0f0f1b296..e6cd9625cc 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -229,7 +229,7 @@ struct SaveLoadWindow : public Window { private: QueryString filename_editbox; ///< Filename editbox. AbstractFileType abstract_filetype; /// Type of file to select. - FileOperation fop; ///< File operation to perform. + SaveLoadOperation fop; ///< File operation to perform. FileList fios_items; ///< Save game list. FiosItem o_dir; const FiosItem *selected; ///< Selected game in #fios_items, or \c NULL. @@ -243,13 +243,13 @@ public: this->filename_editbox.text.UpdateSize(); } - SaveLoadWindow(WindowDesc *desc, AbstractFileType abstract_filetype, FileOperation fop) + SaveLoadWindow(WindowDesc *desc, AbstractFileType abstract_filetype, SaveLoadOperation fop) : Window(desc), filename_editbox(64), abstract_filetype(abstract_filetype), fop(fop) { - assert(this->fop == FOP_SAVE || this->fop == FOP_LOAD); + assert(this->fop == SLO_SAVE || this->fop == SLO_LOAD); /* For saving, construct an initial file name. */ - if (this->fop == FOP_SAVE) { + if (this->fop == SLO_SAVE) { switch (this->abstract_filetype) { case FT_SAVEGAME: this->GenerateFileName(); @@ -268,7 +268,7 @@ public: this->filename_editbox.ok_button = WID_SL_SAVE_GAME; this->CreateNestedTree(true); - if (this->fop == FOP_LOAD && this->abstract_filetype == FT_SAVEGAME) { + if (this->fop == SLO_LOAD && this->abstract_filetype == FT_SAVEGAME) { this->GetWidget(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL); } @@ -276,15 +276,15 @@ public: StringID caption_string; switch (this->abstract_filetype) { case FT_SAVEGAME: - caption_string = (this->fop == FOP_SAVE) ? STR_SAVELOAD_SAVE_CAPTION : STR_SAVELOAD_LOAD_CAPTION; + caption_string = (this->fop == SLO_SAVE) ? STR_SAVELOAD_SAVE_CAPTION : STR_SAVELOAD_LOAD_CAPTION; break; case FT_SCENARIO: - caption_string = (this->fop == FOP_SAVE) ? STR_SAVELOAD_SAVE_SCENARIO : STR_SAVELOAD_LOAD_SCENARIO; + caption_string = (this->fop == SLO_SAVE) ? STR_SAVELOAD_SAVE_SCENARIO : STR_SAVELOAD_LOAD_SCENARIO; break; case FT_HEIGHTMAP: - caption_string = (this->fop == FOP_SAVE) ? STR_SAVELOAD_SAVE_HEIGHTMAP : STR_SAVELOAD_LOAD_HEIGHTMAP; + caption_string = (this->fop == SLO_SAVE) ? STR_SAVELOAD_SAVE_HEIGHTMAP : STR_SAVELOAD_LOAD_HEIGHTMAP; break; default: @@ -328,7 +328,7 @@ public: } /* Focus the edit box by default in the save windows */ - if (this->fop == FOP_SAVE) this->SetFocusedWidget(WID_SL_SAVE_OSK_TITLE); + if (this->fop == SLO_SAVE) this->SetFocusedWidget(WID_SL_SAVE_OSK_TITLE); } virtual ~SaveLoadWindow() @@ -438,7 +438,7 @@ public: } /* Hide the NewGRF stuff when saving. We also hide the button. */ - if (this->fop == FOP_LOAD && (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO)) { + if (this->fop == SLO_LOAD && (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO)) { y += WD_PAR_VSEP_NORMAL; if (y > y_max) break; @@ -583,19 +583,19 @@ public: if (GetDetailedFileType(file->type) == DFT_GAME_FILE) { /* Other detailed file types cannot be checked before. */ - SaveOrLoad(name, FOP_CHECK, DFT_GAME_FILE, NO_DIRECTORY, false); + SaveOrLoad(name, SLO_CHECK, DFT_GAME_FILE, NO_DIRECTORY, false); } this->InvalidateData(1); } - if (this->fop == FOP_SAVE) { + if (this->fop == SLO_SAVE) { /* Copy clicked name to editbox */ this->filename_editbox.text.Assign(file->title); this->SetWidgetDirty(WID_SL_SAVE_OSK_TITLE); } } else if (!_load_check_data.HasErrors()) { this->selected = file; - if (this->fop == FOP_LOAD) { + if (this->fop == SLO_LOAD) { if (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO) { this->OnClick(pt, WID_SL_LOAD_BUTTON, 1); } else { @@ -621,7 +621,7 @@ public: ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR); } else { #if defined(ENABLE_NETWORK) - assert(this->fop == FOP_LOAD); + assert(this->fop == SLO_LOAD); switch (this->abstract_filetype) { default: NOT_REACHED(); case FT_SCENARIO: ShowNetworkContentListWindow(NULL, CONTENT_TYPE_SCENARIO); break; @@ -654,7 +654,7 @@ public: virtual void OnTimeout() { /* Widgets WID_SL_DELETE_SELECTION and WID_SL_SAVE_GAME only exist when saving to a file. */ - if (this->fop != FOP_SAVE) return; + if (this->fop != SLO_SAVE) return; if (this->IsWidgetLowered(WID_SL_DELETE_SELECTION)) { // Delete button clicked if (!FiosDelete(this->filename_editbox.text.buf)) { @@ -707,7 +707,7 @@ public: /* Selection changes */ if (!gui_scope) break; - if (this->fop != FOP_LOAD) break; + if (this->fop != SLO_LOAD) break; switch (this->abstract_filetype) { case FT_HEIGHTMAP: @@ -764,12 +764,12 @@ static WindowDesc _save_dialog_desc( * @param abstract_filetype Kind of file to handle. * @param fop File operation to perform (load or save). */ -void ShowSaveLoadDialog(AbstractFileType abstract_filetype, FileOperation fop) +void ShowSaveLoadDialog(AbstractFileType abstract_filetype, SaveLoadOperation fop) { DeleteWindowById(WC_SAVELOAD, 0); WindowDesc *sld; - if (fop == FOP_SAVE) { + if (fop == SLO_SAVE) { sld = &_save_dialog_desc; } else { /* Dialogue for loading a file. */ diff --git a/src/genworld.cpp b/src/genworld.cpp index 95e28fcdf0..a08b323116 100644 --- a/src/genworld.cpp +++ b/src/genworld.cpp @@ -204,7 +204,7 @@ static void _GenerateWorld(void *) if (_debug_desync_level > 0) { char name[MAX_PATH]; seprintf(name, lastof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date); - SaveOrLoad(name, FOP_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR, false); + SaveOrLoad(name, SLO_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR, false); } } catch (...) { BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP, true); diff --git a/src/intro_gui.cpp b/src/intro_gui.cpp index b6ac73cb74..a09a59a10d 100644 --- a/src/intro_gui.cpp +++ b/src/intro_gui.cpp @@ -111,9 +111,9 @@ struct SelectGameWindow : public Window { } break; - case WID_SGI_LOAD_GAME: ShowSaveLoadDialog(FT_SAVEGAME, FOP_LOAD); break; - case WID_SGI_PLAY_SCENARIO: ShowSaveLoadDialog(FT_SCENARIO, FOP_LOAD); break; - case WID_SGI_PLAY_HEIGHTMAP: ShowSaveLoadDialog(FT_HEIGHTMAP,FOP_LOAD); break; + case WID_SGI_LOAD_GAME: ShowSaveLoadDialog(FT_SAVEGAME, SLO_LOAD); break; + case WID_SGI_PLAY_SCENARIO: ShowSaveLoadDialog(FT_SCENARIO, SLO_LOAD); break; + case WID_SGI_PLAY_HEIGHTMAP: ShowSaveLoadDialog(FT_HEIGHTMAP,SLO_LOAD); break; case WID_SGI_EDIT_SCENARIO: StartScenarioEditor(); break; case WID_SGI_PLAY_NETWORK: diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index a33e5c6814..edfe4587f3 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -520,7 +520,7 @@ bool ClientNetworkGameSocketHandler::IsConnected() * DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p ************/ -extern bool SafeLoad(const char *filename, FileOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL); +extern bool SafeLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL); NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_FULL(Packet *p) { @@ -836,7 +836,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet /* The map is done downloading, load it */ ClearErrorMessages(); - bool load_success = SafeLoad(NULL, FOP_LOAD, DFT_GAME_FILE, GM_NORMAL, NO_DIRECTORY, lf); + bool load_success = SafeLoad(NULL, SLO_LOAD, DFT_GAME_FILE, GM_NORMAL, NO_DIRECTORY, lf); /* Long savegame loads shouldn't affect the lag calculation! */ this->last_packet = _realtime_tick; diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index c3aef5b0fd..520c4f60a8 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -1187,17 +1187,17 @@ struct NetworkStartServerWindow : public Window { case WID_NSS_LOAD_GAME: _is_network_server = true; - ShowSaveLoadDialog(FT_SAVEGAME, FOP_LOAD); + ShowSaveLoadDialog(FT_SAVEGAME, SLO_LOAD); break; case WID_NSS_PLAY_SCENARIO: _is_network_server = true; - ShowSaveLoadDialog(FT_SCENARIO, FOP_LOAD); + ShowSaveLoadDialog(FT_SCENARIO, SLO_LOAD); break; case WID_NSS_PLAY_HEIGHTMAP: _is_network_server = true; - ShowSaveLoadDialog(FT_HEIGHTMAP,FOP_LOAD); + ShowSaveLoadDialog(FT_HEIGHTMAP,SLO_LOAD); break; } } diff --git a/src/openttd.cpp b/src/openttd.cpp index f00ce74c37..58f011b082 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -328,7 +328,7 @@ static void LoadIntroGame(bool load_newgrfs = true) SetupColoursAndInitialWindow(); /* Load the default opening screen savegame */ - if (SaveOrLoad("opntitle.dat", FOP_LOAD, DFT_GAME_FILE, BASESET_DIR) != SL_OK) { + if (SaveOrLoad("opntitle.dat", SLO_LOAD, DFT_GAME_FILE, BASESET_DIR) != SL_OK) { GenerateWorld(GWM_EMPTY, 64, 64); // if failed loading, make empty world. WaitTillGeneratedWorld(); SetLocalCompany(COMPANY_SPECTATOR); @@ -621,12 +621,12 @@ int openttd_main(int argc, char *argv[]) _file_to_saveload.SetName(mgo.opt); bool is_scenario = _switch_mode == SM_EDITOR || _switch_mode == SM_LOAD_SCENARIO; _switch_mode = is_scenario ? SM_LOAD_SCENARIO : SM_LOAD_GAME; - _file_to_saveload.SetMode(FOP_LOAD, is_scenario ? FT_SCENARIO : FT_SAVEGAME, DFT_GAME_FILE); + _file_to_saveload.SetMode(SLO_LOAD, is_scenario ? FT_SCENARIO : FT_SAVEGAME, DFT_GAME_FILE); /* 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, '.'); if (t != NULL) { - FiosType ft = FiosGetSavegameListCallback(FOP_LOAD, _file_to_saveload.name, t, NULL, NULL); + FiosType ft = FiosGetSavegameListCallback(SLO_LOAD, _file_to_saveload.name, t, NULL, NULL); if (ft != FIOS_TYPE_INVALID) _file_to_saveload.SetMode(ft); } @@ -648,10 +648,10 @@ int openttd_main(int argc, char *argv[]) char title[80]; title[0] = '\0'; - FiosGetSavegameListCallback(FOP_LOAD, mgo.opt, strrchr(mgo.opt, '.'), title, lastof(title)); + FiosGetSavegameListCallback(SLO_LOAD, mgo.opt, strrchr(mgo.opt, '.'), title, lastof(title)); _load_check_data.Clear(); - SaveOrLoadResult res = SaveOrLoad(mgo.opt, FOP_CHECK, DFT_GAME_FILE, SAVE_DIR, false); + SaveOrLoadResult res = SaveOrLoad(mgo.opt, SLO_CHECK, DFT_GAME_FILE, SAVE_DIR, false); if (res != SL_OK || _load_check_data.HasErrors()) { fprintf(stderr, "Failed to open savegame\n"); if (_load_check_data.HasErrors()) { @@ -998,9 +998,9 @@ static void MakeNewEditorWorld() * @param subdir default directory to look for filename, set to 0 if not needed * @param lf Load filter to use, if NULL: use filename + subdir. */ -bool SafeLoad(const char *filename, FileOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL) +bool SafeLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL) { - assert(fop == FOP_LOAD); + assert(fop == SLO_LOAD); assert(dft == DFT_GAME_FILE || (lf == NULL && dft == DFT_OLD_GAME_FILE)); GameMode ogm = _game_mode; @@ -1158,7 +1158,7 @@ void SwitchToMode(SwitchMode new_mode) case SM_SAVE_GAME: // Save game. /* Make network saved games on pause compatible to singleplayer */ - if (SaveOrLoad(_file_to_saveload.name, FOP_SAVE, DFT_GAME_FILE, NO_DIRECTORY) != SL_OK) { + if (SaveOrLoad(_file_to_saveload.name, SLO_SAVE, DFT_GAME_FILE, NO_DIRECTORY) != SL_OK) { SetDParamStr(0, GetSaveLoadErrorString()); ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR); } else { @@ -1369,7 +1369,7 @@ void StateGameLoop() /* Save the desync savegame if needed. */ char name[MAX_PATH]; seprintf(name, lastof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date); - SaveOrLoad(name, FOP_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR, false); + SaveOrLoad(name, SLO_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR, false); } CheckCaches(); @@ -1426,7 +1426,7 @@ static void DoAutosave() } DEBUG(sl, 2, "Autosaving to '%s'", buf); - if (SaveOrLoad(buf, FOP_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR) != SL_OK) { + if (SaveOrLoad(buf, SLO_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR) != SL_OK) { ShowErrorMessage(STR_ERROR_AUTOSAVE_FAILED, INVALID_STRING_ID, WL_ERROR); } } diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 8da0d03984..7b7341115a 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -2778,10 +2778,10 @@ SaveOrLoadResult LoadWithFilter(LoadFilter *reader) * @param threaded True when threaded saving is allowed * @return Return the result of the action. #SL_OK, #SL_ERROR, or #SL_REINIT ("unload" the game) */ -SaveOrLoadResult SaveOrLoad(const char *filename, FileOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded) +SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded) { /* An instance of saving is already active, so don't go saving again */ - if (_sl.saveinprogress && fop == FOP_SAVE && dft == DFT_GAME_FILE && threaded) { + if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) { /* if not an autosave, but a user action, show error message */ if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR); return SL_OK; @@ -2790,7 +2790,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, FileOperation fop, DetailedFil try { /* Load a TTDLX or TTDPatch game */ - if (fop == FOP_LOAD && dft == DFT_OLD_GAME_FILE) { + if (fop == SLO_LOAD && dft == DFT_OLD_GAME_FILE) { InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them @@ -2813,33 +2813,33 @@ SaveOrLoadResult SaveOrLoad(const char *filename, FileOperation fop, DetailedFil assert(dft == DFT_GAME_FILE); switch (fop) { - case FOP_CHECK: + case SLO_CHECK: _sl.action = SLA_LOAD_CHECK; break; - case FOP_LOAD: + case SLO_LOAD: _sl.action = SLA_LOAD; break; - case FOP_SAVE: + case SLO_SAVE: _sl.action = SLA_SAVE; break; default: NOT_REACHED(); } - FILE *fh = (fop == FOP_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb); + FILE *fh = (fop == SLO_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb); /* Make it a little easier to load savegames from the console */ - if (fh == NULL && fop != FOP_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR); - if (fh == NULL && fop != FOP_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR); - if (fh == NULL && fop != FOP_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR); + if (fh == NULL && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR); + if (fh == NULL && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR); + if (fh == NULL && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR); if (fh == NULL) { - SlError(fop == FOP_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE); + SlError(fop == SLO_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE); } - if (fop == FOP_SAVE) { // SAVE game + if (fop == SLO_SAVE) { // SAVE game DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename); if (_network_server || !_settings_client.gui.threaded_saves) threaded = false; @@ -2847,25 +2847,25 @@ SaveOrLoadResult SaveOrLoad(const char *filename, FileOperation fop, DetailedFil } /* LOAD game */ - assert(fop == FOP_LOAD || fop == FOP_CHECK); + assert(fop == SLO_LOAD || fop == SLO_CHECK); DEBUG(desync, 1, "load: %s", filename); - return DoLoad(new FileReader(fh), fop == FOP_CHECK); + return DoLoad(new FileReader(fh), fop == SLO_CHECK); } catch (...) { /* This code may be executed both for old and new save games. */ ClearSaveLoadState(); /* Skip the "colour" character */ - if (fop != FOP_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3); + if (fop != SLO_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3); /* A saver/loader exception!! reinitialize all variables to prevent crash! */ - return (fop == FOP_LOAD) ? SL_REINIT : SL_ERROR; + return (fop == SLO_LOAD) ? SL_REINIT : SL_ERROR; } } /** Do a save when exiting the game (_settings_client.gui.autosave_on_exit) */ void DoExitSave() { - SaveOrLoad("exit.sav", FOP_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR); + SaveOrLoad("exit.sav", SLO_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR); } /** @@ -2909,7 +2909,7 @@ void GenerateDefaultSaveName(char *buf, const char *last) */ void FileToSaveLoad::SetMode(FiosType ft) { - this->SetMode(FOP_LOAD, GetAbstractFileType(ft), GetDetailedFileType(ft)); + this->SetMode(SLO_LOAD, GetAbstractFileType(ft), GetDetailedFileType(ft)); } /** @@ -2918,10 +2918,10 @@ void FileToSaveLoad::SetMode(FiosType ft) * @param aft Abstract file type. * @param dft Detailed file type. */ -void FileToSaveLoad::SetMode(FileOperation fop, AbstractFileType aft, DetailedFileType dft) +void FileToSaveLoad::SetMode(SaveLoadOperation fop, AbstractFileType aft, DetailedFileType dft) { if (aft == FT_INVALID || aft == FT_NONE) { - this->file_op = FOP_INVALID; + this->file_op = SLO_INVALID; this->detail_ftype = DFT_INVALID; this->abstract_ftype = FT_INVALID; return; diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index b25e91e2cd..1513355c8d 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -24,14 +24,14 @@ enum SaveOrLoadResult { /** Deals with the type of the savegame, independent of extension */ struct FileToSaveLoad { - FileOperation file_op; ///< File operation to perform. + SaveLoadOperation file_op; ///< File operation to perform. DetailedFileType detail_ftype; ///< Concrete file type (PNG, BMP, old save, etc). AbstractFileType abstract_ftype; ///< Abstract type of file (scenario, heightmap, etc). char name[MAX_PATH]; ///< Name of the file. char title[255]; ///< Internal name of the game. void SetMode(FiosType ft); - void SetMode(FileOperation fop, AbstractFileType aft, DetailedFileType dft); + void SetMode(SaveLoadOperation fop, AbstractFileType aft, DetailedFileType dft); void SetName(const char *name); void SetTitle(const char *title); }; @@ -51,7 +51,7 @@ extern FileToSaveLoad _file_to_saveload; void GenerateDefaultSaveName(char *buf, const char *last); void SetSaveLoadError(uint16 str); const char *GetSaveLoadErrorString(); -SaveOrLoadResult SaveOrLoad(const char *filename, FileOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded = true); +SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded = true); void WaitTillSaved(); void ProcessAsyncSaveFinish(); void DoExitSave(); diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index 55b49fd748..2649552f45 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -427,17 +427,17 @@ static CallBackFunction MenuClickSaveLoad(int index = 0) { if (_game_mode == GM_EDITOR) { switch (index) { - case SLEME_SAVE_SCENARIO: ShowSaveLoadDialog(FT_SCENARIO, FOP_SAVE); break; - case SLEME_LOAD_SCENARIO: ShowSaveLoadDialog(FT_SCENARIO, FOP_LOAD); break; - case SLEME_SAVE_HEIGHTMAP: ShowSaveLoadDialog(FT_HEIGHTMAP,FOP_SAVE); break; - case SLEME_LOAD_HEIGHTMAP: ShowSaveLoadDialog(FT_HEIGHTMAP,FOP_LOAD); break; + case SLEME_SAVE_SCENARIO: ShowSaveLoadDialog(FT_SCENARIO, SLO_SAVE); break; + case SLEME_LOAD_SCENARIO: ShowSaveLoadDialog(FT_SCENARIO, SLO_LOAD); break; + case SLEME_SAVE_HEIGHTMAP: ShowSaveLoadDialog(FT_HEIGHTMAP,SLO_SAVE); break; + case SLEME_LOAD_HEIGHTMAP: ShowSaveLoadDialog(FT_HEIGHTMAP,SLO_LOAD); break; case SLEME_EXIT_TOINTRO: AskExitToGameMenu(); break; case SLEME_EXIT_GAME: HandleExitGameRequest(); break; } } else { switch (index) { - case SLNME_SAVE_GAME: ShowSaveLoadDialog(FT_SAVEGAME, FOP_SAVE); break; - case SLNME_LOAD_GAME: ShowSaveLoadDialog(FT_SAVEGAME, FOP_LOAD); break; + case SLNME_SAVE_GAME: ShowSaveLoadDialog(FT_SAVEGAME, SLO_SAVE); break; + case SLNME_LOAD_GAME: ShowSaveLoadDialog(FT_SAVEGAME, SLO_LOAD); break; case SLNME_EXIT_TOINTRO: AskExitToGameMenu(); break; case SLNME_EXIT_GAME: HandleExitGameRequest(); break; } @@ -1712,7 +1712,7 @@ struct MainToolbarWindow : Window { case MTHK_FASTFORWARD: ToolbarFastForwardClick(this); break; case MTHK_SETTINGS: ShowGameOptions(); break; case MTHK_SAVEGAME: MenuClickSaveLoad(); break; - case MTHK_LOADGAME: ShowSaveLoadDialog(FT_SAVEGAME, FOP_LOAD); break; + case MTHK_LOADGAME: ShowSaveLoadDialog(FT_SAVEGAME, SLO_LOAD); break; case MTHK_SMALLMAP: ShowSmallMap(); break; case MTHK_TOWNDIRECTORY: ShowTownDirectory(); break; case MTHK_SUBSIDIES: ShowSubsidiesList(); break; diff --git a/src/video/dedicated_v.cpp b/src/video/dedicated_v.cpp index 95d40cd000..e038df9751 100644 --- a/src/video/dedicated_v.cpp +++ b/src/video/dedicated_v.cpp @@ -142,7 +142,7 @@ static void *_dedicated_video_mem; /* Whether a fork has been done. */ bool _dedicated_forks; -extern bool SafeLoad(const char *filename, FileOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL); +extern bool SafeLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL); static FVideoDriver_Dedicated iFVideoDriver_Dedicated; From 533cb7c156ec4ffc36e688379afa94e1b814885c Mon Sep 17 00:00:00 2001 From: frosch Date: Mon, 5 Sep 2016 19:11:01 +0000 Subject: [PATCH 22/28] (svn r27654) -Change: Enable C++11 for clang 3.3 (LordAro) --- config.lib | 7 ++++++- src/table/newgrf_debug_data.h | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/config.lib b/config.lib index ab9bb82b61..a144b9e3ea 100644 --- a/config.lib +++ b/config.lib @@ -1285,7 +1285,7 @@ make_compiler_cflags() { # -W alone doesn't enable all warnings enabled by -Wall; on the other hand, # -Weverything enables too many useless warnings that can't be disabled (as of 3.0) - flags="$flags -Wall -W" + flags="$flags -Wall -W -Wextra" # warning: unused parameter '...' flags="$flags -Wno-unused-parameter" @@ -1326,6 +1326,11 @@ make_compiler_cflags() { flags="$flags -Wno-unused-variable" fi + if [ "$cc_version" -ge "33" ]; then + # clang completed C++11 support in version 3.3 + flags="$flags -std=c++11" + fi + # rdynamic is used to get useful stack traces from crash reports. ldflags="$ldflags -rdynamic" else diff --git a/src/table/newgrf_debug_data.h b/src/table/newgrf_debug_data.h index b7d0c92024..afe19bbedb 100644 --- a/src/table/newgrf_debug_data.h +++ b/src/table/newgrf_debug_data.h @@ -17,7 +17,7 @@ #define NIP_END() { NULL, 0, 0, 0, 0 } /* Helper for filling callback tables */ -#define NIC(cb_id, base, variable, bit) { #cb_id, cpp_offsetof(base, variable), cpp_sizeof(base, variable), bit, cb_id } +#define NIC(cb_id, base, variable, bit) { #cb_id, (ptrdiff_t)cpp_offsetof(base, variable), cpp_sizeof(base, variable), bit, cb_id } #define NIC_END() { NULL, 0, 0, 0, 0 } /* Helper for filling variable tables */ From 2fe7571034d8d29ced235e912f76764bd084836b Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 5 Sep 2016 01:38:35 +0100 Subject: [PATCH 23/28] Require C++11 support. Set minimum compiler versions to: GCC 4.7 clang 3.3 ICC 14.0 --- config.lib | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/config.lib b/config.lib index a144b9e3ea..961e5eae8c 100644 --- a/config.lib +++ b/config.lib @@ -1265,7 +1265,12 @@ make_compiler_cflags() { # remark #2259: non-pointer conversion from ... to ... may lose significant bits flags="$flags -wd2259" # Use c++0x mode so static_assert() is available - cxxflags="$cxxflags -std=c++0x" + cxxflags="$cxxflags -std=c++11" + fi + + if [ $cc_version -lt 140 ]; then + log 1 "configure: error: ICC version is too old: `$1 -dumpversion`, minumum: 14.0" + exit 1 fi if [ "$enable_lto" != "0" ]; then @@ -1329,6 +1334,9 @@ make_compiler_cflags() { if [ "$cc_version" -ge "33" ]; then # clang completed C++11 support in version 3.3 flags="$flags -std=c++11" + else + log 1 "configure: error: clang version is too old: `$1 -v 2>&1 | head -n 1`, minumum: 3.3" + exit 1 fi # rdynamic is used to get useful stack traces from crash reports. @@ -1390,7 +1398,7 @@ make_compiler_cflags() { if [ $cc_version -ge 403 ] && [ $cc_version -lt 600 ]; then # Use gnu++0x mode so static_assert() is available. # Don't use c++0x, it breaks mingw (with gcc 4.4.0). - cxxflags="$cxxflags -std=gnu++0x" + cxxflags="$cxxflags -std=gnu++11" fi if [ $cc_version -eq 405 ]; then @@ -1406,6 +1414,9 @@ make_compiler_cflags() { cxxflags="$cxxflags -Wno-narrowing" # Disable bogus 'attempt to free a non-heap object' warning flags="$flags -Wno-free-nonheap-object" + else + log 1 "configure: error: GCC version is too old: `$1 -dumpversion`, minumum: 4.7" + exit 1 fi if [ $cc_version -ge 600 ]; then From 51099c3572255e9364ce1fbbb5b93535b052beca Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 5 Sep 2016 01:15:09 +0100 Subject: [PATCH 24/28] Unconditionally use static_assert. --- src/stdafx.h | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/stdafx.h b/src/stdafx.h index 4616212f18..4ca0a12c30 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -361,21 +361,8 @@ typedef unsigned char byte; #define PERSONAL_DIR "" #endif -/* Compile time assertions. Prefer c++0x static_assert(). - * Older compilers cannot evaluate some expressions at compile time, - * typically when templates are involved, try assert_tcompile() in those cases. */ -#if defined(__STDCXX_VERSION__) || defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(__GXX_EXPERIMENTAL_CPP0X__) || defined(static_assert) - /* __STDCXX_VERSION__ is c++0x feature macro, __GXX_EXPERIMENTAL_CXX0X__ is used by gcc, __GXX_EXPERIMENTAL_CPP0X__ by icc */ - #define assert_compile(expr) static_assert(expr, #expr ) - #define assert_tcompile(expr) assert_compile(expr) -#elif defined(__OS2__) - /* Disabled for OS/2 */ - #define assert_compile(expr) - #define assert_tcompile(expr) assert_compile(expr) -#else - #define assert_compile(expr) typedef int __ct_assert__[1 - 2 * !(expr)] - #define assert_tcompile(expr) assert(expr) -#endif +#define assert_compile(expr) static_assert(expr, #expr ) +#define assert_tcompile(expr) assert_compile(expr) /* Check if the types have the bitsizes like we are using them */ assert_compile(sizeof(uint64) == 8); From 0b7d68a6d1e22c6566ec57e938d0322ac302331e Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 5 Sep 2016 01:18:09 +0100 Subject: [PATCH 25/28] Add support for verbose asserts. (cherry picked from commit 48e4c35fdce27ce2cb139f72374301f658276d0c) --- src/openttd.cpp | 19 +++++++++++++++++++ src/stdafx.h | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/src/openttd.cpp b/src/openttd.cpp index 58f011b082..cc382bdbf0 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -123,6 +123,25 @@ void CDECL error(const char *s, ...) abort(); } +void CDECL assert_msg_error(int line, const char *file, const char *expr, const char *str, ...) +{ + va_list va; + char buf[2048]; + + char *b = buf; + b += seprintf(b, lastof(buf), "Assertion failed at line %i of %s: %s\n\t", line, file, expr); + + va_start(va, str); + vseprintf(b, lastof(buf), str, va); + va_end(va); + + ShowOSErrorBox(buf, true); + + /* Set the error message for the crash log and then invoke it. */ + CrashLog::SetErrorMessage(buf); + abort(); +} + /** * Shows some information on the console/a popup box depending on the OS. * @param str the text to show. diff --git a/src/stdafx.h b/src/stdafx.h index 4ca0a12c30..cf9fe92f99 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -433,6 +433,7 @@ assert_compile(SIZE_MAX >= UINT32_MAX); void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2); void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2); +void NORETURN CDECL assert_msg_error(int line, const char *file, const char *expr, const char *str, ...) WARN_FORMAT(4, 5); #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__) /* For non-debug builds with assertions enabled use the special assertion handler: @@ -447,6 +448,9 @@ void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2); /* Asserts are enabled if NDEBUG isn't defined, or if we are using MSVC and WITH_ASSERT is defined. */ #if !defined(NDEBUG) || (defined(_MSC_VER) && defined(WITH_ASSERT)) #define OTTD_ASSERT + #define assert_msg(expression, ...) if (!(expression)) assert_msg_error(__LINE__, __FILE__, #expression, __VA_ARGS__); +#else + #define assert_msg(expression, ...) #endif #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__) From bb6ea150a38d1438876b0049ba57902c6eb61f93 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Thu, 10 Mar 2016 00:13:58 +0000 Subject: [PATCH 26/28] Use likely/__builtin_expect for assertion macros. (cherry picked from commit f82002cda2e3cb1ef1264f0c2e091bd88b449f48) --- src/stdafx.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/stdafx.h b/src/stdafx.h index cf9fe92f99..631f64cca7 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -431,6 +431,14 @@ assert_compile(SIZE_MAX >= UINT32_MAX); #define CloseConnection OTTD_CloseConnection #endif /* __APPLE__ */ +#ifdef __GNUC__ +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) +#else +#define likely(x) (x) +#define unlikely(x) (x) +#endif + void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2); void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2); void NORETURN CDECL assert_msg_error(int line, const char *file, const char *expr, const char *str, ...) WARN_FORMAT(4, 5); @@ -442,13 +450,13 @@ void NORETURN CDECL assert_msg_error(int line, const char *file, const char *exp */ #if (defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_ASSERT)) || (!defined(_MSC_VER) && !defined(NDEBUG) && !defined(_DEBUG)) #undef assert - #define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression); + #define assert(expression) if (unlikely(!(expression))) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression); #endif /* Asserts are enabled if NDEBUG isn't defined, or if we are using MSVC and WITH_ASSERT is defined. */ #if !defined(NDEBUG) || (defined(_MSC_VER) && defined(WITH_ASSERT)) #define OTTD_ASSERT - #define assert_msg(expression, ...) if (!(expression)) assert_msg_error(__LINE__, __FILE__, #expression, __VA_ARGS__); + #define assert_msg(expression, ...) if (unlikely(!(expression))) assert_msg_error(__LINE__, __FILE__, #expression, __VA_ARGS__); #else #define assert_msg(expression, ...) #endif From 6e9ea59ab50cb9598c3b83e357b7fd13a645c4b4 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 5 Sep 2016 20:56:31 +0100 Subject: [PATCH 27/28] Add a simple scope guard include file. --- projects/openttd_vs100.vcxproj | 1 + projects/openttd_vs100.vcxproj.filters | 3 ++ projects/openttd_vs140.vcxproj | 1 + projects/openttd_vs140.vcxproj.filters | 3 ++ projects/openttd_vs80.vcproj | 4 ++ projects/openttd_vs90.vcproj | 4 ++ source.list | 1 + src/scope.h | 53 ++++++++++++++++++++++++++ 8 files changed, 70 insertions(+) create mode 100644 src/scope.h diff --git a/projects/openttd_vs100.vcxproj b/projects/openttd_vs100.vcxproj index 59e087ee70..a7abe4d1ab 100644 --- a/projects/openttd_vs100.vcxproj +++ b/projects/openttd_vs100.vcxproj @@ -571,6 +571,7 @@ + diff --git a/projects/openttd_vs100.vcxproj.filters b/projects/openttd_vs100.vcxproj.filters index 06800ffdaf..d996963c06 100644 --- a/projects/openttd_vs100.vcxproj.filters +++ b/projects/openttd_vs100.vcxproj.filters @@ -942,6 +942,9 @@ Header Files + + Header Files + Header Files diff --git a/projects/openttd_vs140.vcxproj b/projects/openttd_vs140.vcxproj index 8d8b980649..67acda0402 100644 --- a/projects/openttd_vs140.vcxproj +++ b/projects/openttd_vs140.vcxproj @@ -588,6 +588,7 @@ + diff --git a/projects/openttd_vs140.vcxproj.filters b/projects/openttd_vs140.vcxproj.filters index 06800ffdaf..d996963c06 100644 --- a/projects/openttd_vs140.vcxproj.filters +++ b/projects/openttd_vs140.vcxproj.filters @@ -942,6 +942,9 @@ Header Files + + Header Files + Header Files diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj index 70dcab226a..fcd9c0e3a3 100644 --- a/projects/openttd_vs80.vcproj +++ b/projects/openttd_vs80.vcproj @@ -1558,6 +1558,10 @@ RelativePath=".\..\src\safeguards.h" > + + diff --git a/projects/openttd_vs90.vcproj b/projects/openttd_vs90.vcproj index dd722d085d..cd5eea9d4e 100644 --- a/projects/openttd_vs90.vcproj +++ b/projects/openttd_vs90.vcproj @@ -1555,6 +1555,10 @@ RelativePath=".\..\src\safeguards.h" > + + diff --git a/source.list b/source.list index df35cdd26e..59b2824ad0 100644 --- a/source.list +++ b/source.list @@ -310,6 +310,7 @@ road_type.h roadstop_base.h roadveh.h safeguards.h +scope.h screenshot.h sdl.h sound/sdl_s.h diff --git a/src/scope.h b/src/scope.h new file mode 100644 index 0000000000..d341b50b1f --- /dev/null +++ b/src/scope.h @@ -0,0 +1,53 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file scope.h Simple scope guard... */ + +#ifndef SCOPE_H +#define SCOPE_H + +template +class scope_exit_obj { + T f; + bool shouldexec; + + + public: + + scope_exit_obj(T &&func) + : f(std::move(func)), shouldexec(true) { } + + scope_exit_obj(const scope_exit_obj ©src) = delete; + scope_exit_obj(scope_exit_obj &&movesrc) + : f(std::move(movesrc.f)), shouldexec(movesrc.shouldexec) { + movesrc.shouldexec = false; + } + + ~scope_exit_obj() { + exec(); + } + + void exec() { + if (shouldexec) { + f(); + shouldexec = false; + } + } + + void cancel() { + shouldexec = false; + } +}; + +template +scope_exit_obj::type> scope_guard(T &&func) { + return scope_exit_obj::type>(std::forward(func)); +} + +#endif /* SCOPE_H */ From f7af8b8bdc8d0c35216a263deab96349ed0a968e Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 5 Sep 2016 23:00:45 +0100 Subject: [PATCH 28/28] Silence -Wunused-variable warnings in src/table/townname.h --- src/table/townname.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/table/townname.h b/src/table/townname.h index 25c997d255..d14999340c 100644 --- a/src/table/townname.h +++ b/src/table/townname.h @@ -11,6 +11,10 @@ #include "../core/enum_type.hpp" +#if defined(__GNUC__) +#pragma GCC diagnostic ignored "-Wunused-variable" +#endif + static const char * const _name_original_english_1[] = { "Great ", "Little ",