diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index 0a841f53..8a762d70 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -12,9 +12,6 @@ namespace YooAsset private static GameObject _driver = null; private static readonly List _packages = new List(); - public const string DefaultPackageVersion_Key = "DefaultPackageVersion_Key"; - public const string DefaultPcakageVersion = "100000"; - /// /// 是否已经初始化 /// diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearAllBundleFilesOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearAllBundleFilesOperation.cs index e65e51fc..0e03ba48 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearAllBundleFilesOperation.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearAllBundleFilesOperation.cs @@ -1,87 +1,48 @@ #if UNITY_WEBGL && WEIXINMINIGAME using System.Collections.Generic; -using WeChatWASM; -using YooAsset; -using UnityEngine; using System.Linq; +using UnityEngine; +using YooAsset; +using WeChatWASM; internal class WXFSClearAllBundleFilesOperation : FSClearAllBundleFilesOperation { private enum ESteps { None, - GetAllCacheFiles, - ClearAllWXCacheBundleFiles, + ClearAllCacheFiles, Done, } - private List _wxBundleFilePaths; - private int _fileTotalCount = 0; - private WechatFileSystem _fileSystem; + private readonly WechatFileSystem _fileSystem; private ESteps _steps = ESteps.None; + + internal WXFSClearAllBundleFilesOperation(WechatFileSystem fileSystem) { _fileSystem = fileSystem; - var allCacheFilePathDic = _fileSystem.GetWXAllCacheFilePath(); - _wxBundleFilePaths = allCacheFilePathDic.Values.ToList(); } internal override void InternalOnStart() { - _steps = ESteps.GetAllCacheFiles; + _steps = ESteps.ClearAllCacheFiles; } internal override void InternalOnUpdate() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.GetAllCacheFiles) + if (_steps == ESteps.ClearAllCacheFiles) { - if (_wxBundleFilePaths == null) + WX.CleanAllFileCache((bool isOk) => { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - return; - } - else - { - _steps = ESteps.ClearAllWXCacheBundleFiles; - _fileTotalCount = _wxBundleFilePaths.Count; - } - } - - if (_steps == ESteps.ClearAllWXCacheBundleFiles) - { - for (int i = _wxBundleFilePaths.Count - 1; i >= 0; i--) - { - string bundlePath = _wxBundleFilePaths[i]; - if (_fileSystem.CheckWXFileIsExist(bundlePath)) - { - WX.RemoveFile(bundlePath, (bool isOk) => - { - Debug.Log($"{_wxBundleFilePaths.Count}---删除缓存文件路径成功====={bundlePath}=="); - _wxBundleFilePaths.Remove(bundlePath); - }); - } + if (isOk) + YooLogger.Log("微信缓存清理成功!"); else - { - _wxBundleFilePaths.Remove(bundlePath); - //Debug.LogWarning($"Not Exit Cache file:{bundlePath}"); - } + YooLogger.Log("微信缓存清理失败!"); + }); - if (OperationSystem.IsBusy) - break; - } - - if (_fileTotalCount == 0) - Progress = 1.0f; - else - Progress = 1.0f - (_wxBundleFilePaths.Count / _fileTotalCount); - - if (_wxBundleFilePaths.Count == 0) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; } } } diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs index ab6683bc..68230e70 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs @@ -1,35 +1,29 @@ #if UNITY_WEBGL && WEIXINMINIGAME using System.Collections.Generic; -using WeChatWASM; -using YooAsset; +using System.IO; using UnityEngine; -using System.Linq; +using YooAsset; +using WeChatWASM; + internal class WXFSClearUnusedBundleFilesAsync : FSClearUnusedBundleFilesOperation { private enum ESteps { None, - LoadCachePackageInfo, - VerifyFileData, - LoadManifest, + GetAllCacheFiles, + WaitResult, GetUnusedCacheFiles, ClearUnusedCacheFiles, Done, } - private WechatFileSystem _fileSystem; + private readonly WechatFileSystem _fileSystem; private readonly PackageManifest _manifest; - private PackageManifest _cacheManifest; - private List _unusedBundleGUIDs; - private ESteps _steps = ESteps.None; - private DeserializeManifestOperation _deserializer; - private byte[] _fileData; - private string _packageHash; + private List _unusedCacheFiles; private int _unusedFileTotalCount = 0; - private string _lastPackageVersion; - private string _cacheManifestHashPath; - private string _cacheManifestPath; + private GetSavedFileListSuccessCallbackResult _result; + private ESteps _steps = ESteps.None; internal WXFSClearUnusedBundleFilesAsync(WechatFileSystem fileSystem, PackageManifest manifest) { @@ -38,98 +32,54 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearUnusedBundleFilesOperati } internal override void InternalOnStart() { - _steps = ESteps.LoadCachePackageInfo; + _steps = ESteps.GetAllCacheFiles; } internal override void InternalOnUpdate() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.LoadCachePackageInfo) + if (_steps == ESteps.GetAllCacheFiles) { - LoadManifestInfo(); - if(_fileData != null && _fileData.Length > 0 && !string.IsNullOrEmpty(_packageHash)) - { - _steps = ESteps.VerifyFileData; - } - else + _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; - Error = "Failed to load cache package manifest file!"; - } - } - - if(_steps == ESteps.VerifyFileData) - { - string fileHash = HashUtility.BytesMD5(_fileData); - if (fileHash == _packageHash) - { - _steps = ESteps.LoadManifest; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = "Failed to verify cache package manifest file!"; - } - } - - if (_steps == ESteps.LoadManifest) - { - if (_deserializer == null) - { - _deserializer = new DeserializeManifestOperation(_fileData); - OperationSystem.StartOperation(_fileSystem.PackageName, _deserializer); - } - - Progress = _deserializer.Progress; - if (_deserializer.IsDone == false) - return; - - if (_deserializer.Status == EOperationStatus.Succeed) + }; + option.success += (GetSavedFileListSuccessCallbackResult result) => { + _result = result; _steps = ESteps.GetUnusedCacheFiles; - _cacheManifest = _deserializer.Manifest; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _deserializer.Error; - } + }; } - if(_steps == ESteps.GetUnusedCacheFiles) + if (_steps == ESteps.WaitResult) { - _unusedBundleGUIDs = GetUnusedBundleGUIDs(); - _unusedFileTotalCount = _unusedBundleGUIDs.Count; + return; + } + + if (_steps == ESteps.GetUnusedCacheFiles) + { + _unusedCacheFiles = GetUnusedCacheFiles(); + _unusedFileTotalCount = _unusedCacheFiles.Count; _steps = ESteps.ClearUnusedCacheFiles; YooLogger.Log($"Found unused cache files count : {_unusedFileTotalCount}"); } if (_steps == ESteps.ClearUnusedCacheFiles) { - for (int i = _unusedBundleGUIDs.Count - 1; i >= 0; i--) + for (int i = _unusedCacheFiles.Count - 1; i >= 0; i--) { - string bundleGUID = _unusedBundleGUIDs[i]; - PackageBundle bundle = null; - if(_cacheManifest.TryGetPackageBundleByBundleGUID(bundleGUID,out bundle)) - { - if (bundle != null) - { - var cachePath = GetCachePathByFileName(bundle.FileName); - WX.RemoveFile(cachePath, (bool isOk) => - { - Debug.Log($"{_unusedBundleGUIDs.Count}---删除缓存文件路径成功====={cachePath}=="); - //_unusedBundleGUIDs.Remove(cachePath); - }); + string cacheFilePath = _unusedCacheFiles[i]; + WX.RemoveFile(cacheFilePath, null); + _unusedCacheFiles.RemoveAt(i); - //_fileSystem.DeleteCacheFile(bundleGUID); - _unusedBundleGUIDs.RemoveAt(i); - } - } - if (OperationSystem.IsBusy) break; } @@ -137,74 +87,38 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearUnusedBundleFilesOperati if (_unusedFileTotalCount == 0) Progress = 1.0f; else - Progress = 1.0f - (_unusedBundleGUIDs.Count / _unusedFileTotalCount); + Progress = 1.0f - (_unusedCacheFiles.Count / _unusedFileTotalCount); - if (_unusedBundleGUIDs.Count == 0) + if (_unusedCacheFiles.Count == 0) { - CheckPackageVerion(); _steps = ESteps.Done; Status = EOperationStatus.Succeed; } } } - private List GetUnusedBundleGUIDs() + private List GetUnusedCacheFiles() { - var allBundleGUIDs = _cacheManifest.BundleDic3.Keys.ToList(); - List result = new List(allBundleGUIDs.Count); - foreach (var bundleGUID in allBundleGUIDs) + List result = new List(_result.fileList.Length); + foreach (var fileInfo in _result.fileList) { - if (_manifest.IsIncludeBundleFile(bundleGUID) == false) + // 如果存储文件名是按照Bundle文件哈希值存储 + string bundleGUID = Path.GetFileNameWithoutExtension(fileInfo.filePath); + if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false) { - result.Add(bundleGUID); + result.Add(fileInfo.filePath); } + + // 如果存储文件名是按照Bundle文件名称存储 + /* + string bundleName = Path.GetFileNameWithoutExtension(fileInfo.filePath); + if (_manifest.TryGetPackageBundleByBundleName(bundleName, out PackageBundle value) == false) + { + result.Add(fileInfo.filePath); + } + */ } return result; } - - private void LoadManifestInfo() - { - var packageName = _fileSystem.PackageName; - var packageVersion = _manifest.PackageVersion; - if (WX.StorageHasKeySync(YooAssets.DefaultPackageVersion_Key)) - { - _lastPackageVersion = WX.StorageGetStringSync(YooAssets.DefaultPackageVersion_Key, YooAssets.DefaultPcakageVersion); - Debug.Log($"==========Get Storage PackageVerion Succ==={_lastPackageVersion}"); - if (!string.IsNullOrEmpty(_lastPackageVersion) && (_lastPackageVersion != packageVersion)) - { - _cacheManifestHashPath = GetCachePathByFileName(YooAssetSettingsData.GetPackageHashFileName(packageName, _lastPackageVersion)); - _cacheManifestPath = GetCachePathByFileName(YooAssetSettingsData.GetManifestBinaryFileName(packageName, _lastPackageVersion)); - if(string.IsNullOrEmpty(_cacheManifestHashPath) || string.IsNullOrEmpty(_cacheManifestPath)) { return; } - - _packageHash = _fileSystem.ReadFileText(_cacheManifestHashPath); - _fileData = _fileSystem.ReadFileData(_cacheManifestPath); - } - } - else - { - WX.StorageSetStringSync(YooAssets.DefaultPackageVersion_Key, packageVersion); - Debug.Log($"first Set Storage PackageVerion Succ==={packageVersion}"); - } - } - - private void CheckPackageVerion() - { - var packageName = _fileSystem.PackageName; - var packageVersion = _manifest.PackageVersion; - if (_lastPackageVersion != packageVersion) - { - WX.StorageSetStringSync(YooAssets.DefaultPackageVersion_Key, packageVersion); - //删除旧的资源清单文件和哈希文件 - WX.RemoveFile(_cacheManifestHashPath, (bool isOk) => { Debug.Log("====Delect manifestHashPath Succ"); }); - WX.RemoveFile(_cacheManifestPath, (bool isOk) => { Debug.Log("====Delect manifestPath Succ"); }); - Debug.Log($"==========Set Storage PackageVerion Succ==={packageVersion}"); - } - } - - private string GetUnuseCachePathByBundleName(string fileName) - { - var filePath = $"StreamingAssets/WebGL/{fileName}"; - return WX.GetCachePath(filePath); - } } #endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/WechatFileSystem.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/WechatFileSystem.cs index 7caa639e..11b02957 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/WechatFileSystem.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/WechatFileSystem.cs @@ -1,9 +1,9 @@ #if UNITY_WEBGL && WEIXINMINIGAME +using System; using System.Collections.Generic; using UnityEngine; using YooAsset; using WeChatWASM; -using System; public static class WechatFileSystemCreater { @@ -55,9 +55,9 @@ internal class WechatFileSystem : IFileSystem /// /// Key:资源包GUID Value:缓存路径 /// - private readonly Dictionary _wxFilePaths = new Dictionary(10000); + private readonly Dictionary _cacheFilePaths = new Dictionary(10000); private WXFileSystemManager _wxFileSystemMgr; - private string _wxFileCacheRoot = string.Empty; + private string _fileCacheRoot = string.Empty; /// /// 包裹名称 @@ -71,7 +71,7 @@ internal class WechatFileSystem : IFileSystem { get { - return _wxFileCacheRoot; + return _fileCacheRoot; } } @@ -86,12 +86,12 @@ internal class WechatFileSystem : IFileSystem } } -#region 自定义参数 + #region 自定义参数 /// /// 自定义参数:远程服务接口 /// public IRemoteServices RemoteServices { private set; get; } = null; -#endregion + #endregion public WechatFileSystem() @@ -121,7 +121,6 @@ internal class WechatFileSystem : IFileSystem OperationSystem.StartOperation(PackageName, operation); return operation; } - public virtual FSClearUnusedBundleFilesOperation ClearUnusedBundleFilesAsync(PackageManifest manifest) { var operation = new WXFSClearUnusedBundleFilesAsync(this, manifest); @@ -172,8 +171,8 @@ internal class WechatFileSystem : IFileSystem } _wxFileSystemMgr = WX.GetFileSystemManager(); - _wxFileCacheRoot = PathUtility.Combine(WX.PluginCachePath, $"StreamingAssets/WebGL");// WX.PluginCachePath; //注意:如果有子目录,请修改此处! - Debug.Log($"==_wxFileCacheRoot=={_wxFileCacheRoot}"); + _fileCacheRoot = WX.env.USER_DATA_PATH; //注意:如果有子目录,请修改此处! + //_fileCacheRoot = PathUtility.Combine(WX.PluginCachePath, $"StreamingAssets/WebGL"); } public virtual void OnUpdate() { @@ -185,16 +184,14 @@ internal class WechatFileSystem : IFileSystem } public virtual bool Exists(PackageBundle bundle) { - string filePath = GetWXFileLoadPath(bundle); - //Debug.Log($"CacheFile:{WX.GetCachePath($"StreamingAssets/WebGL/v1.0.0/{bundle.FileName}")}"); - return CheckWXFileIsExist(filePath); + string filePath = GetCacheFileLoadPath(bundle); + return CheckCacheFileExist(filePath); } public virtual bool NeedDownload(PackageBundle bundle) { if (Belong(bundle) == false) return false; - //Debug.Log($"WX NeedDownload: bundleName:{bundle.BundleName} + Exists:{Exists(bundle)}"); return Exists(bundle) == false; } public virtual bool NeedUnpack(PackageBundle bundle) @@ -208,111 +205,36 @@ internal class WechatFileSystem : IFileSystem public virtual byte[] ReadFileData(PackageBundle bundle) { - throw new System.NotImplementedException(); - } - public virtual string ReadFileText(PackageBundle bundle) - { - throw new System.NotImplementedException(); - } - - public byte[] ReadFileData(string filePath) - { - if (CheckWXFileIsExist(filePath)) + string filePath = GetCacheFileLoadPath(bundle); + if (CheckCacheFileExist(filePath)) return _wxFileSystemMgr.ReadFileSync(filePath); else return Array.Empty(); - //throw new System.NotImplementedException(); } - - public string ReadFileText(string filePath) + public virtual string ReadFileText(PackageBundle bundle) { - if(CheckWXFileIsExist(filePath)) + string filePath = GetCacheFileLoadPath(bundle); + if (CheckCacheFileExist(filePath)) return _wxFileSystemMgr.ReadFileSync(filePath, "utf8"); else return string.Empty; - //throw new System.NotImplementedException(); } - /// - /// 获取所有缓存文件的路径 - /// - /// - public Dictionary GetWXAllCacheFilePath() - { - return _wxFilePaths; - } - /// - /// 判断微信缓存文件是否存在 - /// - /// - /// - public bool CheckWXFileIsExist(string filePath) + #region 内部方法 + public bool CheckCacheFileExist(string filePath) { string result = _wxFileSystemMgr.AccessSync(filePath); return result.Equals("access:ok"); } -#region 调用微信小游戏接口删除缓存文件目录下所有文件 - public void ClearAllCacheFile() + public string GetCacheFileLoadPath(PackageBundle bundle) { -#if !UNITY_EDITOR && UNITY_WEBGL && WEIXINMINIGAME - ShowModalOption showModalOp = new ShowModalOption(); - showModalOp.title = "提示"; - showModalOp.content = "是否确定要清理缓存并重启"; - showModalOp.confirmText = "确定"; - showModalOp.cancelText = "取消"; - showModalOp.complete = (GeneralCallbackResult callResult) => { Debug.Log($"complete==={callResult.errMsg}"); }; - showModalOp.fail = (GeneralCallbackResult callResult) => { Debug.Log($"fail==={callResult.errMsg}"); }; - showModalOp.success = (ShowModalSuccessCallbackResult callResult) => - { - if(callResult.confirm) - RestartMiniGame(); - }; - WX.ShowModal(showModalOp); -#endif - } - - /// - /// 微信小游戏清除缓存并且重启小游戏 - /// 参考小游戏=>出发吧麦芬 - /// - private void RestartMiniGame() - { - WX.CleanAllFileCache((bool isOk) => + if (_cacheFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false) { - RestartMiniProgramOption restartMini = new RestartMiniProgramOption(); - restartMini.complete = RestartMiniComplete; - restartMini.fail = RestartMiniFailComplete; - restartMini.success = RestartMiniSuccComplete; - WX.RestartMiniProgram(restartMini); - }); - } - - private void RestartMiniComplete(GeneralCallbackResult result) - { - Debug.Log($"RestartMiniComplete:{result.errMsg}"); - } - - private void RestartMiniFailComplete(GeneralCallbackResult result) - { - Debug.Log($"RestartMiniFailComplete:{result.errMsg}"); - } - - private void RestartMiniSuccComplete(GeneralCallbackResult result) - { - Debug.Log($"RestartMiniSuccComplete:{result.errMsg}"); - } - -#endregion -#region 内部方法 - private string GetWXFileLoadPath(PackageBundle bundle) - { - if (_wxFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false) - { - filePath = PathUtility.Combine(_wxFileCacheRoot, bundle.FileName); - _wxFilePaths.Add(bundle.BundleGUID, filePath); + filePath = PathUtility.Combine(_fileCacheRoot, bundle.FileName); + _cacheFilePaths.Add(bundle.BundleGUID, filePath); } return filePath; } -#endregion + #endregion } #endif \ No newline at end of file