btBulletFile.cpp 9.7 KB

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