【问题标题】:Trying to use LUAJ in android app, can't find class Lua尝试在 android 应用程序中使用 LUAJ,找不到类 Lua
【发布时间】:2015-06-01 12:26:56
【问题描述】:

我怀疑我的无知比 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 本身添加到 Eclipse 中的外部 Jar 之类的东西?但我没有找到这样做的说明(另外,如果 LUAJ 是 Lua 的完整 java 实现,那么我希望这个接口类也是 LUAJ jar 的一部分。

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

【问题讨论】:

  • 我想我对示例中使用的接口类的阅读可能比我应该阅读的要多。它不是严格需要的。无论如何,我自己制作并解决了编译问题。现在我只是纠结于 LuaClosure 是否真的有 setfenv( LuaTable ) 方法,或者这里的文档 (luaj.sourceforge.net/api/2.0/org/luaj/vm2/…) 说是,但 Eclipse 说没有。我假设的版本差异
  • 这是我受到启发的示例。我可能应该在那里发表评论。
  • 好吧,我缺少的是我查看了许多不同年份的示例和网页,这些都发生了很大变化。一旦我找到了 3.0 版文档树的顶部,事情就开始变得更加确定。经过一些其他的麻烦,我能够从 android 调用 lua,调用特定函数,双向传递值,并让 lua 调用特殊方法来访问游戏状态。重量。

标签: android eclipse luaj


【解决方案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 游戏函数,如

whatever = 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 版开始,这一切都是正确的,只有未来才知道这一切会在以后变得多么误导。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-12
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多