27#define SIZEOFBLENDERHEADER 12
28#define MAX_ARRAY_LENGTH 512
30#define MAX_STRLEN 1024
34 int slen = strlen(memName);
37 for (
int i = 0; i < slen; i++)
39 if (memName[i] ==
']' || memName[i] ==
'[')
45 buffer[i] = memName[i];
55bFile::bFile(
const char *filename,
const char headerString[7])
65 for (
int i = 0; i < 7; i++)
70 FILE *fp = fopen(filename,
"rb");
73 fseek(fp, 0L, SEEK_END);
75 fseek(fp, 0L, SEEK_SET);
90bFile::bFile(
char *memoryBuffer,
int len,
const char headerString[7])
100 for (
int i = 0; i < 7; i++)
140 if (header[6] ==
'd')
145 char *ver = header + 9;
152 int littleEndian = 1;
153 littleEndian = ((
char *)&littleEndian)[0];
156 if (header[7] ==
'-')
166 if (header[8] ==
'V')
168 if (littleEndian == 1)
171 else if (littleEndian == 0)
202 setFileDNA(verboseMode, memDna, memDnaLength);
211 char *tempBuffer = blenderData;
217 if (!
mDataStart && strncmp(tempBuffer,
"REND", 4) == 0)
220 if (strncmp(tempBuffer,
"DNA1", 4) == 0)
236 else if (strncmp(tempBuffer,
"SDNANAME", 8) == 0)
238 dna.
oldPtr = blenderData + i;
280 int littleEndian = 1;
281 littleEndian = ((
char *)&littleEndian)[0];
313 const char s[] =
"SoftBodyMaterialData";
315 if (strncmp((
char *)&dataChunk.
code,
"ARAY", 4) == 0)
319 if (strncmp(oldType, s, szs) == 0)
327 for (
int i = 0; i < dataChunk.
nr; i++)
336 const bool VOID_IS_8 = ((
sizeof(
void *) == 8));
342 if ((c->
code & 0xFFFF) == 0)
351 if ((c->
code & 0xFFFF) == 0)
363 if ((c->
code & 0xFFFF) == 0)
372 if ((c->
code & 0xFFFF) == 0)
387 char *data = &ptr[offset];
394 intPtr = (
int *)data;
404 if (strncmp(data,
"SDNA", 4) == 0)
412 if (strncmp(data + 4,
"SDNA", 4) == 0)
432 for (i = 0; i < dataLen; i++)
448 assert(strncmp(cp,
"TYPE", 4) == 0);
461 for (i = 0; i < dataLen; i++)
477 assert(strncmp(cp,
"TLEN", 4) == 0);
480 shtPtr = (
short *)intPtr;
481 for (i = 0; i < dataLen; i++, shtPtr++)
501 intPtr = (
int *)shtPtr;
503 assert(strncmp(cp,
"STRC", 4) == 0);
515 shtPtr = (
short *)intPtr;
516 for (i = 0; i < dataLen; i++)
527 for (
int a = 0; a < len; a++, shtPtr += 2)
540 FILE *f = fopen(fileName,
"wb");
550 int littleEndian = 1;
551 littleEndian = ((
char *)&littleEndian)[0];
568 bool ignoreEndianFlag =
true;
574 char *dataPtrHead = 0;
590 if (dataChunk.
dna_nr >= 0)
592 swap(dataPtrHead, dataChunk, ignoreEndianFlag);
610 mFlags &= ~FD_ENDIAN_SWAP;
621 bool ignoreEndianFlag =
false;
624 swap(head, dataChunk, ignoreEndianFlag);
629 short *oldStruct, *curStruct;
630 char *oldType, *newType;
631 int oldLen, curLen, reverseOld;
640 if ((strcmp(oldType,
"btQuantizedBvhNodeData") == 0) && oldLen == 20)
644 if ((strcmp(oldType,
"btShortIntIndexData") == 0))
647 char *dataAlloc =
new char[(dataChunk.
nr * allocLen) +
sizeof(
void*)];
648 memset(dataAlloc, 0, (dataChunk.
nr * allocLen) +
sizeof(
void*));
649 short *dest = (
short *)dataAlloc;
650 const short *src = (
short *)head;
651 for (
int i = 0; i < dataChunk.
nr; i++)
665 if (strcmp(
"Link", oldType) != 0)
669 if ((reverseOld != -1))
680 assert((strcmp(oldType, newType) == 0) &&
"internal error, struct mismatch!");
685 int allocLen = (curLen);
686 char *dataAlloc =
new char[(dataChunk.
nr * allocLen) +
sizeof(
void*)];
687 memset(dataAlloc, 0, (dataChunk.
nr * allocLen) +
sizeof(
void*));
692 char *cur = dataAlloc;
694 for (
int block = 0; block < dataChunk.
nr; block++)
696 bool fixupPointers =
true;
714#ifdef DEBUG_EQUAL_STRUCTS
719 printf(
"%s equal structure, just memcpy\n", oldType);
723 char *dataAlloc =
new char[(dataChunk.
len) +
sizeof(
void*)];
724 memset(dataAlloc, 0, dataChunk.
len +
sizeof(
void*));
729 memcpy(dataAlloc, head, dataChunk.
len);
736 if (old_dna == -1)
return;
737 if (new_dna == -1)
return;
745 memcpy(strcPtr, dtPtr, len);
750 char *memType, *memName, *cpc, *cpo;
751 short *fileStruct, *filePtrOld, *memoryStruct, *firstStruct;
752 int elementLength,
size, revType, old_nr, new_nr, fpLen;
753 short firstStructType;
758 firstStruct = fileStruct;
760 filePtrOld = fileStruct;
764 elementLength = memoryStruct[1];
769 for (
int ele = 0; ele < elementLength; ele++, memoryStruct += 2)
777 if (revType != -1 && memoryStruct[0] >= firstStructType && memName[0] !=
'*')
779 cpo =
getFileElement(firstStruct, memName, memType, dtPtr, &filePtrOld);
788 parseStruct(cpc, cpo, old_nr, new_nr, fixupPointers);
795 for (
int i = 0; i < arrayLen; i++)
797 parseStruct(tmpCpc, tmpCpo, old_nr, new_nr, fixupPointers);
798 tmpCpc +=
size / arrayLen;
799 tmpCpo += fpLen / arrayLen;
817static void getElement(
int arrayLen,
const char *cur,
const char *old,
char *oldPtr,
char *curData)
819#define getEle(value, current, type, cast, size, ptr) \
820 if (strcmp(current, type) == 0) \
822 value = (*(cast *)ptr); \
826#define setEle(value, current, type, cast, size, ptr) \
827 if (strcmp(current, type) == 0) \
829 (*(cast *)ptr) = (cast)value; \
834 for (
int i = 0; i < arrayLen; i++)
836 getEle(value, old,
"char",
char,
sizeof(
char), oldPtr);
837 setEle(value, cur,
"char",
char,
sizeof(
char), curData);
838 getEle(value, old,
"short",
short,
sizeof(
short), oldPtr);
839 setEle(value, cur,
"short",
short,
sizeof(
short), curData);
840 getEle(value, old,
"ushort",
unsigned short,
sizeof(
unsigned short), oldPtr);
841 setEle(value, cur,
"ushort",
unsigned short,
sizeof(
unsigned short), curData);
842 getEle(value, old,
"int",
int,
sizeof(
int), oldPtr);
843 setEle(value, cur,
"int",
int,
sizeof(
int), curData);
844 getEle(value, old,
"long",
int,
sizeof(
int), oldPtr);
845 setEle(value, cur,
"long",
int,
sizeof(
int), curData);
846 getEle(value, old,
"float",
float,
sizeof(
float), oldPtr);
847 setEle(value, cur,
"float",
float,
sizeof(
float), curData);
848 getEle(value, old,
"double",
double,
sizeof(
double), oldPtr);
849 setEle(value, cur,
"double",
double,
sizeof(
double), curData);
858 if (type == 2 || type == 3)
860 short *sp = (
short *)data;
861 for (
int i = 0; i < arraySize; i++)
867 if (type > 3 && type < 8)
871 for (
int i = 0; i < arraySize; i++)
893 if (ptrFile == ptrMem)
895 memcpy(dst, src, ptrMem);
897 else if (ptrMem == 4 && ptrFile == 8)
916 *((
int *)dst) = (int)(longValue >> 3);
919 else if (ptrMem == 8 && ptrFile == 4)
930 *((
long64 *)dst) = *((
int *)src);
935 printf(
"%d %d\n", ptrFile, ptrMem);
936 assert(0 &&
"Invalid pointer len");
941void bFile::getMatchingFileDNA(
short *dna_addr,
const char *lookupName,
const char *lookupType,
char *strcData,
char *data,
bool fixupPointers)
947 int len = dna_addr[1];
950 for (
int i = 0; i < len; i++, dna_addr += 2)
959 if ((strcmp(type,
"short") == 0) && (strcmp(name,
"int") == 0))
965 if (strcmp(lookupName, name) == 0)
986 cpc = (
char *)strcData;
989 for (
int a = 0; a < arrayLen; a++)
1011 else if (strcmp(type, lookupType) == 0)
1012 memcpy(strcData, data, eleLen);
1014 getElement(arrayLen, lookupType, type, data, strcData);
1026 short *old = firstStruct;
1027 int elementLength = old[1];
1030 for (
int i = 0; i < elementLength; i++, old += 2)
1036 if (strcmp(lookupName, name) == 0)
1038 if (strcmp(type, lookupType) == 0)
1054 if (dna_nr == -1)
return;
1059 int elementLen = strc[1];
1065 for (
int i = 0; i < elementLen; i++, strc += 2)
1071 if (strc[0] >= first && name[0] !=
'*')
1082 for (
int i = 0; i < arrayLen; i++)
1084 swapStruct(old_nr, tmpBuf, ignoreEndianFlag);
1085 tmpBuf +=
size / arrayLen;
1094 swapData(buf, strc[0], arrayLen, ignoreEndianFlag);
1109 void **ptrptr = (
void **)cur;
1110 void *ptr = *ptrptr;
1126 void **ptrptr = (
void **)cur;
1134 int blockLen = block->
len / ptrFile;
1139 char *newPtr =
new char[blockLen * ptrMem];
1141 memset(newPtr, 0, blockLen * ptrMem);
1143 void **onarray = (
void **)onptr;
1144 char *oldPtr = (
char *)onarray;
1147 while (blockLen-- > 0)
1152 void **tptr = (
void **)(newPtr + p * ptrMem);
1171 short oldLen = fileDna->
getLength(oldStruct[0]);
1175 for (
int block = 0; block < dataChunk.
nr; block++)
1188 short firstStructType = fileDna->
getStruct(0)[0];
1190 char *elemPtr = strcPtr;
1192 short int *oldStruct = fileDna->
getStruct(dna_nr);
1194 int elementLength = oldStruct[1];
1199 for (
int ele = 0; ele < elementLength; ele++, oldStruct += 2)
1201 memType = fileDna->
getType(oldStruct[0]);
1202 memName = fileDna->
getName(oldStruct[1]);
1205 if (memName[0] ==
'*')
1209 void **array = (
void **)elemPtr;
1210 for (
int a = 0; a < arrayLen; a++)
1214 for (
int i = 0; i < recursion; i++)
1219 printf(
"<%s type=\"pointer\"> ", &memName[1]);
1220 printf(
"%p ", array[a]);
1221 printf(
"</%s>\n", &memName[1]);
1229 void **ptrptr = (
void **)elemPtr;
1230 void *ptr = *ptrptr;
1233 for (
int i = 0; i < recursion; i++)
1237 printf(
"<%s type=\"pointer\"> ", &memName[1]);
1239 printf(
"</%s>\n", &memName[1]);
1247 if (memName[1] ==
'*' && ptrptr && *ptrptr)
1250 void **array = (
void **)*(ptrptr);
1251 void *np = array[0];
1256 if (np) array[n] = np;
1270 if (oldStruct[0] >= firstStructType)
1280 for (
int i = 0; i < recursion; i++)
1287 printf(
"<%s type=\"%s\" count=%d>\n", cleanName, memType, arrayLen);
1291 printf(
"<%s type=\"%s\">\n", cleanName, memType);
1295 for (
int i = 0; i < arrayLen; i++)
1301 for (
int i = 0; i < recursion; i++)
1305 printf(
"</%s>\n", cleanName);
1315 printf(
"too long\n");
1321 bool isIntegerType = (strcmp(memType,
"char") == 0) || (strcmp(memType,
"int") == 0) || (strcmp(memType,
"short") == 0);
1325 const char *newtype =
"int";
1328 char *tmp = elemPtr;
1329 dbPtr = &dbarray[0];
1336 getElement(arrayLen, newtype, memType, tmp, (
char *)dbPtr);
1337 for (i = 0; i < recursion; i++)
1340 printf(
"<%s type=\"%s\">", cleanName, memType);
1342 printf(
"<%s type=\"%s\" count=%d>", cleanName, memType, arrayLen);
1343 for (i = 0; i < arrayLen; i++)
1344 printf(
" %d ", dbPtr[i]);
1345 printf(
"</%s>\n", cleanName);
1350 const char *newtype =
"double";
1353 char *tmp = elemPtr;
1354 dbPtr = &dbarray[0];
1358 getElement(arrayLen, newtype, memType, tmp, (
char *)dbPtr);
1359 for (i = 0; i < recursion; i++)
1366 printf(
"<%s type=\"%s\">", memName, memType);
1370 printf(
"<%s type=\"%s\" count=%d>", cleanName, memType, arrayLen);
1372 for (i = 0; i < arrayLen; i++)
1373 printf(
" %f ", dbPtr[i]);
1374 printf(
"</%s>\n", cleanName);
1405 printf(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
1407 printf(
"<bullet_physics version=%d itemcount = %d>\n",
btGetVersion(), numitems);
1409 for (
int i = 0; i <
m_chunks.size(); i++)
1417 char *oldType = fileDna->
getType(oldStruct[0]);
1420 printf(
" <%s pointer=%p>\n", oldType, dataChunk.
oldPtr);
1425 printf(
" </%s>\n", oldType);
1434 printf(
"</bullet_physics>\n");
1452 for (i = 0; i <
m_chunks.size(); i++)
1462 for (i = 0; i <
m_chunks.size(); i++)
1465 char *codeptr = (
char *)&dataChunk.
code;
1466 char codestr[5] = {codeptr[0], codeptr[1], codeptr[2], codeptr[3], 0};
1468 short *newStruct = dna->
getStruct(dataChunk.dna_nr);
1469 char *typeName = dna->
getType(newStruct[0]);
1470 printf(
"%3d: %s ", i, typeName);
1472 printf(
"code=%s ", codestr);
1474 printf(
"ptr=%p ", dataChunk.oldPtr);
1475 printf(
"len=%d ", dataChunk.len);
1476 printf(
"nr=%d ", dataChunk.nr);
1477 if (dataChunk.nr != 1)
1488 ifd.just_print_it = 1;
1489 for (i=0; i<bf->m_blocks.size(); ++i)
1491 BlendBlock* bb = bf->m_blocks[i];
1492 printf(
"tag='%s'\tptr=%p\ttype=%s\t[%4d]", bb->tag, bb,bf->types[bb->type_index].name,bb->m_array_entries_.size());
1493 block_ID_finder(bb, bf, &ifd);
1503 for (
int i = 0; i <
m_chunks.size(); i++)
1508 short *oldStruct, *curStruct;
1509 char *oldType, *newType;
1510 int curLen, reverseOld;
1513 oldType = fileDna->
getType(oldStruct[0]);
1518 if ((reverseOld != -1))
1526 assert((strcmp(oldType, newType) == 0) &&
"internal error, struct mismatch!");
1529 dataChunk.
dna_nr = reverseOld;
1530 if (strcmp(
"Link", oldType) != 0)
1532 dataChunk.
len = curLen * dataChunk.
nr;
1540 fwrite(&dataChunk,
sizeof(
bChunkInd), 1, fp);
1542 short int *curStruct1;
1544 assert(curStruct1 == curStruct);
1549 fwrite(cur, dataChunk.
len, 1, fp);
1553 printf(
"serious error, struct mismatch: don't write\n");
1562 bool varies =
false;
1587 if ((chunk.
code & 0xFFFF) == 0)
1595 memcpy(dataChunk, &chunk,
sizeof(
bChunkInd));
1604 if ((c.
code & 0xFFFF) == 0)
1612 memcpy(dataChunk, &c,
sizeof(
bChunkInd));
1644 if ((chunk.
code & 0xFFFF) == 0)
1652 memcpy(dataChunk, &chunk,
sizeof(
bChunkInd));
1661 if ((c.
code & 0xFFFF) == 0)
1668 memcpy(dataChunk, &c,
sizeof(
bChunkInd));
1672 if (dataChunk->
len < 0)
1676 print (
"----------");
1677 print (dataChunk->
code);
1678 print (dataChunk->
len);
1679 print (dataChunk->old);
1680 print (dataChunk->
dna_nr);
1681 print (dataChunk->
nr);
#define SWITCH_LONGINT(a)
#define setEle(value, current, type, cast, size, ptr)
const char * getCleanName(const char *memName, char *buffer)
static void getElement(int arrayLen, const char *cur, const char *old, char *oldPtr, char *curData)
#define SIZEOFBLENDERHEADER
#define getEle(value, current, type, cast, size, ptr)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
const T & btMin(const T &a, const T &b)
T * btAlignPointer(T *unalignedPtr, size_t alignment)
align a pointer to the provided alignment, upwards
static int getOffset(int flags)
static short swapShort(short sht)
static int swapInt(int inte)
int getArraySizeNew(short name)
void initCmpFlags(bDNA *memDNA)
void init(char *data, int len, bool swap=false)
bool flagEqual(int dna_nr)
void dumpTypeDefinitions()
bool lessThan(bDNA *other)
int getReverseType(short type)
short * getStruct(int ind)
int getElementSize(short type, short name)
bFile(const char *filename, const char headerString[7])
virtual void addDataBlock(char *dataBlock)=0
void swap(char *head, class bChunkInd &ch, bool ignoreEndianFlag)
btAlignedObjectArray< char * > m_pointerFixupArray
void resolvePointersChunk(const bChunkInd &dataChunk, int verboseMode)
this loop only works fine if the Blender DNA structure of the file matches the headerfiles
virtual void parseInternal(int verboseMode, char *memDna, int memDnaLength)
void writeFile(const char *fileName)
virtual void parseData()=0
btAlignedObjectArray< char * > m_pointerPtrFixupArray
virtual void setFileDNA(int verboseMode, char *buffer, int len)
virtual void parseHeader()
btAlignedObjectArray< bChunkInd > m_chunks
void parseStruct(char *strcPtr, char *dtPtr, int old_dna, int new_dna, bool fixupPointers)
bPtrMap & getLibPointers()
btHashMap< btHashPtr, bChunkInd > m_chunkPtrPtrMap
void dumpChunks(bDNA *dna)
void resolvePointers(int verboseMode)
Resolve pointers replaces the original pointers in structures, and linked lists by the new in-memory ...
char * readStruct(char *head, class bChunkInd &chunk)
virtual void writeChunks(FILE *fp, bool fixupPointers)
void swapLen(char *dataPtr)
int getNextBlock(bChunkInd *dataChunk, const char *dataPtr, const int flags)
void safeSwapPtr(char *dst, const char *src)
char * getFileElement(short *firstStruct, char *lookupName, char *lookupType, char *data, short **foundPos)
int resolvePointersStructRecursive(char *strcPtr, int old_dna, int verboseMode, int recursion)
void resolvePointersMismatch()
void swapStruct(int dna_nr, char *data, bool ignoreEndianFlag)
void swapData(char *data, short type, int arraySize, bool ignoreEndianFlag)
void * findLibPointer(void *ptr)
void getMatchingFileDNA(short *old, const char *lookupName, const char *lookupType, char *strcData, char *data, bool fixupPointers)
int size() const
return the number of elements in the array
void push_back(const T &_Val)
const T & at(int n) const
void insert(const Key &key, const Value &value)
const Value * find(const Key &key) const
@ FD_VERBOSE_DUMP_DNA_TYPE_DEFINITIONS