From 12adc4099f7bb37e05365425871e305a64ef7291 Mon Sep 17 00:00:00 2001 From: goat <16760685+goaaats@users.noreply.github.com> Date: Thu, 15 Jul 2021 03:42:10 +0200 Subject: [PATCH] feat: move plugin update into separate function, add update button to applicable available plugin --- .../Internal/Windows/PluginInstallerWindow.cs | 21 ++- Dalamud/Plugin/Internal/PluginManager.cs | 137 +++++++++--------- 2 files changed, 92 insertions(+), 66 deletions(-) diff --git a/Dalamud/Interface/Internal/Windows/PluginInstallerWindow.cs b/Dalamud/Interface/Internal/Windows/PluginInstallerWindow.cs index 6780ade6b..dc9d80535 100644 --- a/Dalamud/Interface/Internal/Windows/PluginInstallerWindow.cs +++ b/Dalamud/Interface/Internal/Windows/PluginInstallerWindow.cs @@ -591,8 +591,9 @@ namespace Dalamud.Interface.Internal.Windows label += Locs.PluginTitleMod_UnloadError; } + var availablePluginUpdate = this.pluginListUpdatable.FirstOrDefault(up => up.InstalledPlugin == plugin); // Update available - if (this.pluginListUpdatable.FirstOrDefault(up => up.InstalledPlugin == plugin) != default) + if (availablePluginUpdate != default) { label += Locs.PluginTitleMod_HasUpdate; } @@ -676,6 +677,9 @@ namespace Dalamud.Interface.Internal.Windows this.DrawDevPluginButtons(plugin); this.DrawVisitRepoUrlButton(plugin.Manifest.RepoUrl); + if (availablePluginUpdate != default) + this.DrawUpdateSinglePluginButton(plugin, availablePluginUpdate); + ImGui.SameLine(); ImGui.TextColored(ImGuiColors.DalamudGrey3, $" v{plugin.Manifest.AssemblyVersion}"); @@ -776,6 +780,19 @@ namespace Dalamud.Interface.Internal.Windows } } + private void DrawUpdateSinglePluginButton(LocalPlugin plugin, AvailablePluginUpdate update) + { + ImGui.SameLine(); + + if (ImGuiComponents.IconButton(FontAwesomeIcon.Download)) + { + this.dalamud.PluginManager.UpdateSinglePlugin(update, false); + } + + if (ImGui.IsItemHovered()) + ImGui.SetTooltip(Locs.PluginButtonToolTip_UpdateSingle(update.UpdateManifest.AssemblyVersion.ToString())); + } + private void DrawOpenPluginSettingsButton(LocalPlugin plugin) { if (plugin.DalamudInterface?.UiBuilder?.HasConfigUi ?? false) @@ -1130,6 +1147,8 @@ namespace Dalamud.Interface.Internal.Windows public static string PluginButtonToolTip_VisitPluginUrl => Loc.Localize("InstallerVisitPluginUrl", "Visit plugin URL"); + public static string PluginButtonToolTip_UpdateSingle(string version) => Loc.Localize("InstallerUpdateSingle", "Update to {0}").Format(version); + #endregion #region Footer diff --git a/Dalamud/Plugin/Internal/PluginManager.cs b/Dalamud/Plugin/Internal/PluginManager.cs index 7a32e9eab..64dca30b8 100644 --- a/Dalamud/Plugin/Internal/PluginManager.cs +++ b/Dalamud/Plugin/Internal/PluginManager.cs @@ -17,6 +17,7 @@ using Dalamud.Game.Text; using Dalamud.Plugin.Internal.Exceptions; using Dalamud.Plugin.Internal.Types; using HarmonyLib; +using JetBrains.Annotations; using Newtonsoft.Json; namespace Dalamud.Plugin.Internal @@ -599,84 +600,90 @@ namespace Dalamud.Plugin.Internal { Log.Information("Starting plugin update"); - var listChanged = false; - var updatedList = new List(); // Prevent collection was modified errors for (var i = 0; i < this.updatablePlugins.Count; i++) { - var metadata = this.updatablePlugins[i]; - - var plugin = metadata.InstalledPlugin; - - // Can't update that! - if (plugin is LocalDevPlugin) - continue; - - var updateStatus = new PluginUpdateStatus() - { - InternalName = plugin.Manifest.InternalName, - Name = plugin.Manifest.Name, - Version = metadata.UseTesting - ? metadata.UpdateManifest.TestingAssemblyVersion - : metadata.UpdateManifest.AssemblyVersion, - }; - - if (dryRun) - { - updateStatus.WasUpdated = true; - updatedList.Add(updateStatus); - } - else - { - // Unload if loaded - if (plugin.State == PluginState.Loaded || plugin.State == PluginState.LoadError) - { - try - { - plugin.Unload(); - } - catch (Exception ex) - { - Log.Error(ex, "Error during unload (update)"); - continue; - } - } - - try - { - plugin.Disable(); - this.installedPlugins.Remove(plugin); - listChanged = true; - } - catch (Exception ex) - { - Log.Error(ex, "Error during disable (update)"); - continue; - } - - try - { - this.InstallPlugin(metadata.UpdateManifest, metadata.UseTesting, PluginLoadReason.Update); - listChanged = true; - } - catch (Exception ex) - { - Log.Error(ex, "Error during install (update)"); - continue; - } - } + updatedList.Add(this.UpdateSinglePlugin(this.updatablePlugins[i], dryRun)); } - if (listChanged) - this.NotifyInstalledPluginsChanged(); + this.NotifyInstalledPluginsChanged(); Log.Information("Plugin update OK."); return updatedList; } + /// + /// Update a single plugin, provided a valid . + /// + /// The available plugin update. + /// Whether or not to actually perform the update, or just indicate success. + /// The status of the update. + [CanBeNull] + public PluginUpdateStatus UpdateSinglePlugin(AvailablePluginUpdate metadata, bool dryRun) + { + var plugin = metadata.InstalledPlugin; + + // Can't update that! + if (plugin is LocalDevPlugin) + return null; + + var updateStatus = new PluginUpdateStatus + { + InternalName = plugin.Manifest.InternalName, + Name = plugin.Manifest.Name, + Version = metadata.UseTesting ? metadata.UpdateManifest.TestingAssemblyVersion : metadata.UpdateManifest.AssemblyVersion + }; + + if (dryRun) + { + updateStatus.WasUpdated = true; + } + else + { + updateStatus.WasUpdated = true; + + // Unload if loaded + if (plugin.State == PluginState.Loaded || plugin.State == PluginState.LoadError) + { + try + { + plugin.Unload(); + } + catch (Exception ex) + { + Log.Error(ex, "Error during unload (update)"); + updateStatus.WasUpdated = false; + } + } + + try + { + plugin.Disable(); + this.installedPlugins.Remove(plugin); + } + catch (Exception ex) + { + Log.Error(ex, "Error during disable (update)"); + updateStatus.WasUpdated = false; + } + + try + { + this.InstallPlugin(metadata.UpdateManifest, metadata.UseTesting, PluginLoadReason.Update); + } + catch (Exception ex) + { + Log.Error(ex, "Error during install (update)"); + updateStatus.WasUpdated = false; + } + } + + return updateStatus; + } + /// /// Print to chat any plugin updates and whether they were successful. ///