feat : add GetAllCacheFileInfosAsync method

pull/241/merge
hevinci 2024-02-18 12:11:29 +08:00
parent 7586882a97
commit 6eb9a90a03
9 changed files with 204 additions and 65 deletions

View File

@ -1,36 +1,19 @@
using System;
using System.IO;

namespace YooAsset
{
internal class CacheFileInfo
public class CacheFileInfo
{
private static readonly BufferWriter SharedBuffer = new BufferWriter(1024);
public string RemoteFileName { private set; get; }
public string FilePath { private set; get; }
public string FileCRC { private set; get; }
public long FileSize { private set; get; }
/// <summary>
/// 写入资源包信息
/// </summary>
public static void WriteInfoToFile(string filePath, string dataFileCRC, long dataFileSize)
public CacheFileInfo(string remoteFileName, string filePath, string fileCRC, long fileSize)
{
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
SharedBuffer.Clear();
SharedBuffer.WriteUTF8(dataFileCRC);
SharedBuffer.WriteInt64(dataFileSize);
SharedBuffer.WriteToStream(fs);
fs.Flush();
}
}
/// <summary>
/// 读取资源包信息
/// </summary>
public static void ReadInfoFromFile(string filePath, out string dataFileCRC, out long dataFileSize)
{
byte[] binaryData = FileUtility.ReadAllBytes(filePath);
BufferReader buffer = new BufferReader(binaryData);
dataFileCRC = buffer.ReadUTF8();
dataFileSize = buffer.ReadInt64();
RemoteFileName = remoteFileName;
FilePath = filePath;
FileCRC = fileCRC;
FileSize = fileSize;
}
}
}

View File

@ -1,8 +1,5 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace YooAsset
{
@ -13,6 +10,37 @@ namespace YooAsset
/// </summary>
public static bool DisableUnityCacheOnWebGL = false;
#region 资源信息文件相关
private static readonly BufferWriter SharedBuffer = new BufferWriter(1024);
/// <summary>
/// 写入资源包信息
/// </summary>
public static void WriteInfoToFile(string filePath, string dataFileCRC, long dataFileSize)
{
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
SharedBuffer.Clear();
SharedBuffer.WriteUTF8(dataFileCRC);
SharedBuffer.WriteInt64(dataFileSize);
SharedBuffer.WriteToStream(fs);
fs.Flush();
}
}
/// <summary>
/// 读取资源包信息
/// </summary>
public static void ReadInfoFromFile(string filePath, out string dataFileCRC, out long dataFileSize)
{
byte[] binaryData = FileUtility.ReadAllBytes(filePath);
BufferReader buffer = new BufferReader(binaryData);
dataFileCRC = buffer.ReadUTF8();
dataFileSize = buffer.ReadInt64();
}
#endregion
#region 资源文件验证相关
/// <summary>
/// 验证缓存文件(子线程内操作)
/// </summary>
@ -34,7 +62,7 @@ namespace YooAsset
return EVerifyResult.InfoFileNotExisted;
// 解析信息文件获取验证数据
CacheFileInfo.ReadInfoFromFile(element.InfoFilePath, out element.DataFileCRC, out element.DataFileSize);
CacheHelper.ReadInfoFromFile(element.InfoFilePath, out element.DataFileCRC, out element.DataFileSize);
}
}
catch (Exception)
@ -99,5 +127,6 @@ namespace YooAsset
return EVerifyResult.Exception;
}
}
#endregion
}
}

View File

@ -0,0 +1,99 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
public class GetAllCacheFileInfosOperation : AsyncOperationBase
{
private enum ESteps
{
None,
TryLoadCacheManifest,
GetCacheFileInfos,
Done,
}
private readonly PersistentManager _persistent;
private readonly CacheManager _cache;
private readonly string _packageVersion;
private LoadCacheManifestOperation _tryLoadCacheManifestOp;
private PackageManifest _manifest;
private ESteps _steps = ESteps.None;
private List<CacheFileInfo> _cacheFileInfos;
/// <summary>
/// 搜索结果
/// </summary>
public List<CacheFileInfo> Result
{
get { return _cacheFileInfos; }
}
internal GetAllCacheFileInfosOperation(PersistentManager persistent, CacheManager cache, string packageVersion)
{
_persistent = persistent;
_cache = cache;
_packageVersion = packageVersion;
}
internal override void InternalOnStart()
{
_steps = ESteps.TryLoadCacheManifest;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.TryLoadCacheManifest)
{
if (_tryLoadCacheManifestOp == null)
{
_tryLoadCacheManifestOp = new LoadCacheManifestOperation(_persistent, _packageVersion);
OperationSystem.StartOperation(_cache.PackageName, _tryLoadCacheManifestOp);
}
if (_tryLoadCacheManifestOp.IsDone == false)
return;
if (_tryLoadCacheManifestOp.Status == EOperationStatus.Succeed)
{
_manifest = _tryLoadCacheManifestOp.Manifest;
_steps = ESteps.GetCacheFileInfos;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _tryLoadCacheManifestOp.Error;
}
}
if (_steps == ESteps.GetCacheFileInfos)
{
var allCachedGUIDs = _cache.GetAllCachedGUIDs();
_cacheFileInfos = new List<CacheFileInfo>(allCachedGUIDs.Count);
for (int i = 0; i < allCachedGUIDs.Count; i++)
{
var cachedGUID = allCachedGUIDs[i];
var wrapper = _cache.TryGetWrapper(cachedGUID);
if (wrapper != null)
{
if (_manifest.TryGetPackageBundleByCacheGUID(cachedGUID, out var packageBundle))
{
var cacheFileInfo = new CacheFileInfo(packageBundle.FileName, wrapper.DataFilePath, wrapper.DataFileCRC, wrapper.DataFileSize);
_cacheFileInfos.Add(cacheFileInfo);
}
}
}
// 注意:总是返回成功
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
}
}

View File

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

View File

@ -207,7 +207,7 @@ namespace YooAsset
fileInfo.MoveTo(dataFilePath);
// 写入信息文件记录验证数据
CacheFileInfo.WriteInfoToFile(infoFilePath, dataFileCRC, dataFileSize);
CacheHelper.WriteInfoToFile(infoFilePath, dataFileCRC, dataFileSize);
// 记录缓存文件
_bundleInfo.CacheRecord();

View File

@ -190,6 +190,7 @@ namespace YooAsset
Manifest.BundleList = new List<PackageBundle>(_packageBundleCount);
Manifest.BundleDic1 = new Dictionary<string, PackageBundle>(_packageBundleCount);
Manifest.BundleDic2 = new Dictionary<string, PackageBundle>(_packageBundleCount);
Manifest.BundleDic3 = new Dictionary<string, PackageBundle>(_packageBundleCount);
_progressTotalValue = _packageBundleCount;
_steps = ESteps.DeserializeBundleList;
}
@ -212,8 +213,8 @@ namespace YooAsset
Manifest.BundleDic2.Add(packageBundle.FileName, packageBundle);
// 注意原始文件可能存在相同的CacheGUID
if (Manifest.CacheGUIDs.Contains(packageBundle.CacheGUID) == false)
Manifest.CacheGUIDs.Add(packageBundle.CacheGUID);
if (Manifest.BundleDic3.ContainsKey(packageBundle.CacheGUID) == false)
Manifest.BundleDic3.Add(packageBundle.CacheGUID, packageBundle);
_packageBundleCount--;
Progress = 1f - _packageBundleCount / _progressTotalValue;

View File

@ -134,7 +134,6 @@ namespace YooAsset
TryLoadCacheManifest,
DownloadManifest,
LoadCacheManifest,
CheckDeserializeManifest,
Done,
}

View File

@ -75,6 +75,12 @@ namespace YooAsset
[NonSerialized]
public Dictionary<string, PackageBundle> BundleDic2;
/// <summary>
/// 资源包集合提供CacheGUID获取PackageBundle
/// </summary>
[NonSerialized]
public Dictionary<string, PackageBundle> BundleDic3;
/// <summary>
/// 资源映射集合提供AssetPath获取PackageAsset
/// </summary>
@ -93,12 +99,6 @@ namespace YooAsset
[NonSerialized]
public Dictionary<string, string> AssetPathMapping2;
/// <summary>
/// 该资源清单所有文件的缓存GUID集合
/// </summary>
[NonSerialized]
public HashSet<string> CacheGUIDs = new HashSet<string>();
/// <summary>
/// 尝试映射为资源路径
@ -189,12 +189,20 @@ namespace YooAsset
return BundleDic2.TryGetValue(fileName, out result);
}
/// <summary>
/// 尝试获取包裹的资源包
/// </summary>
public bool TryGetPackageBundleByCacheGUID(string cacheGUID, out PackageBundle result)
{
return BundleDic3.TryGetValue(cacheGUID, out result);
}
/// <summary>
/// 是否包含资源文件
/// </summary>
public bool IsIncludeBundleFile(string cacheGUID)
{
return CacheGUIDs.Contains(cacheGUID);
return BundleDic3.ContainsKey(cacheGUID);
}
/// <summary>

View File

@ -314,28 +314,6 @@ namespace YooAsset
return _playModeImpl.PreDownloadContentAsync(packageVersion, timeout);
}
/// <summary>
/// 清理包裹未使用的缓存文件
/// </summary>
public ClearUnusedCacheFilesOperation ClearUnusedCacheFilesAsync()
{
DebugCheckInitialize();
var operation = new ClearUnusedCacheFilesOperation(this, _cacheMgr);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
/// <summary>
/// 清理包裹本地所有的缓存文件
/// </summary>
public ClearAllCacheFilesOperation ClearAllCacheFilesAsync()
{
DebugCheckInitialize();
var operation = new ClearAllCacheFilesOperation(_cacheMgr);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
/// <summary>
/// 获取本地包裹的版本信息
/// </summary>
@ -412,6 +390,37 @@ namespace YooAsset
_persistentMgr.DeleteSandboxPackageFolder();
_cacheMgr.ClearAll();
}
/// <summary>
/// 清理包裹未使用的缓存文件
/// </summary>
public ClearUnusedCacheFilesOperation ClearUnusedCacheFilesAsync()
{
DebugCheckInitialize();
var operation = new ClearUnusedCacheFilesOperation(this, _cacheMgr);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
/// <summary>
/// 清理包裹本地所有的缓存文件
/// </summary>
public ClearAllCacheFilesOperation ClearAllCacheFilesAsync()
{
DebugCheckInitialize();
var operation = new ClearAllCacheFilesOperation(_cacheMgr);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public GetAllCacheFileInfosOperation GetAllCacheFileInfosAsync(string packageVersion)
{
DebugCheckInitialize();
var operation = new GetAllCacheFileInfosOperation(_persistentMgr, _cacheMgr, packageVersion);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
#endregion
#region 资源信息