From 62bd3af45935b56757a739750f10a40ca7119ad9 Mon Sep 17 00:00:00 2001 From: goat <16760685+goaaats@users.noreply.github.com> Date: Tue, 21 Jul 2020 16:07:35 +0200 Subject: [PATCH] feat: delete old versions of plugins before load --- Dalamud/Dalamud.cs | 54 ++++++++++++++++-------------- Dalamud/Plugin/PluginRepository.cs | 31 +++++++++++++++++ 2 files changed, 59 insertions(+), 26 deletions(-) diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs index c4cf16a72..7a33caf87 100644 --- a/Dalamud/Dalamud.cs +++ b/Dalamud/Dalamud.cs @@ -90,30 +90,27 @@ namespace Dalamud { this.WinSock2 = new WinSockHandlers(); AssetManager.EnsureAssets(this.baseDirectory).ContinueWith(async task => { - if (task.IsCanceled || task.IsFaulted) { - throw new Exception("Could not ensure assets.", task.Exception); - } + if (task.IsCanceled || task.IsFaulted) throw new Exception("Could not ensure assets.", task.Exception); this.LocalizationManager = new Localization(this.StartInfo.WorkingDirectory); - if (!string.IsNullOrEmpty(this.Configuration.LanguageOverride)) { + if (!string.IsNullOrEmpty(this.Configuration.LanguageOverride)) this.LocalizationManager.SetupWithLangCode(this.Configuration.LanguageOverride); - } else { + else this.LocalizationManager.SetupWithUiCulture(); - } var pluginDir = this.StartInfo.PluginDirectory; if (this.Configuration.DoPluginTest) pluginDir = Path.Combine(pluginDir, "..", "testPlugins"); - this.PluginRepository = new PluginRepository(this, pluginDir, this.StartInfo.GameVersion); + PluginRepository = new PluginRepository(this, pluginDir, this.StartInfo.GameVersion); - if (Environment.GetEnvironmentVariable("DALAMUD_NOT_HAVE_INTERFACE") != "True") { + if (!bool.Parse(Environment.GetEnvironmentVariable("DALAMUD_NOT_HAVE_INTERFACE") ?? "false")) { try { - this.InterfaceManager = new InterfaceManager(this, this.SigScanner); - this.InterfaceManager.OnDraw += BuildDalamudUi; + InterfaceManager = new InterfaceManager(this, this.SigScanner); + InterfaceManager.OnDraw += BuildDalamudUi; - this.InterfaceManager.Enable(); + InterfaceManager.Enable(); } catch (Exception e) { @@ -121,29 +118,34 @@ namespace Dalamud { } } - this.Data = new DataManager(this.StartInfo.Language); - await this.Data.Initialize(this.baseDirectory); + Data = new DataManager(this.StartInfo.Language); + await Data.Initialize(this.baseDirectory); - SeStringManager = new SeStringManager(Data); + SeStringManager = new SeStringManager(Data); - this.NetworkHandlers = new NetworkHandlers(this, this.Configuration.OptOutMbCollection); + NetworkHandlers = new NetworkHandlers(this, this.Configuration.OptOutMbCollection); // Initialize managers. Basically handlers for the logic - this.CommandManager = new CommandManager(this, info.Language); + CommandManager = new CommandManager(this, info.Language); SetupCommands(); - this.ChatHandlers = new ChatHandlers(this); + ChatHandlers = new ChatHandlers(this); // Discord Bot Manager - this.BotManager = new DiscordBotManager(this, this.Configuration.DiscordFeatureConfig); - this.BotManager.Start(); + BotManager = new DiscordBotManager(this, this.Configuration.DiscordFeatureConfig); + BotManager.Start(); - try { - this.PluginManager = new PluginManager(this, pluginDir, this.StartInfo.DefaultPluginDirectory); - this.PluginManager.LoadPlugins(); - } - catch (Exception ex) - { - Log.Error(ex, "Plugin load failed."); + if (!bool.Parse(Environment.GetEnvironmentVariable("DALAMUD_NOT_HAVE_PLUGINS") ?? "false")) { + try + { + PluginRepository.CleanupPlugins(); + + PluginManager = new PluginManager(this, pluginDir, this.StartInfo.DefaultPluginDirectory); + PluginManager.LoadPlugins(); + } + catch (Exception ex) + { + Log.Error(ex, "Plugin load failed."); + } } this.Framework.Enable(); diff --git a/Dalamud/Plugin/PluginRepository.cs b/Dalamud/Plugin/PluginRepository.cs index d7903c811..12b2530c1 100644 --- a/Dalamud/Plugin/PluginRepository.cs +++ b/Dalamud/Plugin/PluginRepository.cs @@ -241,5 +241,36 @@ namespace Dalamud.Plugin return (!hasError, updatedList); } + + public void CleanupPlugins() { + try + { + var pluginsDirectory = new DirectoryInfo(this.pluginDirectory); + foreach (var installed in pluginsDirectory.GetDirectories()) + { + var versions = installed.GetDirectories(); + + if (versions.Length == 0) + { + Log.Information("[PLUGINR] Has no versions: {0}", installed.FullName); + continue; + } + + var sortedVersions = versions.OrderBy(x => int.Parse(x.Name.Replace(".", ""))).ToArray(); + for (var i = 0; i < sortedVersions.Length - 1; i++) { + Log.Information("[PLUGINR] Trying to delete old {0} at ", installed.Name, sortedVersions[i].FullName); + try { + sortedVersions[i].Delete(true); + } catch (Exception ex) { + Log.Error(ex, "[PLUGINR] Could not delete old version"); + } + } + } + } + catch (Exception ex) + { + Log.Error(ex, "[PLUGINR] Plugin cleanup failed."); + } + } } }