找回密码
 立即注册
查看: 198|回复: 0

XLua添加pbc

[复制链接]
发表于 2023-4-8 22:52 | 显示全部楼层 |阅读模式
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
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-21 10:21 , Processed in 0.089668 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表