InitializationOperation.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. namespace YooAsset
  6. {
  7. /// <summary>
  8. /// 初始化操作
  9. /// </summary>
  10. public abstract class InitializationOperation : AsyncOperationBase
  11. {
  12. }
  13. /// <summary>
  14. /// 编辑器下模拟模式的初始化操作
  15. /// </summary>
  16. internal sealed class EditorSimulateModeInitializationOperation : InitializationOperation
  17. {
  18. private enum ESteps
  19. {
  20. None,
  21. Builder,
  22. Load,
  23. Done,
  24. }
  25. private readonly EditorSimulateModeImpl _impl;
  26. private string _simulatePatchManifestPath;
  27. private ESteps _steps = ESteps.None;
  28. internal EditorSimulateModeInitializationOperation(EditorSimulateModeImpl impl, string simulatePatchManifestPath)
  29. {
  30. _impl = impl;
  31. _simulatePatchManifestPath = simulatePatchManifestPath;
  32. }
  33. internal override void Start()
  34. {
  35. if (string.IsNullOrEmpty(_simulatePatchManifestPath))
  36. _steps = ESteps.Builder;
  37. else
  38. _steps = ESteps.Load;
  39. }
  40. internal override void Update()
  41. {
  42. if (_steps == ESteps.Builder)
  43. {
  44. _simulatePatchManifestPath = EditorSimulateModeHelper.SimulateBuild();
  45. if (string.IsNullOrEmpty(_simulatePatchManifestPath))
  46. {
  47. _steps = ESteps.Done;
  48. Status = EOperationStatus.Failed;
  49. Error = "Simulate build failed, see the detail info on the console window.";
  50. return;
  51. }
  52. _steps = ESteps.Load;
  53. }
  54. if (_steps == ESteps.Load)
  55. {
  56. if (File.Exists(_simulatePatchManifestPath) == false)
  57. {
  58. _steps = ESteps.Done;
  59. Status = EOperationStatus.Failed;
  60. Error = $"Manifest file not found : {_simulatePatchManifestPath}";
  61. return;
  62. }
  63. YooLogger.Log($"Load manifest file : {_simulatePatchManifestPath}");
  64. string jsonContent = FileUtility.ReadFile(_simulatePatchManifestPath);
  65. var simulatePatchManifest = PatchManifest.Deserialize(jsonContent);
  66. _impl.SetSimulatePatchManifest(simulatePatchManifest);
  67. _steps = ESteps.Done;
  68. Status = EOperationStatus.Succeed;
  69. }
  70. }
  71. }
  72. /// <summary>
  73. /// 离线运行模式的初始化操作
  74. /// </summary>
  75. internal sealed class OfflinePlayModeInitializationOperation : InitializationOperation
  76. {
  77. private enum ESteps
  78. {
  79. None,
  80. Update,
  81. Done,
  82. }
  83. private readonly OfflinePlayModeImpl _impl;
  84. private readonly AppManifestLoader _appManifestLoader = new AppManifestLoader();
  85. private ESteps _steps = ESteps.None;
  86. internal OfflinePlayModeInitializationOperation(OfflinePlayModeImpl impl)
  87. {
  88. _impl = impl;
  89. }
  90. internal override void Start()
  91. {
  92. _steps = ESteps.Update;
  93. }
  94. internal override void Update()
  95. {
  96. if (_steps == ESteps.None || _steps == ESteps.Done)
  97. return;
  98. if (_steps == ESteps.Update)
  99. {
  100. _appManifestLoader.Update();
  101. Progress = _appManifestLoader.Progress();
  102. if (_appManifestLoader.IsDone() == false)
  103. return;
  104. if (_appManifestLoader.Result == null)
  105. {
  106. _steps = ESteps.Done;
  107. Status = EOperationStatus.Failed;
  108. Error = _appManifestLoader.Error;
  109. }
  110. else
  111. {
  112. _steps = ESteps.Done;
  113. Status = EOperationStatus.Succeed;
  114. _impl.SetAppPatchManifest(_appManifestLoader.Result);
  115. }
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// 联机运行模式的初始化操作
  121. /// </summary>
  122. internal sealed class HostPlayModeInitializationOperation : InitializationOperation
  123. {
  124. private enum ESteps
  125. {
  126. None,
  127. InitCache,
  128. LoadManifest,
  129. CopyManifest,
  130. Done,
  131. }
  132. private readonly HostPlayModeImpl _impl;
  133. private readonly AppManifestLoader _appManifestLoader = new AppManifestLoader();
  134. private readonly AppManifestCopyer _appManifestCopyer = new AppManifestCopyer();
  135. private ESteps _steps = ESteps.None;
  136. internal HostPlayModeInitializationOperation(HostPlayModeImpl impl)
  137. {
  138. _impl = impl;
  139. }
  140. internal override void Start()
  141. {
  142. _steps = ESteps.InitCache;
  143. }
  144. internal override void Update()
  145. {
  146. if (_steps == ESteps.None || _steps == ESteps.Done)
  147. return;
  148. if (_steps == ESteps.InitCache)
  149. {
  150. // 每次启动时比对APP版本号是否一致
  151. CacheData cacheData = CacheData.LoadCache();
  152. if (cacheData.CacheAppVersion != Application.version)
  153. {
  154. YooLogger.Warning($"Cache is dirty ! Cache application version is {cacheData.CacheAppVersion}, Current application version is {Application.version}");
  155. // 注意:在覆盖安装的时候,会保留APP沙盒目录,可以选择清空缓存目录
  156. if (_impl.ClearCacheWhenDirty)
  157. {
  158. YooLogger.Warning("Clear cache files.");
  159. SandboxHelper.DeleteCacheFolder();
  160. }
  161. // 更新缓存文件
  162. CacheData.UpdateCache();
  163. }
  164. _steps = ESteps.LoadManifest;
  165. }
  166. if (_steps == ESteps.LoadManifest)
  167. {
  168. _appManifestLoader.Update();
  169. Progress = _appManifestLoader.Progress();
  170. if (_appManifestLoader.IsDone() == false)
  171. return;
  172. if (_appManifestLoader.Result == null)
  173. {
  174. _steps = ESteps.Done;
  175. Status = EOperationStatus.Failed;
  176. Error = _appManifestLoader.Error;
  177. }
  178. else
  179. {
  180. _impl.SetAppPatchManifest(_appManifestLoader.Result);
  181. _impl.SetLocalPatchManifest(_appManifestLoader.Result);
  182. _appManifestCopyer.Init(_appManifestLoader.StaticVersion);
  183. _steps = ESteps.CopyManifest;
  184. }
  185. }
  186. if (_steps == ESteps.CopyManifest)
  187. {
  188. _appManifestCopyer.Update();
  189. if (_appManifestCopyer.IsDone() == false)
  190. return;
  191. if (_appManifestCopyer.Result == false)
  192. {
  193. _steps = ESteps.Done;
  194. Status = EOperationStatus.Failed;
  195. Error = _appManifestCopyer.Error;
  196. }
  197. else
  198. {
  199. _steps = ESteps.Done;
  200. Status = EOperationStatus.Succeed;
  201. }
  202. }
  203. }
  204. }
  205. /// <summary>
  206. /// 内置补丁清单加载器
  207. /// </summary>
  208. internal class AppManifestLoader
  209. {
  210. private enum ESteps
  211. {
  212. LoadStaticVersion,
  213. CheckStaticVersion,
  214. LoadAppManifest,
  215. CheckAppManifest,
  216. Done,
  217. }
  218. private ESteps _steps = ESteps.LoadStaticVersion;
  219. private UnityWebDataRequester _downloader1;
  220. private UnityWebDataRequester _downloader2;
  221. /// <summary>
  222. /// 错误日志
  223. /// </summary>
  224. public string Error { private set; get; }
  225. /// <summary>
  226. /// 加载结果
  227. /// </summary>
  228. public PatchManifest Result { private set; get; }
  229. /// <summary>
  230. /// 内置补丁清单版本号
  231. /// </summary>
  232. public int StaticVersion { private set; get; }
  233. /// <summary>
  234. /// 是否已经完成
  235. /// </summary>
  236. public bool IsDone()
  237. {
  238. return _steps == ESteps.Done;
  239. }
  240. /// <summary>
  241. /// 加载进度
  242. /// </summary>
  243. public float Progress()
  244. {
  245. if (_downloader2 == null)
  246. return 0;
  247. return _downloader2.Progress();
  248. }
  249. /// <summary>
  250. /// 更新流程
  251. /// </summary>
  252. public void Update()
  253. {
  254. if (IsDone())
  255. return;
  256. if (_steps == ESteps.LoadStaticVersion)
  257. {
  258. YooLogger.Log($"Load application static version.");
  259. string filePath = PathHelper.MakeStreamingLoadPath(YooAssetSettings.VersionFileName);
  260. string url = PathHelper.ConvertToWWWPath(filePath);
  261. _downloader1 = new UnityWebDataRequester();
  262. _downloader1.SendRequest(url);
  263. _steps = ESteps.CheckStaticVersion;
  264. }
  265. if (_steps == ESteps.CheckStaticVersion)
  266. {
  267. if (_downloader1.IsDone() == false)
  268. return;
  269. if (_downloader1.HasError())
  270. {
  271. Error = _downloader1.GetError();
  272. _steps = ESteps.Done;
  273. }
  274. else
  275. {
  276. StaticVersion = int.Parse(_downloader1.GetText());
  277. _steps = ESteps.LoadAppManifest;
  278. }
  279. _downloader1.Dispose();
  280. }
  281. if (_steps == ESteps.LoadAppManifest)
  282. {
  283. YooLogger.Log($"Load application patch manifest.");
  284. string filePath = PathHelper.MakeStreamingLoadPath(YooAssetSettingsData.GetPatchManifestFileName(StaticVersion));
  285. string url = PathHelper.ConvertToWWWPath(filePath);
  286. _downloader2 = new UnityWebDataRequester();
  287. _downloader2.SendRequest(url);
  288. _steps = ESteps.CheckAppManifest;
  289. }
  290. if (_steps == ESteps.CheckAppManifest)
  291. {
  292. if (_downloader2.IsDone() == false)
  293. return;
  294. if (_downloader2.HasError())
  295. {
  296. Error = _downloader2.GetError();
  297. _steps = ESteps.Done;
  298. }
  299. else
  300. {
  301. // 解析APP里的补丁清单
  302. Result = PatchManifest.Deserialize(_downloader2.GetText());
  303. _steps = ESteps.Done;
  304. }
  305. _downloader2.Dispose();
  306. }
  307. }
  308. }
  309. /// <summary>
  310. /// 内置补丁清单复制器
  311. /// </summary>
  312. internal class AppManifestCopyer
  313. {
  314. private enum ESteps
  315. {
  316. CopyAppManifest,
  317. CheckAppManifest,
  318. Done,
  319. }
  320. private ESteps _steps = ESteps.CopyAppManifest;
  321. private UnityWebFileRequester _downloader1;
  322. private int _staticVersion;
  323. /// <summary>
  324. /// 错误日志
  325. /// </summary>
  326. public string Error { private set; get; }
  327. /// <summary>
  328. /// 拷贝结果
  329. /// </summary>
  330. public bool Result { private set; get; }
  331. /// <summary>
  332. /// 是否已经完成
  333. /// </summary>
  334. public bool IsDone()
  335. {
  336. return _steps == ESteps.Done;
  337. }
  338. /// <summary>
  339. /// 初始化流程
  340. /// </summary>
  341. public void Init(int staticVersion)
  342. {
  343. _staticVersion = staticVersion;
  344. }
  345. /// <summary>
  346. /// 更新流程
  347. /// </summary>
  348. public void Update()
  349. {
  350. if (IsDone())
  351. return;
  352. if (_steps == ESteps.CopyAppManifest)
  353. {
  354. string destFilePath = PathHelper.MakePersistentLoadPath(YooAssetSettingsData.GetPatchManifestFileName(_staticVersion));
  355. if (File.Exists(destFilePath))
  356. {
  357. Result = true;
  358. _steps = ESteps.Done;
  359. return;
  360. }
  361. else
  362. {
  363. YooLogger.Log($"Copy application patch manifest.");
  364. string sourceFilePath = PathHelper.MakeStreamingLoadPath(YooAssetSettingsData.GetPatchManifestFileName(_staticVersion));
  365. string url = PathHelper.ConvertToWWWPath(sourceFilePath);
  366. _downloader1 = new UnityWebFileRequester();
  367. _downloader1.SendRequest(url, destFilePath);
  368. _steps = ESteps.CheckAppManifest;
  369. }
  370. }
  371. if (_steps == ESteps.CheckAppManifest)
  372. {
  373. if (_downloader1.IsDone() == false)
  374. return;
  375. if (_downloader1.HasError())
  376. {
  377. Result = false;
  378. Error = _downloader1.GetError();
  379. _steps = ESteps.Done;
  380. }
  381. else
  382. {
  383. Result = true;
  384. _steps = ESteps.Done;
  385. }
  386. _downloader1.Dispose();
  387. }
  388. }
  389. }
  390. }