Welcome to mirror list, hosted at ThFree Co, Russian Federation.

makesrna.c « intern « makesrna « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 03b1855cefbf4ef4a76ab199434faec261023de8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
/**
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contributor(s): Blender Foundation (2008).
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include <float.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "MEM_guardedalloc.h"

#include "RNA_access.h"
#include "RNA_define.h"
#include "RNA_types.h"

#include "rna_internal.h"

#define RNA_VERSION_DATE "$Id$"

#ifdef _WIN32
#ifndef snprintf
#define snprintf _snprintf
#endif
#endif

/* Sorting */

int cmp_struct(const void *a, const void *b)
{
	const StructRNA *structa= *(const StructRNA**)a;
	const StructRNA *structb= *(const StructRNA**)b;

	return strcmp(structa->identifier, structb->identifier);
}

int cmp_property(const void *a, const void *b)
{
	const PropertyRNA *propa= *(const PropertyRNA**)a;
	const PropertyRNA *propb= *(const PropertyRNA**)b;

	if(strcmp(propa->identifier, "rna_type") == 0) return -1;
	else if(strcmp(propb->identifier, "rna_type") == 0) return 1;

	if(strcmp(propa->identifier, "name") == 0) return -1;
	else if(strcmp(propb->identifier, "name") == 0) return 1;

	return strcmp(propa->name, propb->name);
}

void rna_sortlist(ListBase *listbase, int(*cmp)(const void*, const void*))
{
	Link *link;
	void **array;
	int a, size;
	
	if(listbase->first == listbase->last)
		return;

	for(size=0, link=listbase->first; link; link=link->next)
		size++;

	array= MEM_mallocN(sizeof(void*)*size, "rna_sortlist");
	for(a=0, link=listbase->first; link; link=link->next, a++)
		array[a]= link;

	qsort(array, size, sizeof(void*), cmp);

	listbase->first= listbase->last= NULL;
	for(a=0; a<size; a++) {
		link= array[a];
		link->next= link->prev= NULL;
		rna_addtail(listbase, link);
	}

	MEM_freeN(array);
}

/* Preprocessing */

static void rna_print_c_string(FILE *f, const char *str)
{
	static char *escape[] = {"\''", "\"\"", "\??", "\\\\","\aa", "\bb", "\ff", "\nn", "\rr", "\tt", "\vv", NULL};
	int i, j;

	fprintf(f, "\"");
	for(i=0; str[i]; i++) {
		for(j=0; escape[j]; j++)
			if(str[i] == escape[j][0])
				break;

		if(escape[j]) fprintf(f, "\\%c", escape[j][1]);
		else fprintf(f, "%c", str[i]);
	}
	fprintf(f, "\"");
}

static void rna_print_data_get(FILE *f, PropertyDefRNA *dp)
{
	if(dp->dnastructfromname && dp->dnastructfromprop)
		fprintf(f, "	%s *data= (%s*)(((%s*)ptr->data)->%s);\n", dp->dnastructname, dp->dnastructname, dp->dnastructfromname, dp->dnastructfromprop);
	else
		fprintf(f, "	%s *data= (%s*)(ptr->data);\n", dp->dnastructname, dp->dnastructname);
}

static char *rna_alloc_function_name(const char *structname, const char *propname, const char *type)
{
	AllocDefRNA *alloc;
	char buffer[2048];
	char *result;

	snprintf(buffer, sizeof(buffer), "rna_%s_%s_%s", structname, propname, type);
	result= MEM_callocN(sizeof(char)*strlen(buffer)+1, "rna_alloc_function_name");
	strcpy(result, buffer);

	alloc= MEM_callocN(sizeof(AllocDefRNA), "AllocDefRNA");
	alloc->mem= result;
	rna_addtail(&DefRNA.allocs, alloc);

	return result;
}

static const char *rna_type_type(PropertyRNA *prop)
{
	switch(prop->type) {
		case PROP_BOOLEAN:
		case PROP_INT:
		case PROP_ENUM:
			return "int";
		case PROP_FLOAT:
			return "float";
		case PROP_STRING:
			return "char*";
		default:
			return "void*";
	}
}

static int rna_enum_bitmask(PropertyRNA *prop)
{
	EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
	int a, mask= 0;

	for(a=0; a<eprop->totitem; a++)
		mask |= eprop->item[a].value;
	
	return mask;
}

static char *rna_def_property_get_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp)
{
	char *func;

	if(prop->flag & PROP_IDPROPERTY)
		return NULL;

	if(!dp->dnastructname || !dp->dnaname) {
		fprintf(stderr, "rna_def_property_get_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
		DefRNA.error= 1;
		return NULL;
	}

	if(prop->type == PROP_STRING && ((StringPropertyRNA*)prop)->maxlength == 0) {
		fprintf(stderr, "rna_def_property_get_func: string %s.%s has max length 0.\n", srna->identifier, prop->identifier);
		DefRNA.error= 1;
		return NULL;
	}

	func= rna_alloc_function_name(srna->identifier, prop->identifier, "get");

	switch(prop->type) {
		case PROP_STRING: {
			StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
			fprintf(f, "static void %s(PointerRNA *ptr, char *value)\n", func);
			fprintf(f, "{\n");
			rna_print_data_get(f, dp);
			fprintf(f, "	BLI_strncpy(value, data->%s, %d);\n", dp->dnaname, sprop->maxlength);
			fprintf(f, "}\n\n");
			break;
		}
		default:
			if(prop->arraylength) {
				fprintf(f, "static %s %s(PointerRNA *ptr, int index)\n", rna_type_type(prop), func);
				fprintf(f, "{\n");
				rna_print_data_get(f, dp);
				if(dp->dnaarraylength == 1) {
					if(prop->type == PROP_BOOLEAN && dp->booleanbit)
						fprintf(f, "	return ((data->%s & (%d<<index)) != 0);\n", dp->dnaname, dp->booleanbit);
					else
						fprintf(f, "	return (%s)((&data->%s)[index]);\n", rna_type_type(prop), dp->dnaname);
				}
				else {
					if(prop->type == PROP_BOOLEAN && dp->booleanbit)
						fprintf(f, "	return ((data->%s[index] & %d) != 0);\n", dp->dnaname, dp->booleanbit);
					else
						fprintf(f, "	return (%s)(data->%s[index]);\n", rna_type_type(prop), dp->dnaname);
				}
				fprintf(f, "}\n\n");
			}
			else {
				fprintf(f, "static %s %s(PointerRNA *ptr)\n", rna_type_type(prop), func);
				fprintf(f, "{\n");
				rna_print_data_get(f, dp);
				if(prop->type == PROP_BOOLEAN && dp->booleanbit)
					fprintf(f, "	return (((data->%s) & %d) != 0);\n", dp->dnaname, dp->booleanbit);
				else if(prop->type == PROP_ENUM && dp->enumbitflags)
					fprintf(f, "	return ((data->%s) & %d);\n", dp->dnaname, rna_enum_bitmask(prop));
				else if(prop->type == PROP_POINTER && dp->dnapointerlevel == 0)
					fprintf(f, "	return (%s)&(data->%s);\n", rna_type_type(prop), dp->dnaname);
				else
					fprintf(f, "	return (%s)(data->%s);\n", rna_type_type(prop), dp->dnaname);
				fprintf(f, "}\n\n");
			}
			break;
	}

	return func;
}

static const char *rna_function_string(void *func)
{
	return (func)? (const char*)func: "NULL";
}

static void rna_float_print(FILE *f, float num)
{
	if(num == -FLT_MAX) fprintf(f, "-FLT_MAX");
	else if(num == FLT_MAX) fprintf(f, "FLT_MAX");
	else if((int)num == num) fprintf(f, "%.1ff", num);
	else fprintf(f, "%.10ff", num);
}

static void rna_int_print(FILE *f, int num)
{
	if(num == INT_MIN) fprintf(f, "INT_MIN");
	else if(num == INT_MAX) fprintf(f, "INT_MAX");
	else fprintf(f, "%d", num);
}

static void rna_clamp_value(FILE *f, PropertyRNA *prop)
{
	if(prop->type == PROP_INT) {
		IntPropertyRNA *iprop= (IntPropertyRNA*)prop;

		if(iprop->hardmin != INT_MIN || iprop->hardmax != INT_MAX) {
			fprintf(f, "	CLAMP(value, ");
			rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
			rna_int_print(f, iprop->hardmax); fprintf(f, ");\n");
		}
	}
	else if(prop->type == PROP_FLOAT) {
		FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;

		if(fprop->hardmin != -FLT_MAX || fprop->hardmax != FLT_MAX) {
			fprintf(f, "	CLAMP(value, ");
			rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
			rna_float_print(f, fprop->hardmax); fprintf(f, ");\n");
		}
	}
}

static char *rna_def_property_set_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp)
{
	char *func;

	if(prop->flag & PROP_IDPROPERTY)
		return NULL;

	if(!dp->dnastructname || !dp->dnaname) {
		if(!(prop->flag & PROP_NOT_EDITABLE)) {
			fprintf(stderr, "rna_def_property_set_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
			DefRNA.error= 1;
		}
		return NULL;
	}

	func= rna_alloc_function_name(srna->identifier, prop->identifier, "set");

	switch(prop->type) {
		case PROP_STRING: {
			StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
			fprintf(f, "static void %s(PointerRNA *ptr, const char *value)\n", func);
			fprintf(f, "{\n");
			rna_print_data_get(f, dp);
			fprintf(f, "	BLI_strncpy(data->%s, value, %d);\n", dp->dnaname, sprop->maxlength);
			fprintf(f, "}\n\n");
			break;
		}
		default:
			if(prop->arraylength) {
				fprintf(f, "static void %s(PointerRNA *ptr, int index, %s value)\n", func, rna_type_type(prop));
				fprintf(f, "{\n");
				rna_print_data_get(f, dp);
				if(dp->dnaarraylength == 1) {
					if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
						fprintf(f, "	if(value) data->%s |= (%d<<index);\n", dp->dnaname, dp->booleanbit);
						fprintf(f, "	else data->%s &= ~(%d<<index);\n", dp->dnaname, dp->booleanbit);
					}
					else {
						rna_clamp_value(f, prop);
						fprintf(f, "	(&data->%s)[index]= value;\n", dp->dnaname);
					}
				}
				else {
					if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
						fprintf(f, "	if(value) data->%s[index] |= %d;\n", dp->dnaname, dp->booleanbit);
						fprintf(f, "	else data->%s[index] &= ~%d;\n", dp->dnaname, dp->booleanbit);
					}
					else {
						rna_clamp_value(f, prop);
						fprintf(f, "	data->%s[index]= value;\n", dp->dnaname);
					}
				}
				fprintf(f, "}\n\n");
			}
			else {
				fprintf(f, "static void %s(PointerRNA *ptr, %s value)\n", func, rna_type_type(prop));
				fprintf(f, "{\n");
				rna_print_data_get(f, dp);
				if(prop->type == PROP_BOOLEAN && dp->booleanbit) {
					fprintf(f, "	if(value) data->%s |= %d;\n", dp->dnaname, dp->booleanbit);
					fprintf(f, "	else data->%s &= ~%d;\n", dp->dnaname, dp->booleanbit);
				}
				else if(prop->type == PROP_ENUM && dp->enumbitflags) {
					fprintf(f, "	data->%s &= ~%d;\n", dp->dnaname, rna_enum_bitmask(prop));
					fprintf(f, "	data->%s |= value;\n", dp->dnaname);
				}
				else {
					rna_clamp_value(f, prop);
					fprintf(f, "	data->%s= value;\n", dp->dnaname);
				}
				fprintf(f, "}\n\n");
			}
			break;
	}

	return func;
}

static char *rna_def_property_length_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp)
{
	char *func= NULL;

	if(prop->flag & PROP_IDPROPERTY)
		return NULL;

	if(prop->type == PROP_STRING) {
		if(!dp->dnastructname || !dp->dnaname) {
			fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
			DefRNA.error= 1;
			return NULL;
		}

		func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");

		fprintf(f, "static int %s(PointerRNA *ptr)\n", func);
		fprintf(f, "{\n");
		rna_print_data_get(f, dp);
		fprintf(f, "	return strlen(data->%s);\n", dp->dnaname);
		fprintf(f, "}\n\n");
	}
	else if(prop->type == PROP_COLLECTION) {
		if(prop->type == PROP_COLLECTION && (!(dp->dnalengthname || dp->dnalengthfixed)|| !dp->dnaname)) {
			fprintf(stderr, "rna_def_property_length_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
			DefRNA.error= 1;
			return NULL;
		}

		func= rna_alloc_function_name(srna->identifier, prop->identifier, "length");

		fprintf(f, "static int %s(PointerRNA *ptr)\n", func);
		fprintf(f, "{\n");
		rna_print_data_get(f, dp);
		if(dp->dnalengthname)
			fprintf(f, "	return (data->%s == NULL)? 0: data->%s;\n", dp->dnaname, dp->dnalengthname);
		else
			fprintf(f, "	return (data->%s == NULL)? 0: %d;\n", dp->dnaname, dp->dnalengthfixed);
		fprintf(f, "}\n\n");
	}

	return func;
}

static char *rna_def_property_begin_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp)
{
	char *func;

	if(prop->flag & PROP_IDPROPERTY)
		return NULL;

	if(!dp->dnastructname || !dp->dnaname) {
		fprintf(stderr, "rna_def_property_begin_func: %s.%s has no valid dna info.\n", srna->identifier, prop->identifier);
		DefRNA.error= 1;
		return NULL;
	}

	func= rna_alloc_function_name(srna->identifier, prop->identifier, "begin");

	if(dp->dnalengthname || dp->dnalengthfixed) {
		fprintf(f, "static void %s(CollectionPropertyIterator *iter, PointerRNA *ptr)\n", func);
		fprintf(f, "{\n");
		rna_print_data_get(f, dp);
		if(dp->dnalengthname)
			fprintf(f, "	rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), data->%s, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthname);
		else
			fprintf(f, "	rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), %d, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthfixed);
		fprintf(f, "}\n\n");
	}
	else {
		fprintf(f, "static void %s(CollectionPropertyIterator *iter, PointerRNA *ptr)\n", func);
		fprintf(f, "{\n");
		rna_print_data_get(f, dp);
		fprintf(f, "	rna_iterator_listbase_begin(iter, &data->%s, NULL);\n", dp->dnaname);
		fprintf(f, "}\n\n");
	}

	return func;
}

static void rna_def_property_funcs(FILE *f, PropertyDefRNA *dp)
{
	PropertyRNA *prop;
	StructRNA *srna;

	srna= dp->srna;
	prop= dp->prop;

	switch(prop->type) {
		case PROP_BOOLEAN: {
			BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;

			if(!prop->arraylength) {
				if(!bprop->get) bprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp);
				if(!bprop->set) bprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp);
			}
			else {
				if(!bprop->getarray) bprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp);
				if(!bprop->setarray) bprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp);
			}
			break;
		}
		case PROP_INT: {
			IntPropertyRNA *iprop= (IntPropertyRNA*)prop;

			if(!prop->arraylength) {
				if(!iprop->get) iprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp);
				if(!iprop->set) iprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp);
			}
			else {
				if(!iprop->getarray) iprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp);
				if(!iprop->setarray) iprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp);
			}
			break;
		}
		case PROP_FLOAT: {
			FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;

			if(!prop->arraylength) {
				if(!fprop->get) fprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp);
				if(!fprop->set) fprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp);
			}
			else {
				if(!fprop->getarray) fprop->getarray= (void*)rna_def_property_get_func(f, srna, prop, dp);
				if(!fprop->setarray) fprop->setarray= (void*)rna_def_property_set_func(f, srna, prop, dp);
			}
			break;
		}
		case PROP_ENUM: {
			EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;

			if(!eprop->get) eprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp);
			if(!eprop->set) eprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp);
			break;
		}
		case PROP_STRING: {
			StringPropertyRNA *sprop= (StringPropertyRNA*)prop;

			if(!sprop->get) sprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp);
			if(!sprop->length) sprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp);
			if(!sprop->set) sprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp);
			break;
		}
		case PROP_POINTER: {
			PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;

			if(!pprop->get) pprop->get= (void*)rna_def_property_get_func(f, srna, prop, dp);
			if(!pprop->set) pprop->set= (void*)rna_def_property_set_func(f, srna, prop, dp);
			if(!pprop->structtype && !pprop->type) {
				fprintf(stderr, "rna_def_property_funcs: %s.%s, pointer must have either type function or fixed type.\n", srna->identifier, prop->identifier);
				DefRNA.error= 1;
			}
			break;
		}
		case PROP_COLLECTION: {
			CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;

			if(dp->dnatype && strcmp(dp->dnatype, "ListBase")==0) {
				if(!cprop->begin)
					cprop->begin= (void*)rna_def_property_begin_func(f, srna, prop, dp);
			}
			else if(dp->dnalengthname || dp->dnalengthfixed) {
				if(!cprop->begin)
					cprop->begin= (void*)rna_def_property_begin_func(f, srna, prop, dp);
				if(!cprop->length)
					cprop->length= (void*)rna_def_property_length_func(f, srna, prop, dp);
			}

			if(!cprop->begin) {
				fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a begin function.\n", srna->identifier, prop->identifier);
				DefRNA.error= 1;
			}
			if(!cprop->next) {
				fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a next function.\n", srna->identifier, prop->identifier);
				DefRNA.error= 1;
			}
			if(!cprop->get) {
				fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have a get function.\n", srna->identifier, prop->identifier);
				DefRNA.error= 1;
			}
			if(!cprop->structtype && !cprop->type) {
				fprintf(stderr, "rna_def_property_funcs: %s.%s, collection must have either type function or fixed type.\n", srna->identifier, prop->identifier);
				DefRNA.error= 1;
			}
			break;
		}
	}
}

static const char *rna_find_type(const char *type)
{
	StructDefRNA *ds;

	for(ds=DefRNA.structs.first; ds; ds=ds->next)
		if(ds->dnaname && strcmp(ds->dnaname, type)==0)
			return ds->srna->identifier;
	
	return NULL;
}

static void rna_auto_types()
{
	StructDefRNA *ds;
	PropertyDefRNA *dp;

	for(ds=DefRNA.structs.first; ds; ds=ds->next) {
		for(dp=ds->properties.first; dp; dp=dp->next) {
			if(dp->dnatype) {
				if(dp->prop->type == PROP_POINTER) {
					PointerPropertyRNA *pprop= (PointerPropertyRNA*)dp->prop;

					if(!pprop->structtype && !pprop->type)
						pprop->structtype= (StructRNA*)rna_find_type(dp->dnatype);
				}
				else if(dp->prop->type== PROP_COLLECTION) {
					CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;

					if(!cprop->structtype && !cprop->type && strcmp(dp->dnatype, "ListBase")==0)
						cprop->structtype= (StructRNA*)rna_find_type(dp->dnatype);
				}
			}
		}
	}
}

static void rna_auto_functions(FILE *f)
{
	StructDefRNA *ds;
	PropertyDefRNA *dp;

	fprintf(f, "/* Autogenerated Functions */\n\n");

	for(ds=DefRNA.structs.first; ds; ds=ds->next)
		for(dp=ds->properties.first; dp; dp=dp->next)
			rna_def_property_funcs(f, dp);
}

static void rna_sort(BlenderRNA *brna)
{
	StructRNA *srna;

	rna_sortlist(&brna->structs, cmp_struct);

	for(srna=brna->structs.first; srna; srna=srna->next)
		rna_sortlist(&srna->properties, cmp_property);
}

static const char *rna_property_structname(PropertyType type)
{
	switch(type) {
		case PROP_BOOLEAN: return "BooleanPropertyRNA";
		case PROP_INT: return "IntPropertyRNA";
		case PROP_FLOAT: return "FloatPropertyRNA";
		case PROP_STRING: return "StringPropertyRNA";
		case PROP_ENUM: return "EnumPropertyRNA";
		case PROP_POINTER: return "PointerPropertyRNA";
		case PROP_COLLECTION: return "CollectionPropertyRNA";
		default: return "UnknownPropertyRNA";
	}
}

static const char *rna_property_typename(PropertyType type)
{
	switch(type) {
		case PROP_BOOLEAN: return "PROP_BOOLEAN";
		case PROP_INT: return "PROP_INT";
		case PROP_FLOAT: return "PROP_FLOAT";
		case PROP_STRING: return "PROP_STRING";
		case PROP_ENUM: return "PROP_ENUM";
		case PROP_POINTER: return "PROP_POINTER";
		case PROP_COLLECTION: return "PROP_COLLECTION";
		default: return "PROP_UNKNOWN";
	}
}

static const char *rna_property_subtypename(PropertyType type)
{
	switch(type) {
		case PROP_NONE: return "PROP_NONE";
		case PROP_UNSIGNED: return "PROP_UNSIGNED";
		case PROP_FILEPATH: return "PROP_FILEPATH";
		case PROP_COLOR: return "PROP_COLOR";
		case PROP_VECTOR: return "PROP_VECTOR";
		case PROP_MATRIX: return "PROP_MATRIX";
		case PROP_ROTATION: return "PROP_ROTATION";
		default: return "PROP_UNKNOWN";
	}
}

static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
{
	StructRNA *srna;

	for(srna=brna->structs.first; srna; srna=srna->next)
		fprintf(f, "StructRNA RNA_%s;\n", srna->identifier);
	fprintf(f, "\n");

	fprintf(f, "BlenderRNA BLENDER_RNA = {");

	srna= brna->structs.first;
	if(srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
	else fprintf(f, "{NULL, ");

	srna= brna->structs.last;
	if(srna) fprintf(f, "&RNA_%s}", srna->identifier);
	else fprintf(f, "NULL}");

	fprintf(f, "};\n\n");
}

static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
{
	PropertyRNA *prop;

	fprintf(f, "/* %s */\n", srna->name);

	if(srna->properties.first)
		fprintf(f, "\n");

	for(prop=srna->properties.first; prop; prop=prop->next)
		fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "static ", rna_property_structname(prop->type), srna->identifier, prop->identifier);
	fprintf(f, "\n");

	for(prop=srna->properties.first; prop; prop=prop->next) {
		switch(prop->type) {
			case PROP_ENUM: {
				EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
				int i;

				if(eprop->item) {
					fprintf(f, "static EnumPropertyItem rna_%s_%s_items[%d] = {", srna->identifier, prop->identifier, eprop->totitem);

					for(i=0; i<eprop->totitem; i++) {
						fprintf(f, "{%d, ", eprop->item[i].value);
						rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
						rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
						rna_print_c_string(f, eprop->item[i].description); fprintf(f, "}");
						if(i != eprop->totitem-1)
							fprintf(f, ", ");
					}

					fprintf(f, "};\n\n");
				}
				else {
					fprintf(stderr, "rna_generate_structs: %s.%s, enum must have items defined.\n", srna->identifier, prop->identifier);
					DefRNA.error= 1;
				}
				break;
			}
			case PROP_BOOLEAN: {
				BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
				unsigned int i;

				if(prop->arraylength) {
					fprintf(f, "static int rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);

					for(i=0; i<prop->arraylength; i++) {
						if(bprop->defaultarray)
							fprintf(f, "%d", bprop->defaultarray[i]);
						else
							fprintf(f, "%d", bprop->defaultvalue);
						if(i != prop->arraylength-1)
							fprintf(f, ", ");
					}

					fprintf(f, "};\n\n");
				}
				break;
			}
			case PROP_INT: {
				IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
				unsigned int i;

				if(prop->arraylength) {
					fprintf(f, "static int rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);

					for(i=0; i<prop->arraylength; i++) {
						if(iprop->defaultarray)
							fprintf(f, "%d", iprop->defaultarray[i]);
						else
							fprintf(f, "%d", iprop->defaultvalue);
						if(i != prop->arraylength-1)
							fprintf(f, ", ");
					}

					fprintf(f, "};\n\n");
				}
				break;
			}
			case PROP_FLOAT: {
				FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
				unsigned int i;

				if(prop->arraylength) {
					fprintf(f, "static float rna_%s_%s_default[%d] = {", srna->identifier, prop->identifier, prop->arraylength);

					for(i=0; i<prop->arraylength; i++) {
						if(fprop->defaultarray)
							rna_float_print(f, fprop->defaultarray[i]);
						else
							rna_float_print(f, fprop->defaultvalue);
						if(i != prop->arraylength-1)
							fprintf(f, ", ");
					}

					fprintf(f, "};\n\n");
				}
				break;
			}
			default:
				break;
		}

		fprintf(f, "%s%s rna_%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "static ", rna_property_structname(prop->type), srna->identifier, prop->identifier);

		if(prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->next->identifier);
		else fprintf(f, "\t{NULL, ");
		if(prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s_%s,\n", srna->identifier, prop->prev->identifier);
		else fprintf(f, "NULL,\n");
		fprintf(f, "\t%d, ", prop->magic);
		rna_print_c_string(f, prop->identifier);
		fprintf(f, ", %d, ", prop->flag);
		rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
		rna_print_c_string(f, prop->description); fprintf(f, ",\n");
		fprintf(f, "\t%s, %s, %d,\n", rna_property_typename(prop->type), rna_property_subtypename(prop->subtype), prop->arraylength);
		fprintf(f, "\t%s, %s},\n", rna_function_string(prop->notify), rna_function_string(prop->editable));

		switch(prop->type) {
			case PROP_BOOLEAN: {
				BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;
				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);
				if(prop->arraylength) fprintf(f, "rna_%s_%s_default\n", srna->identifier, prop->identifier);
				else fprintf(f, "NULL\n");
				break;
			}
			case PROP_INT: {
				IntPropertyRNA *iprop= (IntPropertyRNA*)prop;
				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));
				rna_int_print(f, iprop->softmin); fprintf(f, ", ");
				rna_int_print(f, iprop->softmax); fprintf(f, ", ");
				rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
				rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
				rna_int_print(f, iprop->step); fprintf(f, ", ");
				rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
				if(prop->arraylength) fprintf(f, "rna_%s_%s_default\n", srna->identifier, prop->identifier);
				else fprintf(f, "NULL\n");
				break;
			}
			case PROP_FLOAT: {
				FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;
				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));
				rna_float_print(f, fprop->softmin); fprintf(f, ", ");
				rna_float_print(f, fprop->softmax); fprintf(f, ", ");
				rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
				rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
				rna_float_print(f, fprop->step); fprintf(f, ", ");
				rna_float_print(f, fprop->precision); fprintf(f, ", ");
				rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
				if(prop->arraylength) fprintf(f, "rna_%s_%s_default\n", srna->identifier, prop->identifier);
				else fprintf(f, "NULL\n");
				break;
			}
			case PROP_STRING: {
				StringPropertyRNA *sprop= (StringPropertyRNA*)prop;
				fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
				rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
				break;
			}
			case PROP_ENUM: {
				EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;
				fprintf(f, "\t%s, %s, rna_%s_%s_items, %d, %d\n", rna_function_string(eprop->get), rna_function_string(eprop->set), srna->identifier, prop->identifier, eprop->totitem, eprop->defaultvalue);
				break;
			}
			case PROP_POINTER: {
				PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
				fprintf(f, "\t%s, %s, %s, ", rna_function_string(pprop->get), rna_function_string(pprop->set), rna_function_string(pprop->type));
				if(pprop->structtype) fprintf(f, "&RNA_%s\n", (char*)pprop->structtype);
				else fprintf(f, "NULL\n");
				break;
			}
			case PROP_COLLECTION: {
				CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
				fprintf(f, "\t%s, %s, %s, %s, %s, %s, %s, %s, ", rna_function_string(cprop->begin), rna_function_string(cprop->next), rna_function_string(cprop->end), rna_function_string(cprop->get), rna_function_string(cprop->type), rna_function_string(cprop->length), rna_function_string(cprop->lookupint), rna_function_string(cprop->lookupstring));
				if(cprop->structtype) fprintf(f, "&RNA_%s\n", (char*)cprop->structtype);
				else fprintf(f, "NULL\n");
				break;
			}
		}

		fprintf(f, "};\n\n");
	}

	fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);

	if(srna->next) fprintf(f, "\t&RNA_%s, ", srna->next->identifier);
	else fprintf(f, "\tNULL, ");
	if(srna->prev) fprintf(f, "&RNA_%s,\n", srna->prev->identifier);
	else fprintf(f, "NULL,\n");

	fprintf(f, "\t");
	rna_print_c_string(f, srna->identifier);
	fprintf(f, ", %d, ", srna->flag);
	rna_print_c_string(f, srna->name);
	fprintf(f, ",\n");

	prop= srna->nameproperty;
	if(prop) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
	else fprintf(f, "\tNULL, ");

	fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", srna->identifier);

	if(srna->from) fprintf(f, "\t&RNA_%s,\n", (char*)srna->from);
	else fprintf(f, "\tNULL,\n");

	fprintf(f, "\t%s, %s,\n", rna_function_string(srna->notify), rna_function_string(srna->refine));

	prop= srna->properties.first;
	if(prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
	else fprintf(f, "\t{NULL, ");

	prop= srna->properties.last;
	if(prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}\n", srna->identifier, prop->identifier);
	else fprintf(f, "NULL}\n");

	fprintf(f, "};\n");

	fprintf(f, "\n");
}

typedef struct RNAProcessItem {
	char *filename;
	void (*define)(BlenderRNA *brna);
} RNAProcessItem;

RNAProcessItem PROCESS_ITEMS[]= {
	{"rna_ID.c", RNA_def_ID},
	{"rna_actuator.c", RNA_def_actuator},
	{"rna_brush.c", RNA_def_brush},
	{"rna_camera.c", RNA_def_camera},
	{"rna_color.c", RNA_def_color},
	{"rna_controller.c", RNA_def_controller},
	{"rna_curve.c", RNA_def_curve},
	{"rna_group.c", RNA_def_group},
	{"rna_image.c", RNA_def_image},
	{"rna_ipo.c", RNA_def_ipo},
	{"rna_lamp.c", RNA_def_lamp},
	{"rna_lattice.c", RNA_def_lattice},
	{"rna_main.c", RNA_def_main},
	{"rna_material.c", RNA_def_material},
	{"rna_mesh.c", RNA_def_mesh},
	{"rna_meta.c", RNA_def_meta},
	{"rna_modifier.c", RNA_def_modifier},
	{"rna_nodetree.c", RNA_def_nodetree},
	{"rna_object.c", RNA_def_object},
	{"rna_packedfile.c", RNA_def_packedfile},
	{"rna_property.c", RNA_def_gameproperty},
	{"rna_radio.c", RNA_def_radio},
	{"rna_rna.c", RNA_def_rna},
	{"rna_scene.c", RNA_def_scene},
	{"rna_screen.c", RNA_def_screen},
	{"rna_sensor.c", RNA_def_sensor},
	{"rna_vfont.c", RNA_def_vfont},
	{"rna_wm.c", RNA_def_wm},
	{"rna_world.c", RNA_def_world},	
	{NULL, NULL}};

static int rna_preprocess(char *basedirectory, FILE *f)
{
	BlenderRNA *brna;
	StructRNA *srna;
	int i, status;
	
	fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
	                "   Do not edit manually, changes will be overwritten */\n\n"
	                "#define RNA_RUNTIME\n\n");

	brna= RNA_create();

	fprintf(f, "#include <float.h>\n");
	fprintf(f, "#include <limits.h>\n");
	fprintf(f, "#include <string.h>\n\n");

	fprintf(f, "#include \"BLI_blenlib.h\"\n\n");

	fprintf(f, "#include \"BKE_utildefines.h\"\n\n");

	fprintf(f, "#include \"RNA_define.h\"\n");
	fprintf(f, "#include \"RNA_types.h\"\n");
	fprintf(f, "#include \"rna_internal.h\"\n\n");

	/* this is ugly, but we cannot have c files compiled for both
	 * makesrna and blender with some build systems at the moment */
	fprintf(f, "#include \"rna_define.c\"\n\n");

	for(i=0; PROCESS_ITEMS[i].filename; i++)
		if(PROCESS_ITEMS[i].define)
			PROCESS_ITEMS[i].define(brna);

	rna_sort(brna);
	rna_auto_types();
	
	rna_generate_prototypes(brna, f);

	for(i=0; PROCESS_ITEMS[i].filename; i++)
		fprintf(f, "#include \"%s\"\n", PROCESS_ITEMS[i].filename);
	fprintf(f, "\n");

	rna_auto_functions(f);

	for(srna=brna->structs.first; srna; srna=srna->next)
		rna_generate_struct(brna, srna, f);
	
	status= DefRNA.error;

	RNA_define_free(brna);
	RNA_free(brna);

	return status;
}

static void make_bad_file(char *file)
{
	FILE *fp= fopen(file, "w");
	fprintf(fp, "ERROR! Cannot make correct RNA.c file, STUPID!\n");
	fclose(fp);
}

#ifndef BASE_HEADER
#define BASE_HEADER "../"
#endif

int main(int argc, char **argv)
{
	FILE *file;
	int totblock, return_status = 0;

	if (argc!=2 && argc!=3) {
		printf("Usage: %s outfile.c [base directory]\n", argv[0]);
		return_status = 1;
	}
	else {
		file = fopen(argv[1], "w");

		if (!file) {
			printf ("Unable to open file: %s\n", argv[1]);
			return_status = 1;
		}
		else {
			char baseDirectory[256];

			printf("Running makesrna, program versions %s\n",  RNA_VERSION_DATE);

			if (argc==3)
				strcpy(baseDirectory, argv[2]);
			else
				strcpy(baseDirectory, BASE_HEADER);

			return_status= (rna_preprocess(baseDirectory, file));
			fclose(file);

			if(return_status) {
				/* error */
				make_bad_file(argv[1]);
				return_status = 1;
			}
		}
	}

	totblock= MEM_get_memory_blocks_in_use();
	if(totblock!=0) {
		printf("Error Totblock: %d\n",totblock);
		MEM_printmemlist();
	}

	return return_status;
}