Program.cs 8.9 KB

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