0
\$\begingroup\$

Does a game have to have built-in functions to accept and run lua scripts, or can I design Events and Hooks using Lua on any game I please, akin to the days where C code could be used to hook into the WinAPI using dlls?

The reason I ask is, I am trying to create a background application that will perform events and hooks on a particular game that does not currently support lua in-game.

Brief examples:

Events: - An action executed by the PLAYER is detected. For instance, hitting the Q key will normally make my character use an ability, but with my Lua script running in the background, will cause a sound to play on my computer (or something).

Hooks: - An action within the GAME is detected. For instance, the game spawns an enemy every minute. When an enemy spawns, the script will detect this and perform an action, for instance playing a sound locally on the computer.

I would like to do both, but I know for games like Garry's Mod, the game already has built-in support for running lua scripts.

Is there a way to do either events OR hooks using lua similarly to how C/C++ can connect to a game using WinAPI dlls?

\$\endgroup\$
  • 1
    \$\begingroup\$ It might be possible, but I doubt it'd be very easy, and would require hacking the EXE of the game. \$\endgroup\$ – thedaian Jan 20 '12 at 16:05
3
\$\begingroup\$

Lua is not magic. Lua cannot magically associate itself with any application and interfere in any function call.

You can't even do that from C. Yes, with Win32 DLL injection, you can interpose a DLL within a DLL boundary. But if there is no DLL boundary to inject your code... you can't do anything. And I'm guessing most games don't expose the "spawn entity" function across a DLL boundary.

In general, most games put the main game code either in a DLL by itself or within the executable itself. The graphics system, sound system, etc, may be DLLs. But even if the game code is a DLL, internal game processes (create entity, manage entity, etc, stuff you want to hook to) would all be internal within that DLL. And therefore not injectable.

Oh, you can do this (not with Lua directly). But you would have to decompile the application, reverse-engineer the flow of functions and such, and manually edit the compiled binaries to add your external hook functions.

\$\endgroup\$
  • \$\begingroup\$ As LUA gets compiled by the developers, they decide what LUA can do and what not. The game I want to get into does not allow e.g. to create a network socket. Do you know if it could be possible to inject a LUA function into the game in order to get more control? I want to be able to create a socket such that I am able to communicate over network in a later stage. \$\endgroup\$ – displayname Nov 30 '16 at 19:58
  • \$\begingroup\$ @displayname: First, that's a new question. We ask new questions with the "Ask A Question" button. Second, Lua is a proper name, not an acronym. It shouldn't be in ALL CAPS. Third... no. You cannot force a game to use Lua unless you hack it. Not unless the game is moddable, and even then, it would have to be moddable by DLLs or other compiled code rather than a scripting language. \$\endgroup\$ – Nicol Bolas Nov 30 '16 at 22:43
  • \$\begingroup\$ Yes, I am quite aware about how stackexchange works - nevertheless I can spare the question if somebody (like you) I assume might know it it's feasible at all answers with a clear no. Maybe I stated my question wrong: The game is already using Lua but, as I said, a custom built. The question is if it is possible to get past those introduced restriction e.g. not being able to create sockets etc. One answer could be: "No, this is all in a VM and all addresses are getting magically randomized and you'll not even be able to hack or reverse engineer it.". But something tells me there is a way \$\endgroup\$ – displayname Dec 1 '16 at 9:59
  • \$\begingroup\$ @displayname: And by "sparing the question", you make it infinitely harder for someone else to find the answer to that question. So it helps you, and nobody else. \$\endgroup\$ – Nicol Bolas Dec 1 '16 at 15:48
  • \$\begingroup\$ There you go: gamedev.stackexchange.com/questions/133773/hacking-lua-built \$\endgroup\$ – displayname Dec 1 '16 at 17:23
0
\$\begingroup\$

I did a similar thing with Just Cause 2 using the WinAPI, I used cheat engine to find out where the players position was stored in memory and networked that to my iphone to display a 'GPS'.

You can see the code here: https://github.com/perky/JC2-GPS

I don't know about accessing memory from Lua but cheatengine seems to use Lua, it's worth taking a look: http://www.cheatengine.org/

\$\endgroup\$
  • \$\begingroup\$ As LUA gets compiled by the developers, they decide what LUA can do and what not. The game I want to get into does not allow e.g. to create a network socket. Do you know if it could be possible to inject a LUA function into the game in order to get more control? I want to be able to create a socket such that I am able to communicate over network in a later stage. \$\endgroup\$ – displayname Nov 30 '16 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.