RNA: C API
[blender-staging.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         fprintf(f, "\"");
123         for(i=0; str[i]; i++) {
124                 for(j=0; escape[j]; j++)
125                         if(str[i] == escape[j][0])
126                                 break;
127
128                 if(escape[j]) fprintf(f, "\\%c", escape[j][1]);
129                 else fprintf(f, "%c", str[i]);
130         }
131         fprintf(f, "\"");
132 }
133
134 static void rna_print_data_get(FILE *f, PropertyDefRNA *dp)
135 {
136         if(dp->dnastructfromname && dp->dnastructfromprop)
137                 fprintf(f, "    %s *data= (%s*)(((%s*)ptr->data)->%s);\n", dp->dnastructname, dp->dnastructname, dp->dnastructfromname, dp->dnastructfromprop);
138         else
139                 fprintf(f, "    %s *data= (%s*)(ptr->data);\n", dp->dnastructname, dp->dnastructname);
140 }
141
142 static char *rna_alloc_function_name(const char *structname, const char *propname, const char *type)
143 {
144         AllocDefRNA *alloc;
145         char buffer[2048];
146         char *result;
147
148         snprintf(buffer, sizeof(buffer), "%s_%s_%s", structname, propname, type);
149         result= MEM_callocN(sizeof(char)*strlen(buffer)+1, "rna_alloc_function_name");
150         strcpy(result, buffer);
151
152         alloc= MEM_callocN(sizeof(AllocDefRNA), "AllocDefRNA");
153         alloc->mem= result;
154         rna_addtail(&DefRNA.allocs, alloc);
155
156         return result;
157 }
158
159 static const char *rna_type_type(PropertyRNA *prop)
160 {
161         switch(prop->type) {
162                 case PROP_BOOLEAN:
163                 case PROP_INT:
164                 case PROP_ENUM:
165                         return "int";
166                 case PROP_FLOAT:
167                         return "float";
168                 case PROP_STRING:
169                         return "char*";
170                 default:
171                         return "PointerRNA";
172         }
173 }
174
175 static int rna_enum_bitmask(PropertyRNA *prop)
176 {
177         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
178         int a, mask= 0;
179
180         for(a=0; a<eprop->totitem; a++)
181                 mask |= eprop->item[a].value;
182         
183         return mask;
184 }
185
186 static int rna_color_quantize(PropertyRNA *prop, PropertyDefRNA *dp)
187 {
188         if(prop->type == PROP_FLOAT && prop->subtype == PROP_COLOR)
189                 if(strcmp(dp->dnatype, "float") != 0 && strcmp(dp->dnatype, "double") != 0)
190                         return 1;
191         
192         return 0;
193 }
194
195 static const char *rna_function_string(void *func)
196 {
197         return (func)? (const char*)func: "NULL";
198 }
199
200 static void rna_float_print(FILE *f, float num)
201 {
202         if(num == -FLT_MAX) fprintf(f, "-FLT_MAX");
203         else if(num == FLT_MAX) fprintf(f, "FLT_MAX");
204         else if((int)num == num) fprintf(f, "%.1ff", num);
205         else fprintf(f, "%.10ff", num);
206 }
207
208 static void rna_int_print(FILE *f, int num)
209 {
210         if(num == INT_MIN) fprintf(f, "INT_MIN");
211         else if(num == INT_MAX) fprintf(f, "INT_MAX");
212         else fprintf(f, "%d", num);
213 }
214
215 static char *rna_def_property_get_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
216 {
217         char *func;
218         int i;
219
220         if(prop->flag & PROP_IDPROPERTY)
221                 return NULL;
222         
223         if(!manualfunc) {
224                 if(!dp->dnastructname || !dp->dnaname) {
225                         fprintf(stderr, "rna_def_property_get_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
226                         DefRNA.error= 1;
227                         return NULL;
228                 }
229
230                 if(prop->type == PROP_STRING && ((StringPropertyRNA*)prop)->maxlength == 0) {
231                         fprintf(stderr, "rna_def_property_get_func: string %s.%s has max length 0.\n", srna->identifier, prop->identifier);
232                         DefRNA.error= 1;
233                         return NULL;
234                 }
235         }
236
237         func= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
238
239         switch(prop->type) {
240                 case PROP_STRING: {
241                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
242                         fprintf(f, "void %s(PointerRNA *ptr, char *value)\n", func);
243                         fprintf(f, "{\n");
244                         if(manualfunc) {
245                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
246                         }
247                         else {
248                                 rna_print_data_get(f, dp);
249                                 fprintf(f, "    BLI_strncpy(value, data->%s, %d);\n", dp->dnaname, sprop->maxlength);
250                         }
251                         fprintf(f, "}\n\n");
252                         break;
253                 }
254                 case PROP_POINTER: {
255                         fprintf(f, "PointerRNA %s(PointerRNA *ptr)\n", func);
256                         fprintf(f, "{\n");
257                         if(manualfunc) {
258                                 fprintf(f, "    return %s(ptr);\n", manualfunc);
259                         }
260                         else {
261                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
262                                 rna_print_data_get(f, dp);
263                                 if(dp->dnapointerlevel == 0)
264                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, &data->%s);\n", (char*)pprop->structtype, dp->dnaname);
265                                 else
266                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, data->%s);\n", (char*)pprop->structtype, dp->dnaname);
267                         }
268                         fprintf(f, "}\n\n");
269                         break;
270                 }
271                 case PROP_COLLECTION: {
272                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
273
274                         fprintf(f, "static PointerRNA %s(CollectionPropertyIterator *iter)\n", func);
275                         fprintf(f, "{\n");
276                         if(manualfunc) {
277                                 if(strcmp(manualfunc, "rna_iterator_listbase_get") == 0 ||
278                                    strcmp(manualfunc, "rna_iterator_array_get") == 0 ||
279                                    strcmp(manualfunc, "rna_iterator_array_dereference_get") == 0)
280                                         fprintf(f, "    return rna_pointer_inherit_refine(&iter->parent, &RNA_%s, %s(iter));\n", (cprop->structtype)? (char*)cprop->structtype: "UnknownType", manualfunc);
281                                 else
282                                         fprintf(f, "    return %s(iter);\n", manualfunc);
283                         }
284                         fprintf(f, "}\n\n");
285                         break;
286                 }
287                 default:
288                         if(prop->arraylength) {
289                                 fprintf(f, "void %s(PointerRNA *ptr, %s values[%d])\n", func, rna_type_type(prop), prop->arraylength);
290                                 fprintf(f, "{\n");
291
292                                 if(manualfunc) {
293                                         fprintf(f, "    return %s(ptr, values);\n", manualfunc);
294                                 }
295                                 else {
296                                         rna_print_data_get(f, dp);
297
298                                         for(i=0; i<prop->arraylength; i++) {
299                                                 if(dp->dnaarraylength == 1) {
300                                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit)
301                                                                 fprintf(f, "    values[%d]= (%s(data->%s & (%d<<%d)) != 0);\n", i, (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit, i);
302                                                         else
303                                                                 fprintf(f, "    values[%d]= (%s)%s((&data->%s)[%d]);\n", i, rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname, i);
304                                                 }
305                                                 else {
306                                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
307                                                                 fprintf(f, "    values[%d]= (%s(data->%s[%d] & ", i, (dp->booleannegative)? "!": "", dp->dnaname, i);
308                                                                 rna_int_print(f, dp->booleanbit);
309                                                                 fprintf(f, ") != 0);\n");
310                                                         }
311                                                         else if(rna_color_quantize(prop, dp))
312                                                                 fprintf(f, "    values[%d]= (%s)(data->%s[%d]*(1.0f/255.0f));\n", i, rna_type_type(prop), dp->dnaname, i);
313                                                         else
314                                                                 fprintf(f, "    values[%d]= (%s)%s(data->%s[%d]);\n", i, rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname, i);
315                                                 }
316                                         }
317                                 }
318                                 fprintf(f, "}\n\n");
319                         }
320                         else {
321                                 fprintf(f, "%s %s(PointerRNA *ptr)\n", rna_type_type(prop), func);
322                                 fprintf(f, "{\n");
323
324                                 if(manualfunc) {
325                                         fprintf(f, "    return %s(ptr);\n", manualfunc);
326                                 }
327                                 else {
328                                         rna_print_data_get(f, dp);
329                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
330                                                 fprintf(f, "    return (%s((data->%s) & ", (dp->booleannegative)? "!": "", dp->dnaname);
331                                                 rna_int_print(f, dp->booleanbit);
332                                                 fprintf(f, ") != 0);\n");
333                                         }
334                                         else if(prop->type == PROP_ENUM && dp->enumbitflags) {
335                                                 fprintf(f, "    return ((data->%s) & ", dp->dnaname);
336                                                 rna_int_print(f, rna_enum_bitmask(prop));
337                                                 fprintf(f, ");\n");
338                                         }
339                                         else
340                                                 fprintf(f, "    return (%s)%s(data->%s);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
341                                 }
342
343                                 fprintf(f, "}\n\n");
344                         }
345                         break;
346         }
347
348         return func;
349 }
350
351 static void rna_clamp_value(FILE *f, PropertyRNA *prop, int array, int i)
352 {
353         if(prop->type == PROP_INT) {
354                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
355
356                 if(iprop->hardmin != INT_MIN || iprop->hardmax != INT_MAX) {
357                         if(array) fprintf(f, "CLAMPIS(values[%d], ", i);
358                         else fprintf(f, "CLAMPIS(value, ");
359                         rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
360                         rna_int_print(f, iprop->hardmax); fprintf(f, ");\n");
361                         return;
362                 }
363         }
364         else if(prop->type == PROP_FLOAT) {
365                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
366
367                 if(fprop->hardmin != -FLT_MAX || fprop->hardmax != FLT_MAX) {
368                         if(array) fprintf(f, "CLAMPIS(values[%d], ", i);
369                         else fprintf(f, "CLAMPIS(value, ");
370                         rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
371                         rna_float_print(f, fprop->hardmax); fprintf(f, ");\n");
372                         return;
373                 }
374         }
375
376         if(array)
377                 fprintf(f, "values[%d];\n", i);
378         else
379                 fprintf(f, "value;\n");
380 }
381
382 static char *rna_def_property_set_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
383 {
384         char *func;
385         int i;
386
387         if(prop->flag & (PROP_IDPROPERTY|PROP_NOT_EDITABLE))
388                 return NULL;
389
390         if(!manualfunc) {
391                 if(!dp->dnastructname || !dp->dnaname) {
392                         if(!(prop->flag & PROP_NOT_EDITABLE)) {
393                                 fprintf(stderr, "rna_def_property_set_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
394                                 DefRNA.error= 1;
395                         }
396                         return NULL;
397                 }
398         }
399
400         func= rna_alloc_function_name(srna->identifier, prop->identifier, "set");
401
402         switch(prop->type) {
403                 case PROP_STRING: {
404                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
405                         fprintf(f, "void %s(PointerRNA *ptr, const char *value)\n", func);
406                         fprintf(f, "{\n");
407                         if(manualfunc) {
408                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
409                         }
410                         else {
411                                 rna_print_data_get(f, dp);
412                                 fprintf(f, "    BLI_strncpy(data->%s, value, %d);\n", dp->dnaname, sprop->maxlength);
413                         }
414                         fprintf(f, "}\n\n");
415                         break;
416                 }
417                 case PROP_POINTER: {
418                         fprintf(f, "void %s(PointerRNA *ptr, PointerRNA value)\n", func);
419                         fprintf(f, "{\n");
420                         if(manualfunc) {
421                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
422                         }
423                         else {
424                                 rna_print_data_get(f, dp);
425                                 fprintf(f, "    data->%s= value.data;\n", dp->dnaname);
426                         }
427                         fprintf(f, "}\n\n");
428                         break;
429                 }
430                 default:
431                         if(prop->arraylength) {
432                                 fprintf(f, "void %s(PointerRNA *ptr, const %s values[%d])\n", func, rna_type_type(prop), prop->arraylength);
433                                 fprintf(f, "{\n");
434
435                                 if(manualfunc) {
436                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
437                                 }
438                                 else {
439                                         rna_print_data_get(f, dp);
440
441                                         for(i=0; i<prop->arraylength; i++) {
442                                                 if(dp->dnaarraylength == 1) {
443                                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
444                                                                 fprintf(f, "    if(%svalues[%d]) data->%s |= (%d<<%d);\n", (dp->booleannegative)? "!": "", i, dp->dnaname, dp->booleanbit, i);
445                                                                 fprintf(f, "    else data->%s &= ~(%d<<%d);\n", dp->dnaname, dp->booleanbit, i);
446                                                         }
447                                                         else {
448                                                                 fprintf(f, "    (&data->%s)[%d]= %s\n", dp->dnaname, i, (dp->booleannegative)? "!": "");
449                                                                 rna_clamp_value(f, prop, 1, i);
450                                                         }
451                                                 }
452                                                 else {
453                                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
454                                                                 fprintf(f, "    if(%svalues[%d]) data->%s[%d] |= ", (dp->booleannegative)? "!": "", i, dp->dnaname, i);
455                                                                 rna_int_print(f, dp->booleanbit);
456                                                                 fprintf(f, ";\n");
457                                                                 fprintf(f, "    else data->%s[%d] &= ~", dp->dnaname, i);
458                                                                 rna_int_print(f, dp->booleanbit);
459                                                                 fprintf(f, ";\n");
460                                                         }
461                                                         else if(rna_color_quantize(prop, dp)) {
462                                                                 fprintf(f, "    data->%s[%d]= FTOCHAR(values[%d]);\n", dp->dnaname, i, i);
463                                                         }
464                                                         else {
465                                                                 fprintf(f, "    data->%s[%d]= %s\n", dp->dnaname, i, (dp->booleannegative)? "!": "");
466                                                                 rna_clamp_value(f, prop, 1, i);
467                                                         }
468                                                 }
469                                         }
470                                 }
471                                 fprintf(f, "}\n\n");
472                         }
473                         else {
474                                 fprintf(f, "void %s(PointerRNA *ptr, %s value)\n", func, rna_type_type(prop));
475                                 fprintf(f, "{\n");
476
477                                 if(manualfunc) {
478                                         fprintf(f, "    %s(ptr, value);\n", manualfunc);
479                                 }
480                                 else {
481                                         rna_print_data_get(f, dp);
482                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
483                                                 fprintf(f, "    if(%svalue) data->%s |= ", (dp->booleannegative)? "!": "", dp->dnaname);
484                                                 rna_int_print(f, dp->booleanbit);
485                                                 fprintf(f, ";\n");
486                                                 fprintf(f, "    else data->%s &= ~", dp->dnaname);
487                                                 rna_int_print(f, dp->booleanbit);
488                                                 fprintf(f, ";\n");
489                                         }
490                                         else if(prop->type == PROP_ENUM && dp->enumbitflags) {
491                                                 fprintf(f, "    data->%s &= ~", dp->dnaname);
492                                                 rna_int_print(f, rna_enum_bitmask(prop));
493                                                 fprintf(f, ";\n");
494                                                 fprintf(f, "    data->%s |= value;\n", dp->dnaname);
495                                         }
496                                         else {
497                                                 fprintf(f, "    data->%s= %s", dp->dnaname, (dp->booleannegative)? "!": "");
498                                                 rna_clamp_value(f, prop, 0, 0);
499                                         }
500                                 }
501                                 fprintf(f, "}\n\n");
502                         }
503                         break;
504         }
505
506         return func;
507 }
508
509 static char *rna_def_property_length_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
510 {
511         char *func= NULL;
512
513         if(prop->flag & PROP_IDPROPERTY)
514                 return NULL;
515
516         if(prop->type == PROP_STRING) {
517                 if(!manualfunc) {
518                         if(!dp->dnastructname || !dp->dnaname) {
519                                 fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
520                                 DefRNA.error= 1;
521                                 return NULL;
522                         }
523                 }
524
525                 func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");
526
527                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
528                 fprintf(f, "{\n");
529                 if(manualfunc) {
530                         fprintf(f, "    return %s(ptr);\n", manualfunc);
531                 }
532                 else {
533                         rna_print_data_get(f, dp);
534                         fprintf(f, "    return strlen(data->%s);\n", dp->dnaname);
535                 }
536                 fprintf(f, "}\n\n");
537         }
538         else if(prop->type == PROP_COLLECTION) {
539                 if(!manualfunc) {
540                         if(prop->type == PROP_COLLECTION && (!(dp->dnalengthname || dp->dnalengthfixed)|| !dp->dnaname)) {
541                                 fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
542                                 DefRNA.error= 1;
543                                 return NULL;
544                         }
545                 }
546
547                 func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");
548
549                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
550                 fprintf(f, "{\n");
551                 if(manualfunc) {
552                         fprintf(f, "    return %s(ptr);\n", manualfunc);
553                 }
554                 else {
555                         rna_print_data_get(f, dp);
556                         if(dp->dnalengthname)
557                                 fprintf(f, "    return (data->%s == NULL)? 0: data->%s;\n", dp->dnaname, dp->dnalengthname);
558                         else
559                                 fprintf(f, "    return (data->%s == NULL)? 0: %d;\n", dp->dnaname, dp->dnalengthfixed);
560                 }
561                 fprintf(f, "}\n\n");
562         }
563
564         return func;
565 }
566
567 static char *rna_def_property_begin_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
568 {
569         char *func, *getfunc;
570
571         if(prop->flag & PROP_IDPROPERTY)
572                 return NULL;
573
574         if(!manualfunc) {
575                 if(!dp->dnastructname || !dp->dnaname) {
576                         fprintf(stderr, "rna_def_property_begin_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
577                         DefRNA.error= 1;
578                         return NULL;
579                 }
580         }
581
582         func= rna_alloc_function_name(srna->identifier, prop->identifier, "begin");
583
584         fprintf(f, "void %s(CollectionPropertyIterator *iter, PointerRNA *ptr)\n", func);
585         fprintf(f, "{\n");
586
587         if(!manualfunc)
588                 rna_print_data_get(f, dp);
589
590         fprintf(f, "\n  memset(iter, 0, sizeof(*iter));\n");
591         fprintf(f, "    iter->parent= *ptr;\n");
592         fprintf(f, "    iter->prop= (PropertyRNA*)&rna_%s_%s;\n", srna->identifier, prop->identifier);
593
594         if(dp->dnalengthname || dp->dnalengthfixed) {
595                 if(manualfunc) {
596                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
597                 }
598                 else {
599                         if(dp->dnalengthname)
600                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), data->%s, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthname);
601                         else
602                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), %d, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthfixed);
603                 }
604         }
605         else {
606                 if(manualfunc)
607                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
608                 else
609                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, &data->%s, NULL);\n", dp->dnaname);
610         }
611
612         getfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
613
614         fprintf(f, "\n  if(iter->valid)\n");
615         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
616
617         fprintf(f, "}\n\n");
618
619
620         return func;
621 }
622
623 static char *rna_def_property_next_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
624 {
625         char *func, *getfunc;
626
627         if(prop->flag & PROP_IDPROPERTY)
628                 return NULL;
629
630         if(!manualfunc)
631                 return NULL;
632
633         func= rna_alloc_function_name(srna->identifier, prop->identifier, "next");
634
635         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
636         fprintf(f, "{\n");
637         fprintf(f, "    %s(iter);\n", manualfunc);
638
639         getfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
640
641         fprintf(f, "\n  if(iter->valid)\n");
642         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
643
644         fprintf(f, "}\n\n");
645
646         return func;
647 }
648
649 static char *rna_def_property_end_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
650 {
651         char *func;
652
653         if(prop->flag & PROP_IDPROPERTY)
654                 return NULL;
655
656         func= rna_alloc_function_name(srna->identifier, prop->identifier, "end");
657
658         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
659         fprintf(f, "{\n");
660         if(manualfunc)
661                 fprintf(f, "    %s(iter);\n", manualfunc);
662         fprintf(f, "}\n\n");
663
664         return func;
665 }
666
667 static void rna_def_property_funcs(FILE *f, PropertyDefRNA *dp)
668 {
669         PropertyRNA *prop;
670         StructRNA *srna;
671
672         srna= dp->srna;
673         prop= dp->prop;
674
675         switch(prop->type) {
676                 case PROP_BOOLEAN: {
677                         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
678
679                         if(!prop->arraylength) {
680                                 bprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->get);
681                                 bprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->set);
682                         }
683                         else {
684                                 bprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->getarray);
685                                 bprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->setarray);
686                         }
687                         break;
688                 }
689                 case PROP_INT: {
690                         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
691
692                         if(!prop->arraylength) {
693                                 iprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->get);
694                                 iprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->set);
695                         }
696                         else {
697                                 iprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->getarray);
698                                 iprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->setarray);
699                         }
700                         break;
701                 }
702                 case PROP_FLOAT: {
703                         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
704
705                         if(!prop->arraylength) {
706                                 fprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->get);
707                                 fprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->set);
708                         }
709                         else {
710                                 fprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->getarray);
711                                 fprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->setarray);
712                         }
713                         break;
714                 }
715                 case PROP_ENUM: {
716                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
717
718                         eprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)eprop->get);
719                         eprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)eprop->set);
720                         break;
721                 }
722                 case PROP_STRING: {
723                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
724
725                         sprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)sprop->get);
726                         sprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)sprop->length);
727                         sprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)sprop->set);
728                         break;
729                 }
730                 case PROP_POINTER: {
731                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
732
733                         pprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)pprop->get);
734                         pprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)pprop->set);
735                         if(!pprop->structtype && !pprop->get) {
736                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, pointer must have either type function or fixed type.\n", srna->identifier, prop->identifier);
737                                 DefRNA.error= 1;
738                         }
739                         break;
740                 }
741                 case PROP_COLLECTION: {
742                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
743
744                         if(dp->dnatype && strcmp(dp->dnatype, "ListBase")==0);
745                         else if(dp->dnalengthname || dp->dnalengthfixed)
746                                 cprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)cprop->length);
747
748                         cprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)cprop->get);
749                         cprop->begin= (void*)rna_def_property_begin_func(f, srna, prop, dp, (char*)cprop->begin);
750                         cprop->next= (void*)rna_def_property_next_func(f, srna, prop, dp, (char*)cprop->next);
751                         cprop->end= (void*)rna_def_property_end_func(f, srna, prop, dp, (char*)cprop->end);
752
753                         if(!(prop->flag & PROP_IDPROPERTY)) {
754                                 if(!cprop->begin) {
755                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a begin function.\n", srna->identifier, prop->identifier);
756                                         DefRNA.error= 1;
757                                 }
758                                 if(!cprop->next) {
759                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a next function.\n", srna->identifier, prop->identifier);
760                                         DefRNA.error= 1;
761                                 }
762                                 if(!cprop->get) {
763                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a get function.\n", srna->identifier, prop->identifier);
764                                         DefRNA.error= 1;
765                                 }
766                         }
767                         if(!cprop->structtype && !cprop->get) {
768                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have either type function or fixed type.\n", srna->identifier, prop->identifier);
769                                 DefRNA.error= 1;
770                         }
771                         break;
772                 }
773         }
774 }
775
776 static void rna_def_property_funcs_header(FILE *f, PropertyDefRNA *dp)
777 {
778         PropertyRNA *prop;
779         StructRNA *srna;
780         char *func;
781
782         srna= dp->srna;
783         prop= dp->prop;
784
785         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
786                 return;
787
788         func= rna_alloc_function_name(srna->identifier, prop->identifier, "");
789
790         switch(prop->type) {
791                 case PROP_BOOLEAN:
792                 case PROP_INT: {
793                         if(!prop->arraylength) {
794                                 fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
795                                 //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
796                         }
797                         else {
798                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[%d]);\n", func, prop->arraylength);
799                                 //fprintf(f, "void %sset(PointerRNA *ptr, const int values[%d]);\n", func, prop->arraylength);
800                         }
801                         break;
802                 }
803                 case PROP_FLOAT: {
804                         if(!prop->arraylength) {
805                                 fprintf(f, "float %sget(PointerRNA *ptr);\n", func);
806                                 //fprintf(f, "void %sset(PointerRNA *ptr, float value);\n", func);
807                         }
808                         else {
809                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[%d]);\n", func, prop->arraylength);
810                                 //fprintf(f, "void %sset(PointerRNA *ptr, const float values[%d]);\n", func, prop->arraylength);
811                         }
812                         break;
813                 }
814                 case PROP_ENUM: {
815                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
816                         int i;
817
818                         if(eprop->item) {
819                                 fprintf(f, "enum {\n");
820
821                                 for(i=0; i<eprop->totitem; i++)
822                                         fprintf(f, "\t%s_%s_%s = %d,\n", srna->identifier, prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
823
824                                 fprintf(f, "};\n\n");
825                         }
826
827                         fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
828                         //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
829
830                         break;
831                 }
832                 case PROP_STRING: {
833                         fprintf(f, "void %sget(PointerRNA *ptr, char *value);\n", func);
834                         fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
835                         //fprintf(f, "void %sset(PointerRNA *ptr, const char *value);\n", func);
836
837                         break;
838                 }
839                 case PROP_POINTER: {
840                         fprintf(f, "PointerRNA %sget(PointerRNA *ptr);\n", func);
841                         //fprintf(f, "void %sset(PointerRNA *ptr, PointerRNA value);\n", func);
842                         break;
843                 }
844                 case PROP_COLLECTION: {
845                         fprintf(f, "void %sbegin(CollectionPropertyIterator *iter, PointerRNA *ptr);\n", func);
846                         fprintf(f, "void %snext(CollectionPropertyIterator *iter);\n", func);
847                         fprintf(f, "void %send(CollectionPropertyIterator *iter);\n", func);
848                         //fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
849                         //fprintf(f, "void %slookup_int(PointerRNA *ptr, int key, StructRNA **type);\n", func);
850                         //fprintf(f, "void %slookup_string(PointerRNA *ptr, const char *key, StructRNA **type);\n", func);
851                         break;
852                 }
853         }
854
855         fprintf(f, "\n");
856 }
857
858 static const char *rna_find_type(const char *type)
859 {
860         StructDefRNA *ds;
861
862         for(ds=DefRNA.structs.first; ds; ds=ds->next)
863                 if(ds->dnaname && strcmp(ds->dnaname, type)==0)
864                         return ds->srna->identifier;
865         
866         return NULL;
867 }
868
869 static void rna_auto_types()
870 {
871         StructDefRNA *ds;
872         PropertyDefRNA *dp;
873
874         for(ds=DefRNA.structs.first; ds; ds=ds->next) {
875                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
876                 if(ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
877                         ds->dnaname= "bScreen";
878
879                 for(dp=ds->properties.first; dp; dp=dp->next) {
880                         if(dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
881                                 dp->dnastructname= "bScreen";
882
883                         if(dp->dnatype) {
884                                 if(dp->prop->type == PROP_POINTER) {
885                                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
886
887                                         if(!pprop->structtype && !pprop->get)
888                                                 pprop->structtype= (StructRNA*)rna_find_type(dp->dnatype);
889                                 }
890                                 else if(dp->prop->type== PROP_COLLECTION) {
891                                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
892
893                                         if(!cprop->structtype && !cprop->get && strcmp(dp->dnatype, "ListBase")==0)
894                                                 cprop->structtype= (StructRNA*)rna_find_type(dp->dnatype);
895                                 }
896                         }
897                 }
898         }
899 }
900
901 static void rna_sort(BlenderRNA *brna)
902 {
903         StructDefRNA *ds;
904         StructRNA *srna;
905
906         rna_sortlist(&brna->structs, cmp_struct);
907         rna_sortlist(&DefRNA.structs, cmp_def_struct);
908
909         for(srna=brna->structs.first; srna; srna=srna->next)
910                 rna_sortlist(&srna->properties, cmp_property);
911
912         for(ds=DefRNA.structs.first; ds; ds=ds->next)
913                 rna_sortlist(&ds->properties, cmp_def_property);
914 }
915
916 static const char *rna_property_structname(PropertyType type)
917 {
918         switch(type) {
919                 case PROP_BOOLEAN: return "BooleanPropertyRNA";
920                 case PROP_INT: return "IntPropertyRNA";
921                 case PROP_FLOAT: return "FloatPropertyRNA";
922                 case PROP_STRING: return "StringPropertyRNA";
923                 case PROP_ENUM: return "EnumPropertyRNA";
924                 case PROP_POINTER: return "PointerPropertyRNA";
925                 case PROP_COLLECTION: return "CollectionPropertyRNA";
926                 default: return "UnknownPropertyRNA";
927         }
928 }
929
930 static const char *rna_property_typename(PropertyType type)
931 {
932         switch(type) {
933                 case PROP_BOOLEAN: return "PROP_BOOLEAN";
934                 case PROP_INT: return "PROP_INT";
935                 case PROP_FLOAT: return "PROP_FLOAT";
936                 case PROP_STRING: return "PROP_STRING";
937                 case PROP_ENUM: return "PROP_ENUM";
938                 case PROP_POINTER: return "PROP_POINTER";
939                 case PROP_COLLECTION: return "PROP_COLLECTION";
940                 default: return "PROP_UNKNOWN";
941         }
942 }
943
944 static const char *rna_property_subtypename(PropertyType type)
945 {
946         switch(type) {
947                 case PROP_NONE: return "PROP_NONE";
948                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
949                 case PROP_FILEPATH: return "PROP_FILEPATH";
950                 case PROP_DIRPATH: return "PROP_DIRPATH";
951                 case PROP_COLOR: return "PROP_COLOR";
952                 case PROP_VECTOR: return "PROP_VECTOR";
953                 case PROP_MATRIX: return "PROP_MATRIX";
954                 case PROP_ROTATION: return "PROP_ROTATION";
955                 case PROP_NEVER_NULL: return "PROP_NEVER_NULL";
956                 default: return "PROP_UNKNOWN";
957         }
958 }
959
960 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
961 {
962         StructRNA *srna;
963
964         for(srna=brna->structs.first; srna; srna=srna->next)
965                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
966         fprintf(f, "\n");
967 }
968
969 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
970 {
971         StructRNA *srna;
972
973         fprintf(f, "BlenderRNA BLENDER_RNA = {");
974
975         srna= brna->structs.first;
976         if(srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
977         else fprintf(f, "{NULL, ");
978
979         srna= brna->structs.last;
980         if(srna) fprintf(f, "&RNA_%s}", srna->identifier);
981         else fprintf(f, "NULL}");
982
983         fprintf(f, "};\n\n");
984 }
985
986 static void rna_generate_property_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
987 {
988         PropertyRNA *prop;
989         StructRNA *base;
990
991         base= srna->base;
992         while (base) {
993                 fprintf(f, "\n");
994                 for(prop=base->properties.first; prop; prop=prop->next)
995                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type), base->identifier, prop->identifier);
996                 base= base->base;
997         }
998
999         if(srna->properties.first)
1000                 fprintf(f, "\n");
1001
1002         for(prop=srna->properties.first; prop; prop=prop->next)
1003                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, prop->identifier);
1004         fprintf(f, "\n");
1005 }
1006
1007 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
1008 {
1009         PropertyRNA *prop;
1010         StructRNA *base;
1011
1012         fprintf(f, "/* %s */\n", srna->name);
1013
1014         for(prop=srna->properties.first; prop; prop=prop->next) {
1015                 switch(prop->type) {
1016                         case PROP_ENUM: {
1017                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1018                                 int i;
1019
1020                                 if(eprop->item) {
1021                                         fprintf(f, "static EnumPropertyItem rna_%s_%s_items[%d] = {", srna->identifier, prop->identifier, eprop->totitem);
1022
1023                                         for(i=0; i<eprop->totitem; i++) {
1024                                                 fprintf(f, "{%d, ", eprop->item[i].value);
1025                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
1026                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
1027                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "}");
1028                                                 if(i != eprop->totitem-1)
1029                                                         fprintf(f, ", ");
1030                                         }
1031
1032                                         fprintf(f, "};\n\n");
1033                                 }
1034                                 else {
1035                                         fprintf(stderr, "rna_generate_structs: %s.%s, enum must have items defined.\n", srna->identifier, prop->identifier);
1036                                         DefRNA.error= 1;
1037                                 }
1038                                 break;
1039                         }
1040                         case PROP_BOOLEAN: {
1041                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1042                                 unsigned int i;
1043
1044                                 if(prop->arraylength) {
1045                                         fprintf(f, "static int rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);
1046
1047                                         for(i=0; i<prop->arraylength; i++) {
1048                                                 if(bprop->defaultarray)
1049                                                         fprintf(f, "%d", bprop->defaultarray[i]);
1050                                                 else
1051                                                         fprintf(f, "%d", bprop->defaultvalue);
1052                                                 if(i != prop->arraylength-1)
1053                                                         fprintf(f, ", ");
1054                                         }
1055
1056                                         fprintf(f, "};\n\n");
1057                                 }
1058                                 break;
1059                         }
1060                         case PROP_INT: {
1061                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1062                                 unsigned int i;
1063
1064                                 if(prop->arraylength) {
1065                                         fprintf(f, "static int rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);
1066
1067                                         for(i=0; i<prop->arraylength; i++) {
1068                                                 if(iprop->defaultarray)
1069                                                         fprintf(f, "%d", iprop->defaultarray[i]);
1070                                                 else
1071                                                         fprintf(f, "%d", iprop->defaultvalue);
1072                                                 if(i != prop->arraylength-1)
1073                                                         fprintf(f, ", ");
1074                                         }
1075
1076                                         fprintf(f, "};\n\n");
1077                                 }
1078                                 break;
1079                         }
1080                         case PROP_FLOAT: {
1081                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1082                                 unsigned int i;
1083
1084                                 if(prop->arraylength) {
1085                                         fprintf(f, "static float rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);
1086
1087                                         for(i=0; i<prop->arraylength; i++) {
1088                                                 if(fprop->defaultarray)
1089                                                         rna_float_print(f, fprop->defaultarray[i]);
1090                                                 else
1091                                                         rna_float_print(f, fprop->defaultvalue);
1092                                                 if(i != prop->arraylength-1)
1093                                                         fprintf(f, ", ");
1094                                         }
1095
1096                                         fprintf(f, "};\n\n");
1097                                 }
1098                                 break;
1099                         }
1100                         default:
1101                                 break;
1102                 }
1103
1104                 fprintf(f, "%s%s rna_%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, prop->identifier);
1105
1106                 if(prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->next->identifier);
1107                 else fprintf(f, "\t{NULL, ");
1108                 if(prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s_%s,\n", srna->identifier, prop->prev->identifier);
1109                 else fprintf(f, "NULL,\n");
1110                 fprintf(f, "\t%d, ", prop->magic);
1111                 rna_print_c_string(f, prop->identifier);
1112                 fprintf(f, ", %d, ", prop->flag);
1113                 rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
1114                 rna_print_c_string(f, prop->description); fprintf(f, ",\n");
1115                 fprintf(f, "\t%s, %s, %d,\n", rna_property_typename(prop->type), rna_property_subtypename(prop->subtype), prop->arraylength);
1116                 fprintf(f, "\t%s, %d, %s},\n", rna_function_string(prop->update), prop->noteflag, rna_function_string(prop->editable));
1117
1118                 switch(prop->type) {
1119                         case PROP_BOOLEAN: {
1120                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1121                                 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);
1122                                 if(prop->arraylength) fprintf(f, "rna_%s_%s_default\n", srna->identifier, prop->identifier);
1123                                 else fprintf(f, "NULL\n");
1124                                 break;
1125                         }
1126                         case PROP_INT: {
1127                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1128                                 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));
1129                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
1130                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
1131                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
1132                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
1133                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
1134                                 rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
1135                                 if(prop->arraylength) fprintf(f, "rna_%s_%s_default\n", srna->identifier, prop->identifier);
1136                                 else fprintf(f, "NULL\n");
1137                                 break;
1138                         }
1139                         case PROP_FLOAT: {
1140                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1141                                 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));
1142                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
1143                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
1144                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
1145                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
1146                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
1147                                 rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
1148                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
1149                                 if(prop->arraylength) fprintf(f, "rna_%s_%s_default\n", srna->identifier, prop->identifier);
1150                                 else fprintf(f, "NULL\n");
1151                                 break;
1152                         }
1153                         case PROP_STRING: {
1154                                 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1155                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
1156                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
1157                                 break;
1158                         }
1159                         case PROP_ENUM: {
1160                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1161                                 fprintf(f, "\t%s, %s, rna_%s_%s_items, %d, %d\n", rna_function_string(eprop->get), rna_function_string(eprop->set), srna->identifier, prop->identifier, eprop->totitem, eprop->defaultvalue);
1162                                 break;
1163                         }
1164                         case PROP_POINTER: {
1165                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
1166                                 fprintf(f, "\t%s, %s, ", rna_function_string(pprop->get), rna_function_string(pprop->set));
1167                                 if(pprop->structtype) fprintf(f, "&RNA_%s\n", (char*)pprop->structtype);
1168                                 else fprintf(f, "NULL\n");
1169                                 break;
1170                         }
1171                         case PROP_COLLECTION: {
1172                                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1173                                 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));
1174                                 if(cprop->structtype) fprintf(f, "&RNA_%s\n", (char*)cprop->structtype);
1175                                 else fprintf(f, "NULL\n");
1176                                 break;
1177                         }
1178                 }
1179
1180                 fprintf(f, "};\n\n");
1181         }
1182
1183         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
1184
1185         if(srna->next) fprintf(f, "\t&RNA_%s, ", srna->next->identifier);
1186         else fprintf(f, "\tNULL, ");
1187         if(srna->prev) fprintf(f, "&RNA_%s,\n", srna->prev->identifier);
1188         else fprintf(f, "NULL,\n");
1189
1190         fprintf(f, "\t");
1191         rna_print_c_string(f, srna->identifier);
1192         fprintf(f, ", %d, ", srna->flag);
1193         rna_print_c_string(f, srna->name);
1194         fprintf(f, ", ");
1195         rna_print_c_string(f, srna->description);
1196         fprintf(f, ",\n");
1197
1198         prop= srna->nameproperty;
1199         if(prop) {
1200                 base= srna;
1201                 while (base->base && base->base->nameproperty==prop)
1202                         base= base->base;
1203
1204                 fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", base->identifier, prop->identifier);
1205         }
1206         else fprintf(f, "\tNULL, ");
1207
1208         prop= srna->iteratorproperty;
1209         base= srna;
1210         while (base->base && base->base->iteratorproperty==prop)
1211                 base= base->base;
1212         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", base->identifier);
1213
1214         if(srna->base) fprintf(f, "\t&RNA_%s,\n", srna->base->identifier);
1215         else fprintf(f, "\tNULL,\n");
1216
1217         if(srna->nested) fprintf(f, "\t&RNA_%s,\n", srna->nested->identifier);
1218         else fprintf(f, "\tNULL,\n");
1219
1220         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
1221
1222         prop= srna->properties.first;
1223         if(prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
1224         else fprintf(f, "\t{NULL, ");
1225
1226         prop= srna->properties.last;
1227         if(prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}\n", srna->identifier, prop->identifier);
1228         else fprintf(f, "NULL}\n");
1229
1230         fprintf(f, "};\n");
1231
1232         fprintf(f, "\n");
1233 }
1234
1235 typedef struct RNAProcessItem {
1236         char *filename;
1237         void (*define)(BlenderRNA *brna);
1238 } RNAProcessItem;
1239
1240 RNAProcessItem PROCESS_ITEMS[]= {
1241         {"rna_ID.c", RNA_def_ID},
1242         {"rna_texture.c", RNA_def_texture},
1243         {"rna_action.c", RNA_def_animation},
1244         {"rna_actuator.c", RNA_def_actuator},
1245         {"rna_armature.c", RNA_def_armature},
1246         {"rna_brush.c", RNA_def_brush},
1247         {"rna_camera.c", RNA_def_camera},
1248         {"rna_cloth.c", RNA_def_cloth},
1249         {"rna_color.c", RNA_def_color},
1250         {"rna_constraint.c", RNA_def_constraint},
1251         {"rna_controller.c", RNA_def_controller},
1252         {"rna_curve.c", RNA_def_curve},
1253         {"rna_fluidsim.c", RNA_def_fluidsim},
1254         {"rna_group.c", RNA_def_group},
1255         {"rna_image.c", RNA_def_image},
1256         {"rna_key.c", RNA_def_key},
1257         {"rna_lamp.c", RNA_def_lamp},
1258         {"rna_lattice.c", RNA_def_lattice},
1259         {"rna_main.c", RNA_def_main},
1260         {"rna_material.c", RNA_def_material},
1261         {"rna_mesh.c", RNA_def_mesh},
1262         {"rna_meta.c", RNA_def_meta},
1263         {"rna_modifier.c", RNA_def_modifier},
1264         {"rna_nodetree.c", RNA_def_nodetree},
1265         {"rna_object.c", RNA_def_object},
1266         {"rna_object_force.c", RNA_def_object_force},
1267         {"rna_packedfile.c", RNA_def_packedfile},
1268         {"rna_particle.c", RNA_def_particle},
1269         {"rna_pose.c", RNA_def_pose},
1270         {"rna_property.c", RNA_def_gameproperty},
1271         {"rna_radio.c", RNA_def_radio},
1272         {"rna_rna.c", RNA_def_rna},
1273         {"rna_scene.c", RNA_def_scene},
1274         {"rna_screen.c", RNA_def_screen},
1275         {"rna_scriptlink.c", RNA_def_scriptlink},
1276         {"rna_sensor.c", RNA_def_sensor},
1277         {"rna_sequence.c", RNA_def_sequence},
1278         {"rna_space.c", RNA_def_space},
1279         {"rna_text.c", RNA_def_text},
1280         {"rna_sound.c", RNA_def_sound},
1281         {"rna_userdef.c", RNA_def_userdef},
1282         {"rna_vfont.c", RNA_def_vfont},
1283         {"rna_vpaint.c", RNA_def_vpaint},
1284         {"rna_wm.c", RNA_def_wm},
1285         {"rna_world.c", RNA_def_world}, 
1286         {NULL, NULL}};
1287
1288 static void rna_generate(BlenderRNA *brna, FILE *f, char *filename)
1289 {
1290         StructDefRNA *ds;
1291         PropertyDefRNA *dp;
1292         
1293         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
1294                      "   Do not edit manually, changes will be overwritten.           */\n\n"
1295                       "#define RNA_RUNTIME\n\n");
1296
1297         fprintf(f, "#include <float.h>\n");
1298         fprintf(f, "#include <limits.h>\n");
1299         fprintf(f, "#include <string.h>\n\n");
1300
1301         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
1302
1303         fprintf(f, "#include \"BKE_utildefines.h\"\n\n");
1304
1305         fprintf(f, "#include \"RNA_define.h\"\n");
1306         fprintf(f, "#include \"RNA_types.h\"\n");
1307         fprintf(f, "#include \"rna_internal.h\"\n\n");
1308
1309         rna_generate_prototypes(brna, f);
1310
1311         fprintf(f, "#include \"%s\"\n\n", filename);
1312
1313         fprintf(f, "/* Autogenerated Functions */\n\n");
1314
1315         for(ds=DefRNA.structs.first; ds; ds=ds->next)
1316                 if(!filename || ds->filename == filename)
1317                         rna_generate_property_prototypes(brna, ds->srna, f);
1318
1319         for(ds=DefRNA.structs.first; ds; ds=ds->next)
1320                 if(!filename || ds->filename == filename)
1321                         for(dp=ds->properties.first; dp; dp=dp->next)
1322                                 rna_def_property_funcs(f, dp);
1323
1324         for(ds=DefRNA.structs.first; ds; ds=ds->next)
1325                 if(!filename || ds->filename == filename)
1326                         rna_generate_struct(brna, ds->srna, f);
1327
1328         if(strcmp(filename, "rna_ID.c") == 0) {
1329                 /* this is ugly, but we cannot have c files compiled for both
1330                  * makesrna and blender with some build systems at the moment */
1331                 fprintf(f, "#include \"rna_define.c\"\n\n");
1332
1333                 rna_generate_blender(brna, f);
1334         }
1335 }
1336
1337 static void rna_generate_header(BlenderRNA *brna, FILE *f)
1338 {
1339         StructDefRNA *ds;
1340         PropertyDefRNA *dp;
1341         StructRNA *srna;
1342
1343         fprintf(f, "\n/* Automatically generated function declarations for the Data API.\n"
1344                      "   Do not edit manually, changes will be overwritten.              */\n\n");
1345
1346         fprintf(f, "#include \"RNA_types.h\"\n\n");
1347
1348         fprintf(f, "#define FOREACH_BEGIN(property, ptr, itemptr) \\\n");
1349         fprintf(f, "    { \\\n");
1350         fprintf(f, "            CollectionPropertyIterator itemptr##_macro_iter; \\\n");
1351         fprintf(f, "            for(property##_begin(sptr, propname, &itemptr##_macro_iter); itemptr##_macro_iter.valid; property##_end(&itemptr##_macro_iter)) { \\\n");
1352         fprintf(f, "                    PointerRNA itemptr= itemptr##_macro_iter.ptr;\n\n");
1353
1354         fprintf(f, "#define FOREACH_END \\\n");
1355         fprintf(f, "            } \\\n");
1356         fprintf(f, "            property##__end(&itemptr##_macro_iter); \\\n");
1357         fprintf(f, "    }\n\n");
1358
1359         for(ds=DefRNA.structs.first; ds; ds=ds->next) {
1360                 srna= ds->srna;
1361
1362                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
1363
1364                 while(srna) {
1365                         fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1366                         srna= srna->base;
1367                 }
1368                 fprintf(f, "\n");
1369
1370                 for(dp=ds->properties.first; dp; dp=dp->next)
1371                         rna_def_property_funcs_header(f, dp);
1372         }
1373 }
1374
1375 static void make_bad_file(char *file)
1376 {
1377         FILE *fp= fopen(file, "w");
1378         fprintf(fp, "ERROR! Cannot make correct RNA file, STUPID!\n");
1379         fclose(fp);
1380 }
1381
1382 static int rna_preprocess(char *outfile)
1383 {
1384         BlenderRNA *brna;
1385         StructDefRNA *ds;
1386         FILE *file;
1387         char deffile[4096];
1388         int i, status;
1389
1390         /* define rna */
1391         brna= RNA_create();
1392
1393         for(i=0; PROCESS_ITEMS[i].filename; i++) {
1394                 if(PROCESS_ITEMS[i].define) {
1395                         PROCESS_ITEMS[i].define(brna);
1396
1397                         for(ds=DefRNA.structs.first; ds; ds=ds->next)
1398                                 if(!ds->filename)
1399                                         ds->filename= PROCESS_ITEMS[i].filename;
1400                 }
1401         }
1402
1403         rna_sort(brna);
1404         rna_auto_types();
1405
1406         status= (DefRNA.error != 0);
1407
1408         /* create rna_gen_*.c files */
1409         for(i=0; PROCESS_ITEMS[i].filename; i++) {
1410                 strcpy(deffile, outfile);
1411                 strcat(deffile, PROCESS_ITEMS[i].filename);
1412                 deffile[strlen(deffile)-2] = '\0';
1413                 strcat(deffile, "_gen.c");
1414
1415                 if(status) {
1416                         make_bad_file(deffile);
1417                 }
1418                 else {
1419                         file = fopen(deffile, "w");
1420
1421                         if(!file) {
1422                                 printf ("Unable to open file: %s\n", deffile);
1423                                 status = 1;
1424                         }
1425                         else {
1426                                 rna_generate(brna, file, PROCESS_ITEMS[i].filename);
1427                                 fclose(file);
1428
1429                                 status= (DefRNA.error != 0);
1430                         }
1431                 }
1432         }
1433
1434         /* create RNA_blender.h */
1435         strcpy(deffile, outfile);
1436         strcat(deffile, "RNA_blender.h");
1437
1438         if(status) {
1439                 make_bad_file(deffile);
1440         }
1441         else {
1442                 file = fopen(deffile, "w");
1443
1444                 if(!file) {
1445                         printf ("Unable to open file: %s\n", deffile);
1446                         status = 1;
1447                 }
1448                 else {
1449                         rna_generate_header(brna, file);
1450                         fclose(file);
1451
1452                         status= (DefRNA.error != 0);
1453                 }
1454         }
1455
1456         /* free RNA */
1457         RNA_define_free(brna);
1458         RNA_free(brna);
1459
1460         return status;
1461 }
1462
1463 int main(int argc, char **argv)
1464 {
1465         int totblock, return_status = 0;
1466
1467         if (argc<2) {
1468                 printf("Usage: %s outdirectory/\n", argv[0]);
1469                 return_status = 1;
1470         }
1471         else {
1472                 printf("Running makesrna, program versions %s\n",  RNA_VERSION_DATE);
1473                 return_status= rna_preprocess(argv[1]);
1474         }
1475
1476         totblock= MEM_get_memory_blocks_in_use();
1477         if(totblock!=0) {
1478                 printf("Error Totblock: %d\n",totblock);
1479                 MEM_printmemlist();
1480         }
1481
1482         return return_status;
1483 }
1484
1485