DataExtractorMenu.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 indentChoice = EditorUtility.DisplayDialogComplex(
  36. $"{level} Export Options",
  37. "Choose the JSON output format.",
  38. "Compact (No Indent)", // button 0
  39. "Indented (Readable)", // button 1
  40. "Cancel" // button 2
  41. );
  42. string indentArgument;
  43. switch (indentChoice)
  44. {
  45. case 0: // Compact
  46. indentArgument = ""; // Default is compact
  47. break;
  48. case 1: // Indented
  49. indentArgument = " --indent 2";
  50. break;
  51. default: // Cancel or closed dialog
  52. UnityEngine.Debug.Log($"{level} export cancelled by user.");
  53. return;
  54. }
  55. var projectRoot = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
  56. string pythonExecutable;
  57. if (Application.platform == RuntimePlatform.WindowsEditor)
  58. {
  59. pythonExecutable = Path.Combine(projectRoot, "venv", "Scripts", "python.exe");
  60. }
  61. else
  62. {
  63. pythonExecutable = Path.Combine(projectRoot, "venv", "bin", "python");
  64. }
  65. var fullScriptPath = Path.Combine(projectRoot, scriptPath);
  66. if (!File.Exists(pythonExecutable))
  67. {
  68. UnityEngine.Debug.LogError($"Python executable not found at: {pythonExecutable}");
  69. EditorUtility.DisplayDialog("Export Error", "The Python virtual environment executable was not found. Please ensure the 'venv' directory is set up correctly.", "OK");
  70. return;
  71. }
  72. if (!File.Exists(fullScriptPath))
  73. {
  74. UnityEngine.Debug.LogError($"Extractor script not found at: {fullScriptPath}");
  75. EditorUtility.DisplayDialog("Export Error", $"The Python script for the {level} extractor was not found.", "OK");
  76. return;
  77. }
  78. var arguments = $"\"{fullScriptPath}\" --input \"{projectRoot}\" --output \"{outputPath}\"{indentArgument}";
  79. UnityEngine.Debug.Log($"Running command: \"{pythonExecutable}\" {arguments}");
  80. var process = new Process
  81. {
  82. StartInfo = new ProcessStartInfo
  83. {
  84. FileName = pythonExecutable,
  85. Arguments = arguments,
  86. RedirectStandardOutput = true,
  87. RedirectStandardError = true,
  88. UseShellExecute = false,
  89. CreateNoWindow = true,
  90. WorkingDirectory = projectRoot
  91. }
  92. };
  93. EditorUtility.DisplayProgressBar("Data Extractor", $"Running {level} export...", 0.5f);
  94. process.OutputDataReceived += (_, e) => { if (e.Data != null) UnityEngine.Debug.Log($"[Extractor] {e.Data}"); };
  95. process.ErrorDataReceived += (_, e) => { if (e.Data != null) UnityEngine.Debug.LogError($"[Extractor ERROR] {e.Data}"); };
  96. process.Start();
  97. process.BeginOutputReadLine();
  98. process.BeginErrorReadLine();
  99. process.WaitForExit();
  100. EditorUtility.ClearProgressBar();
  101. if (process.ExitCode == 0)
  102. {
  103. EditorUtility.DisplayDialog("Export Complete", $"{level} data export finished successfully.", "OK");
  104. }
  105. else
  106. {
  107. EditorUtility.DisplayDialog("Export Failed", $"The {level} data export failed. Check the Unity Console for error messages.", "OK");
  108. }
  109. }
  110. }
  111. }