mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
Refactoring [WIP]
This commit is contained in:
@@ -11,9 +11,9 @@ using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Shapes;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
using Wox.Plugin;
|
||||
using Wox.PluginLoader;
|
||||
using MessageBox = System.Windows.MessageBox;
|
||||
|
||||
namespace Wox
|
||||
@@ -25,7 +25,7 @@ namespace Wox
|
||||
public ActionKeyword(string pluginId)
|
||||
{
|
||||
InitializeComponent();
|
||||
PluginPair plugin = Plugins.AllPlugins.FirstOrDefault(o => o.Metadata.ID == pluginId);
|
||||
PluginPair plugin = PluginManager.GetPlugin(pluginId);
|
||||
if (plugin == null)
|
||||
{
|
||||
MessageBox.Show("Can't find specific plugin");
|
||||
@@ -56,7 +56,7 @@ namespace Wox
|
||||
}
|
||||
|
||||
//check new action keyword didn't used by other plugin
|
||||
if (Plugins.AllPlugins.Exists(o => o.Metadata.ActionKeyword == tbAction.Text.Trim()))
|
||||
if (PluginManager.AllPlugins.Exists(o => o.Metadata.ActionKeyword == tbAction.Text.Trim()))
|
||||
{
|
||||
MessageBox.Show("New ActionKeyword has been assigned to other plugin, please assign another new action keyword");
|
||||
return;
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Input;
|
||||
using Wox.Core.Plugin;
|
||||
|
||||
namespace Wox.CommandArgs
|
||||
{
|
||||
@@ -18,7 +19,7 @@ namespace Wox.CommandArgs
|
||||
if (args.Count > 0)
|
||||
{
|
||||
var pluginFolderPath = args[0];
|
||||
PluginLoader.Plugins.ActivatePluginDebugger(pluginFolderPath);
|
||||
PluginManager.ActivatePluginDebugger(pluginFolderPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Wox.PluginLoader;
|
||||
using Wox.Core.Plugin;
|
||||
|
||||
namespace Wox.CommandArgs
|
||||
{
|
||||
@@ -15,7 +15,7 @@ namespace Wox.CommandArgs
|
||||
|
||||
public void Execute(IList<string> args)
|
||||
{
|
||||
Plugins.Init();
|
||||
PluginManager.Init();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Helper;
|
||||
using Wox.Plugin;
|
||||
using Wox.PluginLoader;
|
||||
|
||||
namespace Wox.Commands
|
||||
{
|
||||
@@ -15,7 +15,7 @@ namespace Wox.Commands
|
||||
|
||||
public static void DispatchCommand(Query query)
|
||||
{
|
||||
if (Plugins.HitThirdpartyKeyword(query))
|
||||
if (PluginManager.HitThirdpartyKeyword(query))
|
||||
{
|
||||
pluginCmd.Dispatch(query);
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
using Wox.Plugin;
|
||||
using Wox.PluginLoader;
|
||||
|
||||
namespace Wox.Commands
|
||||
{
|
||||
@@ -14,7 +14,7 @@ namespace Wox.Commands
|
||||
{
|
||||
public override void Dispatch(Query query)
|
||||
{
|
||||
PluginPair thirdPlugin = Plugins.AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.ActionName);
|
||||
PluginPair thirdPlugin = PluginManager.AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.ActionName);
|
||||
if (thirdPlugin != null && !string.IsNullOrEmpty(thirdPlugin.Metadata.ActionKeyword))
|
||||
{
|
||||
var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == thirdPlugin.Metadata.ID);
|
||||
|
||||
@@ -3,16 +3,16 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
using Wox.Plugin;
|
||||
using Wox.Plugin.SystemPlugins;
|
||||
using Wox.PluginLoader;
|
||||
|
||||
namespace Wox.Commands
|
||||
{
|
||||
public class SystemCommand : BaseCommand
|
||||
{
|
||||
private IEnumerable<PluginPair> allSytemPlugins = Plugins.AllPlugins.Where(o => o.Metadata.PluginType == PluginType.System);
|
||||
private IEnumerable<PluginPair> allSytemPlugins = PluginManager.AllPlugins.Where(o => o.Metadata.PluginType == PluginType.System);
|
||||
|
||||
public override void Dispatch(Query query)
|
||||
{
|
||||
|
||||
@@ -5,8 +5,8 @@ using System.Linq;
|
||||
using System.Windows;
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Plugin;
|
||||
using Wox.PluginLoader;
|
||||
|
||||
namespace Wox.Helper
|
||||
{
|
||||
@@ -57,7 +57,7 @@ namespace Wox.Helper
|
||||
string content = string.Format(
|
||||
"Do you want to install following plugin?\r\n\r\nName: {0}\r\nVersion: {1}\r\nAuthor: {2}",
|
||||
plugin.Name, plugin.Version, plugin.Author);
|
||||
PluginPair existingPlugin = Plugins.AllPlugins.FirstOrDefault(o => o.Metadata.ID == plugin.ID);
|
||||
PluginPair existingPlugin = PluginManager.GetPlugin(plugin.ID);
|
||||
|
||||
if (existingPlugin != null)
|
||||
{
|
||||
|
||||
@@ -1,133 +0,0 @@
|
||||
|
||||
/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Wox and other plugins,
|
||||
* like python or other self-execute program. But, we added addtional infos (proxy and so on) into rpc request. Also, we didn't use the
|
||||
* "id" and "jsonrpc" in the request, since it's not so useful in our request model.
|
||||
*
|
||||
* When execute a query:
|
||||
* Wox -------JsonRPCServerRequestModel--------> client
|
||||
* Wox <------JsonRPCQueryResponseModel--------- client
|
||||
*
|
||||
* When execute a action (which mean user select an item in reulst item):
|
||||
* Wox -------JsonRPCServerRequestModel--------> client
|
||||
* Wox <------JsonRPCResponseModel-------------- client
|
||||
*
|
||||
*/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.JsonRPC
|
||||
{
|
||||
public class JsonRPCErrorModel
|
||||
{
|
||||
public int Code { get; set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
|
||||
public string Data { get; set; }
|
||||
}
|
||||
|
||||
public class JsonRPCModelBase
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class JsonRPCResponseModel : JsonRPCModelBase
|
||||
{
|
||||
public string Result { get; set; }
|
||||
|
||||
public JsonRPCErrorModel Error { get; set; }
|
||||
}
|
||||
|
||||
public class JsonRPCQueryResponseModel : JsonRPCResponseModel
|
||||
{
|
||||
public new List<JsonRPCResult> Result { get; set; }
|
||||
}
|
||||
|
||||
public class JsonRPCRequestModel : JsonRPCModelBase
|
||||
{
|
||||
public string Method { get; set; }
|
||||
|
||||
public object[] Parameters { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
string rpc = string.Empty;
|
||||
if (Parameters != null && Parameters.Length > 0)
|
||||
{
|
||||
string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParamterByType(o) + ","));
|
||||
parameters = parameters.Substring(0, parameters.Length - 1) + "]";
|
||||
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":{1}", Method, parameters);
|
||||
}
|
||||
else
|
||||
{
|
||||
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":[]", Method);
|
||||
}
|
||||
|
||||
return rpc;
|
||||
|
||||
}
|
||||
|
||||
private string GetParamterByType(object paramter)
|
||||
{
|
||||
|
||||
if (paramter is string)
|
||||
{
|
||||
return string.Format(@"\""{0}\""", paramter);
|
||||
}
|
||||
if (paramter is int || paramter is float || paramter is double)
|
||||
{
|
||||
return string.Format(@"{0}", paramter);
|
||||
}
|
||||
if (paramter is bool)
|
||||
{
|
||||
return string.Format(@"{0}", paramter.ToString().ToLower());
|
||||
}
|
||||
return paramter.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Json RPC Request that Wox sent to client
|
||||
/// </summary>
|
||||
public class JsonRPCServerRequestModel : JsonRPCRequestModel
|
||||
{
|
||||
public IHttpProxy HttpProxy { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
string rpc = base.ToString();
|
||||
if (HttpProxy != null)
|
||||
{
|
||||
rpc += string.Format(@",\""proxy\"":{{\""enabled\"":{0},\""server\"":\""{1}\"",\""port\"":{2},\""username\"":\""{3}\"",\""password\"":\""{4}\""}}",
|
||||
HttpProxy.Enabled.ToString().ToLower(), HttpProxy.Server, HttpProxy.Port, HttpProxy.UserName, HttpProxy.Password);
|
||||
}
|
||||
return rpc + "}";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Json RPC Request(in query response) that client sent to Wox
|
||||
/// </summary>
|
||||
public class JsonRPCClientRequestModel : JsonRPCRequestModel
|
||||
{
|
||||
public bool DontHideAfterAction { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
string rpc = base.ToString();
|
||||
return rpc + "}";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represent the json-rpc result item that client send to Wox
|
||||
/// Typically, we will send back this request model to client after user select the result item
|
||||
/// But if the request method starts with "Wox.", we will invoke the public APIs we expose.
|
||||
/// </summary>
|
||||
public class JsonRPCResult : Result
|
||||
{
|
||||
public JsonRPCClientRequestModel JsonRPCAction { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -15,14 +15,13 @@ using WindowsInput.Native;
|
||||
using NHotkey;
|
||||
using NHotkey.Wpf;
|
||||
using Wox.Commands;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Helper;
|
||||
using Wox.ImageLoader;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Hotkey;
|
||||
using Wox.Infrastructure.Storage;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
using Wox.Plugin;
|
||||
using Wox.PluginLoader;
|
||||
using Wox.Update;
|
||||
using Application = System.Windows.Application;
|
||||
using Brushes = System.Windows.Media.Brushes;
|
||||
@@ -128,12 +127,12 @@ namespace Wox
|
||||
|
||||
public void ReloadPlugins()
|
||||
{
|
||||
Dispatcher.Invoke(new Action(Plugins.Init));
|
||||
Dispatcher.Invoke(new Action(PluginManager.Init));
|
||||
}
|
||||
|
||||
public List<PluginPair> GetAllPlugins()
|
||||
{
|
||||
return Plugins.AllPlugins;
|
||||
return PluginManager.AllPlugins;
|
||||
}
|
||||
|
||||
public event WoxKeyDownEventHandler BackKeyDownEvent;
|
||||
@@ -193,7 +192,7 @@ namespace Wox
|
||||
ThreadPool.QueueUserWorkItem(o =>
|
||||
{
|
||||
Thread.Sleep(50);
|
||||
Plugins.Init();
|
||||
PluginManager.Init();
|
||||
});
|
||||
ThreadPool.QueueUserWorkItem(o =>
|
||||
{
|
||||
@@ -360,7 +359,7 @@ namespace Wox
|
||||
var q = new Query(lastQuery);
|
||||
CommandFactory.DispatchCommand(q);
|
||||
BackToResultMode();
|
||||
if (Plugins.HitThirdpartyKeyword(q))
|
||||
if (PluginManager.HitThirdpartyKeyword(q))
|
||||
{
|
||||
Dispatcher.DelayInvoke("ShowProgressbar", originQuery =>
|
||||
{
|
||||
|
||||
@@ -1,167 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Helper;
|
||||
using Wox.Helper.ErrorReporting;
|
||||
using Wox.Infrastructure.Exceptions;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.JsonRPC;
|
||||
using Wox.Plugin;
|
||||
using MessageBox = System.Windows.MessageBox;
|
||||
|
||||
namespace Wox.PluginLoader
|
||||
{
|
||||
public abstract class BasePlugin : IPlugin
|
||||
{
|
||||
protected PluginInitContext context;
|
||||
|
||||
public abstract string SupportedLanguage { get; }
|
||||
|
||||
protected abstract string ExecuteQuery(Query query);
|
||||
protected abstract string ExecuteAction(JsonRPCRequestModel rpcRequest);
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
{
|
||||
string output = ExecuteQuery(query);
|
||||
if (!string.IsNullOrEmpty(output))
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Result> results = new List<Result>();
|
||||
|
||||
JsonRPCQueryResponseModel queryResponseModel = JsonConvert.DeserializeObject<JsonRPCQueryResponseModel>(output);
|
||||
if (queryResponseModel.Result == null) return null;
|
||||
|
||||
foreach (JsonRPCResult result in queryResponseModel.Result)
|
||||
{
|
||||
JsonRPCResult result1 = result;
|
||||
result.Action = (c) =>
|
||||
{
|
||||
if (result1.JsonRPCAction == null) return false;
|
||||
|
||||
if (!string.IsNullOrEmpty(result1.JsonRPCAction.Method))
|
||||
{
|
||||
if (result1.JsonRPCAction.Method.StartsWith("Wox."))
|
||||
{
|
||||
ExecuteWoxAPI(result1.JsonRPCAction.Method.Substring(4), result1.JsonRPCAction.Parameters);
|
||||
}
|
||||
else
|
||||
{
|
||||
ThreadPool.QueueUserWorkItem(state =>
|
||||
{
|
||||
string actionReponse = ExecuteAction(result1.JsonRPCAction);
|
||||
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionReponse);
|
||||
if (jsonRpcRequestModel != null
|
||||
&& !string.IsNullOrEmpty(jsonRpcRequestModel.Method)
|
||||
&& jsonRpcRequestModel.Method.StartsWith("Wox."))
|
||||
{
|
||||
ExecuteWoxAPI(jsonRpcRequestModel.Method.Substring(4), jsonRpcRequestModel.Parameters);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return !result1.JsonRPCAction.DontHideAfterAction;
|
||||
};
|
||||
results.Add(result);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ErrorReporting.TryShowErrorMessageBox(e.Message, e);
|
||||
Log.Error(e.Message);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void ExecuteWoxAPI(string method, object[] parameters)
|
||||
{
|
||||
MethodInfo methodInfo = App.Window.GetType().GetMethod(method);
|
||||
if (methodInfo != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
methodInfo.Invoke(App.Window, parameters);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute external program and return the output
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="arguments"></param>
|
||||
/// <returns></returns>
|
||||
protected string Execute(string fileName, string arguments)
|
||||
{
|
||||
ProcessStartInfo start = new ProcessStartInfo();
|
||||
start.FileName = fileName;
|
||||
start.Arguments = arguments;
|
||||
start.UseShellExecute = false;
|
||||
start.CreateNoWindow = true;
|
||||
start.RedirectStandardOutput = true;
|
||||
start.RedirectStandardError = true;
|
||||
return Execute(start);
|
||||
}
|
||||
|
||||
protected string Execute(ProcessStartInfo startInfo)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (Process process = Process.Start(startInfo))
|
||||
{
|
||||
if (process != null)
|
||||
{
|
||||
using (StreamReader reader = process.StandardOutput)
|
||||
{
|
||||
string result = reader.ReadToEnd();
|
||||
if (result.StartsWith("DEBUG:"))
|
||||
{
|
||||
System.Windows.Forms.MessageBox.Show(new Form { TopMost = true }, result.Substring(6));
|
||||
return "";
|
||||
}
|
||||
if (string.IsNullOrEmpty(result))
|
||||
{
|
||||
using (StreamReader errorReader = process.StandardError)
|
||||
{
|
||||
string error = errorReader.ReadToEnd();
|
||||
if (!string.IsNullOrEmpty(error))
|
||||
{
|
||||
ErrorReporting.TryShowErrorMessageBox(error, new WoxJsonRPCException(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Init(PluginInitContext ctx)
|
||||
{
|
||||
this.context = ctx;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.PluginLoader
|
||||
{
|
||||
public class BasePluginLoader<T> : IPluginLoader where T : BasePlugin, new()
|
||||
{
|
||||
public virtual List<PluginPair> LoadPlugin(List<PluginMetadata> pluginMetadatas)
|
||||
{
|
||||
string supportedLanguage = new T().SupportedLanguage;
|
||||
List<PluginMetadata> metadatas = pluginMetadatas.Where(o => supportedLanguage.ToUpper() == o.Language.ToUpper()).ToList();
|
||||
|
||||
return metadatas.Select(metadata => new PluginPair()
|
||||
{
|
||||
Plugin = new T(),
|
||||
Metadata = metadata
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Plugin;
|
||||
using Wox.Plugin.SystemPlugins;
|
||||
|
||||
namespace Wox.PluginLoader {
|
||||
|
||||
public class CSharpPluginLoader : IPluginLoader
|
||||
{
|
||||
public List<PluginPair> LoadPlugin(List<PluginMetadata> pluginMetadatas)
|
||||
{
|
||||
var plugins = new List<PluginPair>();
|
||||
|
||||
List<PluginMetadata> metadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
|
||||
foreach (PluginMetadata metadata in metadatas) {
|
||||
try {
|
||||
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
|
||||
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && (o.BaseType == typeof(BaseSystemPlugin) || o.GetInterfaces().Contains(typeof(IPlugin)))).ToList();
|
||||
if (types.Count == 0) {
|
||||
Log.Warn(string.Format("Couldn't load plugin {0}: didn't find the class who implement IPlugin", metadata.Name));
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (Type type in types) {
|
||||
PluginPair pair = new PluginPair() {
|
||||
Plugin = Activator.CreateInstance(type) as IPlugin,
|
||||
Metadata = metadata
|
||||
};
|
||||
|
||||
var sys = pair.Plugin as BaseSystemPlugin;
|
||||
if (sys != null) {
|
||||
sys.PluginDirectory = metadata.PluginDirectory;
|
||||
}
|
||||
|
||||
plugins.Add(pair);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
Log.Error(string.Format("Couldn't load plugin {0}: {1}", metadata.Name, e.Message));
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.PluginLoader
|
||||
{
|
||||
public interface IPluginLoader
|
||||
{
|
||||
List<PluginPair> LoadPlugin(List<PluginMetadata> pluginMetadatas);
|
||||
}
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure.Exceptions;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
using Wox.Plugin;
|
||||
using Wox.Plugin.SystemPlugins;
|
||||
|
||||
namespace Wox.PluginLoader {
|
||||
public abstract class PluginConfigLoader {
|
||||
private static string PluginPath = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Plugins");
|
||||
private static string PluginConfigName = "plugin.json";
|
||||
private static List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
|
||||
|
||||
public static List<PluginMetadata> ParsePluginsConfig()
|
||||
{
|
||||
pluginMetadatas.Clear();
|
||||
ParseSystemPlugins();
|
||||
ParseThirdPartyPlugins();
|
||||
|
||||
if (Plugins.DebuggerMode != null) {
|
||||
PluginMetadata metadata = GetMetadataFromJson(Plugins.DebuggerMode);
|
||||
if (metadata != null) pluginMetadatas.Add(metadata);
|
||||
}
|
||||
return pluginMetadatas;
|
||||
}
|
||||
|
||||
private static void ParseSystemPlugins() {
|
||||
pluginMetadatas.Add(new PluginMetadata() {
|
||||
Name = "System Plugins",
|
||||
Author = "System",
|
||||
Description = "system plugins collection",
|
||||
Website = "http://www.getwox.com",
|
||||
Language = AllowedLanguage.CSharp,
|
||||
Version = "1.0",
|
||||
PluginType = PluginType.System,
|
||||
ActionKeyword = "*",
|
||||
ExecuteFileName = "Wox.Plugin.SystemPlugins.dll",
|
||||
PluginDirectory = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath)
|
||||
});
|
||||
}
|
||||
|
||||
private static void ParseThirdPartyPlugins() {
|
||||
if (!Directory.Exists(PluginPath))
|
||||
Directory.CreateDirectory(PluginPath);
|
||||
|
||||
string[] directories = Directory.GetDirectories(PluginPath);
|
||||
foreach (string directory in directories) {
|
||||
if (File.Exists((Path.Combine(directory, "NeedDelete.txt")))) {
|
||||
Directory.Delete(directory, true);
|
||||
continue;
|
||||
}
|
||||
PluginMetadata metadata = GetMetadataFromJson(directory);
|
||||
if (metadata != null) pluginMetadatas.Add(metadata);
|
||||
}
|
||||
}
|
||||
|
||||
private static PluginMetadata GetMetadataFromJson(string pluginDirectory) {
|
||||
string configPath = Path.Combine(pluginDirectory, PluginConfigName);
|
||||
PluginMetadata metadata;
|
||||
|
||||
if (!File.Exists(configPath)) {
|
||||
Log.Warn(string.Format("parse plugin {0} failed: didn't find config file.", configPath));
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
||||
metadata.PluginType = PluginType.ThirdParty;
|
||||
metadata.PluginDirectory = pluginDirectory;
|
||||
}
|
||||
catch (Exception) {
|
||||
string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
|
||||
Log.Warn(error);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new WoxException(error);
|
||||
}
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
if (!AllowedLanguage.IsAllowed(metadata.Language)) {
|
||||
string error = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath, metadata.Language);
|
||||
Log.Warn(error);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new WoxException(error);
|
||||
}
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
if (!File.Exists(metadata.ExecuteFilePath)) {
|
||||
string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath, metadata.ExecuteFilePath);
|
||||
Log.Warn(error);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new WoxException(error);
|
||||
}
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
|
||||
var customizedPluginConfig =
|
||||
UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == metadata.ID);
|
||||
if (customizedPluginConfig != null && !string.IsNullOrEmpty(customizedPluginConfig.Actionword))
|
||||
{
|
||||
metadata.ActionKeyword = customizedPluginConfig.Actionword;
|
||||
}
|
||||
|
||||
return metadata;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Http;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.PluginLoader
|
||||
{
|
||||
public static class Plugins
|
||||
{
|
||||
public static String DebuggerMode { get; private set; }
|
||||
private static List<PluginPair> plugins = new List<PluginPair>();
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
plugins.Clear();
|
||||
List<PluginMetadata> pluginMetadatas = PluginConfigLoader.ParsePluginsConfig();
|
||||
|
||||
plugins.AddRange(new CSharpPluginLoader().LoadPlugin(pluginMetadatas));
|
||||
plugins.AddRange(new BasePluginLoader<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,
|
||||
API = App.Window
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
public static List<PluginPair> AllPlugins
|
||||
{
|
||||
get
|
||||
{
|
||||
return plugins;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool HitThirdpartyKeyword(Query query)
|
||||
{
|
||||
if (string.IsNullOrEmpty(query.ActionName)) return false;
|
||||
|
||||
return plugins.Any(o => o.Metadata.PluginType == PluginType.ThirdParty && o.Metadata.ActionKeyword == query.ActionName);
|
||||
}
|
||||
|
||||
public static void ActivatePluginDebugger(string path)
|
||||
{
|
||||
DebuggerMode = path;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Http;
|
||||
using Wox.JsonRPC;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.PluginLoader
|
||||
{
|
||||
public class PythonPlugin : BasePlugin
|
||||
{
|
||||
private static string woxDirectory = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
|
||||
private ProcessStartInfo startInfo;
|
||||
|
||||
public override string SupportedLanguage
|
||||
{
|
||||
get { return AllowedLanguage.Python; }
|
||||
}
|
||||
|
||||
public PythonPlugin()
|
||||
{
|
||||
startInfo = new ProcessStartInfo
|
||||
{
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true
|
||||
};
|
||||
string additionalPythonPath = string.Format("{0};{1}",
|
||||
Path.Combine(woxDirectory, "PythonHome\\DLLs"),
|
||||
Path.Combine(woxDirectory, "PythonHome\\Lib\\site-packages"));
|
||||
if (!startInfo.EnvironmentVariables.ContainsKey("PYTHONPATH"))
|
||||
{
|
||||
|
||||
startInfo.EnvironmentVariables.Add("PYTHONPATH", additionalPythonPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
startInfo.EnvironmentVariables["PYTHONPATH"] = additionalPythonPath;
|
||||
}
|
||||
}
|
||||
|
||||
protected override string ExecuteQuery(Query query)
|
||||
{
|
||||
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel()
|
||||
{
|
||||
Method = "query",
|
||||
Parameters = new object[] { query.GetAllRemainingParameter() },
|
||||
HttpProxy = HttpProxy.Instance
|
||||
};
|
||||
//Add -B flag to tell python don't write .py[co] files. Because .pyc contains location infos which will prevent python portable
|
||||
startInfo.FileName = Path.Combine(woxDirectory, "PythonHome\\pythonw.exe");
|
||||
startInfo.Arguments = string.Format("-B \"{0}\" \"{1}\"", context.CurrentPluginMetadata.ExecuteFilePath, request);
|
||||
|
||||
return Execute(startInfo);
|
||||
}
|
||||
|
||||
protected override string ExecuteAction(JsonRPCRequestModel rpcRequest)
|
||||
{
|
||||
startInfo.FileName = Path.Combine(woxDirectory, "PythonHome\\pythonw.exe");
|
||||
startInfo.Arguments = string.Format("-B \"{0}\" \"{1}\"", context.CurrentPluginMetadata.ExecuteFilePath, rpcRequest);
|
||||
return Execute(startInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,11 +11,11 @@ using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using IWshRuntimeLibrary;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
using Wox.Plugin;
|
||||
using Wox.Helper;
|
||||
using Wox.Plugin.SystemPlugins;
|
||||
using Wox.PluginLoader;
|
||||
using Wox.Update;
|
||||
using Application = System.Windows.Forms.Application;
|
||||
using File = System.IO.File;
|
||||
@@ -187,7 +187,7 @@ namespace Wox
|
||||
new CollectionContainer
|
||||
{
|
||||
Collection =
|
||||
PluginLoader.Plugins.AllPlugins.Where(o => o.Metadata.PluginType == PluginType.System)
|
||||
PluginManager.AllPlugins.Where(o => o.Metadata.PluginType == PluginType.System)
|
||||
.Select(o => o.Plugin)
|
||||
.Cast<ISystemPlugin>()
|
||||
},
|
||||
@@ -195,7 +195,7 @@ namespace Wox
|
||||
new CollectionContainer
|
||||
{
|
||||
Collection =
|
||||
PluginLoader.Plugins.AllPlugins.Where(o => o.Metadata.PluginType == PluginType.ThirdParty)
|
||||
PluginManager.AllPlugins.Where(o => o.Metadata.PluginType == PluginType.ThirdParty)
|
||||
}
|
||||
};
|
||||
lbPlugins.ItemsSource = plugins;
|
||||
@@ -576,7 +576,7 @@ namespace Wox
|
||||
string id = pair.Metadata.ID;
|
||||
ActionKeyword changeKeywordWindow = new ActionKeyword(id);
|
||||
changeKeywordWindow.ShowDialog();
|
||||
PluginPair plugin = Plugins.AllPlugins.FirstOrDefault(o => o.Metadata.ID == id);
|
||||
PluginPair plugin = PluginManager.GetPlugin(id);
|
||||
if (plugin != null) pluginActionKeyword.Text = plugin.Metadata.ActionKeyword;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,17 +153,9 @@
|
||||
<DependentUpon>HotkeyControl.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Converters\ImagePathConverter.cs" />
|
||||
<Compile Include="PluginLoader\IPluginLoader.cs" />
|
||||
<Compile Include="Msg.xaml.cs">
|
||||
<DependentUpon>Msg.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="PluginLoader\PluginConfigLoader.cs" />
|
||||
<Compile Include="PluginLoader\CSharpPluginLoader.cs" />
|
||||
<Compile Include="PluginLoader\BasePluginLoader.cs" />
|
||||
<Compile Include="PluginLoader\BasePlugin.cs" />
|
||||
<Compile Include="JsonRPC\JsonPRCModel.cs" />
|
||||
<Compile Include="PluginLoader\Plugins.cs" />
|
||||
<Compile Include="PluginLoader\PythonPlugin.cs" />
|
||||
<Compile Include="Properties\Annotations.cs" />
|
||||
<Compile Include="ResultPanel.xaml.cs">
|
||||
<DependentUpon>ResultPanel.xaml</DependentUpon>
|
||||
@@ -272,6 +264,10 @@
|
||||
<AppDesigner Include="Properties\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Wox.Core\Wox.Core.csproj">
|
||||
<Project>{B749F0DB-8E75-47DB-9E5E-265D16D0C0D2}</Project>
|
||||
<Name>Wox.Core</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Wox.Infrastructure\Wox.Infrastructure.csproj">
|
||||
<Project>{4fd29318-a8ab-4d8f-aa47-60bc241b8da3}</Project>
|
||||
<Name>Wox.Infrastructure</Name>
|
||||
|
||||
Reference in New Issue
Block a user