using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Threading; using Wox.Infrastructure.Http; using Wox.Plugin; namespace Wox.Core.Plugin { /// /// The entry for managing Wox plugins /// public static class PluginManager { public static String DebuggerMode { get; private set; } public static IPublicAPI API { get; private set; } private static List plugins = new List(); /// /// Directories that will hold Wox plugin directory /// private static List pluginDirectories = new List(); /// /// Default plugin directory /// new plugin will be installed to this directory /// public static string DefaultPluginDirectory { get { string userProfilePath = Environment.GetEnvironmentVariable("USERPROFILE"); if (userProfilePath != null) { return Path.Combine(Path.Combine(userProfilePath, ".Wox"), "Plugins"); } return string.Empty; } } static PluginManager() { pluginDirectories.Add(DefaultPluginDirectory); pluginDirectories.Add( Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Plugins")); MakesurePluginDirectoriesExist(); } private static void MakesurePluginDirectoriesExist() { foreach (string pluginDirectory in pluginDirectories) { if (!Directory.Exists(pluginDirectory)) { Directory.CreateDirectory(pluginDirectory); } } } /// /// Load and init all Wox plugins /// public static void Init(IPublicAPI api) { API = api; plugins.Clear(); List pluginMetadatas = PluginConfig.Parse(pluginDirectories); plugins.AddRange(new CSharpPluginLoader().LoadPlugin(pluginMetadatas)); plugins.AddRange(new JsonRPCPluginLoader().LoadPlugin(pluginMetadatas)); foreach (PluginPair pluginPair in plugins) { PluginPair pair = pluginPair; ThreadPool.QueueUserWorkItem(o => pair.Plugin.Init(new PluginInitContext() { CurrentPluginMetadata = pair.Metadata, Proxy = HttpProxy.Instance, API = API })); } } public static void InstallPlugin(string path) { PluginInstaller.Install(path); } public static void Query(Query query) { QueryDispatcher.QueryDispatcher.Dispatch(query); } public static List AllPlugins { get { return plugins; } } public static bool IsUserPluginQuery(Query query) { if (string.IsNullOrEmpty(query.ActionName)) return false; return plugins.Any(o => o.Metadata.PluginType == PluginType.User && o.Metadata.ActionKeyword == query.ActionName); } public static void ActivatePluginDebugger(string path) { DebuggerMode = path; } /// /// get specified plugin, return null if not found /// /// /// public static PluginPair GetPlugin(string id) { return AllPlugins.FirstOrDefault(o => o.Metadata.ID == id); } } }