aboutsummaryrefslogtreecommitdiff
path: root/common/serializer.h
blob: 803807814a20496726cfcd4330243ec889c2f408 (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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#ifndef COMMON_SERIALIZER_H
#define COMMON_SERIALIZER_H

#include "common/stream.h"
#include "common/str.h"

namespace Common {


#define SYNC_AS(SUFFIX,TYPE,SIZE) \
	template <typename T> \
	void syncAs ## SUFFIX(T &val) { \
		if (_loadStream) \
			val = static_cast<T>(_loadStream->read ## SUFFIX()); \
		else { \
			TYPE tmp = val; \
			_saveStream->write ## SUFFIX(tmp); \
		} \
		_bytesSynced += SIZE; \
	}


// TODO: Write comment for this
// TODO: Inspired by the SCUMM engine -- move to common/ code and use in more engines?
class Serializer {
public:
	Serializer(Common::SeekableReadStream *in, Common::WriteStream *out)
		: _loadStream(in), _saveStream(out), _bytesSynced(0) {
		assert(in || out);
	}

	bool isSaving() { return (_saveStream != 0); }
	bool isLoading() { return (_loadStream != 0); }

	/**
	 * Return the total number of bytes synced so far.
	 */
	uint bytesSynced() const { return _bytesSynced; }


	/**
	 * Skip a number of bytes in the data stream.
	 * This is useful to skip obsolete fields in old savestates.
	 */
	void skip(uint32 size) {
		_bytesSynced += size;
		if (_loadStream)
			_loadStream->skip(size);
		else {
			while (size--)
				_saveStream->writeByte(0);
		}
	}

	/**
	 * Sync a block of arbitrary fixed-length data.
	 */
	void syncBytes(byte *buf, uint32 size) {
		if (_loadStream)
			_loadStream->read(buf, size);
		else
			_saveStream->write(buf, size);
		_bytesSynced += size;
	}

	/**
	 * Sync a C-string, by treating it as a zero-terminated byte sequence.
	 */
	void syncString(Common::String &str) {
		if (_loadStream) {
			char c;
			str.clear();
			while ((c = _loadStream->readByte())) {
				str += c;
				_bytesSynced++;
			}
			_bytesSynced++;
		} else {
			_saveStream->writeString(str);
			_saveStream->writeByte(0);
			_bytesSynced += str.size() + 1;
		}
	}

	/**
	 * Sync a fixed length C-string
	 */
	void syncString(char *buf, uint16 size) {
		syncBytes((byte *)buf, size);
	}

	SYNC_AS(Byte, byte, 1)

	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)

protected:
	Common::SeekableReadStream *_loadStream;
	Common::WriteStream *_saveStream;

	uint _bytesSynced;
};

#undef SYNC_AS

// TODO: Make a subclass "VersionedSerializer", which makes it easy to support
//       multiple versions of a savegame format (again inspired by SCUMM).
/*
class VersionedSerializer : public Serializer {
public:
	// "version" is the version of the savegame we are loading/creating
	VersionedSerializer(Common::SeekableReadStream *in, Common::OutSaveFile *out, int version)
		: Serializer(in, out), _version(version) {
		assert(in || out);
	}

	void syncBytes(byte *buf, uint16 size, int minVersion = 0, int maxVersion = INF) {
		if (_version < minVersion || _version > maxVersion)
			return;	// Do nothing if too old or too new
		if (_loadStream) {
			_loadStream->read(buf, size);
		} else {
			_saveStream->write(buf, size);
		}
	}
	...

};

*/

// Mixin class / interface
// TODO Maybe call it ISerializable or SerializableMixin ?
// TODO: Taken from SCUMM engine -- move to common/ code?
class Serializable {
public:
	virtual ~Serializable() {}

	// Maybe rename this method to "syncWithSerializer" or "syncUsingSerializer" ?
	virtual void saveLoadWithSerializer(Serializer &ser) = 0;
};


} // end of namespace Common

#endif