diff options
author | Einar Johan Trøan Sømåen | 2012-06-02 11:09:22 +0200 |
---|---|---|
committer | Einar Johan Trøan Sømåen | 2012-06-02 13:09:58 +0200 |
commit | 4a10bc8b141f575ceb9c4d87563290d2791e3380 (patch) | |
tree | ef0e854b8b61ba8272466978332d83c2c2a1ac88 /engines/wintermute/Base/file | |
parent | afe556742110ac477115083bcd4aeec2f9482b9f (diff) | |
download | scummvm-rg350-4a10bc8b141f575ceb9c4d87563290d2791e3380.tar.gz scummvm-rg350-4a10bc8b141f575ceb9c4d87563290d2791e3380.tar.bz2 scummvm-rg350-4a10bc8b141f575ceb9c4d87563290d2791e3380.zip |
WINTERMUTE: Revamp the file-system to deliver Common::-streams directly
Diffstat (limited to 'engines/wintermute/Base/file')
-rw-r--r-- | engines/wintermute/Base/file/BDiskFile.cpp | 208 | ||||
-rw-r--r-- | engines/wintermute/Base/file/BDiskFile.h | 19 | ||||
-rw-r--r-- | engines/wintermute/Base/file/BFile.h | 1 | ||||
-rw-r--r-- | engines/wintermute/Base/file/BPkgFile.cpp | 136 | ||||
-rw-r--r-- | engines/wintermute/Base/file/BPkgFile.h | 22 | ||||
-rw-r--r-- | engines/wintermute/Base/file/BResourceFile.cpp | 103 | ||||
-rw-r--r-- | engines/wintermute/Base/file/BResourceFile.h | 50 |
7 files changed, 92 insertions, 447 deletions
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
diff --git a/engines/wintermute/Base/file/BDiskFile.h b/engines/wintermute/Base/file/BDiskFile.h index d9e7af548f..ebe1400128 100644 --- a/engines/wintermute/Base/file/BDiskFile.h +++ b/engines/wintermute/Base/file/BDiskFile.h @@ -29,30 +29,13 @@ #ifndef WINTERMUTE_BDISKFILE_H
#define WINTERMUTE_BDISKFILE_H
-
-#include "engines/wintermute/Base/file/BFile.h"
-
namespace Common {
class SeekableReadStream;
}
namespace WinterMute {
-class CBDiskFile : public CBFile {
-public:
- CBDiskFile(CBGame *inGame);
- virtual ~CBDiskFile();
- virtual HRESULT Seek(uint32 pos, TSeek origin = SEEK_TO_BEGIN);
- virtual HRESULT Read(void *buffer, uint32 size);
- virtual HRESULT Close();
- virtual HRESULT Open(const Common::String &filename);
-private:
- void correctSlashes(char *fileName);
- Common::SeekableReadStream *_file;
- byte *_data;
- bool _compressed;
- uint32 _prefixSize;
-};
+Common::SeekableReadStream *openDiskFile(const Common::String &Filename, CBFileManager *fileManager);
} // end of namespace WinterMute
diff --git a/engines/wintermute/Base/file/BFile.h b/engines/wintermute/Base/file/BFile.h index fa6fafcb25..caeac3e14b 100644 --- a/engines/wintermute/Base/file/BFile.h +++ b/engines/wintermute/Base/file/BFile.h @@ -32,6 +32,7 @@ #include "engines/wintermute/Base/BBase.h"
#include "common/str.h"
+#include "common/stream.h"
namespace Common {
class SeekableReadStream;
diff --git a/engines/wintermute/Base/file/BPkgFile.cpp b/engines/wintermute/Base/file/BPkgFile.cpp index fe04b816d2..ed3a24f313 100644 --- a/engines/wintermute/Base/file/BPkgFile.cpp +++ b/engines/wintermute/Base/file/BPkgFile.cpp @@ -39,24 +39,23 @@ namespace WinterMute {
-//////////////////////////////////////////////////////////////////////////
-CBPkgFile::CBPkgFile(CBGame *inGame): CBFile(inGame) {
- _fileEntry = NULL;
- _file = NULL;
- _compressed = false;
-
-}
-
-//////////////////////////////////////////////////////////////////////////
-CBPkgFile::~CBPkgFile() {
- Close();
-}
-
-
-//////////////////////////////////////////////////////////////////////////
-HRESULT CBPkgFile::Open(const Common::String &Filename) {
- Close();
-
+// HACK: wrapCompressedStream might set the size to 0, so we need a way to override it.
+class CBPkgFile : public Common::SeekableReadStream {
+ uint32 _size;
+ Common::SeekableReadStream *_stream;
+public:
+ CBPkgFile(Common::SeekableReadStream *stream, uint32 knownLength) : _size(knownLength), _stream(stream) {}
+ virtual ~CBPkgFile() { delete _stream; }
+ virtual uint32 read(void *dataPtr, uint32 dataSize) { return _stream->read(dataPtr, dataSize); }
+ virtual bool eos() const { return _stream->eos(); }
+ virtual int32 pos() const { return _stream->pos(); }
+ virtual int32 size() const { return _size; }
+ virtual bool seek(int32 offset, int whence = SEEK_SET) { return _stream->seek(offset, whence); }
+};
+
+Common::SeekableReadStream *openPkgFile(const Common::String &Filename, CBFileManager *fileManager) {
+ CBFileEntry *fileEntry;
+ Common::SeekableReadStream *file = NULL;
char fileName[MAX_PATH];
strcpy(fileName, Filename.c_str());
@@ -65,97 +64,30 @@ HRESULT CBPkgFile::Open(const Common::String &Filename) { if (fileName[i] == '/') fileName[i] = '\\';
}
- _fileEntry = Game->_fileManager->GetPackageEntry(fileName);
- if (!_fileEntry) return E_FAIL;
-
- _file = _fileEntry->_package->GetFilePointer();
- if (!_file) return E_FAIL;
-
+ fileEntry = fileManager->GetPackageEntry(fileName);
+ if (!fileEntry) return NULL;
+
+ file = fileEntry->_package->GetFilePointer();
+ if (!file) return NULL;
+
// TODO: Cleanup
- _compressed = (_fileEntry->_compressedLength != 0);
- _size = _fileEntry->_length;
-
- if (_compressed) {
+ bool compressed = (fileEntry->_compressedLength != 0);
+ /* _size = fileEntry->_length; */
+
+ if (compressed) {
// TODO: Really, most of this logic might be doable directly in the fileEntry?
// But for now, this should get us rolling atleast.
- _file = Common::wrapCompressedReadStream(new Common::SeekableSubReadStream(_file, _fileEntry->_offset, _fileEntry->_offset + _fileEntry->_length, DisposeAfterUse::YES));
+ file = Common::wrapCompressedReadStream(new Common::SeekableSubReadStream(file, fileEntry->_offset, fileEntry->_offset + fileEntry->_length, DisposeAfterUse::YES));
} else {
- _file = new Common::SeekableSubReadStream(_file, _fileEntry->_offset, _fileEntry->_offset + _fileEntry->_length, DisposeAfterUse::YES);
+ file = new Common::SeekableSubReadStream(file, fileEntry->_offset, fileEntry->_offset + fileEntry->_length, DisposeAfterUse::YES);
}
-
- SeekToPos(0);
-
- return S_OK;
-}
-
-
-//////////////////////////////////////////////////////////////////////////
-HRESULT CBPkgFile::Close() {
- if (_fileEntry) {
- _fileEntry->_package->CloseFilePointer(_file);
- _fileEntry = NULL;
+ if (file->size() == 0) {
+ file = new CBPkgFile(file, fileEntry->_length);
}
- _file = NULL;
-
- // TODO: Do we really need to take care of our position and size at all (or could (Safe)SubStreams fix that for us?
- _pos = 0;
- _size = 0;
-
- return S_OK;
-}
-
-
-//////////////////////////////////////////////////////////////////////////
-HRESULT CBPkgFile::Read(void *Buffer, uint32 Size) {
- if (!_fileEntry) return E_FAIL;
-
- HRESULT ret = S_OK;
-
- if (_pos + Size > _size) {
- Size = _size - _pos;
- if (Size == 0) return E_FAIL;
- }
-
- ret = _file->read(Buffer, Size);
-
- _pos += Size;
-
- return ret;
-}
-
-
-//////////////////////////////////////////////////////////////////////////
-HRESULT CBPkgFile::Seek(uint32 pos, TSeek origin) {
- if (!_fileEntry) return E_FAIL;
-
- 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;
-
- return SeekToPos(newPos);
-}
-
-
-#define STREAM_BUFFER_SIZE 4096
-//////////////////////////////////////////////////////////////////////////
-HRESULT CBPkgFile::SeekToPos(uint32 newPos) {
- HRESULT ret = S_OK;
- // seek compressed stream to NewPos
- _pos = newPos;
- return ret;
+ file->seek(0);
+
+ return file;
}
} // end of namespace WinterMute
diff --git a/engines/wintermute/Base/file/BPkgFile.h b/engines/wintermute/Base/file/BPkgFile.h index 912b351ac8..e2d90e2b50 100644 --- a/engines/wintermute/Base/file/BPkgFile.h +++ b/engines/wintermute/Base/file/BPkgFile.h @@ -29,12 +29,7 @@ #ifndef WINTERMUTE_BPKGFILE_H
#define WINTERMUTE_BPKGFILE_H
-
-#include "engines/wintermute/Base/file/BFile.h"
#include "engines/wintermute/Base/BFileEntry.h"
-#include <zlib.h> // Added by ClassView
-
-#define COMPRESSED_BUFFER_SIZE 4096
namespace Common {
class SeekableReadStream;
@@ -43,21 +38,8 @@ class File; namespace WinterMute {
-class CBPkgFile : public CBFile {
-public:
- CBPkgFile(CBGame *inGame);
- virtual ~CBPkgFile();
- virtual HRESULT Seek(uint32 pos, TSeek origin = SEEK_TO_BEGIN);
- virtual HRESULT Read(void *buffer, uint32 size);
- virtual HRESULT Close();
- virtual HRESULT Open(const Common::String &filename);
-private:
- bool _inflateInit;
- HRESULT SeekToPos(uint32 newPos);
- bool _compressed;
- CBFileEntry *_fileEntry;
- Common::SeekableReadStream *_file;
-};
+class CBFileManager;
+Common::SeekableReadStream *openPkgFile(const Common::String &Filename, CBFileManager *fileManager);
} // end of namespace WinterMute
diff --git a/engines/wintermute/Base/file/BResourceFile.cpp b/engines/wintermute/Base/file/BResourceFile.cpp deleted file mode 100644 index 9ac93c4f9b..0000000000 --- a/engines/wintermute/Base/file/BResourceFile.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* ScummVM - Graphic Adventure Engine
- *
- * ScummVM is the legal property of its developers, whose names
- * are too numerous to list here. Please refer to the COPYRIGHT
- * file distributed with this source distribution.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
-
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
-
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- */
-
-/*
- * This file is based on WME Lite.
- * http://dead-code.org/redir.php?target=wmelite
- * Copyright (c) 2011 Jan Nedoma
- */
-
-#include "engines/wintermute/dcgf.h"
-#include "engines/wintermute/Base/file/BResourceFile.h"
-#include "engines/wintermute/Base/BResources.h"
-
-namespace WinterMute {
-
-//////////////////////////////////////////////////////////////////////////
-CBResourceFile::CBResourceFile(CBGame *inGame): CBFile(inGame) {
- _data = NULL;
-}
-
-
-//////////////////////////////////////////////////////////////////////////
-CBResourceFile::~CBResourceFile() {
- Close();
-}
-
-
-//////////////////////////////////////////////////////////////////////////
-HRESULT CBResourceFile::Open(const Common::String &filename) {
- Close();
-
- if (CBResources::GetFile(filename.c_str(), _data, _size)) {
- _pos = 0;
- return S_OK;
- }
- return E_FAIL;
-}
-
-//////////////////////////////////////////////////////////////////////////
-HRESULT CBResourceFile::Close() {
- _data = NULL;
- _pos = 0;
- _size = 0;
-
- return S_OK;
-}
-
-
-//////////////////////////////////////////////////////////////////////////
-HRESULT CBResourceFile::Read(void *buffer, uint32 size) {
- if (!_data || _pos + size > _size) return E_FAIL;
-
- memcpy(buffer, (byte *)_data + _pos, size);
- _pos += size;
-
- return S_OK;
-}
-
-
-//////////////////////////////////////////////////////////////////////////
-HRESULT CBResourceFile::Seek(uint32 pos, TSeek origin) {
- if (!_data) return E_FAIL;
-
- int32 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;
-}
-
-} // end of namespace WinterMute
diff --git a/engines/wintermute/Base/file/BResourceFile.h b/engines/wintermute/Base/file/BResourceFile.h deleted file mode 100644 index 77d8b629b1..0000000000 --- a/engines/wintermute/Base/file/BResourceFile.h +++ /dev/null @@ -1,50 +0,0 @@ -/* ScummVM - Graphic Adventure Engine
- *
- * ScummVM is the legal property of its developers, whose names
- * are too numerous to list here. Please refer to the COPYRIGHT
- * file distributed with this source distribution.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
-
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
-
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- */
-
-/*
- * This file is based on WME Lite.
- * http://dead-code.org/redir.php?target=wmelite
- * Copyright (c) 2011 Jan Nedoma
- */
-
-#ifndef WINTERMUTE_BRESOURCEFILE_H
-#define WINTERMUTE_BRESOURCEFILE_H
-
-#include "engines/wintermute/Base/file/BFile.h"
-
-namespace WinterMute {
-
-class CBResourceFile : public CBFile {
-public:
- CBResourceFile(CBGame *inGame);
- virtual ~CBResourceFile();
- virtual HRESULT Seek(uint32 Pos, TSeek Origin = SEEK_TO_BEGIN);
- virtual HRESULT Read(void *Buffer, uint32 Size);
- virtual HRESULT Close();
- virtual HRESULT Open(const Common::String &Filename);
-private:
- byte *_data;
-};
-
-} // end of namespace WinterMute
-
-#endif
|