cocos2d-x中集成protoc-gen-lua
【版本說明】
python:2.7.5
protobuf:2.5.0
lua:5.1.5
protoc-gen-lua:latest
cocos2d-x:3.2
?
【安裝步驟】
1. python/lua/cocos2d-x環境默認已安裝
安裝protobuf
進入protobuf-2.5.0\python,執行:
| 1 2 3 | python setup.py build python setup.py test python setup.py install |
2.?protoc-gen-lua解壓放置到某個目錄下,假定為c:\
protoc-gen-lua\plugin下創建protoc-gen-lua.bat,文件內容為:
| 1 | python c:\protoc-gen-lua\plugin\protoc-gen-lua |
3. 通過proto生成lua,命令為:
| ? |
|
【集成步驟】
1. 加入pb.c文件,在frameworks\cocos2d-x\external\lua下創建protobuf目錄,copy pb.c文件至此
win32下編譯出錯,修改pb.c文件如下:
| 1 2 3 | #ifndef _WIN32? //添加 #include <endian.h> #endif //添加 |
?
2. protobuf目錄下加入pb.h文件:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #ifndef __LUA_PROTOBUF_H_ #define __LUA_PROTOBUF_H_ ?? #include "lua.h" ?? #if __cplusplus extern?"C"?{ #endif ????int?luaopen_pb(lua_State?*L); #if __cplusplus } #endif ?? #endif |
?
3.?修改lua_extensions.c文件:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #if __cplusplus extern?"C"?{ #endif //?socket #include "luasocket/luasocket.h" #include "luasocket/mime.h" #include "lpack/lpack.h" #include "cjson/lua_cjson.h" #include "protobuf/pb.h" //添加 ?? static luaL_Reg luax_exts[]?=?{ ????{"cjson", luaopen_cjson_safe}, ????{"pack", luaopen_pack}, ????{"pb", luaopen_pb},??//添加 ????{"socket.core", luaopen_socket_core}, ????{"mime.core", luaopen_mime_core}, ????{NULL, NULL} }; |
4.?將protoc-gen-lua\protobuf下的lua文件放置到項目的搜索目錄中
?
【重要說明】
protoc-gen-lua這個坑爹的玩意兒不支持嵌套message,如果嵌套的message在同一個proto里面是可以的,但是如果在不同的文件里面,通過import的方式引用就有問題了。
?
可以通過修改生成lua的python腳本解決這個問題,方法如下:
1. 首先定義的被引用的proto文件頭要加上package,如:
?
| 1 2 3 4 5 | package pb; message BaseMsg { ... } |
2.在引用的proto文件中修改為這樣:
| 1 2 3 4 5 6 | import?"Base.proto" message PlayerMsg { ????optional pb.BaseMsg msg?=?1; ????... } |
3.修改protoc-gen-lua\plugin\protoc-gen-lua文件:
| 1 2 3 | if?field_desc.HasField('type_name'): ???????type_name?=?env.get_ref_name(field_desc.type_name).upper()?//這一行 ???????if?field_desc.type?==?FDP.TYPE_MESSAGE: |
| 1 2 3 4 5 6 7 | def?code_gen_message(message_descriptor, env, containing_type?=?None): ????env.enter(message_descriptor.name) ????full_name?=?env.get_local_name() ????obj_name?=?full_name.upper().replace('.',?'_') ????env.descriptor.append( ????????"%s = protobuf.Descriptor();\n"%?obj_name??//這一行去掉local ????) |
| 1 2 3 4 5 6 7 | if?is_gen: ???????lua?=?Writer() ???????write_header(lua) ???????lua('local protobuf = require "protobuf"\n') ???????for?i?in?includes: ???????????lua('local %s_PB = require("%s_pb")\n'?%?(i.upper(), i))??//這一行PB大寫 ???????lua("module('%s_pb')\n"?%?env.filename) |
然后就可以了。
?
【轉載】
lua下使用protobuf
開源中國
發表于 2014-09-12 19:43:01下載地址: protobuf:https://code.google.com/p/protobuf/downloads/list下載最新的protobuf 我這里下載的是2.5.0 protoc-gen-lua:https://github.com/sean-lin/protoc-gen-lua下載master分支 Linux下安裝: protobuf: 解壓后到目錄
./configure && make && sudo make install 一般protoc就會安裝到/usr/local/bin下 安裝python支持(Linux下可選,裝也沒啥問題) cd python python setup.py build python setup.py install protoc-gen-lua: 解壓后到目錄 cd protobuf make 這里可能會報個lua5.1的error, 生成pb.so就OK, 無視ERROR了, 使用時請將pb.so加入package.cpath中, 也可以放在安裝后lua的lib下,我的是在/usr/local/lib/lua/5.1/ 那直接將pb.so放到該目錄, 以后項目也不用特別新加一句package.cpath=...了 cd /usr/local/bin && sudo ln -s /YOURPATH/protoc-gen-lua/plugin/protoc-gen-lua 這樣就能使用命令 protoc --lua_out=./ foo.proto 生成lua的protobuf支持文件了 到protoc-gen-lua的example目錄可以試試 protoc --lua_out=./ person.proto 這時候查看目錄應該生成了一個person_pb.lua文件, 運行下測試代碼 lua test.lua 查看輸出 Windows下: protobuf: 解壓后到目錄 到目錄vsprojects下打開protobuf.sln項目,編譯,會在vsprojects/Debug或者vsprojects/Release生成protoc.exe執行文件, 放到某個目錄加入到PATH環境變量中... 到protobuf目錄下的python目錄,和linux下一樣安裝, 請確認你的電腦安裝了python,并將python執行文件所在目錄加入到了PATH;python建議安裝2.X最新系列 windows下安裝protobuf的python支持是必須的 tips:不選中任何文件的情況下,在目錄的空白位置shift+右鍵會出現"在此處打開命令行窗口",然后你懂的 重點是protoc-gen-lua了: github下面下載出來的文件格式是linux下的utf-8, 可能得轉成PC下的GB...主要針對protoc-gen-luaprotobufpb.c windows下將pb.c幾個位置改一下 #include <endian.h> 改成 #ifndef WIN32 #include <endian.h> #else #pragma comment(lib,"lua5.1.lib") #endif static int struct_unpack(lua_State *L){ 函數下的 buffer += pos;uint8_t out[8]; 要改成 uint8_t out[8];buffer += pos; 函數 int luaopen_pb(lua_State *L) 改成 __declspec(dllexport) int luaopen_pb(lua_State *L) OK,改造完畢, VS新建項目->win32項目->下一步->應用程序類型:點選dll, 附加符號:先"導出符號", 后"空項目"->完成 將pb.c加入到項目中, 生成解決方案 到目錄下會發現生成了YOURNAME.dll 將之重命名為pb.dll 如果要做到項目中都可以使用的話可以放到lua安裝目錄的clib下,不然自己加到package.cpath中 到protoc-gen-lua的plugin目錄 建立兩個腳本文件, 注意改成你自己的目錄, Win下做開發就少放在含有中文的目錄下了 1. build.bat @python E:/protobuf/protoc-gen-lua/plugin/protoc-gen-lua 2.make_proto.bat for %%i in (*.proto) do (protoc --lua_out=./ --plugin=protoc-gen-lua="E:/protobuf/protoc-gen-lua/plugin/build.bat"%%i ) OK, 以后就只要將這個make_proto.bat放到proto所在的目錄運行下就能得到.lua的文件了 可以用example下的例子做測試 PS:關于pb.so pb.dll名稱主要是protoc-gen-lua里面有個protobuf.lua的函數有個require"pb" 轉載請標明出處:http://ziplee.iteye.com/blog/2078851?@HAPPY CODINE@?
轉載于:https://www.cnblogs.com/wkccs/p/4131033.html
總結
以上是生活随笔為你收集整理的cocos2d-x中集成protoc-gen-lua的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从中国到沙特阿拉伯有多远?
- 下一篇: 坦克300透明隐形盖加强筋要放到哪个位置