update cache system

新增方法ResoucePackage.ClearAllCacheFilesAsync()
pull/122/head
hevinci 2023-05-12 14:30:08 +08:00
parent 812c46adeb
commit e6397559ff
5 changed files with 101 additions and 2 deletions

View File

@ -112,8 +112,6 @@ namespace YooAsset
var retObject = assetObject as TObject;
if (retObject != null)
ret.Add(retObject);
else
YooLogger.Warning($"The type conversion failed : {assetObject.name}");
}
return ret.ToArray();
}

View File

@ -157,6 +157,14 @@ namespace YooAsset
return result;
}
/// <summary>
/// 获取所有的缓存文件
/// </summary>
public static List<string> GetAllCacheGUIDs(ResourcePackage package)
{
var cache = GetOrCreateCache(package.PackageName);
return cache.GetAllKeys();
}
private static EVerifyResult VerifyingInternal(string filePath, long fileSize, string fileCRC, EVerifyLevel verifyLevel)
{

View File

@ -0,0 +1,71 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace YooAsset
{
/// <summary>
/// 清理本地包裹所有的缓存文件
/// </summary>
public sealed class ClearAllCacheFilesOperation : AsyncOperationBase
{
private enum ESteps
{
None,
GetAllCacheFiles,
ClearAllCacheFiles,
Done,
}
private readonly ResourcePackage _package;
private List<string> _allCacheGUIDs;
private int _fileTotalCount = 0;
private ESteps _steps = ESteps.None;
internal ClearAllCacheFilesOperation(ResourcePackage package)
{
_package = package;
}
internal override void Start()
{
_steps = ESteps.GetAllCacheFiles;
}
internal override void Update()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.GetAllCacheFiles)
{
_allCacheGUIDs = CacheSystem.GetAllCacheGUIDs(_package);
_fileTotalCount = _allCacheGUIDs.Count;
YooLogger.Log($"Found all cache file count : {_fileTotalCount}");
_steps = ESteps.ClearAllCacheFiles;
}
if (_steps == ESteps.ClearAllCacheFiles)
{
for (int i = _allCacheGUIDs.Count - 1; i >= 0; i--)
{
string cacheGUID = _allCacheGUIDs[i];
CacheSystem.DiscardFile(_package.PackageName, cacheGUID);
_allCacheGUIDs.RemoveAt(i);
if (OperationSystem.IsBusy)
break;
}
if (_fileTotalCount == 0)
Progress = 1.0f;
else
Progress = 1.0f - (_allCacheGUIDs.Count / _fileTotalCount);
if (_allCacheGUIDs.Count == 0)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
}
}
}

View File

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

View File

@ -251,6 +251,17 @@ namespace YooAsset
return operation;
}
/// <summary>
/// 清理包裹本地所有的缓存文件
/// </summary>
public ClearAllCacheFilesOperation ClearAllCacheFilesAsync()
{
DebugCheckInitialize();
var operation = new ClearAllCacheFilesOperation(this);
OperationSystem.StartOperation(operation);
return operation;
}
/// <summary>
/// 获取本地包裹的版本信息
/// </summary>