1 /* Copyright (C) 2006 Charlie C
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 * 2. Altered source versions must be plainly marked as such, and must not be
16 * misrepresented as being the original software.
17 * 3. This notice may not be removed or altered from any source distribution.
21 #include "bBlenderFile.h"
22 #include "btBulletFile.h"
28 bool isBulletFile = false;
30 using namespace bParse;
31 typedef std::string bString;
33 ///////////////////////////////////////////////////////////////////////////////
34 typedef std::map<bString, bString> bStringMap;
35 typedef std::vector<class bVariable> bVariableList;
36 typedef std::vector<bString> bStringList;
39 ///////////////////////////////////////////////////////////////////////////////
40 static FILE *dump = 0;
42 static bStringMap mStructs;
45 ///////////////////////////////////////////////////////////////////////////////
60 bString memberVariable;
61 bString memberDataType;
65 void initialize(bString dataType, bString variable, bStringMap refDataTable);
79 ///////////////////////////////////////////////////////////////////////////////
80 bool dataTypeStandard(bString dataType)
82 if (dataType == "char")
84 if (dataType == "short")
86 if (dataType == "int")
88 if (dataType == "long")
90 if (dataType == "float")
92 if (dataType == "double")
94 if (dataType == "void")
96 if (dataType == "btScalar")
101 ///////////////////////////////////////////////////////////////////////////////
102 void writeTemplate(short *structData)
104 bString type = mDNA->getType(structData[0]);
105 bString className=type;
106 bString prefix = isBulletFile? "bullet_" : "blender_";
108 int thisLen = structData[1];
111 bString fileName = prefix+type;
113 bVariableList dataTypes;
114 bStringMap includeFiles;
117 for (int dataVal =0; dataVal<thisLen; dataVal++, structData+=2)
119 bString dataType = mDNA->getType(structData[0]);
120 bString dataName = mDNA->getName(structData[1]);
122 bString newDataType = "";
123 bString newDataName = "";
125 bStringMap::iterator addB = mStructs.find(dataType);
126 if (addB != mStructs.end())
128 newDataType = addB->second;
129 newDataName = dataName;
134 if (dataTypeStandard(dataType))
136 newDataType = dataType;
137 newDataName = dataName;
142 // set it to an empty struct
143 // if it's not a ptr generate an error
144 newDataType = "bInvalidHandle";
145 newDataName = dataName;
147 if (dataName[0] != '*')
154 if (!newDataType.empty() && !newDataName.empty())
156 bVariable var = bVariable();
157 var.initialize(newDataType, newDataName, mStructs);
158 dataTypes.push_back(var);
163 bStringMap::iterator include = mStructs.find(dataType);
164 if (include != mStructs.end())
166 if (dataName[0] != '*')
168 if (includeFiles.find(dataType)== includeFiles.end())
170 includeFiles[dataType]=prefix+dataType;
177 fprintf(dump, "###############################################################\n");
178 fprintf(dump, "%s = bStructClass()\n", fileName.c_str());
179 fprintf(dump, "%s.name = '%s'\n", fileName.c_str(), className.c_str());
180 fprintf(dump, "%s.filename = '%s'\n", fileName.c_str(), fileName.c_str());
182 bVariableList::iterator vars = dataTypes.begin();
183 while (vars!= dataTypes.end())
185 fprintf(dump, "%s.dataTypes.append('%s %s')\n", fileName.c_str(), vars->dataType.c_str(), vars->variableName.c_str());
189 bStringMap::iterator inc = includeFiles.begin();
190 while (inc != includeFiles.end())
192 fprintf(dump, "%s.includes.append('%s.h')\n", fileName.c_str(), inc->second.c_str());
195 fprintf(dump, "DataTypeList.append(%s)\n", fileName.c_str());
199 ///////////////////////////////////////////////////////////////////////////////
202 "class bStructClass:\n"
203 " def __init__(self):\n"
204 " self.name = \"\";\n"
205 " self.filename = \"\";\n"
206 " self.includes = []\n"
207 " self.dataTypes = []\n"
209 "DataTypeList = []\n"
213 ///////////////////////////////////////////////////////////////////////////////
214 int main(int argc,char** argv)
216 using namespace bParse;
217 dump = fopen("dump.py", "w");
220 fprintf(dump, "%s\n", data);
223 char* filename = "../../../Demos/SerializeDemo/testFile.bullet";
228 bString fileStr(filename);
229 bString extension(".bullet");
231 int index2 = fileStr.find(extension);
236 FILE* fp = fopen (filename,"rb");
240 printf("error: file not found %s\n",filename);
247 long currentpos = ftell(fp); /* save current cursor position */
251 fseek(fp, 0, SEEK_END); /* seek to end */
252 newpos = ftell(fp); /* find position of end -- this is the length */
253 fseek(fp, currentpos, SEEK_SET); /* restore previous cursor position */
257 memBuf = (char*)malloc(len);
258 bytesRead = fread(memBuf,len,1,fp);
265 btBulletFile f(memBuf,len);
266 swap = (f.getFlags() & FD_ENDIAN_SWAP)!=0;
269 bBlenderFile f(memBuf,len);
270 swap = (f.getFlags() & FD_ENDIAN_SWAP)!=0;
277 char *blenderData = memBuf;
281 char *tempBuffer = blenderData;
282 for (int i=0; i<len; i++)
284 // looking for the data's starting position
285 // and the start of SDNA decls
287 if (!mDataStart && strncmp(tempBuffer, "REND", 4)==0)
289 if (!sdnaPos && strncmp(tempBuffer, "SDNA", 4)==0)
292 if (mDataStart && sdnaPos) break;
298 FILE* fpdna = fopen("dnaString.txt","w");
301 for (int i=0;i<len-sdnaPos;i++)
303 int dnaval = (memBuf+sdnaPos)[i];
307 sprintf(buf,"%d,\n",dnaval);
311 sprintf(buf,"%d,",dnaval);
315 fwrite(buf,strlen(buf),1,fpdna);
323 //mDNA->initMemory();
325 mDNA->init(memBuf+sdnaPos, len-sdnaPos, swap);
328 for (int i=0; i<mDNA->getNumStructs(); i++)
330 short *structData = mDNA->getStruct(i);
331 bString type = mDNA->getType(structData[0]);
333 bString className = type;
334 mStructs[type]=className;
338 for (int i=0; i<mDNA->getNumStructs(); i++)
340 short *structData = mDNA->getStruct(i);
341 writeTemplate(structData);
348 ///////////////////////////////////////////////////////////////////////////////
351 ///////////////////////////////////////////////////////////////////////////////
352 int _getArraySize(char* str)
355 char stri[100], *cp=0;
356 int len = (int)strlen(str);
358 memcpy(stri, str, len+1);
359 for (a=0; a<len; a++)
363 else if ( str[a]==']' && cp)
372 ///////////////////////////////////////////////////////////////////////////////
373 bVariable::bVariable()
374 : dataType("invalid"),
375 variableName("invalid"),
382 isFunctionPtr(false),
388 isCommentedOut(false),
389 isGeneratedType(false),
395 ///////////////////////////////////////////////////////////////////////////////
396 bVariable::~bVariable()
399 variableName.clear();
403 ///////////////////////////////////////////////////////////////////////////////
404 void bVariable::initialize(bString type, bString variable, bStringMap refDataTable)
407 variableName = variable;
409 if (variableName[0] == '*')
412 if (variableName[1] == '*')
415 if (variableName[0] == '(')
416 if (variableName[1] == '*')
417 isFunctionPtr = true;
419 if (variableName[variableName.size()-1] == ']')
426 if (type == "ListBase")
429 if (variableName[0] == 'p')
431 bString sub = variableName.substr(0,3);
435 if (dataType[0] == '/' && dataType[1] == '/')
436 isCommentedOut = true;
439 if (refDataTable.find(dataType) != refDataTable.end())
440 isGeneratedType = true;
444 // replace valid float arrays
445 if (dataType == "float" && isArray)
447 int size = _getArraySize((char*)variableName.c_str());
451 variableName = variableName.substr(0, variableName.find_first_of("["));
456 variableName = variableName.substr(0, variableName.find_first_of("["));
460 memberDataType = dataType;
461 functionArgs = variableName;