ScummVM API documentation
savefile.h
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 modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  *
21  * This file is dual-licensed.
22  * In addition to the GPLv3 license mentioned above, this code is also
23  * licensed under LGPL 2.1. See LICENSES/COPYING.LGPL file for the
24  * full text of the license.
25  *
26  */
27 
28 #ifndef GOB_SAVE_SAVEFILE_H
29 #define GOB_SAVE_SAVEFILE_H
30 
31 #include "common/endian.h"
32 #include "common/array.h"
33 #include "common/savefile.h"
34 
35 namespace Gob {
36 
37 class GobEngine;
38 class Surface;
39 
49 class SaveHeader {
50 public:
52  static const int kSize = 20;
53  static const uint32 kID1 = MKTAG(0,'S','C','V');
54  static const uint32 kID2 = MKTAG('M','G','O','B');
55 
56  SaveHeader(uint32 type = 0, uint32 version = 0, uint32 size = 0);
57 
58  bool operator==(const SaveHeader &header) const;
59  bool operator!=(const SaveHeader &header) const;
60 
62  bool read(Common::ReadStream &stream);
64  bool verify(Common::ReadStream &stream) const;
68  bool verifyReadSize(Common::ReadStream &stream);
70  bool write(Common::WriteStream &stream) const;
71 
72  uint32 getType() const;
73  uint32 getVersion() const;
74  uint32 getSize() const;
75 
76  void setType(uint32 type);
77  void setVersion(uint32 version);
78  void setSize(uint32 size);
79 
80 private:
82  uint32 _type;
84  uint32 _version;
86  uint32 _size;
87 };
88 
90 class SavePart {
91 public:
92  SavePart();
93  virtual ~SavePart();
94 
96  virtual uint32 getSize() const;
97 
99  virtual bool read(Common::ReadStream &stream) = 0;
101  virtual bool write(Common::WriteStream &stream) const = 0;
102 
103 protected:
104  SaveHeader _header;
105 };
106 
108 class SavePartMem : public SavePart {
109 public:
110  static const uint32 kVersion = 1;
111  static const uint32 kID = MKTAG('P','M','E','M');
112 
113  SavePartMem(uint32 size);
114  ~SavePartMem() override;
115 
116  bool read(Common::ReadStream &stream) override;
117  bool write(Common::WriteStream &stream) const override;
118 
120  bool readFrom(const byte *data, uint32 offset, uint32 size);
122  bool writeInto(byte *data, uint32 offset, uint32 size) const;
123 
124 private:
125  uint32 _size;
126  byte *_data;
127 };
128 
130 class SavePartVars : public SavePart {
131 public:
132  static const uint32 kVersion = 1;
133  static const uint32 kID = MKTAG('V','A','R','S');
134 
135  SavePartVars(GobEngine *vm, uint32 size);
136  ~SavePartVars() override;
137 
138  bool read(Common::ReadStream &stream) override;
139  bool write(Common::WriteStream &stream) const override;
140 
142  bool readFrom(uint32 var, uint32 offset, uint32 size);
144  bool writeInto(uint32 var, uint32 offset, uint32 size) const;
145 
147  bool readFromRaw(const byte *data, uint32 offset, uint32 size);
148 
150  bool writeIntoRaw(byte *data, uint32 offset, uint32 size) const;
151 
152  const byte* data() const { return _data; }
153 
154 private:
155  GobEngine *_vm;
156 
157  uint32 _size;
158  byte *_data;
159 };
160 
162 class SavePartSprite : public SavePart {
163 public:
164  static const uint32 kVersion = 2;
165  static const uint32 kID = MKTAG('S','P','R','T');
166 
167  SavePartSprite(uint32 width, uint32 height, bool trueColor = false);
168  ~SavePartSprite() override;
169 
170  bool read(Common::ReadStream &stream) override;
171  bool write(Common::WriteStream &stream) const override;
172 
174  bool readPalette(const byte *palette);
176  bool readSprite(const Surface &sprite);
177 
179  bool readSpriteRaw(const byte *data, uint32 size);
180 
182  bool writePalette(byte *palette) const;
184  bool writeSprite(Surface &sprite) const;
185 
186 private:
187  uint32 _width;
188  uint32 _height;
189 
190  uint32 _spriteSize;
191 
192  bool _oldFormat;
193  bool _trueColor;
194 
195  byte *_dataSprite;
196  byte *_dataPalette;
197 };
198 
200 class SavePartInfo : public SavePart {
201 public:
202  static const uint32 kVersion = 1;
203  static const uint32 kID = MKTAG('I','N','F','O');
204 
213  SavePartInfo(uint32 descMaxLength, uint32 gameID,
214  uint32 gameVersion, byte endian, uint32 varCount);
215  ~SavePartInfo() override;
216 
218  const char *getDesc() const;
220  uint32 getDescMaxLength() const;
221 
223  void setVarCount(uint32 varCount);
225  void setDesc(const char *desc = 0);
227  void setDesc(const byte *desc, uint32 size);
228 
229  bool read(Common::ReadStream &stream) override;
230  bool write(Common::WriteStream &stream) const override;
231 
232 private:
233  char *_desc;
234  uint32 _descMaxLength;
235  uint32 _gameID;
236  uint32 _gameVersion;
237  uint32 _varCount;
238  byte _endian;
239 };
240 
243 public:
244  static const uint32 kVersion = 1;
245  static const uint32 kID = MKTAG('C','O','N','T');
246 
252  SaveContainer(uint32 partCount, uint32 slot);
253  ~SaveContainer();
254 
255  uint32 getSlot() const;
256  uint32 getSize() const;
257 
259  bool hasAllParts() const;
260 
262  void clear();
263 
265  bool writePart(uint32 partN, const SavePart *part);
267  bool readPart(uint32 partN, SavePart *part) const;
269  bool readPartHeader(uint32 partN, SaveHeader *header) const;
270 
272  static bool isSave(Common::SeekableReadStream &stream);
273 
274 protected:
276  struct Part {
277  uint32 size;
278  byte *data;
279 
280  Part(uint32 s);
281  ~Part();
282 
283  Common::WriteStream *createWriteStream();
284  Common::ReadStream *createReadStream() const;
285  };
286 
288  struct PartInfo {
289  uint32 id;
290  uint32 offset;
291  uint32 size;
292  };
293 
294  typedef Common::Array<Part *>::iterator PartIterator;
295  typedef Common::Array<Part *>::const_iterator PartConstIterator;
296 
297  uint32 _partCount;
298  uint32 _slot;
299 
300  SaveHeader _header;
301  Common::Array<Part *> _parts;
302 
303  uint32 calcSize() const;
304 
305  bool read(Common::ReadStream &stream);
306  bool write(Common::WriteStream &stream) const;
307 
309  static Common::Array<PartInfo> *getPartsInfo(Common::SeekableReadStream &stream);
310 };
311 
313 class SaveReader : public SaveContainer {
314 public:
315  SaveReader(uint32 partCount, uint32 slot, const Common::String &fileName);
316  SaveReader(uint32 partCount, uint32 slot, Common::SeekableReadStream &stream);
317  ~SaveReader();
318 
319  bool load();
320 
321  bool readPart(uint32 partN, SavePart *part) const;
322  bool readPartHeader(uint32 partN, SaveHeader *header) const;
323 
325  static bool getInfo(Common::SeekableReadStream &stream, SavePartInfo &info);
327  static bool getInfo(const Common::String &fileName, SavePartInfo &info);
328 
329 protected:
330  Common::String _fileName;
332 
333  bool _loaded;
334 
335  static Common::InSaveFile *openSave(const Common::String &fileName);
336  Common::InSaveFile *openSave();
337 };
338 
340 class SaveWriter: public SaveContainer {
341 public:
342  SaveWriter(uint32 partCount, uint32 slot);
343  SaveWriter(uint32 partCount, uint32 slot, const Common::String &fileName);
344  ~SaveWriter();
345 
346  bool writePart(uint32 partN, const SavePart *part);
347 
348  bool save(Common::WriteStream &stream);
349  bool deleteFile();
350 
351 protected:
352  bool save();
353 
354  Common::String _fileName;
355 
357  bool canSave() const;
358 
359  static Common::OutSaveFile *openSave(const Common::String &fileName);
360  Common::OutSaveFile *openSave();
361 };
362 
363 } // End of namespace Gob
364 
365 #endif // GOB_SAVE_SAVEFILE_H
Definition: gob.h:162
Definition: str.h:59
Definition: savefile.h:313
bool write(Common::WriteStream &stream) const
Definition: stream.h:77
Definition: savefile.h:54
Definition: savefile.h:340
static const int kSize
Definition: savefile.h:52
Definition: array.h:52
Definition: savefile.h:108
T * iterator
Definition: array.h:54
Definition: savefile.h:130
Definition: stream.h:745
Definition: savefile.h:242
bool verifyReadSize(Common::ReadStream &stream)
Definition: savefile.h:200
Definition: savefile.h:276
Definition: savefile.h:49
Definition: anifile.h:40
bool verify(Common::ReadStream &stream) const
const T * const_iterator
Definition: array.h:55
Definition: savefile.h:90
Definition: savefile.h:162
Definition: surface.h:100
#define MKTAG(a0, a1, a2, a3)
Definition: endian.h:188
bool read(Common::ReadStream &stream)
Definition: stream.h:385
Definition: savefile.h:288