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