且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

更新时间:2022-04-07 02:32:38

本站文章均为 李华明Himi 原创,转载务必在明显处注明: 
转载自【黑米GameDev街区】 原文链接: http://www.himigame.com/lua-game/1644.html

对于google的PB大家应该都不陌生了,那么Himi这里转一篇在cocos2dx-lua下的使用教程。

原文地址:http://cn.cocos2d-x.org/tutorial/show?id=2947

 

1.什么是Protobuf?

数据传输格式有很多种最常见XML和Json,这两种格式生成速度非常快,但是解析的效率却很低。尤其在数据量非常大的情况下,会卡住好半天。并且这种格式的传输也会使得传输所耗费流量变大。解析效率最高,传输消耗流量最小的数据格式自然是Buffer。很多公司都封装过自己的Buffer流传输的模块。

Google公司将他们封装的Buffer流模块给贡献了出来!也就是我这里所提到的Protobuf。

传送门:https://github.com/google/protobuf

Protobuf提供了很多种语言的实现,但是官方实现中是没有LUA的,这里我们采用第三方的LUA版ProtoBuf

2.如何在LUA中使用Protobuf

Protobuf在Cocos2d-LUA配置步骤相当繁琐。但是,按照步骤配置一定能够成功编译并运行正常,请大家耐心往下看。

首先我们到GtiHub上下载第三方的LUA版Protobuf(GitHub:https://github.com/cloudwu/pbc

如果以上地址失效可以去我Fork的地址下载:https://github.com/ArcherPeng/pbc(直接右键->另存为这个链接也可以:下载地址

下载完后会得到一个pbc-master.zip,将其解压缩,重命名为”pbc”并拷贝到游戏项目的Classes目录下:

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

然后回到Xcode中将pbc项目导入Xcode工程中:

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

导入完毕后,在导入的pbc.xcodeproj中创建一个头文件”pbc-lua.h”(***放在工程的pbc目录下)

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

注意是创建头文件:

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

在创建的文件中添加如下代码:

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//  
//  pbc-lua.h  
//  pbc  
//  
//  Created by ArcherPeng on 15/7/21.  
//  Copyright (c) 2015年 ztgame. All rights reserved.  
//  
  
#ifndef pbc_pbc_lua_h  
#define pbc_pbc_lua_h  
  
#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 */

然后导入pbc/binding/lua/pbc-lua.c (也放在工程的pbc目录下)

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

然后在”pbc-lua.c”中引入刚刚创建的头文件#include”pbc-lua.h”:

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

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

下边两步比较重要!很多人搞不定都是因为这两步:

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

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

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

然后还要修改pbc工程的Valid Architectures,为其加入 x86_64 ,如图所示

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

以上添加完成后,修改编译目标位pbc->IOS Device

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

然后Run一下,会提示编译成功,然后在pbc工程的Products目录下能够找到一个libpbc.a,则表示配置成功了!

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

再次之后,将libpbc.a加入整个游戏项目的LinkBinary中:

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

添加完成后,回到AppDelegate.cpp中,引入头文件#include”pbc/pbc/pbc-lua.h”

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

并在boolAppDelegate::applicationDidFinishLaunching()方法中加入luaopen_protobuf_c(L);用于将protobuf的函数注册进LUA,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;  
}

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

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

1
2
3
4
5
6
7
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 */
}

并将该函数注册到lua中,在staticint register_all_packages()中添加如下语句:

1
2
3
4
5
6
7
8
9
10
// 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  
}

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

更多关于C/C++与LUA互掉的文章请浏览:http://www.archerpeng.com/?p=52

此时,编译目标切换回Cocos游戏的主工程:

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

编译并运行,编译通过后,游戏运行成功!

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

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

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

【转】在COCOS2D-LUA中使用PROTOBUF(XCODE配置方法)

在LUA中Protobuf的简单例程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
local pbFilePath = cc.FileUtils:getInstance():fullPathForFilename("res/addressbook.pb")    
release_print("PB file path: "..pbFilePath)    
  
local pb = require "protobuf"
local buffer = readProtobufFile</span>(pbFilePath)    
  
pb.register(buffer)    
local stringbuffer = pb.encode("tutorial.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 = pb.decode("tutorial.Person", stringbuffer)    
release_print("result name: "..result.name)

关于如何编译生成addressbook.pb,参见:http://www.archerpeng.com/?p=26

http://blog.csdn.net/qq446569365/article/details/44957971

关于如何在windows平台集成lua版Protobuf及简单使用说明参见:http://blog.csdn.net/leelyn/article/details/44998547

来源网址:http://blog.csdn.net/qq446569365/article/details/46981521