Files
Workspace/Assets/Demos/ReplacedPerson/Runtime/ReplacedPersonAppIntegration.cs
T
2026-08-17 18:18:13 +08:00

76 lines
2.8 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
using ReplacedPerson.Core;
using ShrinkApp;
using ShrinkContext;
using ShrinkContext.AppAdapter;
using UnityEngine;
namespace ReplacedPerson.Runtime
{
[ShrinkAppModuleInstaller]
public sealed class ReplacedPersonAppInstaller : IShrinkAppModuleInstaller
{
public string ModuleId => "demo.replaced-person";
public int Order => 1400;
public IReadOnlyList<string> DependsOn => new[] { "shrink.command", "shrink.datasaver", "shrink.network" };
public void RegisterServices(ShrinkAppContext context)
{
if (!context.Services.TryGet<ReplacedPersonGameService>(out _))
context.Services.Register(new ReplacedPersonGameService());
}
public UniTask InitializeAsync(ShrinkAppContext context) => UniTask.CompletedTask;
}
public sealed class ReplacedPersonAppComponent : IShrinkComponent
{
public const string ModuleKey = "app.module.demo.replaced-person";
public const string ServiceKey = "demo.replaced-person.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 => "demo.replaced-person";
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<ReplacedPersonGameService>(out var existing) && existing != null
? existing
: new ReplacedPersonGameService();
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 ReplacedPersonContextComposition
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
private static void Register()
{
var previous = ShrinkAppLoaderBootstrapper.DefaultComposition;
ShrinkAppLoaderBootstrapper.DefaultComposition = host =>
{
previous?.Invoke(host);
if (host.ModuleIds.Contains("demo.replaced-person"))
host.OverrideModuleComponent("demo.replaced-person", static () => new ReplacedPersonAppComponent());
};
}
}
}