PowerToys/Wox.Core/Plugin/PluginManager.cs

308 lines
11 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.Threading;
using Wox.Core.Resource;
using Wox.Core.UserSettings;
using Wox.Infrastructure;
2015-11-09 09:32:33 +08:00
using Wox.Infrastructure.Exception;
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";
2016-01-08 09:13:36 +08:00
private static IEnumerable<PluginPair> _contextMenuPlugins;
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>
2016-01-08 09:13:36 +08:00
private static readonly List<string> PluginDirectories = new List<string>();
2014-12-26 19:36:43 +08:00
public static IEnumerable<PluginPair> AllPlugins { get; private set; }
2014-12-27 12:34:51 +08:00
2016-01-08 09:13:36 +08:00
public static readonly List<PluginPair> GlobalPlugins = new List<PluginPair>();
public static readonly Dictionary<string, PluginPair> NonGlobalPlugins = new Dictionary<string, PluginPair>();
private static IEnumerable<PluginPair> InstantQueryPlugins { get; set; }
public static IPublicAPI API { private set; get; }
2016-01-08 09:13:36 +08:00
public static readonly string PluginDirectory = Path.Combine(WoxDirectroy.Executable, DirectoryName);
2014-12-26 19:36:43 +08:00
private static void SetupPluginDirectories()
{
2016-01-08 09:13:36 +08:00
PluginDirectories.Add(PluginDirectory);
MakesurePluginDirectoriesExist();
}
2014-12-26 19:36:43 +08:00
private static void MakesurePluginDirectoriesExist()
{
2016-01-08 09:13:36 +08:00
foreach (string pluginDirectory in PluginDirectories)
2014-12-26 19:36:43 +08:00
{
if (!Directory.Exists(pluginDirectory))
{
try
{
Directory.CreateDirectory(pluginDirectory);
}
catch (Exception e)
{
Log.Error(e);
}
2014-12-26 19:36:43 +08:00
}
}
}
/// <summary>
/// Load and init all Wox plugins
/// </summary>
///
public static void Initialize()
2014-12-26 19:36:43 +08:00
{
2014-12-29 23:02:50 +08:00
SetupPluginDirectories();
2014-12-26 19:36:43 +08:00
2016-01-08 09:13:36 +08:00
var metadatas = PluginConfig.Parse(PluginDirectories);
AllPlugins = (new CSharpPluginLoader().LoadPlugin(metadatas)).
Concat(new JsonRPCPluginLoader<PythonPlugin>().LoadPlugin(metadatas));
2014-12-26 19:36:43 +08:00
}
2015-01-07 22:23:10 +08:00
public static void InitializePlugins(IPublicAPI api)
{
//load plugin i18n languages
ResourceMerger.UpdatePluginLanguages();
API = api;
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
{
var milliseconds = Stopwatch.Normal($"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 = milliseconds;
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 =>
{
2016-01-08 09:13:36 +08:00
_contextMenuPlugins = GetPluginsForInterface<IContextMenu>();
2015-11-07 11:50:15 +08:00
foreach (var plugin in AllPlugins)
{
if (IsGlobalPlugin(plugin.Metadata))
{
GlobalPlugins.Add(plugin);
}
else
{
foreach (string actionKeyword in plugin.Metadata.ActionKeywords)
{
NonGlobalPlugins[actionKeyword] = plugin;
}
}
}
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.TermSeperater }, StringSplitOptions.RemoveEmptyEntries);
var rawQuery = string.Join(Query.TermSeperater, terms);
2015-11-03 13:09:54 +08:00
var actionKeyword = string.Empty;
var search = rawQuery;
List<string> actionParameters = terms.ToList();
2015-11-03 13:09:54 +08:00
if (terms.Length == 0) return null;
2015-11-07 11:50:15 +08:00
if (NonGlobalPlugins.ContainsKey(terms[0]))
2015-11-01 00:02:56 +08:00
{
2015-11-03 13:09:54 +08:00
actionKeyword = terms[0];
actionParameters = terms.Skip(1).ToList();
search = string.Join(Query.TermSeperater, actionParameters.ToArray());
}
2015-11-03 13:09:54 +08:00
return new Query
{
Terms = terms,
RawQuery = rawQuery,
ActionKeyword = actionKeyword,
Search = search,
2015-11-03 13:09:54 +08:00
// Obsolete value initialisation
ActionName = actionKeyword,
ActionParameters = actionParameters
2015-11-03 13:09:54 +08:00
};
}
public static List<PluginPair> ValidPluginsForQuery(Query query)
{
if (NonGlobalPlugins.ContainsKey(query.ActionKeyword))
{
var plugin = NonGlobalPlugins[query.ActionKeyword];
return new List<PluginPair> { plugin };
}
else
{
return GlobalPlugins;
}
2014-12-26 22:51:04 +08:00
}
public static List<Result> QueryForPlugin(PluginPair pair, Query query)
2014-12-26 19:36:43 +08:00
{
var results = new List<Result>();
try
2014-12-26 19:36:43 +08:00
{
var milliseconds = Stopwatch.Normal($"Plugin.Query cost for {pair.Metadata.Name}", () =>
{
results = pair.Plugin.Query(query) ?? results;
results.ForEach(o =>
{
o.PluginDirectory = pair.Metadata.PluginDirectory;
o.PluginID = pair.Metadata.ID;
o.OriginQuery = query;
});
});
pair.QueryCount += 1;
pair.AvgQueryTime = pair.QueryCount == 1 ? milliseconds : (pair.AvgQueryTime + milliseconds) / 2;
}
catch (Exception e)
{
throw new WoxPluginException(pair.Metadata.Name, $"QueryForPlugin failed", e);
2014-12-26 19:36:43 +08:00
}
return results;
2014-12-26 19:36:43 +08:00
}
2015-11-06 09:19:13 +08:00
private static bool IsGlobalPlugin(PluginMetadata metadata)
2015-01-04 23:08:26 +08:00
{
return metadata.ActionKeywords.Contains(Query.GlobalPluginWildcardSign);
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 GetPluginForId(string id)
2014-12-26 19:36:43 +08:00
{
return AllPlugins.FirstOrDefault(o => o.Metadata.ID == id);
2014-12-26 19:36:43 +08:00
}
2015-02-05 18:43:05 +08:00
public static IEnumerable<PluginPair> GetPluginsForInterface<T>() where T : IFeatures
2015-02-05 22:20:42 +08:00
{
return AllPlugins.Where(p => p.Plugin is T);
2015-02-05 22:20:42 +08:00
}
public static List<Result> GetContextMenusForPlugin(Result result)
{
2016-01-08 09:13:36 +08:00
var pluginPair = _contextMenuPlugins.FirstOrDefault(o => o.Metadata.ID == result.PluginID);
if (pluginPair != null)
{
var metadata = pluginPair.Metadata;
var plugin = (IContextMenu)pluginPair?.Plugin;
try
{
var results = plugin.LoadContextMenus(result);
foreach (var r in results)
{
r.PluginDirectory = metadata.PluginDirectory;
r.PluginID = metadata.ID;
r.OriginQuery = result.OriginQuery;
}
return results;
}
catch (Exception e)
{
Log.Error(new WoxPluginException(metadata.Name, "Couldn't load plugin context menus", e));
return new List<Result>();
}
}
else
{
return new List<Result>();
}
}
public static void UpdateActionKeywordForPlugin(PluginPair plugin, string oldActionKeyword, string newActionKeyword)
{
var actionKeywords = plugin.Metadata.ActionKeywords;
if (string.IsNullOrEmpty(newActionKeyword))
{
string msg = InternationalizationManager.Instance.GetTranslation("newActionKeywordsCannotBeEmpty");
throw new WoxPluginException(plugin.Metadata.Name, msg);
}
// do nothing if they are same
if (oldActionKeyword == newActionKeyword) return;
if (NonGlobalPlugins.ContainsKey(newActionKeyword))
{
string msg = InternationalizationManager.Instance.GetTranslation("newActionKeywordsHasBeenAssigned");
throw new WoxPluginException(plugin.Metadata.Name, msg);
}
// add new action keyword
if (string.IsNullOrEmpty(oldActionKeyword))
{
actionKeywords.Add(newActionKeyword);
if (newActionKeyword == Query.GlobalPluginWildcardSign)
{
GlobalPlugins.Add(plugin);
}
else
{
NonGlobalPlugins[newActionKeyword] = plugin;
}
}
// update existing action keyword
else
{
int index = actionKeywords.IndexOf(oldActionKeyword);
actionKeywords[index] = newActionKeyword;
if (oldActionKeyword == Query.GlobalPluginWildcardSign)
{
GlobalPlugins.Remove(plugin);
}
else
{
NonGlobalPlugins.Remove(oldActionKeyword);
}
if (newActionKeyword == Query.GlobalPluginWildcardSign)
{
GlobalPlugins.Add(plugin);
}
else
{
NonGlobalPlugins[newActionKeyword] = plugin;
}
}
}
2014-12-26 19:36:43 +08:00
}
}