| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- using System.IO;
- using UnityEngine;
- namespace YooAsset
- {
- /// <summary>
- /// 加载AssetBundle文件
- /// </summary>
- internal class DBFSLoadAssetBundleOperation : FSLoadBundleOperation
- {
- private enum ESteps
- {
- None,
- LoadAssetBundle,
- CheckResult,
- Done,
- }
- private readonly DefaultBuildinFileSystem _fileSystem;
- private readonly PackageBundle _bundle;
- private AssetBundleCreateRequest _createRequest;
- private AssetBundle _assetBundle;
- private Stream _managedStream;
- private ESteps _steps = ESteps.None;
- internal DBFSLoadAssetBundleOperation(DefaultBuildinFileSystem fileSystem, PackageBundle bundle)
- {
- _fileSystem = fileSystem;
- _bundle = bundle;
- }
- internal override void InternalStart()
- {
- DownloadProgress = 1f;
- DownloadedBytes = _bundle.FileSize;
- _steps = ESteps.LoadAssetBundle;
- }
- internal override void InternalUpdate()
- {
- if (_steps == ESteps.None || _steps == ESteps.Done)
- return;
- if (_steps == ESteps.LoadAssetBundle)
- {
- if (_bundle.Encrypted)
- {
- if (_fileSystem.DecryptionServices == null)
- {
- _steps = ESteps.Done;
- Status = EOperationStatus.Failed;
- Error = $"The {nameof(IDecryptionServices)} is null !";
- YooLogger.Error(Error);
- return;
- }
- }
- if (IsWaitForAsyncComplete)
- {
- if (_bundle.Encrypted)
- {
- var decryptResult = _fileSystem.LoadEncryptedAssetBundle(_bundle);
- _assetBundle = decryptResult.Result;
- _managedStream = decryptResult.ManagedStream;
- }
- else
- {
- string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle);
- _assetBundle = AssetBundle.LoadFromFile(filePath);
- }
- }
- else
- {
- if (_bundle.Encrypted)
- {
- var decryptResult = _fileSystem.LoadEncryptedAssetBundleAsync(_bundle);
- _createRequest = decryptResult.CreateRequest;
- _managedStream = decryptResult.ManagedStream;
- }
- else
- {
- string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle);
- _createRequest = AssetBundle.LoadFromFileAsync(filePath);
- }
- }
- _steps = ESteps.CheckResult;
- }
- if (_steps == ESteps.CheckResult)
- {
- if (_createRequest != null)
- {
- if (IsWaitForAsyncComplete)
- {
- // 强制挂起主线程(注意:该操作会很耗时)
- YooLogger.Warning("Suspend the main thread to load unity bundle.");
- _assetBundle = _createRequest.assetBundle;
- }
- else
- {
- if (_createRequest.isDone == false)
- return;
- _assetBundle = _createRequest.assetBundle;
- }
- }
- if (_assetBundle != null)
- {
- _steps = ESteps.Done;
- Result = new AssetBundleResult(_fileSystem, _bundle, _assetBundle, _managedStream);
- Status = EOperationStatus.Succeed;
- return;
- }
- if (_bundle.Encrypted)
- {
- _steps = ESteps.Done;
- Status = EOperationStatus.Failed;
- Error = $"Failed to load encrypted buildin asset bundle file : {_bundle.BundleName}";
- YooLogger.Error(Error);
- }
- else
- {
- _steps = ESteps.Done;
- Status = EOperationStatus.Failed;
- Error = $"Failed to load buildin asset bundle file : {_bundle.BundleName}";
- YooLogger.Error(Error);
- }
- }
- }
- internal override void InternalWaitForAsyncComplete()
- {
- while (true)
- {
- if (ExecuteWhileDone())
- {
- _steps = ESteps.Done;
- break;
- }
- }
- }
- }
- /// <summary>
- /// 加载原生文件
- /// </summary>
- internal class DBFSLoadRawBundleOperation : FSLoadBundleOperation
- {
- private enum ESteps
- {
- None,
- LoadBuildinRawBundle,
- Done,
- }
- private readonly DefaultBuildinFileSystem _fileSystem;
- private readonly PackageBundle _bundle;
- private ESteps _steps = ESteps.None;
- internal DBFSLoadRawBundleOperation(DefaultBuildinFileSystem fileSystem, PackageBundle bundle)
- {
- _fileSystem = fileSystem;
- _bundle = bundle;
- }
- internal override void InternalStart()
- {
- DownloadProgress = 1f;
- DownloadedBytes = _bundle.FileSize;
- _steps = ESteps.LoadBuildinRawBundle;
- }
- internal override void InternalUpdate()
- {
- if (_steps == ESteps.None || _steps == ESteps.Done)
- return;
- if (_steps == ESteps.LoadBuildinRawBundle)
- {
- #if UNITY_ANDROID
- //TODO : 安卓平台内置文件属于APK压缩包内的文件。
- _steps = ESteps.Done;
- Result = new RawBundleResult(_fileSystem, _bundle);
- Status = EOperationStatus.Succeed;
- #else
- string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle);
- if (File.Exists(filePath))
- {
- _steps = ESteps.Done;
- Result = new RawBundleResult(_fileSystem, _bundle);
- Status = EOperationStatus.Succeed;
- }
- else
- {
- _steps = ESteps.Done;
- Status = EOperationStatus.Failed;
- Error = $"Can not found buildin raw bundle file : {filePath}";
- YooLogger.Error(Error);
- }
- #endif
- }
- }
- internal override void InternalWaitForAsyncComplete()
- {
- while (true)
- {
- if (ExecuteWhileDone())
- {
- _steps = ESteps.Done;
- break;
- }
- }
- }
- }
- }
|