Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

373 lignes
10 KiB

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