SourceCodeProvider.cs 993 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace LLM.Editor.Analysis
  5. {
  6. /// <summary>
  7. /// Provides the full text content of a C# script file.
  8. /// </summary>
  9. public class SourceCodeProvider : IContextProvider
  10. {
  11. public object GetContext(Object target, string qualifier)
  12. {
  13. string path = null;
  14. if (target is MonoScript script)
  15. {
  16. path = AssetDatabase.GetAssetPath(script);
  17. }
  18. else if (target != null)
  19. {
  20. path = AssetDatabase.GetAssetPath(target);
  21. }
  22. if (string.IsNullOrEmpty(path) || !path.EndsWith(".cs"))
  23. {
  24. return "Error: Target must be a MonoScript or a valid path to a C# file.";
  25. }
  26. if (!File.Exists(path))
  27. {
  28. return $"Error: File not found at path '{path}'.";
  29. }
  30. return File.ReadAllText(path);
  31. }
  32. }
  33. }