Program.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace ET
  6. {
  7. internal class OpcodeInfo
  8. {
  9. public string Name;
  10. public int Opcode;
  11. }
  12. public static class Program
  13. {
  14. public static void Main()
  15. {
  16. // InnerMessage.proto生成cs代码
  17. InnerProto2CS.Proto2CS();
  18. Console.WriteLine("proto2cs succeed!");
  19. }
  20. }
  21. public static class InnerProto2CS
  22. {
  23. private const string protoPath = ".";
  24. private const string clientMessagePath = "../Unity/Assets/Model/Module/Message/";
  25. private const string serverMessagePath = "../Server/Model/Module/Message/";
  26. private static readonly char[] splitChars = { ' ', '\t' };
  27. private static readonly List<OpcodeInfo> msgOpcode = new List<OpcodeInfo>();
  28. public static void Proto2CS()
  29. {
  30. msgOpcode.Clear();
  31. Proto2CS("ET", "InnerMessage.proto", serverMessagePath, "InnerOpcode", 10000);
  32. GenerateOpcode("ET", "InnerOpcode", serverMessagePath);
  33. Proto2CS("ET", "OuterMessage.proto", clientMessagePath, "OuterOpcode", 20000);
  34. GenerateOpcode("ET", "OuterOpcode", clientMessagePath);
  35. }
  36. public static void Proto2CS(string ns, string protoName, string outputPath, string opcodeClassName, int startOpcode)
  37. {
  38. msgOpcode.Clear();
  39. string proto = Path.Combine(protoPath, protoName);
  40. string csPath = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(proto) + ".cs");
  41. string s = File.ReadAllText(proto);
  42. StringBuilder sb = new StringBuilder();
  43. sb.Append("using ET;\n");
  44. sb.Append("using ProtoBuf;\n");
  45. sb.Append("using System.Collections.Generic;\n");
  46. sb.Append($"namespace {ns}\n");
  47. sb.Append("{\n");
  48. bool isMsgStart = false;
  49. string parentClass = "";
  50. foreach (string line in s.Split('\n'))
  51. {
  52. string newline = line.Trim();
  53. if (newline == "")
  54. {
  55. continue;
  56. }
  57. if (newline.StartsWith("//"))
  58. {
  59. sb.Append($"{newline}\n");
  60. }
  61. if (newline.StartsWith("message"))
  62. {
  63. parentClass = "";
  64. isMsgStart = true;
  65. string msgName = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries)[1];
  66. string[] ss = newline.Split(new[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
  67. if (ss.Length == 2)
  68. {
  69. parentClass = ss[1].Trim();
  70. }
  71. msgOpcode.Add(new OpcodeInfo() { Name = msgName, Opcode = ++startOpcode });
  72. sb.Append($"\t[Message({opcodeClassName}.{msgName})]\n");
  73. sb.Append($"\t[ProtoContract]\n");
  74. sb.Append($"\tpublic partial class {msgName}");
  75. if (parentClass == "IActorMessage" || parentClass == "IActorRequest" || parentClass == "IActorResponse")
  76. {
  77. sb.Append($": {parentClass}\n");
  78. }
  79. else if (parentClass != "")
  80. {
  81. sb.Append($": {parentClass}\n");
  82. }
  83. else
  84. {
  85. sb.Append("\n");
  86. }
  87. continue;
  88. }
  89. if (isMsgStart)
  90. {
  91. if (newline == "{")
  92. {
  93. sb.Append("\t{\n");
  94. continue;
  95. }
  96. if (newline == "}")
  97. {
  98. isMsgStart = false;
  99. sb.Append("\t}\n\n");
  100. continue;
  101. }
  102. if (newline.Trim().StartsWith("//"))
  103. {
  104. sb.AppendLine(newline);
  105. continue;
  106. }
  107. if (newline.Trim() != "" && newline != "}")
  108. {
  109. if (newline.StartsWith("repeated"))
  110. {
  111. Repeated(sb, ns, newline);
  112. }
  113. else
  114. {
  115. Members(sb, newline, true);
  116. }
  117. }
  118. }
  119. }
  120. sb.Append("}\n");
  121. File.WriteAllText(csPath, sb.ToString());
  122. }
  123. private static void GenerateOpcode(string ns, string outputFileName, string outputPath)
  124. {
  125. StringBuilder sb = new StringBuilder();
  126. sb.AppendLine($"namespace {ns}");
  127. sb.AppendLine("{");
  128. sb.AppendLine($"\tpublic static partial class {outputFileName}");
  129. sb.AppendLine("\t{");
  130. foreach (OpcodeInfo info in msgOpcode)
  131. {
  132. sb.AppendLine($"\t\t public const ushort {info.Name} = {info.Opcode};");
  133. }
  134. sb.AppendLine("\t}");
  135. sb.AppendLine("}");
  136. string csPath = Path.Combine(outputPath, outputFileName + ".cs");
  137. File.WriteAllText(csPath, sb.ToString());
  138. }
  139. private static void Repeated(StringBuilder sb, string ns, string newline)
  140. {
  141. try
  142. {
  143. int index = newline.IndexOf(";");
  144. newline = newline.Remove(index);
  145. string[] ss = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
  146. string type = ss[1];
  147. type = ConvertType(type);
  148. string name = ss[2];
  149. int n = int.Parse(ss[4]);
  150. sb.Append($"\t\t[ProtoMember({n})]\n");
  151. sb.Append($"\t\tpublic List<{type}> {name} = new List<{type}>();\n\n");
  152. }
  153. catch (Exception e)
  154. {
  155. Console.WriteLine($"{newline}\n {e}");
  156. }
  157. }
  158. private static string ConvertType(string type)
  159. {
  160. string typeCs = "";
  161. switch (type)
  162. {
  163. case "int16":
  164. typeCs = "short";
  165. break;
  166. case "int32":
  167. typeCs = "int";
  168. break;
  169. case "bytes":
  170. typeCs = "byte[]";
  171. break;
  172. case "uint32":
  173. typeCs = "uint";
  174. break;
  175. case "long":
  176. typeCs = "long";
  177. break;
  178. case "int64":
  179. typeCs = "long";
  180. break;
  181. case "uint64":
  182. typeCs = "ulong";
  183. break;
  184. case "uint16":
  185. typeCs = "ushort";
  186. break;
  187. default:
  188. typeCs = type;
  189. break;
  190. }
  191. return typeCs;
  192. }
  193. private static void Members(StringBuilder sb, string newline, bool isRequired)
  194. {
  195. try
  196. {
  197. int index = newline.IndexOf(";");
  198. newline = newline.Remove(index);
  199. string[] ss = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
  200. string type = ss[0];
  201. string name = ss[1];
  202. int n = int.Parse(ss[3]);
  203. string typeCs = ConvertType(type);
  204. sb.Append($"\t\t[ProtoMember({n})]\n");
  205. sb.Append($"\t\tpublic {typeCs} {name} {{ get; set; }}\n\n");
  206. }
  207. catch (Exception e)
  208. {
  209. Console.WriteLine($"{newline}\n {e}");
  210. }
  211. }
  212. }
  213. }