Added ServiceProvider to ApplicationContext and added static callable reference to the ApplicationContext instance

This commit is contained in:
clodan
2025-03-09 16:50:09 +00:00
parent 587c1f4547
commit 3c83018513
3 changed files with 21 additions and 2 deletions
@@ -9,6 +9,22 @@ public class ApplicationContext
protected readonly Dictionary<ContextVariableType, LinkedList<ContextVariable>> variables = new();
private readonly Lock _lockObject = new();
private static ApplicationContext? _applicationContext;
/// <summary>
/// When ApplicationContext gets created by the DI container we store the singleton reference so we can provide it
/// statically for harmony patches!
/// </summary>
public ApplicationContext()
{
_applicationContext = this;
}
public static ApplicationContext? GetInstance()
{
return _applicationContext;
}
public ContextVariable? GetLatestValue(ContextVariableType type)
{
lock (_lockObject)
@@ -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
}
+3 -1
View File
@@ -45,6 +45,9 @@ public static class Program
// Initialize Watermak
watermark?.Initialize();
var appContext = serviceProvider.GetService<ApplicationContext>();
appContext?.AddValue(ContextVariableType.SERVICE_PROVIDER, serviceProvider);
// Initialize PreSptMods
var preSptLoadMods = serviceProvider.GetServices<IPreSptLoadMod>();
foreach (var preSptLoadMod in preSptLoadMods)
@@ -52,7 +55,6 @@ public static class Program
preSptLoadMod.PreSptLoad();
}
var appContext = serviceProvider.GetService<ApplicationContext>();
// 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