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