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