From 5e5ea5cba5ddcd002eddb5872ac8245e89d02c5d Mon Sep 17 00:00:00 2001 From: wraitii Date: Tue, 2 Mar 2021 08:35:39 +0000 Subject: [PATCH] Fix replayprofile/ graphing tool for Profiler1 All modern browsers block ajax request to local file. This changes extract.pl to generate a single HTML file with data embedded. This is now the default behaviour, --to-json to export, --from-json to load exported. Patch by: nwtour Reviewed By: wraitii Differential Revision: https://code.wildfiregames.com/D3621 This was SVN commit r24975. --- source/tools/replayprofile/extract.pl | 66 +++++++++++++++++++++++---- source/tools/replayprofile/graph.html | 12 ++--- source/tools/replayprofile/graph.js | 17 ++----- 3 files changed, 63 insertions(+), 32 deletions(-) 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 @@ 0 A.D. Simulation Profiling - - - - + + - +
-
-
File does not exist!
diff --git a/source/tools/replayprofile/graph.js b/source/tools/replayprofile/graph.js index dfe1d9d1df..2e4327a64f 100644 --- a/source/tools/replayprofile/graph.js +++ b/source/tools/replayprofile/graph.js @@ -50,6 +50,7 @@ var graphFormat = { function showReplayData() { + loadReplayGraphData(); var displayedColumn = $("#replayGraphSelection").val(); $.plot($("#replayGraph"), getReplayGraphData(displayedColumn), { @@ -137,20 +138,8 @@ function showTooltip(x, y, displayedColumn, label, turn, value) function loadReplayGraphData() { - $.ajax({ - "dataType": "json", - "url": $("#filename").val(), - "success": function(data) { - $("#errorMsg").hide(); - $("#replayGraphContainer").show(); - replayData = data; - showReplayData(); - }, - "error": function() { - $("#replayGraphContainer").hide(); - $("#errorMsg").show(); - } - }); +replayData = + } $(loadReplayGraphData);