using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json.Linq;
using System.IO;
using Newtonsoft.Json;
using System.Linq;
using UnityEngine.SceneManagement;
using WpfApp1.Util;
using NPOI.SS.Formula.Functions;
public class CallForTest : MonoBehaviour
{
public static CallForTest instance;
///
/// 启动参数
///
[HideInInspector]
public List infos = null;
///
/// 人员信息
///
public ICT_user user;
///
/// 暂存信息
///
public MyTmpSaveInfo saveInfo;
///
/// 章节id
///
[HideInInspector]
public string currentContentId;
///
/// 任务开始时间
///
public DateTime currentStartTime;
///
/// 实训记录id
///
[HideInInspector]
public string recordId;
///
/// 当前任务详情数据
///
public TaskItemData currentTaskData;
///
/// 读取的任务配置
///
public List tasks;
///
/// 应用appid
///
private string appid;
///
/// 应用密钥
///
private string secret;
///
/// AES公钥
///
private string aesKey;
///
/// RSA公钥
///
private string rsaKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCXLr8EzT/lt9XYKOX2CzkwqVcW//UyIP5824OEgdPHLhBvGy5QXtyIj0myw3LlbqZkOoX8SZFMxJOnHiCKIVhXrTYOaxTdWYv95DR4gCZ9q55U7NQPUkeMtTbuLa5HHdpBy8DM+DfS2VJv6RTK3AJLjWkNjpyhGgWIlOTGbMsj7QIDAQAB";
public string Appid { get => appid; set => appid = value; }
///
/// AES公钥
///
public string AesKey { get => aesKey; set => aesKey = value; }
///
/// 是否开启加密
///
[HideInInspector]
public bool is加密;
///
/// 临时文件根目录
///
[HideInInspector]
public string pathRoot;
///
/// 任务模式临时工程路径
///
[HideInInspector]
public string pathProject;
///
/// 下载缓存路径
///
[HideInInspector]
public string pathCache;
///
/// 临时存储任务截图,录屏
///
[HideInInspector]
public string pathTaskFile;
///
/// 自由模式工程根目录
///
[HideInInspector]
public string pathFreeProjectRoot;
///
/// 是否打印信息
///
private bool showDebug;
public bool IsStartWithService;
private void Awake()
{
//if(DateTime.Now>new DateTime(2024,12,31))
//{
// return;
//}
instance = this;
#if !UNITY_EDITOR
pathRoot= Application.streamingAssetsPath + "/ProjectCode";
pathProject = Application.streamingAssetsPath + "/ProjectCode/TmpCode";
pathCache = Application.streamingAssetsPath + "/ProjectCode/Cache";
pathTaskFile= Application.streamingAssetsPath + "/ProjectCode/Submit";
pathFreeProjectRoot=Application.streamingAssetsPath + "/ProjectCode/FreeModeTmpCode";
#else
//编辑器
pathRoot = "D:/UserFiles/ProjectCode";
pathProject = "D:/UserFiles/ProjectCode/TmpCode";
pathCache = "D:/UserFiles/ProjectCode/Cache";
pathTaskFile = "D:/UserFiles/ProjectCode/Submit";
pathFreeProjectRoot = "D:/UserFiles/ProjectCode/FreeModeTmpCode";
#endif
//Debug.LogError(pathFreeProjectRoot);
if (!Directory.Exists(pathRoot))
Directory.CreateDirectory(pathRoot);
if (!Directory.Exists(pathProject))
Directory.CreateDirectory(pathProject);
if (!Directory.Exists(pathCache))
Directory.CreateDirectory(pathCache);
if (!Directory.Exists(pathTaskFile))
Directory.CreateDirectory(pathTaskFile);
if (!Directory.Exists(pathFreeProjectRoot))
{
Directory.CreateDirectory(pathFreeProjectRoot);
Directory.CreateDirectory(pathFreeProjectRoot + "/Python");
Directory.CreateDirectory(pathFreeProjectRoot + "/Java");
Directory.CreateDirectory(pathFreeProjectRoot + "/C");
}
//读配置
File.ReadAllLines(Application.streamingAssetsPath + "/MainConfig.ini").ToList().ForEach(a =>
{
if (!a.StartsWith("#") && !string.IsNullOrEmpty(a))
{
string[] tmp = a.Split('=');
Debug.Log(tmp);
if (tmp[0].Trim() == "appid")
{
appid = tmp[1].Trim();
}
else if (tmp[0].Trim() == "secret")
{
secret = tmp[1].Trim();
}
else if (tmp[0].Trim() == "IdAddress")
{
InterfaceManager.IdAddress = tmp[1].Trim();
}
else if(tmp[0].Trim() == "showDebug")
{
showDebug = (tmp[1].Trim() == "on" ? true : false);
}
}
});
}
private void Start()
{
//if (DateTime.Now > new DateTime(2024, 12, 31))
//{
// return;
// return;
//}
//LoginSceneUIManager.current_preselect_scene = "深圳市民中心";
//SceneManager.LoadSceneAsync("LoadingScene", LoadSceneMode.Additive);
//获取启动参数
infos = Environment.GetCommandLineArgs().ToList();
//string info = "ict://;https//wqx-ailabedu.oss-cn-shenzhen.aliyuncs.com/ict-component/APP%E5%A4%96%E7%BD%91%E7%89%889.6.zip;APP/ict_component.exe;;2024112515161497559412755756;3ec6299b5e840e63ae556fb8b3ca0e27;2024.09.06.10.43;%E7%BC%96%E7%A8%8B%E6%95%99%E5%AD%A6;%E8%BD%A8%E8%BF%B9%E5%8F%8A%E7%81%AF%E5%85%89%E7%A7%80%E8%A1%A8%E6%BC%94;%5B%7B%22Name%22:%22PyCharm%22,%22RegistryPath%22:%22SOFTWWARE%5C%5CClasses%22,%22RegistryValueName%22:%22(%E9%BB%98%E8%AE%A4)%22,%22RegistryValue%22:%22JetBrains%20PyCharm%20Community%20Edition%22,%22NodeName%22:%22PyCharm%22%7D%5D;6;AILAB;;0;%E9%80%9A%E8%AF%86%E5%B9%B3%E5%8F%B0%E5%AD%A6%E7%94%9F;https://ailabedu.com/static/images/defaultAvatar.png;0;0;http://ict.ailabedu.com/";
string ticketa = infos.Find(a => a.StartsWith("ticket="));
string component_typeta = infos.Find(a => a.StartsWith("component_type="));
Debug.Log(ticketa);
//ticketa = "ticket=3ec6299b5e840e63ae556fb8b3ca0e27";
//component_typeta = "component_type=0";
//获取密钥
string url11 = InterfaceManager.IdAddress + ":8080/config/secret";
Debug.Log(url11);
StartCoroutine(Task_Setp_Back.Get(url11, false, (ok, result) =>
{
if (ok)
{
JObject jb = JObject.Parse(result);
if(jb["enable"].ToObject())
{
//开启加密
is加密 = true;
string rsa = jb["key"].ToString();
AesKey=RSAHelper.DecryptByPublicKey(rsa, rsaKey);
Debug.Log("开启加密");
}
else
{
//未开启加密
is加密 = false;
Debug.Log("关闭加密");
}
//获取token
if (! string.IsNullOrEmpty(ticketa) && !string.IsNullOrEmpty(component_typeta) && !string.IsNullOrEmpty(ticketa.Split("ticket=")[1]) && !string.IsNullOrEmpty(component_typeta.Split("component_type=")[1]))
{
Debug.Log("有任务启动:"+ ticketa +" "+ component_typeta);
string ticket = ticketa.Split("ticket=")[1];
string component_type = component_typeta.Split("component_type=")[1];
//有任务启动
GetToken(ticket, a =>
{
if (a)
{
GetData(ticket, (MainMenuType)int.Parse(component_type));
}
});
}
else
{
//无任务启动
Debug.Log("无任务启动");
GameInit.Instance.LoadScene();
}
}
else
{
Debug.LogError("获取密文失败");
}
}));
}
///
/// tcp切换任务
///
[HideInInspector]
public string doChangTask = "";
[HideInInspector]
public string doChangeType = "";
private void Update()
{
if(!string.IsNullOrEmpty(doChangTask) && !string.IsNullOrEmpty(doChangeType))
{
string tmp = doChangTask;
string tmptype = doChangeType;
doChangTask = "";
doChangeType = "";
//任务切换
GetToken(tmp, a =>
{
if (a)
{
GetData(tmp, (MainMenuType)int.Parse(tmptype));
}
});
}
}
public string 测试ticket;
public MainMenuType 测试type;
[ContextMenu("切换任务")]
public void Test()
{
GetToken(测试ticket, a =>
{
if (a)
{
GetData(测试ticket, 测试type);
}
});
}
///
/// 获取当前实训任务,并启动(网页切换任务)
///
///
private void GetData(string ticket,MainMenuType mainMenuType)
{
//如果当前任务已启动,不切换任务
if (!UnityEngine.SceneManagement.SceneManager.GetSceneByName("UIScene").isLoaded)
{
Debug.Log("任务加载中ticket=" + ticket);
string url = InterfaceManager.IdAddress + ":8080/postback/getTicketInfo?ticket=" + ticket;
StartCoroutine(Task_Setp_Back.Get(url, true,(isok, info) =>
{
if (isok)
{
if (info != null)
{
JObject jb = JObject.Parse(info);
if ((int)jb["code"] == 200)
{
var data = jb["data"];
ICT_user user = new ICT_user();
user.userId = data["userId"].ToString();
Debug.Log(user.userId);
user.nickName = data["userName"].ToString();
this.user = user;
currentContentId = data["contentId"].ToString();
MyTask _task = new MyTask();
_task._taskId = (string)data["taskId"];
_task.taskCode = (string)data["taskCode"];
_task._contentId = (string)data["contentId"];
_task._startTime = (string)data["startTime"];
_task._endTime = (string)data["expireTime"];
_task._ticket = (string)data["ticket"];
_task.programLanguage = data["programLanguage"].ToString();
_task.studyRecordId = data["studyRecordId"].ToString();
CallForTest.instance.recordId = data["studyRecordId"].ToString();
GameManager.is_login = true;
GameManager.current_main_menu_type = mainMenuType;
//加载任务,跳场景
Debug.Log("加载任务,跳场景");
//启动器启动任务,直接跳转至对应任务场景
GetTaskDetail(_task._taskId, _task._contentId, _task.programLanguage, true);
}
else
{
Debug.LogError(jb["msg"].ToString());
}
}
}
}));
}
else
{
MainCanvasManager.confirm_panel.OnPopup("切换任务失败,已有任务进行中。请先点击上方返回按钮,退出到首页。","");
}
}
private void OnGUI()
{
if (showDebug)
{
if (infos != null && infos.Count > 0)
{
GUILayout.Label("启动参数:");
for (int i = 0; i < infos.Count; i++)
{
GUILayout.Label(infos[i]);
}
}
}
}
public void GetTaskDetail(string taskid, string ContentId, string laugulage, bool has_task = false)
{
//获取任务详情
var _url = InterfaceManager.IdAddress + ":8080/component/app/task/detail?programLanguage=" + laugulage + "&taskId=" + taskid;
StartCoroutine(InterfaceManager.Get(_url,true, (_data) =>
{
JObject obj = JObject.Parse(_data);
if (obj["code"].ToObject() == 200)
{
var tmpde = obj["data"];
CallForTest.instance.currentTaskData = HandleData(tmpde);
Debug.Log(CallForTest.instance.currentTaskData.task_steplist+"任务详情列表");
CallForTest.instance.currentContentId = ContentId;
CallForTest.instance.currentStartTime = DateTime.Now;
List senceNames = new List { "数字人舞台", "机器人舞台", "深圳市民中心", "西丽湖校区" };
if (senceNames.Contains(CallForTest.instance.currentTaskData.task_scenename))
{
string tmpscene = "";
if (CallForTest.instance.currentTaskData.task_scenename == "深圳市民中心" || CallForTest.instance.currentTaskData.task_scenename == "西丽湖校区")
{
tmpscene = CallForTest.instance.currentTaskData.task_scenename + "_昼";
}
else
{
tmpscene = CallForTest.instance.currentTaskData.task_scenename;
}
LoginSceneUIManager.current_preselect_scene = tmpscene;
GameManager.current_scene = CallForTest.instance.currentTaskData.task_scenename;
GameManager.current_component_type = (ComponentType)int.Parse(CallForTest.instance.currentTaskData.task_componentType);
//跳
if (has_task)
{
SceneManager.LoadSceneAsync("LoadingScene", LoadSceneMode.Additive);
}
else
{
LoginSceneUIManager.loading_panel.OnActive(LoginSceneUIManager.course_task_detail_panel);
}
Debug.Log("启动关卡!");
}
else
{
Debug.LogError("无此场景:" + CallForTest.instance.currentTaskData.task_scenename);
}
}
}));
}
///
/// 解析任务详情数据
///
///
private TaskItemData HandleData(JToken _data)
{
TaskItemData _taskdata = new TaskItemData();
_taskdata.task_id = _data["id"].ToString();
_taskdata.task_componentType = _data["componentType"].ToString();
_taskdata.task_name = _data["taskName"].ToString();
_taskdata.task_scenename = _data["sceneName"].ToString();
_taskdata.task_packagefilename = _data["taskPackageFileName"].ToString();
_taskdata.task_packagefileurl = _data["taskPackageFileUrl"].ToString();
_taskdata.investigatePointList = _data["investigatePointList"].ToObject>();
_taskdata.task_description = _data["taskDescription"].ToString();
_taskdata.task_picturelist = _data["taskPictureList"].ToObject>();
_taskdata.task_steplist = _data["taskStepList"].ToObject>();
_taskdata.scene_formdtoslist = _data["sceneFormDto"].ToObject();
_taskdata.programDto = _data["programDto"].ToObject();
return _taskdata;
}
#region 暂时无用
// public IEnumerator GetCall(string url,Action back)
// {
// UnityWebRequest webRequest =UnityWebRequest.Get(url);
// webRequest.SetRequestHeader("Authorization","Bearer "+access_token);
// yield return webRequest.SendWebRequest();
// if(webRequest.isDone)
// {
// if(webRequest.isNetworkError)
// {
// Debug.LogError(webRequest.error);
// }
// else
// {
// JObject jb=JObject.Parse(webRequest.downloadHandler.text);
// if(jb["code"].ToObject()==200)
// {
// back(true, jb["data"]);
// }
// else
// {
// Debug.LogError(jb["msg"].ToString());
// }
// }
// }
//
// back(false, null);
// }
public IEnumerator PostCall(string url, string postData, Action back)
{
UnityWebRequest webRequest = UnityWebRequest.Post(url, postData, "application/json");
yield return webRequest.SendWebRequest();
if (webRequest.isDone)
{
if (webRequest.isNetworkError)
{
Debug.LogError(webRequest.error);
}
else
{
JObject jb = JObject.Parse(webRequest.downloadHandler.text);
if (jb["code"].ToObject() == 200)
{
back(true, jb["access_token"]);
}
else
{
Debug.LogError(jb["msg"].ToString());
}
}
}
}
///
/// 根据启动器返回的ticket去获取token
///
public void GetToken(string ticket, Action callback)
{
string url = InterfaceManager.IdAddress + ":8080/appAuth";
var tmpdata = new
{
appId = Appid, //应用ID
ticket = ticket, //票据ID
signature = ticket + Appid + secret//签名信息:票据ID+应用ID+秘钥(暂未RSA加密)
};
string postData = JsonConvert.SerializeObject(tmpdata);
StartCoroutine(Task_Setp_Back.Post(url, postData, null,(result, data) =>
{
if (result)
{
JObject jb = JObject.Parse(data);
if (jb["code"].ToObject() == 200)
{
InterfaceManager.Token = jb["access_token"].ToString();
callback(true);
}
else
{
Debug.LogError(jb["msg"].ToString());
callback(false);
}
}
else
{
callback(false);
}
}));
}
#endregion
}
///
/// ict人员信息
///
public class ICT_user
{
public string userId;
///
/// 登录账号
///
public string userName;
///
/// 人员姓名
///
public string nickName;
///
/// 头像地址
///
public string avatar;
}
///
/// 任务
///
public class MyTask
{
///
/// 任务名称
///
public string name;
///
/// 任务编号
///
public string code;
public string _taskId;
public string taskCode;
public string _contentId;
public string _startTime;
public string _endTime;
public string _ticket;
public string _staginstate;//暂存状态
public List stepList;
public string programLanguage;
public string studyRecordId;
}
///
/// 步骤
///
public class MyStep
{
public string name;
public string code;//步骤编码
public string stepName;
public string stepStatus;
}