mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 18:57:19 +02:00
Merge PowerLauncher branch (#2345)
* minor modifications in README * Added launcher project and changed references to common * Added the code to launch another process * added shellapi header * Added launcher to runner * added functions to remove the abstract class error * added the wox launcher dll * Readded the reference to common * modified the additional include directories for the release version * changed the name to be displayed from Wox.Launcher to Launcher * Localized the strings of Launcher * Added a SearchResult Helper class * Created a helper class to use indexer and return search results * Added the main and settings file which declare most of the plugin interfaces * Added references and renamed a class * Added the images folder which contains the windows indexer icon * Added the image * Added the plugin.json file * Minor changes in project files * Added plugin.json to the project * Modified the output paths to create the dll for the plugin in the correct location * Refactored the code to make it unit testable * Made the code unit testable and added unit tests for the indexer plugin * Removed commented out code * Code to terminate wox when we exit PowerToys * Copy from Jeremy's fork PR * Removed unnecessary plugins from source tree * add missing files * Fix postbuild event * Added x64 for all Wox project - Debug mode * Removed the AnyCPU config - Debug * Added the build paths for release x64 (removed AnyCPU) * Set warning level to 4 : Release * Set warning level 4 : Debug * set optimize code to false * Removed notify icon component * Removed setting UI XAML file and references * Readded necessary callback functions * Removed python path and python plugin functions * Removed UI related to python path and the bindings related to that * Remove python bindings * Removed the localized string translations from pythonDirectory and SelectPythonDirectory * Manually resolving conflicts * Fix Wox build in project settings * deleting unused project files * Undo change internal * Fix internal variable * All wox exceptions in debug are getting rethrown, and thus causing the app to crash. This change removes the debug specific code and allows exceptions to be handled the same was as release. * Ported Indexer plugin to .net core 3 * Added the test project back * Removed the .net framework indexer folder * readding the plugin.json file * Changed the anyCPU config to x64 for windows indexer plugin * Adding dependency to wox project on the runner. This should make sure the wox.exe can be build and launched by the runner so wox can run as a background process. * Updating build dependencies. Wox.Launcher wasn't built as part of the F5 Experience, and nor were the plugins * removing locks from the ResultListBox code behind file. All callbacks are accessed from the Main/UI thread. * Remove anyCPU config and changed it to x64 * Removed anycpu of test proj * Adding dependency * Renaming executable to PowerLauncher. Replaceing icons with placeholder. Deleting Docs folder * Renaming AppData directory from 'Wox' to 'PowerLauncher'. Also replacing issue link with powertoys github. * adding support for xaml islands * Added Neils UI code * Replace Niels Code references * Added assets and behaviours * Add missing reference * Add main view model binding * Using proper executable name when closing 'PowerLauncher' process * changed x:bind to Binding to avoid reference * Updated bindings for launcher * Added binding on searchBox * Adding Directory.Build.targets file to kill the PowerLauncher pprocess on Build or Clean operations of all 'Launcher' projects. * Fixed exception preventing result display * Fixed issue with wpf marshalling events to a non UI thread * Optimised result binding by inserting search result from multiple plugin in parallel * copy resource files to output folder * Corrected the output path for the indexer plugin * windows indexer plugin is working * Remove console print statement * Added callback function for mouse click on search result * Working App execution * Cherry picked pinyin performance changes from jjw24's master branch * change nuget package to msft for winrt compat * Working up/down arrow key * updating references and removing a few that seem uneeded for how stuff is referenced * adding two back in * Removing Squirrel dll. unsure on updating so i kept that logic in * Updated functionality on suggestion chosen in autosuggestbox * Added the <useWPF> tag to remove the warning * Removed an unnecessary <useWindowsForms> tag * Removed the item group for properties from calculator plugin * Removed the item group for properties from folder plugin * Removed the item group for properties from indexer, program and shell plugin * Removed itemgrp from wox.core and wox.test csprojs * Removed the unnecessary wox files to clean up codebase * Renamed Wox.Plugin.Indexer to Microsoft.Plugin.Indexer * Renamed Wox.Launcher to Microsoft.Launcher * To avoid DBNull to String typecast exception * Added query submitted event to handle default action on clicking a list view item * Merge pull request #42 from microsoft/AddPinyinPerformance Cherry picked pinyin performance changes from jjw24's master branch (cherry picked from commitb9e437c6cd) * Rectyfying title display * Title display working correctly * Removed .yml files * adding checks * Making wox.csproj build wox assembly instead of powerlauncher * Removing update logic from PowerLuanch App.xaml.cs as was done in Wox project. * Making Query internals visible to wox. * Update Powerlauncher nuget packages to be same version as wox.csproj. Note: FoxyWeavers.xsd change is automatically done as part of nuget package update. * Updating build dependencies to so that wox.launcher depends on powerlauncher.csproj not wox.csproj * Removing 'Wox' branded logos from powerlauncher.csproj as was done previously or wox.csproj * Downgraded the library to the latest stable version * single thread execution of the indexer plugin and InvalidOperationException due to connection being closed on ExecuteReader, handled separately * Modified the test, the connection need not be null after being disposed. There is no direct way of checking if an object has been disposed other than to throw the InvalidOperationException * Removing x86,ARM,ARM64, build configurations, as these were added accidentally when adding xaml island support. * Removed STAThread * Modified the output folder produced * Renamed the dll produced * Added dependency on PowerLauncher to the Microsoft.Launcher project * modified the name of the dll in the indexer plugin * Ignoring 128 errors from taskkill, as this means the process isn't running. We don't want htis to show up in the warnings list on build * Added fix for closing wox on pressing escape and app execution (#75) * adding useWPF to get rid of warning * Launcher resize issue on selecting search result (#77) * Added fix for closing wox on pressing escape and app execution * Added fix to prevent autosuggestbox resize * Fixing xaml catastrophic failure, based on Miguels suggestion here: https://github.com/windows-toolkit/Microsoft.Toolkit.Win32/issues/210 * suggested fix * removing unused mutex string * Searches for keyword only in title and not content to improve the quality of results * Display the title from System.Title directly instead of extracting it from the path * Removed additional README instructions before moving to the powerToys repo * Fix Query builder test (#86) * Revert base viewModel class to fix tests. * Removing unused post build and deploy scripts. * tweaked url * Update plugin.json * Update README.md * removed unused dep (#2080) * removed reference to everything (#2133) * SearchIndexer - Modifying QueryContentProperties and QuerySelectColumns to use System.FileName * Search result thumbnail for PowerLauncher (#2124) * Replace WPF Imaging library with UWP * Removed UWP and WPF namespace conflicting files from Wox * Removed Image hashing as it wasn't used anywhere * Updated formatting * Set MainViewModel visibility to hidden on startup * Enable CI build on PowerLauncher Branch (#2181) * Enable CI build on PowerLauncher Branch * Updated Nuget restore to latest and added AssetTargetFallback property * tweak text (#2177) * [Window Walker] Migrate to Launcher (#2093) * Copy the existing calc. plugin foldeR * Blindly rename a bunch of things to Window Walker * Update the solution to reference the new plugin * Get basic Yo returned * Remove all the languages except english * Lower quality of icon as well as test showing it * Add the core non-ui parts to the plugin * Delete calculator png * Get it to compile * Added the actual code which apparently doesn't work * Finally start showing results * Fix up strings * Switch working * Remove unused classes * Remove unneeded async * Added MSI support for Launcher (#2242) * Fixed typo in output folder of calculator plugin * Calculator plugin works * Modified the name of the image to remove space as space is assumed to be a separator in wcx file * Fixed typo in calculator dll * Fixed typo in calculator plugin * Shell, Program, Folde and indexer plugin working * Added the ww plugin * Fixed typos in the calculator plugin * Delete file that was added unintentionally * revert calculator rename changes * Reverted other files changed * Reverted renaming of file with space * Pull changes from master to dev/powerLauncher (#2255) * Dpi unaware placement bug (#2121) Fix for bug when placing dpi unaware window such as Notepad++ in left of right part of monitor. In that application gap of about 7px was left or right. This fixes only single-monitor scenario It skips correction for dpi unaware window that leaves a gap * Move markdown parsing logic outside control thread (#2099) * Move markdown parsing logic outside control thread * Update MarkdownPreviewHandlerControl.cs * Remove trailing whitespace. That'll teach me for trying to make an edit from the GitHub page. * Migrate power rename MRU lists from registry to JSON (#2090) * Handle most recently used search/replace strings within settings. * Check for last modified time of json file and reload it if needed. * Handle changes in MRU search / replace lists size. * Improve handling of changes in MRU list size. * Don't check for last modified time in every getter method. Load only when starting application. * Add const identifier to getter methods. * Address PR comments: Add const to reg and json file paths and set them in constructor initializer. Check pushIdx validity. Move implementation to cpp of PowerRenameUI constructor. * Add error checking when getting values from registry. * Implementing changes suggested in #1992 (#2116) * Implementing changes suggested in #1992 * Update Product.wxs Co-authored-by: Ebenezer Ewumi <ebenezer.ewumi@wsu.edu> * Fix for issue #1532 - [PowerToys tray icon] Show version on tooltip (#2117) * Fix for issue #1532 [PowerToys] Show version on tooltip * Update src/runner/tray_icon.cpp Co-Authored-By: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com> Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com> * FZ editor: Splitted zones positioning (#2158) * Added a mutex to ZoneWindow, ensured no data races occur (#2154) * Added a mutex to ZoneWindow, ensured no data races occur * Protected draggedWindow* members with a mutex * Ensured that critical reads happen in a single transaction * Dpi unaware placement bug - multimontior with same DPI settings fix (#2156) * Dpi unaware placement bug - multimontior with same DPI settings fix * Using different enumerating method * Changed AllMonitorHaveSameDpiScaling method * Removed accidental file * small rename * Changed some methods to CamelCase * Review comments fixes Co-authored-by: PrzemyslawTusinski <61138537+PrzemyslawTusinski@users.noreply.github.com> Co-authored-by: Ben Randall <veleek@gmail.com> Co-authored-by: vldmr11080 <57061786+vldmr11080@users.noreply.github.com> Co-authored-by: eduardodextil <55205162+eduardodextil@users.noreply.github.com> Co-authored-by: Ebenezer Ewumi <ebenezer.ewumi@wsu.edu> Co-authored-by: Nghia M. Luong <32159519+sqrlmn@users.noreply.github.com> Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com> Co-authored-by: Seraphima Zykova <zykovas91@gmail.com> Co-authored-by: Ivan Stošić <ivan100sic@gmail.com> * Somil55/merge custom ui into launcher (#2271) * Remove Autosuggest box (#2192) * Update Settings.Designer.cs * Revert "Update Settings.Designer.cs" This reverts commita1bc0dda56. * Updated LauncherControl XAML to add textbox and listview * List View displayed * Hooking up execution on the selected index, removing two way binding on selection, and experimenting with popup that doesn't work * Updated MainViewModel to Remove context menu and history * Added Resultist XAML Island project * Updated SelectedItem and SelectedList Binding. Issues : List box doesn't open when query is written for first time but opens in subsequent queries. * 1. Mouse Click working 2. List View is can't be focused 3. Fixed width of Launcher * Removed two way QueryText box binding * Removed SelectedItem two way binding and replaced with a callback * [Cleaning] Remove redundant UWP project * [Cleaning] Updated files to keep only atomic changes against dev/powerLauncher * Thmbnail fixed for NEW UI * Removed PreviewMouseDown function required by older WOX code Co-authored-by: ryanbodrug-microsoft <56318517+ryanbodrug-microsoft@users.noreply.github.com> * Added the auto-complete feature * Removing ContextMenuPluginInfo, and ContextMenuTopMost as these commands are not used int the new design. * Fixed merge conflicts * Set only when index is 0 * One way binding * Removed unnecessary binding * Deleting unused (commented out code) that was legacy from wox project. * Binding Buttons to appropriate context menu commands. 1. Buttons are dynamically loaded in a listview based on the actions supported be each plugin. This change also deletes unused commands. Note: Most button events don't seem to be getting routed to the Selected Item. Currently using 'PointerEntered' to validate the behavior. The actions should be trigged by the button command property in the future. * manually handling tab in mainwindow * Loading context buttons on Selecting a suggestion list item * Allowing hover event to load content menu items and display them as well. * Adding context buttons to Indexer plugin. This allows for the following: 1. [Files] Open Containing folder 2. [Folders/Files] Copy Path * Remove White background of list (#2218) * Remove white background of list * Removed comments * Changed to ContainerContentChanging event * add const variables instead of numbers * Added comment before the updatelistSize function * Search box UI (#2224) * Added backdrop and rounded corner * Fix for two alt+space press to bring searchbox issue * Fixed merge conflict * Clean Mainwindow.xaml code * Fix for textbox focus on first visible * Allowing users to tab between the context buttons for the selected resut. Also allowing users to press 'enter' to action on the selected items. * Renaming SelectedIndex to ContextMenuSelectedIndex * Enabling key accelerators on context buttons. 1. Add new object ContextMenuResult instead instead of reusing Result for both query results and context menu results. 2. Binding KeyboardAccelerator keys to contextmenuitemviewmodel 3. Enabling and disabling contextmenu items when selecting or deselecting each row. Because we are manually maintaining selectionwe can't use ScopeOwners as the textbox is really the only item ever in focus. * Launching explorer instead of the UWP application when selecting 'open file location'. * Added fix for border showing up when result count is zero * Updated fix for border on no result * Adding visibility after clearing result in MainViewmodel * Launcher Light/Dark mode (#2235) * Fixed issue with list view background not updating with Windows theme change * Added theme change for WPF * updated ShadowDepth for dropshadow * Updated border thicknes of searchbox and listview * Diff issue with ResultList.xaml * Removed change in result delay * Added code to pull colors from UWP * Updated border resource to use system based SystemControlHighlightAccentBrush * Updated corner radius in dark mode * Updated Launcher description text Co-authored-by: ryanbodrug-microsoft <56318517+ryanbodrug-microsoft@users.noreply.github.com> Co-authored-by: Alekhya Reddy <reddykalekhya@gmail.com> * Changed to SystemChromeLow from Accent brush (#2272) * Removed ListView animations * Positioning ContextButtons and adding background. * Disabling scrollbars for gridview items. * Removed folder plugin * removed deployment for uwp apps (#2298) * Simulating Win+backspace key press, instead of Win+Control as it was launching WW (#2250) * text is selected whenever launcher gets hidden and is then set to visible (#2315) * Update nuget packages across solution to latest (#2334) * simplifying the xaml and making things tighter (#2327) * Setting runcommand as the default execution method, and turning off run as admin by default. * Opening explorer if the user has types a file path. * Enable PowerLauncher toggle * Launcher MSI - Added a required dll and removed folder plugin (#2355) * Modified the product.wxs file * Added the x64 config * Modify scoring algorithm for fuzzy search (#2361) * Modify scoring * modified to if else * Fixes race conditions with PointerEnter/Exit events conflicting with Selection and unselection. This change provides better encapsulation of the logic to enable a selected item for accelerator (hotkey) events, and allow mouse input on results where the pointer is over. * Fixes an issue where PointerExit would hide the selected context buttons. * Result List - Bad rebase overwrote margin and translation fields. * Fixed process executing on clicking enter if no text in search box * Codeflow cleanup when selectedItem is not null * adjusting how programs are displayed. (#2369) * Removing description from title * adjusting subtitle * removing accidently paste * removing desc for uwp apps * Revert "Removed folder plugin" This reverts commit064d638588. We will use the folder plugin to better mimic the way the start menu and run prompt deal with directories * Updating JetBrains.Annotations to 2020.1.0 to be consistent with other projects. * Replacing submenu text with full path as 'Ctrl-Enter' doesn't do anything. Also reducing the String of CreateOpenCurrentResult to fit in one line. * - Making Open Directory subtitle fixed, and the title as the current directory. * Adding back binding to QueryText and updating the cursor position similar to how wox originally did it. * Add the folder plugin wxs back in (#2374) * Deduping results for program plugin (#2375) * Removing description from title * adjusting subtitle * removing accidently paste * removing desc for uwp apps * Getting dups removed from list if LNK exists * adjusting subtitle * removing accidently paste * Getting dups removed from list if LNK exists * changed to normal forloop * Removing WinR (#2381) * Updated program execution to call action on background thread. (#2370) * PowerLauncher Settings integration * Added cold start fix (#2385) * - Fixes cursor jumping around issue. - Seperating the ability to set the text from initiating a query. - Plugins have to explicitly request the query be updated. - Updating Folder plugin to explicty update the query on folder selection. - Removing unused changes from 'Wox' that don't compile. * Fixing gap in logic where query was triggering when programatically setting text. Updating the binding and settext both will trigger the TextChanged event on a seperate event dispatcher. For this reason we dynamically detect which eventhandler is most approapriate on the textchanging event. * Updating the QueryText to the selected item when navigating up/down with the arrow keys. * Removing action on folder result, and displaying folder path on selecting a folder result from the folder plugin. * Making folder results from the search indexer plugin behave like folder results from folder plugin. * Folder Results open the explorer window when selected. * The Open Current Folder result shouldn't change the query text to 'Open {folder path}' when selecting the results. * Initializing query text strings. * Defensive check for QueryText being empty * Adding file watchers for UWP detecting when apps are installed or deleted and reindexing the uwp apps * Removing unused namespace. * Looking at files and not filtering is better because the timer is more likely to reset while an install is happening, preventing uneeded indexing * Fix project references * Fix MaxResultsToShow * Fix Alt + Space display * Fix settings defaults Co-authored-by: Alekhya Reddy Kommuru <reddykalekhya@gmail.com> Co-authored-by: bkudiess <bakudies@microsoft.com> Co-authored-by: Divyansh <somm14divi@gmail.com> Co-authored-by: ryanbodrug-microsoft <56318517+ryanbodrug-microsoft@users.noreply.github.com> Co-authored-by: Barbara Kudiess <bkudiess@me.com> Co-authored-by: Jeremy Wu <jeremy24wu@gmail.com> Co-authored-by: Clint Rutkas <clint@rutkas.com> Co-authored-by: udit3333 <udit3333@gmail.com> Co-authored-by: Betsegaw (Beta) Tadele <betsegaw.ta@gmail.com> Co-authored-by: PrzemyslawTusinski <61138537+PrzemyslawTusinski@users.noreply.github.com> Co-authored-by: Ben Randall <veleek@gmail.com> Co-authored-by: vldmr11080 <57061786+vldmr11080@users.noreply.github.com> Co-authored-by: eduardodextil <55205162+eduardodextil@users.noreply.github.com> Co-authored-by: Ebenezer Ewumi <ebenezer.ewumi@wsu.edu> Co-authored-by: Nghia M. Luong <32159519+sqrlmn@users.noreply.github.com> Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com> Co-authored-by: Seraphima Zykova <zykovas91@gmail.com> Co-authored-by: Ivan Stošić <ivan100sic@gmail.com>
This commit is contained in:
committed by
GitHub
parent
96b2145ba1
commit
d9c4abe0df
54
src/modules/launcher/Wox.Core/Plugin/ExecutablePlugin.cs
Normal file
54
src/modules/launcher/Wox.Core/Plugin/ExecutablePlugin.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin
|
||||
{
|
||||
internal class ExecutablePlugin : JsonRPCPlugin
|
||||
{
|
||||
private readonly ProcessStartInfo _startInfo;
|
||||
public override string SupportedLanguage { get; set; } = AllowedLanguage.Executable;
|
||||
|
||||
public ExecutablePlugin(string filename)
|
||||
{
|
||||
_startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = filename,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true
|
||||
};
|
||||
}
|
||||
|
||||
protected override string ExecuteQuery(Query query)
|
||||
{
|
||||
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
|
||||
{
|
||||
Method = "query",
|
||||
Parameters = new object[] { query.Search },
|
||||
};
|
||||
|
||||
_startInfo.Arguments = $"\"{request}\"";
|
||||
|
||||
return Execute(_startInfo);
|
||||
}
|
||||
|
||||
protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
|
||||
{
|
||||
_startInfo.Arguments = $"\"{rpcRequest}\"";
|
||||
return Execute(_startInfo);
|
||||
}
|
||||
|
||||
protected override string ExecuteContextMenu(Result selectedResult) {
|
||||
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel {
|
||||
Method = "contextmenu",
|
||||
Parameters = new object[] { selectedResult.ContextData },
|
||||
};
|
||||
|
||||
_startInfo.Arguments = $"\"{request}\"";
|
||||
|
||||
return Execute(_startInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
135
src/modules/launcher/Wox.Core/Plugin/JsonPRCModel.cs
Normal file
135
src/modules/launcher/Wox.Core/Plugin/JsonPRCModel.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
|
||||
/* 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.Core.Plugin
|
||||
{
|
||||
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 + (GetParameterByType(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 GetParameterByType(object parameter)
|
||||
{
|
||||
if (parameter == null) {
|
||||
return "null";
|
||||
}
|
||||
if (parameter is string)
|
||||
{
|
||||
return string.Format(@"\""{0}\""", ReplaceEscapes(parameter.ToString()));
|
||||
}
|
||||
if (parameter is int || parameter is float || parameter is double)
|
||||
{
|
||||
return string.Format(@"{0}", parameter);
|
||||
}
|
||||
if (parameter is bool)
|
||||
{
|
||||
return string.Format(@"{0}", parameter.ToString().ToLower());
|
||||
}
|
||||
return parameter.ToString();
|
||||
}
|
||||
|
||||
private string ReplaceEscapes(string str)
|
||||
{
|
||||
return str.Replace(@"\", @"\\") //Escapes in ProcessStartInfo
|
||||
.Replace(@"\", @"\\") //Escapes itself when passed to client
|
||||
.Replace(@"""", @"\\""""");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Json RPC Request that Wox sent to client
|
||||
/// </summary>
|
||||
public class JsonRPCServerRequestModel : JsonRPCRequestModel
|
||||
{
|
||||
public override string ToString()
|
||||
{
|
||||
string rpc = base.ToString();
|
||||
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; }
|
||||
}
|
||||
}
|
||||
208
src/modules/launcher/Wox.Core/Plugin/JsonRPCPlugin.cs
Normal file
208
src/modules/launcher/Wox.Core/Plugin/JsonRPCPlugin.cs
Normal file
@@ -0,0 +1,208 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Infrastructure.Exception;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin
|
||||
{
|
||||
/// <summary>
|
||||
/// Represent the plugin that using JsonPRC
|
||||
/// every JsonRPC plugin should has its own plugin instance
|
||||
/// </summary>
|
||||
internal abstract class JsonRPCPlugin : IPlugin, IContextMenu
|
||||
{
|
||||
protected PluginInitContext context;
|
||||
public const string JsonRPC = "JsonRPC";
|
||||
|
||||
/// <summary>
|
||||
/// The language this JsonRPCPlugin support
|
||||
/// </summary>
|
||||
public abstract string SupportedLanguage { get; set; }
|
||||
|
||||
protected abstract string ExecuteQuery(Query query);
|
||||
protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest);
|
||||
protected abstract string ExecuteContextMenu(Result selectedResult);
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
{
|
||||
string output = ExecuteQuery(query);
|
||||
try
|
||||
{
|
||||
return DeserializedResult(output);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"|JsonRPCPlugin.Query|Exception when query <{query}>", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
|
||||
{
|
||||
string output = ExecuteContextMenu(selectedResult);
|
||||
try
|
||||
{
|
||||
//This should not hit. If it does it's because Wox shares the same interface for querying context menu items as well as search results. In this case please file a bug.
|
||||
//To my knowledge we aren't supporting this JSonRPC commands in Launcher, and am not able to repro this, but I will leave this here for the time being in case I'm proven wrong.
|
||||
//We should remove this, or identify and test officially supported use cases and Deserialize this properly.
|
||||
//return DeserializedResult(output);
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"|JsonRPCPlugin.LoadContextMenus| THIS IS A BUG - Exception on result <{selectedResult}>", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private List<Result> DeserializedResult(string output)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(output))
|
||||
{
|
||||
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
|
||||
{
|
||||
string actionReponse = ExecuteCallback(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;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void ExecuteWoxAPI(string method, object[] parameters)
|
||||
{
|
||||
MethodInfo methodInfo = PluginManager.API.GetType().GetMethod(method);
|
||||
if (methodInfo != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
methodInfo.Invoke(PluginManager.API, 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 (var process = Process.Start(startInfo))
|
||||
{
|
||||
if (process != null)
|
||||
{
|
||||
using (var standardOutput = process.StandardOutput)
|
||||
{
|
||||
var result = standardOutput.ReadToEnd();
|
||||
if (string.IsNullOrEmpty(result))
|
||||
{
|
||||
using (var standardError = process.StandardError)
|
||||
{
|
||||
var error = standardError.ReadToEnd();
|
||||
if (!string.IsNullOrEmpty(error))
|
||||
{
|
||||
Log.Error($"|JsonRPCPlugin.Execute|{error}");
|
||||
return string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Error("|JsonRPCPlugin.Execute|Empty standard output and standard error.");
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (result.StartsWith("DEBUG:"))
|
||||
{
|
||||
MessageBox.Show(new Form { TopMost = true }, result.Substring(6));
|
||||
return string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Error("|JsonRPCPlugin.Execute|Can't start new process");
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"|JsonRPCPlugin.Execute|Exception for filename <{startInfo.FileName}> with argument <{startInfo.Arguments}>", e);
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public void Init(PluginInitContext ctx)
|
||||
{
|
||||
context = ctx;
|
||||
}
|
||||
}
|
||||
}
|
||||
100
src/modules/launcher/Wox.Core/Plugin/PluginConfig.cs
Normal file
100
src/modules/launcher/Wox.Core/Plugin/PluginConfig.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Infrastructure.Exception;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin
|
||||
{
|
||||
|
||||
internal abstract class PluginConfig
|
||||
{
|
||||
private const string PluginConfigName = "plugin.json";
|
||||
private static readonly List<PluginMetadata> PluginMetadatas = new List<PluginMetadata>();
|
||||
|
||||
/// <summary>
|
||||
/// Parse plugin metadata in giving directories
|
||||
/// </summary>
|
||||
/// <param name="pluginDirectories"></param>
|
||||
/// <returns></returns>
|
||||
public static List<PluginMetadata> Parse(string[] pluginDirectories)
|
||||
{
|
||||
PluginMetadatas.Clear();
|
||||
var directories = pluginDirectories.SelectMany(Directory.GetDirectories);
|
||||
ParsePluginConfigs(directories);
|
||||
return PluginMetadatas;
|
||||
}
|
||||
|
||||
private static void ParsePluginConfigs(IEnumerable<string> directories)
|
||||
{
|
||||
// todo use linq when diable plugin is implmented since parallel.foreach + list is not thread saft
|
||||
foreach (var directory in directories)
|
||||
{
|
||||
if (File.Exists(Path.Combine(directory, "NeedDelete.txt")))
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Delete(directory, true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"|PluginConfig.ParsePLuginConfigs|Can't delete <{directory}>", e);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PluginMetadata metadata = GetPluginMetadata(directory);
|
||||
if (metadata != null)
|
||||
{
|
||||
PluginMetadatas.Add(metadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static PluginMetadata GetPluginMetadata(string pluginDirectory)
|
||||
{
|
||||
string configPath = Path.Combine(pluginDirectory, PluginConfigName);
|
||||
if (!File.Exists(configPath))
|
||||
{
|
||||
Log.Error($"|PluginConfig.GetPluginMetadata|Didn't find config file <{configPath}>");
|
||||
return null;
|
||||
}
|
||||
|
||||
PluginMetadata metadata;
|
||||
try
|
||||
{
|
||||
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
||||
metadata.PluginDirectory = pluginDirectory;
|
||||
// for plugins which doesn't has ActionKeywords key
|
||||
metadata.ActionKeywords = metadata.ActionKeywords ?? new List<string> { metadata.ActionKeyword };
|
||||
// for plugin still use old ActionKeyword
|
||||
metadata.ActionKeyword = metadata.ActionKeywords?[0];
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"|PluginConfig.GetPluginMetadata|invalid json for config <{configPath}>", e);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
if (!AllowedLanguage.IsAllowed(metadata.Language))
|
||||
{
|
||||
Log.Error($"|PluginConfig.GetPluginMetadata|Invalid language <{metadata.Language}> for config <{configPath}>");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!File.Exists(metadata.ExecuteFilePath))
|
||||
{
|
||||
Log.Error($"|PluginConfig.GetPluginMetadata|execute file path didn't exist <{metadata.ExecuteFilePath}> for conifg <{configPath}");
|
||||
return null;
|
||||
}
|
||||
|
||||
return metadata;
|
||||
}
|
||||
}
|
||||
}
|
||||
202
src/modules/launcher/Wox.Core/Plugin/PluginInstaller.cs
Normal file
202
src/modules/launcher/Wox.Core/Plugin/PluginInstaller.cs
Normal file
@@ -0,0 +1,202 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin
|
||||
{
|
||||
internal class PluginInstaller
|
||||
{
|
||||
internal static void Install(string path)
|
||||
{
|
||||
if (File.Exists(path))
|
||||
{
|
||||
string tempFoler = Path.Combine(Path.GetTempPath(), "wox\\plugins");
|
||||
if (Directory.Exists(tempFoler))
|
||||
{
|
||||
Directory.Delete(tempFoler, true);
|
||||
}
|
||||
UnZip(path, tempFoler, true);
|
||||
|
||||
string iniPath = Path.Combine(tempFoler, "plugin.json");
|
||||
if (!File.Exists(iniPath))
|
||||
{
|
||||
MessageBox.Show("Install failed: plugin config is missing");
|
||||
return;
|
||||
}
|
||||
|
||||
PluginMetadata plugin = GetMetadataFromJson(tempFoler);
|
||||
if (plugin == null || plugin.Name == null)
|
||||
{
|
||||
MessageBox.Show("Install failed: plugin config is invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
string pluginFolerPath = Infrastructure.Constant.PluginsDirectory;
|
||||
|
||||
string newPluginName = plugin.Name
|
||||
.Replace("/", "_")
|
||||
.Replace("\\", "_")
|
||||
.Replace(":", "_")
|
||||
.Replace("<", "_")
|
||||
.Replace(">", "_")
|
||||
.Replace("?", "_")
|
||||
.Replace("*", "_")
|
||||
.Replace("|", "_")
|
||||
+ "-" + Guid.NewGuid();
|
||||
string newPluginPath = Path.Combine(pluginFolerPath, newPluginName);
|
||||
string content = $"Do you want to install following plugin?{Environment.NewLine}{Environment.NewLine}" +
|
||||
$"Name: {plugin.Name}{Environment.NewLine}" +
|
||||
$"Version: {plugin.Version}{Environment.NewLine}" +
|
||||
$"Author: {plugin.Author}";
|
||||
PluginPair existingPlugin = PluginManager.GetPluginForId(plugin.ID);
|
||||
|
||||
if (existingPlugin != null)
|
||||
{
|
||||
content = $"Do you want to update following plugin?{Environment.NewLine}{Environment.NewLine}" +
|
||||
$"Name: {plugin.Name}{Environment.NewLine}" +
|
||||
$"Old Version: {existingPlugin.Metadata.Version}" +
|
||||
$"{Environment.NewLine}New Version: {plugin.Version}" +
|
||||
$"{Environment.NewLine}Author: {plugin.Author}";
|
||||
}
|
||||
|
||||
var result = MessageBox.Show(content, "Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question);
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
if (existingPlugin != null && Directory.Exists(existingPlugin.Metadata.PluginDirectory))
|
||||
{
|
||||
//when plugin is in use, we can't delete them. That's why we need to make plugin folder a random name
|
||||
File.Create(Path.Combine(existingPlugin.Metadata.PluginDirectory, "NeedDelete.txt")).Close();
|
||||
}
|
||||
|
||||
UnZip(path, newPluginPath, true);
|
||||
Directory.Delete(tempFoler, true);
|
||||
|
||||
//exsiting plugins may be has loaded by application,
|
||||
//if we try to delelte those kind of plugins, we will get a error that indicate the
|
||||
//file is been used now.
|
||||
//current solution is to restart wox. Ugly.
|
||||
//if (MainWindow.Initialized)
|
||||
//{
|
||||
// Plugins.Initialize();
|
||||
//}
|
||||
if (MessageBox.Show($"You have installed plugin {plugin.Name} successfully.{Environment.NewLine}" +
|
||||
"Restart Wox to take effect?",
|
||||
"Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
|
||||
{
|
||||
PluginManager.API.RestarApp();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
|
||||
{
|
||||
string configPath = Path.Combine(pluginDirectory, "plugin.json");
|
||||
PluginMetadata metadata;
|
||||
|
||||
if (!File.Exists(configPath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
||||
metadata.PluginDirectory = pluginDirectory;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
string error = $"Parse plugin config {configPath} failed: json format is not valid";
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new Exception(error);
|
||||
}
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
if (!AllowedLanguage.IsAllowed(metadata.Language))
|
||||
{
|
||||
string error = $"Parse plugin config {configPath} failed: invalid language {metadata.Language}";
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new Exception(error);
|
||||
}
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
if (!File.Exists(metadata.ExecuteFilePath))
|
||||
{
|
||||
string error = $"Parse plugin config {configPath} failed: ExecuteFile {metadata.ExecuteFilePath} didn't exist";
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new Exception(error);
|
||||
}
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// unzip
|
||||
/// </summary>
|
||||
/// <param name="zipedFile">The ziped file.</param>
|
||||
/// <param name="strDirectory">The STR directory.</param>
|
||||
/// <param name="overWrite">overwirte</param>
|
||||
private static void UnZip(string zipedFile, string strDirectory, bool overWrite)
|
||||
{
|
||||
if (strDirectory == "")
|
||||
strDirectory = Directory.GetCurrentDirectory();
|
||||
if (!strDirectory.EndsWith("\\"))
|
||||
strDirectory = strDirectory + "\\";
|
||||
|
||||
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
|
||||
{
|
||||
ZipEntry theEntry;
|
||||
|
||||
while ((theEntry = s.GetNextEntry()) != null)
|
||||
{
|
||||
string directoryName = "";
|
||||
string pathToZip = "";
|
||||
pathToZip = theEntry.Name;
|
||||
|
||||
if (pathToZip != "")
|
||||
directoryName = Path.GetDirectoryName(pathToZip) + "\\";
|
||||
|
||||
string fileName = Path.GetFileName(pathToZip);
|
||||
|
||||
Directory.CreateDirectory(strDirectory + directoryName);
|
||||
|
||||
if (fileName != "")
|
||||
{
|
||||
if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
|
||||
{
|
||||
using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
|
||||
{
|
||||
byte[] data = new byte[2048];
|
||||
while (true)
|
||||
{
|
||||
int size = s.Read(data, 0, data.Length);
|
||||
|
||||
if (size > 0)
|
||||
streamWriter.Write(data, 0, size);
|
||||
else
|
||||
break;
|
||||
}
|
||||
streamWriter.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
291
src/modules/launcher/Wox.Core/Plugin/PluginManager.cs
Normal file
291
src/modules/launcher/Wox.Core/Plugin/PluginManager.cs
Normal file
@@ -0,0 +1,291 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.Storage;
|
||||
using Wox.Infrastructure.UserSettings;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin
|
||||
{
|
||||
/// <summary>
|
||||
/// The entry for managing Wox plugins
|
||||
/// </summary>
|
||||
public static class PluginManager
|
||||
{
|
||||
private static IEnumerable<PluginPair> _contextMenuPlugins;
|
||||
|
||||
/// <summary>
|
||||
/// Directories that will hold Wox plugin directory
|
||||
/// </summary>
|
||||
|
||||
public static List<PluginPair> AllPlugins { get; private set; }
|
||||
public static readonly List<PluginPair> GlobalPlugins = new List<PluginPair>();
|
||||
public static readonly Dictionary<string, PluginPair> NonGlobalPlugins = new Dictionary<string, PluginPair>();
|
||||
|
||||
public static IPublicAPI API { private set; get; }
|
||||
|
||||
// todo happlebao, this should not be public, the indicator function should be embeded
|
||||
public static PluginsSettings Settings;
|
||||
private static List<PluginMetadata> _metadatas;
|
||||
private static readonly string[] Directories = { Constant.PreinstalledDirectory, Constant.PluginsDirectory };
|
||||
|
||||
private static void ValidateUserDirectory()
|
||||
{
|
||||
if (!Directory.Exists(Constant.PluginsDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(Constant.PluginsDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Save()
|
||||
{
|
||||
foreach (var plugin in AllPlugins)
|
||||
{
|
||||
var savable = plugin.Plugin as ISavable;
|
||||
savable?.Save();
|
||||
}
|
||||
}
|
||||
|
||||
public static void ReloadData()
|
||||
{
|
||||
foreach(var plugin in AllPlugins)
|
||||
{
|
||||
var reloadablePlugin = plugin.Plugin as IReloadable;
|
||||
reloadablePlugin?.ReloadData();
|
||||
}
|
||||
}
|
||||
|
||||
static PluginManager()
|
||||
{
|
||||
ValidateUserDirectory();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// because InitializePlugins needs API, so LoadPlugins needs to be called first
|
||||
/// todo happlebao The API should be removed
|
||||
/// </summary>
|
||||
/// <param name="settings"></param>
|
||||
public static void LoadPlugins(PluginsSettings settings)
|
||||
{
|
||||
_metadatas = PluginConfig.Parse(Directories);
|
||||
Settings = settings;
|
||||
Settings.UpdatePluginSettings(_metadatas);
|
||||
AllPlugins = PluginsLoader.Plugins(_metadatas, Settings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Call initialize for all plugins
|
||||
/// </summary>
|
||||
/// <returns>return the list of failed to init plugins or null for none</returns>
|
||||
public static void InitializePlugins(IPublicAPI api)
|
||||
{
|
||||
API = api;
|
||||
var failedPlugins = new ConcurrentQueue<PluginPair>();
|
||||
Parallel.ForEach(AllPlugins, pair =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var milliseconds = Stopwatch.Debug($"|PluginManager.InitializePlugins|Init method time cost for <{pair.Metadata.Name}>", () =>
|
||||
{
|
||||
pair.Plugin.Init(new PluginInitContext
|
||||
{
|
||||
CurrentPluginMetadata = pair.Metadata,
|
||||
API = API
|
||||
});
|
||||
});
|
||||
pair.Metadata.InitTime += milliseconds;
|
||||
Log.Info($"|PluginManager.InitializePlugins|Total init cost for <{pair.Metadata.Name}> is <{pair.Metadata.InitTime}ms>");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception(nameof(PluginManager), $"Fail to Init plugin: {pair.Metadata.Name}", e);
|
||||
pair.Metadata.Disabled = true;
|
||||
failedPlugins.Enqueue(pair);
|
||||
}
|
||||
});
|
||||
|
||||
_contextMenuPlugins = GetPluginsForInterface<IContextMenu>();
|
||||
foreach (var plugin in AllPlugins)
|
||||
{
|
||||
if (IsGlobalPlugin(plugin.Metadata))
|
||||
GlobalPlugins.Add(plugin);
|
||||
|
||||
// Plugins may have multiple ActionKeywords, eg. WebSearch
|
||||
plugin.Metadata.ActionKeywords.Where(x => x != Query.GlobalPluginWildcardSign)
|
||||
.ToList()
|
||||
.ForEach(x => NonGlobalPlugins[x] = plugin);
|
||||
}
|
||||
|
||||
if (failedPlugins.Any())
|
||||
{
|
||||
var failed = string.Join(",", failedPlugins.Select(x => x.Metadata.Name));
|
||||
API.ShowMsg($"Fail to Init Plugins", $"Plugins: {failed} - fail to load and would be disabled, please contact plugin creator for help", "", false);
|
||||
}
|
||||
}
|
||||
|
||||
public static void InstallPlugin(string path)
|
||||
{
|
||||
PluginInstaller.Install(path);
|
||||
}
|
||||
|
||||
public static List<PluginPair> ValidPluginsForQuery(Query query)
|
||||
{
|
||||
if (NonGlobalPlugins.ContainsKey(query.ActionKeyword))
|
||||
{
|
||||
var plugin = NonGlobalPlugins[query.ActionKeyword];
|
||||
return new List<PluginPair> { plugin };
|
||||
}
|
||||
else
|
||||
{
|
||||
return GlobalPlugins;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Result> QueryForPlugin(PluginPair pair, Query query)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Result> results = null;
|
||||
var metadata = pair.Metadata;
|
||||
var milliseconds = Stopwatch.Debug($"|PluginManager.QueryForPlugin|Cost for {metadata.Name}", () =>
|
||||
{
|
||||
results = pair.Plugin.Query(query) ?? new List<Result>();
|
||||
UpdatePluginMetadata(results, metadata, query);
|
||||
});
|
||||
metadata.QueryCount += 1;
|
||||
metadata.AvgQueryTime = metadata.QueryCount == 1 ? milliseconds : (metadata.AvgQueryTime + milliseconds) / 2;
|
||||
return results;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"|PluginManager.QueryForPlugin|Exception for plugin <{pair.Metadata.Name}> when query <{query}>", e);
|
||||
return new List<Result>();
|
||||
}
|
||||
}
|
||||
|
||||
public static void UpdatePluginMetadata(List<Result> results, PluginMetadata metadata, Query query)
|
||||
{
|
||||
foreach (var r in results)
|
||||
{
|
||||
r.PluginDirectory = metadata.PluginDirectory;
|
||||
r.PluginID = metadata.ID;
|
||||
r.OriginQuery = query;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsGlobalPlugin(PluginMetadata metadata)
|
||||
{
|
||||
return metadata.ActionKeywords.Contains(Query.GlobalPluginWildcardSign);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// get specified plugin, return null if not found
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public static PluginPair GetPluginForId(string id)
|
||||
{
|
||||
return AllPlugins.FirstOrDefault(o => o.Metadata.ID == id);
|
||||
}
|
||||
|
||||
public static IEnumerable<PluginPair> GetPluginsForInterface<T>() where T : IFeatures
|
||||
{
|
||||
return AllPlugins.Where(p => p.Plugin is T);
|
||||
}
|
||||
|
||||
public static List<ContextMenuResult> GetContextMenusForPlugin(Result result)
|
||||
{
|
||||
var pluginPair = _contextMenuPlugins.FirstOrDefault(o => o.Metadata.ID == result.PluginID);
|
||||
if (pluginPair != null)
|
||||
{
|
||||
var metadata = pluginPair.Metadata;
|
||||
var plugin = (IContextMenu)pluginPair.Plugin;
|
||||
|
||||
try
|
||||
{
|
||||
var results = plugin.LoadContextMenus(result);
|
||||
return results;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"|PluginManager.GetContextMenusForPlugin|Can't load context menus for plugin <{metadata.Name}>", e);
|
||||
return new List<ContextMenuResult>();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return new List<ContextMenuResult>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static bool ActionKeywordRegistered(string actionKeyword)
|
||||
{
|
||||
if (actionKeyword != Query.GlobalPluginWildcardSign &&
|
||||
NonGlobalPlugins.ContainsKey(actionKeyword))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// used to add action keyword for multiple action keyword plugin
|
||||
/// e.g. web search
|
||||
/// </summary>
|
||||
public static void AddActionKeyword(string id, string newActionKeyword)
|
||||
{
|
||||
var plugin = GetPluginForId(id);
|
||||
if (newActionKeyword == Query.GlobalPluginWildcardSign)
|
||||
{
|
||||
GlobalPlugins.Add(plugin);
|
||||
}
|
||||
else
|
||||
{
|
||||
NonGlobalPlugins[newActionKeyword] = plugin;
|
||||
}
|
||||
plugin.Metadata.ActionKeywords.Add(newActionKeyword);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// used to add action keyword for multiple action keyword plugin
|
||||
/// e.g. web search
|
||||
/// </summary>
|
||||
public static void RemoveActionKeyword(string id, string oldActionkeyword)
|
||||
{
|
||||
var plugin = GetPluginForId(id);
|
||||
if (oldActionkeyword == Query.GlobalPluginWildcardSign
|
||||
&& // Plugins may have multiple ActionKeywords that are global, eg. WebSearch
|
||||
plugin.Metadata.ActionKeywords
|
||||
.Where(x => x == Query.GlobalPluginWildcardSign)
|
||||
.ToList()
|
||||
.Count == 1)
|
||||
{
|
||||
GlobalPlugins.Remove(plugin);
|
||||
}
|
||||
|
||||
if(oldActionkeyword != Query.GlobalPluginWildcardSign)
|
||||
NonGlobalPlugins.Remove(oldActionkeyword);
|
||||
|
||||
|
||||
plugin.Metadata.ActionKeywords.Remove(oldActionkeyword);
|
||||
}
|
||||
|
||||
public static void ReplaceActionKeyword(string id, string oldActionKeyword, string newActionKeyword)
|
||||
{
|
||||
if (oldActionKeyword != newActionKeyword)
|
||||
{
|
||||
AddActionKeyword(id, newActionKeyword);
|
||||
RemoveActionKeyword(id, oldActionKeyword);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
101
src/modules/launcher/Wox.Core/Plugin/PluginsLoader.cs
Normal file
101
src/modules/launcher/Wox.Core/Plugin/PluginsLoader.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Exception;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.UserSettings;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin
|
||||
{
|
||||
public static class PluginsLoader
|
||||
{
|
||||
public const string PATH = "PATH";
|
||||
|
||||
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas, PluginsSettings settings)
|
||||
{
|
||||
var csharpPlugins = CSharpPlugins(metadatas).ToList();
|
||||
var executablePlugins = ExecutablePlugins(metadatas);
|
||||
var plugins = csharpPlugins.Concat(executablePlugins).ToList();
|
||||
return plugins;
|
||||
}
|
||||
|
||||
public static IEnumerable<PluginPair> CSharpPlugins(List<PluginMetadata> source)
|
||||
{
|
||||
var plugins = new List<PluginPair>();
|
||||
var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp);
|
||||
|
||||
foreach (var metadata in metadatas)
|
||||
{
|
||||
var milliseconds = Stopwatch.Debug($"|PluginsLoader.CSharpPlugins|Constructor init cost for {metadata.Name}", () =>
|
||||
{
|
||||
|
||||
#if DEBUG
|
||||
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(metadata.ExecuteFilePath);
|
||||
var types = assembly.GetTypes();
|
||||
var type = types.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin)));
|
||||
var plugin = (IPlugin)Activator.CreateInstance(type);
|
||||
#else
|
||||
Assembly assembly;
|
||||
try
|
||||
{
|
||||
assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(metadata.ExecuteFilePath);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"|PluginsLoader.CSharpPlugins|Couldn't load assembly for {metadata.Name}", e);
|
||||
return;
|
||||
}
|
||||
var types = assembly.GetTypes();
|
||||
Type type;
|
||||
try
|
||||
{
|
||||
type = types.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin)));
|
||||
}
|
||||
catch (InvalidOperationException e)
|
||||
{
|
||||
Log.Exception($"|PluginsLoader.CSharpPlugins|Can't find class implement IPlugin for <{metadata.Name}>", e);
|
||||
return;
|
||||
}
|
||||
IPlugin plugin;
|
||||
try
|
||||
{
|
||||
plugin = (IPlugin)Activator.CreateInstance(type);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"|PluginsLoader.CSharpPlugins|Can't create instance for <{metadata.Name}>", e);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
PluginPair pair = new PluginPair
|
||||
{
|
||||
Plugin = plugin,
|
||||
Metadata = metadata
|
||||
};
|
||||
plugins.Add(pair);
|
||||
});
|
||||
metadata.InitTime += milliseconds;
|
||||
|
||||
}
|
||||
return plugins;
|
||||
}
|
||||
|
||||
public static IEnumerable<PluginPair> ExecutablePlugins(IEnumerable<PluginMetadata> source)
|
||||
{
|
||||
var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.Executable);
|
||||
|
||||
var plugins = metadatas.Select(metadata => new PluginPair
|
||||
{
|
||||
Plugin = new ExecutablePlugin(metadata.ExecuteFilePath),
|
||||
Metadata = metadata
|
||||
});
|
||||
return plugins;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
50
src/modules/launcher/Wox.Core/Plugin/QueryBuilder.cs
Normal file
50
src/modules/launcher/Wox.Core/Plugin/QueryBuilder.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin
|
||||
{
|
||||
public static class QueryBuilder
|
||||
{
|
||||
public static Query Build(string text, Dictionary<string, PluginPair> nonGlobalPlugins)
|
||||
{
|
||||
// replace multiple white spaces with one white space
|
||||
var terms = text.Split(new[] { Query.TermSeperater }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (terms.Length == 0)
|
||||
{ // nothing was typed
|
||||
return null;
|
||||
}
|
||||
|
||||
var rawQuery = string.Join(Query.TermSeperater, terms);
|
||||
string actionKeyword, search;
|
||||
string possibleActionKeyword = terms[0];
|
||||
List<string> actionParameters;
|
||||
if (nonGlobalPlugins.TryGetValue(possibleActionKeyword, out var pluginPair) && !pluginPair.Metadata.Disabled)
|
||||
{ // use non global plugin for query
|
||||
actionKeyword = possibleActionKeyword;
|
||||
actionParameters = terms.Skip(1).ToList();
|
||||
search = actionParameters.Count > 0 ? rawQuery.Substring(actionKeyword.Length + 1) : string.Empty;
|
||||
}
|
||||
else
|
||||
{ // non action keyword
|
||||
actionKeyword = string.Empty;
|
||||
actionParameters = terms.ToList();
|
||||
search = rawQuery;
|
||||
}
|
||||
|
||||
var query = new Query
|
||||
{
|
||||
Terms = terms,
|
||||
RawQuery = rawQuery,
|
||||
ActionKeyword = actionKeyword,
|
||||
Search = search,
|
||||
// Obsolete value initialisation
|
||||
ActionName = actionKeyword,
|
||||
ActionParameters = actionParameters
|
||||
};
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
5
src/modules/launcher/Wox.Core/Plugin/README.md
Normal file
5
src/modules/launcher/Wox.Core/Plugin/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
There are two kinds of plugins:
|
||||
1. System plugin
|
||||
Those plugins that action keyword is "*", those plugin doesn't need action keyword
|
||||
2. User Plugin
|
||||
Those plugins that contains customized action keyword
|
||||
Reference in New Issue
Block a user