PowerToys/Wox.Core/Plugin/PluginManager.cs

348 lines
12 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Diagnostics;
2014-12-26 19:36:43 +08:00
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
2014-12-29 23:02:50 +08:00
using Wox.Core.Exception;
2015-02-07 20:17:49 +08:00
using Wox.Core.i18n;
2015-01-07 22:23:10 +08:00
using Wox.Core.UI;
using Wox.Core.UserSettings;
2015-01-04 23:08:26 +08:00
using Wox.Infrastructure;
using Wox.Infrastructure.Logger;
2014-12-26 19:36:43 +08:00
using Wox.Plugin;
2015-02-05 18:43:05 +08:00
using Wox.Plugin.Features;
2014-12-26 19:36:43 +08:00
namespace Wox.Core.Plugin
{
/// <summary>
/// The entry for managing Wox plugins
/// </summary>
public static class PluginManager
{
public const string ActionKeywordWildcardSign = "*";
2015-11-01 00:02:56 +08:00
private static List<PluginMetadata> pluginMetadatas;
2015-02-07 20:17:49 +08:00
private static List<KeyValuePair<PluginPair, IInstantQuery>> instantSearches;
2015-02-05 22:20:42 +08:00
private static List<KeyValuePair<PluginPair, IExclusiveQuery>> exclusiveSearchPlugins;
private static List<KeyValuePair<PluginPair, IContextMenu>> contextMenuPlugins;
2015-01-23 20:49:00 +08:00
2014-12-26 22:51:04 +08:00
public static IPublicAPI API { get; private set; }
2014-12-26 19:36:43 +08:00
private static List<PluginPair> plugins = new List<PluginPair>();
2014-12-26 22:51:04 +08:00
2014-12-26 19:36:43 +08:00
/// <summary>
/// Directories that will hold Wox plugin directory
/// </summary>
private static List<string> pluginDirectories = new List<string>();
2015-01-20 22:33:45 +08:00
private static void SetupPluginDirectories()
2014-12-27 12:34:51 +08:00
{
2015-01-20 22:33:45 +08:00
pluginDirectories.Add(PluginDirectory);
MakesurePluginDirectoriesExist();
2014-12-27 12:34:51 +08:00
}
2015-01-20 22:33:45 +08:00
public static string PluginDirectory
2014-12-26 19:36:43 +08:00
{
2015-01-20 22:33:45 +08:00
get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Plugins"); }
2014-12-26 19:36:43 +08:00
}
private static void MakesurePluginDirectoriesExist()
{
foreach (string pluginDirectory in pluginDirectories)
{
if (!Directory.Exists(pluginDirectory))
{
try
{
Directory.CreateDirectory(pluginDirectory);
}
catch (System.Exception e)
{
Log.Error(e.Message);
}
2014-12-26 19:36:43 +08:00
}
}
}
/// <summary>
/// Load and init all Wox plugins
/// </summary>
2014-12-26 22:51:04 +08:00
public static void Init(IPublicAPI api)
2014-12-26 19:36:43 +08:00
{
2014-12-29 23:02:50 +08:00
if (api == null) throw new WoxCritialException("api is null");
SetupPluginDirectories();
2014-12-26 22:51:04 +08:00
API = api;
2014-12-26 19:36:43 +08:00
plugins.Clear();
pluginMetadatas = PluginConfig.Parse(pluginDirectories);
2014-12-26 19:36:43 +08:00
plugins.AddRange(new CSharpPluginLoader().LoadPlugin(pluginMetadatas));
plugins.AddRange(new JsonRPCPluginLoader<PythonPlugin>().LoadPlugin(pluginMetadatas));
2015-01-07 22:23:10 +08:00
//load plugin i18n languages
ResourceMerger.ApplyPluginLanguages();
2014-12-26 19:36:43 +08:00
foreach (PluginPair pluginPair in plugins)
{
PluginPair pair = pluginPair;
2015-01-04 23:08:26 +08:00
ThreadPool.QueueUserWorkItem(o =>
2014-12-26 19:36:43 +08:00
{
Stopwatch sw = new Stopwatch();
sw.Start();
pair.Plugin.Init(new PluginInitContext
2015-01-04 23:08:26 +08:00
{
CurrentPluginMetadata = pair.Metadata,
Proxy = HttpProxy.Instance,
API = API
});
sw.Stop();
Debug.WriteLine(string.Format("Plugin init:{0} - {1}", pair.Metadata.Name, sw.ElapsedMilliseconds));
pair.InitTime = sw.ElapsedMilliseconds;
2015-02-07 20:17:49 +08:00
InternationalizationManager.Instance.UpdatePluginMetadataTranslations(pair);
});
2014-12-26 19:36:43 +08:00
}
2015-02-04 23:16:41 +08:00
ThreadPool.QueueUserWorkItem(o =>
{
LoadInstantSearches();
});
2014-12-26 19:36:43 +08:00
}
2014-12-27 12:34:51 +08:00
public static void InstallPlugin(string path)
{
PluginInstaller.Install(path);
}
public static void QueryForAllPlugins(Query query)
2014-12-26 22:51:04 +08:00
{
2015-11-01 00:02:56 +08:00
query.ActionKeyword = string.Empty;
query.Search = query.RawQuery;
if (query.Terms.Length == 0) return;
if (IsVailldActionKeyword(query.Terms[0]))
{
query.ActionKeyword = query.Terms[0];
}
if (!string.IsNullOrEmpty(query.ActionKeyword))
{
query.Search = string.Join(Query.Seperater, query.Terms.Skip(1).ToArray());
}
QueryDispatch(query);
}
private static void QueryDispatch(Query query)
{
var pluginPairs = IsExclusivePluginQuery(query) ? GetExclusivePlugins(query) : GetGenericPlugins();
foreach (var plugin in pluginPairs)
{
var customizedPluginConfig = UserSettingStorage.Instance.
CustomizedPluginConfigs.FirstOrDefault(o => o.ID == plugin.Metadata.ID);
if (customizedPluginConfig != null && customizedPluginConfig.Disabled)
{
return;
}
if (query.IsIntantQuery && IsInstantSearchPlugin(plugin.Metadata))
{
Debug.WriteLine(string.Format("Plugin {0} is executing instant search.", plugin.Metadata.Name));
using (new Timeit(" => instant search took: "))
{
QueryForPlugin(plugin, query);
}
}
else
{
ThreadPool.QueueUserWorkItem(state =>
{
QueryForPlugin(plugin, query);
});
}
}
2014-12-26 22:51:04 +08:00
}
2014-12-26 19:36:43 +08:00
public static List<PluginPair> AllPlugins
{
get
{
return plugins.OrderBy(o => o.Metadata.Name).ToList();
2014-12-26 19:36:43 +08:00
}
}
2015-02-05 22:20:42 +08:00
/// <summary>
2015-02-06 18:13:22 +08:00
/// Check if a query contains valid action keyword
2015-02-05 22:20:42 +08:00
/// </summary>
2015-11-01 00:02:56 +08:00
/// <param name="actionKeyword"></param>
2015-02-05 22:20:42 +08:00
/// <returns></returns>
2015-11-01 00:02:56 +08:00
private static bool IsVailldActionKeyword(string actionKeyword)
2014-12-26 19:36:43 +08:00
{
2015-02-06 18:13:22 +08:00
PluginPair pair = plugins.FirstOrDefault(o => o.Metadata.ActionKeyword == actionKeyword);
if (pair != null)
{
var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == pair.Metadata.ID);
if (customizedPluginConfig != null && customizedPluginConfig.Disabled)
{
return false;
}
return true;
}
return false;
2014-12-26 19:36:43 +08:00
}
2015-02-05 22:20:42 +08:00
public static bool IsGenericPlugin(PluginMetadata metadata)
2015-01-04 23:08:26 +08:00
{
return metadata.ActionKeyword == ActionKeywordWildcardSign;
2015-01-04 23:08:26 +08:00
}
2015-02-05 22:20:42 +08:00
public static bool IsInstantQuery(string query)
{
2015-02-05 22:20:42 +08:00
return LoadInstantSearches().Any(o => o.Value.IsInstantQuery(query));
2015-02-04 23:16:41 +08:00
}
private static bool IsInstantSearchPlugin(PluginMetadata pluginMetadata)
2015-02-04 23:16:41 +08:00
{
//todo:to improve performance, any instant search plugin that takes long than 200ms will not consider a instant plugin anymore
return pluginMetadata.Language.ToUpper() == AllowedLanguage.CSharp &&
2015-02-07 20:17:49 +08:00
LoadInstantSearches().Any(o => o.Key.Metadata.ID == pluginMetadata.ID);
2015-02-04 23:16:41 +08:00
}
private static void QueryForPlugin(PluginPair pair, Query query)
2015-02-04 23:16:41 +08:00
{
try
{
Stopwatch sw = new Stopwatch();
sw.Start();
2015-02-04 23:16:41 +08:00
List<Result> results = pair.Plugin.Query(query) ?? new List<Result>();
results.ForEach(o =>
{
o.PluginID = pair.Metadata.ID;
});
sw.Stop();
Debug.WriteLine(string.Format("Plugin query: {0} - {1}", pair.Metadata.Name, sw.ElapsedMilliseconds));
pair.QueryCount += 1;
if (pair.QueryCount == 1)
{
pair.AvgQueryTime = sw.ElapsedMilliseconds;
}
else
{
pair.AvgQueryTime = (pair.AvgQueryTime + sw.ElapsedMilliseconds) / 2;
}
2015-02-04 23:16:41 +08:00
API.PushResults(query, pair.Metadata, results);
}
catch (System.Exception e)
{
throw new WoxPluginException(pair.Metadata.Name, e);
}
}
2015-02-07 20:17:49 +08:00
private static List<KeyValuePair<PluginPair, IInstantQuery>> LoadInstantSearches()
{
2015-02-04 23:16:41 +08:00
if (instantSearches != null) return instantSearches;
2015-02-07 20:17:49 +08:00
instantSearches = AssemblyHelper.LoadPluginInterfaces<IInstantQuery>();
return instantSearches;
}
2014-12-26 19:36:43 +08:00
/// <summary>
/// get specified plugin, return null if not found
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static PluginPair GetPlugin(string id)
{
return plugins.FirstOrDefault(o => o.Metadata.ID == id);
2014-12-26 19:36:43 +08:00
}
2015-02-05 18:43:05 +08:00
private static List<KeyValuePair<PluginPair, IExclusiveQuery>> LoadExclusiveSearchPlugins()
2015-02-05 18:43:05 +08:00
{
if (exclusiveSearchPlugins != null) return exclusiveSearchPlugins;
2015-02-07 20:17:49 +08:00
exclusiveSearchPlugins = AssemblyHelper.LoadPluginInterfaces<IExclusiveQuery>();
2015-02-05 18:43:05 +08:00
return exclusiveSearchPlugins;
}
private static PluginPair GetExclusivePlugin(Query query)
2015-02-05 18:43:05 +08:00
{
2015-02-05 22:20:42 +08:00
KeyValuePair<PluginPair, IExclusiveQuery> plugin = LoadExclusiveSearchPlugins().FirstOrDefault(o => o.Value.IsExclusiveQuery((query)));
return plugin.Key;
}
private static PluginPair GetActionKeywordPlugin(Query query)
2015-02-05 22:20:42 +08:00
{
2015-11-01 00:02:56 +08:00
//if a query doesn't contain a vaild action keyword, it should not be a action keword plugin query
if (string.IsNullOrEmpty(query.ActionKeyword)) return null;
2015-02-08 00:06:22 +08:00
2015-11-01 00:02:56 +08:00
PluginPair actionKeywordPluginPair = AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.ActionKeyword);
2015-02-06 18:13:22 +08:00
if (actionKeywordPluginPair != null)
2015-02-05 22:20:42 +08:00
{
var customizedPluginConfig = UserSettingStorage.Instance.
2015-02-06 18:13:22 +08:00
CustomizedPluginConfigs.FirstOrDefault(o => o.ID == actionKeywordPluginPair.Metadata.ID);
if (customizedPluginConfig != null && customizedPluginConfig.Disabled)
2015-02-05 22:20:42 +08:00
{
2015-02-06 18:13:22 +08:00
return null;
2015-02-05 22:20:42 +08:00
}
2015-02-06 18:13:22 +08:00
return actionKeywordPluginPair;
2015-02-05 22:20:42 +08:00
}
2015-02-05 18:43:05 +08:00
return null;
}
2015-02-05 22:20:42 +08:00
private static List<PluginPair> GetExclusivePlugins(Query query)
{
List<PluginPair> pluginPairs = new List<PluginPair>();
var exclusivePluginPair = GetExclusivePlugin(query) ??
GetActionKeywordPlugin(query);
if (exclusivePluginPair != null)
{
pluginPairs.Add(exclusivePluginPair);
}
return pluginPairs;
}
private static List<PluginPair> GetGenericPlugins()
{
return plugins.Where(o => IsGenericPlugin(o.Metadata)).ToList();
}
private static bool IsExclusivePluginQuery(Query query)
2015-02-05 22:20:42 +08:00
{
return GetExclusivePlugin(query) != null || GetActionKeywordPlugin(query) != null;
}
public static List<Result> GetPluginContextMenus(Result result)
{
List<Result> contextContextMenus = new List<Result>();
if (contextMenuPlugins == null)
{
contextMenuPlugins = AssemblyHelper.LoadPluginInterfaces<IContextMenu>();
}
var contextMenuPlugin = contextMenuPlugins.FirstOrDefault(o => o.Key.Metadata.ID == result.PluginID);
if (contextMenuPlugin.Value != null)
{
try
{
return contextMenuPlugin.Value.LoadContextMenus(result);
}
catch (System.Exception e)
{
Log.Error(string.Format("Couldn't load plugin context menus {0}: {1}", contextMenuPlugin.Key.Metadata.Name, e.Message));
#if (DEBUG)
{
throw;
}
#endif
}
}
return contextContextMenus;
}
2014-12-26 19:36:43 +08:00
}
}