XLua添加pbc

1.下载pbc

2.在xLua\build目录下创建pbc文件夹(文件名自定义).

3.pbc文件夹下pbc.h

pbc\src文件夹下所有文件

pbc\binding\lua53文件夹下pbc-lua53.c(这个看lua的版本对应一个就行)

所有的文件拷贝到xLua\build\pbc文件夹下。

4.编辑xLua\build\CMakeLists.txt

添加

#begin lua-protobuf
# 添加所有的c或者cpp文件
set (PROTOBUF_SRC
 lua-protobuf/alloc.c
 lua-protobuf/array.c
 lua-protobuf/bootstrap.c
 lua-protobuf/context.c
 lua-protobuf/decode.c
 lua-protobuf/map.c
 lua-protobuf/pbc-lua53.c
 lua-protobuf/proto.c
 lua-protobuf/register.c
 lua-protobuf/rmessage.c
 lua-protobuf/stringpool.c
 lua-protobuf/varint.c
 lua-protobuf/wmessage.c
 lua-protobuf/pattern.c
)
set_property(
 SOURCE ${PROTOBUF_SRC}
 APPEND
 PROPERTY COMPILE_DEFINITIONS
 LUA_LIB
)
# 头文件目录
list(APPEND THIRDPART_INC  lua-protobuf)
set (THIRDPART_SRC ${THIRDPART_SRC} ${PROTOBUF_SRC})
#end lua-protobuf

5.运行xLua\build\make_win64_lua53.bat(根据你需要的版本运行.bat文件)

make_win64_lua53.bat 文件内容

mkdir build64 & pushd build64
cmake -G "Visual Studio 15 2017 Win64" .. #对应的vs版本
popd
cmake --build build64 --config Release
md plugin_lua53\Plugins\x86_64
copy /Y build64\Release\xlua.dll plugin_lua53\Plugins\x86_64\xlua.dll
pause

如果编译错误

pbc.h 文件添加

#ifndef bool
#define bool char
#endif

#ifndef true
#define true 1
#endif

#ifndef false
#define false 0
#endif

pbc-lua53.c 修改871行

修改前 int luaopen_protobuf_c(lua_State *L)

修改后 __declspec(dllexport) int luaopen_protobuf_c(lua_State *L)

6.把编译好的xlua.dll(根据运行的.bat文件找到对应的生成文件)拷贝到unity项目Assets\Plugins下

7.C#添加LuaDLL文件(如果有就添加DLL入口函数)

LuaDLL.cs 内容

using System.Runtime.InteropServices;
using XLua;

namespace XFW.LuaDLL
{
    // 添加第三方库
    public partial class Lua
    {
        const string LUADLL = "xlua";

        [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
       //对应pbc-lua53.c文件__declspec(dllexport) int luaopen_protobuf_c(lua_State *L)函数

        public static extern int luaopen_protobuf_c(System.IntPtr L);

        [MonoPInvokeCallback(typeof(XLua.LuaDLL.lua_CSFunction))]
        public static int LoadProtobuf(System.IntPtr L)
        {
            return luaopen_protobuf_c(L);
        }
    }
}

 

在LuaEnv构造后调用_luaEnv.AddBuildin("protobuf.c", LuaDLL.Lua.LoadProtobuf);

"protobuf.c"是lua require的名字可以自定义

 

lua测试

1.把pbc\binding\lua53\protobuf.lua拷贝到你lua项目里 如果修改过"protobuf.c" 需要修改此文件require的名字。

local protobuf = require "protobuf"

protobuf.register_file(CS.UnityEngine.Application.dataPath .."/Lua/pb/person.pb") 这是.proto

local str = protobuf.encode("test.protocol.sgp.Person", data)
local person = protobuf.decode("test.protocol.sgp.Person", str)

 

 

 

 

 

转载于:https://my.oschina.net/u/3744374/blog/1586307

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值