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