IOC: scoped/on-demand services (#1120)

This commit is contained in:
goat 2023-05-21 22:43:28 +02:00 committed by GitHub
parent ca8a05b672
commit daa9f72218
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 309 additions and 62 deletions

View file

@ -11,6 +11,7 @@ using Dalamud.Game.Gui.Dtr;
using Dalamud.Interface.GameFonts;
using Dalamud.Interface.Internal;
using Dalamud.IoC.Internal;
using Dalamud.Logging;
using Dalamud.Logging.Internal;
using Dalamud.Plugin.Internal.Exceptions;
using Dalamud.Plugin.Internal.Loader;
@ -180,10 +181,15 @@ internal class LocalPlugin : IDisposable
public AssemblyName? AssemblyName { get; private set; }
/// <summary>
/// Gets the plugin name, directly from the plugin or if it is not loaded from the manifest.
/// Gets the plugin name from the manifest.
/// </summary>
public string Name => this.Manifest.Name;
/// <summary>
/// Gets the plugin internal name from the manifest.
/// </summary>
public string InternalName => this.Manifest.Name;
/// <summary>
/// Gets an optional reason, if the plugin is banned.
/// </summary>
@ -238,6 +244,11 @@ internal class LocalPlugin : IDisposable
/// </summary>
public bool IsDev => this is LocalDevPlugin;
/// <summary>
/// Gets the service scope for this plugin.
/// </summary>
public IServiceScope? ServiceScope { get; private set; }
/// <inheritdoc/>
public void Dispose()
{
@ -259,6 +270,9 @@ internal class LocalPlugin : IDisposable
this.DalamudInterface?.ExplicitDispose();
this.DalamudInterface = null;
this.ServiceScope?.Dispose();
this.ServiceScope = null;
this.pluginType = null;
this.pluginAssembly = null;
@ -410,17 +424,20 @@ internal class LocalPlugin : IDisposable
PluginManager.PluginLocations[this.pluginType.Assembly.FullName] = new PluginPatchData(this.DllFile);
this.DalamudInterface =
new DalamudPluginInterface(this.pluginAssembly.GetName().Name!, this.DllFile, reason, this.IsDev, this.Manifest);
new DalamudPluginInterface(this, reason);
this.ServiceScope = ioc.GetScope();
this.ServiceScope.RegisterPrivateScopes(this); // Add this LocalPlugin as a private scope, so services can get it
if (this.Manifest.LoadSync && this.Manifest.LoadRequiredState is 0 or 1)
{
this.instance = await framework.RunOnFrameworkThread(
() => ioc.CreateAsync(this.pluginType!, this.DalamudInterface!)) as IDalamudPlugin;
() => this.ServiceScope.CreateAsync(this.pluginType!, this.DalamudInterface!)) as IDalamudPlugin;
}
else
{
this.instance =
await ioc.CreateAsync(this.pluginType!, this.DalamudInterface!) as IDalamudPlugin;
await this.ServiceScope.CreateAsync(this.pluginType!, this.DalamudInterface!) as IDalamudPlugin;
}
if (this.instance == null)
@ -466,6 +483,7 @@ internal class LocalPlugin : IDisposable
{
var configuration = Service<DalamudConfiguration>.Get();
var framework = Service<Framework>.GetNullable();
var ioc = await Service<ServiceContainer>.GetAsync();
await this.pluginLoadStateLock.WaitAsync();
try
@ -504,6 +522,9 @@ internal class LocalPlugin : IDisposable
this.DalamudInterface?.ExplicitDispose();
this.DalamudInterface = null;
this.ServiceScope?.Dispose();
this.ServiceScope = null;
this.pluginType = null;
this.pluginAssembly = null;