C++中protobuf是個常用的序列化庫,網絡消息發送,消息解析都十分方便,xml可以干的,它都能干。但是它絕不僅僅是序列化庫。
簡單的說,protobuf給C++增加了C# attribute的功能。C++從此就有了元數據了!會c#的同學肯定明白了這句話的意義了。
一. protobuf用作配置文件:
protobuf提供了一種textformat的序列化格式,類似json格式,清晰易讀。比如一棵行為樹節點描述文件:
數據定義為:
message BehaviorNodeConf { required int32 type = 1; // 條件需要的參數 repeated int32 args = 2; // 包含多個子節點 repeated BehaviorNodeConf node = 3; }; message BehaviorTreeConf { // 行為樹類型: AI, ON_HIT, ON_HITTED ... required int32 type = 1; // 行為樹節點 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 } } }
以下兩行代碼即可解析這個配置文件:
BehaviorTreeConf conf; google::protobuf::TextFormat::ParseFromString(fileContent, &conf);
二. protobuf的反射用法
很多人都說C++很難做Orm,因為沒有反射等等,有了protobuf這一切都不是問題了,如下:
select操作:自動生成select語句,查詢一條記錄把它保存到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操作: 自動生成update語句,這段代碼是從我的單元測試里面摳出來的,大家明白意思就行了
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 類似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上面
四.游戲開發中如何利用protobuf的這功能呢?
1.策划使用protobuf作為配置文件,我可以在數據定義中設置某個字段的option為C#的哪個控件,
編輯器讀到這個數據定義就可以直接生成一個控件,因此可以根據數據定義生成編輯器給策划填寫數據。例如:
message BehaviorNodeConf { required int32 type = 1 [control = "textbox" max = 100 min = 0]; // 條件需要的參數 repeated int32 args = 2 [control = "textbox"]; };
2.再如: 技能可以施放buff,每個buff都應該有一個關聯的技能,那么策划填表的時候要填一個技能的proto表還要填一個buff的proto表,兩張表策划很容易就填漏了
我們可以加上一個約束,由編輯器去檢查:
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; }
編輯器填寫一個buff會去檢查是否有關聯這個buff的技能存在,填技能的時候如果填了buff也要強制配置相應的buff,這樣大大減少了配置錯誤,策划從此從填表錯誤中解脫出來。
策划爽了,程序員的爽還遠嗎? 他好我也好!
protobuf寫的一個簡易orm,目前實現了select和update,其它的后續增加,原理是一樣的
代碼地址: https://github.com/egametang/Egametang/tree/master/Cpp/Platform/Orm