2019-08-04 19:47:05 +08:00
|
|
|
|
using System;
|
2020-02-22 05:12:58 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2015-11-03 13:09:54 +08:00
|
|
|
|
using System.Collections.Generic;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2016-05-06 04:15:13 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2014-12-29 22:09:54 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2016-05-03 05:37:01 +08:00
|
|
|
|
using Wox.Infrastructure.Storage;
|
2016-06-19 23:18:43 +08:00
|
|
|
|
using Wox.Infrastructure.UserSettings;
|
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
|
|
|
|
|
{
|
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-04-21 08:53:21 +08:00
|
|
|
|
public static List<PluginPair> AllPlugins { get; private set; }
|
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>();
|
2015-11-06 04:44:14 +08:00
|
|
|
|
|
2015-11-02 06:59:56 +08:00
|
|
|
|
public static IPublicAPI API { private set; get; }
|
2016-06-21 07:14:32 +08:00
|
|
|
|
|
2016-05-12 09:45:35 +08:00
|
|
|
|
// todo happlebao, this should not be public, the indicator function should be embeded
|
|
|
|
|
public static PluginsSettings Settings;
|
2016-05-05 23:08:44 +08:00
|
|
|
|
private static List<PluginMetadata> _metadatas;
|
2016-06-22 07:42:24 +08:00
|
|
|
|
private static readonly string[] Directories = { Constant.PreinstalledDirectory, Constant.PluginsDirectory };
|
2015-11-02 06:59:56 +08:00
|
|
|
|
|
2016-04-27 09:15:53 +08:00
|
|
|
|
private static void ValidateUserDirectory()
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2016-06-22 07:42:24 +08:00
|
|
|
|
if (!Directory.Exists(Constant.PluginsDirectory))
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2016-06-22 07:42:24 +08:00
|
|
|
|
Directory.CreateDirectory(Constant.PluginsDirectory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void DeletePythonBinding()
|
|
|
|
|
{
|
|
|
|
|
const string binding = "wox.py";
|
|
|
|
|
var directory = Constant.PluginsDirectory;
|
|
|
|
|
foreach (var subDirectory in Directory.GetDirectories(directory))
|
|
|
|
|
{
|
|
|
|
|
var path = Path.Combine(subDirectory, binding);
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
}
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 05:37:01 +08:00
|
|
|
|
public static void Save()
|
|
|
|
|
{
|
|
|
|
|
foreach (var plugin in AllPlugins)
|
|
|
|
|
{
|
|
|
|
|
var savable = plugin.Plugin as ISavable;
|
|
|
|
|
savable?.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-06 09:49:47 +08:00
|
|
|
|
public static void ReloadData()
|
|
|
|
|
{
|
|
|
|
|
foreach(var plugin in AllPlugins)
|
|
|
|
|
{
|
|
|
|
|
var reloadablePlugin = plugin.Plugin as IReloadable;
|
|
|
|
|
reloadablePlugin?.ReloadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 23:08:44 +08:00
|
|
|
|
static PluginManager()
|
|
|
|
|
{
|
|
|
|
|
ValidateUserDirectory();
|
2016-06-22 07:42:24 +08:00
|
|
|
|
// force old plugins use new python binding
|
|
|
|
|
DeletePythonBinding();
|
2016-05-05 23:08:44 +08:00
|
|
|
|
}
|
2016-05-10 08:08:54 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// because InitializePlugins needs API, so LoadPlugins needs to be called first
|
|
|
|
|
/// todo happlebao The API should be removed
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="settings"></param>
|
|
|
|
|
public static void LoadPlugins(PluginsSettings settings)
|
2016-03-28 08:09:40 +08:00
|
|
|
|
{
|
2016-05-10 08:08:54 +08:00
|
|
|
|
_metadatas = PluginConfig.Parse(Directories);
|
2016-05-12 09:45:35 +08:00
|
|
|
|
Settings = settings;
|
|
|
|
|
Settings.UpdatePluginSettings(_metadatas);
|
|
|
|
|
AllPlugins = PluginsLoader.Plugins(_metadatas, Settings);
|
2016-05-10 08:08:54 +08:00
|
|
|
|
}
|
2020-02-22 05:12:58 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Call initialize for all plugins
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>return the list of failed to init plugins or null for none</returns>
|
2016-05-10 08:08:54 +08:00
|
|
|
|
public static void InitializePlugins(IPublicAPI api)
|
|
|
|
|
{
|
2016-03-28 08:09:40 +08:00
|
|
|
|
API = api;
|
2020-02-22 05:12:58 +08:00
|
|
|
|
var failedPlugins = new ConcurrentQueue<PluginPair>();
|
2016-05-06 04:15:13 +08:00
|
|
|
|
Parallel.ForEach(AllPlugins, pair =>
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2020-02-22 05:12:58 +08:00
|
|
|
|
try
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2020-02-22 05:12:58 +08:00
|
|
|
|
var milliseconds = Stopwatch.Debug($"|PluginManager.InitializePlugins|Init method time cost for <{pair.Metadata.Name}>", () =>
|
2015-01-04 23:08:26 +08:00
|
|
|
|
{
|
2020-02-22 05:12:58 +08:00
|
|
|
|
pair.Plugin.Init(new PluginInitContext
|
|
|
|
|
{
|
|
|
|
|
CurrentPluginMetadata = pair.Metadata,
|
|
|
|
|
API = API
|
|
|
|
|
});
|
2015-11-05 05:35:04 +08:00
|
|
|
|
});
|
2020-02-22 05:12:58 +08:00
|
|
|
|
pair.Metadata.InitTime += milliseconds;
|
|
|
|
|
Log.Info($"|PluginManager.InitializePlugins|Total init cost for <{pair.Metadata.Name}> is <{pair.Metadata.InitTime}ms>");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Exception(nameof(PluginManager), $"Fail to Init plugin: {pair.Metadata.Name}", e);
|
2020-02-22 17:02:07 +08:00
|
|
|
|
pair.Metadata.Disabled = true;
|
2020-02-22 05:12:58 +08:00
|
|
|
|
failedPlugins.Enqueue(pair);
|
|
|
|
|
}
|
2016-05-06 04:15:13 +08:00
|
|
|
|
});
|
2015-01-27 22:59:03 +08:00
|
|
|
|
|
2016-05-06 04:15:13 +08:00
|
|
|
|
_contextMenuPlugins = GetPluginsForInterface<IContextMenu>();
|
|
|
|
|
foreach (var plugin in AllPlugins)
|
2015-02-04 23:16:41 +08:00
|
|
|
|
{
|
2016-05-06 04:15:13 +08:00
|
|
|
|
if (IsGlobalPlugin(plugin.Metadata))
|
|
|
|
|
GlobalPlugins.Add(plugin);
|
2019-08-04 19:47:05 +08:00
|
|
|
|
|
|
|
|
|
// Plugins may have multiple ActionKeywords, eg. WebSearch
|
|
|
|
|
plugin.Metadata.ActionKeywords.Where(x => x != Query.GlobalPluginWildcardSign)
|
|
|
|
|
.ToList()
|
|
|
|
|
.ForEach(x => NonGlobalPlugins[x] = plugin);
|
2016-05-06 04:15:13 +08:00
|
|
|
|
}
|
2016-05-05 23:08:44 +08:00
|
|
|
|
|
2020-02-22 05:12:58 +08:00
|
|
|
|
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", "", false);
|
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 08:09:40 +08:00
|
|
|
|
public static List<PluginPair> ValidPluginsForQuery(Query query)
|
2015-11-02 01:25:44 +08:00
|
|
|
|
{
|
2016-03-28 08:09:40 +08:00
|
|
|
|
if (NonGlobalPlugins.ContainsKey(query.ActionKeyword))
|
2015-11-02 01:25:44 +08:00
|
|
|
|
{
|
2016-03-28 08:09:40 +08:00
|
|
|
|
var plugin = NonGlobalPlugins[query.ActionKeyword];
|
|
|
|
|
return new List<PluginPair> { plugin };
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return GlobalPlugins;
|
2015-01-26 17:46:55 +08:00
|
|
|
|
}
|
2014-12-26 22:51:04 +08:00
|
|
|
|
}
|
2016-04-21 08:53:21 +08:00
|
|
|
|
|
2016-03-28 08:09:40 +08:00
|
|
|
|
public static List<Result> QueryForPlugin(PluginPair pair, Query query)
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2015-11-02 06:59:56 +08:00
|
|
|
|
try
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2019-11-29 08:05:48 +08:00
|
|
|
|
List<Result> results = null;
|
2016-05-05 23:08:44 +08:00
|
|
|
|
var metadata = pair.Metadata;
|
2017-01-24 08:24:20 +08:00
|
|
|
|
var milliseconds = Stopwatch.Debug($"|PluginManager.QueryForPlugin|Cost for {metadata.Name}", () =>
|
2016-05-05 23:08:44 +08:00
|
|
|
|
{
|
2019-11-29 08:05:48 +08:00
|
|
|
|
results = pair.Plugin.Query(query) ?? new List<Result>();
|
2016-05-05 23:08:44 +08:00
|
|
|
|
UpdatePluginMetadata(results, metadata, query);
|
|
|
|
|
});
|
2016-05-22 12:30:38 +08:00
|
|
|
|
metadata.QueryCount += 1;
|
|
|
|
|
metadata.AvgQueryTime = metadata.QueryCount == 1 ? milliseconds : (metadata.AvgQueryTime + milliseconds) / 2;
|
2017-01-30 08:26:11 +08:00
|
|
|
|
return results;
|
2015-11-02 06:59:56 +08:00
|
|
|
|
}
|
2015-11-09 11:20:02 +08:00
|
|
|
|
catch (Exception e)
|
2015-11-02 06:59:56 +08:00
|
|
|
|
{
|
2017-01-30 08:26:11 +08:00
|
|
|
|
Log.Exception($"|PluginManager.QueryForPlugin|Exception for plugin <{pair.Metadata.Name}> when query <{query}>", e);
|
|
|
|
|
return new List<Result>();
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 23:08:44 +08:00
|
|
|
|
public static void UpdatePluginMetadata(List<Result> results, PluginMetadata metadata, Query query)
|
|
|
|
|
{
|
|
|
|
|
foreach (var r in results)
|
|
|
|
|
{
|
|
|
|
|
r.PluginDirectory = metadata.PluginDirectory;
|
|
|
|
|
r.PluginID = metadata.ID;
|
|
|
|
|
r.OriginQuery = query;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 09:19:13 +08:00
|
|
|
|
private static bool IsGlobalPlugin(PluginMetadata metadata)
|
2015-01-04 23:08:26 +08:00
|
|
|
|
{
|
2015-11-06 04:44:14 +08:00
|
|
|
|
return metadata.ActionKeywords.Contains(Query.GlobalPluginWildcardSign);
|
2015-01-04 23:08:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// get specified plugin, return null if not found
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
2015-11-06 04:44:14 +08:00
|
|
|
|
public static PluginPair GetPluginForId(string id)
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2015-11-02 07:32:17 +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
|
|
|
|
|
2015-11-06 04:44:14 +08:00
|
|
|
|
public static IEnumerable<PluginPair> GetPluginsForInterface<T>() where T : IFeatures
|
2015-02-05 22:20:42 +08:00
|
|
|
|
{
|
2015-11-06 04:44:14 +08:00
|
|
|
|
return AllPlugins.Where(p => p.Plugin is T);
|
2015-02-05 22:20:42 +08:00
|
|
|
|
}
|
2015-02-07 23:49:46 +08:00
|
|
|
|
|
2015-11-06 04:44:14 +08:00
|
|
|
|
public static List<Result> GetContextMenusForPlugin(Result result)
|
2015-02-07 23:49:46 +08:00
|
|
|
|
{
|
2016-01-08 09:13:36 +08:00
|
|
|
|
var pluginPair = _contextMenuPlugins.FirstOrDefault(o => o.Metadata.ID == result.PluginID);
|
2016-03-26 09:20:42 +08:00
|
|
|
|
if (pluginPair != null)
|
2015-02-07 23:49:46 +08:00
|
|
|
|
{
|
2016-03-26 09:20:42 +08:00
|
|
|
|
var metadata = pluginPair.Metadata;
|
2016-04-26 03:49:56 +08:00
|
|
|
|
var plugin = (IContextMenu)pluginPair.Plugin;
|
2016-03-26 09:20:42 +08:00
|
|
|
|
|
2015-02-07 23:49:46 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-03-26 09:20:42 +08:00
|
|
|
|
var results = plugin.LoadContextMenus(result);
|
|
|
|
|
foreach (var r in results)
|
|
|
|
|
{
|
|
|
|
|
r.PluginDirectory = metadata.PluginDirectory;
|
|
|
|
|
r.PluginID = metadata.ID;
|
|
|
|
|
r.OriginQuery = result.OriginQuery;
|
|
|
|
|
}
|
|
|
|
|
return results;
|
2015-02-07 23:49:46 +08:00
|
|
|
|
}
|
2015-11-09 11:20:02 +08:00
|
|
|
|
catch (Exception e)
|
2015-02-07 23:49:46 +08:00
|
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
|
Log.Exception($"|PluginManager.GetContextMenusForPlugin|Can't load context menus for plugin <{metadata.Name}>", e);
|
2016-03-26 09:20:42 +08:00
|
|
|
|
return new List<Result>();
|
2015-02-07 23:49:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-26 09:20:42 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new List<Result>();
|
|
|
|
|
}
|
2015-02-07 23:49:46 +08:00
|
|
|
|
|
|
|
|
|
}
|
2015-11-09 11:20:02 +08:00
|
|
|
|
|
2016-06-21 07:14:32 +08:00
|
|
|
|
public static bool ActionKeywordRegistered(string actionKeyword)
|
2015-11-09 11:20:02 +08:00
|
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
|
if (actionKeyword != Query.GlobalPluginWildcardSign &&
|
|
|
|
|
NonGlobalPlugins.ContainsKey(actionKeyword))
|
2015-11-09 11:20:02 +08:00
|
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
|
return true;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
}
|
2016-06-21 07:14:32 +08:00
|
|
|
|
else
|
2015-11-09 11:20:02 +08:00
|
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
|
return false;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
}
|
2016-06-21 07:14:32 +08:00
|
|
|
|
}
|
2015-11-09 11:20:02 +08:00
|
|
|
|
|
2016-06-23 07:03:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// used to add action keyword for multiple action keyword plugin
|
|
|
|
|
/// e.g. web search
|
|
|
|
|
/// </summary>
|
2016-06-21 07:14:32 +08:00
|
|
|
|
public static void AddActionKeyword(string id, string newActionKeyword)
|
|
|
|
|
{
|
|
|
|
|
var plugin = GetPluginForId(id);
|
|
|
|
|
if (newActionKeyword == Query.GlobalPluginWildcardSign)
|
2015-11-09 11:20:02 +08:00
|
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
|
GlobalPlugins.Add(plugin);
|
2015-11-09 11:20:02 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
|
NonGlobalPlugins[newActionKeyword] = plugin;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
}
|
2016-06-21 07:14:32 +08:00
|
|
|
|
plugin.Metadata.ActionKeywords.Add(newActionKeyword);
|
2015-11-09 11:20:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-23 07:03:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// used to add action keyword for multiple action keyword plugin
|
|
|
|
|
/// e.g. web search
|
|
|
|
|
/// </summary>
|
2016-06-21 07:14:32 +08:00
|
|
|
|
public static void RemoveActionKeyword(string id, string oldActionkeyword)
|
|
|
|
|
{
|
|
|
|
|
var plugin = GetPluginForId(id);
|
2019-08-04 19:44:56 +08:00
|
|
|
|
if (oldActionkeyword == Query.GlobalPluginWildcardSign
|
|
|
|
|
&& // Plugins may have multiple ActionKeywords that are global, eg. WebSearch
|
|
|
|
|
plugin.Metadata.ActionKeywords
|
|
|
|
|
.Where(x => x == Query.GlobalPluginWildcardSign)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Count == 1)
|
2016-06-21 07:14:32 +08:00
|
|
|
|
{
|
|
|
|
|
GlobalPlugins.Remove(plugin);
|
|
|
|
|
}
|
2019-08-04 19:44:56 +08:00
|
|
|
|
|
|
|
|
|
if(oldActionkeyword != Query.GlobalPluginWildcardSign)
|
2016-06-21 07:14:32 +08:00
|
|
|
|
NonGlobalPlugins.Remove(oldActionkeyword);
|
2019-08-04 19:44:56 +08:00
|
|
|
|
|
|
|
|
|
|
2016-06-21 07:14:32 +08:00
|
|
|
|
plugin.Metadata.ActionKeywords.Remove(oldActionkeyword);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ReplaceActionKeyword(string id, string oldActionKeyword, string newActionKeyword)
|
|
|
|
|
{
|
|
|
|
|
if (oldActionKeyword != newActionKeyword)
|
|
|
|
|
{
|
|
|
|
|
AddActionKeyword(id, newActionKeyword);
|
|
|
|
|
RemoveActionKeyword(id, oldActionKeyword);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|