cocos3.9 【 protoc-gen-lua 配置 】

前端之家收集整理的这篇文章主要介绍了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
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 ,就一行代码

  1. @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文件

  1. @echo off
  2.  
  3. setlocal enabledelayedexpansion
  4.  
  5. rem 创建文件
  6. set tpath=%~dp0..\..\src\app\Protobuf
  7. if not exist %tpath% (
  8. md %tpath%
  9. )
  10.  
  11. rem protolua下的所有的.proto文件转换成.lua文件
  12. echo 开始转化.proto文件
  13. echo,set index=0
  14. for %%i in (*.proto) do (
  15. set /a index=index+1
  16. echo 第!index!个文件为%%i
  17. protoc.exe --plugin=protoc-gen-lua="..\plugin\protoc-gen-lua.bat" --lua_out=..\..\src\app\Protobuf %%i
  18. )
  19.  
  20. echo,echo 转换完成!
  21. echo,setlocal disabledelayedexpansion
  22.  
  23. 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的引用。

  1. #include "lua_extensions.h"
  2.  
  3. #if __cplusplus
  4. extern "C" {
  5. #endif
  6. // socket
  7. #include "protobuf/pb.c"
  8. #include "luasocket/luasocket.h"
  9. #include "luasocket/luasocket_scripts.h"
  10. #include "luasocket/mime.h"
  11.  
  12. static luaL_Reg luax_exts[] = {
  13. {"socket.core",luaopen_socket_core},{"mime.core",luaopen_mime_core},{NULL,NULL}
  14. };
  15.  
  16. void luaopen_lua_extensions(lua_State *L)
  17. {
  18. // load extensions
  19. luaL_Reg* lib = luax_exts;
  20. lua_getglobal(L,"package");
  21. lua_getfield(L,-1,"preload");
  22. for (; lib->func; lib++)
  23. {
  24. lua_pushcfunction(L,lib->func);
  25. lua_setfield(L,-2,lib->name);
  26. }
  27. lua_pop(L,2);
  28. luaopen_luasocket_scripts(L);
  29. luaopen_pb(L);
  30. }
  31.  
  32. #if __cplusplus
  33. } // extern "C"
  34. #endif

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

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

  1. #ifndef _WIN32
  2. #include <endian.h>
  3. #endif
避免在windos下缺失文件报错

2、调整struct_unpack函数前几行为

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

五、调用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,代码如下:

  1. require "person_pb"
  2.  
  3. local person= person_pb.Person()
  4. person.id = 1000
  5. person.name = "Alice"
  6. person.email = "Alice@example.com"
  7. local home = person.Extensions[person_pb.Phone.phones]:add()
  8. home.num = "2147483647"
  9. home.type = person_pb.Phone.HOME
  10. local data = person:SerializeToString()
  11. local msg = person_pb.Person()
  12. msg:ParseFromString(data)
  13.  
  14. local pLab=cc.Label:createWithSystemFont(msg.email,"Arial",40)
  15. pLab:setAnchorPoint(0.5,0.5)
  16. pLab:setPosition(display.cx,display.cy-200)
  17. self:addChild(pLab)

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

猜你在找的Cocos2d-x相关文章