Program.cs 11 KB

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