mirror of https://github.com/tuyoogame/YooAsset
feat : add GetAllCacheFileInfosAsync method
parent
7586882a97
commit
6eb9a90a03
|
@ -1,36 +1,19 @@
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace YooAsset
|
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>
|
public CacheFileInfo(string remoteFileName, string filePath, string fileCRC, long fileSize)
|
||||||
/// 写入资源包信息
|
|
||||||
/// </summary>
|
|
||||||
public static void WriteInfoToFile(string filePath, string dataFileCRC, long dataFileSize)
|
|
||||||
{
|
{
|
||||||
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read))
|
RemoteFileName = remoteFileName;
|
||||||
{
|
FilePath = filePath;
|
||||||
SharedBuffer.Clear();
|
FileCRC = fileCRC;
|
||||||
SharedBuffer.WriteUTF8(dataFileCRC);
|
FileSize = fileSize;
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,8 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics;
|
|
||||||
|
|
||||||
namespace YooAsset
|
namespace YooAsset
|
||||||
{
|
{
|
||||||
|
@ -13,6 +10,37 @@ namespace YooAsset
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static bool DisableUnityCacheOnWebGL = false;
|
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>
|
||||||
/// 验证缓存文件(子线程内操作)
|
/// 验证缓存文件(子线程内操作)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -34,7 +62,7 @@ namespace YooAsset
|
||||||
return EVerifyResult.InfoFileNotExisted;
|
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)
|
catch (Exception)
|
||||||
|
@ -99,5 +127,6 @@ namespace YooAsset
|
||||||
return EVerifyResult.Exception;
|
return EVerifyResult.Exception;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: d37e37f5d78ddf8468adcf2dff1edfbb
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -207,7 +207,7 @@ namespace YooAsset
|
||||||
fileInfo.MoveTo(dataFilePath);
|
fileInfo.MoveTo(dataFilePath);
|
||||||
|
|
||||||
// 写入信息文件记录验证数据
|
// 写入信息文件记录验证数据
|
||||||
CacheFileInfo.WriteInfoToFile(infoFilePath, dataFileCRC, dataFileSize);
|
CacheHelper.WriteInfoToFile(infoFilePath, dataFileCRC, dataFileSize);
|
||||||
|
|
||||||
// 记录缓存文件
|
// 记录缓存文件
|
||||||
_bundleInfo.CacheRecord();
|
_bundleInfo.CacheRecord();
|
||||||
|
|
|
@ -190,6 +190,7 @@ namespace YooAsset
|
||||||
Manifest.BundleList = new List<PackageBundle>(_packageBundleCount);
|
Manifest.BundleList = new List<PackageBundle>(_packageBundleCount);
|
||||||
Manifest.BundleDic1 = new Dictionary<string, PackageBundle>(_packageBundleCount);
|
Manifest.BundleDic1 = new Dictionary<string, PackageBundle>(_packageBundleCount);
|
||||||
Manifest.BundleDic2 = new Dictionary<string, PackageBundle>(_packageBundleCount);
|
Manifest.BundleDic2 = new Dictionary<string, PackageBundle>(_packageBundleCount);
|
||||||
|
Manifest.BundleDic3 = new Dictionary<string, PackageBundle>(_packageBundleCount);
|
||||||
_progressTotalValue = _packageBundleCount;
|
_progressTotalValue = _packageBundleCount;
|
||||||
_steps = ESteps.DeserializeBundleList;
|
_steps = ESteps.DeserializeBundleList;
|
||||||
}
|
}
|
||||||
|
@ -212,8 +213,8 @@ namespace YooAsset
|
||||||
Manifest.BundleDic2.Add(packageBundle.FileName, packageBundle);
|
Manifest.BundleDic2.Add(packageBundle.FileName, packageBundle);
|
||||||
|
|
||||||
// 注意:原始文件可能存在相同的CacheGUID
|
// 注意:原始文件可能存在相同的CacheGUID
|
||||||
if (Manifest.CacheGUIDs.Contains(packageBundle.CacheGUID) == false)
|
if (Manifest.BundleDic3.ContainsKey(packageBundle.CacheGUID) == false)
|
||||||
Manifest.CacheGUIDs.Add(packageBundle.CacheGUID);
|
Manifest.BundleDic3.Add(packageBundle.CacheGUID, packageBundle);
|
||||||
|
|
||||||
_packageBundleCount--;
|
_packageBundleCount--;
|
||||||
Progress = 1f - _packageBundleCount / _progressTotalValue;
|
Progress = 1f - _packageBundleCount / _progressTotalValue;
|
||||||
|
|
|
@ -134,7 +134,6 @@ namespace YooAsset
|
||||||
TryLoadCacheManifest,
|
TryLoadCacheManifest,
|
||||||
DownloadManifest,
|
DownloadManifest,
|
||||||
LoadCacheManifest,
|
LoadCacheManifest,
|
||||||
CheckDeserializeManifest,
|
|
||||||
Done,
|
Done,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -75,6 +75,12 @@ namespace YooAsset
|
||||||
[NonSerialized]
|
[NonSerialized]
|
||||||
public Dictionary<string, PackageBundle> BundleDic2;
|
public Dictionary<string, PackageBundle> BundleDic2;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源包集合(提供CacheGUID获取PackageBundle)
|
||||||
|
/// </summary>
|
||||||
|
[NonSerialized]
|
||||||
|
public Dictionary<string, PackageBundle> BundleDic3;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 资源映射集合(提供AssetPath获取PackageAsset)
|
/// 资源映射集合(提供AssetPath获取PackageAsset)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -93,12 +99,6 @@ namespace YooAsset
|
||||||
[NonSerialized]
|
[NonSerialized]
|
||||||
public Dictionary<string, string> AssetPathMapping2;
|
public Dictionary<string, string> AssetPathMapping2;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 该资源清单所有文件的缓存GUID集合
|
|
||||||
/// </summary>
|
|
||||||
[NonSerialized]
|
|
||||||
public HashSet<string> CacheGUIDs = new HashSet<string>();
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 尝试映射为资源路径
|
/// 尝试映射为资源路径
|
||||||
|
@ -189,12 +189,20 @@ namespace YooAsset
|
||||||
return BundleDic2.TryGetValue(fileName, out result);
|
return BundleDic2.TryGetValue(fileName, out result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 尝试获取包裹的资源包
|
||||||
|
/// </summary>
|
||||||
|
public bool TryGetPackageBundleByCacheGUID(string cacheGUID, out PackageBundle result)
|
||||||
|
{
|
||||||
|
return BundleDic3.TryGetValue(cacheGUID, out result);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否包含资源文件
|
/// 是否包含资源文件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsIncludeBundleFile(string cacheGUID)
|
public bool IsIncludeBundleFile(string cacheGUID)
|
||||||
{
|
{
|
||||||
return CacheGUIDs.Contains(cacheGUID);
|
return BundleDic3.ContainsKey(cacheGUID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -314,28 +314,6 @@ namespace YooAsset
|
||||||
return _playModeImpl.PreDownloadContentAsync(packageVersion, timeout);
|
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>
|
||||||
/// 获取本地包裹的版本信息
|
/// 获取本地包裹的版本信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -412,6 +390,37 @@ namespace YooAsset
|
||||||
_persistentMgr.DeleteSandboxPackageFolder();
|
_persistentMgr.DeleteSandboxPackageFolder();
|
||||||
_cacheMgr.ClearAll();
|
_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
|
#endregion
|
||||||
|
|
||||||
#region 资源信息
|
#region 资源信息
|
||||||
|
|
Loading…
Reference in New Issue