乐趣区

关于游戏开发:游戏开发-lua读取bytes文件并解析proto

【游戏(手游,页游,端游等)开发】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
退出移动版