Q3BSPFileImporter.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 "DefaultIOSystem.h"
  36. #include "Q3BSPFileImporter.h"
  37. #include "Q3BSPZipArchive.h"
  38. #include "Q3BSPFileParser.h"
  39. #include "../contrib/zlib/zlib.h"
  40. #include <vector>
  41. namespace Assimp
  42. {
  43. using namespace Q3BSP;
  44. // ------------------------------------------------------------------------------------------------
  45. Q3BSPFileImporter::Q3BSPFileImporter()
  46. {
  47. }
  48. // ------------------------------------------------------------------------------------------------
  49. Q3BSPFileImporter::~Q3BSPFileImporter()
  50. {
  51. }
  52. // ------------------------------------------------------------------------------------------------
  53. bool Q3BSPFileImporter::CanRead( const std::string& rFile, IOSystem* pIOHandler, bool checkSig ) const
  54. {
  55. bool isBSPData = false;
  56. if ( checkSig )
  57. isBSPData = SimpleExtensionCheck( rFile, "pk3" );
  58. return isBSPData;
  59. }
  60. // ------------------------------------------------------------------------------------------------
  61. void Q3BSPFileImporter::GetExtensionList(std::set<std::string>& extensions)
  62. {
  63. extensions.insert("pk3");
  64. }
  65. // ------------------------------------------------------------------------------------------------
  66. void Q3BSPFileImporter::InternReadFile(const std::string &rFile, aiScene* pScene, IOSystem* pIOHandler)
  67. {
  68. Q3BSPZipArchive Archive( rFile );
  69. if ( !Archive.isOpen() )
  70. {
  71. throw new DeadlyImportError( "Failed to open file " + rFile + "." );
  72. }
  73. std::string archiveName( "" ), mapName( "" );
  74. separateMapName( rFile, archiveName, mapName );
  75. if ( mapName.empty() )
  76. {
  77. if ( !findFirstMapInArchive( Archive, mapName ) )
  78. {
  79. return;
  80. }
  81. }
  82. Q3BSPFileParser fileParser( mapName, &Archive );
  83. Q3BSPModel *pBSPModel = fileParser.getModel();
  84. if ( NULL != pBSPModel )
  85. {
  86. }
  87. }
  88. // ------------------------------------------------------------------------------------------------
  89. void Q3BSPFileImporter::separateMapName( const std::string &rImportName, std::string &rArchiveName,
  90. std::string &rMapName )
  91. {
  92. rArchiveName = "";
  93. rMapName = "";
  94. if ( rImportName.empty() )
  95. return;
  96. std::string::size_type pos = rImportName.rfind( "," );
  97. if ( std::string::npos == pos )
  98. {
  99. rArchiveName = rImportName;
  100. return;
  101. }
  102. rArchiveName = rImportName.substr( 0, pos );
  103. rMapName = rImportName.substr( pos, rImportName.size() - pos - 1 );
  104. }
  105. // ------------------------------------------------------------------------------------------------
  106. bool Q3BSPFileImporter::findFirstMapInArchive( Q3BSPZipArchive &rArchive, std::string &rMapName )
  107. {
  108. rMapName = "";
  109. std::vector<std::string> fileList;
  110. rArchive.getFileList( fileList );
  111. if ( fileList.empty() )
  112. return false;
  113. for ( std::vector<std::string>::iterator it = fileList.begin(); it != fileList.end();
  114. ++it )
  115. {
  116. std::string::size_type pos = (*it).find( "maps/" );
  117. if ( std::string::npos != pos )
  118. {
  119. std::string::size_type extPos = (*it).find( ".bsp" );
  120. if ( std::string::npos != extPos )
  121. {
  122. rMapName = *it;
  123. return true;
  124. }
  125. }
  126. }
  127. return false;
  128. }
  129. // ------------------------------------------------------------------------------------------------
  130. } // Namespace Assimp
  131. #endif // ASSIMP_BUILD_NO_Q3BSP_IMPORTER