mirror of https://github.com/tuyoogame/YooAsset
提交微信缓存相关逻辑,删除全部缓存和部分无用缓存
parent
02ba98f120
commit
945842c0e7
|
@ -12,6 +12,8 @@ namespace YooAsset
|
|||
private static GameObject _driver = null;
|
||||
private static readonly List<ResourcePackage> _packages = new List<ResourcePackage>();
|
||||
|
||||
public const string DefaultPackageVersion_Key = "DefaultPackageVersion_Key";
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经初始化
|
||||
/// </summary>
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d48600eb90915544586c0108b94cfd02
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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<string> _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<string> GetUnusedBundleGUIDs()
|
||||
{
|
||||
var allBundleGUIDs = _cacheManifest.BundleDic3.Keys.ToList();
|
||||
List<string> result = new List<string>(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
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e4938d8a5a9d7f24db37f8df6b32501f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AppVersion
|
||||
/// </summary>
|
||||
public static string AppVersion { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -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,7 +212,41 @@ internal class WechatFileSystem : IFileSystem
|
|||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
public byte[] ReadFileData(string filePath)
|
||||
{
|
||||
if (CheckWXFileIsExist(filePath))
|
||||
return _wxFileSystemMgr.ReadFileSync(filePath);
|
||||
else
|
||||
return Array.Empty<byte>();
|
||||
//throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public string ReadFileText(string filePath)
|
||||
{
|
||||
if(CheckWXFileIsExist(filePath))
|
||||
return _wxFileSystemMgr.ReadFileSync(filePath, "utf8");
|
||||
else
|
||||
return string.Empty;
|
||||
//throw new System.NotImplementedException();
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取所有缓存文件的路径
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string,string> GetWXAllCacheFilePath()
|
||||
{
|
||||
return _wxFilePaths;
|
||||
}
|
||||
/// <summary>
|
||||
/// 判断微信缓存文件是否存在
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <returns></returns>
|
||||
public bool CheckWXFileIsExist(string filePath)
|
||||
{
|
||||
string result = _wxFileSystemMgr.AccessSync(filePath);
|
||||
return result.Equals("access:ok");
|
||||
}
|
||||
#region 内部方法
|
||||
private string GetCacheFileLoadPath(PackageBundle bundle)
|
||||
{
|
||||
|
|
|
@ -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<FsmUpdaterDone>();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue