aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/save/savehandler.cpp
blob: 8cb8274402a21be46290230f70b0bedc164c7175 (plain)
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/* 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 "gob/gob.h"
#include "gob/save/savehandler.h"
#include "gob/save/savefile.h"
#include "gob/save/saveconverter.h"
#include "gob/global.h"
#include "gob/video.h"
#include "gob/draw.h"
#include "gob/variables.h"
#include "gob/inter.h"

namespace Gob {

SlotFile::SlotFile(GobEngine *vm, uint32 slotCount, const Common::String &base) : _vm(vm) {
	_base = base;
	_slotCount = slotCount;
}

SlotFile::~SlotFile() {
}

uint32 SlotFileIndexed::getSlotMax() const {
	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	Common::InSaveFile *in;

	// Find the last filled save slot and base the save file size calculate on that
	for (int i = (_slotCount - 1); i >= 0; i--) {
		Common::String slotFile = build(i);

		if (slotFile.empty())
			continue;

		in = saveMan->openForLoading(slotFile);

		if (in) {
			delete in;
			return i + 1;
		}
	}

	return 0;
}

int32 SlotFileIndexed::tallyUpFiles(uint32 slotSize, uint32 indexSize) const {
	uint32 maxSlot = getSlotMax();

	if (maxSlot == 0)
		return -1;

	return ((maxSlot * slotSize) + indexSize);
}

void SlotFileIndexed::buildIndex(byte *buffer, SavePartInfo &info,
		SaveConverter *converter, bool setLongest) const {

	uint32 descLength = info.getDescMaxLength();

	uint32 longest     = 0;
	byte  *bufferStart = buffer;

	// Iterate over all files
	for (uint32 i = 0; i < _slotCount; i++, buffer += descLength) {
		Common::String slotFile = build(i);

		if (!slotFile.empty()) {
			char *desc = 0;

			if (converter && (desc = converter->getDescription(slotFile)))
				// Old style save
				memcpy(buffer, desc, descLength);
			else if (SaveReader::getInfo(slotFile, info))
				// New style save
				memcpy(buffer, info.getDesc(), descLength);
			else
				// No known format, fill with 0
				memset(buffer, 0, descLength);

			delete[] desc;

			longest = MAX<uint32>(longest, strlen((const char *) buffer));

		} else
			// No valid slot, fill with 0
			memset(buffer, 0, descLength);
	}

	if (setLongest) {
		uint32 slot0Len;
		for (slot0Len = strlen((const char *) bufferStart); slot0Len < longest; slot0Len++)
			buffer[slot0Len] = ' ';
		buffer[slot0Len] = '\0';
	}
}

bool SlotFileIndexed::exists(int slot) const {
	Common::InSaveFile *in = openRead(slot);
	bool result = (in != 0);
	delete in;
	return result;
}

Common::InSaveFile *SlotFileIndexed::openRead(int slot) const {
	Common::String name = build(slot);
	if (name.empty())
		return 0;
	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	Common::InSaveFile *result = saveMan->openForLoading(name);
	return result;
}

Common::OutSaveFile *SlotFileIndexed::openWrite(int slot) const {
	Common::String name = build(slot);
	if (name.empty())
		return 0;
	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	Common::OutSaveFile *result = saveMan->openForSaving(name);
	return result;
}


SlotFileIndexed::SlotFileIndexed(GobEngine *vm, uint32 slotCount,
		const Common::String &base, const Common::String &extStub) : SlotFile(vm, slotCount, base) {

	_ext = extStub;
}

SlotFileIndexed::~SlotFileIndexed() {
}

Common::String SlotFileIndexed::build(int slot) const {
	if ((slot < 0) || (((uint32) slot) >= _slotCount))
		return Common::String();

	char buf[4];
	snprintf(buf, sizeof(buf), "%02d", slot);

	return _base + "." + _ext + buf;
}

SlotFileStatic::SlotFileStatic(GobEngine *vm, const Common::String &base,
		const Common::String &ext) : SlotFile(vm, 1, base) {

	_ext = "." + ext;
}

SlotFileStatic::~SlotFileStatic() {
}

int SlotFileStatic::getSlot(int32 offset) const {
	return -1;
}

int SlotFileStatic::getSlotRemainder(int32 offset) const {
	return -1;
}

Common::String SlotFileStatic::build() const {
	return _base + _ext;
}

bool SlotFileStatic::exists() const {
	Common::InSaveFile *in = openRead();
	bool result = (in != 0);
	delete in;
	return result;
}

Common::InSaveFile *SlotFileStatic::openRead() const {
	Common::String name = build();
	if (name.empty())
		return 0;
	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	Common::InSaveFile *result = saveMan->openForLoading(name);
	return result;
}

Common::OutSaveFile *SlotFileStatic::openWrite() const {
	Common::String name = build();
	if (name.empty())
		return 0;
	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	Common::OutSaveFile *result = saveMan->openForSaving(name);
	return result;
}


SaveHandler::SaveHandler(GobEngine *vm) : _vm(vm) {
}

SaveHandler::~SaveHandler() {
}

uint32 SaveHandler::getVarSize(GobEngine *vm) {
	// Sanity checks
	if (!vm || !vm->_inter || !vm->_inter->_variables)
		return 0;

	return vm->_inter->_variables->getSize();
}

bool SaveHandler::deleteFile() {
	return true;
}


TempSpriteHandler::TempSpriteHandler(GobEngine *vm) : SaveHandler(vm) {
	_sprite = 0;
}

TempSpriteHandler::~TempSpriteHandler() {
	delete _sprite;
}

int32 TempSpriteHandler::getSize() {
	if (!_sprite)
		return -1;

	return _sprite->getSize();
}

bool TempSpriteHandler::load(int16 dataVar, int32 size, int32 offset) {
	if (isDummy(size))
		return true;

	// Sprite available?
	if (!_sprite)
		return false;

	// Sprite requested?
	if (!isSprite(size))
		return false;

	// Index sane?
	int index = getIndex(size);
	if ((index < 0) || (index >= Draw::kSpriteCount))
		return false;

	SurfacePtr sprite = _vm->_draw->_spritesArray[index];

	// Target sprite exists?
	if (!sprite)
		return false;

	// Load the sprite
	if (!_sprite->writeSprite(*sprite))
		return false;

	// Handle palette
	if (usesPalette(size)) {
		if (!_sprite->writePalette((byte *)_vm->_global->_pPaletteDesc->vgaPal))
			return false;

		_vm->_video->setFullPalette(_vm->_global->_pPaletteDesc);
	}

	if (index == 21) {
		// We wrote into the backbuffer, blit
		_vm->_draw->forceBlit();
		_vm->_video->retrace();
	} else if (index == 20)
		// We wrote into the frontbuffer, retrace
		_vm->_video->retrace();

	return true;
}

bool TempSpriteHandler::save(int16 dataVar, int32 size, int32 offset) {
	if (isDummy(size))
		return true;

	SurfacePtr sprite = createSprite(dataVar, size, offset);
	if (!sprite)
		return false;

	// Save the sprite
	if (!_sprite->readSprite(*sprite))
		return false;

	// Handle palette
	if (usesPalette(size))
		if (!_sprite->readPalette((const byte *)_vm->_global->_pPaletteDesc->vgaPal))
			return false;

	return true;
}

bool TempSpriteHandler::create(uint32 width, uint32 height, bool trueColor) {
	delete _sprite;
	_sprite = 0;

	// Create a new temporary sprite
	_sprite = new SavePartSprite(width, height, trueColor);

	return true;
}

bool TempSpriteHandler::createFromSprite(int16 dataVar, int32 size, int32 offset) {
	return createSprite(dataVar, size, offset) != 0;
}

SurfacePtr TempSpriteHandler::createSprite(int16 dataVar, int32 size, int32 offset) {
	SurfacePtr sprt;

	// Sprite requested?
	if (!isSprite(size))
		return sprt;

	// Index sane?
	int index = getIndex(size);
	if ((index < 0) || (index >= Draw::kSpriteCount))
		return sprt;

	// Sprite exists?
	if (!(sprt = _vm->_draw->_spritesArray[index]))
		return sprt;

	if (!create(sprt->getWidth(), sprt->getHeight(), sprt->getBPP() > 1))
		sprt.reset();

	return sprt;
}

// A size of 0 means no proper sprite should be saved/loaded,
// but no error should be thrown either.
bool TempSpriteHandler::isDummy(int32 size) {
	return (size == 0);
}

// A negative size is the flag for using a sprite
bool TempSpriteHandler::isSprite(int32 size) {
	return (size < 0);
}

// Contruct the index
int TempSpriteHandler::getIndex(int32 size) {
	// Palette flag
	if (size < -3000)
		size += 3000;
	if (size < -1000)
		size += 1000;

	return (-size - 1);
}

// A size smaller than -1000 indicates palette usage
bool TempSpriteHandler::usesPalette(int32 size) {
	return (size < -1000);
}


NotesHandler::File::File(GobEngine *vm, const Common::String &base) :
	SlotFileStatic(vm, base, "blo") {
}

NotesHandler::File::~File() {
}

NotesHandler::NotesHandler(uint32 notesSize, GobEngine *vm, const Common::String &target) :
	SaveHandler(vm) {

	_notesSize = notesSize;

	_file = new File(vm, target);

	_notes = new SavePartVars(vm, _notesSize);
}

NotesHandler::~NotesHandler() {
	delete _file;
	delete _notes;
}

int32 NotesHandler::getSize() {
	Common::String fileName = _file->build();

	if (fileName.empty())
		return -1;

	Common::InSaveFile *saveFile;

	SaveConverter_Notes converter(_vm, _notesSize, fileName);
	if (converter.isOldSave(&saveFile)) {
		// Old save, get the size olden-style

		int32 size = saveFile->size();

		delete saveFile;
		return size;
	}

	SaveReader reader(1, 0, fileName);
	SaveHeader header;

	if (!reader.load())
		return -1;

	if (!reader.readPartHeader(0, &header))
		return -1;

	// Return the part's size
	return header.getSize();
}

bool NotesHandler::load(int16 dataVar, int32 size, int32 offset) {
	if ((dataVar < 0) || (size < 0) || (offset < 0))
		return false;

	Common::String fileName = _file->build();

	if (fileName.empty())
		return false;

	SaveReader *reader;

	SaveConverter_Notes converter(_vm, _notesSize, fileName);
	if (converter.isOldSave()) {
		// Old save, plug the converter in
		if (!converter.load())
			return false;

		reader = new SaveReader(1, 0, converter);

	} else
		// New save, load directly
		reader = new SaveReader(1, 0, fileName);

	SavePartVars vars(_vm, _notesSize);

	if (!reader->load()) {
		delete reader;
		return false;
	}

	if (!reader->readPart(0, &vars)) {
		delete reader;
		return false;
	}

	if (!vars.writeInto(dataVar, offset, size)) {
		delete reader;
		return false;
	}

	delete reader;
	return true;
}

bool NotesHandler::save(int16 dataVar, int32 size, int32 offset) {
	if ((dataVar < 0) || (size < 0) || (offset < 0))
		return false;

	Common::String fileName = _file->build();

	if (fileName.empty())
		return false;

	SaveWriter writer(1, 0, fileName);
	SavePartVars vars(_vm, _notesSize);

	if (!vars.readFrom(dataVar, offset, size))
		return false;

	return writer.writePart(0, &vars);
}


FakeFileHandler::FakeFileHandler(GobEngine *vm) : SaveHandler(vm) {
}

FakeFileHandler::~FakeFileHandler() {
}

int32 FakeFileHandler::getSize() {
	if (_data.empty())
		return -1;

	return _data.size();
}

bool FakeFileHandler::load(int16 dataVar, int32 size, int32 offset) {
	if (size <= 0)
		return false;

	if ((uint32)(offset + size) > _data.size())
		return false;

	_vm->_inter->_variables->copyFrom(dataVar, &_data[0] + offset, size);

	return true;
}

bool FakeFileHandler::save(int16 dataVar, int32 size, int32 offset) {
	if (size <= 0)
		return false;

	if ((uint32)(offset + size) > _data.size())
		_data.resize(offset + size);

	_vm->_inter->_variables->copyTo(dataVar, &_data[0] + offset, size);

	return true;
}

bool FakeFileHandler::deleteFile() {
	_data.clear();

	return true;
}

} // End of namespace Gob