Program.cs 12 KB

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