mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-22 08:20:18 +00:00
refactor : 代码重构
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9c86f68bc316ce54ca9396e6e6c2dd8f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,121 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 从 WebGL 游戏平台加载非加密 AssetBundle 操作
|
||||
/// </summary>
|
||||
internal sealed class LoadWebGameAssetBundleOperation : BCLoadBundleOperation
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
BundleRequest,
|
||||
CheckRequest,
|
||||
TryAgain,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly LoadWebGameAssetBundleOptions _options;
|
||||
private readonly DownloadRetryController _downloadRetryController;
|
||||
private IDownloadAssetBundleRequest _downloadAssetBundleRequest;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
internal LoadWebGameAssetBundleOperation(LoadWebGameAssetBundleOptions options)
|
||||
{
|
||||
_options = options;
|
||||
|
||||
// 注意:网络原因失败后,重新尝试直到成功
|
||||
_downloadRetryController = new DownloadRetryController(int.MaxValue, options.DownloadRetryPolicy);
|
||||
}
|
||||
protected override void InternalStart()
|
||||
{
|
||||
_steps = ESteps.BundleRequest;
|
||||
}
|
||||
protected override void InternalUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.BundleRequest)
|
||||
{
|
||||
string url = _options.DownloadUrlPolicy.SelectUrl(_options.CandidateUrls);
|
||||
var args = new DownloadRequestArgs(
|
||||
url: url,
|
||||
timeout: 0,
|
||||
watchdogTimeout: _options.WatchdogTimeout);
|
||||
_downloadAssetBundleRequest = new WebGameAssetBundleRequest(args, _options.GamePlatform);
|
||||
_downloadAssetBundleRequest.SendRequest();
|
||||
_steps = ESteps.CheckRequest;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.CheckRequest)
|
||||
{
|
||||
//TODO 部分小游戏平台的 downloadProgress 始终返回 0,导致进度条无法正确显示。
|
||||
Progress = _downloadAssetBundleRequest.DownloadProgress;
|
||||
if (_downloadAssetBundleRequest.IsDone == false)
|
||||
return;
|
||||
|
||||
if (_downloadAssetBundleRequest.Status == EDownloadRequestStatus.Succeeded)
|
||||
{
|
||||
_options.DownloadUrlPolicy.OnRequestSucceeded(_downloadAssetBundleRequest.Url);
|
||||
var assetBundle = _downloadAssetBundleRequest.Result;
|
||||
if (assetBundle == null)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
SetError($"Downloaded asset bundle is null. URL: {_downloadAssetBundleRequest.Url}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
SetResult();
|
||||
BundleHandle = new WebGameAssetBundleHandle(_options.CacheFilePath, _options.Bundle, assetBundle, _options.GamePlatform);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string url = _downloadAssetBundleRequest.Url;
|
||||
long httpCode = _downloadAssetBundleRequest.HttpCode;
|
||||
string httpError = _downloadAssetBundleRequest.HttpError;
|
||||
_options.DownloadUrlPolicy.OnRequestFailed(url, httpCode, httpError);
|
||||
|
||||
if (IsWaitForCompletion == false && _downloadRetryController.CanRetryRequest(url, httpCode, httpError))
|
||||
{
|
||||
_downloadRetryController.StartRetryDelay();
|
||||
_steps = ESteps.TryAgain;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
SetError(_downloadAssetBundleRequest.Error);
|
||||
YooLogger.LogError(Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (_steps == ESteps.TryAgain)
|
||||
{
|
||||
// 注意:失败后释放网络请求
|
||||
if (_downloadAssetBundleRequest != null)
|
||||
{
|
||||
_downloadAssetBundleRequest.Dispose();
|
||||
_downloadAssetBundleRequest = null;
|
||||
}
|
||||
|
||||
if (_downloadRetryController.TickRetryDelay())
|
||||
{
|
||||
Progress = 0f;
|
||||
_steps = ESteps.BundleRequest;
|
||||
}
|
||||
}
|
||||
}
|
||||
protected override void InternalDispose()
|
||||
{
|
||||
if (_downloadAssetBundleRequest != null)
|
||||
{
|
||||
_downloadAssetBundleRequest.Dispose();
|
||||
_downloadAssetBundleRequest = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5ef65b981b91200439b05f29435e7c92
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,58 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// WebGL 游戏平台加载 AssetBundle 的配置选项
|
||||
/// </summary>
|
||||
internal readonly struct LoadWebGameAssetBundleOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 资源包描述
|
||||
/// </summary>
|
||||
public PackageBundle Bundle { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 候选下载地址列表
|
||||
/// </summary>
|
||||
public IReadOnlyList<string> CandidateUrls { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 游戏平台接口
|
||||
/// </summary>
|
||||
public IWebGamePlatform GamePlatform { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 平台侧缓存文件路径
|
||||
/// </summary>
|
||||
public string CacheFilePath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 看门狗超时时间
|
||||
/// </summary>
|
||||
public int WatchdogTimeout { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 下载重试判定策略
|
||||
/// </summary>
|
||||
public IDownloadRetryPolicy DownloadRetryPolicy { get; }
|
||||
|
||||
/// <summary>
|
||||
/// URL 选择策略
|
||||
/// </summary>
|
||||
public IDownloadUrlPolicy DownloadUrlPolicy { get; }
|
||||
|
||||
public LoadWebGameAssetBundleOptions(PackageBundle bundle, IReadOnlyList<string> candidateUrls,
|
||||
IWebGamePlatform gamePlatform, string cacheFilePath, int watchdogTimeout,
|
||||
IDownloadRetryPolicy downloadRetryPolicy, IDownloadUrlPolicy downloadUrlPolicy)
|
||||
{
|
||||
Bundle = bundle;
|
||||
CandidateUrls = candidateUrls;
|
||||
GamePlatform = gamePlatform;
|
||||
CacheFilePath = cacheFilePath;
|
||||
WatchdogTimeout = watchdogTimeout;
|
||||
DownloadRetryPolicy = downloadRetryPolicy;
|
||||
DownloadUrlPolicy = downloadUrlPolicy;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f71ffbb8bf041e5478862cc179cd7e3f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// WebGL 游戏平台缓存系统初始化操作
|
||||
/// </summary>
|
||||
internal sealed class WGBCInitializeOperation : BCInitializeOperation
|
||||
{
|
||||
protected override void InternalStart()
|
||||
{
|
||||
SetResult();
|
||||
}
|
||||
protected override void InternalUpdate()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5b2ea6881f51a434db344d28e4e5f410
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,117 @@
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// WebGL 游戏平台加载 AssetBundle 操作
|
||||
/// </summary>
|
||||
internal sealed class WGBCLoadAssetBundleOperation : BCLoadBundleOperation
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
GetEntry,
|
||||
LoadBundle,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly WebGameBundleCache _fileCache;
|
||||
private readonly BCLoadBundleOptions _options;
|
||||
private BCLoadBundleOperation _loadBundleOp;
|
||||
private WebGameBundleCacheEntry _cacheEntry;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
internal WGBCLoadAssetBundleOperation(WebGameBundleCache fileCache, BCLoadBundleOptions options)
|
||||
{
|
||||
_fileCache = fileCache;
|
||||
_options = options;
|
||||
}
|
||||
protected override void InternalStart()
|
||||
{
|
||||
_steps = ESteps.GetEntry;
|
||||
}
|
||||
protected override void InternalUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.GetEntry)
|
||||
{
|
||||
_cacheEntry = _fileCache.GetEntry(_options.Bundle);
|
||||
if (_cacheEntry == null)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
SetError($"File cache entry not found: '{_options.Bundle.BundleGuid}'.");
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.LoadBundle;
|
||||
}
|
||||
}
|
||||
|
||||
if (_steps == ESteps.LoadBundle)
|
||||
{
|
||||
if (_loadBundleOp == null)
|
||||
{
|
||||
if (_options.Bundle.IsEncrypted)
|
||||
{
|
||||
var options = new LoadWebAssetBundleOptions(
|
||||
cacheName: _fileCache.GetType().Name,
|
||||
bundle: _options.Bundle,
|
||||
candidateUrls: _cacheEntry.Urls,
|
||||
assetBundleDecryptor: _fileCache.Config.AssetBundleDecryptor,
|
||||
downloadBackend: _fileCache.Config.DownloadBackend,
|
||||
downloadVerifyLevel: _fileCache.Config.DownloadVerifyLevel,
|
||||
watchdogTimeout: _fileCache.Config.WatchdogTimeout,
|
||||
disableUnityWebCache: _fileCache.Config.DisableUnityWebCache,
|
||||
downloadRetryPolicy: _fileCache.Config.DownloadRetryPolicy,
|
||||
downloadUrlPolicy: _fileCache.Config.DownloadUrlPolicy);
|
||||
_loadBundleOp = new LoadWebEncryptedAssetBundleOperation(options);
|
||||
}
|
||||
else
|
||||
{
|
||||
var webGameOptions = new LoadWebGameAssetBundleOptions(
|
||||
bundle: _options.Bundle,
|
||||
candidateUrls: _cacheEntry.Urls,
|
||||
gamePlatform: _fileCache.Config.GamePlatform,
|
||||
cacheFilePath: _cacheEntry.CacheFilePath,
|
||||
watchdogTimeout: _fileCache.Config.WatchdogTimeout,
|
||||
downloadRetryPolicy: _fileCache.Config.DownloadRetryPolicy,
|
||||
downloadUrlPolicy: _fileCache.Config.DownloadUrlPolicy);
|
||||
_loadBundleOp = new LoadWebGameAssetBundleOperation(webGameOptions);
|
||||
}
|
||||
|
||||
_loadBundleOp.StartOperation();
|
||||
AddChildOperation(_loadBundleOp);
|
||||
}
|
||||
|
||||
_loadBundleOp.UpdateOperation();
|
||||
Progress = _loadBundleOp.Progress;
|
||||
if (_loadBundleOp.IsDone == false)
|
||||
return;
|
||||
|
||||
if (_loadBundleOp.Status == EOperationStatus.Succeeded)
|
||||
{
|
||||
if (_loadBundleOp.BundleHandle == null)
|
||||
throw new YooInternalException("Loaded bundle handle is null.");
|
||||
|
||||
_steps = ESteps.Done;
|
||||
SetResult();
|
||||
BundleHandle = _loadBundleOp.BundleHandle;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
SetError(_loadBundleOp.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
protected override void InternalWaitForCompletion()
|
||||
{
|
||||
if (_steps != ESteps.Done)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
SetError("WebGL platform does not support synchronous loading.");
|
||||
YooLogger.LogError(Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c5aefbf5ae866434483465f633edf635
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user