照着例子学习 protobuf-lua

时间:2021-05-14 12:34:48

参考文章: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所在的文件夹内,运行如下命令:

照着例子学习 protobuf-lua

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

回车。

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

-- Generated By protoc-gen-lua Do not Edit
local 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 =
PERSON_PHONENUMBER_NUMBER_FIELD.index =
PERSON_PHONENUMBER_NUMBER_FIELD.label =
PERSON_PHONENUMBER_NUMBER_FIELD.has_default_value = false
PERSON_PHONENUMBER_NUMBER_FIELD.default_value = ""
PERSON_PHONENUMBER_NUMBER_FIELD.type =
PERSON_PHONENUMBER_NUMBER_FIELD.cpp_type = PERSON_PHONENUMBER_TYPE_FIELD.name = "type"
PERSON_PHONENUMBER_TYPE_FIELD.full_name = ".tutorial.Person.PhoneNumber.type"
PERSON_PHONENUMBER_TYPE_FIELD.number =
PERSON_PHONENUMBER_TYPE_FIELD.index =
PERSON_PHONENUMBER_TYPE_FIELD.label =
PERSON_PHONENUMBER_TYPE_FIELD.has_default_value = true
PERSON_PHONENUMBER_TYPE_FIELD.default_value = HOME
PERSON_PHONENUMBER_TYPE_FIELD.enum_type = _PERSON_PHONETYPE
PERSON_PHONENUMBER_TYPE_FIELD.type =
PERSON_PHONENUMBER_TYPE_FIELD.cpp_type = PERSON_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 = false
PERSON_PHONENUMBER.extensions = {}
PERSON_PHONENUMBER.containing_type = PERSON
PERSON_PHONETYPE_MOBILE_ENUM.name = "MOBILE"
PERSON_PHONETYPE_MOBILE_ENUM.index =
PERSON_PHONETYPE_MOBILE_ENUM.number =
PERSON_PHONETYPE_HOME_ENUM.name = "HOME"
PERSON_PHONETYPE_HOME_ENUM.index =
PERSON_PHONETYPE_HOME_ENUM.number =
PERSON_PHONETYPE_WORK_ENUM.name = "WORK"
PERSON_PHONETYPE_WORK_ENUM.index =
PERSON_PHONETYPE_WORK_ENUM.number =
PERSON_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 =
PERSON_NAME_FIELD.index =
PERSON_NAME_FIELD.label =
PERSON_NAME_FIELD.has_default_value = false
PERSON_NAME_FIELD.default_value = ""
PERSON_NAME_FIELD.type =
PERSON_NAME_FIELD.cpp_type = PERSON_ID_FIELD.name = "id"
PERSON_ID_FIELD.full_name = ".tutorial.Person.id"
PERSON_ID_FIELD.number =
PERSON_ID_FIELD.index =
PERSON_ID_FIELD.label =
PERSON_ID_FIELD.has_default_value = false
PERSON_ID_FIELD.default_value =
PERSON_ID_FIELD.type =
PERSON_ID_FIELD.cpp_type = PERSON_EMAIL_FIELD.name = "email"
PERSON_EMAIL_FIELD.full_name = ".tutorial.Person.email"
PERSON_EMAIL_FIELD.number =
PERSON_EMAIL_FIELD.index =
PERSON_EMAIL_FIELD.label =
PERSON_EMAIL_FIELD.has_default_value = false
PERSON_EMAIL_FIELD.default_value = ""
PERSON_EMAIL_FIELD.type =
PERSON_EMAIL_FIELD.cpp_type = PERSON_PHONE_FIELD.name = "phone"
PERSON_PHONE_FIELD.full_name = ".tutorial.Person.phone"
PERSON_PHONE_FIELD.number =
PERSON_PHONE_FIELD.index =
PERSON_PHONE_FIELD.label =
PERSON_PHONE_FIELD.has_default_value = false
PERSON_PHONE_FIELD.default_value = {}
PERSON_PHONE_FIELD.message_type = PERSON_PHONENUMBER
PERSON_PHONE_FIELD.type =
PERSON_PHONE_FIELD.cpp_type = PERSON.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 = false
PERSON.extensions = {}
ADDRESSBOOK_PERSON_FIELD.name = "person"
ADDRESSBOOK_PERSON_FIELD.full_name = ".tutorial.AddressBook.person"
ADDRESSBOOK_PERSON_FIELD.number =
ADDRESSBOOK_PERSON_FIELD.index =
ADDRESSBOOK_PERSON_FIELD.label =
ADDRESSBOOK_PERSON_FIELD.has_default_value = false
ADDRESSBOOK_PERSON_FIELD.default_value = {}
ADDRESSBOOK_PERSON_FIELD.message_type = PERSON
ADDRESSBOOK_PERSON_FIELD.type =
ADDRESSBOOK_PERSON_FIELD.cpp_type = ADDRESSBOOK.name = "AddressBook"
ADDRESSBOOK.full_name = ".tutorial.AddressBook"
ADDRESSBOOK.nested_types = {}
ADDRESSBOOK.enum_types = {}
ADDRESSBOOK.fields = {ADDRESSBOOK_PERSON_FIELD}
ADDRESSBOOK.is_extendable = false
ADDRESSBOOK.extensions = {} AddressBook = protobuf.Message(ADDRESSBOOK)
Person = protobuf.Message(PERSON)
Person.PhoneNumber = protobuf.Message(PERSON_PHONENUMBER)

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