This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
forked from Monstrofil/replays_unpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replay_parser.py
104 lines (87 loc) · 2.97 KB
/
replay_parser.py
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
# coding=utf-8
import sys
import json
import logging
import os
import time
import typing
from json import JSONEncoder
from replay_unpack.clients import wows
from replay_unpack.replay_reader import ReplayReader, ReplayInfo
class DefaultEncoder(JSONEncoder):
def default(self, o):
try:
return o.__dict__
except AttributeError:
return str(o)
class ReplayParser(object):
BASE_PATH = os.path.dirname(__file__)
def __init__(self, binary_replay_data: typing.IO, strict: bool = False, raw_data_output=None):
self._binary_replay_data = binary_replay_data
self._is_strict_mode = strict
self._reader = ReplayReader(binary_replay_data)
self._raw_data_output = raw_data_output
def get_info(self):
replay = self._reader.get_replay_data()
error = None
try:
hidden_data = self._get_hidden_data(replay)
except Exception as e:
if isinstance(e, RuntimeError):
error = str(e)
logging.exception(e)
hidden_data = None
# raise error in strict mode
if self._is_strict_mode:
raise
return dict(
open=replay.engine_data,
extra_data=replay.extra_data,
hidden=hidden_data,
error=error
)
def _get_hidden_data(self, replay: ReplayInfo):
player = wows.ReplayPlayer(replay.engine_data
.get('clientVersionFromXml')
.replace(' ', '')
.split(','))
if self._raw_data_output:
with open(self._raw_data_output, 'wb') as f:
f.write(replay.decrypted_data)
player.play(replay.decrypted_data, self._is_strict_mode)
return player.get_info()
if __name__ == '__main__':
start = time.time()
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--replay', type=str, required=False)
parser.add_argument(
'--log_level',
choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'],
required=False,
default='ERROR'
)
parser.add_argument(
'--strict_mode',
action='store_true',
required=False
)
parser.add_argument(
'--raw_data_output',
help='File where raw replay content (decoded and decompressed) will be written',
required=False,
default=None
)
namespace = parser.parse_args()
if namespace.replay is None:
replay_data = sys.stdin.buffer
else:
replay_data = open(namespace.replay, 'rb')
logging.basicConfig(
level=getattr(logging, namespace.log_level))
replay_info = ReplayParser(
replay_data, strict=namespace.strict_mode,
raw_data_output=namespace.raw_data_output).get_info()
print(json.dumps(replay_info, indent=1, cls=DefaultEncoder))
end = time.time()
print("Total duration: " + str(end - start))