FileListWindow.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.IO;
  6. using System.Linq;
  7. using System;
  8. using PublicTools.XMLDataBase;
  9. using XRTool.UI;
  10. namespace XRTool.Util
  11. {
  12. /// <summary>
  13. /// 文件窗口,自动生成文件清单
  14. /// </summary>
  15. public class FileListWindow : EditorWindow
  16. {
  17. private Rect rect;
  18. private string targetPath;
  19. private string fullPath;
  20. private string ignoreExtension = ".meta .cs";
  21. private string[] fileTypes = new string[] { ".xml", ".json" };
  22. private int selectIndex;
  23. [MenuItem("XRTool/Utilities/File Window", false, 0)]
  24. public static void OpenWindow()
  25. {
  26. // Dock it next to the Scene View.
  27. var window = GetWindow<FileListWindow>(typeof(SceneView));
  28. window.titleContent = new GUIContent("File Window");
  29. window.Show();
  30. }
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. private void OnGUI()
  35. {
  36. rect = EditorGUILayout.GetControlRect(GUILayout.Width(800));
  37. targetPath = EditorGUI.TextField(rect, "目标路径", targetPath);
  38. fullPath = EditorGUILayout.TextField("全路径", fullPath);
  39. ignoreExtension = EditorGUILayout.TextField("忽略文件类型", ignoreExtension, GUILayout.Width(300));
  40. selectIndex = EditorGUILayout.Popup(selectIndex, fileTypes);
  41. if (GUILayout.Button("测试读取1", GUILayout.Width(300)))
  42. {
  43. string sqlPath = Application.streamingAssetsPath;
  44. if (XSql.Instance.Open(sqlPath, true))
  45. {
  46. string tableName = typeof(DataInfo).Name + fileTypes[selectIndex];
  47. TableInterface table = XSql.Instance.OpenTable(tableName, fileTypes[selectIndex], true);
  48. if (table != null)
  49. {
  50. if (!table.Open())
  51. {
  52. return;
  53. }
  54. List<DataInfo> list = table.FindAllData<DataInfo>();
  55. if (list != null)
  56. {
  57. for (int i = 0; i < list.Count; i++)
  58. {
  59. UnityLog.Instance.Log(list[i].Path + list[i].Version);
  60. }
  61. }
  62. table.Close();
  63. }
  64. }
  65. XSql.Instance.Close();
  66. }
  67. if (GUILayout.Button("测试读取2", GUILayout.Width(300)))
  68. {
  69. string sqlPath = Application.streamingAssetsPath;
  70. string tableName = typeof(DataInfo).Name + fileTypes[selectIndex];
  71. string path = Path.Combine(sqlPath, tableName);
  72. string data = File.ReadAllText(path);
  73. UnityLog.Instance.Log(data);
  74. List<DataInfo> list = XSql.Instance.ReadServerData<DataInfo>(data);
  75. if (list != null)
  76. {
  77. for (int i = 0; i < list.Count; i++)
  78. {
  79. UnityLog.Instance.Log(list[i].Path + list[i].Version);
  80. }
  81. }
  82. }
  83. if (GUILayout.Button("清除", GUILayout.Width(300)))
  84. {
  85. string sqlPath = Application.streamingAssetsPath;
  86. if (XSql.Instance.Open(sqlPath, true))
  87. {
  88. string tableName = typeof(DataInfo).Name + fileTypes[selectIndex];
  89. TableInterface table = XSql.Instance.OpenTable(tableName, fileTypes[selectIndex], true);
  90. if (table != null)
  91. {
  92. if (!table.Open())
  93. {
  94. return;
  95. }
  96. table.DeleteAllData<DataInfo>();
  97. table.Close();
  98. }
  99. }
  100. XSql.Instance.Close();
  101. }
  102. if (GUILayout.Button("刷新路径", GUILayout.Width(300)))
  103. {
  104. if (Directory.Exists(fullPath))
  105. {
  106. string sqlPath = Application.streamingAssetsPath;
  107. if (XSql.Instance.Open(sqlPath, true))
  108. {
  109. string tableName = typeof(DataInfo).Name + fileTypes[selectIndex];
  110. TableInterface table = XSql.Instance.OpenTable(tableName, fileTypes[selectIndex], true);
  111. if (table != null)
  112. {
  113. if (!table.Open())
  114. {
  115. UnityLog.Instance.Log("表格打开失败,尝试创建", 2);
  116. if (!table.Create(typeof(DataInfo).Name))
  117. {
  118. Debug.LogError("创建表格失败");
  119. return;
  120. }
  121. }
  122. Uri tableUri = new Uri(Path.Combine(sqlPath, tableName));
  123. DataFileUtil.FindFileBreadth(fullPath, file =>
  124. {
  125. var uri = new Uri(file);
  126. if (uri != tableUri)
  127. {
  128. string path = file.Replace("\\", "/");
  129. if (!DataFileUtil.isIgnoreExtension(ignoreExtension, Path.GetExtension(path)))
  130. {
  131. if (path.Contains(Application.dataPath))
  132. {
  133. path = path.Substring(Application.dataPath.Length + 1);
  134. }
  135. string prikey = path.Replace("/", ".");
  136. DataInfo info = new DataInfo();
  137. info.PriKey = prikey;
  138. info.Path = path;
  139. info.Version = DataFileUtil.GetMD5HashFromFile(file);
  140. table.InsertData(info);
  141. }
  142. }
  143. });
  144. table.Close();
  145. }
  146. else
  147. {
  148. Debug.Log("Table is null");
  149. }
  150. XSql.Instance.Close();
  151. }
  152. }
  153. }
  154. if ((Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragExited)
  155. && rect.Contains(Event.current.mousePosition))
  156. {
  157. //改变鼠标的外表
  158. DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
  159. if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
  160. {
  161. targetPath = DragAndDrop.paths[0];
  162. string path = targetPath.Replace("Assets/", "");
  163. if (Directory.Exists(path))
  164. {
  165. fullPath = path;
  166. }
  167. else
  168. {
  169. fullPath = Path.Combine(Application.dataPath, path);
  170. }
  171. if (File.Exists(fullPath))
  172. {
  173. fullPath = Path.GetDirectoryName(fullPath);
  174. }
  175. if (!Directory.Exists(fullPath))
  176. {
  177. fullPath = "";
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }