Update CacheSystem

优化缓存系统代码结构
This commit is contained in:
hevinci
2022-08-15 11:55:13 +08:00
parent aba68f859f
commit fbbf762e70
26 changed files with 306 additions and 446 deletions
@@ -12,22 +12,9 @@ namespace YooAsset
LoadFromEditor,
}
public readonly PatchBundle LoadBundle;
public readonly PatchBundle Bundle;
public readonly ELoadMode LoadMode;
private string _streamingPath;
private string _cachePath;
/// <summary>
/// 资源包名称
/// </summary>
public string BundleName { private set; get; }
/// <summary>
/// 文件名称
/// </summary>
public string FileName { private set; get; }
/// <summary>
/// 远端下载地址
/// </summary>
@@ -43,147 +30,35 @@ namespace YooAsset
/// </summary>
public string EditorAssetPath { private set; get; }
/// <summary>
/// 文件哈希值
/// </summary>
public string FileHash
{
get
{
if (LoadBundle == null)
return string.Empty;
else
return LoadBundle.FileHash;
}
}
/// <summary>
/// 校验的CRC
/// </summary>
public string FileCRC
{
get
{
if (LoadBundle == null)
return string.Empty;
else
return LoadBundle.FileCRC;
}
}
/// <summary>
/// 文件大小
/// </summary>
public long FileSize
{
get
{
if (LoadBundle == null)
return 0;
else
return LoadBundle.FileSize;
}
}
/// <summary>
/// 是否为加密文件
/// </summary>
public bool IsEncrypted
{
get
{
if (LoadBundle == null)
return false;
else
return LoadBundle.IsEncrypted;
}
}
/// <summary>
/// 是否为原生文件
/// </summary>
public bool IsRawFile
{
get
{
if (LoadBundle == null)
return false;
else
return LoadBundle.IsRawFile;
}
}
/// <summary>
/// 身份是否无效
/// </summary>
public bool IsInvalid
{
get
{
return LoadBundle == null;
}
}
private BundleInfo()
{
}
public BundleInfo(PatchBundle patchBundle, ELoadMode loadMode, string mainURL, string fallbackURL)
{
LoadBundle = patchBundle;
Bundle = patchBundle;
LoadMode = loadMode;
BundleName = patchBundle.BundleName;
FileName = patchBundle.FileName;
RemoteMainURL = mainURL;
RemoteFallbackURL = fallbackURL;
EditorAssetPath = string.Empty;
}
public BundleInfo(PatchBundle patchBundle, ELoadMode loadMode, string editorAssetPath)
{
LoadBundle = patchBundle;
Bundle = patchBundle;
LoadMode = loadMode;
BundleName = patchBundle.BundleName;
FileName = patchBundle.FileName;
RemoteMainURL = string.Empty;
RemoteFallbackURL = string.Empty;
EditorAssetPath = editorAssetPath;
}
public BundleInfo(PatchBundle patchBundle, ELoadMode loadMode)
{
LoadBundle = patchBundle;
Bundle = patchBundle;
LoadMode = loadMode;
BundleName = patchBundle.BundleName;
FileName = patchBundle.FileName;
RemoteMainURL = string.Empty;
RemoteFallbackURL = string.Empty;
EditorAssetPath = string.Empty;
}
/// <summary>
/// 获取流文件夹的加载路径
/// </summary>
public string GetStreamingLoadPath()
{
if (LoadBundle == null)
return string.Empty;
if (string.IsNullOrEmpty(_streamingPath))
_streamingPath = PathHelper.MakeStreamingLoadPath(LoadBundle.FileName);
return _streamingPath;
}
/// <summary>
/// 获取缓存文件夹的加载路径
/// </summary>
public string GetCacheLoadPath()
{
if (LoadBundle == null)
return string.Empty;
if (string.IsNullOrEmpty(_cachePath))
_cachePath = SandboxHelper.MakeCacheFilePath(LoadBundle.FileName);
return _cachePath;
}
/// <summary>
/// 是否为JAR包内文件
@@ -86,7 +86,7 @@ namespace YooAsset
TotalDownloadCount = downloadList.Count;
foreach (var patchBundle in downloadList)
{
TotalDownloadBytes += patchBundle.FileSize;
TotalDownloadBytes += patchBundle.Bundle.FileSize;
}
}
}
@@ -138,7 +138,7 @@ namespace YooAsset
// 下载成功
_removeList.Add(downloader);
CurrentDownloadCount++;
CurrentDownloadBytes += bundleInfo.FileSize;
CurrentDownloadBytes += bundleInfo.Bundle.FileSize;
}
// 移除已经完成的下载器(无论成功或失败)
@@ -170,7 +170,7 @@ namespace YooAsset
var operation = DownloadSystem.BeginDownload(bundleInfo, _failedTryAgain);
_downloaders.Add(operation);
_downloadList.RemoveAt(index);
OnStartDownloadFileCallback?.Invoke(bundleInfo.BundleName, bundleInfo.FileSize);
OnStartDownloadFileCallback?.Invoke(bundleInfo.Bundle.BundleName, bundleInfo.Bundle.FileSize);
}
}
@@ -180,7 +180,7 @@ namespace YooAsset
if (_failedList.Count > 0)
{
var failedDownloader = _failedList[0];
string fileName = failedDownloader.GetBundleInfo().BundleName;
string fileName = failedDownloader.GetBundleInfo().Bundle.BundleName;
Error = $"Failed to download file : {fileName}";
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
@@ -198,7 +198,7 @@ namespace YooAsset
foreach (var patchBundle in _remotePatchManifest.BundleList)
{
// 忽略缓存文件
if (CacheSystem.ContainsVerifyFile(patchBundle))
if (CacheSystem.IsCached(patchBundle))
continue;
// 忽略APP资源
@@ -209,16 +209,6 @@ namespace YooAsset
continue;
}
// 注意:通过比对文件大小做快速的文件校验!
// 注意:在初始化的时候会去做最终校验!
string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.FileName);
if (File.Exists(filePath))
{
long fileSize = FileUtility.GetFileSize(filePath);
if (fileSize == patchBundle.FileSize)
continue;
}
downloadList.Add(patchBundle);
}
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.IO;
namespace YooAsset
{
@@ -11,11 +12,6 @@ namespace YooAsset
/// </summary>
public string BundleName;
/// <summary>
/// 内容哈希值
/// </summary>
public string ContentHash;
/// <summary>
/// 文件哈希值
/// </summary>
@@ -62,11 +58,43 @@ namespace YooAsset
/// </summary>
public string FileName { private set; get; }
/// <summary>
/// 缓存文件路径
/// </summary>
private string _cachedFilePath;
public string CachedFilePath
{
get
{
if (string.IsNullOrEmpty(_cachedFilePath) == false)
return _cachedFilePath;
public PatchBundle(string bundleName, string contentHash, string fileHash, string fileCRC, long fileSize, string[] tags)
string cacheRoot = SandboxHelper.GetCacheFolderPath();
_cachedFilePath = $"{cacheRoot}/{FileName}";
return _cachedFilePath;
}
}
/// <summary>
/// 内置文件路径
/// </summary>
private string _streamingFilePath;
public string StreamingFilePath
{
get
{
if (string.IsNullOrEmpty(_streamingFilePath) == false)
return _streamingFilePath;
_streamingFilePath = PathHelper.MakeStreamingLoadPath(FileName);
return _streamingFilePath;
}
}
public PatchBundle(string bundleName, string fileHash, string fileCRC, long fileSize, string[] tags)
{
BundleName = bundleName;
ContentHash = contentHash;
FileHash = fileHash;
FileCRC = fileCRC;
FileSize = fileSize;
@@ -131,7 +131,7 @@ namespace YooAsset
foreach (var patchBundle in LocalPatchManifest.BundleList)
{
// 忽略缓存文件
if (CacheSystem.ContainsVerifyFile(patchBundle))
if (CacheSystem.IsCached(patchBundle))
continue;
// 忽略APP资源
@@ -163,7 +163,7 @@ namespace YooAsset
foreach (var patchBundle in LocalPatchManifest.BundleList)
{
// 忽略缓存文件
if (CacheSystem.ContainsVerifyFile(patchBundle))
if (CacheSystem.IsCached(patchBundle))
continue;
// 忽略APP资源
@@ -233,7 +233,7 @@ namespace YooAsset
foreach (var patchBundle in checkList)
{
// 忽略缓存文件
if (CacheSystem.ContainsVerifyFile(patchBundle))
if (CacheSystem.IsCached(patchBundle))
continue;
// 忽略APP资源
@@ -269,7 +269,7 @@ namespace YooAsset
continue;
// 忽略缓存文件
if (CacheSystem.ContainsVerifyFile(patchBundle))
if (CacheSystem.IsCached(patchBundle))
continue;
// 查询DLC资源
@@ -279,7 +279,7 @@ namespace YooAsset
}
}
return ConvertToUnpackList(downloadList);
return PatchHelper.ConvertToUnpackList(downloadList);
}
/// <summary>
@@ -301,13 +301,13 @@ namespace YooAsset
continue;
// 忽略缓存文件
if (CacheSystem.ContainsVerifyFile(patchBundle))
if (CacheSystem.IsCached(patchBundle))
continue;
downloadList.Add(patchBundle);
}
return ConvertToUnpackList(downloadList);
return PatchHelper.ConvertToUnpackList(downloadList);
}
// WEB相关
@@ -339,26 +339,6 @@ namespace YooAsset
return bundleInfo;
}
// 解压相关
public List<BundleInfo> ConvertToUnpackList(List<PatchBundle> unpackList)
{
List<BundleInfo> result = new List<BundleInfo>(unpackList.Count);
foreach (var patchBundle in unpackList)
{
var bundleInfo = ConvertToUnpackInfo(patchBundle);
result.Add(bundleInfo);
}
return result;
}
public BundleInfo ConvertToUnpackInfo(PatchBundle patchBundle)
{
// 注意:我们把流加载路径指定为远端下载地址
string streamingPath = PathHelper.MakeStreamingLoadPath(patchBundle.FileName);
streamingPath = PathHelper.ConvertToWWWPath(streamingPath);
BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromRemote, streamingPath, streamingPath);
return bundleInfo;
}
// 设置资源清单
internal void SetAppPatchManifest(PatchManifest patchManifest)
{
@@ -377,7 +357,7 @@ namespace YooAsset
throw new Exception("Should never get here !");
// 查询沙盒资源
if (CacheSystem.ContainsVerifyFile(patchBundle))
if (CacheSystem.IsCached(patchBundle))
{
BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromCache);
return bundleInfo;
@@ -43,8 +43,18 @@ namespace YooAsset
if (patchBundle == null)
throw new Exception("Should never get here !");
BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromStreaming);
return bundleInfo;
// 查询沙盒资源
if (CacheSystem.IsCached(patchBundle))
{
BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromCache);
return bundleInfo;
}
// 查询APP资源
{
BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromStreaming);
return bundleInfo;
}
}
BundleInfo IBundleServices.GetBundleInfo(AssetInfo assetInfo)
{