DataTableGenerator.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 Jiang Yin. All rights reserved.
  4. // Homepage: https://gameframework.cn/
  5. // Feedback: mailto:ellan@gameframework.cn
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using UnityEngine;
  14. namespace MetaClient.Editor.DataTableTools
  15. {
  16. public sealed class DataTableGenerator
  17. {
  18. private const string DataTablePath = "Assets/GameMain/DataTables";
  19. private const string CSharpCodePath = "Assets/GameMain/Scripts/DataTable";
  20. private const string CSharpCodeTemplateFileName = "Assets/GameMain/Configs/DataTableCodeTemplate.txt";
  21. private static readonly Regex EndWithNumberRegex = new Regex(@"\d+$");
  22. private static readonly Regex NameRegex = new Regex(@"^[A-Z][A-Za-z0-9_]*$");
  23. public static DataTableProcessor CreateDataTableProcessor(string dataTableName)
  24. {
  25. return new DataTableProcessor(Utility.Path.GetRegularPath(Path.Combine(DataTablePath, dataTableName + ".txt")), Encoding.GetEncoding("GB2312"), 1, 2, null, 3, 4, 1);
  26. }
  27. public static bool CheckRawData(DataTableProcessor dataTableProcessor, string dataTableName)
  28. {
  29. for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
  30. {
  31. string name = dataTableProcessor.GetName(i);
  32. if (string.IsNullOrEmpty(name) || name == "#")
  33. {
  34. continue;
  35. }
  36. if (!NameRegex.IsMatch(name))
  37. {
  38. Debug.LogWarning(Utility.Text.Format("Check raw data failure. DataTableName='{0}' Name='{1}'", dataTableName, name));
  39. return false;
  40. }
  41. }
  42. return true;
  43. }
  44. public static void GenerateDataFile(DataTableProcessor dataTableProcessor, string dataTableName)
  45. {
  46. string binaryDataFileName = Utility.Path.GetRegularPath(Path.Combine(DataTablePath, dataTableName + ".bytes"));
  47. if (!dataTableProcessor.GenerateDataFile(binaryDataFileName) && File.Exists(binaryDataFileName))
  48. {
  49. File.Delete(binaryDataFileName);
  50. }
  51. }
  52. public static void GenerateCodeFile(DataTableProcessor dataTableProcessor, string dataTableName)
  53. {
  54. dataTableProcessor.SetCodeTemplate(CSharpCodeTemplateFileName, Encoding.UTF8);
  55. dataTableProcessor.SetCodeGenerator(DataTableCodeGenerator);
  56. string csharpCodeFileName = Utility.Path.GetRegularPath(Path.Combine(CSharpCodePath, "DR" + dataTableName + ".cs"));
  57. if (!dataTableProcessor.GenerateCodeFile(csharpCodeFileName, Encoding.UTF8, dataTableName) && File.Exists(csharpCodeFileName))
  58. {
  59. File.Delete(csharpCodeFileName);
  60. }
  61. }
  62. private static void DataTableCodeGenerator(DataTableProcessor dataTableProcessor, StringBuilder codeContent, object userData)
  63. {
  64. string dataTableName = (string)userData;
  65. codeContent.Replace("__DATA_TABLE_CREATE_TIME__", DateTime.UtcNow.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss.fff"));
  66. codeContent.Replace("__DATA_TABLE_NAME_SPACE__", "MetaClient");
  67. codeContent.Replace("__DATA_TABLE_CLASS_NAME__", "DR" + dataTableName);
  68. codeContent.Replace("__DATA_TABLE_COMMENT__", dataTableProcessor.GetValue(0, 1) + "。");
  69. codeContent.Replace("__DATA_TABLE_ID_COMMENT__", "获取" + dataTableProcessor.GetComment(dataTableProcessor.IdColumn) + "。");
  70. codeContent.Replace("__DATA_TABLE_PROPERTIES__", GenerateDataTableProperties(dataTableProcessor));
  71. codeContent.Replace("__DATA_TABLE_PARSER__", GenerateDataTableParser(dataTableProcessor));
  72. codeContent.Replace("__DATA_TABLE_PROPERTY_ARRAY__", GenerateDataTablePropertyArray(dataTableProcessor));
  73. }
  74. private static string GenerateDataTableProperties(DataTableProcessor dataTableProcessor)
  75. {
  76. StringBuilder stringBuilder = new StringBuilder();
  77. bool firstProperty = true;
  78. for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
  79. {
  80. if (dataTableProcessor.IsCommentColumn(i))
  81. {
  82. // 注释列
  83. continue;
  84. }
  85. if (dataTableProcessor.IsIdColumn(i))
  86. {
  87. // 编号列
  88. continue;
  89. }
  90. if (firstProperty)
  91. {
  92. firstProperty = false;
  93. }
  94. else
  95. {
  96. stringBuilder.AppendLine().AppendLine();
  97. }
  98. stringBuilder
  99. .AppendLine(" /// <summary>")
  100. .AppendFormat(" /// 获取{0}。", dataTableProcessor.GetComment(i)).AppendLine()
  101. .AppendLine(" /// </summary>")
  102. .AppendFormat(" public {0} {1}", dataTableProcessor.GetLanguageKeyword(i), dataTableProcessor.GetName(i)).AppendLine()
  103. .AppendLine(" {")
  104. .AppendLine(" get;")
  105. .AppendLine(" private set;")
  106. .Append(" }");
  107. }
  108. return stringBuilder.ToString();
  109. }
  110. private static string GenerateDataTableParser(DataTableProcessor dataTableProcessor)
  111. {
  112. StringBuilder stringBuilder = new StringBuilder();
  113. stringBuilder
  114. .AppendLine(" public override bool ParseDataRow(string dataRowString, object userData)")
  115. .AppendLine(" {")
  116. .AppendLine(" string[] columnStrings = dataRowString.Split(DataTableExtension.DataSplitSeparators);")
  117. .AppendLine(" for (int i = 0; i < columnStrings.Length; i++)")
  118. .AppendLine(" {")
  119. .AppendLine(" columnStrings[i] = columnStrings[i].Trim(DataTableExtension.DataTrimSeparators);")
  120. .AppendLine(" }")
  121. .AppendLine()
  122. .AppendLine(" int index = 0;");
  123. for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
  124. {
  125. if (dataTableProcessor.IsCommentColumn(i))
  126. {
  127. // 注释列
  128. stringBuilder.AppendLine(" index++;");
  129. continue;
  130. }
  131. if (dataTableProcessor.IsIdColumn(i))
  132. {
  133. // 编号列
  134. stringBuilder.AppendLine(" m_Id = int.Parse(columnStrings[index++]);");
  135. continue;
  136. }
  137. if (dataTableProcessor.IsSystem(i))
  138. {
  139. string languageKeyword = dataTableProcessor.GetLanguageKeyword(i);
  140. if (languageKeyword == "string")
  141. {
  142. stringBuilder.AppendFormat(" {0} = columnStrings[index++];", dataTableProcessor.GetName(i)).AppendLine();
  143. }
  144. else
  145. {
  146. stringBuilder.AppendFormat(" {0} = {1}.Parse(columnStrings[index++]);", dataTableProcessor.GetName(i), languageKeyword).AppendLine();
  147. }
  148. }
  149. else
  150. {
  151. stringBuilder.AppendFormat(" {0} = DataTableExtension.Parse{1}(columnStrings[index++]);", dataTableProcessor.GetName(i), dataTableProcessor.GetType(i).Name).AppendLine();
  152. }
  153. }
  154. stringBuilder.AppendLine()
  155. .AppendLine(" GeneratePropertyArray();")
  156. .AppendLine(" return true;")
  157. .AppendLine(" }")
  158. .AppendLine()
  159. .AppendLine(" public override bool ParseDataRow(byte[] dataRowBytes, int startIndex, int length, object userData)")
  160. .AppendLine(" {")
  161. .AppendLine(" using (MemoryStream memoryStream = new MemoryStream(dataRowBytes, startIndex, length, false))")
  162. .AppendLine(" {")
  163. .AppendLine(" using (BinaryReader binaryReader = new BinaryReader(memoryStream, Encoding.UTF8))")
  164. .AppendLine(" {");
  165. for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
  166. {
  167. if (dataTableProcessor.IsCommentColumn(i))
  168. {
  169. // 注释列
  170. continue;
  171. }
  172. if (dataTableProcessor.IsIdColumn(i))
  173. {
  174. // 编号列
  175. stringBuilder.AppendLine(" m_Id = binaryReader.Read7BitEncodedInt32();");
  176. continue;
  177. }
  178. string languageKeyword = dataTableProcessor.GetLanguageKeyword(i);
  179. if (languageKeyword == "int" || languageKeyword == "uint" || languageKeyword == "long" || languageKeyword == "ulong")
  180. {
  181. stringBuilder.AppendFormat(" {0} = binaryReader.Read7BitEncoded{1}();", dataTableProcessor.GetName(i), dataTableProcessor.GetType(i).Name).AppendLine();
  182. }
  183. else
  184. {
  185. stringBuilder.AppendFormat(" {0} = binaryReader.Read{1}();", dataTableProcessor.GetName(i), dataTableProcessor.GetType(i).Name).AppendLine();
  186. }
  187. }
  188. stringBuilder
  189. .AppendLine(" }")
  190. .AppendLine(" }")
  191. .AppendLine()
  192. .AppendLine(" GeneratePropertyArray();")
  193. .AppendLine(" return true;")
  194. .Append(" }");
  195. return stringBuilder.ToString();
  196. }
  197. private static string GenerateDataTablePropertyArray(DataTableProcessor dataTableProcessor)
  198. {
  199. List<PropertyCollection> propertyCollections = new List<PropertyCollection>();
  200. for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
  201. {
  202. if (dataTableProcessor.IsCommentColumn(i))
  203. {
  204. // 注释列
  205. continue;
  206. }
  207. if (dataTableProcessor.IsIdColumn(i))
  208. {
  209. // 编号列
  210. continue;
  211. }
  212. string name = dataTableProcessor.GetName(i);
  213. if (!EndWithNumberRegex.IsMatch(name))
  214. {
  215. continue;
  216. }
  217. string propertyCollectionName = EndWithNumberRegex.Replace(name, string.Empty);
  218. int id = int.Parse(EndWithNumberRegex.Match(name).Value);
  219. PropertyCollection propertyCollection = null;
  220. foreach (PropertyCollection pc in propertyCollections)
  221. {
  222. if (pc.Name == propertyCollectionName)
  223. {
  224. propertyCollection = pc;
  225. break;
  226. }
  227. }
  228. if (propertyCollection == null)
  229. {
  230. propertyCollection = new PropertyCollection(propertyCollectionName, dataTableProcessor.GetLanguageKeyword(i));
  231. propertyCollections.Add(propertyCollection);
  232. }
  233. propertyCollection.AddItem(id, name);
  234. }
  235. StringBuilder stringBuilder = new StringBuilder();
  236. bool firstProperty = true;
  237. foreach (PropertyCollection propertyCollection in propertyCollections)
  238. {
  239. if (firstProperty)
  240. {
  241. firstProperty = false;
  242. }
  243. else
  244. {
  245. stringBuilder.AppendLine().AppendLine();
  246. }
  247. stringBuilder
  248. .AppendFormat(" private KeyValuePair<int, {1}>[] m_{0} = null;", propertyCollection.Name, propertyCollection.LanguageKeyword).AppendLine()
  249. .AppendLine()
  250. .AppendFormat(" public int {0}Count", propertyCollection.Name).AppendLine()
  251. .AppendLine(" {")
  252. .AppendLine(" get")
  253. .AppendLine(" {")
  254. .AppendFormat(" return m_{0}.Length;", propertyCollection.Name).AppendLine()
  255. .AppendLine(" }")
  256. .AppendLine(" }")
  257. .AppendLine()
  258. .AppendFormat(" public {1} Get{0}(int id)", propertyCollection.Name, propertyCollection.LanguageKeyword).AppendLine()
  259. .AppendLine(" {")
  260. .AppendFormat(" foreach (KeyValuePair<int, {1}> i in m_{0})", propertyCollection.Name, propertyCollection.LanguageKeyword).AppendLine()
  261. .AppendLine(" {")
  262. .AppendLine(" if (i.Key == id)")
  263. .AppendLine(" {")
  264. .AppendLine(" return i.Value;")
  265. .AppendLine(" }")
  266. .AppendLine(" }")
  267. .AppendLine()
  268. .AppendFormat(" throw new GameFrameworkException(Utility.Text.Format(\"Get{0} with invalid id '{{0}}'.\", id));", propertyCollection.Name).AppendLine()
  269. .AppendLine(" }")
  270. .AppendLine()
  271. .AppendFormat(" public {1} Get{0}At(int index)", propertyCollection.Name, propertyCollection.LanguageKeyword).AppendLine()
  272. .AppendLine(" {")
  273. .AppendFormat(" if (index < 0 || index >= m_{0}.Length)", propertyCollection.Name).AppendLine()
  274. .AppendLine(" {")
  275. .AppendFormat(" throw new GameFrameworkException(Utility.Text.Format(\"Get{0}At with invalid index '{{0}}'.\", index));", propertyCollection.Name).AppendLine()
  276. .AppendLine(" }")
  277. .AppendLine()
  278. .AppendFormat(" return m_{0}[index].Value;", propertyCollection.Name).AppendLine()
  279. .Append(" }");
  280. }
  281. if (propertyCollections.Count > 0)
  282. {
  283. stringBuilder.AppendLine().AppendLine();
  284. }
  285. stringBuilder
  286. .AppendLine(" private void GeneratePropertyArray()")
  287. .AppendLine(" {");
  288. firstProperty = true;
  289. foreach (PropertyCollection propertyCollection in propertyCollections)
  290. {
  291. if (firstProperty)
  292. {
  293. firstProperty = false;
  294. }
  295. else
  296. {
  297. stringBuilder.AppendLine().AppendLine();
  298. }
  299. stringBuilder
  300. .AppendFormat(" m_{0} = new KeyValuePair<int, {1}>[]", propertyCollection.Name, propertyCollection.LanguageKeyword).AppendLine()
  301. .AppendLine(" {");
  302. int itemCount = propertyCollection.ItemCount;
  303. for (int i = 0; i < itemCount; i++)
  304. {
  305. KeyValuePair<int, string> item = propertyCollection.GetItem(i);
  306. stringBuilder.AppendFormat(" new KeyValuePair<int, {0}>({1}, {2}),", propertyCollection.LanguageKeyword, item.Key.ToString(), item.Value).AppendLine();
  307. }
  308. stringBuilder.Append(" };");
  309. }
  310. stringBuilder
  311. .AppendLine()
  312. .Append(" }");
  313. return stringBuilder.ToString();
  314. }
  315. private sealed class PropertyCollection
  316. {
  317. private readonly string m_Name;
  318. private readonly string m_LanguageKeyword;
  319. private readonly List<KeyValuePair<int, string>> m_Items;
  320. public PropertyCollection(string name, string languageKeyword)
  321. {
  322. m_Name = name;
  323. m_LanguageKeyword = languageKeyword;
  324. m_Items = new List<KeyValuePair<int, string>>();
  325. }
  326. public string Name
  327. {
  328. get
  329. {
  330. return m_Name;
  331. }
  332. }
  333. public string LanguageKeyword
  334. {
  335. get
  336. {
  337. return m_LanguageKeyword;
  338. }
  339. }
  340. public int ItemCount
  341. {
  342. get
  343. {
  344. return m_Items.Count;
  345. }
  346. }
  347. public KeyValuePair<int, string> GetItem(int index)
  348. {
  349. if (index < 0 || index >= m_Items.Count)
  350. {
  351. throw new GameFrameworkException(Utility.Text.Format("GetItem with invalid index '{0}'.", index));
  352. }
  353. return m_Items[index];
  354. }
  355. public void AddItem(int id, string propertyName)
  356. {
  357. m_Items.Add(new KeyValuePair<int, string>(id, propertyName));
  358. }
  359. }
  360. }
  361. }