Program.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace KeyraysFramework.Tools
  7. {
  8. internal class OpcodeInfo
  9. {
  10. public string Name;
  11. public int Opcode;
  12. }
  13. class Program
  14. {
  15. private const string protoPath = ".";
  16. private static readonly char[] splitChars = { ' ', '\t' };
  17. private static readonly List<OpcodeInfo> msgOpcode = new List<OpcodeInfo>();
  18. //对应服务端的代码路径
  19. private const string messagePath = "../../../../KYNetwork/Message/";
  20. //对应客户端的代码路径
  21. //private const string messagePath = "../Assets/Scripts/Runtime/Message/";
  22. static void Main(string[] args)
  23. {
  24. string protoc = "";
  25. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  26. protoc = "protoc.exe";
  27. else
  28. protoc = "protoc";
  29. //对应服务端的代码路径
  30. ProcessHelper.Run(protoc, $"--csharp_out={messagePath} --proto_path=\"./\" OuterMessage.proto", waitExit: true);
  31. Proto2CS("KYFramework.Network", "OuterMessage.proto", messagePath, "OuterOpcode", 100);
  32. Console.WriteLine("proto2cs succeed!");
  33. }
  34. public static void Proto2CS(string ns, string protoName, string outputPath, string opcodeClassName, int startOpcode, bool isClient = true)
  35. {
  36. msgOpcode.Clear();
  37. string proto = Path.Combine(protoPath, protoName);
  38. string s = File.ReadAllText(proto);
  39. StringBuilder sb = new StringBuilder();
  40. //sb.Append("using KeyraysFramework.Network;\n");
  41. sb.Append($"namespace {ns}\n");
  42. sb.Append("{\n");
  43. bool isMsgStart = false;
  44. foreach (string line in s.Split('\n'))
  45. {
  46. string newline = line.Trim();
  47. if (newline == "")
  48. {
  49. continue;
  50. }
  51. if (newline.StartsWith("//"))
  52. {
  53. sb.Append($"{newline}\n");
  54. }
  55. if (newline.StartsWith("message"))
  56. {
  57. string parentClass = "";
  58. isMsgStart = true;
  59. string msgName = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries)[1];
  60. string[] ss = newline.Split(new[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
  61. if (ss.Length == 2)
  62. {
  63. parentClass = ss[1].Trim();
  64. }
  65. else
  66. {
  67. parentClass = "";
  68. }
  69. msgOpcode.Add(new OpcodeInfo() { Name = msgName, Opcode = ++startOpcode });
  70. sb.Append($"\t[Message({opcodeClassName}.{msgName})]\n");
  71. sb.Append($"\tpublic partial class {msgName} ");
  72. if (parentClass != "")
  73. {
  74. sb.Append($": {parentClass} ");
  75. }
  76. sb.Append("{}\n\n");
  77. }
  78. if (isMsgStart && newline == "}")
  79. {
  80. isMsgStart = false;
  81. }
  82. }
  83. sb.Append("}\n");
  84. GenerateOpcode(ns, opcodeClassName, outputPath, sb);
  85. }
  86. private static void GenerateOpcode(string ns, string outputFileName, string outputPath, StringBuilder sb)
  87. {
  88. sb.AppendLine($"namespace {ns}");
  89. sb.AppendLine("{");
  90. sb.AppendLine($"\tpublic static partial class {outputFileName}");
  91. sb.AppendLine("\t{");
  92. foreach (OpcodeInfo info in msgOpcode)
  93. {
  94. sb.AppendLine($"\t\t public const ushort {info.Name} = {info.Opcode};");
  95. }
  96. sb.AppendLine("\t}");
  97. sb.AppendLine("}");
  98. string csPath = Path.Combine(outputPath, outputFileName + ".cs");
  99. File.WriteAllText(csPath, sb.ToString());
  100. }
  101. }
  102. }