From 3016f3a7256b20fa714a11fe6228d292e7add828 Mon Sep 17 00:00:00 2001 From: RichardTea <31507749+RichardTea@users.noreply.github.com> Date: Thu, 25 Jul 2019 10:13:53 +0100 Subject: [PATCH] Convert Q3BSP Importer to use ZipArchiveIOSystem Removes duplication, handles large files better --- code/CMakeLists.txt | 2 - code/Q3BSP/Q3BSPFileImporter.cpp | 16 +- code/Q3BSP/Q3BSPFileImporter.h | 12 +- code/Q3BSP/Q3BSPFileParser.cpp | 5 +- code/Q3BSP/Q3BSPFileParser.h | 13 +- code/Q3BSP/Q3BSPZipArchive.cpp | 325 ------------------------------- code/Q3BSP/Q3BSPZipArchive.h | 135 ------------- 7 files changed, 23 insertions(+), 485 deletions(-) delete mode 100644 code/Q3BSP/Q3BSPZipArchive.cpp delete mode 100644 code/Q3BSP/Q3BSPZipArchive.h diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt index 5c1197364..110b211f8 100644 --- a/code/CMakeLists.txt +++ b/code/CMakeLists.txt @@ -690,8 +690,6 @@ ADD_ASSIMP_IMPORTER( Q3BSP Q3BSP/Q3BSPFileParser.cpp Q3BSP/Q3BSPFileImporter.h Q3BSP/Q3BSPFileImporter.cpp - Q3BSP/Q3BSPZipArchive.h - Q3BSP/Q3BSPZipArchive.cpp ) ADD_ASSIMP_IMPORTER( RAW diff --git a/code/Q3BSP/Q3BSPFileImporter.cpp b/code/Q3BSP/Q3BSPFileImporter.cpp index 6a3b95bb8..4add00a07 100644 --- a/code/Q3BSP/Q3BSPFileImporter.cpp +++ b/code/Q3BSP/Q3BSPFileImporter.cpp @@ -43,7 +43,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef ASSIMP_BUILD_NO_Q3BSP_IMPORTER #include "Q3BSPFileImporter.h" -#include "Q3BSPZipArchive.h" #include "Q3BSPFileParser.h" #include "Q3BSPFileData.h" @@ -60,6 +59,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include #include #include @@ -181,7 +181,7 @@ const aiImporterDesc* Q3BSPFileImporter::GetInfo () const { // ------------------------------------------------------------------------------------------------ // Import method. void Q3BSPFileImporter::InternReadFile(const std::string &rFile, aiScene* scene, IOSystem* ioHandler) { - Q3BSPZipArchive Archive( ioHandler, rFile ); + ZipArchiveIOSystem Archive( ioHandler, rFile ); if ( !Archive.isOpen() ) { throw DeadlyImportError( "Failed to open file " + rFile + "." ); } @@ -223,10 +223,10 @@ void Q3BSPFileImporter::separateMapName( const std::string &importName, std::str // ------------------------------------------------------------------------------------------------ // Returns the first map in the map archive. -bool Q3BSPFileImporter::findFirstMapInArchive( Q3BSPZipArchive &bspArchive, std::string &mapName ) { +bool Q3BSPFileImporter::findFirstMapInArchive(ZipArchiveIOSystem &bspArchive, std::string &mapName ) { mapName = ""; std::vector fileList; - bspArchive.getFileList( fileList ); + bspArchive.getFileListExtension( fileList, "bsp" ); if (fileList.empty()) { return false; } @@ -249,7 +249,7 @@ bool Q3BSPFileImporter::findFirstMapInArchive( Q3BSPZipArchive &bspArchive, std: // ------------------------------------------------------------------------------------------------ // Creates the assimp specific data. void Q3BSPFileImporter::CreateDataFromImport( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, - Q3BSPZipArchive *pArchive ) { + ZipArchiveIOSystem *pArchive ) { if (nullptr == pModel || nullptr == pScene) { return; } @@ -418,7 +418,7 @@ void Q3BSPFileImporter::createTriangleTopology( const Q3BSP::Q3BSPModel *pModel, // ------------------------------------------------------------------------------------------------ // Creates all referenced materials. void Q3BSPFileImporter::createMaterials( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, - Q3BSPZipArchive *pArchive ) { + ZipArchiveIOSystem *pArchive ) { if ( m_MaterialLookupMap.empty() ) { return; } @@ -564,7 +564,7 @@ aiFace *Q3BSPFileImporter::getNextFace( aiMesh *mesh, unsigned int &faceIdx ) { // ------------------------------------------------------------------------------------------------ // Imports a texture file. bool Q3BSPFileImporter::importTextureFromArchive( const Q3BSP::Q3BSPModel *model, - Q3BSP::Q3BSPZipArchive *archive, aiScene*, + ZipArchiveIOSystem *archive, aiScene*, aiMaterial *pMatHelper, int textureId ) { if (nullptr == archive || nullptr == pMatHelper ) { return false; @@ -669,7 +669,7 @@ bool Q3BSPFileImporter::importLightmap( const Q3BSP::Q3BSPModel *pModel, aiScene // ------------------------------------------------------------------------------------------------ // Will search for a supported extension. -bool Q3BSPFileImporter::expandFile( Q3BSP::Q3BSPZipArchive *pArchive, const std::string &rFilename, +bool Q3BSPFileImporter::expandFile(ZipArchiveIOSystem *pArchive, const std::string &rFilename, const std::vector &rExtList, std::string &rFile, std::string &rExt ) { diff --git a/code/Q3BSP/Q3BSPFileImporter.h b/code/Q3BSP/Q3BSPFileImporter.h index 5af9fb7bc..ee21fa48e 100644 --- a/code/Q3BSP/Q3BSPFileImporter.h +++ b/code/Q3BSP/Q3BSPFileImporter.h @@ -54,9 +54,9 @@ struct aiMaterial; struct aiTexture; namespace Assimp { + class ZipArchiveIOSystem; namespace Q3BSP { - class Q3BSPZipArchive; struct Q3BSPModel; struct sQ3BSPFace; } @@ -85,24 +85,24 @@ protected: const aiImporterDesc* GetInfo () const; void InternReadFile(const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler); void separateMapName( const std::string &rImportName, std::string &rArchiveName, std::string &rMapName ); - bool findFirstMapInArchive( Q3BSP::Q3BSPZipArchive &rArchive, std::string &rMapName ); - void CreateDataFromImport( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, Q3BSP::Q3BSPZipArchive *pArchive ); + bool findFirstMapInArchive(ZipArchiveIOSystem &rArchive, std::string &rMapName ); + void CreateDataFromImport( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, ZipArchiveIOSystem *pArchive ); void CreateNodes( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, aiNode *pParent ); aiNode *CreateTopology( const Q3BSP::Q3BSPModel *pModel, unsigned int materialIdx, std::vector &rArray, aiMesh **pMesh ); void createTriangleTopology( const Q3BSP::Q3BSPModel *pModel, Q3BSP::sQ3BSPFace *pQ3BSPFace, aiMesh* pMesh, unsigned int &rFaceIdx, unsigned int &rVertIdx ); - void createMaterials( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, Q3BSP::Q3BSPZipArchive *pArchive ); + void createMaterials( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, ZipArchiveIOSystem *pArchive ); size_t countData( const std::vector &rArray ) const; size_t countFaces( const std::vector &rArray ) const; size_t countTriangles( const std::vector &rArray ) const; void createMaterialMap( const Q3BSP::Q3BSPModel *pModel); aiFace *getNextFace( aiMesh *pMesh, unsigned int &rFaceIdx ); - bool importTextureFromArchive( const Q3BSP::Q3BSPModel *pModel, Q3BSP::Q3BSPZipArchive *pArchive, aiScene* pScene, + bool importTextureFromArchive( const Q3BSP::Q3BSPModel *pModel, ZipArchiveIOSystem *pArchive, aiScene* pScene, aiMaterial *pMatHelper, int textureId ); bool importLightmap( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, aiMaterial *pMatHelper, int lightmapId ); bool importEntities( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene ); - bool expandFile( Q3BSP::Q3BSPZipArchive *pArchive, const std::string &rFilename, const std::vector &rExtList, + bool expandFile(ZipArchiveIOSystem *pArchive, const std::string &rFilename, const std::vector &rExtList, std::string &rFile, std::string &rExt ); private: diff --git a/code/Q3BSP/Q3BSPFileParser.cpp b/code/Q3BSP/Q3BSPFileParser.cpp index 5bac5ae76..bed2efe53 100644 --- a/code/Q3BSP/Q3BSPFileParser.cpp +++ b/code/Q3BSP/Q3BSPFileParser.cpp @@ -45,9 +45,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "Q3BSPFileParser.h" #include "Q3BSPFileData.h" -#include "Q3BSPZipArchive.h" #include #include +#include #include namespace Assimp { @@ -55,7 +55,7 @@ namespace Assimp { using namespace Q3BSP; // ------------------------------------------------------------------------------------------------ -Q3BSPFileParser::Q3BSPFileParser( const std::string &mapName, Q3BSPZipArchive *pZipArchive ) : +Q3BSPFileParser::Q3BSPFileParser( const std::string &mapName, ZipArchiveIOSystem *pZipArchive ) : m_sOffset( 0 ), m_Data(), m_pModel(nullptr), @@ -101,6 +101,7 @@ bool Q3BSPFileParser::readData( const std::string &rMapName ) { const size_t readSize = pMapFile->Read( &m_Data[0], sizeof( char ), size ); if ( readSize != size ) { m_Data.clear(); + m_pZipArchive->Close(pMapFile); return false; } m_pZipArchive->Close( pMapFile ); diff --git a/code/Q3BSP/Q3BSPFileParser.h b/code/Q3BSP/Q3BSPFileParser.h index 143e42b15..fd73f5e10 100644 --- a/code/Q3BSP/Q3BSPFileParser.h +++ b/code/Q3BSP/Q3BSPFileParser.h @@ -48,13 +48,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace Assimp { + class ZipArchiveIOSystem; + namespace Q3BSP { - -class Q3BSPZipArchive; -struct Q3BSPModel; -class ZipFile; - + struct Q3BSPModel; + class ZipFile; } // ------------------------------------------------------------------- @@ -62,7 +61,7 @@ class ZipFile; class Q3BSPFileParser { public: - Q3BSPFileParser( const std::string &rMapName, Q3BSP::Q3BSPZipArchive *pZipArchive ); + Q3BSPFileParser( const std::string &rMapName, ZipArchiveIOSystem *pZipArchive ); ~Q3BSPFileParser(); Q3BSP::Q3BSPModel *getModel() const; @@ -83,7 +82,7 @@ private: size_t m_sOffset; std::vector m_Data; Q3BSP::Q3BSPModel *m_pModel; - Q3BSP::Q3BSPZipArchive *m_pZipArchive; + ZipArchiveIOSystem *m_pZipArchive; }; } // Namespace Assimp diff --git a/code/Q3BSP/Q3BSPZipArchive.cpp b/code/Q3BSP/Q3BSPZipArchive.cpp deleted file mode 100644 index 931f2b905..000000000 --- a/code/Q3BSP/Q3BSPZipArchive.cpp +++ /dev/null @@ -1,325 +0,0 @@ -/* -Open Asset Import Library (assimp) ----------------------------------------------------------------------- - -Copyright (c) 2006-2019, assimp team - - -All rights reserved. - -Redistribution and use of this software in source and binary forms, -with or without modification, are permitted provided that the -following conditions are met: - -* Redistributions of source code must retain the above - copyright notice, this list of conditions and the - following disclaimer. - -* Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the - following disclaimer in the documentation and/or other - materials provided with the distribution. - -* Neither the name of the assimp team, nor the names of its - contributors may be used to endorse or promote products - derived from this software without specific prior - written permission of the assimp team. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ----------------------------------------------------------------------- -*/ - -#ifndef ASSIMP_BUILD_NO_Q3BSP_IMPORTER - -#include "Q3BSPZipArchive.h" -#include -#include -#include - -namespace Assimp { -namespace Q3BSP { - -voidpf IOSystem2Unzip::open(voidpf opaque, const char* filename, int mode) { - IOSystem* io_system = (IOSystem*) opaque; - - const char* mode_fopen = NULL; - if((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) { - mode_fopen = "rb"; - } else { - if(mode & ZLIB_FILEFUNC_MODE_EXISTING) { - mode_fopen = "r+b"; - } else { - if(mode & ZLIB_FILEFUNC_MODE_CREATE) { - mode_fopen = "wb"; - } - } - } - - return (voidpf) io_system->Open(filename, mode_fopen); -} - -uLong IOSystem2Unzip::read(voidpf /*opaque*/, voidpf stream, void* buf, uLong size) { - IOStream* io_stream = (IOStream*) stream; - - return static_cast(io_stream->Read(buf, 1, size)); -} - -uLong IOSystem2Unzip::write(voidpf /*opaque*/, voidpf stream, const void* buf, uLong size) { - IOStream* io_stream = (IOStream*) stream; - - return static_cast(io_stream->Write(buf, 1, size)); -} - -long IOSystem2Unzip::tell(voidpf /*opaque*/, voidpf stream) { - IOStream* io_stream = (IOStream*) stream; - - return static_cast(io_stream->Tell()); -} - -long IOSystem2Unzip::seek(voidpf /*opaque*/, voidpf stream, uLong offset, int origin) { - IOStream* io_stream = (IOStream*) stream; - - aiOrigin assimp_origin; - switch (origin) { - default: - case ZLIB_FILEFUNC_SEEK_CUR: - assimp_origin = aiOrigin_CUR; - break; - case ZLIB_FILEFUNC_SEEK_END: - assimp_origin = aiOrigin_END; - break; - case ZLIB_FILEFUNC_SEEK_SET: - assimp_origin = aiOrigin_SET; - break; - } - - return (io_stream->Seek(offset, assimp_origin) == aiReturn_SUCCESS ? 0 : -1); -} - -int IOSystem2Unzip::close(voidpf opaque, voidpf stream) { - IOSystem* io_system = (IOSystem*) opaque; - IOStream* io_stream = (IOStream*) stream; - - io_system->Close(io_stream); - - return 0; -} - -int IOSystem2Unzip::testerror(voidpf /*opaque*/, voidpf /*stream*/) { - return 0; -} - -zlib_filefunc_def IOSystem2Unzip::get(IOSystem* pIOHandler) { - zlib_filefunc_def mapping; - -#ifdef ASSIMP_USE_HUNTER - mapping.zopen_file = (open_file_func)open; - mapping.zread_file = (read_file_func)read; - mapping.zwrite_file = (write_file_func)write; - mapping.ztell_file = (tell_file_func)tell; - mapping.zseek_file = (seek_file_func)seek; - mapping.zclose_file = (close_file_func)close; - mapping.zerror_file = (error_file_func)testerror; -#else - mapping.zopen_file = open; - mapping.zread_file = read; - mapping.zwrite_file = write; - mapping.ztell_file = tell; - mapping.zseek_file = seek; - mapping.zclose_file = close; - mapping.zerror_file = testerror; -#endif - mapping.opaque = (voidpf) pIOHandler; - - return mapping; -} - -ZipFile::ZipFile(size_t size) : m_Size(size) { - ai_assert(m_Size != 0); - - m_Buffer = malloc(m_Size); -} - -ZipFile::~ZipFile() { - free(m_Buffer); - m_Buffer = NULL; -} - -size_t ZipFile::Read(void* pvBuffer, size_t pSize, size_t pCount) { - const size_t size = pSize * pCount; - assert(size <= m_Size); - - std::memcpy(pvBuffer, m_Buffer, size); - - return size; -} - -size_t ZipFile::Write(const void* /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/) { - return 0; -} - -size_t ZipFile::FileSize() const { - return m_Size; -} - -aiReturn ZipFile::Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/) { - return aiReturn_FAILURE; -} - -size_t ZipFile::Tell() const { - return 0; -} - -void ZipFile::Flush() { - // empty -} - -// ------------------------------------------------------------------------------------------------ -// Constructor. -Q3BSPZipArchive::Q3BSPZipArchive(IOSystem* pIOHandler, const std::string& rFile) : m_ZipFileHandle(NULL), m_ArchiveMap() { - if (! rFile.empty()) { - zlib_filefunc_def mapping = IOSystem2Unzip::get(pIOHandler); - - m_ZipFileHandle = unzOpen2(rFile.c_str(), &mapping); - - if(m_ZipFileHandle != nullptr) { - mapArchive(); - } - } -} - -// ------------------------------------------------------------------------------------------------ -// Destructor. -Q3BSPZipArchive::~Q3BSPZipArchive() { - for(auto &file : m_ArchiveMap) { - delete file.second; - } - m_ArchiveMap.clear(); - - if(m_ZipFileHandle != nullptr) { - unzClose(m_ZipFileHandle); - m_ZipFileHandle = nullptr; - } -} - -// ------------------------------------------------------------------------------------------------ -// Returns true, if the archive is already open. -bool Q3BSPZipArchive::isOpen() const { - return (m_ZipFileHandle != nullptr); -} - -// ------------------------------------------------------------------------------------------------ -// Returns true, if the filename is part of the archive. -bool Q3BSPZipArchive::Exists(const char* pFile) const { - bool exist = false; - if (pFile != nullptr) { - std::string rFile(pFile); - std::map::const_iterator it = m_ArchiveMap.find(rFile); - - if(it != m_ArchiveMap.end()) { - exist = true; - } - } - - return exist; -} - -// ------------------------------------------------------------------------------------------------ -// Returns the separator delimiter. -char Q3BSPZipArchive::getOsSeparator() const { -#ifndef _WIN32 - return '/'; -#else - return '\\'; -#endif -} - -// ------------------------------------------------------------------------------------------------ -// Opens a file, which is part of the archive. -IOStream *Q3BSPZipArchive::Open(const char* pFile, const char* /*pMode*/) { - ai_assert(pFile != nullptr); - - IOStream* result = nullptr; - - std::map::iterator it = m_ArchiveMap.find(pFile); - - if(it != m_ArchiveMap.end()) { - result = (IOStream*) it->second; - } - - return result; -} - -// ------------------------------------------------------------------------------------------------ -// Close a filestream. -void Q3BSPZipArchive::Close(IOStream *pFile) { - (void)(pFile); - ai_assert(pFile != nullptr); - - // We don't do anything in case the file would be opened again in the future -} -// ------------------------------------------------------------------------------------------------ -// Returns the file-list of the archive. -void Q3BSPZipArchive::getFileList(std::vector &rFileList) { - rFileList.clear(); - - for(auto &file : m_ArchiveMap) { - rFileList.push_back(file.first); - } -} - -// ------------------------------------------------------------------------------------------------ -// Maps the archive content. -bool Q3BSPZipArchive::mapArchive() { - bool success = false; - - if(m_ZipFileHandle != nullptr) { - if(m_ArchiveMap.empty()) { - // At first ensure file is already open - if(unzGoToFirstFile(m_ZipFileHandle) == UNZ_OK) { - // Loop over all files - do { - char filename[FileNameSize]; - unz_file_info fileInfo; - - if(unzGetCurrentFileInfo(m_ZipFileHandle, &fileInfo, filename, FileNameSize, NULL, 0, NULL, 0) == UNZ_OK) { - // The file has EXACTLY the size of uncompressed_size. In C - // you need to mark the last character with '\0', so add - // another character - if(fileInfo.uncompressed_size != 0 && unzOpenCurrentFile(m_ZipFileHandle) == UNZ_OK) { - std::pair::iterator, bool> result = m_ArchiveMap.insert(std::make_pair(filename, new ZipFile(fileInfo.uncompressed_size))); - - if(unzReadCurrentFile(m_ZipFileHandle, result.first->second->m_Buffer, fileInfo.uncompressed_size) == (long int) fileInfo.uncompressed_size) { - if(unzCloseCurrentFile(m_ZipFileHandle) == UNZ_OK) { - // Nothing to do anymore... - } - } - } - } - } while(unzGoToNextFile(m_ZipFileHandle) != UNZ_END_OF_LIST_OF_FILE); - } - } - - success = true; - } - - return success; -} - -// ------------------------------------------------------------------------------------------------ - -} // Namespace Q3BSP -} // Namespace Assimp - -#endif // ASSIMP_BUILD_NO_Q3BSP_IMPORTER diff --git a/code/Q3BSP/Q3BSPZipArchive.h b/code/Q3BSP/Q3BSPZipArchive.h deleted file mode 100644 index db68925a8..000000000 --- a/code/Q3BSP/Q3BSPZipArchive.h +++ /dev/null @@ -1,135 +0,0 @@ -/* -Open Asset Import Library (assimp) ----------------------------------------------------------------------- - -Copyright (c) 2006-2019, assimp team - - -All rights reserved. - -Redistribution and use of this software in source and binary forms, -with or without modification, are permitted provided that the -following conditions are met: - -* Redistributions of source code must retain the above - copyright notice, this list of conditions and the - following disclaimer. - -* Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the - following disclaimer in the documentation and/or other - materials provided with the distribution. - -* Neither the name of the assimp team, nor the names of its - contributors may be used to endorse or promote products - derived from this software without specific prior - written permission of the assimp team. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ----------------------------------------------------------------------- -*/ -#ifndef AI_Q3BSP_ZIPARCHIVE_H_INC -#define AI_Q3BSP_ZIPARCHIVE_H_INC - -#ifdef ASSIMP_USE_HUNTER -# include -#else -# include -#endif -#include -#include -#include -#include -#include - -namespace Assimp { -namespace Q3BSP { - -// ------------------------------------------------------------------------------------------------ -/// \class IOSystem2Unzip -/// \ingroup Assimp::Q3BSP -/// -/// \brief -// ------------------------------------------------------------------------------------------------ -class IOSystem2Unzip { -public: - static voidpf open(voidpf opaque, const char* filename, int mode); - static uLong read(voidpf opaque, voidpf stream, void* buf, uLong size); - static uLong write(voidpf opaque, voidpf stream, const void* buf, uLong size); - static long tell(voidpf opaque, voidpf stream); - static long seek(voidpf opaque, voidpf stream, uLong offset, int origin); - static int close(voidpf opaque, voidpf stream); - static int testerror(voidpf opaque, voidpf stream); - static zlib_filefunc_def get(IOSystem* pIOHandler); -}; - -// ------------------------------------------------------------------------------------------------ -/// \class ZipFile -/// \ingroup Assimp::Q3BSP -/// -/// \brief -// ------------------------------------------------------------------------------------------------ -class ZipFile : public IOStream { - friend class Q3BSPZipArchive; - -public: - explicit ZipFile(size_t size); - ~ZipFile(); - size_t Read(void* pvBuffer, size_t pSize, size_t pCount ); - size_t Write(const void* /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/); - size_t FileSize() const; - aiReturn Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/); - size_t Tell() const; - void Flush(); - -private: - void* m_Buffer; - size_t m_Size; -}; - -// ------------------------------------------------------------------------------------------------ -/// \class Q3BSPZipArchive -/// \ingroup Assimp::Q3BSP -/// -/// \brief IMplements a zip archive like the WinZip archives. Will be also used to import data -/// from a P3K archive ( Quake level format ). -// ------------------------------------------------------------------------------------------------ -class Q3BSPZipArchive : public Assimp::IOSystem { -public: - static const unsigned int FileNameSize = 256; - -public: - Q3BSPZipArchive(IOSystem* pIOHandler, const std::string & rFile); - ~Q3BSPZipArchive(); - bool Exists(const char* pFile) const; - char getOsSeparator() const; - IOStream* Open(const char* pFile, const char* pMode = "rb"); - void Close(IOStream* pFile); - bool isOpen() const; - void getFileList(std::vector &rFileList); - -private: - bool mapArchive(); - -private: - unzFile m_ZipFileHandle; - std::map m_ArchiveMap; -}; - -// ------------------------------------------------------------------------------------------------ - -} // Namespace Q3BSP -} // Namespace Assimp - -#endif // AI_Q3BSP_ZIPARCHIVE_H_INC