PowerToys/Wox.Core/Plugin/PluginManager.cs

283 lines
10 KiB
C#
Raw Normal View History

2015-11-03 13:09:54 +08:00
using System;
using System.Collections.Generic;
2014-12-26 19:36:43 +08:00
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
2015-11-03 13:09:54 +08:00
using System.Windows.Documents;
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;
namespace Wox.Core.Plugin
{
/// <summary>
/// The entry for managing Wox plugins
/// </summary>
public static class PluginManager
{
public const string DirectoryName = "Plugins";
private static List<PluginMetadata> pluginMetadatas;
2015-11-03 08:34:27 +08:00
private static IEnumerable<PluginPair> instantQueryPlugins;
private static IEnumerable<PluginPair> exclusiveSearchPlugins;
2015-11-03 02:52:34 +08:00
private static IEnumerable<PluginPair> contextMenuPlugins;
private static List<PluginPair> plugins;
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-11-03 09:33:53 +08:00
public static IEnumerable<PluginPair> AllPlugins
2014-12-27 12:34:51 +08:00
{
get { return plugins; }
private set { plugins = value.OrderBy(o => o.Metadata.Name).ToList(); }
2014-12-27 12:34:51 +08:00
}
public static IPublicAPI API { private set; get; }
2015-01-20 22:33:45 +08:00
public static string PluginDirectory
2014-12-26 19:36:43 +08:00
{
get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), DirectoryName); }
2014-12-26 19:36:43 +08:00
}
private static void SetupPluginDirectories()
{
pluginDirectories.Add(PluginDirectory);
MakesurePluginDirectoriesExist();
}
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
pluginMetadatas = PluginConfig.Parse(pluginDirectories);
AllPlugins = (new CSharpPluginLoader().LoadPlugin(pluginMetadatas)).
2015-11-03 09:33:53 +08:00
Concat(new JsonRPCPluginLoader<PythonPlugin>().LoadPlugin(pluginMetadatas));
2014-12-26 19:36:43 +08:00
2015-01-07 22:23:10 +08:00
//load plugin i18n languages
ResourceMerger.ApplyPluginLanguages();
foreach (PluginPair pluginPair in AllPlugins)
2014-12-26 19:36:43 +08:00
{
PluginPair pair = pluginPair;
2015-01-04 23:08:26 +08:00
ThreadPool.QueueUserWorkItem(o =>
2014-12-26 19:36:43 +08:00
{
2015-11-02 08:04:05 +08:00
using (var time = new Timeit($"Plugin init: {pair.Metadata.Name}"))
2015-01-04 23:08:26 +08:00
{
2015-11-02 08:04:05 +08:00
pair.Plugin.Init(new PluginInitContext
{
CurrentPluginMetadata = pair.Metadata,
Proxy = HttpProxy.Instance,
API = API
});
pair.InitTime = time.Current;
}
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 =>
{
2015-11-03 09:33:53 +08:00
instantQueryPlugins = GetPlugins<IInstantQuery>();
exclusiveSearchPlugins = GetPlugins<IExclusiveQuery>();
contextMenuPlugins = GetPlugins<IContextMenu>();
2015-02-04 23:16:41 +08:00
});
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);
}
2015-11-03 13:09:54 +08:00
public static Query QueryInit(string text) //todo is that possible to move it into type Query?
2014-12-26 22:51:04 +08:00
{
2015-11-03 13:09:54 +08:00
// replace multiple white spaces with one white space
var terms = text.Split(new[] { Query.Seperater }, StringSplitOptions.RemoveEmptyEntries);
var rawQuery = string.Join(Query.Seperater, terms.ToArray());
var actionKeyword = string.Empty;
var search = rawQuery;
IEnumerable<string> actionParameters = terms;
if (terms.Length == 0) return null;
if (IsVailldActionKeyword(terms[0]))
2015-11-01 00:02:56 +08:00
{
2015-11-03 13:09:54 +08:00
actionKeyword = terms[0];
2015-11-01 00:02:56 +08:00
}
2015-11-03 13:09:54 +08:00
if (!string.IsNullOrEmpty(actionKeyword))
{
2015-11-03 13:09:54 +08:00
actionParameters = terms.Skip(1);
search = string.Join(Query.Seperater, actionParameters.ToArray());
}
2015-11-03 13:09:54 +08:00
return new Query
{
Terms = terms, RawQuery = rawQuery, ActionKeyword = actionKeyword, Search = search,
// Obsolete value initialisation
ActionName = actionKeyword, ActionParameters = actionParameters.ToList()
};
}
2015-11-03 13:09:54 +08:00
public static void QueryForAllPlugins(Query query)
{
2015-11-03 08:34:27 +08:00
var pluginPairs = GetNonSystemPlugin(query) != null ?
new List<PluginPair> { GetNonSystemPlugin(query) } : GetSystemPlugins();
foreach (var plugin in pluginPairs)
{
var customizedPluginConfig = UserSettingStorage.Instance.
CustomizedPluginConfigs.FirstOrDefault(o => o.ID == plugin.Metadata.ID);
2015-11-02 08:09:42 +08:00
if (customizedPluginConfig != null && customizedPluginConfig.Disabled) return;
2015-11-03 08:34:27 +08:00
if (IsInstantQueryPlugin(plugin))
{
2015-11-02 08:09:42 +08:00
using (new Timeit($"Plugin {plugin.Metadata.Name} is executing instant search"))
{
QueryForPlugin(plugin, query);
}
}
else
{
ThreadPool.QueueUserWorkItem(state =>
{
QueryForPlugin(plugin, query);
});
}
}
2014-12-26 22:51:04 +08:00
}
private static void QueryForPlugin(PluginPair pair, Query query)
2014-12-26 19:36:43 +08:00
{
try
2014-12-26 19:36:43 +08:00
{
2015-11-03 08:34:27 +08:00
using (var time = new Timeit($"Query For {pair.Metadata.Name}"))
{
2015-11-02 08:04:05 +08:00
var results = pair.Plugin.Query(query) ?? new List<Result>();
results.ForEach(o => { o.PluginID = pair.Metadata.ID; });
var seconds = time.Current;
pair.QueryCount += 1;
pair.AvgQueryTime = pair.QueryCount == 1 ? seconds : (pair.AvgQueryTime + seconds) / 2;
API.PushResults(query, pair.Metadata, results);
}
}
catch (System.Exception e)
{
throw new WoxPluginException(pair.Metadata.Name, e);
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-11-03 09:33:53 +08:00
if (string.IsNullOrEmpty(actionKeyword) || actionKeyword == Query.WildcardSign) return false;
PluginPair pair = AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == actionKeyword);
if (pair == null) return false;
2015-11-02 08:09:42 +08:00
var customizedPluginConfig = UserSettingStorage.Instance.
CustomizedPluginConfigs.FirstOrDefault(o => o.ID == pair.Metadata.ID);
return customizedPluginConfig == null || !customizedPluginConfig.Disabled;
2014-12-26 19:36:43 +08:00
}
public static bool IsSystemPlugin(PluginMetadata metadata)
2015-01-04 23:08:26 +08:00
{
2015-11-02 08:09:42 +08:00
return metadata.ActionKeyword == Query.WildcardSign;
2015-01-04 23:08:26 +08:00
}
2015-11-03 08:34:27 +08:00
private static bool IsInstantQueryPlugin(PluginPair plugin)
{
2015-11-03 08:34:27 +08:00
//any plugin that takes more than 200ms for AvgQueryTime won't be treated as IInstantQuery plugin anymore.
return plugin.AvgQueryTime < 200 &&
2015-11-03 09:33:53 +08:00
plugin.Plugin is IInstantQuery &&
instantQueryPlugins.Any(p => p.Metadata.ID == plugin.Metadata.ID);
}
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 AllPlugins.FirstOrDefault(o => o.Metadata.ID == id);
2014-12-26 19:36:43 +08:00
}
2015-02-05 18:43:05 +08:00
2015-11-03 03:27:46 +08:00
public static IEnumerable<PluginPair> GetPlugins<T>() where T : IFeatures
2015-11-03 02:13:53 +08:00
{
2015-11-03 06:15:06 +08:00
return AllPlugins.Where(p => p.Plugin is T);
2015-11-03 02:13:53 +08:00
}
private static PluginPair GetExclusivePlugin(Query query)
2015-02-05 18:43:05 +08:00
{
2015-11-03 09:33:53 +08:00
return exclusiveSearchPlugins.FirstOrDefault(p => ((IExclusiveQuery)p.Plugin).IsExclusiveQuery(query));
2015-02-05 22:20:42 +08:00
}
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
2015-11-03 08:34:27 +08:00
if (string.IsNullOrEmpty(query.ActionKeyword)) return null;
return AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.ActionKeyword);
2015-02-05 18:43:05 +08:00
}
2015-02-05 22:20:42 +08:00
private static PluginPair GetNonSystemPlugin(Query query)
{
return GetExclusivePlugin(query) ?? GetActionKeywordPlugin(query);
}
private static List<PluginPair> GetSystemPlugins()
2015-02-05 22:20:42 +08:00
{
return AllPlugins.Where(o => IsSystemPlugin(o.Metadata)).ToList();
2015-02-05 22:20:42 +08:00
}
public static List<Result> GetPluginContextMenus(Result result)
{
2015-11-03 02:13:53 +08:00
var pluginPair = contextMenuPlugins.FirstOrDefault(o => o.Metadata.ID == result.PluginID);
var plugin = (IContextMenu)pluginPair?.Plugin;
if (plugin != null)
{
try
{
2015-11-03 02:13:53 +08:00
return plugin.LoadContextMenus(result);
}
catch (System.Exception e)
{
2015-11-03 02:13:53 +08:00
Log.Error($"Couldn't load plugin context menus {pluginPair.Metadata.Name}: {e.Message}");
#if (DEBUG)
{
throw;
}
#endif
}
}
2015-11-03 02:13:53 +08:00
return new List<Result>();
}
2014-12-26 19:36:43 +08:00
}
}