BlenderDNA.h 27 KB

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