Merge pull request #1966 from Soreepeong/fix/servicescope

ServiceScope fixes
This commit is contained in:
goat 2024-07-25 21:30:14 +02:00 committed by GitHub
commit 8c593bc31d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 165 additions and 166 deletions

View file

@ -5,6 +5,7 @@ using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Dalamud.Configuration;
using Dalamud.Configuration.Internal;
@ -26,6 +27,8 @@ using Dalamud.Plugin.Ipc;
using Dalamud.Plugin.Ipc.Exceptions;
using Dalamud.Plugin.Ipc.Internal;
using Serilog;
namespace Dalamud.Plugin;
/// <summary>
@ -458,34 +461,52 @@ internal sealed class DalamudPluginInterface : IDalamudPluginInterface, IDisposa
#region Dependency Injection
/// <summary>
/// Create a new object of the provided type using its default constructor, then inject objects and properties.
/// </summary>
/// <param name="scopedObjects">Objects to inject additionally.</param>
/// <typeparam name="T">The type to create.</typeparam>
/// <returns>The created and initialized type.</returns>
/// <inheritdoc/>
public T? Create<T>(params object[] scopedObjects) where T : class
{
var svcContainer = Service<IoC.Internal.ServiceContainer>.Get();
var t = this.CreateAsync<T>(scopedObjects);
t.Wait();
return (T)this.plugin.ServiceScope!.CreateAsync(
typeof(T),
this.GetPublicIocScopes(scopedObjects)).GetAwaiter().GetResult();
if (t.Exception is { } e)
{
Log.Error(
e,
"{who}: Exception during {where}: {what}",
this.plugin.Name,
nameof(this.Create),
typeof(T).FullName ?? typeof(T).Name);
}
return t.IsCompletedSuccessfully ? t.Result : null;
}
/// <summary>
/// Inject services into properties on the provided object instance.
/// </summary>
/// <param name="instance">The instance to inject services into.</param>
/// <param name="scopedObjects">Objects to inject additionally.</param>
/// <returns>Whether or not the injection succeeded.</returns>
/// <inheritdoc/>
public async Task<T> CreateAsync<T>(params object[] scopedObjects) where T : class =>
(T)await this.plugin.ServiceScope!.CreateAsync(typeof(T), this.GetPublicIocScopes(scopedObjects));
/// <inheritdoc/>
public bool Inject(object instance, params object[] scopedObjects)
{
return this.plugin.ServiceScope!.InjectPropertiesAsync(
instance,
this.GetPublicIocScopes(scopedObjects)).GetAwaiter().GetResult();
var t = this.InjectAsync(instance, scopedObjects);
t.Wait();
if (t.Exception is { } e)
{
Log.Error(
e,
"{who}: Exception during {where}: {what}",
this.plugin.Name,
nameof(this.Inject),
instance.GetType().FullName ?? instance.GetType().Name);
}
return t.IsCompletedSuccessfully;
}
/// <inheritdoc/>
public Task InjectAsync(object instance, params object[] scopedObjects) =>
this.plugin.ServiceScope!.InjectPropertiesAsync(instance, this.GetPublicIocScopes(scopedObjects));
#endregion
/// <summary>Unregister the plugin and dispose all references.</summary>

View file

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading.Tasks;
using Dalamud.Configuration;
using Dalamud.Game.Text;
@ -304,14 +305,30 @@ public interface IDalamudPluginInterface
/// </summary>
/// <param name="scopedObjects">Objects to inject additionally.</param>
/// <typeparam name="T">The type to create.</typeparam>
/// <returns>The created and initialized type.</returns>
/// <returns>The created and initialized type, or <c>null</c> on failure.</returns>
T? Create<T>(params object[] scopedObjects) where T : class;
/// <summary>
/// Create a new object of the provided type using its default constructor, then inject objects and properties.
/// </summary>
/// <param name="scopedObjects">Objects to inject additionally.</param>
/// <typeparam name="T">The type to create.</typeparam>
/// <returns>A task representing the created and initialized type.</returns>
Task<T> CreateAsync<T>(params object[] scopedObjects) where T : class;
/// <summary>
/// Inject services into properties on the provided object instance.
/// </summary>
/// <param name="instance">The instance to inject services into.</param>
/// <param name="scopedObjects">Objects to inject additionally.</param>
/// <returns>Whether or not the injection succeeded.</returns>
/// <returns>Whether the injection succeeded.</returns>
bool Inject(object instance, params object[] scopedObjects);
/// <summary>
/// Inject services into properties on the provided object instance.
/// </summary>
/// <param name="instance">The instance to inject services into.</param>
/// <param name="scopedObjects">Objects to inject additionally.</param>
/// <returns>A <see cref="ValueTask"/> representing the status of the operation.</returns>
Task InjectAsync(object instance, params object[] scopedObjects);
}

View file

@ -417,24 +417,16 @@ internal class LocalPlugin : IDisposable
try
{
if (this.manifest.LoadSync && this.manifest.LoadRequiredState is 0 or 1)
{
var newInstance = await framework.RunOnFrameworkThread(
() => this.ServiceScope.CreateAsync(
this.pluginType!,
this.DalamudInterface!)).ConfigureAwait(false);
this.instance = newInstance as IDalamudPlugin;
}
else
{
this.instance =
await this.ServiceScope.CreateAsync(this.pluginType!, this.DalamudInterface!) as IDalamudPlugin;
}
var forceFrameworkThread = this.manifest.LoadSync && this.manifest.LoadRequiredState is 0 or 1;
var newInstanceTask = forceFrameworkThread ? framework.RunOnFrameworkThread(Create) : Create();
this.instance = await newInstanceTask.ConfigureAwait(false);
async Task<IDalamudPlugin> Create() =>
(IDalamudPlugin)await this.ServiceScope!.CreateAsync(this.pluginType!, this.DalamudInterface!);
}
catch (Exception ex)
{
Log.Error(ex, "Exception in plugin constructor");
Log.Error(ex, "Exception during plugin initialization");
this.instance = null;
}