DataExtractorMenu.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.Diagnostics;
  5. namespace LLM.Editor
  6. {
  7. public static class DataExtractorMenu
  8. {
  9. private const string HighLevelScript = "Assets/LLM/source/orchestrators/extract_high_level.py";
  10. private const string MidLevelScript = "Assets/LLM/source/orchestrators/extract_mid_level.py";
  11. private const string LowLevelScript = "Assets/LLM/source/orchestrators/extract_low_level.py";
  12. private const string CreateConfigScript = "Assets/LLM/source/orchestrators/create_config.py";
  13. [MenuItem("Tools/DataExtractor/High Level Export")]
  14. private static void ExportHighLevel()
  15. {
  16. RunExtractor("High Level", HighLevelScript, true);
  17. }
  18. [MenuItem("Tools/DataExtractor/Mid Level Export")]
  19. private static void ExportMidLevel()
  20. {
  21. RunExtractor("Mid Level", MidLevelScript, true);
  22. }
  23. [MenuItem("Tools/DataExtractor/Low Level Export")]
  24. private static void ExportLowLevel()
  25. {
  26. RunExtractor("Low Level", LowLevelScript, true);
  27. }
  28. [MenuItem("Tools/DataExtractor/Create or Update Config File")]
  29. private static void CreateOrUpdateConfigFile()
  30. {
  31. RunExtractor("Create Config", CreateConfigScript, false);
  32. }
  33. private static void RunExtractor(string level, string scriptPath, bool requireOutputPath)
  34. {
  35. string outputPath = "";
  36. if (requireOutputPath)
  37. {
  38. outputPath = EditorUtility.OpenFolderPanel($"Select Output Folder for {level} Export", "", "");
  39. if (string.IsNullOrEmpty(outputPath))
  40. {
  41. UnityEngine.Debug.Log($"{level} export cancelled by user.");
  42. return;
  43. }
  44. }
  45. var projectRoot = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
  46. var pythonExecutable = Application.platform == RuntimePlatform.WindowsEditor ?
  47. Path.Combine(projectRoot, "venv", "Scripts", "python.exe") :
  48. Path.Combine(projectRoot, "venv", "bin", "python");
  49. var fullScriptPath = Path.Combine(projectRoot, scriptPath);
  50. if (!File.Exists(pythonExecutable))
  51. {
  52. UnityEngine.Debug.LogError($"Python executable not found at: {pythonExecutable}");
  53. EditorUtility.DisplayDialog("Export Error", "The Python virtual environment executable was not found. Please ensure the 'venv' directory is set up correctly.", "OK");
  54. return;
  55. }
  56. if (!File.Exists(fullScriptPath))
  57. {
  58. UnityEngine.Debug.LogError($"Extractor script not found at: {fullScriptPath}");
  59. EditorUtility.DisplayDialog("Export Error", $"The Python script for the {level} task was not found.", "OK");
  60. return;
  61. }
  62. var arguments = requireOutputPath ?
  63. $"\"{fullScriptPath}\" --input \"{projectRoot}\" --output \"{outputPath}\"" :
  64. $"{fullScriptPath}";
  65. UnityEngine.Debug.Log($"Running command: \"{pythonExecutable}\" {arguments}");
  66. var process = new Process
  67. {
  68. StartInfo = new ProcessStartInfo
  69. {
  70. FileName = pythonExecutable,
  71. Arguments = arguments,
  72. RedirectStandardOutput = true,
  73. RedirectStandardError = true,
  74. UseShellExecute = false,
  75. CreateNoWindow = true,
  76. WorkingDirectory = projectRoot
  77. }
  78. };
  79. EditorUtility.DisplayProgressBar("Data Extractor", $"Running {level} task...", 0.5f);
  80. process.OutputDataReceived += (_, e) => { if (e.Data != null) UnityEngine.Debug.Log($"[Extractor] {e.Data}"); };
  81. process.ErrorDataReceived += (_, e) => { if (e.Data != null) UnityEngine.Debug.LogError($"[Extractor ERROR] {e.Data}"); };
  82. process.Start();
  83. process.BeginOutputReadLine();
  84. process.BeginErrorReadLine();
  85. process.WaitForExit();
  86. EditorUtility.ClearProgressBar();
  87. if (process.ExitCode == 0)
  88. {
  89. EditorUtility.DisplayDialog("Task Complete", $"{level} task finished successfully.", "OK");
  90. }
  91. else
  92. {
  93. EditorUtility.DisplayDialog("Task Failed", $"The {level} task failed. Check the Unity Console for error messages.", "OK");
  94. }
  95. }
  96. }
  97. }