The Battle for Wesnoth  1.15.1+dev
savegame.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by Jörg Hinrichs, refactored from various
3  places formerly created by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://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 #pragma once
17 
18 #include "config.hpp"
19 #include "filesystem.hpp"
21 #include "saved_game.hpp"
23 
24 #include <exception>
25 
26 class config_writer;
27 class version_info;
28 
29 namespace savegame {
30 /** converts saves from older versions of wesnoth*/
31 void convert_old_saves(config& cfg);
32 /** Returns true if there is already a savegame with that name. */
33 bool save_game_exists(std::string name, compression::format compressed);
34 
35 /** Delete all autosaves of a certain scenario. */
36 void clean_saves(const std::string& label);
37 
39  /** Name of the savefile to be loaded. */
40  std::string filename;
41 
42  /** The difficulty the save is meant to be loaded with. */
43  std::string difficulty;
44 
45  /** State of the "show_replay" checkbox in the load-game dialog. */
47 
48  /** State of the "cancel_orders" checkbox in the load-game dialog. */
50 
51  /** State of the "change_difficulty" checkbox in the load-game dialog. */
53 
54  /** Summary config of the save selected in the load game dialog. */
56 
57  /** Config information of the savefile to be loaded. */
59 
60  explicit load_game_metadata(const std::string& fname = "", const std::string& hard = "",
61  bool replay = false, bool stop = false, bool change = false,
62  const config& summary = config(), const config& info = config())
63  : filename(fname), difficulty(hard)
64  , show_replay(replay), cancel_orders(stop), select_difficulty(change)
65  , summary(summary), load_config(info)
66  {
67  }
68 };
69 
70 /**
71 * Exception used to signal that the user has decided to abortt a game,
72 * and to load another game instead.
73 */
75  : public lua_jailbreak_exception, public std::exception
76 {
77 public:
78  load_game_exception(const std::string& fname)
80  , data_(fname)
81  {
82  }
83 
86  , data_(data)
87  {
88  }
90 private:
91 
93 };
94 
95 /** The class for loading a savefile. */
96 class loadgame
97 {
98 public:
99  loadgame(const config& game_config, saved_game& gamestate);
100  virtual ~loadgame() {}
101 
102  /* In any of the following three function, a bool value of false indicates
103  some failure or abort of the load process */
104 
105  /** Load a game without providing any information. */
106  bool load_game_ingame();
107  /** Load a game with pre-setting information for the load-game dialog. */
108  bool load_game();
109  /** Loading a game from within the multiplayer-create dialog. */
110  bool load_multiplayer_game();
111  /** Generate the gamestate out of the loaded game config. */
112  void set_gamestate();
113 
114  // Getter-methods
116  {
117  return load_data_;
118  }
119 
120  /** GUI Dialog sequence which confirms attempts to load saves from previous game versions. */
121  static bool check_version_compatibility(const version_info & version);
122 
123  static bool is_replay_save(const config& cfg)
124  {
125  return cfg["replay"].to_bool() && !cfg["snapshot"].to_bool(true);
126  }
127 
128 private:
129  /** Display the difficulty dialog. */
130  bool show_difficulty_dialog();
131  /** Call check_version_compatibility above, using the version of this savefile. */
132  bool check_version_compatibility();
133  /** Copy era information into the snapshot. */
134  void copy_era(config& cfg);
135 
137 
138  saved_game& gamestate_; /** Primary output information. */
139 
141 };
142 /**
143  * The base class for all savegame stuff.
144  * This should not be used directly, as it does not directly produce usable saves.
145  * Instead, use one of the derived classes.
146  */
147 class savegame
148 {
149 protected:
150  /** The only constructor of savegame. The title parameter is only necessary if you
151  intend to do interactive saves. */
152  savegame(saved_game& gamestate, const compression::format compress_saves, const std::string& title = "Save");
153 
154 public:
155  enum DIALOG_TYPE { YES_NO, OK_CANCEL};
156 
157  virtual ~savegame() {}
158 
159  /** Saves a game without user interaction, unless the file exists and it should be asked
160  to overwrite it. The return value denotes, if the save was successful or not.
161  This is used by automatically generated replays, start-of-scenario saves, autosaves,
162  and saves from the console (e.g. ":w").
163  */
164  bool save_game_automatic(bool ask_for_overwrite = false, const std::string& filename = "");
165 
166  /** Save a game interactively through the savegame dialog. Used for manual midgame and replay
167  saves. The return value denotes, if the save was successful or not. */
168  bool save_game_interactive(const std::string& message,
169  DIALOG_TYPE dialog_type);
170 
171  const std::string& filename() const { return filename_; }
172 
173  /** Build the filename according to the specific savegame's needs. */
174  std::string create_filename() const
175  {
176  return create_filename(gamestate().get_starting_point()["turn_at"]);
177  }
178 
179  /** Build the filename for the specified turn. */
180  std::string create_filename(unsigned int turn_number) const;
181 
182 protected:
183  /**
184  Save a game without any further user interaction.
185  The return value denotes, if the save was successful or not.
186  */
187  bool save_game(const std::string& filename = "");
188 
189  /** Check, if the filename contains illegal constructs like ".gz". */
190  bool check_filename(const std::string& filename);
191 
192  /** Customize the standard error message */
193  void set_error_message(const std::string& error_message) { error_message_ = error_message; }
194 
195  const std::string& title() const { return title_; }
196  const saved_game& gamestate() const { return gamestate_; }
197 
198  /** If there needs to be some data fiddling before saving the game, this is the place to go. */
199  void before_save();
200 
201  /** Writing the savegame config to a file. */
202  virtual void write_game(config_writer &out);
203 
204  /** Filename of the savegame file on disk */
205  std::string filename_;
206 
207  /** Title of the savegame dialog */
208  std::string title_;
209 
210 private:
211  /** Subclass-specific part of filename building. */
212  virtual std::string create_initial_filename(unsigned int turn_number) const = 0;
213  /** Display the save game dialog. */
214  virtual int show_save_dialog(const std::string& message, DIALOG_TYPE dialog_type);
215  /** Ask the user if an existing file should be overwritten. */
216  bool check_overwrite();
217 
218  /** The actual method for saving the game to disk. All interactive filename choosing and
219  data manipulation has to happen before calling this method. */
220  void write_game_to_disk(const std::string& filename);
221 
222  /** Update the save_index */
223  void finish_save_game(const config_writer &out);
224  /** Throws game::save_game_failed. */
225  filesystem::scoped_ostream open_save_game(const std::string &label);
226  friend class save_info;
227  //before_save (write replay data) changes this so it cannot be const
229 
230  std::string error_message_; /** Error message to be displayed if the savefile could not be generated. */
231 
232  bool show_confirmation_; /** Determines if a confirmation of successful saving the game is shown. */
233 
234  compression::format compress_saves_; /** Determines, what compression format is used for the savegame file */
235 };
236 
237 /** Class for "normal" midgame saves. The additional members are needed for creating the snapshot
238  information. */
239 class ingame_savegame : public savegame
240 {
241 public:
242  ingame_savegame(saved_game& gamestate, const compression::format compress_saves);
243 
244 private:
245  /** Create a filename for automatic saves */
246  virtual std::string create_initial_filename(unsigned int turn_number) const override;
247 
248 
249  void write_game(config_writer &out) override;
250 };
251 
252 /** Class for replay saves (either manually or automatically). */
253 class replay_savegame : public savegame
254 {
255 public:
256  replay_savegame(saved_game& gamestate, const compression::format compress_saves);
257 
258 private:
259  /** Create a filename for automatic saves */
260  virtual std::string create_initial_filename(unsigned int turn_number) const override;
261 
262  void write_game(config_writer &out) override;
263 };
264 
265 /** Class for autosaves. */
267 {
268 public:
269  autosave_savegame(saved_game &gamestate, const compression::format compress_saves);
270 
271  void autosave(const bool disable_autosave, const int autosave_max, const int infinite_autosaves);
272 private:
273  /** Create a filename for automatic saves */
274  virtual std::string create_initial_filename(unsigned int turn_number) const override;
275 };
276 
278 {
279 public:
280  oos_savegame(saved_game& gamestate, bool& ignore);
281 
282  /** Customize the dialog's caption. */
283  void set_title(const std::string& val) { title_ = val; }
284 
285 private:
286  /** Display the save game dialog. */
287  virtual int show_save_dialog(const std::string& message, DIALOG_TYPE dialog_type) override;
288  bool& ignore_;
289 };
290 
291 /** Class for start-of-scenario saves */
293 {
294 public:
295  scenariostart_savegame(saved_game& gamestate, const compression::format compress_saves);
296 
297 private:
298  /** Create a filename for automatic saves */
299  virtual std::string create_initial_filename(unsigned int turn_number) const override;
300 
301  void write_game(config_writer &out) override;
302 };
303 
304 } //end of namespace savegame
Class for start-of-scenario saves.
Definition: savegame.hpp:292
The class for loading a savefile.
Definition: savegame.hpp:96
std::string filename
Name of the savefile to be loaded.
Definition: savegame.hpp:40
#define IMPLEMENT_LUA_JAILBREAK_EXCEPTION(type)
Helper macro for classes deriving from lua_jailbreak_exception.
bool cancel_orders
State of the "cancel_orders" checkbox in the load-game dialog.
Definition: savegame.hpp:49
const std::string & filename() const
Definition: savegame.hpp:171
logger & info()
Definition: log.cpp:90
Class for autosaves.
Definition: savegame.hpp:266
load_game_exception(const std::string &fname)
Definition: savegame.hpp:78
load_game_metadata load_data_
Primary output information.
Definition: savegame.hpp:140
Class for "normal" midgame saves.
Definition: savegame.hpp:239
compression::format compress_saves_
Determines if a confirmation of successful saving the game is shown.
Definition: savegame.hpp:234
Definitions for the interface to Wesnoth Markup Language (WML).
void set_title(const std::string &val)
Customize the dialog&#39;s caption.
Definition: savegame.hpp:283
load_game_metadata & data()
Definition: savegame.hpp:115
load_game_metadata data_
Definition: savegame.hpp:89
std::string filename_
Definition: action_wml.cpp:555
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
load_game_exception(load_game_metadata &&data)
Definition: savegame.hpp:84
const saved_game & gamestate() const
Definition: savegame.hpp:196
Class for writing a config out to a file in pieces.
bool disable_autosave
virtual ~loadgame()
Definition: savegame.hpp:100
saved_game & gamestate_
Definition: savegame.hpp:138
std::string create_filename() const
Build the filename according to the specific savegame&#39;s needs.
Definition: savegame.hpp:174
static bool is_replay_save(const config &cfg)
Definition: savegame.hpp:123
const config & game_config_
Definition: savegame.hpp:136
std::string error_message_
Definition: savegame.hpp:230
void convert_old_saves(config &cfg)
converts saves from older versions of wesnoth
Definition: savegame.cpp:818
std::unique_ptr< std::ostream > scoped_ostream
Definition: filesystem.hpp:40
Exception used to signal that the user has decided to abortt a game, and to load another game instead...
Definition: savegame.hpp:74
void clean_saves(const std::string &label)
Delete all autosaves of a certain scenario.
Definition: savegame.cpp:67
Game configuration data as global variables.
Definition: build_info.cpp:49
saved_game & gamestate_
Definition: savegame.hpp:228
std::string title_
Title of the savegame dialog.
Definition: savegame.hpp:208
std::string difficulty
The difficulty the save is meant to be loaded with.
Definition: savegame.hpp:43
load_game_metadata(const std::string &fname="", const std::string &hard="", bool replay=false, bool stop=false, bool change=false, const config &summary=config(), const config &info=config())
Definition: savegame.hpp:60
bool show_confirmation_
Error message to be displayed if the savefile could not be generated.
Definition: savegame.hpp:232
Declarations for File-IO.
config summary
Summary config of the save selected in the load game dialog.
Definition: savegame.hpp:55
Class for replay saves (either manually or automatically).
Definition: savegame.hpp:253
Represents version numbers.
void set_error_message(const std::string &error_message)
Customize the standard error message.
Definition: savegame.hpp:193
virtual ~savegame()
Definition: savegame.hpp:157
bool show_replay
State of the "show_replay" checkbox in the load-game dialog.
Definition: savegame.hpp:46
Filename and modification date for a file list.
Definition: save_index.hpp:24
bool select_difficulty
State of the "change_difficulty" checkbox in the load-game dialog.
Definition: savegame.hpp:52
const std::string & title() const
Definition: savegame.hpp:195
std::string filename_
Filename of the savegame file on disk.
Definition: savegame.hpp:205
bool save_game_exists(std::string name, compression::format compressed)
Returns true if there is already a savegame with that name.
Definition: savegame.cpp:61
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
Base class for exceptions that want to be thrown &#39;through&#39; lua.
config load_config
Config information of the savefile to be loaded.
Definition: savegame.hpp:58