AuthorTool.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using AuShell;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using UnityEngine.UI;
  9. public enum LoadMethod
  10. {
  11. 同步,
  12. 异步
  13. }
  14. public class AuthorTool : MonoBehaviour
  15. {
  16. public static AuthorTool m_Instance;
  17. /// <summary>
  18. /// 授权完成加载的场景名称
  19. /// </summary>
  20. [Header("授权完成加载的场景名称")]
  21. public int m_LoadSceneIndex;
  22. [Header("场景加载方式")]
  23. public LoadMethod loadMethod = LoadMethod.同步;
  24. [Header("授权完成关掉界面")]
  25. public bool m_HidePanelAfterAuthor;
  26. [Header("展示联系信息")]
  27. public bool m_ShowAuthorMessage;
  28. /// <summary>
  29. /// 授权联系人
  30. /// </summary>
  31. public string authorContact;
  32. /// <summary>
  33. /// 授权联系电话
  34. /// </summary>
  35. public string authorPhone;
  36. /// <summary>
  37. /// 解密狗授权页面
  38. /// </summary>
  39. private UsbKeyAuthorPanel m_UsbAuthorPanel;
  40. /// <summary>
  41. /// 授权码授权页面
  42. /// </summary>
  43. private CodeAuthorPanel m_CodeAuthorPanel;
  44. /// <summary>
  45. /// 背景
  46. /// </summary>
  47. private GameObject m_Bg;
  48. /// <summary>
  49. /// 配置文件路径
  50. /// </summary>
  51. private string clientConfigFilePath;
  52. private AsyncOperation loader;
  53. /// <summary>
  54. /// 授权配置文件
  55. /// </summary>
  56. private AuthorConfig m_AuthorConfig;
  57. /// <summary>
  58. /// 联系人
  59. /// </summary>
  60. private Text m_AuthorContact;
  61. /// <summary>
  62. /// 联系电话
  63. /// </summary>
  64. private Text m_AuthorPhone;
  65. private void Awake()
  66. {
  67. m_Instance = this;
  68. clientConfigFilePath = Path.Combine(Application.streamingAssetsPath, "AuthorConfig.json");
  69. m_AuthorConfig = ParseClientConfig();
  70. //初始化UI
  71. m_UsbAuthorPanel = transform.Find("UsbAuthorPanel").GetComponent<UsbKeyAuthorPanel>();
  72. m_CodeAuthorPanel = transform.Find("CodeAuthorPanel").GetComponent<CodeAuthorPanel>();
  73. m_AuthorContact = transform.Find("Author/AuthorContact/AuthorContact").GetComponent<Text>();
  74. m_AuthorPhone = transform.Find("Author/AuthorPhone/AuthorPhone").GetComponent<Text>();
  75. m_Bg = transform.Find("BG").gameObject;
  76. transform.Find("CodeAuthorPanel/Loading").gameObject.SetActive(false);
  77. }
  78. private void Start()
  79. {
  80. //联系方式
  81. m_AuthorContact.transform.parent.gameObject.SetActive(m_ShowAuthorMessage);
  82. m_AuthorPhone.transform.parent.gameObject.SetActive(m_ShowAuthorMessage);
  83. m_AuthorContact.text = authorContact;
  84. m_AuthorPhone.text = authorPhone;
  85. //授权验证,首先验证是否为参数启动的
  86. if (VerifyArg())
  87. {
  88. //产品已授权
  89. VerifyArgPass();
  90. }
  91. else
  92. {
  93. ShowAuthorPanel();
  94. }
  95. }
  96. /// <summary>
  97. /// 验证参数
  98. /// </summary>
  99. public bool VerifyArg()
  100. {
  101. string[] LineAgrs = Environment.GetCommandLineArgs();
  102. if (LineAgrs.Length >= 2)
  103. {
  104. string tmpLineArg = LineAgrs[1];
  105. try
  106. {
  107. tmpLineArg = tmpLineArg.Decrypt("chivatech");
  108. string[] tmpArgs = tmpLineArg.Split('/');
  109. Debug.Log(tmpLineArg);
  110. Debug.Log(SystemInfo.deviceUniqueIdentifier);
  111. //判断机器码是否相同
  112. if (tmpArgs[0] == SystemInfo.deviceUniqueIdentifier)
  113. {
  114. return true;
  115. }
  116. }
  117. catch (Exception)
  118. {
  119. return false;
  120. }
  121. }
  122. return false;
  123. }
  124. /// <summary>
  125. /// 显示授权界面
  126. /// </summary>
  127. public void ShowAuthorPanel()
  128. {
  129. if (m_AuthorConfig.UsbKeyAuthor)
  130. {
  131. m_UsbAuthorPanel.gameObject.SetActive(true);
  132. m_CodeAuthorPanel.gameObject.SetActive(false);
  133. return;
  134. }
  135. if (m_AuthorConfig.CodeAuthor)
  136. {
  137. m_UsbAuthorPanel.gameObject.SetActive(false);
  138. m_CodeAuthorPanel.gameObject.SetActive(true);
  139. }
  140. }
  141. /// <summary>
  142. /// 加载下一场景
  143. /// </summary>
  144. public void LoadScene()
  145. {
  146. SceneManager.LoadScene(m_LoadSceneIndex);
  147. }
  148. /// <summary>
  149. /// 授权完成关闭界面
  150. /// </summary>
  151. private void HidePanel()
  152. {
  153. m_UsbAuthorPanel.gameObject.SetActive(false);
  154. m_CodeAuthorPanel.gameObject.SetActive(false);
  155. m_Bg.SetActive(false);
  156. }
  157. /// <summary>
  158. /// 授权通过
  159. /// </summary>
  160. public void VerifyArgPass()
  161. {
  162. transform.Find("CodeAuthorPanel/Loading").gameObject.SetActive(true);
  163. m_CodeAuthorPanel.SetMessageText("产品已授权");
  164. if (m_HidePanelAfterAuthor) HidePanel();
  165. switch (loadMethod)
  166. {
  167. case LoadMethod.同步:
  168. LoadScene();
  169. break;
  170. case LoadMethod.异步:
  171. StartCoroutine(LoadSceneAsync(LoadSceneAsyncCallback));
  172. break;
  173. default:
  174. break;
  175. }
  176. }
  177. private void LoadSceneAsyncCallback()
  178. {
  179. loader.allowSceneActivation = true;
  180. }
  181. private IEnumerator LoadSceneAsync(Action callBack)
  182. {
  183. yield return new WaitForEndOfFrame();
  184. loader = SceneManager.LoadSceneAsync(m_LoadSceneIndex);
  185. yield return loader;
  186. loader.allowSceneActivation = false;
  187. if (callBack != null) callBack.Invoke();
  188. }
  189. /// <summary>
  190. /// 解析配置文件
  191. /// </summary>
  192. /// <returns></returns>
  193. public AuthorConfig ParseClientConfig()
  194. {
  195. if (!File.Exists(clientConfigFilePath))
  196. {
  197. StreamWriter streamWriter = File.CreateText(clientConfigFilePath);
  198. streamWriter.Write(JsonUtility.ToJson(new AuthorConfig()));
  199. streamWriter.Close();
  200. }
  201. return JsonUtility.FromJson<AuthorConfig>(File.ReadAllText(clientConfigFilePath));
  202. }
  203. }