File: savegame.cpp

package info (click to toggle)
residualvm 0.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 31,292 kB
  • sloc: cpp: 227,029; sh: 7,256; xml: 1,731; perl: 1,067; java: 861; asm: 738; python: 691; ansic: 272; makefile: 139; objc: 81; sed: 22; php: 1
file content (350 lines) | stat: -rw-r--r-- 9,346 bytes parent folder | download | duplicates (2)
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
/* ResidualVM - A 3D game interpreter
 *
 * ResidualVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the AUTHORS
 * 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.
 *
 */

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

#include "math/vector3d.h"

#include "engines/grim/savegame.h"
#include "engines/grim/color.h"

namespace Grim {

#define SAVEGAME_HEADERTAG  'RSAV'
#define SAVEGAME_FOOTERTAG  'ESAV'

uint SaveGame::SAVEGAME_MAJOR_VERSION = 22;
uint SaveGame::SAVEGAME_MINOR_VERSION = 27;

SaveGame *SaveGame::openForLoading(const Common::String &filename) {
	Common::InSaveFile *inSaveFile = g_system->getSavefileManager()->openForLoading(filename);
	if (!inSaveFile) {
		warning("SaveGame::openForLoading() Error opening savegame file %s", filename.c_str());
		return nullptr;
	}

	SaveGame *save = new SaveGame();

	save->_saving = false;
	save->_inSaveFile = inSaveFile;

	uint32 tag = inSaveFile->readUint32BE();
	if (tag != SAVEGAME_HEADERTAG) {
		delete save;
		return nullptr;
	}
	save->_majorVersion = inSaveFile->readUint32BE();
	save->_minorVersion = inSaveFile->readUint32BE();

	return save;
}

SaveGame *SaveGame::openForSaving(const Common::String &filename) {
	Common::OutSaveFile *outSaveFile =  g_system->getSavefileManager()->openForSaving(filename);
	if (!outSaveFile) {
		warning("SaveGame::openForSaving() Error creating savegame file %s", filename.c_str());
		return nullptr;
	}

	SaveGame *save = new SaveGame();

	save->_saving = true;
	save->_outSaveFile = outSaveFile;

	outSaveFile->writeUint32BE(SAVEGAME_HEADERTAG);
	outSaveFile->writeUint32BE(SAVEGAME_MAJOR_VERSION);
	outSaveFile->writeUint32BE(SAVEGAME_MINOR_VERSION);

	save->_majorVersion = SAVEGAME_MAJOR_VERSION;
	save->_minorVersion = SAVEGAME_MINOR_VERSION;

	return save;
}

SaveGame::SaveGame() :
		_currentSection(0), _sectionBuffer(nullptr), _majorVersion(0),
		_minorVersion(0), _saving(false), _inSaveFile(nullptr), _outSaveFile(nullptr),
		_sectionSize(0), _sectionAlloc(0), _sectionPtr(0) {

}

SaveGame::~SaveGame() {
	if (_saving) {
		_outSaveFile->writeUint32BE(SAVEGAME_FOOTERTAG);
		_outSaveFile->finalize();
		if (_outSaveFile->err())
			warning("SaveGame::~SaveGame() Can't write file. (Disk full?)");
		delete _outSaveFile;
	} else {
		delete _inSaveFile;
	}
	free(_sectionBuffer);
}

bool SaveGame::isCompatible() const {
	return _majorVersion == SAVEGAME_MAJOR_VERSION && _minorVersion <= SAVEGAME_MINOR_VERSION;
}

uint SaveGame::saveMajorVersion() const {
	return _majorVersion;
}

uint SaveGame::saveMinorVersion() const {
	return _minorVersion;
}

uint32 SaveGame::beginSection(uint32 sectionTag) {
	assert(_majorVersion == SAVEGAME_MAJOR_VERSION);

	if (_currentSection != 0)
		error("Tried to begin a new save game section with ending old section");
	_currentSection = sectionTag;
	_sectionSize = 0;
	if (!_saving) {
		uint32 tag = 0;

		while (tag != sectionTag) {
			tag = _inSaveFile->readUint32BE();
			if (tag == SAVEGAME_FOOTERTAG)
				error("Unable to find requested section of savegame");
			_sectionSize = _inSaveFile->readUint32BE();
			_inSaveFile->seek(_sectionSize, SEEK_CUR);
		}
		if (!_sectionBuffer || _sectionAlloc < _sectionSize) {
			_sectionAlloc = _sectionSize;
			byte *buff = (byte *)realloc(_sectionBuffer, _sectionAlloc);
			if (buff == nullptr) {
				free(_sectionBuffer);
				error("Could not allocate memory for save game");
			}
			_sectionBuffer = buff;
		}

		_inSaveFile->seek(-(int32)_sectionSize, SEEK_CUR);
		_inSaveFile->read(_sectionBuffer, _sectionSize);

	} else {
		if (!_sectionBuffer) {
			_sectionAlloc = _allocAmmount;
			_sectionBuffer = (byte *)malloc(_sectionAlloc);
		}
	}
	_sectionPtr = 0;
	return _sectionSize;
}

void SaveGame::endSection() {
	if (_currentSection == 0)
		error("Tried to end a save game section without starting a section");
	if (_saving) {
		_outSaveFile->writeUint32BE(_currentSection);
		_outSaveFile->writeUint32BE(_sectionSize);
		_outSaveFile->write(_sectionBuffer, _sectionSize);
	}
	_currentSection = 0;
}

void SaveGame::read(void *data, int size) {
	if (_saving)
		error("SaveGame::readBlock called when storing a savegame");
	if (_currentSection == 0)
		error("Tried to read a block without starting a section");
	memcpy(data, &_sectionBuffer[_sectionPtr], size);
	_sectionPtr += size;
}

uint32 SaveGame::readLEUint32() {
	if (_saving)
		error("SaveGame::readBlock called when storing a savegame");
	if (_currentSection == 0)
		error("Tried to read a block without starting a section");
	uint32 data = READ_LE_UINT32(&_sectionBuffer[_sectionPtr]);
	_sectionPtr += 4;
	return data;
}

uint16 SaveGame::readLEUint16() {
	if (_saving)
		error("SaveGame::readBlock called when storing a savegame");
	if (_currentSection == 0)
		error("Tried to read a block without starting a section");
	uint16 data = READ_LE_UINT16(&_sectionBuffer[_sectionPtr]);
	_sectionPtr += 2;
	return data;
}

int32 SaveGame::readLESint32() {
	if (_saving)
		error("SaveGame::readBlock called when storing a savegame");
	if (_currentSection == 0)
		error("Tried to read a block without starting a section");
	int32 data = (int32)READ_LE_UINT32(&_sectionBuffer[_sectionPtr]);
	_sectionPtr += 4;
	return data;
}

byte SaveGame::readByte() {
	if (_saving)
		error("SaveGame::readBlock called when storing a savegame");
	if (_currentSection == 0)
		error("Tried to read a block without starting a section");
	byte data = _sectionBuffer[_sectionPtr];
	_sectionPtr++;
	return data;
}

bool SaveGame::readBool() {
	return readByte() != 0;
}

void SaveGame::checkAlloc(int size) {
	if (_sectionSize + size > _sectionAlloc) {
		while (_sectionSize + size > _sectionAlloc)
			_sectionAlloc += _allocAmmount;
		_sectionBuffer = (byte *)realloc(_sectionBuffer, _sectionAlloc);
		if (!_sectionBuffer)
			error("Failed to allocate space for buffer");
	}
}

void SaveGame::write(const void *data, int size) {
	if (!_saving)
		error("SaveGame::writeBlock called when restoring a savegame");
	if (_currentSection == 0)
		error("Tried to write a block without starting a section");

	checkAlloc(size);

	memcpy(&_sectionBuffer[_sectionSize], data, size);
	_sectionSize += size;
}

void SaveGame::writeLEUint32(uint32 data) {
	if (!_saving)
		error("SaveGame::writeBlock called when restoring a savegame");
	if (_currentSection == 0)
		error("Tried to write a block without starting a section");

	checkAlloc(4);

	WRITE_LE_UINT32(&_sectionBuffer[_sectionSize], data);
	_sectionSize += 4;
}

void SaveGame::writeLEUint16(uint16 data) {
	if (!_saving)
		error("SaveGame::writeBlock called when restoring a savegame");
	if (_currentSection == 0)
		error("Tried to write a block without starting a section");

	checkAlloc(2);

	WRITE_LE_UINT16(&_sectionBuffer[_sectionSize], data);
	_sectionSize += 2;
}

void SaveGame::writeLESint32(int32 data) {
	if (!_saving)
		error("SaveGame::writeBlock called when restoring a savegame");
	if (_currentSection == 0)
		error("Tried to write a block without starting a section");

	checkAlloc(4);

	WRITE_LE_UINT32(&_sectionBuffer[_sectionSize], (uint32)data);
	_sectionSize += 4;
}

void SaveGame::writeBool(bool data) {
	writeByte(data);
}

void SaveGame::writeByte(byte data) {
	if (!_saving)
		error("SaveGame::writeBlock called when restoring a savegame");
	if (_currentSection == 0)
		error("Tried to write a block without starting a section");

	checkAlloc(1);

	_sectionBuffer[_sectionSize] = data;
	_sectionSize++;
}

void SaveGame::writeVector3d(const Math::Vector3d &vec) {
	writeFloat(vec.x());
	writeFloat(vec.y());
	writeFloat(vec.z());
}

void SaveGame::writeColor(const Grim::Color &color) {
	writeByte(color.getRed());
	writeByte(color.getGreen());
	writeByte(color.getBlue());
}

void SaveGame::writeFloat(float data) {
	uint32 v;
	memcpy(&v, &data, 4);
	writeLEUint32(v);
}

void SaveGame::writeString(const Common::String &string) {
	int32 len = string.size();
	writeLESint32(len);
	write(string.c_str(), len);
}

Math::Vector3d SaveGame::readVector3d() {
	float x = readFloat();
	float y = readFloat();
	float z = readFloat();
	return Math::Vector3d(x, y, z);
}

Grim::Color SaveGame::readColor() {
	Color color;
	color.getRed() = readByte();
	color.getGreen() = readByte();
	color.getBlue() = readByte();

	return color;
}

float SaveGame::readFloat() {
	float f;
	uint32 v = readLEUint32();
	memcpy(&f, &v, 4);

	return f;
}

Common::String SaveGame::readString() {
	int32 len = readLESint32();
	Common::String s((const char *)&_sectionBuffer[_sectionPtr], len);
	_sectionPtr += len;
	return s;
}

} // end of namespace Grim