2002-07-02 20:57:27 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2002-2006 The ScummVM project
|
2002-07-02 20:57:27 +00:00
|
|
|
*
|
|
|
|
* 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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-07-02 20:57:27 +00:00
|
|
|
*
|
2006-02-11 09:55:41 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2002-07-02 20:57:27 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SAVELOAD_H
|
|
|
|
#define SAVELOAD_H
|
|
|
|
|
2003-08-01 12:21:04 +00:00
|
|
|
#include "common/scummsys.h"
|
2006-02-20 16:03:48 +00:00
|
|
|
#include <stddef.h>
|
2003-06-15 01:42:19 +00:00
|
|
|
|
2005-05-10 23:17:38 +00:00
|
|
|
namespace Common {
|
|
|
|
class InSaveFile;
|
|
|
|
class OutSaveFile;
|
|
|
|
}
|
2003-10-03 18:33:57 +00:00
|
|
|
|
|
|
|
namespace Scumm {
|
|
|
|
|
2002-12-08 16:14:29 +00:00
|
|
|
|
2005-04-26 11:10:27 +00:00
|
|
|
/**
|
|
|
|
* The current savegame format version.
|
2005-07-30 21:11:48 +00:00
|
|
|
* Our save/load system uses an elaborate scheme to allow us to modify the
|
2005-04-26 11:10:27 +00:00
|
|
|
* savegame while keeping full backward compatibility, in the sense that newer
|
|
|
|
* ScummVM versions always are able to load old savegames.
|
|
|
|
* In order to achieve that, we store a version in the savegame files, and whenever
|
|
|
|
* the savegame layout is modified, the version is incremented.
|
|
|
|
*
|
|
|
|
* This roughly works by marking each savegame entry with a range of versions
|
|
|
|
* for which it is valid; the save/load code iterates over all entries, but
|
|
|
|
* only saves/loads those which are valid for the version of the savegame
|
|
|
|
* which is being loaded/saved currently.
|
|
|
|
*/
|
2006-02-14 10:11:15 +00:00
|
|
|
#define CURRENT_VER 65
|
2002-12-08 16:14:29 +00:00
|
|
|
|
2005-04-26 11:10:27 +00:00
|
|
|
/**
|
|
|
|
* An auxillary macro, used to specify savegame versions. We use this instead
|
|
|
|
* of just writing the raw version, because this way they stand out more to
|
|
|
|
* the reading eye, making it a bit easier to navigate through the code.
|
|
|
|
*/
|
|
|
|
#define VER(x) x
|
|
|
|
|
2002-09-22 03:57:41 +00:00
|
|
|
|
2005-04-26 11:10:27 +00:00
|
|
|
/**
|
|
|
|
* The OFFS macro essentially provides the functionality of offsetof(), that
|
2005-07-30 21:11:48 +00:00
|
|
|
* is, it determines the offset of a struct/class member within instances of
|
2005-04-26 11:10:27 +00:00
|
|
|
* that class.
|
|
|
|
*
|
|
|
|
* This is a place where we cheat a bit and sacrifice some potential portability
|
|
|
|
* (although so far we haven't encountered any platform where this matters).
|
|
|
|
*
|
|
|
|
* To work around a warning in GCC 3.2 (and 3.1 ?) regarding non-POD types,
|
|
|
|
* we use a small trick: instead of 0 we use 42. Why? Well, it seems newer GCC
|
|
|
|
* versions have a heuristic built in to detect "offset-of" patterns - which is exactly
|
|
|
|
* what our OFFS macro does. Now, for non-POD types this is not really legal, because
|
|
|
|
* member need not be at a fixed offset relative to the variable, even if they are in
|
2005-07-30 21:11:48 +00:00
|
|
|
* current reality (many of our complex structs are non-POD; for an explanation of
|
2005-04-26 11:10:27 +00:00
|
|
|
* what POD means refer to http://www-cpd.fnal.gov/personal/wb/boost/ISOcxx/doc/POD.html)
|
|
|
|
*/
|
2006-02-20 16:03:48 +00:00
|
|
|
#define OFFS(type,item) (((ptrdiff_t)(&((type*)42)->type::item))-42)
|
2005-04-26 11:10:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to the OFFS macro, this macro computes the size (in bytes) of a
|
|
|
|
* member of a given struct/class type.
|
|
|
|
*/
|
2002-09-22 03:57:41 +00:00
|
|
|
#define SIZE(type,item) sizeof(((type*)42)->type::item)
|
2002-09-22 02:53:15 +00:00
|
|
|
|
2002-12-08 16:14:29 +00:00
|
|
|
// Any item that is still in use automatically gets a maxVersion equal to CURRENT_VER
|
|
|
|
#define MKLINE(type,item,saveas,minVer) {OFFS(type,item),saveas,SIZE(type,item),minVer,CURRENT_VER}
|
2003-05-23 02:58:34 +00:00
|
|
|
#define MKARRAY(type,item,saveas,dim,minVer) {OFFS(type,item),128|saveas,SIZE(type,item),minVer,CURRENT_VER}, {dim,1,0,0,0}
|
|
|
|
#define MKARRAY2(type,item,saveas,dim,dim2,rowlen,minVer) {OFFS(type,item),128|saveas,SIZE(type,item),minVer,CURRENT_VER}, {dim,dim2,rowlen,0,0}
|
2002-12-08 16:14:29 +00:00
|
|
|
|
|
|
|
// Use this if you have an entry that used to be smaller:
|
|
|
|
#define MKLINE_OLD(type,item,saveas,minVer,maxVer) {OFFS(type,item),saveas,SIZE(type,item),minVer,maxVer}
|
2003-05-23 02:58:34 +00:00
|
|
|
#define MKARRAY_OLD(type,item,saveas,dim,minVer,maxVer) {OFFS(type,item),128|saveas,SIZE(type,item),minVer,maxVer}, {dim,1,0,0,0}
|
|
|
|
#define MKARRAY2_OLD(type,item,saveas,dim,dim2,rowlen,minVer,maxVer) {OFFS(type,item),128|saveas,SIZE(type,item),minVer,maxVer}, {dim,dim2,rowlen,0,0}
|
2002-12-08 16:14:29 +00:00
|
|
|
|
|
|
|
// An obsolete item/array, to be ignored upon load. We retain the type/item params to make it easier to debug.
|
|
|
|
// Obsolete items have size == 0.
|
|
|
|
#define MK_OBSOLETE(type,item,saveas,minVer,maxVer) {0,saveas,0,minVer,maxVer}
|
2003-05-23 02:58:34 +00:00
|
|
|
#define MK_OBSOLETE_ARRAY(type,item,saveas,dim,minVer,maxVer) {0,128|saveas,0,minVer,maxVer}, {dim,1,0,0,0}
|
|
|
|
#define MK_OBSOLETE_ARRAY2(type,item,saveas,dim,dim2,rowlen,minVer,maxVer) {0,128|saveas,0,minVer,maxVer}, {dim,dim2,rowlen,0,0}
|
2002-12-08 16:14:29 +00:00
|
|
|
|
|
|
|
// End marker
|
|
|
|
#define MKEND() {0xFFFF,0xFF,0xFF,0,0}
|
|
|
|
|
2002-09-22 02:53:15 +00:00
|
|
|
|
2002-07-16 21:03:14 +00:00
|
|
|
enum {
|
|
|
|
sleByte = 1,
|
|
|
|
sleUint8 = 1,
|
|
|
|
sleInt8 = 1,
|
|
|
|
sleInt16 = 2,
|
|
|
|
sleUint16 = 3,
|
|
|
|
sleInt32 = 4,
|
|
|
|
sleUint32 = 5
|
|
|
|
};
|
|
|
|
|
2002-07-04 10:05:40 +00:00
|
|
|
struct SaveLoadEntry {
|
2003-05-23 02:58:34 +00:00
|
|
|
uint32 offs; // or: array dimension
|
|
|
|
uint16 type; // or: array dimension 2
|
|
|
|
uint16 size; // or: array row length
|
2002-12-08 16:14:29 +00:00
|
|
|
uint8 minVersion;
|
|
|
|
uint8 maxVersion;
|
2002-07-04 10:05:40 +00:00
|
|
|
};
|
|
|
|
|
2002-12-08 16:14:29 +00:00
|
|
|
class Serializer {
|
|
|
|
public:
|
2005-05-10 23:17:38 +00:00
|
|
|
Serializer(Common::InSaveFile *in, Common::OutSaveFile *out, uint32 savegameVersion)
|
2005-10-22 23:42:12 +00:00
|
|
|
: _loadStream(in), _saveStream(out),
|
2002-12-08 16:14:29 +00:00
|
|
|
_savegameVersion(savegameVersion)
|
|
|
|
{ }
|
2002-07-04 10:05:40 +00:00
|
|
|
|
|
|
|
void saveLoadArrayOf(void *b, int len, int datasize, byte filetype);
|
|
|
|
void saveLoadArrayOf(void *b, int num, int datasize, const SaveLoadEntry *sle);
|
2002-12-08 16:14:29 +00:00
|
|
|
void saveLoadEntries(void *d, const SaveLoadEntry *sle);
|
|
|
|
|
2005-04-10 15:13:40 +00:00
|
|
|
bool isSaving() { return (_saveStream != 0); }
|
|
|
|
bool isLoading() { return (_loadStream != 0); }
|
2002-12-21 20:10:47 +00:00
|
|
|
uint32 getVersion() { return _savegameVersion; }
|
2002-12-08 16:14:29 +00:00
|
|
|
|
2002-07-04 10:05:40 +00:00
|
|
|
void saveUint32(uint32 d);
|
2003-12-27 00:10:20 +00:00
|
|
|
void saveUint16(uint16 d);
|
2002-07-04 10:05:40 +00:00
|
|
|
void saveByte(byte b);
|
|
|
|
|
|
|
|
byte loadByte();
|
2003-12-27 00:10:20 +00:00
|
|
|
uint16 loadUint16();
|
2002-07-04 10:05:40 +00:00
|
|
|
uint32 loadUint32();
|
|
|
|
|
2002-12-08 16:14:29 +00:00
|
|
|
void saveBytes(void *b, int len);
|
|
|
|
void loadBytes(void *b, int len);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2002-12-08 16:14:29 +00:00
|
|
|
protected:
|
2005-05-10 23:17:38 +00:00
|
|
|
Common::InSaveFile *_loadStream;
|
|
|
|
Common::OutSaveFile *_saveStream;
|
2002-12-08 16:14:29 +00:00
|
|
|
uint32 _savegameVersion;
|
2002-07-04 10:05:40 +00:00
|
|
|
|
2002-12-08 16:14:29 +00:00
|
|
|
void saveArrayOf(void *b, int len, int datasize, byte filetype);
|
|
|
|
void loadArrayOf(void *b, int len, int datasize, byte filetype);
|
2002-07-04 10:05:40 +00:00
|
|
|
|
2002-12-08 16:14:29 +00:00
|
|
|
void saveEntries(void *d, const SaveLoadEntry *sle);
|
|
|
|
void loadEntries(void *d, const SaveLoadEntry *sle);
|
2002-07-04 10:05:40 +00:00
|
|
|
};
|
|
|
|
|
2005-10-21 23:01:13 +00:00
|
|
|
|
|
|
|
// Mixin class / interface. Maybe call it ISerializable or SerializableMixin ?
|
|
|
|
class Serializable {
|
|
|
|
public:
|
|
|
|
virtual ~Serializable() {}
|
|
|
|
virtual void saveLoadWithSerializer(Serializer *ser) = 0;
|
|
|
|
};
|
|
|
|
|
2003-10-03 18:33:57 +00:00
|
|
|
} // End of namespace Scumm
|
|
|
|
|
2002-07-02 20:57:27 +00:00
|
|
|
#endif
|