2010-06-18 17:36:46 +00:00
|
|
|
/*
|
2012-02-03 03:38:30 +00:00
|
|
|
Open Asset Import Library (assimp)
|
2010-06-18 17:36:46 +00:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
Copyright (c) 2006-2008, assimp team
|
2010-06-18 17:36:46 +00:00
|
|
|
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.
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
* Neither the name of the assimp team, nor the names of its
|
2010-06-18 17:36:46 +00:00
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
2012-02-03 03:38:30 +00:00
|
|
|
written permission of the assimp team.
|
2010-06-18 17:36:46 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
2010-06-21 17:28:02 +00:00
|
|
|
|
2015-04-15 23:00:17 +00:00
|
|
|
|
2012-12-20 11:43:09 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_Q3BSP_IMPORTER
|
|
|
|
|
2010-06-18 17:36:46 +00:00
|
|
|
#include "Q3BSPZipArchive.h"
|
|
|
|
#include <algorithm>
|
2015-04-15 23:00:17 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include "../include/assimp/ai_assert.h"
|
|
|
|
|
2010-06-18 17:36:46 +00:00
|
|
|
|
2013-11-08 17:07:08 +00:00
|
|
|
namespace Assimp {
|
|
|
|
namespace Q3BSP {
|
|
|
|
|
2014-03-10 16:21:17 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-08-18 00:09:06 +00:00
|
|
|
uLong IOSystem2Unzip::read(voidpf /*opaque*/, voidpf stream, void* buf, uLong size) {
|
2014-03-10 16:21:17 +00:00
|
|
|
IOStream* io_stream = (IOStream*) stream;
|
|
|
|
|
|
|
|
return io_stream->Read(buf, 1, size);
|
|
|
|
}
|
|
|
|
|
2014-08-18 00:09:06 +00:00
|
|
|
uLong IOSystem2Unzip::write(voidpf /*opaque*/, voidpf stream, const void* buf, uLong size) {
|
2014-03-10 16:21:17 +00:00
|
|
|
IOStream* io_stream = (IOStream*) stream;
|
|
|
|
|
|
|
|
return io_stream->Write(buf, 1, size);
|
|
|
|
}
|
|
|
|
|
2014-08-18 00:09:06 +00:00
|
|
|
long IOSystem2Unzip::tell(voidpf /*opaque*/, voidpf stream) {
|
2014-03-10 16:21:17 +00:00
|
|
|
IOStream* io_stream = (IOStream*) stream;
|
|
|
|
|
|
|
|
return io_stream->Tell();
|
|
|
|
}
|
|
|
|
|
2014-08-18 00:09:06 +00:00
|
|
|
long IOSystem2Unzip::seek(voidpf /*opaque*/, voidpf stream, uLong offset, int origin) {
|
2014-03-10 16:21:17 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-18 00:09:06 +00:00
|
|
|
int IOSystem2Unzip::testerror(voidpf /*opaque*/, voidpf /*stream*/) {
|
2014-03-10 16:21:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
zlib_filefunc_def IOSystem2Unzip::get(IOSystem* pIOHandler) {
|
|
|
|
zlib_filefunc_def mapping;
|
|
|
|
|
|
|
|
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;
|
|
|
|
mapping.opaque = (voidpf) pIOHandler;
|
|
|
|
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2013-11-19 08:45:17 +00:00
|
|
|
ZipFile::ZipFile(size_t size) : m_Size(size) {
|
|
|
|
ai_assert(m_Size != 0);
|
|
|
|
|
|
|
|
m_Buffer = std::malloc(m_Size);
|
2013-11-08 17:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ZipFile::~ZipFile() {
|
2013-11-19 08:45:17 +00:00
|
|
|
std::free(m_Buffer);
|
|
|
|
m_Buffer = NULL;
|
2013-11-08 17:07:08 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 11:58:45 +00:00
|
|
|
size_t ZipFile::Read(void* pvBuffer, size_t pSize, size_t pCount) {
|
2013-11-19 08:45:17 +00:00
|
|
|
const size_t size = pSize * pCount;
|
|
|
|
assert(size <= m_Size);
|
2013-11-08 17:07:08 +00:00
|
|
|
|
2013-11-19 08:45:17 +00:00
|
|
|
std::memcpy(pvBuffer, m_Buffer, size);
|
2013-11-08 17:07:08 +00:00
|
|
|
|
2013-11-19 08:45:17 +00:00
|
|
|
return size;
|
2013-11-08 17:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t ZipFile::Write(const void* /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ZipFile::FileSize() const {
|
2013-11-19 08:45:17 +00:00
|
|
|
return m_Size;
|
2013-11-08 17:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aiReturn ZipFile::Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/) {
|
|
|
|
return aiReturn_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ZipFile::Tell() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZipFile::Flush() {
|
|
|
|
// empty
|
|
|
|
}
|
2010-06-18 17:36:46 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-11-05 18:45:42 +00:00
|
|
|
// Constructor.
|
2014-03-10 16:21:17 +00:00
|
|
|
Q3BSPZipArchive::Q3BSPZipArchive(IOSystem* pIOHandler, const std::string& rFile) : m_ZipFileHandle(NULL), m_ArchiveMap() {
|
2013-11-08 17:07:08 +00:00
|
|
|
if (! rFile.empty()) {
|
2014-03-19 14:45:12 +00:00
|
|
|
zlib_filefunc_def mapping = IOSystem2Unzip::get(pIOHandler);
|
|
|
|
|
|
|
|
m_ZipFileHandle = unzOpen2(rFile.c_str(), &mapping);
|
|
|
|
|
2013-11-08 17:07:08 +00:00
|
|
|
if(m_ZipFileHandle != NULL) {
|
2010-06-18 17:36:46 +00:00
|
|
|
mapArchive();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-11-05 18:45:42 +00:00
|
|
|
// Destructor.
|
2013-11-08 17:07:08 +00:00
|
|
|
Q3BSPZipArchive::~Q3BSPZipArchive() {
|
2013-11-19 08:45:17 +00:00
|
|
|
for( std::map<std::string, ZipFile*>::iterator it(m_ArchiveMap.begin()), end(m_ArchiveMap.end()); it != end; ++it ) {
|
|
|
|
delete it->second;
|
2013-09-06 15:37:01 +00:00
|
|
|
}
|
|
|
|
m_ArchiveMap.clear();
|
|
|
|
|
2013-11-08 17:07:08 +00:00
|
|
|
if(m_ZipFileHandle != NULL) {
|
|
|
|
unzClose(m_ZipFileHandle);
|
|
|
|
m_ZipFileHandle = NULL;
|
2010-11-05 18:45:42 +00:00
|
|
|
}
|
2010-06-18 17:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-11-05 18:45:42 +00:00
|
|
|
// Returns true, if the archive is already open.
|
2013-11-08 17:07:08 +00:00
|
|
|
bool Q3BSPZipArchive::isOpen() const {
|
|
|
|
return (m_ZipFileHandle != NULL);
|
2010-06-18 17:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-11-05 18:45:42 +00:00
|
|
|
// Returns true, if the filename is part of the archive.
|
2013-11-08 17:07:08 +00:00
|
|
|
bool Q3BSPZipArchive::Exists(const char* pFile) const {
|
|
|
|
ai_assert(pFile != NULL);
|
|
|
|
|
|
|
|
bool exist = false;
|
2010-06-18 17:36:46 +00:00
|
|
|
|
2013-11-08 17:07:08 +00:00
|
|
|
if (pFile != NULL) {
|
|
|
|
std::string rFile(pFile);
|
2013-11-19 08:45:17 +00:00
|
|
|
std::map<std::string, ZipFile*>::const_iterator it = m_ArchiveMap.find(rFile);
|
2013-11-08 17:07:08 +00:00
|
|
|
|
2013-11-19 08:45:17 +00:00
|
|
|
if(it != m_ArchiveMap.end()) {
|
2013-11-08 17:07:08 +00:00
|
|
|
exist = true;
|
|
|
|
}
|
2010-11-05 18:45:42 +00:00
|
|
|
}
|
2010-06-18 17:36:46 +00:00
|
|
|
|
2013-11-08 17:07:08 +00:00
|
|
|
return exist;
|
2010-06-18 17:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-11-05 18:45:42 +00:00
|
|
|
// Returns the separator delimiter.
|
2013-11-08 17:07:08 +00:00
|
|
|
char Q3BSPZipArchive::getOsSeparator() const {
|
|
|
|
#ifndef _WIN32
|
2010-06-18 17:36:46 +00:00
|
|
|
return '/';
|
2013-11-08 17:07:08 +00:00
|
|
|
#else
|
|
|
|
return '\\';
|
|
|
|
#endif
|
2010-06-18 17:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-11-05 18:45:42 +00:00
|
|
|
// Opens a file, which is part of the archive.
|
2013-11-08 17:07:08 +00:00
|
|
|
IOStream *Q3BSPZipArchive::Open(const char* pFile, const char* /*pMode*/) {
|
|
|
|
ai_assert(pFile != NULL);
|
|
|
|
|
|
|
|
IOStream* result = NULL;
|
2010-06-18 17:36:46 +00:00
|
|
|
|
2013-11-19 08:45:17 +00:00
|
|
|
std::map<std::string, ZipFile*>::iterator it = m_ArchiveMap.find(pFile);
|
2013-11-08 17:07:08 +00:00
|
|
|
|
2013-11-19 08:45:17 +00:00
|
|
|
if(it != m_ArchiveMap.end()) {
|
|
|
|
result = (IOStream*) it->second;
|
2013-11-08 17:07:08 +00:00
|
|
|
}
|
2010-06-18 17:36:46 +00:00
|
|
|
|
2013-11-08 17:07:08 +00:00
|
|
|
return result;
|
2010-06-18 17:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-11-05 18:45:42 +00:00
|
|
|
// Close a filestream.
|
2013-11-08 17:07:08 +00:00
|
|
|
void Q3BSPZipArchive::Close(IOStream *pFile) {
|
|
|
|
ai_assert(pFile != NULL);
|
|
|
|
|
2013-11-19 08:45:17 +00:00
|
|
|
// We don't do anything in case the file would be opened again in the future
|
2010-06-18 17:36:46 +00:00
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-11-05 18:45:42 +00:00
|
|
|
// Returns the file-list of the archive.
|
2013-11-08 17:07:08 +00:00
|
|
|
void Q3BSPZipArchive::getFileList(std::vector<std::string> &rFileList) {
|
|
|
|
rFileList.clear();
|
|
|
|
|
2013-11-19 08:45:17 +00:00
|
|
|
for(std::map<std::string, ZipFile*>::iterator it(m_ArchiveMap.begin()), end(m_ArchiveMap.end()); it != end; ++it) {
|
|
|
|
rFileList.push_back(it->first);
|
|
|
|
}
|
2010-06-18 17:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-11-05 18:45:42 +00:00
|
|
|
// Maps the archive content.
|
2013-11-08 17:07:08 +00:00
|
|
|
bool Q3BSPZipArchive::mapArchive() {
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
if(m_ZipFileHandle != NULL) {
|
2013-11-19 08:45:17 +00:00
|
|
|
if(m_ArchiveMap.empty()) {
|
2013-11-08 17:07:08 +00:00
|
|
|
// At first ensure file is already open
|
|
|
|
if(unzGoToFirstFile(m_ZipFileHandle) == UNZ_OK) {
|
|
|
|
// Loop over all files
|
|
|
|
do {
|
|
|
|
char filename[FileNameSize];
|
2013-11-19 08:45:17 +00:00
|
|
|
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(unzOpenCurrentFile(m_ZipFileHandle) == UNZ_OK) {
|
|
|
|
std::pair<std::map<std::string, ZipFile*>::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...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-08 17:07:08 +00:00
|
|
|
}
|
|
|
|
} while(unzGoToNextFile(m_ZipFileHandle) != UNZ_END_OF_LIST_OF_FILE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
success = true;
|
|
|
|
}
|
2010-06-18 17:36:46 +00:00
|
|
|
|
2013-11-08 17:07:08 +00:00
|
|
|
return success;
|
2010-06-18 17:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
} // Namespace Q3BSP
|
|
|
|
} // Namespace Assimp
|
2012-12-20 11:43:09 +00:00
|
|
|
|
|
|
|
#endif // ASSIMP_BUILD_NO_Q3BSP_IMPORTER
|