Proto2CS.cs 14 KB

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