mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
Do not load plugin when it is disabled (#10515)
This commit is contained in:
@@ -3,6 +3,14 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Wox.Plugin.Logger;
|
||||
using Wox.Plugin.Properties;
|
||||
|
||||
namespace Wox.Plugin
|
||||
{
|
||||
@@ -12,6 +20,76 @@ namespace Wox.Plugin
|
||||
|
||||
public PluginMetadata Metadata { get; internal set; }
|
||||
|
||||
public PluginPair(PluginMetadata metadata)
|
||||
{
|
||||
this.Metadata = metadata;
|
||||
}
|
||||
|
||||
public bool IsPluginLoaded { get; set; }
|
||||
|
||||
public void LoadPlugin(IPublicAPI api)
|
||||
{
|
||||
if (Metadata.Disabled)
|
||||
{
|
||||
Log.Info($"Do not load {Metadata.Name} as it is disabled.", GetType());
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsPluginLoaded)
|
||||
{
|
||||
Log.Info($"Plugin {Metadata.Name} is already loaded", GetType());
|
||||
return;
|
||||
}
|
||||
|
||||
var stopWatch = new Stopwatch();
|
||||
stopWatch.Start();
|
||||
if (!CreatePluginInstance())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!InitPlugin(api))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
stopWatch.Stop();
|
||||
IsPluginLoaded = true;
|
||||
Metadata.InitTime += stopWatch.ElapsedMilliseconds;
|
||||
Log.Info($"Total load cost for <{Metadata.Name}> is <{Metadata.InitTime}ms>", GetType());
|
||||
return;
|
||||
}
|
||||
|
||||
public void Update(PowerLauncherPluginSettings setting, IPublicAPI api)
|
||||
{
|
||||
if (setting == null || api == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Metadata.Disabled && !setting.Disabled)
|
||||
{
|
||||
Metadata.Disabled = false;
|
||||
LoadPlugin(api);
|
||||
if (!IsPluginLoaded)
|
||||
{
|
||||
var title = string.Format(CultureInfo.CurrentCulture, Resources.FailedToLoadPluginTitle, Metadata.Name);
|
||||
api.ShowMsg(title, Resources.FailedToLoadPluginDescription, string.Empty, false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Metadata.Disabled = setting.Disabled;
|
||||
}
|
||||
|
||||
Metadata.ActionKeyword = setting.ActionKeyword;
|
||||
Metadata.IsGlobal = setting.IsGlobal;
|
||||
if (Plugin is ISettingProvider)
|
||||
{
|
||||
(Plugin as ISettingProvider).UpdateSettings(setting);
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Metadata.Name;
|
||||
@@ -36,5 +114,69 @@ namespace Wox.Plugin
|
||||
var hashcode = Metadata.ID?.GetHashCode(StringComparison.Ordinal) ?? 0;
|
||||
return hashcode;
|
||||
}
|
||||
|
||||
private bool CreatePluginInstance()
|
||||
{
|
||||
try
|
||||
{
|
||||
_assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(Metadata.ExecuteFilePath);
|
||||
}
|
||||
#pragma warning disable CA1031 // Do not catch general exception types
|
||||
catch (Exception e)
|
||||
#pragma warning restore CA1031 // Do not catch general exception types
|
||||
{
|
||||
Log.Exception($"Couldn't load assembly for {Metadata.Name}", e, MethodBase.GetCurrentMethod().DeclaringType);
|
||||
return false;
|
||||
}
|
||||
|
||||
var types = _assembly.GetTypes();
|
||||
Type type;
|
||||
try
|
||||
{
|
||||
type = types.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin)));
|
||||
}
|
||||
catch (InvalidOperationException e)
|
||||
{
|
||||
Log.Exception($"Can't find class implement IPlugin for <{Metadata.Name}>", e, MethodBase.GetCurrentMethod().DeclaringType);
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Plugin = (IPlugin)Activator.CreateInstance(type);
|
||||
}
|
||||
#pragma warning disable CA1031 // Do not catch general exception types
|
||||
catch (Exception e)
|
||||
#pragma warning restore CA1031 // Do not catch general exception types
|
||||
{
|
||||
Log.Exception($"Can't create instance for <{Metadata.Name}>", e, MethodBase.GetCurrentMethod().DeclaringType);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool InitPlugin(IPublicAPI api)
|
||||
{
|
||||
try
|
||||
{
|
||||
Plugin.Init(new PluginInitContext
|
||||
{
|
||||
CurrentPluginMetadata = Metadata,
|
||||
API = api,
|
||||
});
|
||||
}
|
||||
#pragma warning disable CA1031 // Do not catch general exception types
|
||||
catch (Exception e)
|
||||
#pragma warning restore CA1031 // Do not catch general exception types
|
||||
{
|
||||
Log.Exception($"Fail to Init plugin: {Metadata.Name}", e, GetType());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private Assembly _assembly;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,5 +59,23 @@ namespace Wox.Plugin.Properties {
|
||||
resourceCulture = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Please contact plugin creator for help.
|
||||
/// </summary>
|
||||
public static string FailedToLoadPluginDescription {
|
||||
get {
|
||||
return ResourceManager.GetString("FailedToLoadPluginDescription", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Fail to Load {0} Plugin.
|
||||
/// </summary>
|
||||
public static string FailedToLoadPluginTitle {
|
||||
get {
|
||||
return ResourceManager.GetString("FailedToLoadPluginTitle", resourceCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,4 +117,10 @@
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<data name="FailedToLoadPluginDescription" xml:space="preserve">
|
||||
<value>Please contact plugin creator for help</value>
|
||||
</data>
|
||||
<data name="FailedToLoadPluginTitle" xml:space="preserve">
|
||||
<value>Fail to Load {0} Plugin</value>
|
||||
</data>
|
||||
</root>
|
||||
Reference in New Issue
Block a user