aboutsummaryrefslogtreecommitdiff
path: root/engines/kyra/saveload.cpp
blob: 334c0156891756baa85e0f8fcf9a02d5d63e5b65 (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
/* 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 "common/system.h"

#include "kyra/kyra.h"

#define CURRENT_SAVE_VERSION 8

#define GF_FLOPPY (1 <<  0)
#define GF_TALKIE (1 <<  1)
#define GF_FMTOWNS (1 <<  2)

namespace Kyra {

Common::InSaveFile *KyraEngine::openSaveForReading(const char *filename, uint32 &version, char *saveName) {
	debugC(9, kDebugLevelMain, "KyraEngine::openSaveForReading('%s', %p, %p)", filename, (const void*)&version, saveName);

	Common::InSaveFile *in = 0;
	if (!(in = _saveFileMan->openForLoading(filename))) {
		warning("Can't open file '%s', game not loaded", filename);
		return 0;
	}

	uint32 type = in->readUint32BE();

	// FIXME: The kyra savegame code used to be endian unsafe. Uncomment the
	// following line to graciously handle old savegames from LE machines.
	// if (type != MKID_BE('KYRA') && type != MKID_BE('ARYK')) {
	if (type != MKID_BE(saveGameID())) {
		warning("No Kyrandia savefile header.");
		delete in;
		return 0;
	}

	version = in->readUint32BE();
	if (version > CURRENT_SAVE_VERSION) {
		warning("Savegame is not the right version (%u)", version);
		delete in;
		return 0;
	}

	char saveNameBuffer[31];
	if (!saveName)
		saveName = saveNameBuffer;
	in->read(saveName, 31);

	if (_flags.gameID == GI_KYRA1 && version < 2) {
		warning("Make sure your savefile was from this version! (too old savefile version to detect that)");
	} else {
		uint32 flags = in->readUint32BE();
		if ((flags & GF_FLOPPY) && _flags.isTalkie) {
			warning("Can not load floppy savefile for this (non floppy) gameversion");
			delete in;
			return 0;
		} else if ((flags & GF_TALKIE) && !(_flags.isTalkie)) {
			warning("Can not load cdrom savefile for this (non cdrom) gameversion");
			delete in;
			return 0;
		} else if ((flags & GF_FMTOWNS) && !(_flags.platform == Common::kPlatformFMTowns || _flags.platform == Common::kPlatformPC98)) {
			warning("can not load FM-Towns/PC98 savefile for this (non FM-Towns/PC98) gameversion");
			delete in;
			return 0;
		}
	}

	if (in->ioFailed()) {
		error("Load failed ('%s', '%s').", filename, saveName);
		delete in;
		return 0;
	}

	return in;
}

Common::OutSaveFile *KyraEngine::openSaveForWriting(const char *filename, const char *saveName) const {
	debugC(9, kDebugLevelMain, "KyraEngine::openSaveForWriting('%s', '%s')", filename, saveName);
	if (_quitFlag)
		return 0;

	Common::OutSaveFile *out = 0;
	if (!(out = _saveFileMan->openForSaving(filename))) {
		warning("Can't create file '%s', game not saved", filename);
		return 0;
	}

	// Savegame version
	out->writeUint32BE(saveGameID());
	out->writeUint32BE(CURRENT_SAVE_VERSION);
	out->write(saveName, 31);
	if (_flags.isTalkie)
		out->writeUint32BE(GF_TALKIE);
	else if (_flags.platform == Common::kPlatformFMTowns || _flags.platform == Common::kPlatformPC98)
		out->writeUint32BE(GF_FMTOWNS);
	else
		out->writeUint32BE(GF_FLOPPY);

	if (out->ioFailed()) {
		warning("Can't write file '%s'. (Disk full?)", filename);
		delete out;
		return 0;
	}

	return out;
}

} // end of namespace Kyra