Lua prototype definitions schema
Posted: Sun Aug 28, 2016 2:13 am
I'm not entirely sure if this is the best forum for this, but it does seem to be relevant to people here, so I'll post here instead of the Tools forum.
I was working on a Python library to help me build Factorio tools (a Factorio tool meta-tool if you will ). Loading the information from the Lua files directly, including supporting mods properly, took some time, but I did get it working [1] (including mods downloaded as zip files or specified as symlinks to another directory!). However, one challenge is that the definitions of the prototypes in the raw data table aren't well-specified to a public schema. Since trying to track down where the files might be located to look at examples quickly becomes a pain, I decided to first dump the entire Lua data table out in a JSON format. After staring at that for a bit, I realized what I needed to do.
I'm in the midst of writing a library that can load a static schema file, defined in JSON (the WIP is provided below). This schema file most importantly defines the inheritance order of the various types (e.g., an accumulator is an EntityWithHealth is an Entity). A Python module loads this schema and turns it into Python classes that you can instantiate [2] with the Lua objects. You can even get useful results if you run help on the module, e.g.:
Thoughts/questions/comments/concerns?
[1] Turns out that Lua's custom module loader requires that the data in question be a function, not merely something callable. So I had to write a Lua function that returns a lua function that calls a function passed into it and call the function from Python to get the right data type. Aren't proxies fun?
[2] Yes, this is dynamically defining classes in a module. It's a lot easier to do this in Python than when I had to write out the constant pools for Java interfaces...
I was working on a Python library to help me build Factorio tools (a Factorio tool meta-tool if you will ). Loading the information from the Lua files directly, including supporting mods properly, took some time, but I did get it working [1] (including mods downloaded as zip files or specified as symlinks to another directory!). However, one challenge is that the definitions of the prototypes in the raw data table aren't well-specified to a public schema. Since trying to track down where the files might be located to look at examples quickly becomes a pain, I decided to first dump the entire Lua data table out in a JSON format. After staring at that for a bit, I realized what I needed to do.
I'm in the midst of writing a library that can load a static schema file, defined in JSON (the WIP is provided below). This schema file most importantly defines the inheritance order of the various types (e.g., an accumulator is an EntityWithHealth is an Entity). A Python module loads this schema and turns it into Python classes that you can instantiate [2] with the Lua objects. You can even get useful results if you run help on the module, e.g.:
example help output
The WIP I have for the schema is below. I copied it mostly from the existing wiki pages, but there's a lot missing. The latter two sections are entirely guesswork on my part as to the hierarchy, and so I have 0 confidence in them. I'm also missing several tables entirely (beam, blueprint, combat-robot-count, curved-rail, custom-input, deconstructible-tile-proxy, deconstruction-item, electric-energy-interface, entity-ghost, fire, fluid, item-request-proxy, leaf-particle, module-category, particle-source, rail-planner, rocket-silo-rocket, rocket-silo-rocket-shadow, simple-entity, smoke-with-trigger, straight-rail, stream, tile-ghost, utility-sprites, virtual-signal). The optional/mandatory details according to the wiki seem wrong, given that several items seem to lack them at times.I do hope to validate the types and default values, but that is somewhat less important to me than getting the entire hierarchy accurate first.Prototype schema definition
If any developers are reading this thread, can you validate my facts? I could give you the Python help tree if it helps (or even the module code, though you probably have to be on Linux to get it to work). It would be even more wonderful if this kind of information could be produced automatically (like the LUA documentation), but I'm guessing that the code isn't so nicely structured as to give this information with automated documentation tools.Thoughts/questions/comments/concerns?
[1] Turns out that Lua's custom module loader requires that the data in question be a function, not merely something callable. So I had to write a Lua function that returns a lua function that calls a function passed into it and call the function from Python to get the right data type. Aren't proxies fun?
[2] Yes, this is dynamically defining classes in a module. It's a lot easier to do this in Python than when I had to write out the constant pools for Java interfaces...