diff --git a/src/hotkey/hotkey_handler.cpp b/src/hotkey/hotkey_handler.cpp index 51e80cb3a2e0..b10a7e15bd32 100644 --- a/src/hotkey/hotkey_handler.cpp +++ b/src/hotkey/hotkey_handler.cpp @@ -394,10 +394,14 @@ void play_controller::hotkey_handler::expand_autosaves(std::vector& item std::vector newitems; std::vector newsaves; - const std::string& start_name = saved_game_.classification().label; + compression::format compression_format = preferences::save_compression_format(); + savegame::autosave_savegame autosave(saved_game_, compression_format); + savegame::scenariostart_savegame scenariostart_save(saved_game_, compression_format); + + const std::string start_name = scenariostart_save.create_filename(); for(unsigned int turn = play_controller_.turn(); turn != 0; turn--) { - const std::string name = formatter() << start_name << "-" << _("Auto-Save") << turn; + const std::string name = autosave.create_filename(turn); if(savegame::save_game_exists(name, comp_format)) { newsaves.emplace_back(name + compression::format_extension(comp_format)); diff --git a/src/savegame.cpp b/src/savegame.cpp index 3b7e84f2489e..e23ed176a96e 100644 --- a/src/savegame.cpp +++ b/src/savegame.cpp @@ -337,7 +337,7 @@ savegame::savegame(saved_game& gamestate, const compression::format compress_sav bool savegame::save_game_automatic(bool ask_for_overwrite, const std::string& filename) { if (filename.empty()) - create_filename(); + filename_ = create_filename(); else filename_ = filename; @@ -353,7 +353,7 @@ bool savegame::save_game_automatic(bool ask_for_overwrite, const std::string& fi bool savegame::save_game_interactive(const std::string& message, DIALOG_TYPE dialog_type) { show_confirmation_ = true; - create_filename(); + filename_ = create_filename(); const int res = show_save_dialog(message, dialog_type); @@ -383,8 +383,6 @@ int savegame::show_save_dialog(const std::string& message, DIALOG_TYPE dialog_ty res = dlg.get_retval(); } - set_filename(filename_); - if (!check_filename(filename_)) { res = gui2::window::CANCEL; } @@ -425,11 +423,12 @@ bool savegame::is_illegal_file_char(char c) ; } -void savegame::set_filename(std::string filename) +std::string savegame::create_filename(unsigned int turn_number) const { + std::string filename = create_initial_filename(turn_number); filename.erase(std::remove_if(filename.begin(), filename.end(), is_illegal_file_char), filename.end()); - filename_ = filename; + return filename; } void savegame::before_save() @@ -541,7 +540,12 @@ filesystem::scoped_ostream savegame::open_save_game(const std::string &label) scenariostart_savegame::scenariostart_savegame(saved_game &gamestate, const compression::format compress_saves) : savegame(gamestate, compress_saves) { - set_filename(gamestate.classification().label); + filename_ = create_filename(); +} + +std::string scenariostart_savegame::create_initial_filename(unsigned int) const +{ + return gamestate().classification().label; } void scenariostart_savegame::write_game(config_writer &out){ @@ -553,9 +557,9 @@ replay_savegame::replay_savegame(saved_game &gamestate, const compression::forma : savegame(gamestate, compress_saves, _("Save Replay")) {} -void replay_savegame::create_filename() +std::string replay_savegame::create_initial_filename(unsigned int) const { - set_filename(formatter() << gamestate().classification().label << " " << _("replay")); + return formatter() << gamestate().classification().label << " " << _("replay"); } void replay_savegame::write_game(config_writer &out) { @@ -586,15 +590,15 @@ void autosave_savegame::autosave(const bool disable_autosave, const int autosave remove_old_auto_saves(autosave_max, infinite_autosaves); } -void autosave_savegame::create_filename() +std::string autosave_savegame::create_initial_filename(unsigned int turn_number) const { std::string filename; - if (gamestate().classification().label.empty()) + if(gamestate().classification().label.empty()) filename = _("Auto-Save"); else - filename = gamestate().classification().label + "-" + _("Auto-Save") + gamestate().get_starting_pos()["turn_at"]; + filename = gamestate().classification().label + "-" + _("Auto-Save") + std::to_string(turn_number); - set_filename(filename); + return filename; } oos_savegame::oos_savegame(saved_game& gamestate, bool& ignore) @@ -606,17 +610,13 @@ int oos_savegame::show_save_dialog(const std::string& message, DIALOG_TYPE /*dia { int res = 0; - std::string filename = this->filename(); - if (!ignore_){ - gui2::dialogs::game_save_oos dlg(ignore_, filename, title(), message); + gui2::dialogs::game_save_oos dlg(ignore_, filename_, title(), message); dlg.show(); res = dlg.get_retval(); } - set_filename(filename); - - if (!check_filename(filename)) { + if (!check_filename(filename_)) { res = gui2::window::CANCEL; } @@ -628,10 +628,10 @@ ingame_savegame::ingame_savegame(saved_game &gamestate, const compression::forma { } -void ingame_savegame::create_filename() +std::string ingame_savegame::create_initial_filename(unsigned int turn_number) const { - set_filename(formatter() << gamestate().classification().label - << " " << _("Turn") << " " << gamestate().get_starting_pos()["turn_at"]); + return formatter() << gamestate().classification().label + << " " << _("Turn") << " " << turn_number; } void ingame_savegame::write_game(config_writer &out) { diff --git a/src/savegame.hpp b/src/savegame.hpp index c4ee21d904e5..ee1036c607f0 100644 --- a/src/savegame.hpp +++ b/src/savegame.hpp @@ -18,12 +18,12 @@ #include "config.hpp" #include "filesystem.hpp" #include "lua_jailbreak_exception.hpp" +#include "saved_game.hpp" #include "serialization/compression.hpp" #include class config_writer; -class saved_game; class version_info; namespace savegame { @@ -170,6 +170,15 @@ class savegame const std::string& filename() const { return filename_; } + /** Build the filename according to the specific savegame's needs. */ + std::string create_filename() const + { + return create_filename(gamestate().get_starting_pos()["turn_at"]); + } + + /** Build the filename for the specified turn. */ + std::string create_filename(unsigned int turn_number) const; + protected: /** Save a game without any further user interaction. @@ -177,9 +186,6 @@ class savegame */ bool save_game(const std::string& filename = ""); - /** Sets the filename and removes invalid characters. Don't set the filename directly but - use this method instead. */ - void set_filename(std::string filename); /** Check, if the filename contains illegal constructs like ".gz". */ bool check_filename(const std::string& filename); @@ -195,13 +201,15 @@ class savegame /** Writing the savegame config to a file. */ virtual void write_game(config_writer &out); + /** Filename of the savegame file on disk */ + std::string filename_; + private: /** Checks if a certain character is allowed in a savefile name. */ static bool is_illegal_file_char(char c); - /** Build the filename according to the specific savegame's needs. Subclasses will have to - override this to take effect. */ - virtual void create_filename() {} + /** Subclass-specific part of filename building. */ + virtual std::string create_initial_filename(unsigned int turn_number) const = 0; /** Display the save game dialog. */ virtual int show_save_dialog(const std::string& message, DIALOG_TYPE dialog_type); /** Ask the user if an existing file should be overwritten. */ @@ -218,8 +226,6 @@ class savegame friend class save_info; //before_save (write replay data) changes this so it cannot be const saved_game& gamestate_; - /** Filename of the savegame file on disk */ - std::string filename_; const std::string title_; /** Title of the savegame dialog */ @@ -239,7 +245,7 @@ class ingame_savegame : public savegame private: /** Create a filename for automatic saves */ - virtual void create_filename() override; + virtual std::string create_initial_filename(unsigned int turn_number) const override; void write_game(config_writer &out) override; @@ -253,7 +259,7 @@ class replay_savegame : public savegame private: /** Create a filename for automatic saves */ - virtual void create_filename() override; + virtual std::string create_initial_filename(unsigned int turn_number) const override; void write_game(config_writer &out) override; }; @@ -267,7 +273,7 @@ class autosave_savegame : public ingame_savegame void autosave(const bool disable_autosave, const int autosave_max, const int infinite_autosaves); private: /** Create a filename for automatic saves */ - virtual void create_filename() override; + virtual std::string create_initial_filename(unsigned int turn_number) const override; }; class oos_savegame : public ingame_savegame @@ -288,6 +294,9 @@ class scenariostart_savegame : public savegame scenariostart_savegame(saved_game& gamestate, const compression::format compress_saves); private: + /** Create a filename for automatic saves */ + virtual std::string create_initial_filename(unsigned int turn_number) const override; + void write_game(config_writer &out) override; };