TableHelper.cs 9.5 KB

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