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

tolua添加pbc

[复制链接]
发表于 2021-11-11 08:30 | 显示全部楼层 |阅读模式
准备
1.tolua_runtime:https://github.com/topameng/tolua_runtime
2.pbc:https://github.com/cloudwu/pbc
3.msys2:本人使用的事配置好的msys2,https://pan.baidu.com/s/1c2JzvDQ
4.protoc和bat处理生成.pb文件
1.安装msys2
将3解压,拷贝到C盘,将C:\msys64\mingw64\bin或者C:\msys64\mingw32\bin加入到环境变量即可
2.添加pbc
将2全部copy到1的根目录,修改build_win64.sh如下,参考自https://blog.csdn.net/kudoran/article/details/72650594
#!/bin/bash# 64 Bit Versionmkdir -p window/x86_64cd luajit-2.1mingw32-make cleanmingw32-make BUILDMODE=static CC="gcc -m64 -O2" XCFLAGS=-DLUAJIT_ENABLE_GC64cp src/libluajit.a ../window/x86_64/libluajit.amingw32-make cleancd ..cd pbcmingw32-make BUILDMODE=static CC="gcc -m64 -O2" XCFLAGS=-DLUAJIT_ENABLE_GC64cp build/libpbc.a ../window/x86_64/libpbc.amingw32-make cleancd ..gcc -m64 -O2 -std=gnu99 -shared \ tolua.c \ int64.c \ uint64.c \ pb.c \ lpeg.c \ struct.c \ cjson/strbuf.c \ cjson/lua_cjson.c \ cjson/fpconv.c \ luasocket/auxiliar.c \ luasocket/buffer.c \ luasocket/except.c \ luasocket/inet.c \ luasocket/io.c \ luasocket/luasocket.c \ luasocket/mime.c \ luasocket/options.c \ luasocket/select.c \ luasocket/tcp.c \ luasocket/timeout.c \ luasocket/udp.c \ luasocket/wsocket.c \ pbc/binding/lua/pbc-lua.c \ -o Plugins/x86_64/tolua.dll \ -I./ \ -Iluajit-2.1/src \ -Iluasocket \ -Ipbc \ -Ipbc/src \ -lws2_32 \ -Wl,--whole-archive window/x86_64/libluajit.a window/x86_64/libpbc.a -Wl,--no-whole-archive -static-libgcc -static-libstdc++执行build_win64.sh,可以看到tolua_runtime下的window和Plugins有更新,将对应的dll拷贝到Unity对应的目录下替换即可
3.注册和添加
在LuaDLL里添加protobuf_c函数
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]public static extern int luaopen_protobuf_c(IntPtr L);在LuaClient里添加
protected virtual void OpenLibs(){    //luaState.OpenLibs(LuaDLL.luaopen_pb);    luaState.OpenLibs(LuaDLL.luaopen_struct);    luaState.OpenLibs(LuaDLL.luaopen_lpeg);    luaState.OpenLibs(LuaDLL.luaopen_protobuf_c);}在lua文件里require和register .pb文件。注意一定要注册.pb文件
local function LoadPBFile(fileName,mode)        local fullPath = GetProtoFullPath(fileName)        local file,err = io.open(fullPath,mode)        if file == nil then                print(err)                return nil        end        local buff = file:read "*a"        file:close()        return buffendfunction RegisterProtocol.Register()        if PBFileList ~= nil then                for i,v in ipairs(PBFileList) do                        local buff = LoadPBFile(v,"rb")                        if buff ~= nil then                                print(buff)                                GProtobuf.register(buff)                        else                                print("Can't find PBFile, name = "..v)                        end                end        endend4.使用
编码
function ClientSendMsg.SendNetMsg(msgId, msg)        if msg == nil then                ClientSendMsg.SendEmptyMsg(msgId)        else                local body = get_message_by_id(msgId)                if body == nil then print("Get Message Failed, msgId = "..tostring(msgId)) return end                print("ClientSendMsg.SendNetMsg  msgId = "..msgId.."  body = "..body)                GProtobuf.encode(body,msg,ClientSendMsg.SendMsg,msgId)        endend解码
function ClientHandleMsg.Handle(msgId,msgData,msgDataLength)        local handleFunc = HandleFuncs[msgId]        if handleFunc then                local body = get_message_by_id(msgId)                local msg,err = GProtobuf.decode(body,msgData,msgDataLength)                if err ~= nil then                        print(err)                end                handleFunc(msg)        else        print("ClientHandleMsg.Handle->msg handle function not found!msgId="..tostring(msgId))    endend.测试方便,使用编码的数据直接调用解码方法
ClientHandleMsg.Handle(msgId,buffer,bufferLength)32位(X86)的编译
#!/bin/bash# 32 Bit Versionmkdir -p window/x86cd luajit-2.1mingw32-make cleanmingw32-make BUILDMODE=static CC="gcc -m32 -O2"cp src/libluajit.a ../window/x86/libluajit.amingw32-make cleancd ..cd pbcmingw32-make BUILDMODE=static CC="gcc -m32 -O2"cp build/libpbc.a ../window/x86/libpbc.amingw32-make cleancd ..gcc -m32 -O2 -std=gnu99 -shared \        int64.c \        uint64.c \        tolua.c \        pb.c \        lpeg.c \        struct.c \        cjson/strbuf.c \        cjson/lua_cjson.c \        cjson/fpconv.c \        luasocket/auxiliar.c \        luasocket/buffer.c \        luasocket/except.c \        luasocket/inet.c \        luasocket/io.c \        luasocket/luasocket.c \        luasocket/mime.c \        luasocket/options.c \        luasocket/select.c \        luasocket/tcp.c \        luasocket/timeout.c \        luasocket/udp.c \        luasocket/wsocket.c \        pbc/binding/lua/pbc-lua.c \        -o Plugins/x86/tolua.dll \        -I./ \        -Iluajit-2.1/src \        -Icjson \        -Iluasocket \        -Ipbc \        -Ipbc/src \        -lws2_32 \        -Wl,--whole-archive window/x86/libluajit.a window/x86/libpbc.a -Wl,--no-whole-archive -static-libgcc -static-libstdc++注意在编译64位和32位时,注意修改下环境变量中msys64工具的顺序

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-11-24 21:49 , Processed in 0.095024 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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