From a1ae33bfee3f5a8f61c7617d59bdafa4279887ef Mon Sep 17 00:00:00 2001 From: goat Date: Mon, 23 Dec 2024 21:03:31 +0100 Subject: [PATCH] don't set plugin to fail state if we threw InvalidPluginOperationException These are supposed to indicate to the user that they called a function at the wrong point in time. We don't want to actually mutate the state in that case. --- .../Exceptions/InternalPluginStateException.cs | 16 ++++++++++++++++ Dalamud/Plugin/Internal/Types/LocalPlugin.cs | 11 ++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 Dalamud/Plugin/Internal/Exceptions/InternalPluginStateException.cs diff --git a/Dalamud/Plugin/Internal/Exceptions/InternalPluginStateException.cs b/Dalamud/Plugin/Internal/Exceptions/InternalPluginStateException.cs new file mode 100644 index 000000000..03e37afcf --- /dev/null +++ b/Dalamud/Plugin/Internal/Exceptions/InternalPluginStateException.cs @@ -0,0 +1,16 @@ +namespace Dalamud.Plugin.Internal.Exceptions; + +/// +/// An exception to be thrown when policy blocks a plugin from loading. +/// +internal class InternalPluginStateException : InvalidPluginOperationException +{ + /// + /// Initializes a new instance of the class. + /// + /// The message to associate with this exception. + public InternalPluginStateException(string message) + : base(message) + { + } +} diff --git a/Dalamud/Plugin/Internal/Types/LocalPlugin.cs b/Dalamud/Plugin/Internal/Types/LocalPlugin.cs index 59f6b23c1..ed3a94994 100644 --- a/Dalamud/Plugin/Internal/Types/LocalPlugin.cs +++ b/Dalamud/Plugin/Internal/Types/LocalPlugin.cs @@ -281,7 +281,7 @@ internal class LocalPlugin : IAsyncDisposable case PluginState.Unloaded: if (this.instance is not null) { - throw new InvalidPluginOperationException( + throw new InternalPluginStateException( "Plugin should have been unloaded but instance is not cleared"); } @@ -413,7 +413,9 @@ internal class LocalPlugin : IAsyncDisposable } catch (Exception ex) { - this.State = PluginState.LoadError; + // These are "user errors", we don't want to mark the plugin as failed + if (ex is not InvalidPluginOperationException) + this.State = PluginState.LoadError; // If a precondition fails, don't record it as an error, as it isn't really. if (ex is PluginPreconditionFailedException) @@ -476,7 +478,10 @@ internal class LocalPlugin : IAsyncDisposable } catch (Exception ex) { - this.State = PluginState.UnloadError; + // These are "user errors", we don't want to mark the plugin as failed + if (ex is not InvalidPluginOperationException) + this.State = PluginState.UnloadError; + Log.Error(ex, "Error while unloading {PluginName}", this.InternalName); throw;