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