add plugins lifetime as singleton

This commit is contained in:
zznty
2025-06-03 04:23:30 +07:00
parent a2b13c15c5
commit 51a817cc19
3 changed files with 12 additions and 6 deletions

View File

@@ -46,7 +46,7 @@ public class Launcher : ICorePlugin
private const uint AppId = 244850U; private const uint AppId = 244850U;
private SpaceEngineersGame? _game; private SpaceEngineersGame? _game;
private readonly Harmony _harmony = new("CringeBootstrap"); private readonly Harmony _harmony = new("CringeBootstrap");
private PluginsLifetime? _lifetime; private IPluginsLifetime? _lifetime;
private MyGameRenderComponent? _renderComponent; private MyGameRenderComponent? _renderComponent;
@@ -93,7 +93,7 @@ public class Launcher : ICorePlugin
var serviceProvider = SetupServices(); var serviceProvider = SetupServices();
splash.DefineStage(_lifetime = serviceProvider.GetRequiredService<PluginsLifetime>()); splash.DefineStage(_lifetime = serviceProvider.GetRequiredService<IPluginsLifetime>());
InitTexts(); InitTexts();
SpaceEngineersGame.SetupBasicGameInfo(); SpaceEngineersGame.SetupBasicGameInfo();
@@ -166,6 +166,7 @@ public class Launcher : ICorePlugin
.AddPolicyHandler(HttpPolicyExtensions.HandleTransientHttpError().WaitAndRetryAsync(5, _ => TimeSpan.FromSeconds(1))); .AddPolicyHandler(HttpPolicyExtensions.HandleTransientHttpError().WaitAndRetryAsync(5, _ => TimeSpan.FromSeconds(1)));
services.AddSingleton(_ => RenderHandler.Current) services.AddSingleton(_ => RenderHandler.Current)
.AddSingleton<IPluginsLifetime>(s => s.GetRequiredService<PluginsLifetime>())
.AddSingleton(_ => new ConfigHandler(Directory.CreateDirectory(Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CringeLauncher", "config")))); .AddSingleton(_ => new ConfigHandler(Directory.CreateDirectory(Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CringeLauncher", "config"))));
return GameServicesExtension.GameServices = services.BuildServiceProvider(); return GameServicesExtension.GameServices = services.BuildServiceProvider();

View File

@@ -0,0 +1,8 @@
using CringePlugins.Splash;
namespace CringePlugins.Loader;
internal interface IPluginsLifetime : ILoadingStage
{
void RegisterLifetime();
}

View File

@@ -1,6 +1,5 @@
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.Json;
using CringePlugins.Config; using CringePlugins.Config;
using CringePlugins.Render; using CringePlugins.Render;
using CringePlugins.Resolver; using CringePlugins.Resolver;
@@ -11,13 +10,12 @@ using NuGet;
using NuGet.Deps; using NuGet.Deps;
using NuGet.Frameworks; using NuGet.Frameworks;
using NuGet.Models; using NuGet.Models;
using NuGet.Versioning;
using SharedCringe.Loader; using SharedCringe.Loader;
using VRage.FileSystem; using VRage.FileSystem;
namespace CringePlugins.Loader; namespace CringePlugins.Loader;
public class PluginsLifetime(ConfigHandler configHandler, HttpClient client) : ILoadingStage internal class PluginsLifetime(ConfigHandler configHandler, HttpClient client) : IPluginsLifetime
{ {
public static ImmutableArray<DerivedAssemblyLoadContext> Contexts { get; private set; } = []; public static ImmutableArray<DerivedAssemblyLoadContext> Contexts { get; private set; } = [];
@@ -26,7 +24,6 @@ public class PluginsLifetime(ConfigHandler configHandler, HttpClient client) : I
public string Name => "Loading Plugins"; public string Name => "Loading Plugins";
private ImmutableArray<PluginInstance> _plugins = []; private ImmutableArray<PluginInstance> _plugins = [];
// TODO move this as api for other plugins
private readonly DirectoryInfo _dir = Directory.CreateDirectory(Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CringeLauncher")); private readonly DirectoryInfo _dir = Directory.CreateDirectory(Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CringeLauncher"));
private readonly NuGetRuntimeFramework _runtimeFramework = new(NuGetFramework.ParseFolder("net9.0-windows10.0.19041.0"), RuntimeInformation.RuntimeIdentifier); private readonly NuGetRuntimeFramework _runtimeFramework = new(NuGetFramework.ParseFolder("net9.0-windows10.0.19041.0"), RuntimeInformation.RuntimeIdentifier);