Files
YooAsset/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoader.cs

301 lines
6.6 KiB
C#
Raw Normal View History

2022-03-01 10:44:12 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
2022-03-22 21:18:33 +08:00
internal class AssetBundleLoader
2022-03-01 10:44:12 +08:00
{
2022-03-09 23:57:04 +08:00
public enum EStatus
{
None = 0,
Download,
CheckDownload,
LoadFile,
CheckFile,
Success,
Fail,
}
2022-03-01 10:44:12 +08:00
/// <summary>
2022-03-07 21:20:58 +08:00
/// 资源包文件信息
2022-03-01 10:44:12 +08:00
/// </summary>
2022-03-07 21:20:58 +08:00
public BundleInfo BundleFileInfo { private set; get; }
2022-03-01 10:44:12 +08:00
/// <summary>
/// 引用计数
/// </summary>
public int RefCount { private set; get; }
/// <summary>
/// 加载状态
/// </summary>
2022-03-09 23:57:04 +08:00
public EStatus Status { private set; get; }
2022-03-01 10:44:12 +08:00
/// <summary>
/// 是否已经销毁
/// </summary>
public bool IsDestroyed { private set; get; } = false;
2022-03-22 20:47:22 +08:00
private readonly List<ProviderBase> _providers = new List<ProviderBase>(100);
2022-03-01 10:44:12 +08:00
private bool _isWaitForAsyncComplete = false;
private bool _isShowWaitForAsyncError = false;
2022-03-29 19:45:20 +08:00
private DownloaderBase _downloader;
2022-03-01 10:44:12 +08:00
private AssetBundleCreateRequest _cacheRequest;
internal AssetBundle CacheBundle { private set; get; }
2022-03-09 23:57:04 +08:00
2022-03-01 10:44:12 +08:00
2022-03-22 21:18:33 +08:00
public AssetBundleLoader(BundleInfo bundleInfo)
2022-03-01 10:44:12 +08:00
{
2022-03-07 21:20:58 +08:00
BundleFileInfo = bundleInfo;
2022-03-01 10:44:12 +08:00
RefCount = 0;
2022-03-09 23:57:04 +08:00
Status = EStatus.None;
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 添加附属的资源提供者
/// </summary>
2022-03-22 20:47:22 +08:00
public void AddProvider(ProviderBase provider)
2022-03-01 10:44:12 +08:00
{
if (_providers.Contains(provider) == false)
_providers.Add(provider);
}
/// <summary>
/// 引用(引用计数递加)
/// </summary>
public void Reference()
{
RefCount++;
}
/// <summary>
/// 释放(引用计数递减)
/// </summary>
public void Release()
{
RefCount--;
}
/// <summary>
/// 轮询更新
/// </summary>
public void Update()
{
// 如果资源文件加载完毕
if (IsDone())
return;
2022-03-09 23:57:04 +08:00
if (Status == EStatus.None)
2022-03-01 10:44:12 +08:00
{
// 检测加载地址是否为空
2022-03-07 21:20:58 +08:00
if (string.IsNullOrEmpty(BundleFileInfo.LocalPath))
2022-03-01 10:44:12 +08:00
{
2022-03-09 23:57:04 +08:00
Status = EStatus.Fail;
2022-03-01 10:44:12 +08:00
return;
}
2022-03-07 21:20:58 +08:00
if (string.IsNullOrEmpty(BundleFileInfo.RemoteMainURL))
2022-03-09 23:57:04 +08:00
Status = EStatus.LoadFile;
2022-03-01 10:44:12 +08:00
else
2022-03-09 23:57:04 +08:00
Status = EStatus.Download;
2022-03-01 10:44:12 +08:00
}
// 1. 从服务器下载
2022-03-09 23:57:04 +08:00
if (Status == EStatus.Download)
2022-03-01 10:44:12 +08:00
{
int failedTryAgain = int.MaxValue;
2022-03-29 19:45:20 +08:00
_downloader = DownloadSystem.BeginDownload(BundleFileInfo, failedTryAgain);
2022-03-09 23:57:04 +08:00
Status = EStatus.CheckDownload;
2022-03-01 10:44:12 +08:00
}
// 2. 检测服务器下载结果
2022-03-09 23:57:04 +08:00
if (Status == EStatus.CheckDownload)
2022-03-01 10:44:12 +08:00
{
2022-03-29 19:45:20 +08:00
if (_downloader.IsDone() == false)
2022-03-01 10:44:12 +08:00
return;
2022-03-29 19:45:20 +08:00
if (_downloader.HasError())
2022-03-01 10:44:12 +08:00
{
2022-03-29 19:45:20 +08:00
_downloader.ReportError();
2022-03-09 23:57:04 +08:00
Status = EStatus.Fail;
2022-03-01 10:44:12 +08:00
}
else
{
2022-03-09 23:57:04 +08:00
Status = EStatus.LoadFile;
2022-03-01 10:44:12 +08:00
}
}
// 3. 加载AssetBundle
2022-03-09 23:57:04 +08:00
if (Status == EStatus.LoadFile)
2022-03-01 10:44:12 +08:00
{
#if UNITY_EDITOR
// 注意Unity2017.4编辑器模式下如果AssetBundle文件不存在会导致编辑器崩溃这里做了预判。
2022-03-07 21:20:58 +08:00
if (System.IO.File.Exists(BundleFileInfo.LocalPath) == false)
2022-03-01 10:44:12 +08:00
{
2022-03-09 21:53:01 +08:00
YooLogger.Warning($"Not found assetBundle file : {BundleFileInfo.LocalPath}");
2022-03-09 23:57:04 +08:00
Status = EStatus.Fail;
2022-03-01 10:44:12 +08:00
return;
}
#endif
// Load assetBundle file
2022-03-07 21:20:58 +08:00
if (BundleFileInfo.IsEncrypted)
2022-03-01 10:44:12 +08:00
{
2022-03-09 23:57:04 +08:00
if (AssetSystem.DecryptionServices == null)
2022-03-22 21:18:33 +08:00
throw new Exception($"{nameof(AssetBundleLoader)} need IDecryptServices : {BundleFileInfo.BundleName}");
2022-03-01 10:44:12 +08:00
2022-03-09 23:57:04 +08:00
ulong offset = AssetSystem.DecryptionServices.GetFileOffset(BundleFileInfo);
if (_isWaitForAsyncComplete)
CacheBundle = AssetBundle.LoadFromFile(BundleFileInfo.LocalPath, 0, offset);
2022-03-01 10:44:12 +08:00
else
2022-03-09 23:57:04 +08:00
_cacheRequest = AssetBundle.LoadFromFileAsync(BundleFileInfo.LocalPath, 0, offset);
2022-03-01 10:44:12 +08:00
}
else
{
if (_isWaitForAsyncComplete)
2022-03-07 21:20:58 +08:00
CacheBundle = AssetBundle.LoadFromFile(BundleFileInfo.LocalPath);
2022-03-01 10:44:12 +08:00
else
2022-03-07 21:20:58 +08:00
_cacheRequest = AssetBundle.LoadFromFileAsync(BundleFileInfo.LocalPath);
2022-03-01 10:44:12 +08:00
}
2022-03-09 23:57:04 +08:00
Status = EStatus.CheckFile;
2022-03-01 10:44:12 +08:00
}
// 4. 检测AssetBundle加载结果
2022-03-09 23:57:04 +08:00
if (Status == EStatus.CheckFile)
2022-03-01 10:44:12 +08:00
{
if (_cacheRequest != null)
{
if (_isWaitForAsyncComplete)
{
// 强制挂起主线程(注意:该操作会很耗时)
2022-03-09 21:53:01 +08:00
YooLogger.Warning("Suspend the main thread to load unity bundle.");
2022-03-01 10:44:12 +08:00
CacheBundle = _cacheRequest.assetBundle;
}
else
{
if (_cacheRequest.isDone == false)
return;
CacheBundle = _cacheRequest.assetBundle;
}
}
// Check error
if (CacheBundle == null)
{
2022-03-09 21:53:01 +08:00
YooLogger.Error($"Failed to load assetBundle file : {BundleFileInfo.BundleName}");
2022-03-09 23:57:04 +08:00
Status = EStatus.Fail;
2022-03-01 10:44:12 +08:00
}
else
{
2022-03-09 23:57:04 +08:00
Status = EStatus.Success;
2022-03-01 10:44:12 +08:00
}
}
}
/// <summary>
/// 销毁
/// </summary>
public void Destroy(bool forceDestroy)
{
IsDestroyed = true;
// Check fatal
if (forceDestroy == false)
{
if (RefCount > 0)
2022-03-07 21:20:58 +08:00
throw new Exception($"Bundle file loader ref is not zero : {BundleFileInfo.BundleName}");
2022-03-01 10:44:12 +08:00
if (IsDone() == false)
2022-03-07 21:20:58 +08:00
throw new Exception($"Bundle file loader is not done : {BundleFileInfo.BundleName}");
2022-03-01 10:44:12 +08:00
}
if (CacheBundle != null)
{
CacheBundle.Unload(true);
CacheBundle = null;
}
}
/// <summary>
/// 是否完毕(无论成功或失败)
/// </summary>
public bool IsDone()
{
2022-03-09 23:57:04 +08:00
return Status == EStatus.Success || Status == EStatus.Fail;
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 是否可以销毁
/// </summary>
public bool CanDestroy()
{
if (IsDone() == false)
return false;
return RefCount <= 0;
}
/// <summary>
/// 在满足条件的前提下,销毁所有资源提供者
/// </summary>
public void TryDestroyAllProviders()
{
if (IsDone() == false)
return;
// 注意必须等待所有Provider可以销毁的时候才可以释放Bundle文件。
foreach (var provider in _providers)
{
if (provider.CanDestroy() == false)
return;
}
// 除了自己没有其它引用
if (RefCount > _providers.Count)
return;
// 销毁所有Providers
foreach (var provider in _providers)
{
provider.Destory();
}
// 从列表里移除Providers
AssetSystem.RemoveBundleProviders(_providers);
_providers.Clear();
}
/// <summary>
/// 主线程等待异步操作完毕
/// </summary>
public void WaitForAsyncComplete()
{
_isWaitForAsyncComplete = true;
int frame = 1000;
while (true)
{
// 保险机制
// 注意如果需要从WEB端下载资源可能会触发保险机制
frame--;
if (frame == 0)
{
if (_isShowWaitForAsyncError == false)
{
_isShowWaitForAsyncError = true;
2022-03-09 23:57:04 +08:00
YooLogger.Error($"WaitForAsyncComplete failed ! BundleName : {BundleFileInfo.BundleName} States : {Status}");
2022-03-01 10:44:12 +08:00
}
break;
}
// 驱动流程
Update();
// 完成后退出
if (IsDone())
break;
}
}
}
}