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