Skip to content

Commit ee4f4c5

Browse files
tewaldsrichardapowell
authored andcommitted
Add a script to generate version information from replay files.
PiperOrigin-RevId: 460484266
1 parent 7c90c4d commit ee4f4c5

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

pysc2/bin/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,13 @@ pytype_strict_binary(
312312
"@s2protocol_archive//:versions",
313313
],
314314
)
315+
316+
pytype_strict_binary(
317+
name = "replay_version",
318+
srcs = ["replay_version.py"],
319+
deps = [
320+
"//pysc2/lib:replay",
321+
"//pysc2/run_configs",
322+
"@absl_py//absl:app",
323+
],
324+
)

pysc2/bin/replay_version.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/python
2+
# Copyright 2022 DeepMind Technologies Ltd. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS-IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
"""Generate version information from replays."""
16+
17+
import os
18+
19+
from absl import app
20+
21+
from pysc2 import run_configs
22+
from pysc2.lib import replay
23+
24+
25+
def main(argv):
26+
if len(argv) <= 1:
27+
raise app.UsageError(
28+
"Please give one or more replay files/directories to scan as argv.")
29+
30+
run_config = run_configs.get()
31+
32+
# Use a set over the full version struct to catch cases where Blizzard failed
33+
# to update the version field properly (eg 5.0.0).
34+
versions = set()
35+
36+
def replay_version(replay_path):
37+
"""Query a replay for information."""
38+
if replay_path.lower().endswith("sc2replay"):
39+
data = run_config.replay_data(replay_path)
40+
try:
41+
version = replay.get_replay_version(data)
42+
except (ValueError, KeyError):
43+
pass # Either corrupt or just old.
44+
except Exception as e: # pylint: disable=broad-except
45+
print("Invalid replay:", replay_path, e)
46+
else:
47+
versions.add(version)
48+
49+
try:
50+
for path in argv[1:]:
51+
if os.path.isdir(path):
52+
for root, _, files in os.walk(path):
53+
for file in files:
54+
replay_version(os.path.join(root, file))
55+
else:
56+
replay_version(path)
57+
except KeyboardInterrupt:
58+
pass
59+
60+
for version in sorted(versions):
61+
print(version)
62+
63+
64+
if __name__ == "__main__":
65+
app.run(main)

0 commit comments

Comments
 (0)