From 9759ac6d115f03c77f6a6bf06892c87ae32a2c2c Mon Sep 17 00:00:00 2001 From: goat <16760685+goaaats@users.noreply.github.com> Date: Fri, 16 Jul 2021 22:57:41 +0200 Subject: [PATCH] feat(PluginManager): add DeleteConfiguration() and UI --- Dalamud/Configuration/PluginConfigurations.cs | 16 +++++++++++ .../Internal/Windows/PluginInstallerWindow.cs | 23 +++++++++++++++ Dalamud/Plugin/Internal/PluginManager.cs | 28 ++++++++++++++++++- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/Dalamud/Configuration/PluginConfigurations.cs b/Dalamud/Configuration/PluginConfigurations.cs index 1cadb2180..26577b8be 100644 --- a/Dalamud/Configuration/PluginConfigurations.cs +++ b/Dalamud/Configuration/PluginConfigurations.cs @@ -60,6 +60,22 @@ namespace Dalamud.Configuration }); } + /// + /// Delete the configuration file and folder for the specified plugin. + /// This will throw an if the plugin did not correctly close its handles. + /// + /// The name of the plugin. + public void Delete(string pluginName) + { + var directory = this.GetDirectoryPath(pluginName); + if (directory.Exists) + directory.Delete(true); + + var file = this.GetConfigFile(pluginName); + if (file.Exists) + file.Delete(); + } + /// /// Get plugin directory. /// diff --git a/Dalamud/Interface/Internal/Windows/PluginInstallerWindow.cs b/Dalamud/Interface/Internal/Windows/PluginInstallerWindow.cs index e7f2f68fa..1b041ef35 100644 --- a/Dalamud/Interface/Internal/Windows/PluginInstallerWindow.cs +++ b/Dalamud/Interface/Internal/Windows/PluginInstallerWindow.cs @@ -692,6 +692,25 @@ namespace Dalamud.Interface.Internal.Windows ImGui.Unindent(); } + + if (ImGui.BeginPopupContextItem("InstalledItemContextMenu")) + { + if (ImGui.Selectable(Locs.PluginContext_DeletePluginConfig)) + { + Log.Debug($"Deleting config for {plugin.Manifest.InternalName}"); + + Task.Run(() => this.dalamud.PluginManager.DeleteConfiguration(plugin)) + .ContinueWith(task => + { + // There is no need to set as Complete for an individual plugin installation + this.installStatus = OperationStatus.Idle; + + this.DisplayErrorContinuation(task, Locs.ErrorModal_DeleteConfigFail(plugin.Name)); + }); + } + + ImGui.EndPopup(); + } } private void DrawPluginControlButton(LocalPlugin plugin) @@ -1125,6 +1144,8 @@ namespace Dalamud.Interface.Internal.Windows public static string PluginContext_HidePlugin => Loc.Localize("InstallerHidePlugin", "Hide from installer"); + public static string PluginContext_DeletePluginConfig => Loc.Localize("InstallerDeletePluginConfig", "Delete configuration data & reload"); + #endregion #region Plugin body @@ -1199,6 +1220,8 @@ namespace Dalamud.Interface.Internal.Windows public static string ErrorModal_SingleUpdateFail(string name) => Loc.Localize("InstallerSingleUpdateFail", "Failed to update plugin {0}.").Format(name); + public static string ErrorModal_DeleteConfigFail(string name) => Loc.Localize("InstallerDeleteConfigFail", "Failed to reset the plugin {0}.\n\nThe plugin may not support this action. You can try deleting the configuration manually while the game is shut down - please see the FAQ.").Format(name); + public static string ErrorModal_EnableFail(string name) => Loc.Localize("InstallerEnableFail", "Failed to enable plugin {0}.").Format(name); public static string ErrorModal_DisableFail(string name) => Loc.Localize("InstallerDisableFail", "Failed to disable plugin {0}.").Format(name); diff --git a/Dalamud/Plugin/Internal/PluginManager.cs b/Dalamud/Plugin/Internal/PluginManager.cs index 55078fb85..455074b7f 100644 --- a/Dalamud/Plugin/Internal/PluginManager.cs +++ b/Dalamud/Plugin/Internal/PluginManager.cs @@ -8,7 +8,7 @@ using System.IO.Compression; using System.Linq; using System.Net; using System.Reflection; -using System.Text; +using System.Threading; using System.Threading.Tasks; using CheapLoc; @@ -713,6 +713,32 @@ namespace Dalamud.Plugin.Internal return updateStatus; } + /// + /// Update a single plugin, provided a valid . + /// + /// The available plugin update. + /// Whether to notify that installed plugins have changed afterwards. + /// Whether or not to actually perform the update, or just indicate success. + /// The status of the update. + public void DeleteConfiguration(LocalPlugin plugin) + { + if (plugin.State == PluginState.InProgress) + throw new Exception("Cannot delete configuration for a loading/unloading plugin"); + + if (plugin.IsLoaded) + plugin.Unload(); + + // Let's wait so any handles on files in plugin configurations can be closed + Thread.Sleep(500); + + this.PluginConfigs.Delete(plugin.Name); + + Thread.Sleep(500); + + // Let's indicate "installer" here since this is supposed to be a fresh install + plugin.Load(PluginLoadReason.Installer); + } + /// /// Print to chat any plugin updates and whether they were successful. ///