战魂小筑

讨论群:309800774 知乎关注:http://zhihu.com/people/sunicdavy 开源项目:https://github.com/davyxu

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  257 随笔 :: 0 文章 :: 506 评论 :: 0 Trackbacks

protobuf就是为懒人而造的工具, 啥协议, 啥配置文件, 统统定义proto, 解析就ok, 非常方便

 

文本格式的解析错误不能使用捕获错误来获取, 因此,我们需要使用自定义的错误收集器进行收集, 看代码:

#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <google/protobuf/io/tokenizer.h>
 
class PBTextErrorCollector : public google::protobuf::io::ErrorCollector
{
public:
    PBTextErrorCollector( const std::string& FileName )
        : mFileName( FileName )
    {
 
    }
 
    virtual void AddError(int line, int column, const string& message)
    {
        CCLog("%s(%d:%d) %s ", mFileName.c_str(), line, column, message.c_str() );
    }
 
    virtual void AddWarning(int line, int column, const string& message) 
    {
        CCLog("%s(%d:%d) %s ", mFileName.c_str(), line, column, message.c_str() );
    }
 
private:
    std::string mFileName;
};
 

解析代码

google::protobuf::TextFormat::Parser P;        
    P.RecordErrorsTo( &PEC );
    P.Parse( &AIS, &AF );

 

另外: 文本格式的注释使用unix shell风格: 以#开头

下面是我的文本格式的配置文件

 

AnchorPointX: 0.5
AnchorPointY: 0

SpriteScale: 2

ComponentName: "ActorActionManager"
ComponentName: "ActorFrameEventDispatcher"
#ComponentName: "SoundFXController"
ComponentName: "RoleDeltaMoveController"
ComponentName: "RoleBehaviorDirector"

InitAction: AA_Idle

Animations 
{
  AnimationName: "mai_idle"
  AnimationInterval: 0.067
}
posted on 2013-07-09 18:29 战魂小筑 阅读(6846) 评论(2)  编辑 收藏 引用 所属分类: 脚本技术游戏开发技术C++/ 编程语言

评论

# re: Google Protocolbuf 文本格式的解析 2013-07-10 10:33 永遇乐
很好,学习了。  回复  更多评论
  

# re: Google Protocolbuf 文本格式的解析 2013-07-23 17:02 tb
这工具好啊  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理