using Crosstales.UI; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using RenderHeads.Media.AVProMovieCapture; using SFB; using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using TMPro; using UnityEngine; using UnityEngine.UI; public class IDEsettingPanel : MonoBehaviour { public Button closeBtn; public TextMeshProUGUI pythonPath; public TextMeshProUGUI javaPath; public TextMeshProUGUI cPath; public Button chosePythonBtn; public Button choseJavaBtn; public Button choseCBtn; public Button runPython; public Button runJava; public Button runC; /// /// 打开工程所在位置 /// public Button openProjectPath; /// /// 提示信息 /// public Text tipText; private Process currentIDE; /// /// 正在加载中 /// private bool isLoadBtn; /// /// 工程目录按钮 /// public Button 工程目录按钮; /// /// IDE设置面板 /// public GameObject IDE设置面板; public void Init() { //初始化暂存信息 CallForTest.instance.saveInfo = null; try { //清空临时工程目录 ClearDirectory(CallForTest.instance.pathProject); //清空缓存目录 ClearDirectory(CallForTest.instance.pathCache); //清空提交文件夹 ClearDirectory(CallForTest.instance.pathTaskFile); } catch(Exception e) { UnityEngine.Debug.LogError("清空文件夹失败,自动退出程序================"+e.Message); Application.Quit(); } pythonPath.text = PlayerPrefs.GetString("pythonPath","选择已安装IDE所在文件夹的路径"); UnityEngine.Debug.Log(pythonPath.text); javaPath.text = PlayerPrefs.GetString("javaPath", "选择已安装IDE所在文件夹的路径"); UnityEngine.Debug.Log(javaPath.text); cPath.text = PlayerPrefs.GetString("cPath", "选择已安装IDE所在文件夹的路径"); UnityEngine.Debug.Log(cPath.text); //获取项目目录 //CallForTest.instance.pathFreeProjectRoot = PlayerPrefs.GetString("programPath", "D:/UserFiles/ProjectCode/FreeModeTmpCode"); if (GameManager.current_main_menu_type.ToString().Equals("自由编程")) { //runPython.gameObject.SetActive(true); //runJava.gameObject.SetActive(true); //runC.gameObject.SetActive(true); } closeBtn.onClick.AddListener(() => { gameObject.SetActive(false); }); chosePythonBtn.onClick.AddListener(() => { var path = StandaloneFileBrowser.OpenFilePanel("请选择Python IDE", "", "exe", false); if (path!=null &&path.Length!=0&& !string.IsNullOrEmpty(path[0])) { PlayerPrefs.SetString("pythonPath", path[0]); UnityEngine.Debug.Log(path[0]); pythonPath.text = path[0]; } }); choseJavaBtn.onClick.AddListener(() => { var path = StandaloneFileBrowser.OpenFilePanel("请选择Java IDE", "", "exe", false); if (path != null && path.Length != 0 && !string.IsNullOrEmpty(path[0])) { PlayerPrefs.SetString("javaPath", path[0]); UnityEngine.Debug.Log(path[0]); javaPath.text = path[0]; } }); choseCBtn.onClick.AddListener(() => { var path = StandaloneFileBrowser.OpenFilePanel("请选择C IDE", "", "exe", false); if (path != null && path.Length != 0 && !string.IsNullOrEmpty(path[0])) { PlayerPrefs.SetString("cPath", path[0]); UnityEngine.Debug.Log(path[0]); cPath.text= path[0]; } }); 工程目录按钮.onClick.AddListener(() => { var path = StandaloneFileBrowser.OpenFilePanel("请选择工程目录", "", "", false); if (path != null && path.Length != 0 && !string.IsNullOrEmpty(path[0])) { PlayerPrefs.SetString("programPath", path[0]); UnityEngine.Debug.Log(path[0]); CallForTest.instance.pathFreeProjectRoot = path[0]; } }); runPython.onClick.AddListener(() => { GameObject.Find("整体背景").GetComponent().IDE中开始按钮Function(); //之后可能还得改因为UI得目录可能会变 // IDE设置面板.SetActive(false); RunPython(); }); runJava.onClick.AddListener(() => { GameObject.Find("整体背景").GetComponent().IDE中开始按钮Function(); // IDE设置面板.SetActive(false); RunJava(); }); runC.onClick.AddListener(() => { GameObject.Find("整体背景").GetComponent().IDE中开始按钮Function(); // IDE设置面板.SetActive(false); RunC(); }); } public void RunPython() { UnityEngine.Debug.Log("runPython"); //打开python编辑器 if (pythonPath.text != "选择已安装IDE所在文件夹的路径") { if (File.Exists(pythonPath.text)) { if (currentIDE != null && !currentIDE.HasExited) { ConsolePanel.ConsoleOutput("PythonIDE已运行", "log"); } else { UnityEngine.Debug.Log("startRunPython"); currentIDE = new Process(); if (GameManager.current_main_menu_type == MainMenuType.课程任务 || GameManager.current_main_menu_type == MainMenuType.案例中心) { currentIDE.StartInfo = new ProcessStartInfo(pythonPath.text); currentIDE.StartInfo.UseShellExecute = false; currentIDE.StartInfo.Arguments = CallForTest.instance.pathProject; } else if (GameManager.current_main_menu_type == MainMenuType.自由编程) { currentIDE.StartInfo = new ProcessStartInfo(pythonPath.text); currentIDE.StartInfo.UseShellExecute = false; currentIDE.StartInfo.Arguments = CallForTest.instance.pathFreeProjectRoot + "/Python"; } if (!currentIDE.Start()) { UnityEngine.Debug.Log("运行失败"); ConsolePanel.ConsoleOutput("PythonIDE运行失败", "error"); } else { UnityEngine.Debug.Log("运行成功"); ConsolePanel.ConsoleOutput("正在打开编辑器,请稍等......", "log"); } } } else { ConsolePanel.ConsoleOutput("请检查PythonIDE路径是否正确", "error"); } } else { //提示 ConsolePanel.ConsoleOutput("未配置PythonIDE路径", "error"); } } public void RunJava() { UnityEngine.Debug.Log("runJava"); //打开java编辑器 if (javaPath.text != "选择已安装IDE所在文件夹的路径") { if (File.Exists(javaPath.text)) { if (currentIDE != null && !currentIDE.HasExited) { ConsolePanel.ConsoleOutput("JavaIDE已运行", "log"); } else { UnityEngine.Debug.Log("startRunJava"); currentIDE = new Process(); if (GameManager.current_main_menu_type == MainMenuType.课程任务 || GameManager.current_main_menu_type == MainMenuType.案例中心) { currentIDE.StartInfo = new ProcessStartInfo(javaPath.text, CallForTest.instance.pathProject); } else if (GameManager.current_main_menu_type == MainMenuType.自由编程) { currentIDE.StartInfo = new ProcessStartInfo(javaPath.text, CallForTest.instance.pathFreeProjectRoot + "/Java"); } if (!currentIDE.Start()) { UnityEngine.Debug.Log("运行失败"); ConsolePanel.ConsoleOutput("JavaIDE运行失败", "error"); } else { UnityEngine.Debug.Log("运行成功"); ConsolePanel.ConsoleOutput("正在打开编辑器,请稍等......", "log"); } } } else { ConsolePanel.ConsoleOutput("请检查JavaIDE路径是否正确", "error"); } } else { //提示 ConsolePanel.ConsoleOutput("未配置JavaIDE路径", "error"); } } public void RunC() { UnityEngine.Debug.Log("runC"); //打开C编辑器 if (cPath.text != "选择已安装IDE所在文件夹的路径") { if (File.Exists(cPath.text)) { if (currentIDE != null && !currentIDE.HasExited) { ConsolePanel.ConsoleOutput("C IDE已运行", "log"); } else { UnityEngine.Debug.Log("startRunC"); currentIDE = new Process(); if (GameManager.current_main_menu_type == MainMenuType.课程任务 || GameManager.current_main_menu_type == MainMenuType.案例中心) { currentIDE.StartInfo = new ProcessStartInfo(cPath.text, CallForTest.instance.pathProject); } else if (GameManager.current_main_menu_type == MainMenuType.自由编程) { currentIDE.StartInfo = new ProcessStartInfo(cPath.text, CallForTest.instance.pathFreeProjectRoot + "/C"); } if (!currentIDE.Start()) { UnityEngine.Debug.Log("运行失败"); ConsolePanel.ConsoleOutput("C语言IDE运行失败", "error"); } else { UnityEngine.Debug.Log("运行成功"); ConsolePanel.ConsoleOutput("正在打开编辑器,请稍等......", "log"); } } } else { ConsolePanel.ConsoleOutput("请检查C语言IDE路径是否正确", "error"); } } else { //提示 ConsolePanel.ConsoleOutput("未配置C语言IDE路径", "error"); } } private void Start() { //未下载,则下载代码 if (!isLoadBtn && Directory.GetFiles(CallForTest.instance.pathProject).Length == 0) { DownLoadProject(false); } //打开工程所在路径 openProjectPath.onClick.AddListener(() => { if (GameManager.current_main_menu_type == MainMenuType.课程任务 || GameManager.current_main_menu_type == MainMenuType.案例中心) { Utils.ShowInExplorer(Directory.GetParent(CallForTest.instance.pathProject).FullName); } else if(GameManager.current_main_menu_type== MainMenuType.自由编程) { Utils.ShowInExplorer(CallForTest.instance.pathFreeProjectRoot); } }); if (GameManager.current_main_menu_type == MainMenuType.课程任务 || GameManager.current_main_menu_type == MainMenuType.案例中心) { tipText.text = "工程为TmpCode文件夹"; } else if (GameManager.current_main_menu_type == MainMenuType.自由编程) { tipText.text = "工程为目录下Python、Java、C文件夹"; } } private void OnEnable() { transform.Find("mask_back_ground/main_panel_image/大说明图").gameObject.SetActive(false); } /// /// 下载当前目录 /// public void DownLoadProject(bool isLodingbtn) { isLoadBtn = isLodingbtn; //gameObject.SetActive(true); //先清空临时代码目录 if (GameManager.current_main_menu_type == MainMenuType.课程任务) { if (CallForTest.instance != null && CallForTest.instance.currentTaskData != null && CallForTest.instance.currentTaskData.programDto != null && !string.IsNullOrEmpty(CallForTest.instance.currentTaskData.programDto.programPackageFileUrl)) { //查看是否有暂存 string url1 = InterfaceManager.IdAddress + ":8080/component/userCodeingInfo/getDetail?taskId=" + CallForTest.instance.currentTaskData.task_id + "&userId=" + CallForTest.instance.user.userId + "&dataType=TmpCode.zip"; ConsolePanel.ConsoleOutput("正在检测是否有暂存工程", "log"); StartCoroutine(InterfaceManager.Get(url1, (isok, result) => { if (isok) { JObject jb1 = JObject.Parse(result); UnityEngine.Debug.Log(jb1); if (jb1["code"].ToObject() == 200) { //UnityEngine.Debug.Log(jb1["data"].ToObject()); if (jb1["data"]/*.ToObject()*/ != null) { //有则下载暂存工程 CallForTest.instance.saveInfo = JsonConvert.DeserializeObject(jb1["data"].ToString()); string zipurl = CallForTest.instance.saveInfo.dataContent; if (!string.IsNullOrEmpty(zipurl)) { ConsolePanel.ConsoleOutput("开始下载暂存工程", "log"); LoadPakageAndUnpackZip(zipurl, CallForTest.instance.saveInfo.dataType); } } else { ConsolePanel.ConsoleOutput("开始下载初始代码包0%", "log"); //没有则下载程序包 LoadPakageAndUnpackZip(CallForTest.instance.currentTaskData.programDto.programPackageFileUrl, CallForTest.instance.currentTaskData.programDto.programPackageFileName); } } else { MainCanvasManager.confirm_panel.OnPopup("获取暂存数据失败", jb1["msg"].ToString()); } } else { MainCanvasManager.confirm_panel.OnPopup("获取暂存数据失败", result); } })); } else { ConsolePanel.ConsoleOutput("未找到代码文件", "error"); } } else if(GameManager.current_main_menu_type == MainMenuType.案例中心) { if (CallForTest.instance != null && CallForTest.instance.currentTaskData != null && CallForTest.instance.currentTaskData.programDto != null && !string.IsNullOrEmpty(CallForTest.instance.currentTaskData.programDto.programPackageFileUrl)) { //直接下载程序包 ConsolePanel.ConsoleOutput("开始下载初始代码包0%", "log"); LoadPakageAndUnpackZip(CallForTest.instance.currentTaskData.programDto.programPackageFileUrl, CallForTest.instance.currentTaskData.programDto.programPackageFileName); } else { ConsolePanel.ConsoleOutput("未找到代码文件", "error"); } } else if (GameManager.current_main_menu_type == MainMenuType.自由编程) { //工程目录初始化 //UnityEngine.Debug.LogError(CallForTest.instance.pathFreeProjectRoot+"hahaha"); if (Directory.GetFiles(CallForTest.instance.pathFreeProjectRoot + "/Python").Length == 0) File.Copy(CallForTest.instance.pathRoot+ "/main.py", CallForTest.instance.pathFreeProjectRoot + "/Python/main.py", true); //File.Copy(CallForTest.instance.pathRoot + "/main.py", CallForTest.instance.pathFreeProjectRoot + "/Python/main.py", true); //File.Copy(CallForTest.instance.pathRoot + "/main.py", CallForTest.instance.pathFreeProjectRoot + "/Python/main.py", true); //启用按钮 //runPython.gameObject.SetActive(true); //runJava.gameObject.SetActive(true); //runC.gameObject.SetActive(true); } } /// /// 清空文件夹 /// /// private void ClearDirectory(string path) { if (Directory.Exists(path)) { Directory.GetFiles(path).ToList().ForEach(a => { File.Delete(a); }); Directory.GetDirectories(path).ToList().ForEach(a => { Directory.Delete(a,true); }); } } /// /// 下载工程包,并解压到临时工程文件夹 /// /// /// private void LoadPakageAndUnpackZip(string packageUrl,string packageName) { StartCoroutine(InterfaceManager.GetData(packageUrl, ConsolePanel.ConsoleOutput("0%", "log"), (isok2, data) => { ConsolePanel.ConsoleOutput("下载完成100%", "log"); UnityEngine.Debug.Log("下载完成:" + packageUrl); if (isok2 && data != null) { string zipname = CallForTest.instance.pathCache + "/" + packageName; File.WriteAllBytes(zipname, data); //清空临时工程目录 ClearDirectory(CallForTest.instance.pathProject); ConsolePanel.ConsoleOutput("下载完成", "log"); ConsolePanel.ConsoleOutput("开始解压0%", "log"); System.IO.Compression.ZipFile.ExtractToDirectory(zipname, CallForTest.instance.pathProject, Encoding.GetEncoding("GBK"), true); //解压zip到目标目录 //Zip zip = new Zip(); //Encoding gbk = Encoding.GetEncoding("GBK"); //ZipStrings.CodePage = gbk.CodePage; //zip.Extract(zipname, pathProject + "/", 2048); ConsolePanel.ConsoleOutput("解压完成100%", "log"); //启用按钮 //runPython.gameObject.SetActive(CallForTest.instance.currentTaskData.programDto.programLanguage == "Python"); //runJava.gameObject.SetActive(CallForTest.instance.currentTaskData.programDto.programLanguage == "Java"); //runC.gameObject.SetActive(CallForTest.instance.currentTaskData.programDto.programLanguage == "C"); } else { ConsolePanel.ConsoleOutput("下载失败:" + packageUrl, "log"); UnityEngine.Debug.Log("下载失败:" + packageUrl); //MainCanvasManager.confirm_panel.OnPopup("下载失败" + packageUrl, ""); } })); } private void OnApplicationQuit() { if (currentIDE != null && !currentIDE.HasExited) { try { currentIDE.Kill(); // 结束进程 UnityEngine.Debug.Log("Process terminated."); } catch (System.Exception ex) { UnityEngine.Debug.LogError("Error terminating process: " + ex.Message); } } else { UnityEngine.Debug.Log("No process is running."); } } public void kill() { UnityEngine.Debug.Log("按下了I键"); //ConsolePanel.ConsoleOutput("按下了I键", "log"); if (currentIDE != null && !currentIDE.HasExited) { try { currentIDE.Kill(); // 结束进程 //ConsolePanel.ConsoleOutput("Process terminated.", "log"); } catch (System.Exception ex) { //ConsolePanel.ConsoleOutput("Error terminating process: " + ex.Message, "log"); } } else { //ConsolePanel.ConsoleOutput("No process is running.", "log"); } } }