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

Move write_game to savegame.cpp.
This commit is contained in:
Jörg Hinrichs 2009-04-08 16:44:34 +00:00
parent 50bc508fa3
commit f0f56d4ac8
4 changed files with 56 additions and 58 deletions

View file

@ -657,58 +657,6 @@ void game_state::write_snapshot(config& cfg) const
}
}
void write_game(config_writer &out, const config& snapshot, const game_state& gamestate, WRITE_GAME_MODE mode)
{
log_scope("write_game");
out.write_key_val("label", gamestate.label);
out.write_key_val("history", gamestate.history);
out.write_key_val("abbrev", gamestate.abbrev);
out.write_key_val("version", game_config::version);
out.write_key_val("scenario", gamestate.scenario);
out.write_key_val("next_scenario", gamestate.next_scenario);
out.write_key_val("completion", gamestate.completion);
out.write_key_val("campaign", gamestate.campaign);
out.write_key_val("campaign_type", gamestate.campaign_type);
out.write_key_val("difficulty", gamestate.difficulty);
out.write_key_val("campaign_define", gamestate.campaign_define);
out.write_key_val("campaign_extra_defines", utils::join(gamestate.campaign_xtra_defines));
out.write_key_val("random_seed", lexical_cast<std::string>(gamestate.rng().get_random_seed()));
out.write_key_val("random_calls", lexical_cast<std::string>(gamestate.rng().get_random_calls()));
out.write_key_val("next_underlying_unit_id", lexical_cast<std::string>(n_unit::id_manager::instance().get_save_id()));
out.write_key_val("end_text", gamestate.end_text);
out.write_key_val("end_text_duration", str_cast<unsigned int>(gamestate.end_text_duration));
out.write_child("variables", gamestate.get_variables());
for(std::map<std::string, wml_menu_item *>::const_iterator j=gamestate.wml_menu_items.begin();
j!=gamestate.wml_menu_items.end(); ++j) {
out.open_child("menu_item");
out.write_key_val("id", j->first);
out.write_key_val("image", j->second->image);
out.write_key_val("description", j->second->description);
out.write_key_val("needs_select", (j->second->needs_select) ? "yes" : "no");
if(!j->second->show_if.empty())
out.write_child("show_if", j->second->show_if);
if(!j->second->filter_location.empty())
out.write_child("filter_location", j->second->filter_location);
if(!j->second->command.empty())
out.write_child("command", j->second->command);
out.close_child("menu_item");
}
if(mode == WRITE_FULL_GAME) {
if (!gamestate.replay_data.child("replay")) {
out.write_child("replay", gamestate.replay_data);
}
out.write_child("snapshot",snapshot);
out.write_child("replay_start",gamestate.starting_pos);
out.open_child("statistics");
statistics::write_stats(out);
out.close_child("statistics");
}
}
/**
* A structure for comparing to save_info objects based on their modified time.
* If the times are equal, will order based on the name.

View file

@ -315,12 +315,9 @@ struct save_info {
/** Get a list of available saves. */
std::vector<save_info> get_saves_list(const std::string* dir = NULL, const std::string* filter = NULL);
enum WRITE_GAME_MODE { WRITE_SNAPSHOT_ONLY, WRITE_FULL_GAME };
void read_save_file(const std::string& name, config& cfg, std::string* error_log);
void write_players(game_state& gamestate, config& cfg);
void write_game(config_writer &out, const config& snapshot, const game_state& gamestate, WRITE_GAME_MODE mode=WRITE_FULL_GAME);
/** Returns true iff there is already a savegame with that name. */
bool save_game_exists(const std::string & name);

View file

@ -27,6 +27,7 @@
#include "replay.hpp"
#include "serialization/binary_or_text.hpp"
#include "sound.hpp"
#include "statistics.hpp"
#define LOG_SAVE LOG_STREAM(info, engine)
@ -101,7 +102,7 @@ void savegame::save_game_internal(const std::string& filename)
std::stringstream ss;
{
config_writer out(ss, preferences::compress_saves());
::write_game(out, snapshot_, gamestate_);
write_game(out);
finish_save_game(out);
}
scoped_ostream os(open_save_game(filename_));
@ -112,7 +113,57 @@ void savegame::save_game_internal(const std::string& filename)
}
}
void savegame::finish_save_game(config_writer &out)
void savegame::write_game(config_writer &out) const
{
log_scope("write_game");
out.write_key_val("label", gamestate_.label);
out.write_key_val("history", gamestate_.history);
out.write_key_val("abbrev", gamestate_.abbrev);
out.write_key_val("version", game_config::version);
out.write_key_val("scenario", gamestate_.scenario);
out.write_key_val("next_scenario", gamestate_.next_scenario);
out.write_key_val("completion", gamestate_.completion);
out.write_key_val("campaign", gamestate_.campaign);
out.write_key_val("campaign_type", gamestate_.campaign_type);
out.write_key_val("difficulty", gamestate_.difficulty);
out.write_key_val("campaign_define", gamestate_.campaign_define);
out.write_key_val("campaign_extra_defines", utils::join(gamestate_.campaign_xtra_defines));
out.write_key_val("random_seed", lexical_cast<std::string>(gamestate_.rng().get_random_seed()));
out.write_key_val("random_calls", lexical_cast<std::string>(gamestate_.rng().get_random_calls()));
out.write_key_val("next_underlying_unit_id", lexical_cast<std::string>(n_unit::id_manager::instance().get_save_id()));
out.write_key_val("end_text", gamestate_.end_text);
out.write_key_val("end_text_duration", str_cast<unsigned int>(gamestate_.end_text_duration));
out.write_child("variables", gamestate_.get_variables());
for(std::map<std::string, wml_menu_item *>::const_iterator j=gamestate_.wml_menu_items.begin();
j!=gamestate_.wml_menu_items.end(); ++j) {
out.open_child("menu_item");
out.write_key_val("id", j->first);
out.write_key_val("image", j->second->image);
out.write_key_val("description", j->second->description);
out.write_key_val("needs_select", (j->second->needs_select) ? "yes" : "no");
if(!j->second->show_if.empty())
out.write_child("show_if", j->second->show_if);
if(!j->second->filter_location.empty())
out.write_child("filter_location", j->second->filter_location);
if(!j->second->command.empty())
out.write_child("command", j->second->command);
out.close_child("menu_item");
}
if (!gamestate_.replay_data.child("replay")) {
out.write_child("replay", gamestate_.replay_data);
}
out.write_child("snapshot",snapshot_);
out.write_child("replay_start",gamestate_.starting_pos);
out.open_child("statistics");
statistics::write_stats(out);
out.close_child("statistics");
}
void savegame::finish_save_game(const config_writer &out)
{
std::string name = gamestate_.label;
std::replace(name.begin(),name.end(),' ','_');

View file

@ -19,6 +19,7 @@
#include "global.hpp"
#include "show_dialog.hpp"
#include "gamestatus.hpp"
#include "unit_id.hpp"
#include <string>
@ -73,7 +74,8 @@ private:
data manipulation has to happen before calling this method */
void save_game_internal(const std::string& filename);
void finish_save_game(config_writer &out);
void write_game(config_writer &out) const;
void finish_save_game(const config_writer &out);
void extract_summary_data_from_save(config& out);
game_state& gamestate_;