123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- /* Copyright (C) 2006 Charlie C
- *
- * 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.
- */
- #include <sstream>
- #include "bDNA.h"
- #include "bBlenderFile.h"
- #include "btBulletFile.h"
- #include "bCommon.h"
- #include <map>
- #include <vector>
- #include <string.h>
- bool isBulletFile = false;
- using namespace bParse;
- typedef std::string bString;
- ///////////////////////////////////////////////////////////////////////////////
- typedef std::map<bString, bString> bStringMap;
- typedef std::vector<class bVariable> bVariableList;
- typedef std::vector<bString> bStringList;
- ///////////////////////////////////////////////////////////////////////////////
- static FILE *dump = 0;
- static bDNA *mDNA =0;
- static bStringMap mStructs;
- ///////////////////////////////////////////////////////////////////////////////
- class bVariable
- {
- public:
- bVariable();
- ~bVariable();
- bString dataType;
- bString variableName;
- bString functionName;
- bString classCtor;
- bString memberVariable;
- bString memberDataType;
- bString functionArgs;
- void initialize(bString dataType, bString variable, bStringMap refDataTable);
- bool isPtr;
- bool isFunctionPtr;
- bool isPtrToPtr;
- bool isArray;
- bool isCharArray;
- bool isListBase;
- bool isPadding;
- bool isCommentedOut;
- bool isGeneratedType;
- bool isbString;
- };
- ///////////////////////////////////////////////////////////////////////////////
- bool dataTypeStandard(bString dataType)
- {
- if (dataType == "char")
- return true;
- if (dataType == "short")
- return true;
- if (dataType == "int")
- return true;
- if (dataType == "long")
- return true;
- if (dataType == "float")
- return true;
- if (dataType == "double")
- return true;
- if (dataType == "void")
- return true;
- if (dataType == "btScalar")
- return true;
- return false;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void writeTemplate(short *structData)
- {
- bString type = mDNA->getType(structData[0]);
- bString className=type;
- bString prefix = isBulletFile? "bullet_" : "blender_";
-
- int thisLen = structData[1];
- structData+=2;
- bString fileName = prefix+type;
- bVariableList dataTypes;
- bStringMap includeFiles;
- for (int dataVal =0; dataVal<thisLen; dataVal++, structData+=2)
- {
- bString dataType = mDNA->getType(structData[0]);
- bString dataName = mDNA->getName(structData[1]);
- {
- bString newDataType = "";
- bString newDataName = "";
-
- bStringMap::iterator addB = mStructs.find(dataType);
- if (addB != mStructs.end())
- {
- newDataType = addB->second;
- newDataName = dataName;
- }
- else
- {
- if (dataTypeStandard(dataType))
- {
- newDataType = dataType;
- newDataName = dataName;
- }
- else
- {
- // Unresolved
- // set it to an empty struct
- // if it's not a ptr generate an error
- newDataType = "bInvalidHandle";
- newDataName = dataName;
- if (dataName[0] != '*')
- {
- }
- }
- }
- if (!newDataType.empty() && !newDataName.empty())
- {
- bVariable var = bVariable();
- var.initialize(newDataType, newDataName, mStructs);
- dataTypes.push_back(var);
- }
- }
- bStringMap::iterator include = mStructs.find(dataType);
- if (include != mStructs.end())
- {
- if (dataName[0] != '*')
- {
- if (includeFiles.find(dataType)== includeFiles.end())
- {
- includeFiles[dataType]=prefix+dataType;
- }
- }
- }
- }
- fprintf(dump, "###############################################################\n");
- fprintf(dump, "%s = bStructClass()\n", fileName.c_str());
- fprintf(dump, "%s.name = '%s'\n", fileName.c_str(), className.c_str());
- fprintf(dump, "%s.filename = '%s'\n", fileName.c_str(), fileName.c_str());
- bVariableList::iterator vars = dataTypes.begin();
- while (vars!= dataTypes.end())
- {
- fprintf(dump, "%s.dataTypes.append('%s %s')\n", fileName.c_str(), vars->dataType.c_str(), vars->variableName.c_str());
- vars++;
- }
- bStringMap::iterator inc = includeFiles.begin();
- while (inc != includeFiles.end())
- {
- fprintf(dump, "%s.includes.append('%s.h')\n", fileName.c_str(), inc->second.c_str());
- inc++;
- }
- fprintf(dump, "DataTypeList.append(%s)\n", fileName.c_str());
- }
- ///////////////////////////////////////////////////////////////////////////////
- char data[]={
- "\n"
- "class bStructClass:\n"
- " def __init__(self):\n"
- " self.name = \"\";\n"
- " self.filename = \"\";\n"
- " self.includes = []\n"
- " self.dataTypes = []\n"
- "\n\n"
- "DataTypeList = []\n"
- };
- ///////////////////////////////////////////////////////////////////////////////
- int main(int argc,char** argv)
- {
- using namespace bParse;
- dump = fopen("dump.py", "w");
- if (!dump) return 0;
- fprintf(dump, "%s\n", data);
-
- char* filename = "../../../../data/r2d2_multibody.bullet";
-
- if (argc==2)
- filename = argv[1];
- bString fileStr(filename);
- bString extension(".bullet");
- int index2 = fileStr.find(extension);
- if (index2>=0)
- isBulletFile=true;
-
- FILE* fp = fopen (filename,"rb");
- if (!fp)
- {
- printf("error: file not found %s\n",filename);
- exit(0);
- }
- char* memBuf = 0;
- int len = 0;
- long currentpos = ftell(fp); /* save current cursor position */
- long newpos;
- int bytesRead;
- fseek(fp, 0, SEEK_END); /* seek to end */
- newpos = ftell(fp); /* find position of end -- this is the length */
- fseek(fp, currentpos, SEEK_SET); /* restore previous cursor position */
-
- len = newpos;
-
- memBuf = (char*)malloc(len);
- bytesRead = fread(memBuf,len,1,fp);
- bool swap = false;
-
- if (isBulletFile)
- {
- btBulletFile f(memBuf,len);
- swap = (f.getFlags() & FD_ENDIAN_SWAP)!=0;
- } else
- {
- bBlenderFile f(memBuf,len);
- swap = (f.getFlags() & FD_ENDIAN_SWAP)!=0;
- }
-
-
- char *blenderData = memBuf;
- int sdnaPos=0;
- int mDataStart = 12;
- char *tempBuffer = blenderData;
- for (int i=0; i<len; i++)
- {
- // looking for the data's starting position
- // and the start of SDNA decls
- if (!mDataStart && strncmp(tempBuffer, "REND", 4)==0)
- mDataStart = i;
- if (!sdnaPos && strncmp(tempBuffer, "SDNA", 4)==0)
- sdnaPos = i;
- if (mDataStart && sdnaPos) break;
- tempBuffer++;
- }
-
- FILE* fpdna = fopen("dnaString.txt","w");
- char buf[1024];
- for (int i=0;i<len-sdnaPos;i++)
- {
- int dnaval = (memBuf+sdnaPos)[i];
- if ((i%32)==0)
- {
- sprintf(buf,"%d,\n",dnaval);
-
- } else
- {
- sprintf(buf,"%d,",dnaval);
- }
-
-
- fwrite(buf,strlen(buf),1,fpdna);
- }
- fclose(fpdna);
- mDNA = new bDNA();
- //mDNA->initMemory();
-
- mDNA->init(memBuf+sdnaPos, len-sdnaPos, swap);
-
- for (int i=0; i<mDNA->getNumStructs(); i++)
- {
- short *structData = mDNA->getStruct(i);
- bString type = mDNA->getType(structData[0]);
- bString className = type;
- mStructs[type]=className;
- }
- for (int i=0; i<mDNA->getNumStructs(); i++)
- {
- short *structData = mDNA->getStruct(i);
- writeTemplate(structData);
- }
- delete mDNA;
- fclose(dump);
- return 0;
- }
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- int _getArraySize(char* str)
- {
- int a, mul=1;
- char stri[100], *cp=0;
- int len = (int)strlen(str);
- memcpy(stri, str, len+1);
- for (a=0; a<len; a++)
- {
- if (str[a]== '[')
- cp= &(stri[a+1]);
- else if ( str[a]==']' && cp)
- {
- stri[a]= 0;
- mul*= atoi(cp);
- }
- }
- return mul;
- }
- ///////////////////////////////////////////////////////////////////////////////
- bVariable::bVariable()
- : dataType("invalid"),
- variableName("invalid"),
- functionName(""),
- classCtor(""),
- memberVariable(""),
- memberDataType(""),
- functionArgs(""),
- isPtr(false),
- isFunctionPtr(false),
- isPtrToPtr(false),
- isArray(false),
- isCharArray(false),
- isListBase(false),
- isPadding(false),
- isCommentedOut(false),
- isGeneratedType(false),
- isbString(false)
- {
- }
- ///////////////////////////////////////////////////////////////////////////////
- bVariable::~bVariable()
- {
- dataType.clear();
- variableName.clear();
- }
- ///////////////////////////////////////////////////////////////////////////////
- void bVariable::initialize(bString type, bString variable, bStringMap refDataTable)
- {
- dataType = type;
- variableName = variable;
- if (variableName[0] == '*')
- {
- isPtr = true;
- if (variableName[1] == '*')
- isPtrToPtr = true;
- }
- if (variableName[0] == '(')
- if (variableName[1] == '*')
- isFunctionPtr = true;
- if (variableName[variableName.size()-1] == ']')
- {
- isArray = true;
- if (type == "char")
- isCharArray = true;
- }
- if (type == "ListBase")
- isListBase = true;
- if (variableName[0] == 'p')
- {
- bString sub = variableName.substr(0,3);
- if (sub == "pad")
- isPadding = true;
- }
- if (dataType[0] == '/' && dataType[1] == '/')
- isCommentedOut = true;
- if (refDataTable.find(dataType) != refDataTable.end())
- isGeneratedType = true;
- if (!isBulletFile)
- {
- // replace valid float arrays
- if (dataType == "float" && isArray)
- {
- int size = _getArraySize((char*)variableName.c_str());
- if (size==3)
- {
- dataType = "vec3f";
- variableName = variableName.substr(0, variableName.find_first_of("["));
- }
- if (size==4)
- {
- dataType = "vec4f";
- variableName = variableName.substr(0, variableName.find_first_of("["));
- }
- }
- }
- memberDataType = dataType;
- functionArgs = variableName;
- }
- // eof
|