8a915c264c02d31529a5d42009e99cd14308eaac
[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 (%s(data->%s & (%d<<index)) != 0);\n", (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit);
209                                         else
210                                                 fprintf(f, "    return (%s)%s((&data->%s)[index]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
211                                 }
212                                 else {
213                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit)
214                                                 fprintf(f, "    return (%s(data->%s[index] & %d) != 0);\n", (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit);
215                                         else
216                                                 fprintf(f, "    return (%s)%s(data->%s[index]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", 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 (%s((data->%s) & %d) != 0);\n", (dp->booleannegative)? "!": "", 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)%s(data->%s);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", 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(%svalue) data->%s |= (%d<<index);\n", (dp->booleannegative)? "!": "", 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]= %svalue;\n", dp->dnaname, (dp->booleannegative)? "!": "");
322                                         }
323                                 }
324                                 else {
325                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
326                                                 fprintf(f, "    if(%svalue) data->%s[index] |= %d;\n", (dp->booleannegative)? "!": "", 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]= %svalue;\n", dp->dnaname, (dp->booleannegative)? "!": "");
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(%svalue) data->%s |= %d;\n", (dp->booleannegative)? "!": "", 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= %svalue;\n", dp->dnaname, (dp->booleannegative)? "!": "");
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(!(prop->flag & PROP_IDPROPERTY)) {
529                                 if(!cprop->begin) {
530                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a begin function.\n", srna->identifier, prop->identifier);
531                                         DefRNA.error= 1;
532                                 }
533                                 if(!cprop->next) {
534                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a next function.\n", srna->identifier, prop->identifier);
535                                         DefRNA.error= 1;
536                                 }
537                                 if(!cprop->get) {
538                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a get function.\n", srna->identifier, prop->identifier);
539                                         DefRNA.error= 1;
540                                 }
541                         }
542                         if(!cprop->structtype && !cprop->type) {
543                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have either type function or fixed type.\n", srna->identifier, prop->identifier);
544                                 DefRNA.error= 1;
545                         }
546                         break;
547                 }
548         }
549 }
550
551 static const char *rna_find_type(const char *type)
552 {
553         StructDefRNA *ds;
554
555         for(ds=DefRNA.structs.first; ds; ds=ds->next)
556                 if(ds->dnaname && strcmp(ds->dnaname, type)==0)
557                         return ds->srna->identifier;
558         
559         return NULL;
560 }
561
562 static void rna_auto_types()
563 {
564         StructDefRNA *ds;
565         PropertyDefRNA *dp;
566
567         for(ds=DefRNA.structs.first; ds; ds=ds->next) {
568                 for(dp=ds->properties.first; dp; dp=dp->next) {
569                         if(dp->dnatype) {
570                                 if(dp->prop->type == PROP_POINTER) {
571                                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
572
573                                         if(!pprop->structtype && !pprop->type)
574                                                 pprop->structtype= (StructRNA*)rna_find_type(dp->dnatype);
575                                 }
576                                 else if(dp->prop->type== PROP_COLLECTION) {
577                                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
578
579                                         if(!cprop->structtype && !cprop->type && strcmp(dp->dnatype, "ListBase")==0)
580                                                 cprop->structtype= (StructRNA*)rna_find_type(dp->dnatype);
581                                 }
582                         }
583                 }
584         }
585 }
586
587 static void rna_auto_functions(FILE *f)
588 {
589         StructDefRNA *ds;
590         PropertyDefRNA *dp;
591
592         fprintf(f, "/* Autogenerated Functions */\n\n");
593
594         for(ds=DefRNA.structs.first; ds; ds=ds->next)
595                 for(dp=ds->properties.first; dp; dp=dp->next)
596                         rna_def_property_funcs(f, dp);
597 }
598
599 static void rna_sort(BlenderRNA *brna)
600 {
601         StructRNA *srna;
602
603         rna_sortlist(&brna->structs, cmp_struct);
604
605         for(srna=brna->structs.first; srna; srna=srna->next)
606                 rna_sortlist(&srna->properties, cmp_property);
607 }
608
609 static const char *rna_property_structname(PropertyType type)
610 {
611         switch(type) {
612                 case PROP_BOOLEAN: return "BooleanPropertyRNA";
613                 case PROP_INT: return "IntPropertyRNA";
614                 case PROP_FLOAT: return "FloatPropertyRNA";
615                 case PROP_STRING: return "StringPropertyRNA";
616                 case PROP_ENUM: return "EnumPropertyRNA";
617                 case PROP_POINTER: return "PointerPropertyRNA";
618                 case PROP_COLLECTION: return "CollectionPropertyRNA";
619                 default: return "UnknownPropertyRNA";
620         }
621 }
622
623 static const char *rna_property_typename(PropertyType type)
624 {
625         switch(type) {
626                 case PROP_BOOLEAN: return "PROP_BOOLEAN";
627                 case PROP_INT: return "PROP_INT";
628                 case PROP_FLOAT: return "PROP_FLOAT";
629                 case PROP_STRING: return "PROP_STRING";
630                 case PROP_ENUM: return "PROP_ENUM";
631                 case PROP_POINTER: return "PROP_POINTER";
632                 case PROP_COLLECTION: return "PROP_COLLECTION";
633                 default: return "PROP_UNKNOWN";
634         }
635 }
636
637 static const char *rna_property_subtypename(PropertyType type)
638 {
639         switch(type) {
640                 case PROP_NONE: return "PROP_NONE";
641                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
642                 case PROP_FILEPATH: return "PROP_FILEPATH";
643                 case PROP_DIRPATH: return "PROP_DIRPATH";
644                 case PROP_COLOR: return "PROP_COLOR";
645                 case PROP_VECTOR: return "PROP_VECTOR";
646                 case PROP_MATRIX: return "PROP_MATRIX";
647                 case PROP_ROTATION: return "PROP_ROTATION";
648                 default: return "PROP_UNKNOWN";
649         }
650 }
651
652 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
653 {
654         StructRNA *srna;
655
656         for(srna=brna->structs.first; srna; srna=srna->next)
657                 fprintf(f, "StructRNA RNA_%s;\n", srna->identifier);
658         fprintf(f, "\n");
659
660         fprintf(f, "BlenderRNA BLENDER_RNA = {");
661
662         srna= brna->structs.first;
663         if(srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
664         else fprintf(f, "{NULL, ");
665
666         srna= brna->structs.last;
667         if(srna) fprintf(f, "&RNA_%s}", srna->identifier);
668         else fprintf(f, "NULL}");
669
670         fprintf(f, "};\n\n");
671 }
672
673 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
674 {
675         PropertyRNA *prop;
676
677         fprintf(f, "/* %s */\n", srna->name);
678
679         if(srna->properties.first)
680                 fprintf(f, "\n");
681
682         for(prop=srna->properties.first; prop; prop=prop->next)
683                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "static ", rna_property_structname(prop->type), srna->identifier, prop->identifier);
684         fprintf(f, "\n");
685
686         for(prop=srna->properties.first; prop; prop=prop->next) {
687                 switch(prop->type) {
688                         case PROP_ENUM: {
689                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
690                                 int i;
691
692                                 if(eprop->item) {
693                                         fprintf(f, "static EnumPropertyItem rna_%s_%s_items[%d] = {", srna->identifier, prop->identifier, eprop->totitem);
694
695                                         for(i=0; i<eprop->totitem; i++) {
696                                                 fprintf(f, "{%d, ", eprop->item[i].value);
697                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
698                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
699                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "}");
700                                                 if(i != eprop->totitem-1)
701                                                         fprintf(f, ", ");
702                                         }
703
704                                         fprintf(f, "};\n\n");
705                                 }
706                                 else {
707                                         fprintf(stderr, "rna_generate_structs: %s.%s, enum must have items defined.\n", srna->identifier, prop->identifier);
708                                         DefRNA.error= 1;
709                                 }
710                                 break;
711                         }
712                         case PROP_BOOLEAN: {
713                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)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(bprop->defaultarray)
721                                                         fprintf(f, "%d", bprop->defaultarray[i]);
722                                                 else
723                                                         fprintf(f, "%d", bprop->defaultvalue);
724                                                 if(i != prop->arraylength-1)
725                                                         fprintf(f, ", ");
726                                         }
727
728                                         fprintf(f, "};\n\n");
729                                 }
730                                 break;
731                         }
732                         case PROP_INT: {
733                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
734                                 unsigned int i;
735
736                                 if(prop->arraylength) {
737                                         fprintf(f, "static int rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);
738
739                                         for(i=0; i<prop->arraylength; i++) {
740                                                 if(iprop->defaultarray)
741                                                         fprintf(f, "%d", iprop->defaultarray[i]);
742                                                 else
743                                                         fprintf(f, "%d", iprop->defaultvalue);
744                                                 if(i != prop->arraylength-1)
745                                                         fprintf(f, ", ");
746                                         }
747
748                                         fprintf(f, "};\n\n");
749                                 }
750                                 break;
751                         }
752                         case PROP_FLOAT: {
753                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
754                                 unsigned int i;
755
756                                 if(prop->arraylength) {
757                                         fprintf(f, "static float rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);
758
759                                         for(i=0; i<prop->arraylength; i++) {
760                                                 if(fprop->defaultarray)
761                                                         rna_float_print(f, fprop->defaultarray[i]);
762                                                 else
763                                                         rna_float_print(f, fprop->defaultvalue);
764                                                 if(i != prop->arraylength-1)
765                                                         fprintf(f, ", ");
766                                         }
767
768                                         fprintf(f, "};\n\n");
769                                 }
770                                 break;
771                         }
772                         default:
773                                 break;
774                 }
775
776                 fprintf(f, "%s%s rna_%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "static ", rna_property_structname(prop->type), srna->identifier, prop->identifier);
777
778                 if(prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->next->identifier);
779                 else fprintf(f, "\t{NULL, ");
780                 if(prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s_%s,\n", srna->identifier, prop->prev->identifier);
781                 else fprintf(f, "NULL,\n");
782                 fprintf(f, "\t%d, ", prop->magic);
783                 rna_print_c_string(f, prop->identifier);
784                 fprintf(f, ", %d, ", prop->flag);
785                 rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
786                 rna_print_c_string(f, prop->description); fprintf(f, ",\n");
787                 fprintf(f, "\t%s, %s, %d,\n", rna_property_typename(prop->type), rna_property_subtypename(prop->subtype), prop->arraylength);
788                 fprintf(f, "\t%s, %d, %s},\n", rna_function_string(prop->update), prop->noteflag, rna_function_string(prop->editable));
789
790                 switch(prop->type) {
791                         case PROP_BOOLEAN: {
792                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
793                                 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);
794                                 if(prop->arraylength) fprintf(f, "rna_%s_%s_default\n", srna->identifier, prop->identifier);
795                                 else fprintf(f, "NULL\n");
796                                 break;
797                         }
798                         case PROP_INT: {
799                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
800                                 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));
801                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
802                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
803                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
804                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
805                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
806                                 rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
807                                 if(prop->arraylength) fprintf(f, "rna_%s_%s_default\n", srna->identifier, prop->identifier);
808                                 else fprintf(f, "NULL\n");
809                                 break;
810                         }
811                         case PROP_FLOAT: {
812                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
813                                 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));
814                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
815                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
816                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
817                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
818                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
819                                 rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
820                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
821                                 if(prop->arraylength) fprintf(f, "rna_%s_%s_default\n", srna->identifier, prop->identifier);
822                                 else fprintf(f, "NULL\n");
823                                 break;
824                         }
825                         case PROP_STRING: {
826                                 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
827                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
828                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
829                                 break;
830                         }
831                         case PROP_ENUM: {
832                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
833                                 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);
834                                 break;
835                         }
836                         case PROP_POINTER: {
837                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
838                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(pprop->get), rna_function_string(pprop->set), rna_function_string(pprop->type));
839                                 if(pprop->structtype) fprintf(f, "&RNA_%s\n", (char*)pprop->structtype);
840                                 else fprintf(f, "NULL\n");
841                                 break;
842                         }
843                         case PROP_COLLECTION: {
844                                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
845                                 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));
846                                 if(cprop->structtype) fprintf(f, "&RNA_%s\n", (char*)cprop->structtype);
847                                 else fprintf(f, "NULL\n");
848                                 break;
849                         }
850                 }
851
852                 fprintf(f, "};\n\n");
853         }
854
855         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
856
857         if(srna->next) fprintf(f, "\t&RNA_%s, ", srna->next->identifier);
858         else fprintf(f, "\tNULL, ");
859         if(srna->prev) fprintf(f, "&RNA_%s,\n", srna->prev->identifier);
860         else fprintf(f, "NULL,\n");
861
862         fprintf(f, "\t");
863         rna_print_c_string(f, srna->identifier);
864         fprintf(f, ", %d, ", srna->flag);
865         rna_print_c_string(f, srna->name);
866         fprintf(f, ", ");
867         rna_print_c_string(f, srna->description);
868         fprintf(f, ",\n");
869
870         prop= srna->nameproperty;
871         if(prop) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
872         else fprintf(f, "\tNULL, ");
873
874         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", srna->identifier);
875
876         if(srna->from) fprintf(f, "\t&RNA_%s,\n", (char*)srna->from);
877         else fprintf(f, "\tNULL,\n");
878
879         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
880
881         prop= srna->properties.first;
882         if(prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
883         else fprintf(f, "\t{NULL, ");
884
885         prop= srna->properties.last;
886         if(prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}\n", srna->identifier, prop->identifier);
887         else fprintf(f, "NULL}\n");
888
889         fprintf(f, "};\n");
890
891         fprintf(f, "\n");
892 }
893
894 typedef struct RNAProcessItem {
895         char *filename;
896         void (*define)(BlenderRNA *brna);
897 } RNAProcessItem;
898
899 RNAProcessItem PROCESS_ITEMS[]= {
900         {"rna_ID.c", RNA_def_ID},
901         {"rna_actuator.c", RNA_def_actuator},
902         {"rna_armature.c", RNA_def_armature},
903         {"rna_brush.c", RNA_def_brush},
904         {"rna_camera.c", RNA_def_camera},
905         {"rna_color.c", RNA_def_color},
906         {"rna_constraint.c", RNA_def_constraint},
907         {"rna_controller.c", RNA_def_controller},
908         {"rna_curve.c", RNA_def_curve},
909         {"rna_fluidsim.c", RNA_def_fluidsim},
910         {"rna_group.c", RNA_def_group},
911         {"rna_image.c", RNA_def_image},
912         {"rna_ipo.c", RNA_def_ipo},
913         {"rna_key.c", RNA_def_key},
914         {"rna_lamp.c", RNA_def_lamp},
915         {"rna_lattice.c", RNA_def_lattice},
916         {"rna_main.c", RNA_def_main},
917         {"rna_material.c", RNA_def_material},
918         {"rna_mesh.c", RNA_def_mesh},
919         {"rna_meta.c", RNA_def_meta},
920         {"rna_modifier.c", RNA_def_modifier},
921         {"rna_nodetree.c", RNA_def_nodetree},
922         {"rna_object.c", RNA_def_object},
923         {"rna_object_force.c", RNA_def_object_force},
924         {"rna_packedfile.c", RNA_def_packedfile},
925         {"rna_particle.c", RNA_def_particle},
926         {"rna_property.c", RNA_def_gameproperty},
927         {"rna_radio.c", RNA_def_radio},
928         {"rna_rna.c", RNA_def_rna},
929         {"rna_scene.c", RNA_def_scene},
930         {"rna_screen.c", RNA_def_screen},
931         {"rna_sensor.c", RNA_def_sensor},
932         {"rna_sequence.c", RNA_def_sequence},
933         {"rna_text.c", RNA_def_text},
934         {"rna_sound.c", RNA_def_sound},
935         {"rna_userdef.c", RNA_def_userdef},
936         {"rna_vfont.c", RNA_def_vfont},
937         {"rna_wm.c", RNA_def_wm},
938         {"rna_world.c", RNA_def_world}, 
939         {NULL, NULL}};
940
941 static int rna_preprocess(char *basedirectory, FILE *f)
942 {
943         BlenderRNA *brna;
944         StructRNA *srna;
945         int i, status;
946         
947         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
948                         "   Do not edit manually, changes will be overwritten */\n\n"
949                         "#define RNA_RUNTIME\n\n");
950
951         brna= RNA_create();
952
953         fprintf(f, "#include <float.h>\n");
954         fprintf(f, "#include <limits.h>\n");
955         fprintf(f, "#include <string.h>\n\n");
956
957         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
958
959         fprintf(f, "#include \"BKE_utildefines.h\"\n\n");
960
961         fprintf(f, "#include \"RNA_define.h\"\n");
962         fprintf(f, "#include \"RNA_types.h\"\n");
963         fprintf(f, "#include \"rna_internal.h\"\n\n");
964
965         /* this is ugly, but we cannot have c files compiled for both
966          * makesrna and blender with some build systems at the moment */
967         fprintf(f, "#include \"rna_define.c\"\n\n");
968
969         for(i=0; PROCESS_ITEMS[i].filename; i++)
970                 if(PROCESS_ITEMS[i].define)
971                         PROCESS_ITEMS[i].define(brna);
972
973         rna_sort(brna);
974         rna_auto_types();
975         
976         rna_generate_prototypes(brna, f);
977
978         for(i=0; PROCESS_ITEMS[i].filename; i++)
979                 fprintf(f, "#include \"%s\"\n", PROCESS_ITEMS[i].filename);
980         fprintf(f, "\n");
981
982         rna_auto_functions(f);
983
984         for(srna=brna->structs.first; srna; srna=srna->next)
985                 rna_generate_struct(brna, srna, f);
986         
987         status= DefRNA.error;
988
989         RNA_define_free(brna);
990         RNA_free(brna);
991
992         return status;
993 }
994
995 static void make_bad_file(char *file)
996 {
997         FILE *fp= fopen(file, "w");
998         fprintf(fp, "ERROR! Cannot make correct RNA.c file, STUPID!\n");
999         fclose(fp);
1000 }
1001
1002 #ifndef BASE_HEADER
1003 #define BASE_HEADER "../"
1004 #endif
1005
1006 int main(int argc, char **argv)
1007 {
1008         FILE *file;
1009         int totblock, return_status = 0;
1010
1011         if (argc!=2 && argc!=3) {
1012                 printf("Usage: %s outfile.c [base directory]\n", argv[0]);
1013                 return_status = 1;
1014         }
1015         else {
1016                 file = fopen(argv[1], "w");
1017
1018                 if (!file) {
1019                         printf ("Unable to open file: %s\n", argv[1]);
1020                         return_status = 1;
1021                 }
1022                 else {
1023                         char baseDirectory[256];
1024
1025                         printf("Running makesrna, program versions %s\n",  RNA_VERSION_DATE);
1026
1027                         if (argc==3)
1028                                 strcpy(baseDirectory, argv[2]);
1029                         else
1030                                 strcpy(baseDirectory, BASE_HEADER);
1031
1032                         return_status= (rna_preprocess(baseDirectory, file));
1033                         fclose(file);
1034
1035                         if(return_status) {
1036                                 /* error */
1037                                 make_bad_file(argv[1]);
1038                                 return_status = 1;
1039                         }
1040                 }
1041         }
1042
1043         totblock= MEM_get_memory_blocks_in_use();
1044         if(totblock!=0) {
1045                 printf("Error Totblock: %d\n",totblock);
1046                 MEM_printmemlist();
1047         }
1048
1049         return return_status;
1050 }
1051
1052