192 lines
7.3 KiB
C#
192 lines
7.3 KiB
C#
using System;
|
|
|
|
namespace YooAsset
|
|
{
|
|
public class InitializePackageOperation : AsyncOperationBase
|
|
{
|
|
private enum ESteps
|
|
{
|
|
None,
|
|
SetPlayMode,
|
|
CheckOptions,
|
|
CheckPlatform,
|
|
CreateCore,
|
|
InitFileSystem,
|
|
Done,
|
|
}
|
|
|
|
private readonly ResourcePackage _package;
|
|
private readonly InitializePackageOptions _options;
|
|
private PlayModeImpl _playModeImpl;
|
|
private InitializeFileSystemOperation _initializeFileSystemOp;
|
|
private EPlayMode _playMode;
|
|
private ESteps _steps = ESteps.None;
|
|
|
|
internal InitializePackageOperation(ResourcePackage package, InitializePackageOptions options)
|
|
{
|
|
_package = package;
|
|
_options = options;
|
|
}
|
|
internal override void InternalStart()
|
|
{
|
|
_steps = ESteps.SetPlayMode;
|
|
}
|
|
internal override void InternalUpdate()
|
|
{
|
|
if (_steps == ESteps.None || _steps == ESteps.Done)
|
|
return;
|
|
|
|
if (_steps == ESteps.SetPlayMode)
|
|
{
|
|
if (_options is EditorSimulateModeOptions)
|
|
_playMode = EPlayMode.EditorSimulateMode;
|
|
else if (_options is OfflinePlayModeOptions)
|
|
_playMode = EPlayMode.OfflinePlayMode;
|
|
else if (_options is HostPlayModeOptions)
|
|
_playMode = EPlayMode.HostPlayMode;
|
|
else if (_options is WebPlayModeOptions)
|
|
_playMode = EPlayMode.WebPlayMode;
|
|
else if (_options is CustomPlayModeOptions)
|
|
_playMode = EPlayMode.CustomPlayMode;
|
|
else
|
|
throw new NotImplementedException($"{_options.GetType().Name}");
|
|
|
|
_package._playMode = _playMode;
|
|
_steps = ESteps.CheckOptions;
|
|
}
|
|
|
|
if (_steps == ESteps.CheckOptions)
|
|
{
|
|
// 检测初始化参数
|
|
if (_options.BundleLoadingMaxConcurrency <= 0)
|
|
{
|
|
_steps = ESteps.Done;
|
|
Status = EOperationStatus.Failed;
|
|
Error = $"{nameof(_options.BundleLoadingMaxConcurrency)} value must be greater than zero.";
|
|
YooLogger.Error(Error);
|
|
return;
|
|
}
|
|
|
|
_steps = ESteps.CheckPlatform;
|
|
}
|
|
|
|
if (_steps == ESteps.CheckPlatform)
|
|
{
|
|
#if !UNITY_EDITOR
|
|
if (_playMode == EPlayMode.EditorSimulateMode)
|
|
{
|
|
_steps = ESteps.Done;
|
|
Status = EOperationStatus.Failed;
|
|
Error = $"Editor simulate mode only support unity editor.";
|
|
YooLogger.Error(Error);
|
|
return;
|
|
}
|
|
#endif
|
|
|
|
#if UNITY_WEBGL
|
|
if (_playMode != EPlayMode.EditorSimulateMode)
|
|
{
|
|
if (_playMode != EPlayMode.WebPlayMode)
|
|
{
|
|
_steps = ESteps.Done;
|
|
Status = EOperationStatus.Failed;
|
|
Error = $"{_playMode} can not support WebGL plateform !";
|
|
YooLogger.Error(Error);
|
|
return;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if !UNITY_WEBGL
|
|
if (_playMode != EPlayMode.EditorSimulateMode)
|
|
{
|
|
if (_playMode == EPlayMode.WebPlayMode)
|
|
{
|
|
_steps = ESteps.Done;
|
|
Status = EOperationStatus.Failed;
|
|
Error = $"{nameof(EPlayMode.WebPlayMode)} only support WebGL plateform !";
|
|
YooLogger.Error(Error);
|
|
return;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
_steps = ESteps.CreateCore;
|
|
}
|
|
|
|
if (_steps == ESteps.CreateCore)
|
|
{
|
|
string packageName = _package.PackageName;
|
|
var resourceManager = new ResourceManager(packageName);
|
|
var playModeImpl = new PlayModeImpl(packageName);
|
|
resourceManager.Initialize(_options, playModeImpl);
|
|
|
|
_playModeImpl = playModeImpl;
|
|
_package._bundleQuery = playModeImpl;
|
|
_package._playModeImpl = playModeImpl;
|
|
_package._resourceManager = resourceManager;
|
|
_steps = ESteps.InitFileSystem;
|
|
}
|
|
|
|
if (_steps == ESteps.InitFileSystem)
|
|
{
|
|
if (_initializeFileSystemOp == null)
|
|
{
|
|
if (_playMode == EPlayMode.EditorSimulateMode)
|
|
{
|
|
var initializeParameters = _options as EditorSimulateModeOptions;
|
|
_initializeFileSystemOp = _playModeImpl.InitializeAsync(initializeParameters.EditorFileSystemParameters);
|
|
}
|
|
else if (_playMode == EPlayMode.OfflinePlayMode)
|
|
{
|
|
var initializeParameters = _options as OfflinePlayModeOptions;
|
|
_initializeFileSystemOp = _playModeImpl.InitializeAsync(initializeParameters.BuildinFileSystemParameters);
|
|
}
|
|
else if (_playMode == EPlayMode.HostPlayMode)
|
|
{
|
|
var initializeParameters = _options as HostPlayModeOptions;
|
|
_initializeFileSystemOp = _playModeImpl.InitializeAsync(initializeParameters.BuildinFileSystemParameters, initializeParameters.CacheFileSystemParameters);
|
|
}
|
|
else if (_playMode == EPlayMode.WebPlayMode)
|
|
{
|
|
var initializeParameters = _options as WebPlayModeOptions;
|
|
_initializeFileSystemOp = _playModeImpl.InitializeAsync(initializeParameters.WebServerFileSystemParameters, initializeParameters.WebRemoteFileSystemParameters);
|
|
}
|
|
else if (_playMode == EPlayMode.CustomPlayMode)
|
|
{
|
|
var initializeParameters = _options as CustomPlayModeOptions;
|
|
_initializeFileSystemOp = _playModeImpl.InitializeAsync(initializeParameters.FileSystemParameterList);
|
|
}
|
|
else
|
|
{
|
|
throw new NotImplementedException(_playMode.ToString());
|
|
}
|
|
|
|
_initializeFileSystemOp.StartOperation();
|
|
AddChildOperation(_initializeFileSystemOp);
|
|
}
|
|
|
|
_initializeFileSystemOp.UpdateOperation();
|
|
if (_initializeFileSystemOp.IsDone == false)
|
|
return;
|
|
|
|
if (_initializeFileSystemOp.Status == EOperationStatus.Succeed)
|
|
{
|
|
_steps = ESteps.Done;
|
|
Status = EOperationStatus.Succeed;
|
|
}
|
|
else
|
|
{
|
|
_steps = ESteps.Done;
|
|
Status = EOperationStatus.Failed;
|
|
Error = _initializeFileSystemOp.Error;
|
|
YooLogger.Error(Error);
|
|
}
|
|
}
|
|
}
|
|
internal override string InternalGetDesc()
|
|
{
|
|
return $"PlayMode : {_playMode}";
|
|
}
|
|
}
|
|
} |