cocos3.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 一、安装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.0src 下,如果不放,后面无法安装 python 版的 protobuf。在protobuf-2.5.0python 下运行 python setup.py build 和 python setup.py install ,如果上面没有放置protoc.exe ,会提示错误,找不到googleprotobufcompiler目录。 三、准备批处理 protobuf 我的项目目录为:D:SGZZ,把protoc-gen-lua 拷贝到项目目录下,在 protoc-gen-luaplugin 下编写批处理protoc-gen-lua.bat ,就一行代码: @python %~dp0protoc-gen-lua 在 protoc-gen-lua 下新建一个 protolua 文件夹,把 protoc-2.5.0-win32 下的protoc.exe 拷贝到 protolua 下,并把protoc-gen-luaexampleperson.proto 拷贝到protolua 下。在 protolua 下添加build_proto.bat 文件,用来批处理转化.proto文件,点击build_proto.bat就可以在srcappProtobuf下查看到所有编译之后的_pb.lua文件。 @echo off setlocal enabledelayedexpansion rem 创建文件夹 set tpath=%~dp0....srcappProtobuf 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="..pluginprotoc-gen-lua.bat" --lua_out=....srcappProtobuf %%i ) echo,echo 转换完成! echo,setlocal disabledelayedexpansion pause 四、配置pb.c文件 把protoc-gen-luaprotobufpb.c拷贝到工程目录下的frameworkscocos2d-xexternalluaprotobuf下,如果在lua下没有protobuf文件夹,就新建一个。修改frameworkscocos2d-xcocosscriptinglua-bindingsmanualnetworklua_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-luaprotobuf下的所有.lua文件到srcappProtobuf下,在新编译出来的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-luaexampletest.lua中也有关于person_pb.lua的调用,可以详细查看下。到此,整个protoc-gen-lua的过程就结束了! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |