Merge pull request #394 from daemitus/PluginLoadReason

This commit is contained in:
goaaats 2021-07-12 16:20:22 +02:00 committed by GitHub
commit a5abe091da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 62 additions and 19 deletions

View file

@ -83,7 +83,7 @@ namespace Dalamud.Interface.Internal.Scratchpad
{
var script = CSharpScript.Create(code, options);
var pi = new DalamudPluginInterface(this.dalamud, "Scratch-" + doc.Id, null);
var pi = new DalamudPluginInterface(this.dalamud, "Scratch-" + doc.Id, null, PluginLoadReason.Unknown);
var plugin = script.ContinueWith<IDalamudPlugin>("return new ScratchPlugin() as IDalamudPlugin;")
.RunAsync().GetAwaiter().GetResult().ReturnValue;

View file

@ -548,8 +548,8 @@ namespace Dalamud.Interface.Internal.Windows
#pragma warning disable CS0618 // Type or member is obsolete
private void DrawIpcDebug()
{
var i1 = new DalamudPluginInterface(this.dalamud, "DalamudTestSub", null);
var i2 = new DalamudPluginInterface(this.dalamud, "DalamudTestPub", null);
var i1 = new DalamudPluginInterface(this.dalamud, "DalamudTestSub", null, PluginLoadReason.Unknown);
var i2 = new DalamudPluginInterface(this.dalamud, "DalamudTestPub", null, PluginLoadReason.Unknown);
if (ImGui.Button("Add test sub"))
{

View file

@ -12,6 +12,7 @@ using CheapLoc;
using Dalamud.Interface.Colors;
using Dalamud.Interface.Components;
using Dalamud.Interface.Windowing;
using Dalamud.Plugin;
using Dalamud.Plugin.Internal;
using Dalamud.Plugin.Internal.Exceptions;
using Dalamud.Plugin.Internal.Types;
@ -526,7 +527,7 @@ namespace Dalamud.Interface.Internal.Windows
{
this.installStatus = OperationStatus.InProgress;
Task.Run(() => this.dalamud.PluginManager.InstallPlugin(manifest, useTesting))
Task.Run(() => this.dalamud.PluginManager.InstallPlugin(manifest, useTesting, PluginLoadReason.Installer))
.ContinueWith(task =>
{
// There is no need to set as Complete for an individual plugin installation
@ -743,7 +744,7 @@ namespace Dalamud.Interface.Internal.Windows
if (!enableTask.Result)
return;
var loadTask = Task.Run(() => plugin.Load())
var loadTask = Task.Run(() => plugin.Load(PluginLoadReason.Installer))
.ContinueWith(this.DisplayErrorContinuation, Locs.ErrorModal_LoadFail(plugin.Name));
loadTask.Wait();

View file

@ -36,7 +36,8 @@ namespace Dalamud.Plugin
/// <param name="dalamud">The dalamud instance to expose.</param>
/// <param name="pluginName">The internal name of the plugin.</param>
/// <param name="assemblyLocation">The equivalent of what Assembly.GetExecutingAssembly().Location should return.</param>
internal DalamudPluginInterface(Dalamud dalamud, string pluginName, string assemblyLocation)
/// <param name="reason">The reason the plugin was loaded.</param>
internal DalamudPluginInterface(Dalamud dalamud, string pluginName, string assemblyLocation, PluginLoadReason reason)
{
this.CommandManager = dalamud.CommandManager;
this.Framework = dalamud.Framework;
@ -50,6 +51,7 @@ namespace Dalamud.Plugin
this.pluginName = pluginName;
this.configs = dalamud.PluginManager.PluginConfigs;
this.AssemblyLocation = assemblyLocation;
this.Reason = reason;
this.GeneralChatType = this.dalamud.Configuration.GeneralChatType;
this.Sanitizer = new Sanitizer(this.Data.Language);
@ -81,6 +83,11 @@ namespace Dalamud.Plugin
/// </summary>
public event LanguageChangedDelegate OnLanguageChanged;
/// <summary>
/// Gets the reason this plugin was loaded.
/// </summary>
public PluginLoadReason Reason { get; }
/// <summary>
/// Gets the plugin assembly location.
/// </summary>

View file

@ -140,7 +140,7 @@ namespace Dalamud.Plugin.Internal
public PluginState State { get; protected set; } = PluginState.Unloaded;
/// <summary>
/// Gets the AssemblyName plugin, populated during <see cref="Load(bool)"/>.
/// Gets the AssemblyName plugin, populated during <see cref="Load(PluginLoadReason, bool)"/>.
/// </summary>
/// <returns>Plugin type.</returns>
public AssemblyName AssemblyName { get; private set; } = null;
@ -188,8 +188,9 @@ namespace Dalamud.Plugin.Internal
/// <summary>
/// Load this plugin.
/// </summary>
/// <param name="reason">The reason why this plugin is being loaded.</param>
/// <param name="reloading">Load while reloading.</param>
public void Load(bool reloading = false)
public void Load(PluginLoadReason reason, bool reloading = false)
{
// Allowed: Unloaded
switch (this.State)
@ -271,7 +272,7 @@ namespace Dalamud.Plugin.Internal
this.Manifest.Save(this.manifestFile);
}
this.DalamudInterface = new DalamudPluginInterface(this.dalamud, this.pluginAssembly.GetName().Name, this.DllFile.FullName);
this.DalamudInterface = new DalamudPluginInterface(this.dalamud, this.pluginAssembly.GetName().Name, this.DllFile.FullName, reason);
if (this.IsDev)
{
@ -365,7 +366,7 @@ namespace Dalamud.Plugin.Internal
public void Reload()
{
this.Unload(true);
this.Load(true);
this.Load(PluginLoadReason.Reload, true);
}
/// <summary>

View file

@ -192,7 +192,7 @@ namespace Dalamud.Plugin.Internal
{
try
{
this.LoadPlugin(pluginDef.DllFile, pluginDef.Manifest, pluginDef.IsDev, isBoot: true);
this.LoadPlugin(pluginDef.DllFile, pluginDef.Manifest, PluginLoadReason.Boot, pluginDef.IsDev, isBoot: true);
}
catch (InvalidPluginException)
{
@ -230,8 +230,7 @@ namespace Dalamud.Plugin.Internal
{
try
{
plugin.Unload();
plugin.Load();
plugin.Reload();
}
catch (Exception ex)
{
@ -300,7 +299,7 @@ namespace Dalamud.Plugin.Internal
try
{
// Add them to the list and let the user decide, nothing is auto-loaded.
this.LoadPlugin(dllFile, manifest, isDev: true, doNotLoad: true);
this.LoadPlugin(dllFile, manifest, PluginLoadReason.Installer, isDev: true, doNotLoad: true);
listChanged = true;
}
catch (InvalidPluginException)
@ -322,7 +321,8 @@ namespace Dalamud.Plugin.Internal
/// </summary>
/// <param name="repoManifest">The plugin definition.</param>
/// <param name="useTesting">If the testing version should be used.</param>
public void InstallPlugin(RemotePluginManifest repoManifest, bool useTesting)
/// <param name="reason">The reason this plugin was loaded.</param>
public void InstallPlugin(RemotePluginManifest repoManifest, bool useTesting, PluginLoadReason reason)
{
Log.Debug($"Installing plugin {repoManifest.Name} (testing={useTesting})");
@ -413,7 +413,7 @@ namespace Dalamud.Plugin.Internal
Log.Information($"Installed plugin {manifest.Name} (testing={useTesting})");
this.LoadPlugin(dllFile, manifest);
this.LoadPlugin(dllFile, manifest, reason);
this.NotifyInstalledPluginsChanged();
}
@ -423,10 +423,11 @@ namespace Dalamud.Plugin.Internal
/// </summary>
/// <param name="dllFile">The <see cref="FileInfo"/> associated with the main assembly of this plugin.</param>
/// <param name="manifest">The already loaded definition, if available.</param>
/// <param name="reason">The reason this plugin was loaded.</param>
/// <param name="isDev">If this plugin should support development features.</param>
/// <param name="isBoot">If this plugin is being loaded at boot.</param>
/// <param name="doNotLoad">Don't load the plugin, just don't do it.</param>
public void LoadPlugin(FileInfo dllFile, LocalPluginManifest manifest, bool isDev = false, bool isBoot = false, bool doNotLoad = false)
public void LoadPlugin(FileInfo dllFile, LocalPluginManifest manifest, PluginLoadReason reason, bool isDev = false, bool isBoot = false, bool doNotLoad = false)
{
var name = manifest?.Name ?? dllFile.Name;
var loadPlugin = !doNotLoad;
@ -454,7 +455,7 @@ namespace Dalamud.Plugin.Internal
if (plugin.IsDisabled)
plugin.Enable();
plugin.Load();
plugin.Load(reason);
}
catch (InvalidPluginException)
{
@ -653,7 +654,7 @@ namespace Dalamud.Plugin.Internal
try
{
this.InstallPlugin(metadata.UpdateManifest, metadata.UseTesting);
this.InstallPlugin(metadata.UpdateManifest, metadata.UseTesting, PluginLoadReason.Update);
listChanged = true;
}
catch (Exception ex)

View file

@ -0,0 +1,33 @@
namespace Dalamud.Plugin
{
/// <summary>
/// This enum reflects reasons for loading a plugin.
/// </summary>
public enum PluginLoadReason
{
/// <summary>
/// We don't know why this plugin was loaded.
/// </summary>
Unknown,
/// <summary>
/// This plugin was loaded because it was installed with the plugin installer.
/// </summary>
Installer,
/// <summary>
/// This plugin was loaded because it was just updated.
/// </summary>
Update,
/// <summary>
/// This plugin was loaded because it was told to reload.
/// </summary>
Reload,
/// <summary>
/// This plugin was loaded because the game was started or Dalamud was reinjected.
/// </summary>
Boot,
}
}