com.alicizax.unity.framework/Runtime/ABase/Service/Core/AppServices.cs

100 lines
2.8 KiB
C#
Raw Normal View History

2026-03-26 10:49:41 +08:00
using System;
namespace AlicizaX
{
public static class AppServices
{
private static ServiceWorld _world;
public static bool HasWorld => _world != null;
2026-04-20 13:46:44 +08:00
internal static ServiceWorld EnsureWorld(int appScopeOrder = ServiceDomainOrder.App)
2026-03-26 10:49:41 +08:00
{
2026-04-20 13:46:44 +08:00
if (_world == null)
_world = new ServiceWorld(appScopeOrder);
return _world;
}
internal static ServiceWorld RequireWorld()
{
if (_world == null)
throw new InvalidOperationException("ServiceWorld has not been created yet.");
return _world;
2026-03-26 10:49:41 +08:00
}
2026-04-20 13:46:44 +08:00
public static T RegisterApp<T>(T service, params Type[] extraContracts) where T : class, IService
=> RequireWorld().App.Register(service, extraContracts);
2026-03-26 10:49:41 +08:00
2026-04-20 13:46:44 +08:00
public static bool TryGetApp<T>(out T service) where T : class, IService
{
if (_world == null)
{
service = null;
return false;
}
return _world.App.TryGet(out service);
}
2026-04-20 13:46:44 +08:00
public static T RequireApp<T>() where T : class, IService
=> RequireWorld().App.Require<T>();
2026-04-20 13:46:44 +08:00
public static bool TryGet<T>(out T service) where T : class, IService
2026-03-26 10:49:41 +08:00
{
if (_world == null)
2026-04-20 13:46:44 +08:00
{
service = null;
return false;
}
return _world.TryGet(out service);
2026-03-26 10:49:41 +08:00
}
2026-04-20 13:46:44 +08:00
public static T Require<T>() where T : class, IService
=> RequireWorld().Require<T>();
internal static ServiceScope EnsureScene(int order = ServiceDomainOrder.Scene)
=> RequireWorld().EnsureScene(order);
2026-03-26 10:49:41 +08:00
2026-04-20 13:46:44 +08:00
internal static bool TryGetScene(out ServiceScope scope)
2026-03-26 10:49:41 +08:00
{
2026-04-20 13:46:44 +08:00
if (_world == null)
{
scope = null;
return false;
}
return _world.TryGetScene(out scope);
2026-03-26 10:49:41 +08:00
}
2026-04-20 13:46:44 +08:00
internal static ServiceScope ResetScene(int order = ServiceDomainOrder.Scene)
=> RequireWorld().ResetScene(order);
2026-04-20 13:46:44 +08:00
internal static bool DestroyScene()
=> _world != null && _world.DestroyScene();
2026-04-20 13:46:44 +08:00
internal static ServiceScope EnsureGameplay(int order = ServiceDomainOrder.Gameplay)
=> RequireWorld().EnsureGameplay(order);
2026-04-20 13:46:44 +08:00
internal static bool TryGetGameplay(out ServiceScope scope)
2026-03-26 10:49:41 +08:00
{
2026-04-20 13:46:44 +08:00
if (_world == null)
{
scope = null;
return false;
}
return _world.TryGetGameplay(out scope);
2026-03-26 10:49:41 +08:00
}
2026-04-20 13:46:44 +08:00
internal static bool DestroyGameplay()
=> _world != null && _world.DestroyGameplay();
2026-03-26 10:49:41 +08:00
public static void Shutdown()
{
if (_world == null) return;
_world.Dispose();
_world = null;
}
}
}