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