using ICSharpCode.SharpZipLib.Zip;
using Kirurobo;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RenderHeads.Media.AVProMovieCapture;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using VRS.Util;
using static MainCanvasManager;
public class MenuPanel : PanelBasic
{
///
/// 视频录制按钮
///
public Button video_record_button;
///
/// 查看媒体按钮
///
public Button view_media_button;
///
/// 导入媒体按钮
///
public Button import_media_button;
///
/// 重置场景按钮
///
public Button reset_scene_button;
///
/// 保存形象按钮
///
public Button save_image_button;
///
/// 提交按钮
///
public Button submit_button;
///
/// 暂存按钮
///
public Button staging_button;
///
/// 下载按钮
///
public Button download_button;
///
/// 文件路径
///
public string file_path;
public TextMeshProUGUI video_record_text;
///
/// 屏幕录制组件
///
public CaptureFromScreen capture;
///
/// 是否正在录制
///
public bool isCapture;
public GameObject 录制标志obj;
protected override void Awake()
{
base.Awake();
}
private void Start()
{
if (GameManager.current_main_menu_type == MainMenuType.自由编程 || GameManager.current_main_menu_type == MainMenuType.案例中心)
{
submit_button.gameObject.SetActive(false);
staging_button.gameObject.SetActive(false);
download_button.gameObject.SetActive(false);
}
video_record_button.onClick.AddListener(() => OnVideoRecord());
import_media_button.onClick.AddListener(() => OnImportMedia());
view_media_button.onClick.AddListener(() => OnMediaViwer());
save_image_button.onClick.AddListener(() => OnSaveImage());
submit_button.onClick.AddListener(() =>
{
MainCanvasManager.confirm_panel.OnPopup("是否提交当前任务?", "", _callback: isok =>
{
if (isok)
{
OnSubmit();
}
});
});
reset_scene_button.onClick.AddListener(() => OnResetScene());
staging_button.onClick.AddListener(() =>
{
//判断是否有文件
if (Directory.GetFiles(CallForTest.instance.pathProject).Length > 0 || Directory.GetDirectories(CallForTest.instance.pathProject).Length > 0 )
{
MainCanvasManager.confirm_panel.OnPopup("是否暂存当前工作目录?", "", _callback: isok =>
{
if (isok)
{
OnStaging("暂存");
}
});
}
else
{
MainCanvasManager.confirm_panel.OnPopup("未打开代码工程,无需暂存", "");
}
});
download_button.onClick.AddListener(() =>
{
MainCanvasManager.confirm_panel.OnPopup("此操作将会覆盖当前工作目录文件,是否要下载上次暂存的代码?", "", _callback: isok =>
{
if (isok)
{
OnDownload();
}
});
});
}
public void OnVideoRecord()
{
if (!isCapture)
{
StartVideoRecord();
}
else
{
StopVideoRecord();
}
}
///
/// 开始录制
///
public void StartVideoRecord()
{
if (capture != null)
{
capture.OutputFolderPath = CallForTest.instance.pathTaskFile;
capture.FilenamePrefix = "任务";
capture.AppendFilenameTimestamp = false;
//提示是否开始录制
MainCanvasManager.confirm_panel.OnPopup("录制确认", "是否开始录屏?", "提示", result =>
{
if (result)
{
isCapture = true;
capture.StartCapture();
Debug.Log("开始录制");
video_record_text.text = "结束录制";
video_record_text.color = Color.red;
录制标志obj.SetActive(true);
}
});
}
}
///
/// 结束录制
///
public void StopVideoRecord()
{
if (capture != null)
{
isCapture = false;
capture.StopCapture();
Debug.Log("停止录制");
video_record_text.text = "开始录制";
video_record_text.color = Color.white;
录制标志obj.SetActive(false);
//提示录制结束,打开所在文件路径
MainCanvasManager.confirm_panel.OnPopup("录制完成", "录制完成,是否打开文件路径?", "提示", result =>
{
if (result)
{
Utils.ShowInExplorer(CaptureBase.LastFileSaved);
}
});
}
}
///
/// 导入媒体
///
public void OnImportMedia()
{
//Path = base.OpenProject();
//OpenSingleFile();
//Debug.Log(file_path);
var _res = OpenFile.OpenFileWin();
if (_res != null)
{
//是否有新增
bool _add_new = false;
//是否仅有音频
bool _only_audio;
//文件类型
string _type;
if (_res.Length == 1)
{
_type = _res[0].Split('.')[^1];
//_type = _res[0].Split('.')[_res[0].Split('.').Length - 1];
_add_new |= media_viwer_panel.Init(_type, _res[0].Split('\\')[^1], _res[0]);
//_add_new |= media_viwer_panel.Init(_type, _res[0].Split('\\')[_res[0].Split('\\').Length - 1], _res[0]);
_only_audio = _type == "mp3";
}
else
{
_only_audio = true;
var _path = _res[0];
for (int i = 1; i < _res.Length; i++)
{
_type = _res[i].Split('.')[1];
_add_new |= media_viwer_panel.Init(_res[i].Split('.')[1], _res[i], _path + "\\" + _res[i]);
_only_audio &= _type == "mp3";
}
}
if (_add_new)
{
//如果有新增,则打开媒体查看面板
media_viwer_panel.OnPopup();
//切换到对应标签,若都有,则显示视频,只有音频,显示音频
//todo
if (_only_audio)
{
media_viwer_panel.audio_files_button.isOn = true;
media_viwer_panel.OnVideoFiles(false);
media_viwer_panel.OnAudioFiles(true);
}
else
{
media_viwer_panel.video_files_button.isOn = true;
media_viwer_panel.OnVideoFiles(true);
media_viwer_panel.OnAudioFiles(false);
}
}
}
else
{
}
}
///
/// 查看媒体资源
///
public void OnMediaViwer()
{
media_viwer_panel.OnPopup();
}
public void OnSaveImage()
{
//Instantiate(OneImage, Canvas);
}
///
/// 提交按钮
///
public void OnSubmit()
{
ConsolePanel.ConsoleOutput("开始提交任务数据", "log");
//任务分数回传
Task_Setp_Back.instance.TaskInfoDataBack(task_panel.task_item_data, ScoreManager.Instance.total_score, (result, msg) =>
{
if (result)
{
ConsolePanel.ConsoleOutput("提交任务数据完成", "log");
if (File.Exists(CallForTest.instance.pathTaskFile + "/任务.png"))
{
//有截图,回传任务截图
SubmitJT();
}
else if(File.Exists(CallForTest.instance.pathTaskFile + "/任务.mp4"))
{
//没截图,回传任务录屏
ConsolePanel.ConsoleOutput("无任务截图提交", "log");
SubmitLP();
}
else
{
//也没录屏,回传步骤截图
ConsolePanel.ConsoleOutput("无任务截图提交", "log");
ConsolePanel.ConsoleOutput("无任务录屏提交", "log");
SubmitStepJT();
}
}
else
{
ConsolePanel.ConsoleOutput("提交任务分数失败", "error");
confirm_panel.OnPopup("提交任务分数失败", msg);
}
});
}
///
/// 提交任务截图
///
private void SubmitJT()
{
ConsolePanel.ConsoleOutput("开始提交任务截图", "log");
Task_Setp_Back.instance.TaskPictureBack(task_panel.task_item_data, File.ReadAllBytes(CallForTest.instance.pathTaskFile + "/任务.png"), ConsolePanel.ConsoleOutput("0%", "log"), (isok1, msg1) =>
{
if (isok1)
{
ConsolePanel.ConsoleOutput("提交任务截图完成", "log");
if (File.Exists(CallForTest.instance.pathTaskFile + "/任务.mp4"))
{
//有录屏,回传录屏
SubmitLP();
}
else
{
//没录屏,回传步骤截图
ConsolePanel.ConsoleOutput("无任务录屏提交", "log");
SubmitStepJT();
}
}
else
{
ConsolePanel.ConsoleOutput("提交任务截图失败", "error");
confirm_panel.OnPopup("提交任务截图失败", msg1);
}
});
}
///
/// 提交任务录屏
///
private void SubmitLP()
{
ConsolePanel.ConsoleOutput("开始提交任务录屏", "log");
//上传minio
UpLoadFileToMinio(CallForTest.instance.pathTaskFile + "/任务.mp4", (isok0, downPath) =>
{
if (isok0)
{
ConsolePanel.ConsoleOutput("提交任务录屏完成", "log");
ConsolePanel.ConsoleOutput("开始同步云端数据", "log");
//提交数据
Task_Setp_Back.instance.TaskVideoBack(task_panel.task_item_data, downPath, (isok1, msg1) =>
{
if (isok1)
{
ConsolePanel.ConsoleOutput("同步云端数据完成", "log");
//回传步骤截图
SubmitStepJT();
}
else
{
ConsolePanel.ConsoleOutput("同步云端数据失败", "error");
confirm_panel.OnPopup("同步云端数据失败", msg1);
}
});
}
else
{
ConsolePanel.ConsoleOutput("提交任务录屏失败", "error");
confirm_panel.OnPopup("提交任务录屏失败", "");
}
});
}
///
/// 提交步骤截图
///
private void SubmitStepJT()
{
List files= new List();
Directory.GetFiles(CallForTest.instance.pathTaskFile).ToList().ForEach(a =>
{
string filename= Path.GetFileName(a);
if(filename.StartsWith("步骤_"))
{
files.Add(filename);
}
});
if(files.Count>0)
{
ConsolePanel.ConsoleOutput("开始提交步骤截图", "log");
SubmOneStepJT(files, 0);
}
else
{
ConsolePanel.ConsoleOutput("无步骤截图提交", "log");
//暂存文件
TijiaoFile();
}
}
private void TijiaoFile()
{
//判断是否有文件
if (Directory.GetFiles(CallForTest.instance.pathProject).Length > 0 || Directory.GetDirectories(CallForTest.instance.pathProject).Length>0)
{
ConsolePanel.ConsoleOutput("开始提交代码工程", "log");
OnStaging("提交");
}
else
{
ConsolePanel.ConsoleOutput("无代码工程可提交", "error");
confirm_panel.OnPopup("提交完成", "可以退出房间了");
}
}
///
/// 上传单个步骤截图
///
private void SubmOneStepJT(List files, int index)
{
if (index < files.Count)
{
string tmpFileName = files[index];
TaskStep step = task_panel.task_item_data.task_steplist.Find(a => "步骤_" + a.stepName + ".png" == tmpFileName);
if (step != null)
{
ConsolePanel.ConsoleOutput("开始提交步骤截图:"+tmpFileName, "log");
Task_Setp_Back.instance.StepPictureBack(step, tmpFileName, File.ReadAllBytes(CallForTest.instance.pathTaskFile +"/"+ tmpFileName), ConsolePanel.ConsoleOutput("0%", "log"), (isok, msg) =>
{
if (isok)
{
ConsolePanel.ConsoleOutput("提交成功:" + tmpFileName, "log");
//提交下一个
SubmOneStepJT(files, index+1);
}
else
{
ConsolePanel.ConsoleOutput("提交步骤截图失败" + tmpFileName, "error");
confirm_panel.OnPopup("提交步骤截图失败" + tmpFileName, msg);
}
});
}
else
{
//找不到此步骤,提交下一个
Debug.Log("找不到图片对应的步骤");
SubmOneStepJT(files, index+1);
}
}
else
{
Debug.Log("结束步骤截图提交");
ConsolePanel.ConsoleOutput("步骤截图全部提交完成", "log");
//提交文件
TijiaoFile();
}
}
///
/// 重置场景
///
public void OnResetScene()
{
on_reset_scene?.Invoke();
}
///
/// 暂存
///
public void OnStaging(string actionMsg)
{
//zip压缩包
string outpath = CallForTest.instance.pathCache+ "/TmpCode.zip";
string inpath = CallForTest.instance.pathProject;
if (File.Exists(outpath))
{
File.Delete(outpath);
}
ConsolePanel.ConsoleOutput("开始压缩0%", "log");
System.IO.Compression.ZipFile.CreateFromDirectory(inpath, outpath);
//Zip zip = new Zip();
//Encoding gbk = Encoding.GetEncoding("GBK");
//ZipStrings.CodePage = gbk.CodePage;
//if (zip.ZipFileDictory(inpath, outpath))
{
ConsolePanel.ConsoleOutput("压缩完成100%", "log");
Debug.Log("压缩完成100%");
UpLoadFileToMinio(outpath, (isok0, downPath) =>
{
if (isok0)
{
//更新数据库
string url2 = InterfaceManager.IdAddress + ":8080/component/userCodeingInfo";
ConsolePanel.ConsoleOutput("开始更新云端数据", "log");
if (CallForTest.instance.saveInfo == null)
{
//没有暂存过
MyTmpSaveInfo saveInfo = new MyTmpSaveInfo();
saveInfo.taskId = CallForTest.instance.currentTaskData.task_id;
saveInfo.userId = CallForTest.instance.user.userId;
saveInfo.dataType = "TmpCode.zip";
saveInfo.dataContent = downPath;
StartCoroutine(Task_Setp_Back.Post(url2, JsonConvert.SerializeObject(saveInfo), null,(isok1, result1) =>
{
if (isok1)
{
JObject jo1 = JObject.Parse(result1);
if (jo1["code"].ToObject() == 200)
{
CallForTest.instance.saveInfo = saveInfo;
confirm_panel.OnPopup(actionMsg + "成功", actionMsg=="提交"? "可以退出房间了":"");
ConsolePanel.ConsoleOutput(actionMsg + "文件成功", "log");
}
else
{
confirm_panel.OnPopup(actionMsg+"失败", jo1["msg"].ToString());
}
}
else
{
confirm_panel.OnPopup(actionMsg+ "失败", result1);
}
}));
}
else
{
//有暂存记录
MyTmpSaveInfo saveInfo = new MyTmpSaveInfo();
saveInfo.id = CallForTest.instance.saveInfo.id;
saveInfo.taskId = CallForTest.instance.saveInfo.taskId;
saveInfo.userId = CallForTest.instance.saveInfo.userId;
saveInfo.dataType = CallForTest.instance.saveInfo.dataType;
saveInfo.dataContent = downPath;
StartCoroutine(Task_Setp_Back.Post(url2, JsonConvert.SerializeObject(saveInfo), null,(isok1, result1) =>
{
if (isok1)
{
CallForTest.instance.saveInfo.dataContent = downPath;
confirm_panel.OnPopup(actionMsg+"成功", actionMsg == "提交" ? "可以退出房间了" : "");
ConsolePanel.ConsoleOutput(actionMsg+"文件成功", "log");
}
else
{
confirm_panel.OnPopup(actionMsg+"失败", result1);
}
}));
}
}
});
}
}
///
/// 上传文件到minio地址
///
private void UpLoadFileToMinio(string filePath,Action callback)
{
//md5值
FileStream file = new FileStream(filePath, FileMode.Open);
long length = file.Length;
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
string md5value = sb.ToString();
Debug.Log("上传文件:" + Path.GetFileName(filePath) + " MD5:" + md5value);
string url = InterfaceManager.IdAddress + ":8080/minio/tasks/upload/" + md5value + "?totalSize=" + length + "&fileName="+Path.GetFileName(filePath) +"&bucketName=ictcomcache";
Debug.Log("获取上传地址:"+url);
//获取文件上传地址
ConsolePanel.ConsoleOutput("获取文件上传地址", "log");
StartCoroutine(Task_Setp_Back.Get(url,false, (isok, result) =>
{
if (isok)
{
if (!string.IsNullOrEmpty(result))
{
JObject jo = JObject.Parse(result);
if (jo["code"].ToObject() == 200)
{
string presignedUrl = jo["data"]["taskRecord"]["presignedUrl"].ToString();
string downloadPath = jo["data"]["path"].ToString();
Debug.Log("presignedUrl:" + presignedUrl);
Debug.Log("downloadPath:" + downloadPath);
if (jo["data"]["finished"].ToObject() == false)
{
//未上传
ConsolePanel.ConsoleOutput("开始上传", "log");
//上传文件
StartCoroutine(Task_Setp_Back.Put(presignedUrl, File.ReadAllBytes(filePath), ConsolePanel.ConsoleOutput("0%", "log"), (isok1, result1) =>
{
if (isok1)
{
ConsolePanel.ConsoleOutput("上传完成100%", "log");
Debug.Log("文件上传成功");
callback(true, downloadPath);
}
else
{
confirm_panel.OnPopup("暂存失败", result1);
callback(false, null);
}
}));
}
else
{
//文件md5重复
Debug.Log("文件已存在,无需上传");
callback(true, downloadPath);
}
}
else
{
confirm_panel.OnPopup("暂存失败", jo["msg"].ToString());
callback(false, null);
}
}
else
{
confirm_panel.OnPopup("暂存失败", "");
callback(false, null);
}
}
else
{
confirm_panel.OnPopup("暂存失败", result);
callback(false, null);
}
}));
}
///
/// 下载
///
public void OnDownload()
{
MainCanvasManager.Instance.transform.Find("ide设置面板").GetComponent().DownLoadProject(true);
}
///
/// 暂存方法
///
public void ZanCunPanel()
{
Debug.Log("暂存成功");
}
///
/// 下载方法
///
public void XiaZaiPanel()
{
Debug.Log("下载方法");
}
///
/// 打开windows选择路径工具
///
private void OpenSingleFile()
{
FilePanel.Settings settings = new FilePanel.Settings();
settings.filters = new FilePanel.Filter[]
{
//new FilePanel.Filter("All files", "*"),
new FilePanel.Filter("媒体文件(*.png;*.jpg;*.jpeg;*.mp3;*.mp4;*.mov;*.avi;)", "png", "jpg", "jpeg","mp3", "mp4", "mov", "avi"),
//new FilePanel.Filter("Vidos files (*.mp4;*.mov;*.avi)", "mp4", "mov", "avi"),
//new FilePanel.Filter("Documents (*.txt;*.rtf;*.doc;*.docx)", "txt", "rtf", "doc", "docx"),
};
settings.title = "导入媒体";
settings.initialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures);
FilePanel.OpenFilePanel(settings, (files) =>
{
file_path = string.Join("\n", files);
});
}
}