Unity中protobuf-lua的使用

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/qq563129582/article/details/79448067

protoc-gen-lua

Protobuf 官方并没有 Lua版本,然后网易的程序猿开发出了 protoc-gen-lua ,可以让我们将 Proto 文件转成 lua 脚本在 Lua中使用,下面是详细的编译、安装、使用教程。文中用到的代码

1.下载protoc-gen-lua

可以通过HG从服务器(hg clone https://code.google.com/p/protoc-gen-lua/)上下载最新的版本。
简单介绍一下里面的三个目录:
* example 存放的一个示例协议,
* plugin 将.proto协议转为LUA脚本需要的工具。要注意,这个工具是用PYTHON写的,所以后面我们需要安装PYTHON。
* rotobuf这里存放了工程需要的文件。其中pb.c是C码文件。主要是在工程中引用,也可以编译成动态文件(.so)供LUA调用。其它LUA文件则需要引入到LUA工程中。

2、安装PYTHON27。推荐是这个版本。

3、下载protobuf然后编译出protoc.exe。

可以用SVN从服务器上(http://protobuf.googlecode.com/svn/trunk)下载最新的protobuf。我使用的是protobuf-2.4.1。
进入protobuf-2.4.1/vsprojects利用VS2010进行编译。生成的protoc.exe放到protobuf-2.4.1/src下。如果不放,后面无法安装python版的protobuf。

4、编译python版本的protobuf

在protobuf-2.4.1\python下运行python setup.py build,然后再执行python setup.py install。
注意:如果第3步的protoc.exe没有放,会出现错误找不到google\protobuf\compiler目录

5、制作转换协议的批处理

在protoc-gen-lua/plugin目录下编写批处理:protoc-gen-lua.bat,就下面一行代码。

@python "%~dp0protoc-gen-lua"

但要确保你的python命令能正常运行。否则将python.exe所在的目录加到环境变量path中。
接着拷贝一份protoc.exe到protoc-gen-lua目录。第3步我们已经编译了出了protoc.exe。在协议转换中,我们需要使用他。
在protoc-gen-lua目录编写批处理:buildproto.bat 来转换协议。

rem 切换到.proto协议所在的目录  
cd ../luascript  
rem 将当前文件夹中的所有协议文件转换为lua文件  
for %%i in (*.proto) do (    
echo %%i  
"..\protobuf\protoc\protoc.exe" --plugin=protoc-gen-lua="..\protobuf\plugin\protoc-gen-lua.bat" --lua_out=. %%i  

)  
echo end  
pause 

请正确指定protoc.exe和protoc-gen-lua.bat相对协议目录的路径。

6、转换协议文件

protoc-gen-lua/example目录中,有一个协议文件person.proto,可以拿他做一下试验,会生成一个person_pb.lua

7、编译pb.c文件

protoc-gen-lua/protobuf目录中有一个pb.c文件。我们需要用他来协助lua完成protobuf的功能。
用vs2010新建一个控制台程序。将pb.c加入到工程中。在windows平台下,要对pb.c做如下修改。
1)将 #include “endian.h”修改为

#ifndef _WIN32
#include <endian.h>
#endif

避免在windows下缺失文件报错.

2)调整struct_unpack函数前几行为

static int struct_unpack(lua_State *L)
{
    uint8_t format = luaL_checkinteger(L, 1);
    size_t len;
    const uint8_t* buffer = (uint8_t*)luaL_checklstring(L, 2, &len);
    size_t pos = luaL_checkinteger(L, 3);
    uint8_t out[8];    
    buffer += pos; 
}

3)在主函数前面申明pb.c的入口。

// 注意防在命名空间外的全局声明
extern "C" { int luaopen_pb (lua_State *L);}   

编写主函数如下:

#include "stdafx.h"  
extern "C"{  
    #include <lua.h>  
    #include <lualib.h>  
    #include <lauxlib.h>  
    int luaopen_pb (lua_State *L);  
}  
int main(int argc, char* argv[])  
{  
    lua_State *L = lua_open();  
    luaL_openlibs(L);  
    luaopen_pb(L);  
    luaL_dofile(L, "main.lua");   
    lua_pcall(L, 0, LUA_MULTRET, 0);  
    lua_close(L);       
    return 0;   
}  

工程需要lua5.1.lib的接入。这个请自行编译。

8、编写main.lua。

也就是测试文件,可以参考protoc-gen-lua/example中的test.lua。

package.path = package.path .. ';./protobuf/?.lua'  

require "person_pb"  
local msg = person_pb.Person()  
msg.id = 100   
msg.name = "foo"   
msg.email = "bar"   

local pb_data = msg:SerializeToString()  -- Parse Example  
print("create:", msg.id, msg.name, msg.email, pb_data)  


local msg = person_pb.Person()   
msg:ParseFromString(pb_data)   
print("parser:", msg.id, msg.name, msg.email, pb_data)  

9、测试

编译并运行VS工程。运行目录应该是LUA文件所在的目录。运行结果如下:

10、总结。

这里实现了,在C++中搭建lua的protobuf环境。但未实现纯粹的Lua-protobuf环境。
如果需要在LUA中实现protobuf,那需要自己将pb.c编译成dll。在linux下需要利用protoc-gen-lua/protobuf中的makefile将pb.c编译成pb.so。
然后将pb.so或pb.dll导入到lua工程中。然后在main.lua中调用pb.c中的入口,代码如下:

local a = package.loadlib("pb.dll", "luaopen_pb");  
a()

猜你喜欢

转载自blog.csdn.net/qq563129582/article/details/79448067