Proto2CS.cs 13 KB

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