diff --git a/Extras/Serialize/BulletFileLoader/bDNA.cpp b/Extras/Serialize/BulletFileLoader/bDNA.cpp index 6496f5821..6f3684cb1 100644 --- a/Extras/Serialize/BulletFileLoader/bDNA.cpp +++ b/Extras/Serialize/BulletFileLoader/bDNA.cpp @@ -341,6 +341,7 @@ static int name_is_array(char* name, int* dim1, int* dim2) { // ----------------------------------------------------- // void bDNA::init(char *data, int len, bool swap) { + printf("swap = %d\n",swap); int *intPtr=0;short *shtPtr=0; char *cp = 0;int dataLen =0;long nr=0; intPtr = (int*)data; diff --git a/Extras/Serialize/BulletFileLoader/bFile.cpp b/Extras/Serialize/BulletFileLoader/bFile.cpp index 4e898e049..4c34f0074 100644 --- a/Extras/Serialize/BulletFileLoader/bFile.cpp +++ b/Extras/Serialize/BulletFileLoader/bFile.cpp @@ -140,8 +140,11 @@ void bFile::parseHeader() else if (VOID_IS_8) mFlags |= FD_BITS_VARIES; // swap endian... - if (header[8]=='V' && littleEndian ==1) - mFlags |= FD_ENDIAN_SWAP; + if (header[8]=='V') + { + if (littleEndian ==1) + mFlags |= FD_ENDIAN_SWAP; + } else if (littleEndian==0) mFlags |= FD_ENDIAN_SWAP; @@ -199,7 +202,10 @@ void bFile::parseInternal(bool verboseDumpAllTypes, char* memDna,int memDnaLengt } mMemoryDNA = new bDNA(); - mMemoryDNA->init(memDna,memDnaLength); + int littleEndian= 1; + littleEndian= ((char*)&littleEndian)[0]; + + mMemoryDNA->init(memDna,memDnaLength,littleEndian==0); @@ -644,14 +650,25 @@ void bFile::swapStruct(int dna_nr, char *data) if (strc[0] >= first && name[0]!='*') { int old_nr = mFileDNA->getReverseType(type); - swapStruct(old_nr,buf); + int arrayLen = mFileDNA->getArraySizeNew(strc[1]); + if (arrayLen==1) + { + swapStruct(old_nr,buf); + } else + { + char* tmpBuf = buf; + for (int i=0;igetArraySize(name); int arrayLen = mFileDNA->getArraySizeNew(strc[1]); //assert(arrayLenOld == arrayLen); - swapData(buf, strc[0], arrayLen); } buf+=size; diff --git a/src/LinearMath/btSerializer.h b/src/LinearMath/btSerializer.h index c6d7470b0..0a6f7958b 100644 --- a/src/LinearMath/btSerializer.h +++ b/src/LinearMath/btSerializer.h @@ -1,384 +1,419 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org - -This software is provided 'as-is', without any express or implied warranty. -In no event will the authors be held liable for any damages arising from the use of this software. -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it freely, -subject to the following restrictions: - -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. -2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. -3. This notice may not be removed or altered from any source distribution. -*/ - -#ifndef BT_SERIALIZER_H -#define BT_SERIALIZER_H - -#include "btScalar.h" // has definitions like SIMD_FORCE_INLINE -#include "btStackAlloc.h" -#include "btHashMap.h" -#include -#include - - - -///only the 32bit versions for now -extern unsigned char sBulletDNAstr[]; -extern int sBulletDNAlen; -extern unsigned char sBulletDNAstr64[]; -extern int sBulletDNAlen64; - - - -class btChunk -{ -public: - int m_chunkCode; - int m_length; - void *m_oldPtr; - int m_dna_nr; - int m_number; -}; - -class btSerializer -{ - -public: - - virtual const unsigned char* getBufferPointer() const = 0; - - virtual int getCurrentBufferSize() const = 0; - - virtual btChunk* allocate(size_t size, int numElements) = 0; - - virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr) const = 0; - - virtual void startSerialization() = 0; - - virtual void finishSerialization() = 0; - -}; - - - -#define BT_HEADER_LENGTH 12 -#if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__BIG_ENDIAN__) -# define MAKE_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) ) -#else -# define MAKE_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) ) -#endif - -#define BT_COLLISIONOBJECT_CODE MAKE_ID('C','O','B','J') -#define BT_RIGIDBODY_CODE MAKE_ID('R','B','D','Y') -#define BT_CONSTRAINT_CODE MAKE_ID('C','O','N','S') -#define BT_BOXSHAPE_CODE MAKE_ID('B','O','X','S') -#define BT_SHAPE_CODE MAKE_ID('S','H','A','P') -#define BT_ARRAY_CODE MAKE_ID('A','R','A','Y') - -class btDefaultSerializer : public btSerializer -{ - - - btAlignedObjectArray mTypes; - btAlignedObjectArray mStructs; - btAlignedObjectArray mTlens; - btHashMap mStructReverse; - btHashMap mTypeLookup; - - btAlignedObjectArray m_chunkPtrs; - - int m_totalSize; - unsigned char* m_buffer; - int m_currentSize; - -protected: - - void writeDNA() - { - unsigned char* dnaTarget = m_buffer+m_currentSize; - memcpy(dnaTarget,m_dna,m_dnaLength); - m_currentSize += m_dnaLength; - } - - int getReverseType(const char *type) const - { - - btHashString key(type); - const int* valuePtr = mTypeLookup.find(key); - if (valuePtr) - return *valuePtr; - - return -1; - } - - void initDNA(const char* bdna,int dnalen) - { - ///was already initialized - if (m_dna) - return; - - m_dna = btAlignedAlloc(dnalen,16); - memcpy(m_dna,bdna,dnalen); - m_dnaLength = dnalen; - - int *intPtr=0;short *shtPtr=0; - char *cp = 0;int dataLen =0;long nr=0; - intPtr = (int*)bdna; - - /* - SDNA (4 bytes) (magic number) - NAME (4 bytes) - (4 bytes) amount of names (int) - - - */ - - if (strncmp((const char*)bdna, "SDNA", 4)==0) - { - // skip ++ NAME - intPtr++; intPtr++; - } - - // Parse names - - dataLen = *intPtr; - intPtr++; - - cp = (char*)intPtr; - for (int i=0; i amount of types (int) - - - */ - - intPtr = (int*)cp; - assert(strncmp(cp, "TYPE", 4)==0); intPtr++; - - dataLen = *intPtr; - intPtr++; - - cp = (char*)intPtr; - for (int i=0; i (short) the lengths of types - - */ - - // Parse type lens - intPtr = (int*)cp; - assert(strncmp(cp, "TLEN", 4)==0); intPtr++; - - dataLen = (int)mTypes.size(); - - shtPtr = (short*)intPtr; - for (int i=0; i amount of structs (int) - - - - - - - */ - - intPtr = (int*)shtPtr; - cp = (char*)intPtr; - assert(strncmp(cp, "STRC", 4)==0); intPtr++; - - dataLen = *intPtr; - intPtr++; - - - shtPtr = (short*)intPtr; - for (int i=0; im_dna_nr = getReverseType(structType); - - chunk->m_chunkCode = chunkCode; - chunk->m_oldPtr = oldPtr; - } - - - - - - virtual btChunk* allocate(size_t size, int numElements) - { - - unsigned char* ptr = m_buffer+m_currentSize; - m_currentSize += int(size)*numElements+sizeof(btChunk); - - unsigned char* data = ptr + sizeof(btChunk); - - btChunk* chunk = (btChunk*)ptr; - chunk->m_chunkCode = 0; - chunk->m_oldPtr = data; - chunk->m_length = int(size)*numElements; - chunk->m_number = numElements; - - m_chunkPtrs.push_back(chunk); - - return chunk; - } - - - - -}; - - -#endif //BT_SERIALIZER_H - +/* +Bullet Continuous Collision Detection and Physics Library +Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org + +This software is provided 'as-is', without any express or implied warranty. +In no event will the authors be held liable for any damages arising from the use of this software. +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it freely, +subject to the following restrictions: + +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. +2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef BT_SERIALIZER_H +#define BT_SERIALIZER_H + +#include "btScalar.h" // has definitions like SIMD_FORCE_INLINE +#include "btStackAlloc.h" +#include "btHashMap.h" +#include +#include + + + +///only the 32bit versions for now +extern unsigned char sBulletDNAstr[]; +extern int sBulletDNAlen; +extern unsigned char sBulletDNAstr64[]; +extern int sBulletDNAlen64; + + + +class btChunk +{ +public: + int m_chunkCode; + int m_length; + void *m_oldPtr; + int m_dna_nr; + int m_number; +}; + +class btSerializer +{ + +public: + + virtual const unsigned char* getBufferPointer() const = 0; + + virtual int getCurrentBufferSize() const = 0; + + virtual btChunk* allocate(size_t size, int numElements) = 0; + + virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr) const = 0; + + virtual void startSerialization() = 0; + + virtual void finishSerialization() = 0; + +}; + + + +#define BT_HEADER_LENGTH 12 +#if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__BIG_ENDIAN__) +# define MAKE_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) ) +#else +# define MAKE_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) ) +#endif + +#define BT_COLLISIONOBJECT_CODE MAKE_ID('C','O','B','J') +#define BT_RIGIDBODY_CODE MAKE_ID('R','B','D','Y') +#define BT_CONSTRAINT_CODE MAKE_ID('C','O','N','S') +#define BT_BOXSHAPE_CODE MAKE_ID('B','O','X','S') +#define BT_SHAPE_CODE MAKE_ID('S','H','A','P') +#define BT_ARRAY_CODE MAKE_ID('A','R','A','Y') + + + +class btDefaultSerializer : public btSerializer +{ + + + btAlignedObjectArray mTypes; + btAlignedObjectArray mStructs; + btAlignedObjectArray mTlens; + btHashMap mStructReverse; + btHashMap mTypeLookup; + + btAlignedObjectArray m_chunkPtrs; + + int m_totalSize; + unsigned char* m_buffer; + int m_currentSize; + +protected: + + void writeDNA() + { + unsigned char* dnaTarget = m_buffer+m_currentSize; + memcpy(dnaTarget,m_dna,m_dnaLength); + m_currentSize += m_dnaLength; + } + + int getReverseType(const char *type) const + { + + btHashString key(type); + const int* valuePtr = mTypeLookup.find(key); + if (valuePtr) + return *valuePtr; + + return -1; + } + + void initDNA(const char* bdnaOrg,int dnalen) + { + ///was already initialized + if (m_dna) + return; + + int littleEndian= 1; + littleEndian= ((char*)&littleEndian)[0]; + + + m_dna = btAlignedAlloc(dnalen,16); + memcpy(m_dna,bdnaOrg,dnalen); + m_dnaLength = dnalen; + + int *intPtr=0; + short *shtPtr=0; + char *cp = 0;int dataLen =0;long nr=0; + intPtr = (int*)m_dna; + + /* + SDNA (4 bytes) (magic number) + NAME (4 bytes) + (4 bytes) amount of names (int) + + + */ + + if (strncmp((const char*)m_dna, "SDNA", 4)==0) + { + // skip ++ NAME + intPtr++; intPtr++; + } + + // Parse names + if (!littleEndian) + *intPtr = btSwapEndian(*intPtr); + + dataLen = *intPtr; + + intPtr++; + + cp = (char*)intPtr; + for (int i=0; i amount of types (int) + + + */ + + intPtr = (int*)cp; + assert(strncmp(cp, "TYPE", 4)==0); intPtr++; + + if (!littleEndian) + *intPtr = btSwapEndian(*intPtr); + + dataLen = *intPtr; + intPtr++; + + cp = (char*)intPtr; + for (int i=0; i (short) the lengths of types + + */ + + // Parse type lens + intPtr = (int*)cp; + assert(strncmp(cp, "TLEN", 4)==0); intPtr++; + + dataLen = (int)mTypes.size(); + + shtPtr = (short*)intPtr; + for (int i=0; i amount of structs (int) + + + + + + + */ + + intPtr = (int*)shtPtr; + cp = (char*)intPtr; + assert(strncmp(cp, "STRC", 4)==0); intPtr++; + + if (!littleEndian) + *intPtr = btSwapEndian(*intPtr); + dataLen = *intPtr ; + intPtr++; + + + shtPtr = (short*)intPtr; + for (int i=0; im_dna_nr = getReverseType(structType); + + chunk->m_chunkCode = chunkCode; + chunk->m_oldPtr = oldPtr; + } + + + + + + virtual btChunk* allocate(size_t size, int numElements) + { + + unsigned char* ptr = m_buffer+m_currentSize; + m_currentSize += int(size)*numElements+sizeof(btChunk); + + unsigned char* data = ptr + sizeof(btChunk); + + btChunk* chunk = (btChunk*)ptr; + chunk->m_chunkCode = 0; + chunk->m_oldPtr = data; + chunk->m_length = int(size)*numElements; + chunk->m_number = numElements; + + m_chunkPtrs.push_back(chunk); + + return chunk; + } + + + + +}; + + +#endif //BT_SERIALIZER_H +