protobuf 在lua中使用(3)

proto文件导出lua文件工具参考

http://download.csdn.net/detail/c471961491/9414388


通过前两篇的介绍已经能搭建一个lua的probuf使用结构了

前两篇博文地址

http://blog.csdn.net/daydayup_chf/article/details/49904785

http://blog.csdn.net/daydayup_chf/article/details/49994023


然而在使用过程中,碰到了以下情况


TestA.proto

package Google;

message TestA
{
	required uint32 id   = 1;	
	optional uint32 name   = 2;
	required string year    = 3;
	optional uint32 nettype   = 4;	
}



TestB.proto

package Google;

message TestB
{
	required uint32 aa   = 1;	
	optional uint32 bb   = 2;
	required string cc    = 3;	
}



TestC.proto

import "TestA.proto";
import "TestB.proto";

package Google;

message TestC
{
	required TestA	testa  = 1;	
	required TestB testb = 2; 
}

message TestD
{
	required uint32	dddd  = 1;	
}

message TestE
{
	required TestD testd  = 1;	
}


当使用 message TestC 时出现错误

具体原因是报message_type是个空值


改正这个的处理方法是对导出的lua文件做修改


TestA.lua

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


TESTA = protobuf.Descriptor(); --此处去掉local
local TESTA_ID_FIELD = protobuf.FieldDescriptor();
local TESTA_NAME_FIELD = protobuf.FieldDescriptor();
local TESTA_YEAR_FIELD = protobuf.FieldDescriptor();
local TESTA_NETTYPE_FIELD = protobuf.FieldDescriptor();

TESTA_ID_FIELD.name = "id"
TESTA_ID_FIELD.full_name = ".Google.TestA.id"
TESTA_ID_FIELD.number = 1
TESTA_ID_FIELD.index = 0
TESTA_ID_FIELD.label = 2
TESTA_ID_FIELD.has_default_value = false
TESTA_ID_FIELD.default_value = 0
TESTA_ID_FIELD.type = 13
TESTA_ID_FIELD.cpp_type = 3

TESTA_NAME_FIELD.name = "name"
TESTA_NAME_FIELD.full_name = ".Google.TestA.name"
TESTA_NAME_FIELD.number = 2
TESTA_NAME_FIELD.index = 1
TESTA_NAME_FIELD.label = 1
TESTA_NAME_FIELD.has_default_value = false
TESTA_NAME_FIELD.default_value = 0
TESTA_NAME_FIELD.type = 13
TESTA_NAME_FIELD.cpp_type = 3

TESTA_YEAR_FIELD.name = "year"
TESTA_YEAR_FIELD.full_name = ".Google.TestA.year"
TESTA_YEAR_FIELD.number = 3
TESTA_YEAR_FIELD.index = 2
TESTA_YEAR_FIELD.label = 2
TESTA_YEAR_FIELD.has_default_value = false
TESTA_YEAR_FIELD.default_value = ""
TESTA_YEAR_FIELD.type = 9
TESTA_YEAR_FIELD.cpp_type = 9

TESTA_NETTYPE_FIELD.name = "nettype"
TESTA_NETTYPE_FIELD.full_name = ".Google.TestA.nettype"
TESTA_NETTYPE_FIELD.number = 4
TESTA_NETTYPE_FIELD.index = 3
TESTA_NETTYPE_FIELD.label = 1
TESTA_NETTYPE_FIELD.has_default_value = false
TESTA_NETTYPE_FIELD.default_value = 0
TESTA_NETTYPE_FIELD.type = 13
TESTA_NETTYPE_FIELD.cpp_type = 3

TESTA.name = "TestA"
TESTA.full_name = ".Google.TestA"
TESTA.nested_types = {}
TESTA.enum_types = {}
TESTA.fields = {TESTA_ID_FIELD, TESTA_NAME_FIELD, TESTA_YEAR_FIELD, TESTA_NETTYPE_FIELD}
TESTA.is_extendable = false
TESTA.extensions = {}

TestA = protobuf.Message(TESTA)



TestB.lua

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


TESTB = protobuf.Descriptor(); --去掉local
local TESTB_AA_FIELD = protobuf.FieldDescriptor();
local TESTB_BB_FIELD = protobuf.FieldDescriptor();
local TESTB_CC_FIELD = protobuf.FieldDescriptor();

TESTB_AA_FIELD.name = "aa"
TESTB_AA_FIELD.full_name = ".Google.TestB.aa"
TESTB_AA_FIELD.number = 1
TESTB_AA_FIELD.index = 0
TESTB_AA_FIELD.label = 2
TESTB_AA_FIELD.has_default_value = false
TESTB_AA_FIELD.default_value = 0
TESTB_AA_FIELD.type = 13
TESTB_AA_FIELD.cpp_type = 3

TESTB_BB_FIELD.name = "bb"
TESTB_BB_FIELD.full_name = ".Google.TestB.bb"
TESTB_BB_FIELD.number = 2
TESTB_BB_FIELD.index = 1
TESTB_BB_FIELD.label = 1
TESTB_BB_FIELD.has_default_value = false
TESTB_BB_FIELD.default_value = 0
TESTB_BB_FIELD.type = 13
TESTB_BB_FIELD.cpp_type = 3

TESTB_CC_FIELD.name = "cc"
TESTB_CC_FIELD.full_name = ".Google.TestB.cc"
TESTB_CC_FIELD.number = 3
TESTB_CC_FIELD.index = 2
TESTB_CC_FIELD.label = 2
TESTB_CC_FIELD.has_default_value = false
TESTB_CC_FIELD.default_value = ""
TESTB_CC_FIELD.type = 9
TESTB_CC_FIELD.cpp_type = 9

TESTB.name = "TestB"
TESTB.full_name = ".Google.TestB"
TESTB.nested_types = {}
TESTB.enum_types = {}
TESTB.fields = {TESTB_AA_FIELD, TESTB_BB_FIELD, TESTB_CC_FIELD}
TESTB.is_extendable = false
TESTB.extensions = {}

TestB = protobuf.Message(TESTB)


TestC.lua

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


local TESTC = protobuf.Descriptor();
local TESTC_TESTA_FIELD = protobuf.FieldDescriptor();
local TESTC_TESTB_FIELD = protobuf.FieldDescriptor();
local TESTD = protobuf.Descriptor();
local TESTD_DDDD_FIELD = protobuf.FieldDescriptor();
local TESTE = protobuf.Descriptor();
local TESTE_TESTD_FIELD = protobuf.FieldDescriptor();

TESTC_TESTA_FIELD.name = "testa"
TESTC_TESTA_FIELD.full_name = ".Google.TestC.testa"
TESTC_TESTA_FIELD.number = 1
TESTC_TESTA_FIELD.index = 0
TESTC_TESTA_FIELD.label = 2
TESTC_TESTA_FIELD.has_default_value = false
TESTC_TESTA_FIELD.default_value = nil
TESTC_TESTA_FIELD.message_type = TestA_pb.TESTA  --改成这样
TESTC_TESTA_FIELD.type = 11
TESTC_TESTA_FIELD.cpp_type = 10

TESTC_TESTB_FIELD.name = "testb"
TESTC_TESTB_FIELD.full_name = ".Google.TestC.testb"
TESTC_TESTB_FIELD.number = 2
TESTC_TESTB_FIELD.index = 1
TESTC_TESTB_FIELD.label = 2
TESTC_TESTB_FIELD.has_default_value = false
TESTC_TESTB_FIELD.default_value = nil
TESTC_TESTB_FIELD.message_type = TestB_pb.TESTB  --改成这样
TESTC_TESTB_FIELD.type = 11
TESTC_TESTB_FIELD.cpp_type = 10

TESTC.name = "TestC"
TESTC.full_name = ".Google.TestC"
TESTC.nested_types = {}
TESTC.enum_types = {}
TESTC.fields = {TESTC_TESTA_FIELD, TESTC_TESTB_FIELD}
TESTC.is_extendable = false
TESTC.extensions = {}
TESTD_DDDD_FIELD.name = "dddd"
TESTD_DDDD_FIELD.full_name = ".Google.TestD.dddd"
TESTD_DDDD_FIELD.number = 1
TESTD_DDDD_FIELD.index = 0
TESTD_DDDD_FIELD.label = 2
TESTD_DDDD_FIELD.has_default_value = false
TESTD_DDDD_FIELD.default_value = 0
TESTD_DDDD_FIELD.type = 13
TESTD_DDDD_FIELD.cpp_type = 3

TESTD.name = "TestD"
TESTD.full_name = ".Google.TestD"
TESTD.nested_types = {}
TESTD.enum_types = {}
TESTD.fields = {TESTD_DDDD_FIELD}
TESTD.is_extendable = false
TESTD.extensions = {}
TESTE_TESTD_FIELD.name = "testd"
TESTE_TESTD_FIELD.full_name = ".Google.TestE.testd"
TESTE_TESTD_FIELD.number = 1
TESTE_TESTD_FIELD.index = 0
TESTE_TESTD_FIELD.label = 2
TESTE_TESTD_FIELD.has_default_value = false
TESTE_TESTD_FIELD.default_value = nil
TESTE_TESTD_FIELD.message_type = TESTD
TESTE_TESTD_FIELD.type = 11
TESTE_TESTD_FIELD.cpp_type = 10

TESTE.name = "TestE"
TESTE.full_name = ".Google.TestE"
TESTE.nested_types = {}
TESTE.enum_types = {}
TESTE.fields = {TESTE_TESTD_FIELD}
TESTE.is_extendable = false
TESTE.extensions = {}

TestC = protobuf.Message(TESTC)
TestD = protobuf.Message(TESTD)
TestE = protobuf.Message(TESTE)


修改完成之后保存,就能调用 message TestC 

你可能感兴趣的:(工具,cocos2dx,lua)