Author: jhinrichs
Date: Sun Apr 12 18:05:53 2009
New Revision: 34793

URL: http://svn.gna.org/viewcvs/wesnoth?rev=34793&view=rev
Log:
Savegame reorganization Step 1: Providing a simpler interface to saving and 
loading.
Move menu_handler::load_game and parts of game_controller::load_game to 
savegame.cpp.

Modified:
    trunk/src/game.cpp
    trunk/src/menu_events.cpp
    trunk/src/menu_events.hpp
    trunk/src/play_controller.cpp
    trunk/src/savegame.cpp
    trunk/src/savegame.hpp

Modified: trunk/src/game.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game.cpp?rev=34793&r1=34792&r2=34793&view=diff
==============================================================================
--- trunk/src/game.cpp (original)
+++ trunk/src/game.cpp Sun Apr 12 18:05:53 2009
@@ -56,6 +56,7 @@
 #include "preferences_display.hpp"
 #include "addon_management.hpp"
 #include "replay.hpp"
+#include "savegame.hpp"
 #include "sound.hpp"
 #include "statistics.hpp"
 #include "thread.hpp"
@@ -871,54 +872,21 @@
 
 bool game_controller::load_game()
 {
-       state_ = game_state();
-
-       bool show_replay = loaded_game_show_replay_;
-       bool cancel_orders = loaded_game_cancel_orders_;
-
-       const std::string game = loaded_game_.empty() ? 
dialogs::load_game_dialog(disp(),game_config_,&show_replay,&cancel_orders) : 
loaded_game_;
-
-       loaded_game_ = "";
-
-       if(game == "") {
-               return false;
-       }
+       loadgame load(disp(), game_config_, state_);
 
        try {
-               //to load a save file, we first load the file in, then we 
re-parse game
-               //data with the save's #defines, and then we finally parse the 
save file,
-               //with the game data ready to go.
-
-               config cfg;
-               std::string error_log;
-               read_save_file(game,cfg,&error_log);
-               if(!error_log.empty()) {
-            try {
-                           gui::show_error_message(disp(),
-                                           _("Warning: The file you have tried 
to load is corrupt. Loading anyway.\n") +
-                                           error_log);
-            } catch (utils::invalid_utf8_exception&) {
-                           gui::show_error_message(disp(),
-                                           _("Warning: The file you have tried 
to load is corrupt. Loading anyway.\n") +
-                        std::string("(UTF-8 ERROR)"));
-            }
-               }
+               load.load_game(loaded_game_, loaded_game_show_replay_, 
loaded_game_cancel_orders_);
 
                cache_.clear_defines();
-               game_config::scoped_preproc_define 
dificulty_def(cfg["difficulty"]);
-
-               const std::string& campaign_define = cfg["campaign_define"];
-
-               game_config::scoped_preproc_define 
campaign_define_def(campaign_define, !campaign_define.empty());
-
-               game_config::scoped_preproc_define 
campaign_type_def("MULTIPLAYER", campaign_define.empty() && 
(cfg["campaign_type"] == "multiplayer"));
-
-
-               const std::vector<std::string> campaign_xtra_defines = 
utils::split(cfg["campaign_extra_defines"]);
+               game_config::scoped_preproc_define 
dificulty_def(state_.difficulty);
+
+               game_config::scoped_preproc_define 
campaign_define_def(state_.campaign_define, !state_.campaign_define.empty());
+
+               game_config::scoped_preproc_define 
campaign_type_def("MULTIPLAYER", state_.campaign_define.empty() && 
(state_.campaign_type == "multiplayer"));
 
                typedef boost::shared_ptr<game_config::scoped_preproc_define> 
define_ptr;
                std::deque<define_ptr> extra_defines;
-               for(std::vector<std::string>::const_iterator i = 
campaign_xtra_defines.begin(); i != campaign_xtra_defines.end(); ++i) {
+               for(std::vector<std::string>::const_iterator i = 
state_.campaign_xtra_defines.begin(); i != state_.campaign_xtra_defines.end(); 
++i) {
                        define_ptr newdefine(new 
game_config::scoped_preproc_define(*i));
                        extra_defines.push_back(newdefine);
                }
@@ -931,43 +899,11 @@
                        return false;
                }
 
-               const std::string version = cfg["version"];
-               if(version != game_config::version) {
-                       const version_info parsed_savegame_version(version);
-                       if(game_config::wesnoth_version.minor_version() % 2 != 
0 ||
-                          game_config::wesnoth_version.major_version() != 
parsed_savegame_version.major_version() ||
-                          game_config::wesnoth_version.minor_version() != 
parsed_savegame_version.minor_version()) {
-                               // do not load if too old, if either the 
savegame or the current game
-                               // has the version 'test' allow loading
-                               
if(!game_config::is_compatible_savegame_version(version)) {
-                                       /* GCC-3.3 needs a temp var otherwise 
compilation fails */
-                                       gui::message_dialog dlg(disp(), "", 
_("This save is from a version too old to be loaded."));
-                                       dlg.show();
-                                       return false;
-                               }
-
-                               const int res = gui::dialog(disp(),"",
-                                                                       _("This 
save is from a different version of the game. Do you want to try to load it?"),
-                                                                       
gui::YES_NO).show();
-                               if(res == 1) {
-                                       return false;
-                               }
-                       }
-               }
-
                paths_manager_.set_paths(game_config_);
-               state_ = game_state(cfg, show_replay);
-
-               // Get the status of the random in the snapshot.
-               // For a replay we need to restore the start only, the 
replaying gets at
-               // proper location.
-               // For normal loading also restore the call count.
-               const int seed = lexical_cast_default<int>
-                       (cfg["random_seed"], 42);
-               const unsigned calls = show_replay ? 0 :
-                       lexical_cast_default<unsigned> 
(state_.snapshot["random_calls"]);
-               state_.rng().seed_random(seed, calls);
-
+               load.set_gamestate();
+
+       } catch(load_game_cancelled_exception&) {
+               return false;
        } catch(config::error& e) {
                if(e.message.empty()) {
                        gui::show_error_message(disp(), _("The file you have 
tried to load is corrupt"));
@@ -999,7 +935,7 @@
 
        if (!state_.snapshot.child("side")) {
                // No snapshot; this is a start-of-scenario
-               if (show_replay) {
+               if (load.show_replay()) {
                        // There won't be any turns to replay, but the
                        // user gets to watch the intro sequence again ...
                        LOG_CONFIG << "replaying (start of scenario)\n";
@@ -1009,7 +945,7 @@
                }
        } else {
                // We have a snapshot. But does the user want to see a replay?
-               if(show_replay) {
+               if(load.show_replay()) {
                        statistics::clear_current_scenario();
                        LOG_CONFIG << "replaying (snapshot)\n";
                } else {
@@ -1031,7 +967,7 @@
                }
        }
 
-       if (cancel_orders) {
+       if (load.cancel_orders()) {
                foreach (config &side, state_.snapshot.child_range("side"))
                {
                        if (side["controller"] != "human") continue;

Modified: trunk/src/menu_events.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/menu_events.cpp?rev=34793&r1=34792&r2=34793&view=diff
==============================================================================
--- trunk/src/menu_events.cpp (original)
+++ trunk/src/menu_events.cpp Sun Apr 12 18:05:53 2009
@@ -630,15 +630,6 @@
                end = SDL_GetTicks();
                LOG_NG << "Milliseconds to save " << save.filename() << ": " << 
end - start << "\n";
                remove_old_auto_saves();
-       }
-
-       void menu_handler::load_game(){
-               bool show_replay = false;
-               bool cancel_orders = false;
-               const std::string game = dialogs::load_game_dialog(*gui_, 
game_config_, &show_replay, &cancel_orders);
-               if(game != "") {
-                       throw 
game::load_game_exception(game,show_replay,cancel_orders);
-               }
        }
 
        void menu_handler::preferences()

Modified: trunk/src/menu_events.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/menu_events.hpp?rev=34793&r1=34792&r2=34793&view=diff
==============================================================================
--- trunk/src/menu_events.hpp (original)
+++ trunk/src/menu_events.hpp Sun Apr 12 18:05:53 2009
@@ -69,10 +69,7 @@
        void show_statistics(const unsigned int team_num);
        void unit_list();
        void status_table(int selected=0);
-       void save_replay(const std::string& message, gui::DIALOG_TYPE 
dialog_type, const bool has_exit_button=false);
-       void save_game(const std::string& message, gui::DIALOG_TYPE 
dialog_type, const bool has_exit_button=false);
        void save_map();
-       void load_game();
        void preferences();
        void show_chat_log();
        void show_help();

Modified: trunk/src/play_controller.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/play_controller.cpp?rev=34793&r1=34792&r2=34793&view=diff
==============================================================================
--- trunk/src/play_controller.cpp (original)
+++ trunk/src/play_controller.cpp Sun Apr 12 18:05:53 2009
@@ -316,7 +316,8 @@
 }
 
 void play_controller::load_game(){
-       menu_handler_.load_game();
+       loadgame load(*gui_, game_config_, gamestate_);
+       load.load_game();
 }
 
 void play_controller::preferences(){

Modified: trunk/src/savegame.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/savegame.cpp?rev=34793&r1=34792&r2=34793&view=diff
==============================================================================
--- trunk/src/savegame.cpp (original)
+++ trunk/src/savegame.cpp Sun Apr 12 18:05:53 2009
@@ -23,13 +23,120 @@
 #include "log.hpp"
 #include "map.hpp"
 #include "map_label.hpp"
+#include "unit_id.hpp"
 #include "preferences_display.hpp"
 #include "replay.hpp"
 #include "serialization/binary_or_text.hpp"
 #include "sound.hpp"
 #include "statistics.hpp"
+#include "version.hpp"
 
 #define LOG_SAVE LOG_STREAM(info, engine)
+
+loadgame::loadgame(display& gui, const config& game_config, game_state& 
gamestate)
+       : gamestate_(gamestate)
+       , gui_(gui)
+       , game_config_(game_config)
+{
+       gamestate_ = game_state();
+}
+
+void loadgame::show_dialog(bool show_replay, bool cancel_orders)
+{
+       bool show_replay_dialog = show_replay;
+       bool cancel_orders_dialog = cancel_orders;
+
+       if (filename_.empty())
+       {
+               //FIXME: Integrate the load_game dialog into this class
+               filename_ = dialogs::load_game_dialog(gui_, game_config_, 
&show_replay_dialog, &cancel_orders_dialog);
+
+               show_replay_ = show_replay;
+               cancel_orders_ = cancel_orders;
+       }
+}
+
+void loadgame::load_game()
+{
+       show_dialog(false, false);
+
+       if(filename_ != "")
+               throw game::load_game_exception(filename_, show_replay_, 
cancel_orders_);
+}
+
+void loadgame::load_game(std::string& filename, bool show_replay, bool 
cancel_orders)
+{
+       filename_ = filename;
+       show_dialog(show_replay, cancel_orders);
+       show_replay_ = show_replay;
+       cancel_orders_ = cancel_orders;
+
+       if (filename_.empty())
+               show_dialog(show_replay, cancel_orders);
+
+       if (filename_.empty())
+               throw load_game_cancelled_exception();
+
+       std::string error_log;
+       ::read_save_file(filename_, load_config_, &error_log);
+
+       if(!error_log.empty()) {
+        try {
+                   gui::show_error_message(gui_,
+                                   _("Warning: The file you have tried to load 
is corrupt. Loading anyway.\n") +
+                                   error_log);
+        } catch (utils::invalid_utf8_exception&) {
+                   gui::show_error_message(gui_,
+                                   _("Warning: The file you have tried to load 
is corrupt. Loading anyway.\n") +
+                    std::string("(UTF-8 ERROR)"));
+        }
+       }
+
+       gamestate_.difficulty = load_config_["difficulty"];
+       gamestate_.campaign_define = load_config_["campaign_define"];
+       gamestate_.campaign_type = load_config_["campaign_type"];
+       gamestate_.campaign_xtra_defines = 
utils::split(load_config_["campaign_extra_defines"]);
+       gamestate_.version = load_config_["version"];
+
+       if(gamestate_.version != game_config::version) {
+               const version_info parsed_savegame_version(gamestate_.version);
+               if(game_config::wesnoth_version.minor_version() % 2 != 0 ||
+                  game_config::wesnoth_version.major_version() != 
parsed_savegame_version.major_version() ||
+                  game_config::wesnoth_version.minor_version() != 
parsed_savegame_version.minor_version()) {
+                       // do not load if too old, if either the savegame or 
the current game
+                       // has the version 'test' allow loading
+                       
if(!game_config::is_compatible_savegame_version(gamestate_.version)) {
+                               /* GCC-3.3 needs a temp var otherwise 
compilation fails */
+                               gui::message_dialog dlg(gui_, "", _("This save 
is from a version too old to be loaded."));
+                               dlg.show();
+                               throw load_game_cancelled_exception();
+                       }
+
+                       const int res = gui::dialog(gui_,"",
+                                                               _("This save is 
from a different version of the game. Do you want to try to load it?"),
+                                                               
gui::YES_NO).show();
+                       if(res == 1) {
+                               throw load_game_cancelled_exception();
+                       }
+               }
+       }
+
+}
+
+void loadgame::set_gamestate()
+{
+       gamestate_ = game_state(load_config_, show_replay_);
+
+       // Get the status of the random in the snapshot.
+       // For a replay we need to restore the start only, the replaying gets at
+       // proper location.
+       // For normal loading also restore the call count.
+       const int seed = lexical_cast_default<int>
+               (load_config_["random_seed"], 42);
+       const unsigned calls = show_replay_ ? 0 :
+               lexical_cast_default<unsigned> 
(gamestate_.snapshot["random_calls"]);
+       gamestate_.rng().seed_random(seed, calls);
+}
 
 savegame::savegame(game_state& gamestate, const std::string title)
        : gamestate_(gamestate)

Modified: trunk/src/savegame.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/savegame.hpp?rev=34793&r1=34792&r2=34793&view=diff
==============================================================================
--- trunk/src/savegame.hpp (original)
+++ trunk/src/savegame.hpp Sun Apr 12 18:05:53 2009
@@ -19,10 +19,38 @@
 #include "global.hpp"
 #include "show_dialog.hpp"
 #include "gamestatus.hpp"
-#include "unit_id.hpp"
 
 #include <string>
 
+struct load_game_cancelled_exception
+{
+};
+
+class loadgame
+{
+public:
+       loadgame(display& gui, const config& game_config, game_state& 
gamestate);
+       virtual ~loadgame() {}
+
+       void load_game();
+       void load_game(std::string& filename, bool show_replay, bool 
cancel_orders);
+       void set_gamestate();
+
+       const bool show_replay() const { return show_replay_; }
+       const bool cancel_orders() const { return cancel_orders_; }
+
+private:
+       void show_dialog(bool show_replay, bool cancel_orders);
+
+       const config& game_config_;
+       display& gui_;
+
+       game_state& gamestate_;
+       std::string filename_;
+       config load_config_;
+       bool show_replay_;
+       bool cancel_orders_;
+};
 
 /** The base class for all savegame stuff */
 class savegame


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to