-
Notifications
You must be signed in to change notification settings - Fork 22
/
replay-ob.pl
executable file
·172 lines (143 loc) · 3.94 KB
/
replay-ob.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/perl
{
package LOL::OB::Replay;
use strict;
use utf8;
use lib './lib';
use JSON;
use HTTP::Server::Simple::CGI;
use base qw(HTTP::Server::Simple::CGI);
my %dispatch = (
'getGameMetaData' => \&getGameMetaData,
'getLastChunkInfo' => \&getLastChunkInfo,
'getGameDataChunk' => \&getGameDataChunk,
'getKeyFrame' => \&getKeyFrame,
'version' => \&version
);
sub workdir {
my ($s, $val) = @_;
if ($val) {
$s->{workdir} = $val;
open my $fh, '<', $s->{workdir} . "/meta.json" or return undef;
my $m = decode_json(<$fh>);
close $fh;
print STDERR $m->{key}, " ", $m->{gameKey}->{gameId}, "\n";
$s->{chunks} = $m->{pendingAvailableChunkInfo};
delete $m->{key};
$m->{clientBackFetchingEnabled} = JSON::true;
for (qw(pendingAvailableChunkInfo pendingAvailableKeyFrameInfo)) {
$m->{$_} = hash_to_sorted_array($m->{$_});
}
my $max_chunk_id = -1;
for (@{$m->{pendingAvailableChunkInfo}}) {
$max_chunk_id = int($_->{id}) if ($max_chunk_id < int($_->{id}));
}
$s->{metadata} = $m;
$s->{max_chunk_id} = $max_chunk_id;
$s->{do_startup} = 1;
}
return $s->{workdir};
}
sub handle_request {
my ($s, $cgi) = @_;
my $path = $cgi->path_info();
if ($path =~ m{^/observer-mode/rest/consumer/([^/]+)(.*)$}) {
my $handler = $dispatch{$1};
my @args = split(/\//, $2);
shift @args if $#args > -1 and $args[0] eq '';
if (ref($handler) eq 'CODE') {
$handler->($s, $cgi, @args);
return;
}
}
print "HTTP/1.0 500 Error\r\n";
print "Connection: close\r\n";
print "\r\n";
}
sub serve_file {
my ($fn) = @_;
my $buf;
open my $fh, '<', $fn or return undef;
binmode $fh;
binmode STDOUT;
while (read($fh, $buf, 65536) and print $buf) {}
close $fh;
return 1;
}
sub version {
my ($s, $cgi, $region, $gid, $idx, $token) = @_;
print "HTTP/1.0 200 OK\r\n", $cgi->header;
print "1.59.01";
}
sub getGameDataChunk {
my ($s, $cgi, $region, $gid, $idx, $token) = @_;
print "HTTP/1.0 200 OK\r\n", $cgi->header;
serve_file($s->{workdir} . "/chunk/$idx");
}
sub getKeyFrame {
my ($s, $cgi, $region, $gid, $idx, $token) = @_;
print "HTTP/1.0 200 OK\r\n", $cgi->header;
serve_file($s->{workdir} . "/keyframe/$idx");
}
sub getLastChunkInfo {
my ($s, $cgi, $region, $gid, $interval, $token) = @_;
my $gm = $s->{metadata};
my $ckid = $s->{max_chunk_id};
my $chunk = $s->{chunks}->{"$ckid"};
my $obj = {
chunkId => int $ckid,
availableSince => 30000,
nextAvailableChunk => 30000,
keyFrameId => int $gm->{lastKeyFrameId},
nextChunkId => int $s->{max_chunk_id},
endStartupChunkId => int $gm->{endStartupChunkId},
startGameChunkId => int $gm->{startGameChunkId},
duration => $chunk ? int $chunk->{duration} : 30000
};
print STDERR "do_startup=>", $s->{do_startup}, "\n";
if ($s->{do_startup} < 10) {
$ckid = $s->{do_startup};
$chunk = $s->{chunks}->{"$ckid"};
my $kf = find_keyframe($s, $ckid);
$obj->{chunkId} = int $ckid;
$obj->{keyFrameId} = int $kf->{id};
$obj->{nextChunkId} = int $kf->{nextChunkId};
$obj->{nextAvailableChunk} = 10000;
$obj->{duration} = $chunk ? int $chunk->{duration} : 30000;
$s->{do_startup}++;
}
print "HTTP/1.0 200 OK\r\n", $cgi->header;
print STDERR encode_json($obj), "\n";
print encode_json($obj);
}
sub getGameMetaData {
my ($s, $cgi, $region, $gid, $zero, $token) = @_;
print "HTTP/1.0 200 OK\r\n", $cgi->header;
print encode_json($s->{metadata});
}
sub find_keyframe {
my ($s, $ckid) = @_;
my $gm = $s->{metadata};
my $kf;
for (@{$gm->{pendingAvailableKeyFrameInfo}}) {
if (not $kf or $_->{nextChunkId} <= $ckid) {
$kf = $_;
} else {
last;
}
}
return $kf;
}
sub hash_to_sorted_array {
my ($h) = @_;
return [sort { int($a->{id}) <=> int($b->{id}) } values %$h];
}
}
if ($#ARGV != 0) {
print STDERR "Usage: $0 dir\n";
exit(-1);
}
my $server = LOL::OB::Replay->new(8088);
print "8088\n";
$server->workdir($ARGV[0]);
$server->run();