From 2abb5f19772125ec5b91619e39ffd2fd4b733641 Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Sun, 17 May 2015 14:01:35 -0400 Subject: SHERLOCK: Fix decompression of resources from library files --- engines/sherlock/resources.cpp | 32 +++++++++++++++++++------------- engines/sherlock/resources.h | 2 ++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/engines/sherlock/resources.cpp b/engines/sherlock/resources.cpp index b5f4f5f6dd..f6fbded9b1 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 d5e83a1745..6bb0682f8d 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); -- cgit v1.2.3