PowerToys/Wox.Core/Plugin/PluginManager.cs

311 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Wox.Core.Resource;
using Wox.Infrastructure;
using Wox.Infrastructure.Exception;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Infrastructure.UserSettings;
using Wox.Plugin;
namespace Wox.Core.Plugin
{
/// <summary>
/// The entry for managing Wox plugins
/// </summary>
public static class PluginManager
{
private static IEnumerable<PluginPair> _contextMenuPlugins;
/// <summary>
/// Directories that will hold Wox plugin directory
/// </summary>
public static List<PluginPair> AllPlugins { get; private set; }
public static readonly List<PluginPair> GlobalPlugins = new List<PluginPair>();
public static readonly Dictionary<string, PluginPair> NonGlobalPlugins = new Dictionary<string, PluginPair>();
public static IPublicAPI API { private set; get; }
// todo happlebao, this should not be public, the indicator function should be embeded
public static PluginsSettings Settings;
private static List<PluginMetadata> _metadatas;
private static readonly string[] Directories = { Constant.PreinstalledDirectory, Constant.PluginsDirectory };
private static void ValidateUserDirectory()
{
if (!Directory.Exists(Constant.PluginsDirectory))
{
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);
}
}
}
public static void Save()
{
foreach (var plugin in AllPlugins)
{
var savable = plugin.Plugin as ISavable;
savable?.Save();
}
}
static PluginManager()
{
ValidateUserDirectory();
// force old plugins use new python binding
DeletePythonBinding();
}
/// <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)
{
_metadatas = PluginConfig.Parse(Directories);
Settings = settings;
Settings.UpdatePluginSettings(_metadatas);
AllPlugins = PluginsLoader.Plugins(_metadatas, Settings);
}
public static void InitializePlugins(IPublicAPI api)
{
//load plugin i18n languages
ResourceMerger.UpdatePluginLanguages();
API = api;
Parallel.ForEach(AllPlugins, pair =>
{
var milliseconds = Stopwatch.Normal($"Plugin init: {pair.Metadata.Name}", () =>
{
pair.Plugin.Init(new PluginInitContext
{
CurrentPluginMetadata = pair.Metadata,
API = API
});
});
pair.Metadata.InitTime = milliseconds;
InternationalizationManager.Instance.UpdatePluginMetadataTranslations(pair);
});
_contextMenuPlugins = GetPluginsForInterface<IContextMenu>();
foreach (var plugin in AllPlugins)
{
if (IsGlobalPlugin(plugin.Metadata))
{
GlobalPlugins.Add(plugin);
}
else
{
foreach (string actionKeyword in plugin.Metadata.ActionKeywords)
{
NonGlobalPlugins[actionKeyword] = plugin;
}
}
}
}
public static void InstallPlugin(string path)
{
PluginInstaller.Install(path);
}
public static Query QueryInit(string text) //todo is that possible to move it into type Query?
{
// replace multiple white spaces with one white space
var terms = text.Split(new[] { Query.TermSeperater }, StringSplitOptions.RemoveEmptyEntries);
var rawQuery = string.Join(Query.TermSeperater, terms);
var actionKeyword = string.Empty;
var search = rawQuery;
List<string> actionParameters = terms.ToList();
if (terms.Length == 0) return null;
if (NonGlobalPlugins.ContainsKey(terms[0]) &&
!Settings.Plugins[NonGlobalPlugins[terms[0]].Metadata.ID].Disabled)
{
actionKeyword = terms[0];
actionParameters = terms.Skip(1).ToList();
search = string.Join(Query.TermSeperater, actionParameters.ToArray());
}
var query = new Query
{
Terms = terms,
RawQuery = rawQuery,
ActionKeyword = actionKeyword,
Search = search,
// Obsolete value initialisation
ActionName = actionKeyword,
ActionParameters = actionParameters
};
return query;
}
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;
}
}
public static List<Result> QueryForPlugin(PluginPair pair, Query query)
{
var results = new List<Result>();
try
{
var metadata = pair.Metadata;
var milliseconds = Stopwatch.Normal($"Plugin.Query cost for {metadata.Name}", () =>
{
results = pair.Plugin.Query(query) ?? results;
UpdatePluginMetadata(results, metadata, query);
});
metadata.QueryCount += 1;
metadata.AvgQueryTime = metadata.QueryCount == 1 ? milliseconds : (metadata.AvgQueryTime + milliseconds) / 2;
}
catch (Exception e)
{
throw new WoxPluginException(pair.Metadata.Name, "QueryForPlugin failed", e);
}
return results;
}
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;
}
}
private static bool IsGlobalPlugin(PluginMetadata metadata)
{
return metadata.ActionKeywords.Contains(Query.GlobalPluginWildcardSign);
}
/// <summary>
/// get specified plugin, return null if not found
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static PluginPair GetPluginForId(string id)
{
return AllPlugins.FirstOrDefault(o => o.Metadata.ID == id);
}
public static IEnumerable<PluginPair> GetPluginsForInterface<T>() where T : IFeatures
{
return AllPlugins.Where(p => p.Plugin is T);
}
public static List<Result> GetContextMenusForPlugin(Result result)
{
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.Exception(new WoxPluginException(metadata.Name, "Couldn't load plugin context menus", e));
return new List<Result>();
}
}
else
{
return new List<Result>();
}
}
public static bool ActionKeywordRegistered(string actionKeyword)
{
if (actionKeyword != Query.GlobalPluginWildcardSign &&
NonGlobalPlugins.ContainsKey(actionKeyword))
{
return true;
}
else
{
return false;
}
}
public static void AddActionKeyword(string id, string newActionKeyword)
{
var plugin = GetPluginForId(id);
if (newActionKeyword == Query.GlobalPluginWildcardSign)
{
GlobalPlugins.Add(plugin);
}
else
{
NonGlobalPlugins[newActionKeyword] = plugin;
}
AllPlugins.Add(plugin);
plugin.Metadata.ActionKeywords.Add(newActionKeyword);
}
public static void RemoveActionKeyword(string id, string oldActionkeyword)
{
var plugin = GetPluginForId(id);
if (oldActionkeyword == Query.GlobalPluginWildcardSign)
{
GlobalPlugins.Remove(plugin);
}
else
{
NonGlobalPlugins.Remove(oldActionkeyword);
}
AllPlugins.Remove(plugin);
plugin.Metadata.ActionKeywords.Remove(oldActionkeyword);
}
public static void ReplaceActionKeyword(string id, string oldActionKeyword, string newActionKeyword)
{
if (oldActionKeyword != newActionKeyword)
{
AddActionKeyword(id, newActionKeyword);
RemoveActionKeyword(id, oldActionKeyword);
}
}
}
}