How to find out what is available in lua?

Many games these days provide some lua scripts, but this is publicly undocumented.

So let me say that I can run the game to run lua script (this is lua 5.1) - and the script can write what finds text files on disk. How much can I learn about the environment in which the script is running?

For example, it seems that I can list the keys in tables and find out what a function is and what other type of object, but there is no obvious way to guess how many arguments the function takes (and an error usually leads to a desktop crash).

Most languages โ€‹โ€‹have some reflection functions that can be used here - how much is this possible in the lua embedded environment?

+2
source share
2 answers

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

The most you can do in this case is to recursively translate the entire table _G and unload each table / function, print the results to a file.

โ€œAn error usually leads to a desktop crashโ€ is a sign of a very poor design - a good API should tell you that it expects A, and you passed B. For example, in Lqt , binding Qt to Lua, we check each parameter for source Qt API so that the programmer is notified of errors:

> QApplication.setFont(1, 2) QApplication::setFont(number, number): incorrect or extra arguments, expecting: QFont*,string,. 
+1
source

The "debug" standard library has some features that may be useful:

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

The Lua reference manual also states:

some of these functions violate some assumptions about the Lua code (for example, that variables local to the function cannot be accessed externally or the metadata of user data cannot be changed using Lua code) and therefore may jeopardize otherwise protected code.

So, with the debug library, you can access more.

+1
source

All Articles