aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/save/savefile.cpp
blob: 3869ea2def4e78910f891c0f655747e7958d8220 (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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/* 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/util.h"
#include "common/endian.h"
#include "common/memstream.h"
#include "common/system.h"
#include "common/savefile.h"

#include "gob/gob.h"
#include "gob/save/savefile.h"
#include "gob/video.h"
#include "gob/inter.h"
#include "gob/variables.h"

namespace Gob {

static inline bool flushStream(Common::WriteStream &stream) {
	// Flush and check for errors

	if (!stream.flush())
		return false;
	if (stream.err())
		return false;

	return true;
}

SaveHeader::SaveHeader(uint32 type, uint32 version, uint32 size) {
	_type = type;
	_version = version;
	_size = size;
}

bool SaveHeader::read(Common::ReadStream &stream) {
	// Read the header and verify the global IDs
	if (stream.readUint32BE() != kID1)
		return false;
	if (stream.readUint32BE() != kID2)
		return false;

	_type = stream.readUint32BE();
	_version = stream.readUint32LE();
	_size = stream.readUint32LE();

	return !stream.err();
}

bool SaveHeader::verify(Common::ReadStream &stream) const {
	// Compare the header with the stream's content

	if (stream.readUint32BE() != kID1)
		return false;
	if (stream.readUint32BE() != kID2)
		return false;
	if (stream.readUint32BE() != _type)
		return false;
	if (stream.readUint32LE() != _version)
		return false;
	if (stream.readUint32LE() != _size)
		return false;

	return !stream.err();
}

bool SaveHeader::verifyReadSize(Common::ReadStream &stream) {
	// Compare the header with the stream's content, expect for the size

	if (stream.readUint32BE() != kID1)
		return false;
	if (stream.readUint32BE() != kID2)
		return false;
	if (stream.readUint32BE() != _type)
		return false;
	if (stream.readUint32LE() != _version)
		return false;

	// Read the size out of the stream instead
	_size = stream.readUint32LE();

	return !stream.err();
}

bool SaveHeader::write(Common::WriteStream &stream) const {
	stream.writeUint32BE(kID1);
	stream.writeUint32BE(kID2);
	stream.writeUint32BE(_type);
	stream.writeUint32LE(_version);
	stream.writeUint32LE(_size);

	return flushStream(stream);
}

uint32 SaveHeader::getType() const {
	return _type;
}

uint32 SaveHeader::getVersion() const {
	return _version;
}

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

void SaveHeader::setType(uint32 type) {
	_type = type;
}

void SaveHeader::setVersion(uint32 version) {
	_version = version;
}

void SaveHeader::setSize(uint32 size) {
	_size = size;
}

SavePart::SavePart() {
}

SavePart::~SavePart() {
}

uint32 SavePart::getSize() const {
	// A part's size is the content's size plus the header's size
	return _header.getSize() + SaveHeader::kSize;
}

SavePartMem::SavePartMem(uint32 size) : SavePart(), _size(size) {
	_header.setType(kID);
	_header.setVersion(kVersion);
	_header.setSize(size);

	_data = new byte[size];
}

SavePartMem::~SavePartMem() {
	delete[] _data;
}

bool SavePartMem::read(Common::ReadStream &stream) {
	if (!_header.verify(stream))
		return false;

	if (stream.read(_data, _size) != _size)
		return false;

	return !stream.err();
}

bool SavePartMem::write(Common::WriteStream &stream) const {
	if (!_header.write(stream))
		return false;

	if (stream.write(_data, _size) != _size)
		return false;

	return flushStream(stream);
}

bool SavePartMem::readFrom(const byte *data, uint32 offset, uint32 size) {
	if ((offset + size) > _size)
		return false;

	memcpy(_data + offset, data, size);

	return true;
}

bool SavePartMem::writeInto(byte *data, uint32 offset, uint32 size) const {
	if ((offset + size) > _size)
		return false;

	memcpy(data, _data + offset, size);

	return true;
}

SavePartVars::SavePartVars(GobEngine *vm, uint32 size) : SavePart(), _size(size), _vm(vm) {
	_header.setType(kID);
	_header.setVersion(kVersion);
	_header.setSize(size);

	_data = new byte[size];
}

SavePartVars::~SavePartVars() {
	delete[] _data;
}

bool SavePartVars::read(Common::ReadStream &stream) {
	if (!_header.verify(stream))
		return false;

	if (stream.read(_data, _size) != _size)
		return false;

	return !stream.err();
}

bool SavePartVars::write(Common::WriteStream &stream) const {
	if (!_header.write(stream))
		return false;

	if (stream.write(_data, _size) != _size)
		return false;

	return flushStream(stream);
}

bool SavePartVars::readFrom(uint32 var, uint32 offset, uint32 size) {
	if (!_vm->_inter->_variables)
		return false;

	if ((offset + size) > _size)
		return false;

	// Get raw variables
	return _vm->_inter->_variables->copyTo(var, _data + offset, size);
}

bool SavePartVars::readFromRaw(const byte *data, uint32 size) {
	if (size != _size)
		return false;

	memcpy(_data, data, size);
	return true;
}

bool SavePartVars::writeInto(uint32 var, uint32 offset, uint32 size) const {
	if (!_vm->_inter->_variables)
		return false;

	if ((offset + size) > _size)
		return false;

	// Write raw variables
	if (!_vm->_inter->_variables->copyFrom(var, _data + offset, size))
		return false;

	return true;
}

SavePartSprite::SavePartSprite(uint32 width, uint32 height) {
	assert((width > 0) && (height > 0));

	_width = width;
	_height = height;

	_header.setType(kID);
	_header.setVersion(kVersion);
	//            width + height +      sprite      + palette
	_header.setSize(4   +   4    + _width * _height +  768);

	_dataSprite  = new byte[_width * _height];
	_dataPalette = new byte[768];

	memset(_dataSprite,  0, _width * _height);
	memset(_dataPalette, 0, 768);
}

SavePartSprite::~SavePartSprite() {
	delete[] _dataSprite;
	delete[] _dataPalette;
}

bool SavePartSprite::read(Common::ReadStream &stream) {
	if (!_header.verify(stream))
		return false;

	// The sprite's dimensions have to fit
	if (stream.readUint32LE() != _width)
		return false;
	if (stream.readUint32LE() != _height)
		return false;

	// Sprite data
	if (stream.read(_dataSprite, _width * _height) != (_width * _height))
		return false;

	// Palette data
	if (stream.read(_dataPalette, 768) != 768)
		return false;

	return !stream.err();
}

bool SavePartSprite::write(Common::WriteStream &stream) const {
	if (!_header.write(stream))
		return false;

	// The sprite's dimensions
	stream.writeUint32LE(_width);
	stream.writeUint32LE(_height);

	// Sprite data
	if (stream.write(_dataSprite, _width * _height) != (_width * _height))
		return false;

	// Palette data
	if (stream.write(_dataPalette, 768) != 768)
		return false;

	return flushStream(stream);
}

bool SavePartSprite::readPalette(const byte *palette) {
	memcpy(_dataPalette, palette, 768);

	return true;
}

bool SavePartSprite::readSprite(const Surface &sprite) {
	// The sprite's dimensions have to fit
	if (((uint32)sprite.getWidth()) != _width)
		return false;
	if (((uint32)sprite.getHeight()) != _height)
		return false;

	// Only 8bit sprites supported for now
	if (sprite.getBPP() != 1)
		return false;

	memcpy(_dataSprite, sprite.getData(), _width * _height);

	return true;
}

bool SavePartSprite::readSpriteRaw(const byte *data, uint32 size) {
	if (size != (_width * _height))
		return false;

	memcpy(_dataSprite, data, size);
	return true;
}

bool SavePartSprite::writePalette(byte *palette) const {
	memcpy(palette, _dataPalette, 768);

	return true;
}

bool SavePartSprite::writeSprite(Surface &sprite) const {
	// The sprite's dimensions have to fit
	if (((uint32)sprite.getWidth()) != _width)
		return false;
	if (((uint32)sprite.getHeight()) != _height)
		return false;

	// Only 8bit sprites supported for now
	if (sprite.getBPP() != 1)
		return false;

	memcpy(sprite.getData(), _dataSprite, _width * _height);

	return true;
}

SavePartInfo::SavePartInfo(uint32 descMaxLength, uint32 gameID,
		uint32 gameVersion, byte endian, uint32 varCount) {

	_header.setType(kID);
	_header.setVersion(kVersion);
	//                        descMaxLength + gameID + gameVersion + endian + varCount
	_header.setSize(descMaxLength + 4       +   4    +     4       +    1   +    4);

	_descMaxLength = descMaxLength;
	_gameID = gameID;
	_gameVersion = gameVersion;
	_endian = endian;
	_varCount = varCount;

	_desc = new char[_descMaxLength + 1];
	memset(_desc, 0, _descMaxLength + 1);
}

SavePartInfo::~SavePartInfo() {
	delete[] _desc;
}

const char *SavePartInfo::getDesc() const {
	return _desc;
}

uint32 SavePartInfo::getDescMaxLength() const {
	return _descMaxLength;
}

void SavePartInfo::setVarCount(uint32 varCount) {
	_varCount = varCount;
}

void SavePartInfo::setDesc(const char *desc) {
	if (!desc) {
		memset(_desc, 0, _descMaxLength + 1);
		return;
	}

	uint32 n = MIN<uint32>(strlen(desc), _descMaxLength);

	// Copy the description and fill with 0
	memcpy(_desc, desc, n);
	memset(_desc + n, 0, _descMaxLength + 1 - n);
}

void SavePartInfo::setDesc(const byte *desc, uint32 size) {
	if (!desc || !size) {
		memset(_desc, 0, _descMaxLength + 1);
		return;
	}

	uint32 n = MIN<uint32>(size, _descMaxLength);
	memcpy(_desc, desc, n);
	memset(_desc + n, 0, _descMaxLength + 1 - n);
}

bool SavePartInfo::read(Common::ReadStream &stream) {
	if (!_header.verify(stream))
		return false;

	if (stream.readUint32LE() != _gameID)
		return false;
	if (stream.readUint32LE() != _gameVersion)
		return false;
	if (stream.readByte() != _endian)
		return false;
	if (stream.readUint32LE() != _varCount)
		return false;
	if (stream.readUint32LE() != _descMaxLength)
		return false;

	if (stream.read(_desc, _descMaxLength) != _descMaxLength)
		return false;

	_desc[_descMaxLength] = 0;

	return !stream.err();
}

bool SavePartInfo::write(Common::WriteStream &stream) const {
	if (!_header.write(stream))
		return false;

	stream.writeUint32LE(_gameID);
	stream.writeUint32LE(_gameVersion);
	stream.writeByte(_endian);
	stream.writeUint32LE(_varCount);
	stream.writeUint32LE(_descMaxLength);

	if (stream.write(_desc, _descMaxLength) != _descMaxLength)
		return false;

	return flushStream(stream);
}

SaveContainer::Part::Part(uint32 s) {
	size = s;
	data = new byte[size];
}

SaveContainer::Part::~Part() {
	delete[] data;
}

Common::WriteStream *SaveContainer::Part::createWriteStream() {
	return new Common::MemoryWriteStream(data, size);
}

Common::ReadStream *SaveContainer::Part::createReadStream() const {
	return new Common::MemoryReadStream(data, size);
}

SaveContainer::SaveContainer(uint32 partCount, uint32 slot) {
	assert(partCount > 0);

	_slot = slot;
	_partCount = partCount;

	_parts.resize(partCount);
	for (PartIterator it = _parts.begin(); it != _parts.end(); ++it)
		*it = 0;

	_header.setType(kID);
	_header.setVersion(kVersion);
	_header.setSize(4); // uint32 # of parts
}

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

uint32 SaveContainer::getSlot() const {
	return _slot;
}

uint32 SaveContainer::getSize() const {
	return _header.getSize() + SaveHeader::kSize;
}

bool SaveContainer::hasAllParts() const {
	for (PartConstIterator it = _parts.begin(); it != _parts.end(); ++it)
		if (!*it)
			return false;

	return true;
}

uint32 SaveContainer::calcSize() const {
	uint32 size = 4; // uint32 # of parts

	for (PartConstIterator it = _parts.begin(); it != _parts.end(); ++it)
		if (*it)
			//              uint32 part size
			size += (*it)->size + 4;

	return size;
}

void SaveContainer::clear() {
	for (PartIterator it = _parts.begin(); it != _parts.end(); ++it) {
		Part *&p = *it;

		delete p;
		p = 0;
	}
}

bool SaveContainer::writePart(uint32 partN, const SavePart *part) {
	// Sanity checks
	if (!part)
		return false;
	if (partN >= _partCount)
		return false;

	Part *&p = _parts[partN];

	delete p;
	// Create the part
	p = new Part(part->getSize());

	Common::WriteStream *pStream = p->createWriteStream();

	// Write
	if (!part->write(*pStream)) {
		delete p;
		p = 0;

		delete pStream;
		return false;
	}

	delete pStream;

	// Update size
	_header.setSize(calcSize());

	return true;
}

bool SaveContainer::readPart(uint32 partN, SavePart *part) const {
	// Sanity checks
	if (!part)
		return false;
	if (partN >= _partCount)
		return false;

	const Part * const &p = _parts[partN];

	// Check if the part actually exists
	if (!p)
		return false;

	Common::ReadStream *pStream = p->createReadStream();

	// Read
	if (!part->read(*pStream)) {
		delete pStream;
		return false;
	}

	delete pStream;
	return true;
}

bool SaveContainer::readPartHeader(uint32 partN, SaveHeader *header) const {
	// Sanity checks
	if (!header)
		return false;
	if (partN >= _partCount)
		return false;

	const Part * const &p = _parts[partN];

	// Check if the part actually exists
	if (!p)
		return false;

	Common::ReadStream *pStream = p->createReadStream();

	// Read
	if (!header->read(*pStream)) {
		delete pStream;
		return false;
	}

	delete pStream;
	return true;
}

bool SaveContainer::read(Common::ReadStream &stream) {
	// Verify the header and get the container's size
	if (!_header.verifyReadSize(stream))
		return false;

	// The part count has to be correct
	if (stream.readUint32LE() != _partCount)
		return false;

	// Iterate over all parts
	for (PartIterator it = _parts.begin(); it != _parts.end(); ++it) {
		// Read the size
		uint32 size = stream.readUint32LE();

		if (stream.err()) {
			clear();
			return false;
		}

		Part *&p = *it;

		delete p;
		// Create a suitable part
		p = new Part(size);
	}

	// Update size
	_header.setSize(calcSize());

	// Iterate over all parts
	for (PartIterator it = _parts.begin(); it != _parts.end(); ++it) {
		Part *&p = *it;

		// Read the part
		if (stream.read(p->data, p->size) != p->size) {
			clear();
			return false;
		}
	}

	return !stream.err();
}

bool SaveContainer::write(Common::WriteStream &stream) const {
	// Write the header
	if (!_header.write(stream))
		return false;

	// Write the part count
	stream.writeUint32LE(_partCount);

	// Iterate over all parts
	for (PartConstIterator it = _parts.begin(); it != _parts.end(); ++it) {
		// Part doesn't actually exist => error
		if (!*it)
			return false;

		// Write the part's size
		stream.writeUint32LE((*it)->size);
	}

	if (!flushStream(stream))
		return false;

	// Iterate over all parts
	for (PartConstIterator it = _parts.begin(); it != _parts.end(); ++it) {
		Part * const &p = *it;

		// Write the part
		if (stream.write(p->data, p->size) != p->size)
			return false;
	}

	return flushStream(stream);
}

Common::Array<SaveContainer::PartInfo> *SaveContainer::getPartsInfo(Common::SeekableReadStream &stream) {
	Common::Array<PartInfo> *parts = 0;

	// Remember the stream's position to seek back to
	uint32 startPos = stream.pos();

	SaveHeader header;

	header.setType(kID);
	header.setVersion(kVersion);

	// Verify the header
	if (!header.verifyReadSize(stream)) {
		// Seek back
		stream.seek(startPos);
		return 0;
	}

	// Read the part count
	uint32 partCount = stream.readUint32LE();

	// Create a part information array
	parts = new Common::Array<PartInfo>;
	parts->resize(partCount);

	// Read all part sizes
	for (uint32 i = 0; i < partCount; i++)
		(*parts)[i].size = stream.readUint32LE();

	// Iterate over all parts
	for (uint32 i = 0; i < partCount; i++) {
		// The part's offset (from the starting point of the stream)
		(*parts)[i].offset = stream.pos() - startPos;

		SaveHeader partHeader;

		// Read the header
		if (!partHeader.read(stream)) {
			// Seek back
			stream.seek(startPos);
			delete parts;
			return 0;
		}

		// Fill in the ID
		(*parts)[i].id = partHeader.getType();

		// Skip the part's content
		stream.skip(partHeader.getSize());
	}

	if (stream.err()) {
		delete parts;
		parts = 0;
	}

	// Seek back
	stream.seek(startPos);

	return parts;
}

bool SaveContainer::isSave(Common::SeekableReadStream &stream) {
	// Remember the stream's position to seek back to
	uint32 startPos = stream.pos();

	SaveHeader header;

	header.setType(kID);
	header.setVersion(kVersion);

	bool result = header.verifyReadSize(stream);

	// Seek back
	stream.seek(startPos);

	return result;
}


SaveReader::SaveReader(uint32 partCount, uint32 slot, const Common::String &fileName) :
	SaveContainer(partCount, slot), _fileName(fileName) {

	_stream = 0;

	_loaded = false;
}

SaveReader::SaveReader(uint32 partCount, uint32 slot, Common::SeekableReadStream &stream) :
	SaveContainer(partCount, slot) {

	_stream = &stream;

	_loaded = false;
}

SaveReader::~SaveReader() {
}

// Open the save and read it
bool SaveReader::load() {

	Common::InSaveFile *in = 0;
	Common::SeekableReadStream *stream;

	if (!_fileName.empty()) {
		in = openSave();

		if (!in)
			return false;

		stream = in;
	} else if (_stream)
		stream = _stream;
	else
		return false;

	if (!SaveContainer::read(*stream)) {
		delete in;
		return false;
	}

	delete in;
	_loaded = true;
	return true;
}

bool SaveReader::readPartHeader(uint32 partN, SaveHeader *header) const {
	// The save has to be loaded
	if (!_loaded)
		return false;

	return SaveContainer::readPartHeader(partN, header);
}

bool SaveReader::readPart(uint32 partN, SavePart *part) const {
	// The save has to be loaded
	if (!_loaded)
		return false;

	if (!SaveContainer::readPart(partN, part))
		return false;

	return true;
}

Common::InSaveFile *SaveReader::openSave(const Common::String &fileName) {
	if (fileName.empty())
		return 0;

	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	return saveMan->openForLoading(fileName);
}

Common::InSaveFile *SaveReader::openSave() {
	return openSave(_fileName);
}

bool SaveReader::getInfo(Common::SeekableReadStream &stream, SavePartInfo &info) {
	// Remeber the stream's starting position to seek back to
	uint32 startPos = stream.pos();

	// Get parts' basic information
	Common::Array<SaveContainer::PartInfo> *partsInfo = getPartsInfo(stream);

	// No parts => fail
	if (!partsInfo) {
		stream.seek(startPos);
		return false;
	}

	bool result = false;
	// Iterate over all parts
	for (Common::Array<SaveContainer::PartInfo>::iterator it = partsInfo->begin();
	     it != partsInfo->end(); ++it) {

		// Check for the info part
		if (it->id == SavePartInfo::kID) {
			if (!stream.seek(it->offset))
				break;

			// Read it
			result = info.read(stream);
			break;
		}
	}

	stream.seek(startPos);

	delete partsInfo;
	return result;
}

bool SaveReader::getInfo(const Common::String &fileName, SavePartInfo &info) {
	Common::InSaveFile *in = openSave(fileName);

	if (!in)
		return false;

	bool result = getInfo(*in, info);

	delete in;

	return result;
}

SaveWriter::SaveWriter(uint32 partCount, uint32 slot) :
	SaveContainer(partCount, slot) {
}

SaveWriter::SaveWriter(uint32 partCount, uint32 slot, const Common::String &fileName) :
	SaveContainer(partCount, slot), _fileName(fileName) {
}

SaveWriter::~SaveWriter() {
}

bool SaveWriter::writePart(uint32 partN, const SavePart *part) {
	// Write the part
	if (!SaveContainer::writePart(partN, part))
		return false;

	// If all parts have been written, save and clear
	if (hasAllParts() && canSave()) {
		if (save()) {
			clear();
			return true;
		}

		return false;
	}

	return true;
}

bool SaveWriter::save(Common::WriteStream &stream) {
	return SaveContainer::write(stream);
}

bool SaveWriter::save() {
	Common::OutSaveFile *out = openSave();

	if (!out)
		return false;

	bool success = save(*out);

	delete out;

	return success;
}

bool SaveWriter::canSave() const {
	return (!_fileName.empty());
}

Common::OutSaveFile *SaveWriter::openSave(const Common::String &fileName) {
	if (fileName.empty())
		return 0;

	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	return saveMan->openForSaving(fileName);
}

Common::OutSaveFile *SaveWriter::openSave() {
	return openSave(_fileName);
}

} // End of namespace Gob