Replace LUA code in a running game from command line?

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
Plawerth
Fast Inserter
Fast Inserter
Posts: 116
Joined: Thu Mar 02, 2017 12:57 am
Contact:

Replace LUA code in a running game from command line?

Post by Plawerth »

Several years ago I recall seeing some hacker genius injecting new LUA code into a running game from the command line, basically overwriting defined code blocks while the game is in session, and adding new events to the game event loop.

Though now I am trying to recall exactly what they did and I can't find it. Does anyone know the method?

This apparently permits fixing LUA bugs in a running game by injecting new fixed code over the top of existing buggy code. It all exists in memory so nothing is permanent except in the saved game after injecting the new code.

In general the LUA specification allows redefining core functions of even the language itself. It does not care.
https://en.wikipedia.org/wiki/Lua_(prog ... #Functions

So apparently it should be possible to redefine game.player.print and anything else under game.* from the command line.

Bilka
Factorio Staff
Factorio Staff
Posts: 3128
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Replace LUA code in a running game from command line?

Post by Bilka »

Sounds like https://github.com/chrisgbk/hotpatch / https://mods.factorio.com/mod/hotpatch-multimod. Note that just because I post this, does not mean I recommend using it.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

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

Re: Replace LUA code in a running game from command line?

Post by DaveMcW »

Note that this only works because you are merging every mod into a single control.lua.

Normally control.lua is sandboxed to prevent this from happening.

Plawerth
Fast Inserter
Fast Inserter
Posts: 116
Joined: Thu Mar 02, 2017 12:57 am
Contact:

Re: Replace LUA code in a running game from command line?

Post by Plawerth »

I'm asking in regard to Comfy Factorio, which if you know anything about it, uses scenarios loaded through a heavily modified control.lua that discards massive amounts of normal game functionality, re-implements the core game in bizarre new ways, adds new GUI features, is playable with the vanilla client, and uses no hard mods.

Since all of that is being exclusively implemented via control.lua, then apparently their scenario code should be able to hot-patched to fix bugs in games already in progress.

,

The main question is ... HOW to patch code already in progress.

The specific method for injecting new code is essentially to have admin permissions and then paste in the replacement code using /c at the command line, which then immediately overwrites existing procedures between tick updates.

That's what I'm trying to determine, the exact method or syntax needed to replace an existing function/procedure via the command line.

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

Re: Replace LUA code in a running game from command line?

Post by DaveMcW »

Console is also sandboxed.

The only way to change control.lua from the console is to add a backdoor into the mod.

Hornwitser
Fast Inserter
Fast Inserter
Posts: 205
Joined: Fri Oct 05, 2018 4:34 pm
Contact:

Re: Replace LUA code in a running game from command line?

Post by Hornwitser »

This is not possible without help from code already present in control.lua. Sure you can replace global functions via commands but this doesn't stick across a save/load cycle and that means players who join the multiplayer game after your replaced the function will not get the replaced function and therefore desync from the game.

The hotpatch scenario gets around this by storing the code in strings located in the global table. When you load the game the hotpatch scenario loads all the stored code using the lua load() function and executes it, thus ensuring that the code you add at runtime is also loaded for players that join the map after the code was added. Personally I find the hotpatch scenario difficult to use, poorly documented, and incomplete in implementation.

There might be an alternative way of accomplishing this using game.reload_script(), but I've never managed to get it to work on a dedicated server and the documentation for it is short, vague, and confusing.

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

Re: Replace LUA code in a running game from command line?

Post by eradicator »

Hornwitser wrote:
Tue Feb 25, 2020 3:18 pm
The hotpatch scenario gets around this by storing the code in strings located in the global table.
Thanks for saving me the time of having to dig through the code to confirm my suspicion. No hacking involved after all. Just a mod built to do what the author wanted.
DaveMcW wrote:
Tue Feb 25, 2020 2:26 pm
Console is also sandboxed.

The only way to change control.lua from the console is to add a backdoor into the mod.
Console shares a sandbox with the scenario. Still need the backdoor for that to be useful ofc.
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.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Replace LUA code in a running game from command line?

Post by darkfrei »

Is it says, that I can get the scenario and console commands via serpent?

https://github.com/pkulchenko/serpent
Keeps shared tables and functions shared after de/serialization.
Supports function serialization using string.dump().
Supports serialization of global functions.

Hornwitser
Fast Inserter
Fast Inserter
Posts: 205
Joined: Fri Oct 05, 2018 4:34 pm
Contact:

Re: Replace LUA code in a running game from command line?

Post by Hornwitser »

darkfrei wrote:
Mon Mar 02, 2020 8:15 am
Is it says, that I can get the scenario and console commands via serpent?
No you cannot.

Post Reply

Return to “Modding discussion”