btBulletFile.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 "btBulletFile.h"
  14. #include "bDefines.h"
  15. #include "bDNA.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 BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  22. #ifdef _WIN64
  23. extern char sBulletDNAstr64[];
  24. extern int sBulletDNAlen64;
  25. #else
  26. extern char sBulletDNAstr[];
  27. extern int sBulletDNAlen;
  28. #endif //_WIN64
  29. #else//BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  30. extern char sBulletDNAstr64[];
  31. extern int sBulletDNAlen64;
  32. extern char sBulletDNAstr[];
  33. extern int sBulletDNAlen;
  34. #endif //BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  35. using namespace bParse;
  36. btBulletFile::btBulletFile()
  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 BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  42. #ifdef _WIN64
  43. m_DnaCopy = (char*)btAlignedAlloc(sBulletDNAlen64,16);
  44. memcpy(m_DnaCopy,sBulletDNAstr64,sBulletDNAlen64);
  45. mMemoryDNA->init(m_DnaCopy,sBulletDNAlen64);
  46. #else//_WIN64
  47. m_DnaCopy = (char*)btAlignedAlloc(sBulletDNAlen,16);
  48. memcpy(m_DnaCopy,sBulletDNAstr,sBulletDNAlen);
  49. mMemoryDNA->init(m_DnaCopy,sBulletDNAlen);
  50. #endif//_WIN64
  51. #else//BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  52. if (VOID_IS_8)
  53. {
  54. m_DnaCopy = (char*) btAlignedAlloc(sBulletDNAlen64,16);
  55. memcpy(m_DnaCopy,sBulletDNAstr64,sBulletDNAlen64);
  56. mMemoryDNA->init(m_DnaCopy,sBulletDNAlen64);
  57. }
  58. else
  59. {
  60. m_DnaCopy =(char*) btAlignedAlloc(sBulletDNAlen,16);
  61. memcpy(m_DnaCopy,sBulletDNAstr,sBulletDNAlen);
  62. mMemoryDNA->init(m_DnaCopy,sBulletDNAlen);
  63. }
  64. #endif//BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  65. }
  66. btBulletFile::btBulletFile(const char* fileName)
  67. :bFile(fileName, "BULLET ")
  68. {
  69. m_DnaCopy = 0;
  70. }
  71. btBulletFile::btBulletFile(char *memoryBuffer, int len)
  72. :bFile(memoryBuffer,len, "BULLET ")
  73. {
  74. m_DnaCopy = 0;
  75. }
  76. btBulletFile::~btBulletFile()
  77. {
  78. if (m_DnaCopy)
  79. btAlignedFree(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 btBulletFile::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. int remain = mFileLen;
  96. mDataStart = 12;
  97. remain-=12;
  98. //invalid/empty file?
  99. if (remain < sizeof(bChunkInd))
  100. return;
  101. char *dataPtr = mFileBuffer+mDataStart;
  102. bChunkInd dataChunk;
  103. dataChunk.code = 0;
  104. //dataPtr += ChunkUtils::getNextBlock(&dataChunk, dataPtr, mFlags);
  105. int seek = getNextBlock(&dataChunk, dataPtr, mFlags);
  106. if (mFlags &FD_ENDIAN_SWAP)
  107. swapLen(dataPtr);
  108. //dataPtr += ChunkUtils::getOffset(mFlags);
  109. char *dataPtrHead = 0;
  110. while (dataChunk.code != DNA1)
  111. {
  112. if (!brokenDNA || (dataChunk.code != BT_QUANTIZED_BVH_CODE) )
  113. {
  114. // one behind
  115. if (dataChunk.code == SDNA) break;
  116. //if (dataChunk.code == DNA1) break;
  117. // same as (BHEAD+DATA dependency)
  118. dataPtrHead = dataPtr+ChunkUtils::getOffset(mFlags);
  119. if (dataChunk.dna_nr>=0)
  120. {
  121. char *id = readStruct(dataPtrHead, dataChunk);
  122. // lookup maps
  123. if (id)
  124. {
  125. m_chunkPtrPtrMap.insert(dataChunk.oldPtr, dataChunk);
  126. mLibPointers.insert(dataChunk.oldPtr, (bStructHandle*)id);
  127. m_chunks.push_back(dataChunk);
  128. // block it
  129. //bListBasePtr *listID = mMain->getListBasePtr(dataChunk.code);
  130. //if (listID)
  131. // listID->push_back((bStructHandle*)id);
  132. }
  133. if (dataChunk.code == BT_CONTACTMANIFOLD_CODE)
  134. {
  135. m_contactManifolds.push_back((bStructHandle*)id);
  136. }
  137. if (dataChunk.code == BT_MULTIBODY_CODE)
  138. {
  139. m_multiBodies.push_back((bStructHandle*)id);
  140. }
  141. if (dataChunk.code == BT_MB_LINKCOLLIDER_CODE)
  142. {
  143. m_multiBodyLinkColliders.push_back((bStructHandle*)id);
  144. }
  145. if (dataChunk.code == BT_SOFTBODY_CODE)
  146. {
  147. m_softBodies.push_back((bStructHandle*) id);
  148. }
  149. if (dataChunk.code == BT_RIGIDBODY_CODE)
  150. {
  151. m_rigidBodies.push_back((bStructHandle*) id);
  152. }
  153. if (dataChunk.code == BT_DYNAMICSWORLD_CODE)
  154. {
  155. m_dynamicsWorldInfo.push_back((bStructHandle*) id);
  156. }
  157. if (dataChunk.code == BT_CONSTRAINT_CODE)
  158. {
  159. m_constraints.push_back((bStructHandle*) id);
  160. }
  161. if (dataChunk.code == BT_QUANTIZED_BVH_CODE)
  162. {
  163. m_bvhs.push_back((bStructHandle*) id);
  164. }
  165. if (dataChunk.code == BT_TRIANLGE_INFO_MAP)
  166. {
  167. m_triangleInfoMaps.push_back((bStructHandle*) id);
  168. }
  169. if (dataChunk.code == BT_COLLISIONOBJECT_CODE)
  170. {
  171. m_collisionObjects.push_back((bStructHandle*) id);
  172. }
  173. if (dataChunk.code == BT_SHAPE_CODE)
  174. {
  175. m_collisionShapes.push_back((bStructHandle*) id);
  176. }
  177. // if (dataChunk.code == GLOB)
  178. // {
  179. // m_glob = (bStructHandle*) id;
  180. // }
  181. } else
  182. {
  183. //printf("unknown chunk\n");
  184. mLibPointers.insert(dataChunk.oldPtr, (bStructHandle*)dataPtrHead);
  185. }
  186. } else
  187. {
  188. printf("skipping BT_QUANTIZED_BVH_CODE due to broken DNA\n");
  189. }
  190. dataPtr += seek;
  191. remain-=seek;
  192. if (remain<=0)
  193. break;
  194. seek = getNextBlock(&dataChunk, dataPtr, mFlags);
  195. if (mFlags &FD_ENDIAN_SWAP)
  196. swapLen(dataPtr);
  197. if (seek < 0)
  198. break;
  199. }
  200. }
  201. void btBulletFile::addDataBlock(char* dataBlock)
  202. {
  203. m_dataBlocks.push_back(dataBlock);
  204. }
  205. void btBulletFile::writeDNA(FILE* fp)
  206. {
  207. bChunkInd dataChunk;
  208. dataChunk.code = DNA1;
  209. dataChunk.dna_nr = 0;
  210. dataChunk.nr = 1;
  211. #ifdef BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  212. if (VOID_IS_8)
  213. {
  214. #ifdef _WIN64
  215. dataChunk.len = sBulletDNAlen64;
  216. dataChunk.oldPtr = sBulletDNAstr64;
  217. fwrite(&dataChunk,sizeof(bChunkInd),1,fp);
  218. fwrite(sBulletDNAstr64, sBulletDNAlen64,1,fp);
  219. #else
  220. btAssert(0);
  221. #endif
  222. }
  223. else
  224. {
  225. #ifndef _WIN64
  226. dataChunk.len = sBulletDNAlen;
  227. dataChunk.oldPtr = sBulletDNAstr;
  228. fwrite(&dataChunk,sizeof(bChunkInd),1,fp);
  229. fwrite(sBulletDNAstr, sBulletDNAlen,1,fp);
  230. #else//_WIN64
  231. btAssert(0);
  232. #endif//_WIN64
  233. }
  234. #else//BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  235. if (VOID_IS_8)
  236. {
  237. dataChunk.len = sBulletDNAlen64;
  238. dataChunk.oldPtr = sBulletDNAstr64;
  239. fwrite(&dataChunk,sizeof(bChunkInd),1,fp);
  240. fwrite(sBulletDNAstr64, sBulletDNAlen64,1,fp);
  241. }
  242. else
  243. {
  244. dataChunk.len = sBulletDNAlen;
  245. dataChunk.oldPtr = sBulletDNAstr;
  246. fwrite(&dataChunk,sizeof(bChunkInd),1,fp);
  247. fwrite(sBulletDNAstr, sBulletDNAlen,1,fp);
  248. }
  249. #endif//BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  250. }
  251. void btBulletFile::parse(int verboseMode)
  252. {
  253. #ifdef BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  254. if (VOID_IS_8)
  255. {
  256. #ifdef _WIN64
  257. if (m_DnaCopy)
  258. delete m_DnaCopy;
  259. m_DnaCopy = (char*)btAlignedAlloc(sBulletDNAlen64,16);
  260. memcpy(m_DnaCopy,sBulletDNAstr64,sBulletDNAlen64);
  261. parseInternal(verboseMode,(char*)sBulletDNAstr64,sBulletDNAlen64);
  262. #else
  263. btAssert(0);
  264. #endif
  265. }
  266. else
  267. {
  268. #ifndef _WIN64
  269. if (m_DnaCopy)
  270. delete m_DnaCopy;
  271. m_DnaCopy = (char*)btAlignedAlloc(sBulletDNAlen,16);
  272. memcpy(m_DnaCopy,sBulletDNAstr,sBulletDNAlen);
  273. parseInternal(verboseMode,m_DnaCopy,sBulletDNAlen);
  274. #else
  275. btAssert(0);
  276. #endif
  277. }
  278. #else//BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  279. if (VOID_IS_8)
  280. {
  281. if (m_DnaCopy)
  282. delete m_DnaCopy;
  283. m_DnaCopy = (char*)btAlignedAlloc(sBulletDNAlen64,16);
  284. memcpy(m_DnaCopy,sBulletDNAstr64,sBulletDNAlen64);
  285. parseInternal(verboseMode,m_DnaCopy,sBulletDNAlen64);
  286. }
  287. else
  288. {
  289. if (m_DnaCopy)
  290. delete m_DnaCopy;
  291. m_DnaCopy = (char*)btAlignedAlloc(sBulletDNAlen,16);
  292. memcpy(m_DnaCopy,sBulletDNAstr,sBulletDNAlen);
  293. parseInternal(verboseMode,m_DnaCopy,sBulletDNAlen);
  294. }
  295. #endif//BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  296. //the parsing will convert to cpu endian
  297. mFlags &=~FD_ENDIAN_SWAP;
  298. int littleEndian= 1;
  299. littleEndian= ((char*)&littleEndian)[0];
  300. mFileBuffer[8] = littleEndian?'v':'V';
  301. }
  302. // experimental
  303. int btBulletFile::write(const char* fileName, bool fixupPointers)
  304. {
  305. FILE *fp = fopen(fileName, "wb");
  306. if (fp)
  307. {
  308. char header[SIZEOFBLENDERHEADER] ;
  309. memcpy(header, m_headerString, 7);
  310. int endian= 1;
  311. endian= ((char*)&endian)[0];
  312. if (endian)
  313. {
  314. header[7] = '_';
  315. } else
  316. {
  317. header[7] = '-';
  318. }
  319. if (VOID_IS_8)
  320. {
  321. header[8]='V';
  322. } else
  323. {
  324. header[8]='v';
  325. }
  326. header[9] = '2';
  327. header[10] = '7';
  328. header[11] = '5';
  329. fwrite(header,SIZEOFBLENDERHEADER,1,fp);
  330. writeChunks(fp, fixupPointers);
  331. writeDNA(fp);
  332. fclose(fp);
  333. } else
  334. {
  335. printf("Error: cannot open file %s for writing\n",fileName);
  336. return 0;
  337. }
  338. return 1;
  339. }
  340. void btBulletFile::addStruct(const char* structType,void* data, int len, void* oldPtr, int code)
  341. {
  342. bParse::bChunkInd dataChunk;
  343. dataChunk.code = code;
  344. dataChunk.nr = 1;
  345. dataChunk.len = len;
  346. dataChunk.dna_nr = mMemoryDNA->getReverseType(structType);
  347. dataChunk.oldPtr = oldPtr;
  348. ///Perform structure size validation
  349. short* structInfo= mMemoryDNA->getStruct(dataChunk.dna_nr);
  350. int elemBytes;
  351. elemBytes= mMemoryDNA->getLength(structInfo[0]);
  352. // int elemBytes = mMemoryDNA->getElementSize(structInfo[0],structInfo[1]);
  353. assert(len==elemBytes);
  354. mLibPointers.insert(dataChunk.oldPtr, (bStructHandle*)data);
  355. m_chunks.push_back(dataChunk);
  356. }