ict.lixian.single/Assets/Scripts/cxx/UI/IDEsettingPanel.cs

438 lines
17 KiB
C#

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;
/// <summary>
/// 打开工程所在位置
/// </summary>
public Button openProjectPath;
/// <summary>
/// 提示信息
/// </summary>
public Text tipText;
private Process currentIDE;
/// <summary>
/// 正在加载中
/// </summary>
private bool isLoadBtn;
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","未设置");
javaPath.text = PlayerPrefs.GetString("javaPath", "未设置");
cPath.text = PlayerPrefs.GetString("cPath", "未设置");
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 && !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 && !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 && !string.IsNullOrEmpty(path[0]))
{
PlayerPrefs.SetString("cPath", path[0]);
UnityEngine.Debug.Log(path[0]);
cPath.text= path[0];
}
});
runPython.onClick.AddListener(() =>
{
//打开python编辑器
if (pythonPath.text != "未设置")
{
if(File.Exists(pythonPath.text))
{
if (currentIDE == null || currentIDE.HasExited)
{
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已运行", "log");
}
}
else
{
ConsolePanel.ConsoleOutput("请检查PythonIDE路径是否正确", "error");
}
}
else
{
//提示
ConsolePanel.ConsoleOutput("未配置PythonIDE路径", "error");
}
});
runJava.onClick.AddListener(() =>
{
//打开java编辑器
if (javaPath.text != "未设置")
{
if (File.Exists(javaPath.text))
{
if (currentIDE == null || currentIDE.HasExited)
{
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已运行", "log");
}
}
else
{
ConsolePanel.ConsoleOutput("请检查JavaIDE路径是否正确", "error");
}
}
else
{
//提示
ConsolePanel.ConsoleOutput("未配置JavaIDE路径", "error");
}
});
runC.onClick.AddListener(() =>
{
//打开C编辑器
if (cPath.text != "未设置")
{
if (File.Exists(cPath.text))
{
if (currentIDE == null || currentIDE.HasExited)
{
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已运行", "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 = "工程为<color=red>TmpCode</color>文件夹";
}
else if (GameManager.current_main_menu_type == MainMenuType.)
{
tipText.text = "工程为目录下<color=red>Python、Java、C</color>文件夹";
}
}
private void OnEnable()
{
transform.Find("mask_back_ground/main_panel_image/大说明图").gameObject.SetActive(false);
}
/// <summary>
/// 下载当前目录
/// </summary>
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);
if (jb1["code"].ToObject<int>() == 200)
{
if (jb1["data"] != null)
{
//有则下载暂存工程
CallForTest.instance.saveInfo = JsonConvert.DeserializeObject<MyTmpSaveInfo>(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.)
{
//工程目录初始化
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);
}
}
/// <summary>
/// 清空文件夹
/// </summary>
/// <param name="path"></param>
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);
});
}
}
/// <summary>
/// 下载工程包,并解压到临时工程文件夹
/// </summary>
/// <param name="packageUrl"></param>
/// <param name="packageName"></param>
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("开始解压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
{
UnityEngine.Debug.Log("下载失败:" + packageUrl);
MainCanvasManager.confirm_panel.OnPopup("下载失败" + packageUrl, "");
}
}));
}
}