- Removed generic exception from LuaCsSetup.cs services retrival, replaced with exception propagation.
- Fixed `PerScopeLifetime` and `Transient` lifetimes for container services.
This commit is contained in:
@@ -111,25 +111,20 @@ namespace Barotrauma
|
||||
*/
|
||||
|
||||
private readonly IServicesProvider _servicesProvider;
|
||||
|
||||
public PerformanceCounterService PerformanceCounter => _servicesProvider.TryGetService<PerformanceCounterService>(out var svc)
|
||||
? svc : throw new NullReferenceException("Performance counter service not found!");
|
||||
public ILoggerService Logger => _servicesProvider.TryGetService<ILoggerService>(out var svc)
|
||||
? svc : throw new NullReferenceException("Logger service not found!");
|
||||
public IConfigService ConfigService => _servicesProvider.TryGetService<IConfigService>(out var svc)
|
||||
? svc : throw new NullReferenceException("Config Manager service not found!");
|
||||
public IPackageManagementService PackageManagementService => _servicesProvider.TryGetService<IPackageManagementService>(out var svc)
|
||||
? svc : throw new NullReferenceException("Package Manager service not found!");
|
||||
public IPluginManagementService PluginManagementService => _servicesProvider.TryGetService<IPluginManagementService>(out var svc)
|
||||
? svc : throw new NullReferenceException("Plugin Manager service not found!");
|
||||
public ILuaScriptManagementService LuaScriptManagementService => _servicesProvider.TryGetService<ILuaScriptManagementService>(out var svc)
|
||||
? svc : throw new NullReferenceException("Lua Script Manager service not found!");
|
||||
public INetworkingService NetworkingService => _servicesProvider.TryGetService<INetworkingService>(out var svc)
|
||||
? svc : throw new NullReferenceException("Networking Manager service not found!");
|
||||
public IEventService EventService => _servicesProvider.TryGetService<IEventService>(out var svc)
|
||||
? svc : throw new NullReferenceException("Networking Manager service not found!");
|
||||
public LuaGame Game => _servicesProvider.TryGetService<LuaGame>(out var svc)
|
||||
? svc : throw new NullReferenceException("LuaGame service not found!");
|
||||
|
||||
public PerformanceCounterService PerformanceCounter =>
|
||||
_servicesProvider.GetService<PerformanceCounterService>();
|
||||
public ILoggerService Logger => _servicesProvider.GetService<ILoggerService>();
|
||||
public IConfigService ConfigService => _servicesProvider.GetService<IConfigService>();
|
||||
public IPackageManagementService PackageManagementService =>
|
||||
_servicesProvider.GetService<IPackageManagementService>();
|
||||
public IPluginManagementService PluginManagementService =>
|
||||
_servicesProvider.GetService<IPluginManagementService>();
|
||||
public ILuaScriptManagementService LuaScriptManagementService =>
|
||||
_servicesProvider.GetService<ILuaScriptManagementService>();
|
||||
public INetworkingService NetworkingService => _servicesProvider.GetService<INetworkingService>();
|
||||
public IEventService EventService => _servicesProvider.GetService<IEventService>();
|
||||
public LuaGame Game => _servicesProvider.GetService<LuaGame>();
|
||||
|
||||
/*
|
||||
* === Config Vars
|
||||
|
||||
@@ -11,18 +11,14 @@ namespace Barotrauma.LuaCs.Services;
|
||||
public class ServicesProvider : IServicesProvider
|
||||
{
|
||||
private ServiceContainer _serviceContainerInst;
|
||||
private ServiceContainer ServiceContainer
|
||||
{
|
||||
get
|
||||
{
|
||||
// ReSharper disable once ConvertIfStatementToNullCoalescingExpression
|
||||
if (_serviceContainerInst is null)
|
||||
_serviceContainerInst = new ServiceContainer();
|
||||
return _serviceContainerInst;
|
||||
}
|
||||
}
|
||||
private ServiceContainer ServiceContainer => _serviceContainerInst;
|
||||
|
||||
private readonly ReaderWriterLockSlim _serviceLock = new();
|
||||
|
||||
public ServicesProvider()
|
||||
{
|
||||
_serviceContainerInst = new ServiceContainer();
|
||||
}
|
||||
|
||||
public void RegisterServiceType<TSvcInterface, TService>(ServiceLifetime lifetime, ILifetime lifetimeInstance = null) where TSvcInterface : class, IService where TService : class, IService, TSvcInterface
|
||||
{
|
||||
@@ -39,9 +35,9 @@ public class ServicesProvider : IServicesProvider
|
||||
// treat these as transient
|
||||
case ServiceLifetime.Transient:
|
||||
case ServiceLifetime.Invalid:
|
||||
case ServiceLifetime.Custom: // lifetime should not be null here
|
||||
case ServiceLifetime.Custom:
|
||||
default:
|
||||
lifetimeInstance = new PerRequestLifeTime();
|
||||
lifetimeInstance = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -49,7 +45,10 @@ public class ServicesProvider : IServicesProvider
|
||||
try
|
||||
{
|
||||
_serviceLock.EnterReadLock();
|
||||
ServiceContainer.Register<TSvcInterface, TService>(lifetimeInstance);
|
||||
if (lifetimeInstance is not null)
|
||||
ServiceContainer.Register<TSvcInterface, TService>(lifetimeInstance);
|
||||
else
|
||||
ServiceContainer.Register<TSvcInterface, TService>();
|
||||
OnServiceRegistered?.Invoke(typeof(TSvcInterface), typeof(TService));
|
||||
}
|
||||
finally
|
||||
@@ -145,6 +144,19 @@ public class ServicesProvider : IServicesProvider
|
||||
}
|
||||
}
|
||||
|
||||
public TSvcInterface GetService<TSvcInterface>() where TSvcInterface : class, IService
|
||||
{
|
||||
try
|
||||
{
|
||||
_serviceLock.EnterReadLock();
|
||||
return ServiceContainer.GetInstance<TSvcInterface>();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_serviceLock.ExitReadLock();
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetService<TSvcInterface>(string name, out TSvcInterface service) where TSvcInterface : class, IService
|
||||
{
|
||||
try
|
||||
|
||||
@@ -62,6 +62,13 @@ public interface IServicesProvider
|
||||
/// <returns></returns>
|
||||
bool TryGetService<TSvcInterface>(out TSvcInterface service) where TSvcInterface : class, IService;
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get a service for the given interface, throws an exception upon failure.
|
||||
/// </summary>
|
||||
/// <typeparam name="TSvcInterface"></typeparam>
|
||||
/// <returns></returns>
|
||||
TSvcInterface GetService<TSvcInterface>() where TSvcInterface : class, IService;
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get a service for the given name and interface, returns success/failure.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user