DCFSLoadBundleOperation.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. namespace YooAsset
  5. {
  6. internal class DCFSLoadAssetBundleOperation : FSLoadBundleOperation
  7. {
  8. protected enum ESteps
  9. {
  10. None,
  11. CheckExist,
  12. DownloadFile,
  13. LoadAssetBundle,
  14. CheckResult,
  15. Done,
  16. }
  17. protected readonly DefaultCacheFileSystem _fileSystem;
  18. protected readonly PackageBundle _bundle;
  19. protected FSDownloadFileOperation _downloadFileOp;
  20. protected AssetBundleCreateRequest _createRequest;
  21. private AssetBundle _assetBundle;
  22. private Stream _managedStream;
  23. protected ESteps _steps = ESteps.None;
  24. internal DCFSLoadAssetBundleOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle)
  25. {
  26. _fileSystem = fileSystem;
  27. _bundle = bundle;
  28. }
  29. internal override void InternalStart()
  30. {
  31. _steps = ESteps.CheckExist;
  32. }
  33. internal override void InternalUpdate()
  34. {
  35. if (_steps == ESteps.None || _steps == ESteps.Done)
  36. return;
  37. if (_steps == ESteps.CheckExist)
  38. {
  39. if (_fileSystem.Exists(_bundle))
  40. {
  41. DownloadProgress = 1f;
  42. DownloadedBytes = _bundle.FileSize;
  43. _steps = ESteps.LoadAssetBundle;
  44. }
  45. else
  46. {
  47. _steps = ESteps.DownloadFile;
  48. }
  49. }
  50. if (_steps == ESteps.DownloadFile)
  51. {
  52. // 注意:边玩边下下载器引用计数没有Release
  53. if (_downloadFileOp == null)
  54. {
  55. DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60);
  56. _downloadFileOp = _fileSystem.DownloadFileAsync(_bundle, downloadParam);
  57. _downloadFileOp.StartOperation();
  58. AddChildOperation(_downloadFileOp);
  59. }
  60. if (IsWaitForAsyncComplete)
  61. _downloadFileOp.WaitForAsyncComplete();
  62. _downloadFileOp.UpdateOperation();
  63. DownloadProgress = _downloadFileOp.DownloadProgress;
  64. DownloadedBytes = _downloadFileOp.DownloadedBytes;
  65. if (_downloadFileOp.IsDone == false)
  66. return;
  67. if (_downloadFileOp.Status == EOperationStatus.Succeed)
  68. {
  69. _steps = ESteps.LoadAssetBundle;
  70. }
  71. else
  72. {
  73. _steps = ESteps.Done;
  74. Status = EOperationStatus.Failed;
  75. Error = _downloadFileOp.Error;
  76. }
  77. }
  78. if (_steps == ESteps.LoadAssetBundle)
  79. {
  80. if (_bundle.Encrypted)
  81. {
  82. if (_fileSystem.DecryptionServices == null)
  83. {
  84. _steps = ESteps.Done;
  85. Status = EOperationStatus.Failed;
  86. Error = $"The {nameof(IDecryptionServices)} is null !";
  87. YooLogger.Error(Error);
  88. return;
  89. }
  90. }
  91. if (IsWaitForAsyncComplete)
  92. {
  93. if (_bundle.Encrypted)
  94. {
  95. var decryptResult = _fileSystem.LoadEncryptedAssetBundle(_bundle);
  96. _assetBundle = decryptResult.Result;
  97. _managedStream = decryptResult.ManagedStream;
  98. }
  99. else
  100. {
  101. string filePath = _fileSystem.GetCacheBundleFileLoadPath(_bundle);
  102. _assetBundle = AssetBundle.LoadFromFile(filePath);
  103. }
  104. }
  105. else
  106. {
  107. if (_bundle.Encrypted)
  108. {
  109. var decryptResult = _fileSystem.LoadEncryptedAssetBundleAsync(_bundle);
  110. _createRequest = decryptResult.CreateRequest;
  111. _managedStream = decryptResult.ManagedStream;
  112. }
  113. else
  114. {
  115. string filePath = _fileSystem.GetCacheBundleFileLoadPath(_bundle);
  116. _createRequest = AssetBundle.LoadFromFileAsync(filePath);
  117. }
  118. }
  119. _steps = ESteps.CheckResult;
  120. }
  121. if (_steps == ESteps.CheckResult)
  122. {
  123. if (_createRequest != null)
  124. {
  125. if (IsWaitForAsyncComplete)
  126. {
  127. // 强制挂起主线程(注意:该操作会很耗时)
  128. YooLogger.Warning("Suspend the main thread to load unity bundle.");
  129. _assetBundle = _createRequest.assetBundle;
  130. }
  131. else
  132. {
  133. if (_createRequest.isDone == false)
  134. return;
  135. _assetBundle = _createRequest.assetBundle;
  136. }
  137. }
  138. if (_assetBundle != null)
  139. {
  140. _steps = ESteps.Done;
  141. Result = new AssetBundleResult(_fileSystem, _bundle, _assetBundle, _managedStream);
  142. Status = EOperationStatus.Succeed;
  143. return;
  144. }
  145. // 注意:当缓存文件的校验等级为Low的时候,并不能保证缓存文件的完整性。
  146. // 说明:在AssetBundle文件加载失败的情况下,我们需要重新验证文件的完整性!
  147. EFileVerifyResult verifyResult = _fileSystem.VerifyCacheFile(_bundle);
  148. if (verifyResult == EFileVerifyResult.Succeed)
  149. {
  150. if (_bundle.Encrypted)
  151. {
  152. _steps = ESteps.Done;
  153. Status = EOperationStatus.Failed;
  154. Error = $"Failed to load encrypted asset bundle file : {_bundle.BundleName}";
  155. YooLogger.Error(Error);
  156. return;
  157. }
  158. // 注意:在安卓移动平台,华为和三星真机上有极小概率加载资源包失败。
  159. // 说明:大多数情况在首次安装下载资源到沙盒内,游戏过程中切换到后台再回到游戏内有很大概率触发!
  160. string filePath = _fileSystem.GetCacheBundleFileLoadPath(_bundle);
  161. byte[] fileData = FileUtility.ReadAllBytes(filePath);
  162. if (fileData != null && fileData.Length > 0)
  163. {
  164. _assetBundle = AssetBundle.LoadFromMemory(fileData);
  165. if (_assetBundle == null)
  166. {
  167. _steps = ESteps.Done;
  168. Status = EOperationStatus.Failed;
  169. Error = $"Failed to load asset bundle from memory : {_bundle.BundleName}";
  170. YooLogger.Error(Error);
  171. }
  172. else
  173. {
  174. _steps = ESteps.Done;
  175. Result = new AssetBundleResult(_fileSystem, _bundle, _assetBundle, null);
  176. Status = EOperationStatus.Succeed;
  177. }
  178. }
  179. else
  180. {
  181. _steps = ESteps.Done;
  182. Status = EOperationStatus.Failed;
  183. Error = $"Failed to read asset bundle file bytes : {_bundle.BundleName}";
  184. YooLogger.Error(Error);
  185. }
  186. }
  187. else
  188. {
  189. _steps = ESteps.Done;
  190. _fileSystem.DeleteCacheBundleFile(_bundle.BundleGUID);
  191. Status = EOperationStatus.Failed;
  192. Error = $"Find corrupted asset bundle file and delete : {_bundle.BundleName}";
  193. YooLogger.Error(Error);
  194. }
  195. }
  196. }
  197. internal override void InternalWaitForAsyncComplete()
  198. {
  199. while (true)
  200. {
  201. if (ExecuteWhileDone())
  202. {
  203. if (_downloadFileOp != null && _downloadFileOp.Status == EOperationStatus.Failed)
  204. YooLogger.Error($"Try load bundle {_bundle.BundleName} from remote !");
  205. _steps = ESteps.Done;
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. internal class DCFSLoadRawBundleOperation : FSLoadBundleOperation
  212. {
  213. protected enum ESteps
  214. {
  215. None,
  216. CheckExist,
  217. DownloadFile,
  218. LoadCacheRawBundle,
  219. Done,
  220. }
  221. protected readonly DefaultCacheFileSystem _fileSystem;
  222. protected readonly PackageBundle _bundle;
  223. protected FSDownloadFileOperation _downloadFileOp;
  224. protected ESteps _steps = ESteps.None;
  225. internal DCFSLoadRawBundleOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle)
  226. {
  227. _fileSystem = fileSystem;
  228. _bundle = bundle;
  229. }
  230. internal override void InternalStart()
  231. {
  232. _steps = ESteps.CheckExist;
  233. }
  234. internal override void InternalUpdate()
  235. {
  236. if (_steps == ESteps.None || _steps == ESteps.Done)
  237. return;
  238. if (_steps == ESteps.CheckExist)
  239. {
  240. if (_fileSystem.Exists(_bundle))
  241. {
  242. // 注意:缓存的原生文件的格式,可能会在业务端根据需求发生变动!
  243. // 注意:这里需要校验文件格式,如果不一致对本地文件进行修正!
  244. string filePath = _fileSystem.GetCacheBundleFileLoadPath(_bundle);
  245. if (File.Exists(filePath) == false)
  246. {
  247. try
  248. {
  249. var recordFileElement = _fileSystem.GetRecordFileElement(_bundle);
  250. File.Move(recordFileElement.DataFilePath, filePath);
  251. _steps = ESteps.LoadCacheRawBundle;
  252. }
  253. catch (Exception e)
  254. {
  255. _steps = ESteps.Done;
  256. Status = EOperationStatus.Failed;
  257. Error = $"Faild rename raw data file : {e.Message}";
  258. }
  259. }
  260. else
  261. {
  262. DownloadProgress = 1f;
  263. DownloadedBytes = _bundle.FileSize;
  264. _steps = ESteps.LoadCacheRawBundle;
  265. }
  266. }
  267. else
  268. {
  269. _steps = ESteps.DownloadFile;
  270. }
  271. }
  272. if (_steps == ESteps.DownloadFile)
  273. {
  274. // 注意:边玩边下下载器引用计数没有Release
  275. if (_downloadFileOp == null)
  276. {
  277. DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60);
  278. _downloadFileOp = _fileSystem.DownloadFileAsync(_bundle, downloadParam);
  279. _downloadFileOp.StartOperation();
  280. AddChildOperation(_downloadFileOp);
  281. }
  282. if (IsWaitForAsyncComplete)
  283. _downloadFileOp.WaitForAsyncComplete();
  284. _downloadFileOp.UpdateOperation();
  285. DownloadProgress = _downloadFileOp.DownloadProgress;
  286. DownloadedBytes = _downloadFileOp.DownloadedBytes;
  287. if (_downloadFileOp.IsDone == false)
  288. return;
  289. if (_downloadFileOp.Status == EOperationStatus.Succeed)
  290. {
  291. _steps = ESteps.LoadCacheRawBundle;
  292. }
  293. else
  294. {
  295. _steps = ESteps.Done;
  296. Status = EOperationStatus.Failed;
  297. Error = _downloadFileOp.Error;
  298. }
  299. }
  300. if (_steps == ESteps.LoadCacheRawBundle)
  301. {
  302. string filePath = _fileSystem.GetCacheBundleFileLoadPath(_bundle);
  303. if (File.Exists(filePath))
  304. {
  305. _steps = ESteps.Done;
  306. Result = new RawBundleResult(_fileSystem, _bundle);
  307. Status = EOperationStatus.Succeed;
  308. }
  309. else
  310. {
  311. _steps = ESteps.Done;
  312. Status = EOperationStatus.Failed;
  313. Error = $"Can not found cache raw bundle file : {filePath}";
  314. YooLogger.Error(Error);
  315. }
  316. }
  317. }
  318. internal override void InternalWaitForAsyncComplete()
  319. {
  320. while (true)
  321. {
  322. if (ExecuteWhileDone())
  323. {
  324. //TODO 拷贝本地文件失败也会触发该错误!
  325. if (_downloadFileOp != null && _downloadFileOp.Status == EOperationStatus.Failed)
  326. YooLogger.Error($"Try load bundle {_bundle.BundleName} from remote !");
  327. _steps = ESteps.Done;
  328. break;
  329. }
  330. }
  331. }
  332. }
  333. }