Proto2CS.cs 9.2 KB

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