BlenderDNA.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2015, 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. // Modified by Lasse Oorni for Urho3D
  34. /** @file BlenderDNA.h
  35. * @brief Blender `DNA` (file format specification embedded in
  36. * blend file itself) loader.
  37. */
  38. #ifndef INCLUDED_AI_BLEND_DNA_H
  39. #define INCLUDED_AI_BLEND_DNA_H
  40. #include "BaseImporter.h"
  41. #include "TinyFormatter.h"
  42. #include "StreamReader.h"
  43. #include "../include/assimp/DefaultLogger.hpp"
  44. // Urho3D: VS2008 compatibility
  45. #if !defined(_MSC_VER) || (_MSC_VER >= 1600)
  46. #include <stdint.h>
  47. #else
  48. #include "../include/assimp/Compiler/pstdint.h"
  49. #endif
  50. #include <boost/shared_ptr.hpp>
  51. // enable verbose log output. really verbose, so be careful.
  52. #ifdef ASSIMP_BUILD_DEBUG
  53. # define ASSIMP_BUILD_BLENDER_DEBUG
  54. #endif
  55. // #define ASSIMP_BUILD_BLENDER_NO_STATS
  56. namespace Assimp {
  57. template <bool,bool> class StreamReader;
  58. typedef StreamReader<true,true> StreamReaderAny;
  59. namespace Blender {
  60. class FileDatabase;
  61. struct FileBlockHead;
  62. template <template <typename> class TOUT>
  63. class ObjectCache;
  64. // -------------------------------------------------------------------------------
  65. /** Exception class used by the blender loader to selectively catch exceptions
  66. * thrown in its own code (DeadlyImportErrors thrown in general utility
  67. * functions are untouched then). If such an exception is not caught by
  68. * the loader itself, it will still be caught by Assimp due to its
  69. * ancestry. */
  70. // -------------------------------------------------------------------------------
  71. struct Error : DeadlyImportError
  72. {
  73. Error (const std::string& s)
  74. : DeadlyImportError(s)
  75. {}
  76. };
  77. // -------------------------------------------------------------------------------
  78. /** The only purpose of this structure is to feed a virtual dtor into its
  79. * descendents. It serves as base class for all data structure fields. */
  80. // -------------------------------------------------------------------------------
  81. struct ElemBase
  82. {
  83. virtual ~ElemBase() {}
  84. /** Type name of the element. The type
  85. * string points is the `c_str` of the `name` attribute of the
  86. * corresponding `Structure`, that is, it is only valid as long
  87. * as the DNA is not modified. The dna_type is only set if the
  88. * data type is not static, i.e. a boost::shared_ptr<ElemBase>
  89. * in the scene description would have its type resolved
  90. * at runtime, so this member is always set. */
  91. const char* dna_type;
  92. };
  93. // -------------------------------------------------------------------------------
  94. /** Represents a generic pointer to a memory location, which can be either 32
  95. * or 64 bits. These pointers are loaded from the BLEND file and finally
  96. * fixed to point to the real, converted representation of the objects
  97. * they used to point to.*/
  98. // -------------------------------------------------------------------------------
  99. struct Pointer
  100. {
  101. Pointer() : val() {}
  102. uint64_t val;
  103. };
  104. // -------------------------------------------------------------------------------
  105. /** Represents a generic offset within a BLEND file */
  106. // -------------------------------------------------------------------------------
  107. struct FileOffset
  108. {
  109. FileOffset() : val() {}
  110. uint64_t val;
  111. };
  112. // -------------------------------------------------------------------------------
  113. /** Dummy derivate of std::vector to be able to use it in templates simultaenously
  114. * with boost::shared_ptr, which takes only one template argument
  115. * while std::vector takes three. Also we need to provide some special member
  116. * functions of shared_ptr */
  117. // -------------------------------------------------------------------------------
  118. template <typename T>
  119. class vector : public std::vector<T>
  120. {
  121. public:
  122. using std::vector<T>::resize;
  123. using std::vector<T>::empty;
  124. void reset() {
  125. resize(0);
  126. }
  127. operator bool () const {
  128. return !empty();
  129. }
  130. };
  131. // -------------------------------------------------------------------------------
  132. /** Mixed flags for use in #Field */
  133. // -------------------------------------------------------------------------------
  134. enum FieldFlags
  135. {
  136. FieldFlag_Pointer = 0x1,
  137. FieldFlag_Array = 0x2
  138. };
  139. // -------------------------------------------------------------------------------
  140. /** Represents a single member of a data structure in a BLEND file */
  141. // -------------------------------------------------------------------------------
  142. struct Field
  143. {
  144. std::string name;
  145. std::string type;
  146. size_t size;
  147. size_t offset;
  148. /** Size of each array dimension. For flat arrays,
  149. * the second dimension is set to 1. */
  150. size_t array_sizes[2];
  151. /** Any of the #FieldFlags enumerated values */
  152. unsigned int flags;
  153. };
  154. // -------------------------------------------------------------------------------
  155. /** Range of possible behaviours for fields absend in the input file. Some are
  156. * mission critical so we need them, while others can silently be default
  157. * initialized and no animations are harmed. */
  158. // -------------------------------------------------------------------------------
  159. enum ErrorPolicy
  160. {
  161. /** Substitute default value and ignore */
  162. ErrorPolicy_Igno,
  163. /** Substitute default value and write to log */
  164. ErrorPolicy_Warn,
  165. /** Substitute a massive error message and crash the whole matrix. Its time for another zion */
  166. ErrorPolicy_Fail
  167. };
  168. #ifdef ASSIMP_BUILD_BLENDER_DEBUG
  169. # define ErrorPolicy_Igno ErrorPolicy_Warn
  170. #endif
  171. // -------------------------------------------------------------------------------
  172. /** Represents a data structure in a BLEND file. A Structure defines n fields
  173. * and their locatios and encodings the input stream. Usually, every
  174. * Structure instance pertains to one equally-named data structure in the
  175. * BlenderScene.h header. This class defines various utilities to map a
  176. * binary `blob` read from the file to such a structure instance with
  177. * meaningful contents. */
  178. // -------------------------------------------------------------------------------
  179. class Structure
  180. {
  181. template <template <typename> class> friend class ObjectCache;
  182. public:
  183. Structure()
  184. : cache_idx(-1)
  185. {}
  186. public:
  187. // publicly accessible members
  188. std::string name;
  189. vector< Field > fields;
  190. std::map<std::string, size_t> indices;
  191. size_t size;
  192. public:
  193. // --------------------------------------------------------
  194. /** Access a field of the structure by its canonical name. The pointer version
  195. * returns NULL on failure while the reference version raises an import error. */
  196. inline const Field& operator [] (const std::string& ss) const;
  197. inline const Field* Get (const std::string& ss) const;
  198. // --------------------------------------------------------
  199. /** Access a field of the structure by its index */
  200. inline const Field& operator [] (const size_t i) const;
  201. // --------------------------------------------------------
  202. inline bool operator== (const Structure& other) const {
  203. return name == other.name; // name is meant to be an unique identifier
  204. }
  205. // --------------------------------------------------------
  206. inline bool operator!= (const Structure& other) const {
  207. return name != other.name;
  208. }
  209. public:
  210. // --------------------------------------------------------
  211. /** Try to read an instance of the structure from the stream
  212. * and attempt to convert to `T`. This is done by
  213. * an appropriate specialization. If none is available,
  214. * a compiler complain is the result.
  215. * @param dest Destination value to be written
  216. * @param db File database, including input stream. */
  217. template <typename T> inline void Convert (T& dest,
  218. const FileDatabase& db) const;
  219. // --------------------------------------------------------
  220. // generic converter
  221. template <typename T>
  222. void Convert(boost::shared_ptr<ElemBase> in,const FileDatabase& db) const;
  223. // --------------------------------------------------------
  224. // generic allocator
  225. template <typename T> boost::shared_ptr<ElemBase> Allocate() const;
  226. // --------------------------------------------------------
  227. // field parsing for 1d arrays
  228. template <int error_policy, typename T, size_t M>
  229. void ReadFieldArray(T (& out)[M], const char* name,
  230. const FileDatabase& db) const;
  231. // --------------------------------------------------------
  232. // field parsing for 2d arrays
  233. template <int error_policy, typename T, size_t M, size_t N>
  234. void ReadFieldArray2(T (& out)[M][N], const char* name,
  235. const FileDatabase& db) const;
  236. // --------------------------------------------------------
  237. // field parsing for pointer or dynamic array types
  238. // (boost::shared_ptr or boost::shared_array)
  239. // The return value indicates whether the data was already cached.
  240. template <int error_policy, template <typename> class TOUT, typename T>
  241. bool ReadFieldPtr(TOUT<T>& out, const char* name,
  242. const FileDatabase& db,
  243. bool non_recursive = false) const;
  244. // --------------------------------------------------------
  245. // field parsing for static arrays of pointer or dynamic
  246. // array types (boost::shared_ptr[] or boost::shared_array[])
  247. // The return value indicates whether the data was already cached.
  248. template <int error_policy, template <typename> class TOUT, typename T, size_t N>
  249. bool ReadFieldPtr(TOUT<T> (&out)[N], const char* name,
  250. const FileDatabase& db) const;
  251. // --------------------------------------------------------
  252. // field parsing for `normal` values
  253. // The return value indicates whether the data was already cached.
  254. template <int error_policy, typename T>
  255. void ReadField(T& out, const char* name,
  256. const FileDatabase& db) const;
  257. private:
  258. // --------------------------------------------------------
  259. template <template <typename> class TOUT, typename T>
  260. bool ResolvePointer(TOUT<T>& out, const Pointer & ptrval,
  261. const FileDatabase& db, const Field& f,
  262. bool non_recursive = false) const;
  263. // --------------------------------------------------------
  264. template <template <typename> class TOUT, typename T>
  265. bool ResolvePointer(vector< TOUT<T> >& out, const Pointer & ptrval,
  266. const FileDatabase& db, const Field& f, bool) const;
  267. // --------------------------------------------------------
  268. bool ResolvePointer( boost::shared_ptr< FileOffset >& out, const Pointer & ptrval,
  269. const FileDatabase& db, const Field& f, bool) const;
  270. // --------------------------------------------------------
  271. inline const FileBlockHead* LocateFileBlockForAddress(
  272. const Pointer & ptrval,
  273. const FileDatabase& db) const;
  274. private:
  275. // ------------------------------------------------------------------------------
  276. template <typename T> T* _allocate(boost::shared_ptr<T>& out, size_t& s) const {
  277. out = boost::shared_ptr<T>(new T());
  278. s = 1;
  279. return out.get();
  280. }
  281. template <typename T> T* _allocate(vector<T>& out, size_t& s) const {
  282. out.resize(s);
  283. return s ? &out.front() : NULL;
  284. }
  285. // --------------------------------------------------------
  286. template <int error_policy>
  287. struct _defaultInitializer {
  288. template <typename T, unsigned int N>
  289. void operator ()(T (& out)[N], const char* = NULL) {
  290. for (unsigned int i = 0; i < N; ++i) {
  291. out[i] = T();
  292. }
  293. }
  294. template <typename T, unsigned int N, unsigned int M>
  295. void operator ()(T (& out)[N][M], const char* = NULL) {
  296. for (unsigned int i = 0; i < N; ++i) {
  297. for (unsigned int j = 0; j < M; ++j) {
  298. out[i][j] = T();
  299. }
  300. }
  301. }
  302. template <typename T>
  303. void operator ()(T& out, const char* = NULL) {
  304. out = T();
  305. }
  306. };
  307. private:
  308. mutable size_t cache_idx;
  309. };
  310. // --------------------------------------------------------
  311. template <> struct Structure :: _defaultInitializer<ErrorPolicy_Warn> {
  312. template <typename T>
  313. void operator ()(T& out, const char* reason = "<add reason>") {
  314. DefaultLogger::get()->warn(reason);
  315. // ... and let the show go on
  316. _defaultInitializer<0 /*ErrorPolicy_Igno*/>()(out);
  317. }
  318. };
  319. template <> struct Structure :: _defaultInitializer<ErrorPolicy_Fail> {
  320. template <typename T>
  321. void operator ()(T& /*out*/,const char* = "") {
  322. // obviously, it is crucial that _DefaultInitializer is used
  323. // only from within a catch clause.
  324. throw;
  325. }
  326. };
  327. // -------------------------------------------------------------------------------------------------------
  328. template <> inline bool Structure :: ResolvePointer<boost::shared_ptr,ElemBase>(boost::shared_ptr<ElemBase>& out,
  329. const Pointer & ptrval,
  330. const FileDatabase& db,
  331. const Field& f,
  332. bool
  333. ) const;
  334. // -------------------------------------------------------------------------------
  335. /** Represents the full data structure information for a single BLEND file.
  336. * This data is extracted from the DNA1 chunk in the file.
  337. * #DNAParser does the reading and represents currently the only place where
  338. * DNA is altered.*/
  339. // -------------------------------------------------------------------------------
  340. class DNA
  341. {
  342. public:
  343. typedef void (Structure::*ConvertProcPtr) (
  344. boost::shared_ptr<ElemBase> in,
  345. const FileDatabase&
  346. ) const;
  347. typedef boost::shared_ptr<ElemBase> (
  348. Structure::*AllocProcPtr) () const;
  349. typedef std::pair< AllocProcPtr, ConvertProcPtr > FactoryPair;
  350. public:
  351. std::map<std::string, FactoryPair > converters;
  352. vector<Structure > structures;
  353. std::map<std::string, size_t> indices;
  354. public:
  355. // --------------------------------------------------------
  356. /** Access a structure by its canonical name, the pointer version returns NULL on failure
  357. * while the reference version raises an error. */
  358. inline const Structure& operator [] (const std::string& ss) const;
  359. inline const Structure* Get (const std::string& ss) const;
  360. // --------------------------------------------------------
  361. /** Access a structure by its index */
  362. inline const Structure& operator [] (const size_t i) const;
  363. public:
  364. // --------------------------------------------------------
  365. /** Add structure definitions for all the primitive types,
  366. * i.e. integer, short, char, float */
  367. void AddPrimitiveStructures();
  368. // --------------------------------------------------------
  369. /** Fill the @c converters member with converters for all
  370. * known data types. The implementation of this method is
  371. * in BlenderScene.cpp and is machine-generated.
  372. * Converters are used to quickly handle objects whose
  373. * exact data type is a runtime-property and not yet
  374. * known at compile time (consier Object::data).*/
  375. void RegisterConverters();
  376. // --------------------------------------------------------
  377. /** Take an input blob from the stream, interpret it according to
  378. * a its structure name and convert it to the intermediate
  379. * representation.
  380. * @param structure Destination structure definition
  381. * @param db File database.
  382. * @return A null pointer if no appropriate converter is available.*/
  383. boost::shared_ptr< ElemBase > ConvertBlobToStructure(
  384. const Structure& structure,
  385. const FileDatabase& db
  386. ) const;
  387. // --------------------------------------------------------
  388. /** Find a suitable conversion function for a given Structure.
  389. * Such a converter function takes a blob from the input
  390. * stream, reads as much as it needs, and builds up a
  391. * complete object in intermediate representation.
  392. * @param structure Destination structure definition
  393. * @param db File database.
  394. * @return A null pointer in .first if no appropriate converter is available.*/
  395. FactoryPair GetBlobToStructureConverter(
  396. const Structure& structure,
  397. const FileDatabase& db
  398. ) const;
  399. #ifdef ASSIMP_BUILD_BLENDER_DEBUG
  400. // --------------------------------------------------------
  401. /** Dump the DNA to a text file. This is for debugging purposes.
  402. * The output file is `dna.txt` in the current working folder*/
  403. void DumpToFile();
  404. #endif
  405. // --------------------------------------------------------
  406. /** Extract array dimensions from a C array declaration, such
  407. * as `...[4][6]`. Returned string would be `...[][]`.
  408. * @param out
  409. * @param array_sizes Receive maximally two array dimensions,
  410. * the second element is set to 1 if the array is flat.
  411. * Both are set to 1 if the input is not an array.
  412. * @throw DeadlyImportError if more than 2 dimensions are
  413. * encountered. */
  414. static void ExtractArraySize(
  415. const std::string& out,
  416. size_t array_sizes[2]
  417. );
  418. };
  419. // special converters for primitive types
  420. template <> inline void Structure :: Convert<int> (int& dest,const FileDatabase& db) const;
  421. template <> inline void Structure :: Convert<short> (short& dest,const FileDatabase& db) const;
  422. template <> inline void Structure :: Convert<char> (char& dest,const FileDatabase& db) const;
  423. template <> inline void Structure :: Convert<float> (float& dest,const FileDatabase& db) const;
  424. template <> inline void Structure :: Convert<double> (double& dest,const FileDatabase& db) const;
  425. template <> inline void Structure :: Convert<Pointer> (Pointer& dest,const FileDatabase& db) const;
  426. // -------------------------------------------------------------------------------
  427. /** Describes a master file block header. Each master file sections holds n
  428. * elements of a certain SDNA structure (or otherwise unspecified data). */
  429. // -------------------------------------------------------------------------------
  430. struct FileBlockHead
  431. {
  432. // points right after the header of the file block
  433. StreamReaderAny::pos start;
  434. std::string id;
  435. size_t size;
  436. // original memory address of the data
  437. Pointer address;
  438. // index into DNA
  439. unsigned int dna_index;
  440. // number of structure instances to follow
  441. size_t num;
  442. // file blocks are sorted by address to quickly locate specific memory addresses
  443. bool operator < (const FileBlockHead& o) const {
  444. return address.val < o.address.val;
  445. }
  446. // for std::upper_bound
  447. operator const Pointer& () const {
  448. return address;
  449. }
  450. };
  451. // for std::upper_bound
  452. inline bool operator< (const Pointer& a, const Pointer& b) {
  453. return a.val < b.val;
  454. }
  455. // -------------------------------------------------------------------------------
  456. /** Utility to read all master file blocks in turn. */
  457. // -------------------------------------------------------------------------------
  458. class SectionParser
  459. {
  460. public:
  461. // --------------------------------------------------------
  462. /** @param stream Inout stream, must point to the
  463. * first section in the file. Call Next() once
  464. * to have it read.
  465. * @param ptr64 Pointer size in file is 64 bits? */
  466. SectionParser(StreamReaderAny& stream,bool ptr64)
  467. : stream(stream)
  468. , ptr64(ptr64)
  469. {
  470. current.size = current.start = 0;
  471. }
  472. public:
  473. // --------------------------------------------------------
  474. const FileBlockHead& GetCurrent() const {
  475. return current;
  476. }
  477. public:
  478. // --------------------------------------------------------
  479. /** Advance to the next section.
  480. * @throw DeadlyImportError if the last chunk was passed. */
  481. void Next();
  482. public:
  483. FileBlockHead current;
  484. StreamReaderAny& stream;
  485. bool ptr64;
  486. };
  487. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  488. // -------------------------------------------------------------------------------
  489. /** Import statistics, i.e. number of file blocks read*/
  490. // -------------------------------------------------------------------------------
  491. class Statistics {
  492. public:
  493. Statistics ()
  494. : fields_read ()
  495. , pointers_resolved ()
  496. , cache_hits ()
  497. // , blocks_read ()
  498. , cached_objects ()
  499. {}
  500. public:
  501. /** total number of fields we read */
  502. unsigned int fields_read;
  503. /** total number of resolved pointers */
  504. unsigned int pointers_resolved;
  505. /** number of pointers resolved from the cache */
  506. unsigned int cache_hits;
  507. /** number of blocks (from FileDatabase::entries)
  508. we did actually read from. */
  509. // unsigned int blocks_read;
  510. /** objects in FileData::cache */
  511. unsigned int cached_objects;
  512. };
  513. #endif
  514. // -------------------------------------------------------------------------------
  515. /** The object cache - all objects addressed by pointers are added here. This
  516. * avoids circular references and avoids object duplication. */
  517. // -------------------------------------------------------------------------------
  518. template <template <typename> class TOUT>
  519. class ObjectCache
  520. {
  521. public:
  522. typedef std::map< Pointer, TOUT<ElemBase> > StructureCache;
  523. public:
  524. ObjectCache(const FileDatabase& db)
  525. : db(db)
  526. {
  527. // currently there are only ~400 structure records per blend file.
  528. // we read only a small part of them and don't cache objects
  529. // which we don't need, so this should suffice.
  530. caches.reserve(64);
  531. }
  532. public:
  533. // --------------------------------------------------------
  534. /** Check whether a specific item is in the cache.
  535. * @param s Data type of the item
  536. * @param out Output pointer. Unchanged if the
  537. * cache doens't know the item yet.
  538. * @param ptr Item address to look for. */
  539. template <typename T> void get (
  540. const Structure& s,
  541. TOUT<T>& out,
  542. const Pointer& ptr) const;
  543. // --------------------------------------------------------
  544. /** Add an item to the cache after the item has
  545. * been fully read. Do not insert anything that
  546. * may be faulty or might cause the loading
  547. * to abort.
  548. * @param s Data type of the item
  549. * @param out Item to insert into the cache
  550. * @param ptr address (cache key) of the item. */
  551. template <typename T> void set
  552. (const Structure& s,
  553. const TOUT<T>& out,
  554. const Pointer& ptr);
  555. private:
  556. mutable vector<StructureCache> caches;
  557. const FileDatabase& db;
  558. };
  559. // -------------------------------------------------------------------------------
  560. // -------------------------------------------------------------------------------
  561. template <> class ObjectCache<Blender::vector>
  562. {
  563. public:
  564. ObjectCache(const FileDatabase&) {}
  565. template <typename T> void get(const Structure&, vector<T>&, const Pointer&) {}
  566. template <typename T> void set(const Structure&, const vector<T>&, const Pointer&) {}
  567. };
  568. #ifdef _MSC_VER
  569. # pragma warning(disable:4355)
  570. #endif
  571. // -------------------------------------------------------------------------------
  572. /** Memory representation of a full BLEND file and all its dependencies. The
  573. * output aiScene is constructed from an instance of this data structure. */
  574. // -------------------------------------------------------------------------------
  575. class FileDatabase
  576. {
  577. template <template <typename> class TOUT> friend class ObjectCache;
  578. public:
  579. FileDatabase()
  580. : _cacheArrays(*this)
  581. , _cache(*this)
  582. , next_cache_idx()
  583. {}
  584. public:
  585. // publicly accessible fields
  586. bool i64bit;
  587. bool little;
  588. DNA dna;
  589. boost::shared_ptr< StreamReaderAny > reader;
  590. vector< FileBlockHead > entries;
  591. public:
  592. Statistics& stats() const {
  593. return _stats;
  594. }
  595. // For all our templates to work on both shared_ptr's and vector's
  596. // using the same code, a dummy cache for arrays is provided. Actually,
  597. // arrays of objects are never cached because we can't easily
  598. // ensure their proper destruction.
  599. template <typename T>
  600. ObjectCache<boost::shared_ptr>& cache(boost::shared_ptr<T>& /*in*/) const {
  601. return _cache;
  602. }
  603. template <typename T>
  604. ObjectCache<vector>& cache(vector<T>& /*in*/) const {
  605. return _cacheArrays;
  606. }
  607. private:
  608. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  609. mutable Statistics _stats;
  610. #endif
  611. mutable ObjectCache<vector> _cacheArrays;
  612. mutable ObjectCache<boost::shared_ptr> _cache;
  613. mutable size_t next_cache_idx;
  614. };
  615. #ifdef _MSC_VER
  616. # pragma warning(default:4355)
  617. #endif
  618. // -------------------------------------------------------------------------------
  619. /** Factory to extract a #DNA from the DNA1 file block in a BLEND file. */
  620. // -------------------------------------------------------------------------------
  621. class DNAParser
  622. {
  623. public:
  624. /** Bind the parser to a empty DNA and an input stream */
  625. DNAParser(FileDatabase& db)
  626. : db(db)
  627. {}
  628. public:
  629. // --------------------------------------------------------
  630. /** Locate the DNA in the file and parse it. The input
  631. * stream is expected to point to the beginning of the DN1
  632. * chunk at the time this method is called and is
  633. * undefined afterwards.
  634. * @throw DeadlyImportError if the DNA cannot be read.
  635. * @note The position of the stream pointer is undefined
  636. * afterwards.*/
  637. void Parse ();
  638. public:
  639. /** Obtain a reference to the extracted DNA information */
  640. const Blender::DNA& GetDNA() const {
  641. return db.dna;
  642. }
  643. private:
  644. FileDatabase& db;
  645. };
  646. } // end Blend
  647. } // end Assimp
  648. #include "BlenderDNA.inl"
  649. #endif