mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-10 18:14:36 +01:00
Add ClientStatePluginScoped (#1384)
* Add ClientStatePluginScoped * Restore InvokeSafely * Add InvokeSafely for basic Action types. * Set delegates to null to prevent leaking memory * Resolve Merge
This commit is contained in:
parent
201a927952
commit
b742abe77f
8 changed files with 165 additions and 37 deletions
|
|
@ -1,12 +1,9 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
using Dalamud.Game;
|
||||
using Dalamud.Plugin.Services;
|
||||
using Serilog;
|
||||
|
||||
using static Dalamud.Game.Framework;
|
||||
|
||||
namespace Dalamud.Utility;
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -21,7 +18,7 @@ internal static class EventHandlerExtensions
|
|||
/// <param name="eh">The EventHandler in question.</param>
|
||||
/// <param name="sender">Default sender for Invoke equivalent.</param>
|
||||
/// <param name="e">Default EventArgs for Invoke equivalent.</param>
|
||||
public static void InvokeSafely(this EventHandler eh, object sender, EventArgs e)
|
||||
public static void InvokeSafely(this EventHandler? eh, object sender, EventArgs e)
|
||||
{
|
||||
if (eh == null)
|
||||
return;
|
||||
|
|
@ -40,7 +37,7 @@ internal static class EventHandlerExtensions
|
|||
/// <param name="sender">Default sender for Invoke equivalent.</param>
|
||||
/// <param name="e">Default EventArgs for Invoke equivalent.</param>
|
||||
/// <typeparam name="T">Type of EventArgs.</typeparam>
|
||||
public static void InvokeSafely<T>(this EventHandler<T> eh, object sender, T e)
|
||||
public static void InvokeSafely<T>(this EventHandler<T>? eh, object sender, T e)
|
||||
{
|
||||
if (eh == null)
|
||||
return;
|
||||
|
|
@ -56,7 +53,7 @@ internal static class EventHandlerExtensions
|
|||
/// of a thrown Exception inside of an invocation.
|
||||
/// </summary>
|
||||
/// <param name="act">The Action in question.</param>
|
||||
public static void InvokeSafely(this Action act)
|
||||
public static void InvokeSafely(this Action? act)
|
||||
{
|
||||
if (act == null)
|
||||
return;
|
||||
|
|
@ -67,13 +64,31 @@ internal static class EventHandlerExtensions
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replacement for Invoke() on event Actions to catch exceptions that stop event propagation in case
|
||||
/// of a thrown Exception inside of an invocation.
|
||||
/// </summary>
|
||||
/// <param name="act">The Action in question.</param>
|
||||
/// <param name="argument">Templated argument for Action.</param>
|
||||
/// <typeparam name="T">Type of Action args.</typeparam>
|
||||
public static void InvokeSafely<T>(this Action<T>? act, T argument)
|
||||
{
|
||||
if (act == null)
|
||||
return;
|
||||
|
||||
foreach (var action in act.GetInvocationList().Cast<Action<T>>())
|
||||
{
|
||||
HandleInvoke(action, argument);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replacement for Invoke() on OnUpdateDelegate to catch exceptions that stop event propagation in case
|
||||
/// of a thrown Exception inside of an invocation.
|
||||
/// </summary>
|
||||
/// <param name="updateDelegate">The OnUpdateDelegate in question.</param>
|
||||
/// <param name="framework">Framework to be passed on to OnUpdateDelegate.</param>
|
||||
public static void InvokeSafely(this IFramework.OnUpdateDelegate updateDelegate, Framework framework)
|
||||
public static void InvokeSafely(this IFramework.OnUpdateDelegate? updateDelegate, Framework framework)
|
||||
{
|
||||
if (updateDelegate == null)
|
||||
return;
|
||||
|
|
@ -95,4 +110,16 @@ internal static class EventHandlerExtensions
|
|||
Log.Error(ex, "Exception during raise of {handler}", act.Method);
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleInvoke<T>(Action<T> act, T argument)
|
||||
{
|
||||
try
|
||||
{
|
||||
act(argument);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Exception during raise of {handler}", act.Method);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue