… | |
… | |
22 | |
22 | |
23 | See F<res/README> for more info. |
23 | See F<res/README> for more info. |
24 | |
24 | |
25 | =cut |
25 | =cut |
26 | |
26 | |
|
|
27 | package Crossfire::Data; |
|
|
28 | |
27 | EOF |
29 | EOF |
28 | |
30 | |
29 | use Data::Dumper; |
31 | use Data::Dumper; |
|
|
32 | use XML::Grove::Builder; |
30 | use XML::Parser::Grove; |
33 | use XML::Parser::PerlSAX; |
|
|
34 | |
31 | |
35 | |
32 | sub dump_hash { |
36 | sub dump_hash { |
33 | my ($names, $refs) = @_; |
37 | my ($names, $refs) = @_; |
34 | |
38 | |
35 | $d = new Data::Dumper ($refs, [map "*$_", @$names]); |
39 | $d = new Data::Dumper ($refs, [map "*$_", @$names]); |
… | |
… | |
60 | |
64 | |
61 | print "our $sigil$name = $v;\n\n"; |
65 | print "our $sigil$name = $v;\n\n"; |
62 | } |
66 | } |
63 | } |
67 | } |
64 | |
68 | |
65 | my $type = XML::Parser->new (Style => 'grove')->parsefile ("res/types.xml"); |
69 | my $grove_builder = XML::Grove::Builder->new; |
|
|
70 | my $parser = XML::Parser::PerlSAX->new ( Handler => $grove_builder ); |
|
|
71 | my $type = $parser->parse ( Source => { SystemId => "res/types.xml" } ); |
66 | |
72 | |
67 | my %bitmask; |
73 | my %bitmask; |
68 | my %list; |
74 | my %list; |
69 | my %type; |
75 | my %type; |
70 | my %typename; |
76 | my %typename; |
… | |
… | |
73 | my %ignore_list; |
79 | my %ignore_list; |
74 | my %default_attr; |
80 | my %default_attr; |
75 | my %spell; |
81 | my %spell; |
76 | |
82 | |
77 | sub string($) { |
83 | sub string($) { |
78 | local $_ = join "", @{shift->contents}; |
84 | local $_ = join "", map $_->{Data}, @{shift->{Contents}}; |
79 | $_ =~ s/^\s+//; |
85 | $_ =~ s/^\s+//; |
80 | $_ =~ s/\s+$//; |
86 | $_ =~ s/\s+$//; |
81 | $_ =~ s/\s+/ /g; |
87 | $_ =~ s/\s+/ /g; |
82 | $_ |
88 | $_ |
83 | } |
89 | } |
84 | |
90 | |
85 | sub parse_attr { |
91 | sub parse_attr { |
86 | my ($e, $sect) = @_; |
92 | my ($e, $sect) = @_; |
87 | |
93 | |
88 | my $arch = { |
94 | my $arch = { |
89 | type => $e->attr ("type"), |
95 | type => $e->{Attributes}->{type}, |
90 | name => $e->attr ("editor"), |
96 | name => $e->{Attributes}->{editor}, |
91 | desc => string $e, |
97 | desc => string $e, |
92 | $e->attr("arch_begin") ? (end => $e->attr("arch_end")) : (), |
98 | $e->{Attributes}->{arch_begin} ? (end => $e->{Attributes}->{arch_end}) : (), |
93 | }; |
99 | }; |
94 | |
100 | |
95 | delete $arch->{name} unless defined $arch->{name}; |
101 | delete $arch->{name} unless defined $arch->{name}; |
96 | delete $arch->{desc} unless length $arch->{desc}; |
102 | delete $arch->{desc} unless length $arch->{desc}; |
97 | |
103 | |
98 | if ($arch->{type} =~ s/^(bitmask)_(.*)/$1/) { |
104 | if ($arch->{type} =~ s/^(bitmask)_(.*)/$1/) { |
99 | $arch->{value} = $bitmask{$2} ||= {}; |
105 | $arch->{value} = $bitmask{$2} ||= {}; |
100 | } elsif ($arch->{type} =~ s/^(list)_(.*)/$1/) { |
106 | } elsif ($arch->{type} =~ s/^(list)_(.*)/$1/) { |
101 | $arch->{value} = $list{$2} ||= {}; |
107 | $arch->{value} = $list{$2} ||= {}; |
|
|
108 | } elsif ($arch->{type} eq "fixed") { |
|
|
109 | $arch->{value} = $e->{Attributes}->{value}; |
102 | } elsif ($arch->{type} =~ s/^bool_special$/bool/) { |
110 | } elsif ($arch->{type} =~ s/^bool_special$/bool/) { |
103 | $arch->{value} = [$e->attr ("false"), $e->attr ("true")]; |
111 | $arch->{value} = [$e->{Attributes}->{false}, $e->{Attributes}->{true}]; |
104 | } |
112 | } |
105 | |
113 | |
106 | $sect->{$e->attr ("arch") || $e->attr("arch_begin")} = $arch; |
114 | push @$sect, [$e->{Attributes}->{arch} || $e->{Attributes}->{arch_begin}, $arch]; |
107 | } |
115 | } |
108 | |
116 | |
109 | sub parse_type { |
117 | sub parse_type { |
110 | my ($e, $type) = @_; |
118 | my ($e, $type) = @_; |
111 | |
119 | |
112 | for my $e (grep ref, @{$e->contents}) { |
120 | my %main; |
|
|
121 | |
|
|
122 | for my $e (grep { $_->isa ('XML::Grove::Element') } @{$e->{Contents}}) { |
113 | if ($e->name eq "required") { |
123 | if ($e->{Name} eq "required") { |
114 | for my $i (grep ref, @{$e->contents}) { |
124 | # not used |
115 | $type->{required}{$i->attr ("arch")} = $i->attr ("value"); |
125 | #for my $i (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { |
|
|
126 | # $type->{required}{$i->{Attributes}->{arch}} = $i->{Attributes}->{value}; |
116 | } |
127 | #} |
117 | } elsif ($e->name eq "attribute") { |
128 | } elsif ($e->{Name} eq "attribute") { |
118 | parse_attr $e, $type->{attr}{Main} ||= {}; |
129 | parse_attr $e, $type->{attr} ||= []; |
119 | } elsif ($e->name eq "ignore") { |
130 | } elsif ($e->{Name} eq "ignore") { |
120 | for my $i (grep ref, @{$e->contents}) { |
131 | for my $i (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { |
121 | if ($i->name eq "ignore_list") { |
132 | if ($i->{Name} eq "ignore_list") { |
122 | push @{$type->{ignore}}, $ignore_list{$i->attr ("name")} ||= []; |
133 | push @{$type->{ignore}}, $ignore_list{$i->{Attributes}->{name}} ||= []; |
123 | } elsif ($i->name eq "attribute") { |
134 | } elsif ($i->{Name} eq "attribute") { |
124 | push @{$type->{ignore}}, $i->attr ("arch"); |
135 | push @{$type->{ignore}}, $i->{Attributes}->{arch}; |
125 | } |
136 | } |
126 | } |
137 | } |
127 | } elsif ($e->name eq "import_type") { |
138 | } elsif ($e->{Name} eq "import_type") { |
128 | push @{$type->{import}}, $type{$e->attr ("name")} ||= {}; |
139 | #push @{$type->{import}}, $type{$e->{Attributes}->{name}} ||= {}; |
|
|
140 | push @{$type->{import}}, $e->{Attributes}->{name}; |
129 | } elsif ($e->name eq "use") { |
141 | } elsif ($e->{Name} eq "use") { |
130 | $type->{use} = string $e; |
142 | $type->{use} = string $e; |
131 | } elsif ($e->name eq "description") { |
143 | } elsif ($e->{Name} eq "description") { |
132 | $type->{desc} = string $e; |
144 | $type->{desc} = string $e; |
133 | } elsif ($e->name eq "section") { |
145 | } elsif ($e->{Name} eq "section") { |
134 | for my $i (grep ref, @{$e->contents}) { |
146 | my @attr; |
135 | parse_attr $i, $type->{attr}{$e->attr ("name")} ||= {}; |
147 | for my $i (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { |
|
|
148 | parse_attr $i, \@attr; |
136 | } |
149 | } |
|
|
150 | push @{ $type->{section} }, [$e->{Attributes}->{name} => \@attr]; |
137 | } else { |
151 | } else { |
138 | warn "unknown types subelement ", $e->name; |
152 | warn "unknown types subelement ", $e->{Name}; |
139 | } |
153 | } |
140 | } |
154 | } |
141 | |
155 | |
142 | $type |
156 | $type |
143 | } |
157 | } |
144 | |
158 | |
145 | for my $e (grep ref, @{$type->root->contents}) { |
159 | for my $e (grep $_->isa ('XML::Grove::Element'), @{$type->root->{Contents}}) { |
146 | if ($e->name eq "bitmask") { |
160 | if ($e->{Name} eq "bitmask") { |
147 | my $bm = $bitmask{$e->attr ("name")} ||= {}; |
161 | my $bm = $bitmask{$e->{Attributes}->{name}} ||= {}; |
148 | for my $b (grep ref, @{$e->contents}) { |
162 | for my $b (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { |
149 | $bm->{$b->attr ("bit")} = $b->attr ("name"); |
163 | $bm->{$b->{Attributes}->{bit}} = $b->{Attributes}->{name}; |
150 | } |
164 | } |
151 | } elsif ($e->name eq "list") { |
165 | } elsif ($e->{Name} eq "list") { |
152 | my $list = $list{$e->attr ("name")} ||= {}; |
166 | my $list = $list{$e->{Attributes}->{name}} ||= {}; |
153 | for my $b (grep ref, @{$e->contents}) { |
167 | for my $b (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { |
154 | $list->{$b->attr ("value")} = $b->attr ("name"); |
168 | $list->{$b->{Attributes}->{value}} = $b->{Attributes}->{name}; |
155 | } |
169 | } |
156 | } elsif ($e->name eq "ignore_list") { |
170 | } elsif ($e->{Name} eq "ignore_list") { |
157 | my $list = $ignore_list{$e->attr ("name")} ||= []; |
171 | my $list = $ignore_list{$e->{Attributes}->{name}} ||= []; |
158 | for my $b (grep ref, @{$e->contents}) { |
172 | for my $b (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { |
159 | push @$list, $b->attr ("arch"); |
173 | push @$list, $b->{Attributes}->{arch}; |
160 | } |
174 | } |
161 | } elsif ($e->name eq "default_type") { |
175 | } elsif ($e->{Name} eq "default_type") { |
162 | parse_type $e, \%default_attr; |
176 | parse_type $e, \%default_attr; |
163 | } elsif ($e->name eq "type") { |
177 | } elsif ($e->{Name} eq "type") { |
164 | my $type = $type{$e->attr ("name")} ||= {}; |
178 | my $type = $type{$e->{Attributes}->{name}} ||= {}; |
|
|
179 | |
|
|
180 | $type->{name} = $e->{Attributes}->{name}; |
|
|
181 | |
165 | parse_type $e, $type; |
182 | parse_type $e, $type; |
166 | #unshift @{$type->{import}}, \%default_attr; |
|
|
167 | |
183 | |
168 | if ($e->attr ("number") > 0) { |
184 | if ($e->{Attributes}->{number} > 0) { |
169 | $attr{$e->attr ("number")} = $type; |
185 | $attr{$e->{Attributes}->{number}} = $type; |
170 | } elsif ($e->attr ("name") eq "Misc") { |
186 | } elsif ($e->{Attributes}->{name} eq "Misc") { |
171 | delete $type->{required}; |
187 | delete $type->{required}; |
172 | } else { |
188 | } else { |
173 | push @attr0, $type; |
189 | push @attr0, $type; |
174 | } |
190 | } |
175 | |
191 | |
176 | } else { |
192 | } else { |
177 | warn "unknown types element ", $e->name; |
193 | warn "unknown types element ", $e->{Name}; |
178 | } |
194 | } |
179 | } |
195 | } |
180 | |
196 | |
181 | my $type = XML::Parser->new (Style => 'grove')->parsefile ("res/typenumbers.xml"); |
197 | my $type = $parser->parse ( Source => { SystemId => "res/typenumbers.xml" } ); |
182 | |
198 | |
183 | for (grep ref, @{$type->root->contents}) { |
199 | for (grep $_->isa ('XML::Grove::Element'), @{$type->root->{Contents}}) { |
184 | $typename{$_->attr ("number")} = $_->attr ("name"); |
200 | $typename{$_->{Attributes}->{number}} = $_->{Attributes}->{name}; |
185 | } |
201 | } |
186 | |
202 | |
187 | my $spell = XML::Parser->new (Style => 'grove')->parsefile ("res/spells.xml") |
203 | my $spell = $parser->parse ( Source => { SystemId => "res/spells.xml" } ) |
188 | or die; |
204 | or die; |
189 | |
205 | |
190 | for (grep ref, @{$spell->root->contents}) { |
206 | for (grep $_->isa ('XML::Grove::Element'), @{$spell->root->{Contents}}) { |
191 | $spell{$_->attr ("id")} = $_->attr ("name"); |
207 | $spell{$_->{Attributes}->{id}} = $_->{Attributes}->{name}; |
192 | } |
208 | } |
193 | |
209 | |
194 | dump_hash ["BITMASK", "LIST", "IGNORE_LIST", "DEFAULT_ATTR", "TYPE", "ATTR0", "ATTR", "TYPENAME", "SPELL"], |
210 | dump_hash ["BITMASK", "LIST", "IGNORE_LIST", "DEFAULT_ATTR", "TYPE", "ATTR", "TYPENAME", "SPELL"], |
195 | [\%bitmask, \%list, \%ignore_list, \%default_attr, \%type, \@attr0, \%attr, \%typename, \%spell]; |
211 | [\%bitmask, \%list, \%ignore_list, \%default_attr, \%type, \%attr, \%typename, \%spell]; |
196 | |
212 | |
197 | print <<EOF; |
213 | print <<EOF; |
198 | |
214 | |
199 | =head1 AUTHOR |
215 | =head1 AUTHOR |
200 | |
216 | |