参考链接:
https://blog.csdn.net/asasj57/article/details/108162925
https://blog.csdn.net/asasj57/article/details/108169282
lua-protobuf:
https://github.com/starwing/lua-protobuf
protobuf语法:
https://blog.csdn.net/qq_22660775/article/details/89163881
protobuf中的import与import public:
https://blog.csdn.net/fighting_mjtao/article/details/93179694
在这篇的基础上继续集成:
https://www.cnblogs.com/lyh916/p/15709042.html
查看dll,确保相关的方法已经在dll中
将项目工程\build\lua-protobuf\protoc.lua复制到项目的lua目录下
1.运行lua-protobuf官方的例子:
https://github.com/starwing/lua-protobuf/blob/master/README.zh.md
function Main.TestPB() local pb = require "pb" local protoc = require "protoc" -- 直接载入schema (这么写只是方便, 生产环境推荐使用 protoc.new() 接口) assert(protoc:load [[ message Phone { optional string name = 1; optional int64 phonenumber = 2; } message Person { optional string name = 1; optional int32 age = 2; optional string address = 3; repeated Phone contacts = 4; } ]]) -- lua 表数据 local data = { name = "ilse", age = 18, contacts = { { name = "alice", phonenumber = 12312341234 }, { name = "bob", phonenumber = 45645674567 } } } -- 将Lua表编码为二进制数据 local bytes = assert(pb.encode("Person", data)) print(pb.tohex(bytes)) -- 再解码回Lua表 local data2 = assert(pb.decode("Person", bytes)) print(table.dump(data2)) end
输出如下:
2.使用.proto文件改写上面的例子1
Phone.proto
syntax = "proto3"; message Phone { string name = 1; int64 phonenumber = 2; }
Person.proto
syntax = "proto3"; import "Phone.proto"; message Person { string name = 1; int32 age = 2; string address = 3; repeated Phone contacts = 4; }
Main.lua
function Main.LoadProto(fileName) local path = CS.UnityEngine.Application.dataPath .. "/LuaScript/Proto/" .. fileName return CS.System.IO.File.ReadAllText(path) end function Main.TestPB2() local pb = require "pb" local protoc = require "protoc" local pc = protoc.new() pc:load(Main.LoadProto("Phone.proto"), "Phone.proto") pc:load(Main.LoadProto("Person.proto"), "Person.proto") -- lua 表数据 local data = { name = "ilse", age = 18, contacts = { { name = "alice", phonenumber = 12312341234 }, { name = "bob", phonenumber = 45645674567 } } } -- 将Lua表编码为二进制数据 local bytes = assert(pb.encode("Person", data)) print(pb.tohex(bytes)) -- 再解码回Lua表 local data2 = assert(pb.decode("Person", bytes)) print(table.dump(data2)) end
输出如下:
3.使用.pb文件改写上面的例子2
下载protoc:https://github.com/protocolbuffers/protobuf/releases?page=1
cd到protoc.exe的目录,将.proto文件也放在该目录下,输入protoc.exe -o xx.pb xx.proto即可生成.pb文件,如下:
生成的Phone.pb如下:
Main.lua
function Main.LoadProtoBytes(fileName) local path = CS.UnityEngine.Application.dataPath .. "/LuaScript/Pb/" .. fileName return CS.System.IO.File.ReadAllBytes(path) end function Main.TestPB3() local pb = require "pb" pb.load(Main.LoadProtoBytes("Phone.pb"), "Phone.pb") pb.load(Main.LoadProtoBytes("Person.pb"), "Person.pb") -- lua 表数据 local data = { name = "ilse", age = 18, contacts = { { name = "alice", phonenumber = 12312341234 }, { name = "bob", phonenumber = 45645674567 } } } -- 将Lua表编码为二进制数据 local bytes = assert(pb.encode("Person", data)) print(pb.tohex(bytes)) -- 再解码回Lua表 local data2 = assert(pb.decode("Person", bytes)) print(table.dump(data2)) end
注意上面使用的是pb.load
protoc:load传入的是字符串,然后内部转换为byte[],再调用pb.load
pb.load传入的是byte[]