Lua Protobuf

作者:追风剑情 发布于:2019-1-30 14:27 分类:Unity3d

在使用protoc-gen-lua前,先确保电脑上已经安装了Python2.x以及Protobuf

Python安装Protobuf

一、下载protoc-gen-lua

https://github.com/sean-lin/protoc-gen-lua

二、写两个批处理文件

111.png


python %~dp0protoc-gen-lua


2222.png


cd ./example  
for %%i in (*.proto) do (  
echo %%i  
protoc --plugin=protoc-gen-lua="..\plugin\protoc-gen-lua.bat" --lua_out=.  %%i  
  
)
echo end  
pause


二、运行protoc.bat

3333.png

此时已经通过person.proto生成了一个person_pb.lua文件

4444.png

问题1:如果未生成person_pb.lua文件,但cmd中输出是正确的,可以修改下Plugin\protoc-gen-lua文件

5555.png

问题2:下载的person.proto没声明版本号可能会导致编译报错,需要自己加上。

6666.png


下面贴出生成的person_pb.lua文件内容


-- Generated By protoc-gen-lua Do not Edit
local protobuf = require "protobuf"
module('person_pb')


local PERSON = protobuf.Descriptor();
local PERSON_ID_FIELD = protobuf.FieldDescriptor();
local PERSON_NAME_FIELD = protobuf.FieldDescriptor();
local PERSON_EMAIL_FIELD = protobuf.FieldDescriptor();
local PHONE = protobuf.Descriptor();
local PHONE_PHONE_TYPE = protobuf.EnumDescriptor();
local PHONE_PHONE_TYPE_MOBILE_ENUM = protobuf.EnumValueDescriptor();
local PHONE_PHONE_TYPE_HOME_ENUM = protobuf.EnumValueDescriptor();
local PHONE_NUM_FIELD = protobuf.FieldDescriptor();
local PHONE_TYPE_FIELD = protobuf.FieldDescriptor();
local PHONE_PHONES_FIELD = protobuf.FieldDescriptor();

PERSON_ID_FIELD.name = "id"
PERSON_ID_FIELD.full_name = ".Person.id"
PERSON_ID_FIELD.number = 1
PERSON_ID_FIELD.index = 0
PERSON_ID_FIELD.label = 2
PERSON_ID_FIELD.has_default_value = false
PERSON_ID_FIELD.default_value = 0
PERSON_ID_FIELD.type = 5
PERSON_ID_FIELD.cpp_type = 1

PERSON_NAME_FIELD.name = "name"
PERSON_NAME_FIELD.full_name = ".Person.name"
PERSON_NAME_FIELD.number = 2
PERSON_NAME_FIELD.index = 1
PERSON_NAME_FIELD.label = 2
PERSON_NAME_FIELD.has_default_value = false
PERSON_NAME_FIELD.default_value = ""
PERSON_NAME_FIELD.type = 9
PERSON_NAME_FIELD.cpp_type = 9

PERSON_EMAIL_FIELD.name = "email"
PERSON_EMAIL_FIELD.full_name = ".Person.email"
PERSON_EMAIL_FIELD.number = 3
PERSON_EMAIL_FIELD.index = 2
PERSON_EMAIL_FIELD.label = 1
PERSON_EMAIL_FIELD.has_default_value = false
PERSON_EMAIL_FIELD.default_value = ""
PERSON_EMAIL_FIELD.type = 9
PERSON_EMAIL_FIELD.cpp_type = 9

PERSON.name = "Person"
PERSON.full_name = ".Person"
PERSON.nested_types = {}
PERSON.enum_types = {}
PERSON.fields = {PERSON_ID_FIELD, PERSON_NAME_FIELD, PERSON_EMAIL_FIELD}
PERSON.is_extendable = true
PERSON.extensions = {}
PHONE_PHONE_TYPE_MOBILE_ENUM.name = "MOBILE"
PHONE_PHONE_TYPE_MOBILE_ENUM.index = 0
PHONE_PHONE_TYPE_MOBILE_ENUM.number = 1
PHONE_PHONE_TYPE_HOME_ENUM.name = "HOME"
PHONE_PHONE_TYPE_HOME_ENUM.index = 1
PHONE_PHONE_TYPE_HOME_ENUM.number = 2
PHONE_PHONE_TYPE.name = "PHONE_TYPE"
PHONE_PHONE_TYPE.full_name = ".Phone.PHONE_TYPE"
PHONE_PHONE_TYPE.values = {PHONE_PHONE_TYPE_MOBILE_ENUM,PHONE_PHONE_TYPE_HOME_ENUM}
PHONE_NUM_FIELD.name = "num"
PHONE_NUM_FIELD.full_name = ".Phone.num"
PHONE_NUM_FIELD.number = 1
PHONE_NUM_FIELD.index = 0
PHONE_NUM_FIELD.label = 1
PHONE_NUM_FIELD.has_default_value = false
PHONE_NUM_FIELD.default_value = ""
PHONE_NUM_FIELD.type = 9
PHONE_NUM_FIELD.cpp_type = 9

PHONE_TYPE_FIELD.name = "type"
PHONE_TYPE_FIELD.full_name = ".Phone.type"
PHONE_TYPE_FIELD.number = 2
PHONE_TYPE_FIELD.index = 1
PHONE_TYPE_FIELD.label = 1
PHONE_TYPE_FIELD.has_default_value = false
PHONE_TYPE_FIELD.default_value = nil
PHONE_TYPE_FIELD.enum_type = PHONE_PHONE_TYPE
PHONE_TYPE_FIELD.type = 14
PHONE_TYPE_FIELD.cpp_type = 8

PHONE_PHONES_FIELD.name = "phones"
PHONE_PHONES_FIELD.full_name = ".Phone.phones"
PHONE_PHONES_FIELD.number = 10
PHONE_PHONES_FIELD.index = 0
PHONE_PHONES_FIELD.label = 3
PHONE_PHONES_FIELD.has_default_value = false
PHONE_PHONES_FIELD.default_value = {}
PHONE_PHONES_FIELD.message_type = PHONE
PHONE_PHONES_FIELD.type = 11
PHONE_PHONES_FIELD.cpp_type = 10

PHONE.name = "Phone"
PHONE.full_name = ".Phone"
PHONE.nested_types = {}
PHONE.enum_types = {PHONE_PHONE_TYPE}
PHONE.fields = {PHONE_NUM_FIELD, PHONE_TYPE_FIELD}
PHONE.is_extendable = false
PHONE.extensions = {PHONE_PHONES_FIELD}

Person = protobuf.Message(PERSON)
Phone = protobuf.Message(PHONE)

Person.RegisterExtension(PHONE_PHONES_FIELD)


三、gcc编译pb.c为对应平台的库文件

windows平台,编译成pb.dll

1、下载min-gw工具

https://sourceforge.net/projects/mingw/files/

2、安装min-gw

99999.png

8888.png

3、{安装目录}\MinGW\bin 将此目录配到环境变量Path中

4、在cmd中输入mingw-get,回车。若能弹出min-gw管理界面,则说明安装成功

5、安装gcc编译器

7777.png

gcc参数详解 https://www.cnblogs.com/zhangsir6/articles/2956798.html

6、安装mingw32-make

6666.png

四、直接利用Visual Studio编译pb.c

1、将pc.c文件导入到Visual Studio工程中

1111.png

2222.png

2、引用Lua的静态链接库

2222.png

3、pb.c上点右键->属性

44444.png

生成dll

333.png

问题: 如果报以下错

1111.png

解决方法: 检查有没正确引用Lua的静态连接库

更多参考

编译Lua源代码

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号