From a02b3fbf6ee057a338161dc9f6a4f0ea5665e6c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Fri, 7 Feb 2025 15:30:53 +0800 Subject: [PATCH] update extension sample MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化了小游戏文件系统的缓存机制。 --- .../ByteGameFileSystem/ByteGameFileSystem.cs | 88 ++++++++++++++++--- .../Operation/BGFSInitializeOperation.cs | 37 +++++++- .../Operation/BGFSLoadBundleOperation.cs | 2 +- .../RecordByteGameCacheFilesOperation.cs | 57 ++++++++++++ .../RecordByteGameCacheFilesOperation.cs.meta | 11 +++ .../WXFSClearAllBundleFilesOperation.cs | 17 +++- .../WXFSClearUnusedBundleFilesAsync.cs | 49 +++-------- .../Operation/WXFSDownloadFileOperation.cs | 1 + .../Operation/WXFSInitializeOperation.cs | 37 +++++++- .../Operation/WXFSLoadBundleOperation.cs | 1 + .../RecordWechatCacheFilesOperation.cs | 58 ++++++++++++ .../RecordWechatCacheFilesOperation.cs.meta | 11 +++ .../WechatFileSystem/WechatFileSystem.cs | 79 +++++++++++++---- .../Runtime/YooAsset.RuntimeExtension.asmdef | 2 +- 14 files changed, 374 insertions(+), 76 deletions(-) create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/internal/RecordByteGameCacheFilesOperation.cs create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/internal/RecordByteGameCacheFilesOperation.cs.meta create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/internal/RecordWechatCacheFilesOperation.cs create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/internal/RecordWechatCacheFilesOperation.cs.meta diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/ByteGameFileSystem.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/ByteGameFileSystem.cs index 9d9e4687..1501c731 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/ByteGameFileSystem.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/ByteGameFileSystem.cs @@ -2,14 +2,17 @@ using System.Collections.Generic; using UnityEngine; using YooAsset; -using StarkSDKSpace; +using TTSDK; +using System.Linq; +using WeChatWASM; +using System; public static class ByteGameFileSystemCreater { - public static FileSystemParameters CreateByteGameFileSystemParameters(IRemoteServices remoteServices) + public static FileSystemParameters CreateByteGameFileSystemParameters(IRemoteServices remoteServices, string packageRoot) { string fileSystemClass = $"{nameof(ByteGameFileSystem)},YooAsset.RuntimeExtension"; - var fileSystemParams = new FileSystemParameters(fileSystemClass, null); + var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot); fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices); return fileSystemParams; } @@ -51,8 +54,10 @@ internal class ByteGameFileSystem : IFileSystem } } + private readonly HashSet _recorders = new HashSet(); private readonly Dictionary _cacheFilePaths = new Dictionary(10000); - private StarkFileSystemManager _fileSystemManager; + private TTFileSystemManager _fileSystemMgr; + private string _ttCacheRoot = string.Empty; /// /// 包裹名称 @@ -66,7 +71,7 @@ internal class ByteGameFileSystem : IFileSystem { get { - return string.Empty; + return _ttCacheRoot; } } @@ -77,7 +82,7 @@ internal class ByteGameFileSystem : IFileSystem { get { - return 0; + return _recorders.Count; } } @@ -155,6 +160,12 @@ internal class ByteGameFileSystem : IFileSystem public virtual void OnCreate(string packageName, string rootDirectory) { PackageName = packageName; + _ttCacheRoot = rootDirectory; + + if (string.IsNullOrEmpty(_ttCacheRoot)) + { + throw new System.Exception("请配置抖音小游戏的缓存根目录!"); + } // 注意:CDN服务未启用的情况下,使用抖音WEB服务器 if (RemoteServices == null) @@ -163,7 +174,7 @@ internal class ByteGameFileSystem : IFileSystem RemoteServices = new WebRemoteServices(webRoot); } - _fileSystemManager = StarkSDK.API.GetStarkFileSystemManager(); + _fileSystemMgr = TT.GetFileSystemManager(); } public virtual void OnUpdate() { @@ -176,7 +187,7 @@ internal class ByteGameFileSystem : IFileSystem public virtual bool Exists(PackageBundle bundle) { string filePath = GetCacheFileLoadPath(bundle); - return _fileSystemManager.AccessSync(filePath); + return _recorders.Contains(filePath); } public virtual bool NeedDownload(PackageBundle bundle) { @@ -196,27 +207,80 @@ internal class ByteGameFileSystem : IFileSystem public virtual string GetBundleFilePath(PackageBundle bundle) { - throw new System.NotImplementedException(); + return GetCacheFileLoadPath(bundle); } public virtual byte[] ReadBundleFileData(PackageBundle bundle) { - throw new System.NotImplementedException(); + string filePath = GetCacheFileLoadPath(bundle); + if (CheckCacheFileExist(filePath)) + return _fileSystemMgr.ReadFileSync(filePath); + else + return Array.Empty(); } public virtual string ReadBundleFileText(PackageBundle bundle) { - throw new System.NotImplementedException(); + string filePath = GetCacheFileLoadPath(bundle); + if (CheckCacheFileExist(filePath)) + return _fileSystemMgr.ReadFileSync(filePath, "utf8"); + else + return string.Empty; } #region 内部方法 + public TTFileSystemManager GetFileSystemMgr() + { + return _fileSystemMgr; + } + public bool CheckCacheFileExist(string filePath) + { + return _fileSystemMgr.AccessSync(filePath); + } private string GetCacheFileLoadPath(PackageBundle bundle) { if (_cacheFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false) { - filePath = _fileSystemManager.GetLocalCachedPathForUrl(bundle.FileName); + filePath = _fileSystemMgr.GetLocalCachedPathForUrl(bundle.FileName); _cacheFilePaths.Add(bundle.BundleGUID, filePath); } return filePath; } #endregion + + #region 本地记录 + public List GetAllRecords() + { + return _recorders.ToList(); + } + public bool RecordBundleFile(string filePath) + { + if (_recorders.Contains(filePath)) + { + YooLogger.Error($"{nameof(WechatFileSystem)} has element : {filePath}"); + return false; + } + + _recorders.Add(filePath); + return true; + } + public void TryRecordBundle(PackageBundle bundle) + { + string filePath = GetCacheFileLoadPath(bundle); + if (_recorders.Contains(filePath) == false) + { + _recorders.Add(filePath); + } + } + public void ClearAllRecords() + { + _recorders.Clear(); + } + public void ClearRecord(string filePath) + { + if (_recorders.Contains(filePath)) + { + _recorders.Remove(filePath); + } + } + #endregion } #endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/BGFSInitializeOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/BGFSInitializeOperation.cs index 0853e649..9af003d1 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/BGFSInitializeOperation.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/BGFSInitializeOperation.cs @@ -3,7 +3,16 @@ using YooAsset; internal partial class BGFSInitializeOperation : FSInitializeFileSystemOperation { + private enum ESteps + { + None, + RecordCacheFiles, + Done, + } + private readonly ByteGameFileSystem _fileSystem; + private RecordTiktokCacheFilesOperation _recordTiktokCacheFilesOp; + private ESteps _steps = ESteps.None; public BGFSInitializeOperation(ByteGameFileSystem fileSystem) { @@ -11,10 +20,36 @@ internal partial class BGFSInitializeOperation : FSInitializeFileSystemOperation } internal override void InternalOnStart() { - Status = EOperationStatus.Succeed; + _steps = ESteps.RecordCacheFiles; } internal override void InternalOnUpdate() { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.RecordCacheFiles) + { + if (_recordTiktokCacheFilesOp == null) + { + _recordTiktokCacheFilesOp = new RecordTiktokCacheFilesOperation(_fileSystem); + OperationSystem.StartOperation(_fileSystem.PackageName, _recordTiktokCacheFilesOp); + } + + if (_recordTiktokCacheFilesOp.IsDone == false) + return; + + if (_recordTiktokCacheFilesOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _recordTiktokCacheFilesOp.Error; + } + } } } #endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/BGFSLoadBundleOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/BGFSLoadBundleOperation.cs index 6ac562cd..3d03047a 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/BGFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/BGFSLoadBundleOperation.cs @@ -2,7 +2,7 @@ using UnityEngine; using UnityEngine.Networking; using YooAsset; -using StarkSDKSpace; +using TTSDK; internal class BGFSLoadBundleOperation : FSLoadBundleOperation { diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/internal/RecordByteGameCacheFilesOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/internal/RecordByteGameCacheFilesOperation.cs new file mode 100644 index 00000000..ec691dbf --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/internal/RecordByteGameCacheFilesOperation.cs @@ -0,0 +1,57 @@ +#if UNITY_WEBGL && BYTEMINIGAME +using YooAsset; +using TTSDK; + +internal class RecordTiktokCacheFilesOperation : AsyncOperationBase +{ + private enum ESteps + { + None, + RecordCacheFiles, + WaitResponse, + Done, + } + + private readonly ByteGameFileSystem _fileSystem; + private ESteps _steps = ESteps.None; + + public RecordTiktokCacheFilesOperation(ByteGameFileSystem fileSystem) + { + _fileSystem = fileSystem; + } + internal override void InternalOnStart() + { + _steps = ESteps.RecordCacheFiles; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.RecordCacheFiles) + { + _steps = ESteps.WaitResponse; + + var fileSystemMgr = _fileSystem.GetFileSystemMgr(); + var getSavedFileListParam = new GetSavedFileListParam(); + getSavedFileListParam.success = (TTGetSavedFileListResponse response) => + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + foreach (var fileInfo in response.fileList) + { + //TODO 需要确认存储文件为Bundle文件 + _fileSystem.RecordBundleFile(fileInfo.filePath); + } + }; + getSavedFileListParam.fail = (TTGetSavedFileListResponse response) => + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = response.errMsg; + }; + fileSystemMgr.GetSavedFileList(getSavedFileListParam); + } + } +} +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/internal/RecordByteGameCacheFilesOperation.cs.meta b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/internal/RecordByteGameCacheFilesOperation.cs.meta new file mode 100644 index 00000000..5ec7d443 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/ByteGameFileSystem/Operation/internal/RecordByteGameCacheFilesOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e4420bd73f37dec468a9b23425be68f2 +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/WXFSClearAllBundleFilesOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSClearAllBundleFilesOperation.cs index 1b042b46..a04fbdb4 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSClearAllBundleFilesOperation.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSClearAllBundleFilesOperation.cs @@ -11,13 +11,13 @@ internal class WXFSClearAllBundleFilesOperation : FSClearCacheFilesOperation { None, ClearAllCacheFiles, + WaitResult, Done, } private readonly WechatFileSystem _fileSystem; private ESteps _steps = ESteps.None; - internal WXFSClearAllBundleFilesOperation(WechatFileSystem fileSystem) { _fileSystem = fileSystem; @@ -33,16 +33,25 @@ internal class WXFSClearAllBundleFilesOperation : FSClearCacheFilesOperation if (_steps == ESteps.ClearAllCacheFiles) { + _steps = ESteps.WaitResult; + WX.CleanAllFileCache((bool isOk) => { if (isOk) + { YooLogger.Log("微信缓存清理成功!"); + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + _fileSystem.ClearAllRecords(); + } else + { YooLogger.Log("微信缓存清理失败!"); + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "微信缓存清理失败!"; + } }); - - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; } } } diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs index 03eef01d..42a1de75 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs @@ -5,14 +5,11 @@ using UnityEngine; using YooAsset; using WeChatWASM; - internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation { private enum ESteps { None, - GetAllCacheFiles, - WaitResult, GetUnusedCacheFiles, ClearUnusedCacheFiles, Done, @@ -22,7 +19,6 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation private readonly PackageManifest _manifest; private List _unusedCacheFiles; private int _unusedFileTotalCount = 0; - private GetSavedFileListSuccessCallbackResult _result; private ESteps _steps = ESteps.None; internal WXFSClearUnusedBundleFilesAsync(WechatFileSystem fileSystem, PackageManifest manifest) @@ -32,38 +28,13 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation } internal override void InternalOnStart() { - _steps = ESteps.GetAllCacheFiles; + _steps = ESteps.GetUnusedCacheFiles; } internal override void InternalOnUpdate() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.GetAllCacheFiles) - { - _steps = ESteps.WaitResult; - - var fileSystemMgr = WX.GetFileSystemManager(); - var option = new GetSavedFileListOption(); - fileSystemMgr.GetSavedFileList(option); - option.fail += (FileError error) => - { - _steps = ESteps.Done; - Error = error.errMsg; - Status = EOperationStatus.Failed; - }; - option.success += (GetSavedFileListSuccessCallbackResult result) => - { - _result = result; - _steps = ESteps.GetUnusedCacheFiles; - }; - } - - if (_steps == ESteps.WaitResult) - { - return; - } - if (_steps == ESteps.GetUnusedCacheFiles) { _unusedCacheFiles = GetUnusedCacheFiles(); @@ -76,9 +47,10 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation { for (int i = _unusedCacheFiles.Count - 1; i >= 0; i--) { - string cacheFilePath = _unusedCacheFiles[i]; - WX.RemoveFile(cacheFilePath, null); + string clearFilePath = _unusedCacheFiles[i]; _unusedCacheFiles.RemoveAt(i); + _fileSystem.ClearRecord(clearFilePath); + WX.RemoveFile(clearFilePath, null); if (OperationSystem.IsBusy) break; @@ -99,22 +71,23 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation private List GetUnusedCacheFiles() { - List result = new List(_result.fileList.Length); - foreach (var fileInfo in _result.fileList) + var allRecords = _fileSystem.GetAllRecords(); + List result = new List(allRecords.Count); + foreach (var filePath in allRecords) { // 如果存储文件名是按照Bundle文件哈希值存储 - string bundleGUID = Path.GetFileNameWithoutExtension(fileInfo.filePath); + string bundleGUID = Path.GetFileNameWithoutExtension(filePath); if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false) { - result.Add(fileInfo.filePath); + result.Add(filePath); } // 如果存储文件名是按照Bundle文件名称存储 /* - string bundleName = Path.GetFileNameWithoutExtension(fileInfo.filePath); + string bundleName = Path.GetFileNameWithoutExtension(filePath); if (_manifest.TryGetPackageBundleByBundleName(bundleName, out PackageBundle value) == false) { - result.Add(fileInfo.filePath); + result.Add(filePath); } */ } diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSDownloadFileOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSDownloadFileOperation.cs index 451834a7..3d015355 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSDownloadFileOperation.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSDownloadFileOperation.cs @@ -51,6 +51,7 @@ internal class WXFSDownloadFileOperation : DefaultDownloadFileOperation { _steps = ESteps.Done; Status = EOperationStatus.Succeed; + _fileSystem.TryRecordBundle(Bundle); //记录下载文件 } else { diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSInitializeOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSInitializeOperation.cs index 397220ee..71f71381 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSInitializeOperation.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSInitializeOperation.cs @@ -3,7 +3,16 @@ using YooAsset; internal partial class WXFSInitializeOperation : FSInitializeFileSystemOperation { + private enum ESteps + { + None, + RecordCacheFiles, + Done, + } + private readonly WechatFileSystem _fileSystem; + private RecordWechatCacheFilesOperation _recordWechatCacheFilesOp; + private ESteps _steps = ESteps.None; public WXFSInitializeOperation(WechatFileSystem fileSystem) { @@ -11,10 +20,36 @@ internal partial class WXFSInitializeOperation : FSInitializeFileSystemOperation } internal override void InternalOnStart() { - Status = EOperationStatus.Succeed; + _steps = ESteps.RecordCacheFiles; } internal override void InternalOnUpdate() { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.RecordCacheFiles) + { + if (_recordWechatCacheFilesOp == null) + { + _recordWechatCacheFilesOp = new RecordWechatCacheFilesOperation(_fileSystem); + OperationSystem.StartOperation(_fileSystem.PackageName, _recordWechatCacheFilesOp); + } + + if (_recordWechatCacheFilesOp.IsDone == false) + return; + + if (_recordWechatCacheFilesOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _recordWechatCacheFilesOp.Error; + } + } } } #endif \ No newline at end of file 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 d0f241e2..647beb42 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 @@ -61,6 +61,7 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation _steps = ESteps.Done; Result = new WXAssetBundleResult(_fileSystem, _bundle, assetBundle); Status = EOperationStatus.Succeed; + _fileSystem.TryRecordBundle(_bundle); //记录下载文件 } } else diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/internal/RecordWechatCacheFilesOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/internal/RecordWechatCacheFilesOperation.cs new file mode 100644 index 00000000..d05c34db --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/internal/RecordWechatCacheFilesOperation.cs @@ -0,0 +1,58 @@ +#if UNITY_WEBGL && WEIXINMINIGAME +using YooAsset; +using WeChatWASM; +using System.IO; + +internal class RecordWechatCacheFilesOperation : AsyncOperationBase +{ + private enum ESteps + { + None, + RecordCacheFiles, + WaitResponse, + Done, + } + + private readonly WechatFileSystem _fileSystem; + private ESteps _steps = ESteps.None; + + public RecordWechatCacheFilesOperation(WechatFileSystem fileSystem) + { + _fileSystem = fileSystem; + } + internal override void InternalOnStart() + { + _steps = ESteps.RecordCacheFiles; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.RecordCacheFiles) + { + _steps = ESteps.WaitResponse; + + var fileSystemMgr = _fileSystem.GetFileSystemMgr(); + var getSavedFileListOption = new GetSavedFileListOption(); + getSavedFileListOption.success = (GetSavedFileListSuccessCallbackResult response) => + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + foreach (var fileInfo in response.fileList) + { + //TODO 需要确认存储文件为Bundle文件 + _fileSystem.RecordBundleFile(fileInfo.filePath); + } + }; + getSavedFileListOption.fail = (FileError fileError) => + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = fileError.errMsg; + }; + fileSystemMgr.GetSavedFileList(getSavedFileListOption); + } + } +} +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/internal/RecordWechatCacheFilesOperation.cs.meta b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/internal/RecordWechatCacheFilesOperation.cs.meta new file mode 100644 index 00000000..3ddd1e8d --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/internal/RecordWechatCacheFilesOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 640ec7bd883b8314db53b508278aea6e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/WechatFileSystem.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/WechatFileSystem.cs index 8d1187fb..c1059b22 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/WechatFileSystem.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/WechatFileSystem.cs @@ -1,16 +1,17 @@ #if UNITY_WEBGL && WEIXINMINIGAME using System; using System.Collections.Generic; +using System.Linq; using UnityEngine; using YooAsset; using WeChatWASM; public static class WechatFileSystemCreater { - public static FileSystemParameters CreateWechatFileSystemParameters(IRemoteServices remoteServices) + public static FileSystemParameters CreateWechatFileSystemParameters(IRemoteServices remoteServices, string packageRoot) { string fileSystemClass = $"{nameof(WechatFileSystem)},YooAsset.RuntimeExtension"; - var fileSystemParams = new FileSystemParameters(fileSystemClass, null); + var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot); fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices); return fileSystemParams; } @@ -52,12 +53,10 @@ internal class WechatFileSystem : IFileSystem } } - /// - /// Key:资源包GUID Value:缓存路径 - /// + private readonly HashSet _recorders = new HashSet(); private readonly Dictionary _cacheFilePaths = new Dictionary(10000); - private WXFileSystemManager _wxFileSystemMgr; - private string _fileCacheRoot = string.Empty; + private WXFileSystemManager _fileSystemMgr; + private string _wxCacheRoot = string.Empty; /// /// 包裹名称 @@ -71,7 +70,7 @@ internal class WechatFileSystem : IFileSystem { get { - return _fileCacheRoot; + return _wxCacheRoot; } } @@ -82,7 +81,7 @@ internal class WechatFileSystem : IFileSystem { get { - return 0; + return _recorders.Count; } } @@ -176,6 +175,12 @@ internal class WechatFileSystem : IFileSystem public virtual void OnCreate(string packageName, string rootDirectory) { PackageName = packageName; + _wxCacheRoot = rootDirectory; + + if (string.IsNullOrEmpty(_wxCacheRoot)) + { + throw new System.Exception("请配置微信小游戏缓存根目录!"); + } // 注意:CDN服务未启用的情况下,使用微信WEB服务器 if (RemoteServices == null) @@ -184,10 +189,7 @@ internal class WechatFileSystem : IFileSystem RemoteServices = new WebRemoteServices(webRoot); } - _wxFileSystemMgr = WX.GetFileSystemManager(); - _fileCacheRoot = $"{WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE"; - //_fileCacheRoot = $"{WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE/子目录"; //注意:如果有子目录,请修改此处! - //_fileCacheRoot = PathUtility.Combine(WX.PluginCachePath, $"StreamingAssets/WebGL"); + _fileSystemMgr = WX.GetFileSystemManager(); } public virtual void OnUpdate() { @@ -200,7 +202,7 @@ internal class WechatFileSystem : IFileSystem public virtual bool Exists(PackageBundle bundle) { string filePath = GetCacheFileLoadPath(bundle); - return CheckCacheFileExist(filePath); + return _recorders.Contains(filePath); } public virtual bool NeedDownload(PackageBundle bundle) { @@ -226,7 +228,7 @@ internal class WechatFileSystem : IFileSystem { string filePath = GetCacheFileLoadPath(bundle); if (CheckCacheFileExist(filePath)) - return _wxFileSystemMgr.ReadFileSync(filePath); + return _fileSystemMgr.ReadFileSync(filePath); else return Array.Empty(); } @@ -234,26 +236,67 @@ internal class WechatFileSystem : IFileSystem { string filePath = GetCacheFileLoadPath(bundle); if (CheckCacheFileExist(filePath)) - return _wxFileSystemMgr.ReadFileSync(filePath, "utf8"); + return _fileSystemMgr.ReadFileSync(filePath, "utf8"); else return string.Empty; } #region 内部方法 + public WXFileSystemManager GetFileSystemMgr() + { + return _fileSystemMgr; + } public bool CheckCacheFileExist(string filePath) { - string result = _wxFileSystemMgr.AccessSync(filePath); + string result = _fileSystemMgr.AccessSync(filePath); return result.Equals("access:ok"); } public string GetCacheFileLoadPath(PackageBundle bundle) { if (_cacheFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false) { - filePath = PathUtility.Combine(_fileCacheRoot, bundle.FileName); + filePath = PathUtility.Combine(_wxCacheRoot, bundle.FileName); _cacheFilePaths.Add(bundle.BundleGUID, filePath); } return filePath; } #endregion + + #region 本地记录 + public List GetAllRecords() + { + return _recorders.ToList(); + } + public bool RecordBundleFile(string filePath) + { + if (_recorders.Contains(filePath)) + { + YooLogger.Error($"{nameof(WechatFileSystem)} has element : {filePath}"); + return false; + } + + _recorders.Add(filePath); + return true; + } + public void TryRecordBundle(PackageBundle bundle) + { + string filePath = GetCacheFileLoadPath(bundle); + if (_recorders.Contains(filePath) == false) + { + _recorders.Add(filePath); + } + } + public void ClearAllRecords() + { + _recorders.Clear(); + } + public void ClearRecord(string filePath) + { + if (_recorders.Contains(filePath)) + { + _recorders.Remove(filePath); + } + } + #endregion } #endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/YooAsset.RuntimeExtension.asmdef b/Assets/YooAsset/Samples~/Extension Sample/Runtime/YooAsset.RuntimeExtension.asmdef index 30958377..f2d902c5 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/YooAsset.RuntimeExtension.asmdef +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/YooAsset.RuntimeExtension.asmdef @@ -4,7 +4,7 @@ "references": [ "GUID:e34a5702dd353724aa315fb8011f08c3", "GUID:5efd170ecd8084500bed5692932fe14e", - "GUID:0f8df04a5a494444eb8fa0504f6fb965" + "GUID:bb21d6197862c4c3e863390dec9859a7" ], "includePlatforms": [], "excludePlatforms": [],