使用LUA 热更新模块

最近准备在手机项目客户端中使用lua, 以前一直在服务器使用luabind. 另外, tolua++也体验过, LuaPlus也在早年用过. 以下是本人对这些绑定库的个人感觉:

luabind

利用boost机制把绑定做到极致, 比较适合主c++, 弱lua的脚本框架.

作者已经停止更新, 在windows/linux编译没问题, 但是在ios的LLVM下, 无法编译

tolua++

像cocos2dx使用tolua++也是可以理解的, 那么多函数需要绑定, tolua++的头文件parse及自动代码生成节约了很多手动绑定的时间.

但是看到代码中有一部分bugfix就心存不安(纯个人感觉, 本人使用不多, 欢迎砖头伺候),另外, tolua++只能由脚本层驱动C++, 而没有将已经实例化的句柄注册到lua的功能也是煞笔啊

 

LuaPlus

接口较为简单, 适于初学者上手, 无任何的模板, 性能不高

 

luaBridge

项目地址: https://github.com/vinniefalco/LuaBridge

手册: http://vinniefalco.com/LuaBridge/Manual.html

纯头文件实现, 无需编译, 包含进入工程即可, 接口简洁高效

相比luabind, 唯一不能实现的常用功能就是枚举, 但是可以支持类成员静态变量注册, 这个就无所谓了, 手写一个枚举支持也很简单

看下演示代码:

<span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">class</span> A
{
<span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">public</span>:
    A( )
    {

    }
    <span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">virtual</span> <span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">void</span> foo( <span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">int</span> a )
    {
        printf(<span class="str" style="line-height: 15.6px; color: rgb(0, 96, 128);">"foo base\n"</span>);
    }

    std::<span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">string</span> Member;
};

<span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">class</span> B : <span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">public</span> A
{
<span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">public</span>:
    <span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">virtual</span> <span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">void</span> foo( <span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">int</span> a )
    {
        printf(<span class="str" style="line-height: 15.6px; color: rgb(0, 96, 128);">"foo inherited\n"</span>);
    }
};
<span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">void</span> foo( <span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">int</span> b )
{

}

luabridge::getGlobalNamespace(L)
        .beginClass<A>(<span class="str" style="line-height: 15.6px; color: rgb(0, 96, 128);">"Sobj"</span>)
            .addConstructor<<span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">void</span> (*) (<span class="kwrd" style="line-height: 15.6px; color: rgb(0, 0, 255);">void</span>)> ()
            .addFunction(<span class="str" style="line-height: 15.6px; color: rgb(0, 96, 128);">"foo"</span>, &A::foo)
            .addData(<span class="str" style="line-height: 15.6px; color: rgb(0, 96, 128);">"Member"</span>,&A::Member)
        .endClass()
        .deriveClass<B, A>(<span class="str" style="line-height: 15.6px; color: rgb(0, 96, 128);">"SSec"</span>)
            .addFunction(<span class="str" style="line-height: 15.6px; color: rgb(0, 96, 128);">"foo"</span>,&B::foo )
        .endClass();

    luabridge::getGlobalNamespace(L).addFunction(<span class="str" style="line-height: 15.6px; color: rgb(0, 96, 128);">"foo"</span>, foo );


    B ins;
    ins.Member = <span class="str" style="line-height: 15.6px; color: rgb(0, 96, 128);">"data"</span>;
    luabridge::setGlobal(L, ins, <span class="str" style="line-height: 15.6px; color: rgb(0, 96, 128);">"ins"</span>);

lua侧的代码

local a = Sobj()
a:foo(2)
a.Member = <span class="str" style="line-height: 15.6px; color: rgb(0, 96, 128);">"hello"</span>
ins:foo(3)
</pre><pre class="csharpcode" name="code" style="white-space: pre-wrap; word-wrap: break-word; font-size: 13px; font-family: consolas, 'Courier New', courier, monospace; line-height: 16.9px; background-color: rgb(255, 255, 255);">
应用在COCOS2DX中的部分测试部分:
<span style="color: rgb(51, 51, 51); font-family: verdana, sans-serif; line-height: 19px;">直接解压,把LuaBridge.h,RefCountedObject.h,RefCountedPtr.h和detail下的所有头文件,全部复制到classes目录,然后在AppDelegate包含luabrigde.h就可以了。</span><br style="color: rgb(51, 51, 51); font-family: verdana, sans-serif; line-height: 19px;" /><span style="color: rgb(51, 51, 51); font-family: verdana, sans-serif; line-height: 19px;">下面是一个用于测试的类</span><br style="color: rgb(51, 51, 51); font-family: verdana, sans-serif; line-height: 19px;" /><span style="font-family: verdana, sans-serif; line-height: 19px; color: rgb(0, 0, 255);">class</span><span style="font-family: verdana, sans-serif; line-height: 19px; color: rgb(51, 51, 51);"> testA</span><span style="font-family: verdana, sans-serif; line-height: 19px; color: rgb(51, 51, 51);"></span><span style="font-family: Arial; font-size: 14px; line-height: 26px;"></span><div style="font-family: verdana, sans-serif; line-height: 19px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 1615.03px; word-break: break-all; color: rgb(51, 51, 51);">{
<span style="color: rgb(0, 0, 255);">public</span>:
    testA()
    {

    }
    <span style="color: rgb(0, 0, 255);">int</span> getValue()
    {
        <span style="color: rgb(0, 0, 255);">return</span> 100;
    }
};</div><span style="font-family: verdana, sans-serif; line-height: 19px; color: rgb(51, 51, 51);">下面是绑定代码</span><br style="font-family: verdana, sans-serif; line-height: 19px; color: rgb(51, 51, 51);" /><div style="font-family: verdana, sans-serif; line-height: 19px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 1615.03px; word-break: break-all; color: rgb(51, 51, 51);">    luabridge::getGlobalNamespace(pEngine->getLuaStack()->getLuaState())
        .beginClass<testA>("testA")
        .addConstructor<<span style="color: rgb(0, 0, 255);">void</span>(*) ()>()
        .addFunction("getValue", &testA::getValue)
        .endClass();</div><span style="font-family: verdana, sans-serif; line-height: 19px; color: rgb(51, 51, 51);">然后,就可以在你的lua代码使用这个类了。</span><br style="font-family: verdana, sans-serif; line-height: 19px; color: rgb(51, 51, 51);" /><div style="font-family: verdana, sans-serif; line-height: 19px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 1615.03px; word-break: break-all; color: rgb(51, 51, 51);">    local testa = testA ()
    cclog("testa=%d", testa:getValue()) </div><span style="font-family: verdana, sans-serif; line-height: 19px; color: rgb(51, 51, 51);">现在cocos2dx上面,现在出现了两个重要的lua分支, 一个是quickx,一个cocos-code-ide。</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值