| 1 | package IO::SWF::Tag::Action; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'IO::SWF::Tag::Base'; | ||
| 7 | |||
| 8 | use IO::SWF::Bit; | ||
| 9 | use IO::SWF::Type::Action; | ||
| 10 | |||
| 11 | __PACKAGE__->mk_accessors( qw( | ||
| 12 | _actions | ||
| 13 | _spriteId | ||
| 14 | )); | ||
| 15 | |||
| 16 | sub parseContent { | ||
| 17 | my ($self, $tagCode, $content, $opts_href) = @_; | ||
| 18 | my $reader = IO::SWF::Bit->new(); | ||
| 19 | my @actions = (); | ||
| 20 | $reader->input($content); | ||
| 21 | if ($tagCode == 59) { # DoInitAction | ||
| 22 | $self->_spriteId($reader->getUI16LE()); | ||
| 23 | } | ||
| 24 | while ($reader->getUI8() != 0) { | ||
| 25 | $reader->incrementOffset(-1, 0); # 1 byte back | ||
| 26 | my $action = IO::SWF::Type::Action::parse($reader); | ||
| 27 | push @actions, $action; | ||
| 28 | } | ||
| 29 | $self->_actions(\@actions); | ||
| 30 | # ActionEndFlag | ||
| 31 | } | ||
| 32 | |||
| 33 | sub dumpContent { | ||
| 34 | my ($self, $tagCode, $opts_href) = @_; | ||
| 35 | print " Actions:"; | ||
| 36 | if ($tagCode == 59) { # DoInitAction | ||
| 37 | print " SpriteID=".$self->_spriteId; | ||
| 38 | } | ||
| 39 | print "\n"; | ||
| 40 | foreach my $action (@{$self->_actions}) { | ||
| 41 | my $action_str = IO::SWF::Type::Action::string($action); | ||
| 42 | print "\t$action_str\n"; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | sub buildContent { | ||
| 47 | my ($self, $tagCode, $opts_href) = @_; | ||
| 48 | my $writer = IO::SWF::Bit->new(); | ||
| 49 | if ($tagCode == 59) { # DoInitAction | ||
| 50 | $writer->putUI16LE($self->_spriteId); | ||
| 51 | } | ||
| 52 | |||
| 53 | foreach my $action (@{$self->_actions}) { | ||
| 54 | IO::SWF::Type::Action::build($writer, $action); | ||
| 55 | } | ||
| 56 | $writer->putUI8(0); # ActionEndFlag | ||
| 57 | return $writer->output(); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub replaceActionStrings { | ||
| 61 | my ($self, $trans_table_href) = @_; | ||
| 62 | my %trans_table = ref($trans_table_href) ? %{$trans_table_href} : (); | ||
| 63 | foreach my $action (@{$self->_actions}) { | ||
| 64 | if ($action->{'Code'} == 0x83) { | ||
| 65 | # 0x83: // ActionGetURL | ||
| 66 | if (exists ($trans_table{$action->{'UrlString'}})) { | ||
| 67 | $action->{'UrlString'} = $trans_table{$action->{'UrlString'}}; | ||
| 68 | } | ||
| 69 | if (exists ($trans_table{$action->{'TargetString'}})) { | ||
| 70 | $action->{'TargetString'} = $trans_table{$action->{'TargetString'}}; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | elsif ($action->{'Code'} == 0x88) { | ||
| 74 | # 0x88: // ActionConstantPool | ||
| 75 | for(my $idx_cp = 0;$idx_cp < @{$action->{'ConstantPool'}};$idx_cp++) { | ||
| 76 | my $cp = @{$action->{'ConstantPool'}}[$idx_cp]; | ||
| 77 | if (exists($trans_table{$cp})) { | ||
| 78 | @{$action->{'ConstantPool'}}[$idx_cp] = $trans_table{$cp}; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | elsif ($action->{'Code'} == 0x96) { | ||
| 83 | # 0x96: // ActionPush | ||
| 84 | foreach my $value (%{$action->{'Values'}}) { | ||
| 85 | if ($value->{'Type'} == 0) { # Type String | ||
| 86 | if (exists($trans_table{$value->{'String'}})) { | ||
| 87 | $value->{'String'} = $trans_table{$value->{'String'}}; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | # don't touch $action, danger! | ||
| 94 | } | ||
| 95 | |||
| 96 | 1; |