Skip to main content

Python bindings for faf-replay-parser

Project description

FAF Replay Parser

Build Status pypi Supported Python Versions

A fast library for parsing Supreme Commander Forged Alliance replay files.

Installation

Pre-built packages are available for Linux, MacOS, and Windows. You can install them with:

pip install faf-replay-parser

Documentation

Here are some examples of using the parser. Check help(fafreplay) for more details on available functions.

Gathering basic info

For the most basic uses there are a few special functions that should be preferred over constructing a Parser object. These do only one job, but they do it very quickly.

Current functions:

  • body_offset - Used for splitting replay data into header and body.
  • body_ticks - Used for extracting the game length.

Example

from datetime import timedelta
from fafreplay import body_offset, body_ticks

# Split replay data into header and body
offset = body_offset(data)
header_data, body_data = data[:offset], data[offset:]

# Get replay length in ticks
ticks = body_ticks(body_data)
print("Game length:", timedelta(milliseconds=ticks*100))

Using the Parser object

The Parser object can be used to get fine grained control over how replay commands are parsed. Generally it's a good idea to parse only the minimum commands needed, as conversion back to python dictionaries is quite expensive.

from datetime import timedelta
from fafreplay import Parser, commands

parser = Parser(
    # Skip all commands except the ones defined here
    commands=[
        commands.Advance,                   # For the tick counter
        commands.VerifyChecksum,            # For desync detection
    ],
    # Throw away commands right after we parse them. Setting this to `True` will
    # significantly increase the parse time.
    save_commands=False,
    limit=None,
    stop_on_desync=False
)
# Or create a parser with default arguments (turn off save_commands though)
# parser = Parser(save_commands=False)

# Read replay to a `bytes` object
with open("12345.scfareplay", "rb") as f:
    data = f.read()

# Parse to a python dictionary. Data must of type `bytes` or `bytearray`
replay = parser.parse(data)
print("Game time:", timedelta(milliseconds=replay["body"]["sim"]["tick"]*100))
if replay["body"]["sim"]["desync_ticks"]:
    print("Replay desynced!")

Benchmark comparison

To see how much faster the basic functions can be, consider this simple example done on replay 8653680 (an almost 50 minute long Seton's game).

>>> len(body_data)
5586339
>>> body_ticks(body_data)
28917
>>> parser = Parser(
...     commands=[commands.Advance],
...     save_commands=False
... )
>>> timeit.timeit("parser.parse_body(body_data)['sim']['tick']", globals=globals(), number=100)
1.4510237049980788
>>> timeit.timeit("body_ticks(body_data)", globals=globals(), number=100)
0.20173147800232982

In this case body_ticks turned out to be more than 7x faster than using a Parser.

Reading .fafreplay files

Replays downloaded from faforever.com use a compressed data format to reduce the size of the files. These can be decompressed to the original .scfareplay data using the extract_scfa function.

from fafreplay import extract_scfa


with open("12345.scfareplay", "rb") as f:
    scfa_data = f.read()

with open("12345.fafreplay", "rb") as f:
    faf_data = extract_scfa(f)

# The extracted data is in the .scfareplay format
assert faf_data == scfa_data

Note that there are several versions of the .fafreplay format. Version 1 uses base64 and zlib compression which are both part of the python standard library. However, version 2 uses zstd which must be installed through a third party package. To ensure that this dependency is installed you can use the faf extra when installing the parser:

pip install "faf-replay-parser[faf]"

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

faf-replay-parser-0.5.3.tar.gz (11.3 kB view hashes)

Uploaded Source

Built Distributions

faf_replay_parser-0.5.3-pp39-pypy39_pp73-win_amd64.whl (183.8 kB view hashes)

Uploaded PyPy Windows x86-64

faf_replay_parser-0.5.3-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

faf_replay_parser-0.5.3-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

faf_replay_parser-0.5.3-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (302.0 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

faf_replay_parser-0.5.3-pp38-pypy38_pp73-win_amd64.whl (184.1 kB view hashes)

Uploaded PyPy Windows x86-64

faf_replay_parser-0.5.3-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

faf_replay_parser-0.5.3-pp38-pypy38_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

faf_replay_parser-0.5.3-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (301.8 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

faf_replay_parser-0.5.3-pp37-pypy37_pp73-win_amd64.whl (186.1 kB view hashes)

Uploaded PyPy Windows x86-64

faf_replay_parser-0.5.3-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

faf_replay_parser-0.5.3-pp37-pypy37_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

faf_replay_parser-0.5.3-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (303.5 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

faf_replay_parser-0.5.3-cp310-cp310-win_amd64.whl (183.9 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

faf_replay_parser-0.5.3-cp310-cp310-win32.whl (173.9 kB view hashes)

Uploaded CPython 3.10 Windows x86

faf_replay_parser-0.5.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

faf_replay_parser-0.5.3-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

faf_replay_parser-0.5.3-cp310-cp310-macosx_10_9_x86_64.whl (301.6 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

faf_replay_parser-0.5.3-cp39-cp39-win_amd64.whl (184.2 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

faf_replay_parser-0.5.3-cp39-cp39-win32.whl (173.8 kB view hashes)

Uploaded CPython 3.9 Windows x86

faf_replay_parser-0.5.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

faf_replay_parser-0.5.3-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

faf_replay_parser-0.5.3-cp39-cp39-macosx_10_9_x86_64.whl (302.1 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

faf_replay_parser-0.5.3-cp38-cp38-win_amd64.whl (184.0 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

faf_replay_parser-0.5.3-cp38-cp38-win32.whl (174.4 kB view hashes)

Uploaded CPython 3.8 Windows x86

faf_replay_parser-0.5.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

faf_replay_parser-0.5.3-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

faf_replay_parser-0.5.3-cp38-cp38-macosx_10_9_x86_64.whl (301.6 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

faf_replay_parser-0.5.3-cp37-cp37m-win_amd64.whl (184.0 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

faf_replay_parser-0.5.3-cp37-cp37m-win32.whl (174.4 kB view hashes)

Uploaded CPython 3.7m Windows x86

faf_replay_parser-0.5.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

faf_replay_parser-0.5.3-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

faf_replay_parser-0.5.3-cp37-cp37m-macosx_10_9_x86_64.whl (301.6 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

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