RNA
[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 /* Preprocessing */
48
49 static void rna_print_c_string(FILE *f, const char *str)
50 {
51         static char *escape[] = {"\''", "\"\"", "\??", "\\\\","\aa", "\bb", "\ff", "\nn", "\rr", "\tt", "\vv", NULL};
52         int i, j;
53
54         fprintf(f, "\"");
55         for(i=0; str[i]; i++) {
56                 for(j=0; escape[j]; j++)
57                         if(str[i] == escape[j][0])
58                                 break;
59
60                 if(escape[j]) fprintf(f, "\\%c", escape[j][1]);
61                 else fprintf(f, "%c", str[i]);
62         }
63         fprintf(f, "\"");
64 }
65
66 static char *rna_alloc_function_name(const char *structname, const char *propname, const char *type)
67 {
68         AllocDefRNA *alloc;
69         char buffer[2048];
70         char *result;
71
72         snprintf(buffer, sizeof(buffer), "rna_%s_%s_%s", structname, propname, type);
73         result= MEM_callocN(sizeof(char)*strlen(buffer)+1, "rna_alloc_function_name");
74         strcpy(result, buffer);
75
76         alloc= MEM_callocN(sizeof(AllocDefRNA), "AllocDefRNA");
77         alloc->mem= result;
78         rna_addtail(&DefRNA.allocs, alloc);
79
80         return result;
81 }
82
83 static const char *rna_type_type(PropertyRNA *prop)
84 {
85         switch(prop->type) {
86                 case PROP_BOOLEAN:
87                 case PROP_INT:
88                 case PROP_ENUM:
89                         return "int";
90                 case PROP_FLOAT:
91                         return "float";
92                 case PROP_STRING:
93                         return "char*";
94                 default:
95                         return "void*";
96         }
97 }
98
99 static char *rna_def_property_get_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp)
100 {
101         char *func;
102
103         if(!dp->dnastructname || !dp->dnaname) {
104                 fprintf(stderr, "rna_def_property_get_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
105                 DefRNA.error= 1;
106                 return NULL;
107         }
108
109         if(prop->type == PROP_STRING && ((StringPropertyRNA*)prop)->maxlength == 0) {
110                 fprintf(stderr, "rna_def_property_get_func: string %s.%s has max length 0.\n", srna->identifier, prop->identifier);
111                 DefRNA.error= 1;
112                 return NULL;
113         }
114
115         func= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
116
117         switch(prop->type) {
118                 case PROP_STRING: {
119                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
120                         fprintf(f, "static void %s(PointerRNA *ptr, char *value)\n", func);
121                         fprintf(f, "{\n");
122                         fprintf(f, "    %s *data= (%s*)ptr->data;\n", dp->dnastructname, dp->dnastructname);
123                         fprintf(f, "    BLI_strncpy(value, data->%s, %d);\n", dp->dnaname, sprop->maxlength);
124                         fprintf(f, "}\n\n");
125                         break;
126                 }
127                 default:
128                         if(prop->arraylength) {
129                                 fprintf(f, "static %s %s(PointerRNA *ptr, int index)\n", rna_type_type(prop), func);
130                                 fprintf(f, "{\n");
131                                 fprintf(f, "    %s *data= (%s*)ptr->data;\n", dp->dnastructname, dp->dnastructname);
132                                 if(prop->type == PROP_BOOLEAN && dp->booleanbit && dp->dnaarraylength==1)
133                                         fprintf(f, "    return ((data->%s & (%d<<index)) != 0);\n", dp->dnaname, dp->booleanbit);
134                                 else if(prop->type == PROP_BOOLEAN && dp->booleanbit)
135                                         fprintf(f, "    return ((data->%s[index] & %d) != 0);\n", dp->dnaname, dp->booleanbit);
136                                 else
137                                         fprintf(f, "    return (%s)(data->%s[index]);\n", rna_type_type(prop), dp->dnaname);
138                                 fprintf(f, "}\n\n");
139                         }
140                         else {
141                                 fprintf(f, "static %s %s(PointerRNA *ptr)\n", rna_type_type(prop), func);
142                                 fprintf(f, "{\n");
143                                 fprintf(f, "    %s *data= (%s*)ptr->data;\n", dp->dnastructname, dp->dnastructname);
144                                 if(prop->type == PROP_BOOLEAN && dp->booleanbit)
145                                         fprintf(f, "    return (((data->%s) & %d) != 0);\n", dp->dnaname, dp->booleanbit);
146                                 else
147                                         fprintf(f, "    return (%s)(data->%s);\n", rna_type_type(prop), dp->dnaname);
148                                 fprintf(f, "}\n\n");
149                         }
150                         break;
151         }
152
153         return func;
154 }
155
156 static const char *rna_function_string(void *func)
157 {
158         return (func)? (const char*)func: "NULL";
159 }
160
161 static void rna_float_print(FILE *f, float num)
162 {
163         if(num == -FLT_MAX) fprintf(f, "-FLT_MAX");
164         else if(num == FLT_MAX) fprintf(f, "FLT_MAX");
165         else if((int)num == num) fprintf(f, "%.1ff", num);
166         else fprintf(f, "%.10ff", num);
167 }
168
169 static void rna_clamp_value(FILE *f, PropertyRNA *prop)
170 {
171         if(prop->type == PROP_INT) {
172                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
173
174                 if(iprop->hardmin != INT_MIN || iprop->hardmax != INT_MAX)
175                         fprintf(f, "    CLAMP(value, %d, %d);\n", iprop->hardmin, iprop->hardmax);
176         }
177         else if(prop->type == PROP_FLOAT) {
178                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
179
180                 if(fprop->hardmin != -FLT_MAX || fprop->hardmax != FLT_MAX) {
181                         fprintf(f, "    CLAMP(value, ");
182                         rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
183                         rna_float_print(f, fprop->hardmax); fprintf(f, ");\n");
184                 }
185         }
186 }
187
188 static char *rna_def_property_set_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp)
189 {
190         char *func;
191
192         if(!dp->dnastructname || !dp->dnaname) {
193                 if(!(prop->flag & PROP_NOT_EDITABLE)) {
194                         fprintf(stderr, "rna_def_property_set_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
195                         DefRNA.error= 1;
196                 }
197                 return NULL;
198         }
199
200         func= rna_alloc_function_name(srna->identifier, prop->identifier, "set");
201
202         switch(prop->type) {
203                 case PROP_STRING: {
204                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
205                         fprintf(f, "static void %s(PointerRNA *ptr, const char *value)\n", func);
206                         fprintf(f, "{\n");
207                         fprintf(f, "    %s *data= (%s*)ptr->data;\n", dp->dnastructname, dp->dnastructname);
208                         fprintf(f, "    BLI_strncpy(data->%s, value, %d);\n", dp->dnaname, sprop->maxlength);
209                         fprintf(f, "}\n\n");
210                         break;
211                 }
212                 default:
213                         if(prop->arraylength) {
214                                 fprintf(f, "static void %s(PointerRNA *ptr, int index, %s value)\n", func, rna_type_type(prop));
215                                 fprintf(f, "{\n");
216                                 fprintf(f, "    %s *data= (%s*)ptr->data;\n", dp->dnastructname, dp->dnastructname);
217                                 if(prop->type == PROP_BOOLEAN && dp->booleanbit && dp->dnaarraylength==1) {
218                                         fprintf(f, "    if(value) data->%s |= (%d<<index);\n", dp->dnaname, dp->booleanbit);
219                                         fprintf(f, "    else data->%s &= ~(%d<<index);\n", dp->dnaname, dp->booleanbit);
220                                 }
221                                 else if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
222                                         fprintf(f, "    if(value) data->%s[index] |= %d;\n", dp->dnaname, dp->booleanbit);
223                                         fprintf(f, "    else data->%s[index] &= ~%d;\n", dp->dnaname, dp->booleanbit);
224                                 }
225                                 else {
226                                         rna_clamp_value(f, prop);
227                                         fprintf(f, "    data->%s[index]= value;\n", dp->dnaname);
228                                 }
229                                 fprintf(f, "}\n\n");
230                         }
231                         else {
232                                 fprintf(f, "static void %s(PointerRNA *ptr, %s value)\n", func, rna_type_type(prop));
233                                 fprintf(f, "{\n");
234                                 fprintf(f, "    %s *data= (%s*)ptr->data;\n", dp->dnastructname, dp->dnastructname);
235                                 if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
236                                         fprintf(f, "    if(value) data->%s |= %d;\n", dp->dnaname, dp->booleanbit);
237                                         fprintf(f, "    else data->%s &= ~%d;\n", dp->dnaname, dp->booleanbit);
238                                 }
239                                 else {
240                                         rna_clamp_value(f, prop);
241                                         fprintf(f, "    data->%s= value;\n", dp->dnaname);
242                                 }
243                                 fprintf(f, "}\n\n");
244                         }
245                         break;
246         }
247
248         return func;
249 }
250
251 static char *rna_def_property_length_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp)
252 {
253         char *func= NULL;
254
255         if(prop->type == PROP_STRING) {
256                 if(!dp->dnastructname || !dp->dnaname) {
257                         fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
258                         DefRNA.error= 1;
259                         return NULL;
260                 }
261
262                 func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");
263
264                 fprintf(f, "static int %s(PointerRNA *ptr)\n", func);
265                 fprintf(f, "{\n");
266                 fprintf(f, "    %s *data= (%s*)ptr->data;\n", dp->dnastructname, dp->dnastructname);
267                 fprintf(f, "    return strlen(data->%s);\n", dp->dnaname);
268                 fprintf(f, "}\n\n");
269         }
270         else if(prop->type == PROP_COLLECTION) {
271                 if(prop->type == PROP_COLLECTION && !dp->dnalengthname) {
272                         fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
273                         DefRNA.error= 1;
274                         return NULL;
275                 }
276
277                 func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");
278
279                 fprintf(f, "static int %s(PointerRNA *ptr)\n", func);
280                 fprintf(f, "{\n");
281                 fprintf(f, "    %s *data= (%s*)ptr->data;\n", dp->dnastructname, dp->dnastructname);
282                 fprintf(f, "    return data->%s;\n", dp->dnalengthname);
283                 fprintf(f, "}\n\n");
284         }
285
286         return func;
287 }
288
289 static char *rna_def_property_begin_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp)
290 {
291         char *func;
292
293         if(!dp->dnastructname || !dp->dnaname) {
294                 fprintf(stderr, "rna_def_property_begin_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
295                 DefRNA.error= 1;
296                 return NULL;
297         }
298
299         func= rna_alloc_function_name(srna->identifier, prop->identifier, "begin");
300
301         if(dp->dnalengthname) {
302                 fprintf(f, "static void %s(CollectionPropertyIterator *iter, PointerRNA *ptr)\n", func);
303                 fprintf(f, "{\n");
304                 fprintf(f, "    %s *data= (%s*)ptr->data;\n", dp->dnastructname, dp->dnastructname);
305                 fprintf(f, "    rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), data->%s);\n", dp->dnaname, dp->dnaname, dp->dnalengthname);
306                 fprintf(f, "}\n\n");
307         }
308         else {
309                 fprintf(f, "static void %s(CollectionPropertyIterator *iter, PointerRNA *ptr)\n", func);
310                 fprintf(f, "{\n");
311                 fprintf(f, "    %s *data= (%s*)ptr->data;\n", dp->dnastructname, dp->dnastructname);
312                 fprintf(f, "    rna_iterator_listbase_begin(iter, &data->%s);\n", dp->dnaname);
313                 fprintf(f, "}\n\n");
314         }
315
316         return func;
317 }
318
319 static void rna_def_property_funcs(FILE *f, PropertyDefRNA *dp)
320 {
321         PropertyRNA *prop;
322         StructRNA *srna;
323
324         srna= dp->srna;
325         prop= dp->prop;
326
327         switch(prop->type) {
328                 case PROP_BOOLEAN: {
329                         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
330
331                         if(!prop->arraylength) {
332                                 if(!bprop->get) bprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp);
333                                 if(!bprop->set) bprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp);
334                         }
335                         else {
336                                 if(!bprop->getarray) bprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp);
337                                 if(!bprop->setarray) bprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp);
338                         }
339                         break;
340                 }
341                 case PROP_INT: {
342                         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
343
344                         if(!prop->arraylength) {
345                                 if(!iprop->get) iprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp);
346                                 if(!iprop->set) iprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp);
347                         }
348                         else {
349                                 if(!iprop->getarray) iprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp);
350                                 if(!iprop->setarray) iprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp);
351                         }
352                         break;
353                 }
354                 case PROP_FLOAT: {
355                         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
356
357                         if(!prop->arraylength) {
358                                 if(!fprop->get) fprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp);
359                                 if(!fprop->set) fprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp);
360                         }
361                         else {
362                                 if(!fprop->getarray) fprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp);
363                                 if(!fprop->setarray) fprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp);
364                         }
365                         break;
366                 }
367                 case PROP_ENUM: {
368                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
369
370                         if(!eprop->get) eprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp);
371                         if(!eprop->set) eprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp);
372                         break;
373                 }
374                 case PROP_STRING: {
375                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
376
377                         if(!sprop->get) sprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp);
378                         if(!sprop->length) sprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp);
379                         if(!sprop->set) sprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp);
380                         break;
381                 }
382                 case PROP_POINTER: {
383                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
384
385                         if(!pprop->get) pprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp);
386                         if(!pprop->set) pprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp);
387                         if(!pprop->structtype && !pprop->type) {
388                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have either type function or fixed type.\n", srna->identifier, prop->identifier);
389                                 DefRNA.error= 1;
390                         }
391                         break;
392                 }
393                 case PROP_COLLECTION: {
394                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
395
396                         if(dp->dnatype && strcmp(dp->dnatype, "ListBase")==0) {
397                                 if(!cprop->begin)
398                                         cprop->begin= (void*)rna_def_property_begin_func(f, srna, prop, dp);
399                         }
400                         else if(dp->dnalengthname) {
401                                 if(!cprop->begin)
402                                         cprop->begin= (void*)rna_def_property_begin_func(f, srna, prop, dp);
403                                 if(!cprop->length)
404                                         cprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp);
405                         }
406
407                         if(!cprop->begin) {
408                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a begin function.\n", srna->identifier, prop->identifier);
409                                 DefRNA.error= 1;
410                         }
411                         if(!cprop->next) {
412                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a next function.\n", srna->identifier, prop->identifier);
413                                 DefRNA.error= 1;
414                         }
415                         if(!cprop->get) {
416                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a get function.\n", srna->identifier, prop->identifier);
417                                 DefRNA.error= 1;
418                         }
419                         if(!cprop->structtype && !cprop->type) {
420                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have either type function or fixed type.\n", srna->identifier, prop->identifier);
421                                 DefRNA.error= 1;
422                         }
423                         break;
424                 }
425         }
426 }
427
428 static const char *rna_find_type(const char *type)
429 {
430         StructDefRNA *ds;
431
432         for(ds=DefRNA.structs.first; ds; ds=ds->next)
433                 if(ds->dnaname && strcmp(ds->dnaname, type)==0)
434                         return ds->srna->identifier;
435         
436         return NULL;
437 }
438
439 static void rna_auto_types()
440 {
441         StructDefRNA *ds;
442         PropertyDefRNA *dp;
443
444         for(ds=DefRNA.structs.first; ds; ds=ds->next) {
445                 for(dp=ds->properties.first; dp; dp=dp->next) {
446                         if(dp->dnatype) {
447                                 if(dp->prop->type == PROP_POINTER) {
448                                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
449
450                                         if(!pprop->structtype && !pprop->type)
451                                                 pprop->structtype= (StructRNA*)rna_find_type(dp->dnatype);
452                                 }
453                                 else if(dp->prop->type== PROP_COLLECTION) {
454                                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
455
456                                         if(!cprop->structtype && !cprop->type && strcmp(dp->dnatype, "ListBase")==0)
457                                                 cprop->structtype= (StructRNA*)rna_find_type(dp->dnatype);
458                                 }
459                         }
460                 }
461         }
462 }
463
464 static void rna_auto_functions(FILE *f)
465 {
466         StructDefRNA *ds;
467         PropertyDefRNA *dp;
468
469         fprintf(f, "/* Autogenerated Functions */\n\n");
470
471         for(ds=DefRNA.structs.first; ds; ds=ds->next)
472                 for(dp=ds->properties.first; dp; dp=dp->next)
473                         rna_def_property_funcs(f, dp);
474 }
475
476 static const char *rna_property_structname(PropertyType type)
477 {
478         switch(type) {
479                 case PROP_BOOLEAN: return "BooleanPropertyRNA";
480                 case PROP_INT: return "IntPropertyRNA";
481                 case PROP_FLOAT: return "FloatPropertyRNA";
482                 case PROP_STRING: return "StringPropertyRNA";
483                 case PROP_ENUM: return "EnumPropertyRNA";
484                 case PROP_POINTER: return "PointerPropertyRNA";
485                 case PROP_COLLECTION: return "CollectionPropertyRNA";
486                 default: return "UnknownPropertyRNA";
487         }
488 }
489
490 static const char *rna_property_typename(PropertyType type)
491 {
492         switch(type) {
493                 case PROP_BOOLEAN: return "PROP_BOOLEAN";
494                 case PROP_INT: return "PROP_INT";
495                 case PROP_FLOAT: return "PROP_FLOAT";
496                 case PROP_STRING: return "PROP_STRING";
497                 case PROP_ENUM: return "PROP_ENUM";
498                 case PROP_POINTER: return "PROP_POINTER";
499                 case PROP_COLLECTION: return "PROP_COLLECTION";
500                 default: return "PROP_UNKNOWN";
501         }
502 }
503
504 static const char *rna_property_subtypename(PropertyType type)
505 {
506         switch(type) {
507                 case PROP_NONE: return "PROP_NONE";
508                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
509                 case PROP_FILEPATH: return "PROP_FILEPATH";
510                 case PROP_COLOR: return "PROP_COLOR";
511                 case PROP_VECTOR: return "PROP_VECTOR";
512                 case PROP_MATRIX: return "PROP_MATRIX";
513                 default: return "PROP_UNKNOWN";
514         }
515 }
516
517 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
518 {
519         StructRNA *srna;
520
521         for(srna=brna->structs.first; srna; srna=srna->next)
522                 fprintf(f, "StructRNA RNA_%s;\n", srna->identifier);
523         fprintf(f, "\n");
524
525         fprintf(f, "BlenderRNA BLENDER_RNA = {");
526
527         srna= brna->structs.first;
528         if(srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
529         else fprintf(f, "{NULL, ");
530
531         srna= brna->structs.last;
532         if(srna) fprintf(f, "&RNA_%s}", srna->identifier);
533         else fprintf(f, "NULL}");
534
535         fprintf(f, "};\n\n");
536 }
537
538 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
539 {
540         PropertyRNA *prop;
541
542         fprintf(f, "/* %s */\n", srna->name);
543
544         if(srna->properties.first)
545                 fprintf(f, "\n");
546
547         for(prop=srna->properties.first; prop; prop=prop->next)
548                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "static ", rna_property_structname(prop->type), srna->identifier, prop->identifier);
549         fprintf(f, "\n");
550
551         for(prop=srna->properties.first; prop; prop=prop->next) {
552                 switch(prop->type) {
553                         case PROP_ENUM: {
554                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
555                                 int i;
556
557                                 if(eprop->item) {
558                                         fprintf(f, "static EnumPropertyItem rna_%s_%s_items[%d] = {", srna->identifier, prop->identifier, eprop->totitem);
559
560                                         for(i=0; i<eprop->totitem; i++) {
561                                                 fprintf(f, "{%d, ", eprop->item[i].value);
562                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
563                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, "}");
564                                                 if(i != eprop->totitem-1)
565                                                         fprintf(f, ", ");
566                                         }
567
568                                         fprintf(f, "};\n\n");
569                                 }
570                                 else {
571                                         fprintf(stderr, "rna_generate_structs: %s.%s, enum must have items defined.\n", srna->identifier, prop->identifier);
572                                         DefRNA.error= 1;
573                                 }
574                                 break;
575                         }
576                         case PROP_BOOLEAN: {
577                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
578                                 unsigned int i;
579
580                                 if(bprop->defaultarray) {
581                                         fprintf(f, "static int rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);
582
583                                         for(i=0; i<prop->arraylength; i++) {
584                                                 fprintf(f, "%d", bprop->defaultarray[i]);
585                                                 if(i != prop->arraylength-1)
586                                                         fprintf(f, ", ");
587                                         }
588
589                                         fprintf(f, "};\n\n");
590                                 }
591                                 break;
592                         }
593                         case PROP_INT: {
594                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
595                                 unsigned int i;
596
597                                 if(iprop->defaultarray) {
598                                         fprintf(f, "static int rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);
599
600                                         for(i=0; i<prop->arraylength; i++) {
601                                                 fprintf(f, "%d", iprop->defaultarray[i]);
602                                                 if(i != prop->arraylength-1)
603                                                         fprintf(f, ", ");
604                                         }
605
606                                         fprintf(f, "};\n\n");
607                                 }
608                                 break;
609                         }
610                         case PROP_FLOAT: {
611                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
612                                 unsigned int i;
613
614                                 if(fprop->defaultarray) {
615                                         fprintf(f, "static float rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);
616
617                                         for(i=0; i<prop->arraylength; i++) {
618                                                 rna_float_print(f, fprop->defaultarray[i]);
619                                                 if(i != prop->arraylength-1)
620                                                         fprintf(f, ", ");
621                                         }
622
623                                         fprintf(f, "};\n\n");
624                                 }
625                                 break;
626                         }
627                         default:
628                                 break;
629                 }
630
631                 fprintf(f, "%s%s rna_%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "static ", rna_property_structname(prop->type), srna->identifier, prop->identifier);
632
633                 if(prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->next->identifier);
634                 else fprintf(f, "\t{NULL, ");
635                 if(prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s_%s,\n", srna->identifier, prop->prev->identifier);
636                 else fprintf(f, "NULL,\n");
637                 fprintf(f, "\t%d, ", prop->magic);
638                 rna_print_c_string(f, prop->identifier);
639                 fprintf(f, ", %d, ", prop->flag);
640                 rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
641                 rna_print_c_string(f, prop->description); fprintf(f, ",\n");
642                 fprintf(f, "\t%s, %s, %d,\n", rna_property_typename(prop->type), rna_property_subtypename(prop->subtype), prop->arraylength);
643                 fprintf(f, "\t%s},\n", rna_function_string(prop->notify));
644
645                 switch(prop->type) {
646                         case PROP_BOOLEAN: {
647                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
648                                 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);
649                                 if(bprop->defaultarray) fprintf(f, "rna_%s_%s_default\n", srna->name, prop->identifier);
650                                 else fprintf(f, "NULL\n");
651                                 break;
652                         }
653                         case PROP_INT: {
654                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
655                                 fprintf(f, "\t%s, %s, %s, %s, %d, %d, %d, %d, %d,\n\t%d, \n", rna_function_string(iprop->get), rna_function_string(iprop->set), rna_function_string(iprop->getarray), rna_function_string(iprop->setarray), iprop->softmin, iprop->softmax, iprop->hardmin, iprop->hardmax, iprop->step, iprop->defaultvalue);
656                                 if(iprop->defaultarray) fprintf(f, "rna_%s_%s_default\n", srna->name, prop->identifier);
657                                 else fprintf(f, "NULL\n");
658                                 break;
659                         }
660                         case PROP_FLOAT: {
661                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
662                                 fprintf(f, "\t%s, %s, %s, %s, ", rna_function_string(fprop->get), rna_function_string(fprop->set), rna_function_string(fprop->getarray), rna_function_string(fprop->setarray));
663                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
664                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
665                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
666                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
667                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
668                                 rna_float_print(f, fprop->precision); fprintf(f, ", ");
669                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
670                                 if(fprop->defaultarray) fprintf(f, "rna_%s_%s_default\n", srna->name, prop->identifier);
671                                 else fprintf(f, "NULL\n");
672                                 break;
673                         }
674                         case PROP_STRING: {
675                                 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
676                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
677                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
678                                 break;
679                         }
680                         case PROP_ENUM: {
681                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
682                                 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);
683                                 break;
684                         }
685                         case PROP_POINTER: {
686                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
687                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(pprop->get), rna_function_string(pprop->set), rna_function_string(pprop->type));
688                                 if(pprop->structtype) fprintf(f, "&RNA_%s\n", (char*)pprop->structtype);
689                                 else fprintf(f, "NULL\n");
690                                 break;
691                         }
692                         case PROP_COLLECTION: {
693                                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
694                                 fprintf(f, "\t%s, %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->type), rna_function_string(cprop->length), rna_function_string(cprop->lookupint), rna_function_string(cprop->lookupstring));
695                                 if(cprop->structtype) fprintf(f, "&RNA_%s\n", (char*)cprop->structtype);
696                                 else fprintf(f, "NULL\n");
697                                 break;
698                         }
699                 }
700
701                 fprintf(f, "};\n\n");
702         }
703
704         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
705
706         if(srna->next) fprintf(f, "\t&RNA_%s, ", srna->next->identifier);
707         else fprintf(f, "\tNULL, ");
708         if(srna->prev) fprintf(f, "&RNA_%s,\n", srna->prev->identifier);
709         else fprintf(f, "NULL,\n");
710
711         fprintf(f, "\t");
712         rna_print_c_string(f, srna->identifier);
713         fprintf(f, ", %d, ", srna->flag);
714         rna_print_c_string(f, srna->name);
715         fprintf(f, ",\n");
716
717         prop= srna->nameproperty;
718         if(prop) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
719         else fprintf(f, "\tNULL, ");
720
721         fprintf(f, "\t(PropertyRNA*)&rna_%s_rna_properties,\n", srna->identifier);
722
723         prop= srna->properties.first;
724         if(prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
725         else fprintf(f, "\t{NULL, ");
726
727         prop= srna->properties.last;
728         if(prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}\n", srna->identifier, prop->identifier);
729         else fprintf(f, "NULL}\n");
730
731         fprintf(f, "};\n");
732
733         fprintf(f, "\n");
734 }
735
736 typedef struct RNAProcessItem {
737         char *filename;
738         void (*define)(BlenderRNA *brna);
739 } RNAProcessItem;
740
741 RNAProcessItem PROCESS_ITEMS[]= {
742         {"rna_ID.c", RNA_def_ID_types},
743         {"rna_main.c", RNA_def_main},
744         {"rna_mesh.c", RNA_def_mesh},
745         {"rna_object.c", RNA_def_object},
746         {"rna_rna.c", RNA_def_rna},
747         {"rna_scene.c", RNA_def_scene},
748         {NULL, NULL}};
749
750 static int rna_preprocess(char *basedirectory, FILE *f)
751 {
752         BlenderRNA *brna;
753         StructRNA *srna;
754         int i, status;
755         
756         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
757                         "   Do not edit manually, changes will be overwritten */\n\n"
758                         "#define RNA_RUNTIME\n\n");
759
760         brna= RNA_create();
761
762         fprintf(f, "#include <float.h>\n");
763         fprintf(f, "#include <limits.h>\n");
764         fprintf(f, "#include <string.h>\n\n");
765
766         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
767
768         fprintf(f, "#include \"BKE_utildefines.h\"\n\n");
769
770         fprintf(f, "#include \"RNA_define.h\"\n");
771         fprintf(f, "#include \"RNA_types.h\"\n");
772         fprintf(f, "#include \"rna_internal.h\"\n\n");
773
774         for(i=0; PROCESS_ITEMS[i].filename; i++)
775                 if(PROCESS_ITEMS[i].define)
776                         PROCESS_ITEMS[i].define(brna);
777         rna_auto_types();
778         
779         rna_generate_prototypes(brna, f);
780
781         for(i=0; PROCESS_ITEMS[i].filename; i++)
782                 fprintf(f, "#include \"%s\"\n", PROCESS_ITEMS[i].filename);
783         fprintf(f, "\n");
784
785         rna_auto_functions(f);
786
787         for(srna=brna->structs.first; srna; srna=srna->next)
788                 rna_generate_struct(brna, srna, f);
789         
790         status= DefRNA.error;
791
792         RNA_define_free(brna);
793         RNA_free(brna);
794
795         return status;
796 }
797
798 static void make_bad_file(char *file)
799 {
800         FILE *fp= fopen(file, "w");
801         fprintf(fp, "ERROR! Cannot make correct RNA.c file, STUPID!\n");
802         fclose(fp);
803 }
804
805 #ifndef BASE_HEADER
806 #define BASE_HEADER "../"
807 #endif
808
809 int main(int argc, char **argv)
810 {
811         FILE *file;
812         int totblock, return_status = 0;
813
814         if (argc!=2 && argc!=3) {
815                 printf("Usage: %s outfile.c [base directory]\n", argv[0]);
816                 return_status = 1;
817         }
818         else {
819                 file = fopen(argv[1], "w");
820
821                 if (!file) {
822                         printf ("Unable to open file: %s\n", argv[1]);
823                         return_status = 1;
824                 }
825                 else {
826                         char baseDirectory[256];
827
828                         printf("Running makesrna, program versions %s\n",  RNA_VERSION_DATE);
829
830                         if (argc==3)
831                                 strcpy(baseDirectory, argv[2]);
832                         else
833                                 strcpy(baseDirectory, BASE_HEADER);
834
835                         return_status= (rna_preprocess(baseDirectory, file));
836                         fclose(file);
837
838                         if(return_status) {
839                                 /* error */
840                                 make_bad_file(argv[1]);
841                                 return_status = 1;
842                         }
843                 }
844         }
845
846         totblock= MEM_get_memory_blocks_in_use();
847         if(totblock!=0) {
848                 printf("Error Totblock: %d\n",totblock);
849                 MEM_printmemlist();
850         }
851
852         return return_status;
853 }
854