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