Is it possible to call functions in cpp from lua?

Place to get help with not working mods / modding interface.
Post Reply
yagaodirac
Fast Inserter
Fast Inserter
Posts: 152
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

Is it possible to call functions in cpp from lua?

Post 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?

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

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

Post 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.

Qon
Smart Inserter
Smart Inserter
Posts: 2119
Joined: Thu Mar 17, 2016 6:27 am
Contact:

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

Post 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.

yagaodirac
Fast Inserter
Fast Inserter
Posts: 152
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

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

Post 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.

Creidhne
Inserter
Inserter
Posts: 28
Joined: Mon Jun 10, 2019 9:43 am
Contact:

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

Post 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.

yagaodirac
Fast Inserter
Fast Inserter
Posts: 152
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

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

Post 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.

blahfasel2000
Inserter
Inserter
Posts: 49
Joined: Sat Mar 28, 2020 2:10 pm
Contact:

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

Post 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.

posila
Factorio Staff
Factorio Staff
Posts: 5201
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

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

Post 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

yagaodirac
Fast Inserter
Fast Inserter
Posts: 152
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

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

Post 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.

posila
Factorio Staff
Factorio Staff
Posts: 5201
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

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

Post 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

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

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

Post 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. ;(
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: ζ—₯本θͺž, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

yagaodirac
Fast Inserter
Fast Inserter
Posts: 152
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

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

Post 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.

Post Reply

Return to β€œModding help”