2012年4月12日

一点小牢骚 cppblog和博客园是啥关系呢?同两边的人交流交流 难道我两边都要发一遍?

      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

posted @ 2012-04-12 12:09 唐诗 阅读(2634) | 评论 (1)编辑 收藏

仅列出标题