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