TableHelper.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using XRTool.Util;
  7. namespace PublicTools.XMLDataBase
  8. {
  9. /// <summary>
  10. /// 一个辅助读写的脚本
  11. /// 提供相关类型的读写操作
  12. /// </summary>
  13. public class TableHelper<T>
  14. {
  15. private TableInterface table;
  16. private string path;
  17. private List<T> dataList;
  18. public event Action readConfComplete;
  19. public bool isInit = false;
  20. private Dictionary<string, T> dataDic = new Dictionary<string, T>();
  21. private string priKey;
  22. /// <summary>
  23. /// 是否可写,默认可写
  24. /// </summary>
  25. private bool isWrite = true;
  26. public TableHelper(string path, string priKey = null)
  27. {
  28. this.path = path;
  29. this.priKey = priKey;
  30. readConfComplete += OnRreadConfComplete;
  31. }
  32. /// <summary>
  33. /// 读取完成之后
  34. /// </summary>
  35. private void OnRreadConfComplete()
  36. {
  37. if (DataList != null)
  38. {
  39. for (int i = 0; i < DataList.Count; i++)
  40. {
  41. if (string.IsNullOrEmpty(priKey))
  42. {
  43. if (DataList[i] is TableBase)
  44. {
  45. var da = DataList[i] as TableBase;
  46. if (!dataDic.ContainsKey(da.priKey))
  47. {
  48. dataDic.Add(da.priKey, DataList[i]);
  49. }
  50. }
  51. else if (DataList[i] is UnityEngine.Object)
  52. {
  53. var da = DataList[i] as UnityEngine.Object;
  54. if (!dataDic.ContainsKey(da.name))
  55. {
  56. dataDic.Add(da.name, DataList[i]);
  57. }
  58. }
  59. }
  60. else
  61. {
  62. string key = ClazzFactory.GetPriKey<T>(DataList[i], priKey);
  63. if (!dataDic.ContainsKey(key))
  64. {
  65. dataDic.Add(key, DataList[i]);
  66. }
  67. }
  68. }
  69. }
  70. isInit = true;
  71. }
  72. public List<T> DataList { get => dataList; set => dataList = value; }
  73. /// <summary>
  74. /// 打开指定的数据
  75. /// </summary>
  76. /// <param name="priKey"></param>
  77. public void Open(string priKey = null)
  78. {
  79. if (!string.IsNullOrEmpty(priKey))
  80. {
  81. this.priKey = priKey;
  82. }
  83. isWrite = true;
  84. if (path.StartsWith(Application.streamingAssetsPath))
  85. {
  86. #if !UNITY_EDITOR
  87. isWrite = false;
  88. #endif
  89. }
  90. if (isWrite)
  91. {
  92. table = XSql.Instance.OpenTable(path, typeof(T).Name, ".xml", true);
  93. if (!table.Open())
  94. {
  95. table.Create(typeof(T).Name);
  96. }
  97. this.DataList = table.FindAllData<T>();
  98. readConfComplete?.Invoke();
  99. }
  100. else
  101. {
  102. if (TimerMgr.ForceInstance())
  103. {
  104. string fullPath = Path.Combine(path, typeof(T).Name + ".xml");
  105. TimerMgr.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List<T> scrollList) =>
  106. {
  107. this.DataList = scrollList;
  108. readConfComplete?.Invoke();
  109. }));
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// 添加一条配置
  115. /// </summary>
  116. /// <param name="conf"></param>
  117. public void AddData(T conf, bool isSave = true)
  118. {
  119. if (table == null)
  120. {
  121. Open(priKey);
  122. }
  123. if (table != null)
  124. {
  125. if (string.IsNullOrEmpty(priKey))
  126. {
  127. if (conf is TableBase)
  128. {
  129. var tab = conf as TableBase;
  130. if (table.InsertData(conf, tab.priKey))
  131. {
  132. if (isSave)
  133. table.Save();
  134. UnityLog.Log("Add Success!" + tab.priKey, 2);
  135. }
  136. else
  137. {
  138. UnityLog.LogError("Add DataConf Error!" + conf);
  139. }
  140. }
  141. else if (conf is UnityEngine.Object)
  142. {
  143. var tab = conf as UnityEngine.Object;
  144. if (table.InsertData(conf, tab.name))
  145. {
  146. if (isSave)
  147. table.Save();
  148. UnityLog.Log("Add Success!" + tab.name, 2);
  149. }
  150. else
  151. {
  152. UnityLog.LogError("Add DataConf Error!" + conf);
  153. }
  154. }
  155. else
  156. {
  157. if (table.InsertData(conf))
  158. {
  159. if (isSave)
  160. table.Save();
  161. UnityLog.Log("Add Success!" + conf, 2);
  162. }
  163. else
  164. {
  165. UnityLog.LogError("Add DataConf Error!" + conf);
  166. }
  167. }
  168. }
  169. else
  170. {
  171. string key = ClazzFactory.GetPriKey<T>(conf, priKey);
  172. if (table.InsertData(conf, key))
  173. {
  174. if (isSave)
  175. table.Save();
  176. UnityLog.Log("Add Success!" + conf + key, 2);
  177. }
  178. else
  179. {
  180. UnityLog.LogError("Add DataConf Error!" + conf);
  181. }
  182. }
  183. }
  184. else
  185. {
  186. UnityLog.LogError("Add DataConf Error!No table" + conf);
  187. }
  188. }
  189. /// <summary>
  190. /// 删除这条配置
  191. /// </summary>
  192. /// <param name="key"></param>
  193. public void DelConf(string key, bool isSave = true)
  194. {
  195. if (table != null)
  196. {
  197. table.DeleteData<T>(key);
  198. if (isSave)
  199. table.Save();
  200. }
  201. Open(key);
  202. }
  203. /// <summary>
  204. /// 根据key值查找数据
  205. /// 如果缓存列表里没有这条记录,则去循环列表中查找
  206. /// </summary>
  207. /// <param name="name"></param>
  208. /// <returns></returns>
  209. public T FindData(string name, string priKey = null)
  210. {
  211. if (string.IsNullOrEmpty(priKey))
  212. {
  213. priKey = this.priKey;
  214. }
  215. if (!string.IsNullOrEmpty(name))
  216. {
  217. if (dataDic != null && dataDic.ContainsKey(name))
  218. {
  219. return dataDic[name];
  220. }
  221. else
  222. {
  223. ///存在主键值
  224. if (!string.IsNullOrEmpty(priKey))
  225. {
  226. return DataList.Find((data) =>
  227. {
  228. if (name == ClazzFactory.GetPriKey<T>(data, priKey))
  229. {
  230. return true;
  231. }
  232. return false;
  233. });
  234. }
  235. else
  236. {
  237. ///暴力破解法,很少使用,请尽量避免使用此分支
  238. UnityLog.LogError("Use Wrong method!");
  239. return DataList.Find((data) =>
  240. {
  241. var dic = ClazzFactory.ClazzAnalyze<T>(data);
  242. if (dic != null)
  243. {
  244. foreach (var item in dic)
  245. {
  246. if (item.Value == name)
  247. {
  248. return true;
  249. }
  250. }
  251. }
  252. return false;
  253. });
  254. }
  255. }
  256. }
  257. return default;
  258. }
  259. }
  260. }