savegame.hpp

Go to the documentation of this file.
00001 /* $Id: savegame.hpp 54210 2012-05-18 20:11:11Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2003 - 2012 by Jörg Hinrichs, refactored from various
00004    places formerly created by David White <dave@whitevine.net>
00005    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY.
00013 
00014    See the COPYING file for more details.
00015 */
00016 
00017 #ifndef SAVEGAME_H_INCLUDED
00018 #define SAVEGAME_H_INCLUDED
00019 
00020 #include "filesystem.hpp"
00021 #include "gamestatus.hpp"
00022 #include "tod_manager.hpp"
00023 #include "show_dialog.hpp"
00024 
00025 class config_writer;
00026 class game_display;
00027 
00028 struct load_game_cancelled_exception {};
00029 struct illegal_filename_exception {};
00030 
00031 namespace savegame {
00032 
00033 /** Filename and modification date for a file list */
00034 class save_info {
00035 private:
00036     friend class create_save_info;
00037 private:
00038     save_info(const std::string& name, const time_t& modified);
00039 public:
00040     const std::string& name()     const;
00041     const time_t&      modified() const;
00042 public:
00043     const std::string format_time_summary() const;
00044     const std::string format_time_local() const;
00045     const config& summary() const;
00046 private:
00047     std::string name_;
00048     time_t modified_;
00049 };
00050 
00051 /**
00052  * A structure for comparing to save_info objects based on their modified time.
00053  * If the times are equal, will order based on the name.
00054  */
00055 struct save_info_less_time {
00056     bool operator()(const save_info& a, const save_info& b) const;
00057 };
00058 
00059 std::vector<save_info> get_saves_list(const std::string* dir = NULL, const std::string* filter = NULL);
00060 
00061 /** Read the complete config information out of a savefile. */
00062 void read_save_file(const std::string& name, config& cfg, std::string* error_log);
00063 
00064 /** Returns true if there is already a savegame with that name. */
00065 bool save_game_exists(const std::string& name, bool compress_saves);
00066 
00067 /** Delete all autosaves of a certain scenario. */
00068 void clean_saves(const std::string& label);
00069 
00070 /** Remove autosaves that are no longer needed (according to the autosave policy in the preferences). */
00071 void remove_old_auto_saves(const int autosavemax, const int infinite_auto_saves);
00072 
00073 /** Delete a savegame. */
00074 void delete_game(const std::string& name);
00075 
00076 /** The class for loading a savefile. */
00077 class loadgame
00078 {
00079 public:
00080     loadgame(display& gui, const config& game_config, game_state& gamestate);
00081     virtual ~loadgame() {}
00082 
00083     /** Load a game without providing any information. */
00084     void load_game();
00085     /** Load a game with pre-setting information for the load-game dialog. */
00086     void load_game(
00087               const std::string& filename
00088             , const bool show_replay
00089             , const bool cancel_orders
00090             , const bool select_difficulty
00091             , const std::string& difficulty);
00092     /** Loading a game from within the multiplayer-create dialog. */
00093     void load_multiplayer_game();
00094     /** Populates the level-config for multiplayer from the loaded savegame information. */
00095     void fill_mplevel_config(config& level);
00096     /** Generate the gamestate out of the loaded game config. */
00097     void set_gamestate();
00098 
00099     // Getter-methods
00100     bool show_replay() const { return show_replay_; }
00101     bool cancel_orders() const { return cancel_orders_; }
00102     const std::string filename() const { return filename_; }
00103 
00104 private:
00105     /** Display the load-game dialog. */
00106     void show_dialog(bool show_replay, bool cancel_orders);
00107     /** Display the difficulty dialog. */
00108     void show_difficulty_dialog();
00109     /** Check if the version of the savefile is compatible with the current version. */
00110     void check_version_compatibility();
00111     /** Copy era information into the snapshot. */
00112     void copy_era(config& cfg);
00113 
00114     const config& game_config_;
00115     display& gui_;
00116 
00117     game_state& gamestate_; /** Primary output information. */
00118     std::string filename_; /** Name of the savefile to be loaded. */
00119     std::string difficulty_; /** The difficulty the save is meant to be loaded with. */
00120     config load_config_; /** Config information of the savefile to be loaded. */
00121     bool show_replay_; /** State of the "show_replay" checkbox in the load-game dialog. */
00122     bool cancel_orders_; /** State of the "cancel_orders" checkbox in the load-game dialog. */
00123     bool select_difficulty_; /** State of the "change_difficulty" checkbox in the load-game dialog. */
00124 };
00125 
00126 /** The base class for all savegame stuff */
00127 class savegame
00128 {
00129 public:
00130     /** The only constructor of savegame. The title parameter is only necessary if you
00131         intend to do interactive saves. */
00132     savegame(game_state& gamestate, const bool compress_saves, const std::string& title = "Save");
00133 
00134     virtual ~savegame() {}
00135 
00136     /** Save a game without any further user interaction. Atm, this is only used by the
00137         console_handler save actions. The return value denotes, if the save was successful or not. */
00138     //bool save_game(const std::string& filename);
00139 
00140     /** Saves a game without user interaction, unless the file exists and it should be asked
00141         to overwrite it. The return value denotes, if the save was successful or not.
00142         This is used by automatically generated replays, start-of-scenario saves and autosaves.
00143     */
00144     bool save_game_automatic(CVideo& video, bool ask_for_overwrite = false, const std::string& filename = "");
00145 
00146     /** Save a game interactively through the savegame dialog. Used for manual midgame and replay
00147         saves. The return value denotes, if the save was successful or not. */
00148     bool save_game_interactive(CVideo& video, const std::string& message,
00149         gui::DIALOG_TYPE dialog_type);
00150 
00151     const std::string& filename() const { return filename_; }
00152 
00153 protected:
00154     /**
00155         Save a game without any further user interaction. If you want notifying messages
00156         or error messages to appear, you have to provide the gui parameter.
00157         The return value denotes, if the save was successful or not.
00158     */
00159     bool save_game(CVideo* video = NULL, const std::string& filename = "");
00160 
00161     /** Sets the filename and removes invalid characters. Don't set the filename directly but
00162         use this method instead. */
00163     void set_filename(std::string filename);
00164     /** Check, if the filename contains illegal constructs like ".gz". */
00165     void check_filename(const std::string& filename, CVideo& video);
00166 
00167     /** Customize the standard error message */
00168     void set_error_message(const std::string& error_message) { error_message_ = error_message; }
00169 
00170     const std::string& title() const { return title_; }
00171     game_state& gamestate() const { return gamestate_; }
00172     config& snapshot() { return snapshot_; }
00173 
00174     /** If there needs to be some data fiddling before saving the game, this is the place to go. */
00175     virtual void before_save();
00176 
00177 private:
00178     /** Checks if a certain character is allowed in a savefile name. */
00179     static bool is_illegal_file_char(char c);
00180 
00181     /** Build the filename according to the specific savegame's needs. Subclasses will have to
00182         override this to take effect. */
00183     virtual void create_filename() {}
00184     /** Display the save game dialog. */
00185     virtual int show_save_dialog(CVideo& video, const std::string& message, const gui::DIALOG_TYPE dialog_type);
00186     /** Ask the user if an existing file should be overwritten. */
00187     bool check_overwrite(CVideo& video);
00188 
00189     /** The actual method for saving the game to disk. All interactive filename choosing and
00190         data manipulation has to happen before calling this method. */
00191     void write_game_to_disk(const std::string& filename);
00192 
00193     /** Writing the savegame config to a file. */
00194     void write_game(config_writer &out) const;
00195     /** Update the save_index */
00196     void finish_save_game(const config_writer &out);
00197     /** Throws game::save_game_failed. */
00198     scoped_ostream open_save_game(const std::string &label);
00199     friend class save_info;
00200 
00201     game_state& gamestate_;
00202 
00203     /** Gamestate information at the time of saving. Note that this object is needed here, since
00204         even if it is empty the code relies on it to be there. */
00205     config snapshot_;
00206 
00207     std::string filename_; /** Filename of the savegame file on disk */
00208 
00209     const std::string title_; /** Title of the savegame dialog */
00210 
00211     std::string error_message_; /** Error message to be displayed if the savefile could not be generated. */
00212 
00213     bool show_confirmation_; /** Determines if a confirmation of successful saving the game is shown. */
00214 
00215     bool compress_saves_; /** Determines, if compression is used for the savegame file */
00216 };
00217 
00218 /** Class for "normal" midgame saves. The additional members are needed for creating the snapshot
00219     information. */
00220 class game_savegame : public savegame
00221 {
00222 public:
00223     game_savegame(game_state& gamestate,
00224         game_display& gui, const config& snapshot_cfg, const bool compress_saves);
00225 
00226 private:
00227     /** Create a filename for automatic saves */
00228     virtual void create_filename();
00229 
00230     /** Builds the snapshot config. */
00231     virtual void before_save();
00232 
00233     /** For normal game saves. Builds a snapshot config object out of the relevant information. */
00234     void write_game_snapshot();
00235 
00236 protected:
00237     game_display& gui_;
00238 };
00239 
00240 /** Class for replay saves (either manually or automatically). */
00241 class replay_savegame : public savegame
00242 {
00243 public:
00244     replay_savegame(game_state& gamestate, const bool compress_saves);
00245 
00246 private:
00247     /** Create a filename for automatic saves */
00248     virtual void create_filename();
00249 };
00250 
00251 /** Class for autosaves. */
00252 class autosave_savegame : public game_savegame
00253 {
00254 public:
00255     autosave_savegame(game_state &gamestate,
00256                      game_display& gui, const config& snapshot_cfg, const bool compress_saves);
00257 
00258     void autosave(const bool disable_autosave, const int autosave_max, const int infinite_autosaves);
00259 private:
00260     /** Create a filename for automatic saves */
00261     virtual void create_filename();
00262 };
00263 
00264 class oos_savegame : public game_savegame
00265 {
00266 public:
00267     oos_savegame(const config& snapshot_cfg);
00268 
00269 private:
00270     /** Display the save game dialog. */
00271     virtual int show_save_dialog(CVideo& video, const std::string& message, const gui::DIALOG_TYPE dialog_type);
00272 };
00273 
00274 /** Class for start-of-scenario saves */
00275 class scenariostart_savegame : public savegame
00276 {
00277 public:
00278     scenariostart_savegame(game_state& gamestate, const bool compress_saves);
00279 
00280 private:
00281     /** Adds the player information to the starting position (= [replay_start]). */
00282     virtual void before_save();
00283 };
00284 
00285 } //end of namespace savegame
00286 
00287 void replace_underbar2space(std::string &name);
00288 void replace_space2underbar(std::string &name);
00289 
00290 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:08 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs