Program.cs 4.3 KB

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