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
}