Update PatchSystem

增加清理缓存资源的异步操作类
pull/40/head
hevinci 2022-09-06 18:19:50 +08:00
parent f112f18723
commit 3f518ec321
5 changed files with 167 additions and 13 deletions

View File

@ -13,6 +13,17 @@ namespace YooAsset
private static long _maxTimeSlice;
private static long _frameTime;
/// <summary>
/// 处理器是否繁忙
/// </summary>
public static bool IsBusy
{
get
{
return _watch.ElapsedMilliseconds - _frameTime >= _maxTimeSlice;
}
}
/// <summary>
/// 初始化异步操作系统
@ -32,7 +43,7 @@ namespace YooAsset
for (int i = _operations.Count - 1; i >= 0; i--)
{
if (_watch.ElapsedMilliseconds - _frameTime >= _maxTimeSlice)
if (IsBusy)
return;
var operation = _operations[i];

View File

@ -0,0 +1,111 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 清理未使用的缓存资源操作类
/// </summary>
public abstract class ClearUnusedCacheFilesOperation : AsyncOperationBase
{
}
/// <summary>
/// 编辑器模式
/// </summary>
internal sealed class EditorPlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation
{
internal override void Start()
{
Status = EOperationStatus.Succeed;
}
internal override void Update()
{
}
}
/// <summary>
/// 离线模式
/// </summary>
internal sealed class OfflinePlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation
{
internal override void Start()
{
Status = EOperationStatus.Succeed;
}
internal override void Update()
{
}
}
/// <summary>
/// 联机模式
/// </summary>
internal sealed class HostPlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation
{
private enum ESteps
{
None,
GetUnusedCacheFiles,
ClearUnusedCacheFiles,
Done,
}
private ESteps _steps = ESteps.None;
private List<string> _unusedCacheFilePaths;
private int _unusedFileTotalCount = 0;
private HostPlayModeImpl _impl;
internal HostPlayModeClearUnusedCacheFilesOperation(HostPlayModeImpl impl)
{
_impl = impl;
}
internal override void Start()
{
_steps = ESteps.GetUnusedCacheFiles;
}
internal override void Update()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.GetUnusedCacheFiles)
{
_unusedCacheFilePaths = _impl.ClearUnusedCacheFilePaths();
_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))
{
YooLogger.Log($"Delete unused cache file : {filePath}");
File.Delete(filePath);
}
_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;
}
}
}
}
}

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

@ -88,16 +88,18 @@ namespace YooAsset
}
/// <summary>
/// 清空未被使用的缓存文件
/// 获取未被使用的缓存文件路径集合
/// </summary>
public void ClearUnusedCacheFiles()
public List<string> ClearUnusedCacheFilePaths()
{
string cacheFolderPath = SandboxHelper.GetCacheFolderPath();
if (Directory.Exists(cacheFolderPath) == false)
return;
return new List<string>();
DirectoryInfo directoryInfo = new DirectoryInfo(cacheFolderPath);
foreach (FileInfo fileInfo in directoryInfo.GetFiles())
FileInfo[] fileInfos = directoryInfo.GetFiles();
List<string> result = new List<string>(fileInfos.Length);
foreach (FileInfo fileInfo in fileInfos)
{
bool used = false;
foreach (var patchBundle in LocalPatchManifest.BundleList)
@ -109,11 +111,9 @@ namespace YooAsset
}
}
if (used == false)
{
YooLogger.Log($"Delete unused cache file : {fileInfo.Name}");
File.Delete(fileInfo.FullName);
}
result.Add(fileInfo.FullName);
}
return result;
}
/// <summary>

View File

@ -1040,10 +1040,31 @@ namespace YooAsset
/// <summary>
/// 清空未被使用的缓存文件
/// </summary>
public static void ClearUnusedCacheFiles()
public static ClearUnusedCacheFilesOperation ClearUnusedCacheFiles()
{
if (_playMode == EPlayMode.HostPlayMode)
_hostPlayModeImpl.ClearUnusedCacheFiles();
DebugCheckInitialize();
if (_playMode == EPlayMode.EditorSimulateMode)
{
var operation = new EditorPlayModeClearUnusedCacheFilesOperation();
OperationSystem.StartOperation(operation);
return operation;
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
var operation = new OfflinePlayModeClearUnusedCacheFilesOperation();
OperationSystem.StartOperation(operation);
return operation;
}
else if (_playMode == EPlayMode.HostPlayMode)
{
var operation = new HostPlayModeClearUnusedCacheFilesOperation(_hostPlayModeImpl);
OperationSystem.StartOperation(operation);
return operation;
}
else
{
throw new NotImplementedException();
}
}
#endregion