From 6a01c8cc5b165ed8f53c4b9bc9fbae3281eb9152 Mon Sep 17 00:00:00 2001 From: goat Date: Tue, 10 Oct 2023 19:16:42 +0200 Subject: [PATCH] chore: handle profile errors properly and stop being lazy --- Dalamud/Plugin/Internal/PluginManager.cs | 11 +++- Dalamud/Plugin/Internal/Profiles/Profile.cs | 62 +++++++++++++++++---- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/Dalamud/Plugin/Internal/PluginManager.cs b/Dalamud/Plugin/Internal/PluginManager.cs index 3ead8d5eab..664406157a 100644 --- a/Dalamud/Plugin/Internal/PluginManager.cs +++ b/Dalamud/Plugin/Internal/PluginManager.cs @@ -790,8 +790,15 @@ public async Task InstallPluginAsync(RemotePluginManifest repoManif // or the user removed the plugin manually in which case we don't care if (reason == PluginLoadReason.Installer) { - // We don't need to apply, it doesn't matter - await this.profileManager.DefaultProfile.RemoveAsync(repoManifest.InternalName, false, false); + try + { + // We don't need to apply, it doesn't matter + await this.profileManager.DefaultProfile.RemoveAsync(repoManifest.InternalName, false); + } + catch (ProfileOperationException) + { + // ignored + } } else { diff --git a/Dalamud/Plugin/Internal/Profiles/Profile.cs b/Dalamud/Plugin/Internal/Profiles/Profile.cs index 0b2d62ccfe..592720c14b 100644 --- a/Dalamud/Plugin/Internal/Profiles/Profile.cs +++ b/Dalamud/Plugin/Internal/Profiles/Profile.cs @@ -200,22 +200,17 @@ public async Task AddOrUpdateAsync(string internalName, bool state, bool apply = /// /// The internal name of the plugin. /// Whether or not the current state should immediately be applied. - /// - /// Throw if certain operations are invalid. - /// * Throw if the plugin is not in this profile. - /// * If this is the default profile, check if we are in any other, then throw if we aren't. - /// /// A representing the asynchronous operation. - public async Task RemoveAsync(string internalName, bool apply = true, bool guardrails = true) + public async Task RemoveAsync(string internalName, bool apply = true) { ProfileModelV1.ProfileModelV1Plugin entry; lock (this) { entry = this.modelV1.Plugins.FirstOrDefault(x => x.InternalName == internalName); - if (entry == null && guardrails) - throw new ArgumentException($"No plugin \"{internalName}\" in profile \"{this.Guid}\""); + if (entry == null) + throw new PluginNotFoundException(internalName); - if (!this.modelV1.Plugins.Remove(entry) && guardrails) + if (!this.modelV1.Plugins.Remove(entry)) throw new Exception("Couldn't remove plugin from model collection"); } @@ -226,9 +221,9 @@ public async Task RemoveAsync(string internalName, bool apply = true, bool guard { await this.manager.DefaultProfile.AddOrUpdateAsync(internalName, this.IsEnabled && entry.IsEnabled, false); } - else if (guardrails) + else { - throw new Exception("Removed plugin from default profile, but wasn't in any other profile"); + throw new PluginNotInDefaultProfileException(internalName); } } @@ -241,3 +236,48 @@ public async Task RemoveAsync(string internalName, bool apply = true, bool guard /// public override string ToString() => $"{this.Guid} ({this.Name})"; } + +/// +/// Exception indicating an issue during a profile operation. +/// +internal abstract class ProfileOperationException : Exception +{ + /// + /// Initializes a new instance of the class. + /// + /// Message to pass on. + protected ProfileOperationException(string message) + : base(message) + { + } +} + +/// +/// Exception indicating that a plugin was not found in the default profile. +/// +internal sealed class PluginNotInDefaultProfileException : ProfileOperationException +{ + /// + /// Initializes a new instance of the class. + /// + /// The internal name of the plugin causing the error. + public PluginNotInDefaultProfileException(string internalName) + : base($"The plugin '{internalName}' is not in the default profile, and cannot be removed") + { + } +} + +/// +/// Exception indicating that the plugin was not found. +/// +internal sealed class PluginNotFoundException : ProfileOperationException +{ + /// + /// Initializes a new instance of the class. + /// + /// The internal name of the plugin causing the error. + public PluginNotFoundException(string internalName) + : base($"The plugin '{internalName}' was not found in the profile") + { + } +}