mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
Refactoring.
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Infrastructure.Exceptions;
|
||||
using Wox.Infrastructure.Logger;
|
||||
@@ -74,7 +75,6 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ErrorReporting.TryShowErrorMessageBox(e.Message, e);
|
||||
Log.Error(e.Message);
|
||||
}
|
||||
}
|
||||
@@ -83,12 +83,12 @@ namespace Wox.Core.Plugin
|
||||
|
||||
private void ExecuteWoxAPI(string method, object[] parameters)
|
||||
{
|
||||
MethodInfo methodInfo = App.Window.GetType().GetMethod(method);
|
||||
if (methodInfo != null)
|
||||
MethodInfo methodInfo = PluginManager.API.GetType().GetMethod(method);
|
||||
if (methodInfo != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
methodInfo.Invoke(App.Window, parameters);
|
||||
methodInfo.Invoke(PluginManager.API, parameters);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -132,7 +132,7 @@ namespace Wox.Core.Plugin
|
||||
string result = reader.ReadToEnd();
|
||||
if (result.StartsWith("DEBUG:"))
|
||||
{
|
||||
System.Windows.Forms.MessageBox.Show(new Form { TopMost = true }, result.Substring(6));
|
||||
MessageBox.Show(new Form { TopMost = true }, result.Substring(6));
|
||||
return "";
|
||||
}
|
||||
if (string.IsNullOrEmpty(result))
|
||||
@@ -142,7 +142,8 @@ namespace Wox.Core.Plugin
|
||||
string error = errorReader.ReadToEnd();
|
||||
if (!string.IsNullOrEmpty(error))
|
||||
{
|
||||
ErrorReporting.TryShowErrorMessageBox(error, new WoxJsonRPCException(error));
|
||||
//todo:
|
||||
// ErrorReporting.TryShowErrorMessageBox(error, new WoxJsonRPCException(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace Wox.Core.Plugin
|
||||
ParseSystemPlugins();
|
||||
foreach (string pluginDirectory in pluginDirectories)
|
||||
{
|
||||
ParseThirdPartyPlugins(pluginDirectory);
|
||||
ParseUserPlugins(pluginDirectory);
|
||||
}
|
||||
|
||||
if (PluginManager.DebuggerMode != null)
|
||||
@@ -56,7 +56,7 @@ namespace Wox.Core.Plugin
|
||||
});
|
||||
}
|
||||
|
||||
private static void ParseThirdPartyPlugins(string pluginDirectory)
|
||||
private static void ParseUserPlugins(string pluginDirectory)
|
||||
{
|
||||
|
||||
string[] directories = Directory.GetDirectories(pluginDirectory);
|
||||
@@ -88,7 +88,7 @@ namespace Wox.Core.Plugin
|
||||
try
|
||||
{
|
||||
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
||||
metadata.PluginType = PluginType.ThirdParty;
|
||||
metadata.PluginType = PluginType.User;
|
||||
metadata.PluginDirectory = pluginDirectory;
|
||||
}
|
||||
catch (Exception)
|
||||
|
||||
@@ -15,8 +15,10 @@ namespace Wox.Core.Plugin
|
||||
public static class PluginManager
|
||||
{
|
||||
public static String DebuggerMode { get; private set; }
|
||||
public static IPublicAPI API { get; private set; }
|
||||
|
||||
private static List<PluginPair> plugins = new List<PluginPair>();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Directories that will hold Wox plugin directory
|
||||
/// </summary>
|
||||
@@ -26,10 +28,14 @@ namespace Wox.Core.Plugin
|
||||
{
|
||||
pluginDirectories.Add(
|
||||
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Plugins"));
|
||||
pluginDirectories.Add(
|
||||
Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), ".Wox"),"Plugins"));
|
||||
|
||||
MakesurePluginDirectoriesExist();
|
||||
string userProfilePath = Environment.GetEnvironmentVariable("USERPROFILE");
|
||||
if (userProfilePath != null)
|
||||
{
|
||||
pluginDirectories.Add(Path.Combine(Path.Combine(userProfilePath, ".Wox"), "Plugins"));
|
||||
}
|
||||
|
||||
MakesurePluginDirectoriesExist();
|
||||
}
|
||||
|
||||
private static void MakesurePluginDirectoriesExist()
|
||||
@@ -46,8 +52,9 @@ namespace Wox.Core.Plugin
|
||||
/// <summary>
|
||||
/// Load and init all Wox plugins
|
||||
/// </summary>
|
||||
public static void Init()
|
||||
public static void Init(IPublicAPI api)
|
||||
{
|
||||
API = api;
|
||||
plugins.Clear();
|
||||
|
||||
List<PluginMetadata> pluginMetadatas = PluginConfig.Parse(pluginDirectories);
|
||||
@@ -61,11 +68,16 @@ namespace Wox.Core.Plugin
|
||||
{
|
||||
CurrentPluginMetadata = pair.Metadata,
|
||||
Proxy = HttpProxy.Instance,
|
||||
API = App.Window
|
||||
API = API
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
public static void Query(Query query)
|
||||
{
|
||||
QueryDispatcher.QueryDispatcher.Dispatch(query);
|
||||
}
|
||||
|
||||
public static List<PluginPair> AllPlugins
|
||||
{
|
||||
get
|
||||
@@ -74,11 +86,11 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
}
|
||||
|
||||
public static bool HitThirdpartyKeyword(Query query)
|
||||
public static bool IsUserPluginQuery(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.User && o.Metadata.ActionKeyword == query.ActionName);
|
||||
}
|
||||
|
||||
public static void ActivatePluginDebugger(string path)
|
||||
|
||||
7
Wox.Core/Plugin/QueryDispatcher/IQueryDispatcher.cs
Normal file
7
Wox.Core/Plugin/QueryDispatcher/IQueryDispatcher.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Wox.Core.Plugin.QueryDispatcher
|
||||
{
|
||||
internal interface IQueryDispatcher
|
||||
{
|
||||
void Dispatch(Wox.Plugin.Query query);
|
||||
}
|
||||
}
|
||||
21
Wox.Core/Plugin/QueryDispatcher/QueryDispatcher.cs
Normal file
21
Wox.Core/Plugin/QueryDispatcher/QueryDispatcher.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
namespace Wox.Core.Plugin.QueryDispatcher
|
||||
{
|
||||
internal static class QueryDispatcher
|
||||
{
|
||||
private static IQueryDispatcher pluginCmd = new UserPluginQueryDispatcher();
|
||||
private static IQueryDispatcher systemCmd = new SystemPluginQueryDispatcher();
|
||||
|
||||
public static void Dispatch(Wox.Plugin.Query query)
|
||||
{
|
||||
if (PluginManager.IsUserPluginQuery(query))
|
||||
{
|
||||
pluginCmd.Dispatch(query);
|
||||
}
|
||||
else
|
||||
{
|
||||
systemCmd.Dispatch(query);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
using Wox.Plugin;
|
||||
using Wox.Plugin.SystemPlugins;
|
||||
|
||||
namespace Wox.Core.Plugin.QueryDispatcher
|
||||
{
|
||||
public class SystemPluginQueryDispatcher : IQueryDispatcher
|
||||
{
|
||||
private IEnumerable<PluginPair> allSytemPlugins = PluginManager.AllPlugins.Where(o => o.Metadata.PluginType == PluginType.System);
|
||||
|
||||
public void Dispatch(Query query)
|
||||
{
|
||||
var queryPlugins = allSytemPlugins;
|
||||
if (UserSettingStorage.Instance.WebSearches.Exists(o => o.ActionWord == query.ActionName && o.Enabled))
|
||||
{
|
||||
//websearch mode
|
||||
queryPlugins = new List<PluginPair>()
|
||||
{
|
||||
allSytemPlugins.First(o => ((ISystemPlugin)o.Plugin).ID == "565B73353DBF4806919830B9202EE3BF")
|
||||
};
|
||||
}
|
||||
|
||||
foreach (PluginPair pair in queryPlugins)
|
||||
{
|
||||
PluginPair pair1 = pair;
|
||||
ThreadPool.QueueUserWorkItem(state =>
|
||||
{
|
||||
List<Result> results = pair1.Plugin.Query(query);
|
||||
results.ForEach(o => { o.AutoAjustScore = true; });
|
||||
|
||||
PluginManager.API.PushResults(query, pair1.Metadata, results);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
47
Wox.Core/Plugin/QueryDispatcher/UserPluginQueryDispatcher.cs
Normal file
47
Wox.Core/Plugin/QueryDispatcher/UserPluginQueryDispatcher.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin.QueryDispatcher
|
||||
{
|
||||
public class UserPluginQueryDispatcher : IQueryDispatcher
|
||||
{
|
||||
public void Dispatch(Query query)
|
||||
{
|
||||
PluginPair userPlugin = PluginManager.AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.ActionName);
|
||||
if (userPlugin != null && !string.IsNullOrEmpty(userPlugin.Metadata.ActionKeyword))
|
||||
{
|
||||
var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == userPlugin.Metadata.ID);
|
||||
if (customizedPluginConfig != null && customizedPluginConfig.Disabled)
|
||||
{
|
||||
//need to stop the loading animation
|
||||
PluginManager.API.StopLoadingBar();
|
||||
return;
|
||||
}
|
||||
|
||||
ThreadPool.QueueUserWorkItem(t =>
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Result> results = userPlugin.Plugin.Query(query) ?? new List<Result>();
|
||||
PluginManager.API.PushResults(query,userPlugin.Metadata,results);
|
||||
}
|
||||
catch (Exception queryException)
|
||||
{
|
||||
Log.Error(string.Format("Plugin {0} query failed: {1}", userPlugin.Metadata.Name,
|
||||
queryException.Message));
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
What does Wox.Core do?
|
||||
|
||||
* Handle Query
|
||||
* Loading Plugins
|
||||
* Loading Plugins (including system plugin and user plugin)
|
||||
@@ -18,7 +18,7 @@
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<OutputPath>..\Output\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
@@ -38,12 +38,17 @@
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Plugin\QueryDispatcher\IQueryDispatcher.cs" />
|
||||
<Compile Include="Plugin\QueryDispatcher\QueryDispatcher.cs" />
|
||||
<Compile Include="Plugin\QueryDispatcher\UserPluginQueryDispatcher.cs" />
|
||||
<Compile Include="Plugin\QueryDispatcher\SystemPluginQueryDispatcher.cs" />
|
||||
<Compile Include="Plugin\JsonRPCPlugin.cs" />
|
||||
<Compile Include="Plugin\JsonRPCPluginLoader.cs" />
|
||||
<Compile Include="Plugin\CSharpPluginLoader.cs" />
|
||||
|
||||
Reference in New Issue
Block a user