照着例子学习 protobuf-lua
首先得下载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 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 = 1 PERSON_PHONENUMBER_NUMBER_FIELD.index = 0 PERSON_PHONENUMBER_NUMBER_FIELD.label = 2 PERSON_PHONENUMBER_NUMBER_FIELD.has_default_value = false PERSON_PHONENUMBER_NUMBER_FIELD.default_value = "" PERSON_PHONENUMBER_NUMBER_FIELD.type = 9 PERSON_PHONENUMBER_NUMBER_FIELD.cpp_type = 9 PERSON_PHONENUMBER_TYPE_FIELD.name = "type" PERSON_PHONENUMBER_TYPE_FIELD.full_name = ".tutorial.Person.PhoneNumber.type" PERSON_PHONENUMBER_TYPE_FIELD.number = 2 PERSON_PHONENUMBER_TYPE_FIELD.index = 1 PERSON_PHONENUMBER_TYPE_FIELD.label = 1 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 = 14 PERSON_PHONENUMBER_TYPE_FIELD.cpp_type = 8 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 = 0 PERSON_PHONETYPE_MOBILE_ENUM.number = 0 PERSON_PHONETYPE_HOME_ENUM.name = "HOME" PERSON_PHONETYPE_HOME_ENUM.index = 1 PERSON_PHONETYPE_HOME_ENUM.number = 1 PERSON_PHONETYPE_WORK_ENUM.name = "WORK" PERSON_PHONETYPE_WORK_ENUM.index = 2 PERSON_PHONETYPE_WORK_ENUM.number = 2 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 = 1 PERSON_NAME_FIELD.index = 0 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_ID_FIELD.name = "id" PERSON_ID_FIELD.full_name = ".tutorial.Person.id" PERSON_ID_FIELD.number = 2 PERSON_ID_FIELD.index = 1 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_EMAIL_FIELD.name = "email" PERSON_EMAIL_FIELD.full_name = ".tutorial.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_PHONE_FIELD.name = "phone" PERSON_PHONE_FIELD.full_name = ".tutorial.Person.phone" PERSON_PHONE_FIELD.number = 4 PERSON_PHONE_FIELD.index = 3 PERSON_PHONE_FIELD.label = 3 PERSON_PHONE_FIELD.has_default_value = false PERSON_PHONE_FIELD.default_value = {} PERSON_PHONE_FIELD.message_type = PERSON_PHONENUMBER PERSON_PHONE_FIELD.type = 11 PERSON_PHONE_FIELD.cpp_type = 10 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 = 1 ADDRESSBOOK_PERSON_FIELD.index = 0 ADDRESSBOOK_PERSON_FIELD.label = 3 ADDRESSBOOK_PERSON_FIELD.has_default_value = false ADDRESSBOOK_PERSON_FIELD.default_value = {} ADDRESSBOOK_PERSON_FIELD.message_type = PERSON ADDRESSBOOK_PERSON_FIELD.type = 11 ADDRESSBOOK_PERSON_FIELD.cpp_type = 10 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代码的,现在最要紧的是下班,呵呵。