DBFSLoadBundleOperation.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System.IO;
  2. using UnityEngine;
  3. namespace YooAsset
  4. {
  5. /// <summary>
  6. /// 加载AssetBundle文件
  7. /// </summary>
  8. internal class DBFSLoadAssetBundleOperation : FSLoadBundleOperation
  9. {
  10. private enum ESteps
  11. {
  12. None,
  13. LoadAssetBundle,
  14. CheckResult,
  15. Done,
  16. }
  17. private readonly DefaultBuildinFileSystem _fileSystem;
  18. private readonly PackageBundle _bundle;
  19. private AssetBundleCreateRequest _createRequest;
  20. private AssetBundle _assetBundle;
  21. private Stream _managedStream;
  22. private ESteps _steps = ESteps.None;
  23. internal DBFSLoadAssetBundleOperation(DefaultBuildinFileSystem fileSystem, PackageBundle bundle)
  24. {
  25. _fileSystem = fileSystem;
  26. _bundle = bundle;
  27. }
  28. internal override void InternalStart()
  29. {
  30. DownloadProgress = 1f;
  31. DownloadedBytes = _bundle.FileSize;
  32. _steps = ESteps.LoadAssetBundle;
  33. }
  34. internal override void InternalUpdate()
  35. {
  36. if (_steps == ESteps.None || _steps == ESteps.Done)
  37. return;
  38. if (_steps == ESteps.LoadAssetBundle)
  39. {
  40. if (_bundle.Encrypted)
  41. {
  42. if (_fileSystem.DecryptionServices == null)
  43. {
  44. _steps = ESteps.Done;
  45. Status = EOperationStatus.Failed;
  46. Error = $"The {nameof(IDecryptionServices)} is null !";
  47. YooLogger.Error(Error);
  48. return;
  49. }
  50. }
  51. if (IsWaitForAsyncComplete)
  52. {
  53. if (_bundle.Encrypted)
  54. {
  55. var decryptResult = _fileSystem.LoadEncryptedAssetBundle(_bundle);
  56. _assetBundle = decryptResult.Result;
  57. _managedStream = decryptResult.ManagedStream;
  58. }
  59. else
  60. {
  61. string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle);
  62. _assetBundle = AssetBundle.LoadFromFile(filePath);
  63. }
  64. }
  65. else
  66. {
  67. if (_bundle.Encrypted)
  68. {
  69. var decryptResult = _fileSystem.LoadEncryptedAssetBundleAsync(_bundle);
  70. _createRequest = decryptResult.CreateRequest;
  71. _managedStream = decryptResult.ManagedStream;
  72. }
  73. else
  74. {
  75. string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle);
  76. _createRequest = AssetBundle.LoadFromFileAsync(filePath);
  77. }
  78. }
  79. _steps = ESteps.CheckResult;
  80. }
  81. if (_steps == ESteps.CheckResult)
  82. {
  83. if (_createRequest != null)
  84. {
  85. if (IsWaitForAsyncComplete)
  86. {
  87. // 强制挂起主线程(注意:该操作会很耗时)
  88. YooLogger.Warning("Suspend the main thread to load unity bundle.");
  89. _assetBundle = _createRequest.assetBundle;
  90. }
  91. else
  92. {
  93. if (_createRequest.isDone == false)
  94. return;
  95. _assetBundle = _createRequest.assetBundle;
  96. }
  97. }
  98. if (_assetBundle != null)
  99. {
  100. _steps = ESteps.Done;
  101. Result = new AssetBundleResult(_fileSystem, _bundle, _assetBundle, _managedStream);
  102. Status = EOperationStatus.Succeed;
  103. return;
  104. }
  105. if (_bundle.Encrypted)
  106. {
  107. _steps = ESteps.Done;
  108. Status = EOperationStatus.Failed;
  109. Error = $"Failed to load encrypted buildin asset bundle file : {_bundle.BundleName}";
  110. YooLogger.Error(Error);
  111. }
  112. else
  113. {
  114. _steps = ESteps.Done;
  115. Status = EOperationStatus.Failed;
  116. Error = $"Failed to load buildin asset bundle file : {_bundle.BundleName}";
  117. YooLogger.Error(Error);
  118. }
  119. }
  120. }
  121. internal override void InternalWaitForAsyncComplete()
  122. {
  123. while (true)
  124. {
  125. if (ExecuteWhileDone())
  126. {
  127. _steps = ESteps.Done;
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// 加载原生文件
  135. /// </summary>
  136. internal class DBFSLoadRawBundleOperation : FSLoadBundleOperation
  137. {
  138. private enum ESteps
  139. {
  140. None,
  141. LoadBuildinRawBundle,
  142. Done,
  143. }
  144. private readonly DefaultBuildinFileSystem _fileSystem;
  145. private readonly PackageBundle _bundle;
  146. private ESteps _steps = ESteps.None;
  147. internal DBFSLoadRawBundleOperation(DefaultBuildinFileSystem fileSystem, PackageBundle bundle)
  148. {
  149. _fileSystem = fileSystem;
  150. _bundle = bundle;
  151. }
  152. internal override void InternalStart()
  153. {
  154. DownloadProgress = 1f;
  155. DownloadedBytes = _bundle.FileSize;
  156. _steps = ESteps.LoadBuildinRawBundle;
  157. }
  158. internal override void InternalUpdate()
  159. {
  160. if (_steps == ESteps.None || _steps == ESteps.Done)
  161. return;
  162. if (_steps == ESteps.LoadBuildinRawBundle)
  163. {
  164. #if UNITY_ANDROID
  165. //TODO : 安卓平台内置文件属于APK压缩包内的文件。
  166. _steps = ESteps.Done;
  167. Result = new RawBundleResult(_fileSystem, _bundle);
  168. Status = EOperationStatus.Succeed;
  169. #else
  170. string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle);
  171. if (File.Exists(filePath))
  172. {
  173. _steps = ESteps.Done;
  174. Result = new RawBundleResult(_fileSystem, _bundle);
  175. Status = EOperationStatus.Succeed;
  176. }
  177. else
  178. {
  179. _steps = ESteps.Done;
  180. Status = EOperationStatus.Failed;
  181. Error = $"Can not found buildin raw bundle file : {filePath}";
  182. YooLogger.Error(Error);
  183. }
  184. #endif
  185. }
  186. }
  187. internal override void InternalWaitForAsyncComplete()
  188. {
  189. while (true)
  190. {
  191. if (ExecuteWhileDone())
  192. {
  193. _steps = ESteps.Done;
  194. break;
  195. }
  196. }
  197. }
  198. }
  199. }