Operation handles support error queries

操作句柄支持错误信息查询。
pull/5/head
hevinci 2022-04-24 18:30:35 +08:00
parent cbf142dbf8
commit 9bf22f2c79
19 changed files with 170 additions and 45 deletions

View File

@ -83,7 +83,7 @@ namespace YooAsset
{ {
if (_providers[i].CanDestroy()) if (_providers[i].CanDestroy())
{ {
_providers[i].Destory(); _providers[i].Destroy();
_providers.RemoveAt(i); _providers.RemoveAt(i);
} }
} }
@ -114,7 +114,7 @@ namespace YooAsset
{ {
foreach (var provider in _providers) foreach (var provider in _providers)
{ {
provider.Destory(); provider.Destroy();
} }
_providers.Clear(); _providers.Clear();

View File

@ -32,6 +32,19 @@ namespace YooAsset
} }
} }
/// <summary>
/// 最近的错误信息
/// </summary>
public string LastError
{
get
{
if (IsValid == false)
return string.Empty;
return _provider.LastError;
}
}
/// <summary> /// <summary>
/// 加载进度 /// 加载进度
/// </summary> /// </summary>

View File

@ -43,10 +43,10 @@ namespace YooAsset
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EStatus.Failed; Status = EStatus.Failed;
return; LastError = $"Invalid load mode : {BundleFileInfo.BundleName}";
YooLogger.Error(LastError);
} }
else if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
{ {
_steps = ESteps.Download; _steps = ESteps.Download;
_fileLoadPath = BundleFileInfo.GetCacheLoadPath(); _fileLoadPath = BundleFileInfo.GetCacheLoadPath();
@ -83,9 +83,9 @@ namespace YooAsset
if (_downloader.HasError()) if (_downloader.HasError())
{ {
_downloader.ReportError();
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EStatus.Failed; Status = EStatus.Failed;
LastError = _downloader.GetLastError();
} }
else else
{ {
@ -100,9 +100,10 @@ namespace YooAsset
// 注意Unity2017.4编辑器模式下如果AssetBundle文件不存在会导致编辑器崩溃这里做了预判。 // 注意Unity2017.4编辑器模式下如果AssetBundle文件不存在会导致编辑器崩溃这里做了预判。
if (System.IO.File.Exists(_fileLoadPath) == false) if (System.IO.File.Exists(_fileLoadPath) == false)
{ {
YooLogger.Warning($"Not found assetBundle file : {_fileLoadPath}");
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EStatus.Failed; Status = EStatus.Failed;
LastError = $"Not found assetBundle file : {_fileLoadPath}";
YooLogger.Error(LastError);
return; return;
} }
#endif #endif
@ -151,9 +152,10 @@ namespace YooAsset
// Check error // Check error
if (CacheBundle == null) if (CacheBundle == null)
{ {
YooLogger.Error($"Failed to load assetBundle file : {BundleFileInfo.BundleName}");
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EStatus.Failed; Status = EStatus.Failed;
LastError = $"Failed to load assetBundle : {BundleFileInfo.BundleName}";
YooLogger.Error(LastError);
} }
else else
{ {

View File

@ -29,6 +29,11 @@ namespace YooAsset
/// </summary> /// </summary>
public EStatus Status { protected set; get; } public EStatus Status { protected set; get; }
/// <summary>
/// 最近的错误信息
/// </summary>
public string LastError { protected set; get; }
/// <summary> /// <summary>
/// 是否已经销毁 /// 是否已经销毁
/// </summary> /// </summary>
@ -139,7 +144,7 @@ namespace YooAsset
// 销毁所有Providers // 销毁所有Providers
foreach (var provider in _providers) foreach (var provider in _providers)
{ {
provider.Destory(); provider.Destroy();
} }
// 从列表里移除Providers // 从列表里移除Providers

View File

@ -41,10 +41,10 @@ namespace YooAsset
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EStatus.Failed; Status = EStatus.Failed;
return; LastError = $"Invalid load mode : {BundleFileInfo.BundleName}";
YooLogger.Error(LastError);
} }
else if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
{ {
_steps = ESteps.LoadFile; _steps = ESteps.LoadFile;
_webURL = BundleFileInfo.GetStreamingLoadPath(); _webURL = BundleFileInfo.GetStreamingLoadPath();
@ -76,7 +76,7 @@ namespace YooAsset
if (_webRequest.isNetworkError || _webRequest.isHttpError) if (_webRequest.isNetworkError || _webRequest.isHttpError)
#endif #endif
{ {
Debug.LogWarning($"Failed to get asset bundle form web : {_webURL} Error : {_webRequest.error}"); YooLogger.Warning($"Failed to get asset bundle form web : {_webURL} Error : {_webRequest.error}");
_steps = ESteps.TryLoad; _steps = ESteps.TryLoad;
_tryTimer = 0; _tryTimer = 0;
} }
@ -85,9 +85,10 @@ namespace YooAsset
CacheBundle = DownloadHandlerAssetBundle.GetContent(_webRequest); CacheBundle = DownloadHandlerAssetBundle.GetContent(_webRequest);
if (CacheBundle == null) if (CacheBundle == null)
{ {
Debug.LogError($"Get asset bundle error : {_webRequest.error}");
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EStatus.Failed; Status = EStatus.Failed;
LastError = $"AssetBundle file is invalid : {BundleFileInfo.BundleName}";
YooLogger.Error(LastError);
} }
else else
{ {

View File

@ -11,6 +11,7 @@ namespace YooAsset
/// </summary> /// </summary>
private readonly List<AssetBundleLoaderBase> _dependBundles; private readonly List<AssetBundleLoaderBase> _dependBundles;
public DependAssetBundleGrouper(string assetPath) public DependAssetBundleGrouper(string assetPath)
{ {
_dependBundles = AssetSystem.CreateDependAssetBundleLoaders(assetPath); _dependBundles = AssetSystem.CreateDependAssetBundleLoaders(assetPath);
@ -29,6 +30,36 @@ namespace YooAsset
return true; return true;
} }
/// <summary>
/// 依赖资源包是否全部加载成功
/// </summary>
public bool IsSucceed()
{
foreach (var loader in _dependBundles)
{
if (loader.Status != AssetBundleLoaderBase.EStatus.Succeed)
{
return false;
}
}
return true;
}
/// <summary>
/// 获取某个加载失败的资源包错误信息
/// </summary>
public string GetLastError()
{
foreach (var loader in _dependBundles)
{
if (loader.Status != AssetBundleLoaderBase.EStatus.Succeed)
{
return loader.LastError;
}
}
return string.Empty;
}
/// <summary> /// <summary>
/// 主线程等待异步操作完毕 /// 主线程等待异步操作完毕
/// </summary> /// </summary>

View File

@ -45,15 +45,23 @@ namespace YooAsset
if (OwnerBundle.IsDone() == false) if (OwnerBundle.IsDone() == false)
return; return;
if (OwnerBundle.CacheBundle == null) if (DependBundles.IsSucceed() == false)
{ {
Status = EStatus.Fail; Status = EStatus.Fail;
LastError = DependBundles.GetLastError();
InvokeCompletion(); InvokeCompletion();
return;
} }
else
if (OwnerBundle.Status != AssetBundleLoaderBase.EStatus.Succeed)
{ {
Status = EStatus.Loading; Status = EStatus.Fail;
LastError = OwnerBundle.LastError;
InvokeCompletion();
return;
} }
Status = EStatus.Loading;
} }
// 2. 加载资源对象 // 2. 加载资源对象
@ -97,7 +105,10 @@ namespace YooAsset
Status = AssetObject == null ? EStatus.Fail : EStatus.Success; Status = AssetObject == null ? EStatus.Fail : EStatus.Success;
if (Status == EStatus.Fail) if (Status == EStatus.Fail)
YooLogger.Warning($"Failed to load asset : {AssetName} from bundle : {OwnerBundle.BundleFileInfo.BundleName}"); {
LastError = $"Failed to load asset : {AssetName} from bundle : {OwnerBundle.BundleFileInfo.BundleName}";
YooLogger.Error(LastError);
}
InvokeCompletion(); InvokeCompletion();
} }
} }

View File

@ -16,9 +16,9 @@ namespace YooAsset
DependBundles = new DependAssetBundleGrouper(assetPath); DependBundles = new DependAssetBundleGrouper(assetPath);
DependBundles.Reference(); DependBundles.Reference();
} }
public override void Destory() public override void Destroy()
{ {
base.Destory(); base.Destroy();
// 释放资源包 // 释放资源包
if (OwnerBundle != null) if (OwnerBundle != null)

View File

@ -46,15 +46,23 @@ namespace YooAsset
if (OwnerBundle.IsDone() == false) if (OwnerBundle.IsDone() == false)
return; return;
if (OwnerBundle.CacheBundle == null) if (DependBundles.IsSucceed() == false)
{ {
Status = EStatus.Fail; Status = EStatus.Fail;
LastError = DependBundles.GetLastError();
InvokeCompletion(); InvokeCompletion();
return;
} }
else
if (OwnerBundle.Status != AssetBundleLoaderBase.EStatus.Succeed)
{ {
Status = EStatus.Loading; Status = EStatus.Fail;
LastError = OwnerBundle.LastError;
InvokeCompletion();
return;
} }
Status = EStatus.Loading;
} }
// 2. 加载场景 // 2. 加载场景
@ -69,8 +77,9 @@ namespace YooAsset
} }
else else
{ {
YooLogger.Warning($"Failed to load scene : {AssetName}");
Status = EStatus.Fail; Status = EStatus.Fail;
LastError = $"Failed to load scene : {AssetName}";
YooLogger.Error(LastError);
InvokeCompletion(); InvokeCompletion();
} }
} }
@ -85,6 +94,11 @@ namespace YooAsset
SceneManager.SetActiveScene(SceneObject); SceneManager.SetActiveScene(SceneObject);
Status = SceneObject.IsValid() ? EStatus.Success : EStatus.Fail; Status = SceneObject.IsValid() ? EStatus.Success : EStatus.Fail;
if(Status == EStatus.Fail)
{
LastError = $"The load scene is invalid : {AssetPath}";
YooLogger.Error(LastError);
}
InvokeCompletion(); InvokeCompletion();
} }
} }

View File

@ -45,15 +45,23 @@ namespace YooAsset
if (OwnerBundle.IsDone() == false) if (OwnerBundle.IsDone() == false)
return; return;
if (OwnerBundle.CacheBundle == null) if (DependBundles.IsSucceed() == false)
{ {
Status = EStatus.Fail; Status = EStatus.Fail;
LastError = DependBundles.GetLastError();
InvokeCompletion(); InvokeCompletion();
return;
} }
else
if (OwnerBundle.Status != AssetBundleLoaderBase.EStatus.Succeed)
{ {
Status = EStatus.Loading; Status = EStatus.Fail;
LastError = OwnerBundle.LastError;
InvokeCompletion();
return;
} }
Status = EStatus.Loading;
} }
// 2. 加载资源对象 // 2. 加载资源对象
@ -97,7 +105,10 @@ namespace YooAsset
Status = AllAssetObjects == null ? EStatus.Fail : EStatus.Success; Status = AllAssetObjects == null ? EStatus.Fail : EStatus.Success;
if (Status == EStatus.Fail) if (Status == EStatus.Fail)
YooLogger.Warning($"Failed to load sub assets : {AssetName} from bundle : {OwnerBundle.BundleFileInfo.BundleName}"); {
LastError = $"Failed to load sub assets : {AssetName} from bundle : {OwnerBundle.BundleFileInfo.BundleName}";
YooLogger.Error(LastError);
}
InvokeCompletion(); InvokeCompletion();
} }
} }

View File

@ -34,6 +34,8 @@ namespace YooAsset
if (string.IsNullOrEmpty(guid)) if (string.IsNullOrEmpty(guid))
{ {
Status = EStatus.Fail; Status = EStatus.Fail;
LastError = $"Not found asset : {AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion(); InvokeCompletion();
return; return;
} }
@ -59,7 +61,10 @@ namespace YooAsset
{ {
Status = AssetObject == null ? EStatus.Fail : EStatus.Success; Status = AssetObject == null ? EStatus.Fail : EStatus.Success;
if (Status == EStatus.Fail) if (Status == EStatus.Fail)
YooLogger.Warning($"Failed to load asset object : {AssetPath}"); {
LastError = $"Failed to load asset object : {AssetPath}";
YooLogger.Error(LastError);
}
InvokeCompletion(); InvokeCompletion();
} }
#endif #endif

View File

@ -51,8 +51,9 @@ namespace YooAsset
} }
else else
{ {
YooLogger.Warning($"Failed to load scene : {AssetName}");
Status = EStatus.Fail; Status = EStatus.Fail;
LastError = $"Failed to load scene : {AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion(); InvokeCompletion();
} }
} }
@ -67,6 +68,11 @@ namespace YooAsset
SceneManager.SetActiveScene(SceneObject); SceneManager.SetActiveScene(SceneObject);
Status = SceneObject.IsValid() ? EStatus.Success : EStatus.Fail; Status = SceneObject.IsValid() ? EStatus.Success : EStatus.Fail;
if (Status == EStatus.Fail)
{
LastError = $"The load scene is invalid : {AssetPath}";
YooLogger.Error(LastError);
}
InvokeCompletion(); InvokeCompletion();
} }
} }

View File

@ -34,6 +34,8 @@ namespace YooAsset
if (string.IsNullOrEmpty(guid)) if (string.IsNullOrEmpty(guid))
{ {
Status = EStatus.Fail; Status = EStatus.Fail;
LastError = $"Not found asset : {AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion(); InvokeCompletion();
return; return;
} }
@ -73,7 +75,10 @@ namespace YooAsset
{ {
Status = AllAssetObjects == null ? EStatus.Fail : EStatus.Success; Status = AllAssetObjects == null ? EStatus.Fail : EStatus.Success;
if (Status == EStatus.Fail) if (Status == EStatus.Fail)
YooLogger.Warning($"Failed to load sub assets : {AssetName}"); {
LastError = $"Failed to load sub assets : {nameof(AssetType)} in {AssetPath}";
YooLogger.Error(LastError);
}
InvokeCompletion(); InvokeCompletion();
} }
#endif #endif

View File

@ -53,6 +53,11 @@ namespace YooAsset
/// </summary> /// </summary>
public EStatus Status { protected set; get; } = EStatus.None; public EStatus Status { protected set; get; } = EStatus.None;
/// <summary>
/// 最近的错误信息
/// </summary>
public string LastError { protected set; get; } = string.Empty;
/// <summary> /// <summary>
/// 引用计数 /// 引用计数
/// </summary> /// </summary>
@ -105,7 +110,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 销毁资源对象 /// 销毁资源对象
/// </summary> /// </summary>
public virtual void Destory() public virtual void Destroy()
{ {
IsDestroyed = true; IsDestroyed = true;
} }

View File

@ -98,19 +98,27 @@ namespace YooAsset
} }
/// <summary> /// <summary>
/// 报告错误信息 /// 按照错误级别打印错误
/// </summary> /// </summary>
public void ReportError() public void ReportError()
{ {
YooLogger.Error($"Failed to download : {_requestURL} Error : {_lastError}"); YooLogger.Error(GetLastError());
} }
/// <summary> /// <summary>
/// 获取最近一条错误日志 /// 按照警告级别打印错误
/// </summary>
public void ReportWarning()
{
YooLogger.Warning(GetLastError());
}
/// <summary>
/// 获取最近发生的错误信息
/// </summary> /// </summary>
public string GetLastError() public string GetLastError()
{ {
return _lastError; return $"Failed to download : {_requestURL} Error : {_lastError}";
} }
} }
} }

View File

@ -95,18 +95,22 @@ namespace YooAsset
} }
else else
{ {
ReportError();
string cacheFilePath = _bundleInfo.GetCacheLoadPath(); string cacheFilePath = _bundleInfo.GetCacheLoadPath();
if (File.Exists(cacheFilePath)) if (File.Exists(cacheFilePath))
File.Delete(cacheFilePath); File.Delete(cacheFilePath);
// 失败后重新尝试 // 失败后重新尝试
if (_failedTryAgain > 0) if (_failedTryAgain > 0)
{
ReportWarning();
_steps = ESteps.TryAgain; _steps = ESteps.TryAgain;
}
else else
{
ReportError();
_steps = ESteps.Failed; _steps = ESteps.Failed;
} }
}
// 释放下载器 // 释放下载器
DisposeWebRequest(); DisposeWebRequest();

View File

@ -211,14 +211,19 @@ namespace YooAsset
if (_threadDownloader.HasError()) if (_threadDownloader.HasError())
{ {
_lastError = _threadDownloader.Error; _lastError = _threadDownloader.Error;
ReportError();
// 失败后重新尝试 // 失败后重新尝试
if (_failedTryAgain > 0) if (_failedTryAgain > 0)
{
ReportWarning();
_steps = ESteps.TryAgain; _steps = ESteps.TryAgain;
}
else else
{
ReportError();
_steps = ESteps.Failed; _steps = ESteps.Failed;
} }
}
else else
{ {
DownloadSystem.CacheVerifyFile(_bundleInfo.Hash, _bundleInfo.BundleName); DownloadSystem.CacheVerifyFile(_bundleInfo.Hash, _bundleInfo.BundleName);

View File

@ -105,7 +105,6 @@ namespace YooAsset
// 检测是否下载失败 // 检测是否下载失败
if (downloader.HasError()) if (downloader.HasError())
{ {
downloader.ReportError();
_removeList.Add(downloader); _removeList.Add(downloader);
_loadFailedList.Add(bundleInfo); _loadFailedList.Add(bundleInfo);
continue; continue;

View File

@ -84,7 +84,7 @@ namespace YooAsset
{ {
if (string.IsNullOrEmpty(location)) if (string.IsNullOrEmpty(location))
{ {
Debug.LogError("location param is null or empty!"); YooLogger.Error("location param is null or empty!");
} }
else else
{ {
@ -93,11 +93,11 @@ namespace YooAsset
if (index != -1) if (index != -1)
{ {
if (location.Length == index + 1) if (location.Length == index + 1)
Debug.LogWarning($"Found blank character in location : \"{location}\""); YooLogger.Warning($"Found blank character in location : \"{location}\"");
} }
if (location.IndexOfAny(Path.GetInvalidPathChars()) >= 0) if (location.IndexOfAny(Path.GetInvalidPathChars()) >= 0)
Debug.LogWarning($"Found illegal character in location : \"{location}\""); YooLogger.Warning($"Found illegal character in location : \"{location}\"");
} }
} }
#endif #endif