The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
savegame.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by Jörg Hinrichs, refactored from various
3  places formerly created by David White <[email protected]>
4  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #ifndef SAVEGAME_H_INCLUDED
17 #define SAVEGAME_H_INCLUDED
18 
19 #include "filesystem.hpp"
20 #include "saved_game.hpp"
21 #include "show_dialog.hpp"
23 class config_writer;
24 class game_display;
25 
26 namespace savegame {
27 /** converts saves from older versions of wesnoth*/
28 void convert_old_saves(config& cfg);
29 /** Returns true if there is already a savegame with that name. */
30 bool save_game_exists(const std::string& name, compression::format compressed);
31 
32 /** Delete all autosaves of a certain scenario. */
33 void clean_saves(const std::string& label);
34 
35 /** The class for loading a savefile. */
36 class loadgame
37 {
38 public:
39  loadgame(CVideo& video, const config& game_config, saved_game& gamestate);
40  virtual ~loadgame() {}
41 
42  /* In any of the following three function, a bool value of false indicates
43  some failure or abort of the load process */
44 
45  /** Load a game without providing any information. */
46  bool load_game();
47  /** Load a game with pre-setting information for the load-game dialog. */
48  bool load_game(
49  const std::string& filename
50  , const bool show_replay
51  , const bool cancel_orders
52  , const bool select_difficulty
53  , const std::string& difficulty
54  , bool skip_version_check);
55  /** Loading a game from within the multiplayer-create dialog. */
56  bool load_multiplayer_game();
57  /** Generate the gamestate out of the loaded game config. */
58  void set_gamestate();
59 
60  // Getter-methods
61  bool show_replay() const { return show_replay_; }
62  bool cancel_orders() const { return cancel_orders_; }
63  const std::string & filename() const { return filename_; }
64 
65  /** GUI Dialog sequence which confirms attempts to load saves from previous game versions. */
66  static bool check_version_compatibility(const version_info & version, CVideo & video);
67 
68  static bool is_replay_save(const config& cfg)
69  {
70  return cfg["replay"].to_bool() && !cfg["snapshot"].to_bool(true);
71  }
72 
73 private:
74  /** Display the load-game dialog. */
75  void show_dialog();
76  /** Display the difficulty dialog. */
78  /** Call check_version_compatibility above, using the version of this savefile. */
80  /** Copy era information into the snapshot. */
81  void copy_era(config& cfg);
82 
85 
86  saved_game& gamestate_; /** Primary output information. */
87  std::string filename_; /** Name of the savefile to be loaded. */
88  std::string difficulty_; /** The difficulty the save is meant to be loaded with. */
89  config load_config_; /** Config information of the savefile to be loaded. */
90  bool show_replay_; /** State of the "show_replay" checkbox in the load-game dialog. */
91  bool cancel_orders_; /** State of the "cancel_orders" checkbox in the load-game dialog. */
92  bool select_difficulty_; /** State of the "change_difficulty" checkbox in the load-game dialog. */
93  config summary_; /** Summary config of the save selected in the load game dialog. */
94 };
95 
96 /**
97  * The base class for all savegame stuff.
98  * This should not be used directly, as it does not directly produce usable saves.
99  * Instead, use one of the derived classes.
100  */
101 class savegame
102 {
103 protected:
104  /** The only constructor of savegame. The title parameter is only necessary if you
105  intend to do interactive saves. */
106  savegame(saved_game& gamestate, const compression::format compress_saves, const std::string& title = "Save");
107 
108 public:
109  virtual ~savegame() {}
110 
111  /** Saves a game without user interaction, unless the file exists and it should be asked
112  to overwrite it. The return value denotes, if the save was successful or not.
113  This is used by automatically generated replays, start-of-scenario saves, autosaves,
114  and saves from the console (e.g. ":w").
115  */
116  bool save_game_automatic(CVideo& video, bool ask_for_overwrite = false, const std::string& filename = "");
117 
118  /** Save a game interactively through the savegame dialog. Used for manual midgame and replay
119  saves. The return value denotes, if the save was successful or not. */
120  bool save_game_interactive(CVideo& video, const std::string& message,
121  gui::DIALOG_TYPE dialog_type);
122 
123  const std::string& filename() const { return filename_; }
124 
125 protected:
126  /**
127  Save a game without any further user interaction. If you want notifying messages
128  or error messages to appear, you have to provide the gui parameter.
129  The return value denotes, if the save was successful or not.
130  */
131  bool save_game(CVideo* video = nullptr, const std::string& filename = "");
132 
133  /** Sets the filename and removes invalid characters. Don't set the filename directly but
134  use this method instead. */
135  void set_filename(std::string filename);
136  /** Check, if the filename contains illegal constructs like ".gz". */
137  bool check_filename(const std::string& filename, CVideo& video);
138 
139  /** Customize the standard error message */
140  void set_error_message(const std::string& error_message) { error_message_ = error_message; }
141 
142  const std::string& title() { return title_; }
143  const saved_game& gamestate() { return gamestate_; }
144 
145  /** If there needs to be some data fiddling before saving the game, this is the place to go. */
146  void before_save();
147 
148  /** Writing the savegame config to a file. */
149  virtual void write_game(config_writer &out);
150 
151 private:
152  /** Checks if a certain character is allowed in a savefile name. */
153  static bool is_illegal_file_char(char c);
154 
155  /** Build the filename according to the specific savegame's needs. Subclasses will have to
156  override this to take effect. */
157  virtual void create_filename() {}
158  /** Display the save game dialog. */
159  virtual int show_save_dialog(CVideo& video, const std::string& message, const gui::DIALOG_TYPE dialog_type);
160  /** Ask the user if an existing file should be overwritten. */
161  bool check_overwrite(CVideo& video);
162 
163  /** The actual method for saving the game to disk. All interactive filename choosing and
164  data manipulation has to happen before calling this method. */
165  void write_game_to_disk(const std::string& filename);
166 
167  /** Update the save_index */
168  void finish_save_game(const config_writer &out);
169  /** Throws game::save_game_failed. */
170  filesystem::scoped_ostream open_save_game(const std::string &label);
171  friend class save_info;
172  //before_save (write replay data) changes this so it cannot be const
174  /** Filename of the savegame file on disk */
176 
177  const std::string title_; /** Title of the savegame dialog */
178 
179  std::string error_message_; /** Error message to be displayed if the savefile could not be generated. */
180 
181  bool show_confirmation_; /** Determines if a confirmation of successful saving the game is shown. */
182 
183  compression::format compress_saves_; /** Determines, what compression format is used for the savegame file */
184 };
185 
186 /** Class for "normal" midgame saves. The additional members are needed for creating the snapshot
187  information. */
188 class ingame_savegame : public savegame
189 {
190 public:
192  game_display& gui, const compression::format compress_saves);
193 
194 private:
195  /** Create a filename for automatic saves */
196  virtual void create_filename();
197 
198 
199  void write_game(config_writer &out);
200 
201 protected:
203 };
204 
205 /** Class for replay saves (either manually or automatically). */
206 class replay_savegame : public savegame
207 {
208 public:
210 
211 private:
212  /** Create a filename for automatic saves */
213  virtual void create_filename();
214 
215  void write_game(config_writer &out);
216 };
217 
218 /** Class for autosaves. */
220 {
221 public:
223  game_display& gui, const compression::format compress_saves);
224 
225  void autosave(const bool disable_autosave, const int autosave_max, const int infinite_autosaves);
226 private:
227  /** Create a filename for automatic saves */
228  virtual void create_filename();
229 };
230 
232 {
233 public:
235 
236 private:
237  /** Display the save game dialog. */
238  virtual int show_save_dialog(CVideo& video, const std::string& message, const gui::DIALOG_TYPE dialog_type);
239  bool& ignore_;
240 };
241 
242 /** Class for start-of-scenario saves */
244 {
245 public:
247 
248 private:
249  void write_game(config_writer &out);
250 };
251 
252 } //end of namespace savegame
253 
254 #endif
std::string difficulty_
Name of the savefile to be loaded.
Definition: savegame.hpp:88
void show_difficulty_dialog()
Display the difficulty dialog.
Definition: savegame.cpp:121
bool select_difficulty_
State of the "cancel_orders" checkbox in the load-game dialog.
Definition: savegame.hpp:92
bool load_game()
Load a game without providing any information.
Definition: savegame.cpp:152
Class for start-of-scenario saves.
Definition: savegame.hpp:243
bool cancel_orders() const
Definition: savegame.hpp:62
The class for loading a savefile.
Definition: savegame.hpp:36
void write_game(config_writer &out)
Writing the savegame config to a file.
Definition: savegame.cpp:553
void set_gamestate()
Generate the gamestate out of the loaded game config.
Definition: savegame.cpp:284
const GLfloat * c
Definition: glew.h:12741
replay_savegame(saved_game &gamestate, const compression::format compress_saves)
Definition: savegame.cpp:558
Definition: video.hpp:58
ingame_savegame(saved_game &gamestate, game_display &gui, const compression::format compress_saves)
Definition: savegame.cpp:633
Class for autosaves.
Definition: savegame.hpp:219
const std::string title_
Definition: savegame.hpp:177
void show_dialog()
Display the load-game dialog.
Definition: savegame.cpp:97
General purpose widgets.
Class for "normal" midgame saves.
Definition: savegame.hpp:188
virtual void create_filename()
Create a filename for automatic saves.
Definition: savegame.cpp:640
compression::format compress_saves_
Determines if a confirmation of successful saving the game is shown.
Definition: savegame.hpp:183
loadgame(CVideo &video, const config &game_config, saved_game &gamestate)
Definition: savegame.cpp:84
scenariostart_savegame(saved_game &gamestate, const compression::format compress_saves)
Definition: savegame.cpp:547
std::string filename_
Definition: action_wml.cpp:506
void write_game(config_writer &out)
Writing the savegame config to a file.
Definition: savegame.cpp:567
virtual void create_filename()
Create a filename for automatic saves.
Definition: savegame.cpp:596
config summary_
State of the "change_difficulty" checkbox in the load-game dialog.
Definition: savegame.hpp:93
Class for writing a config out to a file in pieces.
autosave_savegame(saved_game &gamestate, game_display &gui, const compression::format compress_saves)
Definition: savegame.cpp:579
bool disable_autosave
Definition: game_config.cpp:64
virtual ~loadgame()
Definition: savegame.hpp:40
saved_game & gamestate_
Definition: savegame.hpp:86
bool save_game_exists(const std::string &name, compression::format compressed)
Returns true if there is already a savegame with that name.
Definition: savegame.cpp:61
static bool is_replay_save(const config &cfg)
Definition: savegame.hpp:68
virtual int show_save_dialog(CVideo &video, const std::string &message, const gui::DIALOG_TYPE dialog_type)
Display the save game dialog.
Definition: savegame.cpp:612
const config & game_config_
Definition: savegame.hpp:83
std::string error_message_
Title of the savegame dialog.
Definition: savegame.hpp:179
void convert_old_saves(config &cfg)
converts saves from older versions of wesnoth
Definition: savegame.cpp:840
void write_game(config_writer &out)
Writing the savegame config to a file.
Definition: savegame.cpp:646
void clean_saves(const std::string &label)
Delete all autosaves of a certain scenario.
Definition: savegame.cpp:71
Game configuration data as global variables.
Definition: build_info.cpp:38
saved_game & gamestate_
Definition: savegame.hpp:173
const std::string & filename() const
Definition: savegame.hpp:63
bool show_confirmation_
Error message to be displayed if the savefile could not be generated.
Definition: savegame.hpp:181
Declarations for File-IO.
virtual void create_filename()
Create a filename for automatic saves.
Definition: savegame.cpp:562
bool show_replay() const
Definition: savegame.hpp:61
bool load_multiplayer_game()
Loading a game from within the multiplayer-create dialog.
Definition: savegame.cpp:289
Class for replay saves (either manually or automatically).
Definition: savegame.hpp:206
Represents version numbers.
Definition: version.hpp:44
const std::string & title()
Definition: savegame.hpp:142
GLuint const GLchar * name
Definition: glew.h:1782
void copy_era(config &cfg)
Copy era information into the snapshot.
Definition: savegame.cpp:327
void set_error_message(const std::string &error_message)
Customize the standard error message.
Definition: savegame.hpp:140
virtual ~savegame()
Definition: savegame.hpp:109
std::string filename_
Primary output information.
Definition: savegame.hpp:87
Filename and modification date for a file list.
Definition: save_index.hpp:29
const saved_game & gamestate()
Definition: savegame.hpp:143
virtual void create_filename()
Build the filename according to the specific savegame's needs.
Definition: savegame.hpp:157
void autosave(const bool disable_autosave, const int autosave_max, const int infinite_autosaves)
Definition: savegame.cpp:586
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
std::string filename_
Filename of the savegame file on disk.
Definition: savegame.hpp:175
const std::string & filename() const
Definition: savegame.hpp:123
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
config load_config_
The difficulty the save is meant to be loaded with.
Definition: savegame.hpp:89
bool cancel_orders_
State of the "show_replay" checkbox in the load-game dialog.
Definition: savegame.hpp:91
bool check_version_compatibility()
Call check_version_compatibility above, using the version of this savefile.
Definition: savegame.cpp:236
const std::string version
Definition: game_config.cpp:48
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool show_replay_
Config information of the savefile to be loaded.
Definition: savegame.hpp:90
oos_savegame(saved_game &gamestate, game_display &gui, bool &ignore)
Definition: savegame.cpp:607