Proto2CS.cs 9.2 KB

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