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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/* 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.
*
* $URL$
* $Id$
*
*/
#include "common/endian.h"
#include "common/savefile.h"
#include "gob/gob.h"
#include "gob/save/saveconverter.h"
#include "gob/save/savefile.h"
#include "gob/save/savehandler.h"
namespace Gob {
SaveConverter::SaveConverter(GobEngine *vm, const Common::String &fileName)
: _vm(vm), _fileName(fileName) {
_data = 0;
_stream = 0;
}
SaveConverter::~SaveConverter() {
delete _stream;
delete[] _data;
}
void SaveConverter::clear() {
delete[] _data;
delete _stream;
_data = 0;
_stream = 0;
}
void SaveConverter::setFileName(const Common::String &fileName) {
clear();
_fileName = fileName;
}
Common::InSaveFile *SaveConverter::openSave() const {
if (_fileName.empty())
return 0;
Common::SaveFileManager *saveMan = g_system->getSavefileManager();
return saveMan->openForLoading(_fileName);
}
void SaveConverter::displayWarning() const {
warning("Old save format detected, trying to convert. If this does not work, your "
"save is broken and can't be used anymore. Sorry for the inconvenience");
}
char *SaveConverter::getDescription(const Common::String &fileName) {
setFileName(fileName);
return getDescription();
}
char *SaveConverter::getDescription() const {
Common::InSaveFile *save;
// Test if it's an old savd
if (!isOldSave(&save) || !save)
return 0;
char *desc = getDescription(*save);
delete save;
return desc;
}
uint32 SaveConverter::getActualSize(Common::InSaveFile **save) const {
Common::InSaveFile *saveFile = openSave();
if (!saveFile)
return false;
// Is it a valid new save?
if (SaveContainer::isSave(*saveFile)) {
delete saveFile;
return false;
}
int32 saveSize = saveFile->size();
if (saveSize <= 0) {
delete saveFile;
return 0;
}
if (save)
*save = saveFile;
else
delete saveFile;
return saveSize;
}
bool SaveConverter::swapDataEndian(byte *data, const byte *sizes, uint32 count) {
if (!data || !sizes || (count == 0))
return false;
while (count-- > 0) {
if (*sizes == 3) // 32bit value (3 additional bytes)
WRITE_UINT32(data, SWAP_BYTES_32(READ_UINT32(data)));
else if (*sizes == 1) // 16bit value (1 additional byte)
WRITE_UINT16(data, SWAP_BYTES_16(READ_UINT16(data)));
else if (*sizes != 0) // else, it has to be an 8bit value
return false;
count -= *sizes;
data += *sizes + 1;
sizes += *sizes + 1;
}
return true;
}
SavePartInfo *SaveConverter::readInfo(Common::SeekableReadStream &stream,
uint32 descLength, bool hasSizes) const {
uint32 varSize = SaveHandler::getVarSize(_vm);
if (varSize == 0)
return 0;
char *desc = getDescription(stream);
if (!desc)
return 0;
// If it has sizes, skip them
if (hasSizes)
if (!stream.skip(descLength)) {
delete[] desc;
return 0;
}
SavePartInfo *info = new SavePartInfo(descLength, (uint32) _vm->getGameType(),
0, _vm->getEndianness(), varSize);
info->setDesc(desc);
delete[] desc;
return info;
}
byte *SaveConverter::readData(Common::SeekableReadStream &stream,
uint32 count, bool endian) const {
byte *data = new byte[count];
// Read variable data
if (stream.read(data, count) != count) {
delete[] data;
return 0;
}
/* Check the endianness. The old save data was always written
* as little endian, so we might need to swap the bytes. */
if (endian && (_vm->getEndianness() == kEndiannessBE)) {
// Big endian => swapping needed
// Read variable sizes
byte *sizes = new byte[count];
if (stream.read(sizes, count) != count) {
delete[] data;
delete[] sizes;
return 0;
}
// Swap bytes
if (!swapDataEndian(data, sizes, count)) {
delete[] data;
delete[] sizes;
return 0;
}
delete[] sizes;
} else {
// Little endian => just skip the sizes part
if (!stream.skip(count)) {
delete[] data;
return 0;
}
}
return data;
}
SavePartVars *SaveConverter::readVars(Common::SeekableReadStream &stream,
uint32 count, bool endian) const {
byte *data = readData(stream, count, endian);
if (!data)
return 0;
SavePartVars *vars = new SavePartVars(_vm, count);
// Read variables into part
if (!vars->readFromRaw(data, count)) {
delete[] data;
delete vars;
return 0;
}
delete[] data;
return vars;
}
SavePartMem *SaveConverter::readMem(Common::SeekableReadStream &stream,
uint32 count, bool endian) const {
byte *data = readData(stream, count, endian);
if (!data)
return 0;
SavePartMem *mem = new SavePartMem(count);
// Read mem into part
if (!mem->readFrom(data, 0, count)) {
delete[] data;
delete mem;
return 0;
}
delete[] data;
return mem;
}
SavePartSprite *SaveConverter::readSprite(Common::SeekableReadStream &stream,
uint32 width, uint32 height, bool palette) const {
assert((width > 0) && (height > 0));
uint32 spriteSize = width * height;
byte pal[768];
if (palette)
if (stream.read(pal, 768) != 768)
return 0;
byte *data = new byte[spriteSize];
// Read variable data
if (stream.read(data, spriteSize) != spriteSize) {
delete[] data;
return 0;
}
SavePartSprite *sprite = new SavePartSprite(width, height);
if (!sprite->readSpriteRaw(data, spriteSize)) {
delete[] data;
return 0;
}
delete[] data;
if (palette)
if (!sprite->readPalette(pal))
return 0;
return sprite;
}
bool SaveConverter::createStream(SaveWriter &writer) {
// Allocate memory for the internal new save data
uint32 contSize = writer.getSize();
_data = new byte[contSize];
// Save the newly created new save data
Common::MemoryWriteStream writeStream(_data, contSize);
if (!writer.save(writeStream))
return false;
// Create a reading stream upon that new save data
_stream = new Common::MemoryReadStream(_data, contSize);
return true;
}
/* Stream functions. If the new save data stream is available, redirect the stream
* operations to that stream. Normal stream error behaviour if not. */
bool SaveConverter::err() const {
if (!_data || !_stream)
return true;
return _stream->err();
}
void SaveConverter::clearErr() {
if (!_data || !_stream)
return;
_stream->clearErr();
}
bool SaveConverter::eos() const {
if (!_data || !_stream)
return true;
return _stream->eos();
}
uint32 SaveConverter::read(void *dataPtr, uint32 dataSize) {
if (!_data || !_stream)
return 0;
return _stream->read(dataPtr, dataSize);
}
int32 SaveConverter::pos() const {
if (!_data || !_stream)
return -1;
return _stream->pos();
}
int32 SaveConverter::size() const {
if (!_data || !_stream)
return -1;
return _stream->size();
}
bool SaveConverter::seek(int32 offset, int whence) {
if (!_data || !_stream)
return false;
return _stream->seek(offset, whence);
}
SaveConverter_Notes::SaveConverter_Notes(GobEngine *vm, uint32 notesSize,
const Common::String &fileName) : SaveConverter(vm, fileName) {
_size = notesSize;
}
SaveConverter_Notes::~SaveConverter_Notes() {
}
int SaveConverter_Notes::isOldSave(Common::InSaveFile **save) const {
if (_size == 0)
return 0;
uint32 saveSize = getActualSize(save);
if (saveSize == 0)
return 0;
// The size of the old save always follows that rule
if (saveSize == (_size * 2))
return 1;
// Not an old save, clean up
if (save) {
delete *save;
*save = 0;
}
return 0;
}
char *SaveConverter_Notes::getDescription(Common::SeekableReadStream &save) const {
return 0;
}
bool SaveConverter_Notes::loadFail(SavePartVars *vars, Common::InSaveFile *save) {
delete vars;
delete save;
clear();
return false;
}
// Loads the old save by constructing a new save containing the old save's data
bool SaveConverter_Notes::load() {
if (_size == 0)
return false;
Common::InSaveFile *save;
// Test if it's an old savd
if (!isOldSave(&save) || !save)
return false;
displayWarning();
SaveWriter writer(1, 0);
SavePartVars *vars = readVars(*save, _size, false);
if (!vars)
return loadFail(0, save);
// We don't need the save anymore
delete save;
// Write all parts
if (!writer.writePart(0, vars))
return loadFail(0, 0);
// We don't need this anymore
delete vars;
// Create the final read stream
if (!createStream(writer))
return loadFail(0, 0);
return true;
}
} // End of namespace Gob
|