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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Save and restore scene and game.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tinsel/actors.h"
|
2012-02-11 10:45:16 +00:00
|
|
|
#include "tinsel/config.h"
|
2008-12-01 20:35:36 +00:00
|
|
|
#include "tinsel/dialogs.h"
|
|
|
|
#include "tinsel/drives.h"
|
2008-07-23 09:02:47 +00:00
|
|
|
#include "tinsel/dw.h"
|
|
|
|
#include "tinsel/rince.h"
|
|
|
|
#include "tinsel/savescn.h"
|
|
|
|
#include "tinsel/timers.h"
|
|
|
|
#include "tinsel/tinlib.h"
|
|
|
|
#include "tinsel/tinsel.h"
|
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
#include "common/serializer.h"
|
2008-07-23 09:02:47 +00:00
|
|
|
#include "common/savefile.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2011-06-13 21:19:18 +00:00
|
|
|
#include "common/translation.h"
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2009-08-02 12:49:57 +00:00
|
|
|
#include "gui/message.h"
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
namespace Tinsel {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current savegame format version.
|
|
|
|
* Our save/load system uses an elaborate scheme to allow us to modify the
|
|
|
|
* savegame while keeping full backward compatibility, in the sense that newer
|
|
|
|
* ScummVM versions always are able to load old savegames.
|
|
|
|
* In order to achieve that, we store a version in the savegame files, and whenever
|
|
|
|
* the savegame layout is modified, the version is incremented.
|
|
|
|
*
|
|
|
|
* This roughly works by marking each savegame entry with a range of versions
|
|
|
|
* for which it is valid; the save/load code iterates over all entries, but
|
|
|
|
* only saves/loads those which are valid for the version of the savegame
|
|
|
|
* which is being loaded/saved currently.
|
|
|
|
*/
|
2012-08-06 10:03:56 +00:00
|
|
|
#define CURRENT_VER 2
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An auxillary macro, used to specify savegame versions. We use this instead
|
|
|
|
* of just writing the raw version, because this way they stand out more to
|
|
|
|
* the reading eye, making it a bit easier to navigate through the code.
|
|
|
|
*/
|
|
|
|
#define VER(x) x
|
|
|
|
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
//----------------- GLOBAL GLOBAL DATA --------------------
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
int g_thingHeld = 0;
|
|
|
|
int g_restoreCD = 0;
|
|
|
|
SRSTATE g_SRstate = SR_IDLE;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
//----------------- EXTERN FUNCTIONS --------------------
|
|
|
|
|
|
|
|
// in DOS_DW.C
|
2009-03-15 20:30:57 +00:00
|
|
|
extern void syncSCdata(Common::Serializer &s);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
// in PCODE.C
|
2009-03-15 20:30:57 +00:00
|
|
|
extern void syncGlobInfo(Common::Serializer &s);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
// in POLYGONS.C
|
2009-03-15 20:30:57 +00:00
|
|
|
extern void syncPolyInfo(Common::Serializer &s);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
extern int g_sceneCtr;
|
2009-07-20 09:19:27 +00:00
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
extern bool g_ASceneIsSaved;
|
2011-10-18 08:47:45 +00:00
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
//----------------- LOCAL DEFINES --------------------
|
|
|
|
|
|
|
|
struct SaveGameHeader {
|
|
|
|
uint32 id;
|
|
|
|
uint32 size;
|
|
|
|
uint32 ver;
|
|
|
|
char desc[SG_DESC_LEN];
|
2009-10-08 19:41:38 +00:00
|
|
|
TimeDate dateTime;
|
2012-02-11 10:45:16 +00:00
|
|
|
bool scnFlag;
|
|
|
|
byte language;
|
2012-08-06 10:03:56 +00:00
|
|
|
uint16 numInterpreters; // Savegame version 2 or later only
|
2008-07-23 09:02:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2008-12-01 20:35:36 +00:00
|
|
|
DW1_SAVEGAME_ID = 0x44575399, // = 'DWSc' = "DiscWorld 1 ScummVM"
|
2008-12-20 23:08:37 +00:00
|
|
|
DW2_SAVEGAME_ID = 0x44573253, // = 'DW2S' = "DiscWorld 2 ScummVM"
|
2012-08-06 10:03:56 +00:00
|
|
|
SAVEGAME_HEADER_SIZE = 4 + 4 + 4 + SG_DESC_LEN + 7 + 1 + 1 + 2
|
2008-07-23 09:02:47 +00:00
|
|
|
};
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
#define SAVEGAME_ID (TinselV2 ? (uint32)DW2_SAVEGAME_ID : (uint32)DW1_SAVEGAME_ID)
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2010-10-31 21:50:48 +00:00
|
|
|
enum {
|
|
|
|
// FIXME: Save file names in ScummVM can be longer than 8.3, overflowing the
|
|
|
|
// name field in savedFiles. Raising it to 256 as a preliminary fix.
|
|
|
|
FNAMELEN = 256 // 8.3
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SFILES {
|
|
|
|
char name[FNAMELEN];
|
|
|
|
char desc[SG_DESC_LEN + 2];
|
|
|
|
TimeDate dateTime;
|
|
|
|
};
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
//----------------- LOCAL GLOBAL DATA --------------------
|
|
|
|
|
2010-11-16 09:53:55 +00:00
|
|
|
// FIXME: Avoid non-const global vars
|
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
static int g_numSfiles = 0;
|
|
|
|
static SFILES g_savedFiles[MAX_SAVED_FILES];
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
static bool g_NeedLoad = true;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
static SAVED_DATA *g_srsd = 0;
|
|
|
|
static int g_RestoreGameNumber = 0;
|
|
|
|
static char *g_SaveSceneName = 0;
|
|
|
|
static const char *g_SaveSceneDesc = 0;
|
|
|
|
static int *g_SaveSceneSsCount = 0;
|
|
|
|
static SAVED_DATA *g_SaveSceneSsData = 0; // points to 'SAVED_DATA ssdata[MAX_NEST]'
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
//------------- SAVE/LOAD SUPPORT METHODS ----------------
|
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
void setNeedLoad() {
|
|
|
|
g_NeedLoad = true;
|
|
|
|
}
|
2008-12-02 18:34:32 +00:00
|
|
|
|
2009-10-08 19:41:38 +00:00
|
|
|
static void syncTime(Common::Serializer &s, TimeDate &t) {
|
2008-07-23 09:02:47 +00:00
|
|
|
s.syncAsUint16LE(t.tm_year);
|
|
|
|
s.syncAsByte(t.tm_mon);
|
|
|
|
s.syncAsByte(t.tm_mday);
|
|
|
|
s.syncAsByte(t.tm_hour);
|
|
|
|
s.syncAsByte(t.tm_min);
|
|
|
|
s.syncAsByte(t.tm_sec);
|
|
|
|
}
|
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
static bool syncSaveGameHeader(Common::Serializer &s, SaveGameHeader &hdr) {
|
2008-07-23 09:02:47 +00:00
|
|
|
s.syncAsUint32LE(hdr.id);
|
|
|
|
s.syncAsUint32LE(hdr.size);
|
|
|
|
s.syncAsUint32LE(hdr.ver);
|
|
|
|
|
|
|
|
s.syncBytes((byte *)hdr.desc, SG_DESC_LEN);
|
2008-07-30 08:23:04 +00:00
|
|
|
hdr.desc[SG_DESC_LEN - 1] = 0;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
syncTime(s, hdr.dateTime);
|
|
|
|
|
|
|
|
int tmp = hdr.size - s.bytesSynced();
|
2011-08-07 12:08:14 +00:00
|
|
|
|
|
|
|
// NOTE: We can't use SAVEGAME_ID here when attempting to remove a saved game from the launcher,
|
|
|
|
// as there is no TinselEngine initialized then. This means that we can't check if this is a DW1
|
|
|
|
// or DW2 savegame in this case, but it doesn't really matter, as the saved game is about to be
|
|
|
|
// deleted anyway. Refer to bug #3387551.
|
|
|
|
bool correctID = _vm ? (hdr.id == SAVEGAME_ID) : (hdr.id == DW1_SAVEGAME_ID || hdr.id == DW2_SAVEGAME_ID);
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
// Perform sanity check
|
2011-08-07 12:08:14 +00:00
|
|
|
if (tmp < 0 || !correctID || hdr.ver > CURRENT_VER || hdr.size > 1024)
|
2008-07-23 09:02:47 +00:00
|
|
|
return false;
|
2012-02-11 10:45:16 +00:00
|
|
|
|
|
|
|
if (tmp > 0) {
|
|
|
|
// If there's header space left, handling syncing the Scn flag and game language
|
|
|
|
s.syncAsByte(hdr.scnFlag);
|
|
|
|
s.syncAsByte(hdr.language);
|
|
|
|
tmp -= 2;
|
|
|
|
|
|
|
|
if (_vm && s.isLoading()) {
|
|
|
|
// If the engine is loaded, ensure the Scn/Gra usage is correct, and it's the correct language
|
|
|
|
if ((hdr.scnFlag != ((_vm->getFeatures() & GF_SCNFILES) != 0)) ||
|
|
|
|
(hdr.language != _vm->_config->_language))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 10:03:56 +00:00
|
|
|
// Handle the number of interpreter contexts that will be saved in the savegame
|
|
|
|
if (tmp >= 2) {
|
|
|
|
tmp -= 2;
|
|
|
|
hdr.numInterpreters = NUM_INTERPRET;
|
|
|
|
s.syncAsUint16LE(hdr.numInterpreters);
|
|
|
|
} else {
|
|
|
|
hdr.numInterpreters = (TinselV2 ? 70 : 64) - 20;
|
|
|
|
}
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
// Skip over any extra bytes
|
2009-03-12 03:26:03 +00:00
|
|
|
s.skip(tmp);
|
2008-07-23 09:02:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
static void syncSavedMover(Common::Serializer &s, SAVED_MOVER &sm) {
|
2011-05-10 10:39:42 +00:00
|
|
|
int i, j;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
s.syncAsUint32LE(sm.bActive);
|
2008-07-23 09:02:47 +00:00
|
|
|
s.syncAsSint32LE(sm.actorID);
|
2008-12-01 20:35:36 +00:00
|
|
|
s.syncAsSint32LE(sm.objX);
|
|
|
|
s.syncAsSint32LE(sm.objY);
|
|
|
|
s.syncAsUint32LE(sm.hLastfilm);
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2011-05-10 10:39:42 +00:00
|
|
|
// Sync walk reels
|
|
|
|
for (i = 0; i < TOTAL_SCALES; ++i)
|
|
|
|
for (j = 0; j < 4; ++j)
|
|
|
|
s.syncAsUint32LE(sm.walkReels[i][j]);
|
|
|
|
|
|
|
|
// Sync stand reels
|
|
|
|
for (i = 0; i < TOTAL_SCALES; ++i)
|
|
|
|
for (j = 0; j < 4; ++j)
|
|
|
|
s.syncAsUint32LE(sm.standReels[i][j]);
|
|
|
|
|
|
|
|
// Sync talk reels
|
|
|
|
for (i = 0; i < TOTAL_SCALES; ++i)
|
|
|
|
for (j = 0; j < 4; ++j)
|
|
|
|
s.syncAsUint32LE(sm.talkReels[i][j]);
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
|
|
|
|
if (TinselV2) {
|
|
|
|
s.syncAsByte(sm.bHidden);
|
|
|
|
|
|
|
|
s.syncAsSint32LE(sm.brightness);
|
2011-04-14 12:12:27 +00:00
|
|
|
s.syncAsSint32LE(sm.startColor);
|
2008-12-01 20:35:36 +00:00
|
|
|
s.syncAsSint32LE(sm.paletteLength);
|
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
static void syncSavedActor(Common::Serializer &s, SAVED_ACTOR &sa) {
|
2008-07-23 09:02:47 +00:00
|
|
|
s.syncAsUint16LE(sa.actorID);
|
2008-12-01 20:35:36 +00:00
|
|
|
s.syncAsUint16LE(sa.zFactor);
|
2010-06-06 01:12:41 +00:00
|
|
|
s.syncAsUint16LE(sa.bAlive);
|
|
|
|
s.syncAsUint16LE(sa.bHidden);
|
2008-07-23 09:02:47 +00:00
|
|
|
s.syncAsUint32LE(sa.presFilm);
|
|
|
|
s.syncAsUint16LE(sa.presRnum);
|
2008-12-01 20:35:36 +00:00
|
|
|
s.syncAsUint16LE(sa.presPlayX);
|
|
|
|
s.syncAsUint16LE(sa.presPlayY);
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
extern void syncAllActorsAlive(Common::Serializer &s);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
static void syncNoScrollB(Common::Serializer &s, NOSCROLLB &ns) {
|
2008-07-23 09:02:47 +00:00
|
|
|
s.syncAsSint32LE(ns.ln);
|
|
|
|
s.syncAsSint32LE(ns.c1);
|
|
|
|
s.syncAsSint32LE(ns.c2);
|
|
|
|
}
|
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
static void syncZPosition(Common::Serializer &s, Z_POSITIONS &zp) {
|
2008-12-01 20:35:36 +00:00
|
|
|
s.syncAsSint16LE(zp.actor);
|
|
|
|
s.syncAsSint16LE(zp.column);
|
|
|
|
s.syncAsSint32LE(zp.z);
|
|
|
|
}
|
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
static void syncPolyVolatile(Common::Serializer &s, POLY_VOLATILE &p) {
|
2008-12-01 20:35:36 +00:00
|
|
|
s.syncAsByte(p.bDead);
|
|
|
|
s.syncAsSint16LE(p.xoff);
|
|
|
|
s.syncAsSint16LE(p.yoff);
|
|
|
|
}
|
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
static void syncSoundReel(Common::Serializer &s, SOUNDREELS &sr) {
|
2008-12-01 20:35:36 +00:00
|
|
|
s.syncAsUint32LE(sr.hFilm);
|
|
|
|
s.syncAsSint32LE(sr.column);
|
|
|
|
s.syncAsSint32LE(sr.actorCol);
|
|
|
|
}
|
|
|
|
|
2012-08-06 10:03:56 +00:00
|
|
|
static void syncSavedData(Common::Serializer &s, SAVED_DATA &sd, int numInterp) {
|
2008-07-23 09:02:47 +00:00
|
|
|
s.syncAsUint32LE(sd.SavedSceneHandle);
|
|
|
|
s.syncAsUint32LE(sd.SavedBgroundHandle);
|
|
|
|
for (int i = 0; i < MAX_MOVERS; ++i)
|
|
|
|
syncSavedMover(s, sd.SavedMoverInfo[i]);
|
|
|
|
for (int i = 0; i < MAX_SAVED_ACTORS; ++i)
|
|
|
|
syncSavedActor(s, sd.SavedActorInfo[i]);
|
|
|
|
|
|
|
|
s.syncAsSint32LE(sd.NumSavedActors);
|
|
|
|
s.syncAsSint32LE(sd.SavedLoffset);
|
|
|
|
s.syncAsSint32LE(sd.SavedToffset);
|
2012-08-06 10:03:56 +00:00
|
|
|
for (int i = 0; i < numInterp; ++i)
|
2008-07-23 09:02:47 +00:00
|
|
|
sd.SavedICInfo[i].syncWithSerializer(s);
|
|
|
|
for (int i = 0; i < MAX_POLY; ++i)
|
|
|
|
s.syncAsUint32LE(sd.SavedDeadPolys[i]);
|
|
|
|
s.syncAsUint32LE(sd.SavedControl);
|
|
|
|
s.syncAsUint32LE(sd.SavedMidi);
|
|
|
|
s.syncAsUint32LE(sd.SavedLoop);
|
|
|
|
s.syncAsUint32LE(sd.SavedNoBlocking);
|
|
|
|
|
|
|
|
// SavedNoScrollData
|
|
|
|
for (int i = 0; i < MAX_VNOSCROLL; ++i)
|
|
|
|
syncNoScrollB(s, sd.SavedNoScrollData.NoVScroll[i]);
|
|
|
|
for (int i = 0; i < MAX_HNOSCROLL; ++i)
|
|
|
|
syncNoScrollB(s, sd.SavedNoScrollData.NoHScroll[i]);
|
|
|
|
s.syncAsUint32LE(sd.SavedNoScrollData.NumNoV);
|
|
|
|
s.syncAsUint32LE(sd.SavedNoScrollData.NumNoH);
|
2008-12-01 20:35:36 +00:00
|
|
|
|
|
|
|
// Tinsel 2 fields
|
|
|
|
if (TinselV2) {
|
|
|
|
// SavedNoScrollData
|
|
|
|
s.syncAsUint32LE(sd.SavedNoScrollData.xTrigger);
|
|
|
|
s.syncAsUint32LE(sd.SavedNoScrollData.xDistance);
|
|
|
|
s.syncAsUint32LE(sd.SavedNoScrollData.xSpeed);
|
|
|
|
s.syncAsUint32LE(sd.SavedNoScrollData.yTriggerTop);
|
|
|
|
s.syncAsUint32LE(sd.SavedNoScrollData.yTriggerBottom);
|
|
|
|
s.syncAsUint32LE(sd.SavedNoScrollData.yDistance);
|
|
|
|
s.syncAsUint32LE(sd.SavedNoScrollData.ySpeed);
|
|
|
|
|
|
|
|
for (int i = 0; i < NUM_ZPOSITIONS; ++i)
|
|
|
|
syncZPosition(s, sd.zPositions[i]);
|
|
|
|
s.syncBytes(sd.savedActorZ, MAX_SAVED_ACTOR_Z);
|
|
|
|
for (int i = 0; i < MAX_POLY; ++i)
|
|
|
|
syncPolyVolatile(s, sd.SavedPolygonStuff[i]);
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
s.syncAsUint32LE(sd.SavedTune[i]);
|
|
|
|
s.syncAsByte(sd.bTinselDim);
|
|
|
|
s.syncAsSint32LE(sd.SavedScrollFocus);
|
|
|
|
for (int i = 0; i < SV_TOPVALID; ++i)
|
|
|
|
s.syncAsSint32LE(sd.SavedSystemVars[i]);
|
|
|
|
for (int i = 0; i < MAX_SOUNDREELS; ++i)
|
|
|
|
syncSoundReel(s, sd.SavedSoundReels[i]);
|
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
2009-10-08 19:41:38 +00:00
|
|
|
/**
|
|
|
|
* Compare two TimeDate structs to see which one was earlier.
|
|
|
|
* Returns 0 if they are equal, a negative value if a is lower / first, and
|
|
|
|
* a positive value if b is lower / first.
|
|
|
|
*/
|
|
|
|
static int cmpTimeDate(const TimeDate &a, const TimeDate &b) {
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
#define CMP_ENTRY(x) tmp = a.x - b.x; if (tmp != 0) return tmp
|
|
|
|
|
|
|
|
CMP_ENTRY(tm_year);
|
|
|
|
CMP_ENTRY(tm_mon);
|
|
|
|
CMP_ENTRY(tm_mday);
|
|
|
|
CMP_ENTRY(tm_hour);
|
|
|
|
CMP_ENTRY(tm_min);
|
|
|
|
CMP_ENTRY(tm_sec);
|
|
|
|
|
|
|
|
#undef CMP_ENTRY
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
/**
|
2010-10-31 21:50:48 +00:00
|
|
|
* Compute a list of all available saved game files.
|
2008-07-23 09:02:47 +00:00
|
|
|
* Store the file details, ordered by time, in savedFiles[] and return
|
2010-10-31 21:50:48 +00:00
|
|
|
* the number of files found.
|
2008-07-23 09:02:47 +00:00
|
|
|
*/
|
2008-09-01 21:13:11 +00:00
|
|
|
int getList(Common::SaveFileManager *saveFileMan, const Common::String &target) {
|
2008-12-01 20:35:36 +00:00
|
|
|
// No change since last call?
|
|
|
|
// TODO/FIXME: Just always reload this data? Be careful about slow downs!!!
|
2012-02-22 22:14:29 +00:00
|
|
|
if (!g_NeedLoad)
|
|
|
|
return g_numSfiles;
|
2008-12-01 20:35:36 +00:00
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
int i;
|
|
|
|
|
2008-09-01 21:13:11 +00:00
|
|
|
const Common::String pattern = target + ".???";
|
2010-03-18 15:54:40 +00:00
|
|
|
Common::StringArray files = saveFileMan->listSavefiles(pattern);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
g_numSfiles = 0;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2010-03-18 15:54:40 +00:00
|
|
|
for (Common::StringArray::const_iterator file = files.begin(); file != files.end(); ++file) {
|
2012-02-22 22:14:29 +00:00
|
|
|
if (g_numSfiles >= MAX_SAVED_FILES)
|
2008-07-23 09:02:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
const Common::String &fname = *file;
|
2009-05-29 14:38:22 +00:00
|
|
|
Common::InSaveFile *f = saveFileMan->openForLoading(fname);
|
2008-07-23 09:02:47 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to load save game header
|
2009-03-15 20:30:57 +00:00
|
|
|
Common::Serializer s(f, 0);
|
2008-07-23 09:02:47 +00:00
|
|
|
SaveGameHeader hdr;
|
|
|
|
bool validHeader = syncSaveGameHeader(s, hdr);
|
|
|
|
delete f;
|
|
|
|
if (!validHeader) {
|
|
|
|
continue; // Invalid header, or savegame too new -> skip it
|
|
|
|
// TODO: In SCUMM, we still show an entry for the save, but with description
|
|
|
|
// "incompatible version".
|
|
|
|
}
|
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
i = g_numSfiles;
|
2008-07-23 09:02:47 +00:00
|
|
|
#ifndef DISABLE_SAVEGAME_SORTING
|
2012-02-22 22:14:29 +00:00
|
|
|
for (i = 0; i < g_numSfiles; i++) {
|
|
|
|
if (cmpTimeDate(hdr.dateTime, g_savedFiles[i].dateTime) > 0) {
|
|
|
|
Common::copy_backward(&g_savedFiles[i], &g_savedFiles[g_numSfiles], &g_savedFiles[g_numSfiles + 1]);
|
2008-07-23 09:02:47 +00:00
|
|
|
break;
|
2009-01-01 15:06:43 +00:00
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
Common::strlcpy(g_savedFiles[i].name, fname.c_str(), FNAMELEN);
|
|
|
|
Common::strlcpy(g_savedFiles[i].desc, hdr.desc, SG_DESC_LEN);
|
|
|
|
g_savedFiles[i].dateTime = hdr.dateTime;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
++g_numSfiles;
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next getList() needn't do its stuff again
|
2012-02-22 22:14:29 +00:00
|
|
|
g_NeedLoad = false;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
return g_numSfiles;
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
int getList() {
|
2008-09-01 21:13:11 +00:00
|
|
|
// No change since last call?
|
|
|
|
// TODO/FIXME: Just always reload this data? Be careful about slow downs!!!
|
2012-02-22 22:14:29 +00:00
|
|
|
if (!g_NeedLoad)
|
|
|
|
return g_numSfiles;
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2008-09-01 21:13:11 +00:00
|
|
|
return getList(_vm->getSaveFileMan(), _vm->getTargetName());
|
|
|
|
}
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
char *ListEntry(int i, letype which) {
|
|
|
|
if (i == -1)
|
2012-02-22 22:14:29 +00:00
|
|
|
i = g_numSfiles;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
assert(i >= 0);
|
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
if (i < g_numSfiles)
|
|
|
|
return which == LE_NAME ? g_savedFiles[i].name : g_savedFiles[i].desc;
|
2008-07-23 09:02:47 +00:00
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-06 10:03:56 +00:00
|
|
|
static bool DoSync(Common::Serializer &s, int numInterp) {
|
2009-01-28 21:11:18 +00:00
|
|
|
int sg = 0;
|
2008-12-01 20:35:36 +00:00
|
|
|
|
|
|
|
if (TinselV2) {
|
|
|
|
if (s.isSaving())
|
2012-02-22 22:14:29 +00:00
|
|
|
g_restoreCD = GetCurrentCD();
|
|
|
|
s.syncAsSint16LE(g_restoreCD);
|
2008-12-01 20:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (TinselV2 && s.isLoading())
|
|
|
|
HoldItem(INV_NOICON);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-08-06 10:03:56 +00:00
|
|
|
syncSavedData(s, *g_srsd, numInterp);
|
2008-07-23 09:02:47 +00:00
|
|
|
syncGlobInfo(s); // Glitter globals
|
|
|
|
syncInvInfo(s); // Inventory data
|
|
|
|
|
|
|
|
// Held object
|
|
|
|
if (s.isSaving())
|
|
|
|
sg = WhichItemHeld();
|
|
|
|
s.syncAsSint32LE(sg);
|
2008-12-01 20:35:36 +00:00
|
|
|
if (s.isLoading()) {
|
2012-08-06 10:03:56 +00:00
|
|
|
if (sg != -1 && !GetIsInvObject(sg))
|
|
|
|
// Not a valid inventory object, so return false
|
|
|
|
return false;
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
if (TinselV2)
|
2012-02-22 22:14:29 +00:00
|
|
|
g_thingHeld = sg;
|
2008-12-01 20:35:36 +00:00
|
|
|
else
|
|
|
|
HoldItem(sg);
|
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
syncTimerInfo(s); // Timer data
|
2008-12-01 20:35:36 +00:00
|
|
|
if (!TinselV2)
|
|
|
|
syncPolyInfo(s); // Dead polygon data
|
2008-07-23 09:02:47 +00:00
|
|
|
syncSCdata(s); // Hook Scene and delayed scene
|
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
s.syncAsSint32LE(*g_SaveSceneSsCount);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
if (*g_SaveSceneSsCount != 0) {
|
|
|
|
SAVED_DATA *sdPtr = g_SaveSceneSsData;
|
|
|
|
for (int i = 0; i < *g_SaveSceneSsCount; ++i, ++sdPtr)
|
2012-08-06 10:03:56 +00:00
|
|
|
syncSavedData(s, *sdPtr, numInterp);
|
2011-10-18 08:47:45 +00:00
|
|
|
|
|
|
|
// Flag that there is a saved scene to return to. Note that in this context 'saved scene'
|
|
|
|
// is a stored scene to return to from another scene, such as from the Summoning Book close-up
|
|
|
|
// in Discworld 1 to whatever scene Rincewind was in prior to that
|
2012-02-22 22:14:29 +00:00
|
|
|
g_ASceneIsSaved = true;
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
if (!TinselV2)
|
|
|
|
syncAllActorsAlive(s);
|
2012-08-06 10:03:56 +00:00
|
|
|
|
|
|
|
return true;
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DoRestore
|
|
|
|
*/
|
2008-12-20 23:08:37 +00:00
|
|
|
static bool DoRestore() {
|
2012-02-22 22:14:29 +00:00
|
|
|
Common::InSaveFile *f = _vm->getSaveFileMan()->openForLoading(g_savedFiles[g_RestoreGameNumber].name);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
if (f == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-15 20:30:57 +00:00
|
|
|
Common::Serializer s(f, 0);
|
2008-07-23 09:02:47 +00:00
|
|
|
SaveGameHeader hdr;
|
|
|
|
if (!syncSaveGameHeader(s, hdr)) {
|
|
|
|
delete f; // Invalid header, or savegame too new -> skip it
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-26 02:17:31 +00:00
|
|
|
|
2012-08-06 10:03:56 +00:00
|
|
|
// Load in the data. For older savegame versions, we potentially need to load the data twice, once
|
|
|
|
// for pre 1.5 savegames, and if that fails, a second time for 1.5 savegames
|
|
|
|
int numInterpreters = hdr.numInterpreters;
|
|
|
|
int32 currentPos = f->pos();
|
|
|
|
for (int tryNumber = 0; tryNumber < ((hdr.ver >= 2) ? 1 : 2); ++tryNumber) {
|
|
|
|
// If it's the second loop iteration, try with the 1.5 savegame number of interpreter contexts
|
|
|
|
if (tryNumber == 1) {
|
|
|
|
f->seek(currentPos);
|
|
|
|
numInterpreters = 80;
|
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-08-06 10:03:56 +00:00
|
|
|
// Load the savegame data
|
|
|
|
if (DoSync(s, numInterpreters))
|
|
|
|
// Data load was successful (or likely), so break out of loop
|
|
|
|
break;
|
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2008-12-20 23:08:37 +00:00
|
|
|
uint32 id = f->readSint32LE();
|
2008-07-23 09:02:47 +00:00
|
|
|
if (id != (uint32)0xFEEDFACE)
|
|
|
|
error("Incompatible saved game");
|
|
|
|
|
2009-12-15 08:18:57 +00:00
|
|
|
bool failed = (f->eos() || f->err());
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
delete f;
|
|
|
|
|
2009-08-02 12:49:57 +00:00
|
|
|
if (failed) {
|
2011-06-13 21:19:18 +00:00
|
|
|
GUI::MessageDialog dialog(_("Failed to load game state from file."));
|
2009-08-02 12:49:57 +00:00
|
|
|
dialog.runModal();
|
|
|
|
}
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
return !failed;
|
|
|
|
}
|
|
|
|
|
2011-12-08 14:27:28 +00:00
|
|
|
static void SaveFailure(Common::OutSaveFile *f) {
|
|
|
|
if (f) {
|
|
|
|
delete f;
|
2012-02-22 22:14:29 +00:00
|
|
|
_vm->getSaveFileMan()->removeSavefile(g_SaveSceneName);
|
2011-12-08 14:27:28 +00:00
|
|
|
}
|
2013-01-02 20:25:51 +00:00
|
|
|
g_SaveSceneName = NULL; // Invalidate save name
|
2011-12-08 14:27:28 +00:00
|
|
|
GUI::MessageDialog dialog(_("Failed to save game state to file."));
|
|
|
|
dialog.runModal();
|
|
|
|
}
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
/**
|
|
|
|
* DoSave
|
|
|
|
*/
|
2009-11-02 21:54:57 +00:00
|
|
|
static void DoSave() {
|
2008-07-23 09:02:47 +00:00
|
|
|
Common::OutSaveFile *f;
|
2011-04-10 11:19:28 +00:00
|
|
|
char tmpName[FNAMELEN];
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
// Next getList() must do its stuff again
|
2012-02-22 22:14:29 +00:00
|
|
|
g_NeedLoad = true;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
if (g_SaveSceneName == NULL) {
|
2011-06-19 22:59:48 +00:00
|
|
|
// Generate a new unique save name
|
2011-04-10 11:19:28 +00:00
|
|
|
int i;
|
|
|
|
int ano = 1; // Allocated number
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
Common::String fname = _vm->getSavegameFilename(ano);
|
|
|
|
strcpy(tmpName, fname.c_str());
|
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
for (i = 0; i < g_numSfiles; i++)
|
|
|
|
if (!strcmp(g_savedFiles[i].name, tmpName))
|
2011-04-10 11:19:28 +00:00
|
|
|
break;
|
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
if (i == g_numSfiles)
|
2011-04-10 11:19:28 +00:00
|
|
|
break;
|
|
|
|
ano++;
|
|
|
|
}
|
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
g_SaveSceneName = tmpName;
|
2011-04-10 11:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
if (g_SaveSceneDesc[0] == 0)
|
|
|
|
g_SaveSceneDesc = "unnamed";
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
f = _vm->getSaveFileMan()->openForSaving(g_SaveSceneName);
|
2009-03-15 20:30:57 +00:00
|
|
|
Common::Serializer s(0, f);
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2011-12-08 14:27:28 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
SaveFailure(f);
|
|
|
|
return;
|
|
|
|
}
|
2009-08-02 12:49:57 +00:00
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
// Write out a savegame header
|
|
|
|
SaveGameHeader hdr;
|
|
|
|
hdr.id = SAVEGAME_ID;
|
|
|
|
hdr.size = SAVEGAME_HEADER_SIZE;
|
|
|
|
hdr.ver = CURRENT_VER;
|
2012-02-22 22:14:29 +00:00
|
|
|
memcpy(hdr.desc, g_SaveSceneDesc, SG_DESC_LEN);
|
2008-07-30 08:23:04 +00:00
|
|
|
hdr.desc[SG_DESC_LEN - 1] = 0;
|
2008-07-23 09:02:47 +00:00
|
|
|
g_system->getTimeAndDate(hdr.dateTime);
|
2012-02-11 10:45:16 +00:00
|
|
|
hdr.scnFlag = _vm->getFeatures() & GF_SCNFILES;
|
|
|
|
hdr.language = _vm->_config->_language;
|
|
|
|
|
2009-05-19 11:42:14 +00:00
|
|
|
if (!syncSaveGameHeader(s, hdr) || f->err()) {
|
2011-12-08 14:27:28 +00:00
|
|
|
SaveFailure(f);
|
|
|
|
return;
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
2012-08-06 10:03:56 +00:00
|
|
|
DoSync(s, hdr.numInterpreters);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
// Write out the special Id for Discworld savegames
|
|
|
|
f->writeUint32LE(0xFEEDFACE);
|
2011-12-08 14:27:28 +00:00
|
|
|
if (f->err()) {
|
|
|
|
SaveFailure(f);
|
|
|
|
return;
|
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
f->finalize();
|
|
|
|
delete f;
|
2012-02-22 22:14:29 +00:00
|
|
|
g_SaveSceneName = NULL; // Invalidate save name
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ProcessSRQueue
|
|
|
|
*/
|
2009-11-02 21:54:57 +00:00
|
|
|
void ProcessSRQueue() {
|
2012-02-22 22:14:29 +00:00
|
|
|
switch (g_SRstate) {
|
2008-07-23 09:02:47 +00:00
|
|
|
case SR_DORESTORE:
|
2009-07-20 09:19:27 +00:00
|
|
|
// If a load has been done directly from title screens, set a larger value for scene ctr so the
|
|
|
|
// code used to skip the title screens in Discworld 1 gets properly disabled
|
2012-02-22 22:14:29 +00:00
|
|
|
if (g_sceneCtr < 10)
|
|
|
|
g_sceneCtr = 10;
|
2009-07-20 09:19:27 +00:00
|
|
|
|
2008-12-20 23:08:37 +00:00
|
|
|
if (DoRestore()) {
|
2012-02-22 22:14:29 +00:00
|
|
|
DoRestoreScene(g_srsd, false);
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
2012-02-22 22:14:29 +00:00
|
|
|
g_SRstate = SR_IDLE;
|
2008-07-23 09:02:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SR_DOSAVE:
|
|
|
|
DoSave();
|
2012-02-22 22:14:29 +00:00
|
|
|
g_SRstate = SR_IDLE;
|
2008-07-23 09:02:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RequestSaveGame(char *name, char *desc, SAVED_DATA *sd, int *pSsCount, SAVED_DATA *pSsData) {
|
2012-02-22 22:14:29 +00:00
|
|
|
assert(g_SRstate == SR_IDLE);
|
|
|
|
|
|
|
|
g_SaveSceneName = name;
|
|
|
|
g_SaveSceneDesc = desc;
|
|
|
|
g_SaveSceneSsCount = pSsCount;
|
|
|
|
g_SaveSceneSsData = pSsData;
|
|
|
|
g_srsd = sd;
|
|
|
|
g_SRstate = SR_DOSAVE;
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
2008-12-20 23:08:37 +00:00
|
|
|
void RequestRestoreGame(int num, SAVED_DATA *sd, int *pSsCount, SAVED_DATA *pSsData) {
|
2008-12-01 20:35:36 +00:00
|
|
|
if (TinselV2) {
|
|
|
|
if (num == -1)
|
|
|
|
return;
|
|
|
|
else if (num == -2) {
|
|
|
|
// From CD change for restore
|
2012-02-22 22:14:29 +00:00
|
|
|
num = g_RestoreGameNumber;
|
2008-12-01 20:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
assert(num >= 0);
|
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
g_RestoreGameNumber = num;
|
|
|
|
g_SaveSceneSsCount = pSsCount;
|
|
|
|
g_SaveSceneSsData = pSsData;
|
|
|
|
g_srsd = sd;
|
|
|
|
g_SRstate = SR_DORESTORE;
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
/**
|
|
|
|
* Returns the index of the most recently saved savegame. This will always be
|
|
|
|
* the file at the first index, since the list is sorted by date/time
|
|
|
|
*/
|
2009-11-02 21:54:57 +00:00
|
|
|
int NewestSavedGame() {
|
2008-12-01 20:35:36 +00:00
|
|
|
int numFiles = getList();
|
|
|
|
|
|
|
|
return (numFiles == 0) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2009-10-04 21:26:33 +00:00
|
|
|
} // End of namespace Tinsel
|