1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef AGS_ENGINE_GAME_SAVEGAME_H
24 #define AGS_ENGINE_GAME_SAVEGAME_H
25 
26 #include "ags/lib/std/memory.h"
27 #include "ags/shared/core/platform.h"
28 #include "ags/shared/ac/game_version.h"
29 #include "ags/shared/util/error.h"
30 #include "ags/shared/util/version.h"
31 
32 namespace AGS3 {
33 namespace AGS {
34 
35 namespace Shared {
36 class Bitmap;
37 class Stream;
38 } // namespace Shared
39 
40 namespace Engine {
41 
42 using Shared::Bitmap;
43 using Shared::ErrorHandle;
44 using Shared::TypedCodeError;
45 using Shared::Stream;
46 using Shared::String;
47 using Shared::Version;
48 
49 typedef std::shared_ptr<Stream> PStream;
50 
51 //-----------------------------------------------------------------------------
52 // Savegame version history
53 //
54 // 8      last old style saved game format (of AGS 3.2.1)
55 // 9      first new style (self-descriptive block-based) format version
56 //-----------------------------------------------------------------------------
57 enum SavegameVersion {
58 	kSvgVersion_Undefined = 0,
59 	kSvgVersion_321 = 8,
60 	kSvgVersion_Components = 9,
61 	kSvgVersion_Cmp_64bit = 10,
62 	kSvgVersion_350_final = 11,
63 	kSvgVersion_350_final2 = 12,
64 	kSvgVersion_351 = 13,
65 	kSvgVersion_Current = kSvgVersion_351,
66 	kSvgVersion_LowestSupported = kSvgVersion_321 // change if support dropped
67 };
68 
69 // Error codes for save restoration routine
70 enum SavegameErrorType {
71 	kSvgErr_NoError,
72 	kSvgErr_FileOpenFailed,
73 	kSvgErr_SignatureFailed,
74 	kSvgErr_FormatVersionNotSupported,
75 	kSvgErr_IncompatibleEngine,
76 	kSvgErr_GameGuidMismatch,
77 	kSvgErr_ComponentListOpeningTagFormat,
78 	kSvgErr_ComponentListClosingTagMissing,
79 	kSvgErr_ComponentOpeningTagFormat,
80 	kSvgErr_ComponentClosingTagFormat,
81 	kSvgErr_ComponentSizeMismatch,
82 	kSvgErr_UnsupportedComponent,
83 	kSvgErr_ComponentSerialization,
84 	kSvgErr_ComponentUnserialization,
85 	kSvgErr_InconsistentFormat,
86 	kSvgErr_UnsupportedComponentVersion,
87 	kSvgErr_GameContentAssertion,
88 	kSvgErr_InconsistentData,
89 	kSvgErr_InconsistentPlugin,
90 	kSvgErr_DifferentColorDepth,
91 	kSvgErr_GameObjectInitFailed,
92 	kNumSavegameError
93 };
94 
95 String GetSavegameErrorText(SavegameErrorType err);
96 
97 typedef TypedCodeError<SavegameErrorType, GetSavegameErrorText> SavegameError;
98 typedef ErrorHandle<SavegameError> HSaveError;
99 typedef std::unique_ptr<Bitmap> UBitmap;
100 #ifdef UNUSED_AGS_PLATFORM_SCUMMVM
101 typedef std::shared_ptr<Stream> UStream;
102 #else
103 typedef std::unique_ptr<Stream> UStream;
104 #endif
105 
106 
107 // SavegameSource defines a successfully opened savegame stream
108 struct SavegameSource {
109 	// Signature of the current savegame format
110 	static const char *Signature;
111 	// Signature of the legacy savegame format
112 	static const char *LegacySignature;
113 
114 	// Name of the savefile
115 	String              Filename;
116 	// Savegame format version
117 	SavegameVersion     Version;
118 	// A ponter to the opened stream
119 	UStream             InputStream;
120 
121 	SavegameSource();
122 };
123 
124 // Supported elements of savegame description;
125 // these may be used as flags to define valid fields
126 enum SavegameDescElem {
127 	kSvgDesc_None = 0,
128 	kSvgDesc_EnvInfo = 0x0001,
129 	kSvgDesc_UserText = 0x0002,
130 	kSvgDesc_UserImage = 0x0004,
131 	kSvgDesc_All = kSvgDesc_EnvInfo | kSvgDesc_UserText | kSvgDesc_UserImage
132 };
133 
134 // SavegameDescription describes savegame with information about the enviroment
135 // it was created in, and custom data provided by user
136 struct SavegameDescription {
137 	// Name of the engine that saved the game
138 	String              EngineName;
139 	// Version of the engine that saved the game
140 	Version             EngineVersion;
141 	// Guid of the game which made this save
142 	String              GameGuid;
143 	// Legacy uniqueid of the game, for use in older games with no GUID
144 	int                 LegacyID;
145 	// Title of the game which made this save
146 	String              GameTitle;
147 	// Name of the main data file used; this is needed to properly
148 	// load saves made by "minigames"
149 	String              MainDataFilename;
150 	// Game's main data version; should be checked early to know
151 	// if the save was made for the supported game format
152 	GameDataVersion     MainDataVersion;
153 	// Native color depth of the game; this is required to
154 	// properly restore dynamic graphics from the save
155 	int                 ColorDepth;
156 
157 	String              UserText;
158 	UBitmap             UserImage;
159 
160 	SavegameDescription();
161 };
162 
163 
164 // Opens savegame for reading; optionally reads description, if any is provided
165 HSaveError     OpenSavegame(const String &filename, SavegameSource &src,
166                             SavegameDescription &desc, SavegameDescElem elems = kSvgDesc_All);
167 // Opens savegame and reads the savegame description
168 HSaveError     OpenSavegame(const String &filename, SavegameDescription &desc, SavegameDescElem elems = kSvgDesc_All);
169 
170 // Reads the game data from the save stream and reinitializes game state
171 HSaveError     RestoreGameState(Stream *in, SavegameVersion svg_version);
172 
173 // Opens savegame for writing and puts in savegame description
174 Stream *StartSavegame(const String &filename, const String &user_text, const Bitmap *user_image);
175 
176 // Prepares game for saving state and writes game data into the save stream
177 void           SaveGameState(Stream *out);
178 
179 } // namespace Engine
180 } // namespace AGS
181 } // namespace AGS3
182 
183 #endif
184