Program.cs 11 KB

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