InnerProto2CS.cs 5.1 KB

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