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