mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 20:27:36 +02:00
Divide load and initialize of plugins into two stages (#10650)
This commit is contained in:
@@ -128,9 +128,9 @@ namespace PowerLauncher.Plugin
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pair.LoadPlugin(API);
|
pair.InitializePlugin(API);
|
||||||
|
|
||||||
if (!pair.IsPluginLoaded)
|
if (!pair.IsPluginInitialized)
|
||||||
{
|
{
|
||||||
failedPlugins.Enqueue(pair);
|
failedPlugins.Enqueue(pair);
|
||||||
}
|
}
|
||||||
@@ -159,7 +159,7 @@ namespace PowerLauncher.Plugin
|
|||||||
throw new ArgumentNullException(nameof(pair));
|
throw new ArgumentNullException(nameof(pair));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!pair.IsPluginLoaded)
|
if (!pair.IsPluginInitialized)
|
||||||
{
|
{
|
||||||
return new List<Result>();
|
return new List<Result>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -187,8 +187,8 @@ namespace PowerLauncher
|
|||||||
return PluginManager.AllPlugins.Select(x => new PowerLauncherPluginSettings()
|
return PluginManager.AllPlugins.Select(x => new PowerLauncherPluginSettings()
|
||||||
{
|
{
|
||||||
Id = x.Metadata.ID,
|
Id = x.Metadata.ID,
|
||||||
Name = x.Plugin.Name,
|
Name = x.Plugin == null ? x.Metadata.Name : x.Plugin.Name,
|
||||||
Description = x.Plugin.Description,
|
Description = x.Plugin?.Description,
|
||||||
Author = x.Metadata.Author,
|
Author = x.Metadata.Author,
|
||||||
Disabled = x.Metadata.Disabled,
|
Disabled = x.Metadata.Disabled,
|
||||||
IsGlobal = x.Metadata.IsGlobal,
|
IsGlobal = x.Metadata.IsGlobal,
|
||||||
|
|||||||
@@ -23,40 +23,36 @@ namespace Wox.Plugin
|
|||||||
public PluginPair(PluginMetadata metadata)
|
public PluginPair(PluginMetadata metadata)
|
||||||
{
|
{
|
||||||
this.Metadata = metadata;
|
this.Metadata = metadata;
|
||||||
|
LoadPlugin();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsPluginLoaded { get; set; }
|
public bool IsPluginInitialized { get; set; }
|
||||||
|
|
||||||
public void LoadPlugin(IPublicAPI api)
|
public void InitializePlugin(IPublicAPI api)
|
||||||
{
|
{
|
||||||
if (Metadata.Disabled)
|
if (Metadata.Disabled)
|
||||||
{
|
{
|
||||||
Log.Info($"Do not load {Metadata.Name} as it is disabled.", GetType());
|
Log.Info($"Do not initialize {Metadata.Name} as it is disabled.", GetType());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsPluginLoaded)
|
if (IsPluginInitialized)
|
||||||
{
|
{
|
||||||
Log.Info($"Plugin {Metadata.Name} is already loaded", GetType());
|
Log.Info($"{Metadata.Name} plugin is already initialized", GetType());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var stopWatch = new Stopwatch();
|
var stopWatch = new Stopwatch();
|
||||||
stopWatch.Start();
|
stopWatch.Start();
|
||||||
if (!CreatePluginInstance())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!InitPlugin(api))
|
if (!InitPlugin(api))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
stopWatch.Stop();
|
stopWatch.Stop();
|
||||||
IsPluginLoaded = true;
|
IsPluginInitialized = true;
|
||||||
Metadata.InitTime += stopWatch.ElapsedMilliseconds;
|
Metadata.InitTime += stopWatch.ElapsedMilliseconds;
|
||||||
Log.Info($"Total load cost for <{Metadata.Name}> is <{Metadata.InitTime}ms>", GetType());
|
Log.Info($"Total initialize cost for <{Metadata.Name}> is <{Metadata.InitTime}ms>", GetType());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,8 +66,8 @@ namespace Wox.Plugin
|
|||||||
if (Metadata.Disabled && !setting.Disabled)
|
if (Metadata.Disabled && !setting.Disabled)
|
||||||
{
|
{
|
||||||
Metadata.Disabled = false;
|
Metadata.Disabled = false;
|
||||||
LoadPlugin(api);
|
InitializePlugin(api);
|
||||||
if (!IsPluginLoaded)
|
if (!IsPluginInitialized)
|
||||||
{
|
{
|
||||||
var title = string.Format(CultureInfo.CurrentCulture, Resources.FailedToLoadPluginTitle, Metadata.Name);
|
var title = string.Format(CultureInfo.CurrentCulture, Resources.FailedToLoadPluginTitle, Metadata.Name);
|
||||||
api.ShowMsg(title, Resources.FailedToLoadPluginDescription, string.Empty, false);
|
api.ShowMsg(title, Resources.FailedToLoadPluginDescription, string.Empty, false);
|
||||||
@@ -115,8 +111,23 @@ namespace Wox.Plugin
|
|||||||
return hashcode;
|
return hashcode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void LoadPlugin()
|
||||||
|
{
|
||||||
|
var stopWatch = new Stopwatch();
|
||||||
|
CreatePluginInstance();
|
||||||
|
stopWatch.Stop();
|
||||||
|
Metadata.InitTime += stopWatch.ElapsedMilliseconds;
|
||||||
|
Log.Info($"Load cost for <{Metadata.Name}> is <{Metadata.InitTime}ms>", GetType());
|
||||||
|
}
|
||||||
|
|
||||||
private bool CreatePluginInstance()
|
private bool CreatePluginInstance()
|
||||||
{
|
{
|
||||||
|
if (Plugin != null)
|
||||||
|
{
|
||||||
|
Log.Warn($"{Metadata.Name} plugin was already loaded", GetType());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(Metadata.ExecuteFilePath);
|
_assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(Metadata.ExecuteFilePath);
|
||||||
@@ -158,6 +169,12 @@ namespace Wox.Plugin
|
|||||||
|
|
||||||
private bool InitPlugin(IPublicAPI api)
|
private bool InitPlugin(IPublicAPI api)
|
||||||
{
|
{
|
||||||
|
if (Plugin == null)
|
||||||
|
{
|
||||||
|
Log.Warn($"Can not initialize {Metadata.Name} plugin as it was not loaded", GetType());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Plugin.Init(new PluginInitContext
|
Plugin.Init(new PluginInitContext
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ namespace Wox.Test
|
|||||||
var pluginPair = new PluginPair(metadata)
|
var pluginPair = new PluginPair(metadata)
|
||||||
{
|
{
|
||||||
Plugin = pluginMock.Object,
|
Plugin = pluginMock.Object,
|
||||||
IsPluginLoaded = true,
|
IsPluginInitialized = true,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
|
|||||||
Reference in New Issue
Block a user