您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx 3.x以上版本 lua中使用protobuf

2015-06-18 16:30 465 查看
参考 :

http://my.oschina.net/ffs/blog/393574

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


这样在lua文件中 require "pb" 即可使用protobuf了

如何根据proto文件生成对应lua文件

1,安装Mercurial-2.9.1.exe,即可使用hg指令

2,打开cmd

3,输入指令hg version,检查安装是否成功

4,输入hg help init,查看帮助

5,输入hg clone https://code.google.com/p/protoc-gen-lua/下载protoc-gen-lua至C:\Users\Administrator目录(将它拷贝到需要存放的位置即可)
6,下载protobuf-2.5.0.tar.gz并解压protobuf-2.5.0

7,cmd进入protobuf-2.5.0\python目录

8,输入python setup.py build编译

9,输入python setup.py install安装

10,进入protobuf-2.5.0\vsprojects,用vs2010打开protobuf.sln,选择protoc为启动项,编译生成protoc.exe

11,将protoc.exe拷贝到protoc-gen-lua\example

12,在protoc-gen-lua\plugin目录下新建protoc-gen-lua.bat文件并输入内容@python "%~dp0protoc-gen-lua"

13,在protoc-gen-lua\example目录下新建build.bat并输入内容如下:

E:\protoc-gen-lua\protoc-gen-lua\example\protoc.exe --lua_out=./ --plugin=protoc-gen-lua="E:\protoc-gen-lua\protoc-gen-lua\plugin\protoc-gen-lua.bat" person.proto

(protoc.exe路径 --lua_out=./ --plugin=protoc-gen-lua.bat路径 proto文件)

14,将proto文件拷贝到protoc-gen-lua\example目录下,运行cmd,进入protoc-gen-lua\example目录,执行build.bat即可生成对应的lua文件(双击也行)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: