--- deliantra/Deliantra/res2pm 2006/02/22 21:57:29 1.3 +++ deliantra/Deliantra/res2pm 2010/03/19 21:38:14 1.13 @@ -2,13 +2,13 @@ # usage: res2pm -open STDOUT, ">:utf8", "Crossfire/Data.pm" - or die "Crossfire/Data.pm: $!"; +open STDOUT, ">:utf8", "Deliantra/Data.pm" + or die "Deliantra/Data.pm: $!"; print <new (Style => 'grove')->parsefile ("res/types.xml"); +my $grove_builder = XML::Grove::Builder->new; +my $parser = XML::Parser::PerlSAX->new ( Handler => $grove_builder ); +my $type = $parser->parse ( Source => { SystemId => "res/types.xml" } ); my %bitmask; my %list; @@ -75,7 +81,7 @@ my %spell; sub string($) { - local $_ = join "", @{shift->contents}; + local $_ = join "", map $_->{Data}, @{shift->{Contents}}; $_ =~ s/^\s+//; $_ =~ s/\s+$//; $_ =~ s/\s+/ /g; @@ -86,10 +92,10 @@ my ($e, $sect) = @_; my $arch = { - type => $e->attr ("type"), - name => $e->attr ("editor"), + type => $e->{Attributes}->{type}, + name => $e->{Attributes}->{editor}, desc => string $e, - $e->attr("arch_begin") ? (end => $e->attr("arch_end")) : (), + $e->{Attributes}->{arch_begin} ? (end => $e->{Attributes}->{arch_end}) : (), }; delete $arch->{name} unless defined $arch->{name}; @@ -99,101 +105,111 @@ $arch->{value} = $bitmask{$2} ||= {}; } elsif ($arch->{type} =~ s/^(list)_(.*)/$1/) { $arch->{value} = $list{$2} ||= {}; + } elsif ($arch->{type} eq "fixed") { + $arch->{value} = $e->{Attributes}->{value}; } elsif ($arch->{type} =~ s/^bool_special$/bool/) { - $arch->{value} = [$e->attr ("false"), $e->attr ("true")]; + $arch->{value} = [$e->{Attributes}->{false}, $e->{Attributes}->{true}]; } - $sect->{$e->attr ("arch") || $e->attr("arch_begin")} = $arch; + push @$sect, [$e->{Attributes}->{arch} || $e->{Attributes}->{arch_begin}, $arch]; } sub parse_type { my ($e, $type) = @_; - for my $e (grep ref, @{$e->contents}) { - if ($e->name eq "required") { - for my $i (grep ref, @{$e->contents}) { - $type->{required}{$i->attr ("arch")} = $i->attr ("value"); - } - } elsif ($e->name eq "attribute") { - parse_attr $e, $type->{attr}{Main} ||= {}; - } elsif ($e->name eq "ignore") { - for my $i (grep ref, @{$e->contents}) { - if ($i->name eq "ignore_list") { - push @{$type->{ignore}}, $ignore_list{$i->attr ("name")} ||= []; - } elsif ($i->name eq "attribute") { - push @{$type->{ignore}}, $i->attr ("arch"); + my %main; + + for my $e (grep { $_->isa ('XML::Grove::Element') } @{$e->{Contents}}) { + if ($e->{Name} eq "required") { + # not used + #for my $i (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { + # $type->{required}{$i->{Attributes}->{arch}} = $i->{Attributes}->{value}; + #} + } elsif ($e->{Name} eq "attribute") { + parse_attr $e, $type->{attr} ||= []; + } elsif ($e->{Name} eq "ignore") { + for my $i (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { + if ($i->{Name} eq "ignore_list") { + push @{$type->{ignore}}, $ignore_list{$i->{Attributes}->{name}} ||= []; + } elsif ($i->{Name} eq "attribute") { + warn "ACDD $i->{Attributes}->{arch}\n"; + push @{$type->{ignore}}, [$i->{Attributes}->{arch}]; } } - } elsif ($e->name eq "import_type") { - push @{$type->{import}}, $type{$e->attr ("name")} ||= {}; - } elsif ($e->name eq "use") { + } elsif ($e->{Name} eq "import_type") { + #push @{$type->{import}}, $type{$e->{Attributes}->{name}} ||= {}; + push @{$type->{import}}, $e->{Attributes}->{name}; + } elsif ($e->{Name} eq "use") { $type->{use} = string $e; - } elsif ($e->name eq "description") { + } elsif ($e->{Name} eq "description") { $type->{desc} = string $e; - } elsif ($e->name eq "section") { - for my $i (grep ref, @{$e->contents}) { - parse_attr $i, $type->{attr}{$e->attr ("name")} ||= {}; + } elsif ($e->{Name} eq "section") { + my @attr; + for my $i (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { + parse_attr $i, \@attr; } -# $type->{desc} = string $e; + push @{ $type->{section} }, [$e->{Attributes}->{name} => \@attr]; } else { - warn "unknown types subelement ", $e->name; + warn "unknown types subelement ", $e->{Name}; } } $type } -for my $e (grep ref, @{$type->root->contents}) { - if ($e->name eq "bitmask") { - my $bm = $bitmask{$e->attr ("name")} ||= {}; - for my $b (grep ref, @{$e->contents}) { - $bm->{$b->attr ("bit")} = $b->attr ("name"); +for my $e (grep $_->isa ('XML::Grove::Element'), @{$type->root->{Contents}}) { + if ($e->{Name} eq "bitmask") { + my $bm = $bitmask{$e->{Attributes}->{name}} ||= {}; + for my $b (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { + $bm->{$b->{Attributes}->{bit}} = $b->{Attributes}->{name}; } - } elsif ($e->name eq "list") { - my $list = $list{$e->attr ("name")} ||= {}; - for my $b (grep ref, @{$e->contents}) { - $list->{$b->attr ("value")} = $b->attr ("name"); + } elsif ($e->{Name} eq "list") { + my $list = $list{$e->{Attributes}->{name}} ||= {}; + for my $b (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { + $list->{$b->{Attributes}->{value}} = $b->{Attributes}->{name}; } - } elsif ($e->name eq "ignore_list") { - my $list = $ignore_list{$e->attr ("name")} ||= []; - for my $b (grep ref, @{$e->contents}) { - push @$list, $b->attr ("arch"); + } elsif ($e->{Name} eq "ignore_list") { + my $list = $ignore_list{$e->{Attributes}->{name}} ||= []; + for my $b (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { + push @$list, $b->{Attributes}->{arch}; } - } elsif ($e->name eq "default_type") { + } elsif ($e->{Name} eq "default_type") { parse_type $e, \%default_attr; - } elsif ($e->name eq "type") { - my $type = $type{$e->attr ("name")} ||= {}; + } elsif ($e->{Name} eq "type") { + my $type = $type{$e->{Attributes}->{name}} ||= {}; + + $type->{name} = $e->{Attributes}->{name}; + parse_type $e, $type; - #unshift @{$type->{import}}, \%default_attr; - if ($e->attr ("number") > 0) { - $attr{$e->attr ("number")} = $type; - } elsif ($e->attr ("name") eq "Misc") { + if ($e->{Attributes}->{number} > 0) { + $attr{$e->{Attributes}->{number}} = $type; + } elsif ($e->{Attributes}->{name} eq "Misc") { delete $type->{required}; } else { push @attr0, $type; } } else { - warn "unknown types element ", $e->name; + warn "unknown types element ", $e->{Name}; } } -my $type = XML::Parser->new (Style => 'grove')->parsefile ("res/typenumbers.xml"); +my $type = $parser->parse ( Source => { SystemId => "res/typenumbers.xml" } ); -for (grep ref, @{$type->root->contents}) { - $typename{$_->attr ("number")} = $_->attr ("name"); +for (grep $_->isa ('XML::Grove::Element'), @{$type->root->{Contents}}) { + $typename{$_->{Attributes}->{number}} = $_->{Attributes}->{name}; } -my $spell = XML::Parser->new (Style => 'grove')->parsefile ("res/spells.xml") +my $spell = $parser->parse ( Source => { SystemId => "res/spells.xml" } ) or die; -for (grep ref, @{$spell->root->contents}) { - $spell{$_->attr ("id")} = $_->attr ("name"); +for (grep $_->isa ('XML::Grove::Element'), @{$spell->root->{Contents}}) { + $spell{$_->{Attributes}->{id}} = $_->{Attributes}->{name}; } -dump_hash ["BITMASK", "LIST", "IGNORE_LIST", "DEFAULT_ATTR", "TYPE", "ATTR0", "ATTR", "TYPENAME", "SPELL"], - [\%bitmask, \%list, \%ignore_list, \%default_attr, \%type, \@attr0, \%attr, \%typename, \%spell]; +dump_hash ["BITMASK", "LIST", "IGNORE_LIST", "DEFAULT_ATTR", "TYPE", "ATTR", "TYPENAME", "SPELL"], + [\%bitmask, \%list, \%ignore_list, \%default_attr, \%type, \%attr, \%typename, \%spell]; print <