2025-04-23 10:01:05 +08:00
|
|
|
using System;
|
2025-04-24 14:54:32 +08:00
|
|
|
using System.Collections;
|
2025-04-23 10:01:05 +08:00
|
|
|
using System.IO;
|
2025-04-23 10:12:42 +08:00
|
|
|
using TMPro;
|
2025-04-23 10:01:05 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Events;
|
2025-04-24 14:54:32 +08:00
|
|
|
using UnityEngine.Networking;
|
2025-04-23 10:12:42 +08:00
|
|
|
using UnityEngine.UI;
|
2025-04-23 10:01:05 +08:00
|
|
|
|
|
|
|
|
public class DateManager : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
private string EndTimer;//结束时间
|
|
|
|
|
private string RecordData;
|
2025-04-23 10:12:42 +08:00
|
|
|
public GameObject ui;
|
|
|
|
|
public TextMeshProUGUI text;
|
|
|
|
|
public Button btn;
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
2025-04-23 10:15:28 +08:00
|
|
|
ui.gameObject.SetActive(false);
|
2025-04-23 10:12:42 +08:00
|
|
|
btn.onClick.AddListener(() =>
|
|
|
|
|
{
|
|
|
|
|
Application.Quit();
|
|
|
|
|
});
|
2025-04-24 14:54:32 +08:00
|
|
|
#if UNITY_WEBGL
|
|
|
|
|
StartCoroutine(WebUpdateTime());
|
|
|
|
|
#else
|
|
|
|
|
UpdateTime(DecryptFileReader.ReadAndDecryptData("Timer.txt"), ShowTip, ShowTip, () =>
|
2025-04-23 10:37:52 +08:00
|
|
|
{
|
|
|
|
|
gameObject.SetActive(false);
|
|
|
|
|
});
|
2025-04-24 14:54:32 +08:00
|
|
|
#endif
|
2025-04-23 10:12:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ShowTip(string str)
|
|
|
|
|
{
|
2025-04-23 11:45:50 +08:00
|
|
|
gameObject.SetActive(true);
|
2025-04-23 10:12:42 +08:00
|
|
|
ui.SetActive(true);
|
|
|
|
|
text.text = str;
|
2025-04-23 10:01:05 +08:00
|
|
|
}
|
|
|
|
|
|
2025-04-24 14:54:32 +08:00
|
|
|
public IEnumerator WebUpdateTime()
|
|
|
|
|
{
|
|
|
|
|
string path = Path.Combine(Application.streamingAssetsPath, "Timer.txt");
|
|
|
|
|
using (UnityWebRequest request = new UnityWebRequest(path))
|
|
|
|
|
{
|
|
|
|
|
request.downloadHandler = new DownloadHandlerBuffer();
|
|
|
|
|
yield return request.SendWebRequest();
|
|
|
|
|
if (string.IsNullOrEmpty(request.error))
|
|
|
|
|
{
|
|
|
|
|
string datas = DecryptFileReader.ReadAndDecryptData(request.downloadHandler.data);
|
|
|
|
|
UpdateTime(datas, ShowTip, ShowTip, () =>
|
|
|
|
|
{
|
|
|
|
|
gameObject.SetActive(false);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError(request.error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-23 10:01:05 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 更新系统时间
|
|
|
|
|
/// </summary>
|
2025-04-24 14:54:32 +08:00
|
|
|
public void UpdateTime(string datas, UnityAction<string> error = null, UnityAction<string> timeOut = null, UnityAction updateTimer = null)
|
2025-04-23 10:01:05 +08:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
EndTimer = datas.Split('|')[0];
|
|
|
|
|
RecordData = datas.Split('|')[1];
|
|
|
|
|
//第一次获取获取系统时间
|
|
|
|
|
DateTime currentDateTime = DateTime.Now;
|
|
|
|
|
string Data = currentDateTime.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
if (DateTime.TryParse(RecordData, out DateTime recordDateTime) && DateTime.TryParse(Data, out DateTime nowDateTime))
|
|
|
|
|
{
|
|
|
|
|
if (recordDateTime > nowDateTime)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("仿真文件被损坏,请联系管理员进行修复");
|
2025-04-23 10:12:42 +08:00
|
|
|
error?.Invoke("仿真文件被损坏,请联系管理员进行修复");
|
2025-04-23 10:01:05 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-04-24 14:54:32 +08:00
|
|
|
#if !UNITY_WEBGL
|
2025-04-23 10:01:05 +08:00
|
|
|
//把上一次存储得系统时间更新到最新
|
|
|
|
|
string timer = "Timer.txt";
|
|
|
|
|
RecordData = Data;
|
|
|
|
|
string strMerge = EndTimer + "|" + RecordData;
|
|
|
|
|
EncryptFileCreator.EncryptAndSaveData(strMerge, timer);
|
2025-04-24 14:54:32 +08:00
|
|
|
#endif
|
2025-04-23 10:12:42 +08:00
|
|
|
updateTimer?.Invoke();
|
2025-04-23 10:01:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"数据出错: {e.Message}");
|
2025-04-23 10:12:42 +08:00
|
|
|
error?.Invoke($"数据出错: {e.Message}");
|
2025-04-23 11:45:50 +08:00
|
|
|
return;
|
2025-04-23 10:01:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (JudgeExpire())
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("请联系管理员进行升级");
|
2025-04-23 10:12:42 +08:00
|
|
|
timeOut?.Invoke("请联系管理员进行升级");
|
2025-04-23 11:45:50 +08:00
|
|
|
return;
|
2025-04-23 10:01:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 判断是否到期
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool JudgeExpire()
|
|
|
|
|
{
|
|
|
|
|
if (DateTime.TryParse(EndTimer, out DateTime endDataTime) && DateTime.TryParse(RecordData, out DateTime recordDateTime))
|
|
|
|
|
{
|
|
|
|
|
//结束日期小于目前日期代表到期
|
|
|
|
|
if (endDataTime < recordDateTime)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 给客户打包的时候,需要设置EndTimer,然后运行一次生成文件并注释掉
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void CreatTimer()
|
|
|
|
|
{
|
|
|
|
|
//第一次获取获取系统时间
|
|
|
|
|
DateTime currentDateTime = DateTime.Now;
|
|
|
|
|
RecordData = currentDateTime.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
string strMerge = EndTimer + "|" + RecordData;
|
|
|
|
|
EncryptFileCreator.EncryptAndSaveData(strMerge, "Timer.txt");
|
|
|
|
|
}
|
|
|
|
|
}
|