svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22075:22099
[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                 ptrstr= (dparm->prop->type == PROP_POINTER || dparm->prop->arraylength > 0)? "*" : "";
1202                 fprintf(f, "\t%s%s %s%s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
1203         }
1204
1205         fprintf(f, "\tchar *_data");
1206         if(func->ret) fprintf(f, ", *_retdata");
1207         fprintf(f, ";\n");
1208         fprintf(f, "\t\n");
1209
1210         /* assign self */
1211         if((func->flag & FUNC_NO_SELF)==0) {
1212                 if(dsrna->dnaname) fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", dsrna->dnaname);
1213                 else fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", srna->identifier);
1214         }
1215
1216         fprintf(f, "\t_data= (char *)_parms->data;\n");
1217
1218         dparm= dfunc->cont.properties.first;
1219         for(; dparm; dparm= dparm->next) {
1220                 if(dparm->prop==func->ret) 
1221                         fprintf(f, "\t_retdata= _data;\n");
1222                 else if(dparm->prop->arraylength)
1223                         fprintf(f, "\t%s= ((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1224                 else if(dparm->prop->type == PROP_POINTER) {
1225                         if(dparm->prop->flag & PROP_RNAPTR)
1226                                 fprintf(f, "\t%s= ((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1227                         else
1228                                 fprintf(f, "\t%s= *((%s%s**)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1229                 }
1230                 else
1231                         fprintf(f, "\t%s= *((%s%s*)_data);\n", dparm->prop->identifier, rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1232
1233                 if(dparm->next)
1234                         fprintf(f, "\t_data+= %d;\n", rna_parameter_size(dparm->prop));
1235         }
1236
1237         if(dfunc->call) {
1238                 fprintf(f, "\t\n");
1239                 fprintf(f, "\t");
1240                 if(func->ret) fprintf(f, "%s= ", func->ret->identifier);
1241                 fprintf(f, "%s(", dfunc->call);
1242
1243                 first= 1;
1244
1245                 if((func->flag & FUNC_NO_SELF)==0) {
1246                         fprintf(f, "_self");
1247                         first= 0;
1248                 }
1249
1250                 if(func->flag & FUNC_USE_CONTEXT) {
1251                         if(!first) fprintf(f, ", ");
1252                         first= 0;
1253                         fprintf(f, "C");
1254                 }
1255
1256                 if(func->flag & FUNC_USE_REPORTS) {
1257                         if(!first) fprintf(f, ", ");
1258                         first= 0;
1259                         fprintf(f, "reports");
1260                 }
1261
1262                 dparm= dfunc->cont.properties.first;
1263                 for(; dparm; dparm= dparm->next) {
1264                         if(dparm->prop==func->ret)
1265                                 continue;
1266
1267                         if(!first) fprintf(f, ", ");
1268                         first= 0;
1269
1270                         fprintf(f, "%s", dparm->prop->identifier);
1271                 }
1272
1273                 fprintf(f, ");\n");
1274
1275                 if(func->ret) {
1276                         dparm= rna_find_parameter_def(func->ret);
1277                         ptrstr= dparm->prop->type == PROP_POINTER || dparm->prop->arraylength > 0 ? "*" : "";
1278                         fprintf(f, "\t*((%s%s%s*)_retdata)= %s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, func->ret->identifier);
1279                 }
1280         }
1281
1282         fprintf(f, "}\n\n");
1283
1284         dfunc->gencall= funcname;
1285 }
1286
1287 static void rna_auto_types()
1288 {
1289         StructDefRNA *ds;
1290         PropertyDefRNA *dp;
1291
1292         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1293                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
1294                 if(ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
1295                         ds->dnaname= "bScreen";
1296
1297                 for(dp=ds->cont.properties.first; dp; dp=dp->next) {
1298                         if(dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
1299                                 dp->dnastructname= "bScreen";
1300
1301                         if(dp->dnatype) {
1302                                 if(dp->prop->type == PROP_POINTER) {
1303                                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1304                                         StructRNA *type;
1305
1306                                         if(!pprop->type && !pprop->get)
1307                                                 pprop->type= (StructRNA*)rna_find_type(dp->dnatype);
1308
1309                                         if(pprop->type) {
1310                                                 type= rna_find_struct((char*)pprop->type);
1311                                                 if(type && (type->flag & STRUCT_ID_REFCOUNT))
1312                                                         pprop->property.flag |= PROP_ID_REFCOUNT;
1313                                         }
1314                                 }
1315                                 else if(dp->prop->type== PROP_COLLECTION) {
1316                                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1317
1318                                         if(!cprop->type && !cprop->get && strcmp(dp->dnatype, "ListBase")==0)
1319                                                 cprop->type= (StructRNA*)rna_find_type(dp->dnatype);
1320                                 }
1321                         }
1322                 }
1323         }
1324 }
1325
1326 static void rna_sort(BlenderRNA *brna)
1327 {
1328         StructDefRNA *ds;
1329         StructRNA *srna;
1330
1331         rna_sortlist(&brna->structs, cmp_struct);
1332         rna_sortlist(&DefRNA.structs, cmp_def_struct);
1333
1334         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1335                 rna_sortlist(&srna->cont.properties, cmp_property);
1336
1337         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1338                 rna_sortlist(&ds->cont.properties, cmp_def_property);
1339 }
1340
1341 static const char *rna_property_structname(PropertyType type)
1342 {
1343         switch(type) {
1344                 case PROP_BOOLEAN: return "BooleanPropertyRNA";
1345                 case PROP_INT: return "IntPropertyRNA";
1346                 case PROP_FLOAT: return "FloatPropertyRNA";
1347                 case PROP_STRING: return "StringPropertyRNA";
1348                 case PROP_ENUM: return "EnumPropertyRNA";
1349                 case PROP_POINTER: return "PointerPropertyRNA";
1350                 case PROP_COLLECTION: return "CollectionPropertyRNA";
1351                 default: return "UnknownPropertyRNA";
1352         }
1353 }
1354
1355 static const char *rna_property_typename(PropertyType type)
1356 {
1357         switch(type) {
1358                 case PROP_BOOLEAN: return "PROP_BOOLEAN";
1359                 case PROP_INT: return "PROP_INT";
1360                 case PROP_FLOAT: return "PROP_FLOAT";
1361                 case PROP_STRING: return "PROP_STRING";
1362                 case PROP_ENUM: return "PROP_ENUM";
1363                 case PROP_POINTER: return "PROP_POINTER";
1364                 case PROP_COLLECTION: return "PROP_COLLECTION";
1365                 default: return "PROP_UNKNOWN";
1366         }
1367 }
1368
1369 static const char *rna_property_subtypename(PropertyType type)
1370 {
1371         switch(type) {
1372                 case PROP_NONE: return "PROP_NONE";
1373                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
1374                 case PROP_FILEPATH: return "PROP_FILEPATH";
1375                 case PROP_DIRPATH: return "PROP_DIRPATH";
1376                 case PROP_COLOR: return "PROP_COLOR";
1377                 case PROP_VECTOR: return "PROP_VECTOR";
1378                 case PROP_MATRIX: return "PROP_MATRIX";
1379                 case PROP_ROTATION: return "PROP_ROTATION";
1380                 case PROP_NEVER_NULL: return "PROP_NEVER_NULL";
1381                 case PROP_PERCENTAGE: return "PROP_PERCENTAGE";
1382                 default: return "PROP_UNKNOWN";
1383         }
1384 }
1385
1386 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
1387 {
1388         StructRNA *srna;
1389
1390         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1391                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1392         fprintf(f, "\n");
1393 }
1394
1395 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
1396 {
1397         StructRNA *srna;
1398
1399         fprintf(f, "BlenderRNA BLENDER_RNA = {");
1400
1401         srna= brna->structs.first;
1402         if(srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
1403         else fprintf(f, "{NULL, ");
1404
1405         srna= brna->structs.last;
1406         if(srna) fprintf(f, "&RNA_%s}", srna->identifier);
1407         else fprintf(f, "NULL}");
1408
1409         fprintf(f, "};\n\n");
1410 }
1411
1412 static void rna_generate_property_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1413 {
1414         PropertyRNA *prop;
1415         StructRNA *base;
1416
1417         base= srna->base;
1418         while (base) {
1419                 fprintf(f, "\n");
1420                 for(prop=base->cont.properties.first; prop; prop=prop->next)
1421                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type), base->identifier, prop->identifier);
1422                 base= base->base;
1423         }
1424
1425         if(srna->cont.properties.first)
1426                 fprintf(f, "\n");
1427
1428         for(prop=srna->cont.properties.first; prop; prop=prop->next)
1429                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, prop->identifier);
1430         fprintf(f, "\n");
1431 }
1432
1433 static void rna_generate_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionRNA *func, FILE *f)
1434 {
1435         PropertyRNA *parm;
1436
1437         for(parm= func->cont.properties.first; parm; parm= parm->next)
1438                 fprintf(f, "%s%s rna_%s_%s_%s;\n", "extern ", rna_property_structname(parm->type), srna->identifier, func->identifier, parm->identifier);
1439
1440         if(func->cont.properties.first)
1441                 fprintf(f, "\n");
1442 }
1443
1444 static void rna_generate_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1445 {
1446         FunctionRNA *func;
1447         StructRNA *base;
1448
1449         base= srna->base;
1450         while (base) {
1451                 for(func= base->functions.first; func; func= func->cont.next) {
1452                         fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", base->identifier, func->identifier);
1453                         rna_generate_parameter_prototypes(brna, base, func, f);
1454                 }
1455
1456                 if(base->functions.first)
1457                         fprintf(f, "\n");
1458
1459                 base= base->base;
1460         }
1461
1462         for(func= srna->functions.first; func; func= func->cont.next) {
1463                 fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", srna->identifier, func->identifier);
1464                 rna_generate_parameter_prototypes(brna, srna, func, f);
1465         }
1466
1467         if(srna->functions.first)
1468                 fprintf(f, "\n");
1469 }
1470
1471 static void rna_generate_static_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionDefRNA *dfunc, FILE *f)
1472 {
1473         FunctionRNA *func;
1474         PropertyDefRNA *dparm;
1475         StructDefRNA *dsrna;
1476         int first;
1477
1478         dsrna= rna_find_struct_def(srna);
1479         func= dfunc->func;
1480
1481         /* return type */
1482         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1483                 if(dparm->prop==func->ret) {
1484                         if(dparm->prop->arraylength)
1485                                 fprintf(f, "XXX no array return types yet"); /* XXX not supported */
1486                         else if(dparm->prop->type == PROP_POINTER)
1487                                 fprintf(f, "%s%s *", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1488                         else
1489                                 fprintf(f, "%s%s ", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1490
1491                         break;
1492                 }
1493         }
1494
1495         /* void if nothing to return */
1496         if(!dparm)
1497                 fprintf(f, "void ");
1498
1499         /* function name */
1500         fprintf(f, "%s(", dfunc->call);
1501
1502         first= 1;
1503
1504         /* self, context and reports parameters */
1505         if((func->flag & FUNC_NO_SELF)==0) {
1506                 if(dsrna->dnaname) fprintf(f, "struct %s *_self", dsrna->dnaname);
1507                 else fprintf(f, "struct %s *_self", srna->identifier);
1508                 first= 0;
1509         }
1510
1511         if(func->flag & FUNC_USE_CONTEXT) {
1512                 if(!first) fprintf(f, ", ");
1513                 first= 0;
1514                 fprintf(f, "bContext *C");
1515         }
1516
1517         if(func->flag & FUNC_USE_REPORTS) {
1518                 if(!first) fprintf(f, ", ");
1519                 first= 0;
1520                 fprintf(f, "ReportList *reports");
1521         }
1522
1523         /* defined parameters */
1524         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1525                 if(dparm->prop==func->ret)
1526                         continue;
1527
1528                 if(!first) fprintf(f, ", ");
1529                 first= 0;
1530
1531                 if(dparm->prop->arraylength)
1532                         fprintf(f, "%s%s %s[%d]", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier, dparm->prop->arraylength);
1533                 else if(dparm->prop->type == PROP_POINTER)
1534                         fprintf(f, "%s%s *%s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier);
1535                 else
1536                         fprintf(f, "%s%s %s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier);
1537         }
1538
1539         fprintf(f, ");\n");
1540 }
1541
1542 static void rna_generate_static_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1543 {
1544         FunctionRNA *func;
1545         FunctionDefRNA *dfunc;
1546
1547         fprintf(f, "/* Repeated prototypes to detect errors */\n\n");
1548
1549         for(func= srna->functions.first; func; func= func->cont.next) {
1550                 if(func->flag & FUNC_REGISTER)
1551                         continue;
1552
1553                 dfunc= rna_find_function_def(func);
1554                 if(dfunc->call)
1555                         rna_generate_static_parameter_prototypes(brna, srna, dfunc, f);
1556         }
1557
1558         fprintf(f, "\n");
1559 }
1560
1561 static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, PropertyRNA *prop) 
1562 {
1563         char *strnest= "", *errnest= "";
1564         int len, freenest= 0;
1565         
1566         if(nest != NULL) {
1567                 len= strlen(nest);
1568
1569                 strnest= MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> strnest");
1570                 errnest= MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> errnest");
1571
1572                 strcpy(strnest, "_"); strcat(strnest, nest);
1573                 strcpy(errnest, "."); strcat(errnest, nest);
1574
1575                 freenest= 1;
1576         }
1577
1578         switch(prop->type) {
1579                         case PROP_ENUM: {
1580                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1581                                 int i, defaultfound= 0;
1582
1583                                 if(eprop->item) {
1584                                         fprintf(f, "static EnumPropertyItem rna_%s%s_%s_items[%d] = {", srna->identifier, strnest, prop->identifier, eprop->totitem+1);
1585
1586                                         for(i=0; i<eprop->totitem; i++) {
1587                                                 fprintf(f, "{%d, ", eprop->item[i].value);
1588                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
1589                                                 fprintf(f, "%d, ", eprop->item[i].icon);
1590                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
1591                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "}, ");
1592
1593                                                 if(eprop->item[i].identifier[0])
1594                                                         if(eprop->defaultvalue == eprop->item[i].value)
1595                                                                 defaultfound= 1;
1596                                         }
1597
1598                                         fprintf(f, "{0, NULL, 0, NULL, NULL}};\n\n");
1599
1600                                         if(!defaultfound) {
1601                                                 fprintf(stderr, "rna_generate_structs: %s%s.%s, enum default is not in items.\n", srna->identifier, errnest, prop->identifier);
1602                                                 DefRNA.error= 1;
1603                                         }
1604                                 }
1605                                 else {
1606                                         fprintf(stderr, "rna_generate_structs: %s%s.%s, enum must have items defined.\n", srna->identifier, errnest, prop->identifier);
1607                                         DefRNA.error= 1;
1608                                 }
1609                                 break;
1610                         }
1611                         case PROP_BOOLEAN: {
1612                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1613                                 unsigned int i;
1614
1615                                 if(prop->arraylength) {
1616                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->arraylength);
1617
1618                                         for(i=0; i<prop->arraylength; i++) {
1619                                                 if(bprop->defaultarray)
1620                                                         fprintf(f, "%d", bprop->defaultarray[i]);
1621                                                 else
1622                                                         fprintf(f, "%d", bprop->defaultvalue);
1623                                                 if(i != prop->arraylength-1)
1624                                                         fprintf(f, ", ");
1625                                         }
1626
1627                                         fprintf(f, "};\n\n");
1628                                 }
1629                                 break;
1630                         }
1631                         case PROP_INT: {
1632                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1633                                 unsigned int i;
1634
1635                                 if(prop->arraylength) {
1636                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->arraylength);
1637
1638                                         for(i=0; i<prop->arraylength; i++) {
1639                                                 if(iprop->defaultarray)
1640                                                         fprintf(f, "%d", iprop->defaultarray[i]);
1641                                                 else
1642                                                         fprintf(f, "%d", iprop->defaultvalue);
1643                                                 if(i != prop->arraylength-1)
1644                                                         fprintf(f, ", ");
1645                                         }
1646
1647                                         fprintf(f, "};\n\n");
1648                                 }
1649                                 break;
1650                         }
1651                         case PROP_FLOAT: {
1652                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1653                                 unsigned int i;
1654
1655                                 if(prop->arraylength) {
1656                                         fprintf(f, "static float rna_%s%s_%s_default[%d] = {", srna->identifier, strnest, prop->identifier, prop->arraylength);
1657
1658                                         for(i=0; i<prop->arraylength; i++) {
1659                                                 if(fprop->defaultarray)
1660                                                         rna_float_print(f, fprop->defaultarray[i]);
1661                                                 else
1662                                                         rna_float_print(f, fprop->defaultvalue);
1663                                                 if(i != prop->arraylength-1)
1664                                                         fprintf(f, ", ");
1665                                         }
1666
1667                                         fprintf(f, "};\n\n");
1668                                 }
1669                                 break;
1670                         }
1671                         default:
1672                                 break;
1673         }
1674
1675         fprintf(f, "%s%s rna_%s%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, strnest, prop->identifier);
1676
1677         if(prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s%s_%s, ", srna->identifier, strnest, prop->next->identifier);
1678         else fprintf(f, "\t{NULL, ");
1679         if(prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s%s_%s,\n", srna->identifier, strnest, prop->prev->identifier);
1680         else fprintf(f, "NULL,\n");
1681         fprintf(f, "\t%d, ", prop->magic);
1682         rna_print_c_string(f, prop->identifier);
1683         fprintf(f, ", %d, ", prop->flag);
1684         rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
1685         rna_print_c_string(f, prop->description); fprintf(f, ",\n\t");
1686         fprintf(f, "%d,\n", prop->icon);
1687         fprintf(f, "\t%s, %s, %d,\n", rna_property_typename(prop->type), rna_property_subtypename(prop->subtype), prop->arraylength);
1688         fprintf(f, "\t%s, %d, %s,\n", rna_function_string(prop->update), prop->noteflag, rna_function_string(prop->editable));
1689
1690         if(prop->flag & PROP_RAW_ACCESS) rna_set_raw_offset(f, srna, prop);
1691         else fprintf(f, "\t0, 0");
1692         fprintf(f, "},\n");
1693
1694         switch(prop->type) {
1695                         case PROP_BOOLEAN: {
1696                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1697                                 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);
1698                                 if(prop->arraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1699                                 else fprintf(f, "NULL\n");
1700                                 break;
1701                         }
1702                         case PROP_INT: {
1703                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1704                                 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));
1705                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
1706                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
1707                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
1708                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
1709                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
1710                                 rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
1711                                 if(prop->arraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1712                                 else fprintf(f, "NULL\n");
1713                                 break;
1714                          }
1715                         case PROP_FLOAT: {
1716                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1717                                 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));
1718                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
1719                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
1720                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
1721                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
1722                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
1723                                 rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
1724                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
1725                                 if(prop->arraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
1726                                 else fprintf(f, "NULL\n");
1727                                 break;
1728                          }
1729                         case PROP_STRING: {
1730                                 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1731                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
1732                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
1733                                 break;
1734                         }
1735                         case PROP_ENUM: {
1736                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1737                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(eprop->get), rna_function_string(eprop->set), rna_function_string(eprop->itemf));
1738                                 if(eprop->item)
1739                                         fprintf(f, "rna_%s%s_%s_items, ", srna->identifier, strnest, prop->identifier);
1740                                 else
1741                                         fprintf(f, "NULL, ");
1742                                 fprintf(f, "%d, %d\n", eprop->totitem, eprop->defaultvalue);
1743                                 break;
1744                         }
1745                         case PROP_POINTER: {
1746                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
1747                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(pprop->get), rna_function_string(pprop->set), rna_function_string(pprop->typef));
1748                                 if(pprop->type) fprintf(f, "&RNA_%s\n", (char*)pprop->type);
1749                                 else fprintf(f, "NULL\n");
1750                                 break;
1751                          }
1752                         case PROP_COLLECTION: {
1753                                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1754                                 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));
1755                                 if(cprop->add) fprintf(f, "&rna_%s_%s_func, ", srna->identifier, (char*)cprop->add);
1756                                 else fprintf(f, "NULL, ");
1757                                 if(cprop->remove) fprintf(f, "&rna_%s_%s_func, ", srna->identifier, (char*)cprop->remove);
1758                                 else fprintf(f, "NULL, ");
1759                                 if(cprop->type) fprintf(f, "&RNA_%s\n", (char*)cprop->type);
1760                                 else fprintf(f, "NULL\n");
1761                                 break;
1762                         }
1763         }
1764
1765         fprintf(f, "};\n\n");
1766
1767         if(freenest) {
1768                 MEM_freeN(strnest);
1769                 MEM_freeN(errnest);
1770         }
1771 }
1772
1773 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
1774 {
1775         FunctionRNA *func;
1776         FunctionDefRNA *dfunc;
1777         PropertyRNA *prop, *parm;
1778         StructRNA *base;
1779
1780         fprintf(f, "/* %s */\n", srna->name);
1781
1782         for(prop= srna->cont.properties.first; prop; prop= prop->next)
1783                 rna_generate_property(f, srna, NULL, prop);
1784
1785         for(func= srna->functions.first; func; func= func->cont.next) {
1786                 for(parm= func->cont.properties.first; parm; parm= parm->next)
1787                         rna_generate_property(f, srna, func->identifier, parm);
1788
1789                 fprintf(f, "%s%s rna_%s_%s_func = {\n", "", "FunctionRNA", srna->identifier, func->identifier);
1790
1791                 if(func->cont.next) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, ((FunctionRNA*)func->cont.next)->identifier);
1792                 else fprintf(f, "\t{NULL, ");
1793                 if(func->cont.prev) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func,\n", srna->identifier, ((FunctionRNA*)func->cont.prev)->identifier);
1794                 else fprintf(f, "NULL,\n");
1795
1796                 fprintf(f, "\tNULL,\n");
1797
1798                 parm= func->cont.properties.first;
1799                 if(parm) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s_%s, ", srna->identifier, func->identifier, parm->identifier);
1800                 else fprintf(f, "\t{NULL, ");
1801
1802                 parm= func->cont.properties.last;
1803                 if(parm) fprintf(f, "(PropertyRNA*)&rna_%s_%s_%s}},\n", srna->identifier, func->identifier, parm->identifier);
1804                 else fprintf(f, "NULL}},\n");
1805
1806                 fprintf(f, "\t");
1807                 rna_print_c_string(f, func->identifier);
1808                 fprintf(f, ", %d, ", func->flag);
1809                 rna_print_c_string(f, func->description); fprintf(f, ",\n");
1810
1811                 dfunc= rna_find_function_def(func);
1812                 if(dfunc->gencall) fprintf(f, "\t%s,\n", dfunc->gencall);
1813                 else fprintf(f, "\tNULL,\n");
1814         
1815                 if(func->ret) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s_%s\n", srna->identifier, func->identifier, func->ret->identifier);
1816                 else fprintf(f, "\tNULL\n");
1817
1818                 fprintf(f, "};\n");
1819                 fprintf(f, "\n");
1820         }
1821
1822         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
1823
1824         if(srna->cont.next) fprintf(f, "\t{(ContainerRNA *)&RNA_%s, ", ((StructRNA*)srna->cont.next)->identifier);
1825         else fprintf(f, "\t{NULL, ");
1826         if(srna->cont.prev) fprintf(f, "(ContainerRNA *)&RNA_%s,\n", ((StructRNA*)srna->cont.prev)->identifier);
1827         else fprintf(f, "NULL,\n");
1828
1829         fprintf(f, "\tNULL,\n");
1830
1831         prop= srna->cont.properties.first;
1832         if(prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
1833         else fprintf(f, "\t{NULL, ");
1834
1835         prop= srna->cont.properties.last;
1836         if(prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}},\n", srna->identifier, prop->identifier);
1837         else fprintf(f, "NULL}},\n");
1838
1839         fprintf(f, "\tNULL,NULL,\n"); /* PyType - Cant initialize here */
1840         
1841         fprintf(f, "\t");
1842         rna_print_c_string(f, srna->identifier);
1843         fprintf(f, ", %d, ", srna->flag);
1844         rna_print_c_string(f, srna->name);
1845         fprintf(f, ", ");
1846         rna_print_c_string(f, srna->description);
1847         fprintf(f, ",\n %d,\n", srna->icon);
1848
1849         prop= srna->nameproperty;
1850         if(prop) {
1851                 base= srna;
1852                 while (base->base && base->base->nameproperty==prop)
1853                         base= base->base;
1854
1855                 fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", base->identifier, prop->identifier);
1856         }
1857         else fprintf(f, "\tNULL, ");
1858
1859         prop= srna->iteratorproperty;
1860         base= srna;
1861         while (base->base && base->base->iteratorproperty==prop)
1862                 base= base->base;
1863         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", base->identifier);
1864
1865         if(srna->base) fprintf(f, "\t&RNA_%s,\n", srna->base->identifier);
1866         else fprintf(f, "\tNULL,\n");
1867
1868         if(srna->nested) fprintf(f, "\t&RNA_%s,\n", srna->nested->identifier);
1869         else fprintf(f, "\tNULL,\n");
1870
1871         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
1872         fprintf(f, "\t%s,\n", rna_function_string(srna->path));
1873         fprintf(f, "\t%s,\n", rna_function_string(srna->reg));
1874         fprintf(f, "\t%s,\n", rna_function_string(srna->unreg));
1875         fprintf(f, "\t%s,\n", rna_function_string(srna->idproperties));
1876
1877         if(srna->reg && !srna->refine) {
1878                 fprintf(stderr, "rna_generate_struct: %s has a register function, must also have refine function.\n", srna->identifier);
1879                 DefRNA.error= 1;
1880         }
1881
1882         func= srna->functions.first;
1883         if(func) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, func->identifier);
1884         else fprintf(f, "\t{NULL, ");
1885
1886         func= srna->functions.last;
1887         if(func) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func}\n", srna->identifier, func->identifier);
1888         else fprintf(f, "NULL}\n");
1889
1890         fprintf(f, "};\n");
1891
1892         fprintf(f, "\n");
1893 }
1894
1895 typedef struct RNAProcessItem {
1896         char *filename;
1897         char *api_filename;
1898         void (*define)(BlenderRNA *brna);
1899 } RNAProcessItem;
1900
1901 RNAProcessItem PROCESS_ITEMS[]= {
1902         {"rna_rna.c", NULL, RNA_def_rna},
1903         {"rna_ID.c", NULL, RNA_def_ID},
1904         {"rna_texture.c", NULL, RNA_def_texture},
1905         {"rna_action.c", NULL, RNA_def_action},
1906         {"rna_animation.c", NULL, RNA_def_animation},
1907         {"rna_actuator.c", NULL, RNA_def_actuator},
1908         {"rna_armature.c", NULL, RNA_def_armature},
1909         {"rna_boid.c", NULL, RNA_def_boid},
1910         {"rna_brush.c", NULL, RNA_def_brush},
1911         {"rna_camera.c", NULL, RNA_def_camera},
1912         {"rna_cloth.c", NULL, RNA_def_cloth},
1913         {"rna_color.c", NULL, RNA_def_color},
1914         {"rna_constraint.c", NULL, RNA_def_constraint},
1915         {"rna_context.c", NULL, RNA_def_context},
1916         {"rna_controller.c", NULL, RNA_def_controller},
1917         {"rna_curve.c", NULL, RNA_def_curve},
1918         {"rna_fcurve.c", NULL, RNA_def_fcurve},
1919         {"rna_fluidsim.c", NULL, RNA_def_fluidsim},
1920         {"rna_group.c", NULL, RNA_def_group},
1921         {"rna_image.c", NULL, RNA_def_image},
1922         {"rna_key.c", NULL, RNA_def_key},
1923         {"rna_lamp.c", NULL, RNA_def_lamp},
1924         {"rna_lattice.c", NULL, RNA_def_lattice},
1925         {"rna_main.c", "rna_main_api.c", RNA_def_main},
1926         {"rna_material.c", NULL, RNA_def_material},
1927         {"rna_mesh.c", "rna_mesh_api.c", RNA_def_mesh},
1928         {"rna_meta.c", NULL, RNA_def_meta},
1929         {"rna_modifier.c", NULL, RNA_def_modifier},
1930         {"rna_nla.c", NULL, RNA_def_nla},
1931         {"rna_nodetree.c", NULL, RNA_def_nodetree},
1932         {"rna_object.c", "rna_object_api.c", RNA_def_object},
1933         {"rna_object_force.c", NULL, RNA_def_object_force},
1934         {"rna_packedfile.c", NULL, RNA_def_packedfile},
1935         {"rna_particle.c", NULL, RNA_def_particle},
1936         {"rna_pose.c", NULL, RNA_def_pose},
1937         {"rna_property.c", NULL, RNA_def_gameproperty},
1938         {"rna_render.c", NULL, RNA_def_render},
1939         {"rna_scene.c", NULL, RNA_def_scene},
1940         {"rna_screen.c", NULL, RNA_def_screen},
1941         {"rna_sculpt_paint.c", NULL, RNA_def_sculpt_paint},
1942         {"rna_sensor.c", NULL, RNA_def_sensor},
1943         {"rna_sequence.c", NULL, RNA_def_sequence},
1944         {"rna_smoke.c", NULL, RNA_def_smoke},
1945         {"rna_space.c", NULL, RNA_def_space},
1946         {"rna_text.c", NULL, RNA_def_text},
1947         {"rna_timeline.c", NULL, RNA_def_timeline_marker},
1948         {"rna_sound.c", NULL, RNA_def_sound},
1949         {"rna_ui.c", "rna_ui_api.c", RNA_def_ui},
1950         {"rna_userdef.c", NULL, RNA_def_userdef},
1951         {"rna_vfont.c", NULL, RNA_def_vfont},
1952         {"rna_wm.c", "rna_wm_api.c", RNA_def_wm},
1953         {"rna_world.c", NULL, RNA_def_world},   
1954         {NULL, NULL}};
1955
1956 static void rna_generate(BlenderRNA *brna, FILE *f, char *filename, char *api_filename)
1957 {
1958         StructDefRNA *ds;
1959         PropertyDefRNA *dp;
1960         FunctionDefRNA *dfunc;
1961         
1962         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
1963                      "   Do not edit manually, changes will be overwritten.           */\n\n"
1964                       "#define RNA_RUNTIME\n\n");
1965
1966         fprintf(f, "#include <float.h>\n");
1967         fprintf(f, "#include <limits.h>\n");
1968         fprintf(f, "#include <string.h>\n\n");
1969         fprintf(f, "#include <stddef.h>\n\n");
1970
1971         fprintf(f, "#include \"DNA_ID.h\"\n");
1972
1973         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
1974
1975         fprintf(f, "#include \"BKE_context.h\"\n");
1976         fprintf(f, "#include \"BKE_library.h\"\n");
1977         fprintf(f, "#include \"BKE_report.h\"\n");
1978         fprintf(f, "#include \"BKE_utildefines.h\"\n\n");
1979
1980         fprintf(f, "#include \"RNA_define.h\"\n");
1981         fprintf(f, "#include \"RNA_types.h\"\n");
1982         fprintf(f, "#include \"rna_internal.h\"\n\n");
1983
1984         rna_generate_prototypes(brna, f);
1985
1986         fprintf(f, "#include \"%s\"\n", filename);
1987         if(api_filename)
1988                 fprintf(f, "#include \"%s\"\n", api_filename);
1989         fprintf(f, "\n");
1990
1991         fprintf(f, "/* Autogenerated Functions */\n\n");
1992
1993         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1994                 if(!filename || ds->filename == filename) {
1995                         rna_generate_property_prototypes(brna, ds->srna, f);
1996                         rna_generate_function_prototypes(brna, ds->srna, f);
1997                 }
1998         }
1999
2000         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2001                 if(!filename || ds->filename == filename)
2002                         for(dp=ds->cont.properties.first; dp; dp=dp->next)
2003                                 rna_def_property_funcs(f, ds->srna, dp);
2004
2005         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2006                 if(!filename || ds->filename == filename) {
2007                         for(dfunc=ds->functions.first; dfunc; dfunc= dfunc->cont.next)
2008                                 rna_def_function_funcs(f, ds, dfunc);
2009
2010                         rna_generate_static_function_prototypes(brna, ds->srna, f);
2011                 }
2012         }
2013
2014         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2015                 if(!filename || ds->filename == filename)
2016                         rna_generate_struct(brna, ds->srna, f);
2017
2018         if(strcmp(filename, "rna_ID.c") == 0) {
2019                 /* this is ugly, but we cannot have c files compiled for both
2020                  * makesrna and blender with some build systems at the moment */
2021                 fprintf(f, "#include \"rna_define.c\"\n\n");
2022
2023                 rna_generate_blender(brna, f);
2024         }
2025 }
2026
2027 static void rna_generate_header(BlenderRNA *brna, FILE *f)
2028 {
2029         StructDefRNA *ds;
2030         PropertyDefRNA *dp;
2031         StructRNA *srna;
2032
2033         fprintf(f, "\n#ifndef __RNA_BLENDER_H__\n");
2034         fprintf(f, "#define __RNA_BLENDER_H__\n\n");
2035
2036         fprintf(f, "/* Automatically generated function declarations for the Data API.\n"
2037                      "   Do not edit manually, changes will be overwritten.              */\n\n");
2038
2039         fprintf(f, "#include \"RNA_types.h\"\n\n");
2040
2041         fprintf(f, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
2042
2043         fprintf(f, "#define FOREACH_BEGIN(property, sptr, itemptr) \\\n");
2044         fprintf(f, "    { \\\n");
2045         fprintf(f, "            CollectionPropertyIterator rna_macro_iter; \\\n");
2046         fprintf(f, "            for(property##_begin(&rna_macro_iter, sptr); rna_macro_iter.valid; property##_next(&rna_macro_iter)) { \\\n");
2047         fprintf(f, "                    itemptr= rna_macro_iter.ptr;\n\n");
2048
2049         fprintf(f, "#define FOREACH_END(property) \\\n");
2050         fprintf(f, "            } \\\n");
2051         fprintf(f, "            property##_end(&rna_macro_iter); \\\n");
2052         fprintf(f, "    }\n\n");
2053
2054         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2055                 srna= ds->srna;
2056
2057                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2058
2059                 while(srna) {
2060                         fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
2061                         srna= srna->base;
2062                 }
2063                 fprintf(f, "\n");
2064
2065                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2066                         rna_def_property_funcs_header(f, ds->srna, dp);
2067         }
2068
2069         fprintf(f, "#ifdef __cplusplus\n}\n#endif\n\n");
2070
2071         fprintf(f, "#endif /* __RNA_BLENDER_H__ */\n\n");
2072 }
2073
2074 static const char *cpp_classes = ""
2075 "\n"
2076 "#include <string>\n"
2077 "\n"
2078 "namespace RNA {\n"
2079 "\n"
2080 "#define BOOLEAN_PROPERTY(sname, identifier) \\\n"
2081 "       bool sname::identifier(void) { return (bool)sname##_##identifier##_get(&ptr); }\n"
2082 "\n"
2083 "#define BOOLEAN_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2084 "       Array<int,size> sname::identifier(void) \\\n"
2085 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2086 "\n"
2087 "#define INT_PROPERTY(sname, identifier) \\\n"
2088 "       int sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2089 "\n"
2090 "#define INT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2091 "       Array<int,size> sname::identifier(void) \\\n"
2092 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2093 "\n"
2094 "#define FLOAT_PROPERTY(sname, identifier) \\\n"
2095 "       float sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2096 "\n"
2097 "#define FLOAT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2098 "       Array<float,size> sname::identifier(void) \\\n"
2099 "               { Array<float, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2100 "\n"
2101 "#define ENUM_PROPERTY(type, sname, identifier) \\\n"
2102 "       sname::type sname::identifier(void) { return (type)sname##_##identifier##_get(&ptr); }\n"
2103 "\n"
2104 "#define STRING_PROPERTY(sname, identifier) \\\n"
2105 "       std::string sname::identifier(void) { \\\n"
2106 "               int len= sname##_##identifier##_length(&ptr); \\\n"
2107 "               std::string str; str.resize(len); \\\n"
2108 "               sname##_##identifier##_get(&ptr, &str[0]); return str; } \\\n"
2109 "\n"
2110 "#define POINTER_PROPERTY(type, sname, identifier) \\\n"
2111 "       type sname::identifier(void) { return type(sname##_##identifier##_get(&ptr)); }\n"
2112 "\n"
2113 "#define COLLECTION_PROPERTY(type, sname, identifier) \\\n"
2114 "       typedef CollectionIterator<type, sname##_##identifier##_begin, \\\n"
2115 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier##_iterator; \\\n"
2116 "       Collection<sname, type, sname##_##identifier##_begin, \\\n"
2117 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier;\n"
2118 "\n"
2119 "class Pointer {\n"
2120 "public:\n"
2121 "       Pointer(const PointerRNA& p) : ptr(p) { }\n"
2122 "       operator const PointerRNA&() { return ptr; }\n"
2123 "       bool is_a(StructRNA *type) { return RNA_struct_is_a(&ptr, type); }\n"
2124 "       operator void*() { return ptr.data; }\n"
2125 "       operator bool() { return ptr.data != NULL; }\n"
2126 "\n"
2127 "       PointerRNA ptr;\n"
2128 "};\n"
2129 "\n"
2130 "\n"
2131 "template<typename T, int Tsize>\n"
2132 "class Array {\n"
2133 "public:\n"
2134 "       T data[Tsize];\n"
2135 "       operator T*() { return data; }\n"
2136 "};\n"
2137 "\n"
2138 "typedef void (*TBeginFunc)(CollectionPropertyIterator *iter, PointerRNA *ptr);\n"
2139 "typedef void (*TNextFunc)(CollectionPropertyIterator *iter);\n"
2140 "typedef void (*TEndFunc)(CollectionPropertyIterator *iter);\n"
2141 "\n"
2142 "template<typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2143 "class CollectionIterator {\n"
2144 "public:\n"
2145 "       CollectionIterator() : t(iter.ptr), init(false) { iter.valid= false; }\n"
2146 "       ~CollectionIterator(void) { if(init) Tend(&iter); };\n"
2147 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator=(const CollectionIterator<T, Tbegin, Tnext, Tend>& copy)\n"
2148 "       { 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"
2149 "\n"
2150 "       operator bool(void)\n"
2151 "       { return iter.valid != 0; }\n"
2152 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator++() { Tnext(&iter); t = T(iter.ptr); return *this; }\n"
2153 "       T& operator*(void) { return t; }\n"
2154 "       T* operator->(void) { return &t; }\n"
2155 "       bool operator==(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid == other.iter.valid; }\n"
2156 "       bool operator!=(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid != other.iter.valid; }\n"
2157 "\n"
2158 "       void begin(const Pointer& ptr)\n"
2159 "       { if(init) Tend(&iter); Tbegin(&iter, (PointerRNA*)&ptr.ptr); t = T(iter.ptr); init = true; }\n"
2160 "\n"
2161 "private:\n"
2162 "       CollectionPropertyIterator iter;\n"
2163 "       T t;\n"
2164 "       bool init;\n"
2165 "};\n"
2166 "\n"
2167 "template<typename Tp, typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2168 "class Collection {\n"
2169 "public:\n"
2170 "       Collection(const PointerRNA& p) : ptr(p) {}\n"
2171 "\n"
2172 "       CollectionIterator<T, Tbegin, Tnext, Tend> begin()\n"
2173 "       { CollectionIterator<T, Tbegin, Tnext, Tend> iter; iter.begin(ptr); return iter; }\n"
2174 "       CollectionIterator<T, Tbegin, Tnext, Tend> end()\n"
2175 "       { return CollectionIterator<T, Tbegin, Tnext, Tend>(); } /* test */ \n"
2176 "\n"
2177 "private:\n"
2178 "       PointerRNA ptr;\n"
2179 "};\n"
2180 "\n";
2181
2182 static void rna_generate_header_cpp(BlenderRNA *brna, FILE *f)
2183 {
2184         StructDefRNA *ds;
2185         PropertyDefRNA *dp;
2186         StructRNA *srna;
2187
2188         fprintf(f, "\n#ifndef __RNA_BLENDER_CPP_H__\n");
2189         fprintf(f, "#define __RNA_BLENDER_CPP_H__\n\n");
2190
2191         fprintf(f, "/* Automatically generated classes for the Data API.\n"
2192                      "   Do not edit manually, changes will be overwritten. */\n\n");
2193
2194         fprintf(f, "#include \"RNA_blender.h\"\n");
2195         fprintf(f, "#include \"RNA_types.h\"\n");
2196
2197         fprintf(f, cpp_classes);
2198
2199         fprintf(f, "/**************** Declarations ****************/\n\n");
2200
2201         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2202                 fprintf(f, "class %s;\n", ds->srna->identifier);
2203         fprintf(f, "\n");
2204
2205         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2206                 srna= ds->srna;
2207
2208                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2209
2210                 fprintf(f, "class %s : public %s {\n", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2211                 fprintf(f, "public:\n");
2212                 fprintf(f, "\t%s(const PointerRNA& ptr) :\n\t\t%s(ptr)", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2213                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2214                         if(!(dp->prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN)))
2215                                 if(dp->prop->type == PROP_COLLECTION)
2216                                         fprintf(f, ",\n\t\t%s(ptr)", dp->prop->identifier);
2217                 fprintf(f, "\n\t\t{}\n\n");
2218
2219                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2220                         rna_def_property_funcs_header_cpp(f, ds->srna, dp);
2221                 fprintf(f, "};\n\n");
2222         }
2223
2224
2225         fprintf(f, "/**************** Implementation ****************/\n");
2226
2227         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2228                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2229                         rna_def_property_funcs_impl_cpp(f, ds->srna, dp);
2230
2231                 fprintf(f, "\n");
2232         }
2233
2234         fprintf(f, "}\n\n#endif /* __RNA_BLENDER_CPP_H__ */\n\n");
2235 }
2236
2237 static void make_bad_file(char *file)
2238 {
2239         FILE *fp= fopen(file, "w");
2240         fprintf(fp, "ERROR! Cannot make correct RNA file, STUPID!\n");
2241         fclose(fp);
2242 }
2243
2244 static int rna_preprocess(char *outfile)
2245 {
2246         BlenderRNA *brna;
2247         StructDefRNA *ds;
2248         FILE *file;
2249         char deffile[4096];
2250         int i, status;
2251
2252         /* define rna */
2253         brna= RNA_create();
2254
2255         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2256                 if(PROCESS_ITEMS[i].define) {
2257                         PROCESS_ITEMS[i].define(brna);
2258
2259                         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2260                                 if(!ds->filename)
2261                                         ds->filename= PROCESS_ITEMS[i].filename;
2262                 }
2263         }
2264
2265         rna_auto_types();
2266
2267
2268         /* create RNA_blender_cpp.h */
2269         strcpy(deffile, outfile);
2270         strcat(deffile, "RNA_blender_cpp.h");
2271
2272         status= (DefRNA.error != 0);
2273
2274         if(status) {
2275                 make_bad_file(deffile);
2276         }
2277         else {
2278                 file = fopen(deffile, "w");
2279
2280                 if(!file) {
2281                         printf ("Unable to open file: %s\n", deffile);
2282                         status = 1;
2283                 }
2284                 else {
2285                         rna_generate_header_cpp(brna, file);
2286                         fclose(file);
2287
2288                         status= (DefRNA.error != 0);
2289                 }
2290         }
2291
2292         rna_sort(brna);
2293
2294         /* create rna_gen_*.c files */
2295         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2296                 strcpy(deffile, outfile);
2297                 strcat(deffile, PROCESS_ITEMS[i].filename);
2298                 deffile[strlen(deffile)-2] = '\0';
2299                 strcat(deffile, "_gen.c");
2300
2301                 if(status) {
2302                         make_bad_file(deffile);
2303                 }
2304                 else {
2305                         file = fopen(deffile, "w");
2306
2307                         if(!file) {
2308                                 printf ("Unable to open file: %s\n", deffile);
2309                                 status = 1;
2310                         }
2311                         else {
2312                                 rna_generate(brna, file, PROCESS_ITEMS[i].filename, PROCESS_ITEMS[i].api_filename);
2313                                 fclose(file);
2314
2315                                 status= (DefRNA.error != 0);
2316                         }
2317                 }
2318         }
2319
2320         /* create RNA_blender.h */
2321         strcpy(deffile, outfile);
2322         strcat(deffile, "RNA_blender.h");
2323
2324         if(status) {
2325                 make_bad_file(deffile);
2326         }
2327         else {
2328                 file = fopen(deffile, "w");
2329
2330                 if(!file) {
2331                         printf ("Unable to open file: %s\n", deffile);
2332                         status = 1;
2333                 }
2334                 else {
2335                         rna_generate_header(brna, file);
2336                         fclose(file);
2337
2338                         status= (DefRNA.error != 0);
2339                 }
2340         }
2341
2342         /* free RNA */
2343         RNA_define_free(brna);
2344         RNA_free(brna);
2345
2346         return status;
2347 }
2348
2349 static void mem_error_cb(char *errorStr)
2350 {
2351         fprintf(stderr, "%s", errorStr);
2352         fflush(stderr);
2353 }
2354
2355 int main(int argc, char **argv)
2356 {
2357         int totblock, return_status = 0;
2358
2359         if(argc<2) {
2360                 printf("Usage: %s outdirectory/\n", argv[0]);
2361                 return_status = 1;
2362         }
2363         else {
2364                 printf("Running makesrna, program versions %s\n",  RNA_VERSION_DATE);
2365                 return_status= rna_preprocess(argv[1]);
2366         }
2367
2368         totblock= MEM_get_memory_blocks_in_use();
2369         if(totblock!=0) {
2370                 printf("Error Totblock: %d\n",totblock);
2371                 MEM_set_error_callback(mem_error_cb);
2372                 MEM_printmemlist();
2373         }
2374
2375         return return_status;
2376 }
2377
2378