diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs index 9e7ceff3c..4a74d6981 100644 --- a/Dalamud/Dalamud.cs +++ b/Dalamud/Dalamud.cs @@ -2,7 +2,7 @@ using System; using System.Diagnostics; using System.IO; using System.Threading; - +using System.Threading.Tasks; using Dalamud.Configuration; using Dalamud.Data; using Dalamud.Game; @@ -325,7 +325,9 @@ namespace Dalamud this, this.StartInfo.PluginDirectory, this.StartInfo.DefaultPluginDirectory); - this.PluginManager.LoadPlugins(); + this.PluginManager.LoadSynchronousPlugins(); + + Task.Run(() => this.PluginManager.LoadDeferredPlugins()); Log.Information("[T3] PM OK!"); } diff --git a/Dalamud/Plugin/PluginManager.cs b/Dalamud/Plugin/PluginManager.cs index 7eab04130..ffc9715ea 100644 --- a/Dalamud/Plugin/PluginManager.cs +++ b/Dalamud/Plugin/PluginManager.cs @@ -109,10 +109,12 @@ namespace Dalamud.Plugin this.Plugins.Clear(); } + private IEnumerable<(FileInfo dllFile, PluginDefinition definition, bool isRaw)> deferredPlugins; + /// - /// Load all regular and dev plugins. + /// Load plugins that need to be loaded synchronously and prepare plugins that can be loaded asynchronously. /// - public void LoadPlugins() + public void LoadSynchronousPlugins() { var loadDirectories = new List<(DirectoryInfo dirInfo, bool isRaw)> { @@ -147,8 +149,36 @@ namespace Dalamud.Plugin return prio2.CompareTo(prio1); }); - // Pass preloaded definitions to LoadPluginFromAssembly, because we already loaded them anyways - foreach (var (dllFile, definition, isRaw) in pluginDefs) + this.deferredPlugins = pluginDefs.Where(x => x.definition?.LoadPriority <= 0); + + // Pass preloaded definitions for "synchronous load" plugins to LoadPluginFromAssembly, because we already loaded them anyways + foreach (var (dllFile, definition, isRaw) in pluginDefs.Where(x => x.definition?.LoadPriority > 0)) + { + try + { + this.LoadPluginFromAssembly(dllFile, isRaw, PluginLoadReason.Boot, definition); + } + catch (Exception ex) + { + Log.Error(ex, $"Plugin load for {dllFile.FullName} failed."); + if (ex is ReflectionTypeLoadException typeLoadException) + { + foreach (var exception in typeLoadException.LoaderExceptions) + { + Log.Error(exception, "LoaderException:"); + } + } + } + } + } + + public void LoadDeferredPlugins() + { + if (this.deferredPlugins == null) + throw new Exception("Synchronous plugins need to be loaded before deferred plugins."); + + // Pass preloaded definitions for "deferred load" plugins to LoadPluginFromAssembly, because we already loaded them anyways + foreach (var (dllFile, definition, isRaw) in this.deferredPlugins) { try { @@ -343,7 +373,7 @@ namespace Dalamud.Plugin public void ReloadPlugins() { this.UnloadPlugins(); - this.LoadPlugins(); + this.LoadSynchronousPlugins(); } private class BannedPlugin