remove unused rna includes
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_define.h"
34
35 #include "rna_internal.h"
36
37 #define RNA_VERSION_DATE "$Id$"
38
39 #ifdef _WIN32
40 #ifndef snprintf
41 #define snprintf _snprintf
42 #endif
43 #endif
44
45 /* Replace if different */
46 #define TMP_EXT ".tmp"
47
48 static int replace_if_different(char *tmpfile)
49 {
50
51 #define REN_IF_DIFF \
52         remove(orgfile); \
53         if(rename(tmpfile, orgfile) != 0) { \
54                 fprintf(stderr, "%s:%d, rename error: \"%s\" -> \"%s\"\n", __FILE__, __LINE__, tmpfile, orgfile); \
55                 return -1; \
56         } \
57         remove(tmpfile); \
58         return 1; \
59 /* end REN_IF_DIFF */
60
61
62         FILE *fp_new, *fp_org;
63         int len_new, len_org;
64         char *arr_new, *arr_org;
65         int cmp;
66
67         char orgfile[4096];
68
69         strcpy(orgfile, tmpfile);
70         orgfile[strlen(orgfile) - strlen(TMP_EXT)] = '\0'; /* strip '.tmp' */
71
72         fp_org= fopen(orgfile, "rb");
73
74         if(fp_org==NULL) {
75                 REN_IF_DIFF;
76         }
77
78         fp_new= fopen(tmpfile, "rb");
79
80         if(fp_new==NULL) {
81                 /* shouldn't happen, just to be safe */
82                 fprintf(stderr, "%s:%d, open error: \"%s\"\n", __FILE__, __LINE__, tmpfile);
83                 return -1;
84         }
85
86         fseek(fp_new, 0L, SEEK_END); len_new = ftell(fp_new); fseek(fp_new, 0L, SEEK_SET);
87         fseek(fp_org, 0L, SEEK_END); len_org = ftell(fp_org); fseek(fp_org, 0L, SEEK_SET);
88
89
90         if(len_new != len_org) {
91                 fclose(fp_new);
92                 fclose(fp_org);
93                 REN_IF_DIFF;
94         }
95
96         /* now compare the files... */
97         arr_new= MEM_mallocN(sizeof(char)*len_new, "rna_cmp_file_new");
98         arr_org= MEM_mallocN(sizeof(char)*len_org, "rna_cmp_file_org");
99
100         if(fread(arr_new, sizeof(char), len_new, fp_new) != len_new)
101                 fprintf(stderr, "%s:%d, error reading file %s for comparison.\n", __FILE__, __LINE__, tmpfile);
102         if(fread(arr_org, sizeof(char), len_org, fp_org) != len_org)
103                 fprintf(stderr, "%s:%d, error reading file %s for comparison.\n", __FILE__, __LINE__, orgfile);
104
105         fclose(fp_new);
106         fclose(fp_org);
107
108         cmp= memcmp(arr_new, arr_org, len_new);
109
110         MEM_freeN(arr_new);
111         MEM_freeN(arr_org);
112
113         if(cmp) {
114                 REN_IF_DIFF;
115         }
116         else {
117                 remove(tmpfile);
118                 return 0;
119         }
120
121 #undef REN_IF_DIFF
122 }
123
124
125
126 /* Sorting */
127
128 static int cmp_struct(const void *a, const void *b)
129 {
130         const StructRNA *structa= *(const StructRNA**)a;
131         const StructRNA *structb= *(const StructRNA**)b;
132
133         return strcmp(structa->identifier, structb->identifier);
134 }
135
136 static int cmp_property(const void *a, const void *b)
137 {
138         const PropertyRNA *propa= *(const PropertyRNA**)a;
139         const PropertyRNA *propb= *(const PropertyRNA**)b;
140
141         if(strcmp(propa->identifier, "rna_type") == 0) return -1;
142         else if(strcmp(propb->identifier, "rna_type") == 0) return 1;
143
144         if(strcmp(propa->identifier, "name") == 0) return -1;
145         else if(strcmp(propb->identifier, "name") == 0) return 1;
146
147         return strcmp(propa->name, propb->name);
148 }
149
150 static int cmp_def_struct(const void *a, const void *b)
151 {
152         const StructDefRNA *dsa= *(const StructDefRNA**)a;
153         const StructDefRNA *dsb= *(const StructDefRNA**)b;
154
155         return cmp_struct(&dsa->srna, &dsb->srna);
156 }
157
158 static int cmp_def_property(const void *a, const void *b)
159 {
160         const PropertyDefRNA *dpa= *(const PropertyDefRNA**)a;
161         const PropertyDefRNA *dpb= *(const PropertyDefRNA**)b;
162
163         return cmp_property(&dpa->prop, &dpb->prop);
164 }
165
166 static void rna_sortlist(ListBase *listbase, int(*cmp)(const void*, const void*))
167 {
168         Link *link;
169         void **array;
170         int a, size;
171         
172         if(listbase->first == listbase->last)
173                 return;
174
175         for(size=0, link=listbase->first; link; link=link->next)
176                 size++;
177
178         array= MEM_mallocN(sizeof(void*)*size, "rna_sortlist");
179         for(a=0, link=listbase->first; link; link=link->next, a++)
180                 array[a]= link;
181
182         qsort(array, size, sizeof(void*), cmp);
183
184         listbase->first= listbase->last= NULL;
185         for(a=0; a<size; a++) {
186                 link= array[a];
187                 link->next= link->prev= NULL;
188                 rna_addtail(listbase, link);
189         }
190
191         MEM_freeN(array);
192 }
193
194 /* Preprocessing */
195
196 static void rna_print_c_string(FILE *f, const char *str)
197 {
198         static char *escape[] = {"\''", "\"\"", "\??", "\\\\","\aa", "\bb", "\ff", "\nn", "\rr", "\tt", "\vv", NULL};
199         int i, j;
200
201         if(!str) {
202                 fprintf(f, "NULL");
203                 return;
204         }
205
206         fprintf(f, "\"");
207         for(i=0; str[i]; i++) {
208                 for(j=0; escape[j]; j++)
209                         if(str[i] == escape[j][0])
210                                 break;
211
212                 if(escape[j]) fprintf(f, "\\%c", escape[j][1]);
213                 else fprintf(f, "%c", str[i]);
214         }
215         fprintf(f, "\"");
216 }
217
218 static void rna_print_data_get(FILE *f, PropertyDefRNA *dp)
219 {
220         if(dp->dnastructfromname && dp->dnastructfromprop)
221                 fprintf(f, "    %s *data= (%s*)(((%s*)ptr->data)->%s);\n", dp->dnastructname, dp->dnastructname, dp->dnastructfromname, dp->dnastructfromprop);
222         else
223                 fprintf(f, "    %s *data= (%s*)(ptr->data);\n", dp->dnastructname, dp->dnastructname);
224 }
225
226 static void rna_print_id_get(FILE *f, PropertyDefRNA *dp)
227 {
228         fprintf(f, "    ID *id= ptr->id.data;\n");
229 }
230
231 static char *rna_alloc_function_name(const char *structname, const char *propname, const char *type)
232 {
233         AllocDefRNA *alloc;
234         char buffer[2048];
235         char *result;
236
237         snprintf(buffer, sizeof(buffer), "%s_%s_%s", structname, propname, type);
238         result= MEM_callocN(sizeof(char)*strlen(buffer)+1, "rna_alloc_function_name");
239         strcpy(result, buffer);
240
241         alloc= MEM_callocN(sizeof(AllocDefRNA), "AllocDefRNA");
242         alloc->mem= result;
243         rna_addtail(&DefRNA.allocs, alloc);
244
245         return result;
246 }
247
248 static StructRNA *rna_find_struct(const char *identifier)
249 {
250         StructDefRNA *ds;
251
252         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
253                 if(strcmp(ds->srna->identifier, identifier)==0)
254                         return ds->srna;
255
256         return NULL;
257 }
258
259 static const char *rna_find_type(const char *type)
260 {
261         StructDefRNA *ds;
262
263         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
264                 if(ds->dnaname && strcmp(ds->dnaname, type)==0)
265                         return ds->srna->identifier;
266
267         return NULL;
268 }
269
270 static const char *rna_find_dna_type(const char *type)
271 {
272         StructDefRNA *ds;
273
274         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
275                 if(strcmp(ds->srna->identifier, type)==0)
276                         return ds->dnaname;
277
278         return NULL;
279 }
280
281 static const char *rna_type_type_name(PropertyRNA *prop)
282 {
283         switch(prop->type) {
284                 case PROP_BOOLEAN:
285                 case PROP_INT:
286                 case PROP_ENUM:
287                         return "int";
288                 case PROP_FLOAT:
289                         return "float";
290                 case PROP_STRING:
291                         return "char*";
292                 default:
293                         return NULL;
294         }
295 }
296
297 static const char *rna_type_type(PropertyRNA *prop)
298 {
299         const char *type;
300
301         type= rna_type_type_name(prop);
302
303         if(type)
304                 return type;
305
306         return "PointerRNA";
307 }
308
309 static const char *rna_type_struct(PropertyRNA *prop)
310 {
311         const char *type;
312
313         type= rna_type_type_name(prop);
314
315         if(type)
316                 return "";
317
318         return "struct ";
319 }
320
321 static const char *rna_parameter_type_name(PropertyRNA *parm)
322 {
323         const char *type;
324
325         type= rna_type_type_name(parm);
326
327         if(type)
328                 return type;
329
330         switch(parm->type) {
331                 case PROP_POINTER:  {
332                         PointerPropertyRNA *pparm= (PointerPropertyRNA*)parm;
333
334                         if(parm->flag & PROP_RNAPTR)
335                                 return "PointerRNA";
336                         else
337                                 return rna_find_dna_type((const char *)pparm->type);
338                 }
339                 case PROP_COLLECTION: {
340                         return "ListBase";
341                 }
342                 default:
343                         return "<error, no type specified>";
344         }
345 }
346
347 static int rna_enum_bitmask(PropertyRNA *prop)
348 {
349         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
350         int a, mask= 0;
351
352         if(eprop->item) {
353                 for(a=0; a<eprop->totitem; a++)
354                         if(eprop->item[a].identifier[0])
355                                 mask |= eprop->item[a].value;
356         }
357         
358         return mask;
359 }
360
361 static int rna_color_quantize(PropertyRNA *prop, PropertyDefRNA *dp)
362 {
363         if(prop->type == PROP_FLOAT && prop->subtype == PROP_COLOR)
364                 if(strcmp(dp->dnatype, "float") != 0 && strcmp(dp->dnatype, "double") != 0)
365                         return 1;
366         
367         return 0;
368 }
369
370 static const char *rna_function_string(void *func)
371 {
372         return (func)? (const char*)func: "NULL";
373 }
374
375 static void rna_float_print(FILE *f, float num)
376 {
377         if(num == -FLT_MAX) fprintf(f, "-FLT_MAX");
378         else if(num == FLT_MAX) fprintf(f, "FLT_MAX");
379         else if((int)num == num) fprintf(f, "%.1ff", num);
380         else fprintf(f, "%.10ff", num);
381 }
382
383 static void rna_int_print(FILE *f, int num)
384 {
385         if(num == INT_MIN) fprintf(f, "INT_MIN");
386         else if(num == INT_MAX) fprintf(f, "INT_MAX");
387         else fprintf(f, "%d", num);
388 }
389
390 static char *rna_def_property_get_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
391 {
392         char *func;
393
394         if(prop->flag & PROP_IDPROPERTY)
395                 return NULL;
396
397         if(!manualfunc) {
398                 if(!dp->dnastructname || !dp->dnaname) {
399                         fprintf(stderr, "rna_def_property_get_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
400                         DefRNA.error= 1;
401                         return NULL;
402                 }
403         }
404
405         func= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
406
407         switch(prop->type) {
408                 case PROP_STRING: {
409                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
410                         fprintf(f, "void %s(PointerRNA *ptr, char *value)\n", func);
411                         fprintf(f, "{\n");
412                         if(manualfunc) {
413                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
414                         }
415                         else {
416                                 rna_print_data_get(f, dp);
417                                 if(sprop->maxlength)
418                                         fprintf(f, "    BLI_strncpy(value, data->%s, %d);\n", dp->dnaname, sprop->maxlength);
419                                 else
420                                         fprintf(f, "    BLI_strncpy(value, data->%s, sizeof(data->%s));\n", dp->dnaname, dp->dnaname);
421                         }
422                         fprintf(f, "}\n\n");
423                         break;
424                 }
425                 case PROP_POINTER: {
426                         fprintf(f, "PointerRNA %s(PointerRNA *ptr)\n", func);
427                         fprintf(f, "{\n");
428                         if(manualfunc) {
429                                 fprintf(f, "    return %s(ptr);\n", manualfunc);
430                         }
431                         else {
432                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
433                                 rna_print_data_get(f, dp);
434                                 if(dp->dnapointerlevel == 0)
435                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, &data->%s);\n", (char*)pprop->type, dp->dnaname);
436                                 else
437                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, data->%s);\n", (char*)pprop->type, dp->dnaname);
438                         }
439                         fprintf(f, "}\n\n");
440                         break;
441                 }
442                 case PROP_COLLECTION: {
443                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
444
445                         fprintf(f, "static PointerRNA %s(CollectionPropertyIterator *iter)\n", func);
446                         fprintf(f, "{\n");
447                         if(manualfunc) {
448                                 if(strcmp(manualfunc, "rna_iterator_listbase_get") == 0 ||
449                                    strcmp(manualfunc, "rna_iterator_array_get") == 0 ||
450                                    strcmp(manualfunc, "rna_iterator_array_dereference_get") == 0)
451                                         fprintf(f, "    return rna_pointer_inherit_refine(&iter->parent, &RNA_%s, %s(iter));\n", (cprop->item_type)? (char*)cprop->item_type: "UnknownType", manualfunc);
452                                 else
453                                         fprintf(f, "    return %s(iter);\n", manualfunc);
454                         }
455                         fprintf(f, "}\n\n");
456                         break;
457                 }
458                 default:
459                         if(prop->arraydimension) {
460                                 if(prop->flag & PROP_DYNAMIC)
461                                         fprintf(f, "void %s(PointerRNA *ptr, %s values[])\n", func, rna_type_type(prop));
462                                 else
463                                         fprintf(f, "void %s(PointerRNA *ptr, %s values[%d])\n", func, rna_type_type(prop), prop->totarraylength);
464                                 fprintf(f, "{\n");
465
466                                 if(manualfunc) {
467                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
468                                 }
469                                 else {
470                                         rna_print_data_get(f, dp);
471
472                                         if(prop->flag & PROP_DYNAMIC) {
473                                                 char *lenfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get_length");
474                                                 fprintf(f, "    int i, arraylen[RNA_MAX_ARRAY_DIMENSION];\n");
475                                                 fprintf(f, "    int len= %s(ptr, arraylen);\n\n", lenfunc);
476                                                 fprintf(f, "    for(i=0; i<len; i++) {\n");
477                                                 MEM_freeN(lenfunc);
478                                         }
479                                         else {
480                                                 fprintf(f, "    int i;\n\n");
481                                                 fprintf(f, "    for(i=0; i<%d; i++) {\n", prop->totarraylength);
482                                         }
483
484                                         if(dp->dnaarraylength == 1) {
485                                                 if(prop->type == PROP_BOOLEAN && dp->booleanbit)
486                                                         fprintf(f, "            values[i]= (%s(data->%s & (%d<<i)) != 0);\n", (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit);
487                                                 else
488                                                         fprintf(f, "            values[i]= (%s)%s((&data->%s)[i]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
489                                         }
490                                         else {
491                                                 if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
492                                                         fprintf(f, "            values[i]= (%s(data->%s[i] & ", (dp->booleannegative)? "!": "", dp->dnaname);
493                                                         rna_int_print(f, dp->booleanbit);
494                                                         fprintf(f, ") != 0);\n");
495                                                 }
496                                                 else if(rna_color_quantize(prop, dp))
497                                                         fprintf(f, "            values[i]= (%s)(data->%s[i]*(1.0f/255.0f));\n", rna_type_type(prop), dp->dnaname);
498                                                 else if(dp->dnatype)
499                                                         fprintf(f, "            values[i]= (%s)%s(((%s*)data->%s)[i]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnatype, dp->dnaname);
500                                                 else
501                                                         fprintf(f, "            values[i]= (%s)%s((data->%s)[i]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
502                                         }
503                                         fprintf(f, "    }\n");
504                                 }
505                                 fprintf(f, "}\n\n");
506                         }
507                         else {
508                                 fprintf(f, "%s %s(PointerRNA *ptr)\n", rna_type_type(prop), func);
509                                 fprintf(f, "{\n");
510
511                                 if(manualfunc) {
512                                         fprintf(f, "    return %s(ptr);\n", manualfunc);
513                                 }
514                                 else {
515                                         rna_print_data_get(f, dp);
516                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
517                                                 fprintf(f, "    return (%s((data->%s) & ", (dp->booleannegative)? "!": "", dp->dnaname);
518                                                 rna_int_print(f, dp->booleanbit);
519                                                 fprintf(f, ") != 0);\n");
520                                         }
521                                         else if(prop->type == PROP_ENUM && dp->enumbitflags) {
522                                                 fprintf(f, "    return ((data->%s) & ", dp->dnaname);
523                                                 rna_int_print(f, rna_enum_bitmask(prop));
524                                                 fprintf(f, ");\n");
525                                         }
526                                         else
527                                                 fprintf(f, "    return (%s)%s(data->%s);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
528                                 }
529
530                                 fprintf(f, "}\n\n");
531                         }
532                         break;
533         }
534
535         return func;
536 }
537
538 static void rna_clamp_value(FILE *f, PropertyRNA *prop, int array)
539 {
540         if(prop->type == PROP_INT) {
541                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
542
543                 if(iprop->hardmin != INT_MIN || iprop->hardmax != INT_MAX) {
544                         if(array) fprintf(f, "CLAMPIS(values[i], ");
545                         else fprintf(f, "CLAMPIS(value, ");
546                         rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
547                         rna_int_print(f, iprop->hardmax); fprintf(f, ");\n");
548                         return;
549                 }
550         }
551         else if(prop->type == PROP_FLOAT) {
552                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
553
554                 if(fprop->hardmin != -FLT_MAX || fprop->hardmax != FLT_MAX) {
555                         if(array) fprintf(f, "CLAMPIS(values[i], ");
556                         else fprintf(f, "CLAMPIS(value, ");
557                         rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
558                         rna_float_print(f, fprop->hardmax); fprintf(f, ");\n");
559                         return;
560                 }
561         }
562
563         if(array)
564                 fprintf(f, "values[i];\n");
565         else
566                 fprintf(f, "value;\n");
567 }
568
569 static char *rna_def_property_set_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
570 {
571         char *func;
572
573         if(!(prop->flag & PROP_EDITABLE))
574                 return NULL;
575         if(prop->flag & PROP_IDPROPERTY)
576                 return NULL;
577
578         if(!manualfunc) {
579                 if(!dp->dnastructname || !dp->dnaname) {
580                         if(prop->flag & PROP_EDITABLE) {
581                                 fprintf(stderr, "rna_def_property_set_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
582                                 DefRNA.error= 1;
583                         }
584                         return NULL;
585                 }
586         }
587
588         func= rna_alloc_function_name(srna->identifier, prop->identifier, "set");
589
590         switch(prop->type) {
591                 case PROP_STRING: {
592                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
593                         fprintf(f, "void %s(PointerRNA *ptr, const char *value)\n", func);
594                         fprintf(f, "{\n");
595                         if(manualfunc) {
596                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
597                         }
598                         else {
599                                 rna_print_data_get(f, dp);
600                                 if(sprop->maxlength)
601                                         fprintf(f, "    BLI_strncpy(data->%s, value, %d);\n", dp->dnaname, sprop->maxlength);
602                                 else
603                                         fprintf(f, "    BLI_strncpy(data->%s, value, sizeof(data->%s));\n", dp->dnaname, dp->dnaname);
604                         }
605                         fprintf(f, "}\n\n");
606                         break;
607                 }
608                 case PROP_POINTER: {
609                         fprintf(f, "void %s(PointerRNA *ptr, PointerRNA value)\n", func);
610                         fprintf(f, "{\n");
611                         if(manualfunc) {
612                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
613                         }
614                         else {
615                                 rna_print_data_get(f, dp);
616
617                                 if(prop->flag & PROP_ID_SELF_CHECK) {
618                                         rna_print_id_get(f, dp);
619                                         fprintf(f, "    if(id==value.data) return;\n\n");
620                                 }
621
622                                 if(prop->flag & PROP_ID_REFCOUNT) {
623                                         fprintf(f, "\n  if(data->%s)\n", dp->dnaname);
624                                         fprintf(f, "            id_us_min((ID*)data->%s);\n", dp->dnaname);
625                                         fprintf(f, "    if(value.data)\n");
626                                         fprintf(f, "            id_us_plus((ID*)value.data);\n\n");
627                                 }
628
629                                 fprintf(f, "    data->%s= value.data;\n", dp->dnaname);
630
631                         }
632                         fprintf(f, "}\n\n");
633                         break;
634                 }
635                 default:
636                         if(prop->arraydimension) {
637                                 if(prop->flag & PROP_DYNAMIC)
638                                         fprintf(f, "void %s(PointerRNA *ptr, const %s values[])\n", func, rna_type_type(prop));
639                                 else
640                                         fprintf(f, "void %s(PointerRNA *ptr, const %s values[%d])\n", func, rna_type_type(prop), prop->totarraylength);
641                                 fprintf(f, "{\n");
642
643                                 if(manualfunc) {
644                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
645                                 }
646                                 else {
647                                         rna_print_data_get(f, dp);
648
649                                         if(prop->flag & PROP_DYNAMIC) {
650                                                 char *lenfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "set_length");
651                                                 fprintf(f, "    int i, arraylen[RNA_MAX_ARRAY_DIMENSION];\n");
652                                                 fprintf(f, "    int len= %s(ptr, arraylen);\n\n", lenfunc);
653                                                 fprintf(f, "    for(i=0; i<len; i++) {\n");
654                                                 MEM_freeN(lenfunc);
655                                         }
656                                         else {
657                                                 fprintf(f, "    int i;\n\n");
658                                                 fprintf(f, "    for(i=0; i<%d; i++) {\n", prop->totarraylength);
659                                         }
660
661                                         if(dp->dnaarraylength == 1) {
662                                                 if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
663                                                         fprintf(f, "            if(%svalues[i]) data->%s |= (%d<<i);\n", (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit);
664                                                         fprintf(f, "            else data->%s &= ~(%d<<i);\n", dp->dnaname, dp->booleanbit);
665                                                 }
666                                                 else {
667                                                         fprintf(f, "            (&data->%s)[i]= %s", dp->dnaname, (dp->booleannegative)? "!": "");
668                                                         rna_clamp_value(f, prop, 1);
669                                                 }
670                                         }
671                                         else {
672                                                 if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
673                                                         fprintf(f, "            if(%svalues[i]) data->%s[i] |= ", (dp->booleannegative)? "!": "", dp->dnaname);
674                                                         rna_int_print(f, dp->booleanbit);
675                                                         fprintf(f, ";\n");
676                                                         fprintf(f, "            else data->%s[i] &= ~", dp->dnaname);
677                                                         rna_int_print(f, dp->booleanbit);
678                                                         fprintf(f, ";\n");
679                                                 }
680                                                 else if(rna_color_quantize(prop, dp)) {
681                                                         fprintf(f, "            data->%s[i]= FTOCHAR(values[i]);\n", dp->dnaname);
682                                                 }
683                                                 else {
684                                                         if(dp->dnatype)
685                                                                 fprintf(f, "            ((%s*)data->%s)[i]= %s", dp->dnatype, dp->dnaname, (dp->booleannegative)? "!": "");
686                                                         else
687                                                                 fprintf(f, "            (data->%s)[i]= %s", dp->dnaname, (dp->booleannegative)? "!": "");
688                                                         rna_clamp_value(f, prop, 1);
689                                                 }
690                                         }
691                                         fprintf(f, "    }\n");
692                                 }
693                                 fprintf(f, "}\n\n");
694                         }
695                         else {
696                                 fprintf(f, "void %s(PointerRNA *ptr, %s value)\n", func, rna_type_type(prop));
697                                 fprintf(f, "{\n");
698
699                                 if(manualfunc) {
700                                         fprintf(f, "    %s(ptr, value);\n", manualfunc);
701                                 }
702                                 else {
703                                         rna_print_data_get(f, dp);
704                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
705                                                 fprintf(f, "    if(%svalue) data->%s |= ", (dp->booleannegative)? "!": "", dp->dnaname);
706                                                 rna_int_print(f, dp->booleanbit);
707                                                 fprintf(f, ";\n");
708                                                 fprintf(f, "    else data->%s &= ~", dp->dnaname);
709                                                 rna_int_print(f, dp->booleanbit);
710                                                 fprintf(f, ";\n");
711                                         }
712                                         else if(prop->type == PROP_ENUM && dp->enumbitflags) {
713                                                 fprintf(f, "    data->%s &= ~", dp->dnaname);
714                                                 rna_int_print(f, rna_enum_bitmask(prop));
715                                                 fprintf(f, ";\n");
716                                                 fprintf(f, "    data->%s |= value;\n", dp->dnaname);
717                                         }
718                                         else {
719                                                 fprintf(f, "    data->%s= %s", dp->dnaname, (dp->booleannegative)? "!": "");
720                                                 rna_clamp_value(f, prop, 0);
721                                         }
722                                 }
723                                 fprintf(f, "}\n\n");
724                         }
725                         break;
726         }
727
728         return func;
729 }
730
731 static char *rna_def_property_length_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
732 {
733         char *func= NULL;
734
735         if(prop->flag & PROP_IDPROPERTY)
736                 return NULL;
737
738         if(prop->type == PROP_STRING) {
739                 if(!manualfunc) {
740                         if(!dp->dnastructname || !dp->dnaname) {
741                                 fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
742                                 DefRNA.error= 1;
743                                 return NULL;
744                         }
745                 }
746
747                 func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");
748
749                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
750                 fprintf(f, "{\n");
751                 if(manualfunc) {
752                         fprintf(f, "    return %s(ptr);\n", manualfunc);
753                 }
754                 else {
755                         rna_print_data_get(f, dp);
756                         fprintf(f, "    return strlen(data->%s);\n", dp->dnaname);
757                 }
758                 fprintf(f, "}\n\n");
759         }
760         else if(prop->type == PROP_COLLECTION) {
761                 if(!manualfunc) {
762                         if(prop->type == PROP_COLLECTION && (!(dp->dnalengthname || dp->dnalengthfixed)|| !dp->dnaname)) {
763                                 fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
764                                 DefRNA.error= 1;
765                                 return NULL;
766                         }
767                 }
768
769                 func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");
770
771                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
772                 fprintf(f, "{\n");
773                 if(manualfunc) {
774                         fprintf(f, "    return %s(ptr);\n", manualfunc);
775                 }
776                 else {
777                         rna_print_data_get(f, dp);
778                         if(dp->dnalengthname)
779                                 fprintf(f, "    return (data->%s == NULL)? 0: data->%s;\n", dp->dnaname, dp->dnalengthname);
780                         else
781                                 fprintf(f, "    return (data->%s == NULL)? 0: %d;\n", dp->dnaname, dp->dnalengthfixed);
782                 }
783                 fprintf(f, "}\n\n");
784         }
785
786         return func;
787 }
788
789 static char *rna_def_property_begin_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
790 {
791         char *func, *getfunc;
792
793         if(prop->flag & PROP_IDPROPERTY)
794                 return NULL;
795
796         if(!manualfunc) {
797                 if(!dp->dnastructname || !dp->dnaname) {
798                         fprintf(stderr, "rna_def_property_begin_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
799                         DefRNA.error= 1;
800                         return NULL;
801                 }
802         }
803
804         func= rna_alloc_function_name(srna->identifier, prop->identifier, "begin");
805
806         fprintf(f, "void %s(CollectionPropertyIterator *iter, PointerRNA *ptr)\n", func);
807         fprintf(f, "{\n");
808
809         if(!manualfunc)
810                 rna_print_data_get(f, dp);
811
812         fprintf(f, "\n  memset(iter, 0, sizeof(*iter));\n");
813         fprintf(f, "    iter->parent= *ptr;\n");
814         fprintf(f, "    iter->prop= (PropertyRNA*)&rna_%s_%s;\n", srna->identifier, prop->identifier);
815
816         if(dp->dnalengthname || dp->dnalengthfixed) {
817                 if(manualfunc) {
818                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
819                 }
820                 else {
821                         if(dp->dnalengthname)
822                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), data->%s, 0, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthname);
823                         else
824                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), %d, 0, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthfixed);
825                 }
826         }
827         else {
828                 if(manualfunc)
829                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
830                 else if(dp->dnapointerlevel == 0)
831                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, &data->%s, NULL);\n", dp->dnaname);
832                 else
833                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, data->%s, NULL);\n", dp->dnaname);
834         }
835
836         getfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
837
838         fprintf(f, "\n  if(iter->valid)\n");
839         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
840
841         fprintf(f, "}\n\n");
842
843
844         return func;
845 }
846
847 static char *rna_def_property_lookup_int_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc, char *nextfunc)
848 {
849         char *func;
850
851         if(prop->flag & PROP_IDPROPERTY)
852                 return NULL;
853
854         if(!manualfunc) {
855                 if(!dp->dnastructname || !dp->dnaname)
856                         return NULL;
857
858                 /* only supported in case of standard next functions */
859                 if(strcmp(nextfunc, "rna_iterator_array_next") == 0);
860                 else if(strcmp(nextfunc, "rna_iterator_listbase_next") == 0);
861                 else return NULL;
862         }
863
864         func= rna_alloc_function_name(srna->identifier, prop->identifier, "lookup_int");
865
866         fprintf(f, "PointerRNA %s(PointerRNA *ptr, int index)\n", func);
867         fprintf(f, "{\n");
868
869         if(manualfunc) {
870                 fprintf(f, "\n  return %s(ptr, index);\n", manualfunc);
871                 fprintf(f, "}\n\n");
872                 return func;
873         }
874
875         fprintf(f, "    PointerRNA r_ptr;\n");
876         fprintf(f, "    CollectionPropertyIterator iter;\n\n");
877
878         fprintf(f, "    %s_%s_begin(&iter, ptr);\n\n", srna->identifier, prop->identifier);
879         fprintf(f, "    {\n");
880
881         if(strcmp(nextfunc, "rna_iterator_array_next") == 0) {
882                 fprintf(f, "            ArrayIterator *internal= iter.internal;\n");
883                 fprintf(f, "            if(internal->skip) {\n");
884                 fprintf(f, "                    while(index-- > 0) {\n");
885                 fprintf(f, "                            do {\n");
886                 fprintf(f, "                                    internal->ptr += internal->itemsize;\n");
887                 fprintf(f, "                            } while(internal->skip(&iter, internal->ptr));\n");
888                 fprintf(f, "                    }\n");
889                 fprintf(f, "            }\n");
890                 fprintf(f, "            else {\n");
891                 fprintf(f, "                    internal->ptr += internal->itemsize*index;\n");
892                 fprintf(f, "            }\n");
893         }
894         else if(strcmp(nextfunc, "rna_iterator_listbase_next") == 0) {
895                 fprintf(f, "            ListBaseIterator *internal= iter.internal;\n");
896                 fprintf(f, "            if(internal->skip) {\n");
897                 fprintf(f, "                    while(index-- > 0) {\n");
898                 fprintf(f, "                            do {\n");
899                 fprintf(f, "                                    internal->link= internal->link->next;\n");
900                 fprintf(f, "                            } while(internal->skip(&iter, internal->link));\n");
901                 fprintf(f, "                    }\n");
902                 fprintf(f, "            }\n");
903                 fprintf(f, "            else {\n");
904                 fprintf(f, "                    while(index-- > 0)\n");
905                 fprintf(f, "                            internal->link= internal->link->next;\n");
906                 fprintf(f, "            }\n");
907         }
908
909         fprintf(f, "    }\n\n");
910
911         fprintf(f, "    r_ptr = %s_%s_get(&iter);\n", srna->identifier, prop->identifier);
912         fprintf(f, "    %s_%s_end(&iter);\n\n", srna->identifier, prop->identifier);
913
914         fprintf(f, "    return r_ptr;\n");
915
916 #if 0
917         rna_print_data_get(f, dp);
918         item_type= (cprop->item_type)? (char*)cprop->item_type: "UnknownType";
919
920         if(dp->dnalengthname || dp->dnalengthfixed) {
921                 if(dp->dnalengthname)
922                         fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), data->%s, index);\n", item_type, dp->dnaname, dp->dnaname, dp->dnalengthname);
923                 else
924                         fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), %d, index);\n", item_type, dp->dnaname, dp->dnaname, dp->dnalengthfixed);
925         }
926         else {
927                 if(dp->dnapointerlevel == 0)
928                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, &data->%s, index);\n", item_type, dp->dnaname);
929                 else
930                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, data->%s, index);\n", item_type, dp->dnaname);
931         }
932 #endif
933
934         fprintf(f, "}\n\n");
935
936         return func;
937 }
938
939 static char *rna_def_property_next_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
940 {
941         char *func, *getfunc;
942
943         if(prop->flag & PROP_IDPROPERTY)
944                 return NULL;
945
946         if(!manualfunc)
947                 return NULL;
948
949         func= rna_alloc_function_name(srna->identifier, prop->identifier, "next");
950
951         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
952         fprintf(f, "{\n");
953         fprintf(f, "    %s(iter);\n", manualfunc);
954
955         getfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
956
957         fprintf(f, "\n  if(iter->valid)\n");
958         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
959
960         fprintf(f, "}\n\n");
961
962         return func;
963 }
964
965 static char *rna_def_property_end_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
966 {
967         char *func;
968
969         if(prop->flag & PROP_IDPROPERTY)
970                 return NULL;
971
972         func= rna_alloc_function_name(srna->identifier, prop->identifier, "end");
973
974         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
975         fprintf(f, "{\n");
976         if(manualfunc)
977                 fprintf(f, "    %s(iter);\n", manualfunc);
978         fprintf(f, "}\n\n");
979
980         return func;
981 }
982
983 static void rna_set_raw_property(PropertyDefRNA *dp, PropertyRNA *prop)
984 {
985         if(dp->dnapointerlevel != 0)
986                 return;
987         if(!dp->dnatype || !dp->dnaname || !dp->dnastructname)
988                 return;
989         
990         if(strcmp(dp->dnatype, "char") == 0) {
991                 prop->rawtype= PROP_RAW_CHAR;
992                 prop->flag |= PROP_RAW_ACCESS;
993         }
994         else if(strcmp(dp->dnatype, "short") == 0) {
995                 prop->rawtype= PROP_RAW_SHORT;
996                 prop->flag |= PROP_RAW_ACCESS;
997         }
998         else if(strcmp(dp->dnatype, "int") == 0) {
999                 prop->rawtype= PROP_RAW_INT;
1000                 prop->flag |= PROP_RAW_ACCESS;
1001         }
1002         else if(strcmp(dp->dnatype, "float") == 0) {
1003                 prop->rawtype= PROP_RAW_FLOAT;
1004                 prop->flag |= PROP_RAW_ACCESS;
1005         }
1006         else if(strcmp(dp->dnatype, "double") == 0) {
1007                 prop->rawtype= PROP_RAW_DOUBLE;
1008                 prop->flag |= PROP_RAW_ACCESS;
1009         }
1010 }
1011
1012 static void rna_set_raw_offset(FILE *f, StructRNA *srna, PropertyRNA *prop)
1013 {
1014         PropertyDefRNA *dp= rna_find_struct_property_def(srna, prop);
1015
1016         fprintf(f, "\toffsetof(%s, %s), %d", dp->dnastructname, dp->dnaname, prop->rawtype);
1017 }
1018
1019 static void rna_def_property_funcs(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1020 {
1021         PropertyRNA *prop;
1022
1023         prop= dp->prop;
1024
1025         switch(prop->type) {
1026                 case PROP_BOOLEAN: {
1027                         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1028
1029                         if(!prop->arraydimension) {
1030                                 if(!bprop->get && !bprop->set && !dp->booleanbit)
1031                                         rna_set_raw_property(dp, prop);
1032
1033                                 bprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->get);
1034                                 bprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->set);
1035                         }
1036                         else {
1037                                 bprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->getarray);
1038                                 bprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->setarray);
1039                         }
1040                         break;
1041                 }
1042                 case PROP_INT: {
1043                         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1044
1045                         if(!prop->arraydimension) {
1046                                 if(!iprop->get && !iprop->set)
1047                                         rna_set_raw_property(dp, prop);
1048
1049                                 iprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->get);
1050                                 iprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->set);
1051                         }
1052                         else {
1053                                 if(!iprop->getarray && !iprop->setarray)
1054                                         rna_set_raw_property(dp, prop);
1055
1056                                 iprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->getarray);
1057                                 iprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->setarray);
1058                         }
1059                         break;
1060                 }
1061                 case PROP_FLOAT: {
1062                         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1063
1064                         if(!prop->arraydimension) {
1065                                 if(!fprop->get && !fprop->set)
1066                                         rna_set_raw_property(dp, prop);
1067
1068                                 fprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->get);
1069                                 fprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->set);
1070                         }
1071                         else {
1072                                 if(!fprop->getarray && !fprop->setarray)
1073                                         rna_set_raw_property(dp, prop);
1074
1075                                 fprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->getarray);
1076                                 fprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->setarray);
1077                         }
1078                         break;
1079                 }
1080                 case PROP_ENUM: {
1081                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1082
1083                         eprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)eprop->get);
1084                         eprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)eprop->set);
1085                         break;
1086                 }
1087                 case PROP_STRING: {
1088                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1089
1090                         sprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)sprop->get);
1091                         sprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)sprop->length);
1092                         sprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)sprop->set);
1093                         break;
1094                 }
1095                 case PROP_POINTER: {
1096                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
1097
1098                         pprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)pprop->get);
1099                         pprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)pprop->set);
1100                         if(!pprop->type) {
1101                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, pointer must have a struct type.\n", srna->identifier, prop->identifier);
1102                                 DefRNA.error= 1;
1103                         }
1104                         break;
1105                 }
1106                 case PROP_COLLECTION: {
1107                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1108                         char *nextfunc= (char*)cprop->next;
1109
1110                         if(dp->dnatype && strcmp(dp->dnatype, "ListBase")==0);
1111                         else if(dp->dnalengthname || dp->dnalengthfixed)
1112                                 cprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)cprop->length);
1113
1114                         /* test if we can allow raw array access, if it is using our standard
1115                          * array get/next function, we can be sure it is an actual array */
1116                         if(cprop->next && cprop->get)
1117                                 if(strcmp((char*)cprop->next, "rna_iterator_array_next") == 0 &&
1118                                    strcmp((char*)cprop->get, "rna_iterator_array_get") == 0)
1119                                         prop->flag |= PROP_RAW_ARRAY;
1120
1121                         cprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)cprop->get);
1122                         cprop->begin= (void*)rna_def_property_begin_func(f, srna, prop, dp, (char*)cprop->begin);
1123                         cprop->next= (void*)rna_def_property_next_func(f, srna, prop, dp, (char*)cprop->next);
1124                         cprop->end= (void*)rna_def_property_end_func(f, srna, prop, dp, (char*)cprop->end);
1125                         cprop->lookupint= (void*)rna_def_property_lookup_int_func(f, srna, prop, dp, (char*)cprop->lookupint, nextfunc);
1126
1127                         if(!(prop->flag & PROP_IDPROPERTY)) {
1128                                 if(!cprop->begin) {
1129                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a begin function.\n", srna->identifier, prop->identifier);
1130                                         DefRNA.error= 1;
1131                                 }
1132                                 if(!cprop->next) {
1133                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a next function.\n", srna->identifier, prop->identifier);
1134                                         DefRNA.error= 1;
1135                                 }
1136                                 if(!cprop->get) {
1137                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a get function.\n", srna->identifier, prop->identifier);
1138                                         DefRNA.error= 1;
1139                                 }
1140                         }
1141                         if(!cprop->item_type) {
1142                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a struct type.\n", srna->identifier, prop->identifier);
1143                                 DefRNA.error= 1;
1144                         }
1145                         break;
1146                 }
1147         }
1148 }
1149
1150 static void rna_def_property_funcs_header(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1151 {
1152         PropertyRNA *prop;
1153         char *func;
1154
1155         prop= dp->prop;
1156
1157         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1158                 return;
1159
1160         func= rna_alloc_function_name(srna->identifier, prop->identifier, "");
1161
1162         switch(prop->type) {
1163                 case PROP_BOOLEAN:
1164                 case PROP_INT: {
1165                         if(!prop->arraydimension) {
1166                                 fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1167                                 //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
1168                         }
1169                         else {
1170                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[%d]);\n", func, prop->totarraylength);
1171                                 //fprintf(f, "void %sset(PointerRNA *ptr, const int values[%d]);\n", func, prop->arraylength);
1172                         }
1173                         break;
1174                 }
1175                 case PROP_FLOAT: {
1176                         if(!prop->arraydimension) {
1177                                 fprintf(f, "float %sget(PointerRNA *ptr);\n", func);
1178                                 //fprintf(f, "void %sset(PointerRNA *ptr, float value);\n", func);
1179                         }
1180                         else {
1181                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[%d]);\n", func, prop->totarraylength);
1182                                 //fprintf(f, "void %sset(PointerRNA *ptr, const float values[%d]);\n", func, prop->arraylength);
1183                         }
1184                         break;
1185                 }
1186                 case PROP_ENUM: {
1187                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1188                         int i;
1189
1190                         if(eprop->item) {
1191                                 fprintf(f, "enum {\n");
1192
1193                                 for(i=0; i<eprop->totitem; i++)
1194                                         if(eprop->item[i].identifier[0])
1195                                                 fprintf(f, "\t%s_%s_%s = %d,\n", srna->identifier, prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
1196
1197                                 fprintf(f, "};\n\n");
1198                         }
1199
1200                         fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1201                         //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
1202
1203                         break;
1204                 }
1205                 case PROP_STRING: {
1206                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1207
1208                         if(sprop->maxlength) {
1209                                 fprintf(f, "#define %s_%s_MAX %d\n\n", srna->identifier, prop->identifier, sprop->maxlength);
1210                         }
1211                         
1212                         fprintf(f, "void %sget(PointerRNA *ptr, char *value);\n", func);
1213                         fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
1214                         //fprintf(f, "void %sset(PointerRNA *ptr, const char *value);\n", func);
1215
1216                         break;
1217                 }
1218                 case PROP_POINTER: {
1219                         fprintf(f, "PointerRNA %sget(PointerRNA *ptr);\n", func);
1220                         //fprintf(f, "void %sset(PointerRNA *ptr, PointerRNA value);\n", func);
1221                         break;
1222                 }
1223                 case PROP_COLLECTION: {
1224                         fprintf(f, "void %sbegin(CollectionPropertyIterator *iter, PointerRNA *ptr);\n", func);
1225                         fprintf(f, "void %snext(CollectionPropertyIterator *iter);\n", func);
1226                         fprintf(f, "void %send(CollectionPropertyIterator *iter);\n", func);
1227                         //fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
1228                         //fprintf(f, "void %slookup_int(PointerRNA *ptr, int key, StructRNA **type);\n", func);
1229                         //fprintf(f, "void %slookup_string(PointerRNA *ptr, const char *key, StructRNA **type);\n", func);
1230                         break;
1231                 }
1232         }
1233
1234         fprintf(f, "\n");
1235 }
1236
1237 static void rna_def_property_funcs_header_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1238 {
1239         PropertyRNA *prop;
1240
1241         prop= dp->prop;
1242
1243         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1244                 return;
1245         
1246         if(prop->name && prop->description && strcmp(prop->description, "") != 0)
1247                 fprintf(f, "\t/* %s: %s */\n", prop->name, prop->description);
1248         else if(prop->name)
1249                 fprintf(f, "\t/* %s */\n", prop->name);
1250         else
1251                 fprintf(f, "\t/* */\n");
1252
1253         switch(prop->type) {
1254                 case PROP_BOOLEAN: {
1255                         if(!prop->arraydimension)
1256                                 fprintf(f, "\tbool %s(void);", prop->identifier);
1257                         else
1258                                 fprintf(f, "\tArray<int, %d> %s(void);", prop->totarraylength, prop->identifier);
1259                         break;
1260                 }
1261                 case PROP_INT: {
1262                         if(!prop->arraydimension)
1263                                 fprintf(f, "\tint %s(void);", prop->identifier);
1264                         else
1265                                 fprintf(f, "\tArray<int, %d> %s(void);", prop->totarraylength, prop->identifier);
1266                         break;
1267                 }
1268                 case PROP_FLOAT: {
1269                         if(!prop->arraydimension)
1270                                 fprintf(f, "\tfloat %s(void);", prop->identifier);
1271                         else
1272                                 fprintf(f, "\tArray<float, %d> %s(void);", prop->totarraylength, prop->identifier);
1273                         break;
1274                 }
1275                 case PROP_ENUM: {
1276                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1277                         int i;
1278
1279                         if(eprop->item) {
1280                                 fprintf(f, "\tenum %s_enum {\n", prop->identifier);
1281
1282                                 for(i=0; i<eprop->totitem; i++)
1283                                         if(eprop->item[i].identifier[0])
1284                                                 fprintf(f, "\t\t%s_%s = %d,\n", prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
1285
1286                                 fprintf(f, "\t};\n");
1287                         }
1288
1289                         fprintf(f, "\t%s_enum %s(void);", prop->identifier, prop->identifier);
1290                         break;
1291                 }
1292                 case PROP_STRING: {
1293                         fprintf(f, "\tstd::string %s(void);", prop->identifier);
1294                         break;
1295                 }
1296                 case PROP_POINTER: {
1297                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1298
1299                         if(pprop->type)
1300                                 fprintf(f, "\t%s %s(void);", (char*)pprop->type, prop->identifier);
1301                         else
1302                                 fprintf(f, "\t%s %s(void);", "UnknownType", prop->identifier);
1303                         break;
1304                 }
1305                 case PROP_COLLECTION: {
1306                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1307
1308                         if(cprop->item_type)
1309                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (char*)cprop->item_type, srna->identifier, prop->identifier);
1310                         else
1311                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1312                         break;
1313                 }
1314         }
1315
1316         fprintf(f, "\n");
1317 }
1318
1319 static void rna_def_property_funcs_impl_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1320 {
1321         PropertyRNA *prop;
1322
1323         prop= dp->prop;
1324
1325         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1326                 return;
1327
1328         switch(prop->type) {
1329                 case PROP_BOOLEAN: {
1330                         if(!prop->arraydimension)
1331                                 fprintf(f, "\tBOOLEAN_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1332                         else
1333                                 fprintf(f, "\tBOOLEAN_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->totarraylength, prop->identifier);
1334                         break;
1335                 }
1336                 case PROP_INT: {
1337                         if(!prop->arraydimension)
1338                                 fprintf(f, "\tINT_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1339                         else
1340                                 fprintf(f, "\tINT_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->totarraylength, prop->identifier);
1341                         break;
1342                 }
1343                 case PROP_FLOAT: {
1344                         if(!prop->arraydimension)
1345                                 fprintf(f, "\tFLOAT_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1346                         else
1347                                 fprintf(f, "\tFLOAT_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->totarraylength, prop->identifier);
1348                         break;
1349                 }
1350                 case PROP_ENUM: {
1351                         fprintf(f, "\tENUM_PROPERTY(%s_enum, %s, %s)", prop->identifier, srna->identifier, prop->identifier);
1352
1353                         break;
1354                 }
1355                 case PROP_STRING: {
1356                         fprintf(f, "\tSTRING_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1357                         break;
1358                 }
1359                 case PROP_POINTER: {
1360                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1361
1362                         if(pprop->type)
1363                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", (char*)pprop->type, srna->identifier, prop->identifier);
1364                         else
1365                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1366                         break;
1367                 }
1368                 case PROP_COLLECTION: {
1369                         /*CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1370
1371                         if(cprop->type)
1372                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (char*)cprop->type, srna->identifier, prop->identifier);
1373                         else
1374                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);*/
1375                         break;
1376                 }
1377         }
1378
1379         fprintf(f, "\n");
1380 }
1381
1382 static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA *dfunc)
1383 {
1384         StructRNA *srna;
1385         FunctionRNA *func;
1386         PropertyDefRNA *dparm;
1387         PropertyType type;
1388         char *funcname, *ptrstr, *valstr;
1389         int flag, pout, cptr, first;
1390
1391         srna= dsrna->srna;
1392         func= dfunc->func;
1393
1394         if(!dfunc->call)
1395                 return;
1396
1397         funcname= rna_alloc_function_name(srna->identifier, func->identifier, "call");
1398
1399         /* function definition */
1400         fprintf(f, "void %s(bContext *C, ReportList *reports, PointerRNA *_ptr, ParameterList *_parms)", funcname);
1401         fprintf(f, "\n{\n");
1402
1403         /* variable definitions */
1404         if((func->flag & FUNC_NO_SELF)==0) {
1405                 if(func->flag & FUNC_USE_SELF_ID)
1406                         fprintf(f, "\tstruct ID *_selfid;\n");
1407
1408                 if(dsrna->dnaname) fprintf(f, "\tstruct %s *_self;\n", dsrna->dnaname);
1409                 else fprintf(f, "\tstruct %s *_self;\n", srna->identifier);
1410         }
1411
1412         dparm= dfunc->cont.properties.first;
1413         for(; dparm; dparm= dparm->next) {
1414                 type = dparm->prop->type;
1415                 flag = dparm->prop->flag;
1416                 pout = (flag & PROP_OUTPUT);
1417                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1418
1419                 if(dparm->prop==func->c_ret)
1420                         ptrstr= cptr || dparm->prop->arraydimension ? "*" : "";
1421                 /* XXX only arrays and strings are allowed to be dynamic, is this checked anywhere? */
1422                 else if (cptr || (flag & PROP_DYNAMIC))
1423                         ptrstr= pout ? "**" : "*";
1424                 /* fixed size arrays and RNA pointers are pre-allocated on the ParameterList stack, pass a pointer to it */
1425                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
1426                         ptrstr= "*";
1427                 /* PROP_THICK_WRAP strings are pre-allocated on the ParameterList stack, but type name for string props is already char*, so leave empty */
1428                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
1429                         ptrstr= "";
1430                 else
1431                         ptrstr= pout ? "*" : "";
1432
1433                 fprintf(f, "\t%s%s %s%s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
1434
1435                 /* for dynamic parameters we pass an additional int for the length of the parameter */
1436                 if (flag & PROP_DYNAMIC)
1437                         fprintf(f, "\tint %s%s_len;\n", pout ? "*" : "", dparm->prop->identifier);
1438         }
1439
1440         fprintf(f, "\tchar *_data");
1441         if(func->c_ret) fprintf(f, ", *_retdata");
1442         fprintf(f, ";\n");
1443         fprintf(f, "\t\n");
1444
1445         /* assign self */
1446         if((func->flag & FUNC_NO_SELF)==0) {
1447                 if(func->flag & FUNC_USE_SELF_ID)
1448                         fprintf(f, "\t_selfid= (struct ID*)_ptr->id.data;\n");
1449
1450                 if(dsrna->dnaname) fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", dsrna->dnaname);
1451                 else fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", srna->identifier);
1452         }
1453
1454         fprintf(f, "\t_data= (char *)_parms->data;\n");
1455
1456         dparm= dfunc->cont.properties.first;
1457         for(; dparm; dparm= dparm->next) {
1458                 type = dparm->prop->type;
1459                 flag = dparm->prop->flag;
1460                 pout = (flag & PROP_OUTPUT);
1461                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1462
1463                 if(dparm->prop==func->c_ret)
1464                         fprintf(f, "\t_retdata= _data;\n");
1465                 else  {
1466                         if (cptr || (flag & PROP_DYNAMIC)) {
1467                                 ptrstr= "**";
1468                                 valstr= "*";
1469                         }
1470                         else if (type == PROP_POINTER || dparm->prop->arraydimension) {
1471                                 ptrstr= "*";
1472                                 valstr= "";
1473                         }
1474                         else if (type == PROP_STRING && (flag & PROP_THICK_WRAP)) {
1475                                 ptrstr= "";
1476                                 valstr= "";
1477                         }
1478                         else {
1479                                 ptrstr= "*";
1480                                 valstr= "*";
1481                         }
1482
1483                         fprintf(f, "\t%s= ", dparm->prop->identifier);
1484
1485                         if (!pout)
1486                                 fprintf(f, "%s", valstr);
1487
1488                         fprintf(f, "((%s%s%s)_data);\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr);
1489
1490                         /* this must be kept in sync with RNA_parameter_length_get_data, we could just call the function directly, but this is faster */
1491                         if (flag & PROP_DYNAMIC)
1492                                 fprintf(f, "\t%s_len= %s((int *)(_data+%d));\n", dparm->prop->identifier, pout ? "" : "*", rna_parameter_size(dparm->prop));
1493                 }
1494
1495                 if(dparm->next)
1496                         fprintf(f, "\t_data+= %d;\n", rna_parameter_size_alloc(dparm->prop));
1497         }
1498
1499         if(dfunc->call) {
1500                 fprintf(f, "\t\n");
1501                 fprintf(f, "\t");
1502                 if(func->c_ret) fprintf(f, "%s= ", func->c_ret->identifier);
1503                 fprintf(f, "%s(", dfunc->call);
1504
1505                 first= 1;
1506
1507                 if((func->flag & FUNC_NO_SELF)==0) {
1508                         if(func->flag & FUNC_USE_SELF_ID)
1509                                 fprintf(f, "_selfid, ");
1510
1511                         fprintf(f, "_self");
1512                         first= 0;
1513                 }
1514
1515                 if(func->flag & FUNC_USE_CONTEXT) {
1516                         if(!first) fprintf(f, ", ");
1517                         first= 0;
1518                         fprintf(f, "C");
1519                 }
1520
1521                 if(func->flag & FUNC_USE_REPORTS) {
1522                         if(!first) fprintf(f, ", ");
1523                         first= 0;
1524                         fprintf(f, "reports");
1525                 }
1526
1527                 dparm= dfunc->cont.properties.first;
1528                 for(; dparm; dparm= dparm->next) {
1529                         if(dparm->prop==func->c_ret)
1530                                 continue;
1531
1532                         if(!first) fprintf(f, ", ");
1533                         first= 0;
1534
1535                         fprintf(f, "%s", dparm->prop->identifier);
1536
1537                         if (dparm->prop->flag & PROP_DYNAMIC)
1538                                 fprintf(f, ", %s_len", dparm->prop->identifier);
1539                 }
1540
1541                 fprintf(f, ");\n");
1542
1543                 if(func->c_ret) {
1544                         dparm= rna_find_parameter_def(func->c_ret);
1545                         ptrstr= (((dparm->prop->type == PROP_POINTER) && !(dparm->prop->flag & PROP_RNAPTR)) || (dparm->prop->arraydimension))? "*": "";
1546                         fprintf(f, "\t*((%s%s%s*)_retdata)= %s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, func->c_ret->identifier);
1547                 }
1548         }
1549
1550         fprintf(f, "}\n\n");
1551
1552         dfunc->gencall= funcname;
1553 }
1554
1555 static void rna_auto_types()
1556 {
1557         StructDefRNA *ds;
1558         PropertyDefRNA *dp;
1559
1560         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1561                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
1562                 if(ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
1563                         ds->dnaname= "bScreen";
1564
1565                 for(dp=ds->cont.properties.first; dp; dp=dp->next) {
1566                         if(dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
1567                                 dp->dnastructname= "bScreen";
1568
1569                         if(dp->dnatype) {
1570                                 if(dp->prop->type == PROP_POINTER) {
1571                                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1572                                         StructRNA *type;
1573
1574                                         if(!pprop->type && !pprop->get)
1575                                                 pprop->type= (StructRNA*)rna_find_type(dp->dnatype);
1576
1577                                         if(pprop->type) {
1578                                                 type= rna_find_struct((char*)pprop->type);
1579                                                 if(type && (type->flag & STRUCT_ID_REFCOUNT))
1580                                                         pprop->property.flag |= PROP_ID_REFCOUNT;
1581                                         }
1582                                 }
1583                                 else if(dp->prop->type== PROP_COLLECTION) {
1584                                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1585
1586                                         if(!cprop->item_type && !cprop->get && strcmp(dp->dnatype, "ListBase")==0)
1587                                                 cprop->item_type= (StructRNA*)rna_find_type(dp->dnatype);
1588                                 }
1589                         }
1590                 }
1591         }
1592 }
1593
1594 static void rna_sort(BlenderRNA *brna)
1595 {
1596         StructDefRNA *ds;
1597         StructRNA *srna;
1598
1599         rna_sortlist(&brna->structs, cmp_struct);
1600         rna_sortlist(&DefRNA.structs, cmp_def_struct);
1601
1602         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1603                 rna_sortlist(&srna->cont.properties, cmp_property);
1604
1605         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1606                 rna_sortlist(&ds->cont.properties, cmp_def_property);
1607 }
1608
1609 static const char *rna_property_structname(PropertyType type)
1610 {
1611         switch(type) {
1612                 case PROP_BOOLEAN: return "BooleanPropertyRNA";
1613                 case PROP_INT: return "IntPropertyRNA";
1614                 case PROP_FLOAT: return "FloatPropertyRNA";
1615                 case PROP_STRING: return "StringPropertyRNA";
1616                 case PROP_ENUM: return "EnumPropertyRNA";
1617                 case PROP_POINTER: return "PointerPropertyRNA";
1618                 case PROP_COLLECTION: return "CollectionPropertyRNA";
1619                 default: return "UnknownPropertyRNA";
1620         }
1621 }
1622
1623 static const char *rna_property_typename(PropertyType type)
1624 {
1625         switch(type) {
1626                 case PROP_BOOLEAN: return "PROP_BOOLEAN";
1627                 case PROP_INT: return "PROP_INT";
1628                 case PROP_FLOAT: return "PROP_FLOAT";
1629                 case PROP_STRING: return "PROP_STRING";
1630                 case PROP_ENUM: return "PROP_ENUM";
1631                 case PROP_POINTER: return "PROP_POINTER";
1632                 case PROP_COLLECTION: return "PROP_COLLECTION";
1633                 default: return "PROP_UNKNOWN";
1634         }
1635 }
1636
1637 static const char *rna_property_subtypename(PropertyType type)
1638 {
1639         switch(type) {
1640                 case PROP_NONE: return "PROP_NONE";
1641                 case PROP_FILEPATH: return "PROP_FILEPATH";
1642                 case PROP_FILENAME: return "PROP_FILENAME";
1643                 case PROP_DIRPATH: return "PROP_DIRPATH";
1644                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
1645                 case PROP_PERCENTAGE: return "PROP_PERCENTAGE";
1646                 case PROP_FACTOR: return "PROP_FACTOR";
1647                 case PROP_ANGLE: return "PROP_ANGLE";
1648                 case PROP_TIME: return "PROP_TIME";
1649                 case PROP_DISTANCE: return "PROP_DISTANCE";
1650                 case PROP_COLOR: return "PROP_COLOR";
1651                 case PROP_TRANSLATION: return "PROP_TRANSLATION";
1652                 case PROP_DIRECTION: return "PROP_DIRECTION";
1653                 case PROP_MATRIX: return "PROP_MATRIX";
1654                 case PROP_EULER: return "PROP_EULER";
1655                 case PROP_QUATERNION: return "PROP_QUATERNION";
1656                 case PROP_AXISANGLE: return "PROP_AXISANGLE";
1657                 case PROP_VELOCITY: return "PROP_VELOCITY";
1658                 case PROP_ACCELERATION: return "PROP_ACCELERATION";
1659                 case PROP_XYZ: return "PROP_XYZ";
1660                 case PROP_COLOR_GAMMA: return "PROP_COLOR_GAMMA";
1661                 case PROP_LAYER: return "PROP_LAYER";
1662                 case PROP_LAYER_MEMBER: return "PROP_LAYER_MEMBER";
1663                 default: {
1664                         /* incase we dont have a type preset that includes the subtype */
1665                         if(RNA_SUBTYPE_UNIT(type)) {
1666                                 return rna_property_subtypename(type & ~RNA_SUBTYPE_UNIT(type));
1667                         }
1668                         else {
1669                                 return "PROP_SUBTYPE_UNKNOWN";
1670                         }
1671                 }
1672         }
1673 }
1674
1675 static const char *rna_property_subtype_unit(PropertyType type)
1676 {
1677         switch(RNA_SUBTYPE_UNIT(type)) {
1678                 case PROP_UNIT_NONE:            return "PROP_UNIT_NONE";
1679                 case PROP_UNIT_LENGTH:          return "PROP_UNIT_LENGTH";
1680                 case PROP_UNIT_AREA:            return "PROP_UNIT_AREA";
1681                 case PROP_UNIT_VOLUME:          return "PROP_UNIT_VOLUME";
1682                 case PROP_UNIT_MASS:            return "PROP_UNIT_MASS";
1683                 case PROP_UNIT_ROTATION:        return "PROP_UNIT_ROTATION";
1684                 case PROP_UNIT_TIME:            return "PROP_UNIT_TIME";
1685                 case PROP_UNIT_VELOCITY:        return "PROP_UNIT_VELOCITY";
1686                 case PROP_UNIT_ACCELERATION:return "PROP_UNIT_ACCELERATION";
1687                 default: return "PROP_UNKNOWN";
1688         }
1689 }
1690
1691 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
1692 {
1693         StructRNA *srna;
1694
1695         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1696                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1697         fprintf(f, "\n");
1698 }
1699
1700 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
1701 {
1702         StructRNA *srna;
1703
1704         fprintf(f, "BlenderRNA BLENDER_RNA = {");
1705
1706         srna= brna->structs.first;
1707         if(srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
1708         else fprintf(f, "{NULL, ");
1709
1710         srna= brna->structs.last;
1711         if(srna) fprintf(f, "&RNA_%s}", srna->identifier);
1712         else fprintf(f, "NULL}");
1713
1714         fprintf(f, "};\n\n");
1715 }
1716
1717 static void rna_generate_property_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1718 {
1719         PropertyRNA *prop;
1720         StructRNA *base;
1721
1722         base= srna->base;
1723         while (base) {
1724                 fprintf(f, "\n");
1725                 for(prop=base->cont.properties.first; prop; prop=prop->next)
1726                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type), base->identifier, prop->identifier);
1727                 base= base->base;
1728         }
1729
1730         if(srna->cont.properties.first)
1731                 fprintf(f, "\n");
1732
1733         for(prop=srna->cont.properties.first; prop; prop=prop->next)
1734                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, prop->identifier);
1735         fprintf(f, "\n");
1736 }
1737
1738 static void rna_generate_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionRNA *func, FILE *f)
1739 {
1740         PropertyRNA *parm;
1741
1742         for(parm= func->cont.properties.first; parm; parm= parm->next)
1743                 fprintf(f, "%s%s rna_%s_%s_%s;\n", "extern ", rna_property_structname(parm->type), srna->identifier, func->identifier, parm->identifier);
1744
1745         if(func->cont.properties.first)
1746                 fprintf(f, "\n");
1747 }
1748
1749 static void rna_generate_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1750 {
1751         FunctionRNA *func;
1752         StructRNA *base;
1753
1754         base= srna->base;
1755         while (base) {
1756                 for(func= base->functions.first; func; func= func->cont.next) {
1757                         fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", base->identifier, func->identifier);
1758                         rna_generate_parameter_prototypes(brna, base, func, f);
1759                 }
1760
1761                 if(base->functions.first)
1762                         fprintf(f, "\n");
1763
1764                 base= base->base;
1765         }
1766
1767         for(func= srna->functions.first; func; func= func->cont.next) {
1768                 fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", srna->identifier, func->identifier);
1769                 rna_generate_parameter_prototypes(brna, srna, func, f);
1770         }
1771
1772         if(srna->functions.first)
1773                 fprintf(f, "\n");
1774 }
1775
1776 static void rna_generate_static_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionDefRNA *dfunc, FILE *f)
1777 {
1778         FunctionRNA *func;
1779         PropertyDefRNA *dparm;
1780         StructDefRNA *dsrna;
1781         PropertyType type;
1782         int flag, pout, cptr, first;
1783         char *ptrstr;
1784
1785         dsrna= rna_find_struct_def(srna);
1786         func= dfunc->func;
1787
1788         /* return type */
1789         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1790                 if(dparm->prop==func->c_ret) {
1791                         if(dparm->prop->arraydimension)
1792                                 fprintf(f, "XXX no array return types yet"); /* XXX not supported */
1793                         else if(dparm->prop->type == PROP_POINTER && !(dparm->prop->flag & PROP_RNAPTR))
1794                                 fprintf(f, "%s%s *", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1795                         else
1796                                 fprintf(f, "%s%s ", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1797
1798                         break;
1799                 }
1800         }
1801
1802         /* void if nothing to return */
1803         if(!dparm)
1804                 fprintf(f, "void ");
1805
1806         /* function name */
1807         fprintf(f, "%s(", dfunc->call);
1808
1809         first= 1;
1810
1811         /* self, context and reports parameters */
1812         if((func->flag & FUNC_NO_SELF)==0) {
1813                 if(func->flag & FUNC_USE_SELF_ID)
1814                         fprintf(f, "struct ID *_selfid, ");
1815
1816                 if(dsrna->dnaname) fprintf(f, "struct %s *_self", dsrna->dnaname);
1817                 else fprintf(f, "struct %s *_self", srna->identifier);
1818                 first= 0;
1819         }
1820
1821         if(func->flag & FUNC_USE_CONTEXT) {
1822                 if(!first) fprintf(f, ", ");
1823                 first= 0;
1824                 fprintf(f, "bContext *C");
1825         }
1826
1827         if(func->flag & FUNC_USE_REPORTS) {
1828                 if(!first) fprintf(f, ", ");
1829                 first= 0;
1830                 fprintf(f, "ReportList *reports");
1831         }
1832
1833         /* defined parameters */
1834         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1835                 type = dparm->prop->type;
1836                 flag = dparm->prop->flag;
1837                 pout = (flag & PROP_OUTPUT);
1838                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1839
1840                 if(dparm->prop==func->c_ret)
1841                         continue;
1842
1843                 if (cptr || (flag & PROP_DYNAMIC))
1844                         ptrstr= pout ? "**" : "*";
1845                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
1846                         ptrstr= "*";
1847                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
1848                         ptrstr= "";
1849                 else
1850                         ptrstr= pout ? "*" : "";
1851
1852                 if(!first) fprintf(f, ", ");
1853                 first= 0;
1854
1855                 if(!(flag & PROP_DYNAMIC) && dparm->prop->arraydimension)
1856                         fprintf(f, "%s%s %s[%d]", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier, dparm->prop->totarraylength);
1857                 else
1858                         fprintf(f, "%s%s %s%s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
1859
1860                 if (flag & PROP_DYNAMIC)
1861                         fprintf(f, ", int %s%s_len", pout ? "*" : "", dparm->prop->identifier);
1862         }
1863
1864         fprintf(f, ");\n");
1865 }
1866
1867 static void rna_generate_static_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1868 {
1869         FunctionRNA *func;
1870         FunctionDefRNA *dfunc;
1871         int first= 1;
1872
1873         for(func= srna->functions.first; func; func= func->cont.next) {
1874                 dfunc= rna_find_function_def(func);
1875
1876                 if(dfunc->call) {
1877                         if(first) {
1878                                 fprintf(f, "/* Repeated prototypes to detect errors */\n\n");
1879                                 first= 0;
1880                         }
1881
1882                         rna_generate_static_parameter_prototypes(brna, srna, dfunc, f);
1883                 }
1884         }
1885
1886         fprintf(f, "\n");
1887 }
1888
1889 static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, PropertyRNA *prop) 
1890 {
1891         char *strnest= "", *errnest= "";
1892         int len, freenest= 0;
1893         
1894         if(nest != NULL) {
1895                 len= strlen(nest);
1896
1897                 strnest= MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> strnest");
1898                 errnest= MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> errnest");
1899
1900                 strcpy(strnest, "_"); strcat(strnest, nest);
1901                 strcpy(errnest, "."); strcat(errnest, nest);
1902
1903                 freenest= 1;
1904         }
1905
1906         switch(prop->type) {
1907                         case PROP_ENUM: {
1908                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1909                                 int i, defaultfound= 0;
1910
1911                                 if(eprop->item) {
1912                                         fprintf(f, "static EnumPropertyItem rna_%s%s_%s_items[%d] = {\n\t", srna->identifier, strnest, prop->identifier, eprop->totitem+1);
1913
1914                                         for(i=0; i<eprop->totitem; i++) {
1915                                                 fprintf(f, "{%d, ", eprop->item[i].value);
1916                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
1917                                                 fprintf(f, "%d, ", eprop->item[i].icon);
1918                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
1919                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "},\n\t");
1920
1921                                                 if(eprop->item[i].identifier[0])
1922                                                         if(eprop->defaultvalue == eprop->item[i].value)
1923                                                                 defaultfound= 1;
1924                                         }
1925
1926                                         fprintf(f, "{0, NULL, 0, NULL, NULL}\n};\n\n");
1927
1928                                         if(!defaultfound) {
1929                                                 fprintf(stderr, "rna_generate_structs: %s%s.%s, enum default is not in items.\n", srna->identifier, errnest, prop->identifier);
1930                                                 DefRNA.error= 1;
1931                                         }
1932                                 }
1933                                 else {
1934                                         fprintf(stderr, "rna_generate_structs: %s%s.%s, enum must have items defined.\n", srna->identifier, errnest, prop->identifier);
1935                                         DefRNA.error= 1;
1936                                 }
1937                                 break;
1938                         }
1939                         case PROP_BOOLEAN: {
1940                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1941                                 unsigned int i;
1942
1943                                 if(prop->arraydimension && prop->totarraylength) {
1944                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
1945
1946                                         for(i=0; i<prop->totarraylength; i++) {
1947                                                 if(bprop->defaultarray)
1948                                                         fprintf(f, "%d", bprop->defaultarray[i]);
1949                                                 else
1950                                                         fprintf(f, "%d", bprop->defaultvalue);
1951                                                 if(i != prop->totarraylength-1)
1952                                                         fprintf(f, ",\n\t");
1953                                         }
1954
1955                                         fprintf(f, "\n};\n\n");
1956                                 }
1957                                 break;
1958                         }
1959                         case PROP_INT: {
1960                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1961                                 unsigned int i;
1962
1963                                 if(prop->arraydimension && prop->totarraylength) {
1964                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
1965
1966                                         for(i=0; i<prop->totarraylength; i++) {
1967                                                 if(iprop->defaultarray)
1968                                                         fprintf(f, "%d", iprop->defaultarray[i]);
1969                                                 else
1970                                                         fprintf(f, "%d", iprop->defaultvalue);
1971                                                 if(i != prop->totarraylength-1)
1972                                                         fprintf(f, ",\n\t");
1973                                         }
1974
1975                                         fprintf(f, "\n};\n\n");
1976                                 }
1977                                 break;
1978                         }
1979                         case PROP_FLOAT: {
1980                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1981                                 unsigned int i;
1982
1983                                 if(prop->arraydimension && prop->totarraylength) {
1984                                         fprintf(f, "static float rna_%s%s_%s_default[%d] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
1985
1986                                         for(i=0; i<prop->totarraylength; i++) {
1987                                                 if(fprop->defaultarray)
1988                                                         rna_float_print(f, fprop->defaultarray[i]);
1989                                                 else
1990                                                         rna_float_print(f, fprop->defaultvalue);
1991                                                 if(i != prop->totarraylength-1)
1992                                                         fprintf(f, ",\n\t");
1993                                         }
1994
1995                                         fprintf(f, "\n};\n\n");
1996                                 }
1997                                 break;
1998                         }
1999                         default:
2000                                 break;
2001         }
2002
2003         fprintf(f, "%s%s rna_%s%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, strnest, prop->identifier);
2004
2005         if(prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s%s_%s, ", srna->identifier, strnest, prop->next->identifier);
2006         else fprintf(f, "\t{NULL, ");
2007         if(prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s%s_%s,\n", srna->identifier, strnest, prop->prev->identifier);
2008         else fprintf(f, "NULL,\n");
2009         fprintf(f, "\t%d, ", prop->magic);
2010         rna_print_c_string(f, prop->identifier);
2011         fprintf(f, ", %d, ", prop->flag);
2012         rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
2013         rna_print_c_string(f, prop->description); fprintf(f, ",\n\t");
2014         fprintf(f, "%d,\n", prop->icon);
2015         fprintf(f, "\t%s, %s|%s, %s, %d, {%d, %d, %d}, %d,\n", rna_property_typename(prop->type), rna_property_subtypename(prop->subtype), rna_property_subtype_unit(prop->subtype), rna_function_string(prop->getlength), prop->arraydimension, prop->arraylength[0], prop->arraylength[1], prop->arraylength[2], prop->totarraylength);
2016         fprintf(f, "\t%s%s, %d, %s, %s,\n", (prop->flag & PROP_CONTEXT_UPDATE)? "(UpdateFunc)": "", rna_function_string(prop->update), prop->noteflag, rna_function_string(prop->editable), rna_function_string(prop->itemeditable));
2017
2018         if(prop->flag & PROP_RAW_ACCESS) rna_set_raw_offset(f, srna, prop);
2019         else fprintf(f, "\t0, -1");
2020
2021         /* our own type - collections/arrays only */
2022         if(prop->srna) fprintf(f, ", &RNA_%s", (char*)prop->srna);
2023         else fprintf(f, ", NULL");
2024
2025         fprintf(f, "},\n");
2026
2027         switch(prop->type) {
2028                         case PROP_BOOLEAN: {
2029                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
2030                                 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);
2031                                 if(prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2032                                 else fprintf(f, "NULL\n");
2033                                 break;
2034                         }
2035                         case PROP_INT: {
2036                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
2037                                 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));
2038                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
2039                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
2040                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
2041                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
2042                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
2043                                 rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
2044                                 if(prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2045                                 else fprintf(f, "NULL\n");
2046                                 break;
2047                          }
2048                         case PROP_FLOAT: {
2049                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
2050                                 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));
2051                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
2052                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
2053                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
2054                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
2055                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
2056                                 rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
2057                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
2058                                 if(prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2059                                 else fprintf(f, "NULL\n");
2060                                 break;
2061                          }
2062                         case PROP_STRING: {
2063                                 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
2064                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
2065                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
2066                                 break;
2067                         }
2068                         case PROP_ENUM: {
2069                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
2070                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(eprop->get), rna_function_string(eprop->set), rna_function_string(eprop->itemf));
2071                                 if(eprop->item)
2072                                         fprintf(f, "rna_%s%s_%s_items, ", srna->identifier, strnest, prop->identifier);
2073                                 else
2074                                         fprintf(f, "NULL, ");
2075                                 fprintf(f, "%d, %d\n", eprop->totitem, eprop->defaultvalue);
2076                                 break;
2077                         }
2078                         case PROP_POINTER: {
2079                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
2080                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(pprop->get), rna_function_string(pprop->set), rna_function_string(pprop->typef));
2081                                 if(pprop->type) fprintf(f, "&RNA_%s\n", (char*)pprop->type);
2082                                 else fprintf(f, "NULL\n");
2083                                 break;
2084                          }
2085                         case PROP_COLLECTION: {
2086                                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
2087                                 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));
2088                                 if(cprop->item_type) fprintf(f, "&RNA_%s\n", (char*)cprop->item_type);
2089                                 else fprintf(f, "NULL\n");
2090                                 break;
2091                         }
2092         }
2093
2094         fprintf(f, "};\n\n");
2095
2096         if(freenest) {
2097                 MEM_freeN(strnest);
2098                 MEM_freeN(errnest);
2099         }
2100 }
2101
2102 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
2103 {
2104         FunctionRNA *func;
2105         FunctionDefRNA *dfunc;
2106         PropertyRNA *prop, *parm;
2107         StructRNA *base;
2108
2109         fprintf(f, "/* %s */\n", srna->name);
2110
2111         for(prop= srna->cont.properties.first; prop; prop= prop->next)
2112                 rna_generate_property(f, srna, NULL, prop);
2113
2114         for(func= srna->functions.first; func; func= func->cont.next) {
2115                 for(parm= func->cont.properties.first; parm; parm= parm->next)
2116                         rna_generate_property(f, srna, func->identifier, parm);
2117
2118                 fprintf(f, "%s%s rna_%s_%s_func = {\n", "", "FunctionRNA", srna->identifier, func->identifier);
2119
2120                 if(func->cont.next) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, ((FunctionRNA*)func->cont.next)->identifier);
2121                 else fprintf(f, "\t{NULL, ");
2122                 if(func->cont.prev) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func,\n", srna->identifier, ((FunctionRNA*)func->cont.prev)->identifier);
2123                 else fprintf(f, "NULL,\n");
2124
2125                 fprintf(f, "\tNULL,\n");
2126
2127                 parm= func->cont.properties.first;
2128                 if(parm) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s_%s, ", srna->identifier, func->identifier, parm->identifier);
2129                 else fprintf(f, "\t{NULL, ");
2130
2131                 parm= func->cont.properties.last;
2132                 if(parm) fprintf(f, "(PropertyRNA*)&rna_%s_%s_%s}},\n", srna->identifier, func->identifier, parm->identifier);
2133                 else fprintf(f, "NULL}},\n");
2134
2135                 fprintf(f, "\t");
2136                 rna_print_c_string(f, func->identifier);
2137                 fprintf(f, ", %d, ", func->flag);
2138                 rna_print_c_string(f, func->description); fprintf(f, ",\n");
2139
2140                 dfunc= rna_find_function_def(func);
2141                 if(dfunc->gencall) fprintf(f, "\t%s,\n", dfunc->gencall);
2142                 else fprintf(f, "\tNULL,\n");
2143
2144                 if(func->c_ret) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s_%s\n", srna->identifier, func->identifier, func->c_ret->identifier);
2145                 else fprintf(f, "\tNULL\n");
2146
2147                 fprintf(f, "};\n");
2148                 fprintf(f, "\n");
2149         }
2150
2151         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
2152
2153         if(srna->cont.next) fprintf(f, "\t{(ContainerRNA *)&RNA_%s, ", ((StructRNA*)srna->cont.next)->identifier);
2154         else fprintf(f, "\t{NULL, ");
2155         if(srna->cont.prev) fprintf(f, "(ContainerRNA *)&RNA_%s,\n", ((StructRNA*)srna->cont.prev)->identifier);
2156         else fprintf(f, "NULL,\n");
2157
2158         fprintf(f, "\tNULL,\n");
2159
2160         prop= srna->cont.properties.first;
2161         if(prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
2162         else fprintf(f, "\t{NULL, ");
2163
2164         prop= srna->cont.properties.last;
2165         if(prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}},\n", srna->identifier, prop->identifier);
2166         else fprintf(f, "NULL}},\n");
2167
2168         fprintf(f, "\tNULL,NULL,\n"); /* PyType - Cant initialize here */
2169         
2170         fprintf(f, "\t");
2171         rna_print_c_string(f, srna->identifier);
2172         fprintf(f, ", %d, ", srna->flag);
2173         rna_print_c_string(f, srna->name);
2174         fprintf(f, ", ");
2175         rna_print_c_string(f, srna->description);
2176         fprintf(f, ",\n\t%d,\n", srna->icon);
2177
2178         prop= srna->nameproperty;
2179         if(prop) {
2180                 base= srna;
2181                 while (base->base && base->base->nameproperty==prop)
2182                         base= base->base;
2183
2184                 fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", base->identifier, prop->identifier);
2185         }
2186         else fprintf(f, "\tNULL, ");
2187
2188         prop= srna->iteratorproperty;
2189         base= srna;
2190         while (base->base && base->base->iteratorproperty==prop)
2191                 base= base->base;
2192         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", base->identifier);
2193
2194         if(srna->base) fprintf(f, "\t&RNA_%s,\n", srna->base->identifier);
2195         else fprintf(f, "\tNULL,\n");
2196
2197         if(srna->nested) fprintf(f, "\t&RNA_%s,\n", srna->nested->identifier);
2198         else fprintf(f, "\tNULL,\n");
2199
2200         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
2201         fprintf(f, "\t%s,\n", rna_function_string(srna->path));
2202         fprintf(f, "\t%s,\n", rna_function_string(srna->reg));
2203         fprintf(f, "\t%s,\n", rna_function_string(srna->unreg));
2204         fprintf(f, "\t%s,\n", rna_function_string(srna->idproperties));
2205
2206         if(srna->reg && !srna->refine) {
2207                 fprintf(stderr, "rna_generate_struct: %s has a register function, must also have refine function.\n", srna->identifier);
2208                 DefRNA.error= 1;
2209         }
2210
2211         func= srna->functions.first;
2212         if(func) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, func->identifier);
2213         else fprintf(f, "\t{NULL, ");
2214
2215         func= srna->functions.last;
2216         if(func) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func}\n", srna->identifier, func->identifier);
2217         else fprintf(f, "NULL}\n");
2218
2219         fprintf(f, "};\n");
2220
2221         fprintf(f, "\n");
2222 }
2223
2224 typedef struct RNAProcessItem {
2225         char *filename;
2226         char *api_filename;
2227         void (*define)(BlenderRNA *brna);
2228 } RNAProcessItem;
2229
2230 RNAProcessItem PROCESS_ITEMS[]= {
2231         {"rna_rna.c", NULL, RNA_def_rna},
2232         {"rna_ID.c", NULL, RNA_def_ID},
2233         {"rna_texture.c", NULL, RNA_def_texture},
2234         {"rna_action.c", "rna_action_api.c", RNA_def_action},
2235         {"rna_animation.c", "rna_animation_api.c", RNA_def_animation},
2236         {"rna_animviz.c", NULL, RNA_def_animviz},
2237         {"rna_actuator.c", NULL, RNA_def_actuator},
2238         {"rna_armature.c", "rna_armature_api.c", RNA_def_armature},
2239         {"rna_boid.c", NULL, RNA_def_boid},
2240         {"rna_brush.c", NULL, RNA_def_brush},
2241         {"rna_camera.c", NULL, RNA_def_camera},
2242         {"rna_cloth.c", NULL, RNA_def_cloth},
2243         {"rna_color.c", NULL, RNA_def_color},
2244         {"rna_constraint.c", NULL, RNA_def_constraint},
2245         {"rna_context.c", NULL, RNA_def_context},
2246         {"rna_controller.c", NULL, RNA_def_controller},
2247         {"rna_curve.c", NULL, RNA_def_curve},
2248         {"rna_fcurve.c", "rna_fcurve_api.c", RNA_def_fcurve},
2249         {"rna_fluidsim.c", NULL, RNA_def_fluidsim},
2250         {"rna_gpencil.c", NULL, RNA_def_gpencil},
2251         {"rna_group.c", NULL, RNA_def_group},
2252         {"rna_image.c", "rna_image_api.c", RNA_def_image},
2253         {"rna_key.c", NULL, RNA_def_key},
2254         {"rna_lamp.c", NULL, RNA_def_lamp},
2255         {"rna_lattice.c", NULL, RNA_def_lattice},
2256         {"rna_main.c", "rna_main_api.c", RNA_def_main},
2257         {"rna_material.c", "rna_material_api.c", RNA_def_material},
2258         {"rna_mesh.c", "rna_mesh_api.c", RNA_def_mesh},
2259         {"rna_meta.c", NULL, RNA_def_meta},
2260         {"rna_modifier.c", NULL, RNA_def_modifier},
2261         {"rna_nla.c", NULL, RNA_def_nla},
2262         {"rna_nodetree.c", NULL, RNA_def_nodetree},
2263         {"rna_object.c", "rna_object_api.c", RNA_def_object},
2264         {"rna_object_force.c", NULL, RNA_def_object_force},
2265         {"rna_packedfile.c", NULL, RNA_def_packedfile},
2266         {"rna_particle.c", NULL, RNA_def_particle},
2267         {"rna_pose.c", "rna_pose_api.c", RNA_def_pose},
2268         {"rna_property.c", NULL, RNA_def_gameproperty},
2269         {"rna_render.c", NULL, RNA_def_render},
2270         {"rna_scene.c", "rna_scene_api.c", RNA_def_scene},
2271         {"rna_screen.c", NULL, RNA_def_screen},
2272         {"rna_sculpt_paint.c", NULL, RNA_def_sculpt_paint},
2273         {"rna_sensor.c", NULL, RNA_def_sensor},
2274         {"rna_sequencer.c", NULL, RNA_def_sequencer},
2275         {"rna_smoke.c", NULL, RNA_def_smoke},
2276         {"rna_space.c", NULL, RNA_def_space},
2277         {"rna_test.c", NULL, RNA_def_test},
2278         {"rna_text.c", NULL, RNA_def_text},
2279         {"rna_timeline.c", NULL, RNA_def_timeline_marker},
2280         {"rna_sound.c", NULL, RNA_def_sound},
2281         {"rna_ui.c", "rna_ui_api.c", RNA_def_ui},
2282         {"rna_userdef.c", NULL, RNA_def_userdef},
2283         {"rna_vfont.c", NULL, RNA_def_vfont},
2284         {"rna_wm.c", "rna_wm_api.c", RNA_def_wm},
2285         {"rna_world.c", NULL, RNA_def_world},   
2286         {NULL, NULL}};
2287
2288 static void rna_generate(BlenderRNA *brna, FILE *f, char *filename, char *api_filename)
2289 {
2290         StructDefRNA *ds;
2291         PropertyDefRNA *dp;
2292         FunctionDefRNA *dfunc;
2293         
2294         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
2295                                  "   Do not edit manually, changes will be overwritten.           */\n\n"
2296                                   "#define RNA_RUNTIME\n\n");
2297
2298         fprintf(f, "#include <float.h>\n");
2299         fprintf(f, "#include <limits.h>\n");
2300         fprintf(f, "#include <string.h>\n\n");
2301         fprintf(f, "#include <stddef.h>\n\n");
2302
2303         fprintf(f, "#include \"DNA_ID.h\"\n");
2304         fprintf(f, "#include \"DNA_scene_types.h\"\n");
2305
2306         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
2307
2308         fprintf(f, "#include \"BKE_context.h\"\n");
2309         fprintf(f, "#include \"BKE_library.h\"\n");
2310         fprintf(f, "#include \"BKE_main.h\"\n");
2311         fprintf(f, "#include \"BKE_report.h\"\n");
2312         fprintf(f, "#include \"BKE_utildefines.h\"\n\n");
2313
2314         fprintf(f, "#include \"RNA_define.h\"\n");
2315         fprintf(f, "#include \"RNA_types.h\"\n");
2316         fprintf(f, "#include \"rna_internal.h\"\n\n");
2317
2318         rna_generate_prototypes(brna, f);
2319
2320         fprintf(f, "#include \"%s\"\n", filename);
2321         if(api_filename)
2322                 fprintf(f, "#include \"%s\"\n", api_filename);
2323         fprintf(f, "\n");
2324
2325         fprintf(f, "/* Autogenerated Functions */\n\n");
2326
2327         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2328                 if(!filename || ds->filename == filename) {
2329                         rna_generate_property_prototypes(brna, ds->srna, f);
2330                         rna_generate_function_prototypes(brna, ds->srna, f);
2331                 }
2332         }
2333
2334         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2335                 if(!filename || ds->filename == filename)
2336                         for(dp=ds->cont.properties.first; dp; dp=dp->next)
2337                                 rna_def_property_funcs(f, ds->srna, dp);
2338
2339         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2340                 if(!filename || ds->filename == filename) {
2341                         for(dfunc=ds->functions.first; dfunc; dfunc= dfunc->cont.next)
2342                                 rna_def_function_funcs(f, ds, dfunc);
2343
2344                         rna_generate_static_function_prototypes(brna, ds->srna, f);
2345                 }
2346         }
2347
2348         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2349                 if(!filename || ds->filename == filename)
2350                         rna_generate_struct(brna, ds->srna, f);
2351
2352         if(strcmp(filename, "rna_ID.c") == 0) {
2353                 /* this is ugly, but we cannot have c files compiled for both
2354                  * makesrna and blender with some build systems at the moment */
2355                 fprintf(f, "#include \"rna_define.c\"\n\n");
2356
2357                 rna_generate_blender(brna, f);
2358         }
2359 }
2360
2361 static void rna_generate_header(BlenderRNA *brna, FILE *f)
2362 {
2363         StructDefRNA *ds;
2364         PropertyDefRNA *dp;
2365         StructRNA *srna;
2366
2367         fprintf(f, "\n#ifndef __RNA_BLENDER_H__\n");
2368         fprintf(f, "#define __RNA_BLENDER_H__\n\n");
2369
2370         fprintf(f, "/* Automatically generated function declarations for the Data API.\n"
2371                                  "   Do not edit manually, changes will be overwritten.              */\n\n");
2372
2373         fprintf(f, "#include \"RNA_types.h\"\n\n");
2374
2375         fprintf(f, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
2376
2377         fprintf(f, "#define FOREACH_BEGIN(property, sptr, itemptr) \\\n");
2378         fprintf(f, "    { \\\n");
2379         fprintf(f, "            CollectionPropertyIterator rna_macro_iter; \\\n");
2380         fprintf(f, "            for(property##_begin(&rna_macro_iter, sptr); rna_macro_iter.valid; property##_next(&rna_macro_iter)) { \\\n");
2381         fprintf(f, "                    itemptr= rna_macro_iter.ptr;\n\n");
2382
2383         fprintf(f, "#define FOREACH_END(property) \\\n");
2384         fprintf(f, "            } \\\n");
2385         fprintf(f, "            property##_end(&rna_macro_iter); \\\n");
2386         fprintf(f, "    }\n\n");
2387
2388         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2389                 srna= ds->srna;
2390
2391                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2392
2393                 while(srna) {
2394                         fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
2395                         srna= srna->base;
2396                 }
2397                 fprintf(f, "\n");
2398
2399                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2400                         rna_def_property_funcs_header(f, ds->srna, dp);
2401         }
2402
2403         fprintf(f, "#ifdef __cplusplus\n}\n#endif\n\n");
2404
2405         fprintf(f, "#endif /* __RNA_BLENDER_H__ */\n\n");
2406 }
2407
2408 static const char *cpp_classes = ""
2409 "\n"
2410 "#include <string>\n"
2411 "\n"
2412 "namespace RNA {\n"
2413 "\n"
2414 "#define BOOLEAN_PROPERTY(sname, identifier) \\\n"
2415 "       bool sname::identifier(void) { return (bool)sname##_##identifier##_get(&ptr); }\n"
2416 "\n"
2417 "#define BOOLEAN_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2418 "       Array<int,size> sname::identifier(void) \\\n"
2419 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2420 "\n"
2421 "#define INT_PROPERTY(sname, identifier) \\\n"
2422 "       int sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2423 "\n"
2424 "#define INT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2425 "       Array<int,size> sname::identifier(void) \\\n"
2426 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2427 "\n"
2428 "#define FLOAT_PROPERTY(sname, identifier) \\\n"
2429 "       float sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2430 "\n"
2431 "#define FLOAT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2432 "       Array<float,size> sname::identifier(void) \\\n"
2433 "               { Array<float, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2434 "\n"
2435 "#define ENUM_PROPERTY(type, sname, identifier) \\\n"
2436 "       sname::type sname::identifier(void) { return (type)sname##_##identifier##_get(&ptr); }\n"
2437 "\n"
2438 "#define STRING_PROPERTY(sname, identifier) \\\n"
2439 "       std::string sname::identifier(void) { \\\n"
2440 "               int len= sname##_##identifier##_length(&ptr); \\\n"
2441 "               std::string str; str.resize(len); \\\n"
2442 "               sname##_##identifier##_get(&ptr, &str[0]); return str; } \\\n"
2443 "\n"
2444 "#define POINTER_PROPERTY(type, sname, identifier) \\\n"
2445 "       type sname::identifier(void) { return type(sname##_##identifier##_get(&ptr)); }\n"
2446 "\n"
2447 "#define COLLECTION_PROPERTY(type, sname, identifier) \\\n"
2448 "       typedef CollectionIterator<type, sname##_##identifier##_begin, \\\n"
2449 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier##_iterator; \\\n"
2450 "       Collection<sname, type, sname##_##identifier##_begin, \\\n"
2451 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier;\n"
2452 "\n"
2453 "class Pointer {\n"
2454 "public:\n"
2455 "       Pointer(const PointerRNA& p) : ptr(p) { }\n"
2456 "       operator const PointerRNA&() { return ptr; }\n"
2457 "       bool is_a(StructRNA *type) { return RNA_struct_is_a(&ptr, type); }\n"
2458 "       operator void*() { return ptr.data; }\n"
2459 "       operator bool() { return ptr.data != NULL; }\n"
2460 "\n"
2461 "       PointerRNA ptr;\n"
2462 "};\n"
2463 "\n"
2464 "\n"
2465 "template<typename T, int Tsize>\n"
2466 "class Array {\n"
2467 "public:\n"
2468 "       T data[Tsize];\n"
2469 "       operator T*() { return data; }\n"
2470 "};\n"
2471 "\n"
2472 "typedef void (*TBeginFunc)(CollectionPropertyIterator *iter, PointerRNA *ptr);\n"
2473 "typedef void (*TNextFunc)(CollectionPropertyIterator *iter);\n"
2474 "typedef void (*TEndFunc)(CollectionPropertyIterator *iter);\n"
2475 "\n"
2476 "template<typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2477 "class CollectionIterator {\n"
2478 "public:\n"
2479 "       CollectionIterator() : t(iter.ptr), init(false) { iter.valid= false; }\n"
2480 "       ~CollectionIterator(void) { if(init) Tend(&iter); };\n"
2481 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator=(const CollectionIterator<T, Tbegin, Tnext, Tend>& copy)\n"
2482 "       { 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"
2483 "\n"
2484 "       operator bool(void)\n"
2485 "       { return iter.valid != 0; }\n"
2486 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator++() { Tnext(&iter); t = T(iter.ptr); return *this; }\n"
2487 "       T& operator*(void) { return t; }\n"
2488 "       T* operator->(void) { return &t; }\n"
2489 "       bool operator==(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid == other.iter.valid; }\n"
2490 "       bool operator!=(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid != other.iter.valid; }\n"
2491 "\n"
2492 "       void begin(const Pointer& ptr)\n"
2493 "       { if(init) Tend(&iter); Tbegin(&iter, (PointerRNA*)&ptr.ptr); t = T(iter.ptr); init = true; }\n"
2494 "\n"
2495 "private:\n"
2496 "       CollectionPropertyIterator iter;\n"
2497 "       T t;\n"
2498 "       bool init;\n"
2499 "};\n"
2500 "\n"
2501 "template<typename Tp, typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2502 "class Collection {\n"
2503 "public:\n"
2504 "       Collection(const PointerRNA& p) : ptr(p) {}\n"
2505 "\n"
2506 "       CollectionIterator<T, Tbegin, Tnext, Tend> begin()\n"
2507 "       { CollectionIterator<T, Tbegin, Tnext, Tend> iter; iter.begin(ptr); return iter; }\n"
2508 "       CollectionIterator<T, Tbegin, Tnext, Tend> end()\n"
2509 "       { return CollectionIterator<T, Tbegin, Tnext, Tend>(); } /* test */ \n"
2510 "\n"
2511 "private:\n"
2512 "       PointerRNA ptr;\n"
2513 "};\n"
2514 "\n";
2515
2516 static void rna_generate_header_cpp(BlenderRNA *brna, FILE *f)
2517 {
2518         StructDefRNA *ds;
2519         PropertyDefRNA *dp;
2520         StructRNA *srna;
2521
2522         fprintf(f, "\n#ifndef __RNA_BLENDER_CPP_H__\n");
2523         fprintf(f, "#define __RNA_BLENDER_CPP_H__\n\n");
2524
2525         fprintf(f, "/* Automatically generated classes for the Data API.\n"
2526                                  "   Do not edit manually, changes will be overwritten. */\n\n");
2527
2528         fprintf(f, "#include \"RNA_blender.h\"\n");
2529         fprintf(f, "#include \"RNA_types.h\"\n");
2530
2531         fprintf(f, "%s", cpp_classes);
2532
2533         fprintf(f, "/**************** Declarations ****************/\n\n");
2534
2535         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2536                 fprintf(f, "class %s;\n", ds->srna->identifier);
2537         fprintf(f, "\n");
2538
2539         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2540                 srna= ds->srna;
2541
2542                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2543
2544                 fprintf(f, "class %s : public %s {\n", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2545                 fprintf(f, "public:\n");
2546                 fprintf(f, "\t%s(const PointerRNA& ptr) :\n\t\t%s(ptr)", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2547                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2548                         if(!(dp->prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN)))
2549                                 if(dp->prop->type == PROP_COLLECTION)
2550                                         fprintf(f, ",\n\t\t%s(ptr)", dp->prop->identifier);
2551                 fprintf(f, "\n\t\t{}\n\n");
2552
2553                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2554                         rna_def_property_funcs_header_cpp(f, ds->srna, dp);
2555                 fprintf(f, "};\n\n");
2556         }
2557
2558
2559         fprintf(f, "/**************** Implementation ****************/\n");
2560
2561         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2562                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2563                         rna_def_property_funcs_impl_cpp(f, ds->srna, dp);
2564
2565                 fprintf(f, "\n");
2566         }
2567
2568         fprintf(f, "}\n\n#endif /* __RNA_BLENDER_CPP_H__ */\n\n");
2569 }
2570
2571 static void make_bad_file(char *file, int line)
2572 {
2573         FILE *fp= fopen(file, "w");
2574         fprintf(fp, "#error \"Error! can't make correct RNA file from %s:%d, STUPID!\"\n", __FILE__, line);
2575         fclose(fp);
2576 }
2577
2578 static int rna_preprocess(char *outfile)
2579 {
2580         BlenderRNA *brna;
2581         StructDefRNA *ds;
2582         FILE *file;
2583         char deffile[4096];
2584         int i, status;
2585
2586         /* define rna */
2587         brna= RNA_create();
2588
2589         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2590                 if(PROCESS_ITEMS[i].define) {
2591                         PROCESS_ITEMS[i].define(brna);
2592
2593                         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2594                                 if(!ds->filename)
2595                                         ds->filename= PROCESS_ITEMS[i].filename;
2596                 }
2597         }
2598
2599         rna_auto_types();
2600
2601
2602         /* create RNA_blender_cpp.h */
2603         strcpy(deffile, outfile);
2604         strcat(deffile, "RNA_blender_cpp.h" TMP_EXT);
2605
2606         status= (DefRNA.error != 0);
2607
2608         if(status) {
2609                 make_bad_file(deffile, __LINE__);
2610         }
2611         else {
2612                 file = fopen(deffile, "w");
2613
2614                 if(!file) {
2615                         printf ("Unable to open file: %s\n", deffile);
2616                         status = 1;
2617                 }
2618                 else {
2619                         rna_generate_header_cpp(brna, file);
2620                         fclose(file);
2621                         status= (DefRNA.error != 0);
2622                 }
2623         }
2624
2625         replace_if_different(deffile);
2626
2627         rna_sort(brna);
2628
2629         /* create rna_gen_*.c files */
2630         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2631                 strcpy(deffile, outfile);
2632                 strcat(deffile, PROCESS_ITEMS[i].filename);
2633                 deffile[strlen(deffile)-2] = '\0';
2634                 strcat(deffile, "_gen.c" TMP_EXT);
2635
2636                 if(status) {
2637                         make_bad_file(deffile, __LINE__);
2638                 }
2639                 else {
2640                         file = fopen(deffile, "w");
2641
2642                         if(!file) {
2643                                 printf ("Unable to open file: %s\n", deffile);
2644                                 status = 1;
2645                         }
2646                         else {
2647                                 rna_generate(brna, file, PROCESS_ITEMS[i].filename, PROCESS_ITEMS[i].api_filename);
2648                                 fclose(file);
2649                                 status= (DefRNA.error != 0);
2650                         }
2651                 }
2652
2653                 replace_if_different(deffile);
2654         }
2655
2656         /* create RNA_blender.h */
2657         strcpy(deffile, outfile);
2658         strcat(deffile, "RNA_blender.h" TMP_EXT);
2659
2660         if(status) {
2661                 make_bad_file(deffile, __LINE__);
2662         }
2663         else {
2664                 file = fopen(deffile, "w");
2665
2666                 if(!file) {
2667                         printf ("Unable to open file: %s\n", deffile);
2668                         status = 1;
2669                 }
2670                 else {
2671                         rna_generate_header(brna, file);
2672                         fclose(file);
2673                         status= (DefRNA.error != 0);
2674                 }
2675         }
2676
2677         replace_if_different(deffile);
2678
2679         /* free RNA */
2680         RNA_define_free(brna);
2681         RNA_free(brna);
2682
2683         return status;
2684 }
2685
2686 static void mem_error_cb(const char *errorStr)
2687 {
2688         fprintf(stderr, "%s", errorStr);
2689         fflush(stderr);
2690 }
2691
2692 int main(int argc, char **argv)
2693 {
2694         int totblock, return_status = 0;
2695
2696         if(argc<2) {
2697                 printf("Usage: %s outdirectory/\n", argv[0]);
2698                 return_status = 1;
2699         }
2700         else {
2701                 printf("Running makesrna, program versions %s\n",  RNA_VERSION_DATE);
2702                 return_status= rna_preprocess(argv[1]);
2703         }
2704
2705         totblock= MEM_get_memory_blocks_in_use();
2706         if(totblock!=0) {
2707                 printf("Error Totblock: %d\n",totblock);
2708                 MEM_set_error_callback(mem_error_cb);
2709                 MEM_printmemlist();
2710         }
2711
2712         return return_status;
2713 }
2714
2715