diff --git a/source/tools/replayprofile/extract.pl b/source/tools/replayprofile/extract.pl index dff021a5c1..ea713e1ae5 100644 --- a/source/tools/replayprofile/extract.pl +++ b/source/tools/replayprofile/extract.pl @@ -3,13 +3,21 @@ use strict; use warnings; +use Getopt::Long; +use File::Basename qw(dirname); +chdir(dirname(__FILE__)); + # Run the game normally to generate a replay. # Run the non-visual replay like: # ./pyrogenesis -mod=public -replay=$HOME/.local/share/0ad/replays/0.0.23/2018-03-23_0010/commands.txt # to generate profile.txt. # Then run: -# perl extract.pl > data.json -# then open graph.html. +# perl extract.pl +# OR +# perl extract.pl --to-json > data.json ... [manipulation with data.json] ... perl extract.pl --from-json +# then open graph_onepage.html. + +my ($to_json, $from_json); GetOptions ("to-json" => \$to_json, "from-json" => \$from_json); open my $f, '../../../binaries/system/profile.txt' or die $!; my $turn = 0; @@ -33,15 +41,55 @@ while (<$f>) { } } -print "[\n"; +my $json = "[\n"; my $n = 0; for my $k (sort keys %s) { - print ",\n" if $n++; - print "{label: '$k', data:["; + $json .= ",\n" if $n++; + $json .= "{label: '$k', data:["; for my $t (0..$#{$s{$k}}) { - print "," if $t; - print "[$t,".($s{$k}[$t] || 0)."]"; + $json .= "," if $t; + $json .= "[$t,".($s{$k}[$t] || 0)."]"; } - print "]}"; + $json .= "]}"; } -print "\n]\n"; +$json .= "\n]\n"; + +my $onepage = "graph_onepage.html"; + +sub recursive_file_slurp { + my $filename = shift; + my $output = ''; + open my $fd, $filename or die "Open $filename : $!"; + $output .= "\n" if $filename =~ /\.js$/; + close($fd); + return $output; +} + +if($to_json) { + print $json; + exit; +} +elsif($from_json) { + $json = recursive_file_slurp("data.json"); +} + +open my $opfd, '>', $onepage or die $!; +print $opfd recursive_file_slurp("graph.html"); +close($opfd); diff --git a/source/tools/replayprofile/graph.html b/source/tools/replayprofile/graph.html index 94fa9cba12..a99f64bfe3 100644 --- a/source/tools/replayprofile/graph.html +++ b/source/tools/replayprofile/graph.html @@ -3,16 +3,11 @@