Cleaned FileSystemPlugin + Enhanced

THIS NEEDS TESTING!
This commit is contained in:
Aaron Campf
2014-05-09 18:24:16 -07:00
parent 498634fd13
commit 81467a86cc
9 changed files with 626 additions and 718 deletions

View File

@@ -18,114 +18,98 @@ using MessageBoxOptions = System.Windows.Forms.MessageBoxOptions;
using StartupEventArgs = System.Windows.StartupEventArgs;
using UnhandledExceptionEventArgs = System.UnhandledExceptionEventArgs;
namespace Wox
{
public static class EntryPoint
{
[STAThread]
public static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
System.Windows.Forms.Application.ThreadException += ErrorReporting.ThreadException;
// don't combine Main and Entry since Microsoft.VisualBasic may be unable to load
// seperating them into two methods can make error reporting have the chance to catch exception
Entry(args);
}
namespace Wox {
public static class EntryPoint {
[STAThread]
public static void Main(string[] args) {
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
System.Windows.Forms.Application.ThreadException += ErrorReporting.ThreadException;
private static void Entry(string[] args){
SingleInstanceManager manager = new SingleInstanceManager();
manager.Run(args);
}
}
// don't combine Main and Entry since Microsoft.VisualBasic may be unable to load
// seperating them into two methods can make error reporting have the chance to catch exception
Entry(args);
}
// Using VB bits to detect single instances and process accordingly:
// * OnStartup is fired when the first instance loads
// * OnStartupNextInstance is fired when the application is re-run again
// NOTE: it is redirected to this instance thanks to IsSingleInstance
public class SingleInstanceManager : WindowsFormsApplicationBase
{
App app;
public SingleInstanceManager()
{
this.IsSingleInstance = true;
}
private static void Entry(string[] args) {
SingleInstanceManager manager = new SingleInstanceManager();
manager.Run(args);
}
}
protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
{
// First time app is launched
app = new App();
// Using VB bits to detect single instances and process accordingly:
// * OnStartup is fired when the first instance loads
// * OnStartupNextInstance is fired when the application is re-run again
// NOTE: it is redirected to this instance thanks to IsSingleInstance
[System.Diagnostics.DebuggerStepThrough]
public class SingleInstanceManager : WindowsFormsApplicationBase {
App app;
public SingleInstanceManager() {
this.IsSingleInstance = true;
}
protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e) {
// First time app is launched
app = new App();
//app.InitializeComponent();
app.Run();
return true;
}
app.Run();
return true;
}
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
{
// Subsequent launches
base.OnStartupNextInstance(eventArgs);
app.Activate(eventArgs.CommandLine.ToArray());
}
}
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs) {
// Subsequent launches
base.OnStartupNextInstance(eventArgs);
app.Activate(eventArgs.CommandLine.ToArray());
}
}
public partial class App : Application
{
public partial class App : Application {
private static MainWindow window;
private static MainWindow window;
public static MainWindow Window
{
get
{
return window;
}
}
public static MainWindow Window {
get {
return window;
}
}
protected override void OnStartup(StartupEventArgs e)
{
this.DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
protected override void OnStartup(StartupEventArgs e) {
this.DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
base.OnStartup(e);
base.OnStartup(e);
//for install plugin command when wox didn't start up
//we shouldn't init MainWindow, just intall plugin and exit.
if (e.Args.Length > 0 && e.Args[0].ToLower() == "installplugin")
{
var path = e.Args[1];
if (!File.Exists(path))
{
MessageBox.Show("Plugin " + path + " didn't exist");
return;
}
PluginInstaller.Install(path);
Environment.Exit(0);
return;
}
//for install plugin command when wox didn't start up
//we shouldn't init MainWindow, just intall plugin and exit.
if (e.Args.Length > 0 && e.Args[0].ToLower() == "installplugin") {
var path = e.Args[1];
if (!File.Exists(path)) {
MessageBox.Show("Plugin " + path + " didn't exist");
return;
}
PluginInstaller.Install(path);
Environment.Exit(0);
return;
}
if (e.Args.Length > 0 && e.Args[0].ToLower() == "plugindebugger")
{
var path = e.Args[1];
PluginLoader.Plugins.ActivatePluginDebugger(path);
}
if (e.Args.Length > 0 && e.Args[0].ToLower() == "plugindebugger") {
var path = e.Args[1];
PluginLoader.Plugins.ActivatePluginDebugger(path);
}
window = new MainWindow();
if (e.Args.Length == 0 || e.Args[0].ToLower() != "hidestart")
{
window.ShowApp();
}
window = new MainWindow();
if (e.Args.Length == 0 || e.Args[0].ToLower() != "hidestart") {
window.ShowApp();
}
window.ParseArgs(e.Args);
}
window.ParseArgs(e.Args);
}
public void Activate(string[] args)
{
if (args.Length == 0 || args[0].ToLower() != "hidestart")
{
window.ShowApp();
}
window.ParseArgs(args);
}
}
public void Activate(string[] args) {
if (args.Length == 0 || args[0].ToLower() != "hidestart") {
window.ShowApp();
}
window.ParseArgs(args);
}
}
}

View File

@@ -9,117 +9,101 @@ using Wox.Helper;
using Wox.Plugin;
using Wox.Plugin.SystemPlugins;
namespace Wox.PluginLoader
{
public abstract class BasePluginLoader
{
private static string PluginPath = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Plugins");
private static string PluginConfigName = "plugin.json";
protected static List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
public abstract List<PluginPair> LoadPlugin();
namespace Wox.PluginLoader {
public abstract class BasePluginLoader {
private static string PluginPath = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Plugins");
private static string PluginConfigName = "plugin.json";
protected static List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
public abstract List<PluginPair> LoadPlugin();
public static void ParsePluginsConfig()
{
pluginMetadatas.Clear();
ParseSystemPlugins();
ParseThirdPartyPlugins();
public static void ParsePluginsConfig() {
pluginMetadatas.Clear();
ParseSystemPlugins();
ParseThirdPartyPlugins();
if (Plugins.DebuggerMode != null)
{
PluginMetadata metadata = GetMetadataFromJson(Plugins.DebuggerMode);
if (metadata != null) pluginMetadatas.Add(metadata);
}
}
if (Plugins.DebuggerMode != null) {
PluginMetadata metadata = GetMetadataFromJson(Plugins.DebuggerMode);
if (metadata != null) pluginMetadatas.Add(metadata);
}
}
private static void ParseSystemPlugins()
{
PluginMetadata metadata = new PluginMetadata();
metadata.Name = "System Plugins";
metadata.Author = "System";
metadata.Description = "system plugins collection";
metadata.Language = AllowedLanguage.CSharp;
metadata.Version = "1.0";
metadata.PluginType = PluginType.System;
metadata.ActionKeyword = "*";
metadata.ExecuteFileName = "Wox.Plugin.SystemPlugins.dll";
metadata.PluginDirecotry = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
pluginMetadatas.Add(metadata);
}
private static void ParseSystemPlugins() {
pluginMetadatas.Add(new PluginMetadata() {
Name = "System Plugins",
Author = "System",
Description = "system plugins collection",
Language = AllowedLanguage.CSharp,
Version = "1.0",
PluginType = PluginType.System,
ActionKeyword = "*",
ExecuteFileName = "Wox.Plugin.SystemPlugins.dll",
PluginDirecotry = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath)
});
}
private static void ParseThirdPartyPlugins()
{
if (!Directory.Exists(PluginPath))
Directory.CreateDirectory(PluginPath);
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);
}
}
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;
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;
}
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.PluginDirecotry = pluginDirectory;
}
catch (Exception)
{
string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
Log.Warn(error);
try {
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
metadata.PluginType = PluginType.ThirdParty;
metadata.PluginDirecotry = 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);
}
{
throw new WoxException(error);
}
#endif
return null;
}
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 (!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);
}
{
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);
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);
}
{
throw new WoxException(error);
}
#endif
return null;
}
return null;
}
return metadata;
}
}
return metadata;
}
}
}

View File

@@ -1,65 +1,53 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using Wox.Helper;
using Wox.Plugin;
using Wox.Plugin.SystemPlugins;
namespace Wox.PluginLoader
{
public class CSharpPluginLoader : BasePluginLoader
{
public override List<PluginPair> LoadPlugin()
{
List<PluginPair> plugins = new List<PluginPair>();
namespace Wox.PluginLoader {
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("Cound't load plugin {0}: didn't find the class who implement IPlugin",
metadata.Name));
continue;
}
public class CSharpPluginLoader : BasePluginLoader {
foreach (Type type in types)
{
PluginPair pair = new PluginPair()
{
Plugin = Activator.CreateInstance(type) as IPlugin,
Metadata = metadata
};
public override List<PluginPair> LoadPlugin() {
List<PluginPair> plugins = new List<PluginPair>();
var sys = pair.Plugin as BaseSystemPlugin;
if (sys != null)
{
sys.PluginDirectory = metadata.PluginDirecotry;
}
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("Cound't load plugin {0}: didn't find the class who implement IPlugin", metadata.Name));
continue;
}
plugins.Add(pair);
}
}
catch (Exception e)
{
Log.Error(string.Format("Cound't load plugin {0}: {1}", metadata.Name, e.Message));
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.PluginDirecotry;
}
plugins.Add(pair);
}
}
catch (Exception e) {
Log.Error(string.Format("Cound't load plugin {0}: {1}", metadata.Name, e.Message));
#if (DEBUG)
{
throw;
}
{
throw;
}
#endif
}
}
}
}
return plugins;
}
}
}
return plugins;
}
}
}

View File

@@ -24,7 +24,7 @@ namespace Wox.PluginLoader {
initializing = new ManualResetEvent(false);
plugins.Clear();
BasePluginLoader.ParsePluginsConfig();
if (UserSettingStorage.Instance.EnablePythonPlugins) {
plugins.AddRange(new PythonPluginLoader().LoadPlugin());
}

View File

@@ -33,7 +33,7 @@
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>