RNA:
[blender.git] / source / blender / makesrna / intern / makesrna.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Contributor(s): Blender Foundation (2008).
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include <float.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "RNA_access.h"
34 #include "RNA_define.h"
35 #include "RNA_types.h"
36
37 #include "rna_internal.h"
38
39 #define RNA_VERSION_DATE "$Id$"
40
41 #ifdef _WIN32
42 #ifndef snprintf
43 #define snprintf _snprintf
44 #endif
45 #endif
46
47 /* Sorting */
48
49 int cmp_struct(const void *a, const void *b)
50 {
51         const StructRNA *structa= *(const StructRNA**)a;
52         const StructRNA *structb= *(const StructRNA**)b;
53
54         return strcmp(structa->identifier, structb->identifier);
55 }
56
57 int cmp_property(const void *a, const void *b)
58 {
59         const PropertyRNA *propa= *(const PropertyRNA**)a;
60         const PropertyRNA *propb= *(const PropertyRNA**)b;
61
62         if(strcmp(propa->identifier, "rna_type") == 0) return -1;
63         else if(strcmp(propb->identifier, "rna_type") == 0) return 1;
64
65         if(strcmp(propa->identifier, "name") == 0) return -1;
66         else if(strcmp(propb->identifier, "name") == 0) return 1;
67
68         return strcmp(propa->name, propb->name);
69 }
70
71 void rna_sortlist(ListBase *listbase, int(*cmp)(const void*, const void*))
72 {
73         Link *link;
74         void **array;
75         int a, size;
76         
77         if(listbase->first == listbase->last)
78                 return;
79
80         for(size=0, link=listbase->first; link; link=link->next)
81                 size++;
82
83         array= MEM_mallocN(sizeof(void*)*size, "rna_sortlist");
84         for(a=0, link=listbase->first; link; link=link->next, a++)
85                 array[a]= link;
86
87         qsort(array, size, sizeof(void*), cmp);
88
89         listbase->first= listbase->last= NULL;
90         for(a=0; a<size; a++) {
91                 link= array[a];
92                 link->next= link->prev= NULL;
93                 rna_addtail(listbase, link);
94         }
95
96         MEM_freeN(array);
97 }
98
99 /* Preprocessing */
100
101 static void rna_print_c_string(FILE *f, const char *str)
102 {
103         static char *escape[] = {"\''", "\"\"", "\??", "\\\\","\aa", "\bb", "\ff", "\nn", "\rr", "\tt", "\vv", NULL};
104         int i, j;
105
106         fprintf(f, "\"");
107         for(i=0; str[i]; i++) {
108                 for(j=0; escape[j]; j++)
109                         if(str[i] == escape[j][0])
110                                 break;
111
112                 if(escape[j]) fprintf(f, "\\%c", escape[j][1]);
113                 else fprintf(f, "%c", str[i]);
114         }
115         fprintf(f, "\"");
116 }
117
118 static 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(!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_DIRPATH: return "PROP_DIRPATH";
642                 case PROP_COLOR: return "PROP_COLOR";
643                 case PROP_VECTOR: return "PROP_VECTOR";
644                 case PROP_MATRIX: return "PROP_MATRIX";
645                 case PROP_ROTATION: return "PROP_ROTATION";
646                 default: return "PROP_UNKNOWN";
647         }
648 }
649
650 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
651 {
652         StructRNA *srna;
653
654         for(srna=brna->structs.first; srna; srna=srna->next)
655                 fprintf(f, "StructRNA RNA_%s;\n", srna->identifier);
656         fprintf(f, "\n");
657
658         fprintf(f, "BlenderRNA BLENDER_RNA = {");
659
660         srna= brna->structs.first;
661         if(srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
662         else fprintf(f, "{NULL, ");
663
664         srna= brna->structs.last;
665         if(srna) fprintf(f, "&RNA_%s}", srna->identifier);
666         else fprintf(f, "NULL}");
667
668         fprintf(f, "};\n\n");
669 }
670
671 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
672 {
673         PropertyRNA *prop;
674
675         fprintf(f, "/* %s */\n", srna->name);
676
677         if(srna->properties.first)
678                 fprintf(f, "\n");
679
680         for(prop=srna->properties.first; prop; prop=prop->next)
681                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "static ", rna_property_structname(prop->type), srna->identifier, prop->identifier);
682         fprintf(f, "\n");
683
684         for(prop=srna->properties.first; prop; prop=prop->next) {
685                 switch(prop->type) {
686                         case PROP_ENUM: {
687                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
688                                 int i;
689
690                                 if(eprop->item) {
691                                         fprintf(f, "static EnumPropertyItem rna_%s_%s_items[%d] = {", srna->identifier, prop->identifier, eprop->totitem);
692
693                                         for(i=0; i<eprop->totitem; i++) {
694                                                 fprintf(f, "{%d, ", eprop->item[i].value);
695                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
696                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
697                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "}");
698                                                 if(i != eprop->totitem-1)
699                                                         fprintf(f, ", ");
700                                         }
701
702                                         fprintf(f, "};\n\n");
703                                 }
704                                 else {
705                                         fprintf(stderr, "rna_generate_structs: %s.%s, enum must have items defined.\n", srna->identifier, prop->identifier);
706                                         DefRNA.error= 1;
707                                 }
708                                 break;
709                         }
710                         case PROP_BOOLEAN: {
711                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
712                                 unsigned int i;
713
714                                 if(prop->arraylength) {
715                                         fprintf(f, "static int rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);
716
717                                         for(i=0; i<prop->arraylength; i++) {
718                                                 if(bprop->defaultarray)
719                                                         fprintf(f, "%d", bprop->defaultarray[i]);
720                                                 else
721                                                         fprintf(f, "%d", bprop->defaultvalue);
722                                                 if(i != prop->arraylength-1)
723                                                         fprintf(f, ", ");
724                                         }
725
726                                         fprintf(f, "};\n\n");
727                                 }
728                                 break;
729                         }
730                         case PROP_INT: {
731                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
732                                 unsigned int i;
733
734                                 if(prop->arraylength) {
735                                         fprintf(f, "static int rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);
736
737                                         for(i=0; i<prop->arraylength; i++) {
738                                                 if(iprop->defaultarray)
739                                                         fprintf(f, "%d", iprop->defaultarray[i]);
740                                                 else
741                                                         fprintf(f, "%d", iprop->defaultvalue);
742                                                 if(i != prop->arraylength-1)
743                                                         fprintf(f, ", ");
744                                         }
745
746                                         fprintf(f, "};\n\n");
747                                 }
748                                 break;
749                         }
750                         case PROP_FLOAT: {
751                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
752                                 unsigned int i;
753
754                                 if(prop->arraylength) {
755                                         fprintf(f, "static float rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);
756
757                                         for(i=0; i<prop->arraylength; i++) {
758                                                 if(fprop->defaultarray)
759                                                         rna_float_print(f, fprop->defaultarray[i]);
760                                                 else
761                                                         rna_float_print(f, fprop->defaultvalue);
762                                                 if(i != prop->arraylength-1)
763                                                         fprintf(f, ", ");
764                                         }
765
766                                         fprintf(f, "};\n\n");
767                                 }
768                                 break;
769                         }
770                         default:
771                                 break;
772                 }
773
774                 fprintf(f, "%s%s rna_%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "static ", rna_property_structname(prop->type), srna->identifier, prop->identifier);
775
776                 if(prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->next->identifier);
777                 else fprintf(f, "\t{NULL, ");
778                 if(prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s_%s,\n", srna->identifier, prop->prev->identifier);
779                 else fprintf(f, "NULL,\n");
780                 fprintf(f, "\t%d, ", prop->magic);
781                 rna_print_c_string(f, prop->identifier);
782                 fprintf(f, ", %d, ", prop->flag);
783                 rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
784                 rna_print_c_string(f, prop->description); fprintf(f, ",\n");
785                 fprintf(f, "\t%s, %s, %d,\n", rna_property_typename(prop->type), rna_property_subtypename(prop->subtype), prop->arraylength);
786                 fprintf(f, "\t%s, %s},\n", rna_function_string(prop->notify), rna_function_string(prop->editable));
787
788                 switch(prop->type) {
789                         case PROP_BOOLEAN: {
790                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
791                                 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);
792                                 if(prop->arraylength) fprintf(f, "rna_%s_%s_default\n", srna->identifier, prop->identifier);
793                                 else fprintf(f, "NULL\n");
794                                 break;
795                         }
796                         case PROP_INT: {
797                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
798                                 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));
799                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
800                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
801                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
802                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
803                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
804                                 rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
805                                 if(prop->arraylength) fprintf(f, "rna_%s_%s_default\n", srna->identifier, prop->identifier);
806                                 else fprintf(f, "NULL\n");
807                                 break;
808                         }
809                         case PROP_FLOAT: {
810                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
811                                 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));
812                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
813                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
814                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
815                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
816                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
817                                 rna_float_print(f, fprop->precision); fprintf(f, ", ");
818                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
819                                 if(prop->arraylength) fprintf(f, "rna_%s_%s_default\n", srna->identifier, prop->identifier);
820                                 else fprintf(f, "NULL\n");
821                                 break;
822                         }
823                         case PROP_STRING: {
824                                 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
825                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
826                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
827                                 break;
828                         }
829                         case PROP_ENUM: {
830                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
831                                 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);
832                                 break;
833                         }
834                         case PROP_POINTER: {
835                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
836                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(pprop->get), rna_function_string(pprop->set), rna_function_string(pprop->type));
837                                 if(pprop->structtype) fprintf(f, "&RNA_%s\n", (char*)pprop->structtype);
838                                 else fprintf(f, "NULL\n");
839                                 break;
840                         }
841                         case PROP_COLLECTION: {
842                                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
843                                 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));
844                                 if(cprop->structtype) fprintf(f, "&RNA_%s\n", (char*)cprop->structtype);
845                                 else fprintf(f, "NULL\n");
846                                 break;
847                         }
848                 }
849
850                 fprintf(f, "};\n\n");
851         }
852
853         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
854
855         if(srna->next) fprintf(f, "\t&RNA_%s, ", srna->next->identifier);
856         else fprintf(f, "\tNULL, ");
857         if(srna->prev) fprintf(f, "&RNA_%s,\n", srna->prev->identifier);
858         else fprintf(f, "NULL,\n");
859
860         fprintf(f, "\t");
861         rna_print_c_string(f, srna->identifier);
862         fprintf(f, ", %d, ", srna->flag);
863         rna_print_c_string(f, srna->name);
864         fprintf(f, ",\n");
865
866         prop= srna->nameproperty;
867         if(prop) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
868         else fprintf(f, "\tNULL, ");
869
870         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", srna->identifier);
871
872         if(srna->from) fprintf(f, "\t&RNA_%s,\n", (char*)srna->from);
873         else fprintf(f, "\tNULL,\n");
874
875         fprintf(f, "\t%s, %s,\n", rna_function_string(srna->notify), rna_function_string(srna->refine));
876
877         prop= srna->properties.first;
878         if(prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
879         else fprintf(f, "\t{NULL, ");
880
881         prop= srna->properties.last;
882         if(prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}\n", srna->identifier, prop->identifier);
883         else fprintf(f, "NULL}\n");
884
885         fprintf(f, "};\n");
886
887         fprintf(f, "\n");
888 }
889
890 typedef struct RNAProcessItem {
891         char *filename;
892         void (*define)(BlenderRNA *brna);
893 } RNAProcessItem;
894
895 RNAProcessItem PROCESS_ITEMS[]= {
896         {"rna_ID.c", RNA_def_ID},
897         {"rna_actuator.c", RNA_def_actuator},
898         {"rna_armature.c", RNA_def_armature},
899         {"rna_brush.c", RNA_def_brush},
900         {"rna_camera.c", RNA_def_camera},
901         {"rna_color.c", RNA_def_color},
902         {"rna_constraint.c", RNA_def_constraint},
903         {"rna_controller.c", RNA_def_controller},
904         {"rna_curve.c", RNA_def_curve},
905         {"rna_group.c", RNA_def_group},
906         {"rna_image.c", RNA_def_image},
907         {"rna_ipo.c", RNA_def_ipo},
908         {"rna_key.c", RNA_def_key},
909         {"rna_lamp.c", RNA_def_lamp},
910         {"rna_lattice.c", RNA_def_lattice},
911         {"rna_main.c", RNA_def_main},
912         {"rna_material.c", RNA_def_material},
913         {"rna_mesh.c", RNA_def_mesh},
914         {"rna_meta.c", RNA_def_meta},
915         {"rna_modifier.c", RNA_def_modifier},
916         {"rna_nodetree.c", RNA_def_nodetree},
917         {"rna_object.c", RNA_def_object},
918         {"rna_packedfile.c", RNA_def_packedfile},
919         {"rna_property.c", RNA_def_gameproperty},
920         {"rna_radio.c", RNA_def_radio},
921         {"rna_rna.c", RNA_def_rna},
922         {"rna_scene.c", RNA_def_scene},
923         {"rna_screen.c", RNA_def_screen},
924         {"rna_sensor.c", RNA_def_sensor},
925         {"rna_sequence.c", RNA_def_sequence},
926         {"rna_vfont.c", RNA_def_vfont},
927         {"rna_wm.c", RNA_def_wm},
928         {"rna_world.c", RNA_def_world}, 
929         {NULL, NULL}};
930
931 static int rna_preprocess(char *basedirectory, FILE *f)
932 {
933         BlenderRNA *brna;
934         StructRNA *srna;
935         int i, status;
936         
937         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
938                         "   Do not edit manually, changes will be overwritten */\n\n"
939                         "#define RNA_RUNTIME\n\n");
940
941         brna= RNA_create();
942
943         fprintf(f, "#include <float.h>\n");
944         fprintf(f, "#include <limits.h>\n");
945         fprintf(f, "#include <string.h>\n\n");
946
947         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
948
949         fprintf(f, "#include \"BKE_utildefines.h\"\n\n");
950
951         fprintf(f, "#include \"RNA_define.h\"\n");
952         fprintf(f, "#include \"RNA_types.h\"\n");
953         fprintf(f, "#include \"rna_internal.h\"\n\n");
954
955         /* this is ugly, but we cannot have c files compiled for both
956          * makesrna and blender with some build systems at the moment */
957         fprintf(f, "#include \"rna_define.c\"\n\n");
958
959         for(i=0; PROCESS_ITEMS[i].filename; i++)
960                 if(PROCESS_ITEMS[i].define)
961                         PROCESS_ITEMS[i].define(brna);
962
963         rna_sort(brna);
964         rna_auto_types();
965         
966         rna_generate_prototypes(brna, f);
967
968         for(i=0; PROCESS_ITEMS[i].filename; i++)
969                 fprintf(f, "#include \"%s\"\n", PROCESS_ITEMS[i].filename);
970         fprintf(f, "\n");
971
972         rna_auto_functions(f);
973
974         for(srna=brna->structs.first; srna; srna=srna->next)
975                 rna_generate_struct(brna, srna, f);
976         
977         status= DefRNA.error;
978
979         RNA_define_free(brna);
980         RNA_free(brna);
981
982         return status;
983 }
984
985 static void make_bad_file(char *file)
986 {
987         FILE *fp= fopen(file, "w");
988         fprintf(fp, "ERROR! Cannot make correct RNA.c file, STUPID!\n");
989         fclose(fp);
990 }
991
992 #ifndef BASE_HEADER
993 #define BASE_HEADER "../"
994 #endif
995
996 int main(int argc, char **argv)
997 {
998         FILE *file;
999         int totblock, return_status = 0;
1000
1001         if (argc!=2 && argc!=3) {
1002                 printf("Usage: %s outfile.c [base directory]\n", argv[0]);
1003                 return_status = 1;
1004         }
1005         else {
1006                 file = fopen(argv[1], "w");
1007
1008                 if (!file) {
1009                         printf ("Unable to open file: %s\n", argv[1]);
1010                         return_status = 1;
1011                 }
1012                 else {
1013                         char baseDirectory[256];
1014
1015                         printf("Running makesrna, program versions %s\n",  RNA_VERSION_DATE);
1016
1017                         if (argc==3)
1018                                 strcpy(baseDirectory, argv[2]);
1019                         else
1020                                 strcpy(baseDirectory, BASE_HEADER);
1021
1022                         return_status= (rna_preprocess(baseDirectory, file));
1023                         fclose(file);
1024
1025                         if(return_status) {
1026                                 /* error */
1027                                 make_bad_file(argv[1]);
1028                                 return_status = 1;
1029                         }
1030                 }
1031         }
1032
1033         totblock= MEM_get_memory_blocks_in_use();
1034         if(totblock!=0) {
1035                 printf("Error Totblock: %d\n",totblock);
1036                 MEM_printmemlist();
1037         }
1038
1039         return return_status;
1040 }
1041
1042