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