Skip to content

Commit

Permalink
Create savegame filenames with the same code as in actual saving
Browse files Browse the repository at this point in the history
Follow-up to commit 057a530.

Otherwise, Menu -> Back to... would be broken whenever the logic differs
between that feature and actual saving.
  • Loading branch information
jyrkive committed Feb 7, 2018
1 parent 39c3f21 commit 5ec8d63
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
8 changes: 6 additions & 2 deletions src/hotkey/hotkey_handler.cpp
Expand Up @@ -394,10 +394,14 @@ void play_controller::hotkey_handler::expand_autosaves(std::vector<config>& item
std::vector<config> newitems;
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--) {
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));
Expand Down
44 changes: 22 additions & 22 deletions src/savegame.cpp
Expand Up @@ -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;

Expand All @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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){
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}

Expand All @@ -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) {
Expand Down
33 changes: 21 additions & 12 deletions src/savegame.hpp
Expand Up @@ -18,12 +18,12 @@
#include "config.hpp"
#include "filesystem.hpp"
#include "lua_jailbreak_exception.hpp"
#include "saved_game.hpp"
#include "serialization/compression.hpp"

#include <exception>

class config_writer;
class saved_game;
class version_info;

namespace savegame {
Expand Down Expand Up @@ -170,16 +170,22 @@ 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.
The return value denotes, if the save was successful or not.
*/
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);

Expand All @@ -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. */
Expand All @@ -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 */

Expand All @@ -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;
Expand All @@ -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;
};
Expand All @@ -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
Expand All @@ -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;
};

Expand Down

0 comments on commit 5ec8d63

Please sign in to comment.