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