tags:

views:

53

answers:

2

Many games these days make available some lua scripting, but this is universally undocumented.

So let's say I can get a game to run my lua script (it's lua 5.1) - and the script can write what it finds to text files on disk. How much can I discover about environment the script is executing in?

For example it seems I can list keys in tables, and find out what's a function and what's some other type of object, but there's no obvious way to guess how many arguments function takes (and a mistake usually results in crash to desktop).

Most languages provide some reflection functionality that could be used here - how much is possible in embedded lua environment?

+1  A: 

Unfortunately, there is not much you can learn about functions in Lua - they by design accept any number of parameters. Without the ability to look at the sources, your only resort is the documentation and/or other samples.

The most you can do in this case is traverse the entire _G table recursively and dump every table/function, printing the results to a file.

"A mistake usually results in crash to desktop" is a sign of a really bad design - good API should tell you, that it expects A, and you passed B. For example in Lqt, a Qt binding to Lua, we check every parameter against the original Qt API, so that the programmer is notified of mistakes:

> QApplication.setFont(1, 2)
QApplication::setFont(number, number): incorrect or extra arguments, expecting: QFont*,string,.
MiKy
Well, game developers stopped documenting things for modders as soon as they figured out how to make money off DLC and Steam. Times of cooperation are over, and now API is only what we can figure it out to be, and that involved developing a few patches for luadec already. The future looks bleak, but in the meantime thanks for your help.
taw
+1  A: 

"debug" standard library has some functions, which you may find useful:

  • debug.getfenv - Returns the environment of object.
  • debug.getinfo - Returns a table with information about a function.
  • ... and more

Lua Reference Manual also states:

several of these functions violate some assumptions about Lua code (e.g., that variables local to a function cannot be accessed from outside or that userdata metatables cannot be changed by Lua code) and therefore can compromise otherwise secure code.

So with debug library, you may access more.

Olzvoi