This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using ShrinkApp;
|
||||
using ShrinkEventBus;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ShrinkContext.AppAdapter
|
||||
@@ -14,6 +17,7 @@ namespace ShrinkContext.AppAdapter
|
||||
public sealed class ShrinkAppLoaderBootstrapper : MonoBehaviour
|
||||
{
|
||||
private static ShrinkAppLoaderBootstrapper? _instance;
|
||||
private static UniTaskCompletionSource _startupCompletion = new();
|
||||
|
||||
/// <summary>
|
||||
/// Starter/组合包在 SubsystemRegistration 阶段设置的默认装配表。
|
||||
@@ -22,9 +26,43 @@ namespace ShrinkContext.AppAdapter
|
||||
public static System.Action<ShrinkAppLoaderHost>? DefaultComposition { get; set; }
|
||||
|
||||
public static ShrinkAppLoaderBootstrapper? Instance => _instance;
|
||||
public static Exception? StartError { get; private set; }
|
||||
|
||||
public ShrinkAppLoaderHost Host { get; private set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 等待 ContextLoader 宿主完成启动。支持多个并发等待者;宿主失败时以原始异常结束。
|
||||
/// </summary>
|
||||
public static async UniTask WaitUntilStartedAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_instance?.Host?.IsRunning == true)
|
||||
return;
|
||||
if (StartError != null)
|
||||
throw StartError;
|
||||
if (ShrinkAppSettings.Instance.hostingMode != ShrinkAppHostingMode.ContextLoader)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"ShrinkAppLoaderBootstrapper requires ShrinkAppHostingMode.ContextLoader.");
|
||||
}
|
||||
|
||||
var task = _startupCompletion.Task;
|
||||
if (cancellationToken.CanBeCanceled)
|
||||
await task.AttachExternalCancellation(cancellationToken);
|
||||
else
|
||||
await task;
|
||||
|
||||
if (StartError != null)
|
||||
throw StartError;
|
||||
}
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
||||
private static void ResetStaticState()
|
||||
{
|
||||
_instance = null;
|
||||
StartError = null;
|
||||
_startupCompletion = new UniTaskCompletionSource();
|
||||
}
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
private static void AutoBootstrap()
|
||||
{
|
||||
@@ -45,17 +83,39 @@ namespace ShrinkContext.AppAdapter
|
||||
}
|
||||
|
||||
_instance = this;
|
||||
Host = new ShrinkAppLoaderHost();
|
||||
DefaultComposition?.Invoke(Host);
|
||||
var profile = ShrinkAppCompositionProfile.LoadDefault();
|
||||
if (profile != null)
|
||||
Host.ApplyComposition(profile.ResolveDocument());
|
||||
global::ShrinkApp.ShrinkApp.InitializeForExternalHost(Host.Services);
|
||||
Host.StartAsync().Forget(ex =>
|
||||
StartHostAsync().Forget();
|
||||
}
|
||||
|
||||
private async UniTask StartHostAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
Host = new ShrinkAppLoaderHost();
|
||||
DefaultComposition?.Invoke(Host);
|
||||
var profile = ShrinkAppCompositionProfile.LoadDefault();
|
||||
if (profile != null)
|
||||
Host.ApplyComposition(profile.ResolveDocument());
|
||||
global::ShrinkApp.ShrinkApp.InitializeForExternalHost(Host.Services);
|
||||
await Host.StartAsync();
|
||||
CompleteStartup(null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
CompleteStartup(ex);
|
||||
EventBus.Post(new ShrinkAppStartFailedEvent
|
||||
{
|
||||
ErrorMessage = ex.Message,
|
||||
FailedModuleId = string.Empty
|
||||
});
|
||||
Debug.LogException(ex);
|
||||
Debug.LogError("[ShrinkApp.LoaderHost] 启动失败: " + ex.Message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private static void CompleteStartup(Exception? error)
|
||||
{
|
||||
StartError = error;
|
||||
_startupCompletion.TrySetResult();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
|
||||
Reference in New Issue
Block a user