Moved Tinsel::Serializer to Common::Serializer, so that I can use it in the SCI engine

svn-id: r39428
This commit is contained in:
Max Horn 2009-03-15 20:30:57 +00:00
parent 77d5d3093a
commit 9cbed926cd
12 changed files with 81 additions and 51 deletions

View file

@ -21,17 +21,15 @@
* $URL$
* $Id$
*
* Handles timers.
*/
#ifndef TINSEL_SERIALIZER_H
#define TINSEL_SERIALIZER_H
#ifndef COMMON_SERIALIZER_H
#define COMMON_SERIALIZER_H
#include "common/scummsys.h"
#include "common/savefile.h"
#include "common/stream.h"
#include "common/str.h"
namespace Tinsel {
namespace Common {
#define SYNC_AS(SUFFIX,TYPE,SIZE) \
@ -51,7 +49,7 @@ namespace Tinsel {
// TODO: Inspired by the SCUMM engine -- move to common/ code and use in more engines?
class Serializer {
public:
Serializer(Common::SeekableReadStream *in, Common::OutSaveFile *out)
Serializer(Common::SeekableReadStream *in, Common::WriteStream *out)
: _loadStream(in), _saveStream(out), _bytesSynced(0) {
assert(in || out);
}
@ -69,14 +67,33 @@ public:
_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;
}
}
void skip(uint32 size) {
_bytesSynced += size;
if (_loadStream)
_loadStream->skip(size);
else {
while (size--)
_saveStream->writeByte(0);
}
_bytesSynced += size;
}
SYNC_AS(Byte, byte, 1)
@ -93,7 +110,7 @@ public:
protected:
Common::SeekableReadStream *_loadStream;
Common::OutSaveFile *_saveStream;
Common::WriteStream *_saveStream;
uint _bytesSynced;
};
@ -132,10 +149,12 @@ public:
class Serializable {
public:
virtual ~Serializable() {}
virtual void saveLoadWithSerializer(Serializer *ser) = 0;
// Maybe rename this method to "syncWithSerializer" or "syncUsingSerializer" ?
virtual void saveLoadWithSerializer(Serializer &ser) = 0;
};
} // end of namespace Tinsel
} // end of namespace Common
#endif

View file

@ -154,6 +154,10 @@ public:
writeUint32BE((uint32)value);
}
/**
* Write the given string to the stream.
* This writes str.size() characters, but no terminating zero byte.
*/
void writeString(const String &str);
};

View file

@ -39,7 +39,7 @@
#include "tinsel/polygons.h"
#include "tinsel/rince.h"
#include "tinsel/sched.h"
#include "tinsel/serializer.h"
#include "common/serializer.h"
#include "tinsel/sysvar.h"
#include "tinsel/tinsel.h"
#include "tinsel/token.h"
@ -1417,7 +1417,7 @@ void ActorsLife(int ano, bool bAlive) {
}
void syncAllActorsAlive(Serializer &s) {
void syncAllActorsAlive(Common::Serializer &s) {
for (int i = 0; i < MAX_SAVED_ALIVES; i++) {
s.syncAsByte(actorInfo[i].bAlive);
s.syncAsByte(actorInfo[i].tagged);

View file

@ -50,7 +50,7 @@
#include "tinsel/polygons.h"
#include "tinsel/savescn.h"
#include "tinsel/sched.h"
#include "tinsel/serializer.h"
#include "common/serializer.h"
#include "tinsel/sound.h"
#include "tinsel/strres.h"
#include "tinsel/sysvar.h"
@ -5475,7 +5475,7 @@ void SetObjectFilm(int object, SCNHANDLE hFilm) {
/**
* (Un)serialize the inventory data for save/restore game.
*/
void syncInvInfo(Serializer &s) {
void syncInvInfo(Common::Serializer &s) {
for (int i = 0; i < NUM_INV; i++) {
s.syncAsSint32LE(InvD[i].MinHicons);
s.syncAsSint32LE(InvD[i].MinVicons);

View file

@ -31,9 +31,11 @@
#include "tinsel/dw.h"
#include "tinsel/events.h" // for PLR_EVENT, PLR_EVENT
namespace Tinsel {
namespace Common {
class Serializer;
}
namespace Tinsel {
enum {
INV_OPEN = -1, // DW1 only
@ -146,7 +148,7 @@ bool IsInInventory(int object, int invnum);
void KillInventory(void);
void syncInvInfo(Serializer &s);
void syncInvInfo(Common::Serializer &s);
int InvGetLimit(int invno);
void InvSetLimit(int invno, int n);

View file

@ -31,7 +31,7 @@
#include "tinsel/dialogs.h" // for inventory id's
#include "tinsel/pcode.h" // opcodes etc.
#include "tinsel/scn.h" // FindChunk()
#include "tinsel/serializer.h"
#include "common/serializer.h"
#include "tinsel/timers.h"
#include "tinsel/tinlib.h" // Library routines
#include "tinsel/tinsel.h"
@ -356,7 +356,7 @@ void FreeGlobals(void) {
/**
* (Un)serialize the global data for save/restore game.
*/
void syncGlobInfo(Serializer &s) {
void syncGlobInfo(Common::Serializer &s) {
for (int i = 0; i < numGlobals; i++) {
s.syncAsSint32LE(pGlobals[i]);
}
@ -365,7 +365,7 @@ void syncGlobInfo(Serializer &s) {
/**
* (Un)serialize an interpreter context for save/restore game.
*/
void INT_CONTEXT::syncWithSerializer(Serializer &s) {
void INT_CONTEXT::syncWithSerializer(Common::Serializer &s) {
if (s.isLoading()) {
// Null out the pointer fields
pProc = NULL;

View file

@ -30,10 +30,13 @@
#include "tinsel/events.h" // for TINSEL_EVENT
#include "tinsel/sched.h" // for PROCESS
namespace Common {
class Serializer;
}
namespace Tinsel {
// forward declaration
class Serializer;
struct INV_OBJECT;
enum RESUME_STATE {
@ -79,7 +82,7 @@ struct INT_CONTEXT {
RESCODE resumeCode;
RESUME_STATE resumeState;
void syncWithSerializer(Serializer &s);
void syncWithSerializer(Common::Serializer &s);
};
typedef INT_CONTEXT *PINT_CONTEXT;

View file

@ -30,7 +30,7 @@
#include "tinsel/polygons.h"
#include "tinsel/rince.h"
#include "tinsel/sched.h"
#include "tinsel/serializer.h"
#include "common/serializer.h"
#include "tinsel/tinsel.h"
#include "tinsel/token.h"
@ -1198,7 +1198,7 @@ void RebootDeadTags(void) {
/**
* (Un)serialize the dead tag and exit data for save/restore game.
*/
void syncPolyInfo(Serializer &s) {
void syncPolyInfo(Common::Serializer &s) {
int i;
for (i = 0; i < MAX_SCENES; i++) {

View file

@ -30,11 +30,11 @@
#include "tinsel/dw.h"
#include "tinsel/rince.h"
#include "tinsel/savescn.h"
#include "tinsel/serializer.h"
#include "tinsel/timers.h"
#include "tinsel/tinlib.h"
#include "tinsel/tinsel.h"
#include "common/serializer.h"
#include "common/savefile.h"
namespace Tinsel {
@ -73,16 +73,16 @@ SRSTATE SRstate = SR_IDLE;
//----------------- EXTERN FUNCTIONS --------------------
// in DOS_DW.C
extern void syncSCdata(Serializer &s);
extern void syncSCdata(Common::Serializer &s);
// in DOS_MAIN.C
//char HardDriveLetter(void);
// in PCODE.C
extern void syncGlobInfo(Serializer &s);
extern void syncGlobInfo(Common::Serializer &s);
// in POLYGONS.C
extern void syncPolyInfo(Serializer &s);
extern void syncPolyInfo(Common::Serializer &s);
//----------------- LOCAL DEFINES --------------------
@ -120,7 +120,7 @@ static char *SaveSceneSsData = 0; // points to 'SAVED_DATA ssdata[MAX_NEST]'
void setNeedLoad() { NeedLoad = true; }
static void syncTime(Serializer &s, struct tm &t) {
static void syncTime(Common::Serializer &s, struct tm &t) {
s.syncAsUint16LE(t.tm_year);
s.syncAsByte(t.tm_mon);
s.syncAsByte(t.tm_mday);
@ -134,7 +134,7 @@ static void syncTime(Serializer &s, struct tm &t) {
}
}
static bool syncSaveGameHeader(Serializer &s, SaveGameHeader &hdr) {
static bool syncSaveGameHeader(Common::Serializer &s, SaveGameHeader &hdr) {
s.syncAsUint32LE(hdr.id);
s.syncAsUint32LE(hdr.size);
s.syncAsUint32LE(hdr.ver);
@ -153,7 +153,7 @@ static bool syncSaveGameHeader(Serializer &s, SaveGameHeader &hdr) {
return true;
}
static void syncSavedMover(Serializer &s, SAVED_MOVER &sm) {
static void syncSavedMover(Common::Serializer &s, SAVED_MOVER &sm) {
SCNHANDLE *pList[3] = { (SCNHANDLE *)&sm.walkReels,
(SCNHANDLE *)&sm.standReels, (SCNHANDLE *)&sm.talkReels };
@ -178,7 +178,7 @@ static void syncSavedMover(Serializer &s, SAVED_MOVER &sm) {
}
}
static void syncSavedActor(Serializer &s, SAVED_ACTOR &sa) {
static void syncSavedActor(Common::Serializer &s, SAVED_ACTOR &sa) {
s.syncAsUint16LE(sa.actorID);
s.syncAsUint16LE(sa.zFactor);
s.syncAsUint32LE(sa.bAlive);
@ -188,33 +188,33 @@ static void syncSavedActor(Serializer &s, SAVED_ACTOR &sa) {
s.syncAsUint16LE(sa.presPlayY);
}
extern void syncAllActorsAlive(Serializer &s);
extern void syncAllActorsAlive(Common::Serializer &s);
static void syncNoScrollB(Serializer &s, NOSCROLLB &ns) {
static void syncNoScrollB(Common::Serializer &s, NOSCROLLB &ns) {
s.syncAsSint32LE(ns.ln);
s.syncAsSint32LE(ns.c1);
s.syncAsSint32LE(ns.c2);
}
static void syncZPosition(Serializer &s, Z_POSITIONS &zp) {
static void syncZPosition(Common::Serializer &s, Z_POSITIONS &zp) {
s.syncAsSint16LE(zp.actor);
s.syncAsSint16LE(zp.column);
s.syncAsSint32LE(zp.z);
}
static void syncPolyVolatile(Serializer &s, POLY_VOLATILE &p) {
static void syncPolyVolatile(Common::Serializer &s, POLY_VOLATILE &p) {
s.syncAsByte(p.bDead);
s.syncAsSint16LE(p.xoff);
s.syncAsSint16LE(p.yoff);
}
static void syncSoundReel(Serializer &s, SOUNDREELS &sr) {
static void syncSoundReel(Common::Serializer &s, SOUNDREELS &sr) {
s.syncAsUint32LE(sr.hFilm);
s.syncAsSint32LE(sr.column);
s.syncAsSint32LE(sr.actorCol);
}
static void syncSavedData(Serializer &s, SAVED_DATA &sd) {
static void syncSavedData(Common::Serializer &s, SAVED_DATA &sd) {
s.syncAsUint32LE(sd.SavedSceneHandle);
s.syncAsUint32LE(sd.SavedBgroundHandle);
for (int i = 0; i < MAX_MOVERS; ++i)
@ -324,7 +324,7 @@ int getList(Common::SaveFileManager *saveFileMan, const Common::String &target)
}
// Try to load save game header
Serializer s(f, 0);
Common::Serializer s(f, 0);
SaveGameHeader hdr;
bool validHeader = syncSaveGameHeader(s, hdr);
delete f;
@ -379,7 +379,7 @@ char *ListEntry(int i, letype which) {
return NULL;
}
static void DoSync(Serializer &s) {
static void DoSync(Common::Serializer &s) {
int sg = 0;
if (TinselV2) {
@ -433,7 +433,7 @@ static bool DoRestore() {
return false;
}
Serializer s(f, 0);
Common::Serializer s(f, 0);
SaveGameHeader hdr;
if (!syncSaveGameHeader(s, hdr)) {
delete f; // Invalid header, or savegame too new -> skip it
@ -474,7 +474,7 @@ static void DoSave(void) {
if (f == NULL)
return;
Serializer s(0, f);
Common::Serializer s(0, f);
// Write out a savegame header
SaveGameHeader hdr;

View file

@ -30,7 +30,7 @@
#include "tinsel/timers.h"
#include "tinsel/dw.h"
#include "tinsel/serializer.h"
#include "common/serializer.h"
#include "common/system.h"
@ -81,7 +81,7 @@ void RebootTimers(void) {
/**
* (Un)serialize the timer data for save/restore game.
*/
void syncTimerInfo(Serializer &s) {
void syncTimerInfo(Common::Serializer &s) {
for (int i = 0; i < MAX_TIMERS; i++) {
s.syncAsSint32LE(timers[i].tno);
s.syncAsSint32LE(timers[i].ticks);

View file

@ -30,9 +30,11 @@
#include "common/scummsys.h"
#include "tinsel/dw.h"
namespace Tinsel {
namespace Common {
class Serializer;
}
namespace Tinsel {
#define ONE_SECOND 24
@ -40,7 +42,7 @@ uint32 DwGetCurrentTime(void);
void RebootTimers(void);
void syncTimerInfo(Serializer &s);
void syncTimerInfo(Common::Serializer &s);
void FettleTimers(void);

View file

@ -30,6 +30,7 @@
#include "common/file.h"
#include "common/savefile.h"
#include "common/config-manager.h"
#include "common/serializer.h"
#include "common/stream.h"
#include "graphics/cursorman.h"
@ -60,7 +61,6 @@
#include "tinsel/polygons.h"
#include "tinsel/savescn.h"
#include "tinsel/scn.h"
#include "tinsel/serializer.h"
#include "tinsel/sound.h"
#include "tinsel/strres.h"
#include "tinsel/sysvar.h"
@ -614,7 +614,7 @@ void UnSuspendHook(void) {
bCuttingScene = false;
}
void syncSCdata(Serializer &s) {
void syncSCdata(Common::Serializer &s) {
s.syncAsUint32LE(HookScene.scene);
s.syncAsSint32LE(HookScene.entry);
s.syncAsSint32LE(HookScene.trans);