BlenderDNA.h 28 KB

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