InnerProto2CS.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using ETModel;
  6. using UnityEditor;
  7. namespace ETEditor
  8. {
  9. [Flags]
  10. public enum HeadFlag
  11. {
  12. None = 0,
  13. Bson = 1,
  14. Proto = 2,
  15. }
  16. public static class InnerProto2CS
  17. {
  18. private const string protoPath = "../Proto/";
  19. private const string serverMessagePath = "../Server/Hotfix/Module/Message/";
  20. private static readonly char[] splitChars = { ' ', '\t' };
  21. private static readonly List<OpcodeInfo> msgOpcode = new List<OpcodeInfo>();
  22. public static void Proto2CS()
  23. {
  24. msgOpcode.Clear();
  25. Proto2CS("ETHotfix", "InnerMessage.proto", serverMessagePath, "InnerOpcode", 1000);
  26. GenerateOpcode("ETHotfix", "InnerOpcode", serverMessagePath);
  27. AssetDatabase.Refresh();
  28. }
  29. public static void Proto2CS(string ns, string protoName, string outputPath, string opcodeClassName, int startOpcode)
  30. {
  31. msgOpcode.Clear();
  32. string proto = Path.Combine(protoPath, protoName);
  33. string csPath = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(proto) + ".cs");
  34. string s = File.ReadAllText(proto);
  35. StringBuilder sb = new StringBuilder();
  36. sb.Append("using ETModel;\n");
  37. sb.Append("using System.Collections.Generic;\n");
  38. sb.Append($"namespace {ns}\n");
  39. sb.Append("{\n");
  40. bool isMsgStart = false;
  41. string parentClass = "";
  42. foreach (string line in s.Split('\n'))
  43. {
  44. string newline = line.Trim();
  45. if (newline == "")
  46. {
  47. continue;
  48. }
  49. if (newline.StartsWith("//"))
  50. {
  51. sb.Append($"{newline}\n");
  52. }
  53. if (newline.StartsWith("message"))
  54. {
  55. parentClass = "";
  56. isMsgStart = true;
  57. string msgName = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries)[1];
  58. string[] ss = newline.Split(new []{"//"}, StringSplitOptions.RemoveEmptyEntries);
  59. if (ss.Length == 2)
  60. {
  61. parentClass = ss[1].Trim();
  62. }
  63. msgOpcode.Add(new OpcodeInfo() { Name = msgName, Opcode = ++startOpcode});
  64. sb.Append($"\t[Message({opcodeClassName}.{msgName})]\n");
  65. sb.Append($"\tpublic partial class {msgName}");
  66. if (parentClass == "IActorMessage" || parentClass == "IActorRequest" || parentClass == "IActorResponse" || parentClass == "IFrameMessage")
  67. {
  68. sb.Append($": {parentClass}\n");
  69. }
  70. else if (parentClass != "")
  71. {
  72. sb.Append($": {parentClass}\n");
  73. }
  74. else
  75. {
  76. sb.Append("\n");
  77. }
  78. continue;
  79. }
  80. if (isMsgStart)
  81. {
  82. if (newline == "{")
  83. {
  84. sb.Append("\t{\n");
  85. continue;
  86. }
  87. if (newline == "}")
  88. {
  89. isMsgStart = false;
  90. sb.Append("\t}\n\n");
  91. continue;
  92. }
  93. if (newline.Trim().StartsWith("//"))
  94. {
  95. sb.AppendLine(newline);
  96. continue;
  97. }
  98. if (newline.Trim() != "" && newline != "}")
  99. {
  100. if (newline.StartsWith("repeated"))
  101. {
  102. Repeated(sb, ns, newline);
  103. }
  104. else
  105. {
  106. Members(sb, newline, true);
  107. }
  108. }
  109. }
  110. }
  111. sb.Append("}\n");
  112. File.WriteAllText(csPath, sb.ToString());
  113. }
  114. private static void GenerateOpcode(string ns, string outputFileName, string outputPath)
  115. {
  116. StringBuilder sb = new StringBuilder();
  117. sb.AppendLine($"namespace {ns}");
  118. sb.AppendLine("{");
  119. sb.AppendLine($"\tpublic static partial class {outputFileName}");
  120. sb.AppendLine("\t{");
  121. foreach (OpcodeInfo info in msgOpcode)
  122. {
  123. sb.AppendLine($"\t\t public const ushort {info.Name} = {info.Opcode};");
  124. }
  125. sb.AppendLine("\t}");
  126. sb.AppendLine("}");
  127. string csPath = Path.Combine(outputPath, outputFileName + ".cs");
  128. File.WriteAllText(csPath, sb.ToString());
  129. }
  130. private static void Repeated(StringBuilder sb, string ns, string newline)
  131. {
  132. try
  133. {
  134. int index = newline.IndexOf(";");
  135. newline = newline.Remove(index);
  136. string[] ss = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
  137. string type = ss[1];
  138. type = ConvertType(type);
  139. string name = ss[2];
  140. sb.Append($"\t\tpublic List<{type}> {name} = new List<{type}>();\n\n");
  141. }
  142. catch (Exception e)
  143. {
  144. Log.Error($"{newline}\n {e}");
  145. }
  146. }
  147. private static string ConvertType(string type)
  148. {
  149. string typeCs = "";
  150. switch (type)
  151. {
  152. case "int16":
  153. typeCs = "short";
  154. break;
  155. case "int32":
  156. typeCs = "int";
  157. break;
  158. case "bytes":
  159. typeCs = "byte[]";
  160. break;
  161. case "uint32":
  162. typeCs = "uint";
  163. break;
  164. case "long":
  165. typeCs = "long";
  166. break;
  167. case "int64":
  168. typeCs = "long";
  169. break;
  170. case "uint64":
  171. typeCs = "ulong";
  172. break;
  173. case "uint16":
  174. typeCs = "ushort";
  175. break;
  176. default:
  177. typeCs = type;
  178. break;
  179. }
  180. return typeCs;
  181. }
  182. private static void Members(StringBuilder sb, string newline, bool isRequired)
  183. {
  184. try
  185. {
  186. int index = newline.IndexOf(";");
  187. newline = newline.Remove(index);
  188. string[] ss = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
  189. string type = ss[0];
  190. string name = ss[1];
  191. string typeCs = ConvertType(type);
  192. sb.Append($"\t\tpublic {typeCs} {name} {{ get; set; }}\n\n");
  193. }
  194. catch (Exception e)
  195. {
  196. Log.Error($"{newline}\n {e}");
  197. }
  198. }
  199. }
  200. }