Create savegame filenames with the same code as in actual saving

Follow-up to commit 057a53055f.

Otherwise, Menu -> Back to... would be broken whenever the logic differs
between that feature and actual saving.
This commit is contained in:
Jyrki Vesterinen 2018-02-07 21:03:46 +02:00
parent 39c3f21b9b
commit 5ec8d630ed
3 changed files with 49 additions and 36 deletions

View file

@ -394,10 +394,14 @@ void play_controller::hotkey_handler::expand_autosaves(std::vector<config>& item
std::vector<config> newitems; std::vector<config> newitems;
std::vector<std::string> newsaves; std::vector<std::string> 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--) { 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)) { if(savegame::save_game_exists(name, comp_format)) {
newsaves.emplace_back(name + compression::format_extension(comp_format)); newsaves.emplace_back(name + compression::format_extension(comp_format));

View file

@ -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) bool savegame::save_game_automatic(bool ask_for_overwrite, const std::string& filename)
{ {
if (filename.empty()) if (filename.empty())
create_filename(); filename_ = create_filename();
else else
filename_ = filename; 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) bool savegame::save_game_interactive(const std::string& message, DIALOG_TYPE dialog_type)
{ {
show_confirmation_ = true; show_confirmation_ = true;
create_filename(); filename_ = create_filename();
const int res = show_save_dialog(message, dialog_type); 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(); res = dlg.get_retval();
} }
set_filename(filename_);
if (!check_filename(filename_)) { if (!check_filename(filename_)) {
res = gui2::window::CANCEL; 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(), filename.erase(std::remove_if(filename.begin(), filename.end(),
is_illegal_file_char), filename.end()); is_illegal_file_char), filename.end());
filename_ = filename; return filename;
} }
void savegame::before_save() 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) scenariostart_savegame::scenariostart_savegame(saved_game &gamestate, const compression::format compress_saves)
: savegame(gamestate, 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){ 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")) : 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) { 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); 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; std::string filename;
if (gamestate().classification().label.empty()) if(gamestate().classification().label.empty())
filename = _("Auto-Save"); filename = _("Auto-Save");
else 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) 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; int res = 0;
std::string filename = this->filename();
if (!ignore_){ if (!ignore_){
gui2::dialogs::game_save_oos dlg(ignore_, filename, title(), message); gui2::dialogs::game_save_oos dlg(ignore_, filename_, title(), message);
dlg.show(); dlg.show();
res = dlg.get_retval(); res = dlg.get_retval();
} }
set_filename(filename); if (!check_filename(filename_)) {
if (!check_filename(filename)) {
res = gui2::window::CANCEL; 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 return formatter() << gamestate().classification().label
<< " " << _("Turn") << " " << gamestate().get_starting_pos()["turn_at"]); << " " << _("Turn") << " " << turn_number;
} }
void ingame_savegame::write_game(config_writer &out) { void ingame_savegame::write_game(config_writer &out) {

View file

@ -18,12 +18,12 @@
#include "config.hpp" #include "config.hpp"
#include "filesystem.hpp" #include "filesystem.hpp"
#include "lua_jailbreak_exception.hpp" #include "lua_jailbreak_exception.hpp"
#include "saved_game.hpp"
#include "serialization/compression.hpp" #include "serialization/compression.hpp"
#include <exception> #include <exception>
class config_writer; class config_writer;
class saved_game;
class version_info; class version_info;
namespace savegame { namespace savegame {
@ -170,6 +170,15 @@ public:
const std::string& filename() const { return filename_; } 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: protected:
/** /**
Save a game without any further user interaction. Save a game without any further user interaction.
@ -177,9 +186,6 @@ protected:
*/ */
bool save_game(const std::string& filename = ""); 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". */ /** Check, if the filename contains illegal constructs like ".gz". */
bool check_filename(const std::string& filename); bool check_filename(const std::string& filename);
@ -195,13 +201,15 @@ protected:
/** Writing the savegame config to a file. */ /** Writing the savegame config to a file. */
virtual void write_game(config_writer &out); virtual void write_game(config_writer &out);
/** Filename of the savegame file on disk */
std::string filename_;
private: private:
/** Checks if a certain character is allowed in a savefile name. */ /** Checks if a certain character is allowed in a savefile name. */
static bool is_illegal_file_char(char c); static bool is_illegal_file_char(char c);
/** Build the filename according to the specific savegame's needs. Subclasses will have to /** Subclass-specific part of filename building. */
override this to take effect. */ virtual std::string create_initial_filename(unsigned int turn_number) const = 0;
virtual void create_filename() {}
/** Display the save game dialog. */ /** Display the save game dialog. */
virtual int show_save_dialog(const std::string& message, DIALOG_TYPE dialog_type); virtual int show_save_dialog(const std::string& message, DIALOG_TYPE dialog_type);
/** Ask the user if an existing file should be overwritten. */ /** Ask the user if an existing file should be overwritten. */
@ -218,8 +226,6 @@ private:
friend class save_info; friend class save_info;
//before_save (write replay data) changes this so it cannot be const //before_save (write replay data) changes this so it cannot be const
saved_game& gamestate_; saved_game& gamestate_;
/** Filename of the savegame file on disk */
std::string filename_;
const std::string title_; /** Title of the savegame dialog */ const std::string title_; /** Title of the savegame dialog */
@ -239,7 +245,7 @@ public:
private: private:
/** Create a filename for automatic saves */ /** 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; void write_game(config_writer &out) override;
@ -253,7 +259,7 @@ public:
private: private:
/** Create a filename for automatic saves */ /** 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; void write_game(config_writer &out) override;
}; };
@ -267,7 +273,7 @@ public:
void autosave(const bool disable_autosave, const int autosave_max, const int infinite_autosaves); void autosave(const bool disable_autosave, const int autosave_max, const int infinite_autosaves);
private: private:
/** Create a filename for automatic saves */ /** 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 class oos_savegame : public ingame_savegame
@ -288,6 +294,9 @@ public:
scenariostart_savegame(saved_game& gamestate, const compression::format compress_saves); scenariostart_savegame(saved_game& gamestate, const compression::format compress_saves);
private: 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; void write_game(config_writer &out) override;
}; };