如何发现lua环境中可用的内容? [英] How to discover what is available in lua environment?
问题描述
如今,许多游戏都提供了一些lua脚本,但这普遍没有记录.
Many games these days make available some lua scripting, but this is universally undocumented.
因此,假设我可以让一个游戏运行我的lua脚本(它是lua 5.1)-该脚本可以将找到的内容写到磁盘上的文本文件中.我可以从中发现多少有关脚本执行的环境的信息?
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).
大多数语言都提供了一些可以在此处使用的反射功能-嵌入式lua环境中可以实现多少功能?
Most languages provide some reflection functionality that could be used here - how much is possible in embedded lua environment?
推荐答案
不幸的是,您对Lua中的函数了解不多-它们在设计上可以接受任意数量的参数.无法查看源代码,您唯一的选择就是文档和/或其他示例.
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.
在这种情况下,您能做的就是递归地遍历整个_G表并转储每个表/函数,然后将结果打印到文件中.
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.
错误通常会导致桌面崩溃"是一个非常糟糕的设计的标志-好的API应该告诉您,它期望A,并且您通过了B.例如,在
"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,.
这篇关于如何发现lua环境中可用的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!