DependencyGraphProvider.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Linq;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using IntelligentProjectAnalyzer.Editor.Graphing;
  5. using IntelligentProjectAnalyzer.Editor.DependencyViewer;
  6. using Object = UnityEngine.Object;
  7. namespace LLM.Editor.Analysis
  8. {
  9. /// <summary>
  10. /// Provides dependency information for a given asset by querying the cached DependencyGraph.
  11. /// </summary>
  12. public class DependencyGraphProvider : IContextProvider
  13. {
  14. public object GetContext(Object target, string qualifier)
  15. {
  16. if (!target)
  17. {
  18. return "Error: A valid target asset is required to get dependencies.";
  19. }
  20. var path = AssetDatabase.GetAssetPath(target);
  21. var guid = AssetDatabase.AssetPathToGUID(path);
  22. if (string.IsNullOrEmpty(guid))
  23. {
  24. return $"Error: Could not get GUID for asset '{target.name}'.";
  25. }
  26. var dependencyGraph = new DependencyGraph();
  27. if (!dependencyGraph.AssetExists(guid))
  28. {
  29. return $"Error: Asset '{target.name}' not found in the dependency graph. The cache might be out of date.";
  30. }
  31. // The qualifier determines if we get all recursive dependencies or only direct ones.
  32. var recursive = qualifier?.ToLower() == "recursive";
  33. var dependencyGuids = new HashSet<string>();
  34. if (recursive)
  35. {
  36. GetAllDependencies(guid, dependencyGraph, dependencyGuids);
  37. }
  38. else
  39. {
  40. dependencyGuids = new HashSet<string>(dependencyGraph.GetDependencies(guid));
  41. }
  42. // Convert the final list of GUIDs into structured, readable nodes.
  43. return dependencyGuids.Select(AssetNodeResolver.CreateNodeFromGuid).ToList();
  44. }
  45. /// <summary>
  46. /// Recursively traverses the dependency graph to collect all unique dependencies.
  47. /// </summary>
  48. private static void GetAllDependencies(string currentGuid, DependencyGraph graph, HashSet<string> collectedGuids)
  49. {
  50. var directDependencies = graph.GetDependencies(currentGuid);
  51. foreach (var depGuid in directDependencies.Where(collectedGuids.Add))
  52. {
  53. GetAllDependencies(depGuid, graph, collectedGuids);
  54. }
  55. }
  56. }
  57. }