Add Executable Plugin Loader

This commit is contained in:
qianlifeng
2014-07-05 23:10:34 +08:00
parent f01de3a69d
commit 659ff866e1
17 changed files with 850 additions and 542 deletions

View File

@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wox.Plugin;
namespace Wox.PluginLoader
{
public class ExecutablePluginLoader : BasePluginLoader
{
public override List<PluginPair> LoadPlugin()
{
List<PluginPair> plugins = new List<PluginPair>();
List<PluginMetadata> metadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.ExecutableFile.ToUpper()).ToList();
foreach (PluginMetadata metadata in metadatas)
{
ExecutablePluginWrapper executer = new ExecutablePluginWrapper();
PluginPair pair = new PluginPair()
{
Plugin = executer,
Metadata = metadata
};
plugins.Add(pair);
}
return plugins;
}
}
}

View File

@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Wox.Plugin;
using Wox.RPC;
namespace Wox.PluginLoader
{
public class ExecutablePluginWrapper : IPlugin
{
private PluginInitContext context;
private static string executeDirectory = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();
try
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = Path.Combine(executeDirectory, "PYTHONTHOME\\Scripts\\python.exe");
start.Arguments = string.Format("{0} \"{1}\"",
context.CurrentPluginMetadata.ExecuteFilePath,
RPC.JsonRPC.GetRPC("query", query.GetAllRemainingParameter()));
start.UseShellExecute = false;
start.CreateNoWindow = true;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
if (process != null)
{
using (StreamReader reader = process.StandardOutput)
{
string output = reader.ReadToEnd();
if (!string.IsNullOrEmpty(output))
{
JsonPRCModel rpc = JsonConvert.DeserializeObject<JsonPRCModel>(output);
var rpcresults = JsonConvert.DeserializeObject<List<ActionJsonRPCResult>>(rpc.result);
List<Result> r = new List<Result>();
foreach (ActionJsonRPCResult result in rpcresults)
{
if (!string.IsNullOrEmpty(result.ActionJSONRPC))
{
result.Action = (context) =>
{
return true;
};
}
r.Add(result);
}
return r;
}
}
}
}
}
catch
{
}
return results;
}
public void Init(PluginInitContext context)
{
this.context = context;
}
}
}

View File

@@ -1,96 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.CSharp;
using Wox.Helper;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Infrastructure.Storage.UserSettings;
using Wox.Plugin;
namespace Wox.PluginLoader {
public static class Plugins {
//private static string debuggerMode = null;
public static String DebuggerMode { get; private set; }
namespace Wox.PluginLoader
{
public static class Plugins
{
public static String DebuggerMode { get; private set; }
private static List<PluginPair> plugins = new List<PluginPair>();
private static List<PluginPair> plugins = new List<PluginPair>();
private static ManualResetEvent initializing = null;
public static void Init()
{
plugins.Clear();
BasePluginLoader.ParsePluginsConfig();
public static void Init() {
if (initializing != null) return;
plugins.AddRange(new PythonPluginLoader().LoadPlugin());
plugins.AddRange(new CSharpPluginLoader().LoadPlugin());
plugins.AddRange(new ExecutablePluginLoader().LoadPlugin());
initializing = new ManualResetEvent(false);
plugins.Clear();
BasePluginLoader.ParsePluginsConfig();
plugins.AddRange(new PythonPluginLoader().LoadPlugin());
plugins.AddRange(new CSharpPluginLoader().LoadPlugin());
Forker forker = new Forker();
foreach (IPlugin plugin in plugins.Select(pluginPair => pluginPair.Plugin)) {
IPlugin plugin1 = plugin;
PluginPair pluginPair = plugins.FirstOrDefault(o => o.Plugin == plugin1);
if (pluginPair != null) {
PluginMetadata metadata = pluginPair.Metadata;
pluginPair.InitContext = new PluginInitContext() {
Plugins = plugins,
CurrentPluginMetadata = metadata,
ChangeQuery = s => App.Window.Dispatcher.Invoke(new Action(() => App.Window.ChangeQuery(s))),
CloseApp = () => App.Window.Dispatcher.Invoke(new Action(() => App.Window.CloseApp())),
HideApp = () => App.Window.Dispatcher.Invoke(new Action(() => App.Window.HideApp())),
ShowApp = () => App.Window.Dispatcher.Invoke(new Action(() => App.Window.ShowApp())),
ShowMsg = (title, subTitle, iconPath) => App.Window.Dispatcher.Invoke(new Action(() =>
App.Window.ShowMsg(title, subTitle, iconPath))),
OpenSettingDialog = () => App.Window.Dispatcher.Invoke(new Action(() => App.Window.OpenSettingDialog())),
ShowCurrentResultItemTooltip = (msg) => App.Window.Dispatcher.Invoke(new Action(() => App.Window.ShowCurrentResultItemTooltip(msg))),
ReloadPlugins = () => App.Window.Dispatcher.Invoke(new Action(() => Init())),
InstallPlugin = (filePath) => App.Window.Dispatcher.Invoke(new Action(() => {
PluginInstaller.Install(filePath);
})),
StartLoadingBar = () => App.Window.Dispatcher.Invoke(new Action(() => App.Window.StartLoadingBar())),
StopLoadingBar = () => App.Window.Dispatcher.Invoke(new Action(() => App.Window.StopLoadingBar())),
//ShellRun = (cmd) => (bool)App.Window.Dispatcher.Invoke(new Func<bool>(() => App.Window.ShellRun(cmd)))
};
Forker forker = new Forker();
foreach (IPlugin plugin in plugins.Select(pluginPair => pluginPair.Plugin))
{
IPlugin plugin1 = plugin;
PluginPair pluginPair = plugins.FirstOrDefault(o => o.Plugin == plugin1);
if (pluginPair != null)
{
PluginMetadata metadata = pluginPair.Metadata;
pluginPair.InitContext = new PluginInitContext()
{
CurrentPluginMetadata = metadata,
API = App.Window
};
pluginPair.InitContext.ShellRun = (cmd) => {
try {
return (bool)App.Window.Dispatcher.Invoke(new Func<bool>(() => App.Window.ShellRun(cmd)));
}
catch (Exception) {
return false;
}
};
forker.Fork(() => plugin1.Init(pluginPair.InitContext));
}
}
forker.Fork(() => plugin1.Init(pluginPair.InitContext));
}
}
forker.Join();
}
ThreadPool.QueueUserWorkItem(o => {
forker.Join();
initializing.Set();
initializing = null;
});
}
public static List<PluginPair> AllPlugins
{
get
{
return plugins;
}
}
public static List<PluginPair> AllPlugins {
get {
var init = initializing;
if (init != null) {
init.WaitOne();
}
return plugins;
}
}
public static bool HitThirdpartyKeyword(Query query)
{
if (string.IsNullOrEmpty(query.ActionName)) return false;
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);
}
return plugins.Any(o => o.Metadata.PluginType == PluginType.ThirdParty && o.Metadata.ActionKeyword == query.ActionName);
}
public static void ActivatePluginDebugger(string path) {
DebuggerMode = path;
}
}
public static void ActivatePluginDebugger(string path)
{
DebuggerMode = path;
}
}
}