2014-12-26 19:36:43 +08:00
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The entry for managing Wox plugins
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class PluginManager
|
|
|
|
|
{
|
|
|
|
|
public static String DebuggerMode { get; private set; }
|
2014-12-26 22:51:04 +08:00
|
|
|
|
public static IPublicAPI API { get; private set; }
|
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
private static List<PluginPair> plugins = new List<PluginPair>();
|
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>();
|
|
|
|
|
|
2014-12-27 12:34:51 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Default plugin directory
|
|
|
|
|
/// new plugin will be installed to this directory
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string DefaultPluginDirectory
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string userProfilePath = Environment.GetEnvironmentVariable("USERPROFILE");
|
|
|
|
|
if (userProfilePath != null)
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(Path.Combine(userProfilePath, ".Wox"), "Plugins");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
static PluginManager()
|
|
|
|
|
{
|
2014-12-27 12:34:51 +08:00
|
|
|
|
pluginDirectories.Add(DefaultPluginDirectory);
|
2014-12-26 22:51:04 +08:00
|
|
|
|
MakesurePluginDirectoriesExist();
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void MakesurePluginDirectoriesExist()
|
|
|
|
|
{
|
|
|
|
|
foreach (string pluginDirectory in pluginDirectories)
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(pluginDirectory))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(pluginDirectory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
{
|
2014-12-26 22:51:04 +08:00
|
|
|
|
API = api;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
plugins.Clear();
|
|
|
|
|
|
|
|
|
|
List<PluginMetadata> pluginMetadatas = PluginConfig.Parse(pluginDirectories);
|
|
|
|
|
plugins.AddRange(new CSharpPluginLoader().LoadPlugin(pluginMetadatas));
|
|
|
|
|
plugins.AddRange(new JsonRPCPluginLoader<PythonPlugin>().LoadPlugin(pluginMetadatas));
|
|
|
|
|
|
|
|
|
|
foreach (PluginPair pluginPair in plugins)
|
|
|
|
|
{
|
|
|
|
|
PluginPair pair = pluginPair;
|
|
|
|
|
ThreadPool.QueueUserWorkItem(o => pair.Plugin.Init(new PluginInitContext()
|
|
|
|
|
{
|
|
|
|
|
CurrentPluginMetadata = pair.Metadata,
|
|
|
|
|
Proxy = HttpProxy.Instance,
|
2014-12-26 22:51:04 +08:00
|
|
|
|
API = API
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 22:51:04 +08:00
|
|
|
|
public static void Query(Query query)
|
|
|
|
|
{
|
|
|
|
|
QueryDispatcher.QueryDispatcher.Dispatch(query);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
public static List<PluginPair> AllPlugins
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return plugins;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 22:51:04 +08:00
|
|
|
|
public static bool IsUserPluginQuery(Query query)
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(query.ActionName)) return false;
|
|
|
|
|
|
2014-12-26 22:51:04 +08:00
|
|
|
|
return plugins.Any(o => o.Metadata.PluginType == PluginType.User && o.Metadata.ActionKeyword == query.ActionName);
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ActivatePluginDebugger(string path)
|
|
|
|
|
{
|
|
|
|
|
DebuggerMode = path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// get specified plugin, return null if not found
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static PluginPair GetPlugin(string id)
|
|
|
|
|
{
|
|
|
|
|
return AllPlugins.FirstOrDefault(o => o.Metadata.ID == id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|