[ protobuf lua ] cocos2dx-lua 3.9配置protoc-gen-lua

在网上看到很多protoc-gen-lua的资料, 很多都不怎么全面,整理下希望对大家有帮助

环境:vs2013 + cocos3.9 + python2.7.10 + protobuf-2.5.0 + protoc-gen-lua

下载地址:

protobuf-2.5.0:http://download.csdn.net/detail/sunqiqi2121/9452124
protoc-gen-lua:http://download.csdn.net/detail/sunqiqi2121/9452099

一、安装 cocos3.9 和 python27

二、配置 protobuf

解压 protoc-gen-lua 和 protobuf-2.5.0 中的 protobuf-2.5.0.tar 和 protoc-2.5.0-win32 , 并把 protoc-2.5.0-win32 下的 protoc.exe 拷贝到 protobuf-2.5.0\src 下,如果不放,后面无法安装 python 版的 protobuf。在 protobuf-2.5.0\python 下运行 python setup.py build 和 python setup.py install ,如果上面没有放置 protoc.exe ,会提示错误,找不到google\protobuf\compiler目录。

三、准备批处理 protobuf

我的项目目录为:D:\SGZZ,把 protoc-gen-lua 拷贝到项目目录下,在 protoc-gen-lua\plugin 下编写批处理 protoc-gen-lua.bat ,就一行代码:

@python %~dp0protoc-gen-lua

在 protoc-gen-lua 下新建一个 protolua 文件夹,把 protoc-2.5.0-win32 下的 protoc.exe 拷贝到 protolua 下,并把 protoc-gen-lua\example\person.proto 拷贝到 protolua 下。在 protolua 下添加 build_proto.bat 文件,用来批处理转化.proto文件,点击build_proto.bat就可以在src\app\Protobuf下查看到所有编译之后的_pb.lua文件。

@echo off

setlocal enabledelayedexpansion

rem 创建文件夹
set tpath=%~dp0..\..\src\app\Protobuf
if not exist %tpath% (
md %tpath%
)

rem 将protolua下的所有的.proto文件转换成.lua文件
echo 开始转化.proto文件
echo,

set index=0
for %%i in (*.proto) do (
set /a index=index+1
echo 第!index!个文件为%%i
protoc.exe --plugin=protoc-gen-lua="..\plugin\protoc-gen-lua.bat" --lua_out=..\..\src\app\Protobuf %%i
)

echo,
echo 转换完成!
echo,

setlocal disabledelayedexpansion

pause

四、配置pb.c文件

把protoc-gen-lua\protobuf\pb.c拷贝到工程目录下的frameworks\cocos2d-x\external\lua\protobuf下,如果在lua下没有protobuf文件夹,就新建一个。修改frameworks\cocos2d-x\cocos\scripting\lua-bindings\manual\network\lua_extensions.c,增加pb.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

在window平台下要对pb.c做如下的修改,再重新编译。

1、将#include <endian.h>修改为

#ifndef _WIN32
#include <endian.h>
#endif

避免在windos下缺失文件报错

 

2、调整struct_unpack函数前几行为

{
    uint8_t format = luaL_checkinteger(L, 1);
    size_t len;
    const uint8_t* buffer = (uint8_t*)luaL_checklstring(L, 2, &len);
    size_t pos = luaL_checkinteger(L, 3);
    uint8_t out[8];   
    buffer += pos;
}

 

五、调用lua文件

拷贝protoc-gen-lua\protobuf下的所有.lua文件到src\app\Protobuf下,在新编译出来的person_pb.lua文件中会有一个require "protobuf",所以需要添加查找目录,cc.FileUtils:getInstance():addSearchPath("src/app/Protobuf")。新建一个TestPanel.lua文件调用person_pb.lua,代码如下:

require "person_pb"

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)

local pLab=cc.Label:createWithSystemFont(msg.email, "Arial", 40)
pLab:setAnchorPoint(0.5,0.5)
pLab:setPosition(display.cx, display.cy-200)
self:addChild(pLab)

在protoc-gen-lua\example\test.lua中也有关于person_pb.lua的调用,可以详细查看下。到此,整个protoc-gen-lua的过程就结束了!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值