ATIntergrationHotFix.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Xml.Linq;
  7. using UnityEditor;
  8. using UnityEngine;
  9. using UnityEngine.Networking;
  10. using UnityEditor.PackageManager.Requests;
  11. using UnityEditor.PackageManager;
  12. using System.Threading.Tasks;
  13. using System.Threading;
  14. namespace AnyThink.Scripts.IntegrationManager.Editor
  15. {
  16. public class ATIntegrationHotFix {
  17. public static ATIntegrationHotFix Instance = new ATIntegrationHotFix();
  18. private ATIntegrationHotFix()
  19. {
  20. }
  21. private static string plugin_hot_fix_data_file_name = "plugin_hot_fix_data.json";
  22. public void loadHotFixData()
  23. {
  24. var downloadUrl = ATNetInfo.getHotfixPluginDownloadUrl(ATConfig.PLUGIN_VERSION);
  25. ATLog.log("loadHotFixData() >>> downloadUrl: " + downloadUrl);
  26. ATEditorCoroutine.startCoroutine(loadHotFixDataWithIEnumerator(downloadUrl));
  27. }
  28. private IEnumerator loadHotFixDataWithIEnumerator(string url) {
  29. var hotFixDataRequest = UnityWebRequest.Get(url);
  30. var webRequest = hotFixDataRequest.SendWebRequest();
  31. while (!webRequest.isDone)
  32. {
  33. yield return new WaitForSeconds(0.1f);
  34. }
  35. #if UNITY_2020_1_OR_NEWER
  36. if (hotFixDataRequest.result != UnityWebRequest.Result.Success)
  37. #elif UNITY_2017_2_OR_NEWER
  38. if (hotFixDataRequest.isNetworkError || hotFixDataRequest.isHttpError)
  39. #else
  40. if (hotFixDataRequest.isError)
  41. #endif
  42. {
  43. // Debug.Log("loadPluginData failed.");
  44. // callback(null);
  45. ATLog.log("load hotfix data failed.");
  46. }
  47. else
  48. {
  49. //解析热修复的数据
  50. try {
  51. string hotFixData = hotFixDataRequest.downloadHandler.text;
  52. var hotFixDataObj = JsonUtility.FromJson<HotfixPluginData>(hotFixData);
  53. ATLog.log("loadHotFixDataWithIEnumerator() >>> hotFixData: " + hotFixData);
  54. //判断status是否需要进行热更新
  55. if (hotFixDataObj.status != 1) {
  56. ATLog.log("loadHotFixDataWithIEnumerator() >>> 热更新被禁止");
  57. } else {
  58. var localHotFixDataObj = getHotfixPluginData();
  59. if (localHotFixDataObj == null) {
  60. //本地未曾下载过热更新
  61. ATLog.log("loadHotFixDataWithIEnumerator() >>> 本地未曾下载过热更新");
  62. ATEditorCoroutine.startCoroutine(loadHotFixPlugin(hotFixDataObj));
  63. } else {
  64. var compareVersionResult = ATDataUtil.CompareVersions(localHotFixDataObj.hot_fix_version, hotFixDataObj.hot_fix_version);
  65. ATLog.log("loadHotFixDataWithIEnumerator() >>> compareVersionResult: " + compareVersionResult);
  66. //本地版本比远端版本低,则需要更新
  67. if (compareVersionResult == VersionComparisonResult.Lesser) {
  68. ATEditorCoroutine.startCoroutine(loadHotFixPlugin(hotFixDataObj));
  69. } else {
  70. //不需要热更新
  71. saveHotfixData(hotFixData);
  72. }
  73. }
  74. }
  75. } catch(Exception e) {
  76. ATLog.logError("parseNetworksJson() >>> failed: " + e);
  77. }
  78. }
  79. }
  80. private IEnumerator loadHotFixPlugin(HotfixPluginData hotFixDataObj) {
  81. var path = Path.Combine(Application.temporaryCachePath, hotFixDataObj.file_name);
  82. ATLog.log("downloadPluginWithEnumerator() >>> path: " + path);
  83. #if UNITY_2017_2_OR_NEWER
  84. var downloadHandler = new DownloadHandlerFile(path);
  85. #else
  86. var downloadHandler = new ATDownloadHandler(path);
  87. #endif
  88. var downloadUrl = hotFixDataObj.download_url;
  89. UnityWebRequest downloadPluginRequest = new UnityWebRequest(downloadUrl)
  90. { method = UnityWebRequest.kHttpVerbGET,
  91. downloadHandler = downloadHandler
  92. };
  93. #if UNITY_2017_2_OR_NEWER
  94. var operation = downloadPluginRequest.SendWebRequest();
  95. #else
  96. var operation = downloadPluginRequest.Send();
  97. #endif
  98. while (!operation.isDone)
  99. {
  100. yield return new WaitForSeconds(0.1f); // Just wait till downloadPluginRequest is completed. Our coroutine is pretty rudimentary.
  101. if (operation.progress != 1 && operation.isDone)
  102. {
  103. }
  104. }
  105. #if UNITY_2020_1_OR_NEWER
  106. if (downloadPluginRequest.result != UnityWebRequest.Result.Success)
  107. #elif UNITY_2017_2_OR_NEWER
  108. if (downloadPluginRequest.isNetworkError || downloadPluginRequest.isHttpError)
  109. #else
  110. if (downloadPluginRequest.isError)
  111. #endif
  112. {
  113. ATLog.log(downloadPluginRequest.error);
  114. }
  115. else
  116. {
  117. AssetDatabase.ImportPackage(path, false);
  118. AssetDatabase.Refresh();
  119. string hotFixData = JsonUtility.ToJson(hotFixDataObj);
  120. saveHotfixData(hotFixData);
  121. }
  122. downloadPluginRequest.Dispose();
  123. downloadPluginRequest = null;
  124. }
  125. private void saveHotfixData(string hotfixPluginData) {
  126. var directoryPath = ATConfig.plugin_setting_data_path;
  127. // 确保目标文件夹存在
  128. if (!Directory.Exists(directoryPath))
  129. {
  130. // 如果目录不存在,则创建它
  131. Directory.CreateDirectory(directoryPath);
  132. }
  133. string fullPath = Path.Combine(directoryPath, plugin_hot_fix_data_file_name);
  134. ATLog.log("saveHotfixData() >>> fullPath: " + fullPath + " hotfixPluginData: " + hotfixPluginData);
  135. File.WriteAllText(fullPath, hotfixPluginData);
  136. }
  137. private HotfixPluginData getHotfixPluginData() {
  138. string fullPath = Path.Combine(ATConfig.plugin_setting_data_path, plugin_hot_fix_data_file_name);
  139. if (!File.Exists(fullPath)) {
  140. return null;
  141. }
  142. string json = File.ReadAllText(fullPath);
  143. if(json == "") {
  144. return null;
  145. }
  146. return JsonUtility.FromJson<HotfixPluginData>(json);
  147. }
  148. }
  149. }