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