tolua添加pbc

本文详细介绍了如何在Unity环境下集成Lua与protobuf_c,包括下载必要的组件、配置环境变量、编译tolua_runtime和pbc,以及在LuaDLL和LuaClient中添加protobuf_c函数。此外,还提供了编码和解码的示例代码,以及如何在32位和64位环境下进行编译的指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备

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.youkuaiyun.com/kudoran/article/details/72650594

#!/bin/bash
# 64 Bit Version
mkdir -p window/x86_64

cd luajit-2.1
mingw32-make clean

mingw32-make BUILDMODE=static CC="gcc -m64 -O2" XCFLAGS=-DLUAJIT_ENABLE_GC64
cp src/libluajit.a ../window/x86_64/libluajit.a
mingw32-make clean

cd ..

cd pbc
mingw32-make BUILDMODE=static CC="gcc -m64 -O2" XCFLAGS=-DLUAJIT_ENABLE_GC64
cp build/libpbc.a ../window/x86_64/libpbc.a
mingw32-make clean

cd ..

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 buff
end

function 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
	end
end

4.使用

编码

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)
	end
end

解码

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))
    end
end

.测试方便,使用编码的数据直接调用解码方法

ClientHandleMsg.Handle(msgId,buffer,bufferLength)

32位(X86)的编译

#!/bin/bash
# 32 Bit Version
mkdir -p window/x86

cd luajit-2.1
mingw32-make clean

mingw32-make BUILDMODE=static CC="gcc -m32 -O2"
cp src/libluajit.a ../window/x86/libluajit.a
mingw32-make clean

cd ..

cd pbc
mingw32-make BUILDMODE=static CC="gcc -m32 -O2"
cp build/libpbc.a ../window/x86/libpbc.a
mingw32-make clean
cd ..

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工具的顺序

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值