ResourceAnalyzerController.CircularDependencyChecker.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. namespace UnityGameFramework.Editor.ResourceTools
  10. {
  11. public sealed partial class ResourceAnalyzerController
  12. {
  13. private sealed class CircularDependencyChecker
  14. {
  15. private readonly Stamp[] m_Stamps;
  16. public CircularDependencyChecker(Stamp[] stamps)
  17. {
  18. m_Stamps = stamps;
  19. }
  20. public string[][] Check()
  21. {
  22. HashSet<string> hosts = new HashSet<string>();
  23. foreach (Stamp stamp in m_Stamps)
  24. {
  25. hosts.Add(stamp.HostAssetName);
  26. }
  27. List<string[]> results = new List<string[]>();
  28. foreach (string host in hosts)
  29. {
  30. LinkedList<string> route = new LinkedList<string>();
  31. HashSet<string> visited = new HashSet<string>();
  32. if (Check(host, route, visited))
  33. {
  34. results.Add(route.ToArray());
  35. }
  36. }
  37. return results.ToArray();
  38. }
  39. private bool Check(string host, LinkedList<string> route, HashSet<string> visited)
  40. {
  41. visited.Add(host);
  42. route.AddLast(host);
  43. foreach (Stamp stamp in m_Stamps)
  44. {
  45. if (host != stamp.HostAssetName)
  46. {
  47. continue;
  48. }
  49. if (visited.Contains(stamp.DependencyAssetName))
  50. {
  51. route.AddLast(stamp.DependencyAssetName);
  52. return true;
  53. }
  54. if (Check(stamp.DependencyAssetName, route, visited))
  55. {
  56. return true;
  57. }
  58. }
  59. route.RemoveLast();
  60. visited.Remove(host);
  61. return false;
  62. }
  63. }
  64. }
  65. }