2008-07-23 09:02:47 +00:00
|
|
|
/* 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.
|
|
|
|
*
|
2021-12-26 17:47:58 +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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2008-07-23 09:02:47 +00:00
|
|
|
* 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.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2008-07-23 09:02:47 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-12-26 17:47:58 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-07-23 09:02:47 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
#ifndef COMMON_SERIALIZER_H
|
|
|
|
#define COMMON_SERIALIZER_H
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
#include "common/stream.h"
|
|
|
|
#include "common/str.h"
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
namespace Common {
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2020-07-08 21:30:36 +00:00
|
|
|
/**
|
|
|
|
* @defgroup common_serializer Serializer
|
|
|
|
* @ingroup common
|
|
|
|
*
|
|
|
|
* @brief API for serializing data.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2017-11-29 18:05:59 +00:00
|
|
|
#define VER(x) Common::Serializer::Version(x)
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
#define SYNC_AS(SUFFIX,TYPE,SIZE) \
|
2012-02-11 03:14:48 +00:00
|
|
|
template<typename T> \
|
2009-05-19 11:23:13 +00:00
|
|
|
void syncAs ## SUFFIX(T &val, Version minVersion = 0, Version maxVersion = kLastVersion) { \
|
|
|
|
if (_version < minVersion || _version > maxVersion) \
|
2018-04-15 12:00:56 +00:00
|
|
|
return; \
|
2008-07-23 09:02:47 +00:00
|
|
|
if (_loadStream) \
|
|
|
|
val = static_cast<T>(_loadStream->read ## SUFFIX()); \
|
|
|
|
else { \
|
|
|
|
TYPE tmp = val; \
|
|
|
|
_saveStream->write ## SUFFIX(tmp); \
|
|
|
|
} \
|
|
|
|
_bytesSynced += SIZE; \
|
|
|
|
}
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2017-11-29 06:04:18 +00:00
|
|
|
#define SYNC_PRIMITIVE(suffix) \
|
|
|
|
template <typename T> \
|
|
|
|
static inline void suffix(Serializer &s, T &value) { \
|
|
|
|
s.syncAs##suffix(value); \
|
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2009-05-19 11:23:13 +00:00
|
|
|
/**
|
|
|
|
* This class allows syncing / serializing data (primarily game savestates)
|
|
|
|
* between memory and Read/WriteStreams.
|
|
|
|
* It optionally supports versioning the serialized data (client code must
|
|
|
|
* use the syncVersion() method for this). This makes it possible to support
|
|
|
|
* multiple versions of a savegame format with a single codepath
|
|
|
|
*
|
|
|
|
* This class was heavily inspired by the save/load code in the SCUMM engine.
|
|
|
|
*
|
|
|
|
* @todo Maybe rename this to Synchronizer?
|
|
|
|
*
|
|
|
|
* @todo One feature the SCUMM code has but that is missing here: Support for
|
|
|
|
* syncing arrays of a given type and *fixed* size; and also support
|
|
|
|
* for when the array size changed between versions. Also, support for
|
|
|
|
* 2D-arrays.
|
|
|
|
*
|
|
|
|
* @todo Proper error handling!
|
|
|
|
*/
|
2008-07-23 09:02:47 +00:00
|
|
|
class Serializer {
|
2009-05-19 11:23:13 +00:00
|
|
|
public:
|
|
|
|
typedef uint32 Version;
|
|
|
|
static const Version kLastVersion = 0xFFFFFFFF;
|
|
|
|
|
2017-11-29 06:04:18 +00:00
|
|
|
SYNC_PRIMITIVE(Uint32LE)
|
|
|
|
SYNC_PRIMITIVE(Uint32BE)
|
|
|
|
SYNC_PRIMITIVE(Sint32LE)
|
|
|
|
SYNC_PRIMITIVE(Sint32BE)
|
2021-03-31 20:25:22 +00:00
|
|
|
SYNC_PRIMITIVE(FloatLE)
|
|
|
|
SYNC_PRIMITIVE(FloatBE)
|
|
|
|
SYNC_PRIMITIVE(DoubleLE)
|
|
|
|
SYNC_PRIMITIVE(DoubleBE)
|
2017-11-29 06:04:18 +00:00
|
|
|
SYNC_PRIMITIVE(Uint16LE)
|
|
|
|
SYNC_PRIMITIVE(Uint16BE)
|
|
|
|
SYNC_PRIMITIVE(Sint16LE)
|
|
|
|
SYNC_PRIMITIVE(Sint16BE)
|
|
|
|
SYNC_PRIMITIVE(Byte)
|
|
|
|
SYNC_PRIMITIVE(SByte)
|
|
|
|
|
2009-05-19 11:23:13 +00:00
|
|
|
protected:
|
2011-08-06 07:47:19 +00:00
|
|
|
SeekableReadStream *_loadStream;
|
|
|
|
WriteStream *_saveStream;
|
2009-05-19 11:23:13 +00:00
|
|
|
|
|
|
|
uint _bytesSynced;
|
|
|
|
|
|
|
|
Version _version;
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
public:
|
2011-08-06 07:47:19 +00:00
|
|
|
Serializer(SeekableReadStream *in, WriteStream *out)
|
2009-05-19 11:23:13 +00:00
|
|
|
: _loadStream(in), _saveStream(out), _bytesSynced(0), _version(0) {
|
2008-07-23 09:02:47 +00:00
|
|
|
assert(in || out);
|
|
|
|
}
|
2009-05-19 11:23:13 +00:00
|
|
|
virtual ~Serializer() {}
|
|
|
|
|
|
|
|
inline bool isSaving() { return (_saveStream != 0); }
|
|
|
|
inline bool isLoading() { return (_loadStream != 0); }
|
|
|
|
|
2009-11-06 17:21:43 +00:00
|
|
|
SYNC_AS(Byte, byte, 1)
|
2017-11-29 06:04:18 +00:00
|
|
|
SYNC_AS(SByte, int8, 1)
|
2009-11-06 17:21:43 +00:00
|
|
|
|
|
|
|
SYNC_AS(Uint16LE, uint16, 2)
|
|
|
|
SYNC_AS(Uint16BE, uint16, 2)
|
|
|
|
SYNC_AS(Sint16LE, int16, 2)
|
|
|
|
SYNC_AS(Sint16BE, int16, 2)
|
|
|
|
|
|
|
|
SYNC_AS(Uint32LE, uint32, 4)
|
|
|
|
SYNC_AS(Uint32BE, uint32, 4)
|
|
|
|
SYNC_AS(Sint32LE, int32, 4)
|
|
|
|
SYNC_AS(Sint32BE, int32, 4)
|
2021-03-31 20:25:22 +00:00
|
|
|
SYNC_AS(FloatLE, float, 4)
|
|
|
|
SYNC_AS(FloatBE, float, 4)
|
2022-06-17 23:24:36 +00:00
|
|
|
|
|
|
|
SYNC_AS(DoubleLE, double, 8)
|
|
|
|
SYNC_AS(DoubleBE, double, 8)
|
2009-11-06 17:21:43 +00:00
|
|
|
|
2009-05-19 11:23:13 +00:00
|
|
|
/**
|
|
|
|
* Returns true if an I/O failure occurred.
|
|
|
|
* This flag is never cleared automatically. In order to clear it,
|
|
|
|
* client code has to call clearErr() explicitly.
|
|
|
|
*/
|
|
|
|
bool err() const {
|
|
|
|
if (_saveStream)
|
|
|
|
return _saveStream->err();
|
|
|
|
else
|
|
|
|
return _loadStream->err();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the I/O error status as returned by err().
|
|
|
|
*/
|
|
|
|
void clearErr() {
|
|
|
|
if (_saveStream)
|
|
|
|
_saveStream->clearErr();
|
|
|
|
else
|
|
|
|
_loadStream->clearErr();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync the "version" of the savegame we are loading/creating.
|
|
|
|
* @param currentVersion current format version, used when writing a new file
|
|
|
|
* @return true if the version of the savestate is not too new.
|
|
|
|
*/
|
|
|
|
bool syncVersion(Version currentVersion) {
|
|
|
|
_version = currentVersion;
|
|
|
|
syncAsUint32LE(_version);
|
|
|
|
return _version <= currentVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the version of the savestate being serialized. Useful if the engine
|
|
|
|
* needs to perform additional adjustments when loading old savestates.
|
|
|
|
*/
|
|
|
|
Version getVersion() const { return _version; }
|
|
|
|
|
2017-08-07 23:07:25 +00:00
|
|
|
/**
|
|
|
|
* Manually set the version
|
|
|
|
*/
|
|
|
|
void setVersion(Version version) { _version = version; }
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2009-03-24 11:30:52 +00:00
|
|
|
/**
|
|
|
|
* Return the total number of bytes synced so far.
|
|
|
|
*/
|
2008-07-23 09:02:47 +00:00
|
|
|
uint bytesSynced() const { return _bytesSynced; }
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2009-03-24 11:30:52 +00:00
|
|
|
/**
|
|
|
|
* Skip a number of bytes in the data stream.
|
|
|
|
* This is useful to skip obsolete fields in old savestates.
|
|
|
|
*/
|
2009-05-19 11:23:13 +00:00
|
|
|
void skip(uint32 size, Version minVersion = 0, Version maxVersion = kLastVersion) {
|
|
|
|
if (_version < minVersion || _version > maxVersion)
|
2018-04-15 12:00:56 +00:00
|
|
|
return; // Ignore anything which is not supposed to be present in this save game version
|
2009-05-19 11:23:13 +00:00
|
|
|
|
2009-03-24 11:30:52 +00:00
|
|
|
_bytesSynced += size;
|
2009-05-19 11:23:13 +00:00
|
|
|
if (isLoading())
|
2009-03-24 11:30:52 +00:00
|
|
|
_loadStream->skip(size);
|
|
|
|
else {
|
|
|
|
while (size--)
|
|
|
|
_saveStream->writeByte(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync a block of arbitrary fixed-length data.
|
|
|
|
*/
|
2009-05-19 11:23:13 +00:00
|
|
|
void syncBytes(byte *buf, uint32 size, Version minVersion = 0, Version maxVersion = kLastVersion) {
|
|
|
|
if (_version < minVersion || _version > maxVersion)
|
2018-04-15 12:00:56 +00:00
|
|
|
return; // Ignore anything which is not supposed to be present in this save game version
|
2009-05-19 11:23:13 +00:00
|
|
|
|
|
|
|
if (isLoading())
|
2008-07-23 09:02:47 +00:00
|
|
|
_loadStream->read(buf, size);
|
|
|
|
else
|
|
|
|
_saveStream->write(buf, size);
|
|
|
|
_bytesSynced += size;
|
|
|
|
}
|
|
|
|
|
2009-05-26 11:31:45 +00:00
|
|
|
/**
|
|
|
|
* Sync a 'magic id' of up to 256 bytes, and return whether it matched.
|
|
|
|
* When saving, this will simply write out the magic id and return true.
|
|
|
|
* When loading, this will read the specified number of bytes, compare it
|
|
|
|
* to the given magic id and return true on a match, false otherwise.
|
|
|
|
*
|
|
|
|
* A typical magic id is a FOURCC like 'MAGI'.
|
|
|
|
*
|
|
|
|
* @param magic magic id as a byte sequence
|
|
|
|
* @param size length of the magic id in bytes
|
|
|
|
* @return true if the magic id matched, false otherwise
|
|
|
|
*/
|
|
|
|
bool matchBytes(const char *magic, byte size, Version minVersion = 0, Version maxVersion = kLastVersion) {
|
|
|
|
if (_version < minVersion || _version > maxVersion)
|
2018-04-15 12:00:56 +00:00
|
|
|
return true; // Ignore anything which is not supposed to be present in this save game version
|
2009-05-26 11:31:45 +00:00
|
|
|
|
|
|
|
bool match;
|
|
|
|
if (isSaving()) {
|
|
|
|
_saveStream->write(magic, size);
|
|
|
|
match = true;
|
|
|
|
} else {
|
|
|
|
char buf[256];
|
|
|
|
_loadStream->read(buf, size);
|
|
|
|
match = (0 == memcmp(buf, magic, size));
|
|
|
|
}
|
|
|
|
_bytesSynced += size;
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
/**
|
|
|
|
* Sync a C-string, by treating it as a zero-terminated byte sequence.
|
2009-05-19 11:23:13 +00:00
|
|
|
* @todo Replace this method with a special Syncer class for Common::String
|
2009-03-15 20:30:57 +00:00
|
|
|
*/
|
2011-08-06 07:47:19 +00:00
|
|
|
void syncString(String &str, Version minVersion = 0, Version maxVersion = kLastVersion) {
|
2009-05-19 11:23:13 +00:00
|
|
|
if (_version < minVersion || _version > maxVersion)
|
2018-04-15 12:00:56 +00:00
|
|
|
return; // Ignore anything which is not supposed to be present in this save game version
|
2009-05-19 11:23:13 +00:00
|
|
|
|
|
|
|
if (isLoading()) {
|
2009-03-15 20:30:57 +00:00
|
|
|
char c;
|
|
|
|
str.clear();
|
|
|
|
while ((c = _loadStream->readByte())) {
|
|
|
|
str += c;
|
|
|
|
_bytesSynced++;
|
|
|
|
}
|
|
|
|
_bytesSynced++;
|
|
|
|
} else {
|
|
|
|
_saveStream->writeString(str);
|
|
|
|
_saveStream->writeByte(0);
|
|
|
|
_bytesSynced += str.size() + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 22:22:25 +00:00
|
|
|
/**
|
|
|
|
* Sync a U32-string
|
|
|
|
*/
|
|
|
|
void syncString32(U32String &str, Version minVersion = 0, Version maxVersion = kLastVersion) {
|
|
|
|
if (_version < minVersion || _version > maxVersion)
|
|
|
|
return; // Ignore anything which is not supposed to be present in this save game version
|
|
|
|
|
|
|
|
uint32 len = str.size();
|
|
|
|
|
|
|
|
syncAsUint32LE(len);
|
|
|
|
|
|
|
|
if (isLoading()) {
|
|
|
|
U32String::value_type *sl = new U32String::value_type[len];
|
|
|
|
for (uint i = 0; i < len; i++)
|
|
|
|
syncAsUint32LE(sl[i]);
|
|
|
|
str = U32String(sl, len);
|
2022-09-18 18:55:16 +00:00
|
|
|
delete[] sl;
|
2020-10-27 22:22:25 +00:00
|
|
|
} else {
|
|
|
|
for (uint i = 0; i < len; i++)
|
|
|
|
_saveStream->writeUint32LE(str[i]);
|
|
|
|
_bytesSynced += 4 * len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 06:04:18 +00:00
|
|
|
template <typename T>
|
|
|
|
void syncArray(T *arr, size_t entries, void (*serializer)(Serializer &, T &), Version minVersion = 0, Version maxVersion = kLastVersion) {
|
|
|
|
if (_version < minVersion || _version > maxVersion)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < entries; ++i) {
|
|
|
|
serializer(*this, arr[i]);
|
|
|
|
}
|
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
};
|
|
|
|
|
2017-11-29 06:04:18 +00:00
|
|
|
#undef SYNC_PRIMITIVE
|
2008-07-23 09:02:47 +00:00
|
|
|
#undef SYNC_AS
|
|
|
|
|
|
|
|
|
|
|
|
// Mixin class / interface
|
2009-05-19 11:23:13 +00:00
|
|
|
// TODO: Maybe rename this to Syncable ?
|
2008-07-23 09:02:47 +00:00
|
|
|
class Serializable {
|
|
|
|
public:
|
|
|
|
virtual ~Serializable() {}
|
2009-03-15 20:30:57 +00:00
|
|
|
|
|
|
|
// Maybe rename this method to "syncWithSerializer" or "syncUsingSerializer" ?
|
|
|
|
virtual void saveLoadWithSerializer(Serializer &ser) = 0;
|
2008-07-23 09:02:47 +00:00
|
|
|
};
|
|
|
|
|
2020-07-08 21:30:36 +00:00
|
|
|
/** @} */
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2009-10-04 21:26:33 +00:00
|
|
|
} // End of namespace Common
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
#endif
|