Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

318 rindas
9.0 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include "AssimpPCH.h"
  34. #ifndef ASSIMP_BUILD_NO_Q3BSP_IMPORTER
  35. #include "Q3BSPZipArchive.h"
  36. #include <algorithm>
  37. #include <cassert>
  38. namespace Assimp {
  39. namespace Q3BSP {
  40. voidpf IOSystem2Unzip::open(voidpf opaque, const char* filename, int mode) {
  41. IOSystem* io_system = (IOSystem*) opaque;
  42. const char* mode_fopen = NULL;
  43. if((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) {
  44. mode_fopen = "rb";
  45. } else {
  46. if(mode & ZLIB_FILEFUNC_MODE_EXISTING) {
  47. mode_fopen = "r+b";
  48. } else {
  49. if(mode & ZLIB_FILEFUNC_MODE_CREATE) {
  50. mode_fopen = "wb";
  51. }
  52. }
  53. }
  54. return (voidpf) io_system->Open(filename, mode_fopen);
  55. }
  56. uLong IOSystem2Unzip::read(voidpf opaque, voidpf stream, void* buf, uLong size) {
  57. IOStream* io_stream = (IOStream*) stream;
  58. return io_stream->Read(buf, 1, size);
  59. }
  60. uLong IOSystem2Unzip::write(voidpf opaque, voidpf stream, const void* buf, uLong size) {
  61. IOStream* io_stream = (IOStream*) stream;
  62. return io_stream->Write(buf, 1, size);
  63. }
  64. long IOSystem2Unzip::tell(voidpf opaque, voidpf stream) {
  65. IOStream* io_stream = (IOStream*) stream;
  66. return io_stream->Tell();
  67. }
  68. long IOSystem2Unzip::seek(voidpf opaque, voidpf stream, uLong offset, int origin) {
  69. IOStream* io_stream = (IOStream*) stream;
  70. aiOrigin assimp_origin;
  71. switch (origin) {
  72. default:
  73. case ZLIB_FILEFUNC_SEEK_CUR:
  74. assimp_origin = aiOrigin_CUR;
  75. break;
  76. case ZLIB_FILEFUNC_SEEK_END:
  77. assimp_origin = aiOrigin_END;
  78. break;
  79. case ZLIB_FILEFUNC_SEEK_SET:
  80. assimp_origin = aiOrigin_SET;
  81. break;
  82. }
  83. return (io_stream->Seek(offset, assimp_origin) == aiReturn_SUCCESS ? 0 : -1);
  84. }
  85. int IOSystem2Unzip::close(voidpf opaque, voidpf stream) {
  86. IOSystem* io_system = (IOSystem*) opaque;
  87. IOStream* io_stream = (IOStream*) stream;
  88. io_system->Close(io_stream);
  89. return 0;
  90. }
  91. int IOSystem2Unzip::testerror(voidpf opaque, voidpf stream) {
  92. return 0;
  93. }
  94. zlib_filefunc_def IOSystem2Unzip::get(IOSystem* pIOHandler) {
  95. zlib_filefunc_def mapping;
  96. mapping.zopen_file = open;
  97. mapping.zread_file = read;
  98. mapping.zwrite_file = write;
  99. mapping.ztell_file = tell;
  100. mapping.zseek_file = seek;
  101. mapping.zclose_file = close;
  102. mapping.zerror_file = testerror;
  103. mapping.opaque = (voidpf) pIOHandler;
  104. return mapping;
  105. }
  106. // ------------------------------------------------------------------------------------------------
  107. ZipFile::ZipFile(size_t size) : m_Size(size) {
  108. ai_assert(m_Size != 0);
  109. m_Buffer = std::malloc(m_Size);
  110. }
  111. ZipFile::~ZipFile() {
  112. std::free(m_Buffer);
  113. m_Buffer = NULL;
  114. }
  115. size_t ZipFile::Read(void* pvBuffer, size_t pSize, size_t pCount) {
  116. const size_t size = pSize * pCount;
  117. assert(size <= m_Size);
  118. std::memcpy(pvBuffer, m_Buffer, size);
  119. return size;
  120. }
  121. size_t ZipFile::Write(const void* /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/) {
  122. return 0;
  123. }
  124. size_t ZipFile::FileSize() const {
  125. return m_Size;
  126. }
  127. aiReturn ZipFile::Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/) {
  128. return aiReturn_FAILURE;
  129. }
  130. size_t ZipFile::Tell() const {
  131. return 0;
  132. }
  133. void ZipFile::Flush() {
  134. // empty
  135. }
  136. // ------------------------------------------------------------------------------------------------
  137. // Constructor.
  138. Q3BSPZipArchive::Q3BSPZipArchive(IOSystem* pIOHandler, const std::string& rFile) : m_ZipFileHandle(NULL), m_ArchiveMap() {
  139. if (! rFile.empty()) {
  140. zlib_filefunc_def mapping = IOSystem2Unzip::get(pIOHandler);
  141. m_ZipFileHandle = unzOpen2(rFile.c_str(), &mapping);
  142. if(m_ZipFileHandle != NULL) {
  143. mapArchive();
  144. }
  145. }
  146. }
  147. // ------------------------------------------------------------------------------------------------
  148. // Destructor.
  149. Q3BSPZipArchive::~Q3BSPZipArchive() {
  150. for( std::map<std::string, ZipFile*>::iterator it(m_ArchiveMap.begin()), end(m_ArchiveMap.end()); it != end; ++it ) {
  151. delete it->second;
  152. }
  153. m_ArchiveMap.clear();
  154. if(m_ZipFileHandle != NULL) {
  155. unzClose(m_ZipFileHandle);
  156. m_ZipFileHandle = NULL;
  157. }
  158. }
  159. // ------------------------------------------------------------------------------------------------
  160. // Returns true, if the archive is already open.
  161. bool Q3BSPZipArchive::isOpen() const {
  162. return (m_ZipFileHandle != NULL);
  163. }
  164. // ------------------------------------------------------------------------------------------------
  165. // Returns true, if the filename is part of the archive.
  166. bool Q3BSPZipArchive::Exists(const char* pFile) const {
  167. ai_assert(pFile != NULL);
  168. bool exist = false;
  169. if (pFile != NULL) {
  170. std::string rFile(pFile);
  171. std::map<std::string, ZipFile*>::const_iterator it = m_ArchiveMap.find(rFile);
  172. if(it != m_ArchiveMap.end()) {
  173. exist = true;
  174. }
  175. }
  176. return exist;
  177. }
  178. // ------------------------------------------------------------------------------------------------
  179. // Returns the separator delimiter.
  180. char Q3BSPZipArchive::getOsSeparator() const {
  181. #ifndef _WIN32
  182. return '/';
  183. #else
  184. return '\\';
  185. #endif
  186. }
  187. // ------------------------------------------------------------------------------------------------
  188. // Opens a file, which is part of the archive.
  189. IOStream *Q3BSPZipArchive::Open(const char* pFile, const char* /*pMode*/) {
  190. ai_assert(pFile != NULL);
  191. IOStream* result = NULL;
  192. std::map<std::string, ZipFile*>::iterator it = m_ArchiveMap.find(pFile);
  193. if(it != m_ArchiveMap.end()) {
  194. result = (IOStream*) it->second;
  195. }
  196. return result;
  197. }
  198. // ------------------------------------------------------------------------------------------------
  199. // Close a filestream.
  200. void Q3BSPZipArchive::Close(IOStream *pFile) {
  201. ai_assert(pFile != NULL);
  202. // We don't do anything in case the file would be opened again in the future
  203. }
  204. // ------------------------------------------------------------------------------------------------
  205. // Returns the file-list of the archive.
  206. void Q3BSPZipArchive::getFileList(std::vector<std::string> &rFileList) {
  207. rFileList.clear();
  208. for(std::map<std::string, ZipFile*>::iterator it(m_ArchiveMap.begin()), end(m_ArchiveMap.end()); it != end; ++it) {
  209. rFileList.push_back(it->first);
  210. }
  211. }
  212. // ------------------------------------------------------------------------------------------------
  213. // Maps the archive content.
  214. bool Q3BSPZipArchive::mapArchive() {
  215. bool success = false;
  216. if(m_ZipFileHandle != NULL) {
  217. if(m_ArchiveMap.empty()) {
  218. // At first ensure file is already open
  219. if(unzGoToFirstFile(m_ZipFileHandle) == UNZ_OK) {
  220. // Loop over all files
  221. do {
  222. char filename[FileNameSize];
  223. unz_file_info fileInfo;
  224. if(unzGetCurrentFileInfo(m_ZipFileHandle, &fileInfo, filename, FileNameSize, NULL, 0, NULL, 0) == UNZ_OK) {
  225. // The file has EXACTLY the size of uncompressed_size. In C
  226. // you need to mark the last character with '\0', so add
  227. // another character
  228. if(unzOpenCurrentFile(m_ZipFileHandle) == UNZ_OK) {
  229. std::pair<std::map<std::string, ZipFile*>::iterator, bool> result = m_ArchiveMap.insert(std::make_pair(filename, new ZipFile(fileInfo.uncompressed_size)));
  230. if(unzReadCurrentFile(m_ZipFileHandle, result.first->second->m_Buffer, fileInfo.uncompressed_size) == (long int) fileInfo.uncompressed_size) {
  231. if(unzCloseCurrentFile(m_ZipFileHandle) == UNZ_OK) {
  232. // Nothing to do anymore...
  233. }
  234. }
  235. }
  236. }
  237. } while(unzGoToNextFile(m_ZipFileHandle) != UNZ_END_OF_LIST_OF_FILE);
  238. }
  239. }
  240. success = true;
  241. }
  242. return success;
  243. }
  244. // ------------------------------------------------------------------------------------------------
  245. } // Namespace Q3BSP
  246. } // Namespace Assimp
  247. #endif // ASSIMP_BUILD_NO_Q3BSP_IMPORTER