Page 1 of 1

Load data folder with lua outside of Factorio

Posted: Sun Nov 29, 2020 11:10 am
by Mat5i6
Hi! As the title says, my goal is to load (require and execute) all Factorio packages inside data folder as Factorio would.

The main problem which troubles me is how the Factorio is able to manage all the require() calls from within the package. I've read the lua documentation about the require function probably 6 times already and besides the fact that it's an absolute mess I do believe that the Factorio devs had either modified and compiled their own version of lua to suit their needs or modified the require function or used something within the lua to make it work. And that's what I want to ask here. If there is someone with an insight of how this was achieved and how I could replicate it, it would really helped me.


Thanks in advance,
Mat5i6.

Re: Scan data folder with plain lua

Posted: Sun Nov 29, 2020 1:09 pm
by Deadlock989
Mat5i6 wrote:
Sun Nov 29, 2020 11:10 am
I do believe that the Factorio devs had either modified and compiled their own version of lua to suit their needs or modified the require function or used something within the lua to make it work. And that's what I want to ask here. If there is someone with an insight of how this was achieved it would really helped me.
https://lua-api.factorio.com/latest/Libraries.html

Otherwise I understand nothing from your explanation or your code example and have no idea what you are trying to achieve. It's an "absolute mess" as you put it.

Re: Scan data folder with plain lua

Posted: Sun Nov 29, 2020 1:38 pm
by Mat5i6
Thank you for the quick response and the link. Sorry if I wrote somewhat confusingly, since I didn't even know what I'm looking for. I've cleaned up my question so I hope now it will be more clear.

The reason why I'm asking here is that, I would like to load all the packages as Factorio would but without Factorio so I can use it later on in javascript. As such I figured that I would define my own data:extend which would store the prototypes just like data.raw does, required all the packages (order by dependency wise), converted the stored prototypes to JSON and then just returned to my JS code.

Eventually I would like to create a simple modding tool which should first collect all prototypes from dependencies and then use them in auto-complete fields, dropdowns, etc... but that's a long road ahead. Thus firstly I need to find a way to collect those prototypes but that proves to be more complicated than I thought.

Re: Scan data folder with plain lua

Posted: Sun Nov 29, 2020 1:54 pm
by Deadlock989
Data prototypes and the files that make them are not really "packages" in the sense that I think you might mean. Prototypes are constructed in a variety of ways in Factorio base mod code. Some are just simple data:extend calls that take the whole hand-written prototype as a single hardcoded table. Others pull in re-used property values returned from functions in other required files. Yet others are generated more procedurally. All of them are just dumped into the big data.raw table in the end.

If I were attempting what you were attempting I would not be trying to override anything but be looking instead at writing a simple Factorio mod which dumps the specific prototype info I need from the prototypes (i.e. after the base mod has finished constructing them) to the log (in JSON or whatever format) and then write some other external scripts to take it from there, but there are lots of ways of approaching it. As far as I know vanilla does all of its own prototype building in the data stage with the single exception of barrelling items and recipes which are created in data-updates, so if you do your dump in data-updates.lua or data-final-fixes.lua you would catch everything the base Factorio mod creates.

Re: Load data folder with lua outside of Factorio

Posted: Sun Nov 29, 2020 2:07 pm
by ickputzdirwech
If I understand correctly what you are talking about have a look at the following mods. Otherwise ignore me ;)

https://mods.factorio.com/mod/DataRawSerpent

viewtopic.php?f=135&t=45107

Re: Scan data folder with plain lua

Posted: Sun Nov 29, 2020 2:35 pm
by Mat5i6
Deadlock989 wrote:
Sun Nov 29, 2020 1:54 pm
Data prototypes and the files that make them are not really "packages" in the sense that I think you might mean. Prototypes are constructed in a variety of ways in Factorio base mod code. Some are just simple data:extend calls that take the whole hand-written prototype as a single hardcoded table. Others pull in re-used property values returned from functions in other required files. Yet others are generated more procedurally. All of them are just dumped into the big data.raw table in the end.

If I were attempting what you were attempting I would not be trying to override anything but be looking instead at writing a simple Factorio mod which dumps the specific prototype info I need from the prototypes (i.e. after the base mod has finished constructing them) to the log (in JSON or whatever format) and then write some other external scripts to take it from there, but there are lots of ways of approaching it. As far as I know vanilla does all of its own prototype building in the data stage with the single exception of barrelling items and recipes which are created in data-updates, so if you do your dump in data-updates.lua or data-final-fixes.lua you would catch everything the base Factorio mod creates.
Yeah, I probably do get where this comes from. There is no point in reinventing the wheel... I wanted to try it this way to avoid the extra hassle with placing some mod into the game, then running the game and eventually process the result once the game is loaded. When as you said, everything is placed into the big data.raw table in the end. I would just have to simulate the proper order of things but that is probably more pain than gain. Once again thank you for your help.

Re: Load data folder with lua outside of Factorio

Posted: Sun Nov 29, 2020 2:37 pm
by Bilka
If you don't want to use a Factorio mod, I'd recommend you to use existing Factorio data dumping/interaction tools instead of reinventing the wheel. If those tools aren't to your liking, you can at least use them for guidance on how things work.

These are the (non Factorio mod) tools that I could quickly find in my bookmarks. Unsorted; documentation and feature sets vary: Some discussion of some of these tools can be found here: viewtopic.php?p=495794#p495794 and in the Github issue linked there.

Disclaimer: I have not used most of these tools. This is just a list of existing tools that I am aware of, not a recommendation for any specific tool.