forked from WoWs-Builder-Team/minimap_renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplay_parser.py
161 lines (131 loc) · 4.88 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
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
# coding=utf-8
from typing import BinaryIO
import logging
import os
import json
import struct
import zlib
from replay_unpack.clients import wot, wows
from replay_unpack.replay_reader import ReplayReader, ReplayInfo, REPLAY_SIGNATURE
class DefaultEncoder(json.JSONEncoder):
def default(self, o):
try:
return o.__dict__
except AttributeError:
return str(o)
class CustomReader(ReplayReader):
# noinspection PyMissingConstructor
def __init__(self, fp: BinaryIO, dump_binary=False):
self._dump_binary_data = dump_binary
self._fp = fp
self._type = "wowsreplay"
def get_replay_data(self) -> ReplayInfo:
"""
Get open info about replay
(stored as Json at the beginning of file)
and closed one
(after decrypt & decompress);
:rtype: tuple[dict, str]
"""
if self._fp.read(4) != REPLAY_SIGNATURE:
raise ValueError("File %s is not a valid replay" % self._replay_path)
blocks_count = struct.unpack("i", self._fp.read(4))[0]
block_size = struct.unpack("i", self._fp.read(4))[0]
engine_data = json.loads(self._fp.read(block_size))
extra_data = []
for i in range(blocks_count - 1):
block_size = struct.unpack("i", self._fp.read(4))[0]
data = json.loads(self._fp.read(block_size))
extra_data.append(data)
# noinspection PyUnresolvedReferences
decrypted_data = zlib.decompress(self._ReplayReader__decrypt_data(self._fp.read()))
if self._dump_binary_data:
self._save_decrypted_data(decrypted_data)
return ReplayInfo(
game="wows",
engine_data=engine_data,
extra_data=extra_data,
decrypted_data=decrypted_data,
)
class ReplayParser(object):
BASE_PATH = os.path.dirname(__file__)
def __init__(self, fp: BinaryIO, strict: bool = False, raw_data_output=None):
self._fp = fp
self._is_strict_mode = strict
self._reader = CustomReader(fp)
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):
if replay.game == 'wot':
# 'World of Tanks v.1.8.0.2 #252'
version = '.'.join(replay.engine_data.get('clientVersionFromXml')
.replace('World of Tanks v.', '')
.replace(' ', '.')
.replace('#', '')
.split('.')[:3])
player = wot.ReplayPlayer(version)
elif replay.game == 'wows':
player = wows.ReplayPlayer(replay.engine_data
.get('clientVersionFromXml')
.replace(' ', '')
.split(','))
else:
raise NotImplementedError
if self._raw_data_output:
with open(self._raw_data_output, 'wb') as fp:
fp.write(replay.decrypted_data)
player.play(replay.decrypted_data, self._is_strict_mode)
return player.get_info()
def main(replay, strict_mode, raw_data_output):
logging.basicConfig(level=getattr(logging, namespace.log_level))
with open(replay, "rb") as fp:
replay_info = ReplayParser(
fp,
strict=strict_mode,
raw_data_output=raw_data_output
).get_info()
import pickle
with open("data.dat", "wb") as f:
pickle.dump(replay_info["hidden"]["replay_data"], f)
# print(json.dumps(replay_info, indent=1, cls=DefaultEncoder))
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--replay', type=str, required=True)
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()
main(namespace.replay, namespace.strict_mode, namespace.raw_data_output)