From a138701afe9fd7835294ccb94233419c11c16465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Thu, 13 Feb 2025 12:12:52 +0800 Subject: [PATCH] update extension sample MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 抖音小游戏支持资源加密 --- .../Operation/TTFSLoadBundleOperation.cs | 83 ++++-------- .../DownloadTiktokAssetBundleOperation.cs | 127 ++++++++++++++++++ ...DownloadTiktokAssetBundleOperation.cs.meta | 11 ++ .../Operation/WXFSLoadBundleOperation.cs | 2 +- 4 files changed, 168 insertions(+), 55 deletions(-) create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/TiktokFileSystem/Operation/internal/DownloadTiktokAssetBundleOperation.cs create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/TiktokFileSystem/Operation/internal/DownloadTiktokAssetBundleOperation.cs.meta diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/TiktokFileSystem/Operation/TTFSLoadBundleOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/TiktokFileSystem/Operation/TTFSLoadBundleOperation.cs index 470ba5a5..5e780de9 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/TiktokFileSystem/Operation/TTFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/TiktokFileSystem/Operation/TTFSLoadBundleOperation.cs @@ -2,21 +2,19 @@ using UnityEngine; using UnityEngine.Networking; using YooAsset; -using TTSDK; -using WeChatWASM; internal class TTFSLoadBundleOperation : FSLoadBundleOperation { private enum ESteps { None, - LoadBundleFile, + DownloadAssetBundle, Done, } private readonly TiktokFileSystem _fileSystem; private readonly PackageBundle _bundle; - private UnityWebRequest _webRequest; + private DownloadAssetBundleOperation _downloadAssetBundleOp; private ESteps _steps = ESteps.None; internal TTFSLoadBundleOperation(TiktokFileSystem fileSystem, PackageBundle bundle) @@ -26,51 +24,47 @@ internal class TTFSLoadBundleOperation : FSLoadBundleOperation } internal override void InternalOnStart() { - _steps = ESteps.LoadBundleFile; + _steps = ESteps.DownloadAssetBundle; } internal override void InternalOnUpdate() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.LoadBundleFile) + if (_steps == ESteps.DownloadAssetBundle) { - if (_webRequest == null) + if (_downloadAssetBundleOp == null) { - string mainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName); - _webRequest = TTAssetBundle.GetAssetBundle(mainURL); - _webRequest.SendWebRequest(); + DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60); + downloadParam.MainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName); ; + downloadParam.FallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(_bundle.FileName); + + if (_bundle.Encrypted) + { + _downloadAssetBundleOp = new DownloadWebEncryptAssetBundleOperation(_fileSystem.DecryptionServices, _bundle, downloadParam); + OperationSystem.StartOperation(_fileSystem.PackageName, _downloadAssetBundleOp); + } + else + { + _downloadAssetBundleOp = new DownloadTiktokAssetBundleOperation(_bundle, downloadParam); + OperationSystem.StartOperation(_fileSystem.PackageName, _downloadAssetBundleOp); + } } - DownloadProgress = _webRequest.downloadProgress; - DownloadedBytes = (long)_webRequest.downloadedBytes; + DownloadProgress = _downloadAssetBundleOp.DownloadProgress; + DownloadedBytes = (long)_downloadAssetBundleOp.DownloadedBytes; Progress = DownloadProgress; - if (_webRequest.isDone == false) + if (_downloadAssetBundleOp.IsDone == false) return; - if (CheckRequestResult()) + if (_downloadAssetBundleOp.Status == EOperationStatus.Succeed) { - if (_bundle.Encrypted && _fileSystem.DecryptionServices == null) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"The {nameof(IWebDecryptionServices)} is null !"; - YooLogger.Error(Error); - return; - } - - AssetBundle assetBundle; - var downloadHanlder = _webRequest.downloadHandler as DownloadHandlerTTAssetBundle; - if (_bundle.Encrypted) - assetBundle = _fileSystem.LoadEncryptedAssetBundle(_bundle, downloadHanlder.data); - else - assetBundle = downloadHanlder.assetBundle; - + var assetBundle = _downloadAssetBundleOp.Result; if (assetBundle == null) { _steps = ESteps.Done; - Error = $"{nameof(DownloadHandlerTTAssetBundle)} loaded asset bundle is null !"; Status = EOperationStatus.Failed; + Error = $"{nameof(DownloadAssetBundleOperation)} loaded asset bundle is null !"; } else { @@ -83,6 +77,7 @@ internal class TTFSLoadBundleOperation : FSLoadBundleOperation { _steps = ESteps.Done; Status = EOperationStatus.Failed; + Error = _downloadAssetBundleOp.Error; } } } @@ -98,31 +93,11 @@ internal class TTFSLoadBundleOperation : FSLoadBundleOperation } public override void AbortDownloadOperation() { - } - - private bool CheckRequestResult() - { -#if UNITY_2020_3_OR_NEWER - if (_webRequest.result != UnityWebRequest.Result.Success) + if (_steps == ESteps.DownloadAssetBundle) { - Error = _webRequest.error; - return false; + if (_downloadAssetBundleOp != null) + _downloadAssetBundleOp.SetAbort(); } - else - { - return true; - } -#else - if (_webRequest.isNetworkError || _webRequest.isHttpError) - { - Error = _webRequest.error; - return false; - } - else - { - return true; - } -#endif } } #endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/TiktokFileSystem/Operation/internal/DownloadTiktokAssetBundleOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/TiktokFileSystem/Operation/internal/DownloadTiktokAssetBundleOperation.cs new file mode 100644 index 00000000..4ec3dcee --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/TiktokFileSystem/Operation/internal/DownloadTiktokAssetBundleOperation.cs @@ -0,0 +1,127 @@ +#if UNITY_WEBGL && DOUYINMINIGAME +using UnityEngine; +using UnityEngine.Networking; +using TTSDK; + +namespace YooAsset +{ + internal class DownloadTiktokAssetBundleOperation : DownloadAssetBundleOperation + { + private ESteps _steps = ESteps.None; + + internal DownloadTiktokAssetBundleOperation(PackageBundle bundle, DownloadParam param) : base(bundle, param) + { + } + internal override void InternalOnStart() + { + _steps = ESteps.CreateRequest; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + // 创建下载器 + if (_steps == ESteps.CreateRequest) + { + // 获取请求地址 + _requestURL = GetRequestURL(); + + // 重置变量 + ResetRequestFiled(); + + // 创建下载器 + CreateWebRequest(); + + _steps = ESteps.CheckRequest; + } + + // 检测下载结果 + if (_steps == ESteps.CheckRequest) + { + DownloadProgress = _webRequest.downloadProgress; + DownloadedBytes = (long)_webRequest.downloadedBytes; + Progress = DownloadProgress; + if (_webRequest.isDone == false) + { + //TODO 需要验证抖音插件请求器的下载进度 + //CheckRequestTimeout(); + return; + } + + // 检查网络错误 + if (CheckRequestResult()) + { + var downloadHanlder = (DownloadHandlerTTAssetBundle)_webRequest.downloadHandler; + AssetBundle assetBundle = downloadHanlder.assetBundle; + if (assetBundle == null) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "Download handler asset bundle object is null !"; + } + else + { + _steps = ESteps.Done; + Result = assetBundle; + Status = EOperationStatus.Succeed; + + //TODO 需要验证抖音插件请求器的下载进度 + DownloadProgress = 1f; + DownloadedBytes = Bundle.FileSize; + Progress = 1f; + } + } + else + { + _steps = ESteps.TryAgain; + } + + // 注意:最终释放请求器 + DisposeWebRequest(); + } + + // 重新尝试下载 + if (_steps == ESteps.TryAgain) + { + if (FailedTryAgain <= 0) + { + Status = EOperationStatus.Failed; + _steps = ESteps.Done; + YooLogger.Error(Error); + return; + } + + _tryAgainTimer += Time.unscaledDeltaTime; + if (_tryAgainTimer > 1f) + { + FailedTryAgain--; + _steps = ESteps.CreateRequest; + YooLogger.Warning(Error); + } + } + } + internal override void InternalOnAbort() + { + _steps = ESteps.Done; + DisposeWebRequest(); + } + + private void CreateWebRequest() + { + _webRequest = TTAssetBundle.GetAssetBundle(_requestURL); + _webRequest.disposeDownloadHandlerOnDispose = true; + _webRequest.SendWebRequest(); + } + private void DisposeWebRequest() + { + if (_webRequest != null) + { + //注意:引擎底层会自动调用Abort方法 + _webRequest.Dispose(); + _webRequest = null; + } + } + } +} +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/TiktokFileSystem/Operation/internal/DownloadTiktokAssetBundleOperation.cs.meta b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/TiktokFileSystem/Operation/internal/DownloadTiktokAssetBundleOperation.cs.meta new file mode 100644 index 00000000..70d48bde --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/TiktokFileSystem/Operation/internal/DownloadTiktokAssetBundleOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fa5e1bc536118b14ba56f53930539e38 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSLoadBundleOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSLoadBundleOperation.cs index 26229347..864e9a84 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSLoadBundleOperation.cs @@ -51,7 +51,7 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation DownloadProgress = _downloadAssetBundleOp.DownloadProgress; DownloadedBytes = (long)_downloadAssetBundleOp.DownloadedBytes; - Progress = _downloadAssetBundleOp.DownloadProgress; ; + Progress = DownloadProgress; if (_downloadAssetBundleOp.IsDone == false) return;