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 "common/config-manager.h"
24 #include "common/error.h"
25 #include "common/file.h"
26 #include "common/macresman.h"
27 #include "common/substream.h"
28 #include "common/winexe.h"
29 #include "graphics/wincursor.h"
30
31 #include "director/director.h"
32 #include "director/cast.h"
33 #include "director/castmember.h"
34 #include "director/movie.h"
35 #include "director/window.h"
36 #include "director/lingo/lingo.h"
37 #include "director/util.h"
38
39 namespace Director {
40
createArchive()41 Archive *DirectorEngine::createArchive() {
42 if (getPlatform() != Common::kPlatformWindows) {
43 if (getVersion() < 400)
44 return new MacArchive();
45 else
46 return new RIFXArchive();
47 } else {
48 if (getVersion() < 400)
49 return new RIFFArchive();
50 else
51 return new RIFXArchive();
52 }
53 }
54
loadInitialMovie()55 Common::Error Window::loadInitialMovie() {
56 debug(0, "\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
57 debug(0, "@@@@ Loading initial movie");
58 debug(0, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
59 Common::String movie = (_vm->getGameGID() == GID_TESTALL) ? getNextMovieFromQueue().movie : _vm->getEXEName();
60
61 if (g_director->getPlatform() == Common::kPlatformWindows) {
62 loadEXE(movie);
63 } else {
64 probeProjector(movie);
65 loadMac(movie);
66 }
67
68 if (!_mainArchive) {
69 warning("Cannot open main movie");
70 return Common::kNoGameDataFoundError;
71 }
72
73 _currentMovie = new Movie(this);
74 _currentPath = getPath(movie, _currentPath);
75 Common::String sharedCastPath = getSharedCastPath();
76 if (!sharedCastPath.empty() && !sharedCastPath.equalsIgnoreCase(movie))
77 _currentMovie->loadSharedCastsFrom(sharedCastPath);
78
79 // load startup movie
80 Common::String startupPath = g_director->getStartupPath();
81 if (!startupPath.empty()) {
82 Common::SeekableReadStream *const stream = SearchMan.createReadStreamForMember(startupPath);
83 if (stream) {
84 uint size = stream->size();
85 char *script = (char *)calloc(size + 1, 1);
86
87 stream->read(script, size);
88
89 LingoArchive *mainArchive = g_director->getCurrentMovie()->getMainLingoArch();
90 mainArchive->addCode(Common::U32String(script, Common::kMacRoman), kMovieScript, 65535);
91 _currentMovie->processEvent(kEventStartUp);
92
93 free(script);
94 } else {
95 warning("Window::LoadInitialMovie: failed to load startup scripts");
96 }
97 }
98
99 _currentMovie->setArchive(_mainArchive);
100
101 // XLibs are usually loaded in the initial movie.
102 // These may not be present if a --start-movie is specified, so
103 // we sometimes need to load them manually.
104 if (!g_director->getStartMovie().startMovie.empty())
105 loadStartMovieXLibs();
106
107 return Common::kNoError;
108 }
109
probeProjector(const Common::String & movie)110 void Window::probeProjector(const Common::String &movie) {
111 if (g_director->getPlatform() == Common::kPlatformWindows)
112 return;
113
114 MacArchive *archive = new MacArchive();
115 if (!archive->openFile(movie)) {
116 delete archive;
117 return;
118 }
119
120 probeMacBinary(archive);
121 delete archive;
122 }
123
probeMacBinary(MacArchive * archive)124 void Window::probeMacBinary(MacArchive *archive) {
125 // Let's check if it is a projector file
126 // So far tested with Spaceship Warlock, D2
127 if (archive->hasResource(MKTAG('B', 'N', 'D', 'L'), "Projector")) {
128 warning("Detected Projector file");
129
130 if (archive->hasResource(MKTAG('v', 'e', 'r', 's'), -1)) {
131 Common::Array<uint16> vers = archive->getResourceIDList(MKTAG('v', 'e', 'r', 's'));
132 for (Common::Array<uint16>::iterator iterator = vers.begin(); iterator != vers.end(); ++iterator) {
133 Common::SeekableReadStreamEndian *vvers = archive->getResource(MKTAG('v', 'e', 'r', 's'), *iterator);
134 Common::MacResManager::MacVers *v = Common::MacResManager::parseVers(vvers);
135
136 debug(0, "Detected vers %d.%d %s.%d region %d '%s' '%s'", v->majorVer, v->minorVer, v->devStr.c_str(),
137 v->preReleaseVer, v->region, v->str.c_str(), v->msg.c_str());
138
139 delete v;
140 }
141 }
142
143 if (archive->hasResource(MKTAG('S', 'T', 'R', '#'), 0)) {
144 if (_currentMovie)
145 _currentMovie->setArchive(archive);
146
147 Common::SeekableReadStreamEndian *name = archive->getResource(MKTAG('S', 'T', 'R', '#'), 0);
148 int num = name->readUint16();
149 if (num != 1) {
150 warning("Incorrect number of strings in Projector file");
151 }
152
153 if (num == 0)
154 error("No strings in Projector file");
155
156 Common::String sname = name->readPascalString();
157 Common::String moviePath = pathMakeRelative(sname);
158 if (testPath(moviePath)) {
159 _nextMovie.movie = moviePath;
160 warning("Replaced score name with: %s (from %s)", _nextMovie.movie.c_str(), sname.c_str());
161
162 delete _currentMovie;
163 _currentMovie = nullptr;
164
165 } else {
166 warning("Couldn't find score with name: %s", sname.c_str());
167 }
168 delete name;
169
170
171 }
172 }
173
174 if (archive->hasResource(MKTAG('X', 'C', 'O', 'D'), -1)) {
175 Common::Array<uint16> xcod = archive->getResourceIDList(MKTAG('X', 'C', 'O', 'D'));
176 for (Common::Array<uint16>::iterator iterator = xcod.begin(); iterator != xcod.end(); ++iterator) {
177 Resource res = archive->getResourceDetail(MKTAG('X', 'C', 'O', 'D'), *iterator);
178 debug(0, "Detected XObject '%s'", res.name.c_str());
179 g_lingo->openXLib(res.name, kXObj);
180 }
181 }
182 }
183
openMainArchive(const Common::String movie)184 Archive *Window::openMainArchive(const Common::String movie) {
185 debug(1, "openMainArchive(\"%s\")", movie.c_str());
186
187 _mainArchive = g_director->createArchive();
188
189 if (!_mainArchive->openFile(movie)) {
190 delete _mainArchive;
191 _mainArchive = nullptr;
192
193 warning("openMainArchive(): Could not open '%s'", movie.c_str());
194 return nullptr;
195 }
196
197 return _mainArchive;
198 }
199
loadEXE(const Common::String movie)200 void Window::loadEXE(const Common::String movie) {
201 Common::SeekableReadStream *iniStream = SearchMan.createReadStreamForMember("LINGO.INI");
202 if (iniStream) {
203 char *script = (char *)calloc(iniStream->size() + 1, 1);
204 iniStream->read(script, iniStream->size());
205
206 _currentMovie = new Movie(this);
207 _currentMovie->getMainLingoArch()->addCode(Common::U32String(script, Common::kWindows1252), kMovieScript, 0);
208 _currentMovie->processEvent(kEventStartUp);
209 delete _currentMovie;
210 _currentMovie = nullptr;
211
212 free(script);
213 } else {
214 warning("No LINGO.INI");
215 }
216
217 Common::SeekableReadStream *exeStream = SearchMan.createReadStreamForMember(Common::Path(movie, g_director->_dirSeparator));
218 if (!exeStream)
219 error("Failed to open EXE '%s'", g_director->getEXEName().c_str());
220
221 uint32 initialTag = exeStream->readUint32LE();
222 if (initialTag == MKTAG('R', 'I', 'F', 'X') || initialTag == MKTAG('X', 'F', 'I', 'R')) {
223 // we've encountered a movie saved from Director, not a projector.
224 loadEXERIFX(exeStream, 0);
225 } else if (initialTag == MKTAG('R', 'I', 'F', 'F') || initialTag == MKTAG('F', 'F', 'I', 'R')) { // This is just a normal movie
226 _mainArchive = new RIFFArchive();
227
228 if (!_mainArchive->openStream(exeStream, 0))
229 error("Failed to load RIFF");
230 } else {
231 Common::WinResources *exe = Common::WinResources::createFromEXE(movie);
232 if (!exe)
233 error("Failed to open EXE '%s'", g_director->getEXEName().c_str());
234
235 const Common::Array<Common::WinResourceID> versions = exe->getIDList(Common::kWinVersion);
236 for (uint i = 0; i < versions.size(); i++) {
237 Common::WinResources::VersionInfo *info = exe->getVersionResource(versions[i]);
238
239 for (Common::WinResources::VersionHash::const_iterator it = info->hash.begin(); it != info->hash.end(); ++it)
240 warning("info <%s>: <%s>", it->_key.c_str(), it->_value.encode().c_str());
241
242 delete info;
243
244 }
245
246 Common::Array<Common::WinResourceID> idList = exe->getIDList(Common::kWinGroupCursor);
247 for (uint i = 0; i < idList.size(); i++) {
248 Graphics::WinCursorGroup *group = Graphics::WinCursorGroup::createCursorGroup(exe, idList[i]);
249 g_director->_winCursor.push_back(group);
250 }
251
252 delete exe;
253
254 exeStream->seek(-4, SEEK_END);
255 exeStream->seek(exeStream->readUint32LE());
256
257 if (g_director->getVersion() >= 700) {
258 loadEXEv7(exeStream);
259 } else if (g_director->getVersion() >= 500) {
260 loadEXEv5(exeStream);
261 } else if (g_director->getVersion() >= 400) {
262 loadEXEv4(exeStream);
263 } else if (g_director->getVersion() >= 200) {
264 loadEXEv3(exeStream);
265 } else {
266 error("Unhandled Windows EXE version %d", g_director->getVersion());
267 }
268 }
269
270 if (_mainArchive)
271 _mainArchive->setPathName(movie);
272 }
273
loadEXEv3(Common::SeekableReadStream * stream)274 void Window::loadEXEv3(Common::SeekableReadStream *stream) {
275 uint16 entryCount = stream->readUint16LE();
276 if (entryCount != 1)
277 error("Unhandled multiple entry v3 EXE");
278
279 stream->skip(5); // unknown
280
281 uint32 mmmSize = stream->readUint32LE(); // Main MMM size
282
283 Common::String mmmFileName = stream->readPascalString();
284 Common::String directoryName = stream->readPascalString();
285
286 debugC(1, kDebugLoading, "Main MMM: '%s'", mmmFileName.c_str());
287 debugC(1, kDebugLoading, "Directory Name: '%s'", directoryName.c_str());
288 debugC(1, kDebugLoading, "Main mmmSize: %d (0x%x)", mmmSize, mmmSize);
289
290 if (mmmSize) {
291 uint32 riffOffset = stream->pos();
292
293 debugC(1, kDebugLoading, "RIFF offset: %d (%x)", riffOffset, riffOffset);
294
295 if (ConfMan.getBool("dump_scripts")) {
296 Common::DumpFile out;
297 byte *buf = (byte *)malloc(mmmSize);
298 stream->read(buf, mmmSize);
299 stream->seek(riffOffset);
300 Common::String fname = Common::String::format("./dumps/%s", encodePathForDump(mmmFileName).c_str());
301
302
303 if (!out.open(fname.c_str(), true)) {
304 warning("Window::loadEXEv3(): Can not open dump file %s", fname.c_str());
305 } else {
306 out.write(buf, mmmSize);
307
308 out.flush();
309 out.close();
310 }
311
312 free(buf);
313 }
314
315
316 _mainArchive = new RIFFArchive();
317
318 if (!_mainArchive->openStream(stream, riffOffset))
319 warning("Failed to load RIFF from EXE");
320 else
321 return;
322 }
323
324 openMainArchive(mmmFileName);
325 }
326
loadEXEv4(Common::SeekableReadStream * stream)327 void Window::loadEXEv4(Common::SeekableReadStream *stream) {
328 uint32 ver = stream->readUint32BE();
329
330 if (ver != MKTAG('P', 'J', '9', '3'))
331 error("Invalid projector tag found in v4 EXE [%s]", tag2str(ver));
332
333 uint32 rifxOffset = stream->readUint32LE();
334 /* uint32 fontMapOffset = */ stream->readUint32LE();
335 /* uint32 resourceForkOffset1 = */ stream->readUint32LE();
336 /* uint32 resourceForkOffset2 = */ stream->readUint32LE();
337 stream->readUint32LE(); // graphics DLL offset
338 stream->readUint32LE(); // sound DLL offset
339 /* uint32 rifxOffsetAlt = */ stream->readUint32LE(); // equivalent to rifxOffset
340 uint32 flags = stream->readUint32LE();
341
342 warning("PJ93 projector flags: %08x", flags);
343
344 loadEXERIFX(stream, rifxOffset);
345 }
346
loadEXEv5(Common::SeekableReadStream * stream)347 void Window::loadEXEv5(Common::SeekableReadStream *stream) {
348 uint32 ver = stream->readUint32LE();
349
350 if (ver != MKTAG('P', 'J', '9', '5'))
351 error("Invalid projector tag found in v5 EXE [%s]", tag2str(ver));
352
353 uint32 rifxOffset = stream->readUint32LE();
354 uint32 pflags = stream->readUint32LE();
355 uint32 flags = stream->readUint32LE();
356 stream->readUint16LE(); // x
357 stream->readUint16LE(); // y
358 stream->readUint16LE(); // screenWidth
359 stream->readUint16LE(); // screenHeight
360 stream->readUint32LE(); // number of components
361 stream->readUint32LE(); // number of driver files
362 stream->readUint32LE(); // fontMapOffset
363
364 warning("PJ95 projector pflags: %08x flags: %08x", pflags, flags);
365
366 loadEXERIFX(stream, rifxOffset);
367 }
368
loadEXEv7(Common::SeekableReadStream * stream)369 void Window::loadEXEv7(Common::SeekableReadStream *stream) {
370 uint32 ver = stream->readUint32LE();
371
372 if (ver != MKTAG('P', 'J', '0', '0') && ver != MKTAG('P', 'J', '0', '1'))
373 error("Invalid projector tag found in v7 EXE [%s]", tag2str(ver));
374
375 uint32 rifxOffset = stream->readUint32LE();
376 stream->readUint32LE(); // unknown
377 stream->readUint32LE(); // unknown
378 stream->readUint32LE(); // unknown
379 stream->readUint32LE(); // unknown
380 stream->readUint32LE(); // some DLL offset
381
382 loadEXERIFX(stream, rifxOffset);
383 }
384
loadEXERIFX(Common::SeekableReadStream * stream,uint32 offset)385 void Window::loadEXERIFX(Common::SeekableReadStream *stream, uint32 offset) {
386 _mainArchive = new RIFXArchive();
387
388 if (!_mainArchive->openStream(stream, offset))
389 error("Failed to load RIFX from EXE");
390 }
391
loadMac(const Common::String movie)392 void Window::loadMac(const Common::String movie) {
393 if (g_director->getVersion() < 400) {
394 // The data is part of the resource fork of the executable
395 openMainArchive(movie);
396 } else {
397 // The RIFX is located in the data fork of the executable
398 _macBinary = new Common::MacResManager();
399
400 if (!_macBinary->open(Common::Path(movie, g_director->_dirSeparator)) || !_macBinary->hasDataFork())
401 error("Failed to open Mac binary '%s'", movie.c_str());
402
403 Common::SeekableReadStream *dataFork = _macBinary->getDataFork();
404 _mainArchive = new RIFXArchive();
405 _mainArchive->setPathName(movie);
406
407 // First we need to detect PPC vs. 68k
408
409 uint32 tag = dataFork->readUint32BE();
410 uint32 startOffset;
411
412 if (SWAP_BYTES_32(tag) == MKTAG('P', 'J', '9', '3') || tag == MKTAG('P', 'J', '9', '5') || tag == MKTAG('P', 'J', '0', '0')) {
413 // PPC: The RIFX shares the data fork with the binary
414 startOffset = dataFork->readUint32BE();
415 } else {
416 // 68k: The RIFX is the only thing in the data fork
417 startOffset = 0;
418 }
419
420 if (!_mainArchive->openStream(dataFork, startOffset)) {
421 warning("Failed to load RIFX from Mac binary");
422 delete _currentMovie;
423 _currentMovie = nullptr;
424 }
425 }
426 }
427
loadStartMovieXLibs()428 void Window::loadStartMovieXLibs() {
429 if (strcmp(g_director->getGameId(), "warlock") == 0 && g_director->getPlatform() != Common::kPlatformWindows) {
430 g_lingo->openXLib("FPlayXObj", kXObj);
431 }
432 g_lingo->openXLib("SerialPort", kXObj);
433 }
434
435 } // End of namespace Director
436