franciscochonge 发表于 2021-8-14 07:38

xLua集成lua-protobuf

参考资料

xLua官方:https://github.com/Tencent/xLua
xLua集成第三方库官方教程:https://github.com/chexiongsheng/build_xlua_with_libs
lua-protobuf:https://github.com/starwing/lua-protobuf
win环境要求

cmake(需要配置好环境变量)
C/C++编译环境(若vs没有安装好环境,可以在build64目录下,双击Xlua.sln,会提示安装)
第三方扩展的源码,build_xlua_with_libs内已经放好了ffi,lpeg,rapidjson,lua-protobuf,pbc
集成过程

build_xlua_with_libs内的CMakeLists已经配置好了ffi,lpeg,rapidjson,lua-protobuf。且支持pdc。
1,版本同步

为了和xLua版本同步,建议先把https://github.com/Tencent/xLua/tree/master/build下的除CMakeLists.txt之外的文件目录,覆盖到https://github.com/chexiongsheng/build_xlua_with_libs/tree/master/build目录。
2,批处理

使用Windows机器,可以编译Win平台和Android平台的库。
window平台库编译(需要C/C++环境配置好)
双击make_win64_lua53.bat,生成build64目录
用VS2017运行Xlua.sln并生成
macos平台编译
在macos系统上,chmod 777 make_osx_lua53.sh
然后执行 ./make_osx_lua53.sh
ios平台编译
在macos系统上,chmod 777 make_ios_lua53.sh
然后执行 ./make_ios_lua53.sh
android平台编译
在macos系统上,下载ndk,地址如下:http://dl.google.com/android/ndk/android-ndk-r10e-darwin-x86_64.bin
解压bin
编辑make_android_lua53.sh ,修改ANDROID_NDK目录为解压的ndk目录
chmod 777 make_android_lua53.sh
./make_android_lua53.sh
这里以win平台下说明。前提是配置好C/C++环境。
这里稍微修改了make_win64_lua53.bat为下:
mkdir build64 & pushd build64
cmake -G “Visual Studio 15 2017 Win64” …
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
执行上述批处理命令后,在拷贝目标目录plugin_lua53\Plugins\x86_64中的xlua.dll,替换Unity工程中Plugins/x86_64目录中的xlua.dll
安卓平台库编译,参考:
https://blog.csdn.net/yudianxia/article/details/81738699
https://blog.csdn.net/codingriver/article/details/83271053
3,Unity3D侧,XLUA增加对lua-protobuf的支持

1)LuaDLL.cs 大概40行,增加
      //增加lua-protobuf支持
      
      public static extern int luaopen_pb(System.IntPtr L);

      
      public static int LoadLuaProfobuf(System.IntPtr L)
      {
            return luaopen_pb(L);
      }
      //增加rapidjson支持
      //
      //public static extern int luaopen_rapidjson(System.IntPtr L);

      //
      //public static int LoadRapidJson(System.IntPtr L)
      //{
      //    return luaopen_rapidjson(L);
      //}
      //增加lpeg支持
      //
      //public static extern int luaopen_lpeg(IntPtr luaState);

      //
      //public static int LoadLpeg(System.IntPtr L)
      //{
      //    return luaopen_lpeg(L);
      //}
      //增加ffi支持
      //
      //public static extern int luaopen_ffi(System.IntPtr L);

      //
      //public static int LoadFFI(System.IntPtr L)
      //{
      //    return luaopen_ffi(L);
      //}2)LuaEnv.cs大概120行,在AddBuildin(“CS”,StaticLuaCallbacks.LoadCS);这句前增加:
AddBuildin("pb", XLua.LuaDLL.Lua.LoadLuaProfobuf);
            //AddBuildin("rapidjson", XLua.LuaDLL.Lua.LoadRapidJson);
            //AddBuildin("lpeg", LuaDLL.Lua.LoadLpeg);            
            //AddBuildin("ffi", XLua.LuaDLL.Lua.LoadFFI);由于项目中其它扩展没有用到,上面代码注释掉。
另外对于上述1),2)部分,也可以不按照上述方式配置,可以把LuaDLL.CS中增加的扩展内容,单独放到一个类中。在luaEnv初始化的时候,调用。
形如:
LuaEnv luaenv = new LuaEnv();
luaenv.AddBuildin("rapidjson", XLua.LuaDLL.Lua.LoadRapidJson);
luaenv.AddBuildin("lpeg", XLua.LuaDLL.Lua.LoadLpeg);
luaenv.AddBuildin("pb", XLua.LuaDLL.Lua.LoadLuaProfobuf);
luaenv.AddBuildin("ffi", XLua.LuaDLL.Lua.LoadFFI);4,lua侧集成

local pb=require “pb”
local protoc=require “protoc”
protoc:load(proto.Schema)–协议内容注册到lua端,proto.Schema结构形如:[]
–编码, msg为lua侧 符合协议结构的table;返回值为userdata
local data=pb.encode(msgName,msg)
–解码, data为pb编码的userdata
local msg=pb.decode(msgName,data)
页: [1]
查看完整版本: xLua集成lua-protobuf