Skip to content

Enable AI replay parsing and improve robustness#35

Open
liouh wants to merge 3 commits intoaoe2ct:mainfrom
liouh:main
Open

Enable AI replay parsing and improve robustness#35
liouh wants to merge 3 commits intoaoe2ct:mainfrom
liouh:main

Conversation

@liouh
Copy link
Copy Markdown

@liouh liouh commented Mar 20, 2026

Description

This PR improves the reliability of the Age of Empires II replay parser and enables support for replay files with AI players, which previously caused parsing failures. This is achieved by implementing a more robust resynchronization mechanism and adding support for several previously unhandled operation types found in AI-driven matches.

Fixes issue #34

Key Changes

  • AI Player Support: Added support for operation IDs 7-13, including Ai (7), MapNote (8), InitialState (9), and AiScript (13), which are necessary for parsing replays with AI.
  • Robust Resync Logic: Implemented a custom parse_operations loop that validates operation magic numbers (range 1-100). If invalid data is encountered, the parser now attempts to "re-sync" by seeking back and trying the next byte instead of failing the entire match.
  • Safe Action Parsing: Modified Operation::Action to treat ActionData as optional. If individual action data fails to parse (e.g., due to unknown formats in AI replays), the parser now records it as None and continues parsing.
  • Improved Error Handling: Gracefully handles UnexpectedEof to prevent crashes on abruptly terminated replay files.

Verification

Confirmed replays with AI players now parse correctly!

Added a new robust_tests module in crates/aoe2rec/src/tests.rs with the following unit tests:

  • test_parse_ai_operation: Confirms correct parsing of the new Ai operation.
  • test_parse_operations_resync: Validates the byte-skipping and resynchronization logic when encountering garbage data.
  • test_parse_action_with_failed_data: Ensures that a failure in ActionData parsing results in a None value rather than a parsing error.

Verified tests by running: cargo test -p aoe2rec robust_tests

Note: Existing integration tests were left untouched to ensure backward compatibility and avoid dependency on local environment file beargwyn_vs_kamlesh.aoe2record.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant