Proto2CS.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace ET
  7. {
  8. internal class OpcodeInfo
  9. {
  10. public string Name;
  11. public int Opcode;
  12. }
  13. public static class Proto2CS
  14. {
  15. public static void Export()
  16. {
  17. InnerProto2CS.Proto2CS();
  18. }
  19. }
  20. public static partial class InnerProto2CS
  21. {
  22. private static string clientMessagePath;
  23. private static string serverMessagePath;
  24. private static string clientServerMessagePath;
  25. private static readonly char[] splitChars = [' ', '\t'];
  26. private static readonly List<OpcodeInfo> msgOpcode = [];
  27. public static void Proto2CS()
  28. {
  29. // 强制调用一下mongo,避免mongo库被裁剪
  30. MongoHelper.ToJson(1);
  31. msgOpcode.Clear();
  32. PackagesLock packagesLock = PackageHelper.LoadEtPackagesLock("./");
  33. PackageInfo protoPackage = packagesLock.dependencies["cn.etetet.proto"];
  34. clientMessagePath = Path.Combine(protoPackage.dir, "CodeMode/Model/Client");
  35. serverMessagePath = Path.Combine(protoPackage.dir, "CodeMode/Model/Server");
  36. clientServerMessagePath = Path.Combine(protoPackage.dir, "CodeMode/Model/ClientServer");
  37. List<(string, string)> list = new ();
  38. foreach ((string key, PackageInfo packageInfo) in packagesLock.dependencies)
  39. {
  40. string p = Path.Combine(packageInfo.dir, "Proto");
  41. if (!Directory.Exists(p))
  42. {
  43. continue;
  44. }
  45. foreach (var f in FileHelper.GetAllFiles(p, "*.proto"))
  46. {
  47. list.Add((f, packageInfo.module));
  48. }
  49. }
  50. foreach ((string s, string module) in list)
  51. {
  52. if (!s.EndsWith(".proto"))
  53. {
  54. continue;
  55. }
  56. string fileName = Path.GetFileNameWithoutExtension(s);
  57. string[] ss2 = fileName.Split('_');
  58. string protoName = ss2[0];
  59. string cs = ss2[1];
  60. int startOpcode = int.Parse(ss2[2]);
  61. ProtoFile2CS(s, module, protoName, cs, startOpcode);
  62. }
  63. }
  64. private static void ProtoFile2CS(string path, string module, string protoName, string cs, int startOpcode)
  65. {
  66. msgOpcode.Clear();
  67. string proto = path;
  68. string s = File.ReadAllText(proto);
  69. StringBuilder sb = new();
  70. sb.Append("using MemoryPack;\n");
  71. sb.Append("using System.Collections.Generic;\n\n");
  72. sb.Append($"namespace ET\n");
  73. sb.Append("{\n");
  74. bool isMsgStart = false;
  75. string msgName = "";
  76. string responseType = "";
  77. StringBuilder sbDispose = new();
  78. Regex responseTypeRegex = ResponseTypeRegex();
  79. foreach (string line in s.Split('\n'))
  80. {
  81. string newline = line.Trim();
  82. if (string.IsNullOrEmpty(newline))
  83. {
  84. continue;
  85. }
  86. if (responseTypeRegex.IsMatch(newline))
  87. {
  88. responseType = responseTypeRegex.Replace(newline, string.Empty);
  89. responseType = responseType.Trim().Split(' ')[0].TrimEnd('\r', '\n');
  90. continue;
  91. }
  92. if (!isMsgStart && newline.StartsWith("//"))
  93. {
  94. if (newline.StartsWith("///"))
  95. {
  96. sb.Append("\t/// <summary>\n");
  97. sb.Append($"\t/// {newline.TrimStart('/', ' ')}\n");
  98. sb.Append("\t/// </summary>\n");
  99. }
  100. else
  101. {
  102. sb.Append($"\t// {newline.TrimStart('/', ' ')}\n");
  103. }
  104. continue;
  105. }
  106. if (newline.StartsWith("message"))
  107. {
  108. isMsgStart = true;
  109. string parentClass = "";
  110. msgName = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries)[1];
  111. string[] ss = newline.Split(["//"], StringSplitOptions.RemoveEmptyEntries);
  112. if (ss.Length == 2)
  113. {
  114. parentClass = ss[1].Trim();
  115. }
  116. msgOpcode.Add(new OpcodeInfo() { Name = msgName, Opcode = ++startOpcode });
  117. sb.Append($"\t[MemoryPackable]\n");
  118. sb.Append($"\t[Message({protoName}.{msgName})]\n");
  119. if (!string.IsNullOrEmpty(responseType))
  120. {
  121. sb.Append($"\t[ResponseType(nameof({responseType}))]\n");
  122. }
  123. sb.Append($"\tpublic partial class {msgName} : MessageObject");
  124. if (parentClass is "IActorMessage" or "IActorRequest" or "IActorResponse")
  125. {
  126. sb.Append($", {parentClass}\n");
  127. }
  128. else if (parentClass != "")
  129. {
  130. sb.Append($", {parentClass}\n");
  131. }
  132. else
  133. {
  134. sb.Append('\n');
  135. }
  136. continue;
  137. }
  138. if (isMsgStart)
  139. {
  140. if (newline.StartsWith('{'))
  141. {
  142. sbDispose.Clear();
  143. sb.Append("\t{\n");
  144. sb.Append($"\t\tpublic static {msgName} Create(bool isFromPool = false)\n\t\t{{\n\t\t\treturn ObjectPool.Fetch<{msgName}>(isFromPool);\n\t\t}}\n\n");
  145. continue;
  146. }
  147. if (newline.StartsWith('}'))
  148. {
  149. isMsgStart = false;
  150. responseType = "";
  151. // 加了no dispose则自己去定义dispose函数,不要自动生成
  152. if (!newline.Contains("// no dispose"))
  153. {
  154. sb.Append($"\t\tpublic override void Dispose()\n\t\t{{\n\t\t\tif (!this.IsFromPool)\n\t\t\t{{\n\t\t\t\treturn;\n\t\t\t}}\n\n\t\t\t{sbDispose.ToString().TrimEnd('\t')}\n\t\t\tObjectPool.Recycle(this);\n\t\t}}\n");
  155. }
  156. sb.Append("\t}\n\n");
  157. continue;
  158. }
  159. if (newline.StartsWith("//"))
  160. {
  161. sb.Append("\t\t/// <summary>\n");
  162. sb.Append($"\t\t/// {newline.TrimStart('/', ' ')}\n");
  163. sb.Append("\t\t/// </summary>\n");
  164. continue;
  165. }
  166. string memberStr;
  167. if (newline.Contains("//"))
  168. {
  169. string[] lineSplit = newline.Split("//");
  170. memberStr = lineSplit[0].Trim();
  171. sb.Append("\t\t/// <summary>\n");
  172. sb.Append($"\t\t/// {lineSplit[1].Trim()}\n");
  173. sb.Append("\t\t/// </summary>\n");
  174. }
  175. else
  176. {
  177. memberStr = newline;
  178. }
  179. if (memberStr.StartsWith("map<"))
  180. {
  181. Map(sb, memberStr, sbDispose);
  182. }
  183. else if (memberStr.StartsWith("repeated"))
  184. {
  185. Repeated(sb, memberStr, sbDispose);
  186. }
  187. else
  188. {
  189. Members(sb, memberStr, sbDispose);
  190. }
  191. }
  192. }
  193. sb.Append("\tpublic static class " + protoName + "\n\t{\n");
  194. foreach (OpcodeInfo info in msgOpcode)
  195. {
  196. sb.Append($"\t\tpublic const ushort {info.Name} = {info.Opcode};\n");
  197. }
  198. sb.Append("\t}\n");
  199. sb.Append('}');
  200. sb.Replace("\t", " ");
  201. string result = sb.ToString().ReplaceLineEndings("\r\n");
  202. if (cs.Contains('C'))
  203. {
  204. GenerateCS(result, clientMessagePath, proto);
  205. GenerateCS(result, serverMessagePath, proto);
  206. GenerateCS(result, clientServerMessagePath, proto);
  207. }
  208. if (cs.Contains('S'))
  209. {
  210. GenerateCS(result, serverMessagePath, proto);
  211. GenerateCS(result, clientServerMessagePath, proto);
  212. }
  213. }
  214. private static void GenerateCS(string result, string path, string proto)
  215. {
  216. if (!Directory.Exists(path))
  217. {
  218. Directory.CreateDirectory(path);
  219. }
  220. string csPath = Path.Combine(path, Path.GetFileNameWithoutExtension(proto) + ".cs");
  221. using FileStream txt = new(csPath, FileMode.Create, FileAccess.ReadWrite);
  222. using StreamWriter sw = new(txt);
  223. sw.Write(result);
  224. }
  225. private static void Map(StringBuilder sb, string newline, StringBuilder sbDispose)
  226. {
  227. int start = newline.IndexOf('<') + 1;
  228. int end = newline.IndexOf('>');
  229. string types = newline.Substring(start, end - start);
  230. string[] ss = types.Split(',');
  231. string keyType = ConvertType(ss[0].Trim());
  232. string valueType = ConvertType(ss[1].Trim());
  233. string tail = newline[(end + 1)..];
  234. ss = tail.Trim().Replace(";", "").Split(' ');
  235. string v = ss[0];
  236. int n = int.Parse(ss[2]);
  237. sb.Append("\t\t[MongoDB.Bson.Serialization.Attributes.BsonDictionaryOptions(MongoDB.Bson.Serialization.Options.DictionaryRepresentation.ArrayOfArrays)]\n");
  238. sb.Append($"\t\t[MemoryPackOrder({n - 1})]\n");
  239. sb.Append($"\t\tpublic Dictionary<{keyType}, {valueType}> {v} {{ get; set; }} = new();\n");
  240. sbDispose.Append($"this.{v}.Clear();\n\t\t\t");
  241. }
  242. private static void Repeated(StringBuilder sb, string newline, StringBuilder sbDispose)
  243. {
  244. try
  245. {
  246. int index = newline.IndexOf(';');
  247. newline = newline.Remove(index);
  248. string[] ss = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
  249. string type = ss[1];
  250. type = ConvertType(type);
  251. string name = ss[2];
  252. int n = int.Parse(ss[4]);
  253. sb.Append($"\t\t[MemoryPackOrder({n - 1})]\n");
  254. sb.Append($"\t\tpublic List<{type}> {name} {{ get; set; }} = new();\n\n");
  255. sbDispose.Append($"this.{name}.Clear();\n\t\t\t");
  256. }
  257. catch (Exception e)
  258. {
  259. Console.WriteLine($"{newline}\n {e}");
  260. }
  261. }
  262. private static string ConvertType(string type)
  263. {
  264. return type switch
  265. {
  266. "int16" => "short",
  267. "int32" => "int",
  268. "bytes" => "byte[]",
  269. "uint32" => "uint",
  270. "long" => "long",
  271. "int64" => "long",
  272. "uint64" => "ulong",
  273. "uint16" => "ushort",
  274. _ => type
  275. };
  276. }
  277. private static void Members(StringBuilder sb, string newline, StringBuilder sbDispose)
  278. {
  279. try
  280. {
  281. int index = newline.IndexOf(';');
  282. newline = newline.Remove(index);
  283. string[] ss = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
  284. string type = ss[0];
  285. string name = ss[1];
  286. int n = int.Parse(ss[3]);
  287. string typeCs = ConvertType(type);
  288. sb.Append($"\t\t[MemoryPackOrder({n - 1})]\n");
  289. sb.Append($"\t\tpublic {typeCs} {name} {{ get; set; }}\n\n");
  290. switch (typeCs)
  291. {
  292. case "bytes":
  293. {
  294. break;
  295. }
  296. default:
  297. sbDispose.Append($"this.{name} = default;\n\t\t\t");
  298. break;
  299. }
  300. }
  301. catch (Exception e)
  302. {
  303. Console.WriteLine($"{newline}\n {e}");
  304. }
  305. }
  306. [GeneratedRegex(@"//\s*ResponseType")]
  307. private static partial Regex ResponseTypeRegex();
  308. }
  309. }