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;
|
2014-12-29 23:02:50 +08:00
|
|
|
|
using Wox.Core.Exception;
|
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-01-04 23:08:26 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
using Wox.Infrastructure.Http;
|
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-01-26 17:46:55 +08:00
|
|
|
|
public const string ActionKeywordWildcardSign = "*";
|
2015-01-27 22:59:03 +08:00
|
|
|
|
private static List<PluginMetadata> pluginMetadatas;
|
|
|
|
|
private static List<IInstantSearch> instantSearches = new List<IInstantSearch>();
|
|
|
|
|
|
2015-01-23 20:49:00 +08:00
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
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>();
|
|
|
|
|
|
2015-01-20 22:33:45 +08:00
|
|
|
|
private static void SetupPluginDirectories()
|
2014-12-27 12:34:51 +08:00
|
|
|
|
{
|
2015-01-20 22:33:45 +08:00
|
|
|
|
pluginDirectories.Add(PluginDirectory);
|
|
|
|
|
MakesurePluginDirectoriesExist();
|
2014-12-27 12:34:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-20 22:33:45 +08:00
|
|
|
|
public static string PluginDirectory
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2015-01-20 22:33:45 +08:00
|
|
|
|
get { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Plugins"); }
|
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);
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(e.Message);
|
|
|
|
|
}
|
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
|
|
|
|
{
|
2014-12-29 23:02:50 +08:00
|
|
|
|
if (api == null) throw new WoxCritialException("api is null");
|
|
|
|
|
|
|
|
|
|
SetupPluginDirectories();
|
2014-12-26 22:51:04 +08:00
|
|
|
|
API = api;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
plugins.Clear();
|
|
|
|
|
|
2015-01-27 22:59:03 +08:00
|
|
|
|
pluginMetadatas = PluginConfig.Parse(pluginDirectories);
|
2014-12-26 19:36:43 +08:00
|
|
|
|
plugins.AddRange(new CSharpPluginLoader().LoadPlugin(pluginMetadatas));
|
|
|
|
|
plugins.AddRange(new JsonRPCPluginLoader<PythonPlugin>().LoadPlugin(pluginMetadatas));
|
|
|
|
|
|
2015-01-07 22:23:10 +08:00
|
|
|
|
//load plugin i18n languages
|
|
|
|
|
ResourceMerger.ApplyPluginLanguages();
|
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
foreach (PluginPair pluginPair in plugins)
|
|
|
|
|
{
|
|
|
|
|
PluginPair pair = pluginPair;
|
2015-01-04 23:08:26 +08:00
|
|
|
|
ThreadPool.QueueUserWorkItem(o =>
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2015-01-08 22:49:42 +08:00
|
|
|
|
using (new Timeit(string.Format("Init {0}", pair.Metadata.Name)))
|
2015-01-04 23:08:26 +08:00
|
|
|
|
{
|
|
|
|
|
pair.Plugin.Init(new PluginInitContext()
|
|
|
|
|
{
|
|
|
|
|
CurrentPluginMetadata = pair.Metadata,
|
|
|
|
|
Proxy = HttpProxy.Instance,
|
|
|
|
|
API = API
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-01-08 22:49:42 +08:00
|
|
|
|
});
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
2015-01-27 22:59:03 +08:00
|
|
|
|
|
|
|
|
|
LoadInstantSearches();
|
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)
|
|
|
|
|
{
|
2015-01-26 17:46:55 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(query.RawQuery.Trim()))
|
|
|
|
|
{
|
|
|
|
|
QueryDispatcher.QueryDispatcher.Dispatch(query);
|
|
|
|
|
}
|
2014-12-26 22:51:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
public static List<PluginPair> AllPlugins
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2015-01-22 20:24:15 +08:00
|
|
|
|
return plugins.OrderBy(o => o.Metadata.Name).ToList();
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-26 17:46:55 +08:00
|
|
|
|
public static bool IsUserPluginQuery(Query query)
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2015-01-26 17:46:55 +08:00
|
|
|
|
if (string.IsNullOrEmpty(query.RawQuery)) return false;
|
|
|
|
|
var strings = query.RawQuery.Split(' ');
|
2015-01-26 22:50:38 +08:00
|
|
|
|
if(strings.Length == 1) return false;
|
|
|
|
|
|
|
|
|
|
var actionKeyword = strings[0].Trim();
|
2015-01-26 17:46:55 +08:00
|
|
|
|
if (string.IsNullOrEmpty(actionKeyword)) return false;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
|
2015-01-26 17:46:55 +08:00
|
|
|
|
return plugins.Any(o => o.Metadata.PluginType == PluginType.User && o.Metadata.ActionKeyword == actionKeyword);
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-26 17:46:55 +08:00
|
|
|
|
public static bool IsSystemPlugin(PluginMetadata metadata)
|
2015-01-04 23:08:26 +08:00
|
|
|
|
{
|
2015-01-26 17:46:55 +08:00
|
|
|
|
return metadata.ActionKeyword == ActionKeywordWildcardSign;
|
2015-01-04 23:08:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
public static void ActivatePluginDebugger(string path)
|
|
|
|
|
{
|
|
|
|
|
DebuggerMode = path;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 22:59:03 +08:00
|
|
|
|
public static bool IsInstantSearch(string query)
|
|
|
|
|
{
|
|
|
|
|
return LoadInstantSearches().Any(o => o.IsInstantSearch(query));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<IInstantSearch> LoadInstantSearches()
|
|
|
|
|
{
|
|
|
|
|
if (instantSearches.Count > 0) return instantSearches;
|
|
|
|
|
List<PluginMetadata> CSharpPluginMetadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
|
|
|
|
|
|
|
|
|
|
foreach (PluginMetadata metadata in CSharpPluginMetadatas)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
|
|
|
|
|
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IInstantSearch))).ToList();
|
|
|
|
|
if (types.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (Type type in types)
|
|
|
|
|
{
|
|
|
|
|
instantSearches.Add(Activator.CreateInstance(type) as IInstantSearch);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(string.Format("Couldn't load plugin {0}: {1}", metadata.Name, e.Message));
|
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return instantSearches;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
/// <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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|