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 | * $URL: https://scummvm.svn.sourceforge.net/svnroot/scummvm/scummvm/tags/release-1-2-1/engines/sci/resource.cpp $
|
---|
22 | * $Id: resource.cpp 52788 2010-09-18 09:46:06Z thebluegr $
|
---|
23 | *
|
---|
24 | */
|
---|
25 |
|
---|
26 | // Resource library
|
---|
27 |
|
---|
28 | #include "common/file.h"
|
---|
29 | #include "common/fs.h"
|
---|
30 | #include "common/macresman.h"
|
---|
31 |
|
---|
32 | #include "sci/resource.h"
|
---|
33 | #include "sci/resource_intern.h"
|
---|
34 | #include "sci/util.h"
|
---|
35 |
|
---|
36 | namespace Sci {
|
---|
37 |
|
---|
38 | enum {
|
---|
39 | SCI0_RESMAP_ENTRIES_SIZE = 6,
|
---|
40 | SCI1_RESMAP_ENTRIES_SIZE = 6,
|
---|
41 | SCI11_RESMAP_ENTRIES_SIZE = 5
|
---|
42 | };
|
---|
43 |
|
---|
44 | /** resource type for SCI1 resource.map file */
|
---|
45 | struct resource_index_t {
|
---|
46 | uint16 wOffset;
|
---|
47 | uint16 wSize;
|
---|
48 | };
|
---|
49 |
|
---|
50 | //////////////////////////////////////////////////////////////////////
|
---|
51 |
|
---|
52 | static SciVersion s_sciVersion = SCI_VERSION_NONE; // FIXME: Move this inside a suitable class, e.g. SciEngine
|
---|
53 |
|
---|
54 | SciVersion getSciVersion() {
|
---|
55 | assert(s_sciVersion != SCI_VERSION_NONE);
|
---|
56 | return s_sciVersion;
|
---|
57 | }
|
---|
58 |
|
---|
59 | const char *getSciVersionDesc(SciVersion version) {
|
---|
60 | switch (version) {
|
---|
61 | case SCI_VERSION_NONE:
|
---|
62 | return "Invalid SCI version";
|
---|
63 | case SCI_VERSION_0_EARLY:
|
---|
64 | return "Early SCI0";
|
---|
65 | case SCI_VERSION_0_LATE:
|
---|
66 | return "Late SCI0";
|
---|
67 | case SCI_VERSION_01:
|
---|
68 | return "SCI01";
|
---|
69 | case SCI_VERSION_1_EGA:
|
---|
70 | return "SCI1 EGA";
|
---|
71 | case SCI_VERSION_1_EARLY:
|
---|
72 | return "Early SCI1";
|
---|
73 | case SCI_VERSION_1_MIDDLE:
|
---|
74 | return "Middle SCI1";
|
---|
75 | case SCI_VERSION_1_LATE:
|
---|
76 | return "Late SCI1";
|
---|
77 | case SCI_VERSION_1_1:
|
---|
78 | return "SCI1.1";
|
---|
79 | case SCI_VERSION_2:
|
---|
80 | return "SCI2";
|
---|
81 | case SCI_VERSION_2_1:
|
---|
82 | return "SCI2.1";
|
---|
83 | case SCI_VERSION_3:
|
---|
84 | return "SCI3";
|
---|
85 | default:
|
---|
86 | return "Unknown";
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | //////////////////////////////////////////////////////////////////////
|
---|
91 |
|
---|
92 |
|
---|
93 | #undef SCI_REQUIRE_RESOURCE_FILES
|
---|
94 |
|
---|
95 | //#define SCI_VERBOSE_resMan 1
|
---|
96 |
|
---|
97 | static const char *sci_error_types[] = {
|
---|
98 | "No error",
|
---|
99 | "I/O error",
|
---|
100 | "Resource is empty (size 0)",
|
---|
101 | "resource.map entry is invalid",
|
---|
102 | "resource.map file not found",
|
---|
103 | "No resource files found",
|
---|
104 | "Unknown compression method",
|
---|
105 | "Decompression failed: Decompression buffer overflow",
|
---|
106 | "Decompression failed: Sanity check failed",
|
---|
107 | "Decompression failed: Resource too big",
|
---|
108 | "SCI version is unsupported"
|
---|
109 | };
|
---|
110 |
|
---|
111 | static const char *s_resourceTypeNames[] = {
|
---|
112 | "view", "pic", "script", "text", "sound",
|
---|
113 | "memory", "vocab", "font", "cursor",
|
---|
114 | "patch", "bitmap", "palette", "cdaudio",
|
---|
115 | "audio", "sync", "message", "map", "heap",
|
---|
116 | "audio36", "sync36", "xlate", "robot", "vmd",
|
---|
117 | "chunk", "macibin", "macibis", "macpict"
|
---|
118 | };
|
---|
119 |
|
---|
120 | static const char *s_resourceTypeSuffixes[] = {
|
---|
121 | "v56", "p56", "scr", "tex", "snd",
|
---|
122 | "", "voc", "fon", "cur", "pat",
|
---|
123 | "bit", "pal", "cda", "aud", "syn",
|
---|
124 | "msg", "map", "hep", "", "",
|
---|
125 | "trn", "rbt", "vmd", "chk", "",
|
---|
126 | "", ""
|
---|
127 | };
|
---|
128 |
|
---|
129 | const char *getResourceTypeName(ResourceType restype) {
|
---|
130 | if (restype != kResourceTypeInvalid)
|
---|
131 | return s_resourceTypeNames[restype];
|
---|
132 | else
|
---|
133 | return "invalid";
|
---|
134 | }
|
---|
135 |
|
---|
136 | static const ResourceType s_resTypeMapSci0[] = {
|
---|
137 | kResourceTypeView, kResourceTypePic, kResourceTypeScript, kResourceTypeText, // 0x00-0x03
|
---|
138 | kResourceTypeSound, kResourceTypeMemory, kResourceTypeVocab, kResourceTypeFont, // 0x04-0x07
|
---|
139 | kResourceTypeCursor, kResourceTypePatch, kResourceTypeBitmap, kResourceTypePalette, // 0x08-0x0B
|
---|
140 | kResourceTypeCdAudio, kResourceTypeAudio, kResourceTypeSync, kResourceTypeMessage, // 0x0C-0x0F
|
---|
141 | kResourceTypeMap, kResourceTypeHeap, kResourceTypeAudio36, kResourceTypeSync36, // 0x10-0x13
|
---|
142 | kResourceTypeTranslation // 0x14
|
---|
143 | };
|
---|
144 |
|
---|
145 | // TODO: 12 should be "Wave", but SCI seems to just store it in Audio resources
|
---|
146 | static const ResourceType s_resTypeMapSci21[] = {
|
---|
147 | kResourceTypeView, kResourceTypePic, kResourceTypeScript, kResourceTypeText, // 0x00-0x03
|
---|
148 | kResourceTypeSound, kResourceTypeMemory, kResourceTypeVocab, kResourceTypeFont, // 0x04-0x07
|
---|
149 | kResourceTypeCursor, kResourceTypePatch, kResourceTypeBitmap, kResourceTypePalette, // 0x08-0x0B
|
---|
150 | kResourceTypeInvalid, kResourceTypeAudio, kResourceTypeSync, kResourceTypeMessage, // 0x0C-0x0F
|
---|
151 | kResourceTypeMap, kResourceTypeHeap, kResourceTypeChunk, kResourceTypeAudio36, // 0x10-0x13
|
---|
152 | kResourceTypeSync36, kResourceTypeTranslation, kResourceTypeRobot, kResourceTypeVMD // 0x14-0x17
|
---|
153 | };
|
---|
154 |
|
---|
155 | ResourceType ResourceManager::convertResType(byte type) {
|
---|
156 | type &= 0x7f;
|
---|
157 |
|
---|
158 | if (_mapVersion != kResVersionSci32) {
|
---|
159 | // SCI0 - SCI2
|
---|
160 | if (type < ARRAYSIZE(s_resTypeMapSci0))
|
---|
161 | return s_resTypeMapSci0[type];
|
---|
162 | } else {
|
---|
163 | // SCI2.1+
|
---|
164 | if (type < ARRAYSIZE(s_resTypeMapSci21)) {
|
---|
165 | // LSL6 hires doesn't have the chunk resource type, to match
|
---|
166 | // the resource types of the lowres version, thus we use the
|
---|
167 | // older resource types here
|
---|
168 | if (g_sci && g_sci->getGameId() == GID_LSL6HIRES)
|
---|
169 | return s_resTypeMapSci0[type];
|
---|
170 | else
|
---|
171 | return s_resTypeMapSci21[type];
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | return kResourceTypeInvalid;
|
---|
176 | }
|
---|
177 |
|
---|
178 | //-- Resource main functions --
|
---|
179 | Resource::Resource(ResourceManager *resMan, ResourceId id) : _resMan(resMan), _id(id) {
|
---|
180 | data = NULL;
|
---|
181 | size = 0;
|
---|
182 | _fileOffset = 0;
|
---|
183 | _status = kResStatusNoMalloc;
|
---|
184 | _lockers = 0;
|
---|
185 | _source = NULL;
|
---|
186 | _header = NULL;
|
---|
187 | _headerSize = 0;
|
---|
188 | }
|
---|
189 |
|
---|
190 | Resource::~Resource() {
|
---|
191 | delete[] data;
|
---|
192 | if (_source && _source->getSourceType() == kSourcePatch)
|
---|
193 | delete _source;
|
---|
194 | }
|
---|
195 |
|
---|
196 | void Resource::unalloc() {
|
---|
197 | delete[] data;
|
---|
198 | data = NULL;
|
---|
199 | _status = kResStatusNoMalloc;
|
---|
200 | }
|
---|
201 |
|
---|
202 | void Resource::writeToStream(Common::WriteStream *stream) const {
|
---|
203 | stream->writeByte(getType() | 0x80); // 0x80 is required by old sierra sci, otherwise it wont accept the patch file
|
---|
204 | stream->writeByte(_headerSize);
|
---|
205 | if (_headerSize > 0)
|
---|
206 | stream->write(_header, _headerSize);
|
---|
207 | stream->write(data, size);
|
---|
208 | }
|
---|
209 |
|
---|
210 | uint32 Resource::getAudioCompressionType() const {
|
---|
211 | return _source->getAudioCompressionType();
|
---|
212 | }
|
---|
213 |
|
---|
214 | uint32 AudioVolumeResourceSource::getAudioCompressionType() const {
|
---|
215 | return _audioCompressionType;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | ResourceSource::ResourceSource(ResSourceType type, const Common::String &name, int volNum, const Common::FSNode *resFile)
|
---|
220 | : _sourceType(type), _name(name), _volumeNumber(volNum), _resourceFile(resFile) {
|
---|
221 | _scanned = false;
|
---|
222 | }
|
---|
223 |
|
---|
224 | ResourceSource::~ResourceSource() {
|
---|
225 | }
|
---|
226 |
|
---|
227 | MacResourceForkResourceSource::MacResourceForkResourceSource(const Common::String &name, int volNum)
|
---|
228 | : ResourceSource(kSourceMacResourceFork, name, volNum) {
|
---|
229 | _macResMan = new Common::MacResManager();
|
---|
230 | assert(_macResMan);
|
---|
231 | }
|
---|
232 |
|
---|
233 | MacResourceForkResourceSource::~MacResourceForkResourceSource() {
|
---|
234 | delete _macResMan;
|
---|
235 | }
|
---|
236 |
|
---|
237 | //-- resMan helper functions --
|
---|
238 |
|
---|
239 | // Resource source list management
|
---|
240 |
|
---|
241 | ResourceSource *ResourceManager::addExternalMap(const Common::String &filename, int volume_nr) {
|
---|
242 | ResourceSource *newsrc = new ExtMapResourceSource(filename, volume_nr);
|
---|
243 |
|
---|
244 | _sources.push_back(newsrc);
|
---|
245 | return newsrc;
|
---|
246 | }
|
---|
247 |
|
---|
248 | ResourceSource *ResourceManager::addExternalMap(const Common::FSNode *mapFile, int volume_nr) {
|
---|
249 | ResourceSource *newsrc = new ExtMapResourceSource(mapFile->getName(), volume_nr, mapFile);
|
---|
250 |
|
---|
251 | _sources.push_back(newsrc);
|
---|
252 | return newsrc;
|
---|
253 | }
|
---|
254 |
|
---|
255 | ResourceSource *ResourceManager::addSource(ResourceSource *newsrc) {
|
---|
256 | assert(newsrc);
|
---|
257 |
|
---|
258 | _sources.push_back(newsrc);
|
---|
259 | return newsrc;
|
---|
260 | }
|
---|
261 |
|
---|
262 | ResourceSource *ResourceManager::addPatchDir(const Common::String &dirname) {
|
---|
263 | ResourceSource *newsrc = new DirectoryResourceSource(dirname);
|
---|
264 |
|
---|
265 | _sources.push_back(newsrc);
|
---|
266 | return 0;
|
---|
267 | }
|
---|
268 |
|
---|
269 | ResourceSource *ResourceManager::findVolume(ResourceSource *map, int volume_nr) {
|
---|
270 | for (Common::List<ResourceSource *>::iterator it = _sources.begin(); it != _sources.end(); ++it) {
|
---|
271 | ResourceSource *src = (*it)->findVolume(map, volume_nr);
|
---|
272 | if (src)
|
---|
273 | return src;
|
---|
274 | }
|
---|
275 |
|
---|
276 | return NULL;
|
---|
277 | }
|
---|
278 |
|
---|
279 | // Resource manager constructors and operations
|
---|
280 |
|
---|
281 | bool Resource::loadPatch(Common::SeekableReadStream *file) {
|
---|
282 | Resource *res = this;
|
---|
283 |
|
---|
284 | // We assume that the resource type matches res->type
|
---|
285 | // We also assume that the current file position is right at the actual data (behind resourceid/headersize byte)
|
---|
286 |
|
---|
287 | res->data = new byte[res->size];
|
---|
288 |
|
---|
289 | if (res->_headerSize > 0)
|
---|
290 | res->_header = new byte[res->_headerSize];
|
---|
291 |
|
---|
292 | if ((res->data == NULL) || ((res->_headerSize > 0) && (res->_header == NULL))) {
|
---|
293 | error("Can't allocate %d bytes needed for loading %s", res->size + res->_headerSize, res->_id.toString().c_str());
|
---|
294 | }
|
---|
295 |
|
---|
296 | unsigned int really_read;
|
---|
297 | if (res->_headerSize > 0) {
|
---|
298 | really_read = file->read(res->_header, res->_headerSize);
|
---|
299 | if (really_read != res->_headerSize)
|
---|
300 | error("Read %d bytes from %s but expected %d", really_read, res->_id.toString().c_str(), res->_headerSize);
|
---|
301 | }
|
---|
302 |
|
---|
303 | really_read = file->read(res->data, res->size);
|
---|
304 | if (really_read != res->size)
|
---|
305 | error("Read %d bytes from %s but expected %d", really_read, res->_id.toString().c_str(), res->size);
|
---|
306 |
|
---|
307 | res->_status = kResStatusAllocated;
|
---|
308 | return true;
|
---|
309 | }
|
---|
310 |
|
---|
311 | bool Resource::loadFromPatchFile() {
|
---|
312 | Common::File file;
|
---|
313 | const Common::String &filename = _source->getLocationName();
|
---|
314 | if (file.open(filename) == false) {
|
---|
315 | warning("Failed to open patch file %s", filename.c_str());
|
---|
316 | unalloc();
|
---|
317 | return false;
|
---|
318 | }
|
---|
319 | // Skip resourceid and header size byte
|
---|
320 | file.seek(2, SEEK_SET);
|
---|
321 | return loadPatch(&file);
|
---|
322 | }
|
---|
323 |
|
---|
324 | Common::SeekableReadStream *ResourceManager::getVolumeFile(ResourceSource *source) {
|
---|
325 | Common::List<Common::File *>::iterator it = _volumeFiles.begin();
|
---|
326 | Common::File *file;
|
---|
327 |
|
---|
328 | if (source->_resourceFile)
|
---|
329 | return source->_resourceFile->createReadStream();
|
---|
330 |
|
---|
331 | const char *filename = source->getLocationName().c_str();
|
---|
332 |
|
---|
333 | // check if file is already opened
|
---|
334 | while (it != _volumeFiles.end()) {
|
---|
335 | file = *it;
|
---|
336 | if (scumm_stricmp(file->getName(), filename) == 0) {
|
---|
337 | // move file to top
|
---|
338 | if (it != _volumeFiles.begin()) {
|
---|
339 | _volumeFiles.erase(it);
|
---|
340 | _volumeFiles.push_front(file);
|
---|
341 | }
|
---|
342 | return file;
|
---|
343 | }
|
---|
344 | ++it;
|
---|
345 | }
|
---|
346 | // adding a new file
|
---|
347 | file = new Common::File;
|
---|
348 | if (file->open(filename)) {
|
---|
349 | if (_volumeFiles.size() == MAX_OPENED_VOLUMES) {
|
---|
350 | it = --_volumeFiles.end();
|
---|
351 | delete *it;
|
---|
352 | _volumeFiles.erase(it);
|
---|
353 | }
|
---|
354 | _volumeFiles.push_front(file);
|
---|
355 | return file;
|
---|
356 | }
|
---|
357 | // failed
|
---|
358 | delete file;
|
---|
359 | return NULL;
|
---|
360 | }
|
---|
361 |
|
---|
362 | void ResourceManager::loadResource(Resource *res) {
|
---|
363 | res->_source->loadResource(this, res);
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 | void PatchResourceSource::loadResource(ResourceManager *resMan, Resource *res) {
|
---|
368 | bool result = res->loadFromPatchFile();
|
---|
369 | if (!result) {
|
---|
370 | // TODO: We used to fallback to the "default" code here if loadFromPatchFile
|
---|
371 | // failed, but I am not sure whether that is really appropriate.
|
---|
372 | // In fact it looks like a bug to me, so I commented this out for now.
|
---|
373 | //ResourceSource::loadResource(res);
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | static Common::Array<uint32> resTypeToMacTags(ResourceType type);
|
---|
378 |
|
---|
379 | void MacResourceForkResourceSource::loadResource(ResourceManager *resMan, Resource *res) {
|
---|
380 | Common::SeekableReadStream *stream = 0;
|
---|
381 | Common::Array<uint32> tagArray = resTypeToMacTags(res->getType());
|
---|
382 |
|
---|
383 | for (uint32 i = 0; i < tagArray.size() && !stream; i++)
|
---|
384 | stream = _macResMan->getResource(tagArray[i], res->getNumber());
|
---|
385 |
|
---|
386 | if (!stream)
|
---|
387 | error("Could not get Mac resource fork resource: %s %d", getResourceTypeName(res->getType()), res->getNumber());
|
---|
388 |
|
---|
389 | int error = res->decompress(resMan->getVolVersion(), stream);
|
---|
390 | if (error) {
|
---|
391 | warning("Error %d occurred while reading %s from Mac resource file: %s",
|
---|
392 | error, res->_id.toString().c_str(), sci_error_types[error]);
|
---|
393 | res->unalloc();
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | Common::SeekableReadStream *ResourceSource::getVolumeFile(ResourceManager *resMan, Resource *res) {
|
---|
398 | Common::SeekableReadStream *fileStream = resMan->getVolumeFile(this);
|
---|
399 |
|
---|
400 | if (!fileStream) {
|
---|
401 | warning("Failed to open %s", getLocationName().c_str());
|
---|
402 | if (res)
|
---|
403 | res->unalloc();
|
---|
404 | }
|
---|
405 |
|
---|
406 | return fileStream;
|
---|
407 | }
|
---|
408 |
|
---|
409 | void WaveResourceSource::loadResource(ResourceManager *resMan, Resource *res) {
|
---|
410 | Common::SeekableReadStream *fileStream = getVolumeFile(resMan, res);
|
---|
411 | if (!fileStream)
|
---|
412 | return;
|
---|
413 |
|
---|
414 | fileStream->seek(res->_fileOffset, SEEK_SET);
|
---|
415 | res->loadFromWaveFile(fileStream);
|
---|
416 | if (_resourceFile)
|
---|
417 | delete fileStream;
|
---|
418 | }
|
---|
419 |
|
---|
420 | void AudioVolumeResourceSource::loadResource(ResourceManager *resMan, Resource *res) {
|
---|
421 | Common::SeekableReadStream *fileStream = getVolumeFile(resMan, res);
|
---|
422 | if (!fileStream)
|
---|
423 | return;
|
---|
424 |
|
---|
425 | if (_audioCompressionType) {
|
---|
426 | // this file is compressed, so lookup our offset in the offset-translation table and get the new offset
|
---|
427 | // also calculate the compressed size by using the next offset
|
---|
428 | int32 *mappingTable = _audioCompressionOffsetMapping;
|
---|
429 | int32 compressedOffset = 0;
|
---|
430 |
|
---|
431 | do {
|
---|
432 | if (*mappingTable == res->_fileOffset) {
|
---|
433 | mappingTable++;
|
---|
434 | compressedOffset = *mappingTable;
|
---|
435 | // Go to next compressed offset and use that to calculate size of compressed sample
|
---|
436 | switch (res->getType()) {
|
---|
437 | case kResourceTypeSync:
|
---|
438 | case kResourceTypeSync36:
|
---|
439 | // we should already have a (valid) size
|
---|
440 | break;
|
---|
441 | default:
|
---|
442 | mappingTable += 2;
|
---|
443 | res->size = *mappingTable - compressedOffset;
|
---|
444 | }
|
---|
445 | break;
|
---|
446 | }
|
---|
447 | mappingTable += 2;
|
---|
448 | } while (*mappingTable);
|
---|
449 |
|
---|
450 | if (!compressedOffset)
|
---|
451 | error("could not translate offset to compressed offset in audio volume");
|
---|
452 | fileStream->seek(compressedOffset, SEEK_SET);
|
---|
453 |
|
---|
454 | switch (res->getType()) {
|
---|
455 | case kResourceTypeAudio:
|
---|
456 | case kResourceTypeAudio36:
|
---|
457 | // Directly read the stream, compressed audio wont have resource type id and header size for SCI1.1
|
---|
458 | res->loadFromAudioVolumeSCI1(fileStream);
|
---|
459 | if (_resourceFile)
|
---|
460 | delete fileStream;
|
---|
461 | return;
|
---|
462 | default:
|
---|
463 | break;
|
---|
464 | }
|
---|
465 | } else {
|
---|
466 | // original file, directly seek to given offset and get SCI1/SCI1.1 audio resource
|
---|
467 | fileStream->seek(res->_fileOffset, SEEK_SET);
|
---|
468 | }
|
---|
469 | if (getSciVersion() < SCI_VERSION_1_1)
|
---|
470 | res->loadFromAudioVolumeSCI1(fileStream);
|
---|
471 | else
|
---|
472 | res->loadFromAudioVolumeSCI11(fileStream);
|
---|
473 |
|
---|
474 | if (_resourceFile)
|
---|
475 | delete fileStream;
|
---|
476 | }
|
---|
477 |
|
---|
478 | void ResourceSource::loadResource(ResourceManager *resMan, Resource *res) {
|
---|
479 | Common::SeekableReadStream *fileStream = getVolumeFile(resMan, res);
|
---|
480 | if (!fileStream)
|
---|
481 | return;
|
---|
482 |
|
---|
483 | fileStream->seek(res->_fileOffset, SEEK_SET);
|
---|
484 |
|
---|
485 | int error = res->decompress(resMan->getVolVersion(), fileStream);
|
---|
486 | if (error) {
|
---|
487 | warning("Error %d occurred while reading %s from resource file %s: %s",
|
---|
488 | error, res->_id.toString().c_str(), res->getResourceLocation().c_str(),
|
---|
489 | sci_error_types[error]);
|
---|
490 | res->unalloc();
|
---|
491 | }
|
---|
492 |
|
---|
493 | if (_resourceFile)
|
---|
494 | delete fileStream;
|
---|
495 | }
|
---|
496 |
|
---|
497 | Resource *ResourceManager::testResource(ResourceId id) {
|
---|
498 | return _resMap.getVal(id, NULL);
|
---|
499 | }
|
---|
500 |
|
---|
501 | int ResourceManager::addAppropriateSources() {
|
---|
502 | Common::ArchiveMemberList files;
|
---|
503 |
|
---|
504 | if (Common::File::exists("resource.map")) {
|
---|
505 | // SCI0-SCI2 file naming scheme
|
---|
506 | ResourceSource *map = addExternalMap("resource.map");
|
---|
507 |
|
---|
508 | SearchMan.listMatchingMembers(files, "resource.0??");
|
---|
509 |
|
---|
510 | for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
|
---|
511 | const Common::String name = (*x)->getName();
|
---|
512 | const char *dot = strrchr(name.c_str(), '.');
|
---|
513 | int number = atoi(dot + 1);
|
---|
514 |
|
---|
515 | addSource(new VolumeResourceSource(name, map, number));
|
---|
516 | }
|
---|
517 | #ifdef ENABLE_SCI32
|
---|
518 | // GK1CD hires content
|
---|
519 | if (Common::File::exists("alt.map") && Common::File::exists("resource.alt"))
|
---|
520 | addSource(new VolumeResourceSource("resource.alt", addExternalMap("alt.map", 10), 10));
|
---|
521 | #endif
|
---|
522 | } else if (Common::File::exists("Data1")) {
|
---|
523 | // Mac SCI1.1+ file naming scheme
|
---|
524 | SearchMan.listMatchingMembers(files, "Data?*");
|
---|
525 |
|
---|
526 | for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
|
---|
527 | Common::String filename = (*x)->getName();
|
---|
528 | addSource(new MacResourceForkResourceSource(filename, atoi(filename.c_str() + 4)));
|
---|
529 | }
|
---|
530 | #ifdef ENABLE_SCI32
|
---|
531 | // Mac SCI32 games have extra folders for patches
|
---|
532 | addPatchDir("Robot Folder");
|
---|
533 | addPatchDir("Sound Folder");
|
---|
534 | addPatchDir("Voices Folder");
|
---|
535 | addPatchDir("Voices");
|
---|
536 | //addPatchDir("VMD Folder");
|
---|
537 |
|
---|
538 | // There can also be a "Patches" resource fork with patches
|
---|
539 | if (Common::File::exists("Patches"))
|
---|
540 | addSource(new MacResourceForkResourceSource("Patches", 100));
|
---|
541 | } else {
|
---|
542 | // SCI2.1-SCI3 file naming scheme
|
---|
543 | Common::ArchiveMemberList mapFiles;
|
---|
544 | SearchMan.listMatchingMembers(mapFiles, "resmap.0??");
|
---|
545 | SearchMan.listMatchingMembers(files, "ressci.0??");
|
---|
546 |
|
---|
547 | // We need to have the same number of maps as resource archives
|
---|
548 | if (mapFiles.empty() || files.empty() || mapFiles.size() != files.size())
|
---|
549 | return 0;
|
---|
550 |
|
---|
551 | for (Common::ArchiveMemberList::const_iterator mapIterator = mapFiles.begin(); mapIterator != mapFiles.end(); ++mapIterator) {
|
---|
552 | Common::String mapName = (*mapIterator)->getName();
|
---|
553 | int mapNumber = atoi(strrchr(mapName.c_str(), '.') + 1);
|
---|
554 |
|
---|
555 | for (Common::ArchiveMemberList::const_iterator fileIterator = files.begin(); fileIterator != files.end(); ++fileIterator) {
|
---|
556 | Common::String resName = (*fileIterator)->getName();
|
---|
557 | int resNumber = atoi(strrchr(resName.c_str(), '.') + 1);
|
---|
558 |
|
---|
559 | if (mapNumber == resNumber) {
|
---|
560 | addSource(new VolumeResourceSource(resName, addExternalMap(mapName, mapNumber), mapNumber));
|
---|
561 | break;
|
---|
562 | }
|
---|
563 | }
|
---|
564 | }
|
---|
565 |
|
---|
566 | // SCI2.1 resource patches
|
---|
567 | if (Common::File::exists("resmap.pat") && Common::File::exists("ressci.pat")) {
|
---|
568 | // We add this resource with a map which surely won't exist
|
---|
569 | addSource(new VolumeResourceSource("ressci.pat", addExternalMap("resmap.pat", 100), 100));
|
---|
570 | }
|
---|
571 | }
|
---|
572 | #else
|
---|
573 | } else
|
---|
574 | return 0;
|
---|
575 | #endif
|
---|
576 |
|
---|
577 | addPatchDir(".");
|
---|
578 |
|
---|
579 | if (Common::File::exists("message.map"))
|
---|
580 | addSource(new VolumeResourceSource("resource.msg", addExternalMap("message.map"), 0));
|
---|
581 |
|
---|
582 | if (Common::File::exists("altres.map"))
|
---|
583 | addSource(new VolumeResourceSource("altres.000", addExternalMap("altres.map"), 0));
|
---|
584 |
|
---|
585 | return 1;
|
---|
586 | }
|
---|
587 |
|
---|
588 | int ResourceManager::addAppropriateSources(const Common::FSList &fslist) {
|
---|
589 | ResourceSource *map = 0;
|
---|
590 | #ifdef ENABLE_SCI32
|
---|
591 | ResourceSource *sci21PatchMap = 0;
|
---|
592 | const Common::FSNode *sci21PatchRes = 0;
|
---|
593 | #endif
|
---|
594 |
|
---|
595 | // First, find resource.map
|
---|
596 | for (Common::FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
|
---|
597 | if (file->isDirectory())
|
---|
598 | continue;
|
---|
599 |
|
---|
600 | Common::String filename = file->getName();
|
---|
601 | filename.toLowercase();
|
---|
602 |
|
---|
603 | if (filename.contains("resource.map"))
|
---|
604 | map = addExternalMap(file);
|
---|
605 |
|
---|
606 | if (filename.contains("resmap.0")) {
|
---|
607 | const char *dot = strrchr(file->getName().c_str(), '.');
|
---|
608 | int number = atoi(dot + 1);
|
---|
609 | map = addExternalMap(file, number);
|
---|
610 | }
|
---|
611 |
|
---|
612 | #ifdef ENABLE_SCI32
|
---|
613 | // SCI2.1 resource patches
|
---|
614 | if (filename.contains("resmap.pat"))
|
---|
615 | sci21PatchMap = addExternalMap(file, 100);
|
---|
616 |
|
---|
617 | if (filename.contains("ressci.pat"))
|
---|
618 | sci21PatchRes = file;
|
---|
619 | #endif
|
---|
620 | }
|
---|
621 |
|
---|
622 | if (!map)
|
---|
623 | return 0;
|
---|
624 |
|
---|
625 | #ifdef ENABLE_SCI32
|
---|
626 | if (sci21PatchMap && sci21PatchRes)
|
---|
627 | addSource(new VolumeResourceSource(sci21PatchRes->getName(), sci21PatchMap, 100, sci21PatchRes));
|
---|
628 | #endif
|
---|
629 |
|
---|
630 | // Now find all the resource.0?? files
|
---|
631 | for (Common::FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
|
---|
632 | if (file->isDirectory())
|
---|
633 | continue;
|
---|
634 |
|
---|
635 | Common::String filename = file->getName();
|
---|
636 | filename.toLowercase();
|
---|
637 |
|
---|
638 | if (filename.contains("resource.0") || filename.contains("ressci.0")) {
|
---|
639 | const char *dot = strrchr(filename.c_str(), '.');
|
---|
640 | int number = atoi(dot + 1);
|
---|
641 |
|
---|
642 | addSource(new VolumeResourceSource(file->getName(), map, number, file));
|
---|
643 | }
|
---|
644 | }
|
---|
645 |
|
---|
646 | // This function is only called by the advanced detector, and we don't really need
|
---|
647 | // to add a patch directory or message.map here
|
---|
648 |
|
---|
649 | return 1;
|
---|
650 | }
|
---|
651 |
|
---|
652 | int ResourceManager::addInternalSources() {
|
---|
653 | Common::List<ResourceId> *resources = listResources(kResourceTypeMap);
|
---|
654 | Common::List<ResourceId>::iterator itr = resources->begin();
|
---|
655 |
|
---|
656 | while (itr != resources->end()) {
|
---|
657 | ResourceSource *src = addSource(new IntMapResourceSource("MAP", itr->getNumber()));
|
---|
658 |
|
---|
659 | if ((itr->getNumber() == 65535) && Common::File::exists("RESOURCE.SFX"))
|
---|
660 | addSource(new AudioVolumeResourceSource(this, "RESOURCE.SFX", src, 0));
|
---|
661 | else if (Common::File::exists("RESOURCE.AUD"))
|
---|
662 | addSource(new AudioVolumeResourceSource(this, "RESOURCE.AUD", src, 0));
|
---|
663 |
|
---|
664 | ++itr;
|
---|
665 | }
|
---|
666 |
|
---|
667 | delete resources;
|
---|
668 | return 1;
|
---|
669 | }
|
---|
670 |
|
---|
671 | void ResourceManager::scanNewSources() {
|
---|
672 | for (Common::List<ResourceSource *>::iterator it = _sources.begin(); it != _sources.end(); ++it) {
|
---|
673 | ResourceSource *source = *it;
|
---|
674 |
|
---|
675 | if (!source->_scanned) {
|
---|
676 | source->_scanned = true;
|
---|
677 | source->scanSource(this);
|
---|
678 | }
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | void DirectoryResourceSource::scanSource(ResourceManager *resMan) {
|
---|
683 | resMan->readResourcePatches();
|
---|
684 |
|
---|
685 | // We can't use getSciVersion() at this point, thus using _volVersion
|
---|
686 | if (resMan->_volVersion >= kResVersionSci11) // SCI1.1+
|
---|
687 | resMan->readResourcePatchesBase36();
|
---|
688 |
|
---|
689 | resMan->readWaveAudioPatches();
|
---|
690 | }
|
---|
691 |
|
---|
692 | void ExtMapResourceSource::scanSource(ResourceManager *resMan) {
|
---|
693 | if (resMan->_mapVersion < kResVersionSci1Late)
|
---|
694 | resMan->readResourceMapSCI0(this);
|
---|
695 | else
|
---|
696 | resMan->readResourceMapSCI1(this);
|
---|
697 | }
|
---|
698 |
|
---|
699 | void ExtAudioMapResourceSource::scanSource(ResourceManager *resMan) {
|
---|
700 | resMan->readAudioMapSCI1(this);
|
---|
701 | }
|
---|
702 |
|
---|
703 | void IntMapResourceSource::scanSource(ResourceManager *resMan) {
|
---|
704 | resMan->readAudioMapSCI11(this);
|
---|
705 | }
|
---|
706 |
|
---|
707 | #ifdef ENABLE_SCI32
|
---|
708 |
|
---|
709 | // Chunk resources are resources that hold other resources. They are normally called
|
---|
710 | // when using the kLoadChunk SCI2.1 kernel function. However, for example, the Lighthouse
|
---|
711 | // SCI2.1 demo has a chunk but no scripts outside of the chunk.
|
---|
712 |
|
---|
713 | // A chunk resource is pretty straightforward in terms of layout
|
---|
714 | // It begins with 11-byte entries in the header:
|
---|
715 | // =========
|
---|
716 | // b resType
|
---|
717 | // w nEntry
|
---|
718 | // dw offset
|
---|
719 | // dw length
|
---|
720 |
|
---|
721 | ChunkResourceSource::ChunkResourceSource(const Common::String &name, uint16 number)
|
---|
722 | : ResourceSource(kSourceChunk, name) {
|
---|
723 |
|
---|
724 | _number = 0;
|
---|
725 | }
|
---|
726 |
|
---|
727 | void ChunkResourceSource::scanSource(ResourceManager *resMan) {
|
---|
728 | Resource *chunk = resMan->findResource(ResourceId(kResourceTypeChunk, _number), false);
|
---|
729 |
|
---|
730 | if (!chunk)
|
---|
731 | error("Trying to load non-existent chunk");
|
---|
732 |
|
---|
733 | byte *ptr = chunk->data;
|
---|
734 | uint32 firstOffset = 0;
|
---|
735 |
|
---|
736 | for (;;) {
|
---|
737 | ResourceType type = resMan->convertResType(*ptr);
|
---|
738 | uint16 number = READ_LE_UINT16(ptr + 1);
|
---|
739 | ResourceId id(type, number);
|
---|
740 |
|
---|
741 | ResourceEntry entry;
|
---|
742 | entry.offset = READ_LE_UINT32(ptr + 3);
|
---|
743 | entry.length = READ_LE_UINT32(ptr + 7);
|
---|
744 |
|
---|
745 | _resMap[id] = entry;
|
---|
746 | ptr += 11;
|
---|
747 |
|
---|
748 | debugC(kDebugLevelResMan, 2, "Found %s in chunk %d", id.toString().c_str(), _number);
|
---|
749 |
|
---|
750 | resMan->updateResource(id, this, entry.length);
|
---|
751 |
|
---|
752 | // There's no end marker to the data table, but the first resource
|
---|
753 | // begins directly after the entry table. So, when we hit the first
|
---|
754 | // resource, we're at the end of the entry table.
|
---|
755 |
|
---|
756 | if (!firstOffset)
|
---|
757 | firstOffset = entry.offset;
|
---|
758 |
|
---|
759 | if ((size_t)(ptr - chunk->data) >= firstOffset)
|
---|
760 | break;
|
---|
761 | }
|
---|
762 | }
|
---|
763 |
|
---|
764 | void ChunkResourceSource::loadResource(ResourceManager *resMan, Resource *res) {
|
---|
765 | Resource *chunk = resMan->findResource(ResourceId(kResourceTypeChunk, _number), false);
|
---|
766 |
|
---|
767 | if (!_resMap.contains(res->_id))
|
---|
768 | error("Trying to load non-existent resource from chunk %d: %s %d", _number, getResourceTypeName(res->_id.getType()), res->_id.getNumber());
|
---|
769 |
|
---|
770 | ResourceEntry entry = _resMap[res->_id];
|
---|
771 | res->data = new byte[entry.length];
|
---|
772 | res->size = entry.length;
|
---|
773 | res->_header = 0;
|
---|
774 | res->_headerSize = 0;
|
---|
775 | res->_status = kResStatusAllocated;
|
---|
776 |
|
---|
777 | // Copy the resource data over
|
---|
778 | memcpy(res->data, chunk->data + entry.offset, entry.length);
|
---|
779 | }
|
---|
780 |
|
---|
781 | void ResourceManager::addResourcesFromChunk(uint16 id) {
|
---|
782 | addSource(new ChunkResourceSource(Common::String::printf("Chunk %d", id), id));
|
---|
783 | scanNewSources();
|
---|
784 | }
|
---|
785 |
|
---|
786 | #endif
|
---|
787 |
|
---|
788 | void ResourceManager::freeResourceSources() {
|
---|
789 | for (Common::List<ResourceSource *>::iterator it = _sources.begin(); it != _sources.end(); ++it)
|
---|
790 | delete *it;
|
---|
791 |
|
---|
792 | _sources.clear();
|
---|
793 | }
|
---|
794 |
|
---|
795 | ResourceManager::ResourceManager() {
|
---|
796 | }
|
---|
797 |
|
---|
798 | void ResourceManager::init() {
|
---|
799 | _memoryLocked = 0;
|
---|
800 | _memoryLRU = 0;
|
---|
801 | _LRU.clear();
|
---|
802 | _resMap.clear();
|
---|
803 | _audioMapSCI1 = NULL;
|
---|
804 |
|
---|
805 | // FIXME: put this in an Init() function, so that we can error out if detection fails completely
|
---|
806 |
|
---|
807 | _mapVersion = detectMapVersion();
|
---|
808 | _volVersion = detectVolVersion();
|
---|
809 | if ((_volVersion == kResVersionUnknown) && (_mapVersion != kResVersionUnknown)) {
|
---|
810 | warning("Volume version not detected, but map version has been detected. Setting volume version to map version");
|
---|
811 | _volVersion = _mapVersion;
|
---|
812 | }
|
---|
813 |
|
---|
814 | if ((_mapVersion == kResVersionUnknown) && (_volVersion != kResVersionUnknown)) {
|
---|
815 | warning("Map version not detected, but volume version has been detected. Setting map version to volume version");
|
---|
816 | _mapVersion = _volVersion;
|
---|
817 | }
|
---|
818 |
|
---|
819 | debugC(1, kDebugLevelResMan, "resMan: Detected resource map version %d: %s", _mapVersion, versionDescription(_mapVersion));
|
---|
820 | debugC(1, kDebugLevelResMan, "resMan: Detected volume version %d: %s", _volVersion, versionDescription(_volVersion));
|
---|
821 |
|
---|
822 | if ((_mapVersion == kResVersionUnknown) && (_volVersion == kResVersionUnknown)) {
|
---|
823 | warning("Volume and map version not detected, assuming that this is not a sci game");
|
---|
824 | _viewType = kViewUnknown;
|
---|
825 | return;
|
---|
826 | }
|
---|
827 |
|
---|
828 | scanNewSources();
|
---|
829 | addInternalSources();
|
---|
830 | scanNewSources();
|
---|
831 |
|
---|
832 | detectSciVersion();
|
---|
833 |
|
---|
834 | debugC(1, kDebugLevelResMan, "resMan: Detected %s", getSciVersionDesc(getSciVersion()));
|
---|
835 |
|
---|
836 | switch (_viewType) {
|
---|
837 | case kViewEga:
|
---|
838 | debugC(1, kDebugLevelResMan, "resMan: Detected EGA graphic resources");
|
---|
839 | break;
|
---|
840 | case kViewVga:
|
---|
841 | debugC(1, kDebugLevelResMan, "resMan: Detected VGA graphic resources");
|
---|
842 | break;
|
---|
843 | case kViewVga11:
|
---|
844 | debugC(1, kDebugLevelResMan, "resMan: Detected SCI1.1 VGA graphic resources");
|
---|
845 | break;
|
---|
846 | case kViewAmiga:
|
---|
847 | debugC(1, kDebugLevelResMan, "resMan: Detected Amiga graphic resources");
|
---|
848 | break;
|
---|
849 | default:
|
---|
850 | #ifdef ENABLE_SCI32
|
---|
851 | error("resMan: Couldn't determine view type");
|
---|
852 | #else
|
---|
853 | if (getSciVersion() >= SCI_VERSION_2) {
|
---|
854 | // SCI support isn't built in, thus the view type won't be determined for
|
---|
855 | // SCI2+ games. This will be handled further up, so throw no error here
|
---|
856 | } else {
|
---|
857 | error("resMan: Couldn't determine view type");
|
---|
858 | }
|
---|
859 | #endif
|
---|
860 | }
|
---|
861 |
|
---|
862 | #ifdef ENABLE_SCI32
|
---|
863 | if (getSciVersion() >= SCI_VERSION_2_1) {
|
---|
864 | // If we have no scripts, but chunk 0 is present, open up the chunk
|
---|
865 | // to try to get to any scripts in there. The Lighthouse SCI2.1 demo
|
---|
866 | // does exactly this.
|
---|
867 |
|
---|
868 | Common::List<ResourceId> *scriptList = listResources(kResourceTypeScript);
|
---|
869 |
|
---|
870 | if (scriptList->empty() && testResource(ResourceId(kResourceTypeChunk, 0)))
|
---|
871 | addResourcesFromChunk(0);
|
---|
872 |
|
---|
873 | delete scriptList;
|
---|
874 | }
|
---|
875 | #endif
|
---|
876 | }
|
---|
877 |
|
---|
878 | ResourceManager::~ResourceManager() {
|
---|
879 | // freeing resources
|
---|
880 | ResourceMap::iterator itr = _resMap.begin();
|
---|
881 | while (itr != _resMap.end()) {
|
---|
882 | delete itr->_value;
|
---|
883 | ++itr;
|
---|
884 | }
|
---|
885 | freeResourceSources();
|
---|
886 |
|
---|
887 | Common::List<Common::File *>::iterator it = _volumeFiles.begin();
|
---|
888 | while (it != _volumeFiles.end()) {
|
---|
889 | delete *it;
|
---|
890 | ++it;
|
---|
891 | }
|
---|
892 | }
|
---|
893 |
|
---|
894 | void ResourceManager::removeFromLRU(Resource *res) {
|
---|
895 | if (res->_status != kResStatusEnqueued) {
|
---|
896 | warning("resMan: trying to remove resource that isn't enqueued");
|
---|
897 | return;
|
---|
898 | }
|
---|
899 | _LRU.remove(res);
|
---|
900 | _memoryLRU -= res->size;
|
---|
901 | res->_status = kResStatusAllocated;
|
---|
902 | }
|
---|
903 |
|
---|
904 | void ResourceManager::addToLRU(Resource *res) {
|
---|
905 | if (res->_status != kResStatusAllocated) {
|
---|
906 | warning("resMan: trying to enqueue resource with state %d", res->_status);
|
---|
907 | return;
|
---|
908 | }
|
---|
909 | _LRU.push_front(res);
|
---|
910 | _memoryLRU += res->size;
|
---|
911 | #if SCI_VERBOSE_resMan
|
---|
912 | debug("Adding %s.%03d (%d bytes) to lru control: %d bytes total",
|
---|
913 | getResourceTypeName(res->type), res->number, res->size,
|
---|
914 | mgr->_memoryLRU);
|
---|
915 | #endif
|
---|
916 | res->_status = kResStatusEnqueued;
|
---|
917 | }
|
---|
918 |
|
---|
919 | void ResourceManager::printLRU() {
|
---|
920 | int mem = 0;
|
---|
921 | int entries = 0;
|
---|
922 | Common::List<Resource *>::iterator it = _LRU.begin();
|
---|
923 | Resource *res;
|
---|
924 |
|
---|
925 | while (it != _LRU.end()) {
|
---|
926 | res = *it;
|
---|
927 | debug("\t%s: %d bytes", res->_id.toString().c_str(), res->size);
|
---|
928 | mem += res->size;
|
---|
929 | ++entries;
|
---|
930 | ++it;
|
---|
931 | }
|
---|
932 |
|
---|
933 | debug("Total: %d entries, %d bytes (mgr says %d)", entries, mem, _memoryLRU);
|
---|
934 | }
|
---|
935 |
|
---|
936 | void ResourceManager::freeOldResources() {
|
---|
937 | while (MAX_MEMORY < _memoryLRU) {
|
---|
938 | assert(!_LRU.empty());
|
---|
939 | Resource *goner = *_LRU.reverse_begin();
|
---|
940 | removeFromLRU(goner);
|
---|
941 | goner->unalloc();
|
---|
942 | #ifdef SCI_VERBOSE_resMan
|
---|
943 | printf("resMan-debug: LRU: Freeing %s.%03d (%d bytes)\n", getResourceTypeName(goner->type), goner->number, goner->size);
|
---|
944 | #endif
|
---|
945 | }
|
---|
946 | }
|
---|
947 |
|
---|
948 | Common::List<ResourceId> *ResourceManager::listResources(ResourceType type, int mapNumber) {
|
---|
949 | Common::List<ResourceId> *resources = new Common::List<ResourceId>;
|
---|
950 |
|
---|
951 | ResourceMap::iterator itr = _resMap.begin();
|
---|
952 | while (itr != _resMap.end()) {
|
---|
953 | if ((itr->_value->getType() == type) && ((mapNumber == -1) || (itr->_value->getNumber() == mapNumber)))
|
---|
954 | resources->push_back(itr->_value->_id);
|
---|
955 | ++itr;
|
---|
956 | }
|
---|
957 |
|
---|
958 | return resources;
|
---|
959 | }
|
---|
960 |
|
---|
961 | Resource *ResourceManager::findResource(ResourceId id, bool lock) {
|
---|
962 | Resource *retval = testResource(id);
|
---|
963 |
|
---|
964 | if (!retval)
|
---|
965 | return NULL;
|
---|
966 |
|
---|
967 | if (retval->_status == kResStatusNoMalloc)
|
---|
968 | loadResource(retval);
|
---|
969 | else if (retval->_status == kResStatusEnqueued)
|
---|
970 | removeFromLRU(retval);
|
---|
971 | // Unless an error occurred, the resource is now either
|
---|
972 | // locked or allocated, but never queued or freed.
|
---|
973 |
|
---|
974 | freeOldResources();
|
---|
975 |
|
---|
976 | if (lock) {
|
---|
977 | if (retval->_status == kResStatusAllocated) {
|
---|
978 | retval->_status = kResStatusLocked;
|
---|
979 | retval->_lockers = 0;
|
---|
980 | _memoryLocked += retval->size;
|
---|
981 | }
|
---|
982 | retval->_lockers++;
|
---|
983 | } else if (retval->_status != kResStatusLocked) { // Don't lock it
|
---|
984 | if (retval->_status == kResStatusAllocated)
|
---|
985 | addToLRU(retval);
|
---|
986 | }
|
---|
987 |
|
---|
988 | if (retval->data)
|
---|
989 | return retval;
|
---|
990 | else {
|
---|
991 | warning("resMan: Failed to read %s", retval->_id.toString().c_str());
|
---|
992 | return NULL;
|
---|
993 | }
|
---|
994 | }
|
---|
995 |
|
---|
996 | void ResourceManager::unlockResource(Resource *res) {
|
---|
997 | assert(res);
|
---|
998 |
|
---|
999 | if (res->_status != kResStatusLocked) {
|
---|
1000 | debugC(kDebugLevelResMan, 2, "[resMan] Attempt to unlock unlocked resource %s", res->_id.toString().c_str());
|
---|
1001 | return;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | if (!--res->_lockers) { // No more lockers?
|
---|
1005 | res->_status = kResStatusAllocated;
|
---|
1006 | _memoryLocked -= res->size;
|
---|
1007 | addToLRU(res);
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | freeOldResources();
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | const char *ResourceManager::versionDescription(ResVersion version) const {
|
---|
1014 | switch (version) {
|
---|
1015 | case kResVersionUnknown:
|
---|
1016 | return "Unknown";
|
---|
1017 | case kResVersionSci0Sci1Early:
|
---|
1018 | return "SCI0 / Early SCI1";
|
---|
1019 | case kResVersionSci1Middle:
|
---|
1020 | return "Middle SCI1";
|
---|
1021 | case kResVersionSci1Late:
|
---|
1022 | return "Late SCI1";
|
---|
1023 | case kResVersionSci11:
|
---|
1024 | return "SCI1.1";
|
---|
1025 | case kResVersionSci11Mac:
|
---|
1026 | return "Mac SCI1.1+";
|
---|
1027 | case kResVersionSci32:
|
---|
1028 | return "SCI32";
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | return "Version not valid";
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | ResVersion ResourceManager::detectMapVersion() {
|
---|
1035 | Common::SeekableReadStream *fileStream = 0;
|
---|
1036 | byte buff[6];
|
---|
1037 | ResourceSource *rsrc= 0;
|
---|
1038 |
|
---|
1039 | for (Common::List<ResourceSource *>::iterator it = _sources.begin(); it != _sources.end(); ++it) {
|
---|
1040 | rsrc = *it;
|
---|
1041 |
|
---|
1042 | if (rsrc->getSourceType() == kSourceExtMap) {
|
---|
1043 | if (rsrc->_resourceFile) {
|
---|
1044 | fileStream = rsrc->_resourceFile->createReadStream();
|
---|
1045 | } else {
|
---|
1046 | Common::File *file = new Common::File();
|
---|
1047 | file->open(rsrc->getLocationName());
|
---|
1048 | if (file->isOpen())
|
---|
1049 | fileStream = file;
|
---|
1050 | }
|
---|
1051 | break;
|
---|
1052 | } else if (rsrc->getSourceType() == kSourceMacResourceFork)
|
---|
1053 | return kResVersionSci11Mac;
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | if (!fileStream)
|
---|
1057 | error("Failed to open resource map file");
|
---|
1058 |
|
---|
1059 | // detection
|
---|
1060 | // SCI0 and SCI01 maps have last 6 bytes set to FF
|
---|
1061 | fileStream->seek(-4, SEEK_END);
|
---|
1062 | uint32 uEnd = fileStream->readUint32LE();
|
---|
1063 | if (uEnd == 0xFFFFFFFF) {
|
---|
1064 | // check if 0 or 01 - try to read resources in SCI0 format and see if exists
|
---|
1065 | fileStream->seek(0, SEEK_SET);
|
---|
1066 | while (fileStream->read(buff, 6) == 6 && !(buff[0] == 0xFF && buff[1] == 0xFF && buff[2] == 0xFF)) {
|
---|
1067 | if (findVolume(rsrc, (buff[5] & 0xFC) >> 2) == NULL)
|
---|
1068 | return kResVersionSci1Middle;
|
---|
1069 | }
|
---|
1070 | return kResVersionSci0Sci1Early;
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | // SCI1 and SCI1.1 maps consist of a fixed 3-byte header, a directory list (3-bytes each) that has one entry
|
---|
1074 | // of id FFh and points to EOF. The actual entries have 6-bytes on SCI1 and 5-bytes on SCI1.1
|
---|
1075 | byte directoryType = 0;
|
---|
1076 | uint16 directoryOffset = 0;
|
---|
1077 | uint16 lastDirectoryOffset = 0;
|
---|
1078 | uint16 directorySize = 0;
|
---|
1079 | ResVersion mapDetected = kResVersionUnknown;
|
---|
1080 | fileStream->seek(0, SEEK_SET);
|
---|
1081 |
|
---|
1082 | while (!fileStream->eos()) {
|
---|
1083 | directoryType = fileStream->readByte();
|
---|
1084 | directoryOffset = fileStream->readUint16LE();
|
---|
1085 |
|
---|
1086 | // Only SCI32 has directory type < 0x80
|
---|
1087 | if (directoryType < 0x80 && (mapDetected == kResVersionUnknown || mapDetected == kResVersionSci32))
|
---|
1088 | mapDetected = kResVersionSci32;
|
---|
1089 | else if (directoryType < 0x80 || ((directoryType & 0x7f) > 0x20 && directoryType != 0xFF))
|
---|
1090 | break;
|
---|
1091 |
|
---|
1092 | // Offset is above file size? -> definitely not SCI1/SCI1.1
|
---|
1093 | if (directoryOffset > fileStream->size())
|
---|
1094 | break;
|
---|
1095 |
|
---|
1096 | if (lastDirectoryOffset && mapDetected == kResVersionUnknown) {
|
---|
1097 | directorySize = directoryOffset - lastDirectoryOffset;
|
---|
1098 | if ((directorySize % 5) && (directorySize % 6 == 0))
|
---|
1099 | mapDetected = kResVersionSci1Late;
|
---|
1100 | if ((directorySize % 5 == 0) && (directorySize % 6))
|
---|
1101 | mapDetected = kResVersionSci11;
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | if (directoryType == 0xFF) {
|
---|
1105 | // FFh entry needs to point to EOF
|
---|
1106 | if (directoryOffset != fileStream->size())
|
---|
1107 | break;
|
---|
1108 |
|
---|
1109 | delete fileStream;
|
---|
1110 |
|
---|
1111 | if (mapDetected)
|
---|
1112 | return mapDetected;
|
---|
1113 | return kResVersionSci1Late;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | lastDirectoryOffset = directoryOffset;
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | delete fileStream;
|
---|
1120 |
|
---|
1121 | return kResVersionUnknown;
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | ResVersion ResourceManager::detectVolVersion() {
|
---|
1125 | Common::SeekableReadStream *fileStream = 0;
|
---|
1126 | ResourceSource *rsrc;
|
---|
1127 |
|
---|
1128 | for (Common::List<ResourceSource *>::iterator it = _sources.begin(); it != _sources.end(); ++it) {
|
---|
1129 | rsrc = *it;
|
---|
1130 |
|
---|
1131 | if (rsrc->getSourceType() == kSourceVolume) {
|
---|
1132 | if (rsrc->_resourceFile) {
|
---|
1133 | fileStream = rsrc->_resourceFile->createReadStream();
|
---|
1134 | } else {
|
---|
1135 | Common::File *file = new Common::File();
|
---|
1136 | file->open(rsrc->getLocationName());
|
---|
1137 | if (file->isOpen())
|
---|
1138 | fileStream = file;
|
---|
1139 | }
|
---|
1140 | break;
|
---|
1141 | } else if (rsrc->getSourceType() == kSourceMacResourceFork)
|
---|
1142 | return kResVersionSci11Mac;
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | if (!fileStream) {
|
---|
1146 | error("Failed to open volume file - if you got resource.p01/resource.p02/etc. files, merge them together into resource.000");
|
---|
1147 | // resource.p01/resource.p02/etc. may be there when directly copying the files from the original floppies
|
---|
1148 | // the sierra installer would merge those together (perhaps we could do this as well?)
|
---|
1149 | // possible TODO
|
---|
1150 | // example for such game: Laura Bow 2
|
---|
1151 | return kResVersionUnknown;
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | // SCI0 volume format: {wResId wPacked+4 wUnpacked wCompression} = 8 bytes
|
---|
1155 | // SCI1 volume format: {bResType wResNumber wPacked+4 wUnpacked wCompression} = 9 bytes
|
---|
1156 | // SCI1.1 volume format: {bResType wResNumber wPacked wUnpacked wCompression} = 9 bytes
|
---|
1157 | // SCI32 volume format: {bResType wResNumber dwPacked dwUnpacked wCompression} = 13 bytes
|
---|
1158 | // Try to parse volume with SCI0 scheme to see if it make sense
|
---|
1159 | // Checking 1MB of data should be enough to determine the version
|
---|
1160 | uint16 resId, wCompression;
|
---|
1161 | uint32 dwPacked, dwUnpacked;
|
---|
1162 | ResVersion curVersion = kResVersionSci0Sci1Early;
|
---|
1163 | bool failed = false;
|
---|
1164 | bool sci11Align = false;
|
---|
1165 |
|
---|
1166 | // Check for SCI0, SCI1, SCI1.1 and SCI32 v2 (Gabriel Knight 1 CD) formats
|
---|
1167 | while (!fileStream->eos() && fileStream->pos() < 0x100000) {
|
---|
1168 | if (curVersion > kResVersionSci0Sci1Early)
|
---|
1169 | fileStream->readByte();
|
---|
1170 | resId = fileStream->readUint16LE();
|
---|
1171 | dwPacked = (curVersion < kResVersionSci32) ? fileStream->readUint16LE() : fileStream->readUint32LE();
|
---|
1172 | dwUnpacked = (curVersion < kResVersionSci32) ? fileStream->readUint16LE() : fileStream->readUint32LE();
|
---|
1173 | wCompression = fileStream->readUint16LE();
|
---|
1174 | if (fileStream->eos()) {
|
---|
1175 | delete fileStream;
|
---|
1176 | return curVersion;
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | int chk = (curVersion == kResVersionSci0Sci1Early) ? 4 : 20;
|
---|
1180 | int offs = curVersion < kResVersionSci11 ? 4 : 0;
|
---|
1181 | if ((curVersion < kResVersionSci32 && wCompression > chk)
|
---|
1182 | || (curVersion == kResVersionSci32 && wCompression != 0 && wCompression != 32)
|
---|
1183 | || (wCompression == 0 && dwPacked != dwUnpacked + offs)
|
---|
1184 | || (dwUnpacked < dwPacked - offs)) {
|
---|
1185 |
|
---|
1186 | // Retry with a newer SCI version
|
---|
1187 | if (curVersion == kResVersionSci0Sci1Early) {
|
---|
1188 | curVersion = kResVersionSci1Late;
|
---|
1189 | } else if (curVersion == kResVersionSci1Late) {
|
---|
1190 | curVersion = kResVersionSci11;
|
---|
1191 | } else if (curVersion == kResVersionSci11 && !sci11Align) {
|
---|
1192 | // Later versions (e.g. QFG1VGA) have resources word-aligned
|
---|
1193 | sci11Align = true;
|
---|
1194 | } else if (curVersion == kResVersionSci11) {
|
---|
1195 | curVersion = kResVersionSci32;
|
---|
1196 | } else {
|
---|
1197 | // All version checks failed, exit loop
|
---|
1198 | failed = true;
|
---|
1199 | break;
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | fileStream->seek(0);
|
---|
1203 | continue;
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 | if (curVersion < kResVersionSci11)
|
---|
1207 | fileStream->seek(dwPacked - 4, SEEK_CUR);
|
---|
1208 | else if (curVersion == kResVersionSci11)
|
---|
1209 | fileStream->seek(sci11Align && ((9 + dwPacked) % 2) ? dwPacked + 1 : dwPacked, SEEK_CUR);
|
---|
1210 | else if (curVersion == kResVersionSci32)
|
---|
1211 | fileStream->seek(dwPacked, SEEK_CUR);
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | delete fileStream;
|
---|
1215 |
|
---|
1216 | if (!failed)
|
---|
1217 | return curVersion;
|
---|
1218 |
|
---|
1219 | // Failed to detect volume version
|
---|
1220 | return kResVersionUnknown;
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 | // version-agnostic patch application
|
---|
1224 | void ResourceManager::processPatch(ResourceSource *source, ResourceType resourceType, uint16 resourceNr, uint32 tuple) {
|
---|
1225 | Common::SeekableReadStream *fileStream = 0;
|
---|
1226 | Resource *newrsc = 0;
|
---|
1227 | ResourceId resId = ResourceId(resourceType, resourceNr, tuple);
|
---|
1228 | ResourceType checkForType = resourceType;
|
---|
1229 |
|
---|
1230 | // base36 encoded patches (i.e. audio36 and sync36) have the same type as their non-base36 encoded counterparts
|
---|
1231 | if (checkForType == kResourceTypeAudio36)
|
---|
1232 | checkForType = kResourceTypeAudio;
|
---|
1233 | else if (checkForType == kResourceTypeSync36)
|
---|
1234 | checkForType = kResourceTypeSync;
|
---|
1235 |
|
---|
1236 | if (source->_resourceFile) {
|
---|
1237 | fileStream = source->_resourceFile->createReadStream();
|
---|
1238 | } else {
|
---|
1239 | Common::File *file = new Common::File();
|
---|
1240 | if (!file->open(source->getLocationName())) {
|
---|
1241 | warning("ResourceManager::processPatch(): failed to open %s", source->getLocationName().c_str());
|
---|
1242 | return;
|
---|
1243 | }
|
---|
1244 | fileStream = file;
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | int fsize = fileStream->size();
|
---|
1248 | if (fsize < 3) {
|
---|
1249 | debug("Patching %s failed - file too small", source->getLocationName().c_str());
|
---|
1250 | return;
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | byte patchType = convertResType(fileStream->readByte());
|
---|
1254 | byte patchDataOffset = fileStream->readByte();
|
---|
1255 |
|
---|
1256 | delete fileStream;
|
---|
1257 |
|
---|
1258 | if (patchType != checkForType) {
|
---|
1259 | debug("Patching %s failed - resource type mismatch", source->getLocationName().c_str());
|
---|
1260 | return;
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 | // Fixes SQ5/German, patch file special case logic taken from SCI View disassembly
|
---|
1264 | if (patchDataOffset & 0x80) {
|
---|
1265 | switch (patchDataOffset & 0x7F) {
|
---|
1266 | case 0:
|
---|
1267 | patchDataOffset = 24;
|
---|
1268 | break;
|
---|
1269 | case 1:
|
---|
1270 | patchDataOffset = 2;
|
---|
1271 | break;
|
---|
1272 | case 4:
|
---|
1273 | patchDataOffset = 8;
|
---|
1274 | break;
|
---|
1275 | default:
|
---|
1276 | error("Resource patch unsupported special case %X", patchDataOffset & 0x7F);
|
---|
1277 | return;
|
---|
1278 | }
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | if (patchDataOffset + 2 >= fsize) {
|
---|
1282 | debug("Patching %s failed - patch starting at offset %d can't be in file of size %d",
|
---|
1283 | source->getLocationName().c_str(), patchDataOffset + 2, fsize);
|
---|
1284 | return;
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | // Overwrite everything, because we're patching
|
---|
1288 | newrsc = updateResource(resId, source, fsize - patchDataOffset - 2);
|
---|
1289 | newrsc->_headerSize = patchDataOffset;
|
---|
1290 | newrsc->_fileOffset = 0;
|
---|
1291 |
|
---|
1292 |
|
---|
1293 | debugC(1, kDebugLevelResMan, "Patching %s - OK", source->getLocationName().c_str());
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 | void ResourceManager::readResourcePatchesBase36() {
|
---|
1297 | // The base36 encoded audio36 and sync36 resources use a different naming scheme, because they
|
---|
1298 | // cannot be described with a single resource number, but are a result of a
|
---|
1299 | // <number, noun, verb, cond, seq> tuple. Please don't be confused with the normal audio patches
|
---|
1300 | // (*.aud) and normal sync patches (*.syn). audio36 patches can be seen for example in the AUD
|
---|
1301 | // folder of GK1CD, and are like this file: @0CS0M00.0X1. GK1CD is the first game where these
|
---|
1302 | // have been observed. The actual audio36 and sync36 resources exist in SCI1.1 as well, but the
|
---|
1303 | // first game where external patch files for them have been found is GK1CD. The names of these
|
---|
1304 | // files are base36 encoded, and we handle their decoding here. audio36 files start with a '@',
|
---|
1305 | // whereas sync36 start with a '#'. Mac versions begin with 'A' (probably meaning AIFF). Torin
|
---|
1306 | // has several that begin with 'B'.
|
---|
1307 |
|
---|
1308 | Common::String name, inputName;
|
---|
1309 | Common::ArchiveMemberList files;
|
---|
1310 | ResourceSource *psrcPatch;
|
---|
1311 |
|
---|
1312 | for (int i = kResourceTypeAudio36; i <= kResourceTypeSync36; ++i) {
|
---|
1313 | files.clear();
|
---|
1314 |
|
---|
1315 | // audio36 resources start with a @, A, or B
|
---|
1316 | // sync36 resources start with a #
|
---|
1317 | if (i == kResourceTypeAudio36) {
|
---|
1318 | SearchMan.listMatchingMembers(files, "@???????.???");
|
---|
1319 | SearchMan.listMatchingMembers(files, "A???????.???");
|
---|
1320 | SearchMan.listMatchingMembers(files, "B???????.???");
|
---|
1321 | } else
|
---|
1322 | SearchMan.listMatchingMembers(files, "#???????.???");
|
---|
1323 |
|
---|
1324 | for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
|
---|
1325 | name = (*x)->getName();
|
---|
1326 |
|
---|
1327 | inputName = (*x)->getName();
|
---|
1328 | inputName.toUppercase();
|
---|
1329 | inputName.deleteChar(0); // delete the first character (type)
|
---|
1330 | inputName.deleteChar(7); // delete the dot
|
---|
1331 |
|
---|
1332 | // The base36 encoded resource contains the following:
|
---|
1333 | // uint16 resourceId, byte noun, byte verb, byte cond, byte seq
|
---|
1334 | uint16 resourceNr = strtol(Common::String(inputName.c_str(), 3).c_str(), 0, 36); // 3 characters
|
---|
1335 | uint16 noun = strtol(Common::String(inputName.c_str() + 3, 2).c_str(), 0, 36); // 2 characters
|
---|
1336 | uint16 verb = strtol(Common::String(inputName.c_str() + 5, 2).c_str(), 0, 36); // 2 characters
|
---|
1337 | uint16 cond = strtol(Common::String(inputName.c_str() + 7, 2).c_str(), 0, 36); // 2 characters
|
---|
1338 | uint16 seq = strtol(Common::String(inputName.c_str() + 9, 1).c_str(), 0, 36); // 1 character
|
---|
1339 |
|
---|
1340 | // Check, if we got valid results
|
---|
1341 | if ((noun <= 255) && (verb <= 255) && (cond <= 255) && (seq <= 255)) {
|
---|
1342 | ResourceId resource36((ResourceType)i, resourceNr, noun, verb, cond, seq);
|
---|
1343 |
|
---|
1344 | /*
|
---|
1345 | if (i == kResourceTypeAudio36)
|
---|
1346 | debug("audio36 patch: %s => %s. tuple:%d, %s\n", name.c_str(), inputName.c_str(), resource36.tuple, resource36.toString().c_str());
|
---|
1347 | else
|
---|
1348 | debug("sync36 patch: %s => %s. tuple:%d, %s\n", name.c_str(), inputName.c_str(), resource36.tuple, resource36.toString().c_str());
|
---|
1349 | */
|
---|
1350 |
|
---|
1351 | // Make sure that the audio patch is a valid resource
|
---|
1352 | if (i == kResourceTypeAudio36) {
|
---|
1353 | Common::SeekableReadStream *stream = SearchMan.createReadStreamForMember(name);
|
---|
1354 | uint32 tag = stream->readUint32BE();
|
---|
1355 |
|
---|
1356 | if (tag == MKID_BE('RIFF') || tag == MKID_BE('FORM')) {
|
---|
1357 | delete stream;
|
---|
1358 | processWavePatch(resource36, name);
|
---|
1359 | continue;
|
---|
1360 | }
|
---|
1361 |
|
---|
1362 | // Check for SOL as well
|
---|
1363 | tag = (tag << 16) | stream->readUint16BE();
|
---|
1364 |
|
---|
1365 | if (tag != MKID_BE('SOL\0')) {
|
---|
1366 | delete stream;
|
---|
1367 | continue;
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | delete stream;
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | psrcPatch = new PatchResourceSource(name);
|
---|
1374 | processPatch(psrcPatch, (ResourceType)i, resourceNr, resource36.getTuple());
|
---|
1375 | }
|
---|
1376 | }
|
---|
1377 | }
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | void ResourceManager::readResourcePatches() {
|
---|
1381 | // Note: since some SCI1 games(KQ5 floppy, SQ4) might use SCI0 naming scheme for patch files
|
---|
1382 | // this function tries to read patch file with any supported naming scheme,
|
---|
1383 | // regardless of s_sciVersion value
|
---|
1384 |
|
---|
1385 | Common::String mask, name;
|
---|
1386 | Common::ArchiveMemberList files;
|
---|
1387 | uint16 resourceNr = 0;
|
---|
1388 | const char *szResType;
|
---|
1389 | ResourceSource *psrcPatch;
|
---|
1390 |
|
---|
1391 | for (int i = kResourceTypeView; i < kResourceTypeInvalid; ++i) {
|
---|
1392 | // Ignore the types that can't be patched (and Robot/VMD is handled externally for now)
|
---|
1393 | if (!s_resourceTypeSuffixes[i] || i == kResourceTypeRobot || i == kResourceTypeVMD)
|
---|
1394 | continue;
|
---|
1395 |
|
---|
1396 | files.clear();
|
---|
1397 | szResType = getResourceTypeName((ResourceType)i);
|
---|
1398 | // SCI0 naming - type.nnn
|
---|
1399 | mask = szResType;
|
---|
1400 | mask += ".???";
|
---|
1401 | SearchMan.listMatchingMembers(files, mask);
|
---|
1402 | // SCI1 and later naming - nnn.typ
|
---|
1403 | mask = "*.";
|
---|
1404 | mask += s_resourceTypeSuffixes[i];
|
---|
1405 | SearchMan.listMatchingMembers(files, mask);
|
---|
1406 |
|
---|
1407 | for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
|
---|
1408 | bool bAdd = false;
|
---|
1409 | name = (*x)->getName();
|
---|
1410 |
|
---|
1411 | // SCI1 scheme
|
---|
1412 | if (isdigit(name[0])) {
|
---|
1413 | char *end = 0;
|
---|
1414 | resourceNr = strtol(name.c_str(), &end, 10);
|
---|
1415 | bAdd = (*end == '.'); // Ensure the next character is the period
|
---|
1416 | } else {
|
---|
1417 | // SCI0 scheme
|
---|
1418 | int resname_len = strlen(szResType);
|
---|
1419 | if (scumm_strnicmp(name.c_str(), szResType, resname_len) == 0
|
---|
1420 | && !isalpha(name[resname_len + 1])) {
|
---|
1421 | resourceNr = atoi(name.c_str() + resname_len + 1);
|
---|
1422 | bAdd = true;
|
---|
1423 | }
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | if (bAdd) {
|
---|
1427 | psrcPatch = new PatchResourceSource(name);
|
---|
1428 | processPatch(psrcPatch, (ResourceType)i, resourceNr);
|
---|
1429 | }
|
---|
1430 | }
|
---|
1431 | }
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | int ResourceManager::readResourceMapSCI0(ResourceSource *map) {
|
---|
1435 | Common::SeekableReadStream *fileStream = 0;
|
---|
1436 | ResourceType type;
|
---|
1437 | uint16 number, id;
|
---|
1438 | uint32 offset;
|
---|
1439 |
|
---|
1440 | if (map->_resourceFile) {
|
---|
1441 | fileStream = map->_resourceFile->createReadStream();
|
---|
1442 | if (!fileStream)
|
---|
1443 | return SCI_ERROR_RESMAP_NOT_FOUND;
|
---|
1444 | } else {
|
---|
1445 | Common::File *file = new Common::File();
|
---|
1446 | if (!file->open(map->getLocationName()))
|
---|
1447 | return SCI_ERROR_RESMAP_NOT_FOUND;
|
---|
1448 | fileStream = file;
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 | fileStream->seek(0, SEEK_SET);
|
---|
1452 |
|
---|
1453 | byte bMask = (_mapVersion == kResVersionSci1Middle) ? 0xF0 : 0xFC;
|
---|
1454 | byte bShift = (_mapVersion == kResVersionSci1Middle) ? 28 : 26;
|
---|
1455 |
|
---|
1456 | do {
|
---|
1457 | id = fileStream->readUint16LE();
|
---|
1458 | offset = fileStream->readUint32LE();
|
---|
1459 |
|
---|
1460 | if (fileStream->eos() || fileStream->err()) {
|
---|
1461 | delete fileStream;
|
---|
1462 | warning("Error while reading %s", map->getLocationName().c_str());
|
---|
1463 | return SCI_ERROR_RESMAP_NOT_FOUND;
|
---|
1464 | }
|
---|
1465 | if (offset == 0xFFFFFFFF)
|
---|
1466 | break;
|
---|
1467 |
|
---|
1468 | type = convertResType(id >> 11);
|
---|
1469 | number = id & 0x7FF;
|
---|
1470 | ResourceId resId = ResourceId(type, number);
|
---|
1471 | // adding a new resource
|
---|
1472 | if (_resMap.contains(resId) == false) {
|
---|
1473 | ResourceSource *source = findVolume(map, offset >> bShift);
|
---|
1474 | if (!source) {
|
---|
1475 | warning("Could not get volume for resource %d, VolumeID %d", id, offset >> bShift);
|
---|
1476 | if (_mapVersion != _volVersion) {
|
---|
1477 | warning("Retrying with the detected volume version instead");
|
---|
1478 | warning("Map version was: %d, retrying with: %d", _mapVersion, _volVersion);
|
---|
1479 | _mapVersion = _volVersion;
|
---|
1480 | bMask = (_mapVersion == kResVersionSci1Middle) ? 0xF0 : 0xFC;
|
---|
1481 | bShift = (_mapVersion == kResVersionSci1Middle) ? 28 : 26;
|
---|
1482 | source = findVolume(map, offset >> bShift);
|
---|
1483 | }
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | addResource(resId, source, offset & (((~bMask) << 24) | 0xFFFFFF));
|
---|
1487 | }
|
---|
1488 | } while (!fileStream->eos());
|
---|
1489 |
|
---|
1490 | delete fileStream;
|
---|
1491 | return 0;
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 | int ResourceManager::readResourceMapSCI1(ResourceSource *map) {
|
---|
1495 | Common::SeekableReadStream *fileStream = 0;
|
---|
1496 |
|
---|
1497 | if (map->_resourceFile) {
|
---|
1498 | fileStream = map->_resourceFile->createReadStream();
|
---|
1499 | if (!fileStream)
|
---|
1500 | return SCI_ERROR_RESMAP_NOT_FOUND;
|
---|
1501 | } else {
|
---|
1502 | Common::File *file = new Common::File();
|
---|
1503 | if (!file->open(map->getLocationName()))
|
---|
1504 | return SCI_ERROR_RESMAP_NOT_FOUND;
|
---|
1505 | fileStream = file;
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | resource_index_t resMap[32];
|
---|
1509 | memset(resMap, 0, sizeof(resource_index_t) * 32);
|
---|
1510 | byte type = 0, prevtype = 0;
|
---|
1511 | byte nEntrySize = _mapVersion == kResVersionSci11 ? SCI11_RESMAP_ENTRIES_SIZE : SCI1_RESMAP_ENTRIES_SIZE;
|
---|
1512 | ResourceId resId;
|
---|
1513 |
|
---|
1514 | // Read resource type and offsets to resource offsets block from .MAP file
|
---|
1515 | // The last entry has type=0xFF (0x1F) and offset equals to map file length
|
---|
1516 | do {
|
---|
1517 | type = fileStream->readByte() & 0x1F;
|
---|
1518 | resMap[type].wOffset = fileStream->readUint16LE();
|
---|
1519 | resMap[prevtype].wSize = (resMap[type].wOffset
|
---|
1520 | - resMap[prevtype].wOffset) / nEntrySize;
|
---|
1521 | prevtype = type;
|
---|
1522 | } while (type != 0x1F); // the last entry is FF
|
---|
1523 |
|
---|
1524 | // reading each type's offsets
|
---|
1525 | uint32 fileOffset = 0;
|
---|
1526 | for (type = 0; type < 32; type++) {
|
---|
1527 | if (resMap[type].wOffset == 0) // this resource does not exist in map
|
---|
1528 | continue;
|
---|
1529 | fileStream->seek(resMap[type].wOffset);
|
---|
1530 | for (int i = 0; i < resMap[type].wSize; i++) {
|
---|
1531 | uint16 number = fileStream->readUint16LE();
|
---|
1532 | int volume_nr = 0;
|
---|
1533 | if (_mapVersion == kResVersionSci11) {
|
---|
1534 | // offset stored in 3 bytes
|
---|
1535 | fileOffset = fileStream->readUint16LE();
|
---|
1536 | fileOffset |= fileStream->readByte() << 16;
|
---|
1537 | fileOffset <<= 1;
|
---|
1538 | } else {
|
---|
1539 | // offset/volume stored in 4 bytes
|
---|
1540 | fileOffset = fileStream->readUint32LE();
|
---|
1541 | if (_mapVersion < kResVersionSci11) {
|
---|
1542 | volume_nr = fileOffset >> 28; // most significant 4 bits
|
---|
1543 | fileOffset &= 0x0FFFFFFF; // least significant 28 bits
|
---|
1544 | } else {
|
---|
1545 | // in SCI32 it's a plain offset
|
---|
1546 | }
|
---|
1547 | }
|
---|
1548 | if (fileStream->eos() || fileStream->err()) {
|
---|
1549 | delete fileStream;
|
---|
1550 | warning("Error while reading %s", map->getLocationName().c_str());
|
---|
1551 | return SCI_ERROR_RESMAP_NOT_FOUND;
|
---|
1552 | }
|
---|
1553 | resId = ResourceId(convertResType(type), number);
|
---|
1554 | // NOTE: We add the map's volume number here to the specified volume number
|
---|
1555 | // for SCI2.1 and SCI3 maps that are not resmap.000. The resmap.* files' numbers
|
---|
1556 | // need to be used in concurrence with the volume specified in the map to get
|
---|
1557 | // the actual resource file.
|
---|
1558 | int mapVolumeNr = volume_nr + map->_volumeNumber;
|
---|
1559 | ResourceSource *source = findVolume(map, mapVolumeNr);
|
---|
1560 | // FIXME: this code has serious issues with multiple RESMAP.* files (like in unmodified gk2)
|
---|
1561 | // adding a resource with source == NULL would crash later on
|
---|
1562 | if (!source)
|
---|
1563 | error("Unable to find volume for map %s volumeNr %d", map->getLocationName().c_str(), mapVolumeNr);
|
---|
1564 |
|
---|
1565 | Resource *resource = _resMap.getVal(resId, NULL);
|
---|
1566 | if (!resource) {
|
---|
1567 | addResource(resId, source, fileOffset);
|
---|
1568 | } else {
|
---|
1569 | // if resource is already present, change it to new content
|
---|
1570 | // this is needed at least for pharkas/german. This version
|
---|
1571 | // contains several duplicate resources INSIDE the resource
|
---|
1572 | // data files like fonts, views, scripts, etc. And if we use
|
---|
1573 | // the first entries, half of the game will be english and
|
---|
1574 | // umlauts will also be missing :P
|
---|
1575 | resource->_source = source;
|
---|
1576 | resource->_fileOffset = fileOffset;
|
---|
1577 | resource->size = 0;
|
---|
1578 | }
|
---|
1579 | }
|
---|
1580 | }
|
---|
1581 |
|
---|
1582 | delete fileStream;
|
---|
1583 | return 0;
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 | struct {
|
---|
1587 | uint32 tag;
|
---|
1588 | ResourceType type;
|
---|
1589 | } static const macResTagMap[] = {
|
---|
1590 | { MKID_BE('V56 '), kResourceTypeView },
|
---|
1591 | { MKID_BE('P56 '), kResourceTypePic },
|
---|
1592 | { MKID_BE('SCR '), kResourceTypeScript },
|
---|
1593 | { MKID_BE('TEX '), kResourceTypeText },
|
---|
1594 | { MKID_BE('SND '), kResourceTypeSound },
|
---|
1595 | { MKID_BE('VOC '), kResourceTypeVocab },
|
---|
1596 | { MKID_BE('FON '), kResourceTypeFont },
|
---|
1597 | { MKID_BE('CURS'), kResourceTypeCursor },
|
---|
1598 | { MKID_BE('crsr'), kResourceTypeCursor },
|
---|
1599 | { MKID_BE('Pat '), kResourceTypePatch },
|
---|
1600 | { MKID_BE('PAL '), kResourceTypePalette },
|
---|
1601 | { MKID_BE('snd '), kResourceTypeAudio },
|
---|
1602 | { MKID_BE('MSG '), kResourceTypeMessage },
|
---|
1603 | { MKID_BE('HEP '), kResourceTypeHeap },
|
---|
1604 | { MKID_BE('IBIN'), kResourceTypeMacIconBarPictN },
|
---|
1605 | { MKID_BE('IBIS'), kResourceTypeMacIconBarPictS },
|
---|
1606 | { MKID_BE('PICT'), kResourceTypeMacPict },
|
---|
1607 | { MKID_BE('SYN '), kResourceTypeSync }
|
---|
1608 | };
|
---|
1609 |
|
---|
1610 | static Common::Array<uint32> resTypeToMacTags(ResourceType type) {
|
---|
1611 | Common::Array<uint32> tags;
|
---|
1612 |
|
---|
1613 | for (uint32 i = 0; i < ARRAYSIZE(macResTagMap); i++)
|
---|
1614 | if (macResTagMap[i].type == type)
|
---|
1615 | tags.push_back(macResTagMap[i].tag);
|
---|
1616 |
|
---|
1617 | return tags;
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 | void MacResourceForkResourceSource::scanSource(ResourceManager *resMan) {
|
---|
1621 | if (!_macResMan->open(getLocationName().c_str()))
|
---|
1622 | error("%s is not a valid Mac resource fork", getLocationName().c_str());
|
---|
1623 |
|
---|
1624 | Common::MacResTagArray tagArray = _macResMan->getResTagArray();
|
---|
1625 |
|
---|
1626 | for (uint32 i = 0; i < tagArray.size(); i++) {
|
---|
1627 | ResourceType type = kResourceTypeInvalid;
|
---|
1628 |
|
---|
1629 | // Map the Mac tags to our ResourceType
|
---|
1630 | for (uint32 j = 0; j < ARRAYSIZE(macResTagMap); j++)
|
---|
1631 | if (tagArray[i] == macResTagMap[j].tag) {
|
---|
1632 | type = macResTagMap[j].type;
|
---|
1633 | break;
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | if (type == kResourceTypeInvalid)
|
---|
1637 | continue;
|
---|
1638 |
|
---|
1639 | Common::MacResIDArray idArray = _macResMan->getResIDArray(tagArray[i]);
|
---|
1640 |
|
---|
1641 | for (uint32 j = 0; j < idArray.size(); j++) {
|
---|
1642 | // Get the size of the file
|
---|
1643 | Common::SeekableReadStream *stream = _macResMan->getResource(tagArray[i], idArray[j]);
|
---|
1644 |
|
---|
1645 | // Some IBIS resources have a size of 0, so we skip them
|
---|
1646 | if (!stream)
|
---|
1647 | continue;
|
---|
1648 |
|
---|
1649 | uint32 fileSize = stream->size();
|
---|
1650 | delete stream;
|
---|
1651 |
|
---|
1652 | ResourceId resId = ResourceId(type, idArray[j]);
|
---|
1653 |
|
---|
1654 | // Overwrite Resource instance. Resource forks may contain patches.
|
---|
1655 | resMan->updateResource(resId, this, fileSize);
|
---|
1656 | }
|
---|
1657 | }
|
---|
1658 | }
|
---|
1659 |
|
---|
1660 | void ResourceManager::addResource(ResourceId resId, ResourceSource *src, uint32 offset, uint32 size) {
|
---|
1661 | // Adding new resource only if it does not exist
|
---|
1662 | if (_resMap.contains(resId) == false) {
|
---|
1663 | Resource *res = new Resource(this, resId);
|
---|
1664 | _resMap.setVal(resId, res);
|
---|
1665 | res->_source = src;
|
---|
1666 | res->_fileOffset = offset;
|
---|
1667 | res->size = size;
|
---|
1668 | }
|
---|
1669 | }
|
---|
1670 |
|
---|
1671 | Resource *ResourceManager::updateResource(ResourceId resId, ResourceSource *src, uint32 size) {
|
---|
1672 | // Update a patched resource, whether it exists or not
|
---|
1673 | Resource *res = 0;
|
---|
1674 |
|
---|
1675 | if (_resMap.contains(resId)) {
|
---|
1676 | res = _resMap.getVal(resId);
|
---|
1677 | } else {
|
---|
1678 | res = new Resource(this, resId);
|
---|
1679 | _resMap.setVal(resId, res);
|
---|
1680 | }
|
---|
1681 |
|
---|
1682 | res->_status = kResStatusNoMalloc;
|
---|
1683 | res->_source = src;
|
---|
1684 | res->_headerSize = 0;
|
---|
1685 | res->size = size;
|
---|
1686 |
|
---|
1687 | return res;
|
---|
1688 | }
|
---|
1689 |
|
---|
1690 | int Resource::readResourceInfo(ResVersion volVersion, Common::SeekableReadStream *file,
|
---|
1691 | uint32 &szPacked, ResourceCompression &compression) {
|
---|
1692 | // SCI0 volume format: {wResId wPacked+4 wUnpacked wCompression} = 8 bytes
|
---|
1693 | // SCI1 volume format: {bResType wResNumber wPacked+4 wUnpacked wCompression} = 9 bytes
|
---|
1694 | // SCI1.1 volume format: {bResType wResNumber wPacked wUnpacked wCompression} = 9 bytes
|
---|
1695 | // SCI32 volume format : {bResType wResNumber dwPacked dwUnpacked wCompression} = 13 bytes
|
---|
1696 | uint16 w, number;
|
---|
1697 | uint32 wCompression, szUnpacked;
|
---|
1698 | ResourceType type;
|
---|
1699 |
|
---|
1700 | switch (volVersion) {
|
---|
1701 | case kResVersionSci0Sci1Early:
|
---|
1702 | case kResVersionSci1Middle:
|
---|
1703 | w = file->readUint16LE();
|
---|
1704 | type = _resMan->convertResType(w >> 11);
|
---|
1705 | number = w & 0x7FF;
|
---|
1706 | szPacked = file->readUint16LE() - 4;
|
---|
1707 | szUnpacked = file->readUint16LE();
|
---|
1708 | wCompression = file->readUint16LE();
|
---|
1709 | break;
|
---|
1710 | case kResVersionSci1Late:
|
---|
1711 | type = _resMan->convertResType(file->readByte());
|
---|
1712 | number = file->readUint16LE();
|
---|
1713 | szPacked = file->readUint16LE() - 4;
|
---|
1714 | szUnpacked = file->readUint16LE();
|
---|
1715 | wCompression = file->readUint16LE();
|
---|
1716 | break;
|
---|
1717 | case kResVersionSci11:
|
---|
1718 | type = _resMan->convertResType(file->readByte());
|
---|
1719 | number = file->readUint16LE();
|
---|
1720 | szPacked = file->readUint16LE();
|
---|
1721 | szUnpacked = file->readUint16LE();
|
---|
1722 | wCompression = file->readUint16LE();
|
---|
1723 | break;
|
---|
1724 | case kResVersionSci11Mac:
|
---|
1725 | // Doesn't store this data in the resource. Fortunately,
|
---|
1726 | // we already have this data.
|
---|
1727 | type = getType();
|
---|
1728 | number = getNumber();
|
---|
1729 | szPacked = file->size();
|
---|
1730 | szUnpacked = file->size();
|
---|
1731 | wCompression = 0;
|
---|
1732 | break;
|
---|
1733 | #ifdef ENABLE_SCI32
|
---|
1734 | case kResVersionSci32:
|
---|
1735 | type = _resMan->convertResType(file->readByte());
|
---|
1736 | number = file->readUint16LE();
|
---|
1737 | szPacked = file->readUint32LE();
|
---|
1738 | szUnpacked = file->readUint32LE();
|
---|
1739 | wCompression = file->readUint16LE();
|
---|
1740 | break;
|
---|
1741 | #endif
|
---|
1742 | default:
|
---|
1743 | return SCI_ERROR_INVALID_RESMAP_ENTRY;
|
---|
1744 | }
|
---|
1745 |
|
---|
1746 | // check if there were errors while reading
|
---|
1747 | if ((file->eos() || file->err()))
|
---|
1748 | return SCI_ERROR_IO_ERROR;
|
---|
1749 |
|
---|
1750 | _id = ResourceId(type, number);
|
---|
1751 | size = szUnpacked;
|
---|
1752 |
|
---|
1753 | // checking compression method
|
---|
1754 | switch (wCompression) {
|
---|
1755 | case 0:
|
---|
1756 | compression = kCompNone;
|
---|
1757 | break;
|
---|
1758 | case 1:
|
---|
1759 | compression = (getSciVersion() <= SCI_VERSION_01) ? kCompLZW : kCompHuffman;
|
---|
1760 | break;
|
---|
1761 | case 2:
|
---|
1762 | compression = (getSciVersion() <= SCI_VERSION_01) ? kCompHuffman : kCompLZW1;
|
---|
1763 | break;
|
---|
1764 | case 3:
|
---|
1765 | compression = kCompLZW1View;
|
---|
1766 | break;
|
---|
1767 | case 4:
|
---|
1768 | compression = kCompLZW1Pic;
|
---|
1769 | break;
|
---|
1770 | case 18:
|
---|
1771 | case 19:
|
---|
1772 | case 20:
|
---|
1773 | compression = kCompDCL;
|
---|
1774 | break;
|
---|
1775 | #ifdef ENABLE_SCI32
|
---|
1776 | case 32:
|
---|
1777 | compression = kCompSTACpack;
|
---|
1778 | break;
|
---|
1779 | #endif
|
---|
1780 | default:
|
---|
1781 | compression = kCompUnknown;
|
---|
1782 | }
|
---|
1783 |
|
---|
1784 | return compression == kCompUnknown ? SCI_ERROR_UNKNOWN_COMPRESSION : 0;
|
---|
1785 | }
|
---|
1786 |
|
---|
1787 | int Resource::decompress(ResVersion volVersion, Common::SeekableReadStream *file) {
|
---|
1788 | int errorNum;
|
---|
1789 | uint32 szPacked = 0;
|
---|
1790 | ResourceCompression compression = kCompUnknown;
|
---|
1791 |
|
---|
1792 | // fill resource info
|
---|
1793 | errorNum = readResourceInfo(volVersion, file, szPacked, compression);
|
---|
1794 | if (errorNum)
|
---|
1795 | return errorNum;
|
---|
1796 |
|
---|
1797 | // getting a decompressor
|
---|
1798 | Decompressor *dec = NULL;
|
---|
1799 | switch (compression) {
|
---|
1800 | case kCompNone:
|
---|
1801 | dec = new Decompressor;
|
---|
1802 | break;
|
---|
1803 | case kCompHuffman:
|
---|
1804 | dec = new DecompressorHuffman;
|
---|
1805 | break;
|
---|
1806 | case kCompLZW:
|
---|
1807 | case kCompLZW1:
|
---|
1808 | case kCompLZW1View:
|
---|
1809 | case kCompLZW1Pic:
|
---|
1810 | dec = new DecompressorLZW(compression);
|
---|
1811 | break;
|
---|
1812 | case kCompDCL:
|
---|
1813 | dec = new DecompressorDCL;
|
---|
1814 | break;
|
---|
1815 | #ifdef ENABLE_SCI32
|
---|
1816 | case kCompSTACpack:
|
---|
1817 | dec = new DecompressorLZS;
|
---|
1818 | break;
|
---|
1819 | #endif
|
---|
1820 | default:
|
---|
1821 | error("Resource %s: Compression method %d not supported", _id.toString().c_str(), compression);
|
---|
1822 | return SCI_ERROR_UNKNOWN_COMPRESSION;
|
---|
1823 | }
|
---|
1824 |
|
---|
1825 | data = new byte[size];
|
---|
1826 | _status = kResStatusAllocated;
|
---|
1827 | errorNum = data ? dec->unpack(file, data, szPacked, size) : SCI_ERROR_RESOURCE_TOO_BIG;
|
---|
1828 | if (errorNum)
|
---|
1829 | unalloc();
|
---|
1830 |
|
---|
1831 | delete dec;
|
---|
1832 | return errorNum;
|
---|
1833 | }
|
---|
1834 |
|
---|
1835 | ResourceCompression ResourceManager::getViewCompression() {
|
---|
1836 | int viewsTested = 0;
|
---|
1837 |
|
---|
1838 | // Test 10 views to see if any are compressed
|
---|
1839 | for (int i = 0; i < 1000; i++) {
|
---|
1840 | Common::SeekableReadStream *fileStream = 0;
|
---|
1841 | Resource *res = testResource(ResourceId(kResourceTypeView, i));
|
---|
1842 |
|
---|
1843 | if (!res)
|
---|
1844 | continue;
|
---|
1845 |
|
---|
1846 | if (res->_source->getSourceType() != kSourceVolume)
|
---|
1847 | continue;
|
---|
1848 |
|
---|
1849 | fileStream = getVolumeFile(res->_source);
|
---|
1850 |
|
---|
1851 | if (!fileStream)
|
---|
1852 | continue;
|
---|
1853 | fileStream->seek(res->_fileOffset, SEEK_SET);
|
---|
1854 |
|
---|
1855 | uint32 szPacked;
|
---|
1856 | ResourceCompression compression;
|
---|
1857 |
|
---|
1858 | if (res->readResourceInfo(_volVersion, fileStream, szPacked, compression)) {
|
---|
1859 | if (res->_source->_resourceFile)
|
---|
1860 | delete fileStream;
|
---|
1861 | continue;
|
---|
1862 | }
|
---|
1863 |
|
---|
1864 | if (res->_source->_resourceFile)
|
---|
1865 | delete fileStream;
|
---|
1866 |
|
---|
1867 | if (compression != kCompNone)
|
---|
1868 | return compression;
|
---|
1869 |
|
---|
1870 | if (++viewsTested == 10)
|
---|
1871 | break;
|
---|
1872 | }
|
---|
1873 |
|
---|
1874 | return kCompNone;
|
---|
1875 | }
|
---|
1876 |
|
---|
1877 | ViewType ResourceManager::detectViewType() {
|
---|
1878 | for (int i = 0; i < 1000; i++) {
|
---|
1879 | Resource *res = findResource(ResourceId(kResourceTypeView, i), 0);
|
---|
1880 |
|
---|
1881 | if (res) {
|
---|
1882 | // Skip views coming from patch files
|
---|
1883 | if (res->_source->getSourceType() == kSourcePatch)
|
---|
1884 | continue;
|
---|
1885 |
|
---|
1886 | switch (res->data[1]) {
|
---|
1887 | case 128:
|
---|
1888 | // If the 2nd byte is 128, it's a VGA game
|
---|
1889 | return kViewVga;
|
---|
1890 | case 0:
|
---|
1891 | // EGA or Amiga, try to read as Amiga view
|
---|
1892 |
|
---|
1893 | if (res->size < 10)
|
---|
1894 | return kViewUnknown;
|
---|
1895 |
|
---|
1896 | // Read offset of first loop
|
---|
1897 | uint16 offset = READ_LE_UINT16(res->data + 8);
|
---|
1898 |
|
---|
1899 | if (offset + 6U >= res->size)
|
---|
1900 | return kViewUnknown;
|
---|
1901 |
|
---|
1902 | // Read offset of first cel
|
---|
1903 | offset = READ_LE_UINT16(res->data + offset + 4);
|
---|
1904 |
|
---|
1905 | if (offset + 4U >= res->size)
|
---|
1906 | return kViewUnknown;
|
---|
1907 |
|
---|
1908 | // Check palette offset, amiga views have no palette
|
---|
1909 | if (READ_LE_UINT16(res->data + 6) != 0)
|
---|
1910 | return kViewEga;
|
---|
1911 |
|
---|
1912 | uint16 width = READ_LE_UINT16(res->data + offset);
|
---|
1913 | offset += 2;
|
---|
1914 | uint16 height = READ_LE_UINT16(res->data + offset);
|
---|
1915 | offset += 6;
|
---|
1916 |
|
---|
1917 | // To improve the heuristic, we skip very small views
|
---|
1918 | if (height < 10)
|
---|
1919 | continue;
|
---|
1920 |
|
---|
1921 | // Check that the RLE data stays within bounds
|
---|
1922 | int y;
|
---|
1923 | for (y = 0; y < height; y++) {
|
---|
1924 | int x = 0;
|
---|
1925 |
|
---|
1926 | while ((x < width) && (offset < res->size)) {
|
---|
1927 | byte op = res->data[offset++];
|
---|
1928 | x += (op & 0x07) ? op & 0x07 : op >> 3;
|
---|
1929 | }
|
---|
1930 |
|
---|
1931 | // Make sure we got exactly the right number of pixels for this row
|
---|
1932 | if (x != width)
|
---|
1933 | return kViewEga;
|
---|
1934 | }
|
---|
1935 |
|
---|
1936 | return kViewAmiga;
|
---|
1937 | }
|
---|
1938 | }
|
---|
1939 | }
|
---|
1940 |
|
---|
1941 | // this may happen if there are serious system issues (or trying to add a broken game)
|
---|
1942 | warning("resMan: Couldn't find any views");
|
---|
1943 | return kViewUnknown;
|
---|
1944 | }
|
---|
1945 |
|
---|
1946 | void ResourceManager::detectSciVersion() {
|
---|
1947 | // We use the view compression to set a preliminary s_sciVersion for the sake of getResourceInfo
|
---|
1948 | // Pretend we have a SCI0 game
|
---|
1949 | s_sciVersion = SCI_VERSION_0_EARLY;
|
---|
1950 | bool oldDecompressors = true;
|
---|
1951 |
|
---|
1952 | ResourceCompression viewCompression;
|
---|
1953 | #ifdef ENABLE_SCI32
|
---|
1954 | viewCompression = getViewCompression();
|
---|
1955 | #else
|
---|
1956 | if (_volVersion == kResVersionSci32) {
|
---|
1957 | // SCI32 support isn't built in, thus view detection will fail
|
---|
1958 | viewCompression = kCompUnknown;
|
---|
1959 | } else {
|
---|
1960 | viewCompression = getViewCompression();
|
---|
1961 | }
|
---|
1962 | #endif
|
---|
1963 |
|
---|
1964 | if (viewCompression != kCompLZW) {
|
---|
1965 | // If it's a different compression type from kCompLZW, the game is probably
|
---|
1966 | // SCI_VERSION_1_EGA or later. If the views are uncompressed, it is
|
---|
1967 | // likely not an early disk game.
|
---|
1968 | s_sciVersion = SCI_VERSION_1_EGA;
|
---|
1969 | oldDecompressors = false;
|
---|
1970 | }
|
---|
1971 |
|
---|
1972 | // Set view type
|
---|
1973 | if (viewCompression == kCompDCL
|
---|
1974 | || _volVersion == kResVersionSci11 // pq4demo
|
---|
1975 | || _volVersion == kResVersionSci11Mac
|
---|
1976 | #ifdef ENABLE_SCI32
|
---|
1977 | || viewCompression == kCompSTACpack
|
---|
1978 | || _volVersion == kResVersionSci32 // kq7
|
---|
1979 | #endif
|
---|
1980 | ) {
|
---|
1981 | // SCI1.1 VGA views
|
---|
1982 | _viewType = kViewVga11;
|
---|
1983 | } else {
|
---|
1984 | #ifdef ENABLE_SCI32
|
---|
1985 | // Otherwise we detect it from a view
|
---|
1986 | _viewType = detectViewType();
|
---|
1987 | #else
|
---|
1988 | if (_volVersion == kResVersionSci32 && viewCompression == kCompUnknown) {
|
---|
1989 | // A SCI32 game, but SCI32 support is disabled. Force the view type
|
---|
1990 | // to kViewVga11, as we can't read from the game's resource files
|
---|
1991 | _viewType = kViewVga11;
|
---|
1992 | } else {
|
---|
1993 | _viewType = detectViewType();
|
---|
1994 | }
|
---|
1995 | #endif
|
---|
1996 | }
|
---|
1997 |
|
---|
1998 | if (_volVersion == kResVersionSci11Mac) {
|
---|
1999 | // SCI32 doesn't have the resource.cfg file, so we can figure out
|
---|
2000 | // which of the games are SCI1.1.
|
---|
2001 | // TODO: Decide between SCI2 and SCI2.1
|
---|
2002 | if (Common::File::exists("resource.cfg"))
|
---|
2003 | s_sciVersion = SCI_VERSION_1_1;
|
---|
2004 | else if (Common::File::exists("Patches"))
|
---|
2005 | s_sciVersion = SCI_VERSION_2_1;
|
---|
2006 | else
|
---|
2007 | s_sciVersion = SCI_VERSION_2;
|
---|
2008 | return;
|
---|
2009 | }
|
---|
2010 |
|
---|
2011 | // Handle SCI32 versions here
|
---|
2012 | if (_volVersion == kResVersionSci32) {
|
---|
2013 | // SCI2.1/3 and SCI1 Late resource maps are the same, except that
|
---|
2014 | // SCI1 Late resource maps have the resource types or'd with
|
---|
2015 | // 0x80. We differentiate between SCI2 and SCI2.1/3 based on that.
|
---|
2016 | // TODO: Differentiate between SCI2.1 and SCI3
|
---|
2017 | if (_mapVersion == kResVersionSci1Late) {
|
---|
2018 | s_sciVersion = SCI_VERSION_2;
|
---|
2019 | return;
|
---|
2020 | } else {
|
---|
2021 | s_sciVersion = SCI_VERSION_2_1;
|
---|
2022 | return;
|
---|
2023 | }
|
---|
2024 | }
|
---|
2025 |
|
---|
2026 | // Check for transitive SCI1/SCI1.1 games, like PQ1 here
|
---|
2027 | // If the game has any heap file (here we check for heap file 0), then
|
---|
2028 | // it definitely uses a SCI1.1 kernel
|
---|
2029 | if (testResource(ResourceId(kResourceTypeHeap, 0))) {
|
---|
2030 | s_sciVersion = SCI_VERSION_1_1;
|
---|
2031 | return;
|
---|
2032 | }
|
---|
2033 |
|
---|
2034 | switch (_mapVersion) {
|
---|
2035 | case kResVersionSci0Sci1Early:
|
---|
2036 | if (_viewType == kViewVga) {
|
---|
2037 | // VGA
|
---|
2038 | s_sciVersion = SCI_VERSION_1_EARLY;
|
---|
2039 | return;
|
---|
2040 | }
|
---|
2041 |
|
---|
2042 | // EGA
|
---|
2043 | if (hasOldScriptHeader()) {
|
---|
2044 | s_sciVersion = SCI_VERSION_0_EARLY;
|
---|
2045 | return;
|
---|
2046 | }
|
---|
2047 |
|
---|
2048 | if (hasSci0Voc999()) {
|
---|
2049 | s_sciVersion = SCI_VERSION_0_LATE;
|
---|
2050 | return;
|
---|
2051 | }
|
---|
2052 |
|
---|
2053 | if (oldDecompressors) {
|
---|
2054 | // It's either SCI_VERSION_0_LATE or SCI_VERSION_01
|
---|
2055 |
|
---|
2056 | // We first check for SCI1 vocab.999
|
---|
2057 | if (testResource(ResourceId(kResourceTypeVocab, 999))) {
|
---|
2058 | s_sciVersion = SCI_VERSION_01;
|
---|
2059 | return;
|
---|
2060 | }
|
---|
2061 |
|
---|
2062 | // If vocab.999 is missing, we try vocab.900
|
---|
2063 | if (testResource(ResourceId(kResourceTypeVocab, 900))) {
|
---|
2064 | if (hasSci1Voc900()) {
|
---|
2065 | s_sciVersion = SCI_VERSION_01;
|
---|
2066 | return;
|
---|
2067 | } else {
|
---|
2068 | s_sciVersion = SCI_VERSION_0_LATE;
|
---|
2069 | return;
|
---|
2070 | }
|
---|
2071 | }
|
---|
2072 |
|
---|
2073 | error("Failed to accurately determine SCI version");
|
---|
2074 | // No parser, we assume SCI_VERSION_01.
|
---|
2075 | s_sciVersion = SCI_VERSION_01;
|
---|
2076 | return;
|
---|
2077 | }
|
---|
2078 |
|
---|
2079 | // New decompressors. It's either SCI_VERSION_1_EGA or SCI_VERSION_1_EARLY.
|
---|
2080 | if (hasSci1Voc900()) {
|
---|
2081 | s_sciVersion = SCI_VERSION_1_EGA;
|
---|
2082 | return;
|
---|
2083 | }
|
---|
2084 |
|
---|
2085 | // SCI_VERSION_1_EARLY EGA versions lack the parser vocab
|
---|
2086 | s_sciVersion = SCI_VERSION_1_EARLY;
|
---|
2087 | return;
|
---|
2088 | case kResVersionSci1Middle:
|
---|
2089 | s_sciVersion = SCI_VERSION_1_MIDDLE;
|
---|
2090 | return;
|
---|
2091 | case kResVersionSci1Late:
|
---|
2092 | if (_volVersion == kResVersionSci11) {
|
---|
2093 | s_sciVersion = SCI_VERSION_1_1;
|
---|
2094 | return;
|
---|
2095 | }
|
---|
2096 | // FIXME: this is really difficult, lsl1 spanish has map/vol sci1late
|
---|
2097 | // and the only current detection difference is movecounttype which
|
---|
2098 | // is increment here, but ignore for all the regular sci1late games
|
---|
2099 | // the problem is, we dont have access to that detection till later
|
---|
2100 | // so maybe (part of?) that detection should get moved in here
|
---|
2101 | if (g_sci && (g_sci->getGameId() == GID_LSL1) && (g_sci->getLanguage() == Common::ES_ESP)) {
|
---|
2102 | s_sciVersion = SCI_VERSION_1_MIDDLE;
|
---|
2103 | return;
|
---|
2104 | }
|
---|
2105 | s_sciVersion = SCI_VERSION_1_LATE;
|
---|
2106 | return;
|
---|
2107 | case kResVersionSci11:
|
---|
2108 | s_sciVersion = SCI_VERSION_1_1;
|
---|
2109 | return;
|
---|
2110 | default:
|
---|
2111 | s_sciVersion = SCI_VERSION_NONE;
|
---|
2112 | error("detectSciVersion(): Unable to detect the game's SCI version");
|
---|
2113 | }
|
---|
2114 | }
|
---|
2115 |
|
---|
2116 | bool ResourceManager::detectHires() {
|
---|
2117 | // SCI 1.1 and prior is never hires
|
---|
2118 | if (getSciVersion() <= SCI_VERSION_1_1)
|
---|
2119 | return false;
|
---|
2120 |
|
---|
2121 | #ifdef ENABLE_SCI32
|
---|
2122 | for (int i = 0; i < 32768; i++) {
|
---|
2123 | Resource *res = findResource(ResourceId(kResourceTypePic, i), 0);
|
---|
2124 |
|
---|
2125 | if (res) {
|
---|
2126 | if (READ_LE_UINT16(res->data) == 0x0e) {
|
---|
2127 | // SCI32 picture
|
---|
2128 | uint16 width = READ_LE_UINT16(res->data + 10);
|
---|
2129 | uint16 height = READ_LE_UINT16(res->data + 12);
|
---|
2130 | // Surely lowres (e.g. QFG4CD)
|
---|
2131 | if ((width == 320) && ((height == 190) || (height == 200)))
|
---|
2132 | return false;
|
---|
2133 | // Surely hires
|
---|
2134 | if ((width >= 600) || (height >= 400))
|
---|
2135 | return true;
|
---|
2136 | }
|
---|
2137 | }
|
---|
2138 | }
|
---|
2139 |
|
---|
2140 | // We haven't been able to find hires content
|
---|
2141 |
|
---|
2142 | return false;
|
---|
2143 | #else
|
---|
2144 | error("no sci32 support");
|
---|
2145 | #endif
|
---|
2146 | }
|
---|
2147 |
|
---|
2148 | bool ResourceManager::detectFontExtended() {
|
---|
2149 |
|
---|
2150 | Resource *res = findResource(ResourceId(kResourceTypeFont, 0), 0);
|
---|
2151 | if (res) {
|
---|
2152 | if (res->size >= 4) {
|
---|
2153 | uint16 numChars = READ_LE_UINT16(res->data + 2);
|
---|
2154 | if (numChars > 0x80)
|
---|
2155 | return true;
|
---|
2156 | }
|
---|
2157 | }
|
---|
2158 | return false;
|
---|
2159 | }
|
---|
2160 |
|
---|
2161 | // detects, if SCI1.1 game uses palette merging or copying - this is supposed to only get used on SCI1.1 games
|
---|
2162 | bool ResourceManager::detectForPaletteMergingForSci11() {
|
---|
2163 | // Load palette 999 (default palette)
|
---|
2164 | Resource *res = findResource(ResourceId(kResourceTypePalette, 999), false);
|
---|
2165 |
|
---|
2166 | if ((res) && (res->size > 30)) {
|
---|
2167 | byte *data = res->data;
|
---|
2168 | // Old palette format used in palette resource? -> it's merging
|
---|
2169 | if ((data[0] == 0 && data[1] == 1) || (data[0] == 0 && data[1] == 0 && READ_LE_UINT16(data + 29) == 0))
|
---|
2170 | return true;
|
---|
2171 | return false;
|
---|
2172 | }
|
---|
2173 | return false;
|
---|
2174 | }
|
---|
2175 |
|
---|
2176 | // is called on SCI0EARLY games to make sure that sound resources are in fact also SCI0EARLY
|
---|
2177 | bool ResourceManager::detectEarlySound() {
|
---|
2178 | Resource *res = findResource(ResourceId(kResourceTypeSound, 1), 0);
|
---|
2179 | if (res) {
|
---|
2180 | if (res->size >= 0x22) {
|
---|
2181 | if (READ_LE_UINT16(res->data + 0x1f) == 0) // channel 15 voice count + play mask is 0 in SCI0LATE
|
---|
2182 | if (res->data[0x21] == 0) // last byte right before actual data is 0 as well
|
---|
2183 | return false;
|
---|
2184 | }
|
---|
2185 | }
|
---|
2186 | return true;
|
---|
2187 | }
|
---|
2188 |
|
---|
2189 | // Functions below are based on PD code by Brian Provinciano (SCI Studio)
|
---|
2190 | bool ResourceManager::hasOldScriptHeader() {
|
---|
2191 | Resource *res = findResource(ResourceId(kResourceTypeScript, 0), 0);
|
---|
2192 |
|
---|
2193 | if (!res) {
|
---|
2194 | error("resMan: Failed to find script.000");
|
---|
2195 | return false;
|
---|
2196 | }
|
---|
2197 |
|
---|
2198 | uint offset = 2;
|
---|
2199 | const int objTypes = 17;
|
---|
2200 |
|
---|
2201 | while (offset < res->size) {
|
---|
2202 | uint16 objType = READ_LE_UINT16(res->data + offset);
|
---|
2203 |
|
---|
2204 | if (!objType) {
|
---|
2205 | offset += 2;
|
---|
2206 | // We should be at the end of the resource now
|
---|
2207 | return offset == res->size;
|
---|
2208 | }
|
---|
2209 |
|
---|
2210 | if (objType >= objTypes) {
|
---|
2211 | // Invalid objType
|
---|
2212 | return false;
|
---|
2213 | }
|
---|
2214 |
|
---|
2215 | int skip = READ_LE_UINT16(res->data + offset + 2);
|
---|
2216 |
|
---|
2217 | if (skip < 2) {
|
---|
2218 | // Invalid size
|
---|
2219 | return false;
|
---|
2220 | }
|
---|
2221 |
|
---|
2222 | offset += skip;
|
---|
2223 | }
|
---|
2224 |
|
---|
2225 | return false;
|
---|
2226 | }
|
---|
2227 |
|
---|
2228 | bool ResourceManager::hasSci0Voc999() {
|
---|
2229 | Resource *res = findResource(ResourceId(kResourceTypeVocab, 999), 0);
|
---|
2230 |
|
---|
2231 | if (!res) {
|
---|
2232 | // No vocab present, possibly a demo version
|
---|
2233 | return false;
|
---|
2234 | }
|
---|
2235 |
|
---|
2236 | if (res->size < 2)
|
---|
2237 | return false;
|
---|
2238 |
|
---|
2239 | uint16 count = READ_LE_UINT16(res->data);
|
---|
2240 |
|
---|
2241 | // Make sure there's enough room for the pointers
|
---|
2242 | if (res->size < (uint)count * 2)
|
---|
2243 | return false;
|
---|
2244 |
|
---|
2245 | // Iterate over all pointers
|
---|
2246 | for (uint i = 0; i < count; i++) {
|
---|
2247 | // Offset to string
|
---|
2248 | uint16 offset = READ_LE_UINT16(res->data + 2 + count * 2);
|
---|
2249 |
|
---|
2250 | // Look for end of string
|
---|
2251 | do {
|
---|
2252 | if (offset >= res->size) {
|
---|
2253 | // Out of bounds
|
---|
2254 | return false;
|
---|
2255 | }
|
---|
2256 | } while (res->data[offset++]);
|
---|
2257 | }
|
---|
2258 |
|
---|
2259 | return true;
|
---|
2260 | }
|
---|
2261 |
|
---|
2262 | bool ResourceManager::hasSci1Voc900() {
|
---|
2263 | Resource *res = findResource(ResourceId(kResourceTypeVocab, 900), 0);
|
---|
2264 |
|
---|
2265 | if (!res )
|
---|
2266 | return false;
|
---|
2267 |
|
---|
2268 | if (res->size < 0x1fe)
|
---|
2269 | return false;
|
---|
2270 |
|
---|
2271 | uint16 offset = 0x1fe;
|
---|
2272 |
|
---|
2273 | while (offset < res->size) {
|
---|
2274 | offset++;
|
---|
2275 | do {
|
---|
2276 | if (offset >= res->size) {
|
---|
2277 | // Out of bounds;
|
---|
2278 | return false;
|
---|
2279 | }
|
---|
2280 | } while (res->data[offset++]);
|
---|
2281 | offset += 3;
|
---|
2282 | }
|
---|
2283 |
|
---|
2284 | return offset == res->size;
|
---|
2285 | }
|
---|
2286 |
|
---|
2287 | // Same function as Script::findBlock(). Slight code
|
---|
2288 | // duplication here, but this has been done to keep the resource
|
---|
2289 | // manager independent from the rest of the engine
|
---|
2290 | static byte *findSci0ExportsBlock(byte *buffer) {
|
---|
2291 | byte *buf = buffer;
|
---|
2292 | bool oldScriptHeader = (getSciVersion() == SCI_VERSION_0_EARLY);
|
---|
2293 |
|
---|
2294 | if (oldScriptHeader)
|
---|
2295 | buf += 2;
|
---|
2296 |
|
---|
2297 | do {
|
---|
2298 | int seekerType = READ_LE_UINT16(buf);
|
---|
2299 |
|
---|
2300 | if (seekerType == 0)
|
---|
2301 | break;
|
---|
2302 | if (seekerType == 7) // exports
|
---|
2303 | return buf;
|
---|
2304 |
|
---|
2305 | int seekerSize = READ_LE_UINT16(buf + 2);
|
---|
2306 | assert(seekerSize > 0);
|
---|
2307 | buf += seekerSize;
|
---|
2308 | } while (1);
|
---|
2309 |
|
---|
2310 | return NULL;
|
---|
2311 | }
|
---|
2312 |
|
---|
2313 | reg_t ResourceManager::findGameObject(bool addSci11ScriptOffset) {
|
---|
2314 | Resource *script = findResource(ResourceId(kResourceTypeScript, 0), false);
|
---|
2315 |
|
---|
2316 | if (!script)
|
---|
2317 | return NULL_REG;
|
---|
2318 |
|
---|
2319 | byte *offsetPtr = 0;
|
---|
2320 |
|
---|
2321 | if (getSciVersion() < SCI_VERSION_1_1) {
|
---|
2322 | byte *buf = (getSciVersion() == SCI_VERSION_0_EARLY) ? script->data + 2 : script->data;
|
---|
2323 |
|
---|
2324 | // Check if the first block is the exports block (in most cases, it is)
|
---|
2325 | bool exportsIsFirst = (READ_LE_UINT16(buf + 4) == 7);
|
---|
2326 | if (exportsIsFirst) {
|
---|
2327 | offsetPtr = buf + 4 + 2;
|
---|
2328 | } else {
|
---|
2329 | offsetPtr = findSci0ExportsBlock(script->data);
|
---|
2330 | if (!offsetPtr)
|
---|
2331 | error("Unable to find exports block from script 0");
|
---|
2332 | offsetPtr += 4 + 2;
|
---|
2333 | }
|
---|
2334 | } else {
|
---|
2335 | offsetPtr = script->data + 4 + 2 + 2;
|
---|
2336 | }
|
---|
2337 |
|
---|
2338 | int16 offset = !isSci11Mac() ? READ_LE_UINT16(offsetPtr) : READ_BE_UINT16(offsetPtr);
|
---|
2339 |
|
---|
2340 | // In SCI1.1 and newer, the heap is appended at the end of the script,
|
---|
2341 | // so adjust the offset accordingly
|
---|
2342 | if (getSciVersion() >= SCI_VERSION_1_1 && addSci11ScriptOffset) {
|
---|
2343 | offset += script->size;
|
---|
2344 |
|
---|
2345 | // Ensure that the start of the heap is word-aligned - same as in Script::init()
|
---|
2346 | if (script->size & 2)
|
---|
2347 | offset++;
|
---|
2348 | }
|
---|
2349 |
|
---|
2350 | return make_reg(1, offset);
|
---|
2351 | }
|
---|
2352 |
|
---|
2353 | Common::String ResourceManager::findSierraGameId() {
|
---|
2354 | // In SCI0-SCI1, the heap is embedded in the script. In SCI1.1+, it's separated
|
---|
2355 | Resource *heap = 0;
|
---|
2356 | int nameSelector = 3;
|
---|
2357 |
|
---|
2358 | if (getSciVersion() < SCI_VERSION_1_1) {
|
---|
2359 | heap = findResource(ResourceId(kResourceTypeScript, 0), false);
|
---|
2360 | } else {
|
---|
2361 | heap = findResource(ResourceId(kResourceTypeHeap, 0), false);
|
---|
2362 | nameSelector += 5;
|
---|
2363 | }
|
---|
2364 |
|
---|
2365 | if (!heap)
|
---|
2366 | return "";
|
---|
2367 |
|
---|
2368 | int16 gameObjectOffset = findGameObject(false).offset;
|
---|
2369 |
|
---|
2370 | if (!gameObjectOffset)
|
---|
2371 | return "";
|
---|
2372 |
|
---|
2373 | // Seek to the name selector of the first export
|
---|
2374 | byte *seeker = heap->data + READ_UINT16(heap->data + gameObjectOffset + nameSelector * 2);
|
---|
2375 | Common::String sierraId;
|
---|
2376 | sierraId += (const char *)seeker;
|
---|
2377 |
|
---|
2378 | return sierraId;
|
---|
2379 | }
|
---|
2380 |
|
---|
2381 | const Common::String &Resource::getResourceLocation() const {
|
---|
2382 | return _source->getLocationName();
|
---|
2383 | }
|
---|
2384 |
|
---|
2385 | } // End of namespace Sci
|
---|