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