using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using XRTool.Util; namespace PublicTools.XMLDataBase { /// /// 一个辅助读写的脚本 /// 提供相关类型的读写操作 /// public class TableHelper { private TableInterface table; private string path; private List dataList; public event Action readConfComplete; public bool isInit = false; private Dictionary dataDic = new Dictionary(); private string priKey; /// /// 是否可写,默认可写 /// private bool isWrite = true; public TableHelper(string path, string priKey = null) { this.path = path; this.priKey = priKey; readConfComplete += OnRreadConfComplete; } /// /// 读取完成之后 /// private void OnRreadConfComplete() { if (DataList != null) { for (int i = 0; i < DataList.Count; i++) { if (string.IsNullOrEmpty(priKey)) { if (DataList[i] is TableBase) { var da = DataList[i] as TableBase; if (!dataDic.ContainsKey(da.priKey)) { dataDic.Add(da.priKey, DataList[i]); } } else if (DataList[i] is UnityEngine.Object) { var da = DataList[i] as UnityEngine.Object; if (!dataDic.ContainsKey(da.name)) { dataDic.Add(da.name, DataList[i]); } } } else { string key = ClazzFactory.GetPriKey(DataList[i], priKey); if (!dataDic.ContainsKey(key)) { dataDic.Add(key, DataList[i]); } } } } isInit = true; } public void Reset(bool isOpen = true) { if (DataList != null) { DataList.Clear(); } isInit = false; DataList = null; dataDic.Clear(); if (table != null) { table.Close(); } XSql.Instance.CloseTable(typeof(T).Name); if (isOpen) { Open(); } } public void Close() { if (table != null) { table.Close(); } XSql.Instance.CloseTable(typeof(T).Name); } public List DataList { get => dataList; set => dataList = value; } /// /// 打开指定的数据 /// /// public void Open(string priKey = null) { if (!string.IsNullOrEmpty(priKey)) { this.priKey = priKey; } isWrite = true; ///如果路径为本地只读路径,同时是安卓平台时,要走异步处理 if (path.StartsWith(Application.streamingAssetsPath)) { #if !UNITY_EDITOR isWrite = false; #endif } if (isWrite) { table = XSql.Instance.OpenTable(path, typeof(T).Name, ".xml", true); if (!table.Open()) { table.Create(typeof(T).Name); } this.DataList = table.FindAllData(); readConfComplete?.Invoke(); } else { if (TimerMgr.ForceInstance()) { string fullPath = Path.Combine(path, typeof(T).Name + ".xml"); TimerMgr.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List scrollList) => { this.DataList = scrollList; readConfComplete?.Invoke(); })); } } } /// /// 添加一条配置 /// /// public bool AddData(T conf, bool isSave = true) { bool isAddSucess = false; if (table == null) { Open(priKey); } if (table != null) { if (string.IsNullOrEmpty(priKey)) { if (conf is TableBase) { var tab = conf as TableBase; if (table.InsertData(conf, tab.priKey)) { if (isSave) table.Save(); UnityLog.Log("Add Success!" + tab.priKey, 2); isAddSucess = true; } else { UnityLog.LogError("Add DataConf Error!" + conf); } } else if (conf is UnityEngine.Object) { var tab = conf as UnityEngine.Object; if (table.InsertData(conf, tab.name)) { if (isSave) table.Save(); isAddSucess = true; UnityLog.Log("Add Success!" + tab.name, 2); } else { UnityLog.LogError("Add DataConf Error!" + conf); } } else { if (table.InsertData(conf)) { if (isSave) table.Save(); UnityLog.Log("Add Success!" + conf, 2); isAddSucess = true; } else { //UnityLog.LogError("Add DataConf Error!" + conf); } } } else { string key = ClazzFactory.GetPriKey(conf, priKey); if (table.InsertData(conf, key)) { if (isSave) table.Save(); UnityLog.Log("Add Success!" + conf + key, 2); isAddSucess = true; } else { UnityLog.LogError("Add DataConf Error!" + conf); } } } else { UnityLog.LogError("Add DataConf Error!No table" + conf); } if (isAddSucess) { if (DataList == null) { DataList = new List(); } if (!DataList.Contains(conf)) { DataList.Add(conf); } if (dataDic == null) { dataDic = new Dictionary(); } if (!dataDic.ContainsKey(priKey)) { dataDic.Add(priKey, conf); } } return isAddSucess; } /// /// 清空表格 /// public void DelAll() { if (table != null) { table.DeleteAllData(); table.Save(); } Reset(); } /// /// 删除这条配置 /// /// public void DelConf(string key, bool isSave = true) { T t = FindData(key); if (t != null) { DataList.Remove(t); dataDic.Remove(key); } table.DeleteData(key); if (isSave) { table.Save(); } } public void SaveData() { table?.Save(); } /// /// 根据key值查找数据 /// 如果缓存列表里没有这条记录,则去循环列表中查找 /// /// /// public T FindData(string name, string priKey = null) { ///按主键进行查找 if (string.IsNullOrEmpty(priKey)) { priKey = this.priKey; } if (!string.IsNullOrEmpty(name)) { if (dataDic != null && dataDic.ContainsKey(name)) { return dataDic[name]; } else { ///存在主键值 if (!string.IsNullOrEmpty(priKey)) { if (DataList == null || DataList.Count < 1) { return default(T); } return DataList.Find((data) => { if (name == ClazzFactory.GetPriKey(data, priKey)) { return true; } return false; }); } else { ///暴力破解法,很少使用,请尽量避免使用此分支 UnityLog.LogError("Use Wrong method!"); return DataList.Find((data) => { var dic = ClazzFactory.ClazzAnalyze(data); if (dic != null) { foreach (var item in dic) { if (item.Value == name) { return true; } } } return false; }); } } } return default; } } }