rna support for passing dynamic sized arrays to rna functions
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /* Replace if different */
48 #define TMP_EXT ".tmp"
49
50 static int replace_if_different(char *tmpfile)
51 {
52         // return 0; // use for testing had edited rna
53
54 #define REN_IF_DIFF \
55         remove(orgfile); \
56         if(rename(tmpfile, orgfile) != 0) { \
57                 fprintf(stderr, "%s:%d, rename error: \"%s\" -> \"%s\"\n", __FILE__, __LINE__, tmpfile, orgfile); \
58                 return -1; \
59         } \
60         remove(tmpfile); \
61         return 1; \
62 /* end REN_IF_DIFF */
63
64
65         FILE *fp_new, *fp_org;
66         int len_new, len_org;
67         char *arr_new, *arr_org;
68         int cmp;
69
70         char orgfile[4096];
71
72         strcpy(orgfile, tmpfile);
73         orgfile[strlen(orgfile) - strlen(TMP_EXT)] = '\0'; /* strip '.tmp' */
74
75         fp_org= fopen(orgfile, "rb");
76
77         if(fp_org==NULL) {
78                 REN_IF_DIFF;
79         }
80
81         fp_new= fopen(tmpfile, "rb");
82
83         if(fp_new==NULL) {
84                 /* shouldn't happen, just to be safe */
85                 fprintf(stderr, "%s:%d, open error: \"%s\"\n", __FILE__, __LINE__, tmpfile);
86                 fclose(fp_org);
87                 return -1;
88         }
89
90         fseek(fp_new, 0L, SEEK_END); len_new = ftell(fp_new); fseek(fp_new, 0L, SEEK_SET);
91         fseek(fp_org, 0L, SEEK_END); len_org = ftell(fp_org); fseek(fp_org, 0L, SEEK_SET);
92
93
94         if(len_new != len_org) {
95                 fclose(fp_new);
96                 fclose(fp_org);
97                 REN_IF_DIFF;
98         }
99
100         /* now compare the files... */
101         arr_new= MEM_mallocN(sizeof(char)*len_new, "rna_cmp_file_new");
102         arr_org= MEM_mallocN(sizeof(char)*len_org, "rna_cmp_file_org");
103
104         if(fread(arr_new, sizeof(char), len_new, fp_new) != len_new)
105                 fprintf(stderr, "%s:%d, error reading file %s for comparison.\n", __FILE__, __LINE__, tmpfile);
106         if(fread(arr_org, sizeof(char), len_org, fp_org) != len_org)
107                 fprintf(stderr, "%s:%d, error reading file %s for comparison.\n", __FILE__, __LINE__, orgfile);
108
109         fclose(fp_new);
110         fclose(fp_org);
111
112         cmp= memcmp(arr_new, arr_org, len_new);
113
114         MEM_freeN(arr_new);
115         MEM_freeN(arr_org);
116
117         if(cmp) {
118                 REN_IF_DIFF;
119         }
120         else {
121                 remove(tmpfile);
122                 return 0;
123         }
124
125 #undef REN_IF_DIFF
126 }
127
128
129
130 /* Sorting */
131
132 static int cmp_struct(const void *a, const void *b)
133 {
134         const StructRNA *structa= *(const StructRNA**)a;
135         const StructRNA *structb= *(const StructRNA**)b;
136
137         return strcmp(structa->identifier, structb->identifier);
138 }
139
140 static int cmp_property(const void *a, const void *b)
141 {
142         const PropertyRNA *propa= *(const PropertyRNA**)a;
143         const PropertyRNA *propb= *(const PropertyRNA**)b;
144
145         if(strcmp(propa->identifier, "rna_type") == 0) return -1;
146         else if(strcmp(propb->identifier, "rna_type") == 0) return 1;
147
148         if(strcmp(propa->identifier, "name") == 0) return -1;
149         else if(strcmp(propb->identifier, "name") == 0) return 1;
150
151         return strcmp(propa->name, propb->name);
152 }
153
154 static int cmp_def_struct(const void *a, const void *b)
155 {
156         const StructDefRNA *dsa= *(const StructDefRNA**)a;
157         const StructDefRNA *dsb= *(const StructDefRNA**)b;
158
159         return cmp_struct(&dsa->srna, &dsb->srna);
160 }
161
162 static int cmp_def_property(const void *a, const void *b)
163 {
164         const PropertyDefRNA *dpa= *(const PropertyDefRNA**)a;
165         const PropertyDefRNA *dpb= *(const PropertyDefRNA**)b;
166
167         return cmp_property(&dpa->prop, &dpb->prop);
168 }
169
170 static void rna_sortlist(ListBase *listbase, int(*cmp)(const void*, const void*))
171 {
172         Link *link;
173         void **array;
174         int a, size;
175         
176         if(listbase->first == listbase->last)
177                 return;
178
179         for(size=0, link=listbase->first; link; link=link->next)
180                 size++;
181
182         array= MEM_mallocN(sizeof(void*)*size, "rna_sortlist");
183         for(a=0, link=listbase->first; link; link=link->next, a++)
184                 array[a]= link;
185
186         qsort(array, size, sizeof(void*), cmp);
187
188         listbase->first= listbase->last= NULL;
189         for(a=0; a<size; a++) {
190                 link= array[a];
191                 link->next= link->prev= NULL;
192                 rna_addtail(listbase, link);
193         }
194
195         MEM_freeN(array);
196 }
197
198 /* Preprocessing */
199
200 static void rna_print_c_string(FILE *f, const char *str)
201 {
202         static char *escape[] = {"\''", "\"\"", "\??", "\\\\","\aa", "\bb", "\ff", "\nn", "\rr", "\tt", "\vv", NULL};
203         int i, j;
204
205         if(!str) {
206                 fprintf(f, "NULL");
207                 return;
208         }
209
210         fprintf(f, "\"");
211         for(i=0; str[i]; i++) {
212                 for(j=0; escape[j]; j++)
213                         if(str[i] == escape[j][0])
214                                 break;
215
216                 if(escape[j]) fprintf(f, "\\%c", escape[j][1]);
217                 else fprintf(f, "%c", str[i]);
218         }
219         fprintf(f, "\"");
220 }
221
222 static void rna_print_data_get(FILE *f, PropertyDefRNA *dp)
223 {
224         if(dp->dnastructfromname && dp->dnastructfromprop)
225                 fprintf(f, "    %s *data= (%s*)(((%s*)ptr->data)->%s);\n", dp->dnastructname, dp->dnastructname, dp->dnastructfromname, dp->dnastructfromprop);
226         else
227                 fprintf(f, "    %s *data= (%s*)(ptr->data);\n", dp->dnastructname, dp->dnastructname);
228 }
229
230 static void rna_print_id_get(FILE *f, PropertyDefRNA *dp)
231 {
232         fprintf(f, "    ID *id= ptr->id.data;\n");
233 }
234
235 static char *rna_alloc_function_name(const char *structname, const char *propname, const char *type)
236 {
237         AllocDefRNA *alloc;
238         char buffer[2048];
239         char *result;
240
241         snprintf(buffer, sizeof(buffer), "%s_%s_%s", structname, propname, type);
242         result= MEM_callocN(sizeof(char)*strlen(buffer)+1, "rna_alloc_function_name");
243         strcpy(result, buffer);
244
245         alloc= MEM_callocN(sizeof(AllocDefRNA), "AllocDefRNA");
246         alloc->mem= result;
247         rna_addtail(&DefRNA.allocs, alloc);
248
249         return result;
250 }
251
252 static StructRNA *rna_find_struct(const char *identifier)
253 {
254         StructDefRNA *ds;
255
256         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
257                 if(strcmp(ds->srna->identifier, identifier)==0)
258                         return ds->srna;
259
260         return NULL;
261 }
262
263 static const char *rna_find_type(const char *type)
264 {
265         StructDefRNA *ds;
266
267         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
268                 if(ds->dnaname && strcmp(ds->dnaname, type)==0)
269                         return ds->srna->identifier;
270
271         return NULL;
272 }
273
274 static const char *rna_find_dna_type(const char *type)
275 {
276         StructDefRNA *ds;
277
278         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
279                 if(strcmp(ds->srna->identifier, type)==0)
280                         return ds->dnaname;
281
282         return NULL;
283 }
284
285 static const char *rna_type_type_name(PropertyRNA *prop)
286 {
287         switch(prop->type) {
288                 case PROP_BOOLEAN:
289                 case PROP_INT:
290                 case PROP_ENUM:
291                         return "int";
292                 case PROP_FLOAT:
293                         return "float";
294                 case PROP_STRING:
295                         return "char*";
296                 default:
297                         return NULL;
298         }
299 }
300
301 static const char *rna_type_type(PropertyRNA *prop)
302 {
303         const char *type;
304
305         type= rna_type_type_name(prop);
306
307         if(type)
308                 return type;
309
310         return "PointerRNA";
311 }
312
313 static const char *rna_type_struct(PropertyRNA *prop)
314 {
315         const char *type;
316
317         type= rna_type_type_name(prop);
318
319         if(type)
320                 return "";
321
322         return "struct ";
323 }
324
325 static const char *rna_parameter_type_name(PropertyRNA *parm)
326 {
327         const char *type;
328
329         type= rna_type_type_name(parm);
330
331         if(type)
332                 return type;
333
334         switch(parm->type) {
335                 case PROP_POINTER:  {
336                         PointerPropertyRNA *pparm= (PointerPropertyRNA*)parm;
337
338                         if(parm->flag & PROP_RNAPTR)
339                                 return "PointerRNA";
340                         else
341                                 return rna_find_dna_type((const char *)pparm->type);
342                 }
343                 case PROP_COLLECTION: {
344                         return "ListBase";
345                 }
346                 default:
347                         return "<error, no type specified>";
348         }
349 }
350
351 static int rna_enum_bitmask(PropertyRNA *prop)
352 {
353         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
354         int a, mask= 0;
355
356         if(eprop->item) {
357                 for(a=0; a<eprop->totitem; a++)
358                         if(eprop->item[a].identifier[0])
359                                 mask |= eprop->item[a].value;
360         }
361         
362         return mask;
363 }
364
365 static int rna_color_quantize(PropertyRNA *prop, PropertyDefRNA *dp)
366 {
367         if(prop->type == PROP_FLOAT && prop->subtype == PROP_COLOR)
368                 if(strcmp(dp->dnatype, "float") != 0 && strcmp(dp->dnatype, "double") != 0)
369                         return 1;
370         
371         return 0;
372 }
373
374 static const char *rna_function_string(void *func)
375 {
376         return (func)? (const char*)func: "NULL";
377 }
378
379 static void rna_float_print(FILE *f, float num)
380 {
381         if(num == -FLT_MAX) fprintf(f, "-FLT_MAX");
382         else if(num == FLT_MAX) fprintf(f, "FLT_MAX");
383         else if((int)num == num) fprintf(f, "%.1ff", num);
384         else fprintf(f, "%.10ff", num);
385 }
386
387 static void rna_int_print(FILE *f, int num)
388 {
389         if(num == INT_MIN) fprintf(f, "INT_MIN");
390         else if(num == INT_MAX) fprintf(f, "INT_MAX");
391         else fprintf(f, "%d", num);
392 }
393
394 static char *rna_def_property_get_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
395 {
396         char *func;
397
398         if(prop->flag & PROP_IDPROPERTY)
399                 return NULL;
400
401         if(!manualfunc) {
402                 if(!dp->dnastructname || !dp->dnaname) {
403                         fprintf(stderr, "rna_def_property_get_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
404                         DefRNA.error= 1;
405                         return NULL;
406                 }
407         }
408
409         func= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
410
411         switch(prop->type) {
412                 case PROP_STRING: {
413                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
414                         fprintf(f, "void %s(PointerRNA *ptr, char *value)\n", func);
415                         fprintf(f, "{\n");
416                         if(manualfunc) {
417                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
418                         }
419                         else {
420                                 rna_print_data_get(f, dp);
421                                 if(sprop->maxlength)
422                                         fprintf(f, "    BLI_strncpy(value, data->%s, %d);\n", dp->dnaname, sprop->maxlength);
423                                 else
424                                         fprintf(f, "    BLI_strncpy(value, data->%s, sizeof(data->%s));\n", dp->dnaname, dp->dnaname);
425                         }
426                         fprintf(f, "}\n\n");
427                         break;
428                 }
429                 case PROP_POINTER: {
430                         fprintf(f, "PointerRNA %s(PointerRNA *ptr)\n", func);
431                         fprintf(f, "{\n");
432                         if(manualfunc) {
433                                 fprintf(f, "    return %s(ptr);\n", manualfunc);
434                         }
435                         else {
436                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
437                                 rna_print_data_get(f, dp);
438                                 if(dp->dnapointerlevel == 0)
439                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, &data->%s);\n", (char*)pprop->type, dp->dnaname);
440                                 else
441                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, data->%s);\n", (char*)pprop->type, dp->dnaname);
442                         }
443                         fprintf(f, "}\n\n");
444                         break;
445                 }
446                 case PROP_COLLECTION: {
447                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
448
449                         fprintf(f, "static PointerRNA %s(CollectionPropertyIterator *iter)\n", func);
450                         fprintf(f, "{\n");
451                         if(manualfunc) {
452                                 if(strcmp(manualfunc, "rna_iterator_listbase_get") == 0 ||
453                                    strcmp(manualfunc, "rna_iterator_array_get") == 0 ||
454                                    strcmp(manualfunc, "rna_iterator_array_dereference_get") == 0)
455                                         fprintf(f, "    return rna_pointer_inherit_refine(&iter->parent, &RNA_%s, %s(iter));\n", (cprop->item_type)? (char*)cprop->item_type: "UnknownType", manualfunc);
456                                 else
457                                         fprintf(f, "    return %s(iter);\n", manualfunc);
458                         }
459                         fprintf(f, "}\n\n");
460                         break;
461                 }
462                 default:
463                         if(prop->arraydimension) {
464                                 if(prop->flag & PROP_DYNAMIC)
465                                         fprintf(f, "void %s(PointerRNA *ptr, %s values[])\n", func, rna_type_type(prop));
466                                 else
467                                         fprintf(f, "void %s(PointerRNA *ptr, %s values[%d])\n", func, rna_type_type(prop), prop->totarraylength);
468                                 fprintf(f, "{\n");
469
470                                 if(manualfunc) {
471                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
472                                 }
473                                 else {
474                                         rna_print_data_get(f, dp);
475
476                                         if(prop->flag & PROP_DYNAMIC) {
477                                                 char *lenfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get_length");
478                                                 fprintf(f, "    int i, arraylen[RNA_MAX_ARRAY_DIMENSION];\n");
479                                                 fprintf(f, "    int len= %s(ptr, arraylen);\n\n", lenfunc);
480                                                 fprintf(f, "    for(i=0; i<len; i++) {\n");
481                                                 MEM_freeN(lenfunc);
482                                         }
483                                         else {
484                                                 fprintf(f, "    int i;\n\n");
485                                                 fprintf(f, "    for(i=0; i<%d; i++) {\n", prop->totarraylength);
486                                         }
487
488                                         if(dp->dnaarraylength == 1) {
489                                                 if(prop->type == PROP_BOOLEAN && dp->booleanbit)
490                                                         fprintf(f, "            values[i]= %s((data->%s & (%d<<i)) != 0);\n", (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit);
491                                                 else
492                                                         fprintf(f, "            values[i]= (%s)%s((&data->%s)[i]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
493                                         }
494                                         else {
495                                                 if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
496                                                         fprintf(f, "            values[i]= %s((data->%s[i] & ", (dp->booleannegative)? "!": "", dp->dnaname);
497                                                         rna_int_print(f, dp->booleanbit);
498                                                         fprintf(f, ") != 0);\n");
499                                                 }
500                                                 else if(rna_color_quantize(prop, dp))
501                                                         fprintf(f, "            values[i]= (%s)(data->%s[i]*(1.0f/255.0f));\n", rna_type_type(prop), dp->dnaname);
502                                                 else if(dp->dnatype)
503                                                         fprintf(f, "            values[i]= (%s)%s(((%s*)data->%s)[i]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnatype, dp->dnaname);
504                                                 else
505                                                         fprintf(f, "            values[i]= (%s)%s((data->%s)[i]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
506                                         }
507                                         fprintf(f, "    }\n");
508                                 }
509                                 fprintf(f, "}\n\n");
510                         }
511                         else {
512                                 fprintf(f, "%s %s(PointerRNA *ptr)\n", rna_type_type(prop), func);
513                                 fprintf(f, "{\n");
514
515                                 if(manualfunc) {
516                                         fprintf(f, "    return %s(ptr);\n", manualfunc);
517                                 }
518                                 else {
519                                         rna_print_data_get(f, dp);
520                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
521                                                 fprintf(f, "    return %s(((data->%s) & ", (dp->booleannegative)? "!": "", dp->dnaname);
522                                                 rna_int_print(f, dp->booleanbit);
523                                                 fprintf(f, ") != 0);\n");
524                                         }
525                                         else if(prop->type == PROP_ENUM && dp->enumbitflags) {
526                                                 fprintf(f, "    return ((data->%s) & ", dp->dnaname);
527                                                 rna_int_print(f, rna_enum_bitmask(prop));
528                                                 fprintf(f, ");\n");
529                                         }
530                                         else
531                                                 fprintf(f, "    return (%s)%s(data->%s);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
532                                 }
533
534                                 fprintf(f, "}\n\n");
535                         }
536                         break;
537         }
538
539         return func;
540 }
541
542 static void rna_clamp_value(FILE *f, PropertyRNA *prop, int array)
543 {
544         if(prop->type == PROP_INT) {
545                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
546
547                 if(iprop->hardmin != INT_MIN || iprop->hardmax != INT_MAX) {
548                         if(array) fprintf(f, "CLAMPIS(values[i], ");
549                         else fprintf(f, "CLAMPIS(value, ");
550                         rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
551                         rna_int_print(f, iprop->hardmax); fprintf(f, ");\n");
552                         return;
553                 }
554         }
555         else if(prop->type == PROP_FLOAT) {
556                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
557
558                 if(fprop->hardmin != -FLT_MAX || fprop->hardmax != FLT_MAX) {
559                         if(array) fprintf(f, "CLAMPIS(values[i], ");
560                         else fprintf(f, "CLAMPIS(value, ");
561                         rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
562                         rna_float_print(f, fprop->hardmax); fprintf(f, ");\n");
563                         return;
564                 }
565         }
566
567         if(array)
568                 fprintf(f, "values[i];\n");
569         else
570                 fprintf(f, "value;\n");
571 }
572
573 static char *rna_def_property_set_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
574 {
575         char *func;
576
577         if(!(prop->flag & PROP_EDITABLE))
578                 return NULL;
579         if(prop->flag & PROP_IDPROPERTY)
580                 return NULL;
581
582         if(!manualfunc) {
583                 if(!dp->dnastructname || !dp->dnaname) {
584                         if(prop->flag & PROP_EDITABLE) {
585                                 fprintf(stderr, "rna_def_property_set_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
586                                 DefRNA.error= 1;
587                         }
588                         return NULL;
589                 }
590         }
591
592         func= rna_alloc_function_name(srna->identifier, prop->identifier, "set");
593
594         switch(prop->type) {
595                 case PROP_STRING: {
596                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
597                         fprintf(f, "void %s(PointerRNA *ptr, const char *value)\n", func);
598                         fprintf(f, "{\n");
599                         if(manualfunc) {
600                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
601                         }
602                         else {
603                                 rna_print_data_get(f, dp);
604                                 if(sprop->maxlength)
605                                         fprintf(f, "    BLI_strncpy(data->%s, value, %d);\n", dp->dnaname, sprop->maxlength);
606                                 else
607                                         fprintf(f, "    BLI_strncpy(data->%s, value, sizeof(data->%s));\n", dp->dnaname, dp->dnaname);
608                         }
609                         fprintf(f, "}\n\n");
610                         break;
611                 }
612                 case PROP_POINTER: {
613                         fprintf(f, "void %s(PointerRNA *ptr, PointerRNA value)\n", func);
614                         fprintf(f, "{\n");
615                         if(manualfunc) {
616                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
617                         }
618                         else {
619                                 rna_print_data_get(f, dp);
620
621                                 if(prop->flag & PROP_ID_SELF_CHECK) {
622                                         rna_print_id_get(f, dp);
623                                         fprintf(f, "    if(id==value.data) return;\n\n");
624                                 }
625
626                                 if(prop->flag & PROP_ID_REFCOUNT) {
627                                         fprintf(f, "\n  if(data->%s)\n", dp->dnaname);
628                                         fprintf(f, "            id_us_min((ID*)data->%s);\n", dp->dnaname);
629                                         fprintf(f, "    if(value.data)\n");
630                                         fprintf(f, "            id_us_plus((ID*)value.data);\n\n");
631                                 }
632                                 else {
633                                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
634                                         StructRNA *type= rna_find_struct((char*)pprop->type);
635                                         if(type && (type->flag & STRUCT_ID)) {
636                                                 fprintf(f, "    if(value.data)\n");
637                                                 fprintf(f, "            id_lib_extern((ID*)value.data);\n\n");
638                                         }
639                                 }
640
641                                 fprintf(f, "    data->%s= value.data;\n", dp->dnaname);
642
643                         }
644                         fprintf(f, "}\n\n");
645                         break;
646                 }
647                 default:
648                         if(prop->arraydimension) {
649                                 if(prop->flag & PROP_DYNAMIC)
650                                         fprintf(f, "void %s(PointerRNA *ptr, const %s values[])\n", func, rna_type_type(prop));
651                                 else
652                                         fprintf(f, "void %s(PointerRNA *ptr, const %s values[%d])\n", func, rna_type_type(prop), prop->totarraylength);
653                                 fprintf(f, "{\n");
654
655                                 if(manualfunc) {
656                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
657                                 }
658                                 else {
659                                         rna_print_data_get(f, dp);
660
661                                         if(prop->flag & PROP_DYNAMIC) {
662                                                 char *lenfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "set_length");
663                                                 fprintf(f, "    int i, arraylen[RNA_MAX_ARRAY_DIMENSION];\n");
664                                                 fprintf(f, "    int len= %s(ptr, arraylen);\n\n", lenfunc);
665                                                 fprintf(f, "    for(i=0; i<len; i++) {\n");
666                                                 MEM_freeN(lenfunc);
667                                         }
668                                         else {
669                                                 fprintf(f, "    int i;\n\n");
670                                                 fprintf(f, "    for(i=0; i<%d; i++) {\n", prop->totarraylength);
671                                         }
672
673                                         if(dp->dnaarraylength == 1) {
674                                                 if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
675                                                         fprintf(f, "            if(%svalues[i]) data->%s |= (%d<<i);\n", (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit);
676                                                         fprintf(f, "            else data->%s &= ~(%d<<i);\n", dp->dnaname, dp->booleanbit);
677                                                 }
678                                                 else {
679                                                         fprintf(f, "            (&data->%s)[i]= %s", dp->dnaname, (dp->booleannegative)? "!": "");
680                                                         rna_clamp_value(f, prop, 1);
681                                                 }
682                                         }
683                                         else {
684                                                 if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
685                                                         fprintf(f, "            if(%svalues[i]) data->%s[i] |= ", (dp->booleannegative)? "!": "", dp->dnaname);
686                                                         rna_int_print(f, dp->booleanbit);
687                                                         fprintf(f, ";\n");
688                                                         fprintf(f, "            else data->%s[i] &= ~", dp->dnaname);
689                                                         rna_int_print(f, dp->booleanbit);
690                                                         fprintf(f, ";\n");
691                                                 }
692                                                 else if(rna_color_quantize(prop, dp)) {
693                                                         fprintf(f, "            data->%s[i]= FTOCHAR(values[i]);\n", dp->dnaname);
694                                                 }
695                                                 else {
696                                                         if(dp->dnatype)
697                                                                 fprintf(f, "            ((%s*)data->%s)[i]= %s", dp->dnatype, dp->dnaname, (dp->booleannegative)? "!": "");
698                                                         else
699                                                                 fprintf(f, "            (data->%s)[i]= %s", dp->dnaname, (dp->booleannegative)? "!": "");
700                                                         rna_clamp_value(f, prop, 1);
701                                                 }
702                                         }
703                                         fprintf(f, "    }\n");
704                                 }
705                                 fprintf(f, "}\n\n");
706                         }
707                         else {
708                                 fprintf(f, "void %s(PointerRNA *ptr, %s value)\n", func, rna_type_type(prop));
709                                 fprintf(f, "{\n");
710
711                                 if(manualfunc) {
712                                         fprintf(f, "    %s(ptr, value);\n", manualfunc);
713                                 }
714                                 else {
715                                         rna_print_data_get(f, dp);
716                                         if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
717                                                 fprintf(f, "    if(%svalue) data->%s |= ", (dp->booleannegative)? "!": "", dp->dnaname);
718                                                 rna_int_print(f, dp->booleanbit);
719                                                 fprintf(f, ";\n");
720                                                 fprintf(f, "    else data->%s &= ~", dp->dnaname);
721                                                 rna_int_print(f, dp->booleanbit);
722                                                 fprintf(f, ";\n");
723                                         }
724                                         else if(prop->type == PROP_ENUM && dp->enumbitflags) {
725                                                 fprintf(f, "    data->%s &= ~", dp->dnaname);
726                                                 rna_int_print(f, rna_enum_bitmask(prop));
727                                                 fprintf(f, ";\n");
728                                                 fprintf(f, "    data->%s |= value;\n", dp->dnaname);
729                                         }
730                                         else {
731                                                 fprintf(f, "    data->%s= %s", dp->dnaname, (dp->booleannegative)? "!": "");
732                                                 rna_clamp_value(f, prop, 0);
733                                         }
734                                 }
735                                 fprintf(f, "}\n\n");
736                         }
737                         break;
738         }
739
740         return func;
741 }
742
743 static char *rna_def_property_length_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
744 {
745         char *func= NULL;
746
747         if(prop->flag & PROP_IDPROPERTY)
748                 return NULL;
749
750         if(prop->type == PROP_STRING) {
751                 if(!manualfunc) {
752                         if(!dp->dnastructname || !dp->dnaname) {
753                                 fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
754                                 DefRNA.error= 1;
755                                 return NULL;
756                         }
757                 }
758
759                 func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");
760
761                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
762                 fprintf(f, "{\n");
763                 if(manualfunc) {
764                         fprintf(f, "    return %s(ptr);\n", manualfunc);
765                 }
766                 else {
767                         rna_print_data_get(f, dp);
768                         fprintf(f, "    return strlen(data->%s);\n", dp->dnaname);
769                 }
770                 fprintf(f, "}\n\n");
771         }
772         else if(prop->type == PROP_COLLECTION) {
773                 if(!manualfunc) {
774                         if(prop->type == PROP_COLLECTION && (!(dp->dnalengthname || dp->dnalengthfixed)|| !dp->dnaname)) {
775                                 fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
776                                 DefRNA.error= 1;
777                                 return NULL;
778                         }
779                 }
780
781                 func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");
782
783                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
784                 fprintf(f, "{\n");
785                 if(manualfunc) {
786                         fprintf(f, "    return %s(ptr);\n", manualfunc);
787                 }
788                 else {
789                         rna_print_data_get(f, dp);
790                         if(dp->dnalengthname)
791                                 fprintf(f, "    return (data->%s == NULL)? 0: data->%s;\n", dp->dnaname, dp->dnalengthname);
792                         else
793                                 fprintf(f, "    return (data->%s == NULL)? 0: %d;\n", dp->dnaname, dp->dnalengthfixed);
794                 }
795                 fprintf(f, "}\n\n");
796         }
797
798         return func;
799 }
800
801 static char *rna_def_property_begin_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
802 {
803         char *func, *getfunc;
804
805         if(prop->flag & PROP_IDPROPERTY)
806                 return NULL;
807
808         if(!manualfunc) {
809                 if(!dp->dnastructname || !dp->dnaname) {
810                         fprintf(stderr, "rna_def_property_begin_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
811                         DefRNA.error= 1;
812                         return NULL;
813                 }
814         }
815
816         func= rna_alloc_function_name(srna->identifier, prop->identifier, "begin");
817
818         fprintf(f, "void %s(CollectionPropertyIterator *iter, PointerRNA *ptr)\n", func);
819         fprintf(f, "{\n");
820
821         if(!manualfunc)
822                 rna_print_data_get(f, dp);
823
824         fprintf(f, "\n  memset(iter, 0, sizeof(*iter));\n");
825         fprintf(f, "    iter->parent= *ptr;\n");
826         fprintf(f, "    iter->prop= (PropertyRNA*)&rna_%s_%s;\n", srna->identifier, prop->identifier);
827
828         if(dp->dnalengthname || dp->dnalengthfixed) {
829                 if(manualfunc) {
830                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
831                 }
832                 else {
833                         if(dp->dnalengthname)
834                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), data->%s, 0, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthname);
835                         else
836                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), %d, 0, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthfixed);
837                 }
838         }
839         else {
840                 if(manualfunc)
841                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
842                 else if(dp->dnapointerlevel == 0)
843                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, &data->%s, NULL);\n", dp->dnaname);
844                 else
845                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, data->%s, NULL);\n", dp->dnaname);
846         }
847
848         getfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
849
850         fprintf(f, "\n  if(iter->valid)\n");
851         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
852
853         fprintf(f, "}\n\n");
854
855
856         return func;
857 }
858
859 static char *rna_def_property_lookup_int_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc, char *nextfunc)
860 {
861         char *func;
862
863         if(prop->flag & PROP_IDPROPERTY)
864                 return NULL;
865
866         if(!manualfunc) {
867                 if(!dp->dnastructname || !dp->dnaname)
868                         return NULL;
869
870                 /* only supported in case of standard next functions */
871                 if(strcmp(nextfunc, "rna_iterator_array_next") == 0);
872                 else if(strcmp(nextfunc, "rna_iterator_listbase_next") == 0);
873                 else return NULL;
874         }
875
876         func= rna_alloc_function_name(srna->identifier, prop->identifier, "lookup_int");
877
878         fprintf(f, "PointerRNA %s(PointerRNA *ptr, int index)\n", func);
879         fprintf(f, "{\n");
880
881         if(manualfunc) {
882                 fprintf(f, "\n  return %s(ptr, index);\n", manualfunc);
883                 fprintf(f, "}\n\n");
884                 return func;
885         }
886
887         fprintf(f, "    PointerRNA r_ptr;\n");
888         fprintf(f, "    CollectionPropertyIterator iter;\n\n");
889
890         fprintf(f, "    %s_%s_begin(&iter, ptr);\n\n", srna->identifier, prop->identifier);
891         fprintf(f, "    {\n");
892
893         if(strcmp(nextfunc, "rna_iterator_array_next") == 0) {
894                 fprintf(f, "            ArrayIterator *internal= iter.internal;\n");
895                 fprintf(f, "            if(internal->skip) {\n");
896                 fprintf(f, "                    while(index-- > 0) {\n");
897                 fprintf(f, "                            do {\n");
898                 fprintf(f, "                                    internal->ptr += internal->itemsize;\n");
899                 fprintf(f, "                            } while(internal->skip(&iter, internal->ptr));\n");
900                 fprintf(f, "                    }\n");
901                 fprintf(f, "            }\n");
902                 fprintf(f, "            else {\n");
903                 fprintf(f, "                    internal->ptr += internal->itemsize*index;\n");
904                 fprintf(f, "            }\n");
905         }
906         else if(strcmp(nextfunc, "rna_iterator_listbase_next") == 0) {
907                 fprintf(f, "            ListBaseIterator *internal= iter.internal;\n");
908                 fprintf(f, "            if(internal->skip) {\n");
909                 fprintf(f, "                    while(index-- > 0) {\n");
910                 fprintf(f, "                            do {\n");
911                 fprintf(f, "                                    internal->link= internal->link->next;\n");
912                 fprintf(f, "                            } while(internal->skip(&iter, internal->link));\n");
913                 fprintf(f, "                    }\n");
914                 fprintf(f, "            }\n");
915                 fprintf(f, "            else {\n");
916                 fprintf(f, "                    while(index-- > 0)\n");
917                 fprintf(f, "                            internal->link= internal->link->next;\n");
918                 fprintf(f, "            }\n");
919         }
920
921         fprintf(f, "    }\n\n");
922
923         fprintf(f, "    r_ptr = %s_%s_get(&iter);\n", srna->identifier, prop->identifier);
924         fprintf(f, "    %s_%s_end(&iter);\n\n", srna->identifier, prop->identifier);
925
926         fprintf(f, "    return r_ptr;\n");
927
928 #if 0
929         rna_print_data_get(f, dp);
930         item_type= (cprop->item_type)? (char*)cprop->item_type: "UnknownType";
931
932         if(dp->dnalengthname || dp->dnalengthfixed) {
933                 if(dp->dnalengthname)
934                         fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), data->%s, index);\n", item_type, dp->dnaname, dp->dnaname, dp->dnalengthname);
935                 else
936                         fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), %d, index);\n", item_type, dp->dnaname, dp->dnaname, dp->dnalengthfixed);
937         }
938         else {
939                 if(dp->dnapointerlevel == 0)
940                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, &data->%s, index);\n", item_type, dp->dnaname);
941                 else
942                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, data->%s, index);\n", item_type, dp->dnaname);
943         }
944 #endif
945
946         fprintf(f, "}\n\n");
947
948         return func;
949 }
950
951 static char *rna_def_property_next_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
952 {
953         char *func, *getfunc;
954
955         if(prop->flag & PROP_IDPROPERTY)
956                 return NULL;
957
958         if(!manualfunc)
959                 return NULL;
960
961         func= rna_alloc_function_name(srna->identifier, prop->identifier, "next");
962
963         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
964         fprintf(f, "{\n");
965         fprintf(f, "    %s(iter);\n", manualfunc);
966
967         getfunc= rna_alloc_function_name(srna->identifier, prop->identifier, "get");
968
969         fprintf(f, "\n  if(iter->valid)\n");
970         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
971
972         fprintf(f, "}\n\n");
973
974         return func;
975 }
976
977 static char *rna_def_property_end_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
978 {
979         char *func;
980
981         if(prop->flag & PROP_IDPROPERTY)
982                 return NULL;
983
984         func= rna_alloc_function_name(srna->identifier, prop->identifier, "end");
985
986         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
987         fprintf(f, "{\n");
988         if(manualfunc)
989                 fprintf(f, "    %s(iter);\n", manualfunc);
990         fprintf(f, "}\n\n");
991
992         return func;
993 }
994
995 static void rna_set_raw_property(PropertyDefRNA *dp, PropertyRNA *prop)
996 {
997         if(dp->dnapointerlevel != 0)
998                 return;
999         if(!dp->dnatype || !dp->dnaname || !dp->dnastructname)
1000                 return;
1001         
1002         if(strcmp(dp->dnatype, "char") == 0) {
1003                 prop->rawtype= PROP_RAW_CHAR;
1004                 prop->flag |= PROP_RAW_ACCESS;
1005         }
1006         else if(strcmp(dp->dnatype, "short") == 0) {
1007                 prop->rawtype= PROP_RAW_SHORT;
1008                 prop->flag |= PROP_RAW_ACCESS;
1009         }
1010         else if(strcmp(dp->dnatype, "int") == 0) {
1011                 prop->rawtype= PROP_RAW_INT;
1012                 prop->flag |= PROP_RAW_ACCESS;
1013         }
1014         else if(strcmp(dp->dnatype, "float") == 0) {
1015                 prop->rawtype= PROP_RAW_FLOAT;
1016                 prop->flag |= PROP_RAW_ACCESS;
1017         }
1018         else if(strcmp(dp->dnatype, "double") == 0) {
1019                 prop->rawtype= PROP_RAW_DOUBLE;
1020                 prop->flag |= PROP_RAW_ACCESS;
1021         }
1022 }
1023
1024 static void rna_set_raw_offset(FILE *f, StructRNA *srna, PropertyRNA *prop)
1025 {
1026         PropertyDefRNA *dp= rna_find_struct_property_def(srna, prop);
1027
1028         fprintf(f, "\toffsetof(%s, %s), %d", dp->dnastructname, dp->dnaname, prop->rawtype);
1029 }
1030
1031 static void rna_def_property_funcs(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1032 {
1033         PropertyRNA *prop;
1034
1035         prop= dp->prop;
1036
1037         switch(prop->type) {
1038                 case PROP_BOOLEAN: {
1039                         BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1040
1041                         if(!prop->arraydimension) {
1042                                 if(!bprop->get && !bprop->set && !dp->booleanbit)
1043                                         rna_set_raw_property(dp, prop);
1044
1045                                 bprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->get);
1046                                 bprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->set);
1047                         }
1048                         else {
1049                                 bprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)bprop->getarray);
1050                                 bprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)bprop->setarray);
1051                         }
1052                         break;
1053                 }
1054                 case PROP_INT: {
1055                         IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1056
1057                         if(!prop->arraydimension) {
1058                                 if(!iprop->get && !iprop->set)
1059                                         rna_set_raw_property(dp, prop);
1060
1061                                 iprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->get);
1062                                 iprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->set);
1063                         }
1064                         else {
1065                                 if(!iprop->getarray && !iprop->setarray)
1066                                         rna_set_raw_property(dp, prop);
1067
1068                                 iprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)iprop->getarray);
1069                                 iprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)iprop->setarray);
1070                         }
1071                         break;
1072                 }
1073                 case PROP_FLOAT: {
1074                         FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1075
1076                         if(!prop->arraydimension) {
1077                                 if(!fprop->get && !fprop->set)
1078                                         rna_set_raw_property(dp, prop);
1079
1080                                 fprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->get);
1081                                 fprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->set);
1082                         }
1083                         else {
1084                                 if(!fprop->getarray && !fprop->setarray)
1085                                         rna_set_raw_property(dp, prop);
1086
1087                                 fprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)fprop->getarray);
1088                                 fprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)fprop->setarray);
1089                         }
1090                         break;
1091                 }
1092                 case PROP_ENUM: {
1093                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1094
1095                         eprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)eprop->get);
1096                         eprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)eprop->set);
1097                         break;
1098                 }
1099                 case PROP_STRING: {
1100                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1101
1102                         sprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)sprop->get);
1103                         sprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)sprop->length);
1104                         sprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)sprop->set);
1105                         break;
1106                 }
1107                 case PROP_POINTER: {
1108                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
1109
1110                         pprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)pprop->get);
1111                         pprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp, (char*)pprop->set);
1112                         if(!pprop->type) {
1113                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, pointer must have a struct type.\n", srna->identifier, prop->identifier);
1114                                 DefRNA.error= 1;
1115                         }
1116                         break;
1117                 }
1118                 case PROP_COLLECTION: {
1119                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
1120                         char *nextfunc= (char*)cprop->next;
1121
1122                         if(dp->dnatype && strcmp(dp->dnatype, "ListBase")==0);
1123                         else if(dp->dnalengthname || dp->dnalengthfixed)
1124                                 cprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp, (char*)cprop->length);
1125
1126                         /* test if we can allow raw array access, if it is using our standard
1127                          * array get/next function, we can be sure it is an actual array */
1128                         if(cprop->next && cprop->get)
1129                                 if(strcmp((char*)cprop->next, "rna_iterator_array_next") == 0 &&
1130                                    strcmp((char*)cprop->get, "rna_iterator_array_get") == 0)
1131                                         prop->flag |= PROP_RAW_ARRAY;
1132
1133                         cprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp, (char*)cprop->get);
1134                         cprop->begin= (void*)rna_def_property_begin_func(f, srna, prop, dp, (char*)cprop->begin);
1135                         cprop->next= (void*)rna_def_property_next_func(f, srna, prop, dp, (char*)cprop->next);
1136                         cprop->end= (void*)rna_def_property_end_func(f, srna, prop, dp, (char*)cprop->end);
1137                         cprop->lookupint= (void*)rna_def_property_lookup_int_func(f, srna, prop, dp, (char*)cprop->lookupint, nextfunc);
1138
1139                         if(!(prop->flag & PROP_IDPROPERTY)) {
1140                                 if(!cprop->begin) {
1141                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a begin function.\n", srna->identifier, prop->identifier);
1142                                         DefRNA.error= 1;
1143                                 }
1144                                 if(!cprop->next) {
1145                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a next function.\n", srna->identifier, prop->identifier);
1146                                         DefRNA.error= 1;
1147                                 }
1148                                 if(!cprop->get) {
1149                                         fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a get function.\n", srna->identifier, prop->identifier);
1150                                         DefRNA.error= 1;
1151                                 }
1152                         }
1153                         if(!cprop->item_type) {
1154                                 fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a struct type.\n", srna->identifier, prop->identifier);
1155                                 DefRNA.error= 1;
1156                         }
1157                         break;
1158                 }
1159         }
1160 }
1161
1162 static void rna_def_property_funcs_header(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1163 {
1164         PropertyRNA *prop;
1165         char *func;
1166
1167         prop= dp->prop;
1168
1169         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1170                 return;
1171
1172         func= rna_alloc_function_name(srna->identifier, prop->identifier, "");
1173
1174         switch(prop->type) {
1175                 case PROP_BOOLEAN:
1176                 case PROP_INT: {
1177                         if(!prop->arraydimension) {
1178                                 fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1179                                 //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
1180                         }
1181                         else {
1182                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[%d]);\n", func, prop->totarraylength);
1183                                 //fprintf(f, "void %sset(PointerRNA *ptr, const int values[%d]);\n", func, prop->arraylength);
1184                         }
1185                         break;
1186                 }
1187                 case PROP_FLOAT: {
1188                         if(!prop->arraydimension) {
1189                                 fprintf(f, "float %sget(PointerRNA *ptr);\n", func);
1190                                 //fprintf(f, "void %sset(PointerRNA *ptr, float value);\n", func);
1191                         }
1192                         else {
1193                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[%d]);\n", func, prop->totarraylength);
1194                                 //fprintf(f, "void %sset(PointerRNA *ptr, const float values[%d]);\n", func, prop->arraylength);
1195                         }
1196                         break;
1197                 }
1198                 case PROP_ENUM: {
1199                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1200                         int i;
1201
1202                         if(eprop->item) {
1203                                 fprintf(f, "enum {\n");
1204
1205                                 for(i=0; i<eprop->totitem; i++)
1206                                         if(eprop->item[i].identifier[0])
1207                                                 fprintf(f, "\t%s_%s_%s = %d,\n", srna->identifier, prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
1208
1209                                 fprintf(f, "};\n\n");
1210                         }
1211
1212                         fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1213                         //fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
1214
1215                         break;
1216                 }
1217                 case PROP_STRING: {
1218                         StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
1219
1220                         if(sprop->maxlength) {
1221                                 fprintf(f, "#define %s_%s_MAX %d\n\n", srna->identifier, prop->identifier, sprop->maxlength);
1222                         }
1223                         
1224                         fprintf(f, "void %sget(PointerRNA *ptr, char *value);\n", func);
1225                         fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
1226                         //fprintf(f, "void %sset(PointerRNA *ptr, const char *value);\n", func);
1227
1228                         break;
1229                 }
1230                 case PROP_POINTER: {
1231                         fprintf(f, "PointerRNA %sget(PointerRNA *ptr);\n", func);
1232                         //fprintf(f, "void %sset(PointerRNA *ptr, PointerRNA value);\n", func);
1233                         break;
1234                 }
1235                 case PROP_COLLECTION: {
1236                         fprintf(f, "void %sbegin(CollectionPropertyIterator *iter, PointerRNA *ptr);\n", func);
1237                         fprintf(f, "void %snext(CollectionPropertyIterator *iter);\n", func);
1238                         fprintf(f, "void %send(CollectionPropertyIterator *iter);\n", func);
1239                         //fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
1240                         //fprintf(f, "void %slookup_int(PointerRNA *ptr, int key, StructRNA **type);\n", func);
1241                         //fprintf(f, "void %slookup_string(PointerRNA *ptr, const char *key, StructRNA **type);\n", func);
1242                         break;
1243                 }
1244         }
1245
1246         fprintf(f, "\n");
1247 }
1248
1249 static void rna_def_property_funcs_header_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1250 {
1251         PropertyRNA *prop;
1252
1253         prop= dp->prop;
1254
1255         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1256                 return;
1257         
1258         if(prop->name && prop->description && strcmp(prop->description, "") != 0)
1259                 fprintf(f, "\t/* %s: %s */\n", prop->name, prop->description);
1260         else if(prop->name)
1261                 fprintf(f, "\t/* %s */\n", prop->name);
1262         else
1263                 fprintf(f, "\t/* */\n");
1264
1265         switch(prop->type) {
1266                 case PROP_BOOLEAN: {
1267                         if(!prop->arraydimension)
1268                                 fprintf(f, "\tbool %s(void);", prop->identifier);
1269                         else
1270                                 fprintf(f, "\tArray<int, %d> %s(void);", prop->totarraylength, prop->identifier);
1271                         break;
1272                 }
1273                 case PROP_INT: {
1274                         if(!prop->arraydimension)
1275                                 fprintf(f, "\tint %s(void);", prop->identifier);
1276                         else
1277                                 fprintf(f, "\tArray<int, %d> %s(void);", prop->totarraylength, prop->identifier);
1278                         break;
1279                 }
1280                 case PROP_FLOAT: {
1281                         if(!prop->arraydimension)
1282                                 fprintf(f, "\tfloat %s(void);", prop->identifier);
1283                         else
1284                                 fprintf(f, "\tArray<float, %d> %s(void);", prop->totarraylength, prop->identifier);
1285                         break;
1286                 }
1287                 case PROP_ENUM: {
1288                         EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1289                         int i;
1290
1291                         if(eprop->item) {
1292                                 fprintf(f, "\tenum %s_enum {\n", prop->identifier);
1293
1294                                 for(i=0; i<eprop->totitem; i++)
1295                                         if(eprop->item[i].identifier[0])
1296                                                 fprintf(f, "\t\t%s_%s = %d,\n", prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
1297
1298                                 fprintf(f, "\t};\n");
1299                         }
1300
1301                         fprintf(f, "\t%s_enum %s(void);", prop->identifier, prop->identifier);
1302                         break;
1303                 }
1304                 case PROP_STRING: {
1305                         fprintf(f, "\tstd::string %s(void);", prop->identifier);
1306                         break;
1307                 }
1308                 case PROP_POINTER: {
1309                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1310
1311                         if(pprop->type)
1312                                 fprintf(f, "\t%s %s(void);", (char*)pprop->type, prop->identifier);
1313                         else
1314                                 fprintf(f, "\t%s %s(void);", "UnknownType", prop->identifier);
1315                         break;
1316                 }
1317                 case PROP_COLLECTION: {
1318                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1319
1320                         if(cprop->item_type)
1321                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (char*)cprop->item_type, srna->identifier, prop->identifier);
1322                         else
1323                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1324                         break;
1325                 }
1326         }
1327
1328         fprintf(f, "\n");
1329 }
1330
1331 static void rna_def_property_funcs_impl_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1332 {
1333         PropertyRNA *prop;
1334
1335         prop= dp->prop;
1336
1337         if(prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1338                 return;
1339
1340         switch(prop->type) {
1341                 case PROP_BOOLEAN: {
1342                         if(!prop->arraydimension)
1343                                 fprintf(f, "\tBOOLEAN_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1344                         else
1345                                 fprintf(f, "\tBOOLEAN_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->totarraylength, prop->identifier);
1346                         break;
1347                 }
1348                 case PROP_INT: {
1349                         if(!prop->arraydimension)
1350                                 fprintf(f, "\tINT_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1351                         else
1352                                 fprintf(f, "\tINT_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->totarraylength, prop->identifier);
1353                         break;
1354                 }
1355                 case PROP_FLOAT: {
1356                         if(!prop->arraydimension)
1357                                 fprintf(f, "\tFLOAT_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1358                         else
1359                                 fprintf(f, "\tFLOAT_ARRAY_PROPERTY(%s, %d, %s)", srna->identifier, prop->totarraylength, prop->identifier);
1360                         break;
1361                 }
1362                 case PROP_ENUM: {
1363                         fprintf(f, "\tENUM_PROPERTY(%s_enum, %s, %s)", prop->identifier, srna->identifier, prop->identifier);
1364
1365                         break;
1366                 }
1367                 case PROP_STRING: {
1368                         fprintf(f, "\tSTRING_PROPERTY(%s, %s)", srna->identifier, prop->identifier);
1369                         break;
1370                 }
1371                 case PROP_POINTER: {
1372                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1373
1374                         if(pprop->type)
1375                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", (char*)pprop->type, srna->identifier, prop->identifier);
1376                         else
1377                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);
1378                         break;
1379                 }
1380                 case PROP_COLLECTION: {
1381                         /*CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1382
1383                         if(cprop->type)
1384                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (char*)cprop->type, srna->identifier, prop->identifier);
1385                         else
1386                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);*/
1387                         break;
1388                 }
1389         }
1390
1391         fprintf(f, "\n");
1392 }
1393
1394 static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA *dfunc)
1395 {
1396         StructRNA *srna;
1397         FunctionRNA *func;
1398         PropertyDefRNA *dparm;
1399         PropertyType type;
1400         char *funcname, *ptrstr, *valstr;
1401         int flag, pout, cptr, first;
1402
1403         srna= dsrna->srna;
1404         func= dfunc->func;
1405
1406         if(!dfunc->call)
1407                 return;
1408
1409         funcname= rna_alloc_function_name(srna->identifier, func->identifier, "call");
1410
1411         /* function definition */
1412         fprintf(f, "void %s(bContext *C, ReportList *reports, PointerRNA *_ptr, ParameterList *_parms)", funcname);
1413         fprintf(f, "\n{\n");
1414
1415         /* variable definitions */
1416         if((func->flag & FUNC_NO_SELF)==0) {
1417                 if(func->flag & FUNC_USE_SELF_ID)
1418                         fprintf(f, "\tstruct ID *_selfid;\n");
1419
1420                 if(dsrna->dnaname) fprintf(f, "\tstruct %s *_self;\n", dsrna->dnaname);
1421                 else fprintf(f, "\tstruct %s *_self;\n", srna->identifier);
1422         }
1423
1424         dparm= dfunc->cont.properties.first;
1425         for(; dparm; dparm= dparm->next) {
1426                 type = dparm->prop->type;
1427                 flag = dparm->prop->flag;
1428                 pout = (flag & PROP_OUTPUT);
1429                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1430
1431                 if(dparm->prop==func->c_ret)
1432                         ptrstr= cptr || dparm->prop->arraydimension ? "*" : "";
1433                 /* XXX only arrays and strings are allowed to be dynamic, is this checked anywhere? */
1434                 else if (cptr || (flag & PROP_DYNAMIC))
1435                         ptrstr= pout ? "**" : "*";
1436                 /* fixed size arrays and RNA pointers are pre-allocated on the ParameterList stack, pass a pointer to it */
1437                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
1438                         ptrstr= "*";
1439                 /* PROP_THICK_WRAP strings are pre-allocated on the ParameterList stack, but type name for string props is already char*, so leave empty */
1440                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
1441                         ptrstr= "";
1442                 else
1443                         ptrstr= pout ? "*" : "";
1444
1445                 /* for dynamic parameters we pass an additional int for the length of the parameter */
1446                 if (flag & PROP_DYNAMIC)
1447                         fprintf(f, "\tint %s%s_len;\n", pout ? "*" : "", dparm->prop->identifier);
1448                 
1449                 fprintf(f, "\t%s%s %s%s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
1450         }
1451
1452         fprintf(f, "\tchar *_data");
1453         if(func->c_ret) fprintf(f, ", *_retdata");
1454         fprintf(f, ";\n");
1455         fprintf(f, "\t\n");
1456
1457         /* assign self */
1458         if((func->flag & FUNC_NO_SELF)==0) {
1459                 if(func->flag & FUNC_USE_SELF_ID)
1460                         fprintf(f, "\t_selfid= (struct ID*)_ptr->id.data;\n");
1461
1462                 if(dsrna->dnaname) fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", dsrna->dnaname);
1463                 else fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", srna->identifier);
1464         }
1465
1466         fprintf(f, "\t_data= (char *)_parms->data;\n");
1467
1468         dparm= dfunc->cont.properties.first;
1469         for(; dparm; dparm= dparm->next) {
1470                 type = dparm->prop->type;
1471                 flag = dparm->prop->flag;
1472                 pout = (flag & PROP_OUTPUT);
1473                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1474
1475                 if(dparm->prop==func->c_ret)
1476                         fprintf(f, "\t_retdata= _data;\n");
1477                 else  {
1478                         char *data_str;
1479                         if (cptr || (flag & PROP_DYNAMIC)) {
1480                                 ptrstr= "**";
1481                                 valstr= "*";
1482                         }
1483                         else if (type == PROP_POINTER || dparm->prop->arraydimension) {
1484                                 ptrstr= "*";
1485                                 valstr= "";
1486                         }
1487                         else if (type == PROP_STRING && (flag & PROP_THICK_WRAP)) {
1488                                 ptrstr= "";
1489                                 valstr= "";
1490                         }
1491                         else {
1492                                 ptrstr= "*";
1493                                 valstr= "*";
1494                         }
1495
1496                         /* this must be kept in sync with RNA_parameter_length_get_data, we could just call the function directly, but this is faster */
1497                         if (flag & PROP_DYNAMIC) {
1498                                 fprintf(f, "\t%s_len= %s((int *)_data);\n", dparm->prop->identifier, pout ? "" : "*");
1499                                 data_str= "(&(((char *)_data)[sizeof(void *)]))";
1500                         }
1501                         else {
1502                                 data_str= "_data";
1503                         }
1504                         fprintf(f, "\t%s= ", dparm->prop->identifier);
1505
1506                         if (!pout)
1507                                 fprintf(f, "%s", valstr);
1508
1509                         fprintf(f, "((%s%s%s)%s);\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, data_str);
1510                 }
1511
1512                 if(dparm->next)
1513                         fprintf(f, "\t_data+= %d;\n", rna_parameter_size_alloc(dparm->prop));
1514         }
1515
1516         if(dfunc->call) {
1517                 fprintf(f, "\t\n");
1518                 fprintf(f, "\t");
1519                 if(func->c_ret) fprintf(f, "%s= ", func->c_ret->identifier);
1520                 fprintf(f, "%s(", dfunc->call);
1521
1522                 first= 1;
1523
1524                 if((func->flag & FUNC_NO_SELF)==0) {
1525                         if(func->flag & FUNC_USE_SELF_ID)
1526                                 fprintf(f, "_selfid, ");
1527
1528                         fprintf(f, "_self");
1529                         first= 0;
1530                 }
1531
1532                 if(func->flag & FUNC_USE_CONTEXT) {
1533                         if(!first) fprintf(f, ", ");
1534                         first= 0;
1535                         fprintf(f, "C");
1536                 }
1537
1538                 if(func->flag & FUNC_USE_REPORTS) {
1539                         if(!first) fprintf(f, ", ");
1540                         first= 0;
1541                         fprintf(f, "reports");
1542                 }
1543
1544                 dparm= dfunc->cont.properties.first;
1545                 for(; dparm; dparm= dparm->next) {
1546                         if(dparm->prop==func->c_ret)
1547                                 continue;
1548
1549                         if(!first) fprintf(f, ", ");
1550                         first= 0;
1551
1552                         if (dparm->prop->flag & PROP_DYNAMIC)
1553                                 fprintf(f, "%s_len, %s", dparm->prop->identifier, dparm->prop->identifier);
1554                         else
1555                                 fprintf(f, "%s", dparm->prop->identifier);
1556                 }
1557
1558                 fprintf(f, ");\n");
1559
1560                 if(func->c_ret) {
1561                         dparm= rna_find_parameter_def(func->c_ret);
1562                         ptrstr= (((dparm->prop->type == PROP_POINTER) && !(dparm->prop->flag & PROP_RNAPTR)) || (dparm->prop->arraydimension))? "*": "";
1563                         fprintf(f, "\t*((%s%s%s*)_retdata)= %s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, func->c_ret->identifier);
1564                 }
1565         }
1566
1567         fprintf(f, "}\n\n");
1568
1569         dfunc->gencall= funcname;
1570 }
1571
1572 static void rna_auto_types()
1573 {
1574         StructDefRNA *ds;
1575         PropertyDefRNA *dp;
1576
1577         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
1578                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
1579                 if(ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
1580                         ds->dnaname= "bScreen";
1581
1582                 for(dp=ds->cont.properties.first; dp; dp=dp->next) {
1583                         if(dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
1584                                 dp->dnastructname= "bScreen";
1585
1586                         if(dp->dnatype) {
1587                                 if(dp->prop->type == PROP_POINTER) {
1588                                         PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;
1589                                         StructRNA *type;
1590
1591                                         if(!pprop->type && !pprop->get)
1592                                                 pprop->type= (StructRNA*)rna_find_type(dp->dnatype);
1593
1594                                         if(pprop->type) {
1595                                                 type= rna_find_struct((char*)pprop->type);
1596                                                 if(type && (type->flag & STRUCT_ID_REFCOUNT))
1597                                                         pprop->property.flag |= PROP_ID_REFCOUNT;
1598                                         }
1599                                 }
1600                                 else if(dp->prop->type== PROP_COLLECTION) {
1601                                         CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1602
1603                                         if(!cprop->item_type && !cprop->get && strcmp(dp->dnatype, "ListBase")==0)
1604                                                 cprop->item_type= (StructRNA*)rna_find_type(dp->dnatype);
1605                                 }
1606                         }
1607                 }
1608         }
1609 }
1610
1611 static void rna_sort(BlenderRNA *brna)
1612 {
1613         StructDefRNA *ds;
1614         StructRNA *srna;
1615
1616         rna_sortlist(&brna->structs, cmp_struct);
1617         rna_sortlist(&DefRNA.structs, cmp_def_struct);
1618
1619         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1620                 rna_sortlist(&srna->cont.properties, cmp_property);
1621
1622         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
1623                 rna_sortlist(&ds->cont.properties, cmp_def_property);
1624 }
1625
1626 static const char *rna_property_structname(PropertyType type)
1627 {
1628         switch(type) {
1629                 case PROP_BOOLEAN: return "BooleanPropertyRNA";
1630                 case PROP_INT: return "IntPropertyRNA";
1631                 case PROP_FLOAT: return "FloatPropertyRNA";
1632                 case PROP_STRING: return "StringPropertyRNA";
1633                 case PROP_ENUM: return "EnumPropertyRNA";
1634                 case PROP_POINTER: return "PointerPropertyRNA";
1635                 case PROP_COLLECTION: return "CollectionPropertyRNA";
1636                 default: return "UnknownPropertyRNA";
1637         }
1638 }
1639
1640 static const char *rna_property_typename(PropertyType type)
1641 {
1642         switch(type) {
1643                 case PROP_BOOLEAN: return "PROP_BOOLEAN";
1644                 case PROP_INT: return "PROP_INT";
1645                 case PROP_FLOAT: return "PROP_FLOAT";
1646                 case PROP_STRING: return "PROP_STRING";
1647                 case PROP_ENUM: return "PROP_ENUM";
1648                 case PROP_POINTER: return "PROP_POINTER";
1649                 case PROP_COLLECTION: return "PROP_COLLECTION";
1650                 default: return "PROP_UNKNOWN";
1651         }
1652 }
1653
1654 static const char *rna_property_subtypename(PropertySubType type)
1655 {
1656         switch(type) {
1657                 case PROP_NONE: return "PROP_NONE";
1658                 case PROP_FILEPATH: return "PROP_FILEPATH";
1659                 case PROP_FILENAME: return "PROP_FILENAME";
1660                 case PROP_DIRPATH: return "PROP_DIRPATH";
1661                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
1662                 case PROP_PERCENTAGE: return "PROP_PERCENTAGE";
1663                 case PROP_FACTOR: return "PROP_FACTOR";
1664                 case PROP_ANGLE: return "PROP_ANGLE";
1665                 case PROP_TIME: return "PROP_TIME";
1666                 case PROP_DISTANCE: return "PROP_DISTANCE";
1667                 case PROP_COLOR: return "PROP_COLOR";
1668                 case PROP_TRANSLATION: return "PROP_TRANSLATION";
1669                 case PROP_DIRECTION: return "PROP_DIRECTION";
1670                 case PROP_MATRIX: return "PROP_MATRIX";
1671                 case PROP_EULER: return "PROP_EULER";
1672                 case PROP_QUATERNION: return "PROP_QUATERNION";
1673                 case PROP_AXISANGLE: return "PROP_AXISANGLE";
1674                 case PROP_VELOCITY: return "PROP_VELOCITY";
1675                 case PROP_ACCELERATION: return "PROP_ACCELERATION";
1676                 case PROP_XYZ: return "PROP_XYZ";
1677                 case PROP_COLOR_GAMMA: return "PROP_COLOR_GAMMA";
1678                 case PROP_LAYER: return "PROP_LAYER";
1679                 case PROP_LAYER_MEMBER: return "PROP_LAYER_MEMBER";
1680                 default: {
1681                         /* incase we dont have a type preset that includes the subtype */
1682                         if(RNA_SUBTYPE_UNIT(type)) {
1683                                 return rna_property_subtypename(type & ~RNA_SUBTYPE_UNIT(type));
1684                         }
1685                         else {
1686                                 return "PROP_SUBTYPE_UNKNOWN";
1687                         }
1688                 }
1689         }
1690 }
1691
1692 static const char *rna_property_subtype_unit(PropertyType type)
1693 {
1694         switch(RNA_SUBTYPE_UNIT(type)) {
1695                 case PROP_UNIT_NONE:            return "PROP_UNIT_NONE";
1696                 case PROP_UNIT_LENGTH:          return "PROP_UNIT_LENGTH";
1697                 case PROP_UNIT_AREA:            return "PROP_UNIT_AREA";
1698                 case PROP_UNIT_VOLUME:          return "PROP_UNIT_VOLUME";
1699                 case PROP_UNIT_MASS:            return "PROP_UNIT_MASS";
1700                 case PROP_UNIT_ROTATION:        return "PROP_UNIT_ROTATION";
1701                 case PROP_UNIT_TIME:            return "PROP_UNIT_TIME";
1702                 case PROP_UNIT_VELOCITY:        return "PROP_UNIT_VELOCITY";
1703                 case PROP_UNIT_ACCELERATION:return "PROP_UNIT_ACCELERATION";
1704                 default:                                        return "PROP_UNIT_UNKNOWN";
1705         }
1706 }
1707
1708 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
1709 {
1710         StructRNA *srna;
1711
1712         for(srna=brna->structs.first; srna; srna=srna->cont.next)
1713                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1714         fprintf(f, "\n");
1715 }
1716
1717 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
1718 {
1719         StructRNA *srna;
1720
1721         fprintf(f, "BlenderRNA BLENDER_RNA = {");
1722
1723         srna= brna->structs.first;
1724         if(srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
1725         else fprintf(f, "{NULL, ");
1726
1727         srna= brna->structs.last;
1728         if(srna) fprintf(f, "&RNA_%s}", srna->identifier);
1729         else fprintf(f, "NULL}");
1730
1731         fprintf(f, "};\n\n");
1732 }
1733
1734 static void rna_generate_property_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1735 {
1736         PropertyRNA *prop;
1737         StructRNA *base;
1738
1739         base= srna->base;
1740         while (base) {
1741                 fprintf(f, "\n");
1742                 for(prop=base->cont.properties.first; prop; prop=prop->next)
1743                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type), base->identifier, prop->identifier);
1744                 base= base->base;
1745         }
1746
1747         if(srna->cont.properties.first)
1748                 fprintf(f, "\n");
1749
1750         for(prop=srna->cont.properties.first; prop; prop=prop->next)
1751                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, prop->identifier);
1752         fprintf(f, "\n");
1753 }
1754
1755 static void rna_generate_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionRNA *func, FILE *f)
1756 {
1757         PropertyRNA *parm;
1758
1759         for(parm= func->cont.properties.first; parm; parm= parm->next)
1760                 fprintf(f, "%s%s rna_%s_%s_%s;\n", "extern ", rna_property_structname(parm->type), srna->identifier, func->identifier, parm->identifier);
1761
1762         if(func->cont.properties.first)
1763                 fprintf(f, "\n");
1764 }
1765
1766 static void rna_generate_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1767 {
1768         FunctionRNA *func;
1769         StructRNA *base;
1770
1771         base= srna->base;
1772         while (base) {
1773                 for(func= base->functions.first; func; func= func->cont.next) {
1774                         fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", base->identifier, func->identifier);
1775                         rna_generate_parameter_prototypes(brna, base, func, f);
1776                 }
1777
1778                 if(base->functions.first)
1779                         fprintf(f, "\n");
1780
1781                 base= base->base;
1782         }
1783
1784         for(func= srna->functions.first; func; func= func->cont.next) {
1785                 fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", srna->identifier, func->identifier);
1786                 rna_generate_parameter_prototypes(brna, srna, func, f);
1787         }
1788
1789         if(srna->functions.first)
1790                 fprintf(f, "\n");
1791 }
1792
1793 static void rna_generate_static_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionDefRNA *dfunc, FILE *f)
1794 {
1795         FunctionRNA *func;
1796         PropertyDefRNA *dparm;
1797         StructDefRNA *dsrna;
1798         PropertyType type;
1799         int flag, pout, cptr, first;
1800         char *ptrstr;
1801
1802         dsrna= rna_find_struct_def(srna);
1803         func= dfunc->func;
1804
1805         /* return type */
1806         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1807                 if(dparm->prop==func->c_ret) {
1808                         if(dparm->prop->arraydimension)
1809                                 fprintf(f, "XXX no array return types yet"); /* XXX not supported */
1810                         else if(dparm->prop->type == PROP_POINTER && !(dparm->prop->flag & PROP_RNAPTR))
1811                                 fprintf(f, "%s%s *", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1812                         else
1813                                 fprintf(f, "%s%s ", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
1814
1815                         break;
1816                 }
1817         }
1818
1819         /* void if nothing to return */
1820         if(!dparm)
1821                 fprintf(f, "void ");
1822
1823         /* function name */
1824         fprintf(f, "%s(", dfunc->call);
1825
1826         first= 1;
1827
1828         /* self, context and reports parameters */
1829         if((func->flag & FUNC_NO_SELF)==0) {
1830                 if(func->flag & FUNC_USE_SELF_ID)
1831                         fprintf(f, "struct ID *_selfid, ");
1832
1833                 if(dsrna->dnaname) fprintf(f, "struct %s *_self", dsrna->dnaname);
1834                 else fprintf(f, "struct %s *_self", srna->identifier);
1835                 first= 0;
1836         }
1837
1838         if(func->flag & FUNC_USE_CONTEXT) {
1839                 if(!first) fprintf(f, ", ");
1840                 first= 0;
1841                 fprintf(f, "bContext *C");
1842         }
1843
1844         if(func->flag & FUNC_USE_REPORTS) {
1845                 if(!first) fprintf(f, ", ");
1846                 first= 0;
1847                 fprintf(f, "ReportList *reports");
1848         }
1849
1850         /* defined parameters */
1851         for(dparm= dfunc->cont.properties.first; dparm; dparm= dparm->next) {
1852                 type = dparm->prop->type;
1853                 flag = dparm->prop->flag;
1854                 pout = (flag & PROP_OUTPUT);
1855                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1856
1857                 if(dparm->prop==func->c_ret)
1858                         continue;
1859
1860                 if (cptr || (flag & PROP_DYNAMIC))
1861                         ptrstr= pout ? "**" : "*";
1862                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
1863                         ptrstr= "*";
1864                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
1865                         ptrstr= "";
1866                 else
1867                         ptrstr= pout ? "*" : "";
1868
1869                 if(!first) fprintf(f, ", ");
1870                 first= 0;
1871
1872                 if (flag & PROP_DYNAMIC)
1873                         fprintf(f, "int %s%s_len, ", pout ? "*" : "", dparm->prop->identifier);
1874
1875                 if(!(flag & PROP_DYNAMIC) && dparm->prop->arraydimension)
1876                         fprintf(f, "%s%s %s[%d]", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier, dparm->prop->totarraylength);
1877                 else
1878                         fprintf(f, "%s%s %s%s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
1879
1880         }
1881
1882         fprintf(f, ");\n");
1883 }
1884
1885 static void rna_generate_static_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1886 {
1887         FunctionRNA *func;
1888         FunctionDefRNA *dfunc;
1889         int first= 1;
1890
1891         for(func= srna->functions.first; func; func= func->cont.next) {
1892                 dfunc= rna_find_function_def(func);
1893
1894                 if(dfunc->call) {
1895                         if(first) {
1896                                 fprintf(f, "/* Repeated prototypes to detect errors */\n\n");
1897                                 first= 0;
1898                         }
1899
1900                         rna_generate_static_parameter_prototypes(brna, srna, dfunc, f);
1901                 }
1902         }
1903
1904         fprintf(f, "\n");
1905 }
1906
1907 static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, PropertyRNA *prop) 
1908 {
1909         char *strnest= "", *errnest= "";
1910         int len, freenest= 0;
1911         
1912         if(nest != NULL) {
1913                 len= strlen(nest);
1914
1915                 strnest= MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> strnest");
1916                 errnest= MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> errnest");
1917
1918                 strcpy(strnest, "_"); strcat(strnest, nest);
1919                 strcpy(errnest, "."); strcat(errnest, nest);
1920
1921                 freenest= 1;
1922         }
1923
1924         switch(prop->type) {
1925                         case PROP_ENUM: {
1926                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
1927                                 int i, defaultfound= 0;
1928
1929                                 if(eprop->item) {
1930                                         fprintf(f, "static EnumPropertyItem rna_%s%s_%s_items[%d] = {\n\t", srna->identifier, strnest, prop->identifier, eprop->totitem+1);
1931
1932                                         for(i=0; i<eprop->totitem; i++) {
1933                                                 fprintf(f, "{%d, ", eprop->item[i].value);
1934                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
1935                                                 fprintf(f, "%d, ", eprop->item[i].icon);
1936                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
1937                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "},\n\t");
1938
1939                                                 if(eprop->item[i].identifier[0])
1940                                                         if(eprop->defaultvalue == eprop->item[i].value)
1941                                                                 defaultfound= 1;
1942                                         }
1943
1944                                         fprintf(f, "{0, NULL, 0, NULL, NULL}\n};\n\n");
1945
1946                                         if(!defaultfound) {
1947                                                 fprintf(stderr, "rna_generate_structs: %s%s.%s, enum default is not in items.\n", srna->identifier, errnest, prop->identifier);
1948                                                 DefRNA.error= 1;
1949                                         }
1950                                 }
1951                                 else {
1952                                         fprintf(stderr, "rna_generate_structs: %s%s.%s, enum must have items defined.\n", srna->identifier, errnest, prop->identifier);
1953                                         DefRNA.error= 1;
1954                                 }
1955                                 break;
1956                         }
1957                         case PROP_BOOLEAN: {
1958                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
1959                                 unsigned int i;
1960
1961                                 if(prop->arraydimension && prop->totarraylength) {
1962                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
1963
1964                                         for(i=0; i<prop->totarraylength; i++) {
1965                                                 if(bprop->defaultarray)
1966                                                         fprintf(f, "%d", bprop->defaultarray[i]);
1967                                                 else
1968                                                         fprintf(f, "%d", bprop->defaultvalue);
1969                                                 if(i != prop->totarraylength-1)
1970                                                         fprintf(f, ",\n\t");
1971                                         }
1972
1973                                         fprintf(f, "\n};\n\n");
1974                                 }
1975                                 break;
1976                         }
1977                         case PROP_INT: {
1978                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
1979                                 unsigned int i;
1980
1981                                 if(prop->arraydimension && prop->totarraylength) {
1982                                         fprintf(f, "static int rna_%s%s_%s_default[%d] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
1983
1984                                         for(i=0; i<prop->totarraylength; i++) {
1985                                                 if(iprop->defaultarray)
1986                                                         fprintf(f, "%d", iprop->defaultarray[i]);
1987                                                 else
1988                                                         fprintf(f, "%d", iprop->defaultvalue);
1989                                                 if(i != prop->totarraylength-1)
1990                                                         fprintf(f, ",\n\t");
1991                                         }
1992
1993                                         fprintf(f, "\n};\n\n");
1994                                 }
1995                                 break;
1996                         }
1997                         case PROP_FLOAT: {
1998                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
1999                                 unsigned int i;
2000
2001                                 if(prop->arraydimension && prop->totarraylength) {
2002                                         fprintf(f, "static float rna_%s%s_%s_default[%d] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
2003
2004                                         for(i=0; i<prop->totarraylength; i++) {
2005                                                 if(fprop->defaultarray)
2006                                                         rna_float_print(f, fprop->defaultarray[i]);
2007                                                 else
2008                                                         rna_float_print(f, fprop->defaultvalue);
2009                                                 if(i != prop->totarraylength-1)
2010                                                         fprintf(f, ",\n\t");
2011                                         }
2012
2013                                         fprintf(f, "\n};\n\n");
2014                                 }
2015                                 break;
2016                         }
2017                         default:
2018                                 break;
2019         }
2020
2021         fprintf(f, "%s%s rna_%s%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, strnest, prop->identifier);
2022
2023         if(prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s%s_%s, ", srna->identifier, strnest, prop->next->identifier);
2024         else fprintf(f, "\t{NULL, ");
2025         if(prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s%s_%s,\n", srna->identifier, strnest, prop->prev->identifier);
2026         else fprintf(f, "NULL,\n");
2027         fprintf(f, "\t%d, ", prop->magic);
2028         rna_print_c_string(f, prop->identifier);
2029         fprintf(f, ", %d, ", prop->flag);
2030         rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
2031         rna_print_c_string(f, prop->description); fprintf(f, ",\n\t");
2032         fprintf(f, "%d,\n", prop->icon);
2033         fprintf(f, "\t%s, %s|%s, %s, %d, {%d, %d, %d}, %d,\n", rna_property_typename(prop->type), rna_property_subtypename(prop->subtype), rna_property_subtype_unit(prop->subtype), rna_function_string(prop->getlength), prop->arraydimension, prop->arraylength[0], prop->arraylength[1], prop->arraylength[2], prop->totarraylength);
2034         fprintf(f, "\t%s%s, %d, %s, %s,\n", (prop->flag & PROP_CONTEXT_UPDATE)? "(UpdateFunc)": "", rna_function_string(prop->update), prop->noteflag, rna_function_string(prop->editable), rna_function_string(prop->itemeditable));
2035
2036         if(prop->flag & PROP_RAW_ACCESS) rna_set_raw_offset(f, srna, prop);
2037         else fprintf(f, "\t0, -1");
2038
2039         /* our own type - collections/arrays only */
2040         if(prop->srna) fprintf(f, ", &RNA_%s", (char*)prop->srna);
2041         else fprintf(f, ", NULL");
2042
2043         fprintf(f, "},\n");
2044
2045         switch(prop->type) {
2046                         case PROP_BOOLEAN: {
2047                                 BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
2048                                 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);
2049                                 if(prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2050                                 else fprintf(f, "NULL\n");
2051                                 break;
2052                         }
2053                         case PROP_INT: {
2054                                 IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
2055                                 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));
2056                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
2057                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
2058                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
2059                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
2060                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
2061                                 rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
2062                                 if(prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2063                                 else fprintf(f, "NULL\n");
2064                                 break;
2065                          }
2066                         case PROP_FLOAT: {
2067                                 FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
2068                                 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));
2069                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
2070                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
2071                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
2072                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
2073                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
2074                                 rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
2075                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
2076                                 if(prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2077                                 else fprintf(f, "NULL\n");
2078                                 break;
2079                          }
2080                         case PROP_STRING: {
2081                                 StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
2082                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
2083                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
2084                                 break;
2085                         }
2086                         case PROP_ENUM: {
2087                                 EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
2088                                 fprintf(f, "\t%s, %s, %s, ", rna_function_string(eprop->get), rna_function_string(eprop->set), rna_function_string(eprop->itemf));
2089                                 if(eprop->item)
2090                                         fprintf(f, "rna_%s%s_%s_items, ", srna->identifier, strnest, prop->identifier);
2091                                 else
2092                                         fprintf(f, "NULL, ");
2093                                 fprintf(f, "%d, %d\n", eprop->totitem, eprop->defaultvalue);
2094                                 break;
2095                         }
2096                         case PROP_POINTER: {
2097                                 PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
2098                                 fprintf(f, "\t%s, %s, %s, %s,", rna_function_string(pprop->get), rna_function_string(pprop->set), rna_function_string(pprop->typef), rna_function_string(pprop->poll));
2099                                 if(pprop->type) fprintf(f, "&RNA_%s\n", (char*)pprop->type);
2100                                 else fprintf(f, "NULL\n");
2101                                 break;
2102                          }
2103                         case PROP_COLLECTION: {
2104                                 CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
2105                                 fprintf(f, "\t%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->length), rna_function_string(cprop->lookupint), rna_function_string(cprop->lookupstring));
2106                                 if(cprop->item_type) fprintf(f, "&RNA_%s\n", (char*)cprop->item_type);
2107                                 else fprintf(f, "NULL\n");
2108                                 break;
2109                         }
2110         }
2111
2112         fprintf(f, "};\n\n");
2113
2114         if(freenest) {
2115                 MEM_freeN(strnest);
2116                 MEM_freeN(errnest);
2117         }
2118 }
2119
2120 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
2121 {
2122         FunctionRNA *func;
2123         FunctionDefRNA *dfunc;
2124         PropertyRNA *prop, *parm;
2125         StructRNA *base;
2126
2127         fprintf(f, "/* %s */\n", srna->name);
2128
2129         for(prop= srna->cont.properties.first; prop; prop= prop->next)
2130                 rna_generate_property(f, srna, NULL, prop);
2131
2132         for(func= srna->functions.first; func; func= func->cont.next) {
2133                 for(parm= func->cont.properties.first; parm; parm= parm->next)
2134                         rna_generate_property(f, srna, func->identifier, parm);
2135
2136                 fprintf(f, "%s%s rna_%s_%s_func = {\n", "", "FunctionRNA", srna->identifier, func->identifier);
2137
2138                 if(func->cont.next) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, ((FunctionRNA*)func->cont.next)->identifier);
2139                 else fprintf(f, "\t{NULL, ");
2140                 if(func->cont.prev) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func,\n", srna->identifier, ((FunctionRNA*)func->cont.prev)->identifier);
2141                 else fprintf(f, "NULL,\n");
2142
2143                 fprintf(f, "\tNULL,\n");
2144
2145                 parm= func->cont.properties.first;
2146                 if(parm) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s_%s, ", srna->identifier, func->identifier, parm->identifier);
2147                 else fprintf(f, "\t{NULL, ");
2148
2149                 parm= func->cont.properties.last;
2150                 if(parm) fprintf(f, "(PropertyRNA*)&rna_%s_%s_%s}},\n", srna->identifier, func->identifier, parm->identifier);
2151                 else fprintf(f, "NULL}},\n");
2152
2153                 fprintf(f, "\t");
2154                 rna_print_c_string(f, func->identifier);
2155                 fprintf(f, ", %d, ", func->flag);
2156                 rna_print_c_string(f, func->description); fprintf(f, ",\n");
2157
2158                 dfunc= rna_find_function_def(func);
2159                 if(dfunc->gencall) fprintf(f, "\t%s,\n", dfunc->gencall);
2160                 else fprintf(f, "\tNULL,\n");
2161
2162                 if(func->c_ret) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s_%s\n", srna->identifier, func->identifier, func->c_ret->identifier);
2163                 else fprintf(f, "\tNULL\n");
2164
2165                 fprintf(f, "};\n");
2166                 fprintf(f, "\n");
2167         }
2168
2169         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
2170
2171         if(srna->cont.next) fprintf(f, "\t{(ContainerRNA *)&RNA_%s, ", ((StructRNA*)srna->cont.next)->identifier);
2172         else fprintf(f, "\t{NULL, ");
2173         if(srna->cont.prev) fprintf(f, "(ContainerRNA *)&RNA_%s,\n", ((StructRNA*)srna->cont.prev)->identifier);
2174         else fprintf(f, "NULL,\n");
2175
2176         fprintf(f, "\tNULL,\n");
2177
2178         prop= srna->cont.properties.first;
2179         if(prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
2180         else fprintf(f, "\t{NULL, ");
2181
2182         prop= srna->cont.properties.last;
2183         if(prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}},\n", srna->identifier, prop->identifier);
2184         else fprintf(f, "NULL}},\n");
2185
2186         fprintf(f, "\tNULL,NULL,\n"); /* PyType - Cant initialize here */
2187         
2188         fprintf(f, "\t");
2189         rna_print_c_string(f, srna->identifier);
2190         fprintf(f, ", %d, ", srna->flag);
2191         rna_print_c_string(f, srna->name);
2192         fprintf(f, ", ");
2193         rna_print_c_string(f, srna->description);
2194         fprintf(f, ",\n\t%d,\n", srna->icon);
2195
2196         prop= srna->nameproperty;
2197         if(prop) {
2198                 base= srna;
2199                 while (base->base && base->base->nameproperty==prop)
2200                         base= base->base;
2201
2202                 fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", base->identifier, prop->identifier);
2203         }
2204         else fprintf(f, "\tNULL, ");
2205
2206         prop= srna->iteratorproperty;
2207         base= srna;
2208         while (base->base && base->base->iteratorproperty==prop)
2209                 base= base->base;
2210         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", base->identifier);
2211
2212         if(srna->base) fprintf(f, "\t&RNA_%s,\n", srna->base->identifier);
2213         else fprintf(f, "\tNULL,\n");
2214
2215         if(srna->nested) fprintf(f, "\t&RNA_%s,\n", srna->nested->identifier);
2216         else fprintf(f, "\tNULL,\n");
2217
2218         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
2219         fprintf(f, "\t%s,\n", rna_function_string(srna->path));
2220         fprintf(f, "\t%s,\n", rna_function_string(srna->reg));
2221         fprintf(f, "\t%s,\n", rna_function_string(srna->unreg));
2222         fprintf(f, "\t%s,\n", rna_function_string(srna->idproperties));
2223
2224         if(srna->reg && !srna->refine) {
2225                 fprintf(stderr, "rna_generate_struct: %s has a register function, must also have refine function.\n", srna->identifier);
2226                 DefRNA.error= 1;
2227         }
2228
2229         func= srna->functions.first;
2230         if(func) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, func->identifier);
2231         else fprintf(f, "\t{NULL, ");
2232
2233         func= srna->functions.last;
2234         if(func) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func}\n", srna->identifier, func->identifier);
2235         else fprintf(f, "NULL}\n");
2236
2237         fprintf(f, "};\n");
2238
2239         fprintf(f, "\n");
2240 }
2241
2242 typedef struct RNAProcessItem {
2243         char *filename;
2244         char *api_filename;
2245         void (*define)(BlenderRNA *brna);
2246 } RNAProcessItem;
2247
2248 RNAProcessItem PROCESS_ITEMS[]= {
2249         {"rna_rna.c", NULL, RNA_def_rna},
2250         {"rna_ID.c", NULL, RNA_def_ID},
2251         {"rna_texture.c", NULL, RNA_def_texture},
2252         {"rna_action.c", "rna_action_api.c", RNA_def_action},
2253         {"rna_animation.c", "rna_animation_api.c", RNA_def_animation},
2254         {"rna_animviz.c", NULL, RNA_def_animviz},
2255         {"rna_actuator.c", "rna_actuator_api.c", RNA_def_actuator},
2256         {"rna_armature.c", "rna_armature_api.c", RNA_def_armature},
2257         {"rna_boid.c", NULL, RNA_def_boid},
2258         {"rna_brush.c", NULL, RNA_def_brush},
2259         {"rna_camera.c", NULL, RNA_def_camera},
2260         {"rna_cloth.c", NULL, RNA_def_cloth},
2261         {"rna_color.c", NULL, RNA_def_color},
2262         {"rna_constraint.c", NULL, RNA_def_constraint},
2263         {"rna_context.c", NULL, RNA_def_context},
2264         {"rna_controller.c", "rna_controller_api.c", RNA_def_controller},
2265         {"rna_curve.c", NULL, RNA_def_curve},
2266         {"rna_fcurve.c", "rna_fcurve_api.c", RNA_def_fcurve},
2267         {"rna_fluidsim.c", NULL, RNA_def_fluidsim},
2268         {"rna_gpencil.c", NULL, RNA_def_gpencil},
2269         {"rna_group.c", NULL, RNA_def_group},
2270         {"rna_image.c", "rna_image_api.c", RNA_def_image},
2271         {"rna_key.c", NULL, RNA_def_key},
2272         {"rna_lamp.c", NULL, RNA_def_lamp},
2273         {"rna_lattice.c", NULL, RNA_def_lattice},
2274         {"rna_main.c", "rna_main_api.c", RNA_def_main},
2275         {"rna_material.c", "rna_material_api.c", RNA_def_material},
2276         {"rna_mesh.c", "rna_mesh_api.c", RNA_def_mesh},
2277         {"rna_meta.c", NULL, RNA_def_meta},
2278         {"rna_modifier.c", NULL, RNA_def_modifier},
2279         {"rna_nla.c", NULL, RNA_def_nla},
2280         {"rna_nodetree.c", NULL, RNA_def_nodetree},
2281         {"rna_object.c", "rna_object_api.c", RNA_def_object},
2282         {"rna_object_force.c", NULL, RNA_def_object_force},
2283         {"rna_packedfile.c", NULL, RNA_def_packedfile},
2284         {"rna_particle.c", NULL, RNA_def_particle},
2285         {"rna_pose.c", "rna_pose_api.c", RNA_def_pose},
2286         {"rna_property.c", NULL, RNA_def_gameproperty},
2287         {"rna_render.c", NULL, RNA_def_render},
2288         {"rna_scene.c", "rna_scene_api.c", RNA_def_scene},
2289         {"rna_screen.c", NULL, RNA_def_screen},
2290         {"rna_sculpt_paint.c", NULL, RNA_def_sculpt_paint},
2291         {"rna_sensor.c", "rna_sensor_api.c", RNA_def_sensor},
2292         {"rna_sequencer.c", "rna_sequencer_api.c", RNA_def_sequencer},
2293         {"rna_smoke.c", NULL, RNA_def_smoke},
2294         {"rna_space.c", NULL, RNA_def_space},
2295         {"rna_test.c", NULL, RNA_def_test},
2296         {"rna_text.c", NULL, RNA_def_text},
2297         {"rna_timeline.c", NULL, RNA_def_timeline_marker},
2298         {"rna_sound.c", NULL, RNA_def_sound},
2299         {"rna_ui.c", "rna_ui_api.c", RNA_def_ui},
2300         {"rna_userdef.c", NULL, RNA_def_userdef},
2301         {"rna_vfont.c", NULL, RNA_def_vfont},
2302         {"rna_wm.c", "rna_wm_api.c", RNA_def_wm},
2303         {"rna_world.c", NULL, RNA_def_world},   
2304         {NULL, NULL}};
2305
2306 static void rna_generate(BlenderRNA *brna, FILE *f, char *filename, char *api_filename)
2307 {
2308         StructDefRNA *ds;
2309         PropertyDefRNA *dp;
2310         FunctionDefRNA *dfunc;
2311         
2312         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
2313                                  "   Do not edit manually, changes will be overwritten.           */\n\n"
2314                                   "#define RNA_RUNTIME\n\n");
2315
2316         fprintf(f, "#include <float.h>\n");
2317         fprintf(f, "#include <limits.h>\n");
2318         fprintf(f, "#include <string.h>\n\n");
2319         fprintf(f, "#include <stddef.h>\n\n");
2320
2321         fprintf(f, "#include \"DNA_ID.h\"\n");
2322         fprintf(f, "#include \"DNA_scene_types.h\"\n");
2323
2324         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
2325
2326         fprintf(f, "#include \"BKE_context.h\"\n");
2327         fprintf(f, "#include \"BKE_library.h\"\n");
2328         fprintf(f, "#include \"BKE_main.h\"\n");
2329         fprintf(f, "#include \"BKE_report.h\"\n");
2330         fprintf(f, "#include \"BKE_utildefines.h\"\n\n");
2331
2332         fprintf(f, "#include \"RNA_define.h\"\n");
2333         fprintf(f, "#include \"RNA_types.h\"\n");
2334         fprintf(f, "#include \"rna_internal.h\"\n\n");
2335
2336         rna_generate_prototypes(brna, f);
2337
2338         fprintf(f, "#include \"%s\"\n", filename);
2339         if(api_filename)
2340                 fprintf(f, "#include \"%s\"\n", api_filename);
2341         fprintf(f, "\n");
2342
2343         fprintf(f, "/* Autogenerated Functions */\n\n");
2344
2345         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2346                 if(!filename || ds->filename == filename) {
2347                         rna_generate_property_prototypes(brna, ds->srna, f);
2348                         rna_generate_function_prototypes(brna, ds->srna, f);
2349                 }
2350         }
2351
2352         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2353                 if(!filename || ds->filename == filename)
2354                         for(dp=ds->cont.properties.first; dp; dp=dp->next)
2355                                 rna_def_property_funcs(f, ds->srna, dp);
2356
2357         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2358                 if(!filename || ds->filename == filename) {
2359                         for(dfunc=ds->functions.first; dfunc; dfunc= dfunc->cont.next)
2360                                 rna_def_function_funcs(f, ds, dfunc);
2361
2362                         rna_generate_static_function_prototypes(brna, ds->srna, f);
2363                 }
2364         }
2365
2366         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2367                 if(!filename || ds->filename == filename)
2368                         rna_generate_struct(brna, ds->srna, f);
2369
2370         if(strcmp(filename, "rna_ID.c") == 0) {
2371                 /* this is ugly, but we cannot have c files compiled for both
2372                  * makesrna and blender with some build systems at the moment */
2373                 fprintf(f, "#include \"rna_define.c\"\n\n");
2374
2375                 rna_generate_blender(brna, f);
2376         }
2377 }
2378
2379 static void rna_generate_header(BlenderRNA *brna, FILE *f)
2380 {
2381         StructDefRNA *ds;
2382         PropertyDefRNA *dp;
2383         StructRNA *srna;
2384
2385         fprintf(f, "\n#ifndef __RNA_BLENDER_H__\n");
2386         fprintf(f, "#define __RNA_BLENDER_H__\n\n");
2387
2388         fprintf(f, "/* Automatically generated function declarations for the Data API.\n"
2389                                  "   Do not edit manually, changes will be overwritten.              */\n\n");
2390
2391         fprintf(f, "#include \"RNA_types.h\"\n\n");
2392
2393         fprintf(f, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
2394
2395         fprintf(f, "#define FOREACH_BEGIN(property, sptr, itemptr) \\\n");
2396         fprintf(f, "    { \\\n");
2397         fprintf(f, "            CollectionPropertyIterator rna_macro_iter; \\\n");
2398         fprintf(f, "            for(property##_begin(&rna_macro_iter, sptr); rna_macro_iter.valid; property##_next(&rna_macro_iter)) { \\\n");
2399         fprintf(f, "                    itemptr= rna_macro_iter.ptr;\n\n");
2400
2401         fprintf(f, "#define FOREACH_END(property) \\\n");
2402         fprintf(f, "            } \\\n");
2403         fprintf(f, "            property##_end(&rna_macro_iter); \\\n");
2404         fprintf(f, "    }\n\n");
2405
2406         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2407                 srna= ds->srna;
2408
2409                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2410
2411                 while(srna) {
2412                         fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
2413                         srna= srna->base;
2414                 }
2415                 fprintf(f, "\n");
2416
2417                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2418                         rna_def_property_funcs_header(f, ds->srna, dp);
2419         }
2420
2421         fprintf(f, "#ifdef __cplusplus\n}\n#endif\n\n");
2422
2423         fprintf(f, "#endif /* __RNA_BLENDER_H__ */\n\n");
2424 }
2425
2426 static const char *cpp_classes = ""
2427 "\n"
2428 "#include <string>\n"
2429 "\n"
2430 "namespace RNA {\n"
2431 "\n"
2432 "#define BOOLEAN_PROPERTY(sname, identifier) \\\n"
2433 "       bool sname::identifier(void) { return (bool)sname##_##identifier##_get(&ptr); }\n"
2434 "\n"
2435 "#define BOOLEAN_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2436 "       Array<int,size> sname::identifier(void) \\\n"
2437 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2438 "\n"
2439 "#define INT_PROPERTY(sname, identifier) \\\n"
2440 "       int sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2441 "\n"
2442 "#define INT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2443 "       Array<int,size> sname::identifier(void) \\\n"
2444 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2445 "\n"
2446 "#define FLOAT_PROPERTY(sname, identifier) \\\n"
2447 "       float sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2448 "\n"
2449 "#define FLOAT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2450 "       Array<float,size> sname::identifier(void) \\\n"
2451 "               { Array<float, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2452 "\n"
2453 "#define ENUM_PROPERTY(type, sname, identifier) \\\n"
2454 "       sname::type sname::identifier(void) { return (type)sname##_##identifier##_get(&ptr); }\n"
2455 "\n"
2456 "#define STRING_PROPERTY(sname, identifier) \\\n"
2457 "       std::string sname::identifier(void) { \\\n"
2458 "               int len= sname##_##identifier##_length(&ptr); \\\n"
2459 "               std::string str; str.resize(len); \\\n"
2460 "               sname##_##identifier##_get(&ptr, &str[0]); return str; } \\\n"
2461 "\n"
2462 "#define POINTER_PROPERTY(type, sname, identifier) \\\n"
2463 "       type sname::identifier(void) { return type(sname##_##identifier##_get(&ptr)); }\n"
2464 "\n"
2465 "#define COLLECTION_PROPERTY(type, sname, identifier) \\\n"
2466 "       typedef CollectionIterator<type, sname##_##identifier##_begin, \\\n"
2467 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier##_iterator; \\\n"
2468 "       Collection<sname, type, sname##_##identifier##_begin, \\\n"
2469 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier;\n"
2470 "\n"
2471 "class Pointer {\n"
2472 "public:\n"
2473 "       Pointer(const PointerRNA& p) : ptr(p) { }\n"
2474 "       operator const PointerRNA&() { return ptr; }\n"
2475 "       bool is_a(StructRNA *type) { return RNA_struct_is_a(&ptr, type); }\n"
2476 "       operator void*() { return ptr.data; }\n"
2477 "       operator bool() { return ptr.data != NULL; }\n"
2478 "\n"
2479 "       PointerRNA ptr;\n"
2480 "};\n"
2481 "\n"
2482 "\n"
2483 "template<typename T, int Tsize>\n"
2484 "class Array {\n"
2485 "public:\n"
2486 "       T data[Tsize];\n"
2487 "       operator T*() { return data; }\n"
2488 "};\n"
2489 "\n"
2490 "typedef void (*TBeginFunc)(CollectionPropertyIterator *iter, PointerRNA *ptr);\n"
2491 "typedef void (*TNextFunc)(CollectionPropertyIterator *iter);\n"
2492 "typedef void (*TEndFunc)(CollectionPropertyIterator *iter);\n"
2493 "\n"
2494 "template<typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2495 "class CollectionIterator {\n"
2496 "public:\n"
2497 "       CollectionIterator() : t(iter.ptr), init(false) { iter.valid= false; }\n"
2498 "       ~CollectionIterator(void) { if(init) Tend(&iter); };\n"
2499 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator=(const CollectionIterator<T, Tbegin, Tnext, Tend>& copy)\n"
2500 "       { if(init) Tend(&iter); iter= copy.iter; if(iter.internal) iter.internal= MEM_dupallocN(iter.internal); t= copy.t; init= copy.init; return *this; }\n"
2501 "\n"
2502 "       operator bool(void)\n"
2503 "       { return iter.valid != 0; }\n"
2504 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator++() { Tnext(&iter); t = T(iter.ptr); return *this; }\n"
2505 "       T& operator*(void) { return t; }\n"
2506 "       T* operator->(void) { return &t; }\n"
2507 "       bool operator==(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid == other.iter.valid; }\n"
2508 "       bool operator!=(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid != other.iter.valid; }\n"
2509 "\n"
2510 "       void begin(const Pointer& ptr)\n"
2511 "       { if(init) Tend(&iter); Tbegin(&iter, (PointerRNA*)&ptr.ptr); t = T(iter.ptr); init = true; }\n"
2512 "\n"
2513 "private:\n"
2514 "       CollectionPropertyIterator iter;\n"
2515 "       T t;\n"
2516 "       bool init;\n"
2517 "};\n"
2518 "\n"
2519 "template<typename Tp, typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2520 "class Collection {\n"
2521 "public:\n"
2522 "       Collection(const PointerRNA& p) : ptr(p) {}\n"
2523 "\n"
2524 "       CollectionIterator<T, Tbegin, Tnext, Tend> begin()\n"
2525 "       { CollectionIterator<T, Tbegin, Tnext, Tend> iter; iter.begin(ptr); return iter; }\n"
2526 "       CollectionIterator<T, Tbegin, Tnext, Tend> end()\n"
2527 "       { return CollectionIterator<T, Tbegin, Tnext, Tend>(); } /* test */ \n"
2528 "\n"
2529 "private:\n"
2530 "       PointerRNA ptr;\n"
2531 "};\n"
2532 "\n";
2533
2534 static void rna_generate_header_cpp(BlenderRNA *brna, FILE *f)
2535 {
2536         StructDefRNA *ds;
2537         PropertyDefRNA *dp;
2538         StructRNA *srna;
2539
2540         fprintf(f, "\n#ifndef __RNA_BLENDER_CPP_H__\n");
2541         fprintf(f, "#define __RNA_BLENDER_CPP_H__\n\n");
2542
2543         fprintf(f, "/* Automatically generated classes for the Data API.\n"
2544                                  "   Do not edit manually, changes will be overwritten. */\n\n");
2545
2546         fprintf(f, "#include \"RNA_blender.h\"\n");
2547         fprintf(f, "#include \"RNA_types.h\"\n");
2548
2549         fprintf(f, "%s", cpp_classes);
2550
2551         fprintf(f, "/**************** Declarations ****************/\n\n");
2552
2553         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2554                 fprintf(f, "class %s;\n", ds->srna->identifier);
2555         fprintf(f, "\n");
2556
2557         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2558                 srna= ds->srna;
2559
2560                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2561
2562                 fprintf(f, "class %s : public %s {\n", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2563                 fprintf(f, "public:\n");
2564                 fprintf(f, "\t%s(const PointerRNA& ptr) :\n\t\t%s(ptr)", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2565                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2566                         if(!(dp->prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN)))
2567                                 if(dp->prop->type == PROP_COLLECTION)
2568                                         fprintf(f, ",\n\t\t%s(ptr)", dp->prop->identifier);
2569                 fprintf(f, "\n\t\t{}\n\n");
2570
2571                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2572                         rna_def_property_funcs_header_cpp(f, ds->srna, dp);
2573                 fprintf(f, "};\n\n");
2574         }
2575
2576
2577         fprintf(f, "/**************** Implementation ****************/\n");
2578
2579         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next) {
2580                 for(dp=ds->cont.properties.first; dp; dp=dp->next)
2581                         rna_def_property_funcs_impl_cpp(f, ds->srna, dp);
2582
2583                 fprintf(f, "\n");
2584         }
2585
2586         fprintf(f, "}\n\n#endif /* __RNA_BLENDER_CPP_H__ */\n\n");
2587 }
2588
2589 static void make_bad_file(char *file, int line)
2590 {
2591         FILE *fp= fopen(file, "w");
2592         fprintf(fp, "#error \"Error! can't make correct RNA file from %s:%d, STUPID!\"\n", __FILE__, line);
2593         fclose(fp);
2594 }
2595
2596 static int rna_preprocess(char *outfile)
2597 {
2598         BlenderRNA *brna;
2599         StructDefRNA *ds;
2600         FILE *file;
2601         char deffile[4096];
2602         int i, status;
2603
2604         /* define rna */
2605         brna= RNA_create();
2606
2607         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2608                 if(PROCESS_ITEMS[i].define) {
2609                         PROCESS_ITEMS[i].define(brna);
2610
2611                         for(ds=DefRNA.structs.first; ds; ds=ds->cont.next)
2612                                 if(!ds->filename)
2613                                         ds->filename= PROCESS_ITEMS[i].filename;
2614                 }
2615         }
2616
2617         rna_auto_types();
2618
2619
2620         /* create RNA_blender_cpp.h */
2621         strcpy(deffile, outfile);
2622         strcat(deffile, "RNA_blender_cpp.h" TMP_EXT);
2623
2624         status= (DefRNA.error != 0);
2625
2626         if(status) {
2627                 make_bad_file(deffile, __LINE__);
2628         }
2629         else {
2630                 file = fopen(deffile, "w");
2631
2632                 if(!file) {
2633                         printf ("Unable to open file: %s\n", deffile);
2634                         status = 1;
2635                 }
2636                 else {
2637                         rna_generate_header_cpp(brna, file);
2638                         fclose(file);
2639                         status= (DefRNA.error != 0);
2640                 }
2641         }
2642
2643         replace_if_different(deffile);
2644
2645         rna_sort(brna);
2646
2647         /* create rna_gen_*.c files */
2648         for(i=0; PROCESS_ITEMS[i].filename; i++) {
2649                 strcpy(deffile, outfile);
2650                 strcat(deffile, PROCESS_ITEMS[i].filename);
2651                 deffile[strlen(deffile)-2] = '\0';
2652                 strcat(deffile, "_gen.c" TMP_EXT);
2653
2654                 if(status) {
2655                         make_bad_file(deffile, __LINE__);
2656                 }
2657                 else {
2658                         file = fopen(deffile, "w");
2659
2660                         if(!file) {
2661                                 printf ("Unable to open file: %s\n", deffile);
2662                                 status = 1;
2663                         }
2664                         else {
2665                                 rna_generate(brna, file, PROCESS_ITEMS[i].filename, PROCESS_ITEMS[i].api_filename);
2666                                 fclose(file);
2667                                 status= (DefRNA.error != 0);
2668                         }
2669                 }
2670
2671                 replace_if_different(deffile);
2672         }
2673
2674         /* create RNA_blender.h */
2675         strcpy(deffile, outfile);
2676         strcat(deffile, "RNA_blender.h" TMP_EXT);
2677
2678         if(status) {
2679                 make_bad_file(deffile, __LINE__);
2680         }
2681         else {
2682                 file = fopen(deffile, "w");
2683
2684                 if(!file) {
2685                         printf ("Unable to open file: %s\n", deffile);
2686                         status = 1;
2687                 }
2688                 else {
2689                         rna_generate_header(brna, file);
2690                         fclose(file);
2691                         status= (DefRNA.error != 0);
2692                 }
2693         }
2694
2695         replace_if_different(deffile);
2696
2697         /* free RNA */
2698         RNA_define_free(brna);
2699         RNA_free(brna);
2700
2701         return status;
2702 }
2703
2704 static void mem_error_cb(const char *errorStr)
2705 {
2706         fprintf(stderr, "%s", errorStr);
2707         fflush(stderr);
2708 }
2709
2710 int main(int argc, char **argv)
2711 {
2712         int totblock, return_status = 0;
2713
2714         if(argc<2) {
2715                 printf("Usage: %s outdirectory/\n", argv[0]);
2716                 return_status = 1;
2717         }
2718         else {
2719                 printf("Running makesrna, program versions %s\n",  RNA_VERSION_DATE);
2720                 return_status= rna_preprocess(argv[1]);
2721         }
2722
2723         totblock= MEM_get_memory_blocks_in_use();
2724         if(totblock!=0) {
2725                 printf("Error Totblock: %d\n",totblock);
2726                 MEM_set_error_callback(mem_error_cb);
2727                 MEM_printmemlist();
2728         }
2729
2730         return return_status;
2731 }
2732
2733