nyurik / fatul

Make Factorio blueprints easy to version in git, minimizing text changes.
MIT License
8 stars 2 forks source link

Factorio Blueprint GIT Tool (FaTul)

Treat Factorio blueprints for what they really are -- software programs: version control, git stored, and CI-tested. This tool makes it possible, and keeps version diffs easy to read.

Copy fatul.py to your project and run pip3 install pyperclip if clipboard is needed for easiest usage. See usage below.

Why?

A commit to brians-blueprints has 23 changed files with 736,839 insertions and 119,118 deletions.

One of the modified files, basic science.json, had 25,875 additions, 25,843 deletions. FaTul makes that 17 insertions, 13 deletions. That's 1,724 times smaller, and can be read by a human!

How?

Factorio JSON is not good to store in GIT because the entity IDs and their order can change on every export, creating a lot of useless text changes. Every entity in a blueprint has an x,y position, so FaTul can create a relative link to the entity:

When saving a new blueprint, entities positions are normalized by shifting them to near-zero values, and storing a global x,y shift. When the blueprint is updated, FaTul attempts to match the old blueprint to the new one, and shift the entities of the new version to match the old one. This way the x,y position of most entities will remain intact between revisions, and only the blueprint main shift values might change.

Sorting is another reason for large diffs. Factorio could order entities in any order on every export, so to minimize that, FaTul re-sorts entities by their names following by the x,y position using Z-order curve. This way entities that are close together on a blueprint are more likely to stay together in a list.

Installation

Simple Installation

The simple method is to download fatul.py into your project and optionally run pip3 install pyperclip for clipboard support.

Advanced Installation

Alternatively, you can clone the repository to get some additional shortcuts and an easier way to update the code.

# Clone/download tools
git clone https://github.com/nyurik/fatul.git
cd fatul

# [Optional] It is usually a good idea to use Python virtualenv.
# If created, make sure to activate it before using fatul.py
virtualenv -p python3 venv
activate venv/bin/activate

# [Optional] Install pyperclip for clipboard support
# If not installed, you can still encode/decode files
pip3 install pyperclip

Usage

# Convert a factorio string in clipboard to a file my_data.json,
# or if it's a blueprint, directory my_data/.
python3 fatul.py decode my_data
# same, but decode clipboard to console
python3 fatul.py decode -

# Same as `decode`, but does not make any changes to JSON
python3 fatul.py dump -

# Convert a file (or dir) to a Factorio string and copy to clipboard.
python3 fatul.py encode my_data.json

# See help for more commands
python3 fatul.py --help
python3 fatul.py decode --help

The repository also contains a few shortcut scripts: decode, encode, and dump:

# this short command
./decode my_data
# is identical to the full command
python3 fatul.py decode my_data

Upgrading between versions

Development

Install just. Run just to run all sample tests and compares the output with the files in the test/expected dir. To update expected results, run just rebuild-expected.