aboutsummaryrefslogtreecommitdiff
path: root/engines/xeen/saves.cpp
blob: 4df4b3727cc0d94a049835016ecbdd21e1e90223 (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
/* 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/scummsys.h"
#include "common/algorithm.h"
#include "common/memstream.h"
#include "xeen/saves.h"
#include "xeen/files.h"
#include "xeen/xeen.h"

namespace Xeen {

OutFile::OutFile(XeenEngine *vm, const Common::String filename) :
	_vm(vm), _filename(filename) {
}

void OutFile::finalize() {
	uint16 id = BaseCCArchive::convertNameToId(_filename);

	if (!_vm->_saves->_newData.contains(id))
		_vm->_saves->_newData[id] = Common::MemoryWriteStreamDynamic(DisposeAfterUse::YES);

	Common::MemoryWriteStreamDynamic &out = _vm->_saves->_newData[id];
	out.write(getData(), size());
}

/*------------------------------------------------------------------------*/

SavesManager::SavesManager(XeenEngine *vm, Party &party) :
		BaseCCArchive(), _vm(vm), _party(party) {
	SearchMan.add("saves", this, 0, false);
	_data = nullptr;
	_wonWorld = false;
	_wonDarkSide = false;
}

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

void SavesManager::syncBitFlags(Common::Serializer &s, bool *startP, bool *endP) {
	byte data = 0;

	int bitCounter = 0;
	for (bool *p = startP; p <= endP; ++p, bitCounter = (bitCounter + 1) % 8) {
		if (p == endP || bitCounter == 0) {
			if (p != endP || s.isSaving())
				s.syncAsByte(data);
			if (p == endP)
				break;

			if (s.isSaving())
				data = 0;
		}

		if (s.isLoading())
			*p = (data >> bitCounter) != 0;
		else if (*p)
			data |= 1 << bitCounter;
	}
}

Common::SeekableReadStream *SavesManager::createReadStreamForMember(const Common::String &name) const {
	CCEntry ccEntry;

	// If the given resource has already been perviously "written" to the
	// save manager, then return that new resource
	uint16 id = BaseCCArchive::convertNameToId(name);
	if (_newData.contains(id)) {
		Common::MemoryWriteStreamDynamic stream = _newData[id];
		return new Common::MemoryReadStream(stream.getData(), stream.size());
	}

	// Retrieve the resource from the loaded savefile
	if (getHeaderEntry(name, ccEntry)) {
		// Open the correct CC entry
		return new Common::MemoryReadStream(_data + ccEntry._offset, ccEntry._size);
	}

	return nullptr;
}

void SavesManager::load(Common::SeekableReadStream *stream) {
	loadIndex(stream);

	delete[] _data;
	_data = new byte[stream->size()];
	stream->seek(0);
	stream->read(_data, stream->size());

	// Load in the character stats and active party
	Common::SeekableReadStream *chr = createReadStreamForMember("maze.chr");
	Common::Serializer sChr(chr, nullptr);
	_party._roster.synchronize(sChr);
	delete chr;

	Common::SeekableReadStream *pty = createReadStreamForMember("maze.pty");
	Common::Serializer sPty(pty, nullptr);
	_party.synchronize(sPty);
	delete pty;
}

void SavesManager::reset() {
	Common::String prefix = _vm->getGameID() != GType_DarkSide ? "xeen|" : "dark|";
	Common::MemoryWriteStreamDynamic saveFile(DisposeAfterUse::YES);
	Common::File fIn;

	const int RESOURCES[6] = { 0x2A0C, 0x2A1C, 0x2A2C, 0x2A3C, 0x284C, 0x2A5C };
	for (int i = 0; i < 6; ++i) {
		Common::String filename = prefix + Common::String::format("%.4x", RESOURCES[i]);
		if (fIn.exists(filename)) {
			// Read in the next resource
			fIn.open(filename);
			byte *data = new byte[fIn.size()];
			fIn.read(data, fIn.size());

			// Copy it to the combined savefile resource
			saveFile.write(data, fIn.size());
			delete[] data;
			fIn.close();
		}
	}

	Common::MemoryReadStream f(saveFile.getData(), saveFile.size());
	load(&f);

	// Set up the party and characters from dark.cur
	CCArchive gameCur("xeen.cur", false);
	File fParty("maze.pty", gameCur);
	Common::Serializer sParty(&fParty, nullptr);
	_party.synchronize(sParty);
	fParty.close();

	File fChar("maze.chr", gameCur);
	Common::Serializer sChar(&fChar, nullptr);
	_party._roster.synchronize(sChar);
	fChar.close();
}

void SavesManager::readCharFile() {
	warning("TODO: readCharFile");
}

void SavesManager::writeCharFile() {
	warning("TODO: writeCharFile");
}

void SavesManager::saveChars() {
	warning("TODO: saveChars");
}

} // End of namespace Xeen