-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatparser.py
307 lines (273 loc) · 9.81 KB
/
statparser.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""
This module provides functions for parsing and prettifying `"stats.dmp"` file.
"""
import base64
import json
import os
import re
import shutil
from dataclasses import dataclass
from datetime import datetime
from struct import unpack
from typing import Dict, List, Union
import mappings
PLAYER_SUFFIXES = "01234567"
_DecodedBlockType = Union[bool, int, str, Dict[str, int]]
_PrettifiedStatsType = Dict[str, Union[_DecodedBlockType, Dict[str, _DecodedBlockType]]]
def bytes_to_ascii(binary_blob: bytes) -> str:
"""
Convert bytes to ASCII string, replace invalid symbols with "?".
Args:
binary_blob: Input bytes.
Returns:
Decoded ASCII string.
"""
binary_blob = re.sub(rb"[^\x20-\x7E]", b"?", binary_blob)
return binary_blob.decode("ascii")
def write_dict_to_json(data: Dict, output_file: str) -> None:
"""
Save a dict as file in human readable JSON format.
Args:
data: Dict to save.
output_file: Output file.
"""
with open(output_file, "w") as file:
json.dump(data, file, indent=4, sort_keys=True)
@dataclass(frozen=True)
class BlockHeader:
"""
Class for storing block header and decoding block data.
"""
tag: str
type: int
length: int
@property
def padding(self) -> int:
"""
Block data padding.
"""
if self.length % 4:
return 4 - self.length % 4
return 0
@classmethod
def from_bytes(cls, binary_blob: bytes) -> "BlockHeader":
"""
Parse a block header from bytes.
"""
if len(binary_blob) != 8:
raise ValueError(
f"Block header should have length 8, "
f"but binary blob of length {len(binary_blob)} received."
)
binary_tag, type_, length = unpack(">4sHH", binary_blob)
tag = bytes_to_ascii(binary_tag)
return cls(tag, type_, length)
def decode_block(self, binary_blob: bytes) -> _DecodedBlockType:
"""
Parse a block data from bytes.
"""
if len(binary_blob) != self.length:
raise ValueError(
f"Block data should have length {self.length}, "
f"but binary blob of length {len(binary_blob)} received."
)
if self.type == 1:
# A single byte.
return unpack(">c", binary_blob)[0]
if self.type == 2:
# A single boolean.
return unpack(">?", binary_blob)[0]
if self.type == 3:
# A single short.
return unpack(">h", binary_blob)[0]
if self.type == 4:
# A single unsigned short.
return unpack(">H", binary_blob)[0]
if self.type == 5:
# A single long.
return unpack(">l", binary_blob)[0]
if self.type == 6:
# A single unsigned long.
return unpack(">L", binary_blob)[0]
if self.type == 7:
# Multiple b"\x00"-terminated chars.
return bytes_to_ascii(binary_blob.rstrip(b"\x00"))
if self.type == 20:
# Custom type and length.
if self.tag[:3] in mappings.HUMAN_READABLE_COUNTABLES:
# Multiple unsigned longs.
if self.length % 4:
raise ValueError(
f"Length of block data should be multiple of 4, "
f"but {self.length} received."
)
counts = unpack(f">{self.length // 4}L", binary_blob)
return {
mappings.COUNTABLE_TYPES[self.tag[:2]][i]: count
for i, count in enumerate(counts)
if count > 0
}
# Raw bytes.
# Bytes are not json serializable, so encode them as base64.
return base64.b64encode(binary_blob).decode("ascii")
raise ValueError(f"Decoding rule for type {self.type} is unknown.")
def parse_stats(filepath: str) -> Dict[str, _DecodedBlockType]:
"""
Parse a `"stats.dmp"` file to dict.
Args:
filepath: Path to a `"stats.dmp"` file.
Returns:
Parsed statistics.
"""
stats: Dict[str, _DecodedBlockType] = {}
with open(filepath, "rb") as file:
# Read file header.
binary_blob = file.read(4)
if len(binary_blob) < 4:
raise ValueError(
f"File header should have length 4, but binary blob of length "
f"{len(binary_blob)} received."
)
while True:
# Read and parse block header.
binary_blob = file.read(8)
if len(binary_blob) == 0:
break # End of file.
block_header = BlockHeader.from_bytes(binary_blob)
# Read and parse block data.
binary_blob = file.read(block_header.length)
stats[block_header.tag] = block_header.decode_block(binary_blob)
# Skip block padding.
file.read(block_header.padding)
return stats
def prettify_player_stats(
raw_stats: Dict[str, _DecodedBlockType]
) -> _PrettifiedStatsType:
"""
Prettify stats of a player. Only tags (keys of `raw_stats`) of one player
without specific suffix expected.
"""
stats: _PrettifiedStatsType = {}
detailed_counts: Dict[str, int] = {}
raw: Dict[str, _DecodedBlockType] = {}
for tag, value in raw_stats.items():
if tag == "CMP":
for code, status in mappings.COMPLETION_CODES.items():
stats[status] = bool(value & code)
elif tag == "RSG":
stats["quit"] = value
elif tag == "DED":
stats["defeated"] = value
elif tag == "SPC":
stats["spectator"] = value
elif tag in ("LCN", "CON"):
stats["disconnected"] = value
elif tag == "CTY":
stats["side"] = mappings.SIDES[value]
elif tag == "NAM":
stats["name"] = value
elif tag == "CRD":
stats["funds_left"] = value
elif tag in mappings.HUMAN_READABLE_COUNTABLES:
human_readable_tag = mappings.HUMAN_READABLE_COUNTABLES[tag]
stats[human_readable_tag] = sum(value.values(), 0)
detailed_counts[human_readable_tag] = value
else:
raw[tag] = value
stats["raw"] = raw
stats["detailed_counts"] = detailed_counts
return stats
def prettify_game_stats(
raw_stats: Dict[str, _DecodedBlockType]
) -> _PrettifiedStatsType:
"""
Prettify stats of a game. Only non-player specific tags (keys of
`raw_stats`) expected.
"""
stats: _PrettifiedStatsType = {}
raw: Dict[str, _DecodedBlockType] = {}
for tag, value in raw_stats.items():
if tag == "DURA":
stats["duration"] = value
elif tag == "AFPS":
stats["fps"] = value
elif tag == "FINI":
stats["finished"] = value
elif tag == "TIME":
stats["epoch_time"] = value
elif tag == "SCEN":
stats["map"] = value
elif tag == "UNIT":
stats["starting_units"] = value
elif tag == "CRED":
stats["starting_credits"] = value
elif tag == "SUPR":
stats["superweapons"] = bool(value)
elif tag == "CRAT":
stats["crates"] = bool(value)
elif tag == "PLRS":
stats["players_in_game"] = value
elif tag == "BAMR":
stats["mcv_repacks"] = bool(value & 1)
stats["build_off_ally_conyards"] = bool(value & 2)
elif tag == "SHRT":
stats["short_game"] = bool(value)
elif tag == "AIPL":
stats["ai_players"] = value
elif tag == "VERS":
stats["game_version"] = value
else:
raw[tag] = value
stats["raw"] = raw
return stats
def process_stats(stats_file: str, output_folder: str, reporter_name: str) -> str:
"""
Backup, parse and prettify a `"stats.dmp"` file.
Args:
stats_file: `"stats.dmp"` file.
output_folder: Backup folder.
reporter_name: Name of the current player to parse the game status from.
Returns:
Path to the prettified stats in JSON format.
"""
if not os.path.isfile(stats_file):
raise FileNotFoundError(
f'Given path "{stats_file}" does not exist or is not a file.'
)
raw_stats = parse_stats(stats_file)
players_stats = []
for suffix in PLAYER_SUFFIXES:
player_raw_stats = {
tag[:-1]: value for tag, value in raw_stats.items() if tag.endswith(suffix)
}
if player_raw_stats:
players_stats.append(prettify_player_stats(player_raw_stats))
game_raw_stats = {
tag: value for tag, value in raw_stats.items() if tag[-1] not in PLAYER_SUFFIXES
}
game_stats = prettify_game_stats(game_raw_stats)
game_result = "unknown"
for player_stats in players_stats:
if player_stats["name"] == reporter_name:
for status in mappings.COMPLETION_CODES.values():
if player_stats[status] is True:
game_result = status
break
stats: Dict[str, Union[str, _PrettifiedStatsType, List[_PrettifiedStatsType]]] = {
"gameReport": game_stats,
"playerStats": players_stats,
"gameResult": game_result,
}
timestamp = game_stats["epoch_time"]
output_folder = os.path.join(
output_folder,
datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H-%M-%S"),
)
os.makedirs(output_folder, exist_ok=True)
shutil.copy(stats_file, os.path.join(output_folder, f"{timestamp}_stats.dmp"))
write_dict_to_json(
raw_stats, os.path.join(output_folder, f"{timestamp}_stats_raw.json")
)
stats_json_file = os.path.join(output_folder, f"{timestamp}_stats_parsed.json")
write_dict_to_json(stats, stats_json_file)
return stats_json_file