b3BulletFile.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. bParse
  3. Copyright (c) 2006-2010 Erwin Coumans http://gamekit.googlecode.com
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "b3BulletFile.h"
  14. #include "b3Defines.h"
  15. #include "b3DNA.h"
  16. #if !defined(__CELLOS_LV2__) && !defined(__MWERKS__)
  17. #include <memory.h>
  18. #endif
  19. #include <string.h>
  20. // 32 && 64 bit versions
  21. #ifdef B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  22. #ifdef _WIN64
  23. extern char b3s_bulletDNAstr64[];
  24. extern int b3s_bulletDNAlen64;
  25. #else
  26. extern char b3s_bulletDNAstr[];
  27. extern int b3s_bulletDNAlen;
  28. #endif //_WIN64
  29. #else //B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  30. extern char b3s_bulletDNAstr64[];
  31. extern int b3s_bulletDNAlen64;
  32. extern char b3s_bulletDNAstr[];
  33. extern int b3s_bulletDNAlen;
  34. #endif //B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  35. using namespace bParse;
  36. b3BulletFile::b3BulletFile()
  37. : bFile("", "BULLET ")
  38. {
  39. mMemoryDNA = new bDNA(); //this memory gets released in the bFile::~bFile destructor,@todo not consistent with the rule 'who allocates it, has to deallocate it"
  40. m_DnaCopy = 0;
  41. #ifdef B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  42. #ifdef _WIN64
  43. m_DnaCopy = (char*)b3AlignedAlloc(b3s_bulletDNAlen64, 16);
  44. memcpy(m_DnaCopy, b3s_bulletDNAstr64, b3s_bulletDNAlen64);
  45. mMemoryDNA->init(m_DnaCopy, b3s_bulletDNAlen64);
  46. #else //_WIN64
  47. m_DnaCopy = (char*)b3AlignedAlloc(b3s_bulletDNAlen, 16);
  48. memcpy(m_DnaCopy, b3s_bulletDNAstr, b3s_bulletDNAlen);
  49. mMemoryDNA->init(m_DnaCopy, b3s_bulletDNAlen);
  50. #endif //_WIN64
  51. #else //B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  52. if (VOID_IS_8)
  53. {
  54. m_DnaCopy = (char*)b3AlignedAlloc(b3s_bulletDNAlen64, 16);
  55. memcpy(m_DnaCopy, b3s_bulletDNAstr64, b3s_bulletDNAlen64);
  56. mMemoryDNA->init(m_DnaCopy, b3s_bulletDNAlen64);
  57. }
  58. else
  59. {
  60. m_DnaCopy = (char*)b3AlignedAlloc(b3s_bulletDNAlen, 16);
  61. memcpy(m_DnaCopy, b3s_bulletDNAstr, b3s_bulletDNAlen);
  62. mMemoryDNA->init(m_DnaCopy, b3s_bulletDNAlen);
  63. }
  64. #endif //B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  65. }
  66. b3BulletFile::b3BulletFile(const char* fileName)
  67. : bFile(fileName, "BULLET ")
  68. {
  69. m_DnaCopy = 0;
  70. }
  71. b3BulletFile::b3BulletFile(char* memoryBuffer, int len)
  72. : bFile(memoryBuffer, len, "BULLET ")
  73. {
  74. m_DnaCopy = 0;
  75. }
  76. b3BulletFile::~b3BulletFile()
  77. {
  78. if (m_DnaCopy)
  79. b3AlignedFree(m_DnaCopy);
  80. while (m_dataBlocks.size())
  81. {
  82. char* dataBlock = m_dataBlocks[m_dataBlocks.size() - 1];
  83. delete[] dataBlock;
  84. m_dataBlocks.pop_back();
  85. }
  86. }
  87. // ----------------------------------------------------- //
  88. void b3BulletFile::parseData()
  89. {
  90. // printf ("Building datablocks");
  91. // printf ("Chunk size = %d",CHUNK_HEADER_LEN);
  92. // printf ("File chunk size = %d",ChunkUtils::getOffset(mFlags));
  93. const bool brokenDNA = (mFlags & FD_BROKEN_DNA) != 0;
  94. //const bool swap = (mFlags&FD_ENDIAN_SWAP)!=0;
  95. mDataStart = 12;
  96. char* dataPtr = mFileBuffer + mDataStart;
  97. bChunkInd dataChunk;
  98. dataChunk.code = 0;
  99. //dataPtr += ChunkUtils::getNextBlock(&dataChunk, dataPtr, mFlags);
  100. int seek = getNextBlock(&dataChunk, dataPtr, mFlags);
  101. if (mFlags & FD_ENDIAN_SWAP)
  102. swapLen(dataPtr);
  103. //dataPtr += ChunkUtils::getOffset(mFlags);
  104. char* dataPtrHead = 0;
  105. while (dataChunk.code != B3_DNA1)
  106. {
  107. if (!brokenDNA || (dataChunk.code != B3_QUANTIZED_BVH_CODE))
  108. {
  109. // one behind
  110. if (dataChunk.code == B3_SDNA) break;
  111. //if (dataChunk.code == DNA1) break;
  112. // same as (BHEAD+DATA dependency)
  113. dataPtrHead = dataPtr + ChunkUtils::getOffset(mFlags);
  114. if (dataChunk.dna_nr >= 0)
  115. {
  116. char* id = readStruct(dataPtrHead, dataChunk);
  117. // lookup maps
  118. if (id)
  119. {
  120. m_chunkPtrPtrMap.insert(dataChunk.oldPtr, dataChunk);
  121. mLibPointers.insert(dataChunk.oldPtr, (bStructHandle*)id);
  122. m_chunks.push_back(dataChunk);
  123. // block it
  124. //bListBasePtr *listID = mMain->getListBasePtr(dataChunk.code);
  125. //if (listID)
  126. // listID->push_back((bStructHandle*)id);
  127. }
  128. if (dataChunk.code == B3_SOFTBODY_CODE)
  129. {
  130. m_softBodies.push_back((bStructHandle*)id);
  131. }
  132. if (dataChunk.code == B3_RIGIDBODY_CODE)
  133. {
  134. m_rigidBodies.push_back((bStructHandle*)id);
  135. }
  136. if (dataChunk.code == B3_DYNAMICSWORLD_CODE)
  137. {
  138. m_dynamicsWorldInfo.push_back((bStructHandle*)id);
  139. }
  140. if (dataChunk.code == B3_CONSTRAINT_CODE)
  141. {
  142. m_constraints.push_back((bStructHandle*)id);
  143. }
  144. if (dataChunk.code == B3_QUANTIZED_BVH_CODE)
  145. {
  146. m_bvhs.push_back((bStructHandle*)id);
  147. }
  148. if (dataChunk.code == B3_TRIANLGE_INFO_MAP)
  149. {
  150. m_triangleInfoMaps.push_back((bStructHandle*)id);
  151. }
  152. if (dataChunk.code == B3_COLLISIONOBJECT_CODE)
  153. {
  154. m_collisionObjects.push_back((bStructHandle*)id);
  155. }
  156. if (dataChunk.code == B3_SHAPE_CODE)
  157. {
  158. m_collisionShapes.push_back((bStructHandle*)id);
  159. }
  160. // if (dataChunk.code == GLOB)
  161. // {
  162. // m_glob = (bStructHandle*) id;
  163. // }
  164. }
  165. else
  166. {
  167. //printf("unknown chunk\n");
  168. mLibPointers.insert(dataChunk.oldPtr, (bStructHandle*)dataPtrHead);
  169. }
  170. }
  171. else
  172. {
  173. printf("skipping B3_QUANTIZED_BVH_CODE due to broken DNA\n");
  174. }
  175. dataPtr += seek;
  176. seek = getNextBlock(&dataChunk, dataPtr, mFlags);
  177. if (mFlags & FD_ENDIAN_SWAP)
  178. swapLen(dataPtr);
  179. if (seek < 0)
  180. break;
  181. }
  182. }
  183. void b3BulletFile::addDataBlock(char* dataBlock)
  184. {
  185. m_dataBlocks.push_back(dataBlock);
  186. }
  187. void b3BulletFile::writeDNA(FILE* fp)
  188. {
  189. bChunkInd dataChunk;
  190. dataChunk.code = B3_DNA1;
  191. dataChunk.dna_nr = 0;
  192. dataChunk.nr = 1;
  193. #ifdef B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  194. if (VOID_IS_8)
  195. {
  196. #ifdef _WIN64
  197. dataChunk.len = b3s_bulletDNAlen64;
  198. dataChunk.oldPtr = b3s_bulletDNAstr64;
  199. fwrite(&dataChunk, sizeof(bChunkInd), 1, fp);
  200. fwrite(b3s_bulletDNAstr64, b3s_bulletDNAlen64, 1, fp);
  201. #else
  202. b3Assert(0);
  203. #endif
  204. }
  205. else
  206. {
  207. #ifndef _WIN64
  208. dataChunk.len = b3s_bulletDNAlen;
  209. dataChunk.oldPtr = b3s_bulletDNAstr;
  210. fwrite(&dataChunk, sizeof(bChunkInd), 1, fp);
  211. fwrite(b3s_bulletDNAstr, b3s_bulletDNAlen, 1, fp);
  212. #else //_WIN64
  213. b3Assert(0);
  214. #endif //_WIN64
  215. }
  216. #else //B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  217. if (VOID_IS_8)
  218. {
  219. dataChunk.len = b3s_bulletDNAlen64;
  220. dataChunk.oldPtr = b3s_bulletDNAstr64;
  221. fwrite(&dataChunk, sizeof(bChunkInd), 1, fp);
  222. fwrite(b3s_bulletDNAstr64, b3s_bulletDNAlen64, 1, fp);
  223. }
  224. else
  225. {
  226. dataChunk.len = b3s_bulletDNAlen;
  227. dataChunk.oldPtr = b3s_bulletDNAstr;
  228. fwrite(&dataChunk, sizeof(bChunkInd), 1, fp);
  229. fwrite(b3s_bulletDNAstr, b3s_bulletDNAlen, 1, fp);
  230. }
  231. #endif //B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  232. }
  233. void b3BulletFile::parse(int verboseMode)
  234. {
  235. #ifdef B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  236. if (VOID_IS_8)
  237. {
  238. #ifdef _WIN64
  239. if (m_DnaCopy)
  240. delete m_DnaCopy;
  241. m_DnaCopy = (char*)b3AlignedAlloc(b3s_bulletDNAlen64, 16);
  242. memcpy(m_DnaCopy, b3s_bulletDNAstr64, b3s_bulletDNAlen64);
  243. parseInternal(verboseMode, (char*)b3s_bulletDNAstr64, b3s_bulletDNAlen64);
  244. #else
  245. b3Assert(0);
  246. #endif
  247. }
  248. else
  249. {
  250. #ifndef _WIN64
  251. if (m_DnaCopy)
  252. delete m_DnaCopy;
  253. m_DnaCopy = (char*)b3AlignedAlloc(b3s_bulletDNAlen, 16);
  254. memcpy(m_DnaCopy, b3s_bulletDNAstr, b3s_bulletDNAlen);
  255. parseInternal(verboseMode, m_DnaCopy, b3s_bulletDNAlen);
  256. #else
  257. b3Assert(0);
  258. #endif
  259. }
  260. #else //B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  261. if (VOID_IS_8)
  262. {
  263. if (m_DnaCopy)
  264. delete m_DnaCopy;
  265. m_DnaCopy = (char*)b3AlignedAlloc(b3s_bulletDNAlen64, 16);
  266. memcpy(m_DnaCopy, b3s_bulletDNAstr64, b3s_bulletDNAlen64);
  267. parseInternal(verboseMode, m_DnaCopy, b3s_bulletDNAlen64);
  268. }
  269. else
  270. {
  271. if (m_DnaCopy)
  272. delete m_DnaCopy;
  273. m_DnaCopy = (char*)b3AlignedAlloc(b3s_bulletDNAlen, 16);
  274. memcpy(m_DnaCopy, b3s_bulletDNAstr, b3s_bulletDNAlen);
  275. parseInternal(verboseMode, m_DnaCopy, b3s_bulletDNAlen);
  276. }
  277. #endif //B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  278. //the parsing will convert to cpu endian
  279. mFlags &= ~FD_ENDIAN_SWAP;
  280. int littleEndian = 1;
  281. littleEndian = ((char*)&littleEndian)[0];
  282. mFileBuffer[8] = littleEndian ? 'v' : 'V';
  283. }
  284. // experimental
  285. int b3BulletFile::write(const char* fileName, bool fixupPointers)
  286. {
  287. FILE* fp = fopen(fileName, "wb");
  288. if (fp)
  289. {
  290. char header[B3_SIZEOFBLENDERHEADER];
  291. memcpy(header, m_headerString, 7);
  292. int endian = 1;
  293. endian = ((char*)&endian)[0];
  294. if (endian)
  295. {
  296. header[7] = '_';
  297. }
  298. else
  299. {
  300. header[7] = '-';
  301. }
  302. if (VOID_IS_8)
  303. {
  304. header[8] = 'V';
  305. }
  306. else
  307. {
  308. header[8] = 'v';
  309. }
  310. header[9] = '2';
  311. header[10] = '7';
  312. header[11] = '5';
  313. fwrite(header, B3_SIZEOFBLENDERHEADER, 1, fp);
  314. writeChunks(fp, fixupPointers);
  315. writeDNA(fp);
  316. fclose(fp);
  317. }
  318. else
  319. {
  320. printf("Error: cannot open file %s for writing\n", fileName);
  321. return 0;
  322. }
  323. return 1;
  324. }
  325. void b3BulletFile::addStruct(const char* structType, void* data, int len, void* oldPtr, int code)
  326. {
  327. bParse::bChunkInd dataChunk;
  328. dataChunk.code = code;
  329. dataChunk.nr = 1;
  330. dataChunk.len = len;
  331. dataChunk.dna_nr = mMemoryDNA->getReverseType(structType);
  332. dataChunk.oldPtr = oldPtr;
  333. ///Perform structure size validation
  334. short* structInfo = mMemoryDNA->getStruct(dataChunk.dna_nr);
  335. int elemBytes;
  336. elemBytes = mMemoryDNA->getLength(structInfo[0]);
  337. // int elemBytes = mMemoryDNA->getElementSize(structInfo[0],structInfo[1]);
  338. assert(len == elemBytes);
  339. mLibPointers.insert(dataChunk.oldPtr, (bStructHandle*)data);
  340. m_chunks.push_back(dataChunk);
  341. }