BlenderDNA.inl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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.inl
  34. * @brief Blender `DNA` (file format specification embedded in
  35. * blend file itself) loader.
  36. */
  37. #ifndef INCLUDED_AI_BLEND_DNA_INL
  38. #define INCLUDED_AI_BLEND_DNA_INL
  39. #include <memory>
  40. #include <assimp/TinyFormatter.h>
  41. namespace Assimp {
  42. namespace Blender {
  43. //--------------------------------------------------------------------------------
  44. const Field& Structure :: operator [] (const std::string& ss) const
  45. {
  46. std::map<std::string, size_t>::const_iterator it = indices.find(ss);
  47. if (it == indices.end()) {
  48. throw Error("BlendDNA: Did not find a field named `",ss,"` in structure `",name,"`");
  49. }
  50. return fields[(*it).second];
  51. }
  52. //--------------------------------------------------------------------------------
  53. const Field* Structure :: Get (const std::string& ss) const
  54. {
  55. std::map<std::string, size_t>::const_iterator it = indices.find(ss);
  56. return it == indices.end() ? nullptr : &fields[(*it).second];
  57. }
  58. //--------------------------------------------------------------------------------
  59. const Field& Structure :: operator [] (const size_t i) const
  60. {
  61. if (i >= fields.size()) {
  62. throw Error("BlendDNA: There is no field with index `",i,"` in structure `",name,"`");
  63. }
  64. return fields[i];
  65. }
  66. //--------------------------------------------------------------------------------
  67. template <typename T> std::shared_ptr<ElemBase> Structure :: Allocate() const
  68. {
  69. return std::shared_ptr<T>(new T());
  70. }
  71. //--------------------------------------------------------------------------------
  72. template <typename T> void Structure :: Convert(
  73. std::shared_ptr<ElemBase> in,
  74. const FileDatabase& db) const
  75. {
  76. Convert<T> (*static_cast<T*> ( in.get() ),db);
  77. }
  78. //--------------------------------------------------------------------------------
  79. template <int error_policy, typename T, size_t M>
  80. void Structure :: ReadFieldArray(T (& out)[M], const char* name, const FileDatabase& db) const
  81. {
  82. const StreamReaderAny::pos old = db.reader->GetCurrentPos();
  83. try {
  84. const Field& f = (*this)[name];
  85. const Structure& s = db.dna[f.type];
  86. // is the input actually an array?
  87. if (!(f.flags & FieldFlag_Array)) {
  88. throw Error("Field `",name,"` of structure `",this->name,"` ought to be an array of size ",M);
  89. }
  90. db.reader->IncPtr(f.offset);
  91. // size conversions are always allowed, regardless of error_policy
  92. unsigned int i = 0;
  93. for(; i < std::min(f.array_sizes[0],M); ++i) {
  94. s.Convert(out[i],db);
  95. }
  96. for(; i < M; ++i) {
  97. _defaultInitializer<ErrorPolicy_Igno>()(out[i]);
  98. }
  99. }
  100. catch (const Error& e) {
  101. _defaultInitializer<error_policy>()(out,e.what());
  102. }
  103. // and recover the previous stream position
  104. db.reader->SetCurrentPos(old);
  105. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  106. ++db.stats().fields_read;
  107. #endif
  108. }
  109. //--------------------------------------------------------------------------------
  110. template <int error_policy, typename T, size_t M, size_t N>
  111. void Structure :: ReadFieldArray2(T (& out)[M][N], const char* name, const FileDatabase& db) const
  112. {
  113. const StreamReaderAny::pos old = db.reader->GetCurrentPos();
  114. try {
  115. const Field& f = (*this)[name];
  116. const Structure& s = db.dna[f.type];
  117. // is the input actually an array?
  118. if (!(f.flags & FieldFlag_Array)) {
  119. throw Error("Field `",name,"` of structure `",
  120. this->name,"` ought to be an array of size ",M,"*",N
  121. );
  122. }
  123. db.reader->IncPtr(f.offset);
  124. // size conversions are always allowed, regardless of error_policy
  125. unsigned int i = 0;
  126. for(; i < std::min(f.array_sizes[0],M); ++i) {
  127. unsigned int j = 0;
  128. for(; j < std::min(f.array_sizes[1],N); ++j) {
  129. s.Convert(out[i][j],db);
  130. }
  131. for(; j < N; ++j) {
  132. _defaultInitializer<ErrorPolicy_Igno>()(out[i][j]);
  133. }
  134. }
  135. for(; i < M; ++i) {
  136. _defaultInitializer<ErrorPolicy_Igno>()(out[i]);
  137. }
  138. }
  139. catch (const Error& e) {
  140. _defaultInitializer<error_policy>()(out,e.what());
  141. }
  142. // and recover the previous stream position
  143. db.reader->SetCurrentPos(old);
  144. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  145. ++db.stats().fields_read;
  146. #endif
  147. }
  148. //--------------------------------------------------------------------------------
  149. template <int error_policy, template <typename> class TOUT, typename T>
  150. bool Structure :: ReadFieldPtr(TOUT<T>& out, const char* name, const FileDatabase& db,
  151. bool non_recursive /*= false*/) const
  152. {
  153. const StreamReaderAny::pos old = db.reader->GetCurrentPos();
  154. Pointer ptrval;
  155. const Field* f;
  156. try {
  157. f = &(*this)[name];
  158. // sanity check, should never happen if the genblenddna script is right
  159. if (!(f->flags & FieldFlag_Pointer)) {
  160. throw Error("Field `",name,"` of structure `",
  161. this->name,"` ought to be a pointer");
  162. }
  163. db.reader->IncPtr(f->offset);
  164. Convert(ptrval,db);
  165. // actually it is meaningless on which Structure the Convert is called
  166. // because the `Pointer` argument triggers a special implementation.
  167. }
  168. catch (const Error& e) {
  169. _defaultInitializer<error_policy>()(out,e.what());
  170. out.reset();
  171. return false;
  172. }
  173. // resolve the pointer and load the corresponding structure
  174. const bool res = ResolvePointer(out,ptrval,db,*f, non_recursive);
  175. if(!non_recursive) {
  176. // and recover the previous stream position
  177. db.reader->SetCurrentPos(old);
  178. }
  179. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  180. ++db.stats().fields_read;
  181. #endif
  182. return res;
  183. }
  184. //--------------------------------------------------------------------------------
  185. template <int error_policy, template <typename> class TOUT, typename T, size_t N>
  186. bool Structure :: ReadFieldPtr(TOUT<T> (&out)[N], const char* name,
  187. const FileDatabase& db) const
  188. {
  189. // XXX see if we can reduce this to call to the 'normal' ReadFieldPtr
  190. const StreamReaderAny::pos old = db.reader->GetCurrentPos();
  191. Pointer ptrval[N];
  192. const Field* f;
  193. try {
  194. f = &(*this)[name];
  195. #ifdef _DEBUG
  196. // sanity check, should never happen if the genblenddna script is right
  197. if ((FieldFlag_Pointer|FieldFlag_Pointer) != (f->flags & (FieldFlag_Pointer|FieldFlag_Pointer))) {
  198. throw Error("Field `",name,"` of structure `",
  199. this->name,"` ought to be a pointer AND an array");
  200. }
  201. #endif // _DEBUG
  202. db.reader->IncPtr(f->offset);
  203. size_t i = 0;
  204. for(; i < std::min(f->array_sizes[0],N); ++i) {
  205. Convert(ptrval[i],db);
  206. }
  207. for(; i < N; ++i) {
  208. _defaultInitializer<ErrorPolicy_Igno>()(ptrval[i]);
  209. }
  210. // actually it is meaningless on which Structure the Convert is called
  211. // because the `Pointer` argument triggers a special implementation.
  212. }
  213. catch (const Error& e) {
  214. _defaultInitializer<error_policy>()(out,e.what());
  215. for(size_t i = 0; i < N; ++i) {
  216. out[i].reset();
  217. }
  218. return false;
  219. }
  220. bool res = true;
  221. for(size_t i = 0; i < N; ++i) {
  222. // resolve the pointer and load the corresponding structure
  223. res = ResolvePointer(out[i],ptrval[i],db,*f) && res;
  224. }
  225. // and recover the previous stream position
  226. db.reader->SetCurrentPos(old);
  227. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  228. ++db.stats().fields_read;
  229. #endif
  230. return res;
  231. }
  232. //--------------------------------------------------------------------------------
  233. template <int error_policy, typename T>
  234. void Structure :: ReadField(T& out, const char* name, const FileDatabase& db) const
  235. {
  236. const StreamReaderAny::pos old = db.reader->GetCurrentPos();
  237. try {
  238. const Field& f = (*this)[name];
  239. // find the structure definition pertaining to this field
  240. const Structure& s = db.dna[f.type];
  241. db.reader->IncPtr(f.offset);
  242. s.Convert(out,db);
  243. }
  244. catch (const Error& e) {
  245. _defaultInitializer<error_policy>()(out,e.what());
  246. }
  247. // and recover the previous stream position
  248. db.reader->SetCurrentPos(old);
  249. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  250. ++db.stats().fields_read;
  251. #endif
  252. }
  253. //--------------------------------------------------------------------------------
  254. // field parsing for raw untyped data (like CustomDataLayer.data)
  255. template <int error_policy>
  256. bool Structure::ReadCustomDataPtr(std::shared_ptr<ElemBase>&out, int cdtype, const char* name, const FileDatabase& db) const {
  257. const StreamReaderAny::pos old = db.reader->GetCurrentPos();
  258. Pointer ptrval;
  259. const Field* f;
  260. try {
  261. f = &(*this)[name];
  262. // sanity check, should never happen if the genblenddna script is right
  263. if (!(f->flags & FieldFlag_Pointer)) {
  264. throw Error("Field `", name, "` of structure `",
  265. this->name, "` ought to be a pointer");
  266. }
  267. db.reader->IncPtr(f->offset);
  268. Convert(ptrval, db);
  269. // actually it is meaningless on which Structure the Convert is called
  270. // because the `Pointer` argument triggers a special implementation.
  271. }
  272. catch (const Error& e) {
  273. _defaultInitializer<error_policy>()(out, e.what());
  274. out.reset();
  275. }
  276. bool readOk = true;
  277. if (ptrval.val) {
  278. // get block for ptr
  279. const FileBlockHead* block = LocateFileBlockForAddress(ptrval, db);
  280. db.reader->SetCurrentPos(block->start + static_cast<size_t>((ptrval.val - block->address.val)));
  281. // read block->num instances of given type to out
  282. readOk = readCustomData(out, cdtype, block->num, db);
  283. }
  284. // and recover the previous stream position
  285. db.reader->SetCurrentPos(old);
  286. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  287. ++db.stats().fields_read;
  288. #endif
  289. return readOk;
  290. }
  291. //--------------------------------------------------------------------------------
  292. template <int error_policy, template <typename> class TOUT, typename T>
  293. bool Structure::ReadFieldPtrVector(vector<TOUT<T>>&out, const char* name, const FileDatabase& db) const {
  294. out.clear();
  295. const StreamReaderAny::pos old = db.reader->GetCurrentPos();
  296. Pointer ptrval;
  297. const Field* f;
  298. try {
  299. f = &(*this)[name];
  300. // sanity check, should never happen if the genblenddna script is right
  301. if (!(f->flags & FieldFlag_Pointer)) {
  302. throw Error("Field `", name, "` of structure `",
  303. this->name, "` ought to be a pointer");
  304. }
  305. db.reader->IncPtr(f->offset);
  306. Convert(ptrval, db);
  307. // actually it is meaningless on which Structure the Convert is called
  308. // because the `Pointer` argument triggers a special implementation.
  309. }
  310. catch (const Error& e) {
  311. _defaultInitializer<error_policy>()(out, e.what());
  312. out.clear();
  313. return false;
  314. }
  315. if (ptrval.val) {
  316. // find the file block the pointer is pointing to
  317. const FileBlockHead* block = LocateFileBlockForAddress(ptrval, db);
  318. db.reader->SetCurrentPos(block->start + static_cast<size_t>((ptrval.val - block->address.val)));
  319. // FIXME: basically, this could cause problems with 64 bit pointers on 32 bit systems.
  320. // I really ought to improve StreamReader to work with 64 bit indices exclusively.
  321. const Structure& s = db.dna[f->type];
  322. for (size_t i = 0; i < block->num; ++i) {
  323. TOUT<T> p(new T);
  324. s.Convert(*p, db);
  325. out.push_back(p);
  326. }
  327. }
  328. db.reader->SetCurrentPos(old);
  329. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  330. ++db.stats().fields_read;
  331. #endif
  332. return true;
  333. }
  334. //--------------------------------------------------------------------------------
  335. template <template <typename> class TOUT, typename T>
  336. bool Structure :: ResolvePointer(TOUT<T>& out, const Pointer & ptrval, const FileDatabase& db,
  337. const Field& f,
  338. bool non_recursive /*= false*/) const
  339. {
  340. out.reset(); // ensure null pointers work
  341. if (!ptrval.val) {
  342. return false;
  343. }
  344. const Structure& s = db.dna[f.type];
  345. // find the file block the pointer is pointing to
  346. const FileBlockHead* block = LocateFileBlockForAddress(ptrval,db);
  347. // also determine the target type from the block header
  348. // and check if it matches the type which we expect.
  349. const Structure& ss = db.dna[block->dna_index];
  350. if (ss != s) {
  351. throw Error("Expected target to be of type `",s.name,
  352. "` but seemingly it is a `",ss.name,"` instead"
  353. );
  354. }
  355. // try to retrieve the object from the cache
  356. db.cache(out).get(s,out,ptrval);
  357. if (out) {
  358. return true;
  359. }
  360. // seek to this location, but save the previous stream pointer.
  361. const StreamReaderAny::pos pold = db.reader->GetCurrentPos();
  362. db.reader->SetCurrentPos(block->start+ static_cast<size_t>((ptrval.val - block->address.val) ));
  363. // FIXME: basically, this could cause problems with 64 bit pointers on 32 bit systems.
  364. // I really ought to improve StreamReader to work with 64 bit indices exclusively.
  365. // continue conversion after allocating the required storage
  366. size_t num = block->size / ss.size;
  367. T* o = _allocate(out,num);
  368. // cache the object before we convert it to avoid cyclic recursion.
  369. db.cache(out).set(s,out,ptrval);
  370. // if the non_recursive flag is set, we don't do anything but leave
  371. // the cursor at the correct position to resolve the object.
  372. if (!non_recursive) {
  373. for (size_t i = 0; i < num; ++i,++o) {
  374. s.Convert(*o,db);
  375. }
  376. db.reader->SetCurrentPos(pold);
  377. }
  378. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  379. if(out) {
  380. ++db.stats().pointers_resolved;
  381. }
  382. #endif
  383. return false;
  384. }
  385. //--------------------------------------------------------------------------------
  386. inline bool Structure :: ResolvePointer( std::shared_ptr< FileOffset >& out, const Pointer & ptrval,
  387. const FileDatabase& db,
  388. const Field&,
  389. bool) const
  390. {
  391. // Currently used exclusively by PackedFile::data to represent
  392. // a simple offset into the mapped BLEND file.
  393. out.reset();
  394. if (!ptrval.val) {
  395. return false;
  396. }
  397. // find the file block the pointer is pointing to
  398. const FileBlockHead* block = LocateFileBlockForAddress(ptrval,db);
  399. out = std::shared_ptr< FileOffset > (new FileOffset());
  400. out->val = block->start+ static_cast<size_t>((ptrval.val - block->address.val) );
  401. return false;
  402. }
  403. //--------------------------------------------------------------------------------
  404. template <template <typename> class TOUT, typename T>
  405. bool Structure :: ResolvePointer(vector< TOUT<T> >& out, const Pointer & ptrval,
  406. const FileDatabase& db,
  407. const Field& f,
  408. bool) const
  409. {
  410. // This is a function overload, not a template specialization. According to
  411. // the partial ordering rules, it should be selected by the compiler
  412. // for array-of-pointer inputs, i.e. Object::mats.
  413. out.reset();
  414. if (!ptrval.val) {
  415. return false;
  416. }
  417. // find the file block the pointer is pointing to
  418. const FileBlockHead* block = LocateFileBlockForAddress(ptrval,db);
  419. const size_t num = block->size / (db.i64bit?8:4);
  420. // keep the old stream position
  421. const StreamReaderAny::pos pold = db.reader->GetCurrentPos();
  422. db.reader->SetCurrentPos(block->start+ static_cast<size_t>((ptrval.val - block->address.val) ));
  423. bool res = false;
  424. // allocate raw storage for the array
  425. out.resize(num);
  426. for (size_t i = 0; i< num; ++i) {
  427. Pointer val;
  428. Convert(val,db);
  429. // and resolve the pointees
  430. res = ResolvePointer(out[i],val,db,f) && res;
  431. }
  432. db.reader->SetCurrentPos(pold);
  433. return res;
  434. }
  435. //--------------------------------------------------------------------------------
  436. template <> bool Structure :: ResolvePointer<std::shared_ptr,ElemBase>(std::shared_ptr<ElemBase>& out,
  437. const Pointer & ptrval,
  438. const FileDatabase& db,
  439. const Field&,
  440. bool
  441. ) const
  442. {
  443. // Special case when the data type needs to be determined at runtime.
  444. // Less secure than in the `strongly-typed` case.
  445. out.reset();
  446. if (!ptrval.val) {
  447. return false;
  448. }
  449. // find the file block the pointer is pointing to
  450. const FileBlockHead* block = LocateFileBlockForAddress(ptrval,db);
  451. // determine the target type from the block header
  452. const Structure& s = db.dna[block->dna_index];
  453. // try to retrieve the object from the cache
  454. db.cache(out).get(s,out,ptrval);
  455. if (out) {
  456. return true;
  457. }
  458. // seek to this location, but save the previous stream pointer.
  459. const StreamReaderAny::pos pold = db.reader->GetCurrentPos();
  460. db.reader->SetCurrentPos(block->start+ static_cast<size_t>((ptrval.val - block->address.val) ));
  461. // FIXME: basically, this could cause problems with 64 bit pointers on 32 bit systems.
  462. // I really ought to improve StreamReader to work with 64 bit indices exclusively.
  463. // continue conversion after allocating the required storage
  464. DNA::FactoryPair builders = db.dna.GetBlobToStructureConverter(s,db);
  465. if (!builders.first) {
  466. // this might happen if DNA::RegisterConverters hasn't been called so far
  467. // or if the target type is not contained in `our` DNA.
  468. out.reset();
  469. ASSIMP_LOG_WARN( "Failed to find a converter for the `",s.name,"` structure" );
  470. return false;
  471. }
  472. // allocate the object hull
  473. out = (s.*builders.first)();
  474. // cache the object immediately to prevent infinite recursion in a
  475. // circular list with a single element (i.e. a self-referencing element).
  476. db.cache(out).set(s,out,ptrval);
  477. // and do the actual conversion
  478. (s.*builders.second)(out,db);
  479. db.reader->SetCurrentPos(pold);
  480. // store a pointer to the name string of the actual type
  481. // in the object itself. This allows the conversion code
  482. // to perform additional type checking.
  483. out->dna_type = s.name.c_str();
  484. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  485. ++db.stats().pointers_resolved;
  486. #endif
  487. return false;
  488. }
  489. //--------------------------------------------------------------------------------
  490. const FileBlockHead* Structure :: LocateFileBlockForAddress(const Pointer & ptrval, const FileDatabase& db) const
  491. {
  492. // the file blocks appear in list sorted by
  493. // with ascending base addresses so we can run a
  494. // binary search to locate the pointer quickly.
  495. // NOTE: Blender seems to distinguish between side-by-side
  496. // data (stored in the same data block) and far pointers,
  497. // which are only used for structures starting with an ID.
  498. // We don't need to make this distinction, our algorithm
  499. // works regardless where the data is stored.
  500. vector<FileBlockHead>::const_iterator it = std::lower_bound(db.entries.begin(),db.entries.end(),ptrval);
  501. if (it == db.entries.end()) {
  502. // this is crucial, pointers may not be invalid.
  503. // this is either a corrupted file or an attempted attack.
  504. throw DeadlyImportError("Failure resolving pointer 0x",
  505. std::hex,ptrval.val,", no file block falls into this address range");
  506. }
  507. if (ptrval.val >= (*it).address.val + (*it).size) {
  508. throw DeadlyImportError("Failure resolving pointer 0x",
  509. std::hex,ptrval.val,", nearest file block starting at 0x",
  510. (*it).address.val," ends at 0x",
  511. (*it).address.val + (*it).size);
  512. }
  513. return &*it;
  514. }
  515. // ------------------------------------------------------------------------------------------------
  516. // NOTE: The MSVC debugger keeps showing up this annoying `a cast to a smaller data type has
  517. // caused a loss of data`-warning. Avoid this warning by a masking with an appropriate bitmask.
  518. template <typename T> struct signless;
  519. template <> struct signless<char> {typedef unsigned char type;};
  520. template <> struct signless<short> {typedef unsigned short type;};
  521. template <> struct signless<int> {typedef unsigned int type;};
  522. template <> struct signless<unsigned char> { typedef unsigned char type; };
  523. template <typename T>
  524. struct static_cast_silent {
  525. template <typename V>
  526. T operator()(V in) {
  527. return static_cast<T>(in & static_cast<typename signless<T>::type>(-1));
  528. }
  529. };
  530. template <> struct static_cast_silent<float> {
  531. template <typename V> float operator()(V in) {
  532. return static_cast<float> (in);
  533. }
  534. };
  535. template <> struct static_cast_silent<double> {
  536. template <typename V> double operator()(V in) {
  537. return static_cast<double>(in);
  538. }
  539. };
  540. // ------------------------------------------------------------------------------------------------
  541. template <typename T> inline void ConvertDispatcher(T& out, const Structure& in,const FileDatabase& db)
  542. {
  543. if (in.name == "int") {
  544. out = static_cast_silent<T>()(db.reader->GetU4());
  545. }
  546. else if (in.name == "short") {
  547. out = static_cast_silent<T>()(db.reader->GetU2());
  548. }
  549. else if (in.name == "char") {
  550. out = static_cast_silent<T>()(db.reader->GetU1());
  551. }
  552. else if (in.name == "float") {
  553. out = static_cast<T>(db.reader->GetF4());
  554. }
  555. else if (in.name == "double") {
  556. out = static_cast<T>(db.reader->GetF8());
  557. }
  558. else {
  559. throw DeadlyImportError("Unknown source for conversion to primitive data type: ", in.name);
  560. }
  561. }
  562. // ------------------------------------------------------------------------------------------------
  563. template <> inline void Structure :: Convert<int> (int& dest,const FileDatabase& db) const
  564. {
  565. ConvertDispatcher(dest,*this,db);
  566. }
  567. // ------------------------------------------------------------------------------------------------
  568. template<> inline void Structure :: Convert<short> (short& dest,const FileDatabase& db) const
  569. {
  570. // automatic rescaling from short to float and vice versa (seems to be used by normals)
  571. if (name == "float") {
  572. float f = db.reader->GetF4();
  573. if ( f > 1.0f )
  574. f = 1.0f;
  575. dest = static_cast<short>( f * 32767.f);
  576. //db.reader->IncPtr(-4);
  577. return;
  578. }
  579. else if (name == "double") {
  580. dest = static_cast<short>(db.reader->GetF8() * 32767.);
  581. //db.reader->IncPtr(-8);
  582. return;
  583. }
  584. ConvertDispatcher(dest,*this,db);
  585. }
  586. // ------------------------------------------------------------------------------------------------
  587. template <> inline void Structure :: Convert<char> (char& dest,const FileDatabase& db) const
  588. {
  589. // automatic rescaling from char to float and vice versa (seems useful for RGB colors)
  590. if (name == "float") {
  591. dest = static_cast<char>(db.reader->GetF4() * 255.f);
  592. return;
  593. }
  594. else if (name == "double") {
  595. dest = static_cast<char>(db.reader->GetF8() * 255.f);
  596. return;
  597. }
  598. ConvertDispatcher(dest,*this,db);
  599. }
  600. // ------------------------------------------------------------------------------------------------
  601. template <> inline void Structure::Convert<unsigned char>(unsigned char& dest, const FileDatabase& db) const
  602. {
  603. // automatic rescaling from char to float and vice versa (seems useful for RGB colors)
  604. if (name == "float") {
  605. dest = static_cast<unsigned char>(db.reader->GetF4() * 255.f);
  606. return;
  607. }
  608. else if (name == "double") {
  609. dest = static_cast<unsigned char>(db.reader->GetF8() * 255.f);
  610. return;
  611. }
  612. ConvertDispatcher(dest, *this, db);
  613. }
  614. // ------------------------------------------------------------------------------------------------
  615. template <> inline void Structure :: Convert<float> (float& dest,const FileDatabase& db) const
  616. {
  617. // automatic rescaling from char to float and vice versa (seems useful for RGB colors)
  618. if (name == "char") {
  619. dest = db.reader->GetI1() / 255.f;
  620. return;
  621. }
  622. // automatic rescaling from short to float and vice versa (used by normals)
  623. else if (name == "short") {
  624. dest = db.reader->GetI2() / 32767.f;
  625. return;
  626. }
  627. ConvertDispatcher(dest,*this,db);
  628. }
  629. // ------------------------------------------------------------------------------------------------
  630. template <> inline void Structure :: Convert<double> (double& dest,const FileDatabase& db) const
  631. {
  632. if (name == "char") {
  633. dest = db.reader->GetI1() / 255.;
  634. return;
  635. }
  636. else if (name == "short") {
  637. dest = db.reader->GetI2() / 32767.;
  638. return;
  639. }
  640. ConvertDispatcher(dest,*this,db);
  641. }
  642. // ------------------------------------------------------------------------------------------------
  643. template <> inline void Structure :: Convert<Pointer> (Pointer& dest,const FileDatabase& db) const
  644. {
  645. if (db.i64bit) {
  646. dest.val = db.reader->GetU8();
  647. //db.reader->IncPtr(-8);
  648. return;
  649. }
  650. dest.val = db.reader->GetU4();
  651. //db.reader->IncPtr(-4);
  652. }
  653. //--------------------------------------------------------------------------------
  654. const Structure& DNA :: operator [] (const std::string& ss) const
  655. {
  656. std::map<std::string, size_t>::const_iterator it = indices.find(ss);
  657. if (it == indices.end()) {
  658. throw Error("BlendDNA: Did not find a structure named `",ss,"`");
  659. }
  660. return structures[(*it).second];
  661. }
  662. //--------------------------------------------------------------------------------
  663. const Structure* DNA :: Get (const std::string& ss) const
  664. {
  665. std::map<std::string, size_t>::const_iterator it = indices.find(ss);
  666. return it == indices.end() ? nullptr : &structures[(*it).second];
  667. }
  668. //--------------------------------------------------------------------------------
  669. const Structure& DNA :: operator [] (const size_t i) const
  670. {
  671. if (i >= structures.size()) {
  672. throw Error("BlendDNA: There is no structure with index `",i,"`");
  673. }
  674. return structures[i];
  675. }
  676. //--------------------------------------------------------------------------------
  677. template <template <typename> class TOUT> template <typename T> void ObjectCache<TOUT> :: get (
  678. const Structure& s,
  679. TOUT<T>& out,
  680. const Pointer& ptr
  681. ) const {
  682. if(s.cache_idx == static_cast<size_t>(-1)) {
  683. s.cache_idx = db.next_cache_idx++;
  684. caches.resize(db.next_cache_idx);
  685. return;
  686. }
  687. typename StructureCache::const_iterator it = caches[s.cache_idx].find(ptr);
  688. if (it != caches[s.cache_idx].end()) {
  689. out = std::static_pointer_cast<T>( (*it).second );
  690. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  691. ++db.stats().cache_hits;
  692. #endif
  693. }
  694. // otherwise, out remains untouched
  695. }
  696. //--------------------------------------------------------------------------------
  697. template <template <typename> class TOUT> template <typename T> void ObjectCache<TOUT> :: set (
  698. const Structure& s,
  699. const TOUT<T>& out,
  700. const Pointer& ptr
  701. ) {
  702. if(s.cache_idx == static_cast<size_t>(-1)) {
  703. s.cache_idx = db.next_cache_idx++;
  704. caches.resize(db.next_cache_idx);
  705. }
  706. caches[s.cache_idx][ptr] = std::static_pointer_cast<ElemBase>( out );
  707. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  708. ++db.stats().cached_objects;
  709. #endif
  710. }
  711. }
  712. }
  713. #endif