cocos2dx 3.x C++搭建protobuf环境

前端之家收集整理的这篇文章主要介绍了cocos2dx 3.x C++搭建protobuf环境前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Cocos2dx 里面在网络游戏通信这一块一般我们都会采用protobuf来进行通信,cocos引擎没有集成C++的protobuf,那我们只能自己来集成了。因为protobuf有很多版本,那么我们怎么去下载与引擎中想对应的protobuf版本呢。

他在

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

  1. protoc.exe MsgProtocol.proto --cpp_out=./
  2. pause

其中:***.proto为文件名,cpp为生成的C++文件,还可以生成其他:java,python等,但是暂时不支持lua的,out后面是路径,跟你的.proto文件是同一路径

.proto文件中的内容可以直接定义message,例如:

  1. message Person {
  2. required int32 id = 1;
  3. required string name = 2;
  4. optional string emial = 3;
  5. optional string text = 4;
  6. enum MsgType {
  7. MsgType_None = 0;
  8. MsgType_ONE = 1;
  9. MsgType_TWO = 2;
  10. MsgType_THREE = 3;
  11. MsgType_FOUR = 4;
  12. MsgType_FIVE = 5;
  13. }
  14. enum MsgStatus {
  15. MsgStatus_NONE = 0;
  16. MsgStatus_ONE = 1;
  17. MsgStatus_TWO = 2;
  18. }
  19.  
  20. optional MsgType msg = 5;
  21. optional MsgStatus status = 6;
  22. }

第七,写完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目录下),内容如下:

  1. #ifndef PBC_LUA_H
  2. #define PBC_LUA_H
  3. //
  4.  
  5. // pbc-lua.h
  6.  
  7. // pbc
  8.  
  9. //
  10.  
  11. // Created by ArcherPeng on 15/7/21.
  12.  
  13. // Copyright (c) 2015年 ztgame. All rights reserved.
  14.  
  15. //
  16.  
  17.  
  18.  
  19.  
  20. #if defined(_USRDLL)
  21.  
  22. #define LUA_EXTENSIONS_DLL __declspec(dllexport)
  23.  
  24. #else /* use a DLL library */
  25.  
  26. #define LUA_EXTENSIONS_DLL
  27.  
  28. #endif
  29.  
  30.  
  31.  
  32. #if __cplusplus
  33.  
  34. extern "C" {
  35.  
  36. #endif
  37. #include "lauxlib.h"
  38.  
  39.  
  40.  
  41. int LUA_EXTENSIONS_DLL luaopen_protobuf_c(lua_State *L);
  42.  
  43.  
  44.  
  45. #if __cplusplus
  46.  
  47. }
  48.  
  49. #endif
  50.  
  51.  
  52.  
  53. #endif/* pbc_pbc_lua_h */

第三:把 binding/lua/pbc-lua.c (也放在工程的pbc目录下),代码如下:

  1. #ifdef __cplusplus
  2.  
  3. extern "C" {
  4.  
  5. #endif
  6.  
  7. #include "lua.h"
  8.  
  9. #include "lualib.h"
  10.  
  11. #include "lauxlib.h"
  12.  
  13.  
  14. #include "pbc.h"
  15.  
  16. #include "pbc-lua.h"//引入刚刚创建的头文件
  17.  
  18.  
  19. #ifdef __cplusplus
  20.  
  21. }
  22.  
  23. #endif

第四:

首先要修改pbc工程的头文件搜索路径:

加入一条路径:项目路径/frameworks/cocos2d-x/external/lua/lua (这里最好使用相对路径,但绝对路径也可以)


第五:

添加完成后,回到AppDelegate.cpp中,引入头文件#include"pbc/pbc/pbc-lua.h",并在boolAppDelegate::applicationDidFinishLaunching()方法中加入luaopen_protobuf_c(L);用于将protobuf的函数注册进LUA,代码如下:

  1. bool AppDelegate::applicationDidFinishLaunching()
  2.  
  3. {
  4.  
  5. // set default FPS
  6.  
  7. Director::getInstance()->setAnimationInterval(1.0 / 60.0f);
  8.  
  9.  
  10. // register lua module
  11.  
  12. auto engine = LuaEngine::getInstance();
  13.  
  14. ScriptEngineManager::getInstance()->setScriptEngine(engine);
  15.  
  16. lua_State* L = engine->getLuaStack()->getLuaState();
  17.  
  18. lua_module_register(L);
  19.  
  20.  
  21. luaopen_protobuf_c(L);//在lua中注册Proto函数
  22.  
  23.  
  24. register_all_packages();
  25.  
  26.  
  27. LuaStack* stack = engine->getLuaStack();
  28.  
  29. stack->setXXTEAKeyAndSign("2dxLua",strlen("2dxLua"),"XXTEA",strlen("XXTEA"));
  30.  
  31.  
  32. if (engine->executeScriptFile("src/main.lua"))
  33.  
  34. {
  35.  
  36. return false;
  37.  
  38. }
  39.  
  40.  
  41. return true;
  42.  
  43. }


第六:接下来我们要再为LUA注册一个函数用于读取pb文件数据:

  1. static int readProtobufFile(lua_State *L)
  2.  
  3. {
  4.  
  5. const char *buff = luaL_checkstring(L,-1);
  6.  
  7. Data data = CCFileUtils::getInstance()->getDataFromFile(buff);
  8.  
  9. lua_pushlstring(L,(const char*)data.getBytes(),data.getSize());
  10.  
  11. return 1; /* number of results */
  12.  
  13. }
并将该函数注册到lua中,在staticint register_all_packages()中添加如下语句:

  1. // If you want to use packages manager to install more packages,// don't modify or remove this function
  2.  
  3. static int register_all_packages()
  4.  
  5. {
  6.  
  7. lua_State *L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  8.  
  9. luaopen_protobuf_c(L);
  10.  
  11.  
  12. lua_register(L,"readProtobufFile",readProtobufFile);
  13.  
  14. return 0; //flag for packages manager
  15.  
  16. }

第七: 编译并运行,编译通过后,游戏运行成功!
第八:

至此,C++端设置完毕。但是并没有结束,还有最后几步。

回到游戏项目的binding/lua目录,找到"protobuf.lua"文件,将其复制到lua项目的src目录下。


第九:

在LUA中Protobuf的简单例程:

require "app/protobuf.lua" //导入你的protobuf.lua文件

  1. local pbFilePath = cc.FileUtils:getInstance():fullPathForFilename("app/MsgProtocol.pb")
  2. release_print("PB file path: "..pbFilePath)
  3.  
  4. local buffer = readProtobufFile(pbFilePath)
  5. protobuf.register(buffer) --注:protobuf 是因为在protobuf.lua里面使用module(protobuf)来修改全局名字
  6.  
  7. local stringbuffer = protobuf.encode("Person",{
  8. name = "Alice",id = 12345,phone = {
  9. {
  10. number = "87654321"
  11. },}
  12. })
  13. local slen = string.len(stringbuffer)
  14. local temp = ""
  15. for i=1,slen do
  16. temp = temp .. string.format("0xX,",string.byte(stringbuffer,i))
  17. end
  18. release_print(temp)
  19. local result = protobuf.decode("Person",stringbuffer)
  20. release_print("result name: "..result.name)
  21. release_print("result name: "..result.id)

MsgProtocol.proto的内容

  1. message Person {
  2. required string name = 1;
  3. required int32 id = 2;
  4. optional string email = 3;
  5.  
  6. enum PhoneType {
  7. MOBILE = 0;
  8. HOME = 1;
  9. WORK = 2;
  10. }
  11.  
  12. message PhoneNumber {
  13. required string number = 1;
  14. optional PhoneType type = 2 [default = HOME];
  15. }
  16.  
  17. repeated PhoneNumber phone = 4;
  18. }
  19.  
  20. message AddressBook {
  21. repeated Person person = 1;
  22. }
需要生成.pb文件的话,你利用前面C++的集成方法生成protoc.exe,.bat文件生成即可

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