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