1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software{} you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation{} either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY{} without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program{} if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "glk/blorb.h"
24 #include "common/memstream.h"
25
26 namespace Glk {
27
28 /*--------------------------------------------------------------------------*/
29
Blorb(const Common::String & filename,InterpreterType interpType)30 Blorb::Blorb(const Common::String &filename, InterpreterType interpType) :
31 Common::Archive(), _filename(filename), _interpType(interpType) {
32 if (load() != Common::kNoError)
33 error("Could not parse blorb file");
34 }
35
Blorb(const Common::FSNode & fileNode,InterpreterType interpType)36 Blorb::Blorb(const Common::FSNode &fileNode, InterpreterType interpType) :
37 Common::Archive(), _fileNode(fileNode), _interpType(interpType) {
38 if (load() != Common::kNoError)
39 error("Could not parse blorb file");
40 }
41
hasFile(const Common::Path & path) const42 bool Blorb::hasFile(const Common::Path &path) const {
43 Common::String name = path.toString();
44 for (uint idx = 0; idx < _chunks.size(); ++idx) {
45 if (_chunks[idx]._filename.equalsIgnoreCase(name))
46 return true;
47 }
48
49 return false;
50 }
51
listMembers(Common::ArchiveMemberList & list) const52 int Blorb::listMembers(Common::ArchiveMemberList &list) const {
53 for (uint idx = 0; idx < _chunks.size(); ++idx) {
54 list.push_back(Common::ArchiveMemberList::value_type(new Common::GenericArchiveMember(_chunks[idx]._filename, this)));
55 }
56
57 return (int)_chunks.size();
58 }
59
getMember(const Common::Path & path) const60 const Common::ArchiveMemberPtr Blorb::getMember(const Common::Path &path) const {
61 Common::String name = path.toString();
62 if (!hasFile(name))
63 return Common::ArchiveMemberPtr();
64
65 return Common::ArchiveMemberPtr(new Common::GenericArchiveMember(name, this));
66 }
67
createReadStreamForMember(const Common::Path & path) const68 Common::SeekableReadStream *Blorb::createReadStreamForMember(const Common::Path &path) const {
69 Common::String name = path.toString();
70 for (uint idx = 0; idx < _chunks.size(); ++idx) {
71 const ChunkEntry &ce = _chunks[idx];
72
73 if (ce._filename.equalsIgnoreCase(name)) {
74 Common::File f;
75 if ((!_filename.empty() && !f.open(_filename)) ||
76 (_filename.empty() && !f.open(_fileNode)))
77 error("Reading failed");
78
79 f.seek(ce._offset);
80 Common::SeekableReadStream *result;
81
82 if (ce._id == ID_FORM) {
83 // AIFF chunks need to be wrapped in a FORM chunk for ScummVM decoder
84 byte *sound = (byte *)malloc(ce._size + 8);
85 WRITE_BE_UINT32(sound, MKTAG('F', 'O', 'R', 'M'));
86 WRITE_BE_UINT32(sound + 4, 0);
87 f.read(sound + 8, ce._size);
88 assert(READ_BE_UINT32(sound + 8) == ID_AIFF);
89
90 result = new Common::MemoryReadStream(sound, ce._size + 8, DisposeAfterUse::YES);
91 } else {
92 result = f.readStream(ce._size);
93 }
94
95 f.close();
96 return result;
97 }
98 }
99
100 return nullptr;
101 }
102
load()103 Common::ErrorCode Blorb::load() {
104 // First, chew through the file and index the chunks
105 Common::File f;
106 if ((!_filename.empty() && !f.open(_filename)) ||
107 (_filename.empty() && !f.open(_fileNode)))
108 return Common::kReadingFailed;
109
110 if (!isBlorb(f))
111 return Common::kReadingFailed;
112
113 if (!readRIdx(f, _chunks))
114 return Common::kReadingFailed;
115
116 // Further iterate through the resources
117 for (uint idx = 0; idx < _chunks.size(); ++idx) {
118 ChunkEntry &ce = _chunks[idx];
119
120 if (ce._type == ID_Pict) {
121 ce._filename = Common::String::format("pic%u", ce._number);
122 if (ce._id == ID_JPEG)
123 ce._filename += ".jpg";
124 else if (ce._id == ID_PNG)
125 ce._filename += ".png";
126 else if (ce._id == ID_Rect)
127 ce._filename += ".rect";
128
129 } else if (ce._type == ID_Snd) {
130 ce._filename = Common::String::format("sound%u", ce._number);
131 if (ce._id == ID_MIDI)
132 ce._filename += ".midi";
133 else if (ce._id == ID_MP3)
134 ce._filename += ".mp3";
135 else if (ce._id == ID_WAVE)
136 ce._filename += ".wav";
137 else if (ce._id == ID_AIFF || ce._id == ID_FORM)
138 ce._filename += ".aiff";
139 else if (ce._id == ID_OGG)
140 ce._filename += ".ogg";
141 else if (ce._id == ID_MOD)
142 ce._filename += ".mod";
143
144 } else if (ce._type == ID_Data) {
145 ce._filename = Common::String::format("data%u", ce._number);
146
147 } else if (ce._type == ID_Exec) {
148 if (
149 (_interpType == INTERPRETER_ADRIFT && ce._id == ID_ADRI) ||
150 (_interpType == INTERPRETER_GLULX && ce._id == ID_GLUL) ||
151 (_interpType == INTERPRETER_HUGO && ce._id == ID_HUGO) ||
152 (_interpType == INTERPRETER_SCOTT && ce._id == ID_SAAI) ||
153 (_interpType == INTERPRETER_TADS2 && ce._id == ID_TAD2) ||
154 (_interpType == INTERPRETER_TADS3 && ce._id == ID_TAD3) ||
155 (_interpType == INTERPRETER_ZCODE && ce._id == ID_ZCOD)
156 ) {
157 // Game executable
158 ce._filename = "game";
159 } else {
160 char buffer[5];
161 WRITE_BE_UINT32(buffer, ce._id);
162 buffer[4] = '\0';
163 Common::String type(buffer);
164 ce._filename = type;
165 }
166 }
167 }
168
169 // Check through any optional remaining chunks for an adaptive palette list
170 while (f.pos() < f.size()) {
171 uint chunkId = f.readUint32BE();
172 uint chunkSize = f.readUint32BE();
173
174 if (chunkId == ID_APal && chunkSize > 0) {
175 // Found one, so create an entry so it can be opened as file named "apal"
176 ChunkEntry ce;
177 ce._filename = "apal";
178 ce._offset = f.pos();
179 ce._size = chunkSize;
180 ce._type = ID_APal;
181 _chunks.push_back(ce);
182 break;
183 }
184
185 if (chunkSize & 1)
186 ++chunkSize;
187 f.skip(chunkSize);
188 }
189
190 return Common::kNoError;
191 }
192
readRIdx(Common::SeekableReadStream & stream,Common::Array<ChunkEntry> & chunks)193 bool Blorb::readRIdx(Common::SeekableReadStream &stream, Common::Array<ChunkEntry> &chunks) {
194 if (stream.readUint32BE() != ID_RIdx)
195 return false;
196
197 uint chunkLen = stream.readUint32BE();
198 uint count = stream.readUint32BE();
199 assert(count == (chunkLen - 4) / 12);
200
201 // First read in the resource index
202 for (uint idx = 0; idx < count; ++idx) {
203 ChunkEntry ce;
204 ce._type = stream.readUint32BE();
205 ce._number = stream.readUint32BE();
206 ce._offset = stream.readUint32BE();
207
208 chunks.push_back(ce);
209 }
210
211 // Temporarily store the start of the next chunk of the file (if any)
212 size_t nextChunkOffset = stream.pos();
213
214 // Further iterate through the resources
215 for (uint idx = 0; idx < chunks.size(); ++idx) {
216 ChunkEntry &ce = chunks[idx];
217 stream.seek(ce._offset);
218 ce._offset += 8;
219
220 ce._id = stream.readUint32BE();
221 ce._size = stream.readUint32BE();
222 }
223
224 // Reset back to the next chunk, and return that the index was successfully read
225 stream.seek(nextChunkOffset);
226 return true;
227 }
228
isBlorb(Common::SeekableReadStream & stream,uint32 type)229 bool Blorb::isBlorb(Common::SeekableReadStream &stream, uint32 type) {
230 if (stream.size() < 12)
231 return false;
232 if (stream.readUint32BE() != ID_FORM)
233 return false;
234 stream.readUint32BE();
235 if (stream.readUint32BE() != ID_IFRS)
236 return false;
237
238 if (type == 0)
239 return true;
240
241 Common::Array<ChunkEntry> chunks;
242 if (!readRIdx(stream, chunks))
243 return false;
244
245 // Further iterate through the resources
246 for (uint idx = 0; idx < chunks.size(); ++idx) {
247 ChunkEntry &ce = chunks[idx];
248 if (ce._type == ID_Exec && ce._id == type)
249 return true;
250 }
251
252 return false;
253 }
254
isBlorb(const Common::String & filename,uint32 type)255 bool Blorb::isBlorb(const Common::String &filename, uint32 type) {
256 Common::File f;
257 if (!filename.empty() && !f.open(filename))
258 return false;
259
260 return isBlorb(f, type);
261 }
262
hasBlorbExt(const Common::String & filename)263 bool Blorb::hasBlorbExt(const Common::String &filename) {
264 return filename.hasSuffixIgnoreCase(".blorb") || filename.hasSuffixIgnoreCase(".zblorb")
265 || filename.hasSuffixIgnoreCase(".gblorb") || filename.hasSuffixIgnoreCase(".blb")
266 || filename.hasSuffixIgnoreCase(".zlb") || filename.hasSuffixIgnoreCase(".a3r");
267 }
268
getBlorbFilenames(const Common::String & srcFilename,Common::StringArray & filenames,InterpreterType interpType,const Common::String & gameId)269 void Blorb::getBlorbFilenames(const Common::String &srcFilename, Common::StringArray &filenames,
270 InterpreterType interpType, const Common::String &gameId) {
271 // Strip off the source filename extension
272 Common::String filename = srcFilename;
273 if (!filename.contains('.')) {
274 filename += '.';
275 } else {
276 while (filename[filename.size() - 1] != '.')
277 filename.deleteLastChar();
278 }
279
280 // Add in the different possible filenames
281 filenames.clear();
282 filenames.push_back(filename + "blorb");
283 filenames.push_back(filename + "blb");
284
285 switch (interpType) {
286 case INTERPRETER_ALAN3:
287 filenames.push_back(filename + "a3r");
288 break;
289 case INTERPRETER_GLULX:
290 filenames.push_back(filename + "gblorb");
291 break;
292 case INTERPRETER_ZCODE:
293 filenames.push_back(filename + "zblorb");
294 getInfocomBlorbFilenames(filenames, gameId);
295 break;
296 default:
297 break;
298 }
299 }
300
getInfocomBlorbFilenames(Common::StringArray & filenames,const Common::String & gameId)301 void Blorb::getInfocomBlorbFilenames(Common::StringArray &filenames, const Common::String &gameId) {
302 if (gameId == "beyondzork")
303 filenames.push_back("beyondzork.blb");
304 else if (gameId == "journey")
305 filenames.push_back("journey.blb");
306 else if (gameId == "lurkinghorror")
307 filenames.push_back("lurking.blb");
308 else if (gameId == "questforexcalibur")
309 filenames.push_back("arthur.blb");
310 else if (gameId == "sherlockriddle")
311 filenames.push_back("sherlock.blb");
312 else if (gameId == "shogun")
313 filenames.push_back("shogun.blb");
314 else if (gameId == "zork0")
315 filenames.push_back("zorkzero.blb");
316 }
317
318 } // End of namespace Glk
319