fix: dev plugins always need to retain their WorkingPluginId, even throughout reloads

This commit is contained in:
goat 2023-09-19 23:14:50 +02:00
parent f7ae34281f
commit 979a5463ca
No known key found for this signature in database
GPG key ID: 49E2AA8C6A76498B
3 changed files with 46 additions and 6 deletions

View file

@ -1,3 +1,5 @@
using System;
namespace Dalamud.Configuration.Internal; namespace Dalamud.Configuration.Internal;
/// <summary> /// <summary>
@ -14,4 +16,9 @@ internal sealed class DevPluginSettings
/// Gets or sets a value indicating whether this plugin should automatically reload on file change. /// Gets or sets a value indicating whether this plugin should automatically reload on file change.
/// </summary> /// </summary>
public bool AutomaticReloading { get; set; } = false; public bool AutomaticReloading { get; set; } = false;
/// <summary>
/// Gets or sets an ID uniquely identifying this specific instance of a devPlugin.
/// </summary>
public Guid WorkingPluginId { get; set; } = Guid.Empty;
} }

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -40,6 +41,22 @@ internal class LocalDevPlugin : LocalPlugin, IDisposable
configuration.DevPluginSettings[dllFile.FullName] = this.devSettings = new DevPluginSettings(); configuration.DevPluginSettings[dllFile.FullName] = this.devSettings = new DevPluginSettings();
configuration.QueueSave(); 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) if (this.AutomaticReload)
{ {
@ -76,6 +93,11 @@ internal class LocalDevPlugin : LocalPlugin, IDisposable
} }
} }
} }
/// <summary>
/// Gets an ID uniquely identifying this specific instance of a devPlugin.
/// </summary>
public Guid DevImposedWorkingPluginId => this.devSettings.WorkingPluginId;
/// <inheritdoc/> /// <inheritdoc/>
public new void Dispose() public new void Dispose()

View file

@ -26,6 +26,13 @@ namespace Dalamud.Plugin.Internal.Types;
/// </summary> /// </summary>
internal class LocalPlugin : IDisposable internal class LocalPlugin : IDisposable
{ {
/// <summary>
/// The underlying manifest for this plugin.
/// </summary>
#pragma warning disable SA1401
protected LocalPluginManifest manifest;
#pragma warning restore SA1401
private static readonly ModuleLog Log = new("LOCALPLUGIN"); private static readonly ModuleLog Log = new("LOCALPLUGIN");
private readonly FileInfo manifestFile; private readonly FileInfo manifestFile;
@ -39,8 +46,6 @@ internal class LocalPlugin : IDisposable
private Type? pluginType; private Type? pluginType;
private IDalamudPlugin? instance; private IDalamudPlugin? instance;
private LocalPluginManifest manifest;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="LocalPlugin"/> class. /// Initializes a new instance of the <see cref="LocalPlugin"/> class.
/// </summary> /// </summary>
@ -659,9 +664,11 @@ internal class LocalPlugin : IDisposable
var manifestPath = LocalPluginManifest.GetManifestFile(this.DllFile); var manifestPath = LocalPluginManifest.GetManifestFile(this.DllFile);
if (manifestPath.Exists) 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 = LocalPluginManifest.Load(manifestPath) ?? throw new Exception("Could not reload manifest.");
// this.manifest.Disabled = isDisabled; this.manifest.WorkingPluginId = guid;
this.SaveManifest("dev reload"); this.SaveManifest("dev reload");
} }
@ -686,6 +693,12 @@ internal class LocalPlugin : IDisposable
}); });
} }
/// <summary>
/// Save this plugin manifest.
/// </summary>
/// <param name="reason">Why it should be saved.</param>
protected void SaveManifest(string reason) => this.manifest.Save(this.manifestFile, reason);
private static void SetupLoaderConfig(LoaderConfig config) private static void SetupLoaderConfig(LoaderConfig config)
{ {
config.IsUnloadable = true; config.IsUnloadable = true;
@ -694,6 +707,4 @@ internal class LocalPlugin : IDisposable
config.SharedAssemblies.Add(typeof(Lumina.GameData).Assembly.GetName()); config.SharedAssemblies.Add(typeof(Lumina.GameData).Assembly.GetName());
config.SharedAssemblies.Add(typeof(Lumina.Excel.ExcelSheetImpl).Assembly.GetName()); config.SharedAssemblies.Add(typeof(Lumina.Excel.ExcelSheetImpl).Assembly.GetName());
} }
private void SaveManifest(string reason) => this.manifest.Save(this.manifestFile, reason);
} }