一點(diǎn)小牢騷 cppblog和博客園是啥關(guān)系呢?同兩邊的人交流交流 難道我兩邊都要發(fā)一遍?
C++中protobuf是個(gè)常用的序列化庫(kù),網(wǎng)絡(luò)消息發(fā)送,消息解析都十分方便,xml可以干的,它都能干。但是它絕不僅僅是序列化庫(kù)。
簡(jiǎn)單的說(shuō),protobuf給C++增加了C# attribute的功能。C++從此就有了元數(shù)據(jù)了!會(huì)c#的同學(xué)肯定明白了這句話的意義了。
一. protobuf用作配置文件:
protobuf提供了一種textformat的序列化格式,類(lèi)似json格式,清晰易讀。比如一棵行為樹(shù)節(jié)點(diǎn)描述文件:
數(shù)據(jù)定義為:
message BehaviorNodeConf
{
required int32 type = 1;
// 條件需要的參數(shù)
repeated int32 args = 2;
// 包含多個(gè)子節(jié)點(diǎn)
repeated BehaviorNodeConf node = 3;
};
message BehaviorTreeConf
{
// 行為樹(shù)類(lèi)型: AI, ON_HIT, ON_HITTED ...
required int32 type = 1;
// 行為樹(shù)節(jié)點(diǎn)
required BehaviorNodeConf node = 2;
};配置文件為:
type: 5
node:
{
type: 1
node:
{
type: 101
args: 2
}
node:
{
type: 1
node:
{
type: 1001
args: 0
args: 100
}
node:
{
type: 1001
args: 1
args: -100
}
}
} 以下兩行代碼即可解析這個(gè)配置文件:BehaviorTreeConf conf;
google::protobuf::TextFormat::ParseFromString(fileContent, &conf);二. protobuf的反射用法
很多人都說(shuō)C++很難做Orm,因?yàn)闆](méi)有反射等等,有了protobuf這一切都不是問(wèn)題了,如下:
select操作:自動(dòng)生成select語(yǔ)句,查詢(xún)一條記錄把它保存到person變量里面
try
{
DbHelper dbHelper("tcp://127.0.0.1:3306", "root", "111111");
DbResultPtr result = dbHelper.Execute(
Select<Person>(Column("*")).
Where(Column("age") > 10)
);
auto person = boost::make_shared<Person>();
result->One(person);
ASSERT_EQ(26, person->age());
}
catch (const Exception& e)
{
} update操作: 自動(dòng)生成update語(yǔ)句,這段代碼是從我的單元測(cè)試?yán)锩鎿赋鰜?lái)的,大家明白意思就行了TEST_F(UpdateTest, Update_Where)
{
std::string expectedSql;
expectedSql =
"update Egametang.Person "
"set guid = 1, age = 18, comment = 'a good student!' "
"where age > 10";
Person person;
person.set_guid(1);
person.set_age(18);
person.set_comment("a good student!");
Update update(person);
update.Where(Column("age") > 10);
EXPECT_EQ(expectedSql, update.ToString());
} 三.protbuf 類(lèi)似c# attribute功能
看如下一段protobuf定義:import "google/protobuf/descriptor.proto";
extend google.protobuf.FileOptions
{
optional string my_file_option = 50000;
}
extend google.protobuf.MessageOptions
{
optional int32 my_message_option = 50001;
}
extend google.protobuf.FieldOptions
{
optional float my_field_option = 50002;
}
extend google.protobuf.EnumOptions
{
optional bool my_enum_option = 50003;
}
extend google.protobuf.EnumValueOptions
{
optional uint32 my_enum_value_option = 50004;
}
extend google.protobuf.ServiceOptions
{
optional MyEnum my_service_option = 50005;
}
extend google.protobuf.MethodOptions
{
optional MyMessage my_method_option = 50006;
}
option (my_file_option) = "Hello world!";
message MyMessage
{
option (my_message_option) = 1234;
optional int32 foo = 1 [(my_field_option) = 4.5];
optional string bar = 2;
}
enum MyEnum
{
option (my_enum_option) = true;
FOO = 1 [(my_enum_value_option) = 321];
BAR = 2;
}
message RequestType {}
message ResponseType {}
service MyService
{
option (my_service_option) = FOO;
rpc MyMethod(RequestType) returns(ResponseType)
{
// Note: my_method_option has type MyMessage. We can set each field
// within it using a separate "option" line.
option (my_method_option).foo = 567;
option (my_method_option).bar = "Some string";
}
}protobuf中的option就是C#中的attribute,option同樣可以放在message(同c#的class) service(同c#的方法) 以及message的field上面
四.游戲開(kāi)發(fā)中如何利用protobuf的這功能呢?
1.策劃使用protobuf作為配置文件,我可以在數(shù)據(jù)定義中設(shè)置某個(gè)字段的option為C#的哪個(gè)控件,
編輯器讀到這個(gè)數(shù)據(jù)定義就可以直接生成一個(gè)控件,因此可以根據(jù)數(shù)據(jù)定義生成編輯器給策劃填寫(xiě)數(shù)據(jù)。例如:
message BehaviorNodeConf
{
required int32 type = 1 [control = "textbox" max = 100 min = 0];
// 條件需要的參數(shù)
repeated int32 args = 2 [control = "textbox"];
}; 2.再如: 技能可以施放buff,每個(gè)buff都應(yīng)該有一個(gè)關(guān)聯(lián)的技能,那么策劃填表的時(shí)候要填一個(gè)技能的proto表還要填一個(gè)buff的proto表,兩張表策劃很容易就填漏了我們可以加上一個(gè)約束,由編輯器去檢查:message Spell
{
required int32 id = 1;
optional int32 buffId = 2 [ref = "Buff.id"];
}
message Buff
{
required int32 id = 1 [ref = "Spell.buffId"];
optional int32 time = 2;
}編輯器填寫(xiě)一個(gè)buff會(huì)去檢查是否有關(guān)聯(lián)這個(gè)buff的技能存在,填技能的時(shí)候如果填了buff也要強(qiáng)制配置相應(yīng)的buff,這樣大大減少了配置錯(cuò)誤,策劃從此從填表錯(cuò)誤中解脫出來(lái)。 策劃爽了,程序員的爽還遠(yuǎn)嗎? 他好我也好! protobuf寫(xiě)的一個(gè)簡(jiǎn)易o(hù)rm,目前實(shí)現(xiàn)了select和update,其它的后續(xù)增加,原理是一樣的代碼地址: https://github.com/egametang/Egametang/tree/master/Cpp/Platform/Orm