From 979a5463ca14d60f558c61f06872a067c126fba9 Mon Sep 17 00:00:00 2001 From: goat Date: Tue, 19 Sep 2023 23:14:50 +0200 Subject: [PATCH] fix: dev plugins always need to retain their WorkingPluginId, even throughout reloads --- .../Internal/DevPluginSettings.cs | 7 ++++++ .../Plugin/Internal/Types/LocalDevPlugin.cs | 22 ++++++++++++++++++ Dalamud/Plugin/Internal/Types/LocalPlugin.cs | 23 ++++++++++++++----- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/Dalamud/Configuration/Internal/DevPluginSettings.cs b/Dalamud/Configuration/Internal/DevPluginSettings.cs index 939b03eca..cfe8ba411 100644 --- a/Dalamud/Configuration/Internal/DevPluginSettings.cs +++ b/Dalamud/Configuration/Internal/DevPluginSettings.cs @@ -1,3 +1,5 @@ +using System; + namespace Dalamud.Configuration.Internal; /// @@ -14,4 +16,9 @@ internal sealed class DevPluginSettings /// Gets or sets a value indicating whether this plugin should automatically reload on file change. /// public bool AutomaticReloading { get; set; } = false; + + /// + /// Gets or sets an ID uniquely identifying this specific instance of a devPlugin. + /// + public Guid WorkingPluginId { get; set; } = Guid.Empty; } diff --git a/Dalamud/Plugin/Internal/Types/LocalDevPlugin.cs b/Dalamud/Plugin/Internal/Types/LocalDevPlugin.cs index 98784ce64..580d5c161 100644 --- a/Dalamud/Plugin/Internal/Types/LocalDevPlugin.cs +++ b/Dalamud/Plugin/Internal/Types/LocalDevPlugin.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.IO; using System.Threading; using System.Threading.Tasks; @@ -40,6 +41,22 @@ internal class LocalDevPlugin : LocalPlugin, IDisposable configuration.DevPluginSettings[dllFile.FullName] = this.devSettings = new DevPluginSettings(); configuration.QueueSave(); } + + // Legacy dev plugins might not have this! + if (this.devSettings.WorkingPluginId == Guid.Empty) + { + this.devSettings.WorkingPluginId = Guid.NewGuid(); + Log.Verbose("{InternalName} was assigned new devPlugin GUID {Guid}", this.InternalName, this.devSettings.WorkingPluginId); + configuration.QueueSave(); + } + + // If the ID in the manifest is wrong, force the good one + if (this.DevImposedWorkingPluginId != this.manifest.WorkingPluginId) + { + Debug.Assert(this.DevImposedWorkingPluginId != Guid.Empty, "Empty guid for devPlugin"); + this.manifest.WorkingPluginId = this.DevImposedWorkingPluginId; + this.SaveManifest("dev imposed working plugin id"); + } if (this.AutomaticReload) { @@ -76,6 +93,11 @@ internal class LocalDevPlugin : LocalPlugin, IDisposable } } } + + /// + /// Gets an ID uniquely identifying this specific instance of a devPlugin. + /// + public Guid DevImposedWorkingPluginId => this.devSettings.WorkingPluginId; /// public new void Dispose() diff --git a/Dalamud/Plugin/Internal/Types/LocalPlugin.cs b/Dalamud/Plugin/Internal/Types/LocalPlugin.cs index 115ab0f8d..f7306b5a7 100644 --- a/Dalamud/Plugin/Internal/Types/LocalPlugin.cs +++ b/Dalamud/Plugin/Internal/Types/LocalPlugin.cs @@ -26,6 +26,13 @@ namespace Dalamud.Plugin.Internal.Types; /// internal class LocalPlugin : IDisposable { + /// + /// The underlying manifest for this plugin. + /// +#pragma warning disable SA1401 + protected LocalPluginManifest manifest; +#pragma warning restore SA1401 + private static readonly ModuleLog Log = new("LOCALPLUGIN"); private readonly FileInfo manifestFile; @@ -39,8 +46,6 @@ internal class LocalPlugin : IDisposable private Type? pluginType; private IDalamudPlugin? instance; - private LocalPluginManifest manifest; - /// /// Initializes a new instance of the class. /// @@ -659,9 +664,11 @@ internal class LocalPlugin : IDisposable var manifestPath = LocalPluginManifest.GetManifestFile(this.DllFile); if (manifestPath.Exists) { - // var isDisabled = this.IsDisabled; // saving the internal state because it could have been deleted + // Save some state that we do actually want to carry over + var guid = this.manifest.WorkingPluginId; + this.manifest = LocalPluginManifest.Load(manifestPath) ?? throw new Exception("Could not reload manifest."); - // this.manifest.Disabled = isDisabled; + this.manifest.WorkingPluginId = guid; this.SaveManifest("dev reload"); } @@ -686,6 +693,12 @@ internal class LocalPlugin : IDisposable }); } + /// + /// Save this plugin manifest. + /// + /// Why it should be saved. + protected void SaveManifest(string reason) => this.manifest.Save(this.manifestFile, reason); + private static void SetupLoaderConfig(LoaderConfig config) { config.IsUnloadable = true; @@ -694,6 +707,4 @@ internal class LocalPlugin : IDisposable config.SharedAssemblies.Add(typeof(Lumina.GameData).Assembly.GetName()); config.SharedAssemblies.Add(typeof(Lumina.Excel.ExcelSheetImpl).Assembly.GetName()); } - - private void SaveManifest(string reason) => this.manifest.Save(this.manifestFile, reason); }