update cache system

优化了缓存系统初始化逻辑,支持分帧获取所有缓存文件。
pull/72/head
hevinci 2023-02-28 18:25:27 +08:00
parent 7c4dbb1f38
commit ebcb05cc55
8 changed files with 257 additions and 67 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a93a516506b2b5c4492fdefe26eb1175
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,168 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
internal class FindCacheFilesOperation : AsyncOperationBase
{
private enum ESteps
{
None,
FindPrepare,
FindBundleFiles,
FindRawFiles,
Done,
}
private readonly string _packageName;
private float _verifyStartTime;
private IEnumerator<DirectoryInfo> _bundleFilesEnumerator = null;
private IEnumerator<DirectoryInfo> _rawFilesEnumerator = null;
private ESteps _steps = ESteps.None;
/// <summary>
/// 需要验证的元素
/// </summary>
public readonly List<VerifyElement> VerifyElements = new List<VerifyElement>(5000);
public FindCacheFilesOperation(string packageName)
{
_packageName = packageName;
}
internal override void Start()
{
_steps = ESteps.FindPrepare;
_verifyStartTime = UnityEngine.Time.realtimeSinceStartup;
}
internal override void Update()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.FindPrepare)
{
// BundleFiles
{
string rootPath = PersistentHelper.GetCachedBundleFileFolderPath(_packageName);
DirectoryInfo rootDirectory = new DirectoryInfo(rootPath);
if (rootDirectory.Exists)
{
var directorieInfos = rootDirectory.EnumerateDirectories();
_bundleFilesEnumerator = directorieInfos.GetEnumerator();
}
}
// RawFiles
{
string rootPath = PersistentHelper.GetCachedRawFileFolderPath(_packageName);
DirectoryInfo rootDirectory = new DirectoryInfo(rootPath);
if (rootDirectory.Exists)
{
var directorieInfos = rootDirectory.EnumerateDirectories();
_rawFilesEnumerator = directorieInfos.GetEnumerator();
}
}
_steps = ESteps.FindBundleFiles;
}
if (_steps == ESteps.FindBundleFiles)
{
if (UpdateFindBundleFiles())
return;
_steps = ESteps.FindRawFiles;
}
if (_steps == ESteps.FindRawFiles)
{
if (UpdateFindRawFiles())
return;
// 注意:总是返回成功
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime;
YooLogger.Log($"Find cache files elapsed time {costTime:f1} seconds");
}
}
private bool UpdateFindBundleFiles()
{
if (_bundleFilesEnumerator == null)
return false;
bool isFindItem;
while (true)
{
isFindItem = _bundleFilesEnumerator.MoveNext();
if (isFindItem == false)
break;
var fileFoder = _bundleFilesEnumerator.Current;
string cacheGUID = fileFoder.Name;
if (CacheSystem.IsCached(_packageName, cacheGUID))
continue;
// 创建验证元素类
string fileRootPath = fileFoder.FullName;
string dataFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleDataFileName}";
string infoFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleInfoFileName}";
VerifyElement element = new VerifyElement(_packageName, cacheGUID, fileRootPath, dataFilePath, infoFilePath);
VerifyElements.Add(element);
if (OperationSystem.IsBusy)
break;
}
return isFindItem;
}
private bool UpdateFindRawFiles()
{
if (_rawFilesEnumerator == null)
return false;
bool isFindItem;
while (true)
{
isFindItem = _rawFilesEnumerator.MoveNext();
if (isFindItem == false)
break;
var fileFoder = _rawFilesEnumerator.Current;
string cacheGUID = fileFoder.Name;
if (CacheSystem.IsCached(_packageName, cacheGUID))
continue;
// 获取数据文件的后缀名
string dataFileExtension = string.Empty;
var fileInfos = fileFoder.GetFiles();
foreach (var fileInfo in fileInfos)
{
if (fileInfo.Extension == ".temp")
continue;
if (fileInfo.Name.StartsWith(YooAssetSettings.CacheBundleDataFileName))
{
dataFileExtension = fileInfo.Extension;
break;
}
}
// 创建验证元素类
string fileRootPath = fileFoder.FullName;
string dataFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleDataFileName}{dataFileExtension}";
string infoFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleInfoFileName}";
VerifyElement element = new VerifyElement(_packageName, cacheGUID, fileRootPath, dataFilePath, infoFilePath);
VerifyElements.Add(element);
if (OperationSystem.IsBusy)
break;
}
return isFindItem;
}
}
}

View File

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

View File

@ -6,14 +6,14 @@ using System.Threading;
namespace YooAsset namespace YooAsset
{ {
internal abstract class PackageVerifyOperation : AsyncOperationBase internal abstract class VerifyCacheFilesOperation : AsyncOperationBase
{ {
public static PackageVerifyOperation CreateOperation(List<VerifyElement> elements) public static VerifyCacheFilesOperation CreateOperation(List<VerifyElement> elements)
{ {
#if UNITY_WEBGL #if UNITY_WEBGL
var operation = new PackageVerifyWithoutThreadOperation(elements); var operation = new VerifyCacheFilesWithoutThreadOperation(elements);
#else #else
var operation = new PackageVerifyWithThreadOperation(elements); var operation = new VerifyCacheFilesWithThreadOperation(elements);
#endif #endif
return operation; return operation;
} }
@ -22,7 +22,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 本地缓存文件验证(线程版) /// 本地缓存文件验证(线程版)
/// </summary> /// </summary>
internal class PackageVerifyWithThreadOperation : PackageVerifyOperation internal class VerifyCacheFilesWithThreadOperation : VerifyCacheFilesOperation
{ {
private enum ESteps private enum ESteps
{ {
@ -42,7 +42,7 @@ namespace YooAsset
private int _failedCount; private int _failedCount;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
public PackageVerifyWithThreadOperation(List<VerifyElement> elements) public VerifyCacheFilesWithThreadOperation(List<VerifyElement> elements)
{ {
_waitingList = elements; _waitingList = elements;
} }
@ -82,7 +82,7 @@ namespace YooAsset
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Succeed; Status = EOperationStatus.Succeed;
float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime; float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime;
YooLogger.Log($"Package verify elapsed time {costTime:f1} seconds"); YooLogger.Log($"Verify cache files elapsed time {costTime:f1} seconds");
} }
for (int i = _waitingList.Count - 1; i >= 0; i--) for (int i = _waitingList.Count - 1; i >= 0; i--)
@ -148,7 +148,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 本地缓存文件验证(非线程版) /// 本地缓存文件验证(非线程版)
/// </summary> /// </summary>
internal class PackageVerifyWithoutThreadOperation : PackageVerifyOperation internal class VerifyCacheFilesWithoutThreadOperation : VerifyCacheFilesOperation
{ {
private enum ESteps private enum ESteps
{ {
@ -167,7 +167,7 @@ namespace YooAsset
private int _failedCount; private int _failedCount;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
public PackageVerifyWithoutThreadOperation(List<VerifyElement> elements) public VerifyCacheFilesWithoutThreadOperation(List<VerifyElement> elements)
{ {
_waitingList = elements; _waitingList = elements;
} }

View File

@ -10,13 +10,14 @@ namespace YooAsset
private enum ESteps private enum ESteps
{ {
None, None,
GetCacheFiles, FindCacheFiles,
VerifyCacheFiles, VerifyCacheFiles,
Done, Done,
} }
private readonly string _packageName; private readonly string _packageName;
private PackageVerifyOperation _packageVerifyOp; private FindCacheFilesOperation _findCacheFilesOp;
private VerifyCacheFilesOperation _verifyCacheFilesOp;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
public PackageCachingOperation(string packageName) public PackageCachingOperation(string packageName)
@ -25,25 +26,40 @@ namespace YooAsset
} }
internal override void Start() internal override void Start()
{ {
_steps = ESteps.GetCacheFiles; _steps = ESteps.FindCacheFiles;
} }
internal override void Update() internal override void Update()
{ {
if (_steps == ESteps.None || _steps == ESteps.Done) if (_steps == ESteps.None || _steps == ESteps.Done)
return; return;
if (_steps == ESteps.GetCacheFiles) if (_steps == ESteps.FindCacheFiles)
{ {
var elements = GetVerifyElements(); if (_findCacheFilesOp == null)
_packageVerifyOp = PackageVerifyOperation.CreateOperation(elements); {
OperationSystem.StartOperation(_packageVerifyOp); _findCacheFilesOp = new FindCacheFilesOperation(_packageName);
OperationSystem.StartOperation(_findCacheFilesOp);
_steps = ESteps.VerifyCacheFiles;
}
Progress = _findCacheFilesOp.Progress;
if (_findCacheFilesOp.IsDone == false)
return;
_steps = ESteps.VerifyCacheFiles; _steps = ESteps.VerifyCacheFiles;
} }
if (_steps == ESteps.VerifyCacheFiles) if (_steps == ESteps.VerifyCacheFiles)
{ {
Progress = _packageVerifyOp.Progress; if (_verifyCacheFilesOp == null)
if (_packageVerifyOp.IsDone == false) {
_verifyCacheFilesOp = VerifyCacheFilesOperation.CreateOperation(_findCacheFilesOp.VerifyElements);
OperationSystem.StartOperation(_verifyCacheFilesOp);
_steps = ESteps.VerifyCacheFiles;
}
Progress = _verifyCacheFilesOp.Progress;
if (_verifyCacheFilesOp.IsDone == false)
return; return;
// 注意:总是返回成功 // 注意:总是返回成功
@ -51,45 +67,5 @@ namespace YooAsset
Status = EOperationStatus.Succeed; Status = EOperationStatus.Succeed;
} }
} }
private List<VerifyElement> GetVerifyElements()
{
string cacheFolderPath = PersistentHelper.GetCacheFolderPath(_packageName);
if (Directory.Exists(cacheFolderPath) == false)
return new List<VerifyElement>();
DirectoryInfo rootDirectory = new DirectoryInfo(cacheFolderPath);
DirectoryInfo[] fileFolders = rootDirectory.GetDirectories();
List<VerifyElement> result = new List<VerifyElement>(fileFolders.Length);
foreach (var fileFoder in fileFolders)
{
string cacheGUID = fileFoder.Name;
if (CacheSystem.IsCached(_packageName, cacheGUID))
continue;
// 获取数据文件的后缀名
string dataFileExtension = string.Empty;
var fileInfos = fileFoder.GetFiles();
foreach (var fileInfo in fileInfos)
{
if (fileInfo.Extension == ".temp")
continue;
if (fileInfo.Name.StartsWith(YooAssetSettings.CacheBundleDataFileName))
{
dataFileExtension = fileInfo.Extension;
break;
}
}
string fileRootPath = fileFoder.FullName;
string dataFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleDataFileName}{dataFileExtension}";
string infoFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleInfoFileName}";
VerifyElement element = new VerifyElement(_packageName, cacheGUID, fileRootPath, dataFilePath, infoFilePath);
result.Add(element);
}
return result;
}
} }
} }

View File

@ -71,8 +71,16 @@ namespace YooAsset
if (string.IsNullOrEmpty(_cachedDataFilePath) == false) if (string.IsNullOrEmpty(_cachedDataFilePath) == false)
return _cachedDataFilePath; return _cachedDataFilePath;
string cacheRoot = PersistentHelper.GetCacheFolderPath(PackageName); if (IsRawFile)
_cachedDataFilePath = $"{cacheRoot}/{CacheGUID}/{YooAssetSettings.CacheBundleDataFileName}{_fileExtension}"; {
string cacheRoot = PersistentHelper.GetCachedRawFileFolderPath(PackageName);
_cachedDataFilePath = $"{cacheRoot}/{CacheGUID}/{YooAssetSettings.CacheBundleDataFileName}{_fileExtension}";
}
else
{
string cacheRoot = PersistentHelper.GetCachedBundleFileFolderPath(PackageName);
_cachedDataFilePath = $"{cacheRoot}/{CacheGUID}/{YooAssetSettings.CacheBundleDataFileName}";
}
return _cachedDataFilePath; return _cachedDataFilePath;
} }
} }
@ -88,8 +96,16 @@ namespace YooAsset
if (string.IsNullOrEmpty(_cachedInfoFilePath) == false) if (string.IsNullOrEmpty(_cachedInfoFilePath) == false)
return _cachedInfoFilePath; return _cachedInfoFilePath;
string cacheRoot = PersistentHelper.GetCacheFolderPath(PackageName); if (IsRawFile)
_cachedInfoFilePath = $"{cacheRoot}/{CacheGUID}/{YooAssetSettings.CacheBundleInfoFileName}"; {
string cacheRoot = PersistentHelper.GetCachedRawFileFolderPath(PackageName);
_cachedInfoFilePath = $"{cacheRoot}/{CacheGUID}/{YooAssetSettings.CacheBundleInfoFileName}";
}
else
{
string cacheRoot = PersistentHelper.GetCachedBundleFileFolderPath(PackageName);
_cachedInfoFilePath = $"{cacheRoot}/{CacheGUID}/{YooAssetSettings.CacheBundleInfoFileName}";
}
return _cachedInfoFilePath; return _cachedInfoFilePath;
} }
} }

View File

@ -84,6 +84,8 @@ namespace YooAsset
internal static class PersistentHelper internal static class PersistentHelper
{ {
private const string CacheFolderName = "CacheFiles"; private const string CacheFolderName = "CacheFiles";
private const string CachedBundleFileFolder = "BundleFiles";
private const string CachedRawFileFolder = "RawFiles";
private const string ManifestFolderName = "ManifestFiles"; private const string ManifestFolderName = "ManifestFiles";
private const string AppFootPrintFileName = "ApplicationFootPrint.bytes"; private const string AppFootPrintFileName = "ApplicationFootPrint.bytes";
@ -120,12 +122,21 @@ namespace YooAsset
/// <summary> /// <summary>
/// 获取缓存文件夹路径 /// 获取缓存的BundleFile文件夹路径
/// </summary> /// </summary>
public static string GetCacheFolderPath(string packageName) public static string GetCachedBundleFileFolderPath(string packageName)
{ {
string root = PathHelper.MakePersistentLoadPath(CacheFolderName); string root = PathHelper.MakePersistentLoadPath(CacheFolderName);
return $"{root}/{packageName}"; return $"{root}/{packageName}/{CachedBundleFileFolder}";
}
/// <summary>
/// 获取缓存的RawFile文件夹路径
/// </summary>
public static string GetCachedRawFileFolderPath(string packageName)
{
string root = PathHelper.MakePersistentLoadPath(CacheFolderName);
return $"{root}/{packageName}/{CachedRawFileFolder}";
} }
/// <summary> /// <summary>