aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/saveload.cpp
blob: 50f41db47e11b6a0aaf27e847604306a00a11c45 (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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/* 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/saveload.h"
#include "gob/draw.h"

namespace Gob {

TempSprite::TempSprite() {
	_sprite = 0;
	_width = _height = 0;
	_size = -1;
	memset(_palette, 0, 768);
}

TempSprite::~TempSprite() {
	delete[] _sprite;
}

int TempSprite::getSpriteIndex(int32 size) const {
	if (size < -1000)
		size += 1000;

	return -size - 1;
}

bool TempSprite::getSpritePalette(int32 size) const {
	return size < -1000;
}

bool TempSprite::getProperties(int16 dataVar, int32 size, int32 offset,
		int &index, bool &palette) const {

	if (size >= 0) {
		warning("Invalid index (%d)", size);
		return false;
	}

	index = getSpriteIndex(size);
	palette = getSpritePalette(size);

	if ((index < 0) || (index >= SPRITES_COUNT)) {
		warning("Index out of range (%d)", index);
		return false;
	}

	return true;
}

int32 TempSprite::getSize() const {
	return _size;
}

bool TempSprite::saveSprite(const SurfaceDesc &surfDesc) {
	delete[] _sprite;

	_width = surfDesc.getWidth();
	_height = surfDesc.getHeight();
	_size = _width * _height;
	_sprite = new byte[_size];

	memcpy(_sprite, surfDesc.getVidMem(), _size);

	return true;
}

bool TempSprite::savePalette(const Video::Color *palette) {
	memcpy((byte *) _palette, (byte *) palette, 768);
	return true;
}

bool TempSprite::loadSprite(SurfaceDesc &surfDesc) {
	if (!_sprite) {
		warning("No sprite saved");
		return false;
	}

	if (_size != (surfDesc.getWidth() * surfDesc.getHeight())) {
		warning("Dimensions don't match (%dx%d - %dx%d",
				_width, _height, surfDesc.getWidth(), surfDesc.getHeight());
		return false;
	}

	memcpy(surfDesc.getVidMem(), _sprite, _size);

	return true;
}

bool TempSprite::loadPalette(Video::Color *palette) {
	memcpy((byte *) palette, (byte *) _palette, 768);
	return true;
}

bool TempSprite::toBuffer(byte *buffer, int32 size, bool palette) const {

	int32 haveSize = _size + (palette ? 768 : 0);
	if (size != haveSize) {
		warning("Sizes don't match (%d != %d)", size, haveSize);
		return false;
	}

	if (palette) {
		memcpy(buffer, (byte *) _palette, 768);
		buffer += 768;
	}

	memcpy(buffer, _sprite, _size);

	return true;
}

bool TempSprite::fromBuffer(const byte *buffer, int32 size, bool palette) {
	if (palette) {
		memcpy((byte *) _palette, buffer, 768);
		buffer += 768;
		size -= 768;
	}

	_size = size;

	delete[] _sprite;
	_sprite = new byte[_size];

	memcpy(_sprite, buffer, _size);

	return true;
}


PlainSave::PlainSave() {
}

PlainSave::~PlainSave() {
}

bool PlainSave::save(int16 dataVar, int32 size, int32 offset, const char *name,
		const byte *variables, const byte *variableSizes) const {

	if ((size <= 0) || (offset != 0)) {
		warning("Invalid size (%d) or offset (%d)", size, offset);
		return false;
	}

	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	Common::OutSaveFile *out = saveMan->openForSaving(name);

	if (!out) {
		warning("Can't open file \"%s\" for writing", name);
		return false;
	}

	bool retVal;
	retVal = SaveLoad::saveDataEndian(*out, dataVar, size, variables, variableSizes);

	out->finalize();
	if (out->ioFailed()) {
		warning("Can't write to file \"%s\"", name);
		retVal = false;
	}

	delete out;
	return retVal;
}

bool PlainSave::load(int16 dataVar, int32 size, int32 offset, const char *name,
		byte *variables, byte *variableSizes) const {

	if ((size <= 0) || (offset != 0)) {
		warning("Invalid size (%d) or offset (%d)", size, offset);
		return false;
	}

	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	Common::InSaveFile *in = saveMan->openForLoading(name);

	if (!in) {
		warning("Can't open file \"%s\" for reading", name);
		return false;
	}

	bool retVal = SaveLoad::loadDataEndian(*in, dataVar, size, variables, variableSizes);
	delete in;
	return retVal;
}


StagedSave::StagedSave() {
	_mode = kModeNone;
	_name = 0;
	_loaded = false;
}

StagedSave::~StagedSave() {
	clear();
}

void StagedSave::addStage(int32 size, bool endianed) {
	int32 offset = 0;

	if (!_stages.empty())
		offset = _stages[_stages.size() - 1].offset +
		         _stages[_stages.size() - 1].size;

	Stage stage(size, offset, endianed);
	_stages.push_back(stage);
}

int StagedSave::findStage(int16 dataVar, int32 size, int32 offset) const {
	for (uint i = 0; i < _stages.size(); i++)
		if ((_stages[i].size == size) &&
		    (_stages[i].offset == offset))
			return i;

	return -1;
}

bool StagedSave::allSaved() const {
	for (uint i = 0; i < _stages.size(); i++)
		if (!_stages[i].bufVar)
			return false;

	return true;
}

uint32 StagedSave::getSize() const {
	uint32 size = 0;

	for (uint i = 0; i < _stages.size(); i++) {
		if (_stages[i].endianed)
			size += 2 * _stages[i].size;
		else
			size += _stages[i].size;
	}
	
	return size;
}

void StagedSave::clear() {
	for (uint i = 0; i < _stages.size(); i++) {
		delete[] _stages[i].bufVar;
		delete[] _stages[i].bufVarSizes;
		_stages[i].bufVar = 0;
		_stages[i].bufVarSizes = 0;
	}

	delete[] _name;
	_name = 0;

	_mode = kModeNone;
	_loaded = false;
}

void StagedSave::assertMode(Mode mode, const char *name) {
	if ((_mode != mode) || ((name[0] != '\0') && strcmp(_name, name))) {
		clear();
		_mode = mode;
		_name = new char[strlen(name) + 1];
		strcpy(_name, name);
	}
}

bool StagedSave::save(int16 dataVar, int32 size, int32 offset, const char *name,
		const byte *variables, const byte *variableSizes) {

	if ((dataVar < 0) || (size <= 0) || (offset < 0)) {
		warning("Invalid dataVar (%d), size (%d) or offset (%d)", dataVar, size, offset);
		return false;
	}

	int stage = findStage(dataVar, size, offset);
	if (stage == -1) {
		warning("Invalid saving procedure");
		return false;
	}

	if (!variables || (_stages[stage].endianed && !variableSizes)) {
		warning("Missing data");
		return false;
	}

	assertMode(kModeSave, name);

	_stages[stage].bufVar = new byte[size];
	memcpy(_stages[stage].bufVar, variables + dataVar, size);

	if (_stages[stage].endianed) {
		_stages[stage].bufVarSizes = new byte[size];
		memcpy(_stages[stage].bufVarSizes, variableSizes + dataVar, size);
	}

	if (allSaved()) {
		bool result = write();
		clear();
		return result;
	}

	return true;
}

bool StagedSave::load(int16 dataVar, int32 size, int32 offset, const char *name,
		byte *variables, byte *variableSizes) {

	if ((dataVar < 0) || (size <= 0) || (offset < 0)) {
		warning("Invalid dataVar (%d), size (%d) or offset (%d)", dataVar, size, offset);
		return false;
	}

	int stage = findStage(dataVar, size, offset);
	if (stage == -1) {
		warning("Invalid loading procedure");
		return false;
	}

	assertMode(kModeLoad, name);

	if (!_loaded) {
		if (!read()) {
			clear();
			return false;
		}
	}

	if (variables)
		memcpy(variables + dataVar, _stages[stage].bufVar, size);
	if (_stages[stage].endianed && variableSizes)
		memcpy(variableSizes + dataVar, _stages[stage].bufVarSizes, size);

	return true;
}

bool StagedSave::write() const {
	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	Common::OutSaveFile *out = saveMan->openForSaving(_name);

	if (!out) {
		warning("Can't open file \"%s\" for writing", _name);
		return false;
	}

	bool result = true;
	for (uint i = 0; (i < _stages.size()) && result; i++) {
		if (!_stages[i].endianed) {

			uint32 written = out->write(_stages[i].bufVar, _stages[i].size);

			result = (written == ((uint32) _stages[i].size));
			if (!result)
				warning("Can't write data: requested %d, wrote %d", _stages[i].size, written);

		} else
			result = SaveLoad::saveDataEndian(*out, 0, _stages[i].size,
					_stages[i].bufVar, _stages[i].bufVarSizes);
	}

	if (result) {
		out->finalize();
		if (out->ioFailed()) {
			warning("Can't write to file \"%s\"", _name);
			result = false;
		}
	}

	delete out;
	return result;
}

bool StagedSave::read() {
	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	Common::InSaveFile *in = saveMan->openForLoading(_name);

	if (!in) {
		warning("Can't open file \"%s\" for reading", _name);
		return false;
	}

	uint32 saveSize = getSize();
	if (in->size() != saveSize) {
		warning("Wrong size (%d != %d)", in->size(), saveSize);
		return false;
	}

	bool result = true;
	for (uint i = 0; ((i < _stages.size()) && result); i++) {
		_stages[i].bufVar = new byte[_stages[i].size];

		if (!_stages[i].endianed) {

			uint32 nRead = in->read(_stages[i].bufVar, _stages[i].size);

			result = (nRead == ((uint32) _stages[i].size));
			if (!result)
				warning("Can't read data: requested %d, got %d", _stages[i].size, nRead);

		} else {
			_stages[i].bufVarSizes = new byte[_stages[i].size];

			result = SaveLoad::loadDataEndian(*in, 0, _stages[i].size,
					_stages[i].bufVar, _stages[i].bufVarSizes);
		}
	}

	if (result)
		_loaded = true;

	delete in;
	return result;
}


PagedBuffer::PagedBuffer(uint32 pageSize) {

	_size = 0;
	_pageSize = pageSize;
}

PagedBuffer::~PagedBuffer() {
	clear();
}

bool PagedBuffer::empty() const {
	return _pages.empty();
}

uint32 PagedBuffer::getSize() const {
	return _size;
}

void PagedBuffer::clear() {
	for (uint i = 0; i < _pages.size(); i++)
		delete[] _pages[i];
	_pages.clear();
	_size = 0;
}

bool PagedBuffer::write(const byte *buffer, uint32 size, uint32 offset) {
	grow(size, offset);

	uint page = offset / _pageSize;
	while (size > 0) {
		if (!_pages[page])
			_pages[page] = new byte[_pageSize];

		uint32 pStart = offset % _pageSize;
		uint32 n = MIN(size, _pageSize - pStart);

		memcpy(_pages[page] + pStart, buffer, n);

		buffer += n;
		offset += n;
		size -= n;
		page++;
	}

	return true;
}

bool PagedBuffer::read(byte *buffer, uint32 size, uint32 offset) const {
	uint page = offset / _pageSize;

	while (size > 0) {
		if (offset >= _size) {
			memset(buffer, 0, size);
			break;
		}

		uint32 pStart = offset % _pageSize;
		uint32 n = MIN(MIN(size, _pageSize - pStart), _size - offset);

		if (_pages[page])
			memcpy(buffer, _pages[page] + pStart, n);
		else
			memset(buffer, 0, n);

		buffer += n;
		offset += n;
		size -= n;
		page++;
	}

	return true;
}

uint32 PagedBuffer::writeToStream(Common::WriteStream &out) const {
	for (uint i = 0; i < _pages.size(); i++) {
		if (!_pages[i]) {
			for (uint32 j = 0; j < _pageSize; j++)
				out.writeByte(0);
		} else
			out.write(_pages[i], _pageSize);
	}

	return _size;
}

uint32 PagedBuffer::readFromStream(Common::ReadStream &in) {
	clear();

	while (!in.eos()) {
		byte *buffer = new byte[_pageSize];

		_size += in.read(buffer, _pageSize);

		_pages.push_back(buffer);
	}

	return _size;
}

void PagedBuffer::grow(uint32 size, uint32 offset) {
	uint32 eSize = offset + size;

	while (_size < eSize) {
		_pages.push_back(0);
		_size += MIN(_pageSize, eSize - _size);
	}
}


SaveLoad::SaveLoad(GobEngine *vm, const char *targetName) : _vm(vm) {

	_targetName = new char[strlen(targetName) + 1];
	strcpy(_targetName, targetName);
}

SaveLoad::~SaveLoad() {
	delete[] _targetName;
}

int32 SaveLoad::getSize(const char *fileName) {
	int type;

	type = getSaveType(stripPath(fileName));
	if (type == -1)
		return -1;

	debugC(3, kDebugSaveLoad, "Requested size of save file \"%s\" (type %d)",
			fileName, type);

	return getSizeVersioned(type);
}

bool SaveLoad::load(const char *fileName, int16 dataVar, int32 size, int32 offset) {
	int type;

	type = getSaveType(stripPath(fileName));
	if (type == -1)
		return false;

	debugC(3, kDebugSaveLoad, "Requested loading of save file \"%s\" (type %d) - %d, %d, %d",
			fileName, type, dataVar, size, offset);

	return loadVersioned(type, dataVar, size, offset);
}

bool SaveLoad::save(const char *fileName, int16 dataVar, int32 size, int32 offset) {
	int type;

	type = getSaveType(stripPath(fileName));
	if (type == -1)
		return false;

	debugC(3, kDebugSaveLoad, "Requested saving of save file \"%s\" (type %d) - %d, %d, %d",
			fileName, type, dataVar, size, offset);

	return saveVersioned(type, dataVar, size, offset);
}

const char *SaveLoad::stripPath(const char *fileName) {
	const char *backSlash;
	if ((backSlash = strrchr(fileName, '\\')))
		return backSlash + 1;

	return fileName;
}

char *SaveLoad::setCurrentSlot(char *destName, int slot) {
	char *slotBase = destName + strlen(destName) - 2;

	snprintf(slotBase, 3, "%02d", slot);

	return destName;
}

void SaveLoad::buildIndex(byte *buffer, char *name, int n, int32 size, int32 offset) {
	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	Common::InSaveFile *in;

	for (int i = 0; i < n; i++, buffer += size) {
		in = saveMan->openForLoading(setCurrentSlot(name, i));
		if (in) {
			in->seek(offset);
			in->read(buffer, size);
			delete in;
		} else
			memset(buffer, 0, size);
	}
}

bool SaveLoad::fromEndian(byte *buf, const byte *sizes, uint32 count) {
	while (count-- > 0) {
		if (*sizes == 3)
			*((uint32 *) buf) = READ_LE_UINT32(buf);
		else if (*sizes == 1)
			*((uint16 *) buf) = READ_LE_UINT16(buf);
		else if (*sizes != 0) {
			warning("SaveLoad::fromEndian(): Corrupted variables sizes");
			return false;
		}

		count -= *sizes;
		buf += *sizes + 1;
		sizes += *sizes + 1;
	}

	return true;
}

bool SaveLoad::toEndian(byte *buf, const byte *sizes, uint32 count) {
	while (count-- > 0) {
		if (*sizes == 3)
			WRITE_LE_UINT32(buf, *((uint32 *) buf));
		else if (*sizes == 1)
			WRITE_LE_UINT16(buf, *((uint16 *) buf));
		else if (*sizes != 0) {
			warning("SaveLoad::toEndian(): Corrupted variables sizes");
			return false;
		}

		count -= *sizes;
		buf += *sizes + 1;
		sizes += *sizes + 1;
	}

	return true;
}

uint32 SaveLoad::read(Common::ReadStream &in,
		byte *buf, byte *sizes, uint32 count) {
	uint32 nRead;

	nRead = in.read(buf, count);
	if (nRead != count) {
		warning("Can't read data: requested %d, got %d", count, nRead);
		return 0;
	}

	nRead = in.read(sizes, count);
	if (nRead != count) {
		warning("Can't read data sizes: requested %d, got %d", count, nRead);
		return 0;
	}

	return count;
}

uint32 SaveLoad::write(Common::WriteStream &out,
		const byte *buf, const byte *sizes, uint32 count) {
	uint32 written;

	written = out.write(buf, count);
	if (written != count) {
		warning("Can't write data: requested %d, wrote %d", count, written);
		return 0;
	}

	written = out.write(sizes, count);
	if (written != count) {
		warning("Can't write data: requested %d, wrote %d", count, written);
		return 0;
	}

	return count;
}

bool SaveLoad::loadDataEndian(Common::ReadStream &in,
		int16 dataVar, uint32 size, byte *variables, byte *variableSizes) {

	bool retVal = false;

	byte *varBuf = new byte[size];
	byte *sizeBuf = new byte[size];

	assert(varBuf && sizeBuf);

	if (read(in, varBuf, sizeBuf, size) == size) {
		if (fromEndian(varBuf, sizeBuf, size)) {
			memcpy(variables + dataVar, varBuf, size);
			memcpy(variableSizes + dataVar, sizeBuf, size);
			retVal = true;
		}
	}

	delete[] varBuf;
	delete[] sizeBuf;

	return retVal;
}

bool SaveLoad::saveDataEndian(Common::WriteStream &out,
		int16 dataVar, uint32 size, const byte *variables, const byte *variableSizes) {

	bool retVal = false;

	byte *varBuf = new byte[size];
	byte *sizeBuf = new byte[size];

	assert(varBuf && sizeBuf);

	memcpy(varBuf, variables + dataVar, size);
	memcpy(sizeBuf, variableSizes + dataVar, size);

	if (toEndian(varBuf, sizeBuf, size))
		if (write(out, varBuf, sizeBuf, size) == size)
			retVal = true;

	delete[] varBuf;
	delete[] sizeBuf;

	return retVal;
}

} // End of namespace Gob