BaseImporter.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 Implementation of BaseImporter */
  35. #include "AssimpPCH.h"
  36. #include "BaseImporter.h"
  37. using namespace Assimp;
  38. // ------------------------------------------------------------------------------------------------
  39. // Constructor to be privately used by Importer
  40. BaseImporter::BaseImporter()
  41. {
  42. // nothing to do here
  43. }
  44. // ------------------------------------------------------------------------------------------------
  45. // Destructor, private as well
  46. BaseImporter::~BaseImporter()
  47. {
  48. // nothing to do here
  49. }
  50. // ------------------------------------------------------------------------------------------------
  51. // Imports the given file and returns the imported data.
  52. aiScene* BaseImporter::ReadFile( const std::string& pFile, IOSystem* pIOHandler)
  53. {
  54. // create a scene object to hold the data
  55. aiScene* scene = new aiScene();
  56. // dispatch importing
  57. try
  58. {
  59. InternReadFile( pFile, scene, pIOHandler);
  60. } catch( ImportErrorException* exception)
  61. {
  62. // extract error description
  63. mErrorText = exception->GetErrorText();
  64. DefaultLogger::get()->error(mErrorText);
  65. delete exception;
  66. // and kill the partially imported data
  67. delete scene;
  68. scene = NULL;
  69. }
  70. // return what we gathered from the import.
  71. return scene;
  72. }
  73. // ------------------------------------------------------------------------------------------------
  74. void BaseImporter::SetupProperties(const Importer* pImp)
  75. {
  76. // the default implementation does nothing
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. bool BaseImporter::SearchFileHeaderForToken(IOSystem* pIOHandler,
  80. const std::string& pFile,
  81. const char** tokens,
  82. unsigned int numTokens,
  83. unsigned int searchBytes /* = 200 */)
  84. {
  85. ai_assert(NULL != tokens && 0 != numTokens && NULL != pIOHandler && 0 != searchBytes);
  86. boost::scoped_ptr<IOStream> pStream (pIOHandler->Open(pFile));
  87. if (pStream.get() )
  88. {
  89. // read 200 characters from the file
  90. boost::scoped_array<char> _buffer (new char[searchBytes+1 /* for the '\0' */]);
  91. char* buffer = _buffer.get();
  92. unsigned int read = (unsigned int)pStream->Read(buffer,1,searchBytes);
  93. if (!read)return false;
  94. for (unsigned int i = 0; i < read;++i)
  95. buffer[i] = ::tolower(buffer[i]);
  96. // It is not a proper handling of unicode files here ...
  97. // ehm ... but it works in most cases.
  98. char* cur = buffer,*cur2 = buffer,*end = &buffer[read];
  99. while (cur != end)
  100. {
  101. if (*cur)*cur2++ = *cur;
  102. ++cur;
  103. }
  104. *cur2 = '\0';
  105. for (unsigned int i = 0; i < numTokens;++i)
  106. {
  107. ai_assert(NULL != tokens[i]);
  108. if (::strstr(buffer,tokens[i]))return true;
  109. }
  110. }
  111. return false;
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. // Represents an import request
  115. struct LoadRequest
  116. {
  117. LoadRequest(const std::string& _file, unsigned int _flags,const BatchLoader::PropertyMap* _map)
  118. : file (_file)
  119. , flags (_flags)
  120. , scene (NULL)
  121. , refCnt (1)
  122. , loaded (false)
  123. , map (*_map)
  124. {}
  125. const std::string file;
  126. unsigned int flags;
  127. unsigned int refCnt;
  128. aiScene* scene;
  129. bool loaded;
  130. BatchLoader::PropertyMap map;
  131. bool operator== (const std::string& f)
  132. {return file == f;}
  133. };
  134. // ------------------------------------------------------------------------------------------------
  135. // BatchLoader::pimpl data structure
  136. struct BatchData
  137. {
  138. // IO system to be used for all imports
  139. IOSystem* pIOSystem;
  140. // Importer used to load all meshes
  141. Importer* pImporter;
  142. // List of all imports
  143. std::list<LoadRequest> requests;
  144. // Base path
  145. std::string pathBase;
  146. };
  147. // ------------------------------------------------------------------------------------------------
  148. BatchLoader::BatchLoader(IOSystem* pIO)
  149. {
  150. ai_assert(NULL != pIO);
  151. pimpl = new BatchData();
  152. BatchData* data = ( BatchData* )pimpl;
  153. data->pIOSystem = pIO;
  154. data->pImporter = new Importer();
  155. }
  156. // ------------------------------------------------------------------------------------------------
  157. BatchLoader::~BatchLoader()
  158. {
  159. // delete all scenes wthat have not been polled by the user
  160. BatchData* data = ( BatchData* )pimpl;
  161. for (std::list<LoadRequest>::iterator it = data->requests.begin();
  162. it != data->requests.end(); ++it)
  163. {
  164. delete (*it).scene;
  165. }
  166. delete data->pImporter;
  167. delete data;
  168. }
  169. // ------------------------------------------------------------------------------------------------
  170. void BatchLoader::SetBasePath (const std::string& pBase)
  171. {
  172. BatchData* data = ( BatchData* )pimpl;
  173. data->pathBase = pBase;
  174. // file name? we just need the directory
  175. std::string::size_type ss,ss2;
  176. if (std::string::npos != (ss = data->pathBase.find_first_of('.')))
  177. {
  178. if (std::string::npos != (ss2 = data->pathBase.find_last_of('\\')) ||
  179. std::string::npos != (ss2 = data->pathBase.find_last_of('/')))
  180. {
  181. if (ss > ss2)
  182. data->pathBase.erase(ss2,data->pathBase.length()-ss2);
  183. }
  184. else return;
  185. }
  186. // make sure the directory is terminated properly
  187. char s;
  188. if ((s = *(data->pathBase.end()-1)) != '\\' && s != '/')
  189. data->pathBase.append("\\");
  190. }
  191. // ------------------------------------------------------------------------------------------------
  192. void BatchLoader::AddLoadRequest (const std::string& file,
  193. unsigned int steps /*= 0*/, const PropertyMap* map /*= NULL*/)
  194. {
  195. ai_assert(!file.empty());
  196. // no threaded implementation for the moment
  197. BatchData* data = ( BatchData* )pimpl;
  198. std::string real;
  199. // build a full path if this is a relative path and
  200. // we have a new base directory given
  201. if (file.length() > 2 && file[1] != ':' && data->pathBase.length())
  202. {
  203. real = data->pathBase + file;
  204. }
  205. else real = file;
  206. // check whether we have this loading request already
  207. std::list<LoadRequest>::iterator it;
  208. for (it = data->requests.begin();it != data->requests.end(); ++it)
  209. {
  210. // Call IOSystem's path comparison function here
  211. if (data->pIOSystem->ComparePaths((*it).file,real))
  212. {
  213. (*it).refCnt++;
  214. return;
  215. }
  216. }
  217. // no, we don't have it. So add it to the queue ...
  218. data->requests.push_back(LoadRequest(real,steps,map));
  219. }
  220. // ------------------------------------------------------------------------------------------------
  221. aiScene* BatchLoader::GetImport (const std::string& file)
  222. {
  223. // no threaded implementation for the moment
  224. BatchData* data = ( BatchData* )pimpl;
  225. std::string real;
  226. // build a full path if this is a relative path and
  227. // we have a new base directory given
  228. if (file.length() > 2 && file[1] != ':' && data->pathBase.length())
  229. {
  230. real = data->pathBase + file;
  231. }
  232. else real = file;
  233. for (std::list<LoadRequest>::iterator it = data->requests.begin();it != data->requests.end(); ++it)
  234. {
  235. // Call IOSystem's path comparison function here
  236. if (data->pIOSystem->ComparePaths((*it).file,real) && (*it).loaded)
  237. {
  238. aiScene* sc = (*it).scene;
  239. if (!(--(*it).refCnt))
  240. {
  241. data->requests.erase(it);
  242. }
  243. return sc;
  244. }
  245. }
  246. return NULL;
  247. }
  248. // ------------------------------------------------------------------------------------------------
  249. void BatchLoader::LoadAll()
  250. {
  251. BatchData* data = ( BatchData* )pimpl;
  252. // no threaded implementation for the moment
  253. for (std::list<LoadRequest>::iterator it = data->requests.begin();
  254. it != data->requests.end(); ++it)
  255. {
  256. // force validation in debug builds
  257. unsigned int pp = (*it).flags;
  258. #ifdef _DEBUG
  259. pp |= aiProcess_ValidateDataStructure;
  260. #endif
  261. // setup config properties if necessary
  262. data->pImporter->mFloatProperties = (*it).map.floats;
  263. data->pImporter->mIntProperties = (*it).map.ints;
  264. data->pImporter->mStringProperties = (*it).map.strings;
  265. if (!DefaultLogger::isNullLogger())
  266. {
  267. DefaultLogger::get()->info("%%% BEGIN EXTERNAL FILE %%%");
  268. DefaultLogger::get()->info("File: " + (*it).file);
  269. }
  270. data->pImporter->ReadFile((*it).file,pp);
  271. (*it).scene = const_cast<aiScene*>(data->pImporter->GetOrphanedScene());
  272. (*it).loaded = true;
  273. DefaultLogger::get()->info("%%% END EXTERNAL FILE %%%");
  274. }
  275. }