DataTableManager.DataTable.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. namespace GameFramework.DataTable
  11. {
  12. internal sealed partial class DataTableManager : GameFrameworkModule, IDataTableManager
  13. {
  14. /// <summary>
  15. /// 数据表。
  16. /// </summary>
  17. /// <typeparam name="T">数据表行的类型。</typeparam>
  18. private sealed class DataTable<T> : DataTableBase, IDataTable<T> where T : class, IDataRow, new()
  19. {
  20. private readonly Dictionary<int, T> m_DataSet;
  21. private T m_MinIdDataRow;
  22. private T m_MaxIdDataRow;
  23. /// <summary>
  24. /// 初始化数据表的新实例。
  25. /// </summary>
  26. /// <param name="name">数据表名称。</param>
  27. public DataTable(string name)
  28. : base(name)
  29. {
  30. m_DataSet = new Dictionary<int, T>();
  31. m_MinIdDataRow = null;
  32. m_MaxIdDataRow = null;
  33. }
  34. /// <summary>
  35. /// 获取数据表行的类型。
  36. /// </summary>
  37. public override Type Type
  38. {
  39. get
  40. {
  41. return typeof(T);
  42. }
  43. }
  44. /// <summary>
  45. /// 获取数据表行数。
  46. /// </summary>
  47. public override int Count
  48. {
  49. get
  50. {
  51. return m_DataSet.Count;
  52. }
  53. }
  54. /// <summary>
  55. /// 获取数据表行。
  56. /// </summary>
  57. /// <param name="id">数据表行的编号。</param>
  58. /// <returns>数据表行。</returns>
  59. public T this[int id]
  60. {
  61. get
  62. {
  63. return GetDataRow(id);
  64. }
  65. }
  66. /// <summary>
  67. /// 获取编号最小的数据表行。
  68. /// </summary>
  69. public T MinIdDataRow
  70. {
  71. get
  72. {
  73. return m_MinIdDataRow;
  74. }
  75. }
  76. /// <summary>
  77. /// 获取编号最大的数据表行。
  78. /// </summary>
  79. public T MaxIdDataRow
  80. {
  81. get
  82. {
  83. return m_MaxIdDataRow;
  84. }
  85. }
  86. /// <summary>
  87. /// 检查是否存在数据表行。
  88. /// </summary>
  89. /// <param name="id">数据表行的编号。</param>
  90. /// <returns>是否存在数据表行。</returns>
  91. public override bool HasDataRow(int id)
  92. {
  93. return m_DataSet.ContainsKey(id);
  94. }
  95. /// <summary>
  96. /// 检查是否存在数据表行。
  97. /// </summary>
  98. /// <param name="condition">要检查的条件。</param>
  99. /// <returns>是否存在数据表行。</returns>
  100. public bool HasDataRow(Predicate<T> condition)
  101. {
  102. if (condition == null)
  103. {
  104. throw new GameFrameworkException("Condition is invalid.");
  105. }
  106. foreach (KeyValuePair<int, T> dataRow in m_DataSet)
  107. {
  108. if (condition(dataRow.Value))
  109. {
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. /// <summary>
  116. /// 获取数据表行。
  117. /// </summary>
  118. /// <param name="id">数据表行的编号。</param>
  119. /// <returns>数据表行。</returns>
  120. public T GetDataRow(int id)
  121. {
  122. T dataRow = null;
  123. if (m_DataSet.TryGetValue(id, out dataRow))
  124. {
  125. return dataRow;
  126. }
  127. return null;
  128. }
  129. /// <summary>
  130. /// 获取符合条件的数据表行。
  131. /// </summary>
  132. /// <param name="condition">要检查的条件。</param>
  133. /// <returns>符合条件的数据表行。</returns>
  134. /// <remarks>当存在多个符合条件的数据表行时,仅返回第一个符合条件的数据表行。</remarks>
  135. public T GetDataRow(Predicate<T> condition)
  136. {
  137. if (condition == null)
  138. {
  139. throw new GameFrameworkException("Condition is invalid.");
  140. }
  141. foreach (KeyValuePair<int, T> dataRow in m_DataSet)
  142. {
  143. if (condition(dataRow.Value))
  144. {
  145. return dataRow.Value;
  146. }
  147. }
  148. return null;
  149. }
  150. /// <summary>
  151. /// 获取符合条件的数据表行。
  152. /// </summary>
  153. /// <param name="condition">要检查的条件。</param>
  154. /// <returns>符合条件的数据表行。</returns>
  155. public T[] GetDataRows(Predicate<T> condition)
  156. {
  157. if (condition == null)
  158. {
  159. throw new GameFrameworkException("Condition is invalid.");
  160. }
  161. List<T> results = new List<T>();
  162. foreach (KeyValuePair<int, T> dataRow in m_DataSet)
  163. {
  164. if (condition(dataRow.Value))
  165. {
  166. results.Add(dataRow.Value);
  167. }
  168. }
  169. return results.ToArray();
  170. }
  171. /// <summary>
  172. /// 获取符合条件的数据表行。
  173. /// </summary>
  174. /// <param name="condition">要检查的条件。</param>
  175. /// <param name="results">符合条件的数据表行。</param>
  176. public void GetDataRows(Predicate<T> condition, List<T> results)
  177. {
  178. if (condition == null)
  179. {
  180. throw new GameFrameworkException("Condition is invalid.");
  181. }
  182. if (results == null)
  183. {
  184. throw new GameFrameworkException("Results is invalid.");
  185. }
  186. results.Clear();
  187. foreach (KeyValuePair<int, T> dataRow in m_DataSet)
  188. {
  189. if (condition(dataRow.Value))
  190. {
  191. results.Add(dataRow.Value);
  192. }
  193. }
  194. }
  195. /// <summary>
  196. /// 获取排序后的数据表行。
  197. /// </summary>
  198. /// <param name="comparison">要排序的条件。</param>
  199. /// <returns>排序后的数据表行。</returns>
  200. public T[] GetDataRows(Comparison<T> comparison)
  201. {
  202. if (comparison == null)
  203. {
  204. throw new GameFrameworkException("Comparison is invalid.");
  205. }
  206. List<T> results = new List<T>();
  207. foreach (KeyValuePair<int, T> dataRow in m_DataSet)
  208. {
  209. results.Add(dataRow.Value);
  210. }
  211. results.Sort(comparison);
  212. return results.ToArray();
  213. }
  214. /// <summary>
  215. /// 获取排序后的数据表行。
  216. /// </summary>
  217. /// <param name="comparison">要排序的条件。</param>
  218. /// <param name="results">排序后的数据表行。</param>
  219. public void GetDataRows(Comparison<T> comparison, List<T> results)
  220. {
  221. if (comparison == null)
  222. {
  223. throw new GameFrameworkException("Comparison is invalid.");
  224. }
  225. if (results == null)
  226. {
  227. throw new GameFrameworkException("Results is invalid.");
  228. }
  229. results.Clear();
  230. foreach (KeyValuePair<int, T> dataRow in m_DataSet)
  231. {
  232. results.Add(dataRow.Value);
  233. }
  234. results.Sort(comparison);
  235. }
  236. /// <summary>
  237. /// 获取排序后的符合条件的数据表行。
  238. /// </summary>
  239. /// <param name="condition">要检查的条件。</param>
  240. /// <param name="comparison">要排序的条件。</param>
  241. /// <returns>排序后的符合条件的数据表行。</returns>
  242. public T[] GetDataRows(Predicate<T> condition, Comparison<T> comparison)
  243. {
  244. if (condition == null)
  245. {
  246. throw new GameFrameworkException("Condition is invalid.");
  247. }
  248. if (comparison == null)
  249. {
  250. throw new GameFrameworkException("Comparison is invalid.");
  251. }
  252. List<T> results = new List<T>();
  253. foreach (KeyValuePair<int, T> dataRow in m_DataSet)
  254. {
  255. if (condition(dataRow.Value))
  256. {
  257. results.Add(dataRow.Value);
  258. }
  259. }
  260. results.Sort(comparison);
  261. return results.ToArray();
  262. }
  263. /// <summary>
  264. /// 获取排序后的符合条件的数据表行。
  265. /// </summary>
  266. /// <param name="condition">要检查的条件。</param>
  267. /// <param name="comparison">要排序的条件。</param>
  268. /// <param name="results">排序后的符合条件的数据表行。</param>
  269. public void GetDataRows(Predicate<T> condition, Comparison<T> comparison, List<T> results)
  270. {
  271. if (condition == null)
  272. {
  273. throw new GameFrameworkException("Condition is invalid.");
  274. }
  275. if (comparison == null)
  276. {
  277. throw new GameFrameworkException("Comparison is invalid.");
  278. }
  279. if (results == null)
  280. {
  281. throw new GameFrameworkException("Results is invalid.");
  282. }
  283. results.Clear();
  284. foreach (KeyValuePair<int, T> dataRow in m_DataSet)
  285. {
  286. if (condition(dataRow.Value))
  287. {
  288. results.Add(dataRow.Value);
  289. }
  290. }
  291. results.Sort(comparison);
  292. }
  293. /// <summary>
  294. /// 获取所有数据表行。
  295. /// </summary>
  296. /// <returns>所有数据表行。</returns>
  297. public T[] GetAllDataRows()
  298. {
  299. int index = 0;
  300. T[] results = new T[m_DataSet.Count];
  301. foreach (KeyValuePair<int, T> dataRow in m_DataSet)
  302. {
  303. results[index++] = dataRow.Value;
  304. }
  305. return results;
  306. }
  307. /// <summary>
  308. /// 获取所有数据表行。
  309. /// </summary>
  310. /// <param name="results">所有数据表行。</param>
  311. public void GetAllDataRows(List<T> results)
  312. {
  313. if (results == null)
  314. {
  315. throw new GameFrameworkException("Results is invalid.");
  316. }
  317. results.Clear();
  318. foreach (KeyValuePair<int, T> dataRow in m_DataSet)
  319. {
  320. results.Add(dataRow.Value);
  321. }
  322. }
  323. /// <summary>
  324. /// 增加数据表行。
  325. /// </summary>
  326. /// <param name="dataRowString">要解析的数据表行字符串。</param>
  327. /// <param name="userData">用户自定义数据。</param>
  328. /// <returns>是否增加数据表行成功。</returns>
  329. public override bool AddDataRow(string dataRowString, object userData)
  330. {
  331. try
  332. {
  333. T dataRow = new T();
  334. if (!dataRow.ParseDataRow(dataRowString, userData))
  335. {
  336. return false;
  337. }
  338. InternalAddDataRow(dataRow);
  339. return true;
  340. }
  341. catch (Exception exception)
  342. {
  343. if (exception is GameFrameworkException)
  344. {
  345. throw;
  346. }
  347. throw new GameFrameworkException(Utility.Text.Format("Can not parse data row string for data table '{0}' with exception '{1}'.", new TypeNamePair(typeof(T), Name), exception), exception);
  348. }
  349. }
  350. /// <summary>
  351. /// 增加数据表行。
  352. /// </summary>
  353. /// <param name="dataRowBytes">要解析的数据表行二进制流。</param>
  354. /// <param name="startIndex">数据表行二进制流的起始位置。</param>
  355. /// <param name="length">数据表行二进制流的长度。</param>
  356. /// <param name="userData">用户自定义数据。</param>
  357. /// <returns>是否增加数据表行成功。</returns>
  358. public override bool AddDataRow(byte[] dataRowBytes, int startIndex, int length, object userData)
  359. {
  360. try
  361. {
  362. T dataRow = new T();
  363. if (!dataRow.ParseDataRow(dataRowBytes, startIndex, length, userData))
  364. {
  365. return false;
  366. }
  367. InternalAddDataRow(dataRow);
  368. return true;
  369. }
  370. catch (Exception exception)
  371. {
  372. if (exception is GameFrameworkException)
  373. {
  374. throw;
  375. }
  376. throw new GameFrameworkException(Utility.Text.Format("Can not parse data row bytes for data table '{0}' with exception '{1}'.", new TypeNamePair(typeof(T), Name), exception), exception);
  377. }
  378. }
  379. /// <summary>
  380. /// 移除指定数据表行。
  381. /// </summary>
  382. /// <param name="id">要移除数据表行的编号。</param>
  383. /// <returns>是否移除数据表行成功。</returns>
  384. public override bool RemoveDataRow(int id)
  385. {
  386. if (!HasDataRow(id))
  387. {
  388. return false;
  389. }
  390. if (!m_DataSet.Remove(id))
  391. {
  392. return false;
  393. }
  394. if (m_MinIdDataRow != null && m_MinIdDataRow.Id == id || m_MaxIdDataRow != null && m_MaxIdDataRow.Id == id)
  395. {
  396. m_MinIdDataRow = null;
  397. m_MaxIdDataRow = null;
  398. foreach (KeyValuePair<int, T> dataRow in m_DataSet)
  399. {
  400. if (m_MinIdDataRow == null || m_MinIdDataRow.Id > dataRow.Key)
  401. {
  402. m_MinIdDataRow = dataRow.Value;
  403. }
  404. if (m_MaxIdDataRow == null || m_MaxIdDataRow.Id < dataRow.Key)
  405. {
  406. m_MaxIdDataRow = dataRow.Value;
  407. }
  408. }
  409. }
  410. return true;
  411. }
  412. /// <summary>
  413. /// 清空所有数据表行。
  414. /// </summary>
  415. public override void RemoveAllDataRows()
  416. {
  417. m_DataSet.Clear();
  418. m_MinIdDataRow = null;
  419. m_MaxIdDataRow = null;
  420. }
  421. /// <summary>
  422. /// 返回循环访问集合的枚举数。
  423. /// </summary>
  424. /// <returns>循环访问集合的枚举数。</returns>
  425. public IEnumerator<T> GetEnumerator()
  426. {
  427. return m_DataSet.Values.GetEnumerator();
  428. }
  429. /// <summary>
  430. /// 返回循环访问集合的枚举数。
  431. /// </summary>
  432. /// <returns>循环访问集合的枚举数。</returns>
  433. IEnumerator IEnumerable.GetEnumerator()
  434. {
  435. return m_DataSet.Values.GetEnumerator();
  436. }
  437. /// <summary>
  438. /// 关闭并清理数据表。
  439. /// </summary>
  440. internal override void Shutdown()
  441. {
  442. m_DataSet.Clear();
  443. }
  444. private void InternalAddDataRow(T dataRow)
  445. {
  446. if (HasDataRow(dataRow.Id))
  447. {
  448. throw new GameFrameworkException(Utility.Text.Format("Already exist '{0}' in data table '{1}'.", dataRow.Id, new TypeNamePair(typeof(T), Name)));
  449. }
  450. m_DataSet.Add(dataRow.Id, dataRow);
  451. if (m_MinIdDataRow == null || m_MinIdDataRow.Id > dataRow.Id)
  452. {
  453. m_MinIdDataRow = dataRow;
  454. }
  455. if (m_MaxIdDataRow == null || m_MaxIdDataRow.Id < dataRow.Id)
  456. {
  457. m_MaxIdDataRow = dataRow;
  458. }
  459. }
  460. }
  461. }
  462. }