终于解决protobuf 问题了
1.python 安装(略)
2.protoc-gen-lua-master 解压到 D:\temp\protoc-gen-lua-master
3.protobuf-2.5.0
下载地址:
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分支 protoc-gen-lua-master.zip (27 KB)
protobuf:
解压后到目录
到目录vsprojects下打开protobuf.sln项目,编译,会在vsprojects/Debug或者vsprojects/Release生成protoc.exe执行文件
将protoc.exe 拷贝到 D:\temp\protoc-gen-lua-master\example
将protoc.exe拷贝到D:\temp\protoc-gen-lua-master\protobuf-2.5.0\python目录下。在cmd下,切换到该目录,执行python setup.py install
注意了,这里如果不导入一个C++编译过的包,会出现这样的错误
file google\protobuf\descriptor_pb2.py (for module google.protobuf.descriptor_pb
2) not found
package init file ‘google\protobuf\compiler_init_.py’ not found (or not a reg
ular file)
查看该目录下的Reademe.txt,才知道python需要用到C++编译后的某些接口,于是乎,用vs2010将protobuf
项目打开并完整让其生成成功,然后将vsprojects\google\protobuf下的compiler复制到python\google\protobuf下。
ok,Processing dependencies for protobuf==2.5.0
Finished processing dependencies for protobuf==2.5.0
将 D:\temp\protoc-gen-lua-master\plugin 加入到环境变量 Path中
在D:\temp\protoc-gen-lua-master\plugin 下建立 protoc-gen-lua.bat 文件
写入@python D:\temp\protoc-gen-lua-master\plugin\protoc-gen-lua
D:\temp\protoc-gen-lua-master\example 下建立 build.bat
写入D:\temp\protoc-gen-lua-master\example\protoc.exe --lua_out=./ --plugin=protoc-gen-lua=“D:\temp\protoc-gen-lua-master\plugin\protoc-gen-lua.bat” person.proto
双击build.bat 会得到文件 person_pb.lua
这个文件我们需要用到的
下面 在就是pb.c 在cocos2dx-quick3.5 扩展
将 D:\temp\protoc-gen-lua-master\protobuf\pb.c 复制到项目 quick-3.5final\quick-3.5\cocos\quick_libs\src\extensions\protobuf 下
由于是windows 需要修改 pb.c 文件
C代码
-
#include <endian.h>
改成
C代码 - #ifndef WIN32
- #include <endian.h>
- #else
- #pragma comment(lib,“lua51.lib”)
- #endif
C代码
- static int struct_unpack(lua_State *L){ 函数下的
C代码
- buffer += pos;
- uint8_t out;
要改成
C代码
- uint8_t out;
- buffer += pos;
修改 quick_extensions.c 文件
1.在
// lsqlite3
#include “lsqlite3/lsqlite3.h”
下面 加入
#include “protobuf/pb.c” //引用protobuf 库文件
2.在 lua_pop(L, 2); 下面加入
luaopen_pb(L); //这是加入的protobuf 扩展注册名
只生成quick_libs , simulator.exe 是不会重新生成的,
重新生成项目 确保 simulator.exe 生成成功
当然 也需要修改 自己 项目路径\frameworks\cocos2d-x\cocos\quick_libs\src\extensions
和上面一样修改 并且生成
在lua项目修改
在 建立目录 项目路径/src/protobuf
将D:\temp\protoc-gen-lua-master\protobuf 的9个 lua 文件 拷贝到 项目路径/src/protobuf
修改 项目路径/src/main.lua
package.path = package.path … “;src/;” 为 package.path = package.path … “;src/;./protobuf/?.lua;./src/protobuf/?.lua;”
修改项目路径/src/app/MyApp.lua
在 local MyApp = class(“MyApp”, cc.mvc.AppBase) 这行前 加入, 当然也可以在其他地方引入
require(“pb”)
require(“Config.person_pb”) --这个就是我上面生成的 person_pb.lua 我放在了 项目路径/src/Config 目录下的
测试
local msg = person_pb.Person()
msg.id = 1000
msg.name = “fsoo”
msg.email = “bar”
local pb_data = msg:SerializeToString() – Parse Example
print(“create:”, msg.id, msg.name, msg.email, pb_data)
print(pb_data)
local msg = person_pb.Person()
msg:ParseFromString(pb_data)
print(“parser:”, msg.id, msg.name, msg.email, pb_data)
输出框
create: 1000 fsoo bar ?fsoobar
?fsoobar
parser: 1000 fsoo bar ?fsoobar