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