Is it possible to call functions in cpp from lua?
-
- Fast Inserter
- Posts: 152
- Joined: Sun Jun 16, 2019 4:04 pm
- Contact:
Is it possible to call functions in cpp from lua?
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?
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?
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.
No, you can't write your own cpp functions or access unlisted ones.
Re: Is it possible to call functions in cpp from lua?
He already knows it. I told him earlier.
Not sure why he is creating another thread to talk about this again.Qon wrote: ↑Thu Oct 15, 2020 3:20 pmNot 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!?yagaodirac wrote: ↑Thu Oct 15, 2020 2:14 pm3, 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.
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.
My mods: Capsule Ammo | HandyHands - Automatic handcrafting | ChunkyChunks - Configurable Gridlines
Some other creations: Combinassembly Language GitHub w instructions and link to run it in your browser | 0~drain Laser
Some other creations: Combinassembly Language GitHub w instructions and link to run it in your browser | 0~drain Laser
-
- Fast Inserter
- Posts: 152
- Joined: Sun Jun 16, 2019 4:04 pm
- Contact:
Re: Is it possible to call functions in cpp from lua?
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.Qon wrote: ↑Fri Oct 16, 2020 12:50 pmHe already knows it. I told him earlier.Not sure why he is creating another thread to talk about this again.Qon wrote: ↑Thu Oct 15, 2020 3:20 pmNot 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!?yagaodirac wrote: ↑Thu Oct 15, 2020 2:14 pm3, 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.
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.
Re: Is it possible to call functions in cpp from lua?
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).yagaodirac wrote: ↑Fri Oct 16, 2020 2:49 amIs it possible to build some dll and call the functions from __my_mod__?
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.
-
- Fast Inserter
- Posts: 152
- Joined: Sun Jun 16, 2019 4:04 pm
- Contact:
Re: Is it possible to call functions in cpp from lua?
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.Creidhne wrote: ↑Sun Oct 18, 2020 4:25 pmAs 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.
-
- Long Handed Inserter
- Posts: 50
- Joined: Sat Mar 28, 2020 2:10 pm
- Contact:
Re: Is it possible to call functions in cpp from lua?
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?
Somebody made a native mod adding a feature that didn't exist a while ago: https://github.com/GrayMage/GrayModblahfasel2000 wrote: ↑Tue Oct 20, 2020 8:47 amStrictly 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.
-
- Fast Inserter
- Posts: 152
- Joined: Sun Jun 16, 2019 4:04 pm
- Contact:
Re: Is it possible to call functions in cpp from lua?
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 wrote: ↑Tue Oct 20, 2020 9:26 amSomebody 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?
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.yagaodirac wrote: ↑Thu Oct 22, 2020 2:34 amBut 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.
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
- eradicator
- Smart Inserter
- Posts: 5206
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Is it possible to call functions in cpp from lua?
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
-
- Fast Inserter
- Posts: 152
- Joined: Sun Jun 16, 2019 4:04 pm
- Contact:
Re: Is it possible to call functions in cpp from lua?
Thanks.posila wrote: ↑Thu Oct 22, 2020 7:59 amPeople 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
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.