Notify when save game failed

This commit is contained in:
uid69206 2004-02-04 01:29:36 +00:00
parent 57d5d8ebe4
commit c9fd32d3fd
7 changed files with 90 additions and 23 deletions

View file

@ -214,6 +214,8 @@ save_game_message="Do you want to save your game?"
save_game_label="Name:"
save_confirm_message="The game has been saved"
save_confirm_overwrite="Save already exists. Do you want to overwrite it ?"
save_game_failed="The game could not be saved"
auto_save_game_failed="Could not auto save the game. Please save the game manually."
scenario="Scenario"
turn="Turn"
tutorial_button="Tutorial"

View file

@ -90,6 +90,10 @@ void read_file_internal(const std::string& fname, std::string& res)
} //end anon namespace
const char* io_exception::what() const throw() {
return message.c_str();
}
std::string read_file(const std::string& fname)
{
//if we have a path to the data,
@ -107,11 +111,13 @@ std::string read_file(const std::string& fname)
return res;
}
//throws io_exception if an error occurs
void write_file(const std::string& fname, const std::string& data)
{
std::ofstream file(fname.c_str());
if(file.bad()) {
if(!file.good()) {
std::cerr << "error writing to file: '" << fname << "'\n";
throw io_exception("Error writing to file: " + fname);
}
for(std::string::const_iterator i = data.begin(); i != data.end(); ++i) {
file << *i;

View file

@ -47,8 +47,20 @@ struct line_source
bool operator<(const line_source& a, const line_source& b);
//an exception object used when an IO error occurs
struct io_exception : public std::exception {
io_exception() {}
io_exception(const std::string& msg) : message(msg) {}
virtual ~io_exception() throw() {}
virtual const char* what() const throw();
private:
std::string message;
};
//basic disk I/O
std::string read_file(const std::string& fname);
//throws io_exception if an error occurs
void write_file(const std::string& fname, const std::string& data);
struct preproc_define {

View file

@ -102,14 +102,26 @@ LEVEL_RESULT play_game(display& disp, game_state& state, config& game_config,
std::string label = state.label + " replay";
const int should_save = dialogs::get_save_name(disp,
string_table["save_replay_message"],
string_table["save_game_label"],
&label);
if(should_save == 0) {
config starting_pos;
recorder.save_game(units_data,label,starting_pos);
}
bool retry;
do {
retry=false;
const int should_save = dialogs::get_save_name(disp,
string_table["save_replay_message"],
string_table["save_game_label"],
&label);
if(should_save == 0) {
try {
config starting_pos;
recorder.save_game(units_data,label,starting_pos);
} catch(gamestatus::save_game_failed& e) {
gui::show_dialog(disp,NULL,"",string_table["save_game_failed"],gui::MESSAGE);
retry=true;
};
}
} while(retry);
state.scenario = orig_scenario;
}
@ -144,15 +156,26 @@ LEVEL_RESULT play_game(display& disp, game_state& state, config& game_config,
//if this isn't the last scenario, then save the game
if(scenario != NULL) {
state.label = translate_string_default((*scenario)["id"],(*scenario)["name"]);
const int should_save = dialogs::get_save_name(disp,
string_table["save_game_message"],
string_table["save_game_label"],
&state.label);
if(should_save == 0) {
save_game(state);
}
bool retry;
do {
retry=false;
const int should_save = dialogs::get_save_name(disp,
string_table["save_game_message"],
string_table["save_game_label"],
&state.label);
if(should_save == 0) {
try {
save_game(state);
} catch(gamestatus::save_game_failed& e) {
gui::show_dialog(disp,NULL,"",string_table["save_game_failed"],gui::MESSAGE);
retry=true;
};
}
} while(retry);
}
}

View file

@ -266,6 +266,7 @@ void load_game(game_data& data, const std::string& name, game_state& state)
state = read_game(data,&cfg);
}
//throws gamestatus::save_game_failed
void save_game(const game_state& state)
{
log_scope("save_game");
@ -273,6 +274,10 @@ void save_game(const game_state& state)
std::replace(name.begin(),name.end(),' ','_');
config cfg;
write_game(state,cfg);
write_file(get_saves_dir() + "/" + name,cfg.write());
try {
write_game(state,cfg);
write_file(get_saves_dir() + "/" + name,cfg.write());
} catch(io_exception& e) {
throw gamestatus::save_game_failed(e.what());
};
}

View file

@ -65,6 +65,14 @@ public:
std::string message;
};
//an exception object used when saving a game fails.
struct save_game_failed {
save_game_failed() {}
save_game_failed(const std::string& msg) : message(msg) {}
std::string message;
};
//an exception object used for any general game error.
//e.g. data files are corrupt.
struct game_error {
@ -123,6 +131,7 @@ bool save_game_exists(const std::string & name);
//functions to load/save games.
void load_game(game_data& data, const std::string& name, game_state& state);
//throws gamestatus::save_game_failed
void save_game(const game_state& state);

View file

@ -889,7 +889,12 @@ void turn_info::end_turn()
//auto-save
config start_pos;
write_game_snapshot(start_pos);
recorder.save_game(gameinfo_,string_table["auto_save"],start_pos);
try {
recorder.save_game(gameinfo_,string_table["auto_save"],start_pos);
} catch(gamestatus::save_game_failed& e) {
gui::show_dialog(gui_,NULL,"",string_table["auto_save_game_failed"],gui::MESSAGE);
//do not bother retrying, since the user can just save the game
};
recorder.end_turn();
}
@ -1160,8 +1165,13 @@ void turn_info::save_game(const std::string& message)
if(res == 0) {
config start;
write_game_snapshot(start);
recorder.save_game(gameinfo_,label,start);
gui::show_dialog(gui_,NULL,"",string_table["save_confirm_message"], gui::OK_ONLY);
try {
recorder.save_game(gameinfo_,label,start);
gui::show_dialog(gui_,NULL,"",string_table["save_confirm_message"], gui::OK_ONLY);
} catch(gamestatus::save_game_failed& e) {
gui::show_dialog(gui_,NULL,"",string_table["save_game_failed"],gui::MESSAGE);
//do not bother retrying, since the user can just try to save the game again
};
}
}