GetDataFromPathProvider.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using System.Reflection;
  7. using LLM.Editor.Helper;
  8. using System.Collections.Generic;
  9. using Object = UnityEngine.Object;
  10. namespace LLM.Editor.Analysis
  11. {
  12. public class GetDataFromPathProvider : IContextProvider
  13. {
  14. public object GetContext(Object target, string qualifier)
  15. {
  16. if (string.IsNullOrEmpty(qualifier))
  17. {
  18. return "Error: A JSON path (qualifier) is required for GetDataFromPath.";
  19. }
  20. var path = qualifier.FromJson<PathRequest>();
  21. if (path?.steps == null || !path.steps.Any())
  22. {
  23. return "Error: Invalid path provided.";
  24. }
  25. object currentObject = target;
  26. var currentGameObject = target switch
  27. {
  28. GameObject gameObject => gameObject,
  29. Component component2 => component2.gameObject,
  30. _ => null
  31. };
  32. foreach (var step in path.steps)
  33. {
  34. if (currentObject == null)
  35. {
  36. return "Error: Path evaluation failed at a null object.";
  37. }
  38. Debug.Log($"Object is: {currentObject}. Type: {currentObject.GetType()}");
  39. currentObject = step.type.ToLower() switch
  40. {
  41. "component" => currentGameObject ? currentGameObject.GetComponent(GetTypeByName(step.name)) : null,
  42. "field" => currentObject.GetType().GetField(step.name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(currentObject),
  43. "property" => currentObject.GetType().GetProperty(step.name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(currentObject),
  44. "child" => ((GameObject)currentObject).transform.Find(step.name)?.gameObject,
  45. _ => "Error: Unknown path step type."
  46. };
  47. // If we just navigated to a new GameObject, update our reference.
  48. if (currentObject is GameObject newGo)
  49. {
  50. currentGameObject = newGo;
  51. }
  52. }
  53. if (currentObject is not Component component) return currentObject;
  54. var allComponents = component.GetComponents<Component>()
  55. .Select(c => c.GetType().FullName)
  56. .ToList();
  57. var result = new Dictionary<string, object>
  58. {
  59. ["requestedComponentData"] = component,
  60. ["allComponentsOnGameObject"] = allComponents
  61. };
  62. if (component is not MonoBehaviour monoBehaviour || !IsCustomScript(monoBehaviour)) return result;
  63. // Eager loading for custom scripts
  64. var script = MonoScript.FromMonoBehaviour(monoBehaviour);
  65. var scriptPath = AssetDatabase.GetAssetPath(script);
  66. result["sourceCode"] = File.ReadAllText(scriptPath);
  67. return result;
  68. }
  69. private static Type GetTypeByName(string typeName)
  70. {
  71. foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
  72. {
  73. var type = assembly.GetType(typeName);
  74. if (type != null) return type;
  75. }
  76. return null;
  77. }
  78. private static bool IsCustomScript(Component component)
  79. {
  80. if (!component) return false;
  81. var script = MonoScript.FromMonoBehaviour(component as MonoBehaviour);
  82. if (!script) return false;
  83. var path = AssetDatabase.GetAssetPath(script);
  84. return !string.IsNullOrEmpty(path) && path.StartsWith("Assets/");
  85. }
  86. }
  87. [Serializable]
  88. public class PathRequest
  89. {
  90. public List<PathStep> steps;
  91. }
  92. [Serializable]
  93. public class PathStep
  94. {
  95. public string type;
  96. public string name;
  97. }
  98. }