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-02-07 20:17:49 +08:00
|
|
|
|
using Wox.Core.i18n;
|
2015-01-07 22:23:10 +08:00
|
|
|
|
using Wox.Core.UI;
|
2015-01-05 22:41:17 +08:00
|
|
|
|
using Wox.Core.UserSettings;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2015-11-09 09:32:33 +08:00
|
|
|
|
using Wox.Infrastructure.Exception;
|
2014-12-29 22:09:54 +08:00
|
|
|
|
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
|
|
|
|
|
{
|
2015-11-02 10:49:38 +08:00
|
|
|
|
public const string DirectoryName = "Plugins";
|
2015-11-03 02:52:34 +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>
|
|
|
|
|
private static List<string> pluginDirectories = new List<string>();
|
|
|
|
|
|
2015-11-06 04:44:14 +08:00
|
|
|
|
public static IEnumerable<PluginPair> AllPlugins { get; private set; }
|
2014-12-27 12:34:51 +08:00
|
|
|
|
|
2015-11-07 11:50:15 +08:00
|
|
|
|
public static List<PluginPair> GlobalPlugins { get; } = new List<PluginPair>();
|
2015-11-09 09:32:33 +08:00
|
|
|
|
public static Dictionary<string, PluginPair> NonGlobalPlugins { get; set; } = new Dictionary<string, PluginPair>();
|
2015-11-06 04:44:14 +08:00
|
|
|
|
|
|
|
|
|
private static IEnumerable<PluginPair> InstantQueryPlugins { get; set; }
|
2015-11-02 06:59:56 +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
|
|
|
|
{
|
2015-11-02 10:49:38 +08:00
|
|
|
|
get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), DirectoryName); }
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-02 06:59:56 +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))
|
|
|
|
|
{
|
2014-12-29 22:09:54 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(pluginDirectory);
|
|
|
|
|
}
|
2015-11-09 11:20:02 +08:00
|
|
|
|
catch (Exception e)
|
2014-12-29 22:09:54 +08:00
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
Log.Error(e);
|
2014-12-29 22:09:54 +08:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
if (api == null) throw new WoxFatalException("api is null");
|
2014-12-29 23:02:50 +08:00
|
|
|
|
|
|
|
|
|
SetupPluginDirectories();
|
2014-12-26 22:51:04 +08:00
|
|
|
|
API = api;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
|
2015-11-06 04:44:14 +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
|
|
|
|
//load plugin i18n languages
|
|
|
|
|
ResourceMerger.ApplyPluginLanguages();
|
|
|
|
|
|
2015-11-02 07:32:17 +08:00
|
|
|
|
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-05 05:49:36 +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
|
|
|
|
|
});
|
2015-11-05 05:35:04 +08:00
|
|
|
|
});
|
|
|
|
|
pair.InitTime = milliseconds;
|
2015-02-07 20:17:49 +08:00
|
|
|
|
InternationalizationManager.Instance.UpdatePluginMetadataTranslations(pair);
|
2015-01-08 22:49:42 +08:00
|
|
|
|
});
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
2015-01-27 22:59:03 +08:00
|
|
|
|
|
2015-02-04 23:16:41 +08:00
|
|
|
|
ThreadPool.QueueUserWorkItem(o =>
|
|
|
|
|
{
|
2015-11-06 04:44:14 +08:00
|
|
|
|
InstantQueryPlugins = GetPluginsForInterface<IInstantQuery>();
|
|
|
|
|
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
|
2015-11-05 06:49:40 +08:00
|
|
|
|
var terms = text.Split(new[] { Query.TermSeperater }, StringSplitOptions.RemoveEmptyEntries);
|
2015-11-06 04:44:14 +08:00
|
|
|
|
var rawQuery = string.Join(Query.TermSeperater, terms);
|
2015-11-03 13:09:54 +08:00
|
|
|
|
var actionKeyword = string.Empty;
|
|
|
|
|
var search = rawQuery;
|
2015-11-06 04:44:14 +08:00
|
|
|
|
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];
|
2015-11-06 04:44:14 +08:00
|
|
|
|
actionParameters = terms.Skip(1).ToList();
|
2015-11-05 06:49:40 +08:00
|
|
|
|
search = string.Join(Query.TermSeperater, actionParameters.ToArray());
|
2015-11-02 01:25:44 +08:00
|
|
|
|
}
|
2015-11-03 13:09:54 +08:00
|
|
|
|
return new Query
|
|
|
|
|
{
|
2015-11-05 05:35:04 +08:00
|
|
|
|
Terms = terms,
|
|
|
|
|
RawQuery = rawQuery,
|
|
|
|
|
ActionKeyword = actionKeyword,
|
|
|
|
|
Search = search,
|
2015-11-03 13:09:54 +08:00
|
|
|
|
// Obsolete value initialisation
|
2015-11-05 05:35:04 +08:00
|
|
|
|
ActionName = actionKeyword,
|
2015-11-06 04:44:14 +08:00
|
|
|
|
ActionParameters = actionParameters
|
2015-11-03 13:09:54 +08:00
|
|
|
|
};
|
2015-11-02 01:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 13:09:54 +08:00
|
|
|
|
public static void QueryForAllPlugins(Query query)
|
2015-11-02 01:25:44 +08:00
|
|
|
|
{
|
2015-11-07 11:50:15 +08:00
|
|
|
|
var pluginPairs = NonGlobalPlugins.ContainsKey(query.ActionKeyword) ?
|
|
|
|
|
new List<PluginPair> { NonGlobalPlugins[query.ActionKeyword] } : GlobalPlugins;
|
2015-11-02 01:25:44 +08:00
|
|
|
|
foreach (var plugin in pluginPairs)
|
|
|
|
|
{
|
|
|
|
|
var customizedPluginConfig = UserSettingStorage.Instance.
|
|
|
|
|
CustomizedPluginConfigs.FirstOrDefault(o => o.ID == plugin.Metadata.ID);
|
2015-11-05 01:07:50 +08:00
|
|
|
|
if (customizedPluginConfig != null && customizedPluginConfig.Disabled) continue;
|
2015-11-03 08:34:27 +08:00
|
|
|
|
if (IsInstantQueryPlugin(plugin))
|
2015-11-02 01:25:44 +08:00
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
Stopwatch.Normal($"Instant QueryForPlugin for {plugin.Metadata.Name}", () =>
|
2015-11-02 01:25:44 +08:00
|
|
|
|
{
|
|
|
|
|
QueryForPlugin(plugin, query);
|
2015-11-05 05:35:04 +08:00
|
|
|
|
});
|
2015-11-02 01:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ThreadPool.QueueUserWorkItem(state =>
|
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
Stopwatch.Normal($"Normal QueryForPlugin for {plugin.Metadata.Name}", () =>
|
|
|
|
|
{
|
|
|
|
|
QueryForPlugin(plugin, query);
|
|
|
|
|
});
|
2015-11-02 01:25:44 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2015-01-26 17:46:55 +08:00
|
|
|
|
}
|
2014-12-26 22:51:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-02 06:59:56 +08:00
|
|
|
|
private static void 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
|
|
|
|
{
|
2015-11-05 05:35:04 +08:00
|
|
|
|
List<Result> results = new List<Result>();
|
2015-11-08 01:32:58 +08:00
|
|
|
|
var milliseconds = Stopwatch.Normal($"Plugin.Query cost for {pair.Metadata.Name}", () =>
|
2015-11-05 05:35:04 +08:00
|
|
|
|
{
|
|
|
|
|
results = pair.Plugin.Query(query) ?? results;
|
|
|
|
|
results.ForEach(o => { o.PluginID = pair.Metadata.ID; });
|
|
|
|
|
});
|
|
|
|
|
pair.QueryCount += 1;
|
|
|
|
|
pair.AvgQueryTime = pair.QueryCount == 1 ? milliseconds : (pair.AvgQueryTime + milliseconds) / 2;
|
|
|
|
|
API.PushResults(query, pair.Metadata, 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
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
throw new WoxPluginException(pair.Metadata.Name, $"QueryForPlugin failed", e);
|
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
|
|
|
|
{
|
2015-11-06 04:44:14 +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-01-27 22:59:03 +08:00
|
|
|
|
{
|
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 &&
|
2015-11-06 04:44:14 +08:00
|
|
|
|
InstantQueryPlugins.Any(p => p.Metadata.ID == plugin.Metadata.ID);
|
2015-01-27 22:59:03 +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
|
|
|
|
{
|
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)
|
2015-02-07 23:49:46 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-11-03 02:13:53 +08:00
|
|
|
|
return plugin.LoadContextMenus(result);
|
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
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
Log.Error(new WoxPluginException(pluginPair.Metadata.Name, $"Couldn't load plugin context menus", e));
|
2015-02-07 23:49:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 02:13:53 +08:00
|
|
|
|
return new List<Result>();
|
2015-02-07 23:49:46 +08:00
|
|
|
|
}
|
2015-11-09 11:20:02 +08:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2015-11-13 13:14:27 +08:00
|
|
|
|
// do nothing if they are same
|
|
|
|
|
if (oldActionKeyword == newActionKeyword) return;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|