解析ET6框架的proto生成及优化

1.介绍

protocol buffers 是一种语言无关、平台无关、可扩展的序列化结构数据的方法,它可用于(数据)通信协议、数据存储等。

2.解析proto生成原理

1.Proto2CS.cs

 将proto文件生成cs文件供c#使用

        public static void Proto2CS()
        {
            msgOpcode.Clear();
            //生成cs脚本
            Proto2CS("ET", "../Proto/InnerMessage.proto", serverMessagePath, "InnerOpcode", 10000);
            //生成opcode脚本
            GenerateOpcode("ET", "InnerOpcode", serverMessagePath);

            Proto2CS("ET", "../Proto/OuterMessage.proto", serverMessagePath, "OuterOpcode", 20000);
            GenerateOpcode("ET", "OuterOpcode", serverMessagePath);

            Proto2CS("ET", "../Proto/OuterMessage.proto", clientMessagePath, "OuterOpcode", 20000);
            GenerateOpcode("ET", "OuterOpcode", clientMessagePath);          
        }

3.优化

现在框架所有的proto都在一个proto中,这在开发过程中极为不便,所以我重写了proto2cs方法,就可以将proto拆分无数个。

        private const string protoSourcePath = "../Proto/";//proto目录
        private const int opcodeIncrement = 200;//每个功能的opcode增量
        public static void Proto2CS()
        {
            //读取proto下所有文件
            DirectoryInfo dir = new DirectoryInfo(protoSourcePath);
            FileInfo[] files = dir.GetFiles();
            int count = 0;
            foreach (FileInfo file in files)
            {               
                if (file.Name.Contains("Inner"))
                {
                    //生成cs脚本
                    Proto2CS("ET", protoSourcePath + file.Name, serverMessagePath, "InnerOpcode", 10000);
                    //生成opcode脚本
                    GenerateOpcode("ET", "InnerOpcode", serverMessagePath);
                    Log.Console(file.Name);
                }
                else if(file.Name.Contains("Outer"))
                {
                    string opcodeName = Path.GetFileNameWithoutExtension(file.Name) + "Opcode";
                    Proto2CS("ET", protoSourcePath + file.Name, serverMessagePath, opcodeName, 20000 + count * opcodeIncrement);
                    GenerateOpcode("ET", opcodeName, serverMessagePath);
                    Proto2CS("ET", protoSourcePath + file.Name, clientMessagePath, opcodeName, 20000 + count * opcodeIncrement);
                    GenerateOpcode("ET", opcodeName, clientMessagePath);
                    count++;
                    Log.Console(file.Name);
                }
            }
        }

4.使用

1.//开头为注释,//ResponseType开头要如果是有返回的要填写,message后面的//为继承接口,必须是ET框架实现的接口

//注释
//ResponseType M2C_TestResponse
message C2M_TestRequest // IActorLocationRequest
{
	int32 RpcId = 90; //测试90
	int64 ActorId = 93;//测试93
	string request = 1;//测试1
}

message M2C_TestResponse // IActorLocationResponse
{
	int32 RpcId = 90;
	int32 Error = 91;
	string Message = 92;
	string response = 1;
}

2.创建的proto文件的名字要包含Inner和Outer才会被生成,Inner只会生成服务端,Outer会双端都生成

3.运行win_startProtoExport.bat生成cs文件

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萧寒大大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值