From 3c830185137064a01c9510c2d1d4e508b094fe9a Mon Sep 17 00:00:00 2001 From: clodan Date: Sun, 9 Mar 2025 16:50:09 +0000 Subject: [PATCH] Added ServiceProvider to ApplicationContext and added static callable reference to the ApplicationContext instance --- .../Context/ApplicationContext.cs | 16 ++++++++++++++++ .../Context/ContextVariableType.cs | 3 ++- SPTarkov.Server/Program.cs | 4 +++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs b/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs index c7308ba7..00d524a0 100644 --- a/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs +++ b/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs @@ -9,6 +9,22 @@ public class ApplicationContext protected readonly Dictionary> variables = new(); private readonly Lock _lockObject = new(); + private static ApplicationContext? _applicationContext; + + /// + /// When ApplicationContext gets created by the DI container we store the singleton reference so we can provide it + /// statically for harmony patches! + /// + public ApplicationContext() + { + _applicationContext = this; + } + + public static ApplicationContext? GetInstance() + { + return _applicationContext; + } + public ContextVariable? GetLatestValue(ContextVariableType type) { lock (_lockObject) diff --git a/Libraries/SPTarkov.Server.Core/Context/ContextVariableType.cs b/Libraries/SPTarkov.Server.Core/Context/ContextVariableType.cs index 85b3d35b..69dcda07 100644 --- a/Libraries/SPTarkov.Server.Core/Context/ContextVariableType.cs +++ b/Libraries/SPTarkov.Server.Core/Context/ContextVariableType.cs @@ -19,5 +19,6 @@ public enum ContextVariableType TRANSIT_INFO = 5, APP_BUILDER = 6, LOADED_MOD_ASSEMBLIES = 7, - WEB_APPLICATION = 8 + WEB_APPLICATION = 8, + SERVICE_PROVIDER = 9 } diff --git a/SPTarkov.Server/Program.cs b/SPTarkov.Server/Program.cs index 85f678e5..cca906fb 100644 --- a/SPTarkov.Server/Program.cs +++ b/SPTarkov.Server/Program.cs @@ -45,6 +45,9 @@ public static class Program // Initialize Watermak watermark?.Initialize(); + var appContext = serviceProvider.GetService(); + appContext?.AddValue(ContextVariableType.SERVICE_PROVIDER, serviceProvider); + // Initialize PreSptMods var preSptLoadMods = serviceProvider.GetServices(); foreach (var preSptLoadMod in preSptLoadMods) @@ -52,7 +55,6 @@ public static class Program preSptLoadMod.PreSptLoad(); } - var appContext = serviceProvider.GetService(); // Add the Loaded Mod Assemblies for later appContext?.AddValue(ContextVariableType.LOADED_MOD_ASSEMBLIES, mods); // This is the builder that will get use by the HttpServer to start up the web application