Factorio python tooling

Calculate optimal ratios for feeding recipes, search through the research-tree, specialized tools to view game-information.
Post Reply
jcranmer
Long Handed Inserter
Long Handed Inserter
Posts: 90
Joined: Wed Jun 29, 2016 9:59 pm
Contact:

Factorio python tooling

Post by jcranmer »

So I've started a python library to read Factorio's data, complete with any installed and enabled mods. The library may be found here: https://github.com/jcranmer/factorio-tools. The library also comes with a Flask-based webserver that shows some statistics pages. The webserver itself does almost nothing, merely mapping URLs to either the static page list or a generated image file from the Factorio data files (including from within zipped mods, this is hard to do with command-line tools) or a JSON file representing parsed data. I could generate static pages for a given version (/mod combo), but including the images is risky licensing, and I don't really have accessible webspace at the moment to do any hosting.

Features:
  • schema.json: This file is a mapping of the Lua prototype data to JSON attributes. It notes the names, default values, types, and inheritance hierarchies of various tables. Well, for the properties I've seen fit to include to date. Getting everything added requires tracking down a whole lot of documentation that is missing or plain wrong.
  • ratio.html: This page dumps ratios, for example, saying that 2 electronics circuits to 3 copper cables is an exact ratio. I plan to add configurable maximum numerators/denominators (currently hardcoded to 100) and productivity module ratios later.
  • tech-tree.html: This page dumps a tech-tree in a layered graph representation. The output is admittedly hard-to-read, because I didn't implement all the steps of Sugiyama layout, and there's no way (yet) to highlight only a single node's induced subgraph.
The tool is written in Python, so it's theoretically cross-platform, but I'm almost certainly not matching the right filepaths on Windows or OS X, and Python itself may be hard to install for people on Windows. I'd be happy to share especially the schema.json file with other projects--reading in the Lua files directly and filling in missing properties is hard for the uninitiated. I may look someday at autogenerating Wiki documentation for the Lua prototype schemata, but the information at present is too incomplete to be worth the bother. Other people hacking on this would be much appreciated.

Fahrradkette
Inserter
Inserter
Posts: 45
Joined: Thu Aug 27, 2015 4:37 pm
Contact:

Re: Factorio python tooling

Post by Fahrradkette »

I did try it out and was first struggling:

Tried to convert it to python3 since it's something new, so no reason to use python2. The 2to3.py converter worked fine but I had problems with the Lupa dependency.
It turned out that my Lupa lib used luajit which by default targets lua 5.1 but factorio-tools uses lua 5.2. From lua 5.1 to 5.2 they changed package.loaders to package.seachers, used when lua loads modules using require().
Fixed it by uninstalling Lupa and luajit (cba building luajit with the 5.2 switch on), installing liblua5.2 and then reinstalling Lupa again.

Some time ago I also tried to get the recipes using python, I used slpp and did some parsing by hand.
Seems like the lua table->python list and lua table->python dict decision can be tricky sometimes.

factorio-tools did work with "manually added" mods but failed with my current AngelsBobs modpack:
lupa._lupa.LuaError: [string "<python>"]:377: attempt to call global 'floatationpipepictures' (a nil value)

I guess the mods directory only gets discovered 1 level deep. According to the bugtracker, Foreman (that windows-only tool) apparently also has(had?) that problem.

Anyways, I made a graph about how I imagine the required steps, I believe only the orange parts requires Lupa.
Image

Attal
Inserter
Inserter
Posts: 23
Joined: Fri Dec 23, 2016 9:49 am
Contact:

Re: Factorio python tooling

Post by Attal »

Hi!

I'm looking for factorio planner with active mods.
Your idea look very well, but I can't run it (mac)

I found few problems
  • Factorio path should be configurable, mine in

    Code: Select all

    FACTORIO_PATH = os.path.join("/Applications/factorio.app", "Contents")
  • please add support of mods with spaces in name - your tool ends with exception, i.e.this mod
  • Got exception after fixing PATH step
    ➜ factorio-tools git:(master) ✗ python server.py
    Traceback (most recent call last):
    File "server.py", line 6, in <module>
    data = factorio.load_factorio()
    File "~/factorio-tools/factorio.py", line 292, in load_factorio
    return FactorioData(path, mod_path, mod_list)
    File "~/factorio-tools/factorio.py", line 179, in __init__
    self._load_mod_file(mod, f)
    File "~/factorio-tools/factorio.py", line 234, in _load_mod_file
    self.lua.require(f)
    File "lupa/_lupa.pyx", line 274, in lupa._lupa.LuaRuntime.require (lupa/_lupa.c:5391)
    File "lupa/_lupa.pyx", line 1263, in lupa._lupa.call_lua (lupa/_lupa.c:19164)
    File "lupa/_lupa.pyx", line 1270, in lupa._lupa.execute_lua_call (lupa/_lupa.c:19255)
    File "lupa/_lupa.pyx", line 237, in lupa._lupa.LuaRuntime.reraise_on_exception (lupa/_lupa.c:4725)
    File "lupa/_lupa.pyx", line 1409, in lupa._lupa.py_call_with_gil (lupa/_lupa.c:20719)
    File "lupa/_lupa.pyx", line 1396, in lupa._lupa.call_python (lupa/_lupa.c:20549)
    File "~/factorio-tools/factorio.py", line 189, in load_fn
    val = self.lua.execute(contents)
    File "lupa/_lupa.pyx", line 259, in lupa._lupa.LuaRuntime.execute (lupa/_lupa.c:5167)
    File "lupa/_lupa.pyx", line 1254, in lupa._lupa.run_lua (lupa/_lupa.c:19015)
    File "lupa/_lupa.pyx", line 1263, in lupa._lupa.call_lua (lupa/_lupa.c:19164)
    File "lupa/_lupa.pyx", line 1270, in lupa._lupa.execute_lua_call (lupa/_lupa.c:19255)
    File "lupa/_lupa.pyx", line 237, in lupa._lupa.LuaRuntime.reraise_on_exception (lupa/_lupa.c:4725)
    File "lupa/_lupa.pyx", line 1409, in lupa._lupa.py_call_with_gil (lupa/_lupa.c:20719)
    File "lupa/_lupa.pyx", line 1372, in lupa._lupa.call_python (lupa/_lupa.c:20412)
    File "lupa/_lupa.pyx", line 1074, in lupa._lupa.py_from_lua (lupa/_lupa.c:16639)
    File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
    UnicodeDecodeError: 'utf8' codec can't decode byte 0xfc in position 4022: invalid start byte

Post Reply

Return to “Cheatsheets / Calculators / Viewers”