BlenderDNA.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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.cpp
  34. * @brief Implementation of the Blender `DNA`, that is its own
  35. * serialized set of data structures.
  36. */
  37. #ifndef ASSIMP_BUILD_NO_BLEND_IMPORTER
  38. #include "BlenderDNA.h"
  39. #include <assimp/StreamReader.h>
  40. #include <assimp/TinyFormatter.h>
  41. #include <assimp/fast_atof.h>
  42. using namespace Assimp;
  43. using namespace Assimp::Blender;
  44. using namespace Assimp::Formatter;
  45. static bool match4(StreamReaderAny &stream, const char *string) {
  46. ai_assert(nullptr != string);
  47. char tmp[4];
  48. tmp[0] = (stream).GetI1();
  49. tmp[1] = (stream).GetI1();
  50. tmp[2] = (stream).GetI1();
  51. tmp[3] = (stream).GetI1();
  52. return (tmp[0] == string[0] && tmp[1] == string[1] && tmp[2] == string[2] && tmp[3] == string[3]);
  53. }
  54. struct Type {
  55. size_t size;
  56. std::string name;
  57. };
  58. // ------------------------------------------------------------------------------------------------
  59. void DNAParser::Parse() {
  60. StreamReaderAny &stream = *db.reader;
  61. DNA &dna = db.dna;
  62. if (!match4(stream, "SDNA")) {
  63. throw DeadlyImportError("BlenderDNA: Expected SDNA chunk");
  64. }
  65. // name dictionary
  66. if (!match4(stream, "NAME")) {
  67. throw DeadlyImportError("BlenderDNA: Expected NAME field");
  68. }
  69. std::vector<std::string> names(stream.GetI4());
  70. for (std::string &s : names) {
  71. while (char c = stream.GetI1()) {
  72. s += c;
  73. }
  74. }
  75. // type dictionary
  76. for (; stream.GetCurrentPos() & 0x3; stream.GetI1())
  77. ;
  78. if (!match4(stream, "TYPE")) {
  79. throw DeadlyImportError("BlenderDNA: Expected TYPE field");
  80. }
  81. std::vector<Type> types(stream.GetI4());
  82. for (Type &s : types) {
  83. while (char c = stream.GetI1()) {
  84. s.name += c;
  85. }
  86. }
  87. // type length dictionary
  88. for (; stream.GetCurrentPos() & 0x3; stream.GetI1())
  89. ;
  90. if (!match4(stream, "TLEN")) {
  91. throw DeadlyImportError("BlenderDNA: Expected TLEN field");
  92. }
  93. for (Type &s : types) {
  94. s.size = stream.GetI2();
  95. }
  96. // structures dictionary
  97. for (; stream.GetCurrentPos() & 0x3; stream.GetI1())
  98. ;
  99. if (!match4(stream, "STRC")) {
  100. throw DeadlyImportError("BlenderDNA: Expected STRC field");
  101. }
  102. size_t end = stream.GetI4(), fields = 0;
  103. dna.structures.reserve(end);
  104. for (size_t i = 0; i != end; ++i) {
  105. uint16_t n = stream.GetI2();
  106. if (n >= types.size()) {
  107. throw DeadlyImportError("BlenderDNA: Invalid type index in structure name", n, " (there are only ", types.size(), " entries)");
  108. }
  109. // maintain separate indexes
  110. dna.indices[types[n].name] = dna.structures.size();
  111. dna.structures.push_back(Structure());
  112. Structure &s = dna.structures.back();
  113. s.name = types[n].name;
  114. n = stream.GetI2();
  115. s.fields.reserve(n);
  116. size_t offset = 0;
  117. for (size_t m = 0; m < n; ++m, ++fields) {
  118. uint16_t j = stream.GetI2();
  119. if (j >= types.size()) {
  120. throw DeadlyImportError("BlenderDNA: Invalid type index in structure field ", j, " (there are only ", types.size(), " entries)");
  121. }
  122. s.fields.push_back(Field());
  123. Field &f = s.fields.back();
  124. f.offset = offset;
  125. f.type = types[j].name;
  126. f.size = types[j].size;
  127. j = stream.GetI2();
  128. if (j >= names.size()) {
  129. throw DeadlyImportError("BlenderDNA: Invalid name index in structure field ", j, " (there are only ", names.size(), " entries)");
  130. }
  131. f.name = names[j];
  132. f.flags = 0u;
  133. // pointers always specify the size of the pointee instead of their own.
  134. // The pointer asterisk remains a property of the lookup name.
  135. if (f.name[0] == '*') {
  136. f.size = db.i64bit ? 8 : 4;
  137. f.flags |= FieldFlag_Pointer;
  138. }
  139. // arrays, however, specify the size of a single element so we
  140. // need to parse the (possibly multi-dimensional) array declaration
  141. // in order to obtain the actual size of the array in the file.
  142. // Also we need to alter the lookup name to include no array
  143. // brackets anymore or size fixup won't work (if our size does
  144. // not match the size read from the DNA).
  145. if (*f.name.rbegin() == ']') {
  146. const std::string::size_type rb = f.name.find('[');
  147. if (rb == std::string::npos) {
  148. throw DeadlyImportError("BlenderDNA: Encountered invalid array declaration ", f.name);
  149. }
  150. f.flags |= FieldFlag_Array;
  151. DNA::ExtractArraySize(f.name, f.array_sizes);
  152. f.name = f.name.substr(0, rb);
  153. f.size *= f.array_sizes[0] * f.array_sizes[1];
  154. }
  155. // maintain separate indexes
  156. s.indices[f.name] = s.fields.size() - 1;
  157. offset += f.size;
  158. }
  159. s.size = offset;
  160. }
  161. ASSIMP_LOG_DEBUG("BlenderDNA: Got ", dna.structures.size(), " structures with totally ", fields, " fields");
  162. #if ASSIMP_BUILD_BLENDER_DEBUG_DNA
  163. dna.DumpToFile();
  164. #endif
  165. dna.AddPrimitiveStructures();
  166. dna.RegisterConverters();
  167. }
  168. #if ASSIMP_BUILD_BLENDER_DEBUG_DNA
  169. #include <fstream>
  170. // ------------------------------------------------------------------------------------------------
  171. void DNA ::DumpToFile() {
  172. // we don't bother using the VFS here for this is only for debugging.
  173. // (and all your bases are belong to us).
  174. std::ofstream f("dna.txt");
  175. if (f.fail()) {
  176. ASSIMP_LOG_ERROR("Could not dump dna to dna.txt");
  177. return;
  178. }
  179. f << "Field format: type name offset size"
  180. << "\n";
  181. f << "Structure format: name size"
  182. << "\n";
  183. for (const Structure &s : structures) {
  184. f << s.name << " " << s.size << "\n\n";
  185. for (const Field &ff : s.fields) {
  186. f << "\t" << ff.type << " " << ff.name << " " << ff.offset << " " << ff.size << "\n";
  187. }
  188. f << "\n";
  189. }
  190. f << std::flush;
  191. ASSIMP_LOG_INFO("BlenderDNA: Dumped dna to dna.txt");
  192. }
  193. #endif // ASSIMP_BUILD_BLENDER_DEBUG_DNA
  194. // ------------------------------------------------------------------------------------------------
  195. /*static*/ void DNA ::ExtractArraySize(
  196. const std::string &out,
  197. size_t array_sizes[2]) {
  198. array_sizes[0] = array_sizes[1] = 1;
  199. std::string::size_type pos = out.find('[');
  200. if (pos++ == std::string::npos) {
  201. return;
  202. }
  203. array_sizes[0] = strtoul10(&out[pos]);
  204. pos = out.find('[', pos);
  205. if (pos++ == std::string::npos) {
  206. return;
  207. }
  208. array_sizes[1] = strtoul10(&out[pos]);
  209. }
  210. // ------------------------------------------------------------------------------------------------
  211. std::shared_ptr<ElemBase> DNA ::ConvertBlobToStructure(
  212. const Structure &structure,
  213. const FileDatabase &db) const {
  214. std::map<std::string, FactoryPair>::const_iterator it = converters.find(structure.name);
  215. if (it == converters.end()) {
  216. return std::shared_ptr<ElemBase>();
  217. }
  218. std::shared_ptr<ElemBase> ret = (structure.*((*it).second.first))();
  219. (structure.*((*it).second.second))(ret, db);
  220. return ret;
  221. }
  222. // ------------------------------------------------------------------------------------------------
  223. DNA::FactoryPair DNA ::GetBlobToStructureConverter(
  224. const Structure &structure,
  225. const FileDatabase & /*db*/
  226. ) const {
  227. std::map<std::string, FactoryPair>::const_iterator it = converters.find(structure.name);
  228. return it == converters.end() ? FactoryPair() : (*it).second;
  229. }
  230. // basing on http://www.blender.org/development/architecture/notes-on-sdna/
  231. // ------------------------------------------------------------------------------------------------
  232. void DNA ::AddPrimitiveStructures() {
  233. // NOTE: these are just dummies. Their presence enforces
  234. // Structure::Convert<target_type> to be called on these
  235. // empty structures. These converters are special
  236. // overloads which scan the name of the structure and
  237. // perform the required data type conversion if one
  238. // of these special names is found in the structure
  239. // in question.
  240. indices["int"] = structures.size();
  241. structures.push_back(Structure());
  242. structures.back().name = "int";
  243. structures.back().size = 4;
  244. indices["short"] = structures.size();
  245. structures.push_back(Structure());
  246. structures.back().name = "short";
  247. structures.back().size = 2;
  248. indices["char"] = structures.size();
  249. structures.push_back(Structure());
  250. structures.back().name = "char";
  251. structures.back().size = 1;
  252. indices["float"] = structures.size();
  253. structures.push_back(Structure());
  254. structures.back().name = "float";
  255. structures.back().size = 4;
  256. indices["double"] = structures.size();
  257. structures.push_back(Structure());
  258. structures.back().name = "double";
  259. structures.back().size = 8;
  260. // no long, seemingly.
  261. }
  262. // ------------------------------------------------------------------------------------------------
  263. void SectionParser ::Next() {
  264. stream.SetCurrentPos(current.start + current.size);
  265. const char tmp[] = {
  266. (char)stream.GetI1(),
  267. (char)stream.GetI1(),
  268. (char)stream.GetI1(),
  269. (char)stream.GetI1()
  270. };
  271. current.id = std::string(tmp, tmp[3] ? 4 : tmp[2] ? 3 : tmp[1] ? 2 : 1);
  272. current.size = stream.GetI4();
  273. current.address.val = ptr64 ? stream.GetU8() : stream.GetU4();
  274. current.dna_index = stream.GetI4();
  275. current.num = stream.GetI4();
  276. current.start = stream.GetCurrentPos();
  277. if (stream.GetRemainingSizeToLimit() < current.size) {
  278. throw DeadlyImportError("BLEND: invalid size of file block");
  279. }
  280. #ifdef ASSIMP_BUILD_BLENDER_DEBUG
  281. ASSIMP_LOG_VERBOSE_DEBUG(current.id);
  282. #endif
  283. }
  284. #endif