Skip to content

Commit cf20473

Browse files
player end search fallback
1 parent 509b120 commit cf20473

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

mgz/fast/header.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from mgz.util import get_version, unpack, Version, as_hex
1010

11+
PLAYER_END = b'\xff\xff\xff\xff\xff\xff\xff\xff.\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0b'
1112
ZLIB_WBITS = -15
1213
CLASSES = [b'\x0a', b'\x1e', b'\x46', b'\x50', b'\x14']
1314
BLOCK_END = b'\x00\x0b'
@@ -139,13 +140,19 @@ def parse_player(header, player_number, num_players, save):
139140
data = header.read(100)
140141
device = data[8]
141142
# Jump to the end of player data
142-
player_end = re.search(b'\xff\xff\xff\xff\xff\xff\xff\xff.\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0b', data, re.DOTALL)
143+
player_end = re.search(PLAYER_END, data, re.DOTALL)
143144
if not player_end:
144-
# only a failure if this is not the last player, since we seek to the next block anyway
145-
# this issue happens on restored games
146-
if player_number < num_players - 1:
145+
# Normally this is 26 bytes in,
146+
# But in some cases (probably where object parsing failed),
147+
# it can be tens of thousands of bytes. So we have to `read()` everything
148+
offset = header.tell()
149+
data = header.read()
150+
player_end = re.search(PLAYER_END, data, re.DOTALL)
151+
if not player_end and player_number < num_players - 1:
152+
# this issue happens on restored games
153+
# only a failure if this is not the last player, since we seek to the next block anyway
147154
raise RuntimeError("could not find player end")
148-
else:
155+
if player_end:
149156
header.seek(offset + player_end.end())
150157

151158
return dict(

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name='mgz',
6-
version='1.8.50',
6+
version='1.8.51',
77
description='Parse Age of Empires 2 recorded games.',
88
url='https://github.com/happyleavesaoc/aoc-mgz/',
99
license='MIT',

0 commit comments

Comments
 (0)