Skip to content

ServiceManager

Namespace Mc.Scripting.Services · Kind class (sealed)

public sealed class ServiceManager

Plugin-to-plugin services: a plugin registers an implementation of an interface (an economy, a chat formatter, a region lookup) and other plugins look it up without referencing the provider’s code. The interface type lives in the provider’s assembly or in a shared one; consumers declare depends / softDepends so the runtime resolves that assembly into their load context (the types then unify). Registrations are owned by the registering plugin and removed when it unloads, which fires ServiceManager.Changed so consumers drop their references.

Member Description
IDisposable Changed<T>(Action onChanged) where T : class Calls onChanged (on the tick thread) whenever the set of providers of T changes: a registration, an unregistration or a provider plugin unloading.
T Get<T>() where T : class The highest-priority provider of T; throws ServiceUnavailableException when none is registered.
IReadOnlyList<ServiceRegistration<T>> GetAll<T>() where T : class All registrations of T, best first.
Register<T> Registers an implementation of T.
T? TryGet<T>() where T : class The highest-priority provider of T, null when none is registered.
bool Unregister<T>() where T : class Removes this plugin’s registration of T.
public IDisposable Changed<T>(Action onChanged) where T : class

Calls onChanged (on the tick thread) whenever the set of providers of T changes: a registration, an unregistration or a provider plugin unloading. Use it to refresh a cached ServiceManager.TryGet result.

Type parameters

  • T: The service type.

Parameters

Name Type Description
onChanged Action The callback.

Returns IDisposable: The subscription (disposed automatically when the plugin unloads).

public T Get<T>() where T : class

The highest-priority provider of T; throws ServiceUnavailableException when none is registered.

Type parameters

  • T: The service type.

Returns T

public IReadOnlyList<ServiceRegistration<T>> GetAll<T>() where T : class

All registrations of T, best first.

Type parameters

  • T: The service type.

Returns IReadOnlyList<ServiceRegistration<T>>

public void Register<T>(T instance, ServicePriority priority = ServicePriority.Normal) where T : class

Registers an implementation of T.

Type parameters

  • T: The service interface (or class) consumers ask for.

Parameters

Name Type Description
instance T The implementation.
priority ServicePriority = ServicePriority.Normal Its rank among several providers.
public T? TryGet<T>() where T : class

The highest-priority provider of T, null when none is registered. Thread-safe.

Type parameters

  • T: The service type.

Returns T?

public bool Unregister<T>() where T : class

Removes this plugin’s registration of T.

Type parameters

  • T: The service type.

Returns bool: Whether a registration was removed.

ServicePriority