Do not load plugin when it is disabled (#10515)

This commit is contained in:
Mykhailo Pylyp
2021-04-05 17:57:22 +03:00
committed by GitHub
parent ed21dba8f0
commit 1c8b7a5ae5
11 changed files with 238 additions and 147 deletions

View File

@@ -10,6 +10,7 @@ using System.IO.Abstractions;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using PowerLauncher.Properties;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
@@ -49,7 +50,10 @@ namespace PowerLauncher.Plugin
{
if (_allPlugins == null)
{
_allPlugins = PluginsLoader.Plugins(PluginConfig.Parse(Directories));
_allPlugins = PluginConfig.Parse(Directories)
.Where(x => x.Language.ToUpperInvariant() == AllowedLanguage.CSharp)
.Select(x => new PluginPair(x))
.ToList();
}
}
}
@@ -119,23 +123,15 @@ namespace PowerLauncher.Plugin
var failedPlugins = new ConcurrentQueue<PluginPair>();
Parallel.ForEach(AllPlugins, pair =>
{
try
if (pair.Metadata.Disabled)
{
var milliseconds = Stopwatch.Debug($"PluginManager.InitializePlugins - Init method time cost for <{pair.Metadata.Name}>", () =>
{
pair.Plugin.Init(new PluginInitContext
{
CurrentPluginMetadata = pair.Metadata,
API = API,
});
});
pair.Metadata.InitTime += milliseconds;
Log.Info($"Total init cost for <{pair.Metadata.Name}> is <{pair.Metadata.InitTime}ms>", MethodBase.GetCurrentMethod().DeclaringType);
return;
}
catch (Exception e)
pair.LoadPlugin(API);
if (!pair.IsPluginLoaded)
{
Log.Exception($"Fail to Init plugin: {pair.Metadata.Name}", e, MethodBase.GetCurrentMethod().DeclaringType);
pair.Metadata.Disabled = true;
failedPlugins.Enqueue(pair);
}
});
@@ -145,7 +141,8 @@ namespace PowerLauncher.Plugin
if (failedPlugins.Any())
{
var failed = string.Join(",", failedPlugins.Select(x => x.Metadata.Name));
API.ShowMsg($"Fail to Init Plugins", $"Plugins: {failed} - fail to load and would be disabled, please contact plugin creator for help", string.Empty, false);
var description = string.Format(CultureInfo.CurrentCulture, Resources.FailedToInitializePluginsDescription, failed);
API.ShowMsg(Resources.FailedToInitializePluginsTitle, description, string.Empty, false);
}
}
@@ -162,6 +159,11 @@ namespace PowerLauncher.Plugin
throw new ArgumentNullException(nameof(pair));
}
if (!pair.IsPluginLoaded)
{
return new List<Result>();
}
try
{
List<Result> results = null;

View File

@@ -1,91 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using Wox.Infrastructure;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace PowerLauncher.Plugin
{
public static class PluginsLoader
{
public const string PATH = "PATH";
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas)
{
var csharpPlugins = CSharpPlugins(metadatas).ToList();
return csharpPlugins;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
public static IEnumerable<PluginPair> CSharpPlugins(List<PluginMetadata> source)
{
var plugins = new List<PluginPair>();
var metadatas = source
.Where(o => o.Language.ToUpperInvariant() == AllowedLanguage.CSharp)
.ToList();
foreach (var metadata in metadatas)
{
var milliseconds = Stopwatch.Debug($"PluginsLoader.CSharpPlugins - Constructor init cost for {metadata.Name}", () =>
{
#if DEBUG
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(metadata.ExecuteFilePath);
var types = assembly.GetTypes();
var type = types.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin)));
var plugin = (IPlugin)Activator.CreateInstance(type);
#else
Assembly assembly;
try
{
assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(metadata.ExecuteFilePath);
}
catch (Exception e)
{
Log.Exception($"Couldn't load assembly for {metadata.Name}", e, MethodBase.GetCurrentMethod().DeclaringType);
return;
}
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;
}
IPlugin plugin;
try
{
plugin = (IPlugin)Activator.CreateInstance(type);
}
catch (Exception e)
{
Log.Exception($"Can't create instance for <{metadata.Name}>", e, MethodBase.GetCurrentMethod().DeclaringType);
return;
}
#endif
PluginPair pair = new PluginPair
{
Plugin = plugin,
Metadata = metadata,
};
plugins.Add(pair);
});
metadata.InitTime += milliseconds;
}
return plugins;
}
}
}