ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/res2pm
(Generate patch)

Comparing deliantra/Deliantra/res2pm (file contents):
Revision 1.1 by root, Wed Feb 22 20:33:05 2006 UTC vs.
Revision 1.4 by root, Wed Feb 22 22:05:53 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3# usage: res2pm >Crossfire/Data.pm 3# usage: res2pm
4
5open STDOUT, ">:utf8", "Crossfire/Data.pm"
6 or die "Crossfire/Data.pm: $!";
4 7
5print <<EOF; 8print <<EOF;
6=head1 NAME 9=head1 NAME
7 10
8Crossfire::Data - various data structures useful for understanding archs and objects 11Crossfire::Data - various data structures useful for understanding archs and objects
25 28
26use Data::Dumper; 29use Data::Dumper;
27use XML::Parser::Grove; 30use XML::Parser::Grove;
28 31
29sub dump_hash { 32sub dump_hash {
30 my ($name, $ref) = @_; 33 my ($names, $refs) = @_;
31 require Data::Dumper; 34
32 $d = new Data::Dumper ([$ref], ["*$name"]); 35 $d = new Data::Dumper ($refs, [map "*$_", @$names]);
33 $d->Terse (1); 36 $d->Terse (1);
34 $d->Indent (1); 37 $d->Indent (1);
35 $d->Quotekeys (0); 38 $d->Quotekeys (0);
36 $d->Useqq (1); 39 $d->Useqq (0);
40 $d->Useperl(1);
37 $d->Sortkeys (sub { 41 $d->Sortkeys (sub {
38 [sort { 42 [sort {
39 $a > 0 && $b > 0 ? $a <=> $b 43 $a > 0 && $b > 0 ? $a <=> $b
40 : $a cmp $b 44 : $a cmp $b
41 } keys %{+shift}] 45 } keys %{+shift}]
42 }); 46 });
47
43 my $d = $d->Dump; 48 my @vals = $d->Dump;
49
50 while (@vals) {
51 my $v = shift @vals;
44 $d =~ s/^ /\t\t/gm; 52 $v =~ s/^ /\t\t/gm;
45 $d =~ s/^ /\t/gm; 53 $v =~ s/^ /\t/gm;
46 $d =~ s/\s+$//; 54 $v =~ s/\s+$//;
55
56 my $name = shift @$names;
57 my $ref = shift @$refs;
58
59 my $sigil = ref $ref eq "ARRAY" ? '@' : '%';
60
47 print "our %$name = $d;\n\n"; 61 print "our $sigil$name = $v;\n\n";
62 }
48} 63}
49 64
50my $type = XML::Parser->new (Style => 'grove')->parsefile ("res/types.xml"); 65my $type = XML::Parser->new (Style => 'grove')->parsefile ("res/types.xml");
51 66
52my %bitmask; 67my %bitmask;
53my %list; 68my %list;
54my %type; 69my %type;
70my %typename;
71my @attr0;
55my $attr; 72my %attr;
56my %ignore_list; 73my %ignore_list;
57my %default_attr; 74my %default_attr;
75my %spell;
58 76
59sub string($) { 77sub string($) {
60 local $_ = join "", @{shift->contents}; 78 local $_ = join "", @{shift->contents};
61 $_ =~ s/^\s+//; 79 $_ =~ s/^\s+//;
62 $_ =~ s/\s+$//; 80 $_ =~ s/\s+$//;
65} 83}
66 84
67sub parse_attr { 85sub parse_attr {
68 my ($e, $sect) = @_; 86 my ($e, $sect) = @_;
69 87
70 $sect->{$e->attr ("arch") || $e->attr("arch_begin")} = { 88 my $arch = {
89 type => $e->attr ("type"),
71 name => $e->attr ("editor"), 90 name => $e->attr ("editor"),
72 type => $e->attr ("type"),
73 desc => string $e, 91 desc => string $e,
74 $e->attr("arch_begin") ? (end => $e->attr("arch_end")) : (), 92 $e->attr("arch_begin") ? (end => $e->attr("arch_end")) : (),
75 }; 93 };
94
95 delete $arch->{name} unless defined $arch->{name};
96 delete $arch->{desc} unless length $arch->{desc};
97
98 if ($arch->{type} =~ s/^(bitmask)_(.*)/$1/) {
99 $arch->{value} = $bitmask{$2} ||= {};
100 } elsif ($arch->{type} =~ s/^(list)_(.*)/$1/) {
101 $arch->{value} = $list{$2} ||= {};
102 } elsif ($arch->{type} =~ s/^bool_special$/bool/) {
103 $arch->{value} = [$e->attr ("false"), $e->attr ("true")];
104 }
105
106 $sect->{$e->attr ("arch") || $e->attr("arch_begin")} = $arch;
76} 107}
77 108
78sub parse_type { 109sub parse_type {
79 my ($e, $type) = @_; 110 my ($e, $type) = @_;
80 111
92 } elsif ($i->name eq "attribute") { 123 } elsif ($i->name eq "attribute") {
93 push @{$type->{ignore}}, $i->attr ("arch"); 124 push @{$type->{ignore}}, $i->attr ("arch");
94 } 125 }
95 } 126 }
96 } elsif ($e->name eq "import_type") { 127 } elsif ($e->name eq "import_type") {
97 push @{$type->{import}}, $xtype{$e->attr ("name")} ||= {}; 128 push @{$type->{import}}, $type{$e->attr ("name")} ||= {};
98 } elsif ($e->name eq "use") { 129 } elsif ($e->name eq "use") {
99 $type->{use} = string $e; 130 $type->{use} = string $e;
100 } elsif ($e->name eq "description") { 131 } elsif ($e->name eq "description") {
101 $type->{desc} = string $e; 132 $type->{desc} = string $e;
102 } elsif ($e->name eq "section") { 133 } elsif ($e->name eq "section") {
103 for my $i (grep ref, @{$e->contents}) { 134 for my $i (grep ref, @{$e->contents}) {
104 parse_attr $i, $type->{attr}{$e->attr ("name")} ||= {}; 135 parse_attr $i, $type->{attr}{$e->attr ("name")} ||= {};
105 } 136 }
106# $type->{desc} = string $e;
107 } else { 137 } else {
108 warn "unknown types subelement ", $e->name; 138 warn "unknown types subelement ", $e->name;
109 } 139 }
110 } 140 }
111 141
112 $type 142 $type
113} 143}
114 144
115for my $e (grep ref, @{$type->root->contents}) { 145for my $e (grep ref, @{$type->root->contents}) {
116 if ($e->name eq "bitmask") { 146 if ($e->name eq "bitmask") {
117 my $bm = $bitmask{$e->attr ("name")} = {}; 147 my $bm = $bitmask{$e->attr ("name")} ||= {};
118 for my $b (grep ref, @{$e->contents}) { 148 for my $b (grep ref, @{$e->contents}) {
119 $bm->{$b->attr ("bit")} = $b->attr ("name"); 149 $bm->{$b->attr ("bit")} = $b->attr ("name");
120 } 150 }
121 } elsif ($e->name eq "list") { 151 } elsif ($e->name eq "list") {
122 my $list = $list{$e->attr ("name")} = {}; 152 my $list = $list{$e->attr ("name")} ||= {};
123 for my $b (grep ref, @{$e->contents}) { 153 for my $b (grep ref, @{$e->contents}) {
124 $list->{$b->attr ("value")} = $b->attr ("name"); 154 $list->{$b->attr ("value")} = $b->attr ("name");
125 } 155 }
126 } elsif ($e->name eq "ignore_list") { 156 } elsif ($e->name eq "ignore_list") {
127 my $list = $ignore_list{$e->attr ("name")} ||= []; 157 my $list = $ignore_list{$e->attr ("name")} ||= [];
129 push @$list, $b->attr ("arch"); 159 push @$list, $b->attr ("arch");
130 } 160 }
131 } elsif ($e->name eq "default_type") { 161 } elsif ($e->name eq "default_type") {
132 parse_type $e, \%default_attr; 162 parse_type $e, \%default_attr;
133 } elsif ($e->name eq "type") { 163 } elsif ($e->name eq "type") {
134 my $type = $attr{$e->attr ("name")} ||= {}; 164 my $type = $type{$e->attr ("name")} ||= {};
135 parse_type $e, $type; 165 parse_type $e, $type;
136 unshift @{$type->{import}}, \%default_attr; 166 #unshift @{$type->{import}}, \%default_attr;
137 $type{$e->attr ("number")}{type}{$e->attr ("name")} = $type; 167
168 if ($e->attr ("number") > 0) {
169 $attr{$e->attr ("number")} = $type;
170 } elsif ($e->attr ("name") eq "Misc") {
171 delete $type->{required};
172 } else {
173 push @attr0, $type;
174 }
138 175
139 } else { 176 } else {
140 warn "unknown types element ", $e->name; 177 warn "unknown types element ", $e->name;
141 } 178 }
142} 179}
143 180
144my $type = XML::Parser->new (Style => 'grove')->parsefile ("res/typenumbers.xml"); 181my $type = XML::Parser->new (Style => 'grove')->parsefile ("res/typenumbers.xml");
145 182
146for (grep ref, @{$type->root->contents}) { 183for (grep ref, @{$type->root->contents}) {
147 $type{$_->attr ("number")}{name} = $_->attr ("name"); 184 $typename{$_->attr ("number")} = $_->attr ("name");
148} 185}
149
150dump_hash "TYPE", \%type;
151
152dump_hash "LIST", \%list;
153dump_hash "BITMASK", \%bitmask;
154 186
155my $spell = XML::Parser->new (Style => 'grove')->parsefile ("res/spells.xml") 187my $spell = XML::Parser->new (Style => 'grove')->parsefile ("res/spells.xml")
156 or die; 188 or die;
157 189
158my %spell;
159
160for (grep ref, @{$spell->root->contents}) { 190for (grep ref, @{$spell->root->contents}) {
161 $spell{$_->attr ("id")} = $_->attr ("name"); 191 $spell{$_->attr ("id")} = $_->attr ("name");
162} 192}
163 193
164dump_hash "SPELL", \%spell; 194dump_hash ["BITMASK", "LIST", "IGNORE_LIST", "DEFAULT_ATTR", "TYPE", "ATTR0", "ATTR", "TYPENAME", "SPELL"],
195 [\%bitmask, \%list, \%ignore_list, \%default_attr, \%type, \@attr0, \%attr, \%typename, \%spell];
165 196
166print <<EOF; 197print <<EOF;
167 198
168=head1 AUTHOR 199=head1 AUTHOR
169 200

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines