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