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