protobuf-gen-lua 不能支持int64 的解决方法

博客讲述了在使用protobuf-gen-lua时遇到int64序列化错误的问题,原因是protobuf-gen-lua默认将int64当作int32处理,导致数据大小范围错误。解决方案是通过GitHub网友zhenmu提供的修改,增加了INT64的数据大小判断,以确保正确序列化。文章提及了相关资源链接和作者的教程推荐。

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

               

protobuf-gen-lua 不能支持int64,序列化的时候数据出错,少了一个字节。很大概率出现。


不知道是 Lua5.1.4的问题还是 protobuf-gen-lua 的问题。

后面有时间来升级SLua到 Lua5.3再测试


前天到 Github 上搜索,发现网友 zhenmu 在 issues 下面提到了这个问题,而且提出了解决方案供我尝试,在我测试无效之后,又提供了完整的修改,实在是非常感谢!

转自http://blog.youkuaiyun.com/huutu http://www.thisisgame.com.cn

Github原址:

https://github.com/sean-lin/protoc-gen-lua/issues/23


因为我对Lua不是很熟悉,所以只能大致的说出来缘由:

1、Protobuf-lua-gen 默认是不支持 int64 以及 UINT64,并不是不能支持,而是作者说他们当时候不需要 int64和UINT64,所以没有加上 。

2、Protobuf-lua-gen 默认对int64是当作int32来处理,UINT64是当作UINT32来处理的。

转自http://blog.youkuaiyun.com/huutu http://www.thisisgame.com.cn

3、由于INT64被当成了int处理,所以数据的大小范围序列化出来的数据Size出错。


所以Github 网友提供的修改方案就是添加 INT64 的数据大小判断,使Protobuf 序列化的时候生成正确的数据。


以下来自 Github 网友 zhenmu 的解决方法 ( https://github.com/sean-lin/protoc-gen-lua/issues/23 ):


1.encoder.lua

    function _VarintSize(value)    if value <= 0x7f then return 1 end    if value <= 0x3fff then return 2 end    if value <= 0x1fffff then return 3 end    if value <= 0xfffffff then return 4 end    return 5    end    function _SignedVarintSize(value)    if value < 0 then return 10 end    if value <= 0x7f then return 1 end    if value <= 0x3fff then return 2 end    if value <= 0x1fffff then return 3 end    if value <= 0xfffffff then return 4 end    return 5    end
改成

    function _VarintSize(value)    if value <= 0x7f then return 1 end    if value <= 0x3fff then return 2 end    if value <= 0x1fffff then return 3 end    if value <= 0xfffffff then return 4 end    if value <= 0x7ffffffff then return 5 end    if value <= 0x3ffffffffff then return 6 end    if value <= 0x1ffffffffffff then return 7 end    if value <= 0xffffffffffffff then return 8 end    if value <= 0x7fffffffffffffff then return 9 end    return 10    end    function _SignedVarintSize(value)    if value < 0 then return 10 end    if value <= 0x7f then return 1 end    if value <= 0x3fff then return 2 end    if value <= 0x1fffff then return 3 end    if value <= 0xfffffff then return 4 end    if value <= 0x7ffffffff then return 5 end    if value <= 0x3ffffffffff then return 6 end    if value <= 0x1ffffffffffff then return 7 end    if value <= 0xffffffffffffff then return 8 end    if value <= 0x7fffffffffffffff then return 9 end    return 10    end


2.ware_format里 (非必要?)

local function _VarUInt64ByteSizeNoTag(uint64)if uint64 <= 0x7f then return 1 endif uint64 <= 0x3fff then return 2 endif uint64 <= 0x1fffff then return 3 endif uint64 <= 0xfffffff then return 4 endreturn 5end

改成

local function _VarUInt64ByteSizeNoTag(uint64)if uint64 <= 0x7f then return 1 endif uint64 <= 0x3fff then return 2 endif uint64 <= 0x1fffff then return 3 endif uint64 <= 0xfffffff then return 4 endif uint64 <= 0x7ffffffff then return 5 endif uint64 <= 0x3ffffffffff then return 6 endif uint64 <= 0x1ffffffffffff then return 7 endif uint64 <= 0xffffffffffffff then return 8 endif uint64 <= 0x7fffffffffffffff then return 9 endreturn 10end

type_checkers.lua里 (这个范围是我们自己根据一些情况 设定了一个范围)增加

function Int64ValueChecker()local _MIN = -562949953421312local _MAX = 562949953421312return function(proposed_value)if type(proposed_value) ~= 'number' thenerror(string.format('%s has type %s, but expected one of: number',proposed_value, type(proposed_value)))endif _MIN > proposed_value or proposed_value > _MAX thenerror('Value out of range: ' .. proposed_value)endendendfunction Uint64ValueChecker(IntValueChecker)local _MIN = 0local _MAX = 1125899906842624 

我在项目里设置这里为上限和下限。

转自http://blog.youkuaiyun.com/huutu http://www.thisisgame.com.cn

4.protobuf.lua 里

[FieldDescriptor.CPPTYPE_INT64] = type_checkers.Int32ValueChecker(),[FieldDescriptor.CPPTYPE_UINT64] = type_checkers.Uint32ValueChecker(),

改成

[FieldDescriptor.CPPTYPE_INT64] = type_checkers.Int64ValueChecker(),[FieldDescriptor.CPPTYPE_UINT64] = type_checkers.Uint64ValueChecker(),


也可以下载我修改好的文件替换

下载地址:

http://pan.baidu.com/s/1eRyWJdC




           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值