1

我怀疑我的无知比 LUAJ 更深刻,所以请温柔一点。

目标:在我的应用程序中定期运行 lua 脚本,在 Lua 和 Java 之间来回传递数据,具有一定的安全性(例如,不要让 lua 删除文件)

我的大部分应用程序都是直接的 android/java,并且工作正常。在这种情况下,我不是一个白痴。

通过各种示例,我最终将 LUAJ Jar 文件作为外部 Jar 放入 Eclipse。之后,这些进口工作

import org.luaj.vm2.Lua;
import org.luaj.vm2.LuaClosure;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Prototype;
import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
import org.luaj.vm2.lib.jse.JsePlatform;

并且大部分代码主要是编译,但我仍然没有在此行中使用的“Lua”接口:

public class MyLuaEngine  implements Lua

该行因“由于 Lua 不是超类而无法实现”而失败。我很确定它不知道 Lua 是什么(除非它在其他命名空间或其他东西中找到同源)。

此外,我对 Lua:add() 等的覆盖也抱怨(他们没有超级可以覆盖)例如:

@Override
public void run(int id, EventArgs args) throws LuaException
{
    LuaClosure script = scripts.get(id);

(必须覆盖超类型)

我假设我需要为 Lua 本身添加一个外部 Jar 之类的东西?,但我没有找到这样做的说明(另外,如果 LUAJ 是 Lua 的完整 java 实现,那么我希望这个接口类也成为 LUAJ jar 的一部分。

在我找到的三个 LUAJ jar 中,我只告诉了 Eclipse 其中一个(JSE,而不是 JME 或 Source)。

4

1 回答 1

1

所以,这对我有用。在 android java 应用程序中使用 Luaj 3.0 和 eclipse。

1.) 从其下载页面下载 Luaj zip 并确保将 jse jar 放入某个已知位置 (luaj-jse-3.0.jar)

2.) 告诉 Eclipse 将其添加为外部 Jar(右键单击项目,buildPath/configureBuildPath/Libraries/Add External Jar

3.)添加这些进口

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.TwoArgFunction;
import org.luaj.vm2.lib.jse.JsePlatform;

(以及其他需要的,但从那些类路径。Eclipse ctrl-shift-O 只要你有 jar 文件就可以计算出来)

4.) 一些示例用法(luaj 将打印文本发送到 android logcat)

void testLua()
{
    //-----
    // Simple Test Lua Script

    String myScript = "require 'com.Your.Class.Path.Here.GameFunctions' \n" 
                    + " \n"
                    + "print 'hello, world from lua' \n" 
                    + "print( game.testFunction( 5 ) ) \n"
                    + " \n"
                    + "function foo() \n"
                    + "  print( 'You have been fooed!' ) \n"
                    + "end \n"
                    + " \n"
                    + "function foo2( a, b ) \n"
                    + "  print( 'Foo2 got '.. a ..', and '.. b ..' !' ) \n"
                    + "end \n"
                    + " \n"
                    + "function foo3( a, b ) \n"
                    + "  print( 'Foo3 got '.. a ..', and '.. b ..' !' ) \n"
                    + "  return 'fantastic!' \n"
                    + "end \n"
                    + " \n"
                    + "print 'Good bye from my lua program' \n"
                    + ""
                    ;
     // Create the globals object and initialize with standard stuff
    Globals globals = JsePlatform.standardGlobals();

     // Load (and compile?) the simple script and get a Chunk
    LuaValue chunk = globals.load( myScript );

     // run the script once from the beginning (root level)
    chunk.call();

     // run it a second time, just to see if it blows up, and
     // get the return value, if any. (didnt blow up)
    LuaValue result = chunk.call();

    // try to call a specific function, no args
    LuaValue foo = globals.get( "foo" );    // get the function to call as a LuaValue
    if( !foo.isnil() ) {
        foo.call();   // runs just that function
    }

    LuaValue foo2 = globals.get( "foo2" );  // pass two args
    if( !foo2.isnil() ) {
        foo2.call(  LuaValue.valueOf("first"), 
                    LuaValue.valueOf("second") );
    }

    LuaValue foo3 = globals.get( "foo3" );  // pass 2 args, get 1 back
    if( !foo3.isnil() ) {
        LuaValue retVal = foo3.call(    LuaValue.valueOf("first"), 
                                        LuaValue.valueOf("second") );
        Log.v( TAG, "LUA: foo3 returned: " + retVal.toString() );
    }

}

lua 脚本的第一行是一个 require 命令,它调用我称为 GameFunctions 的公共类的完整类路径。它是 lua 可以回调到感兴趣的自定义 java 函数的接口(可能是获取玩家的分数。播放声音效果等)

我的 Bare Bones 实现看起来像这样:(非常感谢所有为此做出贡献的网页,尽管最终我只是猜测 2 arg 调用将具有 modName 和 env)

当您 .load() 脚本字符串时,它会被编译并且返回 LuaValue (chunk) 是编译后的代码。但是您不能在块上使用 .get("functionName") ,您必须改用“全局”对象(这是有道理的,但我觉得即时编译有点让我头晕目眩,我觉得发生在,load() 但我想留下某种符号表供全局变量稍后使用。

无论如何,所以这个类在 .call() 命令执行脚本并到达“require”的那一刻被实例化。在调用无操作创建者之后,Luaj 使用 modName 和 env 调用 2 arg 方法。然后,您可以将任何您想要的东西粘贴到环境中。在这种情况下,我们构建了一个 LuaTable 函数,然后将其粘贴到名为“game”的环境中,然后从 lua 中我们可以调用 Java 游戏函数,例如

随便= game.doSomething(一些,垃圾)

尽管您可能希望将所有 lua 放在一个单独的线程中,这样您的 UI 就不会停止。我希望 Lua 调试钩子存在于某个地方,这样我就可以限制播放器提供的无限循环的执行时间 :-)

public class GameFunctions extends TwoArgFunction {
private static final String TAG = GameFunctions.class.getSimpleName();

public GameFunctions() {
}

public LuaValue call( LuaValue modname, LuaValue env ) 
{
    Log.v( TAG, "LUA: modName: " + modname.toString() 
            + ", Env: " + env.toString() );

    LuaTable game = new LuaTable(); 

    // the actual functions get added to game table
    game.set("testFunction", new testFunction());

    // we set it into the environment so lua can see them
    env.set("game", game);

    // we mark it so no 'require' is needed 
    env.get("package").get("loaded").set("game", game);

    return game;
}

// An example Game Function... not a very good one.  Pretend
// it plays a sound effect.  I just wanted to test arg passing
// don't forget to convert to LuaValues as needed.

class testFunction extends OneArgFunction {
    public LuaValue call(LuaValue x) {
        return LuaValue.valueOf( "you said: " + x );
    }
}

}

理论上,最后的位应该使脚本中不需要包含 require 命令。我希望这是真的,但我并不介意我的应用程序中的要求

无论如何,我希望有一天这对其他人有所帮助。从 2015 年 3 月的 luaj 3.0 版开始,这一切都是真的,只有未来才知道这一切会变得多么误导。

于 2015-03-29T06:19:33.550 回答