TableHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 void Reset(bool isOpen = true)
  73. {
  74. if (DataList != null)
  75. {
  76. DataList.Clear();
  77. }
  78. isInit = false;
  79. DataList = null;
  80. dataDic.Clear();
  81. if (table != null)
  82. {
  83. table.Close();
  84. }
  85. XSql.Instance.CloseTable(typeof(T).Name);
  86. if (isOpen)
  87. {
  88. Open();
  89. }
  90. }
  91. public void Close()
  92. {
  93. if (table != null)
  94. {
  95. table.Close();
  96. }
  97. XSql.Instance.CloseTable(typeof(T).Name);
  98. }
  99. public List<T> DataList { get => dataList; set => dataList = value; }
  100. /// <summary>
  101. /// 打开指定的数据
  102. /// </summary>
  103. /// <param name="priKey"></param>
  104. public void Open(string priKey = null)
  105. {
  106. if (!string.IsNullOrEmpty(priKey))
  107. {
  108. this.priKey = priKey;
  109. }
  110. isWrite = true;
  111. ///如果路径为本地只读路径,同时是安卓平台时,要走异步处理
  112. if (path.StartsWith(Application.streamingAssetsPath))
  113. {
  114. #if !UNITY_EDITOR
  115. isWrite = false;
  116. #endif
  117. }
  118. if (isWrite)
  119. {
  120. table = XSql.Instance.OpenTable(path, typeof(T).Name, ".xml", true);
  121. if (!table.Open())
  122. {
  123. table.Create(typeof(T).Name);
  124. }
  125. this.DataList = table.FindAllData<T>();
  126. readConfComplete?.Invoke();
  127. }
  128. else
  129. {
  130. if (TimerMgr.ForceInstance())
  131. {
  132. string fullPath = Path.Combine(path, typeof(T).Name + ".xml");
  133. TimerMgr.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List<T> scrollList) =>
  134. {
  135. this.DataList = scrollList;
  136. readConfComplete?.Invoke();
  137. }));
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// 添加一条配置
  143. /// </summary>
  144. /// <param name="conf"></param>
  145. public bool AddData(T conf, bool isSave = true)
  146. {
  147. bool isAddSucess = false;
  148. if (table == null)
  149. {
  150. Open(priKey);
  151. }
  152. if (table != null)
  153. {
  154. if (string.IsNullOrEmpty(priKey))
  155. {
  156. if (conf is TableBase)
  157. {
  158. var tab = conf as TableBase;
  159. if (table.InsertData(conf, tab.priKey))
  160. {
  161. if (isSave)
  162. table.Save();
  163. UnityLog.Log("Add Success!" + tab.priKey, 2);
  164. isAddSucess = true;
  165. }
  166. else
  167. {
  168. UnityLog.LogError("Add DataConf Error!" + conf);
  169. }
  170. }
  171. else if (conf is UnityEngine.Object)
  172. {
  173. var tab = conf as UnityEngine.Object;
  174. if (table.InsertData(conf, tab.name))
  175. {
  176. if (isSave)
  177. table.Save();
  178. isAddSucess = true;
  179. UnityLog.Log("Add Success!" + tab.name, 2);
  180. }
  181. else
  182. {
  183. UnityLog.LogError("Add DataConf Error!" + conf);
  184. }
  185. }
  186. else
  187. {
  188. if (table.InsertData(conf))
  189. {
  190. if (isSave)
  191. table.Save();
  192. UnityLog.Log("Add Success!" + conf, 2);
  193. isAddSucess = true;
  194. }
  195. else
  196. {
  197. //UnityLog.LogError("Add DataConf Error!" + conf);
  198. }
  199. }
  200. }
  201. else
  202. {
  203. string key = ClazzFactory.GetPriKey<T>(conf, priKey);
  204. if (table.InsertData(conf, key))
  205. {
  206. if (isSave)
  207. table.Save();
  208. UnityLog.Log("Add Success!" + conf + key, 2);
  209. isAddSucess = true;
  210. }
  211. else
  212. {
  213. UnityLog.LogError("Add DataConf Error!" + conf);
  214. }
  215. }
  216. }
  217. else
  218. {
  219. UnityLog.LogError("Add DataConf Error!No table" + conf);
  220. }
  221. if (isAddSucess)
  222. {
  223. if (DataList == null)
  224. {
  225. DataList = new List<T>();
  226. }
  227. if (!DataList.Contains(conf))
  228. {
  229. DataList.Add(conf);
  230. }
  231. if (dataDic == null)
  232. {
  233. dataDic = new Dictionary<string, T>();
  234. }
  235. if (!dataDic.ContainsKey(priKey))
  236. {
  237. dataDic.Add(priKey, conf);
  238. }
  239. }
  240. return isAddSucess;
  241. }
  242. /// <summary>
  243. /// 清空表格
  244. /// </summary>
  245. public void DelAll()
  246. {
  247. if (table != null)
  248. {
  249. table.DeleteAllData<T>();
  250. table.Save();
  251. }
  252. Reset();
  253. }
  254. /// <summary>
  255. /// 删除这条配置
  256. /// </summary>
  257. /// <param name="key"></param>
  258. public void DelConf(string key, bool isSave = true)
  259. {
  260. T t = FindData(key);
  261. if (t != null)
  262. {
  263. DataList.Remove(t);
  264. dataDic.Remove(key);
  265. }
  266. table.DeleteData<T>(key);
  267. if (isSave)
  268. {
  269. table.Save();
  270. }
  271. }
  272. public void SaveData()
  273. {
  274. table?.Save();
  275. }
  276. /// <summary>
  277. /// 根据key值查找数据
  278. /// 如果缓存列表里没有这条记录,则去循环列表中查找
  279. /// </summary>
  280. /// <param name="name"></param>
  281. /// <returns></returns>
  282. public T FindData(string name, string priKey = null)
  283. {
  284. ///按主键进行查找
  285. if (string.IsNullOrEmpty(priKey))
  286. {
  287. priKey = this.priKey;
  288. }
  289. if (!string.IsNullOrEmpty(name))
  290. {
  291. if (dataDic != null && dataDic.ContainsKey(name))
  292. {
  293. return dataDic[name];
  294. }
  295. else
  296. {
  297. ///存在主键值
  298. if (!string.IsNullOrEmpty(priKey))
  299. {
  300. if (DataList == null || DataList.Count < 1)
  301. {
  302. return default(T);
  303. }
  304. return DataList.Find((data) =>
  305. {
  306. if (name == ClazzFactory.GetPriKey<T>(data, priKey))
  307. {
  308. return true;
  309. }
  310. return false;
  311. });
  312. }
  313. else
  314. {
  315. ///暴力破解法,很少使用,请尽量避免使用此分支
  316. UnityLog.LogError("Use Wrong method!");
  317. return DataList.Find((data) =>
  318. {
  319. var dic = ClazzFactory.ClazzAnalyze<T>(data);
  320. if (dic != null)
  321. {
  322. foreach (var item in dic)
  323. {
  324. if (item.Value == name)
  325. {
  326. return true;
  327. }
  328. }
  329. }
  330. return false;
  331. });
  332. }
  333. }
  334. }
  335. return default;
  336. }
  337. }
  338. }