1#!/usr/bin/perl
2
3use strict;
4
5use SWF::Parser;
6use SWF::Element;
7use SWF::BinStream;
8use SWF::BinStream::File;
9
10my $frame = 0;
11
12if (@ARGV==0) {
13    print STDERR <<USAGE;
14dumpswf.plx - Parse SWF file and dump it as a perl script.
15  perl dumpswf.plx swfname [saveto]
16
17USAGE
18
19    exit(1);
20}
21
22my $count = 0;
23
24if (@ARGV==2) {
25    open(F, ">$ARGV[1]") or die "Can't open $ARGV[1]";
26    select F;
27}
28
29print <<START;
30use SWF::Element;
31use SWF::File;
32
33die "This script creates '$ARGV[0]' SWF file. A new file name is needed.\\n" unless \$ARGV[0];
34
35START
36
37my $p=SWF::Parser->new('header-callback' => \&header, 'tag-callback' => \&data);
38$p->parse_file($ARGV[0]);
39
40print <<END;
41
42\$new->close;
43END
44
45
46sub header {
47    my ($self, $signature, $version, $length, $xmin, $ymin, $xmax, $ymax, $rate, $count)=@_;
48    print STDERR <<HEADER;
49Header:
50SIGNATURE = $signature
51VERSION = $version
52File length = $length
53Rect size = ($xmin, $ymin)-($xmax, $ymax)
54Frame rate = $rate
55Frame count = $count
56
57HEADER
58    print <<HEADER2;
59\$new = SWF::File->new("\$ARGV[0].swf", Version => $version);
60
61# $ARGV[0]  SWF header
62
63\$new->FrameSize($xmin, $ymin, $xmax, $ymax);
64\$new->FrameRate($rate);
65#\$new->FrameCount($count);
66
67# $ARGV[0]  SWF tags
68
69HEADER2
70}
71
72sub data {
73    my ($self, $tag, $length, $stream)=@_;
74    my $t = SWF::Element::Tag->new(Tag=>$tag, Length=>$length);
75    my ($tagname) = $t->tag_name;
76
77    print STDERR <<BLOCK;
78Data block:$count
79Tag ID = $tag
80Tag Name = $tagname
81Length = $length
82BLOCK
83    print "# Tag No.: $count\n";
84    $count++;
85    eval {
86	$t->unpack($stream);
87    };
88    if ($@) {
89	my $mes = $@;
90	$t->dumper;
91	die $mes;
92    }
93    $t->dumper;
94    print "->pack(\$new);\n";
95    if ($tagname eq 'ShowFrame') {
96	print "#^------------------------------Frame No. $frame END.\n\n";
97	$frame++;
98    }
99}
100