Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using ETModel;
  8. namespace ETTools
  9. {
  10. internal class OpcodeInfo
  11. {
  12. public string Name;
  13. public int Opcode;
  14. }
  15. public static class Program
  16. {
  17. public static void Main()
  18. {
  19. string protoc = "";
  20. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  21. {
  22. protoc = "protoc.exe";
  23. }
  24. else
  25. {
  26. protoc = "protoc";
  27. }
  28. ProcessHelper.Run(protoc, "--csharp_out=\"../Unity/Assets/Model/Module/Message/\" --proto_path=\"./\" OuterMessage.proto", waitExit: true);
  29. ProcessHelper.Run(protoc, "--csharp_out=\"../Unity/Assets/Hotfix/Module/Message/\" --proto_path=\"./\" HotfixMessage.proto", waitExit: true);
  30. // InnerMessage.proto生成cs代码
  31. InnerProto2CS.Proto2CS();
  32. Proto2CS("ETModel", "OuterMessage.proto", clientMessagePath, "OuterOpcode", 100);
  33. Proto2CS("ETHotfix", "HotfixMessage.proto", hotfixMessagePath, "HotfixOpcode", 10000);
  34. Console.WriteLine("proto2cs succeed!");
  35. }
  36. private const string protoPath = ".";
  37. private const string clientMessagePath = "../Unity/Assets/Model/Module/Message/";
  38. private const string hotfixMessagePath = "../Unity/Assets/Hotfix/Module/Message/";
  39. private static readonly char[] splitChars = { ' ', '\t' };
  40. private static readonly List<OpcodeInfo> msgOpcode = new List<OpcodeInfo>();
  41. public static void Proto2CS(string ns, string protoName, string outputPath, string opcodeClassName, int startOpcode, bool isClient = true)
  42. {
  43. msgOpcode.Clear();
  44. string proto = Path.Combine(protoPath, protoName);
  45. string s = File.ReadAllText(proto);
  46. StringBuilder sb = new StringBuilder();
  47. sb.Append("using ETModel;\n");
  48. sb.Append($"namespace {ns}\n");
  49. sb.Append("{\n");
  50. bool isMsgStart = false;
  51. foreach (string line in s.Split('\n'))
  52. {
  53. string newline = line.Trim();
  54. if (newline == "")
  55. {
  56. continue;
  57. }
  58. if (newline.StartsWith("//"))
  59. {
  60. sb.Append($"{newline}\n");
  61. }
  62. if (newline.StartsWith("message"))
  63. {
  64. string parentClass = "";
  65. isMsgStart = true;
  66. string msgName = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries)[1];
  67. string[] ss = newline.Split(new[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
  68. if (ss.Length == 2)
  69. {
  70. parentClass = ss[1].Trim();
  71. }
  72. else
  73. {
  74. parentClass = "";
  75. }
  76. msgOpcode.Add(new OpcodeInfo() { Name = msgName, Opcode = ++startOpcode });
  77. sb.Append($"\t[Message({opcodeClassName}.{msgName})]\n");
  78. sb.Append($"\tpublic partial class {msgName} ");
  79. if (parentClass != "")
  80. {
  81. sb.Append($": {parentClass} ");
  82. }
  83. sb.Append("{}\n\n");
  84. }
  85. if (isMsgStart && newline == "}")
  86. {
  87. isMsgStart = false;
  88. }
  89. }
  90. sb.Append("}\n");
  91. GenerateOpcode(ns, opcodeClassName, outputPath, sb);
  92. }
  93. private static void GenerateOpcode(string ns, string outputFileName, string outputPath, StringBuilder sb)
  94. {
  95. sb.AppendLine($"namespace {ns}");
  96. sb.AppendLine("{");
  97. sb.AppendLine($"\tpublic static partial class {outputFileName}");
  98. sb.AppendLine("\t{");
  99. foreach (OpcodeInfo info in msgOpcode)
  100. {
  101. sb.AppendLine($"\t\t public const ushort {info.Name} = {info.Opcode};");
  102. }
  103. sb.AppendLine("\t}");
  104. sb.AppendLine("}");
  105. string csPath = Path.Combine(outputPath, outputFileName + ".cs");
  106. File.WriteAllText(csPath, sb.ToString());
  107. }
  108. }
  109. public static class InnerProto2CS
  110. {
  111. private const string protoPath = ".";
  112. private const string serverMessagePath = "../Server/Model/Module/Message/";
  113. private static readonly char[] splitChars = { ' ', '\t' };
  114. private static readonly List<OpcodeInfo> msgOpcode = new List<OpcodeInfo>();
  115. public static void Proto2CS()
  116. {
  117. msgOpcode.Clear();
  118. Proto2CS("ETModel", "InnerMessage.proto", serverMessagePath, "InnerOpcode", 1000);
  119. GenerateOpcode("ETModel", "InnerOpcode", serverMessagePath);
  120. }
  121. public static void Proto2CS(string ns, string protoName, string outputPath, string opcodeClassName, int startOpcode)
  122. {
  123. msgOpcode.Clear();
  124. string proto = Path.Combine(protoPath, protoName);
  125. string csPath = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(proto) + ".cs");
  126. string s = File.ReadAllText(proto);
  127. StringBuilder sb = new StringBuilder();
  128. sb.Append("using ETModel;\n");
  129. sb.Append("using System.Collections.Generic;\n");
  130. sb.Append($"namespace {ns}\n");
  131. sb.Append("{\n");
  132. bool isMsgStart = false;
  133. string parentClass = "";
  134. foreach (string line in s.Split('\n'))
  135. {
  136. string newline = line.Trim();
  137. if (newline == "")
  138. {
  139. continue;
  140. }
  141. if (newline.StartsWith("//"))
  142. {
  143. sb.Append($"{newline}\n");
  144. }
  145. if (newline.StartsWith("message"))
  146. {
  147. parentClass = "";
  148. isMsgStart = true;
  149. string msgName = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries)[1];
  150. string[] ss = newline.Split(new[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
  151. if (ss.Length == 2)
  152. {
  153. parentClass = ss[1].Trim();
  154. }
  155. msgOpcode.Add(new OpcodeInfo() { Name = msgName, Opcode = ++startOpcode });
  156. sb.Append($"\t[Message({opcodeClassName}.{msgName})]\n");
  157. sb.Append($"\tpublic partial class {msgName}");
  158. if (parentClass == "IActorMessage" || parentClass == "IActorRequest" || parentClass == "IActorResponse" ||
  159. parentClass == "IFrameMessage")
  160. {
  161. sb.Append($": {parentClass}\n");
  162. }
  163. else if (parentClass != "")
  164. {
  165. sb.Append($": {parentClass}\n");
  166. }
  167. else
  168. {
  169. sb.Append("\n");
  170. }
  171. continue;
  172. }
  173. if (isMsgStart)
  174. {
  175. if (newline == "{")
  176. {
  177. sb.Append("\t{\n");
  178. continue;
  179. }
  180. if (newline == "}")
  181. {
  182. isMsgStart = false;
  183. sb.Append("\t}\n\n");
  184. continue;
  185. }
  186. if (newline.Trim().StartsWith("//"))
  187. {
  188. sb.AppendLine(newline);
  189. continue;
  190. }
  191. if (newline.Trim() != "" && newline != "}")
  192. {
  193. if (newline.StartsWith("repeated"))
  194. {
  195. Repeated(sb, ns, newline);
  196. }
  197. else
  198. {
  199. Members(sb, newline, true);
  200. }
  201. }
  202. }
  203. }
  204. sb.Append("}\n");
  205. File.WriteAllText(csPath, sb.ToString());
  206. }
  207. private static void GenerateOpcode(string ns, string outputFileName, string outputPath)
  208. {
  209. StringBuilder sb = new StringBuilder();
  210. sb.AppendLine($"namespace {ns}");
  211. sb.AppendLine("{");
  212. sb.AppendLine($"\tpublic static partial class {outputFileName}");
  213. sb.AppendLine("\t{");
  214. foreach (OpcodeInfo info in msgOpcode)
  215. {
  216. sb.AppendLine($"\t\t public const ushort {info.Name} = {info.Opcode};");
  217. }
  218. sb.AppendLine("\t}");
  219. sb.AppendLine("}");
  220. string csPath = Path.Combine(outputPath, outputFileName + ".cs");
  221. File.WriteAllText(csPath, sb.ToString());
  222. }
  223. private static void Repeated(StringBuilder sb, string ns, string newline)
  224. {
  225. try
  226. {
  227. int index = newline.IndexOf(";");
  228. newline = newline.Remove(index);
  229. string[] ss = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
  230. string type = ss[1];
  231. type = ConvertType(type);
  232. string name = ss[2];
  233. sb.Append($"\t\tpublic List<{type}> {name} = new List<{type}>();\n\n");
  234. }
  235. catch (Exception e)
  236. {
  237. Console.WriteLine($"{newline}\n {e}");
  238. }
  239. }
  240. private static string ConvertType(string type)
  241. {
  242. string typeCs = "";
  243. switch (type)
  244. {
  245. case "int16":
  246. typeCs = "short";
  247. break;
  248. case "int32":
  249. typeCs = "int";
  250. break;
  251. case "bytes":
  252. typeCs = "byte[]";
  253. break;
  254. case "uint32":
  255. typeCs = "uint";
  256. break;
  257. case "long":
  258. typeCs = "long";
  259. break;
  260. case "int64":
  261. typeCs = "long";
  262. break;
  263. case "uint64":
  264. typeCs = "ulong";
  265. break;
  266. case "uint16":
  267. typeCs = "ushort";
  268. break;
  269. default:
  270. typeCs = type;
  271. break;
  272. }
  273. return typeCs;
  274. }
  275. private static void Members(StringBuilder sb, string newline, bool isRequired)
  276. {
  277. try
  278. {
  279. int index = newline.IndexOf(";");
  280. newline = newline.Remove(index);
  281. string[] ss = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
  282. string type = ss[0];
  283. string name = ss[1];
  284. string typeCs = ConvertType(type);
  285. sb.Append($"\t\tpublic {typeCs} {name} {{ get; set; }}\n\n");
  286. }
  287. catch (Exception e)
  288. {
  289. Console.WriteLine($"{newline}\n {e}");
  290. }
  291. }
  292. }
  293. }