XSql.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using SC.XR.Unity;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Threading;
  8. using UnityEngine.Networking;
  9. using UnityEngine.XR;
  10. using XRTool.Util;
  11. namespace PublicTools.XMLDataBase
  12. {
  13. public enum DataType
  14. {
  15. /// <summary>
  16. /// xml的方式存储
  17. /// </summary>
  18. Xml = 0,
  19. /// <summary>
  20. /// json文本的方式存储
  21. /// </summary>
  22. Json = 1,
  23. /// <summary>
  24. /// sqlite
  25. /// </summary>
  26. SqlLite = 2,
  27. /// <summary>
  28. /// 文本的方式存储
  29. /// </summary>
  30. Txt = 3,
  31. /// <summary>
  32. /// mysql读写
  33. /// </summary>
  34. MySql = 4,
  35. /// <summary>
  36. /// 预留扩展
  37. /// </summary>
  38. Other = 5
  39. }
  40. /// <summary>
  41. /// 数据库的管理类
  42. /// 注意同时只能操作一个数据库文件,当传入新的路径时,老的文件连接都将被关闭
  43. /// </summary>
  44. public class XSql : Singleton<XSql>
  45. {
  46. private Dictionary<string, string> sqlList = new Dictionary<string, string>();
  47. /// <summary>
  48. /// 缓存表格处理对象
  49. /// </summary>
  50. private Dictionary<string, TableInterface> tableDic = new Dictionary<string, TableInterface>();
  51. private string sqlPath;
  52. /// <summary>
  53. /// 当前正在处理的数据库文件路径
  54. /// </summary>
  55. public string SqlPath { get => sqlPath; set => sqlPath = value; }
  56. /// <summary>
  57. /// 打开数据库
  58. /// 即创建文件夹,开启对应的文件的读取流
  59. /// 同时仅能操作一个文件夹
  60. /// </summary>
  61. /// <param name="sqlPath">绝对路径</param>
  62. public bool Open(string sqlPath, bool isforceCreate = false)
  63. {
  64. if (!string.IsNullOrEmpty(sqlPath) && SqlPath == sqlPath)
  65. {
  66. return true;
  67. }
  68. try
  69. {
  70. if (isforceCreate && !Directory.Exists(sqlPath))
  71. {
  72. Directory.CreateDirectory(sqlPath);
  73. }
  74. if (Directory.Exists(sqlPath))
  75. {
  76. this.SqlPath = sqlPath;
  77. return true;
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. UnityLog.LogException(ex);
  83. UnityLog.LogError(ex.ToString());
  84. }
  85. return false;
  86. }
  87. /// <summary>
  88. /// 关闭数据库
  89. /// 即关闭此文件夹的所有文件读取流
  90. /// </summary>
  91. /// <param name="sqlPath"></param>
  92. public void Close()
  93. {
  94. foreach (var item in tableDic)
  95. {
  96. item.Value.Close();
  97. }
  98. tableDic.Clear();
  99. }
  100. public TableInterface OpenTable(string tableName, DataType dataType, bool forceCreate = false)
  101. {
  102. string suffix = ".xml";
  103. if (dataType == DataType.Xml)
  104. {
  105. suffix = ".xml";
  106. }
  107. else if (dataType == DataType.Json)
  108. {
  109. suffix = ".json";
  110. }
  111. return OpenTable(tableName, suffix, forceCreate);
  112. }
  113. /// <summary>
  114. /// 打开本地表格
  115. /// 打开对应的文件流,文件流存在时可能为空
  116. /// 操作表格的情况:不存在文件,存在但是字节流为空,字节流不为空
  117. /// 字节流为空时如何处理?
  118. /// </summary>
  119. /// <param name="tableName"></param>
  120. /// <param name="forceCreate">强制创建,文件为空时创建我呢见</param>
  121. /// <param name="suffix">文件后缀名</param>
  122. public TableInterface OpenTable(string tableName, string suffix = ".xml", bool forceCreate = false)
  123. {
  124. return OpenTable(SqlPath, tableName, suffix, forceCreate);
  125. }
  126. /// <summary>
  127. /// 打开本地表格
  128. /// 打开对应的文件流,文件流存在时可能为空
  129. /// 操作表格的情况:不存在文件,存在但是字节流为空,字节流不为空
  130. /// 字节流为空时如何处理?
  131. /// 注意安卓下StreamPath下无法使用File类读取!会报告异常
  132. /// </summary>
  133. /// <param name="tableName"></param>
  134. /// <param name="forceCreate">强制创建,文件为空时创建我呢见</param>
  135. /// <param name="suffix">文件后缀名</param>
  136. public TableInterface OpenTable(string sqlPath, string tableName, string suffix = ".xml", bool forceCreate = false)
  137. {
  138. if (!Open(sqlPath, forceCreate))
  139. {
  140. return null;
  141. }
  142. string unExtenName = Path.GetFileNameWithoutExtension(tableName);
  143. if (tableDic.ContainsKey(unExtenName))
  144. {
  145. return tableDic[unExtenName];
  146. }
  147. ///文件后缀,如果table已有后缀不添加后缀,若无后缀名,则添加指定的后缀名
  148. if (string.IsNullOrEmpty(Path.GetExtension(tableName)))
  149. {
  150. tableName += suffix;
  151. }
  152. string path = Path.Combine(sqlPath, tableName);
  153. try
  154. {
  155. bool isCreateNewTable = false;
  156. ///目标文件不存在,强制创建
  157. if (forceCreate && !File.Exists(path))
  158. {
  159. File.Create(path).Dispose();
  160. isCreateNewTable = true;
  161. }
  162. ///文件存在,创建对应的对象
  163. if (File.Exists(path))
  164. {
  165. TableInterface fs = new XTable(path);
  166. tableDic.Add(unExtenName, fs);
  167. if (isCreateNewTable)
  168. {
  169. fs.Create(tableName);
  170. fs.Save();
  171. }
  172. return fs;
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. ///IO异常处理
  178. UnityEngine.Debug.LogException(ex);
  179. UnityLog.LogError(ex.ToString());
  180. }
  181. return null;
  182. }
  183. /// <summary>
  184. /// 关闭本地表操作
  185. /// </summary>
  186. /// <param name="tableName"></param>
  187. public void CloseTable(string tableName)
  188. {
  189. string unExtenName = Path.GetFileNameWithoutExtension(tableName);
  190. if (tableDic.ContainsKey(unExtenName))
  191. {
  192. tableDic[unExtenName].Close();
  193. tableDic.Remove(unExtenName);
  194. }
  195. }
  196. /// <summary>
  197. /// 新建一个表格
  198. /// </summary>
  199. /// <param name="tableName"></param>
  200. public bool CreateTable(string tableName, DataType dataType)
  201. {
  202. string suffix = ".xml";
  203. if (dataType == DataType.Xml)
  204. {
  205. suffix = ".xml";
  206. }
  207. else if (dataType == DataType.Json)
  208. {
  209. suffix = ".json";
  210. }
  211. return CreateTable(tableName, suffix);
  212. }
  213. /// <summary>
  214. /// 新建一个表格
  215. /// </summary>
  216. /// <param name="tableName"></param>
  217. public bool CreateTable(string tableName, string suffix = ".xml")
  218. {
  219. if (string.IsNullOrEmpty(Path.GetExtension(tableName)))
  220. {
  221. tableName += suffix;
  222. }
  223. TableInterface table = new XTable();
  224. return table.Create(tableName);
  225. }
  226. /// <summary>
  227. /// 读取远程的表格数据信息
  228. /// 或者在只读文件夹下读取数据
  229. /// </summary>
  230. /// <typeparam name="T"></typeparam>
  231. /// <param name="data">数据</param>
  232. /// <param name="type">数据类型,可能是.xml或者json文本0代表xml文本</param>
  233. /// <returns></returns>
  234. public List<T> ReadServerData<T>(byte[] data, int type = 0)
  235. {
  236. TableInterface table = new XTable(type);
  237. List<T> list = table.FindAllData<T>(data);
  238. table.Close();
  239. table = null;
  240. return list;
  241. }
  242. /// <summary>
  243. /// 读取远程的表格数据信息
  244. /// 或者在只读文件夹下读取数据
  245. /// </summary>
  246. /// <typeparam name="T"></typeparam>
  247. /// <param name="data">数据</param>
  248. /// <param name="type">数据类型,可能是.xml或者json文本0代表xml文本</param>
  249. /// <returns></returns>
  250. public List<T> ReadServerData<T>(string data, int type = 0)
  251. {
  252. TableInterface table = new XTable(type);
  253. List<T> list = table.FindAllData<T>(data);
  254. table.Close();
  255. table = null;
  256. return list;
  257. }
  258. public IEnumerator ReadServerData<T>(string url, Action<List<T>> action, int type = 0)
  259. {
  260. yield return DataFileUtil.RequestDownData(url, (string error, UnityWebRequest handler) =>
  261. {
  262. if (!string.IsNullOrEmpty(error))
  263. {
  264. UnityLog.LogError(url + "Read Data Error" + error);
  265. action?.Invoke(null);
  266. }
  267. else
  268. {
  269. action?.Invoke(ReadServerData<T>(handler.downloadHandler.data, type));
  270. }
  271. });
  272. }
  273. }
  274. }