Skip to content

Commit

Permalink
parse improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
happyleavesaoc committed May 29, 2023
1 parent e374ede commit c824e05
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 39 deletions.
4 changes: 2 additions & 2 deletions mgz/fast/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ def parse_player(header, player_number, num_players, save):

if save >= 37:
offset = header.tell()
data = header.read()
data = header.read(100)
# Jump to the end of player data
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\x00\x0b', data)
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)
if not player_end:
raise RuntimeError("could not find player end")
header.seek(offset + player_end.end())
Expand Down
2 changes: 1 addition & 1 deletion mgz/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
def enrich_action(action, action_data, dataset, consts):
"""Enrich action data with lookups."""
if 'x' in action_data and 'y' in action_data and action_data['x'] >=0 and action_data['y'] >= 0:
if action.type != fast.Action.SPECIAL or action_data['target_id'] > 0:
if action.type != fast.Action.SPECIAL or ('target_id' in action_data and action_data['target_id'] > 0):
action.position = Position(action_data['x'], action_data['y'])
del action.payload['x']
del action.payload['y']
Expand Down
42 changes: 7 additions & 35 deletions mgz/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@
SEARCH_MAX_BYTES = 3000
POSTGAME_LENGTH = 2096
LOOKAHEAD = 9
DE_MARKERS = {
50: b"\xa4\x70\xbd\x3f",
37: b"\xf6\x28\xbc\x3f",
26.21: b"\xf6\x28\xbc\x3f",
26.16: b"\x48\xe1\xba\x3f",
25.06: b"\x9a\x99\xb9\x3f",
25.03: b"\x85\xeb\x91\x3f",
25.02: b"\xec\x51\xb8\x3f",
25.01: b"\x3d\x0a\xb7\x3f",
20.16: b"\x8f\xc2\xb5\x3f",
20.06: b"\xe1\x7a\xb4\x3f",
13.34: b"\x33\x33\xb3\x3f",
13.07: b"\x29\x5c\xaf\x3f",
12.97: b"\x7b\x14\xae\x3f"
}


class Version(Enum):
Expand Down Expand Up @@ -290,26 +275,13 @@ def _parse(self, stream, context, path):
# Otherwise, this is the last player
else:
# Search for the scenario header
# TODO: make this section more reliable
marker_aok = read_bytes.find(b"\x9a\x99\x99\x3f")
marker_up = read_bytes.find(b"\xf6\x28\x9c\x3f")
marker_hd = read_bytes.find(b"\xae\x47\xa1\x3f")
marker_de = -1
for sv, search in DE_MARKERS.items():
if save_version >= sv:
marker_de = read_bytes.find(search)
break
marker = -1
if marker_aok > 0 and version is Version.AOK:
marker = marker_aok
elif marker_hd > 0 and version is Version.HD:
marker = marker_hd
elif marker_de > 0 and version is Version.DE:
marker = marker_de
elif marker_up > 0:
marker = marker_up
if marker == -1:
raise RuntimeError("could not find scenario marker")
for i, b in enumerate(read_bytes):
if b == 63 and b"\xff\xff\xff\xff\x00\x00\x00\x00" == read_bytes[i-11:i-3]:
flt = struct.unpack("<f", read_bytes[i-3:i+1])[0]
# scenario header version is something like 1.x
if flt > 1.0 and flt < 2.0:
marker = i - 3
break
# Backtrack through the achievements and initial structure footer
backtrack = ((1817 * (num_players - 1)) + 4 + 19)
# Seek to the position we found
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='mgz',
version='1.8.21',
version='1.8.22',
description='Parse Age of Empires 2 recorded games.',
url='https://github.com/happyleavesaoc/aoc-mgz/',
license='MIT',
Expand Down

0 comments on commit c824e05

Please sign in to comment.