diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index 8a762d7..e8a90e0 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -12,6 +12,8 @@ namespace YooAsset private static GameObject _driver = null; private static readonly List _packages = new List(); + public const string DefaultPackageVersion_Key = "DefaultPackageVersion_Key"; + /// /// 是否已经初始化 /// diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearAllBundleFilesOperation.cs.meta b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearAllBundleFilesOperation.cs.meta new file mode 100644 index 0000000..984245b --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearAllBundleFilesOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d48600eb90915544586c0108b94cfd02 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs new file mode 100644 index 0000000..c3384c9 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs @@ -0,0 +1,186 @@ +#if UNITY_WEBGL && WEIXINMINIGAME +using System.Collections.Generic; +using WeChatWASM; +using YooAsset; +using UnityEngine; +using System.Linq; + +internal class WXFSClearUnusedBundleFilesAsync : FSClearUnusedBundleFilesOperation +{ + private enum ESteps + { + None, + LoadCachePackageInfo, + VerifyFileData, + LoadManifest, + GetUnusedCacheFiles, + ClearUnusedCacheFiles, + Done, + } + + private 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 int _unusedFileTotalCount = 0; + private string _lastPackageVersion; + + internal WXFSClearUnusedBundleFilesAsync(WechatFileSystem fileSystem, PackageManifest manifest) + { + _fileSystem = fileSystem; + _manifest = manifest; + } + internal override void InternalOnStart() + { + _steps = ESteps.LoadCachePackageInfo; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.LoadCachePackageInfo) + { + LoadManifestInfo(); + if(_fileData != null && _fileData.Length > 0 && !string.IsNullOrEmpty(_packageHash)) + { + _steps = ESteps.VerifyFileData; + Debug.Log($"===_packageHash==={_packageHash}"); + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "Failed to load cache package manifest file!"; + } + + //YooLogger.Log($"Found unused cache files count : {_unusedFileTotalCount}"); + } + + 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) + { + _steps = ESteps.GetUnusedCacheFiles; + _cacheManifest = _deserializer.Manifest; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _deserializer.Error; + } + } + + if(_steps == ESteps.GetUnusedCacheFiles) + { + _unusedBundleGUIDs = GetUnusedBundleGUIDs(); + _unusedFileTotalCount = _unusedBundleGUIDs.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--) + { + string bundleGUID = _unusedBundleGUIDs[i]; + PackageBundle bundle = null; + if(_cacheManifest.TryGetPackageBundleByBundleGUID(bundleGUID,out bundle)) + { + if (bundle != null) + { + var cachePath = GetUnuseCachePathByBundleName(bundle.FileName); + WX.RemoveFile(cachePath, (bool isOk) => + { + Debug.Log($"{_unusedBundleGUIDs.Count}---删除缓存文件路径成功====={cachePath}=="); + //_unusedBundleGUIDs.Remove(cachePath); + }); + + //_fileSystem.DeleteCacheFile(bundleGUID); + _unusedBundleGUIDs.RemoveAt(i); + } + } + + if (OperationSystem.IsBusy) + break; + } + + if (_unusedFileTotalCount == 0) + Progress = 1.0f; + else + Progress = 1.0f - (_unusedBundleGUIDs.Count / _unusedFileTotalCount); + + if (_unusedBundleGUIDs.Count == 0) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } + + private List GetUnusedBundleGUIDs() + { + var allBundleGUIDs = _cacheManifest.BundleDic3.Keys.ToList(); + List result = new List(allBundleGUIDs.Count); + foreach (var bundleGUID in allBundleGUIDs) + { + if (_manifest.IsIncludeBundleFile(bundleGUID) == false) + { + result.Add(bundleGUID); + } + } + return result; + } + + private void LoadManifestInfo() + { + var packageName = _fileSystem.PackageName; + _lastPackageVersion = WX.StorageGetStringSync(YooAssets.DefaultPackageVersion_Key, "100000"); + Debug.Log($"==========取出本地数据版本文件成功==={_lastPackageVersion}"); + if (!string.IsNullOrEmpty(_lastPackageVersion)) + { + var cacheManifestHashPath = GetUnuseCachePathByBundleName(YooAssetSettingsData.GetPackageHashFileName(packageName, _lastPackageVersion)); + var cacheManifestPath = GetUnuseCachePathByBundleName(YooAssetSettingsData.GetManifestBinaryFileName(packageName, _lastPackageVersion)); + + _packageHash = _fileSystem.ReadFileText(cacheManifestHashPath); + _fileData = _fileSystem.ReadFileData(cacheManifestPath); + } + } + + private string GetUnuseCachePathByBundleName(string fileName) + { + var path = $"StreamingAssets/WebGL/v{WechatFileSystemCreater.AppVersion}/{fileName}"; + return WX.GetCachePath(path); + } +} +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs.meta b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs.meta new file mode 100644 index 0000000..f660e19 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e4938d8a5a9d7f24db37f8df6b32501f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/WechatFileSystem.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/WechatFileSystem.cs index 894a3bd..349b641 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/WechatFileSystem.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/WechatFileSystem.cs @@ -6,13 +6,18 @@ using WeChatWASM; public static class WechatFileSystemCreater { - public static FileSystemParameters CreateWechatFileSystemParameters(IRemoteServices remoteServices) + public static FileSystemParameters CreateWechatFileSystemParameters(IRemoteServices remoteServices,string rootDirectory = null) { string fileSystemClass = $"{nameof(WechatFileSystem)},YooAsset.RuntimeExtension"; - var fileSystemParams = new FileSystemParameters(fileSystemClass, null); + var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory); fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices); return fileSystemParams; } + + /// + /// AppVersion + /// + public static string AppVersion { get; set; } } /// @@ -82,12 +87,12 @@ internal class WechatFileSystem : IFileSystem } } - #region 自定义参数 +#region 自定义参数 /// /// 自定义参数:远程服务接口 /// public IRemoteServices RemoteServices { private set; get; } = null; - #endregion +#endregion public WechatFileSystem() @@ -113,13 +118,13 @@ internal class WechatFileSystem : IFileSystem } public virtual FSClearAllBundleFilesOperation ClearAllBundleFilesAsync() { - var operation = new FSClearAllBundleFilesCompleteOperation(); + var operation = new WXFSClearAllBundleFilesOperation(this); OperationSystem.StartOperation(PackageName, operation); return operation; } public virtual FSClearUnusedBundleFilesOperation ClearUnusedBundleFilesAsync(PackageManifest manifest) { - var operation = new FSClearUnusedBundleFilesCompleteOperation(); + var operation = new WXFSClearUnusedBundleFilesAsync(this, manifest); OperationSystem.StartOperation(PackageName, operation); return operation; } @@ -167,7 +172,7 @@ internal class WechatFileSystem : IFileSystem } _fileSystemManager = WX.GetFileSystemManager(); - _fileCacheRoot = WX.env.USER_DATA_PATH; //注意:如果有子目录,请修改此处! + _fileCacheRoot = rootDirectory;// WX.PluginCachePath; //WX.env.USER_DATA_PATH; //注意:如果有子目录,请修改此处! } public virtual void OnUpdate() { @@ -179,9 +184,9 @@ internal class WechatFileSystem : IFileSystem } public virtual bool Exists(PackageBundle bundle) { - string filePath = GetCacheFileLoadPath(bundle); - string result = _fileSystemManager.AccessSync(filePath); - return result.Equals("access:ok"); + string filePath = GetWXFileLoadPath(bundle); + //Debug.Log($"CacheFile:{WX.GetCachePath($"StreamingAssets/WebGL/v1.0.0/{bundle.FileName}")}"); + return CheckWXFileIsExist(filePath); } public virtual bool NeedDownload(PackageBundle bundle) { @@ -207,8 +212,42 @@ internal class WechatFileSystem : IFileSystem { throw new System.NotImplementedException(); } + public byte[] ReadFileData(string filePath) + { + if (CheckWXFileIsExist(filePath)) + return _wxFileSystemMgr.ReadFileSync(filePath); + else + return Array.Empty(); + //throw new System.NotImplementedException(); + } - #region 内部方法 + public string ReadFileText(string filePath) + { + if(CheckWXFileIsExist(filePath)) + return _wxFileSystemMgr.ReadFileSync(filePath, "utf8"); + else + return string.Empty; + //throw new System.NotImplementedException(); + } + /// + /// 获取所有缓存文件的路径 + /// + /// + public Dictionary GetWXAllCacheFilePath() + { + return _wxFilePaths; + } + /// + /// 判断微信缓存文件是否存在 + /// + /// + /// + public bool CheckWXFileIsExist(string filePath) + { + string result = _wxFileSystemMgr.AccessSync(filePath); + return result.Equals("access:ok"); + } +#region 内部方法 private string GetCacheFileLoadPath(PackageBundle bundle) { if (_cacheFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false) @@ -218,6 +257,6 @@ internal class WechatFileSystem : IFileSystem } return filePath; } - #endregion +#endregion } #endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Space Shooter/GameScript/Runtime/PatchLogic/FsmNode/FsmClearPackageCache.cs b/Assets/YooAsset/Samples~/Space Shooter/GameScript/Runtime/PatchLogic/FsmNode/FsmClearPackageCache.cs index b64b29b..eabb992 100644 --- a/Assets/YooAsset/Samples~/Space Shooter/GameScript/Runtime/PatchLogic/FsmNode/FsmClearPackageCache.cs +++ b/Assets/YooAsset/Samples~/Space Shooter/GameScript/Runtime/PatchLogic/FsmNode/FsmClearPackageCache.cs @@ -32,6 +32,22 @@ internal class FsmClearPackageCache : IStateNode private void Operation_Completed(YooAsset.AsyncOperationBase obj) { +#if UNITY_WEBGL || WEIXINMINIGAME + + //删除旧的资源清单文件和哈希文件 + if (WX.StorageHasKeySync(YooAssets.DefaultPackageVersion_Key)) + { + var packageName = (string)_machine.GetBlackboardValue("PackageName"); + var packageVersion = (string)_machine.GetBlackboardValue("PackageVersion"); + var lastPackageVersion = WX.StorageGetStringSync(YooAssets.DefaultPackageVersion_Key, ""); + if (!string.IsNullOrEmpty(lastPackageVersion) && lastPackageVersion != packageVersion) + { + WX.StorageSetStringSync(YooAssets.DefaultPackageVersion_Key, packageVersion); + Debug.Log($"==========本地数据版本文件设置成功==={packageVersion}"); + + } + } +#endif _machine.ChangeState(); } } \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Space Shooter/GameScript/Runtime/PatchLogic/FsmNode/FsmInitializePackage.cs b/Assets/YooAsset/Samples~/Space Shooter/GameScript/Runtime/PatchLogic/FsmNode/FsmInitializePackage.cs index b986cb8..cb88dee 100644 --- a/Assets/YooAsset/Samples~/Space Shooter/GameScript/Runtime/PatchLogic/FsmNode/FsmInitializePackage.cs +++ b/Assets/YooAsset/Samples~/Space Shooter/GameScript/Runtime/PatchLogic/FsmNode/FsmInitializePackage.cs @@ -74,7 +74,15 @@ internal class FsmInitializePackage : IStateNode if (playMode == EPlayMode.WebPlayMode) { var createParameters = new WebPlayModeParameters(); +#if UNITY_WEBGL && WEIXINMINIGAME && !UNITY_EDITOR + string defaultHostServer = GetHostServerURL(); + string fallbackHostServer = GetHostServerURL(); + IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer); + var rootDir = string.Format("{0}/{1}", WX.PluginCachePath, $"StreamingAssets/WebGL/v1.0.0");//这个1.0.0是App大版本文件,转微信小游戏后,没删除 + createParameters.WebFileSystemParameters = WechatFileSystemCreater.CreateWechatFileSystemParameters(remoteServices, rootDir); +#else createParameters.WebFileSystemParameters = FileSystemParameters.CreateDefaultWebFileSystemParameters(); +#endif initializationOperation = package.InitializeAsync(createParameters); }