Skip to content

Commit

Permalink
make most save methods return false if they failed to save
Browse files Browse the repository at this point in the history
moved save_maps, save_artifacts, save_factions_missions_npcs to game::save

don't quit after failed saving
  • Loading branch information
BevapDin committed Feb 12, 2014
1 parent dcb60bb commit d865bb3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
16 changes: 10 additions & 6 deletions src/auto_pickup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ std::string auto_pickup_header(bool bCharacter)
\n\n";
}

void save_auto_pickup(bool bCharacter)
bool save_auto_pickup(bool bCharacter)
{
std::ofstream fout;
std::string sFile = "data/auto_pickup.txt";
Expand All @@ -633,17 +633,15 @@ void save_auto_pickup(bool bCharacter)

fin.open((world_generator->active_world->world_path + "/" + base64_encode(g->u.name) + ".sav").c_str());
if(!fin.is_open()) {
return;
return true;
}
fin.close();
}

fout.exceptions(std::ios::badbit | std::ios::failbit);
try {
fout.open(sFile.c_str());

if(!fout.is_open()) {
return;
}

fout << auto_pickup_header(bCharacter) << std::endl;
for (unsigned i = 0; i < vAutoPickupRules[(bCharacter) ? 2 : 1].size(); i++) {
fout << vAutoPickupRules[(bCharacter) ? 2 : 1][i].sRule << ";";
Expand All @@ -656,6 +654,12 @@ void save_auto_pickup(bool bCharacter)
merge_vector();
createPickupRules();
}
fout.close();
return true;
} catch(std::ios::failure &) {
popup(_("Failed to write autopickup rules to %s"), sFile.c_str());
return false;
}
}

void create_default_auto_pickup(bool bCharacter)
Expand Down
2 changes: 1 addition & 1 deletion src/auto_pickup.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void createPickupRules(const std::string sItemNameIn = "");
void save_reset_changes(bool bReset);
void show_auto_pickup();
void load_auto_pickup(bool bCharacter);
void save_auto_pickup(bool bCharacter);
bool save_auto_pickup(bool bCharacter);
void show_auto_pickup();
std::string auto_pickup_header(bool bCharacter);
void create_default_auto_pickup(bool bCharacter);
Expand Down
54 changes: 36 additions & 18 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ void game::create_starting_npcs()

void game::cleanup_at_end(){
write_msg();
if (uquit == QUIT_DIED || uquit == QUIT_SUICIDE || uquit == QUIT_SAVED) {
if (uquit == QUIT_DIED || uquit == QUIT_SUICIDE) {
// Save the factions's, missions and set the NPC's overmap coords
// Npcs are saved in the overmap.
save_factions_missions_npcs(); //missions need to be saved as they are global for all saves.
Expand Down Expand Up @@ -2867,10 +2867,11 @@ bool game::handle_action()

case ACTION_SAVE:
if (query_yn(_("Save and quit?"))) {
save();
u.moves = 0;
uquit = QUIT_SAVED;
MAPBUFFER.make_volatile();
if(save()) {
u.moves = 0;
uquit = QUIT_SAVED;
MAPBUFFER.make_volatile();
}
}
break;

Expand Down Expand Up @@ -3340,7 +3341,7 @@ void game::load_world_modfiles(WORLDPTR world)
}

//Saves all factions and missions and npcs.
void game::save_factions_missions_npcs ()
bool game::save_factions_missions_npcs ()
{
std::string masterfile = world_generator->active_world->world_path + "/master.gsav";
try {
Expand All @@ -3350,12 +3351,14 @@ void game::save_factions_missions_npcs ()
fout.open(masterfile.c_str());
serialize_master(fout);
fout.close();
return true;
} catch(std::ios::failure &) {
popup(_("Failed to save factions to %s"), masterfile.c_str());
return false;
}
}

void game::save_artifacts()
bool game::save_artifacts()
{
std::string artfilename = world_generator->active_world->world_path + "/artifacts.gsav";
try {
Expand All @@ -3377,36 +3380,42 @@ void game::save_artifacts()
}
json.end_array();
fout.close();
return true;
} catch(std::ios::failure &) {
popup(_("Failed to save artifacts to %s"), artfilename.c_str());
return false;
}
}

void game::save_maps()
bool game::save_maps()
{
try {
m.save(cur_om, turn, levx, levy, levz);
overmap_buffer.save(); // can throw std::ios::failure
MAPBUFFER.save(); // can throw std::ios::failure
return true;
} catch(std::ios::failure &) {
popup(_("Failed to maps"));
return false;
}
}

void game::save_uistate() {
bool game::save_uistate() {
std::string savefile = world_generator->active_world->world_path + "/uistate.json";
try {
std::ofstream fout;
fout.exceptions(std::ios::badbit | std::ios::failbit);
fout.open(savefile.c_str());
fout << uistate.serialize();
fout.close();
return true;
} catch(std::ios::failure &) {
popup(_("Failed to save uistate to %s"), savefile.c_str());
return false;
}
}

void game::save()
bool game::save()
{
std::string playerfile = world_generator->active_world->world_path + "/" + base64_encode(u.name);
try {
Expand All @@ -3424,11 +3433,25 @@ void game::save()
fout.open( std::string(playerfile + ".log").c_str() );
fout << u.dump_memorial();
fout.close();
//factions, missions, and npcs, maps and artifact data is saved in cleanup_at_end()
save_auto_pickup(true); // Save character auto pickup rules
save_uistate();
if (!save_factions_missions_npcs()) {
return false;
}
if (!save_artifacts()) {
return false;
}
if (!save_maps()) {
return false;
}
if (!save_auto_pickup(true)) { // Save character auto pickup rules
return false;
}
if (!save_uistate()) {
return false;
}
return true;
} catch(std::ios::failure &err) {
popup(_("Failed to save game data"));
return false;
}
}

Expand Down Expand Up @@ -12482,7 +12505,6 @@ void game::update_map(int &x, int &y) {
olevy = 1;
}
if (olevx != 0 || olevy != 0) {
cur_om->save();
cur_om = &overmap_buffer.get(cur_om->pos().x + olevx, cur_om->pos().y + olevy);
}

Expand Down Expand Up @@ -13440,10 +13462,6 @@ void game::quicksave(){

//perform save
save();
save_factions_missions_npcs();
save_artifacts();
save_maps();
save_uistate();
//Now reset counters for autosaving, so we don't immediately autosave after a quicksave or autosave.
moves_since_last_save = 0;
item_exchanges_since_save = 0;
Expand Down
15 changes: 10 additions & 5 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ class game
void unserialize_master(std::ifstream & fin); // for load
bool unserialize_master_legacy(std::ifstream & fin); // for old load

void save();
// returns false if saving faild for whatever reason
bool save();
void delete_world(std::string worldname, bool delete_folder);
std::vector<std::string> list_active_characters();
void write_memorial_file();
Expand Down Expand Up @@ -461,14 +462,18 @@ class game
void start_special_game(special_game_id gametype); // See gamemode.cpp

//private save functions.
void save_factions_missions_npcs();
// returns false if saving failed for whatever reason
bool save_factions_missions_npcs();
void serialize_master(std::ofstream &fout);
void save_artifacts();
void save_maps();
// returns false if saving failed for whatever reason
bool save_artifacts();
// returns false if saving failed for whatever reason
bool save_maps();
void save_weather(std::ofstream &fout);
void load_legacy_future_weather(std::string data);
void load_legacy_future_weather(std::istream &fin);
void save_uistate();
// returns false if saving failed for whatever reason
bool save_uistate();
void load_uistate(std::string worldname);
// Data Initialization
void init_npctalk();
Expand Down

0 comments on commit d865bb3

Please sign in to comment.