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