Update PatchSystem

移除YooAssets.ClearAllCacheFiles()
新增YooAssets.ClearUnusedCacheFiles();
pull/51/head
hevinci 2022-10-18 11:56:05 +08:00
parent eca5d73b94
commit 9bc5580229
9 changed files with 172 additions and 10 deletions

View File

@ -947,6 +947,14 @@ namespace YooAsset
{
return _bundleServices.MappingToAssetPath(location);
}
/// <summary>
/// 是否包含资源文件
/// </summary>
internal bool IsIncludeBundleFile(string fileName)
{
return _bundleServices.IsIncludeBundleFile(fileName);
}
#endregion
#region 调试方法

View File

@ -0,0 +1,112 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace YooAsset
{
/// <summary>
/// 清理未使用的缓存资源操作类
/// </summary>
public sealed class ClearUnusedCacheFilesOperation : AsyncOperationBase
{
private enum ESteps
{
None,
GetUnusedCacheFiles,
ClearUnusedCacheFiles,
Done,
}
private readonly List<AssetsPackage> _packages;
private ESteps _steps = ESteps.None;
private List<string> _unusedCacheFilePaths;
private int _unusedFileTotalCount = 0;
internal ClearUnusedCacheFilesOperation(List<AssetsPackage> packages)
{
_packages = packages;
}
internal override void Start()
{
_steps = ESteps.GetUnusedCacheFiles;
}
internal override void Update()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.GetUnusedCacheFiles)
{
_unusedCacheFilePaths = GetUnusedCacheFilePaths();
_unusedFileTotalCount = _unusedCacheFilePaths.Count;
YooLogger.Log($"Found unused cache file count : {_unusedFileTotalCount}");
_steps = ESteps.ClearUnusedCacheFiles;
}
if (_steps == ESteps.ClearUnusedCacheFiles)
{
for (int i = _unusedCacheFilePaths.Count - 1; i >= 0; i--)
{
string filePath = _unusedCacheFilePaths[i];
if (File.Exists(filePath))
{
try
{
File.Delete(filePath);
YooLogger.Log($"Delete unused cache file : {filePath}");
}
catch (System.Exception e)
{
YooLogger.Warning($"Failed delete cache file : {filePath} Exception : {e}");
}
}
_unusedCacheFilePaths.RemoveAt(i);
if (OperationSystem.IsBusy)
break;
}
if (_unusedFileTotalCount == 0)
Progress = 1.0f;
else
Progress = 1.0f - (_unusedCacheFilePaths.Count / _unusedFileTotalCount);
if (_unusedCacheFilePaths.Count == 0)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
}
/// <summary>
/// 获取未被使用的缓存文件路径集合
/// </summary>
private List<string> GetUnusedCacheFilePaths()
{
string cacheFolderPath = SandboxHelper.GetCacheFolderPath();
if (Directory.Exists(cacheFolderPath) == false)
return new List<string>();
// 获取所有缓存文件
DirectoryInfo directoryInfo = new DirectoryInfo(cacheFolderPath);
FileInfo[] fileInfos = directoryInfo.GetFiles();
List<string> result = new List<string>(fileInfos.Length);
foreach (FileInfo fileInfo in fileInfos)
{
bool used = false;
foreach (var package in _packages)
{
if (package.IsIncludeBundleFile(fileInfo.Name))
{
used = true;
break;
}
}
if (used == false)
result.Add(fileInfo.FullName);
}
return result;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5d188c50fd00bf941b2eeebb374dc0d1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -36,7 +36,7 @@ namespace YooAsset
/// 人类可读的版本信息
/// </summary>
public string HumanReadableVersion;
/// <summary>
/// 资源列表(主动收集的资源列表)
/// </summary>
@ -217,6 +217,19 @@ namespace YooAsset
return BundleDic.TryGetValue(bundleName, out result);
}
/// <summary>
/// 是否包含资源文件
/// </summary>
public bool IsIncludeBundleFile(string fileName)
{
foreach (var patchBundle in BundleList)
{
if (patchBundle.FileName == fileName)
return true;
}
return false;
}
/// <summary>
/// 序列化

View File

@ -30,7 +30,6 @@ namespace YooAsset
return _simulatePatchManifest.HumanReadableVersion;
}
// 设置资源清单
internal void SetSimulatePatchManifest(PatchManifest patchManifest)
{
_simulatePatchManifest = patchManifest;
@ -71,6 +70,10 @@ namespace YooAsset
{
return _simulatePatchManifest.PackageName;
}
bool IBundleServices.IsIncludeBundleFile(string fileName)
{
return _simulatePatchManifest.IsIncludeBundleFile(fileName);
}
#endregion
}
}

View File

@ -396,6 +396,10 @@ namespace YooAsset
{
return LocalPatchManifest.PackageName;
}
bool IBundleServices.IsIncludeBundleFile(string fileName)
{
return LocalPatchManifest.IsIncludeBundleFile(fileName);
}
#endregion
}
}

View File

@ -121,6 +121,10 @@ namespace YooAsset
{
return _appPatchManifest.PackageName;
}
bool IBundleServices.IsIncludeBundleFile(string fileName)
{
return _appPatchManifest.IsIncludeBundleFile(fileName);
}
#endregion
}
}

View File

@ -32,5 +32,10 @@ namespace YooAsset
/// 获取所属的包裹名
/// </summary>
string GetPackageName();
/// <summary>
/// 是否包含资源文件
/// </summary>
bool IsIncludeBundleFile(string fileName);
}
}

View File

@ -177,6 +177,16 @@ namespace YooAsset
#endregion
#region 沙盒相关
/// <summary>
/// 清理未使用的缓存文件
/// </summary>
public static ClearUnusedCacheFilesOperation ClearUnusedCacheFiles()
{
ClearUnusedCacheFilesOperation operation = new ClearUnusedCacheFilesOperation(_packages);
OperationSystem.StartOperation(operation);
return operation;
}
/// <summary>
/// 获取内置文件夹名称
/// </summary>
@ -200,14 +210,6 @@ namespace YooAsset
{
SandboxHelper.DeleteSandbox();
}
/// <summary>
/// 清空所有的缓存文件
/// </summary>
public static void ClearAllCacheFiles()
{
SandboxHelper.DeleteCacheFolder();
}
#endregion
#region 调试信息