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. }
  159. private void CreateMeta()
  160. {
  161. float index = 0;
  162. var assetPaths = AssetDatabase.GetAllAssetPaths();
  163. int assetCount = assetPaths.Length;
  164. foreach (var assetPath in assetPaths)
  165. {
  166. var dataPath = Path.Join(cachePath, assetPath) + ".json";
  167. var metaPath = Path.Join(cachePath, assetPath) + ".meta" + ".json";
  168. var guid = AssetDatabase.AssetPathToGUID(assetPath);
  169. var progress = index / assetCount;
  170. index++;
  171. if (string.IsNullOrEmpty(guid))
  172. {
  173. Debug.LogWarning("Asset is invalid: " + assetPath);
  174. continue;
  175. }
  176. var extension = Path.GetExtension(assetPath);
  177. if (fileExtensionToIgnore.Contains(extension))
  178. {
  179. Debug.LogWarning("Asset file is not supported: " + assetPath);
  180. continue;
  181. }
  182. FileInfo info = new FileInfo(metaPath);
  183. if (info.Directory != null)
  184. {
  185. if (folderToIgnore.Contains(info.Directory.FullName))
  186. {
  187. Debug.Log("Ignoring directory: " + assetPath);
  188. continue;
  189. }
  190. info.Directory.Create();
  191. }
  192. AssetMetaData assetMetaData = new AssetMetaData
  193. {
  194. path = assetPath,
  195. guid = guid,
  196. dependencies = AssetDatabase.GetDependencies(assetPath)
  197. };
  198. if (EditorUtility.DisplayCancelableProgressBar("Updating", "Updating meta for file" + assetPath,
  199. progress))
  200. {
  201. return;
  202. }
  203. Thread.Sleep(1);
  204. var metaData = assetMetaData.ToJson();
  205. File.WriteAllText(metaPath, metaData);
  206. SerializeFile(assetPath, dataPath);
  207. }
  208. EditorUtility.ClearProgressBar();
  209. }
  210. private void SerializeFile(string path, string dataPath)
  211. {
  212. Type type = AssetDatabase.GetMainAssetTypeAtPath(path);
  213. // Looking at supporting types only
  214. if (type != typeof(GameObject) && type != typeof(Texture) && type != typeof(Material) &&
  215. type != typeof(Shader) && type != typeof(Font) && type != typeof(PlayerSettings) &&
  216. type != typeof(Animation) && type != typeof(Material) &&
  217. type != typeof(PrefabAssetType) && type != typeof(TextAsset) && type != typeof(AudioClip) &&
  218. type != typeof(SceneAsset) && type != typeof(MonoScript) &&
  219. type != typeof(ScriptableObject))
  220. {
  221. return;
  222. }
  223. AssetDatabase.LoadAssetAtPath(path, type);
  224. AssetData assetData = new AssetData();
  225. // TODO: Serialization for asset types
  226. var jsonifiedAsset = assetData.ToJson();
  227. File.WriteAllText(dataPath, jsonifiedAsset);
  228. }
  229. }
  230. }