Sequencer WIP
[blender.git] / source / blender / makesrna / intern / makesrna.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Contributor(s): Blender Foundation (2008).
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include <float.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "RNA_access.h"
34 #include "RNA_define.h"
35 #include "RNA_types.h"
36
37 #include "rna_internal.h"
38
39 #define RNA_VERSION_DATE "$Id$"
40
41 #ifdef _WIN32
42 #ifndef snprintf
43 #define snprintf _snprintf
44 #endif
45 #endif
46
47 /* Sorting */
48
49 static int cmp_struct(const void *a, const void *b)
50 {
51         const StructRNA *structa= *(const StructRNA**)a;
52         const StructRNA *structb= *(const StructRNA**)b;
53
54         return strcmp(structa->identifier, structb->identifier);
55 }
56
57 static int cmp_property(const void *a, const void *b)
58 {
59         const PropertyRNA *propa= *(const PropertyRNA**)a;
60         const PropertyRNA *propb= *(const PropertyRNA**)b;
61
62         if(strcmp(propa->identifier, "rna_type") == 0) return -1;
63         else if(strcmp(propb->identifier, "rna_type") == 0) return 1;
64
65         if(strcmp(propa->identifier, "name") == 0) return -1;
66         else if(strcmp(propb->identifier, "name") == 0) return 1;
67
68         return strcmp(propa->name, propb->name);
69 }
70
71 static int cmp_def_struct(const void *a, const void *b)
72 {
73         const StructDefRNA *dsa= *(const StructDefRNA**)a;
74         const StructDefRNA *dsb= *(const StructDefRNA**)b;
75
76         return cmp_struct(&dsa->srna, &dsb->srna);
77 }
78
79 static int cmp_def_property(const void *a, const void *b)
80 {
81         const PropertyDefRNA *dpa= *(const PropertyDefRNA**)a;
82         const PropertyDefRNA *dpb= *(const PropertyDefRNA**)b;
83
84         return cmp_property(&dpa->prop, &dpb->prop);
85 }
86
87 static void rna_sortlist(ListBase *listbase, int(*cmp)(const void*, const void*))
88 {
89         Link *link;
90         void **array;
91         int a, size;
92         
93         if(listbase->first == listbase->last)
94                 return;
95
96         for(size=0, link=listbase->first; link; link=link->next)
97                 size++;
98
99         array= MEM_mallocN(sizeof(void*)*size, "rna_sortlist");
100         for(a=0, link=listbase->first; link; link=link->next, a++)
101                 array[a]= link;
102
103         qsort(array, size, sizeof(void*), cmp);
104
105         listbase->first= listbase->last= NULL;
106         for(a=0; a<size; a++) {
107                 link= array[a];
108                 link->next= link->prev= NULL;
109                 rna_addtail(listbase, link);
110         }
111
112         MEM_freeN(array);
113 }
114
115 /* Preprocessing */
116
117 static void rna_print_c_string(FILE *f, const char *str)
118 {
119         static char *escape[] = {"\''", "\"\"", "\??", "\\\\","\aa", "\bb", "\ff", "\nn", "\rr", "\tt", "\vv", NULL};
120         int i, j;
121
122         fprintf(f, "\"");
123         for(i=0; str[i]; i++) {
124                 for(j=0; escape[j]; j++)
125                         if(str[i] == escape[j][0])
126                                 break;
127
128                 if(escape[j]) fprintf(f, "\\%c", escape[j][1]);
129                 else fprintf(f, "%c", str[i]);
130         }
131         fprintf(f, "\"");
132 }
133
134 static void rna_print_data_get(FILE *f, PropertyDefRNA *dp)
135 {
136         if(dp->dnastructfromname && dp->dnastructfromprop)
137                 fprintf(f, "    %s *data= (%s*)(((%s*)ptr->data)->%s);\n", dp->dnastructname, dp->dnastructname, dp->dnastructfromname, dp->dnastructfromprop);
138         else
139                 fprintf(f, "    %s *data= (%s*)(ptr->data);\n", dp->dnastructname, dp->dnastructname);
140 }
141
142 static char *rna_alloc_function_name(const char *structname, const char *propname, const char *type)
143 {
144         AllocDefRNA *alloc;
145         char buffer[2048];
146         char *result;
147
148         snprintf(buffer, sizeof(buffer), "%s_%s_%s", structname, propname, type);
149         result= MEM_callocN(sizeof(char)*strlen(buffer)+1, "rna_alloc_function_name");
150         strcpy(result, buffer);
151
152         alloc= MEM_callocN(sizeof(AllocDefRNA), "AllocDefRNA");
153         alloc->mem= result;
154         rna_addtail(&DefRNA.allocs, alloc);
155
156         return result;
157 }
158
159 static StructRNA *rna_find_struct(const char *identifier)
160 {
161         StructDefRNA *ds;
162
163         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
164                 if(strcmp(ds->srna->identifier, identifier)==0)
165                         return ds->srna;
166
167         return NULL;
168 }
169
170 static const char *rna_find_type(const char *type)
171 {
172         StructDefRNA *ds;
173
174         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
175                 if(ds->dnaname && strcmp(ds->dnaname, type)==0)
176                         return ds->srna->identifier;
177
178         return NULL;
179 }
180
181 static const char *rna_find_dna_type(const char *type)
182 {
183         StructDefRNA *ds;
184
185         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
186                 if(strcmp(ds->srna->identifier, type)==0)
187                         return ds->dnaname;
188
189         return NULL;
190 }
191
192 static const char *rna_type_type_name(PropertyRNA *prop)
193 {
194         switch(prop->type) {
195                 case PROP_BOOLEAN:
196                 case PROP_INT:
197                 case PROP_ENUM:
198                         return "int";
199                 case PROP_FLOAT:
200                         return "float";
201                 case PROP_STRING:
202                         return "char*";
203                 default:
204                         return NULL;
205         }
206 }
207
208 static const char *rna_type_type(PropertyRNA *prop)
209 {
210         const char *type;
211
212         type= rna_type_type_name(prop);
213
214         if(type)
215                 return type;
216
217         return "PointerRNA";
218 }
219
220 static const char *rna_type_struct(PropertyRNA *prop)
221 {
222         const char *type;
223
224         type= rna_type_type_name(prop);
225
226         if(type)
227                 return "";
228
229         return "struct ";
230 }
231
232 static const char *rna_parameter_type_name(PropertyRNA *parm)
233 {
234         const char *type;
235
236         type= rna_type_type_name(parm);
237
238         if(type)
239                 return type;
240
241         switch(parm->type) {
242                 case PROP_POINTER:  {
243                         PointerPropertyRNA *pparm= (PointerPropertyRNA*)parm;
244
245                         if(strcmp((char*)pparm->type, "AnyType") == 0)
246                                 return "PointerRNA";
247                         else
248                                 return rna_find_dna_type((const char *)pparm->type);
249                 }
250                 case PROP_COLLECTION: {
251                         CollectionPropertyRNA *cparm= (CollectionPropertyRNA*)parm;
252                         return rna_find_dna_type((const char *)cparm->type);
253                 }
254                 default:
255                         return "<error, no type specified>";
256         }
257 }
258
259 static int rna_enum_bitmask(PropertyRNA *prop)
260 {
261         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
262         int a, mask= 0;
263
264         if(eprop->item) {
265                 for(a=0; a<eprop->totitem; a++)
266                         mask |= eprop->item[a].value;
267         }
268         
269         return mask;
270 }
271
272 static int rna_color_quantize(PropertyRNA *prop, PropertyDefRNA *dp)
273 {
274         if(prop->type == PROP_FLOAT && prop->subtype == PROP_COLOR)
275                 if(strcmp(dp->dnatype, "float") != 0 && strcmp(dp->dnatype, "double") != 0)
276                         return 1;
277         
278         return 0;
279 }
280
281 static const char *rna_function_string(void *func)
282 {
283         return (func)? (const char*)func: "NULL";
284 }
285
286 static void rna_float_print(FILE *f, float num)
287 {
288         if(num == -FLT_MAX) fprintf(f, "-FLT_MAX");
289         else if(num == FLT_MAX) fprintf(f, "FLT_MAX");
290         else if((int)num == num) fprintf(f, "%.1ff", num);
291         else fprintf(f, "%.10ff", num);
292 }
293
294 static void rna_int_print(FILE *f, int num)
295 {
296         if(num == INT_MIN) fprintf(f, "INT_MIN");
297         else if(num == INT_MAX) fprintf(f, "INT_MAX");
298         else fprintf(f, "%d", num);
299 }
300
301 static char *rna_def_property_get_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
302 {
303         char *func;
304         int i;
305
306         if(prop->flag & PROP_IDPROPERTY)
307                 return NULL;
308         
309         if(!manualfunc) {
310                 if(!dp->dnastructname || !dp->dnaname) {
311                         fprintf(stderr, "rna_def_property_get_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
312                         DefRNA.error= 1;
313                         return NULL;
314                 }
315         }
316
317         func= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
318
319         switch(prop->type) {
320                 case PROP_STRING: {
321                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
322                         fprintf(f, "void %s(PointerRNA *ptr, char *value)\n", func);
323                         fprintf(f, "{\n");
324                         if(manualfunc) {
325                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
326                         }
327                         else {
328                                 rna_print_data_get(f, dp);
329                                 if(sprop->maxlength)
330                                         fprintf(f, "    BLI_strncpy(value, data->%s, %d);\n", dp->dnaname, sprop->maxlength);
331                                 else
332                                         fprintf(f, "    BLI_strncpy(value, data->%s, sizeof(data->%s));\n", dp->dnaname, dp->dnaname);
333                         }
334                         fprintf(f, "}\n\n");
335                         break;
336                 }
337                 case PROP_POINTER: {
338                         fprintf(f, "PointerRNA %s(PointerRNA *ptr)\n", func);
339                         fprintf(f, "{\n");
340                         if(manualfunc) {
341                                 fprintf(f, "    return %s(ptr);\n", manualfunc);
342                         }
343                         else {
344                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
345                                 rna_print_data_get(f, dp);
346                                 if(dp->dnapointerlevel == 0)
347                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, &data->%s);\n", (char*)pprop->type, dp->dnaname);
348                                 else
349                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, data->%s);\n", (char*)pprop->type, dp->dnaname);
350                         }
351                         fprintf(f, "}\n\n");
352                         break;
353                 }
354                 case PROP_COLLECTION: {
355                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
356
357                         fprintf(f, "static PointerRNA %s(CollectionPropertyIterator *iter)\n", func);
358                         fprintf(f, "{\n");
359                         if(manualfunc) {
360                                 if(strcmp(manualfunc, "rna_iterator_listbase_get") == 0 ||
361                                    strcmp(manualfunc, "rna_iterator_array_get") == 0 ||
362                                    strcmp(manualfunc, "rna_iterator_array_dereference_get") == 0)
363                                         fprintf(f, "    return rna_pointer_inherit_refine(&iter->parent, &RNA_%s, %s(iter));\n", (cprop->type)? (char*)cprop->type: "UnknownType", manualfunc);
364                                 else
365                                         fprintf(f, "    return %s(iter);\n", manualfunc);
366                         }
367                         fprintf(f, "}\n\n");
368                         break;
369                 }
370                 default:
371                         if(prop->arraylength) {
372                                 fprintf(f, "void %s(PointerRNA *ptr, %s values[%d])\n", func, rna_type_type(prop), prop->arraylength);
373                                 fprintf(f, "{\n");
374
375                                 if(manualfunc) {
376                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
377                                 }
378                                 else {
379                                         rna_print_data_get(f, dp);
380
381                                         for(i=0; i<prop->arraylength; i++) {
382                                                 if(dp->dnaarraylength == 1) {
383                                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit)
384                                                                 fprintf(f, "    values[%d]= (%s(data->%s & (%d<<%d)) != 0);\n", i, (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit, i);
385                                                         else
386                                                                 fprintf(f, "    values[%d]= (%s)%s((&data->%s)[%d]);\n", i, rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname, i);
387                                                 }
388                                                 else {
389                                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
390                                                                 fprintf(f, "    values[%d]= (%s(data->%s[%d] & ", i, (dp->booleannegative)? "!": "", dp->dnaname, i);
391                                                                 rna_int_print(f, dp->booleanbit);
392                                                                 fprintf(f, ") != 0);\n");
393                                                         }
394                                                         else if(rna_color_quantize(prop, dp))
395                                                                 fprintf(f, "    values[%d]= (%s)(data->%s[%d]*(1.0f/255.0f));\n", i, rna_type_type(prop), dp->dnaname, i);
396                                                         else
397                                                                 fprintf(f, "    values[%d]= (%s)%s(data->%s[%d]);\n", i, rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname, i);
398                                                 }
399                                         }
400                                 }
401                                 fprintf(f, "}\n\n");
402                         }
403                         else {
404                                 fprintf(f, "%s %s(PointerRNA *ptr)\n", rna_type_type(prop), func);
405                                 fprintf(f, "{\n");
406
407                                 if(manualfunc) {
408                                         fprintf(f, "    return %s(ptr);\n", manualfunc);
409                                 }
410                                 else {
411                                         rna_print_data_get(f, dp);
412                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
413                                                 fprintf(f, "    return (%s((data->%s) & ", (dp->booleannegative)? "!": "", dp->dnaname);
414                                                 rna_int_print(f, dp->booleanbit);
415                                                 fprintf(f, ") != 0);\n");
416                                         }
417                                         else if(prop->type == PROP_ENUM && dp->enumbitflags) {
418                                                 fprintf(f, "    return ((data->%s) & ", dp->dnaname);
419                                                 rna_int_print(f, rna_enum_bitmask(prop));
420                                                 fprintf(f, ");\n");
421                                         }
422                                         else
423                                                 fprintf(f, "    return (%s)%s(data->%s);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
424                                 }
425
426                                 fprintf(f, "}\n\n");
427                         }
428                         break;
429         }
430
431         return func;
432 }
433
434 static void rna_clamp_value(FILE *f, PropertyRNA *prop, int array, int i)
435 {
436         if(prop->type == PROP_INT) {
437                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
438
439                 if(iprop->hardmin != INT_MIN || iprop->hardmax != INT_MAX) {
440                         if(array) fprintf(f, "CLAMPIS(values[%d], ", i);
441                         else fprintf(f, "CLAMPIS(value, ");
442                         rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
443                         rna_int_print(f, iprop->hardmax); fprintf(f, ");\n");
444                         return;
445                 }
446         }
447         else if(prop->type == PROP_FLOAT) {
448                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
449
450                 if(fprop->hardmin != -FLT_MAX || fprop->hardmax != FLT_MAX) {
451                         if(array) fprintf(f, "CLAMPIS(values[%d], ", i);
452                         else fprintf(f, "CLAMPIS(value, ");
453                         rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
454                         rna_float_print(f, fprop->hardmax); fprintf(f, ");\n");
455                         return;
456                 }
457         }
458
459         if(array)
460                 fprintf(f, "values[%d];\n", i);
461         else
462                 fprintf(f, "value;\n");
463 }
464
465 static char *rna_def_property_set_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
466 {
467         char *func;
468         int i;
469
470         if(!(prop->flag & PROP_EDITABLE))
471                 return NULL;
472         if(prop->flag & PROP_IDPROPERTY)
473                 return NULL;
474
475         if(!manualfunc) {
476                 if(!dp->dnastructname || !dp->dnaname) {
477                         if(prop->flag & PROP_EDITABLE) {
478                                 fprintf(stderr, "rna_def_property_set_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
479                                 DefRNA.error= 1;
480                         }
481                         return NULL;
482                 }
483         }
484
485         func= rna_alloc_function_name(srna->identifier, prop->identifier, "set");
486
487         switch(prop->type) {
488                 case PROP_STRING: {
489                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
490                         fprintf(f, "void %s(PointerRNA *ptr, const char *value)\n", func);
491                         fprintf(f, "{\n");
492                         if(manualfunc) {
493                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
494                         }
495                         else {
496                                 rna_print_data_get(f, dp);
497                                 if(sprop->maxlength)
498                                         fprintf(f, "    BLI_strncpy(data->%s, value, %d);\n", dp->dnaname, sprop->maxlength);
499                                 else
500                                         fprintf(f, "    BLI_strncpy(data->%s, value, sizeof(data->%s));\n", dp->dnaname, dp->dnaname);
501                         }
502                         fprintf(f, "}\n\n");
503                         break;
504                 }
505                 case PROP_POINTER: {
506                         fprintf(f, "void %s(PointerRNA *ptr, PointerRNA value)\n", func);
507                         fprintf(f, "{\n");
508                         if(manualfunc) {
509                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
510                         }
511                         else {
512                                 rna_print_data_get(f, dp);
513
514                                 if(prop->flag & PROP_ID_REFCOUNT) {
515                                         fprintf(f, "\n  if(data->%s)\n", dp->dnaname);
516                                         fprintf(f, "            id_us_min((ID*)data->%s);\n", dp->dnaname);
517                                         fprintf(f, "    if(value.data)\n");
518                                         fprintf(f, "            id_us_plus((ID*)value.data);\n\n");
519                                 }
520
521                                 fprintf(f, "    data->%s= value.data;\n", dp->dnaname);
522
523                         }
524                         fprintf(f, "}\n\n");
525                         break;
526                 }
527                 default:
528                         if(prop->arraylength) {
529                                 fprintf(f, "void %s(PointerRNA *ptr, const %s values[%d])\n", func, rna_type_type(prop), prop->arraylength);
530                                 fprintf(f, "{\n");
531
532                                 if(manualfunc) {
533                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
534                                 }
535                                 else {
536                                         rna_print_data_get(f, dp);
537
538                                         for(i=0; i<prop->arraylength; i++) {
539                                                 if(dp->dnaarraylength == 1) {
540                                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
541                                                                 fprintf(f, "    if(%svalues[%d]) data->%s |= (%d<<%d);\n", (dp->booleannegative)? "!": "", i, dp->dnaname, dp->booleanbit, i);
542                                                                 fprintf(f, "    else data->%s &= ~(%d<<%d);\n", dp->dnaname, dp->booleanbit, i);
543                                                         }
544                                                         else {
545                                                                 fprintf(f, "    (&data->%s)[%d]= %s", dp->dnaname, i, (dp->booleannegative)? "!": "");
546                                                                 rna_clamp_value(f, prop, 1, i);
547                                                         }
548                                                 }
549                                                 else {
550                                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
551                                                                 fprintf(f, "    if(%svalues[%d]) data->%s[%d] |= ", (dp->booleannegative)? "!": "", i, dp->dnaname, i);
552                                                                 rna_int_print(f, dp->booleanbit);
553                                                                 fprintf(f, ";\n");
554                                                                 fprintf(f, "    else data->%s[%d] &= ~", dp->dnaname, i);
555                                                                 rna_int_print(f, dp->booleanbit);
556                                                                 fprintf(f, ";\n");
557                                                         }
558                                                         else if(rna_color_quantize(prop, dp)) {
559                                                                 fprintf(f, "    data->%s[%d]= FTOCHAR(values[%d]);\n", dp->dnaname, i, i);
560                                                         }
561                                                         else {
562                                                                 fprintf(f, "    data->%s[%d]= %s", dp->dnaname, i, (dp->booleannegative)? "!": "");
563                                                                 rna_clamp_value(f, prop, 1, i);
564                                                         }
565                                                 }
566                                         }
567                                 }
568                                 fprintf(f, "}\n\n");
569                         }
570                         else {
571                                 fprintf(f, "void %s(PointerRNA *ptr, %s value)\n", func, rna_type_type(prop));
572                                 fprintf(f, "{\n");
573
574                                 if(manualfunc) {
575                                         fprintf(f, "    %s(ptr, value);\n", manualfunc);
576                                 }
577                                 else {
578                                         rna_print_data_get(f, dp);
579                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
580                                                 fprintf(f, "    if(%svalue) data->%s |= ", (dp->booleannegative)? "!": "", dp->dnaname);
581                                                 rna_int_print(f, dp->booleanbit);
582                                                 fprintf(f, ";\n");
583                                                 fprintf(f, "    else data->%s &= ~", dp->dnaname);
584                                                 rna_int_print(f, dp->booleanbit);
585                                                 fprintf(f, ";\n");
586                                         }
587                                         else if(prop->type == PROP_ENUM && dp->enumbitflags) {
588                                                 fprintf(f, "    data->%s &= ~", dp->dnaname);
589                                                 rna_int_print(f, rna_enum_bitmask(prop));
590                                                 fprintf(f, ";\n");
591                                                 fprintf(f, "    data->%s |= value;\n", dp->dnaname);
592                                         }
593                                         else {
594                                                 fprintf(f, "    data->%s= %s", dp->dnaname, (dp->booleannegative)? "!": "");
595                                                 rna_clamp_value(f, prop, 0, 0);
596                                         }
597                                 }
598                                 fprintf(f, "}\n\n");
599                         }
600                         break;
601         }
602
603         return func;
604 }
605
606 static char *rna_def_property_length_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
607 {
608         char *func= NULL;
609
610         if(prop->flag & PROP_IDPROPERTY)
611                 return NULL;
612
613         if(prop->type == PROP_STRING) {
614                 if(!manualfunc) {
615                         if(!dp->dnastructname || !dp->dnaname) {
616                                 fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
617                                 DefRNA.error= 1;
618                                 return NULL;
619                         }
620                 }
621
622                 func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");
623
624                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
625                 fprintf(f, "{\n");
626                 if(manualfunc) {
627                         fprintf(f, "    return %s(ptr);\n", manualfunc);
628                 }
629                 else {
630                         rna_print_data_get(f, dp);
631                         fprintf(f, "    return strlen(data->%s);\n", dp->dnaname);
632                 }
633                 fprintf(f, "}\n\n");
634         }
635         else if(prop->type == PROP_COLLECTION) {
636                 if(!manualfunc) {
637                         if(prop->type == PROP_COLLECTION && (!(dp->dnalengthname || dp->dnalengthfixed)|| !dp->dnaname)) {
638                                 fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
639                                 DefRNA.error= 1;
640                                 return NULL;
641                         }
642                 }
643
644                 func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");
645
646                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
647                 fprintf(f, "{\n");
648                 if(manualfunc) {
649                         fprintf(f, "    return %s(ptr);\n", manualfunc);
650                 }
651                 else {
652                         rna_print_data_get(f, dp);
653                         if(dp->dnalengthname)
654                                 fprintf(f, "    return (data->%s == NULL)? 0: data->%s;\n", dp->dnaname, dp->dnalengthname);
655                         else
656                                 fprintf(f, "    return (data->%s == NULL)? 0: %d;\n", dp->dnaname, dp->dnalengthfixed);
657                 }
658                 fprintf(f, "}\n\n");
659         }
660
661         return func;
662 }
663
664 static char *rna_def_property_begin_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
665 {
666         char *func, *getfunc;
667
668         if(prop->flag & PROP_IDPROPERTY)
669                 return NULL;
670
671         if(!manualfunc) {
672                 if(!dp->dnastructname || !dp->dnaname) {
673                         fprintf(stderr, "rna_def_property_begin_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
674                         DefRNA.error= 1;
675                         return NULL;
676                 }
677         }
678
679         func= rna_alloc_function_name(srna->identifier, prop->identifier, "begin");
680
681         fprintf(f, "void %s(CollectionPropertyIterator *iter, PointerRNA *ptr)\n", func);
682         fprintf(f, "{\n");
683
684         if(!manualfunc)
685                 rna_print_data_get(f, dp);
686
687         fprintf(f, "\n  memset(iter, 0, sizeof(*iter));\n");
688         fprintf(f, "    iter->parent= *ptr;\n");
689         fprintf(f, "    iter->prop= (PropertyRNA*)&rna_%s_%s;\n", srna->identifier, prop->identifier);
690
691         if(dp->dnalengthname || dp->dnalengthfixed) {
692                 if(manualfunc) {
693                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
694                 }
695                 else {
696                         if(dp->dnalengthname)
697                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), data->%s, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthname);
698                         else
699                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), %d, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthfixed);
700                 }
701         }
702         else {
703                 if(manualfunc)
704                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
705                 else
706                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, &data->%s, NULL);\n", dp->dnaname);
707         }
708
709         getfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
710
711         fprintf(f, "\n  if(iter->valid)\n");
712         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
713
714         fprintf(f, "}\n\n");
715
716
717         return func;
718 }
719
720 static char *rna_def_property_next_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
721 {
722         char *func, *getfunc;
723
724         if(prop->flag & PROP_IDPROPERTY)
725                 return NULL;
726
727         if(!manualfunc)
728                 return NULL;
729
730         func= rna_alloc_function_name(srna->identifier, prop->identifier, "next");
731
732         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
733         fprintf(f, "{\n");
734         fprintf(f, "    %s(iter);\n", manualfunc);
735
736         getfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
737
738         fprintf(f, "\n  if(iter->valid)\n");
739         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
740
741         fprintf(f, "}\n\n");
742
743         return func;
744 }
745
746 static char *rna_def_property_end_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
747 {
748         char *func;
749
750         if(prop->flag & PROP_IDPROPERTY)
751                 return NULL;
752
753         func= rna_alloc_function_name(srna->identifier, prop->identifier, "end");
754
755         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
756         fprintf(f, "{\n");
757         if(manualfunc)
758                 fprintf(f, "    %s(iter);\n", manualfunc);
759         fprintf(f, "}\n\n");
760
761         return func;
762 }
763
764 static void rna_def_property_funcs(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
765 {
766         PropertyRNA *prop;
767
768         prop= dp->prop;
769
770         switch(prop->type) {
771                 case PROP_BOOLEAN: {
772                         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
773
774                         if(!prop->arraylength) {
775                                 bprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->get);
776                                 bprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->set);
777                         }
778                         else {
779                                 bprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->getarray);
780                                 bprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->setarray);
781                         }
782                         break;
783                 }
784                 case PROP_INT: {
785                         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
786
787                         if(!prop->arraylength) {
788                                 iprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->get);
789                                 iprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->set);
790                         }
791                         else {
792                                 iprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->getarray);
793                                 iprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->setarray);
794                         }
795                         break;
796                 }
797                 case PROP_FLOAT: {
798                         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
799
800                         if(!prop->arraylength) {
801                                 fprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->get);
802                                 fprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->set);
803                         }
804                         else {
805                                 fprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->getarray);
806                                 fprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->setarray);
807                         }
808                         break;
809                 }
810                 case PROP_ENUM: {
811                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
812
813                         eprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)eprop->get);
814                         eprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)eprop->set);
815                         break;
816                 }
817                 case PROP_STRING: {
818                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
819
820                         sprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)sprop->get);
821                         sprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)sprop->length);
822                         sprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)sprop->set);
823                         break;
824                 }
825                 case PROP_POINTER: {
826                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
827
828                         pprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)pprop->get);
829                         pprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)pprop->set);
830                         if(!pprop->type) {
831                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, pointer must have a struct type.\n", srna->identifier, prop->identifier);
832                                 DefRNA.error= 1;
833                         }
834                         break;
835                 }
836                 case PROP_COLLECTION: {
837                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
838
839                         if(dp->dnatype && strcmp(dp->dnatype, "ListBase")==0);
840                         else if(dp->dnalengthname || dp->dnalengthfixed)
841                                 cprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)cprop->length);
842
843                         cprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)cprop->get);
844                         cprop->begin= (void*)rna_def_property_begin_func(f, srna, prop, dp, (char*)cprop->begin);
845                         cprop->next= (void*)rna_def_property_next_func(f, srna, prop, dp, (char*)cprop->next);
846                         cprop->end= (void*)rna_def_property_end_func(f, srna, prop, dp, (char*)cprop->end);
847
848                         if(!(prop->flag & PROP_IDPROPERTY)) {
849                                 if(!cprop->begin) {
850                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a begin function.\n", srna->identifier, prop->identifier);
851                                         DefRNA.error= 1;
852                                 }
853                                 if(!cprop->next) {
854                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a next function.\n", srna->identifier, prop->identifier);
855                                         DefRNA.error= 1;
856                                 }
857                                 if(!cprop->get) {
858                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a get function.\n", srna->identifier, prop->identifier);
859                                         DefRNA.error= 1;
860                                 }
861                         }
862                         if(!cprop->type) {
863                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a struct type.\n", srna->identifier, prop->identifier);
864                                 DefRNA.error= 1;
865                         }
866                         break;
867                 }
868         }
869 }
870
871 static void rna_def_property_funcs_header(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
872 {
873         PropertyRNA *prop;
874         char *func;
875
876         prop= dp->prop;
877
878         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
879                 return;
880
881         func= rna_alloc_function_name(srna->identifier, prop->identifier, "");
882
883         switch(prop->type) {
884                 case PROP_BOOLEAN:
885                 case PROP_INT: {
886                         if(!prop->arraylength) {
887                                 fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
888                                 //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
889                         }
890                         else {
891                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[%d]);\n", func, prop->arraylength);
892                                 //fprintf(f, "void %sset(PointerRNA *ptr, const int values[%d]);\n", func, prop->arraylength);
893                         }
894                         break;
895                 }
896                 case PROP_FLOAT: {
897                         if(!prop->arraylength) {
898                                 fprintf(f, "float %sget(PointerRNA *ptr);\n", func);
899                                 //fprintf(f, "void %sset(PointerRNA *ptr, float value);\n", func);
900                         }
901                         else {
902                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[%d]);\n", func, prop->arraylength);
903                                 //fprintf(f, "void %sset(PointerRNA *ptr, const float values[%d]);\n", func, prop->arraylength);
904                         }
905                         break;
906                 }
907                 case PROP_ENUM: {
908                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
909                         int i;
910
911                         if(eprop->item) {
912                                 fprintf(f, "enum {\n");
913
914                                 for(i=0; i<eprop->totitem; i++)
915                                         fprintf(f, "\t%s_%s_%s = %d,\n", srna->identifier, prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
916
917                                 fprintf(f, "};\n\n");
918                         }
919
920                         fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
921                         //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
922
923                         break;
924                 }
925                 case PROP_STRING: {
926                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
927
928                         if(sprop->maxlength) {
929                                 fprintf(f, "#define %s_%s_MAX %d\n\n", srna->identifier, prop->identifier, sprop->maxlength);
930                         }
931                         
932                         fprintf(f, "void %sget(PointerRNA *ptr, char *value);\n", func);
933                         fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
934                         //fprintf(f, "void %sset(PointerRNA *ptr, const char *value);\n", func);
935
936                         break;
937                 }
938                 case PROP_POINTER: {
939                         fprintf(f, "PointerRNA %sget(PointerRNA *ptr);\n", func);
940                         //fprintf(f, "void %sset(PointerRNA *ptr, PointerRNA value);\n", func);
941                         break;
942                 }
943                 case PROP_COLLECTION: {
944                         fprintf(f, "void %sbegin(CollectionPropertyIterator *iter, PointerRNA *ptr);\n", func);
945                         fprintf(f, "void %snext(CollectionPropertyIterator *iter);\n", func);
946                         fprintf(f, "void %send(CollectionPropertyIterator *iter);\n", func);
947                         //fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
948                         //fprintf(f, "void %slookup_int(PointerRNA *ptr, int key, StructRNA **type);\n", func);
949                         //fprintf(f, "void %slookup_string(PointerRNA *ptr, const char *key, StructRNA **type);\n", func);
950                         break;
951                 }
952         }
953
954         fprintf(f, "\n");
955 }
956
957 static void rna_def_property_funcs_header_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
958 {
959         PropertyRNA *prop;
960
961         prop= dp->prop;
962
963         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
964                 return;
965         
966         if(prop->name && prop->description && strcmp(prop->description, "") != 0)
967                 fprintf(f, "\t/* %s: %s */\n", prop->name, prop->description);
968         else if(prop->name)
969                 fprintf(f, "\t/* %s */\n", prop->name);
970         else
971                 fprintf(f, "\t/* */\n");
972
973         switch(prop->type) {
974                 case PROP_BOOLEAN: {
975                         if(!prop->arraylength)
976                                 fprintf(f, "\tbool %s(void);", prop->identifier);
977                         else
978                                 fprintf(f, "\tArray<int, %d> %s(void);", prop->arraylength, prop->identifier);
979                         break;
980                 }
981                 case PROP_INT: {
982                         if(!prop->arraylength)
983                                 fprintf(f, "\tint %s(void);", prop->identifier);
984                         else
985                                 fprintf(f, "\tArray<int, %d> %s(void);", prop->arraylength, prop->identifier);
986                         break;
987                 }
988                 case PROP_FLOAT: {
989                         if(!prop->arraylength)
990                                 fprintf(f, "\tfloat %s(void);", prop->identifier);
991                         else
992                                 fprintf(f, "\tArray<float, %d> %s(void);", prop->arraylength, prop->identifier);
993                         break;
994                 }
995                 case PROP_ENUM: {
996                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
997                         int i;
998
999                         if(eprop->item) {
1000                                 fprintf(f, "\tenum %s_enum {\n", prop->identifier);
1001
1002                                 for(i=0; i<eprop->totitem; i++)
1003                                         fprintf(f, "\t\t%s_%s = %d,\n", prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
1004
1005                                 fprintf(f, "\t};\n");
1006                         }
1007
1008                         fprintf(f, "\t%s_enum %s(void);", prop->identifier, prop->identifier);
1009                         break;
1010                 }
1011                 case PROP_STRING: {
1012                         fprintf(f, "\tstd::string %s(void);", prop->identifier);
1013                         break;
1014                 }
1015                 case PROP_POINTER: {
1016                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1017
1018                         if(pprop->type)
1019                                 fprintf(f, "\t%s %s(void);", (char*)pprop->type, prop->identifier);
1020                         else
1021                                 fprintf(f, "\t%s %s(void);", "UnknownType", prop->identifier);
1022                         break;
1023                 }
1024                 case PROP_COLLECTION: {
1025                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1026
1027                         if(cprop->type)
1028                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (char*)cprop->type, srna->identifier, prop->identifier);
1029                         else
1030                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1031                         break;
1032                 }
1033         }
1034
1035         fprintf(f, "\n");
1036 }
1037
1038 static void rna_def_property_funcs_impl_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1039 {
1040         PropertyRNA *prop;
1041
1042         prop= dp->prop;
1043
1044         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1045                 return;
1046
1047         switch(prop->type) {
1048                 case PROP_BOOLEAN: {
1049                         if(!prop->arraylength)
1050                                 fprintf(f, "\tBOOLEAN_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1051                         else
1052                                 fprintf(f, "\tBOOLEAN_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->arraylength, prop->identifier);
1053                         break;
1054                 }
1055                 case PROP_INT: {
1056                         if(!prop->arraylength)
1057                                 fprintf(f, "\tINT_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1058                         else
1059                                 fprintf(f, "\tINT_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->arraylength, prop->identifier);
1060                         break;
1061                 }
1062                 case PROP_FLOAT: {
1063                         if(!prop->arraylength)
1064                                 fprintf(f, "\tFLOAT_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1065                         else
1066                                 fprintf(f, "\tFLOAT_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->arraylength, prop->identifier);
1067                         break;
1068                 }
1069                 case PROP_ENUM: {
1070                         fprintf(f, "\tENUM_PROPERTY(%s_enum, %s, %s)", prop->identifier, srna->identifier, prop->identifier);
1071
1072                         break;
1073                 }
1074                 case PROP_STRING: {
1075                         fprintf(f, "\tSTRING_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1076                         break;
1077                 }
1078                 case PROP_POINTER: {
1079                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1080
1081                         if(pprop->type)
1082                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", (char*)pprop->type, srna->identifier, prop->identifier);
1083                         else
1084                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1085                         break;
1086                 }
1087                 case PROP_COLLECTION: {
1088                         /*CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1089
1090                         if(cprop->type)
1091                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (char*)cprop->type, srna->identifier, prop->identifier);
1092                         else
1093                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);*/
1094                         break;
1095                 }
1096         }
1097
1098         fprintf(f, "\n");
1099 }
1100
1101 static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA *dfunc)
1102 {
1103         StructRNA *srna;
1104         FunctionRNA *func;
1105         PropertyDefRNA *dparm;
1106         char *funcname, *ptrstr;
1107
1108         srna= dsrna->srna;
1109         func= dfunc->func;
1110
1111         if(func->flag & FUNC_REGISTER)
1112                 return;
1113
1114         funcname= rna_alloc_function_name(srna->identifier, func->identifier, "call");
1115
1116         fprintf(f, "void %s(PointerRNA *_ptr, ParameterList *_parms)", funcname);
1117         fprintf(f, "\n{\n");
1118
1119         if((func->flag & FUNC_TYPESTATIC)==0) {
1120                 if(dsrna->dnaname) fprintf(f, "\tstruct %s *_self;\n", dsrna->dnaname);
1121                 else fprintf(f, "\tstruct %s *_self;\n", srna->identifier);
1122         }
1123
1124         dparm= dfunc->cont.properties.first;
1125         for(; dparm; dparm= dparm->next) {
1126                 ptrstr= (dparm->prop->type == PROP_POINTER || dparm->prop->arraylength > 0)? "*" : "";
1127                 fprintf(f, "\t%s%s %s%s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
1128         }
1129
1130         fprintf(f, "\tchar *_data");
1131         if(func->ret) fprintf(f, ", *_retdata");
1132         fprintf(f, ";\n");
1133         fprintf(f, "\t\n");
1134
1135         if((func->flag & FUNC_TYPESTATIC)==0) {
1136                 if(dsrna->dnaname) fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", dsrna->dnaname);
1137                 else fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", srna->identifier);
1138         }
1139
1140         fprintf(f, "\t_data= (char *)_parms->data;\n");
1141
1142         dparm= dfunc->cont.properties.first;
1143         for(; dparm; dparm= dparm->next) {
1144                 if(dparm->prop==func->ret) 
1145                         fprintf(f, "\t_retdata= _data;\n");
1146                 else if(dparm->prop->arraylength)
1147                         fprintf(f, "\t%s= ((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1148                 else if(dparm->prop->type == PROP_POINTER) {
1149                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dparm->prop;
1150
1151                         if(strcmp((char*)pprop->type, "AnyType") == 0)
1152                                 fprintf(f, "\t%s= ((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1153                         else
1154                                 fprintf(f, "\t%s= *((%s%s**)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1155                 }
1156                 else
1157                         fprintf(f, "\t%s= *((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1158
1159                 if(dparm->next)
1160                         fprintf(f, "\t_data+= %d;\n", rna_parameter_size(dparm->prop));
1161         }
1162
1163         if(dfunc->call) {
1164                 fprintf(f, "\t\n");
1165                 fprintf(f, "\t");
1166                 if(func->ret) fprintf(f, "%s= ", func->ret->identifier);
1167                 fprintf(f, "%s(", dfunc->call);
1168
1169                 if((func->flag & FUNC_TYPESTATIC)==0)
1170                         fprintf(f, "_self");
1171
1172                 dparm= dfunc->cont.properties.first;
1173                 for(; dparm; dparm= dparm->next) {
1174                         if(dparm->prop==func->ret)
1175                                 continue;
1176
1177                         if((func->flag & FUNC_TYPESTATIC)==0 || dparm!=dfunc->cont.properties.first)
1178                                 fprintf(f, ", ");
1179                         fprintf(f, "%s", dparm->prop->identifier);
1180                 }
1181
1182                 fprintf(f, ");\n");
1183
1184                 if(func->ret) {
1185                         dparm= rna_find_parameter_def(func->ret);
1186                         ptrstr= dparm->prop->type == PROP_POINTER || dparm->prop->arraylength > 0 ? "*" : "";
1187                         fprintf(f, "\t*((%s%s%s*)_retdata)= %s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, func->ret->identifier);
1188                 }
1189         }
1190
1191         fprintf(f, "}\n\n");
1192
1193         dfunc->gencall= funcname;
1194 }
1195
1196 static void rna_auto_types()
1197 {
1198         StructDefRNA *ds;
1199         PropertyDefRNA *dp;
1200
1201         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1202                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
1203                 if(ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
1204                         ds->dnaname= "bScreen";
1205
1206                 for(dp=ds->cont.properties.first; dp; dp=dp->next) {
1207                         if(dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
1208                                 dp->dnastructname= "bScreen";
1209
1210                         if(dp->dnatype) {
1211                                 if(dp->prop->type == PROP_POINTER) {
1212                                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1213                                         StructRNA *type;
1214
1215                                         if(!pprop->type && !pprop->get)
1216                                                 pprop->type= (StructRNA*)rna_find_type(dp->dnatype);
1217
1218                                         if(pprop->type) {
1219                                                 type= rna_find_struct((char*)pprop->type);
1220                                                 if(type && (type->flag & STRUCT_ID_REFCOUNT))
1221                                                         pprop->property.flag |= PROP_ID_REFCOUNT;
1222                                         }
1223                                 }
1224                                 else if(dp->prop->type== PROP_COLLECTION) {
1225                                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1226
1227                                         if(!cprop->type && !cprop->get && strcmp(dp->dnatype, "ListBase")==0)
1228                                                 cprop->type= (StructRNA*)rna_find_type(dp->dnatype);
1229                                 }
1230                         }
1231                 }
1232         }
1233 }
1234
1235 static void rna_sort(BlenderRNA *brna)
1236 {
1237         StructDefRNA *ds;
1238         StructRNA *srna;
1239
1240         rna_sortlist(&brna->structs, cmp_struct);
1241         rna_sortlist(&DefRNA.structs, cmp_def_struct);
1242
1243         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1244                 rna_sortlist(&srna->cont.properties, cmp_property);
1245
1246         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1247                 rna_sortlist(&ds->cont.properties, cmp_def_property);
1248 }
1249
1250 static const char *rna_property_structname(PropertyType type)
1251 {
1252         switch(type) {
1253                 case PROP_BOOLEAN: return "BooleanPropertyRNA";
1254                 case PROP_INT: return "IntPropertyRNA";
1255                 case PROP_FLOAT: return "FloatPropertyRNA";
1256                 case PROP_STRING: return "StringPropertyRNA";
1257                 case PROP_ENUM: return "EnumPropertyRNA";
1258                 case PROP_POINTER: return "PointerPropertyRNA";
1259                 case PROP_COLLECTION: return "CollectionPropertyRNA";
1260                 default: return "UnknownPropertyRNA";
1261         }
1262 }
1263
1264 static const char *rna_property_typename(PropertyType type)
1265 {
1266         switch(type) {
1267                 case PROP_BOOLEAN: return "PROP_BOOLEAN";
1268                 case PROP_INT: return "PROP_INT";
1269                 case PROP_FLOAT: return "PROP_FLOAT";
1270                 case PROP_STRING: return "PROP_STRING";
1271                 case PROP_ENUM: return "PROP_ENUM";
1272                 case PROP_POINTER: return "PROP_POINTER";
1273                 case PROP_COLLECTION: return "PROP_COLLECTION";
1274                 default: return "PROP_UNKNOWN";
1275         }
1276 }
1277
1278 static const char *rna_property_subtypename(PropertyType type)
1279 {
1280         switch(type) {
1281                 case PROP_NONE: return "PROP_NONE";
1282                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
1283                 case PROP_FILEPATH: return "PROP_FILEPATH";
1284                 case PROP_DIRPATH: return "PROP_DIRPATH";
1285                 case PROP_COLOR: return "PROP_COLOR";
1286                 case PROP_VECTOR: return "PROP_VECTOR";
1287                 case PROP_MATRIX: return "PROP_MATRIX";
1288                 case PROP_ROTATION: return "PROP_ROTATION";
1289                 case PROP_NEVER_NULL: return "PROP_NEVER_NULL";
1290                 case PROP_PERCENTAGE: return "PROP_PERCENTAGE";
1291                 default: return "PROP_UNKNOWN";
1292         }
1293 }
1294
1295 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
1296 {
1297         StructRNA *srna;
1298
1299         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1300                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1301         fprintf(f, "\n");
1302 }
1303
1304 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
1305 {
1306         StructRNA *srna;
1307
1308         fprintf(f, "BlenderRNA BLENDER_RNA = {");
1309
1310         srna= brna->structs.first;
1311         if(srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
1312         else fprintf(f, "{NULL, ");
1313
1314         srna= brna->structs.last;
1315         if(srna) fprintf(f, "&RNA_%s}", srna->identifier);
1316         else fprintf(f, "NULL}");
1317
1318         fprintf(f, "};\n\n");
1319 }
1320
1321 static void rna_generate_property_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1322 {
1323         PropertyRNA *prop;
1324         StructRNA *base;
1325
1326         base= srna->base;
1327         while (base) {
1328                 fprintf(f, "\n");
1329                 for(prop=base->cont.properties.first; prop; prop=prop->next)
1330                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type), base->identifier, prop->identifier);
1331                 base= base->base;
1332         }
1333
1334         if(srna->cont.properties.first)
1335                 fprintf(f, "\n");
1336
1337         for(prop=srna->cont.properties.first; prop; prop=prop->next)
1338                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, prop->identifier);
1339         fprintf(f, "\n");
1340 }
1341
1342 static void rna_generate_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionRNA *func, FILE *f)
1343 {
1344         PropertyRNA *parm;
1345
1346         for(parm= func->cont.properties.first; parm; parm= parm->next)
1347                 fprintf(f, "%s%s rna_%s_%s_%s;\n", "extern ", rna_property_structname(parm->type), srna->identifier, func->identifier, parm->identifier);
1348
1349         if(func->cont.properties.first)
1350                 fprintf(f, "\n");
1351 }
1352
1353 static void rna_generate_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1354 {
1355         FunctionRNA *func;
1356         StructRNA *base;
1357
1358         base= srna->base;
1359         while (base) {
1360                 for(func= base->functions.first; func; func= func->cont.next) {
1361                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", "FunctionRNA", base->identifier, func->identifier);
1362                         rna_generate_parameter_prototypes(brna, base, func, f);
1363                 }
1364
1365                 if(base->functions.first)
1366                         fprintf(f, "\n");
1367
1368                 base= base->base;
1369         }
1370
1371         for(func= srna->functions.first; func; func= func->cont.next) {
1372                 fprintf(f, "%s%s rna_%s_%s;\n", "extern ", "FunctionRNA", srna->identifier, func->identifier);
1373                 rna_generate_parameter_prototypes(brna, srna, func, f);
1374         }
1375
1376         if(srna->functions.first)
1377                 fprintf(f, "\n");
1378 }
1379
1380 static void rna_generate_static_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionDefRNA *dfunc, FILE *f)
1381 {
1382         FunctionRNA *func;
1383         PropertyDefRNA *dparm;
1384         StructDefRNA *dsrna;
1385
1386         dsrna= rna_find_struct_def(srna);
1387         func= dfunc->func;
1388
1389         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1390                 if(dparm->prop==func->ret) {
1391                         if(dparm->prop->arraylength)
1392                                 fprintf(f, "XXX no array return types yet"); /* XXX not supported */
1393                         else if(dparm->prop->type == PROP_POINTER)
1394                                 fprintf(f, "%s%s *", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1395                         else
1396                                 fprintf(f, "%s%s ", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1397
1398                         break;
1399                 }
1400         }
1401
1402         if(!dparm)
1403                 fprintf(f, "void ");
1404
1405         fprintf(f, "%s(", dfunc->call);
1406
1407         if(dsrna->dnaname) fprintf(f, "struct %s *_self", dsrna->dnaname);
1408         else fprintf(f, "struct %s *_self", srna->identifier);
1409
1410         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1411                 if(dparm->prop==func->ret) ;
1412                 else if(dparm->prop->arraylength)
1413                         fprintf(f, ", %s%s %s[%d]", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier, dparm->prop->arraylength);
1414                 else if(dparm->prop->type == PROP_POINTER)
1415                         fprintf(f, ", %s%s *%s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier);
1416                 else
1417                         fprintf(f, ", %s%s %s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier);
1418         }
1419
1420         fprintf(f, ");\n");
1421 }
1422
1423 static void rna_generate_static_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1424 {
1425         FunctionRNA *func;
1426         FunctionDefRNA *dfunc;
1427
1428         fprintf(f, "/* Repeated prototypes to detect errors */\n\n");
1429
1430         for(func= srna->functions.first; func; func= func->cont.next) {
1431                 if(func->flag & FUNC_REGISTER)
1432                         continue;
1433
1434                 dfunc= rna_find_function_def(func);
1435                 if(dfunc->call)
1436                         rna_generate_static_parameter_prototypes(brna, srna, dfunc, f);
1437         }
1438
1439         fprintf(f, "\n");
1440 }
1441
1442 static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, PropertyRNA *prop) 
1443 {
1444         char *strnest= "", *errnest= "";
1445         int len, freenest= 0;
1446         
1447         if(nest != NULL) {
1448                 len= strlen(nest);
1449
1450                 strnest= MEM_mallocN(sizeof(char)*(len+1), "rna_generate_property -> strnest");
1451                 errnest= MEM_mallocN(sizeof(char)*(len+1), "rna_generate_property -> errnest");
1452
1453                 strcpy(strnest, "_"); strcat(strnest, nest);
1454                 strcpy(errnest, "."); strcat(errnest, nest);
1455
1456                 freenest= 1;
1457         }
1458
1459         switch(prop->type) {
1460                         case PROP_ENUM: {
1461                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1462                                 int i, defaultfound= 0;
1463
1464                                 if(eprop->item) {
1465                                         fprintf(f, "static EnumPropertyItem rna_%s%s_%s_items[%d] = {", srna->identifier, strnest, prop->identifier, eprop->totitem);
1466
1467                                         for(i=0; i<eprop->totitem; i++) {
1468                                                 fprintf(f, "{%d, ", eprop->item[i].value);
1469                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
1470                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
1471                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "}");
1472                                                 if(i != eprop->totitem-1)
1473                                                         fprintf(f, ", ");
1474
1475                                                 if(eprop->defaultvalue == eprop->item[i].value)
1476                                                         defaultfound= 1;
1477                                         }
1478
1479                                         fprintf(f, "};\n\n");
1480
1481                                         if(!defaultfound) {
1482                                                 fprintf(stderr, "rna_generate_structs: %s%s.%s, enum default is not in items.\n", srna->identifier, errnest, prop->identifier);
1483                                                 DefRNA.error= 1;
1484                                         }
1485                                 }
1486                                 else if(eprop->itemf);
1487                                 else {
1488                                         fprintf(stderr, "rna_generate_structs: %s%s.%s, enum must have items defined.\n", srna->identifier, errnest, prop->identifier);
1489                                         DefRNA.error= 1;
1490                                 }
1491                                 break;
1492                                                                                         }
1493                         case PROP_BOOLEAN: {
1494                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1495                                 unsigned int i;
1496
1497                                 if(prop->arraylength) {
1498                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->arraylength);
1499
1500                                         for(i=0; i<prop->arraylength; i++) {
1501                                                 if(bprop->defaultarray)
1502                                                         fprintf(f, "%d", bprop->defaultarray[i]);
1503                                                 else
1504                                                         fprintf(f, "%d", bprop->defaultvalue);
1505                                                 if(i != prop->arraylength-1)
1506                                                         fprintf(f, ", ");
1507                                         }
1508
1509                                         fprintf(f, "};\n\n");
1510                                 }
1511                                 break;
1512                                                                                                  }
1513                         case PROP_INT: {
1514                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1515                                 unsigned int i;
1516
1517                                 if(prop->arraylength) {
1518                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->arraylength);
1519
1520                                         for(i=0; i<prop->arraylength; i++) {
1521                                                 if(iprop->defaultarray)
1522                                                         fprintf(f, "%d", iprop->defaultarray[i]);
1523                                                 else
1524                                                         fprintf(f, "%d", iprop->defaultvalue);
1525                                                 if(i != prop->arraylength-1)
1526                                                         fprintf(f, ", ");
1527                                         }
1528
1529                                         fprintf(f, "};\n\n");
1530                                 }
1531                                 break;
1532                                                                                  }
1533                         case PROP_FLOAT: {
1534                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1535                                 unsigned int i;
1536
1537                                 if(prop->arraylength) {
1538                                         fprintf(f, "static float rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->arraylength);
1539
1540                                         for(i=0; i<prop->arraylength; i++) {
1541                                                 if(fprop->defaultarray)
1542                                                         rna_float_print(f, fprop->defaultarray[i]);
1543                                                 else
1544                                                         rna_float_print(f, fprop->defaultvalue);
1545                                                 if(i != prop->arraylength-1)
1546                                                         fprintf(f, ", ");
1547                                         }
1548
1549                                         fprintf(f, "};\n\n");
1550                                 }
1551                                 break;
1552                                                                                          }
1553                         default:
1554                                 break;
1555         }
1556
1557         fprintf(f, "%s%s rna_%s%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, strnest, prop->identifier);
1558
1559         if(prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s%s_%s, ", srna->identifier, strnest, prop->next->identifier);
1560         else fprintf(f, "\t{NULL, ");
1561         if(prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s%s_%s,\n", srna->identifier, strnest, prop->prev->identifier);
1562         else fprintf(f, "NULL,\n");
1563         fprintf(f, "\t%d, ", prop->magic);
1564         rna_print_c_string(f, prop->identifier);
1565         fprintf(f, ", %d, ", prop->flag);
1566         rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
1567         rna_print_c_string(f, prop->description); fprintf(f, ",\n");
1568         fprintf(f, "\t%s, %s, %d,\n", rna_property_typename(prop->type), rna_property_subtypename(prop->subtype), prop->arraylength);
1569         fprintf(f, "\t%s, %d, %s},\n", rna_function_string(prop->update), prop->noteflag, rna_function_string(prop->editable));
1570
1571         switch(prop->type) {
1572                         case PROP_BOOLEAN: {
1573                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1574                                 fprintf(f, "\t%s, %s, %s, %s, %d, ", rna_function_string(bprop->get), rna_function_string(bprop->set), rna_function_string(bprop->getarray), rna_function_string(bprop->setarray), bprop->defaultvalue);
1575                                 if(prop->arraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1576                                 else fprintf(f, "NULL\n");
1577                                 break;
1578                                                                                                  }
1579                         case PROP_INT: {
1580                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1581                                 fprintf(f, "\t%s, %s, %s, %s, %s,\n\t", rna_function_string(iprop->get), rna_function_string(iprop->set), rna_function_string(iprop->getarray), rna_function_string(iprop->setarray), rna_function_string(iprop->range));
1582                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
1583                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
1584                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
1585                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
1586                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
1587                                 rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
1588                                 if(prop->arraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1589                                 else fprintf(f, "NULL\n");
1590                                 break;
1591                                                                                  }
1592                         case PROP_FLOAT: {
1593                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1594                                 fprintf(f, "\t%s, %s, %s, %s, %s, ", rna_function_string(fprop->get), rna_function_string(fprop->set), rna_function_string(fprop->getarray), rna_function_string(fprop->setarray), rna_function_string(fprop->range));
1595                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
1596                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
1597                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
1598                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
1599                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
1600                                 rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
1601                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
1602                                 if(prop->arraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1603                                 else fprintf(f, "NULL\n");
1604                                 break;
1605                                                                                          }
1606                         case PROP_STRING: {
1607                                 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1608                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
1609                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
1610                                 break;
1611                                                                                                 }
1612                         case PROP_ENUM: {
1613                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1614                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(eprop->get), rna_function_string(eprop->set), rna_function_string(eprop->itemf));
1615                                 if(eprop->item)
1616                                         fprintf(f, "rna_%s%s_%s_items, ", srna->identifier, strnest, prop->identifier);
1617                                 else
1618                                         fprintf(f, "NULL, ");
1619                                 fprintf(f, "%d, %d\n", eprop->totitem, eprop->defaultvalue);
1620                                 break;
1621                                                                                         }
1622                         case PROP_POINTER: {
1623                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
1624                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(pprop->get), rna_function_string(pprop->set), rna_function_string(pprop->typef));
1625                                 if(pprop->type) fprintf(f, "&RNA_%s\n", (char*)pprop->type);
1626                                 else fprintf(f, "NULL\n");
1627                                 break;
1628                                                                                                  }
1629                         case PROP_COLLECTION: {
1630                                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1631                                 fprintf(f, "\t%s, %s, %s, %s, %s, %s, %s, ", rna_function_string(cprop->begin), rna_function_string(cprop->next), rna_function_string(cprop->end), rna_function_string(cprop->get), rna_function_string(cprop->length), rna_function_string(cprop->lookupint), rna_function_string(cprop->lookupstring));
1632                                 if(cprop->type) fprintf(f, "&RNA_%s\n", (char*)cprop->type);
1633                                 else fprintf(f, "NULL\n");
1634                                 break;
1635                                                                                                                 }
1636         }
1637
1638         fprintf(f, "};\n\n");
1639
1640         if(freenest) {
1641                 MEM_freeN(strnest);
1642                 MEM_freeN(errnest);
1643         }
1644 }
1645
1646 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
1647 {
1648         FunctionRNA *func;
1649         FunctionDefRNA *dfunc;
1650         PropertyRNA *prop, *parm;
1651         StructRNA *base;
1652
1653         fprintf(f, "/* %s */\n", srna->name);
1654
1655         for(prop= srna->cont.properties.first; prop; prop= prop->next)
1656                 rna_generate_property(f, srna, NULL, prop);
1657
1658         for(func= srna->functions.first; func; func= func->cont.next) {
1659                 for(parm= func->cont.properties.first; parm; parm= parm->next)
1660                         rna_generate_property(f, srna, func->identifier, parm);
1661
1662                 fprintf(f, "%s%s rna_%s_%s = {\n", "", "FunctionRNA", srna->identifier, func->identifier);
1663
1664                 if(func->cont.next) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s, ", srna->identifier, ((FunctionRNA*)func->cont.next)->identifier);
1665                 else fprintf(f, "\t{NULL, ");
1666                 if(func->cont.prev) fprintf(f, "(FunctionRNA*)&rna_%s_%s,\n", srna->identifier, ((FunctionRNA*)func->cont.prev)->identifier);
1667                 else fprintf(f, "NULL,\n");
1668
1669                 parm= func->cont.properties.first;
1670                 if(parm) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s_%s, ", srna->identifier, func->identifier, parm->identifier);
1671                 else fprintf(f, "\t{NULL, ");
1672
1673                 parm= func->cont.properties.last;
1674                 if(parm) fprintf(f, "(PropertyRNA*)&rna_%s_%s_%s}},\n", srna->identifier, func->identifier, parm->identifier);
1675                 else fprintf(f, "NULL}},\n");
1676
1677                 fprintf(f, "\t");
1678                 rna_print_c_string(f, func->identifier);
1679                 fprintf(f, ", %d, ", func->flag);
1680                 rna_print_c_string(f, func->description); fprintf(f, ",\n");
1681
1682                 dfunc= rna_find_function_def(func);
1683                 if(dfunc->gencall) fprintf(f, "\t%s,\n", dfunc->gencall);
1684                 else fprintf(f, "\tNULL,\n");
1685         
1686                 if(func->ret) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s_%s\n", srna->identifier, func->identifier, func->ret->identifier);
1687                 else fprintf(f, "\tNULL\n");
1688
1689                 fprintf(f, "};\n");
1690                 fprintf(f, "\n");
1691         }
1692
1693         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
1694
1695         if(srna->cont.next) fprintf(f, "\t{(ContainerRNA *)&RNA_%s, ", ((StructRNA*)srna->cont.next)->identifier);
1696         else fprintf(f, "\t{NULL, ");
1697         if(srna->cont.prev) fprintf(f, "(ContainerRNA *)&RNA_%s,\n", ((StructRNA*)srna->cont.prev)->identifier);
1698         else fprintf(f, "NULL,\n");
1699
1700         prop= srna->cont.properties.first;
1701         if(prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
1702         else fprintf(f, "\t{NULL, ");
1703
1704         prop= srna->cont.properties.last;
1705         if(prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}},\n", srna->identifier, prop->identifier);
1706         else fprintf(f, "NULL}},\n");
1707
1708         fprintf(f, "\tNULL,NULL,\n"); /* PyType - Cant initialize here */
1709         
1710         fprintf(f, "\t");
1711         rna_print_c_string(f, srna->identifier);
1712         fprintf(f, ", %d, ", srna->flag);
1713         rna_print_c_string(f, srna->name);
1714         fprintf(f, ", ");
1715         rna_print_c_string(f, srna->description);
1716         fprintf(f, ",\n %d,\n", srna->icon);
1717
1718         prop= srna->nameproperty;
1719         if(prop) {
1720                 base= srna;
1721                 while (base->base && base->base->nameproperty==prop)
1722                         base= base->base;
1723
1724                 fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", base->identifier, prop->identifier);
1725         }
1726         else fprintf(f, "\tNULL, ");
1727
1728         prop= srna->iteratorproperty;
1729         base= srna;
1730         while (base->base && base->base->iteratorproperty==prop)
1731                 base= base->base;
1732         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", base->identifier);
1733
1734         if(srna->base) fprintf(f, "\t&RNA_%s,\n", srna->base->identifier);
1735         else fprintf(f, "\tNULL,\n");
1736
1737         if(srna->nested) fprintf(f, "\t&RNA_%s,\n", srna->nested->identifier);
1738         else fprintf(f, "\tNULL,\n");
1739
1740         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
1741         fprintf(f, "\t%s,\n", rna_function_string(srna->path));
1742         fprintf(f, "\t%s,\n", rna_function_string(srna->reg));
1743         fprintf(f, "\t%s,\n", rna_function_string(srna->unreg));
1744         fprintf(f, "\t%s,\n", rna_function_string(srna->idproperties));
1745
1746         if(srna->reg && !srna->refine) {
1747                 fprintf(stderr, "rna_generate_struct: %s has a register function, must also have refine function.\n", srna->identifier);
1748                 DefRNA.error= 1;
1749         }
1750
1751         func= srna->functions.first;
1752         if(func) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s, ", srna->identifier, func->identifier);
1753         else fprintf(f, "\t{NULL, ");
1754
1755         func= srna->functions.last;
1756         if(func) fprintf(f, "(FunctionRNA*)&rna_%s_%s}\n", srna->identifier, func->identifier);
1757         else fprintf(f, "NULL}\n");
1758
1759         fprintf(f, "};\n");
1760
1761         fprintf(f, "\n");
1762 }
1763
1764 typedef struct RNAProcessItem {
1765         char *filename;
1766         void (*define)(BlenderRNA *brna);
1767 } RNAProcessItem;
1768
1769 RNAProcessItem PROCESS_ITEMS[]= {
1770         {"rna_rna.c", RNA_def_rna},
1771         {"rna_ID.c", RNA_def_ID},
1772         {"rna_texture.c", RNA_def_texture},
1773         {"rna_action.c", RNA_def_action},
1774         {"rna_animation.c", RNA_def_animation},
1775         {"rna_actuator.c", RNA_def_actuator},
1776         {"rna_armature.c", RNA_def_armature},
1777         {"rna_brush.c", RNA_def_brush},
1778         {"rna_camera.c", RNA_def_camera},
1779         {"rna_cloth.c", RNA_def_cloth},
1780         {"rna_color.c", RNA_def_color},
1781         {"rna_constraint.c", RNA_def_constraint},
1782         {"rna_context.c", RNA_def_context},
1783         {"rna_controller.c", RNA_def_controller},
1784         {"rna_curve.c", RNA_def_curve},
1785         {"rna_fcurve.c", RNA_def_fcurve},
1786         {"rna_fluidsim.c", RNA_def_fluidsim},
1787         {"rna_group.c", RNA_def_group},
1788         {"rna_image.c", RNA_def_image},
1789         {"rna_key.c", RNA_def_key},
1790         {"rna_lamp.c", RNA_def_lamp},
1791         {"rna_lattice.c", RNA_def_lattice},
1792         {"rna_main.c", RNA_def_main},
1793         {"rna_material.c", RNA_def_material},
1794         {"rna_mesh.c", RNA_def_mesh},
1795         {"rna_meta.c", RNA_def_meta},
1796         {"rna_modifier.c", RNA_def_modifier},
1797         {"rna_nodetree.c", RNA_def_nodetree},
1798         {"rna_object.c", RNA_def_object},
1799         {"rna_object_force.c", RNA_def_object_force},
1800         {"rna_packedfile.c", RNA_def_packedfile},
1801         {"rna_particle.c", RNA_def_particle},
1802         {"rna_pose.c", RNA_def_pose},
1803         {"rna_property.c", RNA_def_gameproperty},
1804         {"rna_radio.c", RNA_def_radio},
1805         {"rna_scene.c", RNA_def_scene},
1806         {"rna_screen.c", RNA_def_screen},
1807         {"rna_scriptlink.c", RNA_def_scriptlink},
1808         {"rna_sensor.c", RNA_def_sensor},
1809         {"rna_sequence.c", RNA_def_sequence},
1810         {"rna_space.c", RNA_def_space},
1811         {"rna_text.c", RNA_def_text},
1812         {"rna_timeline.c", RNA_def_timeline_marker},
1813         {"rna_sound.c", RNA_def_sound},
1814         {"rna_ui.c", RNA_def_ui},
1815         {"rna_userdef.c", RNA_def_userdef},
1816         {"rna_vfont.c", RNA_def_vfont},
1817         {"rna_vpaint.c", RNA_def_vpaint},
1818         {"rna_wm.c", RNA_def_wm},
1819         {"rna_world.c", RNA_def_world}, 
1820         {NULL, NULL}};
1821
1822 static void rna_generate(BlenderRNA *brna, FILE *f, char *filename)
1823 {
1824         StructDefRNA *ds;
1825         PropertyDefRNA *dp;
1826         FunctionDefRNA *dfunc;
1827         
1828         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
1829                      "   Do not edit manually, changes will be overwritten.           */\n\n"
1830                       "#define RNA_RUNTIME\n\n");
1831
1832         fprintf(f, "#include <float.h>\n");
1833         fprintf(f, "#include <limits.h>\n");
1834         fprintf(f, "#include <string.h>\n\n");
1835
1836         fprintf(f, "#include \"DNA_ID.h\"\n");
1837
1838         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
1839
1840         fprintf(f, "#include \"BKE_library.h\"\n");
1841         fprintf(f, "#include \"BKE_utildefines.h\"\n\n");
1842
1843         fprintf(f, "#include \"RNA_define.h\"\n");
1844         fprintf(f, "#include \"RNA_types.h\"\n");
1845         fprintf(f, "#include \"rna_internal.h\"\n\n");
1846
1847         rna_generate_prototypes(brna, f);
1848
1849         fprintf(f, "#include \"%s\"\n\n", filename);
1850
1851         fprintf(f, "/* Autogenerated Functions */\n\n");
1852
1853         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1854                 if(!filename || ds->filename == filename) {
1855                         rna_generate_property_prototypes(brna, ds->srna, f);
1856                         rna_generate_function_prototypes(brna, ds->srna, f);
1857                 }
1858         }
1859
1860         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1861                 if(!filename || ds->filename == filename)
1862                         for(dp=ds->cont.properties.first; dp; dp=dp->next)
1863                                 rna_def_property_funcs(f, ds->srna, dp);
1864
1865         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1866                 if(!filename || ds->filename == filename) {
1867                         for(dfunc=ds->functions.first; dfunc; dfunc= dfunc->cont.next)
1868                                 rna_def_function_funcs(f, ds, dfunc);
1869
1870                         rna_generate_static_function_prototypes(brna, ds->srna, f);
1871                 }
1872         }
1873
1874         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1875                 if(!filename || ds->filename == filename)
1876                         rna_generate_struct(brna, ds->srna, f);
1877
1878         if(strcmp(filename, "rna_ID.c") == 0) {
1879                 /* this is ugly, but we cannot have c files compiled for both
1880                  * makesrna and blender with some build systems at the moment */
1881                 fprintf(f, "#include \"rna_define.c\"\n\n");
1882
1883                 rna_generate_blender(brna, f);
1884         }
1885 }
1886
1887 static void rna_generate_header(BlenderRNA *brna, FILE *f)
1888 {
1889         StructDefRNA *ds;
1890         PropertyDefRNA *dp;
1891         StructRNA *srna;
1892
1893         fprintf(f, "\n#ifndef __RNA_BLENDER_H__\n");
1894         fprintf(f, "#define __RNA_BLENDER_H__\n\n");
1895
1896         fprintf(f, "/* Automatically generated function declarations for the Data API.\n"
1897                      "   Do not edit manually, changes will be overwritten.              */\n\n");
1898
1899         fprintf(f, "#include \"RNA_types.h\"\n\n");
1900
1901         fprintf(f, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
1902
1903         fprintf(f, "#define FOREACH_BEGIN(property, sptr, itemptr) \\\n");
1904         fprintf(f, "    { \\\n");
1905         fprintf(f, "            CollectionPropertyIterator rna_macro_iter; \\\n");
1906         fprintf(f, "            for(property##_begin(&rna_macro_iter, sptr); rna_macro_iter.valid; property##_next(&rna_macro_iter)) { \\\n");
1907         fprintf(f, "                    itemptr= rna_macro_iter.ptr;\n\n");
1908
1909         fprintf(f, "#define FOREACH_END(property) \\\n");
1910         fprintf(f, "            } \\\n");
1911         fprintf(f, "            property##_end(&rna_macro_iter); \\\n");
1912         fprintf(f, "    }\n\n");
1913
1914         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1915                 srna= ds->srna;
1916
1917                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
1918
1919                 while(srna) {
1920                         fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1921                         srna= srna->base;
1922                 }
1923                 fprintf(f, "\n");
1924
1925                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
1926                         rna_def_property_funcs_header(f, ds->srna, dp);
1927         }
1928
1929         fprintf(f, "#ifdef __cplusplus\n}\n#endif\n\n");
1930
1931         fprintf(f, "#endif /* __RNA_BLENDER_H__ */\n\n");
1932 }
1933
1934 static const char *cpp_classes = ""
1935 "\n"
1936 "#include <string>\n"
1937 "\n"
1938 "namespace RNA {\n"
1939 "\n"
1940 "#define BOOLEAN_PROPERTY(sname, identifier) \\\n"
1941 "       bool sname::identifier(void) { return (bool)sname##_##identifier##_get(&ptr); }\n"
1942 "\n"
1943 "#define BOOLEAN_ARRAY_PROPERTY(sname, size, identifier) \\\n"
1944 "       Array<int,size> sname::identifier(void) \\\n"
1945 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
1946 "\n"
1947 "#define INT_PROPERTY(sname, identifier) \\\n"
1948 "       int sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
1949 "\n"
1950 "#define INT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
1951 "       Array<int,size> sname::identifier(void) \\\n"
1952 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
1953 "\n"
1954 "#define FLOAT_PROPERTY(sname, identifier) \\\n"
1955 "       float sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
1956 "\n"
1957 "#define FLOAT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
1958 "       Array<float,size> sname::identifier(void) \\\n"
1959 "               { Array<float, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
1960 "\n"
1961 "#define ENUM_PROPERTY(type, sname, identifier) \\\n"
1962 "       sname::type sname::identifier(void) { return (type)sname##_##identifier##_get(&ptr); }\n"
1963 "\n"
1964 "#define STRING_PROPERTY(sname, identifier) \\\n"
1965 "       std::string sname::identifier(void) { \\\n"
1966 "               int len= sname##_##identifier##_length(&ptr); \\\n"
1967 "               std::string str; str.resize(len); \\\n"
1968 "               sname##_##identifier##_get(&ptr, &str[0]); return str; } \\\n"
1969 "\n"
1970 "#define POINTER_PROPERTY(type, sname, identifier) \\\n"
1971 "       type sname::identifier(void) { return type(sname##_##identifier##_get(&ptr)); }\n"
1972 "\n"
1973 "#define COLLECTION_PROPERTY(type, sname, identifier) \\\n"
1974 "       typedef CollectionIterator<type, sname##_##identifier##_begin, \\\n"
1975 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier##_iterator; \\\n"
1976 "       Collection<sname, type, sname##_##identifier##_begin, \\\n"
1977 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier;\n"
1978 "\n"
1979 "class Pointer {\n"
1980 "public:\n"
1981 "       Pointer(const PointerRNA& p) : ptr(p) { }\n"
1982 "       operator const PointerRNA&() { return ptr; }\n"
1983 "       bool is_a(StructRNA *type) { return RNA_struct_is_a(&ptr, type); }\n"
1984 "       operator void*() { return ptr.data; }\n"
1985 "       operator bool() { return ptr.data != NULL; }\n"
1986 "\n"
1987 "       PointerRNA ptr;\n"
1988 "};\n"
1989 "\n"
1990 "\n"
1991 "template<typename T, int Tsize>\n"
1992 "class Array {\n"
1993 "public:\n"
1994 "       T data[Tsize];\n"
1995 "       operator T*() { return data; }\n"
1996 "};\n"
1997 "\n"
1998 "typedef void (*TBeginFunc)(CollectionPropertyIterator *iter, PointerRNA *ptr);\n"
1999 "typedef void (*TNextFunc)(CollectionPropertyIterator *iter);\n"
2000 "typedef void (*TEndFunc)(CollectionPropertyIterator *iter);\n"
2001 "\n"
2002 "template<typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2003 "class CollectionIterator {\n"
2004 "public:\n"
2005 "       CollectionIterator() : t(iter.ptr), init(false) { iter.valid= false; }\n"
2006 "       ~CollectionIterator(void) { if(init) Tend(&iter); };\n"
2007 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator=(const CollectionIterator<T, Tbegin, Tnext, Tend>& copy)\n"
2008 "       { if(init) Tend(&iter); iter= copy.iter; if(iter.internal) iter.internal= MEM_dupallocN(iter.internal); t= copy.t; init= copy.init; return *this; }\n"
2009 "\n"
2010 "       operator bool(void)\n"
2011 "       { return iter.valid != 0; }\n"
2012 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator++() { Tnext(&iter); t = T(iter.ptr); return *this; }\n"
2013 "       T& operator*(void) { return t; }\n"
2014 "       T* operator->(void) { return &t; }\n"
2015 "       bool operator==(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid == other.iter.valid; }\n"
2016 "       bool operator!=(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid != other.iter.valid; }\n"
2017 "\n"
2018 "       void begin(const Pointer& ptr)\n"
2019 "       { if(init) Tend(&iter); Tbegin(&iter, (PointerRNA*)&ptr.ptr); t = T(iter.ptr); init = true; }\n"
2020 "\n"
2021 "private:\n"
2022 "       CollectionPropertyIterator iter;\n"
2023 "       T t;\n"
2024 "       bool init;\n"
2025 "};\n"
2026 "\n"
2027 "template<typename Tp, typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2028 "class Collection {\n"
2029 "public:\n"
2030 "       Collection(const PointerRNA& p) : ptr(p) {}\n"
2031 "\n"
2032 "       CollectionIterator<T, Tbegin, Tnext, Tend> begin()\n"
2033 "       { CollectionIterator<T, Tbegin, Tnext, Tend> iter; iter.begin(ptr); return iter; }\n"
2034 "       CollectionIterator<T, Tbegin, Tnext, Tend> end()\n"
2035 "       { return CollectionIterator<T, Tbegin, Tnext, Tend>(); } /* test */ \n"
2036 "\n"
2037 "private:\n"
2038 "       PointerRNA ptr;\n"
2039 "};\n"
2040 "\n";
2041
2042 static void rna_generate_header_cpp(BlenderRNA *brna, FILE *f)
2043 {
2044         StructDefRNA *ds;
2045         PropertyDefRNA *dp;
2046         StructRNA *srna;
2047
2048         fprintf(f, "\n#ifndef __RNA_BLENDER_CPP_H__\n");
2049         fprintf(f, "#define __RNA_BLENDER_CPP_H__\n\n");
2050
2051         fprintf(f, "/* Automatically generated classes for the Data API.\n"
2052                      "   Do not edit manually, changes will be overwritten. */\n\n");
2053
2054         fprintf(f, "#include \"RNA_blender.h\"\n");
2055         fprintf(f, "#include \"RNA_types.h\"\n");
2056
2057         fprintf(f, cpp_classes);
2058
2059         fprintf(f, "/**************** Declarations ****************/\n\n");
2060
2061         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2062                 fprintf(f, "class %s;\n", ds->srna->identifier);
2063         fprintf(f, "\n");
2064
2065         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2066                 srna= ds->srna;
2067
2068                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2069
2070                 fprintf(f, "class %s : public %s {\n", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2071                 fprintf(f, "public:\n");
2072                 fprintf(f, "\t%s(const PointerRNA& ptr) :\n\t\t%s(ptr)", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2073                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2074                         if(!(dp->prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN)))
2075                                 if(dp->prop->type == PROP_COLLECTION)
2076                                         fprintf(f, ",\n\t\t%s(ptr)", dp->prop->identifier);
2077                 fprintf(f, "\n\t\t{}\n\n");
2078
2079                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2080                         rna_def_property_funcs_header_cpp(f, ds->srna, dp);
2081                 fprintf(f, "};\n\n");
2082         }
2083
2084
2085         fprintf(f, "/**************** Implementation ****************/\n");
2086
2087         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2088                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2089                         rna_def_property_funcs_impl_cpp(f, ds->srna, dp);
2090
2091                 fprintf(f, "\n");
2092         }
2093
2094         fprintf(f, "}\n\n#endif /* __RNA_BLENDER_CPP_H__ */\n\n");
2095 }
2096
2097 static void make_bad_file(char *file)
2098 {
2099         FILE *fp= fopen(file, "w");
2100         fprintf(fp, "ERROR! Cannot make correct RNA file, STUPID!\n");
2101         fclose(fp);
2102 }
2103
2104 static int rna_preprocess(char *outfile)
2105 {
2106         BlenderRNA *brna;
2107         StructDefRNA *ds;
2108         FILE *file;
2109         char deffile[4096];
2110         int i, status;
2111
2112         /* define rna */
2113         brna= RNA_create();
2114
2115         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2116                 if(PROCESS_ITEMS[i].define) {
2117                         PROCESS_ITEMS[i].define(brna);
2118
2119                         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2120                                 if(!ds->filename)
2121                                         ds->filename= PROCESS_ITEMS[i].filename;
2122                 }
2123         }
2124
2125         rna_auto_types();
2126
2127
2128         /* create RNA_blender_cpp.h */
2129         strcpy(deffile, outfile);
2130         strcat(deffile, "RNA_blender_cpp.h");
2131
2132         status= (DefRNA.error != 0);
2133
2134         if(status) {
2135                 make_bad_file(deffile);
2136         }
2137         else {
2138                 file = fopen(deffile, "w");
2139
2140                 if(!file) {
2141                         printf ("Unable to open file: %s\n", deffile);
2142                         status = 1;
2143                 }
2144                 else {
2145                         rna_generate_header_cpp(brna, file);
2146                         fclose(file);
2147
2148                         status= (DefRNA.error != 0);
2149                 }
2150         }
2151
2152         rna_sort(brna);
2153
2154         /* create rna_gen_*.c files */
2155         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2156                 strcpy(deffile, outfile);
2157                 strcat(deffile, PROCESS_ITEMS[i].filename);
2158                 deffile[strlen(deffile)-2] = '\0';
2159                 strcat(deffile, "_gen.c");
2160
2161                 if(status) {
2162                         make_bad_file(deffile);
2163                 }
2164                 else {
2165                         file = fopen(deffile, "w");
2166
2167                         if(!file) {
2168                                 printf ("Unable to open file: %s\n", deffile);
2169                                 status = 1;
2170                         }
2171                         else {
2172                                 rna_generate(brna, file, PROCESS_ITEMS[i].filename);
2173                                 fclose(file);
2174
2175                                 status= (DefRNA.error != 0);
2176                         }
2177                 }
2178         }
2179
2180         /* create RNA_blender.h */
2181         strcpy(deffile, outfile);
2182         strcat(deffile, "RNA_blender.h");
2183
2184         if(status) {
2185                 make_bad_file(deffile);
2186         }
2187         else {
2188                 file = fopen(deffile, "w");
2189
2190                 if(!file) {
2191                         printf ("Unable to open file: %s\n", deffile);
2192                         status = 1;
2193                 }
2194                 else {
2195                         rna_generate_header(brna, file);
2196                         fclose(file);
2197
2198                         status= (DefRNA.error != 0);
2199                 }
2200         }
2201
2202         /* free RNA */
2203         RNA_define_free(brna);
2204         RNA_free(brna);
2205
2206         return status;
2207 }
2208
2209 static void mem_error_cb(char *errorStr)
2210 {
2211         fprintf(stderr, "%s", errorStr);
2212         fflush(stderr);
2213 }
2214
2215 int main(int argc, char **argv)
2216 {
2217         int totblock, return_status = 0;
2218
2219         if(argc<2) {
2220                 printf("Usage: %s outdirectory/\n", argv[0]);
2221                 return_status = 1;
2222         }
2223         else {
2224                 printf("Running makesrna, program versions %s\n",  RNA_VERSION_DATE);
2225                 return_status= rna_preprocess(argv[1]);
2226         }
2227
2228         totblock= MEM_get_memory_blocks_in_use();
2229         if(totblock!=0) {
2230                 printf("Error Totblock: %d\n",totblock);
2231                 MEM_set_error_callback(mem_error_cb);
2232                 MEM_printmemlist();
2233         }
2234
2235         return return_status;
2236 }
2237
2238