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 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 void AddData(T conf, bool isSave = true) { 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); } 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(); 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); } 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); } else { UnityLog.LogError("Add DataConf Error!" + conf); } } } else { UnityLog.LogError("Add DataConf Error!No table" + conf); } } /// /// 删除这条配置 /// /// public void DelConf(string key, bool isSave = true) { if (table != null) { table.DeleteData(key); if (isSave) table.Save(); } Open(key); } /// /// 根据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)) { 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; } } }