BaseImporter.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file BaseImporter.cpp
  35. * @brief Implementation of BaseImporter
  36. */
  37. #include "AssimpPCH.h"
  38. #include "BaseImporter.h"
  39. #include "FileSystemFilter.h"
  40. using namespace Assimp;
  41. // ------------------------------------------------------------------------------------------------
  42. // Constructor to be privately used by Importer
  43. BaseImporter::BaseImporter()
  44. {
  45. // nothing to do here
  46. }
  47. // ------------------------------------------------------------------------------------------------
  48. // Destructor, private as well
  49. BaseImporter::~BaseImporter()
  50. {
  51. // nothing to do here
  52. }
  53. // ------------------------------------------------------------------------------------------------
  54. // Imports the given file and returns the imported data.
  55. aiScene* BaseImporter::ReadFile( const std::string& pFile, IOSystem* pIOHandler)
  56. {
  57. // Construct a file system filter to improve our success ratio reading external files
  58. FileSystemFilter filter(pFile,pIOHandler);
  59. // create a scene object to hold the data
  60. aiScene* scene = new aiScene();
  61. // dispatch importing
  62. try
  63. {
  64. InternReadFile( pFile, scene, &filter);
  65. } catch( ImportErrorException* exception)
  66. {
  67. // extract error description
  68. mErrorText = exception->GetErrorText();
  69. DefaultLogger::get()->error(mErrorText);
  70. delete exception;
  71. // and kill the partially imported data
  72. delete scene;
  73. scene = NULL;
  74. }
  75. // return what we gathered from the import.
  76. return scene;
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. void BaseImporter::SetupProperties(const Importer* pImp)
  80. {
  81. // the default implementation does nothing
  82. }
  83. // ------------------------------------------------------------------------------------------------
  84. /*static*/ bool BaseImporter::SearchFileHeaderForToken(IOSystem* pIOHandler,
  85. const std::string& pFile,
  86. const char** tokens,
  87. unsigned int numTokens,
  88. unsigned int searchBytes /* = 200 */)
  89. {
  90. ai_assert(NULL != tokens && 0 != numTokens && NULL != pIOHandler && 0 != searchBytes);
  91. boost::scoped_ptr<IOStream> pStream (pIOHandler->Open(pFile));
  92. if (pStream.get() )
  93. {
  94. // read 200 characters from the file
  95. boost::scoped_array<char> _buffer (new char[searchBytes+1 /* for the '\0' */]);
  96. char* buffer = _buffer.get();
  97. unsigned int read = (unsigned int)pStream->Read(buffer,1,searchBytes);
  98. if (!read)return false;
  99. for (unsigned int i = 0; i < read;++i)
  100. buffer[i] = ::tolower(buffer[i]);
  101. // It is not a proper handling of unicode files here ...
  102. // ehm ... but it works in most cases.
  103. char* cur = buffer,*cur2 = buffer,*end = &buffer[read];
  104. while (cur != end)
  105. {
  106. if (*cur)*cur2++ = *cur;
  107. ++cur;
  108. }
  109. *cur2 = '\0';
  110. for (unsigned int i = 0; i < numTokens;++i)
  111. {
  112. ai_assert(NULL != tokens[i]);
  113. if (::strstr(buffer,tokens[i]))
  114. {
  115. DefaultLogger::get()->debug(std::string("Found positive match for header keyword: ") + tokens[i]);
  116. return true;
  117. }
  118. }
  119. }
  120. return false;
  121. }
  122. // ------------------------------------------------------------------------------------------------
  123. // Simple check for file extension
  124. /*static*/ bool BaseImporter::SimpleExtensionCheck (const std::string& pFile,
  125. const char* ext0,
  126. const char* ext1,
  127. const char* ext2)
  128. {
  129. std::string::size_type pos = pFile.find_last_of('.');
  130. // no file extension - can't read
  131. if( pos == std::string::npos)
  132. return false;
  133. const char* ext_real = & pFile[ pos+1 ];
  134. if( !ASSIMP_stricmp(ext_real,ext0) )
  135. return true;
  136. // check for other, optional, file extensions
  137. if (ext1 && !ASSIMP_stricmp(ext_real,ext1))
  138. return true;
  139. if (ext2 && !ASSIMP_stricmp(ext_real,ext2))
  140. return true;
  141. return false;
  142. }
  143. // ------------------------------------------------------------------------------------------------
  144. // Get file extension from path
  145. /*static*/ std::string BaseImporter::GetExtension (const std::string& pFile)
  146. {
  147. std::string::size_type pos = pFile.find_last_of('.');
  148. // no file extension at all
  149. if( pos == std::string::npos)
  150. return "";
  151. std::string ret = pFile.substr(pos+1);
  152. std::transform(ret.begin(),ret.end(),ret.begin(),::tolower); // thanks to Andy Maloney for the hint
  153. return ret;
  154. }
  155. // ------------------------------------------------------------------------------------------------
  156. // Check for magic bytes at the beginning of the file.
  157. /* static */ bool BaseImporter::CheckMagicToken(IOSystem* pIOHandler, const std::string& pFile,
  158. const void* _magic, unsigned int num, unsigned int offset, unsigned int size)
  159. {
  160. ai_assert(size <= 16 && _magic && num && pIOHandler);
  161. const char* magic = (const char*)_magic;
  162. boost::scoped_ptr<IOStream> pStream (pIOHandler->Open(pFile));
  163. if (pStream.get() )
  164. {
  165. // skip to offset
  166. pStream->Seek(offset,aiOrigin_SET);
  167. // read 'size' characters from the file
  168. char data[16];
  169. if(size != pStream->Read(data,1,size))
  170. return false;
  171. for (unsigned int i = 0; i < num; ++i) {
  172. // also check against big endian versions of tokens with size 2,4
  173. // that's just for convinience, the chance that we cause conflicts
  174. // is quite low and it can save some lines and prevent nasty bugs
  175. if (2 == size) {
  176. int16_t rev = *((int16_t*)magic);
  177. ByteSwap::Swap(&rev);
  178. if (*((int16_t*)data) == ((int16_t*)magic)[i] || *((int16_t*)data) == rev)
  179. return true;
  180. }
  181. else if (4 == size) {
  182. int32_t rev = *((int32_t*)magic);
  183. ByteSwap::Swap(&rev);
  184. if (*((int32_t*)data) == ((int32_t*)magic)[i] || *((int32_t*)data) == rev)
  185. return true;
  186. }
  187. else {
  188. // any length ... just compare
  189. if(!::memcmp(magic,data,size))
  190. return true;
  191. }
  192. magic += size;
  193. }
  194. }
  195. return false;
  196. }
  197. // ------------------------------------------------------------------------------------------------
  198. // Represents an import request
  199. struct LoadRequest
  200. {
  201. LoadRequest(const std::string& _file, unsigned int _flags,const BatchLoader::PropertyMap* _map, unsigned int _id)
  202. : file (_file)
  203. , flags (_flags)
  204. , refCnt (1)
  205. , scene (NULL)
  206. , loaded (false)
  207. , id (_id)
  208. {
  209. if (_map)
  210. map = *_map;
  211. }
  212. const std::string file;
  213. unsigned int flags;
  214. unsigned int refCnt;
  215. aiScene* scene;
  216. bool loaded;
  217. BatchLoader::PropertyMap map;
  218. unsigned int id;
  219. bool operator== (const std::string& f) {
  220. return file == f;
  221. }
  222. };
  223. // ------------------------------------------------------------------------------------------------
  224. // BatchLoader::pimpl data structure
  225. struct Assimp::BatchData
  226. {
  227. BatchData()
  228. : next_id(0xffff)
  229. {}
  230. // IO system to be used for all imports
  231. IOSystem* pIOSystem;
  232. // Importer used to load all meshes
  233. Importer* pImporter;
  234. // List of all imports
  235. std::list<LoadRequest> requests;
  236. // Base path
  237. std::string pathBase;
  238. // Id for next item
  239. unsigned int next_id;
  240. };
  241. // ------------------------------------------------------------------------------------------------
  242. BatchLoader::BatchLoader(IOSystem* pIO)
  243. {
  244. ai_assert(NULL != pIO);
  245. data = new BatchData();
  246. data->pIOSystem = pIO;
  247. data->pImporter = new Importer();
  248. data->pImporter->SetIOHandler(data->pIOSystem);
  249. }
  250. // ------------------------------------------------------------------------------------------------
  251. BatchLoader::~BatchLoader()
  252. {
  253. // delete all scenes wthat have not been polled by the user
  254. for (std::list<LoadRequest>::iterator it = data->requests.begin();it != data->requests.end(); ++it) {
  255. delete (*it).scene;
  256. }
  257. data->pImporter->SetIOHandler(NULL); /* get pointer back into our posession */
  258. delete data->pImporter;
  259. delete data;
  260. }
  261. // ------------------------------------------------------------------------------------------------
  262. unsigned int BatchLoader::AddLoadRequest (const std::string& file,
  263. unsigned int steps /*= 0*/, const PropertyMap* map /*= NULL*/)
  264. {
  265. ai_assert(!file.empty());
  266. // check whether we have this loading request already
  267. std::list<LoadRequest>::iterator it;
  268. for (it = data->requests.begin();it != data->requests.end(); ++it) {
  269. // Call IOSystem's path comparison function here
  270. if (data->pIOSystem->ComparePaths((*it).file,file)) {
  271. if (map) {
  272. if (!((*it).map == *map))
  273. continue;
  274. }
  275. else if (!(*it).map.empty())
  276. continue;
  277. (*it).refCnt++;
  278. return (*it).id;
  279. }
  280. }
  281. // no, we don't have it. So add it to the queue ...
  282. data->requests.push_back(LoadRequest(file,steps,map,data->next_id));
  283. return data->next_id++;
  284. }
  285. // ------------------------------------------------------------------------------------------------
  286. aiScene* BatchLoader::GetImport (unsigned int which)
  287. {
  288. for (std::list<LoadRequest>::iterator it = data->requests.begin();it != data->requests.end(); ++it) {
  289. if ((*it).id == which && (*it).loaded) {
  290. aiScene* sc = (*it).scene;
  291. if (!(--(*it).refCnt)) {
  292. data->requests.erase(it);
  293. }
  294. return sc;
  295. }
  296. }
  297. return NULL;
  298. }
  299. // ------------------------------------------------------------------------------------------------
  300. void BatchLoader::LoadAll()
  301. {
  302. // no threaded implementation for the moment
  303. for (std::list<LoadRequest>::iterator it = data->requests.begin();it != data->requests.end(); ++it) {
  304. // force validation in debug builds
  305. unsigned int pp = (*it).flags;
  306. #ifdef _DEBUG
  307. pp |= aiProcess_ValidateDataStructure;
  308. #endif
  309. // setup config properties if necessary
  310. data->pImporter->pimpl->mFloatProperties = (*it).map.floats;
  311. data->pImporter->pimpl->mIntProperties = (*it).map.ints;
  312. data->pImporter->pimpl->mStringProperties = (*it).map.strings;
  313. if (!DefaultLogger::isNullLogger())
  314. {
  315. DefaultLogger::get()->info("%%% BEGIN EXTERNAL FILE %%%");
  316. DefaultLogger::get()->info("File: " + (*it).file);
  317. }
  318. data->pImporter->ReadFile((*it).file,pp);
  319. (*it).scene = const_cast<aiScene*>(data->pImporter->GetOrphanedScene());
  320. (*it).loaded = true;
  321. DefaultLogger::get()->info("%%% END EXTERNAL FILE %%%");
  322. }
  323. }