Skip to content

Commit

Permalink
implemented trigger parsing for scenario records including triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
Void / aoe2insights.com committed Jul 25, 2023
1 parent 8561e5b commit b54a45b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 5 deletions.
39 changes: 37 additions & 2 deletions mgz/fast/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def aoc_string(data):
return data.read(length)


def int_prefixed_string(data):
"""Read length prefixed (4 byte) string."""
length = unpack('<I', data)
return data.read(length)


def de_string(data):
"""Read DE string."""
assert data.read(2) == b'\x60\x0a'
Expand Down Expand Up @@ -273,11 +279,40 @@ def parse_scenario(data, num_players, version, save):
settings_version = 2.4
else:
settings_version = 2.2
end = remainder.find(struct.pack('<d', settings_version))
end += 1045
end = remainder.find(struct.pack('<d', settings_version)) + 8
else:
end = remainder.find(b'\x9a\x99\x99\x99\x99\x99\xf9\x3f') + 13
data.seek(end - len(remainder), 1)

if version is Version.DE:
data.read(1)
n_triggers = unpack("<I", data)

for _ in range(n_triggers):
data.read(22)
data.read(4)

description = int_prefixed_string(data)
name = int_prefixed_string(data)
short_description = int_prefixed_string(data)

n_effects = unpack("<I", data)

for _ in range(n_effects):
data.read(216)

text = int_prefixed_string(data)
sound = int_prefixed_string(data)

data.read(n_effects * 4)
n_condition = unpack("<I", data)

data.read(n_condition * 125)

trigger_list_order = unpack(f"<{n_triggers}I", data) # data.read(n_triggers * 4)

data.read(1032) # default!

return dict(
map_id=map_id,
difficulty_id=difficulty_id,
Expand Down
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/test_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ def test_players(self):
self.assertEqual(len(players), 3)


class TestFastDEScenarioWithTriggers(unittest.TestCase):

@classmethod
def setUpClass(cls):
with open('tests/recs/de-50.6-scenario-with-triggers.aoe2record', 'rb') as handle:
cls.data = parse(handle)

def test_version(self):
self.assertEqual(self.data['version'], Version.DE)

def test_players(self):
players = self.data.get('players')
self.assertEqual(len(players), 3)


class TestFastHD(unittest.TestCase):

@classmethod
Expand Down
50 changes: 47 additions & 3 deletions tests/test_files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import glob
import unittest
from mgz import header, body, fast

Expand Down Expand Up @@ -32,5 +31,50 @@ def test_files_full(self):
parse_file_full('tests/recs/de-13.07.aoe2record')

def test_files_fast(self):
for path in glob.glob('tests/recs/*'):
parse_file_fast(path)
parse_file_fast("tests/recs/test.mgz")
parse_file_fast("tests/recs/small.mgz")
parse_file_fast("tests/recs/size-255.mgz")
parse_file_fast("tests/recs/up-1.4.mgz")
parse_file_fast("tests/recs/aoc-1.0.mgx")
parse_file_fast("tests/recs/aoc-1.0c.mgx")
parse_file_fast("tests/recs/aok-2.0a.mgl")
parse_file_fast("tests/recs/hd-4.6.aoe2record")
parse_file_fast("tests/recs/hd-4.7.aoe2record")
parse_file_fast("tests/recs/hd-4.8.aoe2record")
parse_file_fast("tests/recs/hd-5.0.aoe2record")
parse_file_fast("tests/recs/hd-5.1.aoe2record")
parse_file_fast("tests/recs/hd-5.1a.aoe2record")
parse_file_fast("tests/recs/hd-5.3.aoe2record")
parse_file_fast("tests/recs/hd-5.5.aoe2record")
parse_file_fast("tests/recs/hd-5.6.aoe2record")
parse_file_fast("tests/recs/hd-5.7.aoe2record")
parse_file_fast("tests/recs/hd-5.8.aoe2record")
parse_file_fast("tests/recs/de-12.97-6byte-tile.aoe2record")
parse_file_fast("tests/recs/de-12.97-8byte-tile.aoe2record")
parse_file_fast("tests/recs/de-13.03.aoe2record")
parse_file_fast("tests/recs/de-13.06.aoe2record")
parse_file_fast("tests/recs/de-13.07.aoe2record")
parse_file_fast("tests/recs/de-13.08.aoe2record")
parse_file_fast("tests/recs/de-13.13.aoe2record")
parse_file_fast("tests/recs/de-13.15.aoe2record")
parse_file_fast("tests/recs/de-13.17.aoe2record")
parse_file_fast("tests/recs/de-13.20.aoe2record")
parse_file_fast("tests/recs/de-13.34.aoe2record")
parse_file_fast("tests/recs/de-20.06.aoe2record")
parse_file_fast("tests/recs/de-20.16.aoe2record")
parse_file_fast("tests/recs/de-25.01.aoe2record")
parse_file_fast("tests/recs/de-25.02.aoe2record")
parse_file_fast("tests/recs/de-25.06.aoe2record")
parse_file_fast("tests/recs/de-25.22.aoe2record")
parse_file_fast("tests/recs/de-26.16.aoe2record")
parse_file_fast("tests/recs/de-26.18.aoe2record")
parse_file_fast("tests/recs/de-26.21.aoe2record")
parse_file_fast("tests/recs/de-37.0.aoe2record")
parse_file_fast("tests/recs/de-37-int.aoe2record")
parse_file_fast("tests/recs/de-50.2.aoe2record")
parse_file_fast("tests/recs/de-50.3.aoe2record")
parse_file_fast("tests/recs/de-50.4.aoe2record")

# these files aren't supported by full header parser for now:
# parse_file_fast("tests/recs/de-50.6-scenario.aoe2record")
# parse_file_fast("tests/recs/de-50.6-scenario-with-triggers.aoe2record")

0 comments on commit b54a45b

Please sign in to comment.