Update YooAssets

This commit is contained in:
hevinci
2022-03-07 21:20:58 +08:00
parent 050c962587
commit 33ad0713fe
20 changed files with 139 additions and 119 deletions

View File

@@ -44,7 +44,7 @@ namespace YooAsset
/// 开始下载资源文件
/// 注意:只有第一次请求的参数才是有效的
/// </summary>
public static FileDownloader BeginDownload(AssetBundleInfo bundleInfo, int failedTryAgain, int timeout = 60)
public static FileDownloader BeginDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60)
{
// 查询存在的下载器
if (_downloaderDic.TryGetValue(bundleInfo.Hash, out var downloader))
@@ -119,7 +119,7 @@ namespace YooAsset
}
// 验证文件完整性
public static bool CheckContentIntegrity(AssetBundleInfo bundleInfo)
public static bool CheckContentIntegrity(BundleInfo bundleInfo)
{
return CheckContentIntegrity(bundleInfo.LocalPath, bundleInfo.SizeBytes, bundleInfo.CRC);
}

View File

@@ -19,7 +19,7 @@ namespace YooAsset
Failed,
}
public AssetBundleInfo BundleInfo { private set; get; }
private readonly BundleInfo _bundleInfo;
private UnityWebRequest _webRequest;
private UnityWebRequestAsyncOperation _operationHandle;
@@ -48,13 +48,13 @@ namespace YooAsset
public ulong DownloadedBytes { private set; get; }
internal FileDownloader(AssetBundleInfo bundleInfo)
internal FileDownloader(BundleInfo bundleInfo)
{
BundleInfo = bundleInfo;
_bundleInfo = bundleInfo;
}
internal void SendRequest(int failedTryAgain, int timeout)
{
if (string.IsNullOrEmpty(BundleInfo.LocalPath))
if (string.IsNullOrEmpty(_bundleInfo.LocalPath))
throw new ArgumentNullException();
if (_steps == ESteps.None)
@@ -85,7 +85,7 @@ namespace YooAsset
_requestCount++;
_requestURL = GetRequestURL();
_webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET);
DownloadHandlerFile handler = new DownloadHandlerFile(BundleInfo.LocalPath);
DownloadHandlerFile handler = new DownloadHandlerFile(_bundleInfo.LocalPath);
handler.removeFileOnAbort = true;
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
@@ -124,12 +124,12 @@ namespace YooAsset
if (isError == false)
{
// 注意:如果文件验证失败需要删除文件
if (DownloadSystem.CheckContentIntegrity(BundleInfo) == false)
if (DownloadSystem.CheckContentIntegrity(_bundleInfo) == false)
{
isError = true;
_lastError = $"Verification failed";
if (File.Exists(BundleInfo.LocalPath))
File.Delete(BundleInfo.LocalPath);
if (File.Exists(_bundleInfo.LocalPath))
File.Delete(_bundleInfo.LocalPath);
}
}
@@ -144,7 +144,7 @@ namespace YooAsset
else
{
_steps = ESteps.Succeed;
DownloadSystem.CacheVerifyFile(BundleInfo.Hash, BundleInfo.BundleName);
DownloadSystem.CacheVerifyFile(_bundleInfo.Hash, _bundleInfo.BundleName);
}
// 释放下载器
@@ -172,9 +172,9 @@ namespace YooAsset
{
// 轮流返回请求地址
if (_requestCount % 2 == 0)
return BundleInfo.RemoteFallbackURL;
return _bundleInfo.RemoteFallbackURL;
else
return BundleInfo.RemoteMainURL;
return _bundleInfo.RemoteMainURL;
}
private void CheckTimeout()
{
@@ -206,6 +206,14 @@ namespace YooAsset
}
}
/// <summary>
/// 获取资源包信息
/// </summary>
public BundleInfo GetBundleInfo()
{
return _bundleInfo;
}
/// <summary>
/// 检测下载器是否已经完成(无论成功或失败)
/// </summary>

View File

@@ -19,7 +19,7 @@ namespace YooAsset
Failed,
}
public AssetBundleInfo BundleInfo { private set; get; }
private readonly BundleInfo _bundleInfo;
private ESteps _steps = ESteps.None;
// 线程
@@ -61,6 +61,10 @@ namespace YooAsset
}
internal HttpDownloader(BundleInfo bundleInfo)
{
_bundleInfo = bundleInfo;
}
internal void SendRequest(int failedTryAgain, int timeout)
{
_failedTryAgain = failedTryAgain;
@@ -102,7 +106,7 @@ namespace YooAsset
_downloadError = _threadError;
if (_threadResult)
{
DownloadSystem.CacheVerifyFile(BundleInfo.Hash, BundleInfo.BundleName);
DownloadSystem.CacheVerifyFile(_bundleInfo.Hash, _bundleInfo.BundleName);
_steps = ESteps.Succeed;
}
else
@@ -125,6 +129,14 @@ namespace YooAsset
_steps = ESteps.Succeed;
}
/// <summary>
/// 获取资源包信息
/// </summary>
public BundleInfo GetBundleInfo()
{
return _bundleInfo;
}
/// <summary>
/// 检测下载器是否已经完成(无论成功或失败)
/// </summary>
@@ -155,8 +167,8 @@ namespace YooAsset
private void ThreadRun()
{
string url = GetRequestURL();
string savePath = BundleInfo.LocalPath;
long fileTotalSize = BundleInfo.SizeBytes;
string savePath = _bundleInfo.LocalPath;
long fileTotalSize = _bundleInfo.SizeBytes;
FileStream fileStream = null;
Stream webStream = null;
@@ -202,7 +214,7 @@ namespace YooAsset
}
// 验证下载文件完整性
bool verfiyResult = DownloadSystem.CheckContentIntegrity(savePath, BundleInfo.SizeBytes, BundleInfo.CRC);
bool verfiyResult = DownloadSystem.CheckContentIntegrity(savePath, _bundleInfo.SizeBytes, _bundleInfo.CRC);
if(verfiyResult)
{
_threadResult = true;
@@ -210,7 +222,7 @@ namespace YooAsset
else
{
_threadResult = false;
_threadError = $"Verify file content failed : {BundleInfo.Hash}";
_threadError = $"Verify file content failed : {_bundleInfo.Hash}";
}
}
catch (Exception e)
@@ -245,9 +257,9 @@ namespace YooAsset
// 轮流返回请求地址
_requestCount++;
if (_requestCount % 2 == 0)
return BundleInfo.RemoteFallbackURL;
return _bundleInfo.RemoteFallbackURL;
else
return BundleInfo.RemoteMainURL;
return _bundleInfo.RemoteMainURL;
}
#endregion
}

View File

@@ -20,8 +20,8 @@ namespace YooAsset
private readonly int _fileLoadingMaxNumber;
private readonly int _failedTryAgain;
private readonly List<AssetBundleInfo> _downloadList;
private readonly List<AssetBundleInfo> _loadFailedList = new List<AssetBundleInfo>();
private readonly List<BundleInfo> _downloadList;
private readonly List<BundleInfo> _loadFailedList = new List<BundleInfo>();
private readonly List<FileDownloader> _downloaders = new List<FileDownloader>();
private readonly List<FileDownloader> _removeList = new List<FileDownloader>(MAX_LOADER_COUNT);
@@ -67,7 +67,7 @@ namespace YooAsset
public OnDownloadFileFailed OnDownloadFileFailedCallback { set; get; }
internal DownloaderOperation(List<AssetBundleInfo> downloadList, int fileLoadingMaxNumber, int failedTryAgain)
internal DownloaderOperation(List<BundleInfo> downloadList, int fileLoadingMaxNumber, int failedTryAgain)
{
_downloadList = downloadList;
_fileLoadingMaxNumber = UnityEngine.Mathf.Clamp(fileLoadingMaxNumber, 1, MAX_LOADER_COUNT); ;
@@ -100,7 +100,7 @@ namespace YooAsset
if (downloader.IsDone() == false)
continue;
AssetBundleInfo bundleInfo = downloader.BundleInfo;
BundleInfo bundleInfo = downloader.GetBundleInfo();
// 检测是否下载失败
if (downloader.HasError())

View File

@@ -87,7 +87,7 @@ namespace YooAsset
/// <summary>
/// 获取资源包名称
/// </summary>
public string GetAssetBundleName(string assetPath)
public string GetBundleName(string assetPath)
{
if (Assets.TryGetValue(assetPath, out PatchAsset patchAsset))
{

View File

@@ -25,13 +25,13 @@ namespace YooAsset
}
#region IBundleServices接口
AssetBundleInfo IBundleServices.GetAssetBundleInfo(string bundleName)
BundleInfo IBundleServices.GetBundleInfo(string bundleName)
{
Logger.Warning($"Editor play mode can not get asset bundle info.");
AssetBundleInfo bundleInfo = new AssetBundleInfo(bundleName, bundleName);
Logger.Warning($"Editor play mode can not get bundle info.");
BundleInfo bundleInfo = new BundleInfo(bundleName, bundleName);
return bundleInfo;
}
string IBundleServices.GetAssetBundleName(string assetPath)
string IBundleServices.GetBundleName(string assetPath)
{
return assetPath;
}

View File

@@ -57,11 +57,11 @@ namespace YooAsset
/// </summary>
public DownloaderOperation CreateDownloaderByTags(string[] tags, int fileLoadingMaxNumber, int failedTryAgain)
{
List<AssetBundleInfo> downloadList = GetDownloadListByTags(tags);
List<BundleInfo> downloadList = GetDownloadListByTags(tags);
var operation = new DownloaderOperation(downloadList, fileLoadingMaxNumber, failedTryAgain);
return operation;
}
private List<AssetBundleInfo> GetDownloadListByTags(string[] tags)
private List<BundleInfo> GetDownloadListByTags(string[] tags)
{
List<PatchBundle> downloadList = new List<PatchBundle>(1000);
foreach (var patchBundle in LocalPatchManifest.BundleList)
@@ -103,17 +103,17 @@ namespace YooAsset
/// </summary>
public DownloaderOperation CreateDownloaderByPaths(List<string> assetPaths, int fileLoadingMaxNumber, int failedTryAgain)
{
List<AssetBundleInfo> downloadList = GetDownloadListByPaths(assetPaths);
List<BundleInfo> downloadList = GetDownloadListByPaths(assetPaths);
var operation = new DownloaderOperation(downloadList, fileLoadingMaxNumber, failedTryAgain);
return operation;
}
private List<AssetBundleInfo> GetDownloadListByPaths(List<string> assetPaths)
private List<BundleInfo> GetDownloadListByPaths(List<string> assetPaths)
{
// 获取资源对象的资源包和所有依赖资源包
List<PatchBundle> checkList = new List<PatchBundle>();
foreach (var assetPath in assetPaths)
{
string mainBundleName = LocalPatchManifest.GetAssetBundleName(assetPath);
string mainBundleName = LocalPatchManifest.GetBundleName(assetPath);
if (string.IsNullOrEmpty(mainBundleName) == false)
{
if (LocalPatchManifest.Bundles.TryGetValue(mainBundleName, out PatchBundle mainBundle))
@@ -160,11 +160,11 @@ namespace YooAsset
/// </summary>
public DownloaderOperation CreateUnpackerByTags(string[] tags, int fileUpackingMaxNumber, int failedTryAgain)
{
List<AssetBundleInfo> unpcakList = GetUnpackListByTags(tags);
List<BundleInfo> unpcakList = GetUnpackListByTags(tags);
var operation = new DownloaderOperation(unpcakList, fileUpackingMaxNumber, failedTryAgain);
return operation;
}
private List<AssetBundleInfo> GetUnpackListByTags(string[] tags)
private List<BundleInfo> GetUnpackListByTags(string[] tags)
{
List<PatchBundle> downloadList = new List<PatchBundle>(1000);
foreach (var patchBundle in AppPatchManifest.BundleList)
@@ -213,18 +213,18 @@ namespace YooAsset
}
// 下载相关
private AssetBundleInfo ConvertToDownloadInfo(PatchBundle patchBundle)
private BundleInfo ConvertToDownloadInfo(PatchBundle patchBundle)
{
// 注意:资源版本号只用于确定下载路径
string sandboxPath = PatchHelper.MakeSandboxCacheFilePath(patchBundle.Hash);
string remoteMainURL = GetPatchDownloadMainURL(patchBundle.Version, patchBundle.Hash);
string remoteFallbackURL = GetPatchDownloadFallbackURL(patchBundle.Version, patchBundle.Hash);
AssetBundleInfo bundleInfo = new AssetBundleInfo(patchBundle, sandboxPath, remoteMainURL, remoteFallbackURL);
BundleInfo bundleInfo = new BundleInfo(patchBundle, sandboxPath, remoteMainURL, remoteFallbackURL);
return bundleInfo;
}
private List<AssetBundleInfo> ConvertToDownloadList(List<PatchBundle> downloadList)
private List<BundleInfo> ConvertToDownloadList(List<PatchBundle> downloadList)
{
List<AssetBundleInfo> result = new List<AssetBundleInfo>(downloadList.Count);
List<BundleInfo> result = new List<BundleInfo>(downloadList.Count);
foreach (var patchBundle in downloadList)
{
var bundleInfo = ConvertToDownloadInfo(patchBundle);
@@ -234,16 +234,16 @@ namespace YooAsset
}
// 解压相关
private AssetBundleInfo ConvertToUnpackInfo(PatchBundle patchBundle)
private BundleInfo ConvertToUnpackInfo(PatchBundle patchBundle)
{
string sandboxPath = PatchHelper.MakeSandboxCacheFilePath(patchBundle.Hash);
string streamingLoadPath = AssetPathHelper.MakeStreamingLoadPath(patchBundle.Hash);
AssetBundleInfo bundleInfo = new AssetBundleInfo(patchBundle, sandboxPath, streamingLoadPath, streamingLoadPath);
BundleInfo bundleInfo = new BundleInfo(patchBundle, sandboxPath, streamingLoadPath, streamingLoadPath);
return bundleInfo;
}
private List<AssetBundleInfo> ConvertToUnpackList(List<PatchBundle> unpackList)
private List<BundleInfo> ConvertToUnpackList(List<PatchBundle> unpackList)
{
List<AssetBundleInfo> result = new List<AssetBundleInfo>(unpackList.Count);
List<BundleInfo> result = new List<BundleInfo>(unpackList.Count);
foreach (var patchBundle in unpackList)
{
var bundleInfo = ConvertToUnpackInfo(patchBundle);
@@ -253,10 +253,10 @@ namespace YooAsset
}
#region IBundleServices接口
AssetBundleInfo IBundleServices.GetAssetBundleInfo(string bundleName)
BundleInfo IBundleServices.GetBundleInfo(string bundleName)
{
if (string.IsNullOrEmpty(bundleName))
return new AssetBundleInfo(string.Empty, string.Empty);
return new BundleInfo(string.Empty, string.Empty);
if (LocalPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle patchBundle))
{
@@ -266,7 +266,7 @@ namespace YooAsset
if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash)
{
string appLoadPath = AssetPathHelper.MakeStreamingLoadPath(appPatchBundle.Hash);
AssetBundleInfo bundleInfo = new AssetBundleInfo(appPatchBundle, appLoadPath);
BundleInfo bundleInfo = new BundleInfo(appPatchBundle, appLoadPath);
return bundleInfo;
}
}
@@ -275,7 +275,7 @@ namespace YooAsset
if (DownloadSystem.ContainsVerifyFile(patchBundle.Hash))
{
string sandboxLoadPath = PatchHelper.MakeSandboxCacheFilePath(patchBundle.Hash);
AssetBundleInfo bundleInfo = new AssetBundleInfo(patchBundle, sandboxLoadPath);
BundleInfo bundleInfo = new BundleInfo(patchBundle, sandboxLoadPath);
return bundleInfo;
}
@@ -285,13 +285,13 @@ namespace YooAsset
else
{
Logger.Warning($"Not found bundle in patch manifest : {bundleName}");
AssetBundleInfo bundleInfo = new AssetBundleInfo(bundleName, string.Empty);
BundleInfo bundleInfo = new BundleInfo(bundleName, string.Empty);
return bundleInfo;
}
}
string IBundleServices.GetAssetBundleName(string assetPath)
string IBundleServices.GetBundleName(string assetPath)
{
return LocalPatchManifest.GetAssetBundleName(assetPath);
return LocalPatchManifest.GetBundleName(assetPath);
}
string[] IBundleServices.GetAllDependencies(string assetPath)
{

View File

@@ -29,27 +29,27 @@ namespace YooAsset
}
#region IBundleServices接口
AssetBundleInfo IBundleServices.GetAssetBundleInfo(string bundleName)
BundleInfo IBundleServices.GetBundleInfo(string bundleName)
{
if (string.IsNullOrEmpty(bundleName))
return new AssetBundleInfo(string.Empty, string.Empty);
return new BundleInfo(string.Empty, string.Empty);
if (AppPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle patchBundle))
{
string localPath = AssetPathHelper.MakeStreamingLoadPath(patchBundle.Hash);
AssetBundleInfo bundleInfo = new AssetBundleInfo(patchBundle, localPath);
BundleInfo bundleInfo = new BundleInfo(patchBundle, localPath);
return bundleInfo;
}
else
{
Logger.Warning($"Not found bundle in patch manifest : {bundleName}");
AssetBundleInfo bundleInfo = new AssetBundleInfo(bundleName, string.Empty);
BundleInfo bundleInfo = new BundleInfo(bundleName, string.Empty);
return bundleInfo;
}
}
string IBundleServices.GetAssetBundleName(string assetPath)
string IBundleServices.GetBundleName(string assetPath)
{
return AppPatchManifest.GetAssetBundleName(assetPath);
return AppPatchManifest.GetBundleName(assetPath);
}
string[] IBundleServices.GetAllDependencies(string assetPath)
{