【轉】 cocos2dx 3.x C++搭建protobuf環境
http://blog.csdn.net/ganpengjin1/article/details/50964961
Cocos2dx 裏面在網絡遊戲通信這一塊一般我們都會采用protobuf來進行通信,cocos引擎沒有集成C++的protobuf,那我們只能自己來集成了。因為protobuf有很多版本,那麽我們怎麽去下載與引擎中想對應的protobuf版本呢。
他在
[cpp] view plain copy- cocos2d-x\tools\simulator\libsimulator\lib\protobuf-lite
在這裏目錄下面有個
config.h文件,在第一行就可以看到當前版本,那我們就去http://code.google.com/p/protobuf/downloads/list
下載版本。
第二,下載下來後,我們添加這個protobuf工程到你的項目工程中
第三,右擊你的項目,添加該protobuf為依賴項目,把cocos2d-x\tools\simulator\libsimulator\lib\protobuf-lite,這下面的google下面的protobuf文件夾全部刪掉,替換成你解壓後的src下面的google中的protobuf。config.h文件也替換成你的vsprojects下的config.h文件
第四,開始編譯--》應該是沒有問題的
第五,我們要開始生成我們的.proto文件了,怎麽弄呢?還記得先前下載下來的protobuf文件嗎?解壓後,裏面有個vsprojects 文件夾,protobuf.sln 用Vs打開這個工程編譯protobuf工程,編譯完之後,再編譯test工程,這個時候會生成protoc.exe文件,這個文件就是我們所需要的。
第六,我們開始編寫我們的proto文件了,寫一個.bat文件,內容:
[cpp] view plain copy
- protoc.exe MsgProtocol.proto --cpp_out=./
- pause
其中:***.proto為文件名, cpp為生成的C++文件,還可以生成其他:java, python等,但是暫時不支持lua的,out後面是路徑,跟你的.proto文件是同一路徑
.proto文件中的內容可以直接定義message,例如:
[cpp] view plain copy
- message Person {
- required int32 id = 1;
- required string name = 2;
- optional string emial = 3;
- optional string text = 4;
- enum MsgType {
- MsgType_None = 0;
- MsgType_ONE = 1;
- MsgType_TWO = 2;
- MsgType_THREE = 3;
- MsgType_FOUR = 4;
- MsgType_FIVE = 5;
- }
- enum MsgStatus {
- MsgStatus_NONE = 0;
- MsgStatus_ONE = 1;
- MsgStatus_TWO = 2;
- }
- optional MsgType msg = 5;
- optional MsgStatus status = 6;
- }
第七,寫完bat,proto後,把.bat, protoc.exe, .proto文件復制到你項目目錄下面(其實,任意位置都可以,只要這三個文件保持在同一目錄就可以),運行.bat文件後,會成成.pb.cc,.pb.h文件,這個就是你生成的文件,把這兩個文件復制到你的項目中,你就可以隨意使用了。
以上是C++集成protobuf。。
下面來講一下Lua集成Protobuf,先去下載pbc:https://github.com/cloudwu/pbc
把這個項目下來,添加到你的項目工程中,然後編譯一下。應該是編譯沒有問題的。接下來講重要的部分:
第一:右擊的項目工程,把pbc添加為依賴項
第二:在pbc工程裏面創建一個頭文件"pbc-lua.h"(最好放在工程的pbc目錄下),內容如下:
[cpp] view plain copy
- #ifndef PBC_LUA_H
- #define PBC_LUA_H
- //
- // pbc-lua.h
- // pbc
- //
- // Created by ArcherPeng on 15/7/21.
- // Copyright (c) 2015年 ztgame. All rights reserved.
- //
- #if defined(_USRDLL)
- #define LUA_EXTENSIONS_DLL __declspec(dllexport)
- #else /* use a DLL library */
- #define LUA_EXTENSIONS_DLL
- #endif
- #if __cplusplus
- extern "C" {
- #endif
- #include "lauxlib.h"
- int LUA_EXTENSIONS_DLL luaopen_protobuf_c(lua_State *L);
- #if __cplusplus
- }
- #endif
- #endif/* pbc_pbc_lua_h */
第三:把binding/lua/pbc-lua.c (也放在工程的pbc目錄下),代碼如下:
[cpp] view plain copy
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include "lua.h"
- #include "lualib.h"
- #include "lauxlib.h"
- #include "pbc.h"
- #include "pbc-lua.h"//引入剛剛創建的頭文件
- #ifdef __cplusplus
- }
- #endif
第四:
首先要修改pbc工程的頭文件搜索路徑:
加入一條路徑:項目路徑/frameworks/cocos2d-x/external/lua/lua (這裏最好使用相對路徑,但絕對路徑也可以)
第五:
添加完成後,回到AppDelegate.cpp中,引入頭文件#include"pbc/pbc/pbc-lua.h",並在boolAppDelegate::applicationDidFinishLaunching()方法中加入luaopen_protobuf_c(L);用於將protobuf的函數註冊進LUA,代碼如下:
[cpp] view plain copy
- bool AppDelegate::applicationDidFinishLaunching()
- {
- // set default FPS
- Director::getInstance()->setAnimationInterval(1.0 / 60.0f);
- // register lua module
- auto engine = LuaEngine::getInstance();
- ScriptEngineManager::getInstance()->setScriptEngine(engine);
- lua_State* L = engine->getLuaStack()->getLuaState();
- lua_module_register(L);
- luaopen_protobuf_c(L);//在lua中註冊Proto函數
- register_all_packages();
- LuaStack* stack = engine->getLuaStack();
- stack->setXXTEAKeyAndSign("2dxLua", strlen("2dxLua"), "XXTEA", strlen("XXTEA"));
- if (engine->executeScriptFile("src/main.lua"))
- {
- return false;
- }
- return true;
- }
第六:接下來我們要再為LUA註冊一個函數用於讀取pb文件數據:
[cpp] view plain copy- static int readProtobufFile(lua_State *L)
- {
- const char *buff = luaL_checkstring(L, -1);
- Data data = CCFileUtils::getInstance()->getDataFromFile(buff);
- lua_pushlstring(L, (const char*)data.getBytes(), data.getSize());
- return 1; /* number of results */
- }
[cpp] view plain copy
- // If you want to use packages manager to install more packages,
- // don‘t modify or remove this function
- static int register_all_packages()
- {
- lua_State *L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
- luaopen_protobuf_c(L);
- lua_register(L,"readProtobufFile",readProtobufFile);
- return 0; //flag for packages manager
- }
第七:編譯並運行,編譯通過後,遊戲運行成功!
第八:
至此,C++端設置完畢。但是並沒有結束,還有最後幾步。
回到遊戲項目的binding/lua目錄,找到"protobuf.lua"文件,將其復制到lua項目的src目錄下。
第九:
在LUA中Protobuf的簡單例程:
require "app/protobuf.lua" //導入你的protobuf.lua文件
[cpp] view plain copy
- local pbFilePath = cc.FileUtils:getInstance():fullPathForFilename("app/MsgProtocol.pb")
- release_print("PB file path: "..pbFilePath)
- local buffer = readProtobufFile(pbFilePath)
- protobuf.register(buffer) --註:protobuf 是因為在protobuf.lua裏面使用module(protobuf)來修改全局名字
- local stringbuffer = protobuf.encode("Person",
- {
- name = "Alice",
- id = 12345,
- phone = {
- {
- number = "87654321"
- },
- }
- })
- local slen = string.len(stringbuffer)
- local temp = ""
- for i=1, slen do
- temp = temp .. string.format("0xX, ", string.byte(stringbuffer, i))
- end
- release_print(temp)
- local result = protobuf.decode("Person", stringbuffer)
- release_print("result name: "..result.name)
- release_print("result name: "..result.id)
MsgProtocol.proto的內容:
[cpp] view plain copy
- message Person {
- required string name = 1;
- required int32 id = 2;
- optional string email = 3;
- enum PhoneType {
- MOBILE = 0;
- HOME = 1;
- WORK = 2;
- }
- message PhoneNumber {
- required string number = 1;
- optional PhoneType type = 2 [default = HOME];
- }
- repeated PhoneNumber phone = 4;
- }
- message AddressBook {
- repeated Person person = 1;
- }
- protoc.exe --descriptor_set_out=./MsgProtocol.pb ./MsgProtocol.proto
最後添加到android.mk裏面,蛋疼啊。。。 其實很簡單的,只需要添加你的文件目錄即可,用*.cpp或者*.c來搜索,這樣實在是太方便了,省了很多事情,不再逐個逐個的添加(參考:http://blog.csdn.net/fu_zk/article/details/12836431)
[cpp] view plain copy
- LOCAL_PATH := $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_MODULE := cocos2dlua_shared
- LOCAL_MODULE_FILENAME := libcocos2dlua
- FILE_LIST := hellolua/main.cpp
- FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/*.cpp)
- FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/lua/*.cpp)
- FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/socket/*.cpp)
- FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/hotUpdate/*.cpp)
- FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/ide-support/*.cpp)
- FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/ide-support/*.c)
- #添加
- FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/pbc/binding/lua/*.c)
- FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/pbc/src/*.c)
- LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
- #你的工程目錄
- LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
- #你的工程目錄其他自文件夾目錄
- LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../Classes/pbc
- LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../Classes/pbc/src
- # _COCOS_HEADER_ANDROID_BEGIN
- # _COCOS_HEADER_ANDROID_END
- LOCAL_STATIC_LIBRARIES := cocos2d_lua_static
- LOCAL_STATIC_LIBRARIES += cocos2d_simulator_static
- # _COCOS_LIB_ANDROID_BEGIN
- # _COCOS_LIB_ANDROID_END
- include $(BUILD_SHARED_LIBRARY)
- $(call import-module,scripting/lua-bindings/proj.android)
- $(call import-module,tools/simulator/libsimulator/proj.android)
- # _COCOS_LIB_IMPORT_ANDROID_BEGIN
- # _COCOS_LIB_IMPORT_ANDROID_END
【轉】 cocos2dx 3.x C++搭建protobuf環境