Savegame reorganization Step 1: a simpler interface to saving and loading.

Further refactoring savegame.cpp and slightly simplifying the interface.
This commit is contained in:
Jörg Hinrichs 2009-04-30 18:04:23 +00:00
parent 84fa740084
commit 4809396dc2
5 changed files with 71 additions and 44 deletions

View File

@ -2764,11 +2764,11 @@ private:
} }
void console_handler::do_save() { void console_handler::do_save() {
savegame save(menu_handler_.gamestate_, preferences::compress_saves()); savegame save(menu_handler_.gamestate_, preferences::compress_saves());
save.save_game(get_data()); save.save_game(&menu_handler_.gui_->video(), get_data());
} }
void console_handler::do_save_quit() { void console_handler::do_save_quit() {
savegame save(menu_handler_.gamestate_, preferences::compress_saves()); savegame save(menu_handler_.gamestate_, preferences::compress_saves());
save.save_game(get_data()); save.save_game(&menu_handler_.gui_->video(), get_data());
throw end_level_exception(QUIT); throw end_level_exception(QUIT);
} }
void console_handler::do_quit() { void console_handler::do_quit() {

View File

@ -395,7 +395,7 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
if (preferences::save_replays()) { if (preferences::save_replays()) {
replay_savegame save(gamestate, preferences::compress_saves()); replay_savegame save(gamestate, preferences::compress_saves());
save.save_game_interactive(disp.video(), "", gui::OK_CANCEL, false, false); save.save_game_interactive(disp.video(), "", gui::OK_CANCEL, false);
} }
} }

View File

@ -538,8 +538,8 @@ void playmp_controller::process_oos(const std::string& err_msg){
temp_buf << " \n"; temp_buf << " \n";
} }
game_savegame save(gamestate_, level_, *gui_, teams_, units_, status_, map_, preferences::compress_saves()); oos_savegame save(gamestate_, level_, *gui_, teams_, units_, status_, map_, preferences::compress_saves());
save.save_game_interactive((*gui_).video(), temp_buf.str(), gui::YES_NO, true); save.save_game_interactive((*gui_).video(), temp_buf.str(), gui::YES_NO);
} }
void playmp_controller::handle_generic_event(const std::string& name){ void playmp_controller::handle_generic_event(const std::string& name){

View File

@ -476,8 +476,7 @@ savegame::savegame(game_state& gamestate, const bool compress_saves, const std::
{} {}
bool savegame::save_game_interactive(CVideo& video, const std::string& message, bool savegame::save_game_interactive(CVideo& video, const std::string& message,
gui::DIALOG_TYPE dialog_type, const bool has_exit_button, gui::DIALOG_TYPE dialog_type, bool ask_for_filename)
bool ask_for_filename)
{ {
show_confirmation_ = ask_for_filename; show_confirmation_ = ask_for_filename;
create_filename(); create_filename();
@ -488,7 +487,7 @@ bool savegame::save_game_interactive(CVideo& video, const std::string& message,
do{ do{
try{ try{
if (ask_for_filename){ if (ask_for_filename){
res = show_save_dialog(video, has_exit_button, message, dialog_type); res = show_save_dialog(video, message, dialog_type);
exit = true; exit = true;
} }
@ -516,33 +515,23 @@ bool savegame::save_game_interactive(CVideo& video, const std::string& message,
return save_game(&video); return save_game(&video);
} }
int savegame::show_save_dialog(CVideo& video, bool is_oos, const std::string& message, const gui::DIALOG_TYPE dialog_type) int savegame::show_save_dialog(CVideo& video, const std::string& message, const gui::DIALOG_TYPE dialog_type)
{ {
static bool ignore_all = false;
int res = 0; int res = 0;
std::string filename = filename_; std::string filename = filename_;
if (is_oos && (!ignore_all)){ if (dialog_type == gui::OK_CANCEL){
gui2::tgame_save_oos dlg(title_, filename, message); gui2::tgame_save dlg(title_, filename);
dlg.show(video); dlg.show(video);
filename = dlg.filename(); filename = dlg.filename();
ignore_all = dlg.ignore_all();
res = dlg.get_retval(); res = dlg.get_retval();
} }
else{ else if (dialog_type == gui::YES_NO){
if (dialog_type == gui::OK_CANCEL){ gui2::tgame_save_message dlg(title_, filename, message);
gui2::tgame_save dlg(title_, filename); dlg.show(video);
dlg.show(video); filename = dlg.filename();
filename = dlg.filename(); res = dlg.get_retval();
res = dlg.get_retval();
}
else if (dialog_type == gui::YES_NO){
gui2::tgame_save_message dlg(title_, filename, message);
dlg.show(video);
filename = dlg.filename();
res = dlg.get_retval();
}
} }
check_filename(filename, video); check_filename(filename, video);
@ -593,18 +582,15 @@ void savegame::before_save()
gamestate_.replay_data = recorder.get_replay_data(); gamestate_.replay_data = recorder.get_replay_data();
} }
bool savegame::save_game(const std::string& filename) bool savegame::save_game(CVideo* video, const std::string& filename)
{
filename_ = filename;
return save_game();
}
bool savegame::save_game(CVideo* video)
{ {
try { try {
Uint32 start, end; Uint32 start, end;
start = SDL_GetTicks(); start = SDL_GetTicks();
if (filename_ == "")
filename_ = filename;
before_save(); before_save();
save_game_internal(filename_); save_game_internal(filename_);
@ -870,6 +856,34 @@ void autosave_savegame::create_filename()
set_filename(filename); set_filename(filename);
} }
oos_savegame::oos_savegame(game_state &gamestate, const config& level_cfg,
game_display& gui, const std::vector<team>& teams,
const unit_map& units, const gamestatus& gamestatus,
const gamemap& map, const bool compress_saves)
: game_savegame(gamestate, level_cfg, gui, teams, units, gamestatus, map, compress_saves)
{}
int oos_savegame::show_save_dialog(CVideo& video, const std::string& message, const gui::DIALOG_TYPE dialog_type)
{
static bool ignore_all = false;
int res = 0;
std::string filename = this->filename();
if (!ignore_all){
gui2::tgame_save_oos dlg(title(), filename, message);
dlg.show(video);
filename = dlg.filename();
ignore_all = dlg.ignore_all();
res = dlg.get_retval();
}
check_filename(filename, video);
set_filename(filename);
return res;
}
game_savegame::game_savegame(game_state &gamestate, const config& level_cfg, game_savegame::game_savegame(game_state &gamestate, const config& level_cfg,
game_display& gui, const std::vector<team>& teams, game_display& gui, const std::vector<team>& teams,
const unit_map& units, const gamestatus& gamestatus, const unit_map& units, const gamestatus& gamestatus,

View File

@ -136,7 +136,7 @@ public:
/** Save a game without any further user interaction. Atm, this is only used by the /** Save a game without any further user interaction. Atm, this is only used by the
console_handler save actions. The return value denotes, if the save was successful or not. */ console_handler save actions. The return value denotes, if the save was successful or not. */
bool save_game(const std::string& filename); //bool save_game(const std::string& filename);
/** /**
Save a game without any further user interaction. This is used by autosaves and Save a game without any further user interaction. This is used by autosaves and
@ -144,24 +144,26 @@ public:
to appear, you have to provide the gui parameter. to appear, you have to provide the gui parameter.
The return value denotes, if the save was successful or not. The return value denotes, if the save was successful or not.
*/ */
bool save_game(CVideo* video = NULL); bool save_game(CVideo* video = NULL, const std::string& filename = "");
/** Save a game interactively through the savegame dialog. Used for manual midgame and replay /** Save a game interactively through the savegame dialog. Used for manual midgame and replay
saves. The return value denotes, if the save was successful or not. */ saves. The return value denotes, if the save was successful or not. */
bool save_game_interactive(CVideo& gui, const std::string& message, bool save_game_interactive(CVideo& gui, const std::string& message,
gui::DIALOG_TYPE dialog_type, const bool has_exit_button = false, gui::DIALOG_TYPE dialog_type, bool ask_for_filename = true);
bool ask_for_filename = true);
const std::string filename() const { return filename_; } const std::string& filename() const { return filename_; }
protected: protected:
/** Sets the filename and removes invalid characters. Don't set the filename directly but /** Sets the filename and removes invalid characters. Don't set the filename directly but
use this method instead. */ use this method instead. */
void set_filename(std::string filename); void set_filename(std::string filename);
/** Check, if the filename contains illegal constructs like ".gz". */
void check_filename(const std::string& filename, CVideo& video);
/** Customize the standard error message */ /** Customize the standard error message */
void set_error_message(const std::string error_message) { error_message_ = error_message; } void set_error_message(const std::string error_message) { error_message_ = error_message; }
const std::string& title() const { return title_; }
game_state& gamestate() const { return gamestate_; } game_state& gamestate() const { return gamestate_; }
config& snapshot() { return snapshot_; } config& snapshot() { return snapshot_; }
@ -175,10 +177,8 @@ private:
/** Build the filename according to the specific savegame's needs. Subclasses will have to /** Build the filename according to the specific savegame's needs. Subclasses will have to
override this to take effect. */ override this to take effect. */
virtual void create_filename() {} virtual void create_filename() {}
/** Check, if the filename contains illegal constructs like ".gz". */
void check_filename(const std::string& filename, CVideo& video);
/** Display the save game dialog. */ /** Display the save game dialog. */
int show_save_dialog(CVideo& video, bool is_oos, const std::string& message, const gui::DIALOG_TYPE dialog_type); virtual int show_save_dialog(CVideo& video, const std::string& message, const gui::DIALOG_TYPE dialog_type);
/** Ask the user if an existing file should be overwritten. */ /** Ask the user if an existing file should be overwritten. */
bool check_overwrite(CVideo& video); bool check_overwrite(CVideo& video);
@ -257,9 +257,9 @@ class autosave_savegame : public game_savegame
{ {
public: public:
autosave_savegame(game_state &gamestate, const config& level_cfg, autosave_savegame(game_state &gamestate, const config& level_cfg,
game_display& gui, const std::vector<team>& teams, game_display& gui, const std::vector<team>& teams,
const unit_map& units, const gamestatus& gamestatus, const unit_map& units, const gamestatus& gamestatus,
const gamemap& map, const bool compress_saves); const gamemap& map, const bool compress_saves);
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:
@ -267,6 +267,19 @@ private:
virtual void create_filename(); virtual void create_filename();
}; };
class oos_savegame : public game_savegame
{
public:
oos_savegame(game_state &gamestate, const config& level_cfg,
game_display& gui, const std::vector<team>& teams,
const unit_map& units, const gamestatus& gamestatus,
const gamemap& map, const bool compress_saves);
private:
/** Display the save game dialog. */
virtual int show_save_dialog(CVideo& video, const std::string& message, const gui::DIALOG_TYPE dialog_type);
};
/** Class for start-of-scenario saves */ /** Class for start-of-scenario saves */
class scenariostart_savegame : public savegame class scenariostart_savegame : public savegame
{ {