aboutsummaryrefslogtreecommitdiff
path: root/engines/mortevielle/saveload.cpp
blob: 3032c966908801c720387eab9b71899791464498 (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
/* 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.
 *
 */

/*
 * This code is based on original Mortville Manor DOS source code
 * Copyright (c) 1987-1989 Lankhor
 */

#include "mortevielle/mortevielle.h"
#include "mortevielle/dialogs.h"
#include "mortevielle/mouse.h"
#include "mortevielle/saveload.h"

#include "common/file.h"
#include "common/system.h"

namespace Mortevielle {

static const char SAVEGAME_ID[4] = { 'M', 'O', 'R', 'T' };

SavegameManager::SavegameManager(MortevielleEngine *vm) {
	_vm = vm;
}

/**
 * Handle saving or loading savegame data
 */
void SavegameManager::sync_save(Common::Serializer &sz) {
	sz.syncAsSint16LE(g_vm->_saveStruct._faithScore);
	for (int i = 0; i < 11; ++i)
		sz.syncAsByte(g_vm->_saveStruct._pctHintFound[i]);
	for (int i = 0; i < 43; ++i)
		sz.syncAsByte(g_vm->_saveStruct._availableQuestion[i]);
	for (int i = 0; i < 31; ++i)
		sz.syncAsByte(g_vm->_saveStruct._inventory[i]);

	sz.syncAsSint16LE(g_vm->_saveStruct._currPlace);
	sz.syncAsSint16LE(g_vm->_saveStruct._atticBallHoleObjectId);
	sz.syncAsSint16LE(g_vm->_saveStruct._atticRodHoleObjectId);
	sz.syncAsSint16LE(g_vm->_saveStruct._cellarObjectId);
	sz.syncAsSint16LE(g_vm->_saveStruct._secretPassageObjectId);
	sz.syncAsSint16LE(g_vm->_saveStruct._wellObjectId);
	sz.syncAsSint16LE(g_vm->_saveStruct._selectedObjectId);
	sz.syncAsSint16LE(g_vm->_saveStruct._purpleRoomObjectId);
	sz.syncAsSint16LE(g_vm->_saveStruct._cryptObjectId);
	sz.syncAsByte(g_vm->_saveStruct._alreadyEnteredManor);
	sz.syncAsByte(g_vm->_saveStruct._fullHour);

	sz.syncBytes(_tabdonSaveBuffer, 391);
}

/**
 * Inner code for loading a saved game
 * @remarks	Originally called 'takesav'
 */
bool SavegameManager::loadSavegame(const Common::String &filename) {
	// Try loading first from the save area
	Common::SeekableReadStream *stream = g_system->getSavefileManager()->openForLoading(filename);

	Common::File f;
	if (stream == NULL) {
		if (!f.open(filename)) {
			warning("Unable to open save file '%s'", filename.c_str());
			return false;
		}
		stream = f.readStream(f.size());
		f.close();
	}

	// Check whether it's a ScummVM saved game
	char buffer[4];
	stream->read(buffer, 4);
	if (!strncmp(&buffer[0], &SAVEGAME_ID[0], 4)) {
		// Yes, it is, so skip over the savegame header
		SavegameHeader header;
		readSavegameHeader(stream, header);
		delete header.thumbnail;
	} else {
		stream->seek(0);
	}

	// Read the game contents
	Common::Serializer sz(stream, NULL);
	sync_save(sz);

	g_vm->_coreVar = g_vm->_saveStruct;
	for (int i = 0; i <= 389; ++i)
		g_vm->_tabdon[i + kAcha] = _tabdonSaveBuffer[i];

	// Close the stream
	delete stream;

	return true;
}

/**
 * Load a saved game
 */
Common::Error SavegameManager::loadGame(const Common::String &filename) {
	g_vm->_mouse->hideMouse();
	g_vm->displayEmptyHand();
	if (loadSavegame(filename)) {
		/* Initialization */
		g_vm->charToHour();
		g_vm->initGame();
		g_vm->gameLoaded();
		g_vm->_mouse->showMouse();
		return Common::kNoError;
	} else
		return Common::kUnknownError;
}

/**
 * Save the game
 */
Common::Error SavegameManager::saveGame(int n, const Common::String &saveName) {
	Common::OutSaveFile *f;
	int i;

	g_vm->_mouse->hideMouse();
	g_vm->hourToChar();

	for (i = 0; i <= 389; ++i)
		_tabdonSaveBuffer[i] = g_vm->_tabdon[i + kAcha];
	g_vm->_saveStruct = g_vm->_coreVar;
	if (g_vm->_saveStruct._currPlace == ROOM26)
		g_vm->_saveStruct._currPlace = LANDING;

	Common::String filename = _vm->generateSaveFilename(n);
	f = g_system->getSavefileManager()->openForSaving(filename);

	// Write out the savegame header
	f->write(&SAVEGAME_ID[0], 4);

	// Write out the header
	SavegameHeader header;
	writeSavegameHeader(f, saveName);

	// Write out the savegame contents
	Common::Serializer sz(NULL, f);
	sync_save(sz);

	// Close the save file
	f->finalize();
	delete f;

	// Skipped: dialog asking to swap floppy

	g_vm->_mouse->showMouse();
	return Common::kNoError;
}

Common::Error SavegameManager::loadGame(int slot) {
	return loadGame(_vm->generateSaveFilename(slot));
}

Common::Error SavegameManager::saveGame(int slot) {
	return saveGame(slot, _vm->generateSaveFilename(slot));
}

void SavegameManager::writeSavegameHeader(Common::OutSaveFile *out, const Common::String &saveName) {
	// Write out a savegame header
	out->writeByte(SAVEGAME_VERSION);

	// Write savegame name
	out->writeString(saveName);
	out->writeByte(0);

	// Get the active palette
	uint8 thumbPalette[256 * 3];
	g_system->getPaletteManager()->grabPalette(thumbPalette, 0, 256);

	// Create a thumbnail and save it
	Graphics::Surface *thumb = new Graphics::Surface();
	Graphics::Surface s = g_vm->_screenSurface->lockArea(Common::Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT));

	::createThumbnail(thumb, (const byte *)s.getPixels(), SCREEN_WIDTH, SCREEN_HEIGHT, thumbPalette);
	Graphics::saveThumbnail(*out, *thumb);
	thumb->free();
	delete thumb;

	// Write out the save date/time
	TimeDate td;
	g_system->getTimeAndDate(td);
	out->writeSint16LE(td.tm_year + 1900);
	out->writeSint16LE(td.tm_mon + 1);
	out->writeSint16LE(td.tm_mday);
	out->writeSint16LE(td.tm_hour);
	out->writeSint16LE(td.tm_min);
}

bool SavegameManager::readSavegameHeader(Common::InSaveFile *in, SavegameHeader &header) {
	header.thumbnail = NULL;

	// Get the savegame version
	header.version = in->readByte();

	// Read in the save name
	header.saveName.clear();
	char ch;
	while ((ch = (char)in->readByte()) != '\0')
		header.saveName += ch;

	// Get the thumbnail
	header.thumbnail = Graphics::loadThumbnail(*in);
	if (!header.thumbnail)
		return false;

	// Read in save date/time
	header.saveYear = in->readSint16LE();
	header.saveMonth = in->readSint16LE();
	header.saveDay = in->readSint16LE();
	header.saveHour = in->readSint16LE();
	header.saveMinutes = in->readSint16LE();

	return true;
}

SaveStateList SavegameManager::listSaves(const Common::String &target) {
	Common::String pattern = target;
	pattern += ".###";

	Common::StringArray files = g_system->getSavefileManager()->listSavefiles(pattern);

	SaveStateList saveList;
	for (Common::StringArray::const_iterator file = files.begin(); file != files.end(); ++file) {
		// Obtain the last 3 digits of the filename, since they correspond to the save slot
		const Common::String &fname = *file;
		int slotNumber = atoi(fname.c_str() + fname.size() - 3);

		Common::InSaveFile *in = g_system->getSavefileManager()->openForLoading(fname);
		if (in) {
			// There can be two types of savegames: original interpreter savegames, and ScummVM savegames.
			// Original interpreter savegames are 497 bytes, and still need to be supported because the
			// initial game state is stored as a savegame
			bool validFlag = false;
			Common::String saveDescription;

			char buffer[4];
			in->read(buffer, 4);
			if (!strncmp(&buffer[0], &SAVEGAME_ID[0], 4)) {
				// ScummVm savegame. Read in the header to get the savegame name
				SavegameHeader header;
				validFlag = readSavegameHeader(in, header);

				if (validFlag) {
					delete header.thumbnail;
					saveDescription = header.saveName;
				}
			} else if (file->size() == 497) {
				// Form an appropriate savegame name
				saveDescription = (slotNumber == 0) ? "Initial game state" :
					Common::String::format("Savegame #%d", slotNumber);
				validFlag = true;
			}

			if (validFlag)
				// Got a valid savegame
				saveList.push_back(SaveStateDescriptor(slotNumber, saveDescription));

			delete in;
		}
	}

	Common::sort(saveList.begin(), saveList.end(), SaveStateDescriptorSlotComparator());
	return saveList;
}

SaveStateDescriptor SavegameManager::querySaveMetaInfos(const Common::String &fileName) {
	Common::InSaveFile *f = g_system->getSavefileManager()->openForLoading(fileName);

	if (f) {
		// Get the slot number
		int slot = 1;
		if (fileName.size() > 4 && fileName[fileName.size() - 4] == '.')
			slot = atoi(fileName.c_str() + fileName.size() - 3);

		// Check to see if it's a ScummVM savegame or not
		char buffer[4];
		f->read(buffer, 4);

		bool hasHeader = !strncmp(&buffer[0], &SAVEGAME_ID[0], 4);

		if (!hasHeader) {
			// Original savegame perhaps?
			delete f;

			SaveStateDescriptor desc(slot, Common::String::format("Savegame - %03d", slot));
			desc.setDeletableFlag(slot != 0);
			desc.setWriteProtectedFlag(slot == 0);
			return desc;
		} else {
			// Get the savegame header information
			SavegameHeader header;
			readSavegameHeader(f, header);
			delete f;

			// Create the return descriptor
			SaveStateDescriptor desc(slot, header.saveName);
			desc.setDeletableFlag(true);
			desc.setWriteProtectedFlag(false);
			desc.setThumbnail(header.thumbnail);
			desc.setSaveDate(header.saveYear, header.saveMonth, header.saveDay);
			desc.setSaveTime(header.saveHour, header.saveMinutes);

			return desc;
		}
	}

	return SaveStateDescriptor();
}

} // End of namespace Mortevielle