From 191dfb57e35ef2abec1919e4bd29c8f6e459a2d2 Mon Sep 17 00:00:00 2001 From: Haselnussbomber Date: Mon, 4 Aug 2025 03:28:44 +0200 Subject: [PATCH] [API13] Fire ActivePluginsChanged after a plugin loaded/unloaded (#2334) * Fire ActivePluginsChanged after a plugin loaded/unloaded * Add ActivePluginsChangedEventArgs * Use past tense --- .../Plugin/ActivePluginsChangedEventArgs.cs | 31 +++++++++++++++++++ Dalamud/Plugin/DalamudPluginInterface.cs | 7 ++--- Dalamud/Plugin/IDalamudPluginInterface.cs | 5 ++- Dalamud/Plugin/Internal/PluginManager.cs | 31 ++++++++++--------- Dalamud/Plugin/Internal/Types/LocalPlugin.cs | 6 ++++ Dalamud/Plugin/PluginListInvalidationKind.cs | 12 ++++++- 6 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 Dalamud/Plugin/ActivePluginsChangedEventArgs.cs diff --git a/Dalamud/Plugin/ActivePluginsChangedEventArgs.cs b/Dalamud/Plugin/ActivePluginsChangedEventArgs.cs new file mode 100644 index 000000000..0c857be79 --- /dev/null +++ b/Dalamud/Plugin/ActivePluginsChangedEventArgs.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; + +namespace Dalamud.Plugin; + +/// +/// Contains data about changes to the list of active plugins. +/// +public class ActivePluginsChangedEventArgs : EventArgs +{ + /// + /// Initializes a new instance of the class + /// with the specified parameters. + /// + /// The kind of change that triggered the event. + /// The internal names of the plugins affected by the change. + internal ActivePluginsChangedEventArgs(PluginListInvalidationKind kind, IEnumerable affectedInternalNames) + { + this.Kind = kind; + this.AffectedInternalNames = affectedInternalNames; + } + + /// + /// Gets the invalidation kind that caused this event to be fired. + /// + public PluginListInvalidationKind Kind { get; } + + /// + /// Gets the InternalNames of affected plugins. + /// + public IEnumerable AffectedInternalNames { get; } +} diff --git a/Dalamud/Plugin/DalamudPluginInterface.cs b/Dalamud/Plugin/DalamudPluginInterface.cs index b857fe438..25c998a42 100644 --- a/Dalamud/Plugin/DalamudPluginInterface.cs +++ b/Dalamud/Plugin/DalamudPluginInterface.cs @@ -488,15 +488,14 @@ internal sealed class DalamudPluginInterface : IDalamudPluginInterface, IDisposa /// /// Dispatch the active plugins changed event. /// - /// What action caused this event to be fired. - /// If this plugin was affected by the change. - internal void NotifyActivePluginsChanged(PluginListInvalidationKind kind, bool affectedThisPlugin) + /// The event arguments containing information about the change. + internal void NotifyActivePluginsChanged(ActivePluginsChangedEventArgs args) { foreach (var action in Delegate.EnumerateInvocationList(this.ActivePluginsChanged)) { try { - action(kind, affectedThisPlugin); + action(args); } catch (Exception ex) { diff --git a/Dalamud/Plugin/IDalamudPluginInterface.cs b/Dalamud/Plugin/IDalamudPluginInterface.cs index 4f1192df2..1a1a47403 100644 --- a/Dalamud/Plugin/IDalamudPluginInterface.cs +++ b/Dalamud/Plugin/IDalamudPluginInterface.cs @@ -32,9 +32,8 @@ public interface IDalamudPluginInterface /// /// Delegate for events that listen to changes to the list of active plugins. /// - /// What action caused this event to be fired. - /// If this plugin was affected by the change. - public delegate void ActivePluginsChangedDelegate(PluginListInvalidationKind kind, bool affectedThisPlugin); + /// The event arguments containing information about the change. + public delegate void ActivePluginsChangedDelegate(ActivePluginsChangedEventArgs args); /// /// Event that gets fired when loc is changed diff --git a/Dalamud/Plugin/Internal/PluginManager.cs b/Dalamud/Plugin/Internal/PluginManager.cs index 1d1908144..a4aa3919b 100644 --- a/Dalamud/Plugin/Internal/PluginManager.cs +++ b/Dalamud/Plugin/Internal/PluginManager.cs @@ -1292,6 +1292,23 @@ internal class PluginManager : IInternalDisposableService /// The calling plugin, or null. public LocalPlugin? FindCallingPlugin() => this.FindCallingPlugin(new StackTrace()); + /// + /// Notifies all plugins that the active plugins list changed. + /// + /// The invalidation kind. + /// The affected plugins. + public void NotifyPluginsForStateChange(PluginListInvalidationKind kind, IEnumerable affectedInternalNames) + { + foreach (var installedPlugin in this.installedPluginsList) + { + if (!installedPlugin.IsLoaded || installedPlugin.DalamudInterface == null) + continue; + + installedPlugin.DalamudInterface.NotifyActivePluginsChanged( + new ActivePluginsChangedEventArgs(kind, affectedInternalNames)); + } + } + /// /// Resolves the services that a plugin may have a dependency on.
/// This is required, as the lifetime of a plugin cannot be longer than PluginManager, @@ -1821,20 +1838,6 @@ internal class PluginManager : IInternalDisposableService this.OnInstalledPluginsChanged?.InvokeSafely(); } - private void NotifyPluginsForStateChange(PluginListInvalidationKind kind, IEnumerable affectedInternalNames) - { - foreach (var installedPlugin in this.installedPluginsList) - { - if (!installedPlugin.IsLoaded || installedPlugin.DalamudInterface == null) - continue; - - installedPlugin.DalamudInterface.NotifyActivePluginsChanged( - kind, - // ReSharper disable once PossibleMultipleEnumeration - affectedInternalNames.Contains(installedPlugin.Manifest.InternalName)); - } - } - private void LoadAndStartLoadSyncPlugins() { try diff --git a/Dalamud/Plugin/Internal/Types/LocalPlugin.cs b/Dalamud/Plugin/Internal/Types/LocalPlugin.cs index 4b2b62669..70b1db872 100644 --- a/Dalamud/Plugin/Internal/Types/LocalPlugin.cs +++ b/Dalamud/Plugin/Internal/Types/LocalPlugin.cs @@ -395,6 +395,9 @@ internal class LocalPlugin : IAsyncDisposable this.dalamudInterface); this.State = PluginState.Loaded; Log.Information("Finished loading {PluginName}", this.InternalName); + + var manager = Service.Get(); + manager.NotifyPluginsForStateChange(PluginListInvalidationKind.Loaded, [this.manifest.InternalName]); } catch (Exception ex) { @@ -470,6 +473,9 @@ internal class LocalPlugin : IAsyncDisposable this.State = PluginState.Unloaded; Log.Information("Finished unloading {PluginName}", this.InternalName); + + var manager = Service.Get(); + manager.NotifyPluginsForStateChange(PluginListInvalidationKind.Unloaded, [this.manifest.InternalName]); } catch (Exception ex) { diff --git a/Dalamud/Plugin/PluginListInvalidationKind.cs b/Dalamud/Plugin/PluginListInvalidationKind.cs index 4e7782703..588ae60d7 100644 --- a/Dalamud/Plugin/PluginListInvalidationKind.cs +++ b/Dalamud/Plugin/PluginListInvalidationKind.cs @@ -1,10 +1,20 @@ -namespace Dalamud.Plugin; +namespace Dalamud.Plugin; /// /// Causes for a change to the plugin list. /// public enum PluginListInvalidationKind { + /// + /// A plugin was loaded. + /// + Loaded, + + /// + /// A plugin was unloaded. + /// + Unloaded, + /// /// An installer-initiated update reloaded plugins. ///