Page 1 of 1

Is it possible to call functions in cpp from lua?

Posted: Fri Oct 16, 2020 2:49 am
by yagaodirac
Now we know, game calls the functions in lua, including __base__ and all the mods. Is it possible to build some dll and call the functions from __my_mod__?
If it's possible, what about different os? Sockets? Or is it possible to load some png?

Re: Is it possible to call functions in cpp from lua?

Posted: Fri Oct 16, 2020 3:00 am
by DaveMcW
There are many cpp functions you can call. They are listed here: https://lua-api.factorio.com/latest/

No, you can't write your own cpp functions or access unlisted ones.

Re: Is it possible to call functions in cpp from lua?

Posted: Fri Oct 16, 2020 12:50 pm
by Qon
He already knows it. I told him earlier.
Qon wrote:
Thu Oct 15, 2020 3:20 pm
yagaodirac wrote:
Thu Oct 15, 2020 2:14 pm
3, You shouldn't talk about this idea here. You know amd and intel? Ask them for the solution. But in fact, I just started knowing that, lua can call functions written in cpp. If you mod a interface in lua, take the advantage of cpp and graphics card, with some specially designed algo, I think it might have a chance to do what you want.
Not sure what I shouldn't talk about here... maybe you can say what you are talking about instead of saying "this" and quoting my entire post!?
Also you can't call C++ librararies from Lua code executed by Factorio. That would break the security model and the determinism guarantees and everything else.
Not sure why he is creating another thread to talk about this again.

Re: Is it possible to call functions in cpp from lua?

Posted: Sat Oct 17, 2020 3:18 am
by yagaodirac
Qon wrote:
Fri Oct 16, 2020 12:50 pm
He already knows it. I told him earlier.
Qon wrote:
Thu Oct 15, 2020 3:20 pm
yagaodirac wrote:
Thu Oct 15, 2020 2:14 pm
3, You shouldn't talk about this idea here. You know amd and intel? Ask them for the solution. But in fact, I just started knowing that, lua can call functions written in cpp. If you mod a interface in lua, take the advantage of cpp and graphics card, with some specially designed algo, I think it might have a chance to do what you want.
Not sure what I shouldn't talk about here... maybe you can say what you are talking about instead of saying "this" and quoting my entire post!?
Also you can't call C++ librararies from Lua code executed by Factorio. That would break the security model and the determinism guarantees and everything else.
Not sure why he is creating another thread to talk about this again.
But the interesting point is that, when I tried to answer you as what you quoted, my opinion is totally wrong according to the #2. I haven't tried this. But #2 seems to have tried or have very solid conclusion.

Re: Is it possible to call functions in cpp from lua?

Posted: Sun Oct 18, 2020 4:25 pm
by Creidhne
yagaodirac wrote:
Fri Oct 16, 2020 2:49 am
Is it possible to build some dll and call the functions from __my_mod__?
As mentioned by others, you can't have the Factorio process load any DLL you provide and start calling its functions from your Lua code (And from a security point of view, I hope it never happens).

That being said, if I take your question literally, the answer is that it's possible in multiplayer. You can establish a bidirectional connection between the Factorio server and another program. See Clusterio for an example.

You could have this other program load your C++ library and expose it via a "Remote Procedure Call" protocol to be used by your mods. It certainly is much tedious and much slower, but it does what you aksed for.

Re: Is it possible to call functions in cpp from lua?

Posted: Mon Oct 19, 2020 5:56 am
by yagaodirac
Creidhne wrote:
Sun Oct 18, 2020 4:25 pm
As mentioned by others, you can't have the Factorio process load any DLL you provide and start calling its functions from your Lua code (And from a security point of view, I hope it never happens).

That being said, if I take your question literally, the answer is that it's possible in multiplayer. You can establish a bidirectional connection between the Factorio server and another program. See Clusterio for an example.

You could have this other program load your C++ library and expose it via a "Remote Procedure Call" protocol to be used by your mods. It certainly is much tedious and much slower, but it does what you aksed for.
Yes, the security concerns. I really believe the method which is showed in Clusterio is the exact thing I need. Thank you very much. I'll definitely study the mod in a short future.

Re: Is it possible to call functions in cpp from lua?

Posted: Tue Oct 20, 2020 8:47 am
by blahfasel2000
Creidhne wrote:
Sun Oct 18, 2020 4:25 pm
As mentioned by others, you can't have the Factorio process load any DLL you provide and start calling its functions from your Lua code (And from a security point of view, I hope it never happens).
Strictly speaking there's nothing that would prevent someone from writing an external mod loader that could do such things by directly patching the executable code in memory, similar to how modding in other games without native mod support often works. In fact it would probably be even simpler to implement than in other games because Factorio ships with debug information.

However, that would be something that you'd have to install explicitly, not something that a normal mod could do without your knowledge. And because of the excellent native mod support it's unlikely that someone would put in the effort to implement it.

Re: Is it possible to call functions in cpp from lua?

Posted: Tue Oct 20, 2020 9:26 am
by posila
blahfasel2000 wrote:
Tue Oct 20, 2020 8:47 am
Strictly speaking there's nothing that would prevent someone from writing an external mod loader that could do such things by directly patching the executable code in memory, similar to how modding in other games without native mod support often works. In fact it would probably be even simpler to implement than in other games because Factorio ships with debug information.

However, that would be something that you'd have to install explicitly, not something that a normal mod could do without your knowledge. And because of the excellent native mod support it's unlikely that someone would put in the effort to implement it.
Somebody made a native mod adding a feature that didn't exist a while ago: https://github.com/GrayMage/GrayMod

Re: Is it possible to call functions in cpp from lua?

Posted: Thu Oct 22, 2020 2:34 am
by yagaodirac
posila wrote:
Tue Oct 20, 2020 9:26 am
Somebody made a native mod adding a feature that didn't exist a while ago: https://github.com/GrayMage/GrayMod
But it's very clear, you don't want people to do this. It's not safe at all literally. And it shouldn't be able to run on 1.0.

Re: Is it possible to call functions in cpp from lua?

Posted: Thu Oct 22, 2020 7:59 am
by posila
yagaodirac wrote:
Thu Oct 22, 2020 2:34 am
But it's very clear, you don't want people to do this. It's not safe at all literally. And it shouldn't be able to run on 1.0.
People who buy the game can do whatever they want with it (well, almost anything ... don't put it up on piratebay, don't resell it and stuff like that), as well as they can with their computers. If anyone wants to modify the game by injecting their own machine code into it, all power to them, as long as they don't bother us with crashes, multiplayer desyncs and other bugs. If anyone wants to download and apply such modifications on their computer, that's also perfectly fine, as long as we don't get complaints about any issues caused by this.

GrayMod works only on non-steam version of Factorio 0.14.21, because it has hardcoded function pointer offsets and vtable offsets, which literally change with every build. It also has to match layout of our classes it wants to use, which might change between minor versions, but often change significantly between major updates. I think all of this could be read from factorio.pdb, but you'd still have to recompile the mod against every new release of the game ... unless you made it to read all info it needs from the PDB and just-in-time recompile itself :D

Re: Is it possible to call functions in cpp from lua?

Posted: Thu Oct 22, 2020 5:19 pm
by eradicator
posila wrote:
Thu Oct 22, 2020 7:59 am
I think all of this could be read from factorio.pdb, but you'd still have to recompile the mod against every new release of the game ... unless you made it to read all info it needs from the PDB and just-in-time recompile itself :D
When wube has lost interest in factorio 10 years from now and interface requests aren't implemented anymore that sounds like a likely enough scenario for the modding community to move to. ;(

Re: Is it possible to call functions in cpp from lua?

Posted: Fri Oct 23, 2020 2:53 am
by yagaodirac
posila wrote:
Thu Oct 22, 2020 7:59 am
People who buy the game can do whatever they want with it (well, almost anything ... don't put it up on piratebay, don't resell it and stuff like that), as well as they can with their computers. If anyone wants to modify the game by injecting their own machine code into it, all power to them, as long as they don't bother us with crashes, multiplayer desyncs and other bugs. If anyone wants to download and apply such modifications on their computer, that's also perfectly fine, as long as we don't get complaints about any issues caused by this.

GrayMod works only on non-steam version of Factorio 0.14.21, because it has hardcoded function pointer offsets and vtable offsets, which literally change with every build. It also has to match layout of our classes it wants to use, which might change between minor versions, but often change significantly between major updates. I think all of this could be read from factorio.pdb, but you'd still have to recompile the mod against every new release of the game ... unless you made it to read all info it needs from the PDB and just-in-time recompile itself :D
Thanks.
Personally, I don't know how to dll inject neither will I learn it. The latest idea is that, if rcon works per mod rather than per server, the lua code in a mod decide what to send to something else through rcon and other code send data and instructions back to game via rcon. All the slow calculation task could be implemented out side lua. That helps in some special case. In my mod, that would make sense.
But, it's ok for now. The only trouble is that, mods make use of rcon can't work together. If people prefer Clusterio, they are not gonna play my mod in the future.