From 4a10bc8b141f575ceb9c4d87563290d2791e3380 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Sat, 2 Jun 2012 11:09:22 +0200 Subject: WINTERMUTE: Revamp the file-system to deliver Common::-streams directly --- engines/wintermute/Base/file/BDiskFile.cpp | 208 ++++++++--------------------- 1 file changed, 54 insertions(+), 154 deletions(-) (limited to 'engines/wintermute/Base/file/BDiskFile.cpp') diff --git a/engines/wintermute/Base/file/BDiskFile.cpp b/engines/wintermute/Base/file/BDiskFile.cpp index 8540829f09..57e9a0a0f0 100644 --- a/engines/wintermute/Base/file/BDiskFile.cpp +++ b/engines/wintermute/Base/file/BDiskFile.cpp @@ -33,198 +33,98 @@ #include "engines/wintermute/Base/file/BDiskFile.h" #include "engines/wintermute/Base/BFileManager.h" #include "common/stream.h" +#include "common/memstream.h" #include "common/file.h" +#include "common/zlib.h" namespace WinterMute { -////////////////////////////////////////////////////////////////////////// -CBDiskFile::CBDiskFile(CBGame *inGame): CBFile(inGame) { - _file = NULL; - _data = NULL; - _compressed = false; - _prefixSize = 0; -} - - -////////////////////////////////////////////////////////////////////////// -CBDiskFile::~CBDiskFile() { - Close(); +void correctSlashes(char *fileName) { + for (size_t i = 0; i < strlen(fileName); i++) { + if (fileName[i] == '\\') fileName[i] = '/'; + } } - -////////////////////////////////////////////////////////////////////////// -HRESULT CBDiskFile::Open(const Common::String &Filename) { - Close(); - +Common::SeekableReadStream *openDiskFile(const Common::String &Filename, CBFileManager *fileManager) { char FullPath[MAX_PATH]; + uint32 prefixSize = 0; + Common::SeekableReadStream *file = NULL; - for (int i = 0; i < Game->_fileManager->_singlePaths.GetSize(); i++) { - sprintf(FullPath, "%s%s", Game->_fileManager->_singlePaths[i], Filename.c_str()); + for (int i = 0; i < fileManager->_singlePaths.GetSize(); i++) { + sprintf(FullPath, "%s%s", fileManager->_singlePaths[i], Filename.c_str()); correctSlashes(FullPath); - //_file = Common::createFileStream(FullPath); Common::File *tempFile = new Common::File(); if (tempFile->open(FullPath)) { - _file = tempFile; + file = tempFile; } else { delete tempFile; } - /* if (_file != NULL) { - error("Tried to open %s, but failed", Filename.c_str()); - break; - }*/ } - + // if we didn't find it in search paths, try to open directly - if (!_file) { + if (!file) { strcpy(FullPath, Filename.c_str()); correctSlashes(FullPath); - //error("Tried to open %s, TODO: add SearchMan-support", Filename.c_str()); - //_file = Common::createFileStream(FullPath); + Common::File *tempFile = new Common::File(); if (tempFile->open(FullPath)) { - _file = tempFile; + file = tempFile; } else { delete tempFile; } } - - if (_file) { + + if (file) { uint32 magic1, magic2; - magic1 = _file->readUint32LE(); - magic2 = _file->readUint32LE(); - - if (magic1 == DCGF_MAGIC && magic2 == COMPRESSED_FILE_MAGIC) _compressed = true; - - if (_compressed) { + magic1 = file->readUint32LE(); + magic2 = file->readUint32LE(); + + bool compressed = false; + if (magic1 == DCGF_MAGIC && magic2 == COMPRESSED_FILE_MAGIC) compressed = true; + + if (compressed) { uint32 DataOffset, CompSize, UncompSize; - DataOffset = _file->readUint32LE(); - CompSize = _file->readUint32LE(); - UncompSize = _file->readUint32LE(); - + DataOffset = file->readUint32LE(); + CompSize = file->readUint32LE(); + UncompSize = file->readUint32LE(); + byte *CompBuffer = new byte[CompSize]; if (!CompBuffer) { - Game->LOG(0, "Error allocating memory for compressed file '%s'", Filename.c_str()); - Close(); - return E_FAIL; + error("Error allocating memory for compressed file '%s'", Filename.c_str()); + delete file; + return NULL; } - - _data = new byte[UncompSize]; - if (!_data) { - Game->LOG(0, "Error allocating buffer for file '%s'", Filename.c_str()); + + byte *data = new byte[UncompSize]; + if (!data) { + error("Error allocating buffer for file '%s'", Filename.c_str()); delete [] CompBuffer; - Close(); - return E_FAIL; + delete file; + return NULL; } - _file->seek(DataOffset + _prefixSize, SEEK_SET); - _file->read(CompBuffer, CompSize); - - if (uncompress(_data, (uLongf *)&UncompSize, CompBuffer, CompSize) != Z_OK) { - Game->LOG(0, "Error uncompressing file '%s'", Filename.c_str()); + file->seek(DataOffset + prefixSize, SEEK_SET); + file->read(CompBuffer, CompSize); + + if (Common::uncompress(data, (unsigned long *)&UncompSize, CompBuffer, CompSize) != true) { + error("Error uncompressing file '%s'", Filename.c_str()); delete [] CompBuffer; - Close(); - return E_FAIL; + delete file; + return NULL; } - + delete [] CompBuffer; - _size = UncompSize; - _pos = 0; - delete _file; - _file = NULL; + + return new Common::MemoryReadStream(data, UncompSize, DisposeAfterUse::YES); + delete file; + file = NULL; } else { - _pos = 0; - _file->seek(0, SEEK_END); - _size = _file->pos() - _prefixSize; - _file->seek(_prefixSize, SEEK_SET); - } - - return S_OK; - } else return E_FAIL; -} - - -////////////////////////////////////////////////////////////////////////// -HRESULT CBDiskFile::Close() { - if (_file) { - delete _file; - } - _file = NULL; - _pos = 0; - _size = 0; - - delete[] _data; - _data = NULL; - - _compressed = false; - - return S_OK; -} - - -////////////////////////////////////////////////////////////////////////// -HRESULT CBDiskFile::Read(void *buffer, uint32 size) { - if (_compressed) { - memcpy(buffer, _data + _pos, size); - _pos += size; - return S_OK; - } else { - - if (_file) { - size_t count = _file->read(buffer, size); - _pos += count; - return S_OK; - } else return E_FAIL; - } -} - - -////////////////////////////////////////////////////////////////////////// -HRESULT CBDiskFile::Seek(uint32 pos, TSeek origin) { - // TODO: Should this really need to use uint32? - if (_compressed) { - uint32 newPos = 0; - - switch (origin) { - case SEEK_TO_BEGIN: - newPos = pos; - break; - case SEEK_TO_END: - newPos = _size + pos; - break; - case SEEK_TO_CURRENT: - newPos = _pos + pos; - break; - } - - if (newPos < 0 || newPos > _size) return E_FAIL; - else _pos = newPos; - return S_OK; - } else { - if (!_file) return E_FAIL; - int ret = 1; - - switch (origin) { - case SEEK_TO_BEGIN: - ret = _file->seek(_prefixSize + pos, SEEK_SET); - break; - case SEEK_TO_END: - ret = _file->seek(pos, SEEK_END); - break; - case SEEK_TO_CURRENT: - ret = _file->seek(pos, SEEK_CUR); - break; + return file; } - if (ret == 0) { - _pos = _file->pos() - _prefixSize; - return S_OK; - } else return E_FAIL; - } -} + + return file; -////////////////////////////////////////////////////////////////////////// -void CBDiskFile::correctSlashes(char *fileName) { - for (size_t i = 0; i < strlen(fileName); i++) { - if (fileName[i] == '\\') fileName[i] = '/'; } + return NULL; } } // end of namespace WinterMute -- cgit v1.2.3