cocos2dx3.4 lua整合protobuf

网上看了一些资料但还是遇到不少问题。在这里记录一下

下载地址:

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

1.  复制文件 protoc-gen-lua/protobuf/pb.c  到 frameworks\cocos2d-x\external\lua\protobuf\pb.c 

2. 修改   frameworks\cocos2d-x\cocos\scripting\lua-bindings\manual\network\lua_extensions.c 文件


#include "lua_extensions.h"

#if __cplusplus
extern "C" {
#endif
// socket
#include "protobuf/pb.c"
#include "luasocket/luasocket.h"
#include "luasocket/luasocket_scripts.h"
#include "luasocket/mime.h"
static luaL_Reg luax_exts[] = {
    {"socket.core", luaopen_socket_core},
    {"mime.core", luaopen_mime_core},
    {NULL, NULL}
};

void luaopen_lua_extensions(lua_State *L)
{
    // load extensions
    luaL_Reg* lib = luax_exts;
    lua_getglobal(L, "package");
    lua_getfield(L, -1, "preload");
    for (; lib->func; lib++)
    {
        lua_pushcfunction(L, lib->func);
        lua_setfield(L, -2, lib->name);
    }
    lua_pop(L, 2);

    luaopen_luasocket_scripts(L);
	luaopen_pb(L);   
}

#if __cplusplus
} // extern "C"
#endif




3.重新编译工程C库。 测试  require "pb"应该OK了

4.复制  protoc-gen-lua/protobuf/*.lua 所有文件 到工程里面。src\network\protobuf\src

5. 运行测试lua 脚本  应该没问题了。

在第3步编译工程中报 uint_t错误。需要修改下pb.c文件


static int struct_unpack(lua_State *L)
{
    uint8_t format = luaL_checkinteger(L, 1);
    size_t len;
    
    size_t pos = luaL_checkinteger(L, 3);
	const uint8_t* buffer = (uint8_t*)luaL_checklstring(L, 2, &len) + pos;



另外如果因为LUA module 出错,需要用全局变量来设定。

测试代码

function MainScene:testdata()
    --local sp = require("player")
    --dump(sp[1])
 
    local person= person_pb.Person()
    person.id = 1000
    person.name = "Alice"
    person.email = "Alice@example.com"

    local home = person.Extensions[person_pb.Phone.phones]:add()
    home.num = "2147483647"
    home.type = person_pb.Phone.HOME

    local data = person:SerializeToString()

    local msg = person_pb.Person()

    msg:ParseFromString(data)
    print("parser:", msg.id, msg.name, msg.email, data) 
    print(msg)

end




转载于:https://my.oschina.net/ffs/blog/393574

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值