CheckVersionUpdate.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System.Collections;
  2. using UnityEngine;
  3. using System.IO;
  4. using SC.InputSystem;
  5. public class CheckVersionUpdate : PointerDelegate
  6. {
  7. [SerializeField]
  8. private UnityEngine.UI.Text MText;
  9. [SerializeField]
  10. private Slider3D MSlider;
  11. [SerializeField]
  12. private PopBase pop;
  13. [SerializeField]
  14. private SCButton DebugMark;//是否是测试服的标记
  15. private bool isSending;
  16. void Start()
  17. {
  18. DebugMark.onClick.AddListener(DebugClick);
  19. MessageCenterController.Instance.Register(GameEnum.MESSAGE_UPDATE_START, StartUpdate);
  20. CDebug.Log("当前版本 "+ CurVersionValue().ToString());
  21. Engine.Http.HttpManager.GetInstance().AddHttpMessageEvent(HandleHttpMessage);
  22. DebugClick();
  23. }
  24. void OnDestroy()
  25. {
  26. DebugMark.onClick.RemoveListener(DebugClick);
  27. Engine.Http.HttpManager.GetInstance().DelHttMessageEvent(HandleHttpMessage);
  28. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_UPDATE_START, StartUpdate);
  29. }
  30. private void DebugClick()
  31. {
  32. pop.Hide();
  33. isSending = true;
  34. HttpStaticMessage.AutoSendHttpVerson(Application.version, IsDebug);
  35. }
  36. private void Update()
  37. {
  38. if(Time.realtimeSinceStartup > lastClickTime + 5)
  39. {
  40. clickCt = 0;
  41. }
  42. }
  43. //神秘代码部分
  44. private int clickCt;//点击的计数器
  45. private int OpenCt = 10;
  46. private float lastClickTime;
  47. private float maxResetClickCD = 0.5f;
  48. protected override void partAnyKeyDownDelegate(InputKeyCode keyCode, InputDevicePartBase part)
  49. {
  50. if(!isSending && API_InputSystem.Target != null)
  51. {
  52. CDebug.Log(API_InputSystem.Target.name);
  53. CheckResetClickCt();
  54. clickCt +=1;
  55. lastClickTime = Time.realtimeSinceStartup;
  56. CDebug.Log("clickCt " + clickCt);
  57. if(clickCt == OpenCt)
  58. {
  59. //pop.Hide();
  60. DebugMark.gameObject.SetActive(true);
  61. }
  62. else
  63. {
  64. DebugMark.gameObject.SetActive(false);
  65. }
  66. }
  67. }
  68. private void CheckResetClickCt()
  69. {
  70. if(!IsDebug && Time.realtimeSinceStartup > lastClickTime + maxResetClickCD)
  71. {
  72. clickCt = 0;
  73. }
  74. }
  75. private bool IsDebug{
  76. get{return DebugMark.gameObject.activeSelf;}
  77. }
  78. void LateUpdate()
  79. {
  80. OnUpdate(CStaticMethod.SystemFrameTime());
  81. }
  82. void FixedUpdate()
  83. {
  84. OnFixedUpdate(CStaticMethod.SystemFrameTime());
  85. }
  86. public void OnUpdate(int nTime)
  87. {
  88. Engine.Http.HttpManager.GetInstance().OnUpdate(nTime);
  89. }
  90. public void OnFixedUpdate(int nTime)
  91. {
  92. //Http请求
  93. Engine.Http.HttpManager.GetInstance().OnFixedUpdate(nTime);
  94. }
  95. private bool HandleHttpMessage(Engine.Http.HttpResponse httpResponse)
  96. {
  97. if (httpResponse == null)
  98. {
  99. return false;
  100. }
  101. if(!httpResponse.isSuccess)
  102. {
  103. return false;
  104. }
  105. CDebug.Log ("接受消息"+httpResponse.Code);
  106. switch (httpResponse.Code)
  107. {
  108. //获取角色信息
  109. case MsgConst.HTTP_MSG_VERSION:
  110. CheckRes(httpResponse);
  111. break;
  112. }
  113. return false;
  114. }
  115. private string link;
  116. private void CheckRes(Engine.Http.HttpResponse httpResponse)
  117. {
  118. isSending = false;
  119. bool state = httpResponse.ReadBool("state");
  120. if (state)
  121. {
  122. if(IsDebug)
  123. {
  124. this.link = httpResponse.ReadString("debug_url");
  125. }
  126. else
  127. {
  128. this.link = httpResponse.ReadString("url");
  129. }
  130. MText.text = "";
  131. MSlider.gameObject.SetActive(false);
  132. pop.Show(IsDebug? httpResponse.ReadString("debug_ver"): httpResponse.ReadString("online_ver"));
  133. }
  134. else
  135. {
  136. clickCt = 0;
  137. pop.Hide();
  138. CDebug.Log("StartCoroutine ChangeScene");
  139. StartCoroutine("ChangeScene");
  140. DeleteOldFile(httpResponse.ReadString("debug_url"));
  141. DeleteOldFile(httpResponse.ReadString("url"));
  142. }
  143. }
  144. private IEnumerator ChangeScene()
  145. {
  146. yield return new WaitForSeconds(3f);
  147. while(clickCt != 0)
  148. {
  149. CDebug.Log("等待计数器 " + clickCt);
  150. yield return new WaitForSeconds(1.0f);
  151. }
  152. CDebug.Log("开始切换场景");
  153. UnityEngine.SceneManagement.SceneManager.LoadScene("GameStart3D_beauty");
  154. }
  155. private float CurVersionValue()
  156. {
  157. float v;
  158. if(float.TryParse(Application.version, out v))
  159. {
  160. return v;
  161. }
  162. return 0;
  163. }
  164. //开始更新
  165. private void StartUpdate(System.Object datas)
  166. {
  167. CDebug.Log("StartUpdate");
  168. MSlider.gameObject.SetActive(true);
  169. MText.text = "开始加载";
  170. StartCoroutine("InstallApk");
  171. }
  172. private string InstallApkName
  173. {
  174. get{
  175. string[] strs = link.Split('/');
  176. if(strs.Length >=2)
  177. return strs[strs.Length -1];
  178. else
  179. return link;
  180. }
  181. }
  182. private string ApkPath
  183. {
  184. get {
  185. #if UNITY_EDITOR
  186. return Application.persistentDataPath + "/" + InstallApkName;
  187. #elif UNITY_IPHONE || UNITY_ANDROID
  188. return "/data/data/com.BellCat.MREducation/files"+ "/" + InstallApkName;
  189. #endif
  190. }
  191. }
  192. private string OldPath(string url)
  193. {
  194. #if UNITY_EDITOR
  195. return Application.persistentDataPath + "/" + url;
  196. #elif UNITY_IPHONE || UNITY_ANDROID
  197. return "/data/data/com.BellCat.MREducation/files"+ "/" + url;
  198. #endif
  199. }
  200. public IEnumerator InstallApk()
  201. {
  202. //将apk写入沙盒目录
  203. string path = ApkPath;
  204. CDebug.Log("安装路径 " + path);
  205. if(!File.Exists(path))
  206. {
  207. WWW www = new WWW(link);
  208. //下载需要更新的apk
  209. while (true)
  210. {
  211. if (www.isDone)
  212. {
  213. break;
  214. }
  215. //MText.text = ((int)(www.progress / 1f * 100)).ToString() + "%";
  216. MSlider.value = www.progress;
  217. yield return null;
  218. }
  219. if (!string.IsNullOrEmpty(www.error))
  220. {
  221. MText.text = www.error;
  222. yield return 0;
  223. }
  224. File.WriteAllBytes(path, www.bytes);
  225. CDebug.Log("写入文件-->" + path);
  226. }
  227. else
  228. {
  229. yield return 0;
  230. }
  231. Debug.Log("-->" + path);
  232. #if UNITY_ANDROID && !UNITY_EDITOR
  233. TestAPK(path);
  234. /*
  235. AndroidJavaClass javaClass;
  236. javaClass = new AndroidJavaClass("example.administrator.myapplication.MainActivity");
  237. Debug.Log("-->" + "正在安装" + path);
  238. bool res = javaClass.CallStatic<bool>("installAPK", path);
  239. if (res)
  240. {
  241. MText.text = "加载完成";
  242. //Application.Quit();
  243. }
  244. else
  245. {
  246. MText.text = "加载失败";
  247. }
  248. */
  249. #endif
  250. //UnityEngine.SceneManagement.SceneManager.LoadScene("GameStart3D_beauty");
  251. }
  252. private void TestAPK(string fileproviderAuthority)
  253. {
  254. if(!File.Exists(fileproviderAuthority))
  255. {
  256. CDebug.Log("-->" + "文件不存在" + fileproviderAuthority);
  257. return;
  258. }
  259. CDebug.Log("-->" + "正在安装" + fileproviderAuthority);
  260. string value = SC.Tools.UpdateAPKSystem.AndroidPluginUpdateAPK.getInstant.InstallApkWithActivity(fileproviderAuthority);
  261. CDebug.Log("[" + GetType().ToString() + "]: " + "Install result:" + value);
  262. if (value == "Success")
  263. {
  264. MText.text = "加载完成";
  265. }
  266. else
  267. {
  268. MText.text = "加载失败";
  269. File.Delete(fileproviderAuthority);
  270. }
  271. }
  272. private void DeleteOldFile(string fileName)
  273. {
  274. if(fileName == null)
  275. {
  276. return;
  277. }
  278. //沙盒目录
  279. string path = OldPath(fileName);
  280. if(File.Exists(path))
  281. {
  282. //删除文件
  283. File.Delete(path);
  284. }
  285. }
  286. }