quick3.3下集成云风pbc(windows版)

  感觉记性不是很好,恰好最近有点空闲时间,所以把以前捣鼓过的东西整理一下,防止以后又忘记了:10:       

主要分为下面几个步骤:
1,开始前的准备工作
首先从github上下载google的protobuf编译出protoc.exe 地址https://github.com/google/protobuf
然后也是从github上下载云风大侠的pbc 地址https://github.com/cloudwu/pbc
2,将pbc中的相关代码加入到vs(我用的是vs2013)工程项目中
在frameworks\runtime-src\Classes目录下加入protobuf文件夹文件夹中加入pbc相关代码,它们的结构是这样的
protobuf\src
protobuf\pbc.h
protobuf\pbc-lua.c
protobuf\pbc-lua.h
其中src文件夹即为pbc下的src,pbc.h为pbc下的pbc.h,pbc-lua.c为pbc\binding\lua目录下的puc-lua.c
pbc-lua.h是我们自己创建的头文件,它的内容是:


#ifndef __LUA_PBC_EXTRA_H_
#define __LUA_PBC_EXTRA_H_

#if __cplusplus
extern "C" {
#endif

#include "lauxlib.h"

int luaopen_protobuf_c(lua_State *L);
    
#if __cplusplus
}
#endif

#endif

 然后在vs中新建筛选器protobuf和src将相关代码添加进去,通过操作项目工程属性将protobuf文件夹目录包含进去。
 在AppDelegate.cpp头部加入#include "protobuf/pbc-lua.h" 然后在bool AppDelegate::applicationDidFinishLaunching()
 方法中LuaStack* stack = engine->getLuaStack();这一句的上一行加入luaopen_protobuf_c(L);函数调用
 重新编译工程。

3,在脚本中加入protobuf.lua(为了项目看上去结构比较好看,在app目录下加入了protobuf目录)
在src\app目录下加入protobuf文件夹,在文件夹中加入pbc中binding\lua\protobuf.lua文件
4,编写protobuf文件并用protoc.exe生成pb文件
我的例子protobuf源文件为:person.proto


package test.protocol.sgp;
message Person {   
    required int32 id = 1;   
    required string name = 2;   
    optional string email = 3; 
} 

   在网上找了个批处理文件,pb.bat然后根据需求将其改了下

@echo off
set DIR=%~dp0
cd /d "%DIR%"
setlocal enabledelayedexpansion
for /r %%i in (*.proto) do ( 
set pbname=%%i 
      set pbname=!pbname:~0,-5!b
      protoc -I %DIR% --descriptor_set_out !pbname! %%i 
)

if exist "./pb" rmdir /s /q "./pb"
mkdir "./pb"
move *.pb ./pb

echo "finished"

 将protoc.exe,pb.bat,person.proto 放入同一目录下并执行pb.bat
 执行完成后再当前目录下会生成一个pb文件夹,文件夹中即为我们所需要的person.pb文件

5,编写测试代码
将生成的pb文件夹放入项目资源目录res中
我的MyApp.lua


require("config")
require("cocos.init")
require("framework.init")
require("app.protobuf.protobuf")

MyGame = {}
MyGame.protobuf = protobuf
local MyApp = class("MyApp", cc.mvc.AppBase)

function MyApp:ctor()
    MyApp.super.ctor(self)
end

function MyApp.registerAllProtobuf()
    local pbAllName = { 
                        "pb/person.pb", 
                    }
    for i=1, #pbAllName do
        local fileData = cc.HelperFunc:getFileData(pbAllName*)
        MyGame.protobuf.register(fileData)
    end
end

function MyApp:run()
    cc.FileUtils:getInstance():addSearchPath("res/")
    MyApp.registerAllProtobuf()
    self:enterScene("MainScene")
end

return MyApp
*


     我的MainScene.lua

local MainScene = class(“MainScene”, function()
return display.newScene(“MainScene”)
end)

function MainScene:ctor()
local data = {}
data.id = 12
data.name = “xiaoming”
local strData = MyGame.protobuf.encode(“test.protocol.sgp.Person”, data)
local person = MyGame.protobuf.decode(“test.protocol.sgp.Person”, strData)
dump(person)
end

return MainScene





最后运行项目如果能够dump出person就表示集成成功啦!:7:       



2赞

:2::2::2::2:

折腾了一番,不过我是放在 quick-src\lua_extensions 下面了

错误 26 error LNK2019: 无法解析的外部符号 _luaopen_protobuf_c,该符号在函数 “public: virtual bool __thiscall AppDelegate::applicationDidFinishLaunching(void)” (?applicationDidFinishLaunching@AppDelegate@@UAE_NXZ) 中被引用 E:\mycocos\nettest\frameworks\runtime-src\proj.win32\AppDelegate.obj nettest.

这个报错什么意思啊

刚刚那个问题解决了,应该在pbc-lua.c 中 加入 #include “ppc-lua.h”

求发我一份源码,搞了2天没搞定
qq2300563522

太赞了!写的很详细,按教程一步步做下来,有几处小改动,最后成功打印出来.对cocos不熟,相关的资料太少,两个星期的梗终于解决,可以睡个好觉了