123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- using AuShell;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public enum LoadMethod
- {
- 同步,
- 异步
- }
- public class AuthorTool : MonoBehaviour
- {
- public static AuthorTool m_Instance;
- /// <summary>
- /// 授权完成加载的场景名称
- /// </summary>
- [Header("授权完成加载的场景名称")]
- public int m_LoadSceneIndex;
- [Header("场景加载方式")]
- public LoadMethod loadMethod = LoadMethod.同步;
- [Header("授权完成关掉界面")]
- public bool m_HidePanelAfterAuthor;
- [Header("展示联系信息")]
- public bool m_ShowAuthorMessage;
- /// <summary>
- /// 授权联系人
- /// </summary>
- public string authorContact;
- /// <summary>
- /// 授权联系电话
- /// </summary>
- public string authorPhone;
- /// <summary>
- /// 解密狗授权页面
- /// </summary>
- private UsbKeyAuthorPanel m_UsbAuthorPanel;
- /// <summary>
- /// 授权码授权页面
- /// </summary>
- private CodeAuthorPanel m_CodeAuthorPanel;
- /// <summary>
- /// 背景
- /// </summary>
- private GameObject m_Bg;
- /// <summary>
- /// 配置文件路径
- /// </summary>
- private string clientConfigFilePath;
- private AsyncOperation loader;
- /// <summary>
- /// 授权配置文件
- /// </summary>
- private AuthorConfig m_AuthorConfig;
- /// <summary>
- /// 联系人
- /// </summary>
- private Text m_AuthorContact;
- /// <summary>
- /// 联系电话
- /// </summary>
- private Text m_AuthorPhone;
- private void Awake()
- {
- m_Instance = this;
- clientConfigFilePath = Path.Combine(Application.streamingAssetsPath, "AuthorConfig.json");
- m_AuthorConfig = ParseClientConfig();
- //初始化UI
- m_UsbAuthorPanel = transform.Find("UsbAuthorPanel").GetComponent<UsbKeyAuthorPanel>();
- m_CodeAuthorPanel = transform.Find("CodeAuthorPanel").GetComponent<CodeAuthorPanel>();
- m_AuthorContact = transform.Find("Author/AuthorContact/AuthorContact").GetComponent<Text>();
- m_AuthorPhone = transform.Find("Author/AuthorPhone/AuthorPhone").GetComponent<Text>();
- m_Bg = transform.Find("BG").gameObject;
- transform.Find("CodeAuthorPanel/Loading").gameObject.SetActive(false);
- }
- private void Start()
- {
- //联系方式
- m_AuthorContact.transform.parent.gameObject.SetActive(m_ShowAuthorMessage);
- m_AuthorPhone.transform.parent.gameObject.SetActive(m_ShowAuthorMessage);
- m_AuthorContact.text = authorContact;
- m_AuthorPhone.text = authorPhone;
- //授权验证,首先验证是否为参数启动的
- if (VerifyArg())
- {
- //产品已授权
- VerifyArgPass();
- }
- else
- {
- ShowAuthorPanel();
- }
- }
- /// <summary>
- /// 验证参数
- /// </summary>
- public bool VerifyArg()
- {
- string[] LineAgrs = Environment.GetCommandLineArgs();
- if (LineAgrs.Length >= 2)
- {
- string tmpLineArg = LineAgrs[1];
- try
- {
- tmpLineArg = tmpLineArg.Decrypt("chivatech");
- string[] tmpArgs = tmpLineArg.Split('/');
- Debug.Log(tmpLineArg);
- Debug.Log(SystemInfo.deviceUniqueIdentifier);
- //判断机器码是否相同
- if (tmpArgs[0] == SystemInfo.deviceUniqueIdentifier)
- {
- return true;
- }
- }
- catch (Exception)
- {
- return false;
- }
- }
- return false;
- }
- /// <summary>
- /// 显示授权界面
- /// </summary>
- public void ShowAuthorPanel()
- {
- if (m_AuthorConfig.UsbKeyAuthor)
- {
- m_UsbAuthorPanel.gameObject.SetActive(true);
- m_CodeAuthorPanel.gameObject.SetActive(false);
- return;
- }
- if (m_AuthorConfig.CodeAuthor)
- {
- m_UsbAuthorPanel.gameObject.SetActive(false);
- m_CodeAuthorPanel.gameObject.SetActive(true);
- }
- }
- /// <summary>
- /// 加载下一场景
- /// </summary>
- public void LoadScene()
- {
- SceneManager.LoadScene(m_LoadSceneIndex);
- }
- /// <summary>
- /// 授权完成关闭界面
- /// </summary>
- private void HidePanel()
- {
- m_UsbAuthorPanel.gameObject.SetActive(false);
- m_CodeAuthorPanel.gameObject.SetActive(false);
- m_Bg.SetActive(false);
- }
- /// <summary>
- /// 授权通过
- /// </summary>
- public void VerifyArgPass()
- {
- transform.Find("CodeAuthorPanel/Loading").gameObject.SetActive(true);
- m_CodeAuthorPanel.SetMessageText("产品已授权");
- if (m_HidePanelAfterAuthor) HidePanel();
- switch (loadMethod)
- {
- case LoadMethod.同步:
- LoadScene();
- break;
- case LoadMethod.异步:
- StartCoroutine(LoadSceneAsync(LoadSceneAsyncCallback));
- break;
- default:
- break;
- }
- }
- private void LoadSceneAsyncCallback()
- {
- loader.allowSceneActivation = true;
- }
- private IEnumerator LoadSceneAsync(Action callBack)
- {
- yield return new WaitForEndOfFrame();
- loader = SceneManager.LoadSceneAsync(m_LoadSceneIndex);
- yield return loader;
- loader.allowSceneActivation = false;
- if (callBack != null) callBack.Invoke();
- }
- /// <summary>
- /// 解析配置文件
- /// </summary>
- /// <returns></returns>
- public AuthorConfig ParseClientConfig()
- {
- if (!File.Exists(clientConfigFilePath))
- {
- StreamWriter streamWriter = File.CreateText(clientConfigFilePath);
- streamWriter.Write(JsonUtility.ToJson(new AuthorConfig()));
- streamWriter.Close();
- }
- return JsonUtility.FromJson<AuthorConfig>(File.ReadAllText(clientConfigFilePath));
- }
- }
|