【游戏(手游,页游,端游等)开发】C#序列化proto后生成bytes文件,在lua层解析成proto
function Process_RunBattle:GetProto(path)
local result = read_bytes(path)
--字节流转换成字符串
local str = string.char(unpack(result))
--protobuf解析字符串
local proto = NetStruct_BattleCommand_pb.BattleCommand()
proto:ParseFromString(str)
return proto
end
--输出path,返回字节流table
function read_bytes(path)
local file = io.open(path, "rb")
if not file then
return nil
end
local t = {}
repeat
local str = file:read(4 * 1024)
for c in (str or ''):gmatch('.') do
if c:byte() == 126 then
t = {}
else
table.insert(t, c:byte())
end
end
until not str
file:close()
return t
end
发表回复