makesdna.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /// Current makesdna.cpp is a from Blender, but we will completely rewrite it in C++ under a ZLib license
  2. /// The Original version is at https://svn.blender.org/svnroot/bf-blender/trunk/blender/source/blender/makesdna/intern/makesdna.c
  3. /**
  4. * $Id$
  5. *
  6. * ***** BEGIN GPL LICENSE BLOCK *****
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  23. * All rights reserved.
  24. *
  25. * The Original Code is: all of this file.
  26. *
  27. * Contributor(s): none yet.
  28. *
  29. * ***** END GPL LICENSE BLOCK *****
  30. *
  31. * Struct muncher for making SDNA
  32. *
  33. * Originally by Ton, some mods by Frank, and some cleaning and
  34. * extension by Nzc.
  35. *
  36. * Makesdna creates a .c file with a long string of numbers that
  37. * encode the Blender file format. It is fast, because it is basically
  38. * a binary dump. There are some details to mind when reconstructing
  39. * the file (endianness and byte-alignment).
  40. *
  41. * This little program scans all structs that need to be serialized,
  42. * and determined the names and types of all members. It calculates
  43. * how much memory (on disk or in ram) is needed to store that struct,
  44. * and the offsets for reaching a particular one.
  45. *
  46. * There is a facility to get verbose output from sdna. Search for
  47. * debugSDNA. This int can be set to 0 (no output) to some int. Higher
  48. * numbers give more output.
  49. * */
  50. #ifdef __cplusplus
  51. extern "C"
  52. {
  53. #endif
  54. #if defined(_WIN32) && !defined(FREE_WINDOWS)
  55. /* The __intXX are built-in types of the visual complier! So we don't
  56. * need to include anything else here. */
  57. typedef signed __int8 int8_t;
  58. typedef signed __int16 int16_t;
  59. typedef signed __int32 int32_t;
  60. typedef signed __int64 int64_t;
  61. typedef unsigned __int8 uint8_t;
  62. typedef unsigned __int16 uint16_t;
  63. typedef unsigned __int32 uint32_t;
  64. typedef unsigned __int64 uint64_t;
  65. #ifndef _INTPTR_T_DEFINED
  66. #ifdef _WIN64
  67. typedef __int64 btintptr_t;
  68. #else
  69. typedef long btintptr_t;
  70. #endif
  71. #else
  72. typedef intptr_t btintptr_t;
  73. #endif
  74. #elif defined(__linux__) || defined(__NetBSD__)
  75. /* Linux-i386, Linux-Alpha, Linux-ppc */
  76. #include <stdint.h>
  77. typedef intptr_t btintptr_t;
  78. #elif defined(__APPLE__)
  79. #include <inttypes.h>
  80. typedef intptr_t btintptr_t;
  81. #elif defined(FREE_WINDOWS)
  82. #include <stdint.h>
  83. #else
  84. /* FreeBSD, Irix, Solaris */
  85. #include <sys/types.h>
  86. #endif /* ifdef platform for types */
  87. #ifdef __cplusplus
  88. }
  89. #endif
  90. #include <string.h>
  91. #include <stdlib.h>
  92. #include <stdio.h>
  93. //#include "DNA_sdna_types.h"
  94. // include files for automatic dependancies
  95. #include "DNA_rigidbody.h"
  96. #include "LinearMath/btVector3.h"
  97. #include "LinearMath/btQuaternion.h"
  98. #include "LinearMath/btMatrix3x3.h"
  99. #include "LinearMath/btTransform.h"
  100. #include "BulletCollision/BroadphaseCollision/btQuantizedBvh.h"
  101. #include "BulletCollision/CollisionShapes/btCollisionShape.h"
  102. #include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
  103. #include "BulletCollision/CollisionShapes/btConvexInternalShape.h"
  104. #include "BulletCollision/CollisionShapes/btMultiSphereShape.h"
  105. #include "BulletCollision/CollisionShapes/btConvexHullShape.h"
  106. #include "BulletCollision/CollisionShapes/btStridingMeshInterface.h"
  107. #include "BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h"
  108. #include "BulletCollision/CollisionShapes/btScaledBvhTriangleMeshShape.h"
  109. #include "BulletCollision/CollisionShapes/btCompoundShape.h"
  110. #include "BulletCollision/CollisionShapes/btCylinderShape.h"
  111. #include "BulletCollision/CollisionShapes/btConeShape.h"
  112. #include "BulletCollision/CollisionShapes/btCapsuleShape.h"
  113. #include "BulletCollision/CollisionShapes/btTriangleInfoMap.h"
  114. #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h"
  115. #include "BulletCollision/Gimpact/btGImpactShape.h"
  116. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  117. #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
  118. #include "BulletDynamics/ConstraintSolver/btPoint2PointConstraint.h"
  119. #include "BulletDynamics/ConstraintSolver/btHingeConstraint.h"
  120. #include "BulletDynamics/ConstraintSolver/btConeTwistConstraint.h"
  121. #include "BulletDynamics/ConstraintSolver/btGeneric6DofConstraint.h"
  122. #include "BulletDynamics/ConstraintSolver/btGeneric6DofSpringConstraint.h"
  123. #include "BulletDynamics/ConstraintSolver/btGeneric6DofSpring2Constraint.h"
  124. #include "BulletDynamics/ConstraintSolver/btSliderConstraint.h"
  125. #include "BulletDynamics/ConstraintSolver/btGearConstraint.h"
  126. #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
  127. #include "BulletDynamics/Dynamics/btDynamicsWorld.h"
  128. #include "BulletDynamics/Dynamics/btRigidBody.h"
  129. #include "BulletSoftBody/btSoftBodyData.h"
  130. #include "BulletDynamics/Featherstone/btMultiBody.h"
  131. #include "BulletDynamics/Featherstone/btMultiBodyLinkCollider.h"
  132. #ifdef HAVE_CONFIG_H
  133. #include <config.h>
  134. #endif
  135. #define SDNA_MAX_FILENAME_LENGTH 255
  136. /* Included the path relative from /source/blender/ here, so we can move */
  137. /* headers around with more freedom. */
  138. char *includefiles[] = {
  139. // if you add files here, please add them at the end
  140. // of makesdna.c (this file) as well
  141. "../makesdna/DNA_rigidbody.h",
  142. "../../../src/LinearMath/btVector3.h",
  143. "../../../src/LinearMath/btQuaternion.h",
  144. "../../../src/LinearMath/btMatrix3x3.h",
  145. "../../../src/LinearMath/btTransform.h",
  146. "../../../src/BulletCollision/BroadphaseCollision/btQuantizedBvh.h",
  147. "../../../src/BulletCollision/CollisionShapes/btCollisionShape.h",
  148. "../../../src/BulletCollision/CollisionShapes/btStaticPlaneShape.h",
  149. "../../../src/BulletCollision/CollisionShapes/btConvexInternalShape.h",
  150. "../../../src/BulletCollision/CollisionShapes/btMultiSphereShape.h",
  151. "../../../src/BulletCollision/CollisionShapes/btStridingMeshInterface.h",
  152. "../../../src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h",
  153. "../../../src/BulletCollision/CollisionShapes/btScaledBvhTriangleMeshShape.h",
  154. "../../../src/BulletCollision/CollisionShapes/btCompoundShape.h",
  155. "../../../src/BulletCollision/CollisionShapes/btCylinderShape.h",
  156. "../../../src/BulletCollision/CollisionShapes/btConeShape.h",
  157. "../../../src/BulletCollision/CollisionShapes/btCapsuleShape.h",
  158. "../../../src/BulletCollision/CollisionShapes/btTriangleInfoMap.h",
  159. "../../../src/BulletCollision/NarrowPhaseCollision/btPersistentManifold.h",
  160. "../../../src/BulletCollision/Gimpact/btGImpactShape.h",
  161. "../../../src/BulletCollision/CollisionShapes/btConvexHullShape.h",
  162. "../../../src/BulletCollision/CollisionDispatch/btCollisionObject.h",
  163. "../../../src/BulletDynamics/ConstraintSolver/btContactSolverInfo.h",
  164. "../../../src/BulletDynamics/Dynamics/btDynamicsWorld.h",
  165. "../../../src/BulletDynamics/Dynamics/btRigidBody.h",
  166. "../../../src/BulletDynamics/ConstraintSolver/btTypedConstraint.h",
  167. "../../../src/BulletDynamics/ConstraintSolver/btPoint2PointConstraint.h",
  168. "../../../src/BulletDynamics/ConstraintSolver/btHingeConstraint.h",
  169. "../../../src/BulletDynamics/ConstraintSolver/btConeTwistConstraint.h",
  170. "../../../src/BulletDynamics/ConstraintSolver/btGeneric6DofConstraint.h",
  171. "../../../src/BulletDynamics/ConstraintSolver/btGeneric6DofSpringConstraint.h",
  172. "../../../src/BulletDynamics/ConstraintSolver/btGeneric6DofSpring2Constraint.h",
  173. "../../../src/BulletDynamics/ConstraintSolver/btSliderConstraint.h",
  174. "../../../src/BulletDynamics/ConstraintSolver/btGearConstraint.h",
  175. "../../../src/BulletSoftBody/btSoftBodyData.h",
  176. "../../../src/BulletDynamics/Featherstone/btMultiBody.h",
  177. "../../../src/BulletDynamics/Featherstone/btMultiBodyLinkCollider.h",
  178. // empty string to indicate end of includefiles
  179. ""};
  180. void *malloc_and_setzero(int numbytes)
  181. {
  182. char *buf = (char *)malloc(numbytes);
  183. memset(buf, 0, numbytes);
  184. return buf;
  185. }
  186. int maxdata = 500000, maxnr = 50000;
  187. int nr_names = 0;
  188. int nr_types = 0;
  189. int nr_structs = 0;
  190. char **names, *namedata; /* at adress names[a] is string a */
  191. char **types, *typedata; /* at adress types[a] is string a */
  192. short *typelens; /* at typelens[a] is de length of type a */
  193. short *alphalens; /* contains sizes as they are calculated on the DEC Alpha (64 bits) */
  194. short **structs, *structdata; /* at sp= structs[a] is the first adress of a struct definition
  195. sp[0] is type number
  196. sp[1] is amount of elements
  197. sp[2] sp[3] is typenr, namenr (etc) */
  198. /*
  199. * debugSDNA:
  200. * - 0 = no output, except errors
  201. * - 1 = detail actions
  202. * - 2 = full trace, tell which names and types were found
  203. * - 4 = full trace, plus all gritty details
  204. */
  205. int debugSDNA = 0;
  206. int additional_slen_offset;
  207. /* ************************************************************************** */
  208. /* Functions */
  209. /* ************************************************************************** */
  210. /**
  211. * Add type <str> to struct indexed by <len>, if it was not yet found.
  212. */
  213. int add_type(char *str, int len);
  214. /**
  215. * Add variable <str> to
  216. */
  217. int add_name(char *str);
  218. /**
  219. * Search whether this structure type was already found, and if not,
  220. * add it.
  221. */
  222. short *add_struct(int namecode);
  223. /**
  224. * Remove comments from this buffer. Assumes that the buffer refers to
  225. * ascii-code text.
  226. */
  227. int preprocess_include(char *maindata, int len);
  228. /**
  229. * Scan this file for serializable types.
  230. */
  231. int convert_include(char *filename);
  232. /**
  233. * Determine how many bytes are needed for an array.
  234. */
  235. int arraysize(char *astr, int len);
  236. /**
  237. * Determine how many bytes are needed for each struct.
  238. */
  239. static int calculate_structlens(int);
  240. /**
  241. * Construct the DNA.c file
  242. */
  243. void dna_write(FILE *file, void *pntr, int size);
  244. /**
  245. * Report all structures found so far, and print their lenghts.
  246. */
  247. void printStructLenghts(void);
  248. /* ************************************************************************** */
  249. /* Implementation */
  250. /* ************************************************************************** */
  251. /* ************************* MAKEN DNA ********************** */
  252. int add_type(char *str, int len)
  253. {
  254. int nr;
  255. char *cp;
  256. if (str[0] == 0) return -1;
  257. /* search through type array */
  258. for (nr = 0; nr < nr_types; nr++)
  259. {
  260. if (strcmp(str, types[nr]) == 0)
  261. {
  262. if (len)
  263. {
  264. typelens[nr] = len;
  265. alphalens[nr] = len;
  266. }
  267. return nr;
  268. }
  269. }
  270. /* append new type */
  271. if (nr_types == 0)
  272. cp = typedata;
  273. else
  274. {
  275. cp = types[nr_types - 1] + strlen(types[nr_types - 1]) + 1;
  276. }
  277. strcpy(cp, str);
  278. types[nr_types] = cp;
  279. typelens[nr_types] = len;
  280. alphalens[nr_types] = len;
  281. if (nr_types >= maxnr)
  282. {
  283. printf("too many types\n");
  284. return nr_types - 1;
  285. ;
  286. }
  287. nr_types++;
  288. return nr_types - 1;
  289. }
  290. /**
  291. *
  292. * Because of the weird way of tokenizing, we have to 'cast' function
  293. * pointers to ... (*f)(), whatever the original signature. In fact,
  294. * we add name and type at the same time... There are two special
  295. * cases, unfortunately. These are explicitly checked.
  296. *
  297. * */
  298. int add_name(char *str)
  299. {
  300. int nr, i, j, k;
  301. char *cp;
  302. char buf[255]; /* stupid limit, change it :) */
  303. char *name;
  304. additional_slen_offset = 0;
  305. if ((str[0] == 0) /* || (str[1]==0) */) return -1;
  306. if (str[0] == '(' && str[1] == '*')
  307. {
  308. if (debugSDNA > 3) printf("\t\t\t\t*** Function pointer found\n");
  309. /* functionpointer: transform the type (sometimes) */
  310. i = 0;
  311. j = 0;
  312. while (str[i] != ')')
  313. {
  314. buf[i] = str[i];
  315. i++;
  316. }
  317. /* Another number we need is the extra slen offset. This extra
  318. * offset is the overshoot after a space. If there is no
  319. * space, no overshoot should be calculated. */
  320. j = i; /* j at first closing brace */
  321. if (debugSDNA > 3) printf("first brace after offset %d\n", i);
  322. j++; /* j beyond closing brace ? */
  323. while ((str[j] != 0) && (str[j] != ')'))
  324. {
  325. if (debugSDNA > 3) printf("seen %c ( %d) \n", str[j], str[j]);
  326. j++;
  327. }
  328. if (debugSDNA > 3) printf("seen %c ( %d) \n", str[j], str[j]);
  329. if (debugSDNA > 3) printf("special after offset %d\n", j);
  330. if (str[j] == 0)
  331. {
  332. if (debugSDNA > 3) printf("offsetting for space\n");
  333. /* get additional offset */
  334. k = 0;
  335. while (str[j] != ')')
  336. {
  337. j++;
  338. k++;
  339. }
  340. if (debugSDNA > 3) printf("extra offset %d\n", k);
  341. additional_slen_offset = k;
  342. }
  343. else if (str[j] == ')')
  344. {
  345. if (debugSDNA > 3) printf("offsetting for brace\n");
  346. ; /* don't get extra offset */
  347. }
  348. else
  349. {
  350. printf("Error during tokening function pointer argument list\n");
  351. }
  352. /*
  353. * Put )(void) at the end? Maybe )(). Should check this with
  354. * old sdna. Actually, sometimes )(), sometimes )(void...)
  355. * Alas.. such is the nature of braindamage :(
  356. *
  357. * Sorted it out: always do )(), except for headdraw and
  358. * windraw, part of ScrArea. This is important, because some
  359. * linkers will treat different fp's differently when called
  360. * !!! This has to do with interference in byte-alignment and
  361. * the way args are pushed on the stack.
  362. *
  363. * */
  364. buf[i] = 0;
  365. if (debugSDNA > 3) printf("Name before chomping: %s\n", buf);
  366. if ((strncmp(buf, "(*headdraw", 10) == 0) || (strncmp(buf, "(*windraw", 9) == 0))
  367. {
  368. buf[i] = ')';
  369. buf[i + 1] = '(';
  370. buf[i + 2] = 'v';
  371. buf[i + 3] = 'o';
  372. buf[i + 4] = 'i';
  373. buf[i + 5] = 'd';
  374. buf[i + 6] = ')';
  375. buf[i + 7] = 0;
  376. }
  377. else
  378. {
  379. buf[i] = ')';
  380. buf[i + 1] = '(';
  381. buf[i + 2] = ')';
  382. buf[i + 3] = 0;
  383. }
  384. /* now precede with buf*/
  385. if (debugSDNA > 3) printf("\t\t\t\t\tProposing fp name %s\n", buf);
  386. name = buf;
  387. }
  388. else
  389. {
  390. /* normal field: old code */
  391. name = str;
  392. }
  393. /* search name array */
  394. for (nr = 0; nr < nr_names; nr++)
  395. {
  396. if (strcmp(name, names[nr]) == 0)
  397. {
  398. return nr;
  399. }
  400. }
  401. /* append new type */
  402. if (nr_names == 0)
  403. cp = namedata;
  404. else
  405. {
  406. cp = names[nr_names - 1] + strlen(names[nr_names - 1]) + 1;
  407. }
  408. strcpy(cp, name);
  409. names[nr_names] = cp;
  410. if (nr_names >= maxnr)
  411. {
  412. printf("too many names\n");
  413. return nr_names - 1;
  414. }
  415. nr_names++;
  416. return nr_names - 1;
  417. }
  418. short *add_struct(int namecode)
  419. {
  420. int len;
  421. short *sp;
  422. if (nr_structs == 0)
  423. {
  424. structs[0] = structdata;
  425. }
  426. else
  427. {
  428. sp = structs[nr_structs - 1];
  429. len = sp[1];
  430. structs[nr_structs] = sp + 2 * len + 2;
  431. }
  432. sp = structs[nr_structs];
  433. sp[0] = namecode;
  434. if (nr_structs >= maxnr)
  435. {
  436. printf("too many structs\n");
  437. return sp;
  438. }
  439. nr_structs++;
  440. return sp;
  441. }
  442. int preprocess_include(char *maindata, int len)
  443. {
  444. int a, newlen, comment = 0;
  445. char *cp, *temp, *md;
  446. temp = (char *)malloc_and_setzero(len);
  447. memcpy(temp, maindata, len);
  448. // remove all c++ comments
  449. /* replace all enters/tabs/etc with spaces */
  450. cp = temp;
  451. a = len;
  452. comment = 0;
  453. while (a--)
  454. {
  455. if (cp[0] == '/' && cp[1] == '/')
  456. {
  457. comment = 1;
  458. }
  459. else if (*cp < 32)
  460. {
  461. comment = 0;
  462. }
  463. if (comment || *cp < 32 || *cp > 128) *cp = 32;
  464. cp++;
  465. }
  466. /* data from temp copy to maindata, remove comments and double spaces */
  467. cp = temp;
  468. md = maindata;
  469. newlen = 0;
  470. comment = 0;
  471. a = len;
  472. while (a--)
  473. {
  474. if (cp[0] == '/' && cp[1] == '*')
  475. {
  476. comment = 1;
  477. cp[0] = cp[1] = 32;
  478. }
  479. if (cp[0] == '*' && cp[1] == '/')
  480. {
  481. comment = 0;
  482. cp[0] = cp[1] = 32;
  483. }
  484. /* do not copy when: */
  485. if (comment)
  486. ;
  487. else if (cp[0] == ' ' && cp[1] == ' ')
  488. ;
  489. else if (cp[-1] == '*' && cp[0] == ' ')
  490. ; /* pointers with a space */
  491. else
  492. {
  493. md[0] = cp[0];
  494. md++;
  495. newlen++;
  496. }
  497. cp++;
  498. }
  499. free(temp);
  500. return newlen;
  501. }
  502. static void *read_file_data(char *filename, int *len_r)
  503. {
  504. #ifdef WIN32
  505. FILE *fp = fopen(filename, "rb");
  506. #else
  507. FILE *fp = fopen(filename, "r");
  508. #endif
  509. void *data;
  510. if (!fp)
  511. {
  512. *len_r = -1;
  513. return NULL;
  514. }
  515. fseek(fp, 0L, SEEK_END);
  516. *len_r = ftell(fp);
  517. fseek(fp, 0L, SEEK_SET);
  518. data = malloc_and_setzero(*len_r);
  519. if (!data)
  520. {
  521. *len_r = -1;
  522. fclose(fp);
  523. return NULL;
  524. }
  525. if (fread(data, *len_r, 1, fp) != 1)
  526. {
  527. *len_r = -1;
  528. free(data);
  529. fclose(fp);
  530. return NULL;
  531. }
  532. fclose(fp);
  533. return data;
  534. }
  535. const char *skipStructTypes[] =
  536. {
  537. "btContactSolverInfoData",
  538. "btRigidBodyConstructionInfo",
  539. "Euler",
  540. "btConstraintInfo2",
  541. "btConstraintSetting",
  542. "btTriangleInfo",
  543. ""};
  544. int skipStruct(const char *structType)
  545. {
  546. int i = 0;
  547. while (strlen(skipStructTypes[i]))
  548. {
  549. if (strcmp(structType, skipStructTypes[i]) == 0)
  550. {
  551. return 1;
  552. }
  553. i++;
  554. }
  555. return 0;
  556. }
  557. int convert_include(char *filename)
  558. {
  559. /* read include file, skip structs with a '#' before it.
  560. store all data in temporal arrays.
  561. */
  562. int filelen, count, overslaan, slen, type, name, strct;
  563. short *structpoin, *sp;
  564. char *maindata, *mainend, *md, *md1;
  565. md = maindata = (char *)read_file_data(filename, &filelen);
  566. if (filelen == -1)
  567. {
  568. printf("Can't read file %s\n", filename);
  569. return 1;
  570. }
  571. filelen = preprocess_include(maindata, filelen);
  572. mainend = maindata + filelen - 1;
  573. /* we look for '{' and then back to 'struct' */
  574. count = 0;
  575. overslaan = 0;
  576. while (count < filelen)
  577. {
  578. /* code for skipping a struct: two hashes on 2 lines. (preprocess added a space) */
  579. if (md[0] == '#' && md[1] == ' ' && md[2] == '#')
  580. {
  581. overslaan = 1;
  582. }
  583. if (md[0] == '{')
  584. {
  585. md[0] = 0;
  586. if (overslaan)
  587. {
  588. overslaan = 0;
  589. }
  590. else
  591. {
  592. if (md[-1] == ' ') md[-1] = 0;
  593. md1 = md - 2;
  594. while (*md1 != 32) md1--; /* to beginning of word */
  595. md1++;
  596. /* we've got a struct name when... */
  597. if (strncmp(md1 - 7, "struct", 6) == 0)
  598. {
  599. if (!skipStruct(md1))
  600. {
  601. strct = add_type(md1, 0);
  602. structpoin = add_struct(strct);
  603. sp = structpoin + 2;
  604. if (debugSDNA > 1) printf("\t|\t|-- detected struct %s\n", types[strct]);
  605. /* first lets make it all nice strings */
  606. md1 = md + 1;
  607. while (*md1 != '}')
  608. {
  609. if (md1 > mainend) break;
  610. if (*md1 == ',' || *md1 == ' ') *md1 = 0;
  611. md1++;
  612. }
  613. /* read types and names until first character that is not '}' */
  614. md1 = md + 1;
  615. while (*md1 != '}')
  616. {
  617. if (md1 > mainend) break;
  618. /* skip when it says 'struct' or 'unsigned' or 'const' */
  619. if (*md1)
  620. {
  621. if (strncmp(md1, "struct", 6) == 0) md1 += 7;
  622. if (strncmp(md1, "unsigned", 8) == 0) md1 += 9;
  623. if (strncmp(md1, "const", 5) == 0) md1 += 6;
  624. /* we've got a type! */
  625. type = add_type(md1, 0);
  626. if (debugSDNA > 1) printf("\t|\t|\tfound type %s (", md1);
  627. md1 += strlen(md1);
  628. /* read until ';' */
  629. while (*md1 != ';')
  630. {
  631. if (md1 > mainend) break;
  632. if (*md1)
  633. {
  634. /* We've got a name. slen needs
  635. * correction for function
  636. * pointers! */
  637. slen = (int)strlen(md1);
  638. if (md1[slen - 1] == ';')
  639. {
  640. md1[slen - 1] = 0;
  641. name = add_name(md1);
  642. slen += additional_slen_offset;
  643. sp[0] = type;
  644. sp[1] = name;
  645. if ((debugSDNA > 1) && (names[name] != 0)) printf("%s |", names[name]);
  646. structpoin[1]++;
  647. sp += 2;
  648. md1 += slen;
  649. break;
  650. }
  651. name = add_name(md1);
  652. slen += additional_slen_offset;
  653. sp[0] = type;
  654. sp[1] = name;
  655. if ((debugSDNA > 1) && (names[name] != 0)) printf("%s ||", names[name]);
  656. structpoin[1]++;
  657. sp += 2;
  658. md1 += slen;
  659. }
  660. md1++;
  661. }
  662. if (debugSDNA > 1) printf(")\n");
  663. }
  664. md1++;
  665. }
  666. }
  667. }
  668. }
  669. }
  670. count++;
  671. md++;
  672. }
  673. free(maindata);
  674. return 0;
  675. }
  676. int arraysize(char *astr, int len)
  677. {
  678. int a, mul = 1;
  679. char str[100], *cp = 0;
  680. memcpy(str, astr, len + 1);
  681. for (a = 0; a < len; a++)
  682. {
  683. if (str[a] == '[')
  684. {
  685. cp = &(str[a + 1]);
  686. }
  687. else if (str[a] == ']' && cp)
  688. {
  689. str[a] = 0;
  690. mul *= atoi(cp);
  691. }
  692. }
  693. return mul;
  694. }
  695. static int calculate_structlens(int firststruct)
  696. {
  697. int a, b, len, alphalen, unknown = nr_structs, lastunknown, structtype, type, mul, namelen;
  698. short *sp, *structpoin;
  699. char *cp;
  700. int has_pointer, dna_error = 0;
  701. while (unknown)
  702. {
  703. lastunknown = unknown;
  704. unknown = 0;
  705. /* check all structs... */
  706. for (a = 0; a < nr_structs; a++)
  707. {
  708. structpoin = structs[a];
  709. structtype = structpoin[0];
  710. /* when length is not known... */
  711. if (typelens[structtype] == 0)
  712. {
  713. sp = structpoin + 2;
  714. len = 0;
  715. alphalen = 0;
  716. has_pointer = 0;
  717. /* check all elements in struct */
  718. for (b = 0; b < structpoin[1]; b++, sp += 2)
  719. {
  720. type = sp[0];
  721. cp = names[sp[1]];
  722. namelen = (int)strlen(cp);
  723. /* is it a pointer or function pointer? */
  724. if (cp[0] == '*' || cp[1] == '*')
  725. {
  726. has_pointer = 1;
  727. /* has the name an extra length? (array) */
  728. mul = 1;
  729. if (cp[namelen - 1] == ']') mul = arraysize(cp, namelen);
  730. /* 4-8 aligned/ */
  731. if (sizeof(void *) == 4)
  732. {
  733. if (len % 4)
  734. {
  735. printf("Align pointer error in struct (len4): %s %s\n", types[structtype], cp);
  736. dna_error = 1;
  737. }
  738. }
  739. else
  740. {
  741. if (len % 8)
  742. {
  743. printf("Align pointer error in struct (len8): %s %s\n", types[structtype], cp);
  744. dna_error = 1;
  745. }
  746. }
  747. if (alphalen % 8)
  748. {
  749. printf("Align pointer error in struct (alphalen8): %s %s\n", types[structtype], cp);
  750. dna_error = 1;
  751. }
  752. len += sizeof(void *) * mul;
  753. alphalen += 8 * mul;
  754. }
  755. else if (typelens[type])
  756. {
  757. /* has the name an extra length? (array) */
  758. mul = 1;
  759. if (cp[namelen - 1] == ']') mul = arraysize(cp, namelen);
  760. /* struct alignment */
  761. if (type >= firststruct)
  762. {
  763. if (sizeof(void *) == 8 && (len % 8))
  764. {
  765. printf("Align struct error: %s %s\n", types[structtype], cp);
  766. dna_error = 1;
  767. }
  768. }
  769. /* 2-4 aligned/ */
  770. if (typelens[type] > 3 && (len % 4))
  771. {
  772. printf("Align 4 error in struct: %s %s (add %d padding bytes)\n", types[structtype], cp, len % 4);
  773. dna_error = 1;
  774. }
  775. else if (typelens[type] == 2 && (len % 2))
  776. {
  777. printf("Align 2 error in struct: %s %s (add %d padding bytes)\n", types[structtype], cp, len % 2);
  778. dna_error = 1;
  779. }
  780. len += mul * typelens[type];
  781. alphalen += mul * alphalens[type];
  782. }
  783. else
  784. {
  785. len = 0;
  786. alphalen = 0;
  787. break;
  788. }
  789. }
  790. if (len == 0)
  791. {
  792. unknown++;
  793. }
  794. else
  795. {
  796. typelens[structtype] = len;
  797. alphalens[structtype] = alphalen;
  798. // two ways to detect if a struct contains a pointer:
  799. // has_pointer is set or alphalen != len
  800. if (has_pointer || alphalen != len)
  801. {
  802. if (alphalen % 8)
  803. {
  804. printf("alphalen = %d len = %d\n", alphalen, len);
  805. printf("Sizeerror 8 in struct: %s (add %d bytes)\n", types[structtype], alphalen % 8);
  806. dna_error = 1;
  807. }
  808. }
  809. if (len % 4)
  810. {
  811. printf("Sizeerror 4 in struct: %s (add %d bytes)\n", types[structtype], len % 4);
  812. dna_error = 1;
  813. }
  814. }
  815. }
  816. }
  817. if (unknown == lastunknown) break;
  818. }
  819. if (unknown)
  820. {
  821. printf("ERROR: still %d structs unknown\n", unknown);
  822. if (debugSDNA)
  823. {
  824. printf("*** Known structs : \n");
  825. for (a = 0; a < nr_structs; a++)
  826. {
  827. structpoin = structs[a];
  828. structtype = structpoin[0];
  829. /* length unknown */
  830. if (typelens[structtype] != 0)
  831. {
  832. printf(" %s\n", types[structtype]);
  833. }
  834. }
  835. }
  836. printf("*** Unknown structs : \n");
  837. for (a = 0; a < nr_structs; a++)
  838. {
  839. structpoin = structs[a];
  840. structtype = structpoin[0];
  841. /* length unkown yet */
  842. if (typelens[structtype] == 0)
  843. {
  844. printf(" %s\n", types[structtype]);
  845. }
  846. }
  847. }
  848. return (dna_error);
  849. }
  850. #define MAX_DNA_LINE_LENGTH 20
  851. void dna_write(FILE *file, void *pntr, int size)
  852. {
  853. static int linelength = 0;
  854. int i;
  855. char *data;
  856. data = (char *)pntr;
  857. for (i = 0; i < size; i++)
  858. {
  859. fprintf(file, "char(%d),", data[i]);
  860. linelength++;
  861. if (linelength >= MAX_DNA_LINE_LENGTH)
  862. {
  863. fprintf(file, "\n");
  864. linelength = 0;
  865. }
  866. }
  867. }
  868. void printStructLenghts(void)
  869. {
  870. int a, unknown = nr_structs, lastunknown, structtype;
  871. short *structpoin;
  872. printf("\n\n*** All detected structs:\n");
  873. while (unknown)
  874. {
  875. lastunknown = unknown;
  876. unknown = 0;
  877. /* check all structs... */
  878. for (a = 0; a < nr_structs; a++)
  879. {
  880. structpoin = structs[a];
  881. structtype = structpoin[0];
  882. printf("\t%s\t:%d\n", types[structtype], typelens[structtype]);
  883. }
  884. }
  885. printf("*** End of list\n");
  886. }
  887. int make_structDNA(char *baseDirectory, FILE *file)
  888. {
  889. int len, i;
  890. short *sp;
  891. /* str contains filenames. Since we now include paths, I stretched */
  892. /* it a bit. Hope this is enough :) -nzc- */
  893. char str[SDNA_MAX_FILENAME_LENGTH], *cp;
  894. int firststruct;
  895. if (debugSDNA > -1)
  896. {
  897. fflush(stdout);
  898. printf("Running makesdna at debug level %d\n", debugSDNA);
  899. }
  900. /* the longest known struct is 50k, so we assume 100k is sufficent! */
  901. namedata = (char *)malloc_and_setzero(maxdata);
  902. typedata = (char *)malloc_and_setzero(maxdata);
  903. structdata = (short *)malloc_and_setzero(maxdata);
  904. /* a maximum of 5000 variables, must be sufficient? */
  905. names = (char **)malloc_and_setzero(sizeof(char *) * maxnr);
  906. types = (char **)malloc_and_setzero(sizeof(char *) * maxnr);
  907. typelens = (short *)malloc_and_setzero(sizeof(short) * maxnr);
  908. alphalens = (short *)malloc_and_setzero(sizeof(short) * maxnr);
  909. structs = (short **)malloc_and_setzero(sizeof(short) * maxnr);
  910. /* insertion of all known types */
  911. /* watch it: uint is not allowed! use in structs an unsigned int */
  912. add_type("char", 1); /* 0 */
  913. add_type("uchar", 1); /* 1 */
  914. add_type("short", 2); /* 2 */
  915. add_type("ushort", 2); /* 3 */
  916. add_type("int", 4); /* 4 */
  917. add_type("long", 4); /* 5 */ /* should it be 8 on 64 bits? */
  918. add_type("ulong", 4); /* 6 */
  919. add_type("float", 4); /* 7 */
  920. add_type("double", 8); /* 8 */
  921. add_type("void", 0); /* 9 */
  922. // the defines above shouldn't be output in the padding file...
  923. firststruct = nr_types;
  924. /* add all include files defined in the global array */
  925. /* Since the internal file+path name buffer has limited length, I do a */
  926. /* little test first... */
  927. /* Mind the breaking condition here! */
  928. if (debugSDNA) printf("\tStart of header scan:\n");
  929. for (i = 0; strlen(includefiles[i]); i++)
  930. {
  931. sprintf(str, "%s%s", baseDirectory, includefiles[i]);
  932. if (debugSDNA) printf("\t|-- Converting %s\n", str);
  933. if (convert_include(str))
  934. {
  935. return (1);
  936. }
  937. }
  938. if (debugSDNA) printf("\tFinished scanning %d headers.\n", i);
  939. if (calculate_structlens(firststruct))
  940. {
  941. // error
  942. return (1);
  943. }
  944. /* FOR DEBUG */
  945. if (debugSDNA > 1)
  946. {
  947. int a, b;
  948. /* short *elem; */
  949. short num_types;
  950. printf("nr_names %d nr_types %d nr_structs %d\n", nr_names, nr_types, nr_structs);
  951. for (a = 0; a < nr_names; a++)
  952. {
  953. printf(" %s \n", names[a]);
  954. }
  955. printf("\n");
  956. sp = typelens;
  957. for (a = 0; a < nr_types; a++, sp++)
  958. {
  959. printf(" %s %d\n", types[a], *sp);
  960. }
  961. printf("\n");
  962. for (a = 0; a < nr_structs; a++)
  963. {
  964. sp = structs[a];
  965. printf(" struct %s elems: %d size: %d\n", types[sp[0]], sp[1], typelens[sp[0]]);
  966. num_types = sp[1];
  967. sp += 2;
  968. /* ? num_types was elem? */
  969. for (b = 0; b < num_types; b++, sp += 2)
  970. {
  971. printf(" %s %s\n", types[sp[0]], names[sp[1]]);
  972. }
  973. }
  974. }
  975. /* file writing */
  976. if (debugSDNA > -1) printf("Writing file ... ");
  977. if (nr_names == 0 || nr_structs == 0)
  978. ;
  979. else
  980. {
  981. strcpy(str, "SDNA");
  982. dna_write(file, str, 4);
  983. /* write names */
  984. strcpy(str, "NAME");
  985. dna_write(file, str, 4);
  986. len = nr_names;
  987. dna_write(file, &len, 4);
  988. /* calculate size of datablock with strings */
  989. cp = names[nr_names - 1];
  990. cp += strlen(names[nr_names - 1]) + 1; /* +1: null-terminator */
  991. len = (btintptr_t)(cp - (char *)names[0]);
  992. len = (len + 3) & ~3;
  993. dna_write(file, names[0], len);
  994. /* write TYPES */
  995. strcpy(str, "TYPE");
  996. dna_write(file, str, 4);
  997. len = nr_types;
  998. dna_write(file, &len, 4);
  999. /* calculate datablock size */
  1000. cp = types[nr_types - 1];
  1001. cp += strlen(types[nr_types - 1]) + 1; /* +1: null-terminator */
  1002. len = (btintptr_t)(cp - (char *)types[0]);
  1003. len = (len + 3) & ~3;
  1004. dna_write(file, types[0], len);
  1005. /* WRITE TYPELENGTHS */
  1006. strcpy(str, "TLEN");
  1007. dna_write(file, str, 4);
  1008. len = 2 * nr_types;
  1009. if (nr_types & 1) len += 2;
  1010. dna_write(file, typelens, len);
  1011. /* WRITE STRUCTS */
  1012. strcpy(str, "STRC");
  1013. dna_write(file, str, 4);
  1014. len = nr_structs;
  1015. dna_write(file, &len, 4);
  1016. /* calc datablock size */
  1017. sp = structs[nr_structs - 1];
  1018. sp += 2 + 2 * (sp[1]);
  1019. len = (btintptr_t)((char *)sp - (char *)structs[0]);
  1020. len = (len + 3) & ~3;
  1021. dna_write(file, structs[0], len);
  1022. /* a simple dna padding test */
  1023. if (0)
  1024. {
  1025. FILE *fp;
  1026. int a;
  1027. fp = fopen("padding.c", "w");
  1028. if (fp == NULL)
  1029. ;
  1030. else
  1031. {
  1032. // add all include files defined in the global array
  1033. for (i = 0; strlen(includefiles[i]); i++)
  1034. {
  1035. fprintf(fp, "#include \"%s%s\"\n", baseDirectory, includefiles[i]);
  1036. }
  1037. fprintf(fp, "main(){\n");
  1038. sp = typelens;
  1039. sp += firststruct;
  1040. for (a = firststruct; a < nr_types; a++, sp++)
  1041. {
  1042. if (*sp)
  1043. {
  1044. fprintf(fp, "\tif(sizeof(struct %s) - %d) printf(\"ALIGN ERROR:", types[a], *sp);
  1045. fprintf(fp, "%%d %s %d ", types[a], *sp);
  1046. fprintf(fp, "\\n\", sizeof(struct %s) - %d);\n", types[a], *sp);
  1047. }
  1048. }
  1049. fprintf(fp, "}\n");
  1050. fclose(fp);
  1051. }
  1052. }
  1053. /* end end padding test */
  1054. }
  1055. free(namedata);
  1056. free(typedata);
  1057. free(structdata);
  1058. free(names);
  1059. free(types);
  1060. free(typelens);
  1061. free(structs);
  1062. if (debugSDNA > -1) printf("done.\n");
  1063. return (0);
  1064. }
  1065. /* ************************* END MAKE DNA ********************** */
  1066. static void make_bad_file(char *file)
  1067. {
  1068. FILE *fp = fopen(file, "w");
  1069. fprintf(fp, "ERROR! Cannot make correct DNA.c file, STUPID!\n");
  1070. fclose(fp);
  1071. }
  1072. #ifndef BASE_HEADER
  1073. #define BASE_HEADER "../"
  1074. #endif
  1075. int main(int argc, char **argv)
  1076. {
  1077. // printf("btCollisionObject=%d\n",sizeof(btCollisionObject));
  1078. // printf("btCollisionObjectData=%d\n",sizeof(btCollisionObjectData));
  1079. // printf("btTransform=%d\n",sizeof(btTransform));
  1080. // printf("btTransformData=%d\n",sizeof(btTransformData));
  1081. //
  1082. // btCollisionObject* bla = new btCollisionObject();
  1083. // btCollisionObjectData* bla2 = new btCollisionObjectData();
  1084. //int offsetof(bla,m_hasAnisotropicFriction);
  1085. /*
  1086. btTransformData m_worldTransform;
  1087. btTransform m_interpolationWorldTransform;
  1088. btVector3 m_interpolationLinearVelocity;
  1089. btVector3 m_interpolationAngularVelocity;
  1090. btVector3 m_anisotropicFriction;
  1091. bool m_hasAnisotropicFriction;
  1092. btScalar m_contactProcessingThreshold;
  1093. btBroadphaseProxy *m_broadphaseHandle;
  1094. btCollisionShape *m_collisionShape;
  1095. btCollisionShape *m_rootCollisionShape;
  1096. int m_collisionFlags;
  1097. int m_islandTag1;
  1098. int m_companionId;
  1099. int m_activationState1;
  1100. btScalar m_deactivationTime;
  1101. btScalar m_friction;
  1102. btScalar m_restitution;
  1103. void* m_userObjectPointer;
  1104. int m_internalType;
  1105. btScalar m_hitFraction;
  1106. btScalar m_ccdSweptSphereRadius;
  1107. btScalar m_ccdMotionThreshold;
  1108. bool m_checkCollideWith;
  1109. char m_pad[7];
  1110. */
  1111. FILE *file;
  1112. int return_status = 0;
  1113. if (argc != 2 && argc != 3)
  1114. {
  1115. printf("Usage: %s outfile.c [base directory]\n", argv[0]);
  1116. return_status = 1;
  1117. }
  1118. else
  1119. {
  1120. file = fopen(argv[1], "w");
  1121. if (!file)
  1122. {
  1123. printf("Unable to open file: %s\n", argv[1]);
  1124. return_status = 1;
  1125. }
  1126. else
  1127. {
  1128. char baseDirectory[256];
  1129. if (argc == 3)
  1130. {
  1131. strcpy(baseDirectory, argv[2]);
  1132. }
  1133. else
  1134. {
  1135. strcpy(baseDirectory, BASE_HEADER);
  1136. }
  1137. if (sizeof(void *) == 8)
  1138. {
  1139. fprintf(file, "char sBulletDNAstr64[]= {\n");
  1140. }
  1141. else
  1142. {
  1143. fprintf(file, "char sBulletDNAstr[]= {\n");
  1144. }
  1145. if (make_structDNA(baseDirectory, file))
  1146. {
  1147. // error
  1148. fclose(file);
  1149. make_bad_file(argv[1]);
  1150. return_status = 1;
  1151. }
  1152. else
  1153. {
  1154. fprintf(file, "};\n");
  1155. if (sizeof(void *) == 8)
  1156. {
  1157. fprintf(file, "int sBulletDNAlen64= sizeof(sBulletDNAstr64);\n");
  1158. }
  1159. else
  1160. {
  1161. fprintf(file, "int sBulletDNAlen= sizeof(sBulletDNAstr);\n");
  1162. }
  1163. fclose(file);
  1164. }
  1165. }
  1166. }
  1167. return (return_status);
  1168. }
  1169. /* end of list */