Q3BSPFileParser.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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. #ifndef ASSIMP_BUILD_NO_Q3BSP_IMPORTER
  34. #include "Q3BSPFileParser.h"
  35. #include "Q3BSPFileData.h"
  36. #include <vector>
  37. #include <assimp/DefaultIOSystem.h>
  38. #include <assimp/ZipArchiveIOSystem.h>
  39. #include <assimp/ai_assert.h>
  40. namespace Assimp {
  41. using namespace Q3BSP;
  42. // ------------------------------------------------------------------------------------------------
  43. Q3BSPFileParser::Q3BSPFileParser( const std::string &mapName, ZipArchiveIOSystem *pZipArchive ) :
  44. m_sOffset( 0 ),
  45. m_Data(),
  46. m_pModel(nullptr),
  47. m_pZipArchive( pZipArchive )
  48. {
  49. ai_assert(nullptr != m_pZipArchive );
  50. ai_assert( !mapName.empty() );
  51. if ( !readData( mapName ) )
  52. return;
  53. m_pModel = new Q3BSPModel;
  54. m_pModel->m_ModelName = mapName;
  55. if ( !parseFile() ) {
  56. delete m_pModel;
  57. m_pModel = nullptr;
  58. }
  59. }
  60. // ------------------------------------------------------------------------------------------------
  61. Q3BSPFileParser::~Q3BSPFileParser() {
  62. delete m_pModel;
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. Q3BSP::Q3BSPModel *Q3BSPFileParser::getModel() const {
  66. return m_pModel;
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. bool Q3BSPFileParser::readData( const std::string &rMapName ) {
  70. if ( !m_pZipArchive->Exists( rMapName.c_str() ) )
  71. return false;
  72. IOStream *pMapFile = m_pZipArchive->Open( rMapName.c_str() );
  73. if ( nullptr == pMapFile )
  74. return false;
  75. const size_t size = pMapFile->FileSize();
  76. m_Data.resize( size );
  77. const size_t readSize = pMapFile->Read( &m_Data[0], sizeof( char ), size );
  78. if ( readSize != size ) {
  79. m_Data.clear();
  80. m_pZipArchive->Close(pMapFile);
  81. return false;
  82. }
  83. m_pZipArchive->Close( pMapFile );
  84. return true;
  85. }
  86. // ------------------------------------------------------------------------------------------------
  87. bool Q3BSPFileParser::parseFile() {
  88. if ( m_Data.empty() ) {
  89. return false;
  90. }
  91. if ( !validateFormat() )
  92. {
  93. return false;
  94. }
  95. // Imports the dictionary of the level
  96. getLumps();
  97. // Count data and prepare model data
  98. countLumps();
  99. // Read in Vertices
  100. getVertices();
  101. // Read in Indices
  102. getIndices();
  103. // Read Faces
  104. getFaces();
  105. // Read Textures
  106. getTextures();
  107. // Read Lightmaps
  108. getLightMaps();
  109. // Load the entities
  110. getEntities();
  111. return true;
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. bool Q3BSPFileParser::validateFormat()
  115. {
  116. sQ3BSPHeader *pHeader = (sQ3BSPHeader*) &m_Data[ 0 ];
  117. m_sOffset += sizeof( sQ3BSPHeader );
  118. // Version and identify string validation
  119. if (pHeader->strID[ 0 ] != 'I' || pHeader->strID[ 1 ] != 'B' || pHeader->strID[ 2 ] != 'S'
  120. || pHeader->strID[ 3 ] != 'P')
  121. {
  122. return false;
  123. }
  124. return true;
  125. }
  126. // ------------------------------------------------------------------------------------------------
  127. void Q3BSPFileParser::getLumps()
  128. {
  129. size_t Offset = m_sOffset;
  130. m_pModel->m_Lumps.resize( kMaxLumps );
  131. for ( size_t idx=0; idx < kMaxLumps; idx++ )
  132. {
  133. sQ3BSPLump *pLump = new sQ3BSPLump;
  134. memcpy( pLump, &m_Data[ Offset ], sizeof( sQ3BSPLump ) );
  135. Offset += sizeof( sQ3BSPLump );
  136. m_pModel->m_Lumps[ idx ] = pLump;
  137. }
  138. }
  139. // ------------------------------------------------------------------------------------------------
  140. void Q3BSPFileParser::countLumps()
  141. {
  142. m_pModel->m_Vertices.resize( m_pModel->m_Lumps[ kVertices ]->iSize / sizeof( sQ3BSPVertex ) );
  143. m_pModel->m_Indices.resize( m_pModel->m_Lumps[ kMeshVerts ]->iSize / sizeof( int ) );
  144. m_pModel->m_Faces.resize( m_pModel->m_Lumps[ kFaces ]->iSize / sizeof( sQ3BSPFace ) );
  145. m_pModel->m_Textures.resize( m_pModel->m_Lumps[ kTextures ]->iSize / sizeof( sQ3BSPTexture ) );
  146. m_pModel->m_Lightmaps.resize( m_pModel->m_Lumps[ kLightmaps ]->iSize / sizeof( sQ3BSPLightmap ) );
  147. }
  148. // ------------------------------------------------------------------------------------------------
  149. void Q3BSPFileParser::getVertices()
  150. {
  151. size_t Offset = m_pModel->m_Lumps[ kVertices ]->iOffset;
  152. for ( size_t idx = 0; idx < m_pModel->m_Vertices.size(); idx++ )
  153. {
  154. sQ3BSPVertex *pVertex = new sQ3BSPVertex;
  155. memcpy( pVertex, &m_Data[ Offset ], sizeof( sQ3BSPVertex ) );
  156. Offset += sizeof( sQ3BSPVertex );
  157. m_pModel->m_Vertices[ idx ] = pVertex;
  158. }
  159. }
  160. // ------------------------------------------------------------------------------------------------
  161. void Q3BSPFileParser::getIndices()
  162. {
  163. ai_assert(nullptr != m_pModel );
  164. sQ3BSPLump *lump = m_pModel->m_Lumps[ kMeshVerts ];
  165. size_t Offset = (size_t) lump->iOffset;
  166. const size_t nIndices = lump->iSize / sizeof( int );
  167. m_pModel->m_Indices.resize( nIndices );
  168. memcpy( &m_pModel->m_Indices[ 0 ], &m_Data[ Offset ], lump->iSize );
  169. }
  170. // ------------------------------------------------------------------------------------------------
  171. void Q3BSPFileParser::getFaces()
  172. {
  173. ai_assert(nullptr != m_pModel );
  174. size_t Offset = m_pModel->m_Lumps[ kFaces ]->iOffset;
  175. for ( size_t idx = 0; idx < m_pModel->m_Faces.size(); idx++ )
  176. {
  177. sQ3BSPFace *pFace = new sQ3BSPFace;
  178. memcpy( pFace, &m_Data[ Offset ], sizeof( sQ3BSPFace ) );
  179. m_pModel->m_Faces[ idx ] = pFace;
  180. Offset += sizeof( sQ3BSPFace );
  181. }
  182. }
  183. // ------------------------------------------------------------------------------------------------
  184. void Q3BSPFileParser::getTextures()
  185. {
  186. ai_assert(nullptr != m_pModel );
  187. size_t Offset = m_pModel->m_Lumps[ kTextures ]->iOffset;
  188. for ( size_t idx=0; idx < m_pModel->m_Textures.size(); idx++ )
  189. {
  190. sQ3BSPTexture *pTexture = new sQ3BSPTexture;
  191. memcpy( pTexture, &m_Data[ Offset ], sizeof(sQ3BSPTexture) );
  192. m_pModel->m_Textures[ idx ] = pTexture;
  193. Offset += sizeof(sQ3BSPTexture);
  194. }
  195. }
  196. // ------------------------------------------------------------------------------------------------
  197. void Q3BSPFileParser::getLightMaps()
  198. {
  199. ai_assert(nullptr != m_pModel );
  200. size_t Offset = m_pModel->m_Lumps[kLightmaps]->iOffset;
  201. for ( size_t idx=0; idx < m_pModel->m_Lightmaps.size(); idx++ )
  202. {
  203. sQ3BSPLightmap *pLightmap = new sQ3BSPLightmap;
  204. memcpy( pLightmap, &m_Data[ Offset ], sizeof( sQ3BSPLightmap ) );
  205. Offset += sizeof( sQ3BSPLightmap );
  206. m_pModel->m_Lightmaps[ idx ] = pLightmap;
  207. }
  208. }
  209. // ------------------------------------------------------------------------------------------------
  210. void Q3BSPFileParser::getEntities() {
  211. const int size = m_pModel->m_Lumps[ kEntities ]->iSize;
  212. m_pModel->m_EntityData.resize( size );
  213. if ( size > 0 ) {
  214. size_t Offset = m_pModel->m_Lumps[ kEntities ]->iOffset;
  215. memcpy( &m_pModel->m_EntityData[ 0 ], &m_Data[ Offset ], sizeof( char ) * size );
  216. }
  217. }
  218. // ------------------------------------------------------------------------------------------------
  219. } // Namespace Assimp
  220. #endif // ASSIMP_BUILD_NO_Q3BSP_IMPORTER