[ 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 = "[email protected]"
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的過程就結束了!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章