2009-05-26 14:13:08 +00:00
|
|
|
/* Residual - A 3D game interpreter
|
2008-06-13 14:57:47 +00:00
|
|
|
*
|
|
|
|
* Residual is the legal property of its developers, whose names
|
2011-04-16 12:12:44 +00:00
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
2008-06-13 14:57:47 +00:00
|
|
|
* file distributed with this source distribution.
|
2006-04-02 14:20:45 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*
|
|
|
|
*/
|
2005-12-26 02:36:00 +00:00
|
|
|
|
2008-07-25 22:21:04 +00:00
|
|
|
#include "common/endian.h"
|
2009-05-07 19:06:31 +00:00
|
|
|
#include "common/system.h"
|
2008-07-20 15:00:50 +00:00
|
|
|
|
2011-03-20 21:16:27 +00:00
|
|
|
#include "graphics/vector3d.h"
|
|
|
|
|
2009-05-24 19:13:58 +00:00
|
|
|
#include "engines/grim/savegame.h"
|
2011-03-20 21:16:27 +00:00
|
|
|
#include "engines/grim/color.h"
|
2008-07-20 15:00:50 +00:00
|
|
|
|
2009-05-25 06:49:57 +00:00
|
|
|
namespace Grim {
|
|
|
|
|
2005-12-26 02:36:00 +00:00
|
|
|
#define SAVEGAME_HEADERTAG 'RSAV'
|
|
|
|
#define SAVEGAME_FOOTERTAG 'ESAV'
|
2011-05-08 17:30:12 +00:00
|
|
|
|
2011-05-17 15:08:31 +00:00
|
|
|
int SaveGame::SAVEGAME_VERSION = 14;
|
2005-12-26 02:36:00 +00:00
|
|
|
|
|
|
|
// Constructor. Should create/open a saved game
|
2011-05-22 04:02:20 +00:00
|
|
|
SaveGame::SaveGame(const Common::String &filename, bool saving) :
|
2006-05-13 15:07:35 +00:00
|
|
|
_saving(saving), _currentSection(0) {
|
2005-12-26 02:36:00 +00:00
|
|
|
if (_saving) {
|
2009-05-07 19:06:31 +00:00
|
|
|
_outSaveFile = g_system->getSavefileManager()->openForSaving(filename);
|
2008-07-20 15:53:14 +00:00
|
|
|
if (!_outSaveFile) {
|
2005-12-26 02:36:00 +00:00
|
|
|
warning("SaveGame::SaveGame() Error creating savegame file");
|
|
|
|
return;
|
|
|
|
}
|
2008-07-20 16:05:48 +00:00
|
|
|
_outSaveFile->writeUint32BE(SAVEGAME_HEADERTAG);
|
|
|
|
_outSaveFile->writeUint32BE(SAVEGAME_VERSION);
|
2011-05-08 17:30:12 +00:00
|
|
|
|
|
|
|
_version = SAVEGAME_VERSION;
|
2005-12-26 02:36:00 +00:00
|
|
|
} else {
|
2011-05-08 17:30:12 +00:00
|
|
|
uint32 tag;
|
2008-07-20 15:53:14 +00:00
|
|
|
|
2009-05-07 19:06:31 +00:00
|
|
|
_inSaveFile = g_system->getSavefileManager()->openForLoading(filename);
|
2008-07-20 15:53:14 +00:00
|
|
|
if (!_inSaveFile) {
|
2005-12-26 02:36:00 +00:00
|
|
|
warning("SaveGame::SaveGame() Error opening savegame file");
|
|
|
|
return;
|
|
|
|
}
|
2008-07-20 16:05:48 +00:00
|
|
|
tag = _inSaveFile->readUint32BE();
|
2005-12-26 02:36:00 +00:00
|
|
|
assert(tag == SAVEGAME_HEADERTAG);
|
2011-05-08 17:30:12 +00:00
|
|
|
_version = _inSaveFile->readUint32BE();
|
2005-12-26 02:36:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SaveGame::~SaveGame() {
|
2008-07-20 15:53:14 +00:00
|
|
|
if (_saving) {
|
2008-07-20 16:05:48 +00:00
|
|
|
_outSaveFile->writeUint32BE(SAVEGAME_FOOTERTAG);
|
2008-07-20 15:53:14 +00:00
|
|
|
_outSaveFile->finalize();
|
2009-05-27 13:28:49 +00:00
|
|
|
if (_outSaveFile->err())
|
2009-04-05 14:48:54 +00:00
|
|
|
warning("SaveGame::~SaveGame() Can't write file. (Disk full?)");
|
2008-07-20 15:53:14 +00:00
|
|
|
delete _outSaveFile;
|
2009-04-05 14:48:54 +00:00
|
|
|
} else {
|
2008-07-20 15:53:14 +00:00
|
|
|
delete _inSaveFile;
|
2009-04-05 14:48:54 +00:00
|
|
|
}
|
2005-12-26 02:36:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-08 17:30:12 +00:00
|
|
|
int SaveGame::saveVersion() const {
|
|
|
|
return _version;
|
|
|
|
}
|
|
|
|
|
2006-05-13 15:07:35 +00:00
|
|
|
uint32 SaveGame::beginSection(uint32 sectionTag) {
|
2011-05-08 17:30:12 +00:00
|
|
|
assert(_version == SAVEGAME_VERSION);
|
|
|
|
|
2006-05-13 15:07:35 +00:00
|
|
|
if (_currentSection != 0)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Tried to begin a new save game section with ending old section");
|
2006-05-13 15:07:35 +00:00
|
|
|
_currentSection = sectionTag;
|
|
|
|
_sectionSize = 0;
|
2011-05-03 04:40:28 +00:00
|
|
|
_sectionAlloc = _allocAmmount;
|
2006-05-13 15:07:35 +00:00
|
|
|
if (!_saving) {
|
|
|
|
uint32 tag = 0;
|
2008-07-25 22:21:04 +00:00
|
|
|
|
2006-05-13 15:07:35 +00:00
|
|
|
while (tag != sectionTag) {
|
2008-07-20 16:05:48 +00:00
|
|
|
tag = _inSaveFile->readUint32BE();
|
2006-05-13 15:07:35 +00:00
|
|
|
if (tag == SAVEGAME_FOOTERTAG)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Unable to find requested section of savegame");
|
2008-07-20 16:05:48 +00:00
|
|
|
_sectionSize = _inSaveFile->readUint32BE();
|
2011-05-11 05:27:49 +00:00
|
|
|
_inSaveFile->seek(_sectionSize, SEEK_CUR);
|
2006-05-13 15:07:35 +00:00
|
|
|
}
|
2011-05-11 05:27:49 +00:00
|
|
|
_sectionBuffer = (byte *)malloc(_sectionSize);
|
2011-05-12 17:35:18 +00:00
|
|
|
_inSaveFile->seek(-(int32)_sectionSize, SEEK_CUR);
|
2011-05-11 05:27:49 +00:00
|
|
|
_inSaveFile->read(_sectionBuffer, _sectionSize);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
_sectionBuffer = (byte *)malloc(_sectionAlloc);
|
2006-05-13 15:07:35 +00:00
|
|
|
}
|
|
|
|
_sectionPtr = 0;
|
|
|
|
return _sectionSize;
|
2005-12-26 02:36:00 +00:00
|
|
|
}
|
|
|
|
|
2006-05-13 15:07:35 +00:00
|
|
|
void SaveGame::endSection() {
|
|
|
|
if (_currentSection == 0)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Tried to end a save game section without starting a section");
|
2008-07-20 15:53:14 +00:00
|
|
|
if (_saving) {
|
2008-07-20 16:05:48 +00:00
|
|
|
_outSaveFile->writeUint32BE(_currentSection);
|
|
|
|
_outSaveFile->writeUint32BE(_sectionSize);
|
2008-07-20 15:53:14 +00:00
|
|
|
_outSaveFile->write(_sectionBuffer, _sectionSize);
|
2005-12-26 02:36:00 +00:00
|
|
|
}
|
2008-07-20 15:53:14 +00:00
|
|
|
free(_sectionBuffer);
|
2008-07-05 14:55:09 +00:00
|
|
|
_sectionBuffer = NULL;
|
2008-07-20 13:34:04 +00:00
|
|
|
_currentSection = 0;
|
2006-05-13 15:07:35 +00:00
|
|
|
}
|
2005-12-26 02:36:00 +00:00
|
|
|
|
2008-08-03 13:02:45 +00:00
|
|
|
uint32 SaveGame::getBufferPos() {
|
|
|
|
if (_saving)
|
|
|
|
return _sectionSize;
|
|
|
|
else
|
|
|
|
return _sectionPtr;
|
|
|
|
}
|
|
|
|
|
2006-05-13 15:07:35 +00:00
|
|
|
void SaveGame::read(void *data, int size) {
|
|
|
|
if (_saving)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("SaveGame::readBlock called when storing a savegame");
|
2006-05-13 15:07:35 +00:00
|
|
|
if (_currentSection == 0)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Tried to read a block without starting a section");
|
2006-05-13 15:07:35 +00:00
|
|
|
memcpy(data, &_sectionBuffer[_sectionPtr], size);
|
|
|
|
_sectionPtr += size;
|
2005-12-26 02:36:00 +00:00
|
|
|
}
|
|
|
|
|
2008-07-25 22:21:04 +00:00
|
|
|
uint32 SaveGame::readLEUint32() {
|
|
|
|
if (_saving)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("SaveGame::readBlock called when storing a savegame");
|
2008-07-25 22:21:04 +00:00
|
|
|
if (_currentSection == 0)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Tried to read a block without starting a section");
|
2008-07-25 22:21:04 +00:00
|
|
|
uint32 data = READ_LE_UINT32(&_sectionBuffer[_sectionPtr]);
|
|
|
|
_sectionPtr += 4;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2011-05-20 19:02:30 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-07-25 22:21:04 +00:00
|
|
|
int32 SaveGame::readLESint32() {
|
|
|
|
if (_saving)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("SaveGame::readBlock called when storing a savegame");
|
2008-07-25 22:21:04 +00:00
|
|
|
if (_currentSection == 0)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Tried to read a block without starting a section");
|
2008-07-25 22:21:04 +00:00
|
|
|
int32 data = (int32)READ_LE_UINT32(&_sectionBuffer[_sectionPtr]);
|
|
|
|
_sectionPtr += 4;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte SaveGame::readByte() {
|
|
|
|
if (_saving)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("SaveGame::readBlock called when storing a savegame");
|
2008-07-25 22:21:04 +00:00
|
|
|
if (_currentSection == 0)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Tried to read a block without starting a section");
|
2008-07-25 22:21:04 +00:00
|
|
|
byte data = _sectionBuffer[_sectionPtr];
|
|
|
|
_sectionPtr++;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SaveGame::readLEBool() {
|
|
|
|
if (_saving)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("SaveGame::readBlock called when storing a savegame");
|
2008-07-25 22:21:04 +00:00
|
|
|
if (_currentSection == 0)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Tried to read a block without starting a section");
|
2008-07-25 22:21:04 +00:00
|
|
|
uint32 data = READ_LE_UINT32(&_sectionBuffer[_sectionPtr]);
|
|
|
|
_sectionPtr += 4;
|
|
|
|
return data != 0;
|
|
|
|
}
|
|
|
|
|
2011-05-03 04:40:28 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-20 13:34:04 +00:00
|
|
|
void SaveGame::write(const void *data, int size) {
|
2005-12-26 02:36:00 +00:00
|
|
|
if (!_saving)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("SaveGame::writeBlock called when restoring a savegame");
|
2006-05-13 15:07:35 +00:00
|
|
|
if (_currentSection == 0)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Tried to write a block without starting a section");
|
2011-05-03 04:40:28 +00:00
|
|
|
|
|
|
|
checkAlloc(size);
|
|
|
|
|
2006-05-13 15:07:35 +00:00
|
|
|
memcpy(&_sectionBuffer[_sectionSize], data, size);
|
|
|
|
_sectionSize += size;
|
2005-12-26 02:36:00 +00:00
|
|
|
}
|
2008-07-25 22:21:04 +00:00
|
|
|
|
|
|
|
void SaveGame::writeLEUint32(uint32 data) {
|
|
|
|
if (!_saving)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("SaveGame::writeBlock called when restoring a savegame");
|
2008-07-25 22:21:04 +00:00
|
|
|
if (_currentSection == 0)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Tried to write a block without starting a section");
|
2011-05-03 04:40:28 +00:00
|
|
|
|
|
|
|
checkAlloc(4);
|
2008-07-25 22:21:04 +00:00
|
|
|
|
|
|
|
WRITE_LE_UINT32(&_sectionBuffer[_sectionSize], data);
|
|
|
|
_sectionSize += 4;
|
|
|
|
}
|
|
|
|
|
2011-05-20 19:02:30 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-07-25 22:21:04 +00:00
|
|
|
void SaveGame::writeLESint32(int32 data) {
|
|
|
|
if (!_saving)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("SaveGame::writeBlock called when restoring a savegame");
|
2008-07-25 22:21:04 +00:00
|
|
|
if (_currentSection == 0)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Tried to write a block without starting a section");
|
2011-05-03 04:40:28 +00:00
|
|
|
|
|
|
|
checkAlloc(4);
|
2008-07-25 22:21:04 +00:00
|
|
|
|
|
|
|
WRITE_LE_UINT32(&_sectionBuffer[_sectionSize], (uint32)data);
|
|
|
|
_sectionSize += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SaveGame::writeLEBool(bool data) {
|
|
|
|
if (!_saving)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("SaveGame::writeBlock called when restoring a savegame");
|
2008-07-25 22:21:04 +00:00
|
|
|
if (_currentSection == 0)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Tried to write a block without starting a section");
|
2011-05-03 04:40:28 +00:00
|
|
|
|
|
|
|
checkAlloc(4);
|
2008-07-25 22:21:04 +00:00
|
|
|
|
|
|
|
WRITE_LE_UINT32(&_sectionBuffer[_sectionSize], (uint32)data);
|
|
|
|
_sectionSize += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SaveGame::writeByte(byte data) {
|
|
|
|
if (!_saving)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("SaveGame::writeBlock called when restoring a savegame");
|
2008-07-25 22:21:04 +00:00
|
|
|
if (_currentSection == 0)
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Tried to write a block without starting a section");
|
2011-05-03 04:40:28 +00:00
|
|
|
|
|
|
|
checkAlloc(1);
|
2008-07-25 22:21:04 +00:00
|
|
|
|
|
|
|
_sectionBuffer[_sectionSize] = data;
|
|
|
|
_sectionSize++;
|
|
|
|
}
|
2009-05-25 06:49:57 +00:00
|
|
|
|
2011-03-20 21:16:27 +00:00
|
|
|
void SaveGame::writeVector3d(const Graphics::Vector3d &vec) {
|
2011-03-21 16:18:04 +00:00
|
|
|
writeFloat(vec.x());
|
|
|
|
writeFloat(vec.y());
|
|
|
|
writeFloat(vec.z());
|
2011-03-20 21:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SaveGame::writeColor(const Grim::Color &color) {
|
2011-05-05 08:58:29 +00:00
|
|
|
writeByte(color.getRed());
|
|
|
|
writeByte(color.getGreen());
|
|
|
|
writeByte(color.getBlue());
|
2011-03-20 21:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SaveGame::writeFloat(float data) {
|
2011-05-17 14:49:26 +00:00
|
|
|
byte *udata = (byte *)(&data);
|
|
|
|
uint32 v;
|
|
|
|
#if defined(SCUMM_LITTLE_ENDIAN)
|
|
|
|
byte b[4];
|
|
|
|
b[0] = udata[3];
|
|
|
|
b[1] = udata[2];
|
|
|
|
b[2] = udata[1];
|
|
|
|
b[3] = udata[0];
|
|
|
|
v = *(uint32 *)b;
|
|
|
|
#else
|
|
|
|
memcpy(&v, udata, 4);
|
|
|
|
#endif
|
|
|
|
writeLEUint32(v);
|
2011-03-20 21:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SaveGame::writeString(const Common::String &string) {
|
2011-05-22 04:02:20 +00:00
|
|
|
int32 len = string.size();
|
|
|
|
writeLESint32(len);
|
|
|
|
write(string.c_str(), len);
|
2011-03-20 21:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Graphics::Vector3d SaveGame::readVector3d() {
|
2011-03-21 16:18:04 +00:00
|
|
|
float x = readFloat();
|
|
|
|
float y = readFloat();
|
|
|
|
float z = readFloat();
|
|
|
|
return Graphics::Vector3d(x, y, z);
|
2011-03-20 21:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Grim::Color SaveGame::readColor() {
|
2011-03-21 16:18:04 +00:00
|
|
|
Color color;
|
2011-05-05 08:58:29 +00:00
|
|
|
color.getRed() = readByte();
|
|
|
|
color.getGreen() = readByte();
|
|
|
|
color.getBlue() = readByte();
|
2011-03-20 21:16:27 +00:00
|
|
|
|
2011-03-21 16:18:04 +00:00
|
|
|
return color;
|
2011-03-20 21:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float SaveGame::readFloat() {
|
2011-03-21 16:18:04 +00:00
|
|
|
float f;
|
2011-05-17 14:49:26 +00:00
|
|
|
byte *udata = (byte *)(&f);
|
|
|
|
uint32 v = readLEUint32();
|
|
|
|
#if defined(SCUMM_LITTLE_ENDIAN)
|
|
|
|
byte b[4];
|
|
|
|
*(uint32 *)&b = v;
|
|
|
|
udata[0] = b[3];
|
|
|
|
udata[1] = b[2];
|
|
|
|
udata[2] = b[1];
|
|
|
|
udata[3] = b[0];
|
|
|
|
#else
|
|
|
|
memcpy(udata, &v, 4);
|
|
|
|
#endif
|
|
|
|
|
2011-03-21 16:18:04 +00:00
|
|
|
return f;
|
2011-03-20 21:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Common::String SaveGame::readString() {
|
2011-05-22 04:02:20 +00:00
|
|
|
int32 len = readLESint32();
|
|
|
|
Common::String s((const char *)&_sectionBuffer[_sectionPtr], len);
|
|
|
|
_sectionPtr += len;
|
2011-03-20 21:16:27 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-05-25 06:49:57 +00:00
|
|
|
} // end of namespace Grim
|