FR2_Cache.cs 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. //#define FR2_DEBUG
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace vietlabs.fr2
  9. {
  10. [InitializeOnLoad]
  11. public class FR2_CacheHelper : AssetPostprocessor
  12. {
  13. private static HashSet<string> scenes;
  14. private static HashSet<string> guidsIgnore;
  15. static FR2_CacheHelper()
  16. {
  17. EditorApplication.update -= InitHelper;
  18. EditorApplication.update += InitHelper;
  19. }
  20. private static void InitHelper()
  21. {
  22. if (EditorApplication.isCompiling)
  23. {
  24. return;
  25. }
  26. // if (EditorApplication.isPlayingOrWillChangePlaymode) return;
  27. if (!FR2_Cache.isReady)
  28. {
  29. return;
  30. }
  31. if (!FR2_Cache.Api.disabled)
  32. {
  33. InitListScene();
  34. InitIgnore();
  35. #if UNITY_2018_1_OR_NEWER
  36. EditorBuildSettings.sceneListChanged -= InitListScene;
  37. EditorBuildSettings.sceneListChanged += InitListScene;
  38. #endif
  39. EditorApplication.projectWindowItemOnGUI -= OnGUIProjectItem;
  40. EditorApplication.projectWindowItemOnGUI += OnGUIProjectItem;
  41. FR2_Cache.onReady -= OnCacheReady;
  42. FR2_Cache.onReady += OnCacheReady;
  43. }
  44. EditorApplication.update -= InitHelper;
  45. }
  46. // private class AssetModificationHelper: UnityEditor.AssetModificationProcessor
  47. // {
  48. // static void OnWillCreateAsset(string assetName)
  49. // {
  50. // FR2_Cache.Api.makeDirty();
  51. // }
  52. // static AssetDeleteResult OnWillDeleteAsset(string name,RemoveAssetOptions options)
  53. // {
  54. // FR2_Cache.Api.makeDirty();
  55. // return AssetDeleteResult.DidDelete;
  56. // }
  57. // private static AssetMoveResult OnWillMoveAsset(string sourcePath, string destinationPath)
  58. // {
  59. // FR2_Cache.Api.makeDirty();
  60. // AssetMoveResult assetMoveResult = AssetMoveResult.DidMove;
  61. // // Perform operations on the asset and set the value of 'assetMoveResult' accordingly.
  62. // return assetMoveResult;
  63. // }
  64. // static string[] OnWillSaveAssets(string[] paths)
  65. // {
  66. // FR2_Cache.Api.makeDirty();
  67. // return paths;
  68. // }
  69. // }
  70. private static void OnCacheReady()
  71. {
  72. InitIgnore();
  73. // force repaint all project panels
  74. EditorApplication.RepaintProjectWindow();
  75. }
  76. public static void InitIgnore()
  77. {
  78. guidsIgnore = new HashSet<string>();
  79. foreach (string item in FR2_Setting.IgnoreAsset)
  80. {
  81. string guid = AssetDatabase.AssetPathToGUID(item);
  82. if (guidsIgnore.Contains(guid))
  83. {
  84. continue;
  85. }
  86. guidsIgnore.Add(guid);
  87. }
  88. }
  89. private static void InitListScene()
  90. {
  91. scenes = new HashSet<string>();
  92. // string[] scenes = new string[sceneCount];
  93. foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
  94. {
  95. string sce = AssetDatabase.AssetPathToGUID(scene.path);
  96. // Debug.Log(scene.path + " " + sce);
  97. if (scenes.Contains(sce))
  98. {
  99. continue;
  100. }
  101. scenes.Add(sce);
  102. }
  103. }
  104. private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets,
  105. string[] movedAssets,
  106. string[] movedFromAssetPaths)
  107. {
  108. FR2_Cache.DelayCheck4Changes();
  109. //Debug.Log("OnPostProcessAllAssets : " + ":" + importedAssets.Length + ":" + deletedAssets.Length + ":" + movedAssets.Length + ":" + movedFromAssetPaths.Length);
  110. if (!FR2_Cache.isReady)
  111. {
  112. #if FR2_DEBUG
  113. Debug.Log("Not ready, will refresh anyway !");
  114. #endif
  115. return;
  116. }
  117. // FR2 not yet ready
  118. if (FR2_Cache.Api.AssetMap == null) return;
  119. for (var i = 0; i < importedAssets.Length; i++)
  120. {
  121. if (importedAssets[i] == FR2_Cache.CachePath)
  122. {
  123. continue;
  124. }
  125. string guid = AssetDatabase.AssetPathToGUID(importedAssets[i]);
  126. if (!FR2_Asset.IsValidGUID(guid))
  127. {
  128. continue;
  129. }
  130. if (FR2_Cache.Api.AssetMap.ContainsKey(guid))
  131. {
  132. FR2_Cache.Api.RefreshAsset(guid, true);
  133. #if FR2_DEBUG
  134. Debug.Log("Changed : " + importedAssets[i]);
  135. #endif
  136. continue;
  137. }
  138. FR2_Cache.Api.AddAsset(guid);
  139. #if FR2_DEBUG
  140. Debug.Log("New : " + importedAssets[i]);
  141. #endif
  142. }
  143. for (var i = 0; i < deletedAssets.Length; i++)
  144. {
  145. string guid = AssetDatabase.AssetPathToGUID(deletedAssets[i]);
  146. FR2_Cache.Api.RemoveAsset(guid);
  147. #if FR2_DEBUG
  148. Debug.Log("Deleted : " + deletedAssets[i]);
  149. #endif
  150. }
  151. for (var i = 0; i < movedAssets.Length; i++)
  152. {
  153. string guid = AssetDatabase.AssetPathToGUID(movedAssets[i]);
  154. FR2_Asset asset = FR2_Cache.Api.Get(guid);
  155. if (asset != null)
  156. {
  157. asset.MarkAsDirty(true, false);
  158. }
  159. }
  160. #if FR2_DEBUG
  161. Debug.Log("Changes :: " + importedAssets.Length + ":" + FR2_Cache.Api.workCount);
  162. #endif
  163. FR2_Cache.Api.Check4Work();
  164. }
  165. private static void OnGUIProjectItem(string guid, Rect rect)
  166. {
  167. var r = new Rect(rect.x, rect.y, 1f, 16f);
  168. if (scenes.Contains(guid))
  169. {
  170. EditorGUI.DrawRect(r, GUI2.Theme(new Color32(72, 150, 191, 255), Color.blue));
  171. }
  172. else if (guidsIgnore.Contains(guid))
  173. {
  174. var ignoreRect = new Rect(rect.x + 3f, rect.y + 6f, 2f, 2f);
  175. EditorGUI.DrawRect(ignoreRect, GUI2.darkRed);
  176. }
  177. if (!FR2_Cache.isReady)
  178. {
  179. return; // not ready
  180. }
  181. if (!FR2_Setting.ShowReferenceCount)
  182. {
  183. return;
  184. }
  185. FR2_Cache api = FR2_Cache.Api;
  186. if (FR2_Cache.Api.AssetMap == null)
  187. {
  188. FR2_Cache.Api.Check4Changes(false);
  189. }
  190. FR2_Asset item;
  191. if (!api.AssetMap.TryGetValue(guid, out item))
  192. {
  193. return;
  194. }
  195. if (item == null || item.UsedByMap == null)
  196. {
  197. return;
  198. }
  199. if (item.UsedByMap.Count > 0)
  200. {
  201. var content = new GUIContent(item.UsedByMap.Count.ToString());
  202. r.width = 0f;
  203. r.xMin -= 100f;
  204. GUI.Label(r, content, GUI2.miniLabelAlignRight);
  205. }
  206. }
  207. }
  208. [Serializable]
  209. public class FR2_Setting
  210. {
  211. private static FR2_Setting d;
  212. public bool alternateColor = true;
  213. public int excludeTypes; //32-bit type Mask
  214. public FR2_RefDrawer.Mode groupMode;
  215. public List<string> listIgnore = new List<string>();
  216. public bool pingRow = true;
  217. public bool referenceCount = true;
  218. public bool showFileSize;
  219. public bool displayFileSize = true;
  220. public bool displayAtlasName = false;
  221. public bool displayAssetBundleName = false;
  222. public bool showUsedByClassed = true;
  223. public FR2_RefDrawer.Sort sortMode;
  224. public int treeIndent = 10;
  225. public Color32 rowColor = new Color32(0, 0, 0, 12);
  226. public Color32 ScanColor = new Color32(0, 204, 102, 255);
  227. public Color SelectedColor = new Color(0, 0f, 1f, 0.25f);
  228. [NonSerialized] private static HashSet<string> _hashIgnore;
  229. // private static Dictionary<string, List<string>> _IgnoreFiltered;
  230. public static Action OnIgnoreChange;
  231. //public bool scanScripts = false;
  232. /*
  233. Doesn't have a settings option - I will include one in next update
  234. 2. Hide the reference number - Should be in the setting above so will be coming next
  235. 3. Cache file path should be configurable - coming next in the setting
  236. 4. Disable / Selectable color in alternative rows - coming next in the setting panel
  237. 5. Applied filters aren't saved - Should be fixed in next update too
  238. 6. Hide Selection part - should be com as an option so you can quickly toggle it on or off
  239. 7. Click whole line to ping - coming next by default and can adjustable in the setting panel
  240. */
  241. internal static FR2_Setting s
  242. {
  243. get { return FR2_Cache.Api ? FR2_Cache.Api.setting : d ?? (d = new FR2_Setting()); }
  244. }
  245. public static bool ShowUsedByClassed
  246. {
  247. get { return s.showUsedByClassed; }
  248. }
  249. public static bool ShowFileSize
  250. {
  251. get { return s.showFileSize; }
  252. }
  253. public static int TreeIndent
  254. {
  255. get { return s.treeIndent; }
  256. set
  257. {
  258. if (s.treeIndent == value)
  259. {
  260. return;
  261. }
  262. s.treeIndent = value;
  263. setDirty();
  264. }
  265. }
  266. public static bool ShowReferenceCount
  267. {
  268. get { return s.referenceCount; }
  269. set
  270. {
  271. if (s.referenceCount == value)
  272. {
  273. return;
  274. }
  275. s.referenceCount = value;
  276. setDirty();
  277. }
  278. }
  279. public static bool AlternateRowColor
  280. {
  281. get { return s.alternateColor; }
  282. set
  283. {
  284. if (s.alternateColor == value)
  285. {
  286. return;
  287. }
  288. s.alternateColor = value;
  289. setDirty();
  290. }
  291. }
  292. public static Color32 RowColor
  293. {
  294. get { return s.rowColor; }
  295. set
  296. {
  297. if (s.rowColor.Equals(value))
  298. {
  299. return;
  300. }
  301. s.rowColor = value;
  302. setDirty();
  303. }
  304. }
  305. public static bool PingRow
  306. {
  307. get { return s.pingRow; }
  308. set
  309. {
  310. if (s.pingRow == value)
  311. {
  312. return;
  313. }
  314. s.pingRow = value;
  315. setDirty();
  316. }
  317. }
  318. public static HashSet<string> IgnoreAsset
  319. {
  320. get
  321. {
  322. if (_hashIgnore == null)
  323. {
  324. _hashIgnore = new HashSet<string>();
  325. if (s == null || s.listIgnore == null)
  326. {
  327. return _hashIgnore;
  328. }
  329. for (var i = 0; i < s.listIgnore.Count; i++)
  330. {
  331. if (_hashIgnore.Contains(s.listIgnore[i]))
  332. {
  333. continue;
  334. }
  335. _hashIgnore.Add(s.listIgnore[i]);
  336. }
  337. }
  338. return _hashIgnore;
  339. }
  340. }
  341. // public static Dictionary<string, List<string>> IgnoreFiltered
  342. // {
  343. // get
  344. // {
  345. // if (_IgnoreFiltered == null)
  346. // {
  347. // initIgnoreFiltered();
  348. // }
  349. //
  350. // return _IgnoreFiltered;
  351. // }
  352. // }
  353. //static public bool ScanScripts
  354. //{
  355. // get { return s.scanScripts; }
  356. // set {
  357. // if (s.scanScripts == value) return;
  358. // s.scanScripts = value; setDirty();
  359. // }
  360. //}
  361. public static FR2_RefDrawer.Mode GroupMode
  362. {
  363. get { return s.groupMode; }
  364. set
  365. {
  366. if (s.groupMode.Equals(value))
  367. {
  368. return;
  369. }
  370. s.groupMode = value;
  371. setDirty();
  372. }
  373. }
  374. public static FR2_RefDrawer.Sort SortMode
  375. {
  376. get { return s.sortMode; }
  377. set
  378. {
  379. if (s.sortMode.Equals(value))
  380. {
  381. return;
  382. }
  383. s.sortMode = value;
  384. setDirty();
  385. }
  386. }
  387. public static bool HasTypeExcluded
  388. {
  389. get { return s.excludeTypes != 0; }
  390. }
  391. private static void setDirty()
  392. {
  393. if (FR2_Cache.Api != null)
  394. {
  395. EditorUtility.SetDirty(FR2_Cache.Api);
  396. }
  397. }
  398. // private static void initIgnoreFiltered()
  399. // {
  400. // FR2_Asset.ignoreTS = Time.realtimeSinceStartup;
  401. //
  402. // _IgnoreFiltered = new Dictionary<string, List<string>>();
  403. // var lst = new List<string>(s.listIgnore);
  404. // lst = lst.OrderBy(x => x.Length).ToList();
  405. // int count = lst.Count;
  406. // for (var i = 0; i < count; i++)
  407. // {
  408. // string str = lst[i];
  409. // _IgnoreFiltered.Add(str, new List<string> {str});
  410. // for (int j = count - 1; j > i; j--)
  411. // {
  412. // if (lst[j].StartsWith(str))
  413. // {
  414. // _IgnoreFiltered[str].Add(lst[j]);
  415. // lst.RemoveAt(j);
  416. // count--;
  417. // }
  418. // }
  419. // }
  420. // }
  421. public static void AddIgnore(string path)
  422. {
  423. if (string.IsNullOrEmpty(path) || IgnoreAsset.Contains(path) || path == "Assets")
  424. {
  425. return;
  426. }
  427. s.listIgnore.Add(path);
  428. _hashIgnore.Add(path);
  429. AssetType.SetDirtyIgnore();
  430. FR2_CacheHelper.InitIgnore();
  431. //initIgnoreFiltered();
  432. FR2_Asset.ignoreTS = Time.realtimeSinceStartup;
  433. if (OnIgnoreChange != null)
  434. {
  435. OnIgnoreChange();
  436. }
  437. }
  438. public static void RemoveIgnore(string path)
  439. {
  440. if (!IgnoreAsset.Contains(path))
  441. {
  442. return;
  443. }
  444. _hashIgnore.Remove(path);
  445. s.listIgnore.Remove(path);
  446. AssetType.SetDirtyIgnore();
  447. FR2_CacheHelper.InitIgnore();
  448. //initIgnoreFiltered();
  449. FR2_Asset.ignoreTS = Time.realtimeSinceStartup;
  450. if (OnIgnoreChange != null)
  451. {
  452. OnIgnoreChange();
  453. }
  454. }
  455. public static bool IsTypeExcluded(int type)
  456. {
  457. return (s.excludeTypes >> type & 1) != 0;
  458. }
  459. public static void ToggleTypeExclude(int type)
  460. {
  461. bool v = (s.excludeTypes >> type & 1) != 0;
  462. if (v)
  463. {
  464. s.excludeTypes &= ~(1 << type);
  465. }
  466. else
  467. {
  468. s.excludeTypes |= 1 << type;
  469. }
  470. setDirty();
  471. }
  472. public static int GetExcludeType()
  473. {
  474. return s.excludeTypes;
  475. }
  476. public static bool IsIncludeAllType()
  477. {
  478. // Debug.Log ((AssetType.FILTERS.Length & s.excludeTypes) + " " + Mathf.Pow(2, AssetType.FILTERS.Length) );
  479. return s.excludeTypes == 0 || Mathf.Abs(s.excludeTypes) == Mathf.Pow(2, AssetType.FILTERS.Length);
  480. }
  481. public static void ExcludeAllType()
  482. {
  483. s.excludeTypes = -1;
  484. }
  485. public static void IncludeAllType()
  486. {
  487. s.excludeTypes = 0;
  488. }
  489. public void DrawSettings()
  490. {
  491. if (FR2_Unity.DrawToggle(ref pingRow, "Full Row click to Ping"))
  492. {
  493. setDirty();
  494. }
  495. GUILayout.BeginHorizontal();
  496. {
  497. if (FR2_Unity.DrawToggle(ref alternateColor, "Alternate Odd & Even Row Color"))
  498. {
  499. setDirty();
  500. FR2_Unity.RepaintFR2Windows();
  501. }
  502. EditorGUI.BeginDisabledGroup(!alternateColor);
  503. {
  504. Color c = EditorGUILayout.ColorField(rowColor);
  505. if (!c.Equals(rowColor))
  506. {
  507. rowColor = c;
  508. setDirty();
  509. FR2_Unity.RepaintFR2Windows();
  510. }
  511. }
  512. EditorGUI.EndDisabledGroup();
  513. }
  514. GUILayout.EndHorizontal();
  515. if (FR2_Unity.DrawToggle(ref referenceCount, "Show Usage Count in Project panel"))
  516. {
  517. setDirty();
  518. FR2_Unity.RepaintProjectWindows();
  519. }
  520. if (FR2_Unity.DrawToggle(ref showUsedByClassed, "Show Asset Type in use"))
  521. {
  522. setDirty();
  523. FR2_Unity.RepaintFR2Windows();
  524. }
  525. GUILayout.BeginHorizontal();
  526. {
  527. Color c = EditorGUILayout.ColorField("Duplicate Scan Color", ScanColor);
  528. if (!c.Equals(ScanColor))
  529. {
  530. ScanColor = c;
  531. setDirty();
  532. FR2_Unity.RepaintFR2Windows();
  533. }
  534. }
  535. GUILayout.EndHorizontal();
  536. }
  537. }
  538. public class FR2_Cache : ScriptableObject
  539. {
  540. internal const string CACHE_VERSION = "2.1";
  541. internal const string DEFAULT_CACHE_PATH = "Assets/FR2_Cache.asset";
  542. internal static int cacheStamp;
  543. internal static Action onReady;
  544. internal static bool _triedToLoadCache;
  545. internal static FR2_Cache _cache;
  546. internal static string _cacheGUID;
  547. internal static string _cachePath;
  548. public static int priority = 5;
  549. [SerializeField] private bool _autoRefresh;
  550. [SerializeField] private string _curCacheVersion;
  551. [SerializeField] private bool _disabled;
  552. [SerializeField] public List<FR2_Asset> AssetList;
  553. private int frameSkipped;
  554. [NonSerialized] internal Dictionary<string, FR2_Asset> AssetMap;
  555. [NonSerialized] internal List<FR2_Asset> queueLoadContent;
  556. internal bool ready;
  557. [SerializeField] internal FR2_Setting setting = new FR2_Setting();
  558. // ----------------------------------- INSTANCE -------------------------------------
  559. [SerializeField] public int timeStamp;
  560. [NonSerialized] internal int workCount;
  561. public static void DrawPriorityGUI()
  562. {
  563. float w = EditorGUIUtility.labelWidth;
  564. EditorGUIUtility.labelWidth = 120f;
  565. FR2_Cache.priority = EditorGUILayout.IntSlider(" Scan Priority", FR2_Cache.priority, 0, 5);
  566. EditorGUIUtility.labelWidth = w;
  567. }
  568. internal static string CacheGUID
  569. {
  570. get
  571. {
  572. if (!string.IsNullOrEmpty(_cacheGUID))
  573. {
  574. return _cacheGUID;
  575. }
  576. if (_cache != null)
  577. {
  578. _cachePath = AssetDatabase.GetAssetPath(_cache);
  579. _cacheGUID = AssetDatabase.AssetPathToGUID(_cachePath);
  580. return _cacheGUID;
  581. }
  582. return null;
  583. }
  584. }
  585. internal static string CachePath
  586. {
  587. get
  588. {
  589. if (!string.IsNullOrEmpty(_cachePath))
  590. {
  591. return _cachePath;
  592. }
  593. if (_cache != null)
  594. {
  595. _cachePath = AssetDatabase.GetAssetPath(_cache);
  596. return _cachePath;
  597. }
  598. return null;
  599. }
  600. }
  601. public bool Dirty { get; private set; }
  602. internal static FR2_Cache Api
  603. {
  604. get
  605. {
  606. if (_cache != null)
  607. {
  608. return _cache;
  609. }
  610. if (!_triedToLoadCache)
  611. {
  612. TryLoadCache();
  613. }
  614. return _cache;
  615. }
  616. }
  617. internal bool disabled
  618. {
  619. get { return _disabled; }
  620. set
  621. {
  622. if (_disabled == value)
  623. {
  624. return;
  625. }
  626. _disabled = value;
  627. if (_disabled)
  628. {
  629. //Debug.LogWarning("FR2 is disabled - Stopping all works !");
  630. ready = false;
  631. EditorApplication.update -= AsyncProcess;
  632. }
  633. else
  634. {
  635. FR2_Cache.Api.Check4Changes(false);
  636. }
  637. }
  638. }
  639. internal static bool isReady
  640. {
  641. get
  642. {
  643. if (!_triedToLoadCache)
  644. {
  645. TryLoadCache();
  646. }
  647. return _cache != null && _cache.ready;
  648. }
  649. }
  650. internal static bool hasCache
  651. {
  652. get
  653. {
  654. if (!_triedToLoadCache)
  655. {
  656. TryLoadCache();
  657. }
  658. return _cache != null;
  659. }
  660. }
  661. internal float progress
  662. {
  663. get
  664. {
  665. int n = workCount - queueLoadContent.Count;
  666. return workCount == 0 ? 1 : n / (float) workCount;
  667. }
  668. }
  669. public static bool CheckSameVersion()
  670. {
  671. // Debug.Log((_cache == null) + " " + _cache._curCacheVersion );
  672. if (_cache == null)
  673. {
  674. return false;
  675. }
  676. return _cache._curCacheVersion == CACHE_VERSION;
  677. }
  678. public void makeDirty()
  679. {
  680. Dirty = true;
  681. }
  682. private static void FoundCache(bool savePrefs, bool writeFile)
  683. {
  684. //Debug.LogWarning("Found Cache!");
  685. _cachePath = AssetDatabase.GetAssetPath(_cache);
  686. _cache.ReadFromCache();
  687. _cache.Check4Changes(false);
  688. _cacheGUID = AssetDatabase.AssetPathToGUID(_cachePath);
  689. if (savePrefs)
  690. {
  691. EditorPrefs.SetString("fr2_cache.guid", _cacheGUID);
  692. }
  693. if (writeFile)
  694. {
  695. File.WriteAllText("Library/fr2_cache.guid", _cacheGUID);
  696. }
  697. }
  698. private static bool RestoreCacheFromGUID(string guid, bool savePrefs, bool writeFile)
  699. {
  700. if (string.IsNullOrEmpty(guid))
  701. {
  702. return false;
  703. }
  704. string path = AssetDatabase.GUIDToAssetPath(guid);
  705. if (string.IsNullOrEmpty(path))
  706. {
  707. return false;
  708. }
  709. return RestoreCacheFromPath(path, savePrefs, writeFile);
  710. }
  711. private static bool RestoreCacheFromPath(string path, bool savePrefs, bool writeFile)
  712. {
  713. if (string.IsNullOrEmpty(path))
  714. {
  715. return false;
  716. }
  717. _cache = FR2_Unity.LoadAssetAtPath<FR2_Cache>(path);
  718. if (_cache != null)
  719. {
  720. FoundCache(savePrefs, writeFile);
  721. }
  722. return _cache != null;
  723. }
  724. private static void TryLoadCache()
  725. {
  726. _triedToLoadCache = true;
  727. if (RestoreCacheFromPath(DEFAULT_CACHE_PATH, false, false))
  728. {
  729. return;
  730. }
  731. // Check EditorPrefs
  732. string pref = EditorPrefs.GetString("fr2_cache.guid", string.Empty);
  733. if (RestoreCacheFromGUID(pref, false, false))
  734. {
  735. return;
  736. }
  737. // Read GUID from File
  738. if (File.Exists("Library/fr2_cache.guid"))
  739. {
  740. if (RestoreCacheFromGUID(File.ReadAllText("Library/fr2_cache.guid"), true, false))
  741. {
  742. return;
  743. }
  744. }
  745. // Search whole project
  746. string[] allAssets = AssetDatabase.GetAllAssetPaths();
  747. for (var i = 0; i < allAssets.Length; i++)
  748. {
  749. if (allAssets[i].EndsWith("/FR2_Cache.asset", StringComparison.Ordinal))
  750. {
  751. RestoreCacheFromPath(allAssets[i], true, true);
  752. break;
  753. }
  754. }
  755. }
  756. internal static void DeleteCache()
  757. {
  758. if (_cache == null)
  759. {
  760. return;
  761. }
  762. try
  763. {
  764. if (!string.IsNullOrEmpty(_cachePath))
  765. {
  766. AssetDatabase.DeleteAsset(_cachePath);
  767. }
  768. }
  769. catch { }
  770. AssetDatabase.SaveAssets();
  771. AssetDatabase.Refresh();
  772. }
  773. internal static void CreateCache()
  774. {
  775. _cache = CreateInstance<FR2_Cache>();
  776. _cache._curCacheVersion = CACHE_VERSION;
  777. string path = Application.dataPath + DEFAULT_CACHE_PATH
  778. .Substring(0, DEFAULT_CACHE_PATH.LastIndexOf('/') + 1).Replace("Assets", string.Empty);
  779. if (!Directory.Exists(path))
  780. {
  781. Directory.CreateDirectory(path);
  782. }
  783. AssetDatabase.CreateAsset(_cache, DEFAULT_CACHE_PATH);
  784. EditorUtility.SetDirty(_cache);
  785. FoundCache(true, true);
  786. DelayCheck4Changes();
  787. }
  788. internal static List<string> FindUsage(string[] listGUIDs)
  789. {
  790. if (!isReady)
  791. {
  792. return null;
  793. }
  794. List<FR2_Asset> refs = Api.FindAssets(listGUIDs, true);
  795. for (var i = 0; i < refs.Count; i++)
  796. {
  797. List<FR2_Asset> tmp = FR2_Asset.FindUsage(refs[i]);
  798. for (var j = 0; j < tmp.Count; j++)
  799. {
  800. FR2_Asset itm = tmp[j];
  801. if (refs.Contains(itm))
  802. {
  803. continue;
  804. }
  805. refs.Add(itm);
  806. }
  807. }
  808. return refs.Select(item => item.guid).ToList();
  809. }
  810. private void OnEnable()
  811. {
  812. #if FR2_DEBUG
  813. Debug.Log("OnEnabled : " + _cache);
  814. #endif
  815. if (_cache == null)
  816. {
  817. _cache = this;
  818. }
  819. Check4Changes(false);
  820. }
  821. internal void ReadFromCache()
  822. {
  823. if (AssetList == null) AssetList = new List<FR2_Asset>();
  824. FR2_Unity.Clear(ref queueLoadContent);
  825. FR2_Unity.Clear(ref AssetMap);
  826. for (var i = 0; i < AssetList.Count; i++)
  827. {
  828. var item = AssetList[i];
  829. item.state = FR2_AssetState.CACHE;
  830. var path = AssetDatabase.GUIDToAssetPath(item.guid);
  831. if (string.IsNullOrEmpty(path))
  832. {
  833. item.type = FR2_AssetType.UNKNOWN; // to make sure if GUIDs being reused for a different kind of asset
  834. item.state = FR2_AssetState.MISSING;
  835. AssetMap.Add(item.guid, item);
  836. continue;
  837. }
  838. if (AssetMap.ContainsKey(item.guid))
  839. {
  840. #if FR2_DEBUG
  841. Debug.LogWarning("Something wrong, cache found twice <" + item.guid + ">");
  842. #endif
  843. continue;
  844. }
  845. AssetMap.Add(item.guid, item);
  846. }
  847. }
  848. internal void ReadFromProject(bool force)
  849. {
  850. if (AssetMap == null || AssetMap.Count == 0) ReadFromCache();
  851. var paths = AssetDatabase.GetAllAssetPaths();
  852. cacheStamp++;
  853. workCount = 0;
  854. if (queueLoadContent != null) queueLoadContent.Clear();
  855. // Check for new assets
  856. foreach (var p in paths)
  857. {
  858. var isValid = FR2_Unity.StringStartsWith(p, "Assets/", "Packages/", "Library/", "ProjectSettings/");
  859. if (!isValid)
  860. {
  861. #if FR2_DEBUG
  862. Debug.LogWarning("Ignore asset: " + p);
  863. #endif
  864. continue;
  865. }
  866. var guid = AssetDatabase.AssetPathToGUID(p);
  867. if (!FR2_Asset.IsValidGUID(guid))
  868. {
  869. continue;
  870. }
  871. FR2_Asset asset;
  872. if (!AssetMap.TryGetValue(guid, out asset))
  873. {
  874. AddAsset(guid);
  875. }
  876. else
  877. {
  878. asset.refreshStamp = cacheStamp; // mark this asset so it won't be deleted
  879. if (!asset.isDirty && !force) continue;
  880. if (force) asset.MarkAsDirty(true, true);
  881. workCount++;
  882. queueLoadContent.Add(asset);
  883. }
  884. }
  885. // Check for deleted assets
  886. for (var i = AssetList.Count - 1; i >= 0; i--)
  887. {
  888. if (AssetList[i].refreshStamp != cacheStamp)
  889. {
  890. RemoveAsset(AssetList[i]);
  891. }
  892. }
  893. }
  894. internal static void DelayCheck4Changes()
  895. {
  896. EditorApplication.update -= Check;
  897. EditorApplication.update += Check;
  898. }
  899. static void Check()
  900. {
  901. if (EditorApplication.isCompiling || EditorApplication.isUpdating) return;
  902. if (Api == null) return;
  903. EditorApplication.update -= Check;
  904. Api.Check4Changes(false);
  905. }
  906. internal void Check4Changes(bool force)
  907. {
  908. if (EditorApplication.isCompiling || EditorApplication.isUpdating)
  909. {
  910. DelayCheck4Changes();
  911. return;
  912. }
  913. ready = false;
  914. ReadFromProject(force);
  915. #if FR2_DEBUG
  916. Debug.Log("After checking :: WorkCount :: " + workCount + ":" + AssetMap.Count + ":" + AssetList.Count);
  917. #endif
  918. Check4Work();
  919. }
  920. internal void RefreshAsset(string guid, bool force)
  921. {
  922. FR2_Asset asset;
  923. if (!AssetMap.TryGetValue(guid, out asset))
  924. {
  925. return;
  926. }
  927. RefreshAsset(asset, force);
  928. }
  929. internal void RefreshSelection()
  930. {
  931. string[] list = FR2_Unity.Selection_AssetGUIDs;
  932. for (var i = 0; i < list.Length; i++)
  933. {
  934. RefreshAsset(list[i], true);
  935. }
  936. Check4Work();
  937. }
  938. internal void RefreshAsset(FR2_Asset asset, bool force)
  939. {
  940. asset.MarkAsDirty(true, force);
  941. DelayCheck4Changes();
  942. //#if FR2_DEBUG
  943. // Debug.Log("RefreshAsset: " + asset.guid + ":" + workCount);
  944. //#endif
  945. //
  946. // workCount++;
  947. //
  948. // if (force)
  949. // {
  950. // asset.MarkAsDirty(true, true);
  951. //
  952. // if (asset.type == FR2_AssetType.FOLDER && !asset.IsMissing)
  953. // {
  954. // string[] dirs = Directory.GetDirectories(asset.assetPath, "*", SearchOption.AllDirectories);
  955. // //refresh children directories as well
  956. //
  957. // for (var i = 0; i < dirs.Length; i++)
  958. // {
  959. // string guid = AssetDatabase.AssetPathToGUID(dirs[i]);
  960. // FR2_Asset child = Api.Get(guid);
  961. // if (child == null)
  962. // {
  963. // continue;
  964. // }
  965. //
  966. // workCount++;
  967. // child.MarkAsDirty();
  968. // queueLoadContent.Add(child);
  969. // }
  970. // }
  971. // }
  972. //
  973. // queueLoadContent.Add(asset);
  974. }
  975. internal void AddAsset(string guid)
  976. {
  977. if (AssetMap.ContainsKey(guid))
  978. {
  979. Debug.LogWarning("guid already exist <" + guid + ">");
  980. return;
  981. }
  982. var asset = new FR2_Asset(guid);
  983. asset.LoadPathInfo();
  984. asset.refreshStamp = cacheStamp;
  985. AssetList.Add(asset);
  986. AssetMap.Add(guid, asset);
  987. //Debug.LogWarning("Add - AssetList: " + AssetList.Count);
  988. // Do not load content for FR2_Cache asset
  989. if (guid == CacheGUID)
  990. {
  991. return;
  992. }
  993. workCount++;
  994. queueLoadContent.Add(asset);
  995. }
  996. internal void RemoveAsset(string guid)
  997. {
  998. if (!AssetMap.ContainsKey(guid))
  999. {
  1000. return;
  1001. }
  1002. RemoveAsset(AssetMap[guid]);
  1003. }
  1004. internal void RemoveAsset(FR2_Asset asset)
  1005. {
  1006. AssetList.Remove(asset);
  1007. // Deleted Asset : still in the map but not in the AssetList
  1008. asset.state = FR2_AssetState.MISSING;
  1009. }
  1010. internal void Check4Usage()
  1011. {
  1012. #if FR2_DEBUG
  1013. Debug.Log("Check 4 Usage");
  1014. #endif
  1015. foreach (var item in AssetList)
  1016. {
  1017. if (item.IsMissing) continue;
  1018. FR2_Unity.Clear(ref item.UsedByMap);
  1019. }
  1020. foreach (var item in AssetList)
  1021. {
  1022. if (item.IsMissing) continue;
  1023. AsyncUsedBy(item);
  1024. }
  1025. workCount = 0;
  1026. ready = true;
  1027. }
  1028. internal void Check4Work()
  1029. {
  1030. if (disabled) return;
  1031. if (workCount == 0)
  1032. {
  1033. Check4Usage();
  1034. return;
  1035. }
  1036. ready = false;
  1037. EditorApplication.update -= AsyncProcess;
  1038. EditorApplication.update += AsyncProcess;
  1039. }
  1040. internal void AsyncProcess()
  1041. {
  1042. if (this == null)
  1043. {
  1044. return;
  1045. }
  1046. if (EditorApplication.isCompiling || EditorApplication.isUpdating)
  1047. {
  1048. return;
  1049. }
  1050. if (frameSkipped++ < 10 - 2 * priority)
  1051. {
  1052. return;
  1053. }
  1054. frameSkipped = 0;
  1055. float t = Time.realtimeSinceStartup;
  1056. #if FR2_DEBUG
  1057. Debug.Log(Mathf.Round(t) + " : " + progress*workCount + "/" + workCount + ":" + isReady + " ::: " + queueLoadContent.Count);
  1058. #endif
  1059. if (!AsyncWork(queueLoadContent, AsyncLoadContent, t))
  1060. {
  1061. return;
  1062. }
  1063. EditorApplication.update -= AsyncProcess;
  1064. EditorUtility.SetDirty(this);
  1065. Check4Usage();
  1066. }
  1067. internal bool AsyncWork<T>(List<T> arr, Action<int, T> action, float t)
  1068. {
  1069. float FRAME_DURATION = 1 / 1000f * (priority * 5 + 1); //prevent zero
  1070. int c = arr.Count;
  1071. var counter = 0;
  1072. while (c-- > 0)
  1073. {
  1074. T last = arr[c];
  1075. arr.RemoveAt(c);
  1076. action(c, last);
  1077. //workCount--;
  1078. float dt = Time.realtimeSinceStartup - t - FRAME_DURATION;
  1079. if (dt >= 0)
  1080. {
  1081. return false;
  1082. }
  1083. counter++;
  1084. }
  1085. return true;
  1086. }
  1087. internal void AsyncLoadContent(int idx, FR2_Asset asset)
  1088. {
  1089. //Debug.Log("Async: " + idx);
  1090. if (asset.fileInfoDirty) asset.LoadFileInfo();
  1091. if (asset.fileContentDirty) asset.LoadContent();
  1092. }
  1093. internal void AsyncUsedBy(FR2_Asset asset)
  1094. {
  1095. if (AssetMap == null)
  1096. {
  1097. Check4Changes(false);
  1098. }
  1099. if (asset.IsFolder)
  1100. {
  1101. return;
  1102. }
  1103. #if FR2_DEBUG
  1104. Debug.Log("Async UsedBy: " + asset.assetPath);
  1105. #endif
  1106. foreach (KeyValuePair<string, HashSet<int>> item in asset.UseGUIDs)
  1107. {
  1108. FR2_Asset tAsset;
  1109. if (AssetMap.TryGetValue(item.Key, out tAsset))
  1110. {
  1111. if (tAsset == null || tAsset.UsedByMap == null)
  1112. {
  1113. continue;
  1114. }
  1115. if (!tAsset.UsedByMap.ContainsKey(asset.guid))
  1116. {
  1117. tAsset.AddUsedBy(asset.guid, asset);
  1118. }
  1119. }
  1120. }
  1121. }
  1122. //---------------------------- Dependencies -----------------------------
  1123. internal FR2_Asset Get(string guid, bool isForce = false)
  1124. {
  1125. return AssetMap.ContainsKey(guid) ? AssetMap[guid] : null;
  1126. }
  1127. internal List<FR2_Asset> FindAssetsOfType(FR2_AssetType type)
  1128. {
  1129. var result = new List<FR2_Asset>();
  1130. foreach (KeyValuePair<string, FR2_Asset> item in AssetMap)
  1131. {
  1132. if (item.Value.type != type)
  1133. {
  1134. continue;
  1135. }
  1136. result.Add(item.Value);
  1137. }
  1138. return result;
  1139. }
  1140. internal FR2_Asset FindAsset(string guid, string fileId)
  1141. {
  1142. if (AssetMap == null)
  1143. {
  1144. Check4Changes(false);
  1145. }
  1146. if (!isReady)
  1147. {
  1148. #if FR2_DEBUG
  1149. Debug.LogWarning("Cache not ready !");
  1150. #endif
  1151. return null;
  1152. }
  1153. if (string.IsNullOrEmpty(guid))
  1154. {
  1155. return null;
  1156. }
  1157. //for (var i = 0; i < guids.Length; i++)
  1158. {
  1159. //string guid = guids[i];
  1160. FR2_Asset asset;
  1161. if (!AssetMap.TryGetValue(guid, out asset))
  1162. {
  1163. return null;
  1164. }
  1165. if (asset.IsMissing)
  1166. {
  1167. return null;
  1168. }
  1169. if (asset.IsFolder)
  1170. {
  1171. return null;
  1172. }
  1173. else
  1174. {
  1175. return asset;
  1176. }
  1177. }
  1178. }
  1179. internal List<FR2_Asset> FindAssets(string[] guids, bool scanFolder)
  1180. {
  1181. if (AssetMap == null)
  1182. {
  1183. Check4Changes(false);
  1184. }
  1185. var result = new List<FR2_Asset>();
  1186. if (!isReady)
  1187. {
  1188. #if FR2_DEBUG
  1189. Debug.LogWarning("Cache not ready !");
  1190. #endif
  1191. return result;
  1192. }
  1193. var folderList = new List<FR2_Asset>();
  1194. if (guids.Length == 0)
  1195. {
  1196. return result;
  1197. }
  1198. for (var i = 0; i < guids.Length; i++)
  1199. {
  1200. string guid = guids[i];
  1201. FR2_Asset asset;
  1202. if (!AssetMap.TryGetValue(guid, out asset))
  1203. {
  1204. continue;
  1205. }
  1206. if (asset.IsMissing)
  1207. {
  1208. continue;
  1209. }
  1210. if (asset.IsFolder)
  1211. {
  1212. if (!folderList.Contains(asset))
  1213. {
  1214. folderList.Add(asset);
  1215. }
  1216. }
  1217. else
  1218. {
  1219. result.Add(asset);
  1220. }
  1221. }
  1222. if (!scanFolder || folderList.Count == 0)
  1223. {
  1224. return result;
  1225. }
  1226. int count = folderList.Count;
  1227. for (var i = 0; i < count; i++)
  1228. {
  1229. FR2_Asset item = folderList[i];
  1230. // for (var j = 0; j < item.UseGUIDs.Count; j++)
  1231. // {
  1232. // FR2_Asset a;
  1233. // if (!AssetMap.TryGetValue(item.UseGUIDs[j], out a)) continue;
  1234. foreach (KeyValuePair<string, HashSet<int>> useM in item.UseGUIDs)
  1235. {
  1236. FR2_Asset a;
  1237. if (!AssetMap.TryGetValue(useM.Key, out a))
  1238. {
  1239. continue;
  1240. }
  1241. if (a.IsMissing)
  1242. {
  1243. continue;
  1244. }
  1245. if (a.IsFolder)
  1246. {
  1247. if (!folderList.Contains(a))
  1248. {
  1249. folderList.Add(a);
  1250. count++;
  1251. }
  1252. }
  1253. else
  1254. {
  1255. result.Add(a);
  1256. }
  1257. }
  1258. }
  1259. return result;
  1260. }
  1261. //---------------------------- Dependencies -----------------------------
  1262. internal List<List<string>> ScanSimilar(Action IgnoreWhenScan, Action IgnoreFolderWhenScan)
  1263. {
  1264. if (AssetMap == null)
  1265. {
  1266. Check4Changes(true);
  1267. }
  1268. var dict = new Dictionary<string, List<FR2_Asset>>();
  1269. foreach (KeyValuePair<string, FR2_Asset> item in AssetMap)
  1270. {
  1271. if (item.Value == null)
  1272. {
  1273. continue;
  1274. }
  1275. if (item.Value.IsMissing || item.Value.IsFolder)
  1276. {
  1277. continue;
  1278. }
  1279. if (item.Value.inPlugins)
  1280. {
  1281. continue;
  1282. }
  1283. if (item.Value.inEditor)
  1284. {
  1285. continue;
  1286. }
  1287. // if (item.Value.extension != ".png" && item.Value.extension != ".jpg") continue;
  1288. if (FR2_Setting.IsTypeExcluded(AssetType.GetIndex(item.Value.extension)))
  1289. {
  1290. // Debug.LogWarning("ignore: " +item.Value.assetPath);
  1291. if (IgnoreWhenScan != null)
  1292. {
  1293. IgnoreWhenScan();
  1294. }
  1295. continue;
  1296. }
  1297. var isBreak = false;
  1298. foreach (string ignore in FR2_Setting.s.listIgnore)
  1299. {
  1300. if (item.Value.assetPath.StartsWith(ignore))
  1301. {
  1302. isBreak = true;
  1303. if (IgnoreFolderWhenScan != null)
  1304. {
  1305. IgnoreFolderWhenScan();
  1306. }
  1307. // Debug.Log("ignore " + item.Value.assetPath + " path ignore " + ignore);
  1308. break;
  1309. }
  1310. }
  1311. if (isBreak)
  1312. {
  1313. continue;
  1314. }
  1315. string hash = item.Value.fileInfoHash;
  1316. if (string.IsNullOrEmpty(hash))
  1317. {
  1318. #if FR2_DEBUG
  1319. Debug.LogWarning("Hash can not be null! ");
  1320. #endif
  1321. continue;
  1322. }
  1323. List<FR2_Asset> list;
  1324. if (!dict.TryGetValue(hash, out list))
  1325. {
  1326. list = new List<FR2_Asset>();
  1327. dict.Add(hash, list);
  1328. }
  1329. list.Add(item.Value);
  1330. }
  1331. List<List<FR2_Asset>> result = dict.Values.Where(item => item.Count > 1).ToList();
  1332. result.Sort((item1, item2) => { return item2[0].fileSize.CompareTo(item1[0].fileSize); });
  1333. return result.Select(l => l.Select(i => i.assetPath).ToList()).ToList();
  1334. }
  1335. //internal List<FR2_DuplicateInfo> ScanDuplication(){
  1336. // if (AssetMap == null) Check4Changes(false);
  1337. // var dict = new Dictionary<string, FR2_DuplicateInfo>();
  1338. // foreach (var item in AssetMap){
  1339. // if (item.Value.IsMissing || item.Value.IsFolder) continue;
  1340. // var hash = item.Value.GetFileInfoHash();
  1341. // FR2_DuplicateInfo info;
  1342. // if (!dict.TryGetValue(hash, out info)){
  1343. // info = new FR2_DuplicateInfo(hash, item.Value.fileSize);
  1344. // dict.Add(hash, info);
  1345. // }
  1346. // info.assets.Add(item.Value);
  1347. // }
  1348. // var result = new List<FR2_DuplicateInfo>();
  1349. // foreach (var item in dict){
  1350. // if (item.Value.assets.Count > 1){
  1351. // result.Add(item.Value);
  1352. // }
  1353. // }
  1354. // result.Sort((item1, item2)=>{
  1355. // return item2.fileSize.CompareTo(item1.fileSize);
  1356. // });
  1357. // return result;
  1358. //}
  1359. private static HashSet<string> SPECIAL_USE_ASSETS = new HashSet<string>()
  1360. {
  1361. "Assets/link.xml", // this file used to control build/link process do not remove
  1362. "Assets/csc.rsp",
  1363. "Assets/mcs.rsp",
  1364. "Assets/GoogleService-Info.plist",
  1365. "Assets/google-services.json",
  1366. };
  1367. private static HashSet<string> SPECIAL_EXTENSIONS = new HashSet<string>()
  1368. {
  1369. ".asmdef",
  1370. ".cginc",
  1371. ".cs",
  1372. ".dll",
  1373. };
  1374. internal List<FR2_Asset> ScanUnused()
  1375. {
  1376. if (AssetMap == null)
  1377. {
  1378. Check4Changes(false);
  1379. }
  1380. var result = new List<FR2_Asset>();
  1381. foreach (KeyValuePair<string, FR2_Asset> item in AssetMap)
  1382. {
  1383. FR2_Asset v = item.Value;
  1384. if (v.IsMissing || v.inEditor || v.IsScript || v.inResources || v.inPlugins || v.inStreamingAsset ||
  1385. v.IsFolder)
  1386. {
  1387. continue;
  1388. }
  1389. if (!v.assetPath.StartsWith("Assets/")) continue; // ignore built-in / packages assets
  1390. if (SPECIAL_USE_ASSETS.Contains(v.assetPath)) continue; // ignore assets with special use (can not remove)
  1391. if (SPECIAL_EXTENSIONS.Contains(v.extension)) continue;
  1392. if (v.type == FR2_AssetType.DLL) continue;
  1393. if (v.type == FR2_AssetType.SCRIPT) continue;
  1394. if (v.type == FR2_AssetType.UNKNOWN) continue;
  1395. if (v.IsExcluded) continue;
  1396. if (!string.IsNullOrEmpty(v.AtlasName)) continue;
  1397. if (!string.IsNullOrEmpty(v.AssetBundleName)) continue;
  1398. if (!string.IsNullOrEmpty(v.AddressableName)) continue;
  1399. if (v.UsedByMap.Count == 0) //&& !FR2_Asset.IGNORE_UNUSED_GUIDS.Contains(v.guid)
  1400. {
  1401. result.Add(v);
  1402. }
  1403. }
  1404. result.Sort((item1, item2) =>
  1405. {
  1406. if (item1.extension == item2.extension)
  1407. {
  1408. return item1.assetPath.CompareTo(item2.assetPath);
  1409. }
  1410. return item1.extension.CompareTo(item2.extension);
  1411. });
  1412. return result;
  1413. }
  1414. }
  1415. }