refactor : 重构资源包裹模块
This commit is contained in:
@@ -8,15 +8,11 @@ namespace YooAsset
|
||||
{
|
||||
public class ResourcePackage
|
||||
{
|
||||
private bool _isInitialize = false;
|
||||
private string _initializeError = string.Empty;
|
||||
private EOperationStatus _initializeStatus = EOperationStatus.None;
|
||||
private EPlayMode _playMode;
|
||||
|
||||
// 管理器
|
||||
private ResourceManager _resourceManager;
|
||||
private IBundleQuery _bundleQuery;
|
||||
private IPlayMode _playModeImpl;
|
||||
private InitializePackageOperation _initializeOp;
|
||||
internal ResourceManager _resourceManager;
|
||||
internal IBundleQuery _bundleQuery;
|
||||
internal IPlayMode _playModeImpl;
|
||||
internal EPlayMode _playMode;
|
||||
|
||||
/// <summary>
|
||||
/// 包裹名
|
||||
@@ -28,7 +24,12 @@ namespace YooAsset
|
||||
/// </summary>
|
||||
public EOperationStatus InitializeStatus
|
||||
{
|
||||
get { return _initializeStatus; }
|
||||
get
|
||||
{
|
||||
if (_initializeOp == null)
|
||||
return EOperationStatus.None;
|
||||
return _initializeOp.Status;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -52,7 +53,7 @@ namespace YooAsset
|
||||
get { return OperationSystem.GetSchedulerPriority(PackageName); }
|
||||
set { OperationSystem.SetSchedulerPriority(PackageName, value); }
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal ResourcePackage(string packageName)
|
||||
{
|
||||
@@ -62,40 +63,10 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 销毁资源包裹
|
||||
/// </summary>
|
||||
internal void DestroyPackage()
|
||||
internal void InternalDestroy()
|
||||
{
|
||||
if (_isInitialize)
|
||||
{
|
||||
_isInitialize = false;
|
||||
_initializeError = string.Empty;
|
||||
_initializeStatus = EOperationStatus.None;
|
||||
|
||||
// 销毁资源管理器
|
||||
if (_resourceManager != null)
|
||||
{
|
||||
_resourceManager.Destroy();
|
||||
_resourceManager = null;
|
||||
}
|
||||
|
||||
// 销毁文件系统
|
||||
if (_playModeImpl != null)
|
||||
_playModeImpl.DestroyFileSystem();
|
||||
|
||||
_bundleQuery = null;
|
||||
_playModeImpl = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步初始化
|
||||
/// </summary>
|
||||
public InitializationOperation InitializeAsync(InitializeParameters parameters)
|
||||
{
|
||||
// 注意:联机平台因为网络原因可能会初始化失败!
|
||||
ResetInitializeAfterFailed();
|
||||
|
||||
// 检测初始化参数合法性
|
||||
CheckInitializeParameters(parameters);
|
||||
_initializeOp = null;
|
||||
_bundleQuery = null;
|
||||
|
||||
// 销毁资源管理器
|
||||
if (_resourceManager != null)
|
||||
@@ -104,147 +75,75 @@ namespace YooAsset
|
||||
_resourceManager = null;
|
||||
}
|
||||
|
||||
// 创建资源管理器
|
||||
_resourceManager = new ResourceManager(PackageName);
|
||||
var playModeImpl = new PlayModeImpl(PackageName, _playMode);
|
||||
_bundleQuery = playModeImpl;
|
||||
_playModeImpl = playModeImpl;
|
||||
_resourceManager.Initialize(parameters, _bundleQuery);
|
||||
|
||||
// 初始化资源系统
|
||||
InitializationOperation initializeOperation;
|
||||
if (_playMode == EPlayMode.EditorSimulateMode)
|
||||
// 销毁文件系统
|
||||
if (_playModeImpl != null)
|
||||
{
|
||||
var initializeParameters = parameters as EditorSimulateModeParameters;
|
||||
initializeOperation = playModeImpl.InitializeAsync(initializeParameters.EditorFileSystemParameters);
|
||||
_playModeImpl.DestroyFileSystem();
|
||||
_playModeImpl = null;
|
||||
}
|
||||
else if (_playMode == EPlayMode.OfflinePlayMode)
|
||||
{
|
||||
var initializeParameters = parameters as OfflinePlayModeParameters;
|
||||
initializeOperation = playModeImpl.InitializeAsync(initializeParameters.BuildinFileSystemParameters);
|
||||
}
|
||||
else if (_playMode == EPlayMode.HostPlayMode)
|
||||
{
|
||||
var initializeParameters = parameters as HostPlayModeParameters;
|
||||
initializeOperation = playModeImpl.InitializeAsync(initializeParameters.BuildinFileSystemParameters, initializeParameters.CacheFileSystemParameters);
|
||||
}
|
||||
else if (_playMode == EPlayMode.WebPlayMode)
|
||||
{
|
||||
var initializeParameters = parameters as WebPlayModeParameters;
|
||||
initializeOperation = playModeImpl.InitializeAsync(initializeParameters.WebServerFileSystemParameters, initializeParameters.WebRemoteFileSystemParameters);
|
||||
}
|
||||
else if (_playMode == EPlayMode.CustomPlayMode)
|
||||
{
|
||||
var initializeParameters = parameters as CustomPlayModeParameters;
|
||||
initializeOperation = playModeImpl.InitializeAsync(initializeParameters.FileSystemParameterList);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
// 监听初始化结果
|
||||
_isInitialize = true;
|
||||
OperationSystem.StartOperation(PackageName, initializeOperation);
|
||||
initializeOperation.Completed += InitializeOperation_Completed;
|
||||
return initializeOperation;
|
||||
}
|
||||
private void ResetInitializeAfterFailed()
|
||||
{
|
||||
if (_isInitialize && _initializeStatus == EOperationStatus.Failed)
|
||||
{
|
||||
_isInitialize = false;
|
||||
_initializeStatus = EOperationStatus.None;
|
||||
_initializeError = string.Empty;
|
||||
}
|
||||
}
|
||||
private void CheckInitializeParameters(InitializeParameters parameters)
|
||||
{
|
||||
if (_isInitialize)
|
||||
throw new YooPackageException(PackageName, $"Package '{PackageName}' is already initialized !");
|
||||
|
||||
if (parameters == null)
|
||||
throw new YooPackageException(PackageName, $"Initialize parameters cannot be null.");
|
||||
|
||||
#if !UNITY_EDITOR
|
||||
if (parameters is EditorSimulateModeParameters)
|
||||
throw new YooPlatformNotSupportedException($"Editor simulate mode only support unity editor.");
|
||||
#endif
|
||||
|
||||
// 检测初始化参数
|
||||
if (parameters.BundleLoadingMaxConcurrency <= 0)
|
||||
throw new YooPackageException(PackageName, $"{nameof(parameters.BundleLoadingMaxConcurrency)} value must be greater than zero.");
|
||||
|
||||
// 鉴定运行模式
|
||||
if (parameters is EditorSimulateModeParameters)
|
||||
_playMode = EPlayMode.EditorSimulateMode;
|
||||
else if (parameters is OfflinePlayModeParameters)
|
||||
_playMode = EPlayMode.OfflinePlayMode;
|
||||
else if (parameters is HostPlayModeParameters)
|
||||
_playMode = EPlayMode.HostPlayMode;
|
||||
else if (parameters is WebPlayModeParameters)
|
||||
_playMode = EPlayMode.WebPlayMode;
|
||||
else if (parameters is CustomPlayModeParameters)
|
||||
_playMode = EPlayMode.CustomPlayMode;
|
||||
else
|
||||
throw new NotImplementedException();
|
||||
|
||||
// 检测运行时平台
|
||||
if (_playMode != EPlayMode.EditorSimulateMode)
|
||||
{
|
||||
#if UNITY_WEBGL
|
||||
if (_playMode != EPlayMode.WebPlayMode)
|
||||
{
|
||||
throw new YooPlatformNotSupportedException($"{_playMode} can not support WebGL plateform !");
|
||||
}
|
||||
#else
|
||||
if (_playMode == EPlayMode.WebPlayMode)
|
||||
{
|
||||
throw new YooPlatformNotSupportedException($"{nameof(EPlayMode.WebPlayMode)} only support WebGL plateform !");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
private void InitializeOperation_Completed(AsyncOperationBase op)
|
||||
{
|
||||
_initializeStatus = op.Status;
|
||||
_initializeError = op.Error;
|
||||
if (_initializeStatus != EOperationStatus.Succeed)
|
||||
YooLogger.Error(_initializeError);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步销毁
|
||||
/// 异步初始化包裹
|
||||
/// </summary>
|
||||
public DestroyOperation DestroyAsync()
|
||||
public InitializePackageOperation InitializePackageAsync(InitializePackageOptions options)
|
||||
{
|
||||
var options = new UnloadAllAssetsOptions();
|
||||
options.ReleaseAllHandles = true;
|
||||
options.LockLoadOperation = true;
|
||||
var operation = new DestroyOperation(this, options);
|
||||
// 注意:联机平台因为网络原因可能会初始化失败!
|
||||
ResetInitializeAfterFailed();
|
||||
|
||||
// 检测重复初始化
|
||||
if (_initializeOp != null)
|
||||
throw new YooPackageException(PackageName, $"Package '{PackageName}' is already initialized !");
|
||||
|
||||
// 开始初始化操作
|
||||
_initializeOp = new InitializePackageOperation(this, options);
|
||||
OperationSystem.StartOperation(PackageName, _initializeOp);
|
||||
return _initializeOp;
|
||||
}
|
||||
private void ResetInitializeAfterFailed()
|
||||
{
|
||||
if (InitializeStatus == EOperationStatus.Failed)
|
||||
{
|
||||
InternalDestroy();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步销毁包裹
|
||||
/// </summary>
|
||||
public DestroyPackageOperation DestroyPackageAsync()
|
||||
{
|
||||
var options = new UnloadAllAssetsOptions(true, true);
|
||||
var operation = new DestroyPackageOperation(this, options);
|
||||
OperationSystem.StartOperation(OperationSystem.GLOBAL_SCHEDULER_NAME, operation);
|
||||
return operation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 请求最新的资源版本
|
||||
/// 说明:超时时间默认60秒
|
||||
/// </summary>
|
||||
/// <param name="appendTimeTicks">在URL末尾添加时间戳</param>
|
||||
/// <param name="timeout">超时时间(默认值:60秒)</param>
|
||||
public RequestPackageVersionOperation RequestPackageVersionAsync(bool appendTimeTicks = true, int timeout = 60)
|
||||
public RequestPackageVersionOperation RequestPackageVersionAsync()
|
||||
{
|
||||
var options = new RequestPackageVersionOptions(true, 60);
|
||||
return RequestPackageVersionAsync(options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 请求最新的资源版本
|
||||
/// </summary>
|
||||
public RequestPackageVersionOperation RequestPackageVersionAsync(RequestPackageVersionOptions options)
|
||||
{
|
||||
DebugCheckInitialize(false);
|
||||
var operation = _playModeImpl.RequestPackageVersionAsync(appendTimeTicks, timeout);
|
||||
var operation = _playModeImpl.RequestPackageVersionAsync(options);
|
||||
OperationSystem.StartOperation(PackageName, operation);
|
||||
return operation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新并加载指定版本的资源清单
|
||||
/// 加载指定版本的资源清单
|
||||
/// </summary>
|
||||
/// <param name="packageVersion">包裹版本</param>
|
||||
/// <param name="timeout">超时时间(默认值:60秒)</param>
|
||||
public UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageVersion, int timeout = 60)
|
||||
public LoadPackageManifestOperation LoadPackageManifestAsync(LoadPackageManifestOptions options)
|
||||
{
|
||||
DebugCheckInitialize(false);
|
||||
|
||||
@@ -254,7 +153,7 @@ namespace YooAsset
|
||||
YooLogger.Warning($"Found loaded bundle before update manifest ! Recommended to call the {nameof(UnloadAllAssetsAsync)} method to release loaded bundle !");
|
||||
}
|
||||
|
||||
var operation = _playModeImpl.UpdatePackageManifestAsync(packageVersion, timeout);
|
||||
var operation = _playModeImpl.LoadPackageManifestAsync(options);
|
||||
OperationSystem.StartOperation(PackageName, operation);
|
||||
return operation;
|
||||
}
|
||||
@@ -262,12 +161,10 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 预下载指定版本的包裹资源
|
||||
/// </summary>
|
||||
/// <param name="packageVersion">包裹版本</param>
|
||||
/// <param name="timeout">资源清单下载的超时时间(默认值:60秒)</param>
|
||||
public PreDownloadContentOperation PreDownloadContentAsync(string packageVersion, int timeout = 60)
|
||||
public PreDownloadContentOperation PreDownloadContentAsync(PreDownloadContentOptions options)
|
||||
{
|
||||
DebugCheckInitialize(false);
|
||||
var operation = _playModeImpl.PreDownloadContentAsync(packageVersion, timeout);
|
||||
var operation = _playModeImpl.PreDownloadContentAsync(options);
|
||||
OperationSystem.StartOperation(PackageName, operation);
|
||||
return operation;
|
||||
}
|
||||
@@ -275,30 +172,9 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 清理缓存文件
|
||||
/// </summary>
|
||||
/// <param name="clearMode">清理方式</param>
|
||||
/// <param name="clearParam">执行参数</param>
|
||||
public ClearCacheFilesOperation ClearCacheFilesAsync(EFileClearMode clearMode, object clearParam = null)
|
||||
public ClearCacheFilesOperation ClearCacheFilesAsync(ClearCacheFilesOptions options)
|
||||
{
|
||||
DebugCheckInitialize(false);
|
||||
ClearCacheFilesOptions options = new ClearCacheFilesOptions();
|
||||
options.ClearMode = clearMode.ToString();
|
||||
options.ClearParam = clearParam;
|
||||
var operation = _playModeImpl.ClearCacheFilesAsync(options);
|
||||
OperationSystem.StartOperation(PackageName, operation);
|
||||
return operation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理缓存文件
|
||||
/// </summary>
|
||||
/// <param name="clearMode">清理方式</param>
|
||||
/// <param name="clearParam">执行参数</param>
|
||||
public ClearCacheFilesOperation ClearCacheFilesAsync(string clearMode, object clearParam = null)
|
||||
{
|
||||
DebugCheckInitialize(false);
|
||||
ClearCacheFilesOptions options = new ClearCacheFilesOptions();
|
||||
options.ClearMode = clearMode;
|
||||
options.ClearParam = clearParam;
|
||||
var operation = _playModeImpl.ClearCacheFilesAsync(options);
|
||||
OperationSystem.StartOperation(PackageName, operation);
|
||||
return operation;
|
||||
@@ -1165,10 +1041,22 @@ namespace YooAsset
|
||||
#region 调试方法
|
||||
private void DebugCheckInitialize(bool checkActiveManifest = true)
|
||||
{
|
||||
if (_initializeStatus == EOperationStatus.None)
|
||||
throw new YooPackageException(PackageName, "Package initialize not completed !");
|
||||
else if (_initializeStatus == EOperationStatus.Failed)
|
||||
throw new YooPackageException(PackageName, $"Package initialize failed ! {_initializeError}");
|
||||
if (InitializeStatus != EOperationStatus.Succeed)
|
||||
{
|
||||
if (InitializeStatus == EOperationStatus.None)
|
||||
{
|
||||
throw new YooPackageException(PackageName, "Package not initialize !");
|
||||
}
|
||||
else if (InitializeStatus == EOperationStatus.Processing)
|
||||
{
|
||||
throw new YooPackageException(PackageName, "Package initialize not completed !");
|
||||
}
|
||||
else if (InitializeStatus == EOperationStatus.Failed)
|
||||
{
|
||||
string error = _initializeOp == null ? string.Empty : _initializeOp.Error;
|
||||
throw new YooPackageException(PackageName, $"Package initialize failed ! {error}");
|
||||
}
|
||||
}
|
||||
|
||||
if (checkActiveManifest)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user