mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-03 00:17:44 +00:00
Savegame reorganization Step 1: a simpler interface to saving and loading.
Move gamestatus.cpp::load_game_summary to savegame.cpp and get rid of gamestatus.cpp::read_save_file.
This commit is contained in:
parent
8d4b01ed68
commit
9fb9d5e935
@ -34,6 +34,7 @@
|
||||
#include "mouse_handler_base.hpp"
|
||||
#include "minimap.hpp"
|
||||
#include "replay.hpp"
|
||||
#include "savegame.hpp"
|
||||
#include "thread.hpp"
|
||||
#include "wml_separators.hpp"
|
||||
#include "widgets/progressbar.hpp"
|
||||
@ -420,7 +421,7 @@ void save_preview_pane::draw_contents()
|
||||
config& summary = *(*summaries_)[index_];
|
||||
if (summary["label"] == ""){
|
||||
try {
|
||||
load_game_summary((*info_)[index_].name, summary, &dummy);
|
||||
save_summary::load_summary((*info_)[index_].name, summary, &dummy);
|
||||
*(*summaries_)[index_] = summary;
|
||||
} catch(game::load_game_failed&) {
|
||||
summary["corrupt"] = "yes";
|
||||
|
@ -48,8 +48,6 @@ static void write_player(const player_info& player, config& cfg);
|
||||
|
||||
#endif /* _WIN32 */
|
||||
|
||||
static void extract_summary_from_config(config& cfg_save, config& cfg_summary);
|
||||
|
||||
player_info* game_state::get_player(const std::string& id) {
|
||||
std::map< std::string, player_info >::iterator found = players.find(id);
|
||||
if (found == players.end()) {
|
||||
@ -661,44 +659,6 @@ void delete_game(const std::string& name)
|
||||
remove((get_saves_dir() + "/" + modified_name).c_str());
|
||||
}
|
||||
|
||||
void read_save_file(const std::string& name, config& cfg, std::string* error_log)
|
||||
{
|
||||
std::string modified_name = name;
|
||||
replace_space2underbar(modified_name);
|
||||
|
||||
// Try reading the file both with and without underscores
|
||||
scoped_istream file_stream = istream_file(get_saves_dir() + "/" + modified_name);
|
||||
if (file_stream->fail())
|
||||
file_stream = istream_file(get_saves_dir() + "/" + name);
|
||||
|
||||
cfg.clear();
|
||||
try{
|
||||
if(is_gzip_file(name)) {
|
||||
read_gz(cfg, *file_stream, error_log);
|
||||
} else {
|
||||
detect_format_and_read(cfg, *file_stream, error_log);
|
||||
}
|
||||
} catch (config::error &err)
|
||||
{
|
||||
ERR_NG << err.message;
|
||||
throw game::load_game_failed();
|
||||
}
|
||||
|
||||
if(cfg.empty()) {
|
||||
ERR_NG << "Could not parse file data into config\n";
|
||||
throw game::load_game_failed();
|
||||
}
|
||||
}
|
||||
|
||||
void load_game_summary(const std::string& name, config& cfg_summary, std::string* error_log){
|
||||
log_scope("load_game_summary");
|
||||
|
||||
config cfg;
|
||||
read_save_file(name,cfg,error_log);
|
||||
|
||||
extract_summary_from_config(cfg, cfg_summary);
|
||||
}
|
||||
|
||||
// Throws game::save_game_failed
|
||||
scoped_ostream open_save_game(const std::string &label)
|
||||
{
|
||||
|
@ -315,8 +315,6 @@ 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);
|
||||
|
||||
void read_save_file(const std::string& name, config& cfg, std::string* error_log);
|
||||
|
||||
void write_players(game_state& gamestate, config& cfg);
|
||||
|
||||
/** Returns true iff there is already a savegame with that name. */
|
||||
@ -325,10 +323,6 @@ bool save_game_exists(const std::string & name);
|
||||
/** Throws game::save_game_failed. */
|
||||
scoped_ostream open_save_game(const std::string &label);
|
||||
|
||||
/** Load/Save games. */
|
||||
void load_game(const std::string& name, game_state& gamestate, std::string* error_log);
|
||||
void load_game_summary(const std::string& name, config& cfg_summary, std::string* error_log);
|
||||
|
||||
/** Delete a savegame. */
|
||||
void delete_game(const std::string& name);
|
||||
|
||||
@ -337,5 +331,6 @@ config& save_summary(std::string save);
|
||||
void write_save_index();
|
||||
|
||||
void replace_underbar2space(std::string &name);
|
||||
void extract_summary_from_config(config& cfg_save, config& cfg_summary);
|
||||
|
||||
#endif
|
||||
|
@ -106,6 +106,44 @@
|
||||
}
|
||||
#endif /* _WIN32 */
|
||||
|
||||
static void read_save_file(const std::string& name, config& cfg, std::string* error_log)
|
||||
{
|
||||
std::string modified_name = name;
|
||||
replace_space2underbar(modified_name);
|
||||
|
||||
// Try reading the file both with and without underscores
|
||||
scoped_istream file_stream = istream_file(get_saves_dir() + "/" + modified_name);
|
||||
if (file_stream->fail())
|
||||
file_stream = istream_file(get_saves_dir() + "/" + name);
|
||||
|
||||
cfg.clear();
|
||||
try{
|
||||
if(is_gzip_file(name)) {
|
||||
read_gz(cfg, *file_stream, error_log);
|
||||
} else {
|
||||
detect_format_and_read(cfg, *file_stream, error_log);
|
||||
}
|
||||
} catch (config::error &err)
|
||||
{
|
||||
LOG_SAVE << err.message;
|
||||
throw game::load_game_failed();
|
||||
}
|
||||
|
||||
if(cfg.empty()) {
|
||||
LOG_SAVE << "Could not parse file data into config\n";
|
||||
throw game::load_game_failed();
|
||||
}
|
||||
}
|
||||
|
||||
void save_summary::load_summary(const std::string& name, config& cfg_summary, std::string* error_log){
|
||||
log_scope("load_game_summary");
|
||||
|
||||
config cfg;
|
||||
read_save_file(name,cfg,error_log);
|
||||
|
||||
::extract_summary_from_config(cfg, cfg_summary);
|
||||
}
|
||||
|
||||
loadgame::loadgame(display& gui, const config& game_config, game_state& gamestate)
|
||||
: game_config_(game_config)
|
||||
, gui_(gui)
|
||||
@ -181,35 +219,6 @@ void loadgame::load_game(std::string& filename, bool show_replay, bool cancel_or
|
||||
|
||||
}
|
||||
|
||||
void loadgame::read_save_file(const std::string& name, config& cfg, std::string* error_log)
|
||||
{
|
||||
std::string modified_name = name;
|
||||
replace_space2underbar(modified_name);
|
||||
|
||||
// Try reading the file both with and without underscores
|
||||
scoped_istream file_stream = istream_file(get_saves_dir() + "/" + modified_name);
|
||||
if (file_stream->fail())
|
||||
file_stream = istream_file(get_saves_dir() + "/" + name);
|
||||
|
||||
cfg.clear();
|
||||
try{
|
||||
if(is_gzip_file(name)) {
|
||||
read_gz(cfg, *file_stream, error_log);
|
||||
} else {
|
||||
detect_format_and_read(cfg, *file_stream, error_log);
|
||||
}
|
||||
} catch (config::error &err)
|
||||
{
|
||||
LOG_SAVE << err.message;
|
||||
throw game::load_game_failed();
|
||||
}
|
||||
|
||||
if(cfg.empty()) {
|
||||
LOG_SAVE << "Could not parse file data into config\n";
|
||||
throw game::load_game_failed();
|
||||
}
|
||||
}
|
||||
|
||||
void loadgame::check_version_compatibility()
|
||||
{
|
||||
// do not load if too old, if either the savegame or the current game
|
||||
@ -306,7 +315,7 @@ void savegame::save_game_interactive(display& gui, const std::string& message,
|
||||
gui::DIALOG_TYPE dialog_type, const bool has_exit_button,
|
||||
const bool ask_for_filename)
|
||||
{
|
||||
interactive_ = true;
|
||||
interactive_ = ask_for_filename;
|
||||
create_filename();
|
||||
const int res = dialogs::get_save_name(gui, message, _("Name: "), &filename_, dialog_type, title_, has_exit_button, ask_for_filename);
|
||||
|
||||
|
@ -26,6 +26,15 @@ struct load_game_cancelled_exception
|
||||
{
|
||||
};
|
||||
|
||||
class save_summary
|
||||
{
|
||||
public:
|
||||
save_summary() {}
|
||||
virtual ~save_summary() {}
|
||||
|
||||
static void load_summary(const std::string& name, config& cfg_summary, std::string* error_log);
|
||||
};
|
||||
|
||||
class loadgame
|
||||
{
|
||||
public:
|
||||
@ -43,7 +52,6 @@ public:
|
||||
|
||||
private:
|
||||
void show_dialog(bool show_replay, bool cancel_orders);
|
||||
void read_save_file(const std::string& name, config& cfg, std::string* error_log);
|
||||
void check_version_compatibility();
|
||||
void copy_era(config& cfg);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user