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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/* 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.
*
*/
#include "common/savefile.h"
#include "graphics/palette.h"
#include "graphics/scaler.h"
#include "graphics/thumbnail.h"
#include "tsage/globals.h"
#include "tsage/saveload.h"
#include "tsage/tsage.h"
namespace tSage {
Saver *_saver;
SavedObject::SavedObject() {
_saver->addObject(this);
}
SavedObject::~SavedObject() {
_saver->removeObject(this);
}
/*--------------------------------------------------------------------------*/
Saver::Saver() {
_macroSaveFlag = false;
_macroRestoreFlag = false;
}
Saver::~Saver() {
// Internal validation that no saved object is still present
int totalLost = 0;
for (SynchronizedList<SavedObject *>::iterator i = _saver->_objList.begin(); i != _saver->_objList.end(); ++i) {
SavedObject *so = *i;
if (so)
++totalLost;
}
if (totalLost)
warning("Saved object not destroyed");
}
/*--------------------------------------------------------------------------*/
void Serializer::syncPointer(SavedObject **ptr, Common::Serializer::Version minVersion,
Common::Serializer::Version maxVersion) {
int idx = 0;
assert(ptr);
if (isSaving()) {
// Get the object index for the given pointer and write it out
if (*ptr) {
idx = _saver->blockIndexOf(*ptr);
assert(idx > 0);
}
syncAsUint32LE(idx);
} else {
// Load in the object index and add it into the unresolved pointer list
syncAsUint32LE(idx);
*ptr = NULL;
if (idx > 0)
// For non-zero (null) pointers, create a record for later resolving it to an address
_saver->addSavedObjectPtr(ptr, idx);
}
}
void Serializer::validate(const Common::String &s, Common::Serializer::Version minVersion,
Common::Serializer::Version maxVersion) {
Common::String tempStr = s;
syncString(tempStr, minVersion, maxVersion);
if (isLoading() && (tempStr != s))
error("Savegame is corrupt");
}
void Serializer::validate(int v, Common::Serializer::Version minVersion,
Common::Serializer::Version maxVersion) {
int tempVal = v;
syncAsUint32LE(tempVal, minVersion, maxVersion);
if (isLoading() && (tempVal != v))
error("Savegame is corrupt");
}
/*--------------------------------------------------------------------------*/
Common::Error Saver::save(int slot, const Common::String &saveName) {
assert(!getMacroRestoreFlag());
// Signal any objects registered for notification
_saveNotifiers.notify(false);
// Set fields
_macroSaveFlag = true;
_saveSlot = slot;
// Set up the serializer
Common::OutSaveFile *saveFile = g_system->getSavefileManager()->openForSaving(_vm->generateSaveName(slot));
Serializer serializer(NULL, saveFile);
serializer.setSaveVersion(TSAGE_SAVEGAME_VERSION);
// Write out the savegame header
tSageSavegameHeader header;
header.saveName = saveName;
header.version = TSAGE_SAVEGAME_VERSION;
writeSavegameHeader(saveFile, header);
// Save out objects that need to come at the start of the savegame
for (SynchronizedList<SaveListener *>::iterator i = _listeners.begin(); i != _listeners.end(); ++i) {
(*i)->listenerSynchronize(serializer);
}
// Save each registered SaveObject descendant object into the savegame file
for (SynchronizedList<SavedObject *>::iterator i = _objList.begin(); i != _objList.end(); ++i) {
serializer.validate((*i)->getClassName());
(*i)->synchronize(serializer);
}
// Save file complete
saveFile->writeString("END");
saveFile->finalize();
delete saveFile;
// Final post-save notification
_macroSaveFlag = false;
_saveNotifiers.notify(true);
return Common::kNoError;
}
Common::Error Saver::restore(int slot) {
assert(!getMacroRestoreFlag());
// Signal any objects registered for notification
_loadNotifiers.notify(false);
// Set fields
_macroRestoreFlag = true;
_saveSlot = slot;
_unresolvedPtrs.clear();
// Set up the serializer
Common::InSaveFile *saveFile = g_system->getSavefileManager()->openForLoading(_vm->generateSaveName(slot));
Serializer serializer(saveFile, NULL);
// Read in the savegame header
tSageSavegameHeader header;
readSavegameHeader(saveFile, header);
delete header.thumbnail;
serializer.setSaveVersion(header.version);
// Load in data for objects that need to come at the start of the savegame
for (Common::List<SaveListener *>::iterator i = _listeners.begin(); i != _listeners.end(); ++i) {
(*i)->listenerSynchronize(serializer);
}
// Loop through each registered object to load in the data
for (SynchronizedList<SavedObject *>::iterator i = _objList.begin(); i != _objList.end(); ++i) {
serializer.validate((*i)->getClassName());
(*i)->synchronize(serializer);
}
// Loop through the remaining data of the file, instantiating new objects.
// Note: I don't store pointers to instantiated objects here, because it's not necessary - the mere act
// of instantiating a saved object registers it with the saver, and will then be resolved to whatever
// object originally had a pointer to it as part of the post-processing step
Common::String className;
serializer.syncString(className);
while (className != "END") {
SavedObject *savedObject;
if (!_factoryPtr || ((savedObject = _factoryPtr(className)) == NULL))
error("Unknown class name '%s' encountered trying to restore savegame", className.c_str());
// Populate the contents of the object
savedObject->synchronize(serializer);
// Move to next object
serializer.syncString(className);
}
// Post-process any unresolved pointers to get the correct pointer
resolveLoadPointers();
delete saveFile;
// Final post-restore notifications
_macroRestoreFlag = false;
_loadNotifiers.notify(false);
return Common::kNoError;
}
const char *SAVEGAME_STR = "SCUMMVM_TSAGE";
#define SAVEGAME_STR_SIZE 13
bool Saver::readSavegameHeader(Common::InSaveFile *in, tSageSavegameHeader &header) {
char saveIdentBuffer[SAVEGAME_STR_SIZE + 1];
header.thumbnail = NULL;
// Validate the header Id
in->read(saveIdentBuffer, SAVEGAME_STR_SIZE + 1);
if (strncmp(saveIdentBuffer, SAVEGAME_STR, SAVEGAME_STR_SIZE))
return false;
header.version = in->readByte();
if (header.version > TSAGE_SAVEGAME_VERSION)
return false;
// Read in the string
header.saveName.clear();
char ch;
while ((ch = (char)in->readByte()) != '\0') header.saveName += ch;
// Get the thumbnail
header.thumbnail = new Graphics::Surface();
if (!Graphics::loadThumbnail(*in, *header.thumbnail)) {
delete header.thumbnail;
header.thumbnail = NULL;
return false;
}
// Read in save date/time
header.saveYear = in->readSint16LE();
header.saveMonth = in->readSint16LE();
header.saveDay = in->readSint16LE();
header.saveHour = in->readSint16LE();
header.saveMinutes = in->readSint16LE();
header.totalFrames = in->readUint32LE();
return true;
}
void Saver::writeSavegameHeader(Common::OutSaveFile *out, tSageSavegameHeader &header) {
// Write out a savegame header
out->write(SAVEGAME_STR, SAVEGAME_STR_SIZE + 1);
out->writeByte(TSAGE_SAVEGAME_VERSION);
// Write savegame name
out->write(header.saveName.c_str(), header.saveName.size() + 1);
// Get the active palette
uint8 thumbPalette[256 * 3];
g_system->getPaletteManager()->grabPalette(thumbPalette, 0, 256);
// Create a thumbnail and save it
Graphics::Surface *thumb = new Graphics::Surface();
Graphics::Surface s = _globals->_screenSurface.lockSurface();
::createThumbnail(thumb, (const byte *)s.pixels, SCREEN_WIDTH, SCREEN_HEIGHT, thumbPalette);
Graphics::saveThumbnail(*out, *thumb);
_globals->_screenSurface.unlockSurface();
delete thumb;
// Write out the save date/time
TimeDate td;
g_system->getTimeAndDate(td);
out->writeSint16LE(td.tm_year + 1900);
out->writeSint16LE(td.tm_mon + 1);
out->writeSint16LE(td.tm_mday);
out->writeSint16LE(td.tm_hour);
out->writeSint16LE(td.tm_min);
out->writeUint32LE(_globals->_events.getFrameNumber());
}
/**
* Adds a serialisable object that should be saved/restored before any other objects
*/
void Saver::addListener(SaveListener *obj) {
_listeners.push_back(obj);
}
/**
* Adds a listener to be notified before the saving starts
*/
void Saver::addSaveNotifier(SaveNotifierFn fn) {
_saveNotifiers.push_back(fn);
}
/**
* Adds a listener to be notified before the saving starts
*/
void Saver::addLoadNotifier(SaveNotifierFn fn) {
_loadNotifiers.push_back(fn);
}
/**
* Registers a SavedObject descendant object for being saved in savegame files
*/
void Saver::addObject(SavedObject *obj) {
_objList.push_back(obj);
}
/**
* Removes a SavedObject descendant object from the save object list
*/
void Saver::removeObject(SavedObject *obj) {
_objList.remove(obj);
}
/**
* Returns true if any savegames exist
*/
bool Saver::savegamesExist() const {
Common::String slot1Name = _vm->generateSaveName(1);
Common::InSaveFile *saveFile = g_system->getSavefileManager()->openForLoading(slot1Name);
bool result = saveFile != NULL;
delete saveFile;
return result;
}
/**
* Returns the index of the saved block associated with the given saved object pointer
*/
int Saver::blockIndexOf(SavedObject *p) {
int objIndex = 1;
Common::List<SavedObject *>::iterator iObj;
for (iObj = _objList.begin(); iObj != _objList.end(); ++iObj, ++objIndex) {
SavedObject *iObjP = *iObj;
if (iObjP == p)
return objIndex;
}
return 0;
}
/**
* Returns the number of objects in the object list registry
*/
int Saver::getObjectCount() const {
return _objList.size();
}
/**
* List any currently active objects
*/
void Saver::listObjects() {
Common::List<SavedObject *>::iterator i;
int count = 1;
for (i = _objList.begin(); i != _objList.end(); ++i, ++count)
debug("%d - %s", count, (*i)->getClassName().c_str());
debugN("\n");
}
/**
* Returns the pointer associated with the specified object index
*/
void Saver::resolveLoadPointers() {
if (_unresolvedPtrs.size() == 0)
// Nothing to resolve
return;
// Outer loop through the main object list
int objIndex = 1;
for (SynchronizedList<SavedObject *>::iterator iObj = _objList.begin(); iObj != _objList.end(); ++iObj, ++objIndex) {
Common::List<SavedObjectRef>::iterator iPtr;
SavedObject *pObj = *iObj;
for (iPtr = _unresolvedPtrs.begin(); iPtr != _unresolvedPtrs.end(); ) {
SavedObjectRef &r = *iPtr;
if (r._objIndex == objIndex) {
// Found an unresolved pointer to this object
SavedObject **objPP = r._savedObject;
*objPP = pObj;
iPtr = _unresolvedPtrs.erase(iPtr);
} else {
++iPtr;
}
}
}
// At this point, all the unresolved pointers should have been resolved and removed
if (_unresolvedPtrs.size() > 0)
error("Could not resolve savegame block pointers");
}
} // End of namespace tSage
|