using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; namespace GeneralTools { public class CertificateManager : SingletonBaseAttribute { private static string productName; private SocketBase udpSender; private string udpSenderStr; private Coroutine TrialCor; private static CertificateSetting setting; public override void IAwake() { base.IAwake(); setting = CertificateSetting.GetCertificateSetting(); //读取配置 productName = setting.useProdctName; LockDll.CertificateVerification.OnPrintMsg += (msg) => { Debug.Log(msg); }; if (!setting.isCheckCertificate) { Debug.Log("START: Frame Game Debug"); return; } CheckCertificate(); } public override void IStart() { base.IStart(); udpSender = SocketManager.Instance.CreateUdp(39527).SetSocketName("CertificateUDPSender").AddReceiveEvent(UdpReceiveHandler); StartCoroutine(NetWorkBroadcast()); } public bool CheckCertificate() { if (TrialCor != null) { StopCoroutine(TrialCor); TrialCor = null; } MessageBox.Instance.Hide("证书提示"); bool checkSuccess = LockDll.CertificateVerification.CheckCertificate(DevicePath.CertificateProgramPath, DevicePath.CertificateSystemPath, productName); udpSenderStr = CreateUdpInfo(DevicePath.CertificateSystemPath); if (!checkSuccess) { HandlerLock("证书到期\r\n请联系管理员"); } return checkSuccess; } private void HandlerLock(string showStr) { if (setting.lockType == LockType.直接退出) { Application.Quit(); } else if (setting.lockType == LockType.弹窗不试用) { MessageBox.Instance.Show("提示", showStr, "退出", () => { Application.Quit(); }, "证书提示"); } else if (setting.lockType == LockType.弹窗试用) { MessageBox.Instance.Show("提示", showStr, "试用", () => { MessageBox.Instance.Hide(); TrialProgram(); }, "证书提示"); } } private void TrialProgram() { float time = 3600; MessageBox.Instance.Show("开始试用,时间" + time + "秒"); TrialCor = StartCoroutine(TrialIEnumerator(time)); } IEnumerator TrialIEnumerator(float time) { Debug.Log("开始试用,时间:" + time + "秒"); yield return new WaitForSeconds(time); MessageBox.Instance.Show("试用已经结束,即将关闭。", "退出", Application.Quit); Debug.Log("试用结束"); yield return new WaitForSeconds(10); Application.Quit(); } public static void CreateOneCertificate() { LockDll.CertificateVerification.CreateForeverCertificate(DevicePath.CertificateProgramPath, productName); } #region 网络模块 private string CreateUdpInfo(string path) { Dictionary certificateInfo = LockDll.CertificateVerification.GetCertificateInfo(DevicePath.CertificateProgramPath); string tempInfo = "programProductName=" + productName + "&"; foreach (var item in certificateInfo) { tempInfo += item.Key + "=" + item.Value + "&"; } return tempInfo; } private IEnumerator NetWorkBroadcast() { while (true) { yield return new WaitForSeconds(5); udpSender.SendMsg("certificatehere"); } } private void UdpReceiveHandler(SocketMsg msg) { if (msg.msg == "certificateshow") { UDPClient.SendMsgOnce(msg.remoteIpEndPoint.Address.ToString(), msg.remoteIpEndPoint.Port, udpSenderStr); } else if (msg.msg.StartsWith("certificateinfo:")) { string m = msg.msg.Replace("certificateinfo:", ""); LockDll.CertificateVerification.CreateCertificate(DevicePath.CertificateSystemPath, m); Debug.Log("写入证书,重新检测"); CheckCertificate(); } } #endregion } public enum LockType { 弹窗试用, 弹窗不试用, 直接退出, } }