68.Protobuf协议生成

68.消息处理-第三方协议生成工具Protobuf-协议生成


68.1 知识点

利用 protoc.exe 编译器生成脚本文件

  • 打开 cmd 窗口
  • 进入 protoc.exe 所在文件夹(也可以直接将 exe 文件拖入 cmd 窗口中)
  • 输入转换指令
protoc.exe -I=配置路径 --csharp_out=输出路径 配置文件名
  • 注意:路径不要有中文和特殊符号,避免生成失败
  • 生成的类名会和配置文件名一样,只是首字母大写,将其拷贝到 Unity 中使用
  • 有报错会告诉在哪一行 比如上节课的
//required 在proto2才能使用 给testF去掉required修饰符
float testF = 1; //C# - float
reserved "testInt3233333";//忘记加双引号
  • 对上节课的test和test2都执行生成协议

测试生成对象是否能使用

TestMsg testMsg = new TestMsg();
testMsg.TestBool = true;
// 对应的和 List 以及 Dictionary 使用方式一样的 数组和字典对象
testMsg.ListInt.Add(1);
print(testMsg.ListInt[0]);
testMsg.TestMap.Add(1, "韬老狮");
print(testMsg.TestMap[1]);

// 枚举
testMsg.TestEnum = TestEnum.Boss;
// 内部枚举
testMsg.TestEnum2 = TestMsg.Types.TestEnum2.Boss;

// 其它类对象
testMsg.TestMsg2 = new TestMsg2();
testMsg.TestMsg2.TestInt32 = 99;
// 其它内部类对象
testMsg.TestMsg3 = new TestMsg.Types.TestMsg3();
testMsg.TestMsg3.TestInt32 = 55;
// 在另一个生成的脚本当中的类 如果命名空间不同 需要命名空间点出来使用
testMsg.TestHeart = new GameSystemTest.HeartMsg();

总结

Protobuf 通过配置生成脚本文件,主要使用的就是 protoc.exe 可执行文件。我们需要记住对应的生成指令。


68.2 知识点代码

Lesson68_消息处理_第三方协议生成工具Protobuf_协议生成

using GamePlayerTest;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lesson68_消息处理_第三方协议生成工具Protobuf_协议生成 : MonoBehaviour
{
    void Start()
    {
        #region 知识点一 利用protoc.exe编译器生成脚本文件

        //1.打开cmd窗口
        //2.进入protoc.exe所在文件夹(也可以直接将exe文件拖入cmd窗口中)
        //3.输入转换指令
        //protoc.exe -I=配置路径 --csharp_out=输出路径 配置文件名
        //有报错会告诉在哪一行 比如上节课的
        //required 在proto2才能使用
        //float testF = 1; //C# - float
        //reserved "testInt3233333";忘记加双引号


        //注意:路径不要有中文和特殊符号,避免生成失败

        //生成的类名会和配置文件名一样,只是首字母大写,把他拷贝到Unity中来
        #endregion

        #region 知识点二 测试生成对象是否能使用

        TestMsg testMsg = new TestMsg();
        testMsg.TestBool = true;
        //对应的和List以及Dictionary使用方式一样的 数组和字典对象
        testMsg.ListInt.Add(1);
        print(testMsg.ListInt[0]);
        testMsg.TestMap.Add(1, "韬老狮");
        print(testMsg.TestMap[1]);

        //枚举
        testMsg.TestEnum = TestEnum.Boss;
        //内部枚举
        testMsg.TestEnum2 = TestMsg.Types.TestEnum2.Boss;

        //其它类对象
        testMsg.TestMsg2 = new TestMsg2();
        testMsg.TestMsg2.TestInt32 = 99;
        //其它内部类对象
        testMsg.TestMsg3 = new TestMsg.Types.TestMsg3();
        testMsg.TestMsg3.TestInt32 = 55;
        //在另一个生成的脚本当中的类 如果命名空间不同 需要命名空间点出来使用
        testMsg.TestHeart = new GameSystemTest.HeartMsg();

        #endregion

        #region 总结

        //Protobuf 通过配置生成脚本文件
        //主要使用的就是 protoc.exe可执行文件
        //我们需要记住对应的生成指令

        #endregion
    }
}

Test

// <auto-generated>
//     Generated by the protocol buffer compiler.  DO NOT EDIT!
//     source: test.proto
// </auto-generated>
#pragma warning disable 1591, 0612, 3021, 8981
#region Designer generated code

using pb = global::Google.Protobuf;
using pbc = global::Google.Protobuf.Collections;
using pbr = global::Google.Protobuf.Reflection;
using scg = global::System.Collections.Generic;
namespace GamePlayerTest {

  /// <summary>Holder for reflection information generated from test.proto</summary>
  public static partial class TestReflection {

    #region Descriptor
    /// <summary>File descriptor for test.proto</summary>
    public static pbr::FileDescriptor Descriptor {
      get { return descriptor; }
    }
    private static pbr::FileDescriptor descriptor;

    static TestReflection() {
      byte[] descriptorData = global::System.Convert.FromBase64String(
          string.Concat(
            "Cgp0ZXN0LnByb3RvEg5HYW1lUGxheWVyVGVzdBoLdGVzdDIucHJvdG8ijQYK",
            "B1Rlc3RNc2cSDQoFdGVzdEYYASABKAISEgoFdGVzdEQYAiABKAFIAIgBARIR",
            "Cgl0ZXN0SW50MzIYAyABKAUSEQoJdGVzdEludDY0GAQgASgDEhIKCnRlc3RT",
            "SW50MzIYBSABKBESEgoKdGVzdFNJbnQ2NBgGIAEoEhIQCgh0ZXN0VUludBgH",
            "IAEoDRIRCgl0ZXN0VUxvbmcYCCABKAQSEwoLdGVzdEZpeGVkMzIYCSABKAcS",
            "EwoLdGVzdEZpeGVkNjQYCiABKAYSFAoMdGVzdFNGaXhlZDMyGAsgASgPEhQK",
            "DHRlc3RTRml4ZWQ2NBgMIAEoEBIQCgh0ZXN0Qm9vbBgNIAEoCBIPCgd0ZXN0",
            "U3RyGA4gASgJEhEKCXRlc3RCeXRlcxgPIAEoDBIPCgdsaXN0SW50GBAgAygF",
            "EjUKB3Rlc3RNYXAYESADKAsyJC5HYW1lUGxheWVyVGVzdC5UZXN0TXNnLlRl",
            "c3RNYXBFbnRyeRIqCgh0ZXN0RW51bRgSIAEoDjIYLkdhbWVQbGF5ZXJUZXN0",
            "LlRlc3RFbnVtEioKCHRlc3RNc2cyGBMgASgLMhguR2FtZVBsYXllclRlc3Qu",
            "VGVzdE1zZzISMgoIdGVzdE1zZzMYFCABKAsyIC5HYW1lUGxheWVyVGVzdC5U",
            "ZXN0TXNnLlRlc3RNc2czEjQKCXRlc3RFbnVtMhgVIAEoDjIhLkdhbWVQbGF5",
            "ZXJUZXN0LlRlc3RNc2cuVGVzdEVudW0yEhcKD3Rlc3RCb29sMjEyMzEyMxgX",
            "IAEoCBIrCgl0ZXN0SGVhcnQYGCABKAsyGC5HYW1lU3lzdGVtVGVzdC5IZWFy",
            "dE1zZxouCgxUZXN0TWFwRW50cnkSCwoDa2V5GAEgASgFEg0KBXZhbHVlGAIg",
            "ASgJOgI4ARodCghUZXN0TXNnMxIRCgl0ZXN0SW50MzIYASABKAUiIQoJVGVz",
            "dEVudW0yEgoKBk5PUk1BTBAAEggKBEJPU1MQAUIICgZfdGVzdERKBAgWEBdS",
            "DnRlc3RJbnQzMjMzMzMzIh0KCFRlc3RNc2cyEhEKCXRlc3RJbnQzMhgBIAEo",
            "BSogCghUZXN0RW51bRIKCgZOT1JNQUwQABIICgRCT1NTEAViBnByb3RvMw=="));
      descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
          new pbr::FileDescriptor[] { global::GameSystemTest.Test2Reflection.Descriptor, },
          new pbr::GeneratedClrTypeInfo(new[] {typeof(global::GamePlayerTest.TestEnum), }, null, new pbr::GeneratedClrTypeInfo[] {
            new pbr::GeneratedClrTypeInfo(typeof(global::GamePlayerTest.TestMsg), global::GamePlayerTest.TestMsg.Parser, new[]{ "TestF", "TestD", "TestInt32", "TestInt64", "TestSInt32", "TestSInt64", "TestUInt", "TestULong", "TestFixed32", "TestFixed64", "TestSFixed32", "TestSFixed64", "TestBool", "TestStr", "TestBytes", "ListInt", "TestMap", "TestEnum", "TestMsg2", "TestMsg3", "TestEnum2", "TestBool2123123", "TestHeart" }, new[]{ "TestD" }, new[]{ typeof(global::GamePlayerTest.TestMsg.Types.TestEnum2) }, null, new pbr::GeneratedClrTypeInfo[] { null, new pbr::GeneratedClrTypeInfo(typeof(global::GamePlayerTest.TestMsg.Types.TestMsg3), global::GamePlayerTest.TestMsg.Types.TestMsg3.Parser, new[]{ "TestInt32" }, null, null, null, null)}),
            new pbr::GeneratedClrTypeInfo(typeof(global::GamePlayerTest.TestMsg2), global::GamePlayerTest.TestMsg2.Parser, new[]{ "TestInt32" }, null, null, null, null)
          }));
    }
    #endregion

  }
  #region Enums
  /// <summary>
  ///枚举的声明
  /// </summary>
  public enum TestEnum {
    /// <summary>
    ///第一个常量必须映射到0
    /// </summary>
    [pbr::OriginalName("NORMAL")] Normal = 0,
    [pbr::OriginalName("BOSS")] Boss = 5,
  }

  #endregion

  #region Messages
  /// <summary>
  ///规则四:消息类
  /// </summary>
  public sealed partial class TestMsg : pb::IMessage<TestMsg>
  #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
      , pb::IBufferMessage
  #endif
  {
    private static readonly pb::MessageParser<TestMsg> _parser = new pb::MessageParser<TestMsg>(() => new TestMsg());
    private pb::UnknownFieldSet _unknownFields;
    private int _hasBits0;
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public static pb::MessageParser<TestMsg> Parser { get { return _parser; } }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public static pbr::MessageDescriptor Descriptor {
      get { return global::GamePlayerTest.TestReflection.Descriptor.MessageTypes[0]; }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    pbr::MessageDescriptor pb::IMessage.Descriptor {
      get { return Descriptor; }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public TestMsg() {
      OnConstruction();
    }

    partial void OnConstruction();

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public TestMsg(TestMsg other) : this() {
      _hasBits0 = other._hasBits0;
      testF_ = other.testF_;
      testD_ = other.testD_;
      testInt32_ = other.testInt32_;
      testInt64_ = other.testInt64_;
      testSInt32_ = other.testSInt32_;
      testSInt64_ = other.testSInt64_;
      testUInt_ = other.testUInt_;
      testULong_ = other.testULong_;
      testFixed32_ = other.testFixed32_;
      testFixed64_ = other.testFixed64_;
      testSFixed32_ = other.testSFixed32_;
      testSFixed64_ = other.testSFixed64_;
      testBool_ = other.testBool_;
      testStr_ = other.testStr_;
      testBytes_ = other.testBytes_;
      listInt_ = other.listInt_.Clone();
      testMap_ = other.testMap_.Clone();
      testEnum_ = other.testEnum_;
      testMsg2_ = other.testMsg2_ != null ? other.testMsg2_.Clone() : null;
      testMsg3_ = other.testMsg3_ != null ? other.testMsg3_.Clone() : null;
      testEnum2_ = other.testEnum2_;
      testBool2123123_ = other.testBool2123123_;
      testHeart_ = other.testHeart_ != null ? other.testHeart_.Clone() : null;
      _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public TestMsg Clone() {
      return new TestMsg(this);
    }

    /// <summary>Field number for the "testF" field.</summary>
    public const int TestFFieldNumber = 1;
    private float testF_;
    /// <summary>
    ///浮点数
    /// = 1 不代表默认值 而是代表唯一编号 方便我们进行序列化和反序列化的处理
    ///required 必须赋值的字段 proto2
    ///required 
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public float TestF {
      get { return testF_; }
      set {
        testF_ = value;
      }
    }

    /// <summary>Field number for the "testD" field.</summary>
    public const int TestDFieldNumber = 2;
    private double testD_;
    /// <summary>
    ///optional 可以不赋值的字段
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public double TestD {
      get { if ((_hasBits0 & 1) != 0) { return testD_; } else { return 0D; } }
      set {
        _hasBits0 |= 1;
        testD_ = value;
      }
    }
    /// <summary>Gets whether the "testD" field is set</summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public bool HasTestD {
      get { return (_hasBits0 & 1) != 0; }
    }
    /// <summary>Clears the value of the "testD" field</summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public void ClearTestD() {
      _hasBits0 &= ~1;
    }

    /// <summary>Field number for the "testInt32" field.</summary>
    public const int TestInt32FieldNumber = 3;
    private int testInt32_;
    /// <summary>
    ///变长编码
    ///所谓变长 就是会根据 数字的大小 来使用对应的字节数来存储  1 2 4 
    ///Protobuf帮助我们优化的部分 可以尽量少的使用字节数 来存储内容
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public int TestInt32 {
      get { return testInt32_; }
      set {
        testInt32_ = value;
      }
    }

    /// <summary>Field number for the "testInt64" field.</summary>
    public const int TestInt64FieldNumber = 4;
    private long testInt64_;
    /// <summary>
    ///1 2 4 8
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public long TestInt64 {
      get { return testInt64_; }
      set {
        testInt64_ = value;
      }
    }

    /// <summary>Field number for the "testSInt32" field.</summary>
    public const int TestSInt32FieldNumber = 5;
    private int testSInt32_;
    /// <summary>
    ///更实用与表示负数类型的整数
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public int TestSInt32 {
      get { return testSInt32_; }
      set {
        testSInt32_ = value;
      }
    }

    /// <summary>Field number for the "testSInt64" field.</summary>
    public const int TestSInt64FieldNumber = 6;
    private long testSInt64_;
    /// <summary>
    ///C# - long 适用于来表示负数的整数
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public long TestSInt64 {
      get { return testSInt64_; }
      set {
        testSInt64_ = value;
      }
    }

    /// <summary>Field number for the "testUInt" field.</summary>
    public const int TestUIntFieldNumber = 7;
    private uint testUInt_;
    /// <summary>
    ///无符号 变长编码
    ///1 2 4
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public uint TestUInt {
      get { return testUInt_; }
      set {
        testUInt_ = value;
      }
    }

    /// <summary>Field number for the "testULong" field.</summary>
    public const int TestULongFieldNumber = 8;
    private ulong testULong_;
    /// <summary>
    ///C# - ulong 变长的编码
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public ulong TestULong {
      get { return testULong_; }
      set {
        testULong_ = value;
      }
    }

    /// <summary>Field number for the "testFixed32" field.</summary>
    public const int TestFixed32FieldNumber = 9;
    private uint testFixed32_;
    /// <summary>
    ///固定字节数的类型
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public uint TestFixed32 {
      get { return testFixed32_; }
      set {
        testFixed32_ = value;
      }
    }

    /// <summary>Field number for the "testFixed64" field.</summary>
    public const int TestFixed64FieldNumber = 10;
    private ulong testFixed64_;
    /// <summary>
    ///C# -ulong 它通常用来表示大于2的56次方的数 ,比uint64更有效 始终是8个字节
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public ulong TestFixed64 {
      get { return testFixed64_; }
      set {
        testFixed64_ = value;
      }
    }

    /// <summary>Field number for the "testSFixed32" field.</summary>
    public const int TestSFixed32FieldNumber = 11;
    private int testSFixed32_;
    /// <summary>
    ///C# - int 始终4个字节
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public int TestSFixed32 {
      get { return testSFixed32_; }
      set {
        testSFixed32_ = value;
      }
    }

    /// <summary>Field number for the "testSFixed64" field.</summary>
    public const int TestSFixed64FieldNumber = 12;
    private long testSFixed64_;
    /// <summary>
    ///C# - long 始终8个字节
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public long TestSFixed64 {
      get { return testSFixed64_; }
      set {
        testSFixed64_ = value;
      }
    }

    /// <summary>Field number for the "testBool" field.</summary>
    public const int TestBoolFieldNumber = 13;
    private bool testBool_;
    /// <summary>
    ///其它类型
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public bool TestBool {
      get { return testBool_; }
      set {
        testBool_ = value;
      }
    }

    /// <summary>Field number for the "testStr" field.</summary>
    public const int TestStrFieldNumber = 14;
    private string testStr_ = "";
    /// <summary>
    ///C# - string
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public string TestStr {
      get { return testStr_; }
      set {
        testStr_ = pb::ProtoPreconditions.CheckNotNull(value, "value");
      }
    }

    /// <summary>Field number for the "testBytes" field.</summary>
    public const int TestBytesFieldNumber = 15;
    private pb::ByteString testBytes_ = pb::ByteString.Empty;
    /// <summary>
    ///C# - BytesString 字节字符串
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public pb::ByteString TestBytes {
      get { return testBytes_; }
      set {
        testBytes_ = pb::ProtoPreconditions.CheckNotNull(value, "value");
      }
    }

    /// <summary>Field number for the "listInt" field.</summary>
    public const int ListIntFieldNumber = 16;
    private static readonly pb::FieldCodec<int> _repeated_listInt_codec
        = pb::FieldCodec.ForInt32(130);
    private readonly pbc::RepeatedField<int> listInt_ = new pbc::RepeatedField<int>();
    /// <summary>
    ///数组List
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public pbc::RepeatedField<int> ListInt {
      get { return listInt_; }
    }

    /// <summary>Field number for the "testMap" field.</summary>
    public const int TestMapFieldNumber = 17;
    private static readonly pbc::MapField<int, string>.Codec _map_testMap_codec
        = new pbc::MapField<int, string>.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForString(18, ""), 138);
    private readonly pbc::MapField<int, string> testMap_ = new pbc::MapField<int, string>();
    /// <summary>
    ///字典Dictionary
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public pbc::MapField<int, string> TestMap {
      get { return testMap_; }
    }

    /// <summary>Field number for the "testEnum" field.</summary>
    public const int TestEnumFieldNumber = 18;
    private global::GamePlayerTest.TestEnum testEnum_ = global::GamePlayerTest.TestEnum.Normal;
    /// <summary>
    ///枚举成员变量的声明 需要唯一编码
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public global::GamePlayerTest.TestEnum TestEnum {
      get { return testEnum_; }
      set {
        testEnum_ = value;
      }
    }

    /// <summary>Field number for the "testMsg2" field.</summary>
    public const int TestMsg2FieldNumber = 19;
    private global::GamePlayerTest.TestMsg2 testMsg2_;
    /// <summary>
    ///声明自定义类对象 需要唯一编码
    ///默认值是null
    /// </summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public global::GamePlayerTest.TestMsg2 TestMsg2 {
      get { return testMsg2_; }
      set {
        testMsg2_ = value;
      }
    }

    /// <summary>Field number for the "testMsg3" field.</summary>
    public const int TestMsg3FieldNumber = 20;
    private global::GamePlayerTest.TestMsg.Types.TestMsg3 testMsg3_;
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public global::GamePlayerTest.TestMsg.Types.TestMsg3 TestMsg3 {
      get { return testMsg3_; }
      set {
        testMsg3_ = value;
      }
    }

    /// <summary>Field number for the "testEnum2" field.</summary>
    public const int TestEnum2FieldNumber = 21;
    private global::GamePlayerTest.TestMsg.Types.TestEnum2 testEnum2_ = global::GamePlayerTest.TestMsg.Types.TestEnum2.Normal;
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public global::GamePlayerTest.TestMsg.Types.TestEnum2 TestEnum2 {
      get { return testEnum2_; }
      set {
        testEnum2_ = value;
      }
    }

    /// <summary>Field number for the "testBool2123123" field.</summary>
    public const int TestBool2123123FieldNumber = 23;
    private bool testBool2123123_;
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public bool TestBool2123123 {
      get { return testBool2123123_; }
      set {
        testBool2123123_ = value;
      }
    }

    /// <summary>Field number for the "testHeart" field.</summary>
    public const int TestHeartFieldNumber = 24;
    private global::GameSystemTest.HeartMsg testHeart_;
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public global::GameSystemTest.HeartMsg TestHeart {
      get { return testHeart_; }
      set {
        testHeart_ = value;
      }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public override bool Equals(object other) {
      return Equals(other as TestMsg);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public bool Equals(TestMsg other) {
      if (ReferenceEquals(other, null)) {
        return false;
      }
      if (ReferenceEquals(other, this)) {
        return true;
      }
      if (!pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.Equals(TestF, other.TestF)) return false;
      if (!pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.Equals(TestD, other.TestD)) return false;
      if (TestInt32 != other.TestInt32) return false;
      if (TestInt64 != other.TestInt64) return false;
      if (TestSInt32 != other.TestSInt32) return false;
      if (TestSInt64 != other.TestSInt64) return false;
      if (TestUInt != other.TestUInt) return false;
      if (TestULong != other.TestULong) return false;
      if (TestFixed32 != other.TestFixed32) return false;
      if (TestFixed64 != other.TestFixed64) return false;
      if (TestSFixed32 != other.TestSFixed32) return false;
      if (TestSFixed64 != other.TestSFixed64) return false;
      if (TestBool != other.TestBool) return false;
      if (TestStr != other.TestStr) return false;
      if (TestBytes != other.TestBytes) return false;
      if(!listInt_.Equals(other.listInt_)) return false;
      if (!TestMap.Equals(other.TestMap)) return false;
      if (TestEnum != other.TestEnum) return false;
      if (!object.Equals(TestMsg2, other.TestMsg2)) return false;
      if (!object.Equals(TestMsg3, other.TestMsg3)) return false;
      if (TestEnum2 != other.TestEnum2) return false;
      if (TestBool2123123 != other.TestBool2123123) return false;
      if (!object.Equals(TestHeart, other.TestHeart)) return false;
      return Equals(_unknownFields, other._unknownFields);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public override int GetHashCode() {
      int hash = 1;
      if (TestF != 0F) hash ^= pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.GetHashCode(TestF);
      if (HasTestD) hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(TestD);
      if (TestInt32 != 0) hash ^= TestInt32.GetHashCode();
      if (TestInt64 != 0L) hash ^= TestInt64.GetHashCode();
      if (TestSInt32 != 0) hash ^= TestSInt32.GetHashCode();
      if (TestSInt64 != 0L) hash ^= TestSInt64.GetHashCode();
      if (TestUInt != 0) hash ^= TestUInt.GetHashCode();
      if (TestULong != 0UL) hash ^= TestULong.GetHashCode();
      if (TestFixed32 != 0) hash ^= TestFixed32.GetHashCode();
      if (TestFixed64 != 0UL) hash ^= TestFixed64.GetHashCode();
      if (TestSFixed32 != 0) hash ^= TestSFixed32.GetHashCode();
      if (TestSFixed64 != 0L) hash ^= TestSFixed64.GetHashCode();
      if (TestBool != false) hash ^= TestBool.GetHashCode();
      if (TestStr.Length != 0) hash ^= TestStr.GetHashCode();
      if (TestBytes.Length != 0) hash ^= TestBytes.GetHashCode();
      hash ^= listInt_.GetHashCode();
      hash ^= TestMap.GetHashCode();
      if (TestEnum != global::GamePlayerTest.TestEnum.Normal) hash ^= TestEnum.GetHashCode();
      if (testMsg2_ != null) hash ^= TestMsg2.GetHashCode();
      if (testMsg3_ != null) hash ^= TestMsg3.GetHashCode();
      if (TestEnum2 != global::GamePlayerTest.TestMsg.Types.TestEnum2.Normal) hash ^= TestEnum2.GetHashCode();
      if (TestBool2123123 != false) hash ^= TestBool2123123.GetHashCode();
      if (testHeart_ != null) hash ^= TestHeart.GetHashCode();
      if (_unknownFields != null) {
        hash ^= _unknownFields.GetHashCode();
      }
      return hash;
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public override string ToString() {
      return pb::JsonFormatter.ToDiagnosticString(this);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public void WriteTo(pb::CodedOutputStream output) {
    #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
      output.WriteRawMessage(this);
    #else
      if (TestF != 0F) {
        output.WriteRawTag(13);
        output.WriteFloat(TestF);
      }
      if (HasTestD) {
        output.WriteRawTag(17);
        output.WriteDouble(TestD);
      }
      if (TestInt32 != 0) {
        output.WriteRawTag(24);
        output.WriteInt32(TestInt32);
      }
      if (TestInt64 != 0L) {
        output.WriteRawTag(32);
        output.WriteInt64(TestInt64);
      }
      if (TestSInt32 != 0) {
        output.WriteRawTag(40);
        output.WriteSInt32(TestSInt32);
      }
      if (TestSInt64 != 0L) {
        output.WriteRawTag(48);
        output.WriteSInt64(TestSInt64);
      }
      if (TestUInt != 0) {
        output.WriteRawTag(56);
        output.WriteUInt32(TestUInt);
      }
      if (TestULong != 0UL) {
        output.WriteRawTag(64);
        output.WriteUInt64(TestULong);
      }
      if (TestFixed32 != 0) {
        output.WriteRawTag(77);
        output.WriteFixed32(TestFixed32);
      }
      if (TestFixed64 != 0UL) {
        output.WriteRawTag(81);
        output.WriteFixed64(TestFixed64);
      }
      if (TestSFixed32 != 0) {
        output.WriteRawTag(93);
        output.WriteSFixed32(TestSFixed32);
      }
      if (TestSFixed64 != 0L) {
        output.WriteRawTag(97);
        output.WriteSFixed64(TestSFixed64);
      }
      if (TestBool != false) {
        output.WriteRawTag(104);
        output.WriteBool(TestBool);
      }
      if (TestStr.Length != 0) {
        output.WriteRawTag(114);
        output.WriteString(TestStr);
      }
      if (TestBytes.Length != 0) {
        output.WriteRawTag(122);
        output.WriteBytes(TestBytes);
      }
      listInt_.WriteTo(output, _repeated_listInt_codec);
      testMap_.WriteTo(output, _map_testMap_codec);
      if (TestEnum != global::GamePlayerTest.TestEnum.Normal) {
        output.WriteRawTag(144, 1);
        output.WriteEnum((int) TestEnum);
      }
      if (testMsg2_ != null) {
        output.WriteRawTag(154, 1);
        output.WriteMessage(TestMsg2);
      }
      if (testMsg3_ != null) {
        output.WriteRawTag(162, 1);
        output.WriteMessage(TestMsg3);
      }
      if (TestEnum2 != global::GamePlayerTest.TestMsg.Types.TestEnum2.Normal) {
        output.WriteRawTag(168, 1);
        output.WriteEnum((int) TestEnum2);
      }
      if (TestBool2123123 != false) {
        output.WriteRawTag(184, 1);
        output.WriteBool(TestBool2123123);
      }
      if (testHeart_ != null) {
        output.WriteRawTag(194, 1);
        output.WriteMessage(TestHeart);
      }
      if (_unknownFields != null) {
        _unknownFields.WriteTo(output);
      }
    #endif
    }

    #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) {
      if (TestF != 0F) {
        output.WriteRawTag(13);
        output.WriteFloat(TestF);
      }
      if (HasTestD) {
        output.WriteRawTag(17);
        output.WriteDouble(TestD);
      }
      if (TestInt32 != 0) {
        output.WriteRawTag(24);
        output.WriteInt32(TestInt32);
      }
      if (TestInt64 != 0L) {
        output.WriteRawTag(32);
        output.WriteInt64(TestInt64);
      }
      if (TestSInt32 != 0) {
        output.WriteRawTag(40);
        output.WriteSInt32(TestSInt32);
      }
      if (TestSInt64 != 0L) {
        output.WriteRawTag(48);
        output.WriteSInt64(TestSInt64);
      }
      if (TestUInt != 0) {
        output.WriteRawTag(56);
        output.WriteUInt32(TestUInt);
      }
      if (TestULong != 0UL) {
        output.WriteRawTag(64);
        output.WriteUInt64(TestULong);
      }
      if (TestFixed32 != 0) {
        output.WriteRawTag(77);
        output.WriteFixed32(TestFixed32);
      }
      if (TestFixed64 != 0UL) {
        output.WriteRawTag(81);
        output.WriteFixed64(TestFixed64);
      }
      if (TestSFixed32 != 0) {
        output.WriteRawTag(93);
        output.WriteSFixed32(TestSFixed32);
      }
      if (TestSFixed64 != 0L) {
        output.WriteRawTag(97);
        output.WriteSFixed64(TestSFixed64);
      }
      if (TestBool != false) {
        output.WriteRawTag(104);
        output.WriteBool(TestBool);
      }
      if (TestStr.Length != 0) {
        output.WriteRawTag(114);
        output.WriteString(TestStr);
      }
      if (TestBytes.Length != 0) {
        output.WriteRawTag(122);
        output.WriteBytes(TestBytes);
      }
      listInt_.WriteTo(ref output, _repeated_listInt_codec);
      testMap_.WriteTo(ref output, _map_testMap_codec);
      if (TestEnum != global::GamePlayerTest.TestEnum.Normal) {
        output.WriteRawTag(144, 1);
        output.WriteEnum((int) TestEnum);
      }
      if (testMsg2_ != null) {
        output.WriteRawTag(154, 1);
        output.WriteMessage(TestMsg2);
      }
      if (testMsg3_ != null) {
        output.WriteRawTag(162, 1);
        output.WriteMessage(TestMsg3);
      }
      if (TestEnum2 != global::GamePlayerTest.TestMsg.Types.TestEnum2.Normal) {
        output.WriteRawTag(168, 1);
        output.WriteEnum((int) TestEnum2);
      }
      if (TestBool2123123 != false) {
        output.WriteRawTag(184, 1);
        output.WriteBool(TestBool2123123);
      }
      if (testHeart_ != null) {
        output.WriteRawTag(194, 1);
        output.WriteMessage(TestHeart);
      }
      if (_unknownFields != null) {
        _unknownFields.WriteTo(ref output);
      }
    }
    #endif

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public int CalculateSize() {
      int size = 0;
      if (TestF != 0F) {
        size += 1 + 4;
      }
      if (HasTestD) {
        size += 1 + 8;
      }
      if (TestInt32 != 0) {
        size += 1 + pb::CodedOutputStream.ComputeInt32Size(TestInt32);
      }
      if (TestInt64 != 0L) {
        size += 1 + pb::CodedOutputStream.ComputeInt64Size(TestInt64);
      }
      if (TestSInt32 != 0) {
        size += 1 + pb::CodedOutputStream.ComputeSInt32Size(TestSInt32);
      }
      if (TestSInt64 != 0L) {
        size += 1 + pb::CodedOutputStream.ComputeSInt64Size(TestSInt64);
      }
      if (TestUInt != 0) {
        size += 1 + pb::CodedOutputStream.ComputeUInt32Size(TestUInt);
      }
      if (TestULong != 0UL) {
        size += 1 + pb::CodedOutputStream.ComputeUInt64Size(TestULong);
      }
      if (TestFixed32 != 0) {
        size += 1 + 4;
      }
      if (TestFixed64 != 0UL) {
        size += 1 + 8;
      }
      if (TestSFixed32 != 0) {
        size += 1 + 4;
      }
      if (TestSFixed64 != 0L) {
        size += 1 + 8;
      }
      if (TestBool != false) {
        size += 1 + 1;
      }
      if (TestStr.Length != 0) {
        size += 1 + pb::CodedOutputStream.ComputeStringSize(TestStr);
      }
      if (TestBytes.Length != 0) {
        size += 1 + pb::CodedOutputStream.ComputeBytesSize(TestBytes);
      }
      size += listInt_.CalculateSize(_repeated_listInt_codec);
      size += testMap_.CalculateSize(_map_testMap_codec);
      if (TestEnum != global::GamePlayerTest.TestEnum.Normal) {
        size += 2 + pb::CodedOutputStream.ComputeEnumSize((int) TestEnum);
      }
      if (testMsg2_ != null) {
        size += 2 + pb::CodedOutputStream.ComputeMessageSize(TestMsg2);
      }
      if (testMsg3_ != null) {
        size += 2 + pb::CodedOutputStream.ComputeMessageSize(TestMsg3);
      }
      if (TestEnum2 != global::GamePlayerTest.TestMsg.Types.TestEnum2.Normal) {
        size += 2 + pb::CodedOutputStream.ComputeEnumSize((int) TestEnum2);
      }
      if (TestBool2123123 != false) {
        size += 2 + 1;
      }
      if (testHeart_ != null) {
        size += 2 + pb::CodedOutputStream.ComputeMessageSize(TestHeart);
      }
      if (_unknownFields != null) {
        size += _unknownFields.CalculateSize();
      }
      return size;
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public void MergeFrom(TestMsg other) {
      if (other == null) {
        return;
      }
      if (other.TestF != 0F) {
        TestF = other.TestF;
      }
      if (other.HasTestD) {
        TestD = other.TestD;
      }
      if (other.TestInt32 != 0) {
        TestInt32 = other.TestInt32;
      }
      if (other.TestInt64 != 0L) {
        TestInt64 = other.TestInt64;
      }
      if (other.TestSInt32 != 0) {
        TestSInt32 = other.TestSInt32;
      }
      if (other.TestSInt64 != 0L) {
        TestSInt64 = other.TestSInt64;
      }
      if (other.TestUInt != 0) {
        TestUInt = other.TestUInt;
      }
      if (other.TestULong != 0UL) {
        TestULong = other.TestULong;
      }
      if (other.TestFixed32 != 0) {
        TestFixed32 = other.TestFixed32;
      }
      if (other.TestFixed64 != 0UL) {
        TestFixed64 = other.TestFixed64;
      }
      if (other.TestSFixed32 != 0) {
        TestSFixed32 = other.TestSFixed32;
      }
      if (other.TestSFixed64 != 0L) {
        TestSFixed64 = other.TestSFixed64;
      }
      if (other.TestBool != false) {
        TestBool = other.TestBool;
      }
      if (other.TestStr.Length != 0) {
        TestStr = other.TestStr;
      }
      if (other.TestBytes.Length != 0) {
        TestBytes = other.TestBytes;
      }
      listInt_.Add(other.listInt_);
      testMap_.Add(other.testMap_);
      if (other.TestEnum != global::GamePlayerTest.TestEnum.Normal) {
        TestEnum = other.TestEnum;
      }
      if (other.testMsg2_ != null) {
        if (testMsg2_ == null) {
          TestMsg2 = new global::GamePlayerTest.TestMsg2();
        }
        TestMsg2.MergeFrom(other.TestMsg2);
      }
      if (other.testMsg3_ != null) {
        if (testMsg3_ == null) {
          TestMsg3 = new global::GamePlayerTest.TestMsg.Types.TestMsg3();
        }
        TestMsg3.MergeFrom(other.TestMsg3);
      }
      if (other.TestEnum2 != global::GamePlayerTest.TestMsg.Types.TestEnum2.Normal) {
        TestEnum2 = other.TestEnum2;
      }
      if (other.TestBool2123123 != false) {
        TestBool2123123 = other.TestBool2123123;
      }
      if (other.testHeart_ != null) {
        if (testHeart_ == null) {
          TestHeart = new global::GameSystemTest.HeartMsg();
        }
        TestHeart.MergeFrom(other.TestHeart);
      }
      _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public void MergeFrom(pb::CodedInputStream input) {
    #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
      input.ReadRawMessage(this);
    #else
      uint tag;
      while ((tag = input.ReadTag()) != 0) {
        switch(tag) {
          default:
            _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
            break;
          case 13: {
            TestF = input.ReadFloat();
            break;
          }
          case 17: {
            TestD = input.ReadDouble();
            break;
          }
          case 24: {
            TestInt32 = input.ReadInt32();
            break;
          }
          case 32: {
            TestInt64 = input.ReadInt64();
            break;
          }
          case 40: {
            TestSInt32 = input.ReadSInt32();
            break;
          }
          case 48: {
            TestSInt64 = input.ReadSInt64();
            break;
          }
          case 56: {
            TestUInt = input.ReadUInt32();
            break;
          }
          case 64: {
            TestULong = input.ReadUInt64();
            break;
          }
          case 77: {
            TestFixed32 = input.ReadFixed32();
            break;
          }
          case 81: {
            TestFixed64 = input.ReadFixed64();
            break;
          }
          case 93: {
            TestSFixed32 = input.ReadSFixed32();
            break;
          }
          case 97: {
            TestSFixed64 = input.ReadSFixed64();
            break;
          }
          case 104: {
            TestBool = input.ReadBool();
            break;
          }
          case 114: {
            TestStr = input.ReadString();
            break;
          }
          case 122: {
            TestBytes = input.ReadBytes();
            break;
          }
          case 130:
          case 128: {
            listInt_.AddEntriesFrom(input, _repeated_listInt_codec);
            break;
          }
          case 138: {
            testMap_.AddEntriesFrom(input, _map_testMap_codec);
            break;
          }
          case 144: {
            TestEnum = (global::GamePlayerTest.TestEnum) input.ReadEnum();
            break;
          }
          case 154: {
            if (testMsg2_ == null) {
              TestMsg2 = new global::GamePlayerTest.TestMsg2();
            }
            input.ReadMessage(TestMsg2);
            break;
          }
          case 162: {
            if (testMsg3_ == null) {
              TestMsg3 = new global::GamePlayerTest.TestMsg.Types.TestMsg3();
            }
            input.ReadMessage(TestMsg3);
            break;
          }
          case 168: {
            TestEnum2 = (global::GamePlayerTest.TestMsg.Types.TestEnum2) input.ReadEnum();
            break;
          }
          case 184: {
            TestBool2123123 = input.ReadBool();
            break;
          }
          case 194: {
            if (testHeart_ == null) {
              TestHeart = new global::GameSystemTest.HeartMsg();
            }
            input.ReadMessage(TestHeart);
            break;
          }
        }
      }
    #endif
    }

    #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
      uint tag;
      while ((tag = input.ReadTag()) != 0) {
        switch(tag) {
          default:
            _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input);
            break;
          case 13: {
            TestF = input.ReadFloat();
            break;
          }
          case 17: {
            TestD = input.ReadDouble();
            break;
          }
          case 24: {
            TestInt32 = input.ReadInt32();
            break;
          }
          case 32: {
            TestInt64 = input.ReadInt64();
            break;
          }
          case 40: {
            TestSInt32 = input.ReadSInt32();
            break;
          }
          case 48: {
            TestSInt64 = input.ReadSInt64();
            break;
          }
          case 56: {
            TestUInt = input.ReadUInt32();
            break;
          }
          case 64: {
            TestULong = input.ReadUInt64();
            break;
          }
          case 77: {
            TestFixed32 = input.ReadFixed32();
            break;
          }
          case 81: {
            TestFixed64 = input.ReadFixed64();
            break;
          }
          case 93: {
            TestSFixed32 = input.ReadSFixed32();
            break;
          }
          case 97: {
            TestSFixed64 = input.ReadSFixed64();
            break;
          }
          case 104: {
            TestBool = input.ReadBool();
            break;
          }
          case 114: {
            TestStr = input.ReadString();
            break;
          }
          case 122: {
            TestBytes = input.ReadBytes();
            break;
          }
          case 130:
          case 128: {
            listInt_.AddEntriesFrom(ref input, _repeated_listInt_codec);
            break;
          }
          case 138: {
            testMap_.AddEntriesFrom(ref input, _map_testMap_codec);
            break;
          }
          case 144: {
            TestEnum = (global::GamePlayerTest.TestEnum) input.ReadEnum();
            break;
          }
          case 154: {
            if (testMsg2_ == null) {
              TestMsg2 = new global::GamePlayerTest.TestMsg2();
            }
            input.ReadMessage(TestMsg2);
            break;
          }
          case 162: {
            if (testMsg3_ == null) {
              TestMsg3 = new global::GamePlayerTest.TestMsg.Types.TestMsg3();
            }
            input.ReadMessage(TestMsg3);
            break;
          }
          case 168: {
            TestEnum2 = (global::GamePlayerTest.TestMsg.Types.TestEnum2) input.ReadEnum();
            break;
          }
          case 184: {
            TestBool2123123 = input.ReadBool();
            break;
          }
          case 194: {
            if (testHeart_ == null) {
              TestHeart = new global::GameSystemTest.HeartMsg();
            }
            input.ReadMessage(TestHeart);
            break;
          }
        }
      }
    }
    #endif

    #region Nested types
    /// <summary>Container for nested types declared in the TestMsg message type.</summary>
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public static partial class Types {
      /// <summary>
      ///规则9:允许嵌套
      /// </summary>
      public enum TestEnum2 {
        /// <summary>
        ///第一个常量必须映射到0
        /// </summary>
        [pbr::OriginalName("NORMAL")] Normal = 0,
        [pbr::OriginalName("BOSS")] Boss = 1,
      }

      /// <summary>
      ///规则9:允许嵌套
      ///嵌套一个类在另一个类当中 相当于是内部类
      /// </summary>
      public sealed partial class TestMsg3 : pb::IMessage<TestMsg3>
      #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
          , pb::IBufferMessage
      #endif
      {
        private static readonly pb::MessageParser<TestMsg3> _parser = new pb::MessageParser<TestMsg3>(() => new TestMsg3());
        private pb::UnknownFieldSet _unknownFields;
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public static pb::MessageParser<TestMsg3> Parser { get { return _parser; } }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public static pbr::MessageDescriptor Descriptor {
          get { return global::GamePlayerTest.TestMsg.Descriptor.NestedTypes[1]; }
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        pbr::MessageDescriptor pb::IMessage.Descriptor {
          get { return Descriptor; }
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public TestMsg3() {
          OnConstruction();
        }

        partial void OnConstruction();

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public TestMsg3(TestMsg3 other) : this() {
          testInt32_ = other.testInt32_;
          _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public TestMsg3 Clone() {
          return new TestMsg3(this);
        }

        /// <summary>Field number for the "testInt32" field.</summary>
        public const int TestInt32FieldNumber = 1;
        private int testInt32_;
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public int TestInt32 {
          get { return testInt32_; }
          set {
            testInt32_ = value;
          }
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public override bool Equals(object other) {
          return Equals(other as TestMsg3);
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public bool Equals(TestMsg3 other) {
          if (ReferenceEquals(other, null)) {
            return false;
          }
          if (ReferenceEquals(other, this)) {
            return true;
          }
          if (TestInt32 != other.TestInt32) return false;
          return Equals(_unknownFields, other._unknownFields);
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public override int GetHashCode() {
          int hash = 1;
          if (TestInt32 != 0) hash ^= TestInt32.GetHashCode();
          if (_unknownFields != null) {
            hash ^= _unknownFields.GetHashCode();
          }
          return hash;
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public override string ToString() {
          return pb::JsonFormatter.ToDiagnosticString(this);
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public void WriteTo(pb::CodedOutputStream output) {
        #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
          output.WriteRawMessage(this);
        #else
          if (TestInt32 != 0) {
            output.WriteRawTag(8);
            output.WriteInt32(TestInt32);
          }
          if (_unknownFields != null) {
            _unknownFields.WriteTo(output);
          }
        #endif
        }

        #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) {
          if (TestInt32 != 0) {
            output.WriteRawTag(8);
            output.WriteInt32(TestInt32);
          }
          if (_unknownFields != null) {
            _unknownFields.WriteTo(ref output);
          }
        }
        #endif

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public int CalculateSize() {
          int size = 0;
          if (TestInt32 != 0) {
            size += 1 + pb::CodedOutputStream.ComputeInt32Size(TestInt32);
          }
          if (_unknownFields != null) {
            size += _unknownFields.CalculateSize();
          }
          return size;
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public void MergeFrom(TestMsg3 other) {
          if (other == null) {
            return;
          }
          if (other.TestInt32 != 0) {
            TestInt32 = other.TestInt32;
          }
          _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        public void MergeFrom(pb::CodedInputStream input) {
        #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
          input.ReadRawMessage(this);
        #else
          uint tag;
          while ((tag = input.ReadTag()) != 0) {
            switch(tag) {
              default:
                _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
                break;
              case 8: {
                TestInt32 = input.ReadInt32();
                break;
              }
            }
          }
        #endif
        }

        #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
        void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
          uint tag;
          while ((tag = input.ReadTag()) != 0) {
            switch(tag) {
              default:
                _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input);
                break;
              case 8: {
                TestInt32 = input.ReadInt32();
                break;
              }
            }
          }
        }
        #endif

      }

    }
    #endregion

  }

  public sealed partial class TestMsg2 : pb::IMessage<TestMsg2>
  #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
      , pb::IBufferMessage
  #endif
  {
    private static readonly pb::MessageParser<TestMsg2> _parser = new pb::MessageParser<TestMsg2>(() => new TestMsg2());
    private pb::UnknownFieldSet _unknownFields;
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public static pb::MessageParser<TestMsg2> Parser { get { return _parser; } }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public static pbr::MessageDescriptor Descriptor {
      get { return global::GamePlayerTest.TestReflection.Descriptor.MessageTypes[1]; }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    pbr::MessageDescriptor pb::IMessage.Descriptor {
      get { return Descriptor; }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public TestMsg2() {
      OnConstruction();
    }

    partial void OnConstruction();

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public TestMsg2(TestMsg2 other) : this() {
      testInt32_ = other.testInt32_;
      _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public TestMsg2 Clone() {
      return new TestMsg2(this);
    }

    /// <summary>Field number for the "testInt32" field.</summary>
    public const int TestInt32FieldNumber = 1;
    private int testInt32_;
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public int TestInt32 {
      get { return testInt32_; }
      set {
        testInt32_ = value;
      }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public override bool Equals(object other) {
      return Equals(other as TestMsg2);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public bool Equals(TestMsg2 other) {
      if (ReferenceEquals(other, null)) {
        return false;
      }
      if (ReferenceEquals(other, this)) {
        return true;
      }
      if (TestInt32 != other.TestInt32) return false;
      return Equals(_unknownFields, other._unknownFields);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public override int GetHashCode() {
      int hash = 1;
      if (TestInt32 != 0) hash ^= TestInt32.GetHashCode();
      if (_unknownFields != null) {
        hash ^= _unknownFields.GetHashCode();
      }
      return hash;
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public override string ToString() {
      return pb::JsonFormatter.ToDiagnosticString(this);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public void WriteTo(pb::CodedOutputStream output) {
    #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
      output.WriteRawMessage(this);
    #else
      if (TestInt32 != 0) {
        output.WriteRawTag(8);
        output.WriteInt32(TestInt32);
      }
      if (_unknownFields != null) {
        _unknownFields.WriteTo(output);
      }
    #endif
    }

    #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) {
      if (TestInt32 != 0) {
        output.WriteRawTag(8);
        output.WriteInt32(TestInt32);
      }
      if (_unknownFields != null) {
        _unknownFields.WriteTo(ref output);
      }
    }
    #endif

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public int CalculateSize() {
      int size = 0;
      if (TestInt32 != 0) {
        size += 1 + pb::CodedOutputStream.ComputeInt32Size(TestInt32);
      }
      if (_unknownFields != null) {
        size += _unknownFields.CalculateSize();
      }
      return size;
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public void MergeFrom(TestMsg2 other) {
      if (other == null) {
        return;
      }
      if (other.TestInt32 != 0) {
        TestInt32 = other.TestInt32;
      }
      _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public void MergeFrom(pb::CodedInputStream input) {
    #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
      input.ReadRawMessage(this);
    #else
      uint tag;
      while ((tag = input.ReadTag()) != 0) {
        switch(tag) {
          default:
            _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
            break;
          case 8: {
            TestInt32 = input.ReadInt32();
            break;
          }
        }
      }
    #endif
    }

    #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
      uint tag;
      while ((tag = input.ReadTag()) != 0) {
        switch(tag) {
          default:
            _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input);
            break;
          case 8: {
            TestInt32 = input.ReadInt32();
            break;
          }
        }
      }
    }
    #endif

  }

  #endregion

}

#endregion Designer generated code

Test2

// <auto-generated>
//     Generated by the protocol buffer compiler.  DO NOT EDIT!
//     source: test2.proto
// </auto-generated>
#pragma warning disable 1591, 0612, 3021, 8981
#region Designer generated code

using pb = global::Google.Protobuf;
using pbc = global::Google.Protobuf.Collections;
using pbr = global::Google.Protobuf.Reflection;
using scg = global::System.Collections.Generic;
namespace GameSystemTest {

  /// <summary>Holder for reflection information generated from test2.proto</summary>
  public static partial class Test2Reflection {

    #region Descriptor
    /// <summary>File descriptor for test2.proto</summary>
    public static pbr::FileDescriptor Descriptor {
      get { return descriptor; }
    }
    private static pbr::FileDescriptor descriptor;

    static Test2Reflection() {
      byte[] descriptorData = global::System.Convert.FromBase64String(
          string.Concat(
            "Cgt0ZXN0Mi5wcm90bxIOR2FtZVN5c3RlbVRlc3QiGAoISGVhcnRNc2cSDAoE",
            "dGltZRgBIAEoA2IGcHJvdG8z"));
      descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
          new pbr::FileDescriptor[] { },
          new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] {
            new pbr::GeneratedClrTypeInfo(typeof(global::GameSystemTest.HeartMsg), global::GameSystemTest.HeartMsg.Parser, new[]{ "Time" }, null, null, null, null)
          }));
    }
    #endregion

  }
  #region Messages
  public sealed partial class HeartMsg : pb::IMessage<HeartMsg>
  #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
      , pb::IBufferMessage
  #endif
  {
    private static readonly pb::MessageParser<HeartMsg> _parser = new pb::MessageParser<HeartMsg>(() => new HeartMsg());
    private pb::UnknownFieldSet _unknownFields;
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public static pb::MessageParser<HeartMsg> Parser { get { return _parser; } }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public static pbr::MessageDescriptor Descriptor {
      get { return global::GameSystemTest.Test2Reflection.Descriptor.MessageTypes[0]; }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    pbr::MessageDescriptor pb::IMessage.Descriptor {
      get { return Descriptor; }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public HeartMsg() {
      OnConstruction();
    }

    partial void OnConstruction();

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public HeartMsg(HeartMsg other) : this() {
      time_ = other.time_;
      _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public HeartMsg Clone() {
      return new HeartMsg(this);
    }

    /// <summary>Field number for the "time" field.</summary>
    public const int TimeFieldNumber = 1;
    private long time_;
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public long Time {
      get { return time_; }
      set {
        time_ = value;
      }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public override bool Equals(object other) {
      return Equals(other as HeartMsg);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public bool Equals(HeartMsg other) {
      if (ReferenceEquals(other, null)) {
        return false;
      }
      if (ReferenceEquals(other, this)) {
        return true;
      }
      if (Time != other.Time) return false;
      return Equals(_unknownFields, other._unknownFields);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public override int GetHashCode() {
      int hash = 1;
      if (Time != 0L) hash ^= Time.GetHashCode();
      if (_unknownFields != null) {
        hash ^= _unknownFields.GetHashCode();
      }
      return hash;
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public override string ToString() {
      return pb::JsonFormatter.ToDiagnosticString(this);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public void WriteTo(pb::CodedOutputStream output) {
    #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
      output.WriteRawMessage(this);
    #else
      if (Time != 0L) {
        output.WriteRawTag(8);
        output.WriteInt64(Time);
      }
      if (_unknownFields != null) {
        _unknownFields.WriteTo(output);
      }
    #endif
    }

    #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) {
      if (Time != 0L) {
        output.WriteRawTag(8);
        output.WriteInt64(Time);
      }
      if (_unknownFields != null) {
        _unknownFields.WriteTo(ref output);
      }
    }
    #endif

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public int CalculateSize() {
      int size = 0;
      if (Time != 0L) {
        size += 1 + pb::CodedOutputStream.ComputeInt64Size(Time);
      }
      if (_unknownFields != null) {
        size += _unknownFields.CalculateSize();
      }
      return size;
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public void MergeFrom(HeartMsg other) {
      if (other == null) {
        return;
      }
      if (other.Time != 0L) {
        Time = other.Time;
      }
      _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    public void MergeFrom(pb::CodedInputStream input) {
    #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
      input.ReadRawMessage(this);
    #else
      uint tag;
      while ((tag = input.ReadTag()) != 0) {
        switch(tag) {
          default:
            _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
            break;
          case 8: {
            Time = input.ReadInt64();
            break;
          }
        }
      }
    #endif
    }

    #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
    void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
      uint tag;
      while ((tag = input.ReadTag()) != 0) {
        switch(tag) {
          default:
            _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input);
            break;
          case 8: {
            Time = input.ReadInt64();
            break;
          }
        }
      }
    }
    #endif

  }

  #endregion

}

#endregion Designer generated code

68.3 练习题

请写一个工具,让我们不需要通过在cmd中输入指令,就可以快捷的生成所有消息协议,可以去查询了解C#的Process类用于完成此题

创建ProtobufTool类,定义各个路径

public class ProtobufTool
{
    //协议配置文件所在路径
    private static string PROTO_PATH = "C:\\Users\\MECHREVO\\Desktop\\TeachNet\\Protobuf\\proto";
    //协议生成可执行文件的路径
    private static string PROTOC_PATH = "C:\\Users\\MECHREVO\\Desktop\\TeachNet\\Protobuf\\protoc.exe";
    //C#文件生成的路径
    private static string CSHARP_PATH = "C:\\Users\\MECHREVO\\Desktop\\TeachNet\\Protobuf\\csharp";
    //C++文件生成的路径
    private static string CPP_PATH = "C:\\Users\\MECHREVO\\Desktop\\TeachNet\\Protobuf\\cpp";
    //Java文件生成的路径
    private static string JAVA_PATH = "C:\\Users\\MECHREVO\\Desktop\\TeachNet\\Protobuf\\java";
}

实现生成消息方法,外部可以传入是什么语言

//生成对应脚本的方法
private static void Generate(string outCmd, string outPath)
{
    //第一步:遍历对应协议配置文件夹 得到所有的配置文件 
    DirectoryInfo directoryInfo = Directory.CreateDirectory(PROTO_PATH);
    //获取对应文件夹下所有文件信息
    FileInfo[] fileInfos = directoryInfo.GetFiles();
    //遍历所有的文件 为其生成协议脚本
    for (int i = 0; i < fileInfos.Length; i++)
    {
        //后缀的判断 只有是 配置文件才能用于生成
        if (fileInfos[i].Extension == ".proto")
        {
            //第二步:根据文件内容 来生成对应的C#脚本 (需要使用C#当中的Process类)
            Process cmd = new Process();
            //protoc.exe的路径
            cmd.StartInfo.FileName = PROTOC_PATH;
            //命令
            cmd.StartInfo.Arguments = $"-I={PROTO_PATH} --{outCmd}={outPath} {fileInfos[i]}";
            //执行
            cmd.Start();
            //告诉外部 某一个文件 生成结束
            UnityEngine.Debug.Log(fileInfos[i] + "生成结束");
        }
    }
    UnityEngine.Debug.Log("所有内容生成结束");
}

实现生成不同语言消息的方法

[MenuItem("ProtobufTool/生成C#代码")]
private static void GenerateCSharp()
{
    Generate("csharp_out", CSHARP_PATH);
}

[MenuItem("ProtobufTool/生成C++代码")]
private static void GenerateCPP()
{
    Generate("cpp_out", CPP_PATH);
}

[MenuItem("ProtobufTool/生成Java代码")]
private static void GenerateJava()
{
    Generate("java_out", JAVA_PATH);
}

68.4 练习题代码

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;

public class ProtobufTool
{
    //协议配置文件所在路径
    private static string PROTO_PATH = "C:\\Users\\MECHREVO\\Desktop\\TeachNet\\Protobuf\\proto";
    //协议生成可执行文件的路径
    private static string PROTOC_PATH = "C:\\Users\\MECHREVO\\Desktop\\TeachNet\\Protobuf\\protoc.exe";
    //C#文件生成的路径
    private static string CSHARP_PATH = "C:\\Users\\MECHREVO\\Desktop\\TeachNet\\Protobuf\\csharp";
    //C++文件生成的路径
    private static string CPP_PATH = "C:\\Users\\MECHREVO\\Desktop\\TeachNet\\Protobuf\\cpp";
    //Java文件生成的路径
    private static string JAVA_PATH = "C:\\Users\\MECHREVO\\Desktop\\TeachNet\\Protobuf\\java";


    [MenuItem("ProtobufTool/生成C#代码")]
    private static void GenerateCSharp()
    {
        Generate("csharp_out", CSHARP_PATH);
    }

    [MenuItem("ProtobufTool/生成C++代码")]
    private static void GenerateCPP()
    {
        Generate("cpp_out", CPP_PATH);
    }

    [MenuItem("ProtobufTool/生成Java代码")]
    private static void GenerateJava()
    {
        Generate("java_out", JAVA_PATH);
    }

    //生成对应脚本的方法
    private static void Generate(string outCmd, string outPath)
    {
        //第一步:遍历对应协议配置文件夹 得到所有的配置文件 
        DirectoryInfo directoryInfo = Directory.CreateDirectory(PROTO_PATH);
        //获取对应文件夹下所有文件信息
        FileInfo[] fileInfos = directoryInfo.GetFiles();
        //遍历所有的文件 为其生成协议脚本
        for (int i = 0; i < fileInfos.Length; i++)
        {
            //后缀的判断 只有是 配置文件才能用于生成
            if (fileInfos[i].Extension == ".proto")
            {
                //第二步:根据文件内容 来生成对应的C#脚本 (需要使用C#当中的Process类)
                Process cmd = new Process();
                //protoc.exe的路径
                cmd.StartInfo.FileName = PROTOC_PATH;
                //命令
                cmd.StartInfo.Arguments = $"-I={PROTO_PATH} --{outCmd}={outPath} {fileInfos[i]}";
                //执行
                cmd.Start();
                //告诉外部 某一个文件 生成结束
                UnityEngine.Debug.Log(fileInfos[i] + "生成结束");
            }
        }
        UnityEngine.Debug.Log("所有内容生成结束");
    }
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏