123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using XRTool.Util;
- namespace PublicTools.XMLDataBase
- {
- /// <summary>
- /// 一个辅助读写的脚本
- /// 提供相关类型的读写操作
- /// </summary>
- public class TableHelper<T>
- {
- private TableInterface table;
- private string path;
- private List<T> dataList;
- public event Action readConfComplete;
- public bool isInit = false;
- private Dictionary<string, T> dataDic = new Dictionary<string, T>();
- private string priKey;
- /// <summary>
- /// 是否可写,默认可写
- /// </summary>
- private bool isWrite = true;
- public TableHelper(string path, string priKey = null)
- {
- this.path = path;
- this.priKey = priKey;
- readConfComplete += OnRreadConfComplete;
- }
- /// <summary>
- /// 读取完成之后
- /// </summary>
- 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<T>(DataList[i], priKey);
- if (!dataDic.ContainsKey(key))
- {
- dataDic.Add(key, DataList[i]);
- }
- }
- }
- }
- isInit = true;
- }
- public List<T> DataList { get => dataList; set => dataList = value; }
- /// <summary>
- /// 打开指定的数据
- /// </summary>
- /// <param name="priKey"></param>
- 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<T>();
- readConfComplete?.Invoke();
- }
- else
- {
- if (TimerMgr.ForceInstance())
- {
- string fullPath = Path.Combine(path, typeof(T).Name + ".xml");
- TimerMgr.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List<T> scrollList) =>
- {
- this.DataList = scrollList;
- readConfComplete?.Invoke();
- }));
- }
- }
- }
- /// <summary>
- /// 添加一条配置
- /// </summary>
- /// <param name="conf"></param>
- 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<T>(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);
- }
- }
- /// <summary>
- /// 删除这条配置
- /// </summary>
- /// <param name="key"></param>
- public void DelConf(string key, bool isSave = true)
- {
- if (table != null)
- {
- table.DeleteData<T>(key);
- if (isSave)
- table.Save();
- }
- Open(key);
- }
- /// <summary>
- /// 根据key值查找数据
- /// 如果缓存列表里没有这条记录,则去循环列表中查找
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- 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<T>(data, priKey))
- {
- return true;
- }
- return false;
- });
- }
- else
- {
- ///暴力破解法,很少使用,请尽量避免使用此分支
- UnityLog.LogError("Use Wrong method!");
- return DataList.Find((data) =>
- {
- var dic = ClazzFactory.ClazzAnalyze<T>(data);
- if (dic != null)
- {
- foreach (var item in dic)
- {
- if (item.Value == name)
- {
- return true;
- }
- }
- }
- return false;
- });
- }
- }
- }
- return default;
- }
- }
- }
|