GetDataFromPathProvider.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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(step.name) : target is Component ? target : 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. }
  48. if (currentObject is not Component component) return currentObject;
  49. var allComponents = component.GetComponents<Component>()
  50. .Select(c => c.GetType().FullName)
  51. .ToList();
  52. var result = new Dictionary<string, object>
  53. {
  54. ["requestedComponentData"] = component,
  55. ["allComponentsOnGameObject"] = allComponents
  56. };
  57. if (component is not MonoBehaviour monoBehaviour || !IsCustomScript(monoBehaviour)) return result;
  58. // Eager loading for custom scripts
  59. var script = MonoScript.FromMonoBehaviour(monoBehaviour);
  60. var scriptPath = AssetDatabase.GetAssetPath(script);
  61. result["sourceCode"] = File.ReadAllText(scriptPath);
  62. return result;
  63. }
  64. private static bool IsCustomScript(Component component)
  65. {
  66. if (!component) return false;
  67. var script = MonoScript.FromMonoBehaviour(component as MonoBehaviour);
  68. if (!script) return false;
  69. var path = AssetDatabase.GetAssetPath(script);
  70. return !string.IsNullOrEmpty(path) && path.StartsWith("Assets/");
  71. }
  72. }
  73. [Serializable]
  74. public class PathRequest
  75. {
  76. public List<PathStep> steps;
  77. }
  78. [Serializable]
  79. public class PathStep
  80. {
  81. public string type;
  82. public string name;
  83. }
  84. }