90 lines
2.7 KiB
C#
90 lines
2.7 KiB
C#
using System;
|
||
using System.IO;
|
||
using UnityEngine;
|
||
using UnityEngine.Events;
|
||
|
||
public class DataManager : MonoBehaviour
|
||
{
|
||
[Tooltip("时间格式:yyyy-MM-dd HH:mm:ss")]
|
||
public string EndTimer;//结束时间
|
||
private string RecordData;
|
||
private void Start()
|
||
{
|
||
// CreatTimer();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新系统时间
|
||
/// </summary>
|
||
public void UpdateTime(UnityAction back1,UnityAction back2,UnityAction back3)
|
||
{
|
||
try
|
||
{
|
||
string datas = DecryptFileReader.ReadAndDecryptData("Timer.txt");
|
||
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("仿真文件被损坏,请联系管理员进行修复");
|
||
back1?.Invoke();
|
||
}
|
||
else
|
||
{
|
||
//把上一次存储得系统时间更新到最新
|
||
string timer = "Timer.txt";
|
||
RecordData = Data;
|
||
string strMerge = EndTimer + "|" + RecordData;
|
||
EncryptFileCreator.EncryptAndSaveData(strMerge, timer);
|
||
back3?.Invoke();
|
||
}
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogError($"数据出错: {e.Message}");
|
||
back1?.Invoke();
|
||
}
|
||
|
||
if (JudgeExpire())
|
||
{
|
||
Debug.Log("请联系管理员进行升级");
|
||
back2?.Invoke();
|
||
}
|
||
}
|
||
|
||
/// <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");
|
||
}
|
||
}
|