Update runtime code

优化资源包缓存文件清理方式。
pull/51/head
hevinci 2022-11-18 21:33:58 +08:00
parent 1dab0f2b19
commit 49949ecda5
17 changed files with 115 additions and 90 deletions

View File

@ -163,7 +163,8 @@ namespace YooAsset
// 5. 从WEB网站获取AssetBundle文件
if (_steps == ESteps.LoadWebFile)
{
_webRequest = UnityWebRequestAssetBundle.GetAssetBundle(_fileLoadPath, Hash128.Parse(MainBundleInfo.Bundle.FileHash));
var hash = Hash128.Parse(MainBundleInfo.Bundle.FileHash);
_webRequest = UnityWebRequestAssetBundle.GetAssetBundle(_fileLoadPath, hash);
_webRequest.SendWebRequest();
_steps = ESteps.CheckLoadWebFile;
}

View File

@ -185,24 +185,24 @@ namespace YooAsset
/// 向网络端请求最新的资源版本
/// </summary>
/// <param name="timeout">超时时间默认值60秒</param>
public UpdateStaticVersionOperation UpdateStaticVersionAsync(int timeout = 60)
public UpdatePackageVersionOperation UpdatePackageVersionAsync(int timeout = 60)
{
DebugCheckInitialize();
if (_playMode == EPlayMode.EditorSimulateMode)
{
var operation = new EditorPlayModeUpdateStaticVersionOperation();
var operation = new EditorPlayModeUpdatePackageVersionOperation();
OperationSystem.StartOperation(operation);
return operation;
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
var operation = new OfflinePlayModeUpdateStaticVersionOperation();
var operation = new OfflinePlayModeUpdatePackageVersionOperation();
OperationSystem.StartOperation(operation);
return operation;
}
else if (_playMode == EPlayMode.HostPlayMode)
{
return _hostPlayModeImpl.UpdateStaticVersionAsync(PackageName, timeout);
return _hostPlayModeImpl.UpdatePackageVersionAsync(PackageName, timeout);
}
else
{
@ -215,25 +215,25 @@ namespace YooAsset
/// </summary>
/// <param name="packageVersion">更新的包裹版本</param>
/// <param name="timeout">超时时间默认值60秒</param>
public UpdateManifestOperation UpdateManifestAsync(string packageVersion, int timeout = 60)
public UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageVersion, int timeout = 60)
{
DebugCheckInitialize();
DebugCheckUpdateManifest();
if (_playMode == EPlayMode.EditorSimulateMode)
{
var operation = new EditorPlayModeUpdateManifestOperation();
var operation = new EditorPlayModeUpdatePackageManifestOperation();
OperationSystem.StartOperation(operation);
return operation;
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
var operation = new OfflinePlayModeUpdateManifestOperation();
var operation = new OfflinePlayModeUpdatePackageManifestOperation();
OperationSystem.StartOperation(operation);
return operation;
}
else if (_playMode == EPlayMode.HostPlayMode)
{
return _hostPlayModeImpl.UpdatePatchManifestAsync(PackageName, packageVersion, timeout);
return _hostPlayModeImpl.UpdatePackageManifestAsync(PackageName, packageVersion, timeout);
}
else
{
@ -270,7 +270,18 @@ namespace YooAsset
}
/// <summary>
/// 获取包裹的版本信息
/// 清理本地包裹未使用的缓存文件
/// </summary>
public ClearPackageUnusedCacheFilesOperation ClearPackageUnusedCacheFilesAsync()
{
DebugCheckInitialize();
var operation = new ClearPackageUnusedCacheFilesOperation(this);
OperationSystem.StartOperation(operation);
return operation;
}
/// <summary>
/// 获取本地包裹的版本信息
/// </summary>
public string GetPackageVersion()
{
@ -899,24 +910,24 @@ namespace YooAsset
/// </summary>
/// <param name="packageVersion">指定更新的包裹版本</param>
/// <param name="timeout">超时时间</param>
public UpdatePackageOperation UpdatePackageAsync(string packageVersion, int timeout = 60)
public DownloadPackageOperation DownloadPackageAsync(string packageVersion, int timeout = 60)
{
DebugCheckInitialize();
if (_playMode == EPlayMode.EditorSimulateMode)
{
var operation = new EditorPlayModeUpdatePackageOperation();
var operation = new EditorPlayModeDownloadPackageOperation();
OperationSystem.StartOperation(operation);
return operation;
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
var operation = new OfflinePlayModeUpdatePackageOperation();
var operation = new OfflinePlayModeDownloadPackageOperation();
OperationSystem.StartOperation(operation);
return operation;
}
else if (_playMode == EPlayMode.HostPlayMode)
{
return _hostPlayModeImpl.UpdatePackageAsync(PackageName, packageVersion, timeout);
return _hostPlayModeImpl.DownloadPackageAsync(PackageName, packageVersion, timeout);
}
else
{

View File

@ -2,7 +2,6 @@
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
@ -30,8 +29,8 @@ namespace YooAsset
/// </summary>
public static bool IsCached(PatchBundle patchBundle)
{
string fileHash = patchBundle.FileHash;
if (_cachedDic.ContainsKey(fileHash))
string cacheKey = patchBundle.CacheKey;
if (_cachedDic.ContainsKey(cacheKey))
{
string filePath = patchBundle.CachedFilePath;
if (File.Exists(filePath))
@ -40,7 +39,7 @@ namespace YooAsset
}
else
{
_cachedDic.Remove(fileHash);
_cachedDic.Remove(cacheKey);
YooLogger.Error($"Cache file is missing : {filePath}");
return false;
}
@ -56,12 +55,12 @@ namespace YooAsset
/// </summary>
public static void CacheBundle(PatchBundle patchBundle)
{
string fileHash = patchBundle.FileHash;
if (_cachedDic.ContainsKey(fileHash) == false)
string cacheKey = patchBundle.CacheKey;
if (_cachedDic.ContainsKey(cacheKey) == false)
{
string filePath = patchBundle.CachedFilePath;
YooLogger.Log($"Cache verify file : {filePath}");
_cachedDic.Add(fileHash, patchBundle);
_cachedDic.Add(cacheKey, patchBundle);
}
}

View File

@ -5,9 +5,9 @@ using System.IO;
namespace YooAsset
{
/// <summary>
/// 清理未使用的缓存资源操作类
/// 清理本地包裹未使用的缓存文件
/// </summary>
public sealed class ClearUnusedCacheFilesOperation : AsyncOperationBase
public sealed class ClearPackageUnusedCacheFilesOperation : AsyncOperationBase
{
private enum ESteps
{
@ -17,14 +17,14 @@ namespace YooAsset
Done,
}
private readonly List<AssetsPackage> _packages;
private readonly AssetsPackage _package;
private ESteps _steps = ESteps.None;
private List<string> _unusedCacheFilePaths;
private int _unusedFileTotalCount = 0;
internal ClearUnusedCacheFilesOperation(List<AssetsPackage> packages)
internal ClearPackageUnusedCacheFilesOperation(AssetsPackage package)
{
_packages = packages;
_package = package;
}
internal override void Start()
{
@ -84,27 +84,19 @@ namespace YooAsset
/// </summary>
private List<string> GetUnusedCacheFilePaths()
{
string cacheFolderPath = PersistentHelper.GetCacheFolderPath();
string cacheFolderPath = PersistentHelper.GetCacheFolderPath(_package.PackageName);
if (Directory.Exists(cacheFolderPath) == false)
return new List<string>();
// 获取所有缓存文件
DirectoryInfo directoryInfo = new DirectoryInfo(cacheFolderPath);
FileInfo[] fileInfos = directoryInfo.GetFiles();
List<string> result = new List<string>(fileInfos.Length);
foreach (FileInfo fileInfo in fileInfos)
{
bool used = false;
foreach (var package in _packages)
if (_package.IsIncludeBundleFile(fileInfo.Name) == false)
{
if (package.IsIncludeBundleFile(fileInfo.Name))
{
used = true;
break;
}
}
if (used == false)
result.Add(fileInfo.FullName);
}
}
return result;
}

View File

@ -5,7 +5,7 @@ using System.IO;
namespace YooAsset
{
public abstract class UpdatePackageOperation : AsyncOperationBase
public abstract class DownloadPackageOperation : AsyncOperationBase
{
/// <summary>
/// 创建包裹下载器
@ -19,7 +19,7 @@ namespace YooAsset
/// <summary>
/// 编辑器下模拟运行的更新资源包裹操作
/// </summary>
internal sealed class EditorPlayModeUpdatePackageOperation : UpdatePackageOperation
internal sealed class EditorPlayModeDownloadPackageOperation : DownloadPackageOperation
{
internal override void Start()
{
@ -43,7 +43,7 @@ namespace YooAsset
/// <summary>
/// 离线模式的更新资源包裹操作
/// </summary>
internal sealed class OfflinePlayModeUpdatePackageOperation : UpdatePackageOperation
internal sealed class OfflinePlayModeDownloadPackageOperation : DownloadPackageOperation
{
internal override void Start()
{
@ -67,7 +67,7 @@ namespace YooAsset
/// <summary>
/// 联机模式的更新资源包裹操作
/// </summary>
internal sealed class HostPlayModeUpdatePackageOperation : UpdatePackageOperation
internal sealed class HostPlayModeDownloadPackageOperation : DownloadPackageOperation
{
private enum ESteps
{
@ -86,7 +86,7 @@ namespace YooAsset
private UnityWebDataRequester _downloader;
private PatchManifest _remotePatchManifest;
internal HostPlayModeUpdatePackageOperation(HostPlayModeImpl impl, string packageName, string packageVersion, int timeout)
internal HostPlayModeDownloadPackageOperation(HostPlayModeImpl impl, string packageName, string packageVersion, int timeout)
{
_impl = impl;
_packageName = packageName;
@ -160,7 +160,7 @@ namespace YooAsset
}
else
{
YooLogger.Error($"{nameof(UpdatePackageOperation)} status is failed !");
YooLogger.Error($"{nameof(DownloadPackageOperation)} status is failed !");
var operation = new PackageDownloaderOperation(null, downloadingMaxNumber, failedTryAgain, timeout);
return operation;
}

View File

@ -6,9 +6,9 @@ using System.IO;
namespace YooAsset
{
/// <summary>
/// 更新清单操作
/// 向远端请求并更新补丁清单
/// </summary>
public abstract class UpdateManifestOperation : AsyncOperationBase
public abstract class UpdatePackageManifestOperation : AsyncOperationBase
{
/// <summary>
/// 是否发现了新的补丁清单
@ -19,7 +19,7 @@ namespace YooAsset
/// <summary>
/// 编辑器下模拟运行的更新清单操作
/// </summary>
internal sealed class EditorPlayModeUpdateManifestOperation : UpdateManifestOperation
internal sealed class EditorPlayModeUpdatePackageManifestOperation : UpdatePackageManifestOperation
{
internal override void Start()
{
@ -33,7 +33,7 @@ namespace YooAsset
/// <summary>
/// 离线模式的更新清单操作
/// </summary>
internal sealed class OfflinePlayModeUpdateManifestOperation : UpdateManifestOperation
internal sealed class OfflinePlayModeUpdatePackageManifestOperation : UpdatePackageManifestOperation
{
internal override void Start()
{
@ -48,7 +48,7 @@ namespace YooAsset
/// 联机模式的更新清单操作
/// 注意:优先比对沙盒清单哈希值,如果有变化就更新远端清单文件,并保存到本地。
/// </summary>
internal sealed class HostPlayModeUpdateManifestOperation : UpdateManifestOperation
internal sealed class HostPlayModeUpdatePackageManifestOperation : UpdatePackageManifestOperation
{
private enum ESteps
{
@ -77,7 +77,7 @@ namespace YooAsset
private ESteps _steps = ESteps.None;
private float _verifyTime;
internal HostPlayModeUpdateManifestOperation(HostPlayModeImpl impl, string packageName, string packageVersion, int timeout)
internal HostPlayModeUpdatePackageManifestOperation(HostPlayModeImpl impl, string packageName, string packageVersion, int timeout)
{
_impl = impl;
_packageName = packageName;

View File

@ -5,9 +5,9 @@ using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 获取包裹的最新版本
/// 请求远端包裹的最新版本
/// </summary>
public abstract class UpdateStaticVersionOperation : AsyncOperationBase
public abstract class UpdatePackageVersionOperation : AsyncOperationBase
{
/// <summary>
/// 当前最新的包裹版本
@ -16,9 +16,9 @@ namespace YooAsset
}
/// <summary>
/// 编辑器下模拟运行的获取包裹的最新版本操作
/// 编辑器下模拟模式的请求远端包裹的最新版本
/// </summary>
internal sealed class EditorPlayModeUpdateStaticVersionOperation : UpdateStaticVersionOperation
internal sealed class EditorPlayModeUpdatePackageVersionOperation : UpdatePackageVersionOperation
{
internal override void Start()
{
@ -30,9 +30,9 @@ namespace YooAsset
}
/// <summary>
/// 离线模式的获取包裹的最新版本操作
/// 离线模式的请求远端包裹的最新版本
/// </summary>
internal sealed class OfflinePlayModeUpdateStaticVersionOperation : UpdateStaticVersionOperation
internal sealed class OfflinePlayModeUpdatePackageVersionOperation : UpdatePackageVersionOperation
{
internal override void Start()
{
@ -44,9 +44,9 @@ namespace YooAsset
}
/// <summary>
/// 联机模式的获取包裹的最新版本操作
/// 联机模式的请求远端包裹的最新版本
/// </summary>
internal sealed class HostPlayModeUpdateStaticVersionOperation : UpdateStaticVersionOperation
internal sealed class HostPlayModeUpdatePackageVersionOperation : UpdatePackageVersionOperation
{
private enum ESteps
{
@ -63,7 +63,7 @@ namespace YooAsset
private ESteps _steps = ESteps.None;
private UnityWebDataRequester _downloader;
internal HostPlayModeUpdateStaticVersionOperation(HostPlayModeImpl impl, string packageName, int timeout)
internal HostPlayModeUpdatePackageVersionOperation(HostPlayModeImpl impl, string packageName, int timeout)
{
_impl = impl;
_packageName = packageName;

View File

@ -41,10 +41,11 @@ namespace YooAsset
/// </summary>
public string[] Tags;
/// <summary>
/// 文件名称
/// </summary>
public string FileName { private set; get; }
/// 所属的包裹名称
/// </summary>
private string _packageName;
/// <summary>
/// 缓存文件路径
@ -57,7 +58,7 @@ namespace YooAsset
if (string.IsNullOrEmpty(_cachedFilePath) == false)
return _cachedFilePath;
string cacheRoot = PersistentHelper.GetCacheFolderPath();
string cacheRoot = PersistentHelper.GetCacheFolderPath(_packageName);
_cachedFilePath = $"{cacheRoot}/{FileName}";
return _cachedFilePath;
}
@ -79,6 +80,34 @@ namespace YooAsset
}
}
/// <summary>
/// 文件名称
/// </summary>
private string _fileName;
public string FileName
{
get
{
if (string.IsNullOrEmpty(_fileName))
throw new Exception("Should never get here !");
return _fileName;
}
}
/// <summary>
/// 缓存查询Key
/// </summary>
private string _cacheKey;
public string CacheKey
{
get
{
if (string.IsNullOrEmpty(_cacheKey))
throw new Exception("Should never get here !");
return _cacheKey;
}
}
public PatchBundle(string bundleName, string fileHash, string fileCRC, long fileSize, bool isRawFile, byte loadMethod, string[] tags)
{
@ -92,11 +121,13 @@ namespace YooAsset
}
/// <summary>
/// 解析文件名称
/// 解析资源包
/// </summary>
public void ParseFileName(int nameStype)
public void ParseBundle(string packageName, int nameStype)
{
FileName = PatchManifest.CreateBundleFileName(nameStype, BundleName, FileHash);
_packageName = packageName;
_cacheKey = $"{packageName}-{FileHash}";
_fileName = PatchManifest.CreateBundleFileName(nameStype, BundleName, FileHash);
}
/// <summary>

View File

@ -290,7 +290,7 @@ namespace YooAsset
// BundleList
foreach (var patchBundle in patchManifest.BundleList)
{
patchBundle.ParseFileName(patchManifest.OutputNameStyle);
patchBundle.ParseBundle(patchManifest.PackageName, patchManifest.OutputNameStyle);
patchManifest.BundleDic.Add(patchBundle.BundleName, patchBundle);
}

View File

@ -44,9 +44,9 @@ namespace YooAsset
/// <summary>
/// 异步更新资源版本号
/// </summary>
public UpdateStaticVersionOperation UpdateStaticVersionAsync(string packageName, int timeout)
public UpdatePackageVersionOperation UpdatePackageVersionAsync(string packageName, int timeout)
{
var operation = new HostPlayModeUpdateStaticVersionOperation(this, packageName, timeout);
var operation = new HostPlayModeUpdatePackageVersionOperation(this, packageName, timeout);
OperationSystem.StartOperation(operation);
return operation;
}
@ -54,9 +54,9 @@ namespace YooAsset
/// <summary>
/// 异步更新补丁清单
/// </summary>
public UpdateManifestOperation UpdatePatchManifestAsync(string packageName, string packageVersion, int timeout)
public UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageName, string packageVersion, int timeout)
{
var operation = new HostPlayModeUpdateManifestOperation(this, packageName, packageVersion, timeout);
var operation = new HostPlayModeUpdatePackageManifestOperation(this, packageName, packageVersion, timeout);
OperationSystem.StartOperation(operation);
return operation;
}
@ -74,9 +74,9 @@ namespace YooAsset
/// <summary>
/// 异步更新资源包裹
/// </summary>
public UpdatePackageOperation UpdatePackageAsync(string packageName, string packageVersion, int timeout)
public DownloadPackageOperation DownloadPackageAsync(string packageName, string packageVersion, int timeout)
{
var operation = new HostPlayModeUpdatePackageOperation(this, packageName, packageVersion, timeout);
var operation = new HostPlayModeDownloadPackageOperation(this, packageName, packageVersion, timeout);
OperationSystem.StartOperation(operation);
return operation;
}

View File

@ -97,17 +97,18 @@ namespace YooAsset
/// </summary>
public static void DeleteCacheFolder()
{
string directoryPath = GetCacheFolderPath();
if (Directory.Exists(directoryPath))
Directory.Delete(directoryPath, true);
string root = PathHelper.MakePersistentLoadPath(CacheFolderName);
if (Directory.Exists(root))
Directory.Delete(root, true);
}
/// <summary>
/// 获取缓存文件夹路径
/// </summary>
public static string GetCacheFolderPath()
public static string GetCacheFolderPath(string packageName)
{
return PathHelper.MakePersistentLoadPath(CacheFolderName);
string root = PathHelper.MakePersistentLoadPath(CacheFolderName);
return $"{root}/{packageName}";
}
#region 沙盒内清单相关

View File

@ -193,16 +193,6 @@ namespace YooAsset
#endregion
#region 沙盒相关
/// <summary>
/// 清理未使用的缓存文件
/// </summary>
public static ClearUnusedCacheFilesOperation ClearUnusedCacheFiles()
{
ClearUnusedCacheFilesOperation operation = new ClearUnusedCacheFilesOperation(_packages);
OperationSystem.StartOperation(operation);
return operation;
}
/// <summary>
/// 获取内置文件夹名称
/// </summary>

View File

@ -367,14 +367,14 @@ namespace YooAsset
#region 包裹更新
/// <summary>
/// 创建资源包裹下载器,用于下载更新指定资源版本所有的资源包文件
/// 资源包裹更新,用于下载更新指定资源版本所有的资源包文件
/// </summary>
/// <param name="packageVersion">指定更新的包裹版本</param>
/// <param name="timeout">超时时间</param>
public static UpdatePackageOperation UpdatePackageAsync(string packageVersion, int timeout = 60)
public static DownloadPackageOperation DownloadPackageAsync(string packageVersion, int timeout = 60)
{
DebugCheckDefaultPackageValid();
return _defaultPackage.UpdatePackageAsync(packageVersion, timeout);
return _defaultPackage.DownloadPackageAsync(packageVersion, timeout);
}
#endregion