DataExtractorMenu.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/extract_high_level.py";
  10. private const string MidLevelScript = "Assets/LLM/source/extract_mid_level.py";
  11. private const string LowLevelScript = "Assets/LLM/source/extract_low_level.py";
  12. [MenuItem("Tools/DataExtractor/High Level Export")]
  13. private static void ExportHighLevel()
  14. {
  15. RunExtractor("High Level", HighLevelScript);
  16. }
  17. [MenuItem("Tools/DataExtractor/Mid Level Export")]
  18. private static void ExportMidLevel()
  19. {
  20. RunExtractor("Mid Level", MidLevelScript);
  21. }
  22. [MenuItem("Tools/DataExtractor/Low Level Export")]
  23. private static void ExportLowLevel()
  24. {
  25. RunExtractor("Low Level", LowLevelScript);
  26. }
  27. private static void RunExtractor(string level, string scriptPath)
  28. {
  29. var outputPath = EditorUtility.OpenFolderPanel($"Select Output Folder for {level} Export", "", "");
  30. if (string.IsNullOrEmpty(outputPath))
  31. {
  32. UnityEngine.Debug.Log($"{level} export cancelled by user.");
  33. return;
  34. }
  35. var projectRoot = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
  36. var pythonExecutable = Path.Combine(projectRoot, "venv", "bin", "python3");
  37. var fullScriptPath = Path.Combine(projectRoot, scriptPath);
  38. if (!File.Exists(pythonExecutable))
  39. {
  40. UnityEngine.Debug.LogError($"Python executable not found at: {pythonExecutable}");
  41. EditorUtility.DisplayDialog("Export Error", "The Python virtual environment executable was not found. Please ensure the 'venv' directory is set up correctly.", "OK");
  42. return;
  43. }
  44. if (!File.Exists(fullScriptPath))
  45. {
  46. UnityEngine.Debug.LogError($"Extractor script not found at: {fullScriptPath}");
  47. EditorUtility.DisplayDialog("Export Error", $"The Python script for the {level} extractor was not found.", "OK");
  48. return;
  49. }
  50. var arguments = $"\"{fullScriptPath}\" --input \"{projectRoot}\" --output \"{outputPath}\"";
  51. UnityEngine.Debug.Log($"Running command: \"{pythonExecutable}\" {arguments}");
  52. var process = new Process
  53. {
  54. StartInfo = new ProcessStartInfo
  55. {
  56. FileName = pythonExecutable,
  57. Arguments = arguments,
  58. RedirectStandardOutput = true,
  59. RedirectStandardError = true,
  60. UseShellExecute = false,
  61. CreateNoWindow = true,
  62. WorkingDirectory = projectRoot
  63. }
  64. };
  65. EditorUtility.DisplayProgressBar("Data Extractor", $"Running {level} export...", 0.5f);
  66. process.OutputDataReceived += (_, e) => { if (e.Data != null) UnityEngine.Debug.Log($"[Extractor] {e.Data}"); };
  67. process.ErrorDataReceived += (_, e) => { if (e.Data != null) UnityEngine.Debug.LogError($"[Extractor ERROR] {e.Data}"); };
  68. process.Start();
  69. process.BeginOutputReadLine();
  70. process.BeginErrorReadLine();
  71. process.WaitForExit();
  72. EditorUtility.ClearProgressBar();
  73. if (process.ExitCode == 0)
  74. {
  75. EditorUtility.DisplayDialog("Export Complete", $"{level} data export finished successfully.", "OK");
  76. }
  77. else
  78. {
  79. EditorUtility.DisplayDialog("Export Failed", $"The {level} data export failed. Check the Unity Console for error messages.", "OK");
  80. }
  81. }
  82. }
  83. }