AssetBankScanner.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using LLM.Editor.Analysis;
  8. using LLM.Editor.Helper;
  9. using UnityEditor;
  10. using UnityEditorInternal;
  11. using UnityEngine;
  12. namespace AssetBank.Editor
  13. {
  14. public class AssetBankScanner : EditorWindow
  15. {
  16. private List<string> folderToIgnore = new List<string>()
  17. {
  18. "AssetBank",
  19. "Library"
  20. };
  21. private List<string> fileExtensionToIgnore = new List<string>()
  22. {
  23. ".meta",
  24. ".dylib",
  25. ".bytes",
  26. ".dll"
  27. };
  28. private List<string> typesToIgnore = new List<string>()
  29. {
  30. typeof(DefaultAsset).ToString()
  31. };
  32. private int currentTab = 0;
  33. private string[] tabs = new[] { "General" , "Settings"};
  34. private readonly string folderstoignore = "AssetBankScanner_FolderToIgnore";
  35. private readonly string extensiontoignore = "AssetBankScanner_FileExtensionToIgnore";
  36. private readonly string typesToignore = "AssetBankScanner_TypesToIgnore";
  37. private readonly string CACHE_FOLDER = "AssetBank";
  38. private string cachePath;
  39. private List<ReorderableList> prefLists = new();
  40. private StringBuilder projectReport = new StringBuilder();
  41. [MenuItem("Window/Asset Database Scanner")]
  42. public static void ShowWindow()
  43. {
  44. var window = GetWindow<AssetBankScanner>();
  45. window.titleContent = new GUIContent("Asset Database Scanner");
  46. window.Show();
  47. }
  48. private void OnEnable()
  49. {
  50. cachePath = Path.Join("Library", CACHE_FOLDER);
  51. LoadSettings();
  52. SetupLists();
  53. }
  54. private void SetupLists()
  55. {
  56. prefLists.Add(CreateReorderableList("Folders to ignore", folderToIgnore));
  57. prefLists.Add(CreateReorderableList("Files to ignore", fileExtensionToIgnore));
  58. prefLists.Add(CreateReorderableList("Types to ignore", typesToIgnore));
  59. }
  60. private void LoadSettings()
  61. {
  62. if (!EditorPrefs.HasKey(folderstoignore))
  63. {
  64. EditorPrefs.SetString(folderstoignore, String.Join(";",folderToIgnore));
  65. }
  66. if (!EditorPrefs.HasKey(extensiontoignore))
  67. {
  68. EditorPrefs.SetString(extensiontoignore, String.Join(";", fileExtensionToIgnore));
  69. }
  70. if (!EditorPrefs.HasKey(typesToignore))
  71. {
  72. EditorPrefs.SetString(typesToignore, String.Join(";",typesToIgnore));
  73. }
  74. folderToIgnore = EditorPrefs.GetString(folderstoignore, "").Split(';')
  75. .Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
  76. fileExtensionToIgnore = EditorPrefs.GetString(extensiontoignore, "").Split(';')
  77. .Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
  78. typesToIgnore = EditorPrefs.GetString(typesToignore, "").Split(';').Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
  79. }
  80. private void OnGUI()
  81. {
  82. EditorGUILayout.LabelField("Asset Bank Scanner", EditorStyles.boldLabel);
  83. GUILayout.Space(10);
  84. currentTab = GUILayout.Toolbar(currentTab, tabs);
  85. GUILayout.Space(10);
  86. switch (currentTab)
  87. {
  88. case 0: DrawScanner(); break;
  89. case 1: DrawSettings(); break;
  90. }
  91. }
  92. private void DrawSettings()
  93. {
  94. GUILayout.Label("Settings", EditorStyles.boldLabel);
  95. foreach (var prefList in prefLists)
  96. {
  97. prefList.DoLayoutList();
  98. EditorGUILayout.Space();
  99. }
  100. if (GUILayout.Button("Clear cache"))
  101. {
  102. Directory.Delete(cachePath, true);
  103. }
  104. return;
  105. # region "Clear options"
  106. if (GUILayout.Button("Clear preferences", EditorStyles.miniButton))
  107. {
  108. prefLists.Clear();
  109. EditorPrefs.DeleteKey(folderstoignore);
  110. EditorPrefs.DeleteKey(extensiontoignore);
  111. EditorPrefs.DeleteKey(typesToignore);
  112. }
  113. #endregion
  114. }
  115. private ReorderableList CreateReorderableList(string title, List<string> targetList)
  116. {
  117. var list = new ReorderableList(targetList, typeof(string), true, true, true, true)
  118. {
  119. drawHeaderCallback = rect =>
  120. {
  121. EditorGUI.LabelField(rect, title);
  122. },
  123. drawElementCallback = (rect, index, isActive, isFocused) =>
  124. {
  125. targetList[index] = EditorGUI.TextField(
  126. new Rect(rect.x, rect.y + 2, rect.width, EditorGUIUtility.singleLineHeight),
  127. targetList[index]
  128. );
  129. },
  130. onAddCallback = l =>
  131. {
  132. targetList.Add("");
  133. },
  134. onRemoveCallback = l =>
  135. {
  136. targetList.RemoveAt(l.index);
  137. }
  138. };
  139. return list;
  140. }
  141. private void DrawScanner()
  142. {
  143. if (GUILayout.Button("Scan project", GUILayout.Height(25)))
  144. {
  145. ScanProject();
  146. }
  147. }
  148. private void ScanProject()
  149. {
  150. CreateMeta();
  151. SerializeProjectSettings();
  152. }
  153. private void SerializeProjectSettings()
  154. {
  155. ProjectSettingsProvider _provider = new ProjectSettingsProvider();
  156. var q = _provider.GetContext(null, "QualitySettings");
  157. }
  158. private void CreateMeta()
  159. {
  160. float index = 0;
  161. var assetPaths = AssetDatabase.GetAllAssetPaths();
  162. int assetCount = assetPaths.Length;
  163. foreach (var assetPath in assetPaths)
  164. {
  165. var dataPath = Path.Join(cachePath, assetPath) + ".json";
  166. var metaPath = Path.Join(cachePath, assetPath) + ".meta" + ".json";
  167. var guid = AssetDatabase.AssetPathToGUID(assetPath);
  168. var progress = index / assetCount;
  169. index++;
  170. if (string.IsNullOrEmpty(guid))
  171. {
  172. Debug.LogWarning("Asset is invalid: " + assetPath);
  173. continue;
  174. }
  175. var extension = Path.GetExtension(assetPath);
  176. if (fileExtensionToIgnore.Contains(extension))
  177. {
  178. Debug.LogWarning("Asset file is not supported: " + assetPath);
  179. continue;
  180. }
  181. FileInfo info = new FileInfo(metaPath);
  182. if (info.Directory != null)
  183. {
  184. if (folderToIgnore.Contains(info.Directory.FullName))
  185. {
  186. Debug.Log("Ignoring directory: " + assetPath);
  187. continue;
  188. }
  189. info.Directory.Create();
  190. }
  191. AssetMetaData assetMetaData = new AssetMetaData
  192. {
  193. path = assetPath,
  194. guid = guid,
  195. dependencies = AssetDatabase.GetDependencies(assetPath)
  196. };
  197. if (EditorUtility.DisplayCancelableProgressBar("Updating", "Updating meta for file" + assetPath,
  198. progress))
  199. {
  200. return;
  201. }
  202. Thread.Sleep(1);
  203. var metaData = assetMetaData.ToJson();
  204. File.WriteAllText(metaPath, metaData);
  205. SerializeFile(assetPath, dataPath);
  206. }
  207. EditorUtility.ClearProgressBar();
  208. }
  209. private void SerializeFile(string path, string dataPath)
  210. {
  211. Type type = AssetDatabase.GetMainAssetTypeAtPath(path);
  212. // Looking at supporting types only
  213. if (type != typeof(GameObject) && type != typeof(Texture) && type != typeof(Material) &&
  214. type != typeof(Shader) && type != typeof(Font) && type != typeof(PlayerSettings) &&
  215. type != typeof(Animation) && type != typeof(Material) &&
  216. type != typeof(PrefabAssetType) && type != typeof(TextAsset) && type != typeof(AudioClip) &&
  217. type != typeof(SceneAsset) && type != typeof(MonoScript) &&
  218. type != typeof(ScriptableObject))
  219. {
  220. return;
  221. }
  222. AssetDatabase.LoadAssetAtPath(path, type);
  223. AssetData assetData = new AssetData();
  224. // TODO: Serialization for asset types
  225. var jsonifiedAsset = assetData.ToJson();
  226. File.WriteAllText(dataPath, jsonifiedAsset);
  227. }
  228. }
  229. }