- 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:
MapleWheels
2025-12-30 12:00:14 -05:00
committed by Maplewheels
parent 71c2e54afd
commit 569e14f50f
3 changed files with 46 additions and 32 deletions
@@ -112,24 +112,19 @@ namespace Barotrauma
private readonly IServicesProvider _servicesProvider; private readonly IServicesProvider _servicesProvider;
public PerformanceCounterService PerformanceCounter => _servicesProvider.TryGetService<PerformanceCounterService>(out var svc) public PerformanceCounterService PerformanceCounter =>
? svc : throw new NullReferenceException("Performance counter service not found!"); _servicesProvider.GetService<PerformanceCounterService>();
public ILoggerService Logger => _servicesProvider.TryGetService<ILoggerService>(out var svc) public ILoggerService Logger => _servicesProvider.GetService<ILoggerService>();
? svc : throw new NullReferenceException("Logger service not found!"); public IConfigService ConfigService => _servicesProvider.GetService<IConfigService>();
public IConfigService ConfigService => _servicesProvider.TryGetService<IConfigService>(out var svc) public IPackageManagementService PackageManagementService =>
? svc : throw new NullReferenceException("Config Manager service not found!"); _servicesProvider.GetService<IPackageManagementService>();
public IPackageManagementService PackageManagementService => _servicesProvider.TryGetService<IPackageManagementService>(out var svc) public IPluginManagementService PluginManagementService =>
? svc : throw new NullReferenceException("Package Manager service not found!"); _servicesProvider.GetService<IPluginManagementService>();
public IPluginManagementService PluginManagementService => _servicesProvider.TryGetService<IPluginManagementService>(out var svc) public ILuaScriptManagementService LuaScriptManagementService =>
? svc : throw new NullReferenceException("Plugin Manager service not found!"); _servicesProvider.GetService<ILuaScriptManagementService>();
public ILuaScriptManagementService LuaScriptManagementService => _servicesProvider.TryGetService<ILuaScriptManagementService>(out var svc) public INetworkingService NetworkingService => _servicesProvider.GetService<INetworkingService>();
? svc : throw new NullReferenceException("Lua Script Manager service not found!"); public IEventService EventService => _servicesProvider.GetService<IEventService>();
public INetworkingService NetworkingService => _servicesProvider.TryGetService<INetworkingService>(out var svc) public LuaGame Game => _servicesProvider.GetService<LuaGame>();
? 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!");
/* /*
* === Config Vars * === Config Vars
@@ -11,19 +11,15 @@ namespace Barotrauma.LuaCs.Services;
public class ServicesProvider : IServicesProvider public class ServicesProvider : IServicesProvider
{ {
private ServiceContainer _serviceContainerInst; private ServiceContainer _serviceContainerInst;
private ServiceContainer ServiceContainer private ServiceContainer ServiceContainer => _serviceContainerInst;
{
get
{
// ReSharper disable once ConvertIfStatementToNullCoalescingExpression
if (_serviceContainerInst is null)
_serviceContainerInst = new ServiceContainer();
return _serviceContainerInst;
}
}
private readonly ReaderWriterLockSlim _serviceLock = new(); 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 public void RegisterServiceType<TSvcInterface, TService>(ServiceLifetime lifetime, ILifetime lifetimeInstance = null) where TSvcInterface : class, IService where TService : class, IService, TSvcInterface
{ {
if (lifetimeInstance is null) if (lifetimeInstance is null)
@@ -39,9 +35,9 @@ public class ServicesProvider : IServicesProvider
// treat these as transient // treat these as transient
case ServiceLifetime.Transient: case ServiceLifetime.Transient:
case ServiceLifetime.Invalid: case ServiceLifetime.Invalid:
case ServiceLifetime.Custom: // lifetime should not be null here case ServiceLifetime.Custom:
default: default:
lifetimeInstance = new PerRequestLifeTime(); lifetimeInstance = null;
break; break;
} }
} }
@@ -49,7 +45,10 @@ public class ServicesProvider : IServicesProvider
try try
{ {
_serviceLock.EnterReadLock(); _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)); OnServiceRegistered?.Invoke(typeof(TSvcInterface), typeof(TService));
} }
finally 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 public bool TryGetService<TSvcInterface>(string name, out TSvcInterface service) where TSvcInterface : class, IService
{ {
try try
@@ -62,6 +62,13 @@ public interface IServicesProvider
/// <returns></returns> /// <returns></returns>
bool TryGetService<TSvcInterface>(out TSvcInterface service) where TSvcInterface : class, IService; 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> /// <summary>
/// Tries to get a service for the given name and interface, returns success/failure. /// Tries to get a service for the given name and interface, returns success/failure.
/// </summary> /// </summary>