aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/quetzal.cpp
blob: e60460209aa430c1729ab28a9de72e1a4696bdd8 (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
/* 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 "glk/quetzal.h"
#include "glk/glk_api.h"
#include "glk/events.h"
#include "common/system.h"
#include "common/language.h"
#include "common/memstream.h"
#include "common/translation.h"

namespace Glk {

const uint32 INTERPRETER_IDS[INTERPRETER_TADS3 + 1] = {
	MKTAG('A', 'D', 'R', 'I'),
	MKTAG('A', 'S', 'Y', 'S'),
	MKTAG('A', 'G', 'I', 'L'),
	MKTAG('A', 'L', 'N', '2'),
	MKTAG('A', 'L', 'N', '3'),
	MKTAG('B', 'O', 'C', 'F'),
	MKTAG('Z', 'C', 'O', 'D'),
	MKTAG('G', 'E', 'A', 'S'),
	MKTAG('G', 'L', 'U', 'L'),
	MKTAG('H', 'U', 'G', 'O'),
	MKTAG('J', 'A', 'C', 'L'),
	MKTAG('L', 'V', 'L', '9'),
	MKTAG('M', 'A', 'G', 'N'),
	MKTAG('S', 'C', 'A', 'R'),
	MKTAG('S', 'C', 'O', 'T'),
	MKTAG('T', 'A', 'D', '2'),
	MKTAG('T', 'A', 'D', '3')
};

void QuetzalReader::clear() {
	_chunks.clear();
	_stream = nullptr;
}

bool QuetzalReader::open(Common::SeekableReadStream *stream, uint32 formType) {
	clear();
	stream->seek(0);
	_stream = stream;

	if (stream->readUint32BE() != ID_FORM)
		return false;

	uint32 size = stream->readUint32BE();
	uint32 fileFormType = stream->readUint32BE();

	if ((formType != 0 && fileFormType != formType) ||
		(formType == 0 && fileFormType != ID_IFZS && fileFormType != ID_IFSF))
		return false;

	if ((int)size > stream->size() || (size & 1) || (size < 4))
		return false;
	size -= 4;

	// Iterate through reading chunk headers
	while (size > 0) {
		if (size < 8)
			// Couldn't contain a chunk
			return false;

		// Get in the chunk header
		Chunk c;
		c._id = stream->readUint32BE();
		c._size = stream->readUint32BE();
		c._offset = stream->pos();
		_chunks.push_back(c);

		int chunkRemainder = c._size + (c._size & 1);
		if ((stream->pos() + chunkRemainder) > stream->size())
			// Chunk goes beyond the file size
			return false;

		size -= 8 + chunkRemainder;
		stream->skip(chunkRemainder);
	}

	return true;
}

bool QuetzalReader::getSavegameDescription(Common::SeekableReadStream *rs, Common::String &saveName) {
	QuetzalReader r;
	if (!r.open(rs, 0))
		return false;

	for (Iterator it = r.begin(); it != r.end(); ++it) {
		if ((*it)._id == ID_ANNO) {
			Common::SeekableReadStream *s = it.getStream();
			saveName = readString(s);
			delete s;

			return true;
		}
	}

	saveName = _("Untitled Savegame");
	return true;
}

bool QuetzalReader::getSavegameMetaInfo(Common::SeekableReadStream *rs, SaveStateDescriptor &ssd) {
	QuetzalReader r;
	if (!r.open(rs, 0))
		return false;

	ssd.setDescription(_("Untitled Savegame"));

	for (Iterator it = r.begin(); it != r.end(); ++it) {
		if ((*it)._id == ID_ANNO) {
			Common::SeekableReadStream *s = it.getStream();
			ssd.setDescription(readString(s));
			delete s;

		} else if ((*it)._id == ID_SCVM) {
			Common::SeekableReadStream *s = it.getStream();
			int year = s->readUint16BE();
			int month = s->readUint16BE();
			int day = s->readUint16BE();
			int hour = s->readUint16BE();
			int minute = s->readUint16BE();
			uint32 playTime = s->readUint32BE();
			delete s;

			ssd.setSaveDate(year, month, day);
			ssd.setSaveTime(hour, minute);
			ssd.setPlayTime(playTime);
		}
	}

	return true;
}

Common::String QuetzalReader::readString(Common::ReadStream *src) {
	char c;
	Common::String result;
	while ((c = src->readByte()) != 0)
		result += c;

	return result;
}

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

Common::WriteStream &QuetzalWriter::add(uint32 chunkId) {
	// Sanity check to prevent adding the same chunk multiple times
	for (uint idx = 0; idx < _chunks.size(); ++idx) {
		if (_chunks[idx]._id == chunkId)
			error("Duplicate chunk added");
	}

	_chunks.push_back(Chunk(chunkId));
	return _chunks.back()._stream;
}

void QuetzalWriter::save(Common::WriteStream *out, const Common::String &saveName, uint32 formType) {
	// Add chunks common to all Glk savegames
	addCommonChunks(saveName);
	
	// Calculate the size of the chunks
	uint size = 4;
	for (uint idx = 0; idx < _chunks.size(); ++idx)
		size += 8 + _chunks[idx]._stream.size() + (_chunks[idx]._stream.size() & 1);

	// Write out the header
	out->writeUint32BE(ID_FORM);
	out->writeUint32BE(size);
	out->writeUint32BE(formType);

	// Loop through writing the chunks
	for (uint idx = 0; idx < _chunks.size(); ++idx) {
		Common::MemoryWriteStreamDynamic &s = _chunks[idx]._stream;

		out->writeUint32BE(_chunks[idx]._id);
		out->writeUint32BE(s.size());
		out->write(s.getData(), s.size());
		if (s.size() & 1)
			out->writeByte(0);
	}
}

void QuetzalWriter::addCommonChunks(const Common::String &saveName) {
	// Write 'ANNO' chunk
	{
		Common::WriteStream &ws = add(ID_ANNO);
		ws.write(saveName.c_str(), saveName.size());
		ws.writeByte(0);
	}

	// Write 'SCVM' chunk with game version & gameplay statistics
	{
		Common::WriteStream &ws = add(ID_SCVM);

		// Write out the save date/time
		TimeDate td;
		g_system->getTimeAndDate(td);
		ws.writeSint16BE(td.tm_year + 1900);
		ws.writeSint16BE(td.tm_mon + 1);
		ws.writeSint16BE(td.tm_mday);
		ws.writeSint16BE(td.tm_hour);
		ws.writeSint16BE(td.tm_min);
		ws.writeUint32BE(g_vm->_events->getTotalPlayTicks());

		// Write out intrepreter type, language, and game Id
		ws.writeUint32BE(INTERPRETER_IDS[g_vm->getInterpreterType()]);
		const char *langCode = getLanguageCode(g_vm->getLanguage());
		ws.write(langCode, strlen(langCode) + 1);
		Common::String md5 = g_vm->getGameMD5();
		ws.write(md5.c_str(), md5.size());
		ws.writeByte('\0');
	}
}

} // End of namespace Glk