diff --git a/engines/sherlock/resources.cpp b/engines/sherlock/resources.cpp index b5f4f5f6dd6..f6fbded9b12 100644 --- a/engines/sherlock/resources.cpp +++ b/engines/sherlock/resources.cpp @@ -164,20 +164,10 @@ Common::SeekableReadStream *Resources::load(const Common::String &filename) { stream->seek(entry._offset); Common::SeekableReadStream *resStream = stream->readStream(entry._size); + decompressIfNecessary(resStream); - // Check whether the file is compressed - if (resStream->readUint32BE() == MKTAG('L', 'Z', 'V', 26)) { - resStream->seek(0); - // It's compressed, so decompress the sub-file and return it - Common::SeekableReadStream *decompressed = decompressLZ(*resStream); - delete stream; - delete resStream; - return decompressed; - } else { - resStream->seek(0); - delete stream; - return resStream; - } + delete stream; + return resStream; } } @@ -188,10 +178,25 @@ Common::SeekableReadStream *Resources::load(const Common::String &filename) { Common::SeekableReadStream *stream = f.readStream(f.size()); f.close(); + decompressIfNecessary(stream); return stream; } +/** + * Checks the passed stream, and if is compressed, deletes it and replaces it with it's uncompressed data + */ +void Resources::decompressIfNecessary(Common::SeekableReadStream *&stream) { + bool isCompressed = stream->readUint32BE() == MKTAG('L', 'Z', 'V', 26); + stream->seek(-4, SEEK_CUR); + + if (isCompressed) { + Common::SeekableReadStream *newStream = decompressLZ(*stream); + delete stream; + stream = newStream; + } +} + /** * Loads a specific resource from a given library file */ @@ -207,6 +212,7 @@ Common::SeekableReadStream *Resources::load(const Common::String &filename, cons LibraryEntry &entry = _indexes[libraryFile][filename]; libStream->seek(entry._offset); Common::SeekableReadStream *stream = libStream->readStream(entry._size); + decompressIfNecessary(stream); delete libStream; return stream; diff --git a/engines/sherlock/resources.h b/engines/sherlock/resources.h index d5e83a1745c..6bb0682f8d4 100644 --- a/engines/sherlock/resources.h +++ b/engines/sherlock/resources.h @@ -81,6 +81,8 @@ public: void addToCache(const Common::String &filename, Common::SeekableReadStream &stream); bool isInCache(const Common::String &filename) const { return _cache.isCached(filename); } + void decompressIfNecessary(Common::SeekableReadStream *&stream); + Common::SeekableReadStream *load(const Common::String &filename); Common::SeekableReadStream *load(const Common::String &filename, const Common::String &libraryFile);