btBulletFile.cpp 9.8 KB

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