aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/save/savefile.h
blob: 01825207e04b6567bb7755a43200d9dd67a2b186 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef GOB_SAVE_SAVEFILE_H
#define GOB_SAVE_SAVEFILE_H

#include "common/endian.h"
#include "common/array.h"
#include "common/savefile.h"

namespace Gob {

class GobEngine;
class Surface;

/**
 * A class wrapping a save part header.
 *
 * A save part header consists of 4 fields:
 * ID      : The 8 character ID \0SCVMGOB
 * Type    : The 4 character ID for this part's type
 * Version : This part's version. Each type has its own version counter
 * Size    : The size of the contents, i.e. excluding this header
*/
class SaveHeader {
public:
	/** The size of the header. */
	static const int kSize = 20;
	static const uint32 kID1 = MKTAG(0,'S','C','V');
	static const uint32 kID2 = MKTAG('M','G','O','B');

	SaveHeader(uint32 type = 0, uint32 version = 0, uint32 size = 0);

	bool operator==(const SaveHeader &header) const;
	bool operator!=(const SaveHeader &header) const;

	/** Read the header out of a stream into this class. */
	bool read(Common::ReadStream &stream);
	/** Read the header out of a stream and checks it against this class's contents. */
	bool verify(Common::ReadStream &stream) const;
	/** Read the header out of a stream and checks it against this class's contents,
	 *  but read the size field instead.
	 */
	bool verifyReadSize(Common::ReadStream &stream);
	/** Write this class's contents into a stream. */
	bool write(Common::WriteStream &stream) const;

	uint32 getType() const;
	uint32 getVersion() const;
	uint32 getSize() const;

	void setType(uint32 type);
	void setVersion(uint32 version);
	void setSize(uint32 size);

private:
	/** An ID specifying the part's type. */
	uint32 _type;
	/** The part's version. */
	uint32 _version;
	/** The size of the contents. */
	uint32 _size;
};

/** An abstract class for a part in a save file. */
class SavePart {
public:
	SavePart();
	virtual ~SavePart();

	/** Return the total size of the part. */
	virtual uint32 getSize() const;

	/** Read the part (with header) out of the stream. */
	virtual bool read(Common::ReadStream &stream) = 0;
	/** Write the part (with header) into the stream. */
	virtual bool write(Common::WriteStream &stream) const = 0;

protected:
	SaveHeader _header;
};

/** A save part consisting of plain memory. */
class SavePartMem : public SavePart {
public:
	static const uint32 kVersion = 1;
	static const uint32 kID = MKTAG('P','M','E','M');

	SavePartMem(uint32 size);
	~SavePartMem();

	bool read(Common::ReadStream &stream);
	bool write(Common::WriteStream &stream) const;

	/** Read size bytes of data into the part at the specified offset. */
	bool readFrom(const byte *data, uint32 offset, uint32 size);
	/** Write size bytes of the part at the specified offset int data. */
	bool writeInto(byte *data, uint32 offset, uint32 size) const;

private:
	uint32 _size;
	byte *_data;
};

/** A save part holding script variables. */
class SavePartVars : public SavePart {
public:
	static const uint32 kVersion = 1;
	static const uint32 kID = MKTAG('V','A','R','S');

	SavePartVars(GobEngine *vm, uint32 size);
	~SavePartVars();

	bool read(Common::ReadStream &stream);
	bool write(Common::WriteStream &stream) const;

	/** Read size bytes of variables starting at var into the part at the specified offset. */
	bool readFrom(uint32 var, uint32 offset, uint32 size);
	/** Write size bytes of the part at the specified offset into the variable starting at var. */
	bool writeInto(uint32 var, uint32 offset, uint32 size) const;

	/** Read size bytes of raw data into the part. */
	bool readFromRaw(const byte *data, uint32 size);

private:
	GobEngine *_vm;

	uint32 _size;
	byte *_data;
};

/** A save part holding a sprite. */
class SavePartSprite : public SavePart {
public:
	static const uint32 kVersion = 2;
	static const uint32 kID = MKTAG('S','P','R','T');

	SavePartSprite(uint32 width, uint32 height, bool trueColor = false);
	~SavePartSprite();

	bool read(Common::ReadStream &stream);
	bool write(Common::WriteStream &stream) const;

	/** Read a palette into the part. */
	bool readPalette(const byte *palette);
	/** Read a sprite into the part. */
	bool readSprite(const Surface &sprite);

	/** Read size bytes of raw data into the sprite. */
	bool readSpriteRaw(const byte *data, uint32 size);

	/** Write a palette out of the part. */
	bool writePalette(byte *palette) const;
	/** Write a sprite out of the part. */
	bool writeSprite(Surface &sprite) const;

private:
	uint32 _width;
	uint32 _height;

	uint32 _spriteSize;

	bool _oldFormat;
	bool _trueColor;

	byte *_dataSprite;
	byte *_dataPalette;
};

/** A save part containing informations about the save's game. */
class SavePartInfo : public SavePart {
public:
	static const uint32 kVersion = 1;
	static const uint32 kID = MKTAG('I','N','F','O');

	/**
	 * The constructor.
	 * @param descMaxLength The maximal number of bytes that fit into the description.
	 * @param gameID An ID for the game (Gob1, Gob2, Gob3, ...).
	 * @param gameVersion An ID for game specific versioning
	 * @param endian Endianness of the platform the game originally ran on.
	 * @param varCount The number of script variables.
	 */
	SavePartInfo(uint32 descMaxLength, uint32 gameID,
			uint32 gameVersion, byte endian, uint32 varCount);
	~SavePartInfo();

	/** Return the save's description. */
	const char *getDesc() const;
	/** Return the description's maximal length. */
	uint32 getDescMaxLength() const;

	/** Set the variable count. */
	void setVarCount(uint32 varCount);
	/** Set the save's description. */
	void setDesc(const char *desc = 0);
	/** Set the save's description. */
	void setDesc(const byte *desc, uint32 size);

	bool read(Common::ReadStream &stream);
	bool write(Common::WriteStream &stream) const;

private:
	char *_desc;
	uint32 _descMaxLength;
	uint32 _gameID;
	uint32 _gameVersion;
	uint32 _varCount;
	byte _endian;
};

/** A container for several save parts. */
class SaveContainer {
public:
	static const uint32 kVersion = 1;
	static const uint32 kID = MKTAG('C','O','N','T');

	/**
	 * The constructor.
	 * @param partCount The number parts this container shall hold.
	 * @param slot The save slot this save's for.
	 */
	SaveContainer(uint32 partCount, uint32 slot);
	~SaveContainer();

	uint32 getSlot() const;
	uint32 getSize() const;

	/** All parts filled? */
	bool hasAllParts() const;

	/** Empty all parts. */
	void clear();

	/** Write a SavePart into the container's part. */
	bool writePart(uint32 partN, const SavePart *part);
	/** Read the container's part's content into a SavePart. */
	bool readPart(uint32 partN, SavePart *part) const;
	/** Read only the container's part's header. */
	bool readPartHeader(uint32 partN, SaveHeader *header) const;

	/** Checks if the stream is a valid save container. */
	static bool isSave(Common::SeekableReadStream &stream);

protected:
	/** A part. */
	struct Part {
		uint32 size;
		byte *data;

		Part(uint32 s);
		~Part();

		Common::WriteStream *createWriteStream();
		Common::ReadStream *createReadStream() const;
	};

	/** Basic information about a part. */
	struct PartInfo {
		uint32 id;
		uint32 offset;
		uint32 size;
	};

	typedef Common::Array<Part *>::iterator PartIterator;
	typedef Common::Array<Part *>::const_iterator PartConstIterator;

	uint32 _partCount;
	uint32 _slot;

	SaveHeader _header;
	Common::Array<Part *> _parts;

	uint32 calcSize() const;

	bool read(Common::ReadStream &stream);
	bool write(Common::WriteStream &stream) const;

	/** Get an array containing basic information about all parts in the container in the stream. */
	static Common::Array<PartInfo> *getPartsInfo(Common::SeekableReadStream &stream);
};

/** Reads a save. */
class SaveReader : public SaveContainer {
public:
	SaveReader(uint32 partCount, uint32 slot, const Common::String &fileName);
	SaveReader(uint32 partCount, uint32 slot, Common::SeekableReadStream &stream);
	~SaveReader();

	bool load();

	bool readPart(uint32 partN, SavePart *part) const;
	bool readPartHeader(uint32 partN, SaveHeader *header) const;

	/** Find and read the save's info part. */
	static bool getInfo(Common::SeekableReadStream &stream, SavePartInfo &info);
	/** Find and read the save's info part. */
	static bool getInfo(const Common::String &fileName, SavePartInfo &info);

protected:
	Common::String _fileName;
	Common::SeekableReadStream *_stream;

	bool _loaded;

	static Common::InSaveFile *openSave(const Common::String &fileName);
	Common::InSaveFile *openSave();
};

/** Writes a save. */
class SaveWriter: public SaveContainer {
public:
	SaveWriter(uint32 partCount, uint32 slot);
	SaveWriter(uint32 partCount, uint32 slot, const Common::String &fileName);
	~SaveWriter();

	bool writePart(uint32 partN, const SavePart *part);

	bool save(Common::WriteStream &stream);

protected:
	bool save();

	Common::String _fileName;

	/** Is everything ready for saving? */
	bool canSave() const;

	static Common::OutSaveFile *openSave(const Common::String &fileName);
	Common::OutSaveFile *openSave();
};

} // End of namespace Gob

#endif // GOB_SAVE_SAVEFILE_H