首页 > 代码库 > 照着例子学习 protobuf-lua

照着例子学习 protobuf-lua

参考文章:cocos2dx使用lua和protobuf

首先得下载protobuf-gen-lua的插件,插件Git地址在此。

下载完之后进入到protoc-gen-lua\plugin这个目录,并在其中建立一个批处理文件:

@python E:\3rd\quick-x-protobuf\proto-gen-lua\protoc-gen-lua\plugin\protoc-gen-lua

 

然后进入到上一篇笔记中的addressbook.proto所在的文件夹内,运行如下命令:

protoc --lua_out=生成的lua存放地址 --plugin=protoc-gen-lua=上面新建的bat的完整地址 addressbook.proto

回车。

 

我们到目标地址下面就会看到一个addressbook_pb.lua文件,内容如下:

-- Generated By protoc-gen-lua Do not Editlocal protobuf = require "protobuf"module(addressbook_pb)local PERSON = protobuf.Descriptor();local PERSON_PHONENUMBER = protobuf.Descriptor();local PERSON_PHONENUMBER_NUMBER_FIELD = protobuf.FieldDescriptor();local PERSON_PHONENUMBER_TYPE_FIELD = protobuf.FieldDescriptor();local PERSON_PHONETYPE = protobuf.EnumDescriptor();local PERSON_PHONETYPE_MOBILE_ENUM = protobuf.EnumValueDescriptor();local PERSON_PHONETYPE_HOME_ENUM = protobuf.EnumValueDescriptor();local PERSON_PHONETYPE_WORK_ENUM = protobuf.EnumValueDescriptor();local PERSON_NAME_FIELD = protobuf.FieldDescriptor();local PERSON_ID_FIELD = protobuf.FieldDescriptor();local PERSON_EMAIL_FIELD = protobuf.FieldDescriptor();local PERSON_PHONE_FIELD = protobuf.FieldDescriptor();local ADDRESSBOOK = protobuf.Descriptor();local ADDRESSBOOK_PERSON_FIELD = protobuf.FieldDescriptor();PERSON_PHONENUMBER_NUMBER_FIELD.name = "number"PERSON_PHONENUMBER_NUMBER_FIELD.full_name = ".tutorial.Person.PhoneNumber.number"PERSON_PHONENUMBER_NUMBER_FIELD.number = 1PERSON_PHONENUMBER_NUMBER_FIELD.index = 0PERSON_PHONENUMBER_NUMBER_FIELD.label = 2PERSON_PHONENUMBER_NUMBER_FIELD.has_default_value = falsePERSON_PHONENUMBER_NUMBER_FIELD.default_value = ""PERSON_PHONENUMBER_NUMBER_FIELD.type = 9PERSON_PHONENUMBER_NUMBER_FIELD.cpp_type = 9PERSON_PHONENUMBER_TYPE_FIELD.name = "type"PERSON_PHONENUMBER_TYPE_FIELD.full_name = ".tutorial.Person.PhoneNumber.type"PERSON_PHONENUMBER_TYPE_FIELD.number = 2PERSON_PHONENUMBER_TYPE_FIELD.index = 1PERSON_PHONENUMBER_TYPE_FIELD.label = 1PERSON_PHONENUMBER_TYPE_FIELD.has_default_value = truePERSON_PHONENUMBER_TYPE_FIELD.default_value = HOMEPERSON_PHONENUMBER_TYPE_FIELD.enum_type = _PERSON_PHONETYPEPERSON_PHONENUMBER_TYPE_FIELD.type = 14PERSON_PHONENUMBER_TYPE_FIELD.cpp_type = 8PERSON_PHONENUMBER.name = "PhoneNumber"PERSON_PHONENUMBER.full_name = ".tutorial.Person.PhoneNumber"PERSON_PHONENUMBER.nested_types = {}PERSON_PHONENUMBER.enum_types = {}PERSON_PHONENUMBER.fields = {PERSON_PHONENUMBER_NUMBER_FIELD, PERSON_PHONENUMBER_TYPE_FIELD}PERSON_PHONENUMBER.is_extendable = falsePERSON_PHONENUMBER.extensions = {}PERSON_PHONENUMBER.containing_type = PERSONPERSON_PHONETYPE_MOBILE_ENUM.name = "MOBILE"PERSON_PHONETYPE_MOBILE_ENUM.index = 0PERSON_PHONETYPE_MOBILE_ENUM.number = 0PERSON_PHONETYPE_HOME_ENUM.name = "HOME"PERSON_PHONETYPE_HOME_ENUM.index = 1PERSON_PHONETYPE_HOME_ENUM.number = 1PERSON_PHONETYPE_WORK_ENUM.name = "WORK"PERSON_PHONETYPE_WORK_ENUM.index = 2PERSON_PHONETYPE_WORK_ENUM.number = 2PERSON_PHONETYPE.name = "PhoneType"PERSON_PHONETYPE.full_name = ".tutorial.Person.PhoneType"PERSON_PHONETYPE.values = {PERSON_PHONETYPE_MOBILE_ENUM,PERSON_PHONETYPE_HOME_ENUM,PERSON_PHONETYPE_WORK_ENUM}PERSON_NAME_FIELD.name = "name"PERSON_NAME_FIELD.full_name = ".tutorial.Person.name"PERSON_NAME_FIELD.number = 1PERSON_NAME_FIELD.index = 0PERSON_NAME_FIELD.label = 2PERSON_NAME_FIELD.has_default_value = falsePERSON_NAME_FIELD.default_value = ""PERSON_NAME_FIELD.type = 9PERSON_NAME_FIELD.cpp_type = 9PERSON_ID_FIELD.name = "id"PERSON_ID_FIELD.full_name = ".tutorial.Person.id"PERSON_ID_FIELD.number = 2PERSON_ID_FIELD.index = 1PERSON_ID_FIELD.label = 2PERSON_ID_FIELD.has_default_value = falsePERSON_ID_FIELD.default_value = 0PERSON_ID_FIELD.type = 5PERSON_ID_FIELD.cpp_type = 1PERSON_EMAIL_FIELD.name = "email"PERSON_EMAIL_FIELD.full_name = ".tutorial.Person.email"PERSON_EMAIL_FIELD.number = 3PERSON_EMAIL_FIELD.index = 2PERSON_EMAIL_FIELD.label = 1PERSON_EMAIL_FIELD.has_default_value = falsePERSON_EMAIL_FIELD.default_value = ""PERSON_EMAIL_FIELD.type = 9PERSON_EMAIL_FIELD.cpp_type = 9PERSON_PHONE_FIELD.name = "phone"PERSON_PHONE_FIELD.full_name = ".tutorial.Person.phone"PERSON_PHONE_FIELD.number = 4PERSON_PHONE_FIELD.index = 3PERSON_PHONE_FIELD.label = 3PERSON_PHONE_FIELD.has_default_value = falsePERSON_PHONE_FIELD.default_value = {}PERSON_PHONE_FIELD.message_type = PERSON_PHONENUMBERPERSON_PHONE_FIELD.type = 11PERSON_PHONE_FIELD.cpp_type = 10PERSON.name = "Person"PERSON.full_name = ".tutorial.Person"PERSON.nested_types = {PERSON_PHONENUMBER}PERSON.enum_types = {PERSON_PHONETYPE}PERSON.fields = {PERSON_NAME_FIELD, PERSON_ID_FIELD, PERSON_EMAIL_FIELD, PERSON_PHONE_FIELD}PERSON.is_extendable = falsePERSON.extensions = {}ADDRESSBOOK_PERSON_FIELD.name = "person"ADDRESSBOOK_PERSON_FIELD.full_name = ".tutorial.AddressBook.person"ADDRESSBOOK_PERSON_FIELD.number = 1ADDRESSBOOK_PERSON_FIELD.index = 0ADDRESSBOOK_PERSON_FIELD.label = 3ADDRESSBOOK_PERSON_FIELD.has_default_value = falseADDRESSBOOK_PERSON_FIELD.default_value = {}ADDRESSBOOK_PERSON_FIELD.message_type = PERSONADDRESSBOOK_PERSON_FIELD.type = 11ADDRESSBOOK_PERSON_FIELD.cpp_type = 10ADDRESSBOOK.name = "AddressBook"ADDRESSBOOK.full_name = ".tutorial.AddressBook"ADDRESSBOOK.nested_types = {}ADDRESSBOOK.enum_types = {}ADDRESSBOOK.fields = {ADDRESSBOOK_PERSON_FIELD}ADDRESSBOOK.is_extendable = falseADDRESSBOOK.extensions = {}AddressBook = protobuf.Message(ADDRESSBOOK)Person = protobuf.Message(PERSON)Person.PhoneNumber = protobuf.Message(PERSON_PHONENUMBER)

 

实验完成,打算下一步把这个整合到自己的手游demo里面去,有机会我会好好阅读protoc-gen-lua代码的,现在最要紧的是下班,呵呵。

 

照着例子学习 protobuf-lua