Files
Workspace/Assets/Demos/Demo2/Runtime/Demo2AppIntegration.cs
T
2026-08-18 02:03:34 +08:00

64 lines
2.6 KiB
C#

#nullable enable
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
using ShrinkApp;
using ShrinkContext;
using ShrinkContext.AppAdapter;
using UnityEngine;
namespace Demo2.Runtime
{
[ShrinkAppModuleInstaller]
public sealed class Demo2AppInstaller : IShrinkAppModuleInstaller
{
public string ModuleId => "demo2.rule-factory";
public int Order => 1450;
public IReadOnlyList<string> DependsOn => new[] { "shrink.command", "shrink.datasaver", "shrink.network" };
public void RegisterServices(ShrinkAppContext context)
{
if (!context.Services.TryGet<Demo2GameService>(out _))
context.Services.Register(Demo2GameService.Current ?? new Demo2GameService());
}
public UniTask InitializeAsync(ShrinkAppContext context) => UniTask.CompletedTask;
}
public sealed class Demo2AppComponent : IShrinkComponent
{
public const string ModuleKey = "app.module.demo2.rule-factory";
public const string ServiceKey = "demo2.rule-factory.service";
private static readonly string[] InjectKeys = { "shrink.service.command", "shrink.service.datasaver", "shrink.service.network" };
private static readonly string[] ProvideKeys = { ModuleKey, ServiceKey };
public string Name => "demo2.rule-factory";
public IReadOnlyList<string> Inject => InjectKeys;
public IReadOnlyList<string> Provide => ProvideKeys;
public UniTask ApplyAsync(ShrinkCtx ctx, object? config)
{
var services = config as ShrinkAppServices;
var service = services != null && services.TryGet<Demo2GameService>(out var existing) && existing != null
? existing
: Demo2GameService.Current ?? new Demo2GameService();
services?.Register(service);
ctx.Set(ModuleKey, Name);
ctx.Set(ServiceKey, service);
ctx.EffectInverse(() => { services?.TryUnregister(service); service.Dispose(); return UniTask.CompletedTask; });
return UniTask.CompletedTask;
}
}
public static class Demo2ContextComposition
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
private static void Register()
{
var previous = ShrinkAppLoaderBootstrapper.DefaultComposition;
ShrinkAppLoaderBootstrapper.DefaultComposition = host =>
{
previous?.Invoke(host);
if (host.ModuleIds.Contains("demo2.rule-factory")) host.OverrideModuleComponent("demo2.rule-factory", static () => new Demo2AppComponent());
};
}
}
}