2009-10-04 03:50:10 +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.
|
|
|
|
*
|
|
|
|
* 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.
|
2014-02-18 02:34:19 +01:00
|
|
|
*
|
2009-10-04 03:50:10 +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 02:34:19 +01:00
|
|
|
*
|
2009-10-04 03:50:10 +00:00
|
|
|
* 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 "draci/draci.h"
|
2009-12-09 21:03:22 +00:00
|
|
|
#include "draci/game.h"
|
2009-10-04 03:50:10 +00:00
|
|
|
#include "draci/saveload.h"
|
|
|
|
|
|
|
|
#include "common/serializer.h"
|
|
|
|
#include "common/savefile.h"
|
|
|
|
#include "common/system.h"
|
|
|
|
|
|
|
|
#include "graphics/scaler.h"
|
|
|
|
#include "graphics/thumbnail.h"
|
|
|
|
|
|
|
|
namespace Draci {
|
|
|
|
|
2011-09-08 19:59:36 +02:00
|
|
|
static const char *const draciIdentString = "DRACI";
|
2009-10-04 03:50:10 +00:00
|
|
|
|
ALL: Load savegame thumbnail only when necessary
This commit introduces the following changes:
1. Graphics::loadThumbnail()
Now returns a boolean and takes a new argument skipThumbnail which
defaults to false. In case of true, loadThumbnail() reads past the
thumbnail data in the input stream instead of actually loading the
thumbnail. This simplifies savegame handling where, up until now,
many engines always read the whole savegame metadata (including
the thumbnail) and then threw away the thumbnail when not needed
(which is in almost all cases, the most common exception being
MetaEngine::querySaveMetaInfos() which is responsible for loading
savegame metadata for displaying it in the GUI launcher.
2. readSavegameHeader()
Engines which already implement such a method (name varies) now take
a new argument skipThumbnail (default: true) which is passed
through to loadThumbnail(). This means that the default case for
readSavegameHeader() is now _not_ loading the thumbnail from a
savegame and just reading past it. In those cases, e.g.
querySaveMetaInfos(), where we actually are interested in loading
the thumbnail readSavegameHeader() needs to explicitely be called
with skipThumbnail == false.
Engines whose readSavegameHeader() (name varies) already takes an
argument loadThumbnail have been adapted to have a similar
prototype and semantics.
I.e. readSaveHeader(in, loadThumbnail, header) now is
readSaveHeader(in, header, skipThumbnail).
3. Error handling
Engines which previously did not check the return value of
readSavegameHeader() (name varies) now do so ensuring that possibly
broken savegames (be it a broken thumbnail or something else) don't
make it into the GUI launcher list in the first place.
2018-04-06 00:06:38 +02:00
|
|
|
WARN_UNUSED_RESULT bool readSavegameHeader(Common::InSaveFile *in, DraciSavegameHeader &header, bool skipThumbnail) {
|
2009-10-04 03:50:10 +00:00
|
|
|
char saveIdentBuffer[6];
|
|
|
|
|
|
|
|
// Validate the header Id
|
|
|
|
in->read(saveIdentBuffer, 6);
|
2011-11-02 23:08:23 +00:00
|
|
|
if (strcmp(saveIdentBuffer, draciIdentString) != 0)
|
2009-10-04 03:50:10 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
header.version = in->readByte();
|
2014-03-08 00:31:27 +01:00
|
|
|
// Version 1 is compatible with Version 2
|
|
|
|
if (header.version > DRACI_SAVEGAME_VERSION)
|
2009-10-04 03:50:10 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Read in the string
|
|
|
|
header.saveName.clear();
|
|
|
|
char ch;
|
|
|
|
while ((ch = (char)in->readByte()) != '\0') header.saveName += ch;
|
|
|
|
|
|
|
|
header.date = in->readUint32LE();
|
|
|
|
header.time = in->readUint16LE();
|
|
|
|
header.playtime = in->readUint32LE();
|
|
|
|
|
|
|
|
// Get the thumbnail
|
ALL: Load savegame thumbnail only when necessary
This commit introduces the following changes:
1. Graphics::loadThumbnail()
Now returns a boolean and takes a new argument skipThumbnail which
defaults to false. In case of true, loadThumbnail() reads past the
thumbnail data in the input stream instead of actually loading the
thumbnail. This simplifies savegame handling where, up until now,
many engines always read the whole savegame metadata (including
the thumbnail) and then threw away the thumbnail when not needed
(which is in almost all cases, the most common exception being
MetaEngine::querySaveMetaInfos() which is responsible for loading
savegame metadata for displaying it in the GUI launcher.
2. readSavegameHeader()
Engines which already implement such a method (name varies) now take
a new argument skipThumbnail (default: true) which is passed
through to loadThumbnail(). This means that the default case for
readSavegameHeader() is now _not_ loading the thumbnail from a
savegame and just reading past it. In those cases, e.g.
querySaveMetaInfos(), where we actually are interested in loading
the thumbnail readSavegameHeader() needs to explicitely be called
with skipThumbnail == false.
Engines whose readSavegameHeader() (name varies) already takes an
argument loadThumbnail have been adapted to have a similar
prototype and semantics.
I.e. readSaveHeader(in, loadThumbnail, header) now is
readSaveHeader(in, header, skipThumbnail).
3. Error handling
Engines which previously did not check the return value of
readSavegameHeader() (name varies) now do so ensuring that possibly
broken savegames (be it a broken thumbnail or something else) don't
make it into the GUI launcher list in the first place.
2018-04-06 00:06:38 +02:00
|
|
|
if (!Graphics::loadThumbnail(*in, header.thumbnail, skipThumbnail)) {
|
2009-10-04 03:50:10 +00:00
|
|
|
return false;
|
ALL: Load savegame thumbnail only when necessary
This commit introduces the following changes:
1. Graphics::loadThumbnail()
Now returns a boolean and takes a new argument skipThumbnail which
defaults to false. In case of true, loadThumbnail() reads past the
thumbnail data in the input stream instead of actually loading the
thumbnail. This simplifies savegame handling where, up until now,
many engines always read the whole savegame metadata (including
the thumbnail) and then threw away the thumbnail when not needed
(which is in almost all cases, the most common exception being
MetaEngine::querySaveMetaInfos() which is responsible for loading
savegame metadata for displaying it in the GUI launcher.
2. readSavegameHeader()
Engines which already implement such a method (name varies) now take
a new argument skipThumbnail (default: true) which is passed
through to loadThumbnail(). This means that the default case for
readSavegameHeader() is now _not_ loading the thumbnail from a
savegame and just reading past it. In those cases, e.g.
querySaveMetaInfos(), where we actually are interested in loading
the thumbnail readSavegameHeader() needs to explicitely be called
with skipThumbnail == false.
Engines whose readSavegameHeader() (name varies) already takes an
argument loadThumbnail have been adapted to have a similar
prototype and semantics.
I.e. readSaveHeader(in, loadThumbnail, header) now is
readSaveHeader(in, header, skipThumbnail).
3. Error handling
Engines which previously did not check the return value of
readSavegameHeader() (name varies) now do so ensuring that possibly
broken savegames (be it a broken thumbnail or something else) don't
make it into the GUI launcher list in the first place.
2018-04-06 00:06:38 +02:00
|
|
|
}
|
2009-10-04 03:50:10 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-10-04 05:44:23 +00:00
|
|
|
void writeSavegameHeader(Common::OutSaveFile *out, const DraciSavegameHeader &header) {
|
2009-10-04 03:50:10 +00:00
|
|
|
// Write out a savegame header
|
|
|
|
out->write(draciIdentString, 6);
|
|
|
|
out->writeByte(DRACI_SAVEGAME_VERSION);
|
|
|
|
|
|
|
|
// Write savegame name
|
|
|
|
out->write(header.saveName.c_str(), header.saveName.size() + 1);
|
|
|
|
|
2009-10-04 05:44:23 +00:00
|
|
|
out->writeUint32LE(header.date);
|
|
|
|
out->writeUint16LE(header.time);
|
|
|
|
out->writeUint32LE(header.playtime);
|
2009-10-04 03:50:10 +00:00
|
|
|
|
|
|
|
// Create a thumbnail and save it
|
2009-10-04 05:44:23 +00:00
|
|
|
Graphics::saveThumbnail(*out);
|
2009-10-04 03:50:10 +00:00
|
|
|
}
|
|
|
|
|
2009-10-04 05:44:23 +00:00
|
|
|
Common::Error saveSavegameData(int saveGameIdx, const Common::String &saveName, DraciEngine &vm) {
|
2011-06-02 10:46:29 +02:00
|
|
|
Common::String filename = vm.getSavegameFile(saveGameIdx);
|
2009-10-04 03:50:10 +00:00
|
|
|
Common::SaveFileManager *saveMan = g_system->getSavefileManager();
|
|
|
|
Common::OutSaveFile *f = saveMan->openForSaving(filename);
|
|
|
|
if (f == NULL)
|
|
|
|
return Common::kNoGameDataFoundError;
|
|
|
|
|
2009-10-08 19:41:38 +00:00
|
|
|
TimeDate curTime;
|
2009-10-04 03:50:10 +00:00
|
|
|
vm._system->getTimeAndDate(curTime);
|
|
|
|
|
|
|
|
// Save the savegame header
|
|
|
|
DraciSavegameHeader header;
|
|
|
|
header.saveName = saveName;
|
|
|
|
header.date = ((curTime.tm_mday & 0xFF) << 24) | (((curTime.tm_mon + 1) & 0xFF) << 16) | ((curTime.tm_year + 1900) & 0xFFFF);
|
|
|
|
header.time = ((curTime.tm_hour & 0xFF) << 8) | ((curTime.tm_min) & 0xFF);
|
2010-10-29 16:54:10 +00:00
|
|
|
header.playtime = vm.getTotalPlayTime() / 1000;
|
2009-10-04 05:44:23 +00:00
|
|
|
writeSavegameHeader(f, header);
|
2009-10-04 03:50:10 +00:00
|
|
|
|
|
|
|
if (f->err()) {
|
|
|
|
delete f;
|
|
|
|
saveMan->removeSavefile(filename);
|
|
|
|
return Common::kWritingFailed;
|
|
|
|
} else {
|
|
|
|
// Create the remainder of the savegame
|
|
|
|
Common::Serializer s(NULL, f);
|
2014-03-08 00:31:27 +01:00
|
|
|
vm._game->DoSync(s, header.version);
|
2009-10-04 03:50:10 +00:00
|
|
|
|
|
|
|
f->finalize();
|
|
|
|
delete f;
|
|
|
|
return Common::kNoError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Error loadSavegameData(int saveGameIdx, DraciEngine *vm) {
|
|
|
|
Common::String saveName;
|
|
|
|
|
|
|
|
Common::SaveFileManager *saveMan = g_system->getSavefileManager();
|
|
|
|
Common::InSaveFile *f = saveMan->openForLoading(vm->getSavegameFile(saveGameIdx));
|
|
|
|
|
|
|
|
if (f == NULL) {
|
|
|
|
return Common::kNoGameDataFoundError;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip over the savegame header
|
|
|
|
DraciSavegameHeader header;
|
2009-10-16 00:09:20 +00:00
|
|
|
if (!readSavegameHeader(f, header)) {
|
|
|
|
return Common::kNoGameDataFoundError;
|
|
|
|
}
|
2009-10-04 03:50:10 +00:00
|
|
|
|
2009-10-04 22:11:46 +00:00
|
|
|
// Pre-processing
|
|
|
|
vm->_game->rememberRoomNumAsPrevious();
|
|
|
|
vm->_game->deleteObjectAnimations();
|
|
|
|
|
2009-10-04 03:50:10 +00:00
|
|
|
// Synchronise the remaining data of the savegame
|
|
|
|
Common::Serializer s(f, NULL);
|
2014-03-08 00:31:27 +01:00
|
|
|
vm->_game->DoSync(s, header.version);
|
2009-10-04 03:50:10 +00:00
|
|
|
delete f;
|
|
|
|
|
2009-10-04 22:11:46 +00:00
|
|
|
// Post-processing
|
2009-10-04 05:44:23 +00:00
|
|
|
vm->_game->scheduleEnteringRoomUsingGate(vm->_game->getRoomNum(), 0);
|
2009-11-04 22:54:14 +00:00
|
|
|
vm->_game->setExitLoop(true);
|
2009-11-02 02:28:43 +00:00
|
|
|
vm->_game->setIsReloaded(true);
|
2009-10-04 03:50:10 +00:00
|
|
|
|
2009-10-04 09:13:15 +00:00
|
|
|
vm->_game->inventoryReload();
|
|
|
|
|
2010-10-29 16:54:10 +00:00
|
|
|
vm->setTotalPlayTime(header.playtime * 1000);
|
2009-10-04 22:11:46 +00:00
|
|
|
|
2009-10-04 03:50:10 +00:00
|
|
|
return Common::kNoError;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace Draci
|