新版步移
ProtobufGenBatTool V3 更方便、高效
工具前身C# Protocol buffer 批处理工具
工具一名称:ProtobufGenBatTool v2
因项目需要使用到Protocol buffer,所以最近学习了一下protogen.exe生成cs代码的方法 但是发现protogen.exe参数没有设置输出的cs目录,所以重新写了个工具处理,解决这个问题 另外该工具还处理生成对应协议ID与对应的message类的映射类输出,可在.proto文件中的message定定义:message (pid) = N;来指定协议ID,N就是协议ID,该字段最终不会生出在.cs文件中
有时间的话,后续会对protobuf多了解开发功能。 后面有空再看看:protobuf的extend, extension, import “other.proto”的使用,还有*.protobin的文件结构的解析 现在这个工具先凑合着用
参数说明- -protoc_file 指定protoc.exe工具文件的路径(相对目录或绝对目录都OK)
- -protogen_file 指定protogen.exe工具文件的路径(相对目录或绝对目录都OK)
- -proto_file_inputpath 指定包含.proto文件的一个或多的目录(相对目录或绝对目录都OK),多个目录以空格来分隔
- -cs_proto_file_outputpath 指定我们的cs文件的输出目录(相对目录或绝对目录都OK)
- -cs_pid_map_file_output_path 指定输出的协议与message映射类的路径(相对目录或绝对目录都OK)
- -cs_pid_map_cls_namespace 映射类的namespace
- -pause_on_complete是否在完成时暂停下来,等待任意按键后退出,默认值为true(true or false)
REM 单行
ProtobufGenBatTool\ProtobufGenBatTool.exe -protoc_file ProtobufTools\protoc.exe -protogen_file ProtobufTools\ProtoGen.exe -proto_file_inputpath p protos protos1 -cs_proto_file_outputpath com/net/protobuf/msg -cs_pid_map_file_output_path com/net/protobuf/protomap/ProtoMap.cs -cs_pid_map_cls_namespace com.net.protobuf.protomap
REM 多行
ProtobufGenBatTool\ProtobufGenBatTool.exe
-protoc_file ProtobufTools\protoc.exe
-protogen_file ProtobufTools\ProtoGen.exe
-proto_file_inputpath p protos protos1
-cs_proto_file_outputpath com/net/protobuf/msg
-cs_pid_map_file_output_path com/net/protobuf/protomap/ProtoMap.cs
-cs_pid_map_cls_namespace com.net.protobuf.protomap
代码、工具下载
压缩包内的Usage就是工具的使用范例 点击下载
DEMOMultiProtoDemoTest.proto
package com.net.protobuf.msg;
message MPDemoTest {
required int32 test1=1;
required int32 test2=2;
option (pid) = 11;
}
message MPDemoTest1 {
required int32 test1=1;
required int32 test2=2;
required string name=3;
option (pid) = 12;
}
message MPDemoTest2 {
required int32 test1=1;
required int32 test2=2;
required string name=3;
required int32 age=4;
option (pid) = 13;
}
bat.bat执行后的的dos命令行结果
执行后生成的MultiProtoDemoTest.cs文件(文件内容太长,就不放上来了)
执行后生成的ProtoMap.cs文件,注意MultiProtoDemoTest.proto中定义的每个message 的option (pid) = n;都在ProtoMap.cs有id与类一一注册对应使用
// Generated by the protocol buffer ProtobufPIDMapperTools. DO NOT EDIT!
using System;
using System.Collections.Generic;
namespace com.net.protobuf.protomap
{
public static class ProtoMap
{
public static readonly Dictionary _s_pMapper1 = new Dictionary();
public static readonly Dictionary _s_pMapper2 = new Dictionary();
static ProtoMap()
{
_s_pMapper1[ 1 ] = typeof(com.net.protobuf.msg.Data1);
_s_pMapper1[ 2 ] = typeof(com.net.protobuf.msg.MyData);
_s_pMapper1[ 3 ] = typeof(com.net.protobuf.msg.MyRequest);
_s_pMapper1[ 4 ] = typeof(com.net.protobuf.msg.MyResponse);
_s_pMapper1[ 5 ] = typeof(com.net.protobuf.msg.Test);
_s_pMapper1[ 6 ] = typeof(com.net.protobuf.msg.DemoData);
_s_pMapper1[ 7 ] = typeof(com.net.protobuf.msg.DemoMyData);
_s_pMapper1[ 8 ] = typeof(com.net.protobuf.msg.DemoMyRequest);
_s_pMapper1[ 9 ] = typeof(com.net.protobuf.msg.DemoMyResponse);
_s_pMapper1[ 10 ] = typeof(com.net.protobuf.msg.DemoTest);
_s_pMapper1[ 11 ] = typeof(com.net.protobuf.msg.MPDemoTest);
_s_pMapper1[ 12 ] = typeof(com.net.protobuf.msg.MPDemoTest1);
_s_pMapper1[ 13 ] = typeof(com.net.protobuf.msg.MPDemoTest2);
_s_pMapper2[ typeof(com.net.protobuf.msg.Data1) ] = 1;
_s_pMapper2[ typeof(com.net.protobuf.msg.MyData) ] = 2;
_s_pMapper2[ typeof(com.net.protobuf.msg.MyRequest) ] = 3;
_s_pMapper2[ typeof(com.net.protobuf.msg.MyResponse) ] = 4;
_s_pMapper2[ typeof(com.net.protobuf.msg.Test) ] = 5;
_s_pMapper2[ typeof(com.net.protobuf.msg.DemoData) ] = 6;
_s_pMapper2[ typeof(com.net.protobuf.msg.DemoMyData) ] = 7;
_s_pMapper2[ typeof(com.net.protobuf.msg.DemoMyRequest) ] = 8;
_s_pMapper2[ typeof(com.net.protobuf.msg.DemoMyResponse) ] = 9;
_s_pMapper2[ typeof(com.net.protobuf.msg.DemoTest) ] = 10;
_s_pMapper2[ typeof(com.net.protobuf.msg.MPDemoTest) ] = 11;
_s_pMapper2[ typeof(com.net.protobuf.msg.MPDemoTest1) ] = 12;
_s_pMapper2[ typeof(com.net.protobuf.msg.MPDemoTest2) ] = 13;
}
public static Type getType(int pid)
{
return _s_pMapper1[pid];
}
public static int getPID(Type type)
{
return _s_pMapper2[type];
}
}
}