SearchUtils.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using Leopotam.Ecs;
  6. using UnityEditor;
  7. using UnityEditor.Animations;
  8. using UnityEditor.SceneManagement;
  9. using UnityEngine;
  10. using UnityEngine.SceneManagement;
  11. using static Asset_Cleaner.AufCtx;
  12. using Object = UnityEngine.Object;
  13. namespace Asset_Cleaner {
  14. static class SearchUtils {
  15. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  16. public static bool IsAssignableFromInverse(this Type lhs, Type rhs) {
  17. if (lhs == null || rhs == null)
  18. return false;
  19. return rhs.IsAssignableFrom(lhs);
  20. }
  21. static Queue<SerializedProperty> _tmp = new Queue<SerializedProperty>();
  22. public static void Upd(SearchArg arg) {
  23. if (arg.Target is DefaultAsset folder) {
  24. var path = AssetDatabase.GetAssetPath(folder);
  25. var store = Globals<BacklinkStore>.Value;
  26. arg.UnusedAssetsFiltered = store.UnusedFiles.Where(p => p.Key.StartsWith(path)).Select(p => p.Key).ToList();
  27. arg.UnusedScenesFiltered = store.UnusedScenes.Where(p => p.Key.StartsWith(path)).Select(p => p.Key).ToList();
  28. }
  29. }
  30. public static void Init(SearchArg arg, Object target, Scene scene = default) {
  31. Asr.IsNotNull(target, "Asset you're trying to search is corrupted");
  32. arg.Target = target;
  33. arg.FilePath = AssetDatabase.GetAssetPath(arg.Target);
  34. if (!scene.IsValid()) {
  35. Upd(arg);
  36. arg.Main = AssetDatabase.LoadMainAssetAtPath(arg.FilePath);
  37. if (AssetDatabase.IsSubAsset(arg.Target)) { }
  38. else {
  39. switch (target) {
  40. case SceneAsset _:
  41. // todo support cross-scene references?
  42. // nested = all assets
  43. break;
  44. default:
  45. // AssetDatabase.IsMainAssetAtPathLoaded()
  46. var subAssets = AssetDatabase.LoadAllAssetsAtPath(arg.FilePath).Where(Predicate).ToArray();
  47. arg.SubAssets = subAssets.Length == 0 ? default(Option<Object[]>) : subAssets;
  48. bool Predicate(Object s) {
  49. if (!s)
  50. return false;
  51. return s.GetInstanceID() != arg.Target.GetInstanceID();
  52. }
  53. break;
  54. }
  55. }
  56. }
  57. else {
  58. switch (arg.Target) {
  59. case GameObject gg:
  60. arg.Main = gg;
  61. arg.Scene = scene;
  62. arg.SubAssets = gg.GetComponents<Component>().OfType<Object>().ToArray();
  63. break;
  64. case Component component: {
  65. // treat like subAsset
  66. arg.Main = component.gameObject;
  67. arg.Scene = scene;
  68. break;
  69. }
  70. default:
  71. // project asset such as Material
  72. arg.Main = arg.Target;
  73. arg.Scene = scene;
  74. break;
  75. }
  76. }
  77. }
  78. static bool SearchInChildProperties(SearchArg arg, Object suspect, bool scene, out EcsEntity entity) {
  79. if (IsTargetOrNested(arg, suspect)) {
  80. entity = default;
  81. return false;
  82. }
  83. if (!suspect) {
  84. entity = default;
  85. return false;
  86. }
  87. var so = new SerializedObject(suspect);
  88. _tmp.Clear();
  89. var queue = _tmp;
  90. var propIterator = so.GetIterator();
  91. var prefabInstance = false;
  92. if (scene && !string.IsNullOrEmpty(arg.FilePath) && PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(suspect) == arg.FilePath) {
  93. prefabInstance = true;
  94. while (propIterator.NextVisible(true)) {
  95. if (propIterator.propertyType != SerializedPropertyType.ObjectReference)
  96. continue;
  97. if (!IsTargetOrNested(arg, propIterator.objectReferenceValue))
  98. continue;
  99. queue.Enqueue(propIterator.Copy());
  100. }
  101. }
  102. else {
  103. while (propIterator.Next(true)) {
  104. if (propIterator.propertyType != SerializedPropertyType.ObjectReference)
  105. continue;
  106. if (!IsTargetOrNested(arg, propIterator.objectReferenceValue))
  107. continue;
  108. queue.Enqueue(propIterator.Copy());
  109. }
  110. }
  111. if (queue.Count == 0 && !prefabInstance) {
  112. entity = default;
  113. return false;
  114. }
  115. entity = World.NewEntityWith(out Result data);
  116. var gui = entity.Set<SearchResultGui>();
  117. gui.Properties = new List<SearchResultGui.PropertyData>();
  118. gui.SerializedObject = so;
  119. gui.Label = new GUIContent();
  120. // init header
  121. Texture2D miniTypeThumbnail = null;
  122. if (scene) {
  123. switch (suspect) {
  124. case Component component:
  125. data.RootGo = component.gameObject;
  126. gui.TransformPath = AnimationUtility.CalculateTransformPath(component.transform, null);
  127. gui.Label.image = AssetPreview.GetMiniThumbnail(data.RootGo);
  128. gui.Label.text = gui.TransformPath;
  129. break;
  130. case GameObject go:
  131. data.RootGo = go;
  132. gui.Label.image = AssetPreview.GetMiniThumbnail(data.RootGo);
  133. gui.TransformPath = AnimationUtility.CalculateTransformPath(go.transform, null);
  134. gui.Label.text = gui.TransformPath;
  135. break;
  136. default:
  137. throw new NotImplementedException();
  138. }
  139. miniTypeThumbnail = data.RootGo.GetInstanceID() == suspect.GetInstanceID()
  140. ? null
  141. : AssetPreview.GetMiniThumbnail(suspect);
  142. }
  143. else {
  144. data.File = suspect;
  145. data.FilePath = AssetDatabase.GetAssetPath(data.File);
  146. data.MainFile = AssetDatabase.LoadMainAssetAtPath(data.FilePath);
  147. // todo
  148. var prefabInstanceStatus = PrefabUtility.GetPrefabInstanceStatus(data.MainFile);
  149. switch (prefabInstanceStatus) {
  150. case PrefabInstanceStatus.Connected:
  151. case PrefabInstanceStatus.Disconnected:
  152. switch (data.File) {
  153. case Component comp:
  154. // transformPath = $"{AnimationUtility.CalculateTransformPath(comp.transform, null)}/".Replace("/", "/\n");
  155. gui.TransformPath =
  156. $"{AnimationUtility.CalculateTransformPath(comp.transform, null)}";
  157. break;
  158. case GameObject go:
  159. // transformPath = $"{AnimationUtility.CalculateTransformPath(go.transform, null)}/".Replace("/", "/\n");
  160. gui.TransformPath =
  161. $"{AnimationUtility.CalculateTransformPath(go.transform, null)}";
  162. break;
  163. default:
  164. // Assert.Fail("Not a component"); //todo
  165. break;
  166. }
  167. break;
  168. case PrefabInstanceStatus.NotAPrefab:
  169. case PrefabInstanceStatus.MissingAsset:
  170. if (!AssetDatabase.IsMainAsset(data.File)) {
  171. // {row.Main.name}
  172. gui.TransformPath = $"/{data.File.name}";
  173. }
  174. break;
  175. }
  176. gui.Label.text = data.FilePath.Replace(AssetsRootPath, string.Empty);
  177. gui.Label.image = AssetDatabase.GetCachedIcon(data.FilePath);
  178. }
  179. gui.Label.tooltip = gui.TransformPath;
  180. // init properties (footer)
  181. while (queue.Count > 0) {
  182. var prop = queue.Dequeue();
  183. var targetObject = prop.serializedObject.targetObject;
  184. var item = new SearchResultGui.PropertyData {
  185. Property = prop,
  186. Content = new GUIContent()
  187. };
  188. item.Content.image = miniTypeThumbnail;
  189. item.Content.text = Nicify(prop, targetObject, gui.TransformPath);
  190. item.Content.tooltip = gui.TransformPath;
  191. var typeName = targetObject.GetType().Name;
  192. if (StringComparer.Ordinal.Equals(typeName, targetObject.name))
  193. item.Content.tooltip = $"{gui.TransformPath}.{prop.propertyPath}";
  194. else
  195. item.Content.tooltip = $"{gui.TransformPath}({typeName}).{prop.propertyPath}";
  196. gui.Properties.Add(item: item);
  197. }
  198. return true;
  199. }
  200. public static bool IsFileIgrnoredBySettings(string path) {
  201. if (IgnoreTypes.Check(path, out _)) return true;
  202. if (IgnoredNonAssets(path)) return true;
  203. if (IgnoredPaths(path, out _)) return true;
  204. return false;
  205. }
  206. public static bool IgnoredPaths(string path, out string str) {
  207. var conf = Globals<Config>.Value;
  208. foreach (var substr in conf.IgnorePathContains) {
  209. Asr.IsNotNull(path);
  210. Asr.IsNotNull(substr);
  211. if (!path.Contains(substr)) continue;
  212. str = substr;
  213. return true;
  214. }
  215. str = default;
  216. return false;
  217. }
  218. public static bool IgnoredNonAssets(string path) {
  219. return !path.Contains("Assets/");
  220. }
  221. #region Project
  222. public static bool IsUnused(string path) {
  223. if (IsFileIgrnoredBySettings(path))
  224. return false;
  225. return !AnyDependencies(path);
  226. }
  227. static bool AnyDependencies(string path) {
  228. var store = Globals<BacklinkStore>.Value;
  229. if (store.UnusedFiles.Select(p => p.Key).Contains(path))
  230. return false;
  231. if (store.UnusedScenes.Select(p => p.Key).Contains(path))
  232. return false;
  233. return true;
  234. }
  235. public static void FilesThatReference(SearchArg arg) {
  236. var store = Globals<BacklinkStore>.Value;
  237. var path1 = AssetDatabase.GetAssetPath(arg.Target);
  238. if (!store.Backward.TryGetValue(path1, out var dep))
  239. return;
  240. foreach (var path in dep.Lookup) {
  241. var mainAsset = AssetDatabase.GetMainAssetTypeAtPath(path);
  242. if (mainAsset.IsAssignableFromInverse(typeof(SceneAsset)))
  243. continue;
  244. var any = false;
  245. if (mainAsset.IsAssignableFromInverse(typeof(GameObject))) { }
  246. else {
  247. var allAssetsAtPath = AssetDatabase.LoadAllAssetsAtPath(path);
  248. foreach (var suspect in allAssetsAtPath) {
  249. if (suspect is DefaultAsset || suspect is Transform || !suspect) continue;
  250. if (!SearchInChildProperties(arg, suspect, false, out var entity))
  251. continue;
  252. entity.Set<FileResultTag>();
  253. any = true;
  254. }
  255. }
  256. if (any) continue;
  257. // failed to find any property - just show main asset
  258. var e = World.NewEntity();
  259. var gui = e.Set<SearchResultGui>();
  260. gui.Properties = new List<SearchResultGui.PropertyData>();
  261. var main = AssetDatabase.LoadMainAssetAtPath(path);
  262. gui.Label = new GUIContent() {
  263. image = AssetPreview.GetMiniThumbnail(main),
  264. text = path.Replace(AssetsRootPath, string.Empty)
  265. };
  266. var res = e.Set<Result>();
  267. res.MainFile = main;
  268. e.Set<FileResultTag>();
  269. }
  270. }
  271. public static void ScenesThatContain(Object activeObject) {
  272. var store = Globals<BacklinkStore>.Value;
  273. var path1 = AssetDatabase.GetAssetPath(activeObject);
  274. if (!store.Backward.TryGetValue(path1, out var dep))
  275. return;
  276. foreach (var path in dep.Lookup) {
  277. if (!AssetDatabase.GetMainAssetTypeAtPath(path).IsAssignableFromInverse(typeof(SceneAsset)))
  278. continue;
  279. World.NewEntityWith(out SceneResult sp, out SceneDetails sceneDetails);
  280. sp.PathNicified = path.Replace("Assets/", string.Empty);
  281. // heavy
  282. sceneDetails.Path = path;
  283. var alreadyOpened = false;
  284. for (var i = 0; i < EditorSceneManager.loadedSceneCount; i++) {
  285. var cur = SceneManager.GetSceneAt(i);
  286. if (!cur.path.Eq(sceneDetails.Path)) continue;
  287. alreadyOpened = true;
  288. sceneDetails.Scene = cur;
  289. }
  290. sceneDetails.WasOpened = alreadyOpened;
  291. }
  292. }
  293. #endregion
  294. #region Scene
  295. static Pool<List<Component>> ComponentListPool = new Pool<List<Component>>(() => new List<Component>(), list => list.Clear());
  296. // todo provide explicit scene arg
  297. public static void InScene(SearchArg arg, Scene currentScene) {
  298. var rootGameObjects = currentScene.GetRootGameObjects();
  299. foreach (var suspect in Traverse(rootGameObjects)) {
  300. if (!SearchInChildProperties(arg, suspect, scene: true, out var entity))
  301. continue;
  302. var s = entity.Set<InSceneResult>();
  303. s.ScenePath = arg.Scene.path;
  304. }
  305. IEnumerable<Object> Traverse(GameObject[] roots) {
  306. foreach (var rootGo in roots)
  307. foreach (var comp in GoAndChildComps(rootGo)) {
  308. yield return comp;
  309. }
  310. }
  311. IEnumerable<Object> GoAndChildComps(GameObject rr) {
  312. using (ComponentListPool.GetScoped(out var pooled)) {
  313. rr.GetComponents(pooled);
  314. foreach (var component in pooled) {
  315. if (component is Transform)
  316. continue;
  317. yield return component;
  318. }
  319. }
  320. var transform = rr.transform;
  321. var childCount = transform.childCount;
  322. for (int i = 0; i < childCount; i++) {
  323. var g = transform.GetChild(i).gameObject;
  324. foreach (var res in GoAndChildComps(g))
  325. yield return res;
  326. }
  327. }
  328. }
  329. #if backup
  330. static void InScene(SearchArg arg, Scene currentScene) {
  331. var allObjects = currentScene
  332. .GetRootGameObjects()
  333. .SelectMany(g => g.GetComponentsInChildren<Component>(true)
  334. .Where(c => c && !(c is Transform))
  335. .Union(Enumerable.Repeat(g as Object, 1))
  336. )
  337. .ToArray();
  338. var total = allObjects.Length;
  339. for (var i = 0; i < total; i++) {
  340. var suspect = allObjects[i];
  341. if (SearchInChildProperties(arg, suspect, true, out var entity)) {
  342. var s = entity.Set<InSceneResult>();
  343. s.ScenePath = arg.Scene.path;
  344. }
  345. }
  346. }
  347. #endif
  348. static bool IsTargetOrNested(SearchArg target, Object suspect) {
  349. if (!suspect)
  350. return false;
  351. if (target.Target.GetInstanceID() == suspect.GetInstanceID() || target.Main.GetInstanceID() == (suspect).GetInstanceID())
  352. return true;
  353. if (target.SubAssets.TryGet(out var subassets))
  354. foreach (var asset in subassets) {
  355. if (asset.GetInstanceID() == (suspect).GetInstanceID())
  356. return true;
  357. }
  358. return false;
  359. }
  360. static string Nicify(SerializedProperty sp, Object o, string transformPath) {
  361. // return sp.propertyPath;
  362. string nice;
  363. switch (o) {
  364. case AnimatorState animatorState:
  365. return animatorState.name;
  366. case Material material:
  367. nice = material.name;
  368. break;
  369. default: {
  370. nice = sp.propertyPath.Replace(".Array.data", string.Empty);
  371. if (nice.IndexOf(".m_PersistentCalls.m_Calls", StringComparison.Ordinal) > 0) {
  372. nice = nice.Replace(".m_PersistentCalls.m_Calls", string.Empty)
  373. .Replace(".m_Target", string.Empty);
  374. }
  375. nice = nice.Split('.').Select(t => ObjectNames.NicifyVariableName(t).Replace(" ", string.Empty))
  376. .Aggregate((a, b) => a + "." + b);
  377. break;
  378. }
  379. }
  380. // nice = $"{transformPath}({o.GetType().Name}).{nice}";
  381. nice = $"({o.GetType().Name}).{nice}";
  382. return nice;
  383. }
  384. const string AssetsRootPath = "Assets/";
  385. #endregion
  386. }
  387. }