Program.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace ET
  6. {
  7. internal class OpcodeInfo
  8. {
  9. public string Name;
  10. public int Opcode;
  11. }
  12. public static class Program
  13. {
  14. public static void Main()
  15. {
  16. // InnerMessage.proto生成cs代码
  17. InnerProto2CS.Proto2CS();
  18. Console.WriteLine("proto2cs succeed!");
  19. }
  20. }
  21. public static class InnerProto2CS
  22. {
  23. private const string protoPath = ".";
  24. private const string clientMessagePath = "../../../Unity/Assets/Model/Generate/Message/";
  25. private const string serverMessagePath = "../../../Server/Model/Generate/Message/";
  26. private static readonly char[] splitChars = { ' ', '\t' };
  27. private static readonly List<OpcodeInfo> msgOpcode = new List<OpcodeInfo>();
  28. public static void Proto2CS()
  29. {
  30. msgOpcode.Clear();
  31. Proto2CS("ET", "../../../Proto/InnerMessage.proto", serverMessagePath, "InnerOpcode", 10000);
  32. GenerateOpcode("ET", "InnerOpcode", serverMessagePath);
  33. Proto2CS("ET", "../../../Proto/OuterMessage.proto", serverMessagePath, "OuterOpcode", 20000);
  34. GenerateOpcode("ET", "OuterOpcode", serverMessagePath);
  35. Proto2CS("ET", "../../../Proto/OuterMessage.proto", clientMessagePath, "OuterOpcode", 20000);
  36. GenerateOpcode("ET", "OuterOpcode", clientMessagePath);
  37. }
  38. public static void Proto2CS(string ns, string protoName, string outputPath, string opcodeClassName, int startOpcode)
  39. {
  40. if (!Directory.Exists(outputPath))
  41. {
  42. Directory.CreateDirectory(outputPath);
  43. }
  44. msgOpcode.Clear();
  45. string proto = Path.Combine(protoPath, protoName);
  46. string csPath = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(proto) + ".cs");
  47. string s = File.ReadAllText(proto);
  48. StringBuilder sb = new StringBuilder();
  49. sb.Append("using ET;\n");
  50. sb.Append("using ProtoBuf;\n");
  51. sb.Append("using System.Collections.Generic;\n");
  52. sb.Append($"namespace {ns}\n");
  53. sb.Append("{\n");
  54. bool isMsgStart = false;
  55. string parentClass = "";
  56. foreach (string line in s.Split('\n'))
  57. {
  58. string newline = line.Trim();
  59. if (newline == "")
  60. {
  61. continue;
  62. }
  63. if (newline.StartsWith("//ResponseType"))
  64. {
  65. string responseType = line.Split(" ")[1].TrimEnd('\r', '\n');
  66. sb.AppendLine($"\t[ResponseType(typeof({responseType}))]");
  67. continue;
  68. }
  69. if (newline.StartsWith("//"))
  70. {
  71. sb.Append($"{newline}\n");
  72. }
  73. if (newline.StartsWith("message"))
  74. {
  75. parentClass = "";
  76. isMsgStart = true;
  77. string msgName = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries)[1];
  78. string[] ss = newline.Split(new[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
  79. if (ss.Length == 2)
  80. {
  81. parentClass = ss[1].Trim();
  82. }
  83. msgOpcode.Add(new OpcodeInfo() { Name = msgName, Opcode = ++startOpcode });
  84. sb.Append($"\t[Message({opcodeClassName}.{msgName})]\n");
  85. sb.Append($"\t[ProtoContract]\n");
  86. sb.Append($"\tpublic partial class {msgName}: Object");
  87. if (parentClass == "IActorMessage" || parentClass == "IActorRequest" || parentClass == "IActorResponse")
  88. {
  89. sb.Append($", {parentClass}\n");
  90. }
  91. else if (parentClass != "")
  92. {
  93. sb.Append($", {parentClass}\n");
  94. }
  95. else
  96. {
  97. sb.Append("\n");
  98. }
  99. continue;
  100. }
  101. if (isMsgStart)
  102. {
  103. if (newline == "{")
  104. {
  105. sb.Append("\t{\n");
  106. continue;
  107. }
  108. if (newline == "}")
  109. {
  110. isMsgStart = false;
  111. sb.Append("\t}\n\n");
  112. continue;
  113. }
  114. if (newline.Trim().StartsWith("//"))
  115. {
  116. sb.AppendLine(newline);
  117. continue;
  118. }
  119. if (newline.Trim() != "" && newline != "}")
  120. {
  121. if (newline.StartsWith("repeated"))
  122. {
  123. Repeated(sb, ns, newline);
  124. }
  125. else
  126. {
  127. Members(sb, newline, true);
  128. }
  129. }
  130. }
  131. }
  132. sb.Append("}\n");
  133. using FileStream txt = new FileStream(csPath, FileMode.Create, FileAccess.ReadWrite);
  134. using StreamWriter sw = new StreamWriter(txt);
  135. sw.Write(sb.ToString());
  136. }
  137. private static void GenerateOpcode(string ns, string outputFileName, string outputPath)
  138. {
  139. if (!Directory.Exists(outputPath))
  140. {
  141. Directory.CreateDirectory(outputPath);
  142. }
  143. StringBuilder sb = new StringBuilder();
  144. sb.AppendLine($"namespace {ns}");
  145. sb.AppendLine("{");
  146. sb.AppendLine($"\tpublic static partial class {outputFileName}");
  147. sb.AppendLine("\t{");
  148. foreach (OpcodeInfo info in msgOpcode)
  149. {
  150. sb.AppendLine($"\t\t public const ushort {info.Name} = {info.Opcode};");
  151. }
  152. sb.AppendLine("\t}");
  153. sb.AppendLine("}");
  154. string csPath = Path.Combine(outputPath, outputFileName + ".cs");
  155. using FileStream txt = new FileStream(csPath, FileMode.Create);
  156. using StreamWriter sw = new StreamWriter(txt);
  157. sw.Write(sb.ToString());
  158. }
  159. private static void Repeated(StringBuilder sb, string ns, string newline)
  160. {
  161. try
  162. {
  163. int index = newline.IndexOf(";");
  164. newline = newline.Remove(index);
  165. string[] ss = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
  166. string type = ss[1];
  167. type = ConvertType(type);
  168. string name = ss[2];
  169. int n = int.Parse(ss[4]);
  170. sb.Append($"\t\t[ProtoMember({n})]\n");
  171. sb.Append($"\t\tpublic List<{type}> {name} = new List<{type}>();\n\n");
  172. }
  173. catch (Exception e)
  174. {
  175. Console.WriteLine($"{newline}\n {e}");
  176. }
  177. }
  178. private static string ConvertType(string type)
  179. {
  180. string typeCs = "";
  181. switch (type)
  182. {
  183. case "int16":
  184. typeCs = "short";
  185. break;
  186. case "int32":
  187. typeCs = "int";
  188. break;
  189. case "bytes":
  190. typeCs = "byte[]";
  191. break;
  192. case "uint32":
  193. typeCs = "uint";
  194. break;
  195. case "long":
  196. typeCs = "long";
  197. break;
  198. case "int64":
  199. typeCs = "long";
  200. break;
  201. case "uint64":
  202. typeCs = "ulong";
  203. break;
  204. case "uint16":
  205. typeCs = "ushort";
  206. break;
  207. default:
  208. typeCs = type;
  209. break;
  210. }
  211. return typeCs;
  212. }
  213. private static void Members(StringBuilder sb, string newline, bool isRequired)
  214. {
  215. try
  216. {
  217. int index = newline.IndexOf(";");
  218. newline = newline.Remove(index);
  219. string[] ss = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
  220. string type = ss[0];
  221. string name = ss[1];
  222. int n = int.Parse(ss[3]);
  223. string typeCs = ConvertType(type);
  224. sb.Append($"\t\t[ProtoMember({n})]\n");
  225. sb.Append($"\t\tpublic {typeCs} {name} {{ get; set; }}\n\n");
  226. }
  227. catch (Exception e)
  228. {
  229. Console.WriteLine($"{newline}\n {e}");
  230. }
  231. }
  232. }
  233. }