Skip to main content

A formatter for AoE2 AI rule files (.per).

Project description

Age of Empires 2 AI Rule Formatter

This project provides a script that allows basic syntax checking and formatting of AoE2 .per files.

Note that this tool is currently a work-in-progress. Crucially, it does not yet have full compatibility with .per files built for DE or User Patch versions of the game.

This tool depends on Python >= 3.6.

It has been developed using Python 3.6 on Ubuntu for Windows (WSL), but has also been tested against Python 3.8 on Ubuntu 20.04, and Python 3.8 on Windows 10. In any case, the tool should work fine in any environment which provides the required Python version. Development requires a Linux environment.

Library Usage

The tool can be used from the command line, or can be used as a library in your python projects. To use as a library, you can install via pip.

pip install aoe2-ai-fmt

Then use the format_per function as follows.

from performat import format_per

# Define a string containing some messy per content
my_per_content='''
  (defrule (players-unit-type-count 1 scout-cavalry < 6)
    =>
  (goal 1 100) (disable-self) )
'''

# Write formatted per content to formatted_output.per
with open('formatted_output.per', 'w', encoding='utf8', newline='\r\n') as out:
    format_per(in_string=my_per_content, out_stream=out)

See format.py for an alternative usage example.

Usage From Source

Dependencies

Building the tool requires ANTLR v4.8, pipenv, and GNU Make.

Installation From Source

Clone the repository, and then use pipenv to create a python virtualenv and install the necessary dependencies within it. Specify the --dev argument if you're going to be making changes.

pipenv --python 3 install [--dev]

(Note that python 3.6 is required, but this is not enforced by the Pipfile thanks to a limitation in pipenv.)

It is then necessary to build the lexer, parser, and supporting files using antlr4. The provided Makefile can invoke this for you, provided antlr4 is in your $PATH.

make

Then, simply run the tool from the command line, from within the virtualenv:

# ./format.py <path/to/src.per> <path/to/output.per>
# If the output path is provided as '-' then the formatted script will instead be printed to STDOUT.
pipenv run ./format.py examples/multistmt.input.per -

Input files may use Windows or Unix style line endings. Output files will always be created with Windows line endings, to match the platform of the game.

Testing

A small suite of example inputs and their desired pretty printed equivalents are provided in the repo. A basic test recipe is included in the Makefile to easily run the tool against all inputs, and allow manual inspection of any differences to the desired output.

make test

A make rule exists to run pylint, with the included .pylintrc ignoring the auto-generated ANTLR4 parser files.

make lint

Limitations

As the tool is built using a parser, invalid syntax will cause the formatter to error.

The tool is built based upon the language details provided in the official documentation supplied with The Conquerors. As this document isn't a formal language specification, some assumptions had to be made, particularly regarding allowed characters in identifiers and whitespace. It is possible that files accepted by this tool will not be accepted by the game engine, or vice versa. Notably, the more advanced actions and facts available in DE and User Patch are not currently supported by the tool, but support for these is in progress.

Note that this tool does not check anything more than basic syntax. Non-existant facts or actions, incorrect numbers of arguments, undefined constants, and other similar errors will not be detected by this tool.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aoe2-ai-fmt-0.2.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

aoe2_ai_fmt-0.2-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file aoe2-ai-fmt-0.2.tar.gz.

File metadata

  • Download URL: aoe2-ai-fmt-0.2.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.9

File hashes

Hashes for aoe2-ai-fmt-0.2.tar.gz
Algorithm Hash digest
SHA256 54511324630f583c4ea2a29077c53cc2279fd10ea28ef9e35fda6c19d7d53def
MD5 a8d0a86b864f15e9e03dc01bcb1671ab
BLAKE2b-256 7fb4386b595042bf8a2b2dd36fa201df8b9eb284e224644fef47bbf02d23fe64

See more details on using hashes here.

File details

Details for the file aoe2_ai_fmt-0.2-py3-none-any.whl.

File metadata

  • Download URL: aoe2_ai_fmt-0.2-py3-none-any.whl
  • Upload date:
  • Size: 20.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.9

File hashes

Hashes for aoe2_ai_fmt-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6c9c57d2108954952e06bb11b9698e9e4f95994d9a1171a7b8646cb039fb09e3
MD5 a19999c5b77baf11ea27bb2bf381465e
BLAKE2b-256 d275498e5cc6b4531b996b3ebbf47c9d7541acd035a7c085ce76b1a89340bb9b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page