mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
354 lines
No EOL
13 KiB
C#
354 lines
No EOL
13 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Flow.Launcher.Infrastructure;
|
|
using Flow.Launcher.Infrastructure.Logger;
|
|
using Flow.Launcher.Infrastructure.Storage;
|
|
using Flow.Launcher.Infrastructure.UserSettings;
|
|
using Flow.Launcher.Plugin;
|
|
|
|
namespace Flow.Launcher.Core.Plugin
|
|
{
|
|
/// <summary>
|
|
/// The entry for managing Flow Launcher plugins
|
|
/// </summary>
|
|
public static class PluginManager
|
|
{
|
|
private static IEnumerable<PluginPair> _contextMenuPlugins;
|
|
|
|
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;
|
|
|
|
/// <summary>
|
|
/// Directories that will hold Flow Launcher plugin directory
|
|
/// </summary>
|
|
private static readonly string[] Directories = { Constant.PreinstalledDirectory, DataLocation.PluginsDirectory };
|
|
|
|
private static void DeletePythonBinding()
|
|
{
|
|
const string binding = "flowlauncher.py";
|
|
foreach (var subDirectory in Directory.GetDirectories(DataLocation.PluginsDirectory))
|
|
{
|
|
File.Delete(Path.Combine(subDirectory, binding));
|
|
}
|
|
}
|
|
|
|
public static void Save()
|
|
{
|
|
foreach (var plugin in AllPlugins)
|
|
{
|
|
var savable = plugin.Plugin as ISavable;
|
|
savable?.Save();
|
|
}
|
|
}
|
|
|
|
public static async Task ReloadData()
|
|
{
|
|
await Task.WhenAll(AllPlugins.Select(plugin =>
|
|
{
|
|
{
|
|
switch (plugin)
|
|
{
|
|
case IReloadable p:
|
|
p.ReloadData(); // Sync reload means low time consuming
|
|
return Task.CompletedTask;
|
|
case IAsyncReloadable p:
|
|
return p.ReloadDataAsync();
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
}));
|
|
}
|
|
|
|
static PluginManager()
|
|
{
|
|
// validate user directory
|
|
Directory.CreateDirectory(DataLocation.PluginsDirectory);
|
|
// force old plugins use new python binding
|
|
DeletePythonBinding();
|
|
}
|
|
|
|
/// <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 async Task InitializePlugins(IPublicAPI api)
|
|
{
|
|
API = api;
|
|
var failedPlugins = new ConcurrentQueue<PluginPair>();
|
|
|
|
var InitTasks = AllPlugins.Select(pair => Task.Run(async delegate
|
|
{
|
|
try
|
|
{
|
|
long milliseconds;
|
|
|
|
switch (pair.Plugin)
|
|
{
|
|
case IAsyncPlugin plugin:
|
|
milliseconds = await Stopwatch.DebugAsync(
|
|
$"|PluginManager.InitializePlugins|Init method time cost for <{pair.Metadata.Name}>",
|
|
async delegate
|
|
{
|
|
await plugin.InitAsync(new PluginInitContext
|
|
{
|
|
CurrentPluginMetadata = pair.Metadata,
|
|
API = API
|
|
});
|
|
});
|
|
break;
|
|
case IPlugin plugin:
|
|
milliseconds = Stopwatch.Debug(
|
|
$"|PluginManager.InitializePlugins|Init method time cost for <{pair.Metadata.Name}>",
|
|
() =>
|
|
{
|
|
plugin.Init(new PluginInitContext
|
|
{
|
|
CurrentPluginMetadata = pair.Metadata,
|
|
API = API
|
|
});
|
|
});
|
|
break;
|
|
default:
|
|
throw new ArgumentException();
|
|
}
|
|
|
|
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);
|
|
}
|
|
}));
|
|
|
|
await Task.WhenAll(InitTasks);
|
|
|
|
_contextMenuPlugins = GetPluginsForInterface<IContextMenu>();
|
|
foreach (var plugin in AllPlugins)
|
|
{
|
|
foreach (var actionKeyword in plugin.Metadata.ActionKeywords)
|
|
{
|
|
switch (actionKeyword)
|
|
{
|
|
case Query.GlobalPluginWildcardSign:
|
|
GlobalPlugins.Add(plugin);
|
|
break;
|
|
default:
|
|
NonGlobalPlugins[actionKeyword] = plugin;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
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 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 async Task<List<Result>> QueryForPlugin(PluginPair pair, Query query, CancellationToken token)
|
|
{
|
|
var results = new List<Result>();
|
|
try
|
|
{
|
|
var metadata = pair.Metadata;
|
|
|
|
long milliseconds = -1L;
|
|
|
|
switch (pair.Plugin)
|
|
{
|
|
case IAsyncPlugin plugin:
|
|
milliseconds = await Stopwatch.DebugAsync($"|PluginManager.QueryForPlugin|Cost for {metadata.Name}",
|
|
async () => results = await plugin.QueryAsync(query, token).ConfigureAwait(false));
|
|
break;
|
|
case IPlugin plugin:
|
|
await Task.Run(() => milliseconds = Stopwatch.Debug($"|PluginManager.QueryForPlugin|Cost for {metadata.Name}", () =>
|
|
results = plugin.Query(query)), token).ConfigureAwait(false);
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
token.ThrowIfCancellationRequested();
|
|
UpdatePluginMetadata(results, metadata, query);
|
|
|
|
metadata.QueryCount += 1;
|
|
metadata.AvgQueryTime =
|
|
metadata.QueryCount == 1 ? milliseconds : (metadata.AvgQueryTime + milliseconds) / 2;
|
|
token.ThrowIfCancellationRequested();
|
|
}
|
|
catch (Exception e) when (e is OperationCanceledException || e is TaskCanceledException)
|
|
{
|
|
return results = null;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Exception($"|PluginManager.QueryForPlugin|Exception for plugin <{pair.Metadata.Name}> when query <{query}>", e);
|
|
}
|
|
|
|
// null will be fine since the results will only be added into queue if the token hasn't been cancelled
|
|
return results;
|
|
}
|
|
|
|
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;
|
|
|
|
// ActionKeywordAssigned is used for constructing MainViewModel's query text auto-complete suggestions
|
|
// Plugins may have multi-actionkeywords eg. WebSearches. In this scenario it needs to be overriden on the plugin level
|
|
if (metadata.ActionKeywords.Count == 1)
|
|
r.ActionKeywordAssigned = query.ActionKeyword;
|
|
}
|
|
}
|
|
|
|
/// <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<Result> GetContextMenusForPlugin(Result result)
|
|
{
|
|
var results = new List<Result>();
|
|
var pluginPair = _contextMenuPlugins.FirstOrDefault(o => o.Metadata.ID == result.PluginID);
|
|
if (pluginPair != null)
|
|
{
|
|
var plugin = (IContextMenu)pluginPair.Plugin;
|
|
|
|
try
|
|
{
|
|
results = plugin.LoadContextMenus(result);
|
|
foreach (var r in results)
|
|
{
|
|
r.PluginDirectory = pluginPair.Metadata.PluginDirectory;
|
|
r.PluginID = pluginPair.Metadata.ID;
|
|
r.OriginQuery = result.OriginQuery;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Exception(
|
|
$"|PluginManager.GetContextMenusForPlugin|Can't load context menus for plugin <{pluginPair.Metadata.Name}>",
|
|
e);
|
|
}
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
public static bool ActionKeywordRegistered(string actionKeyword)
|
|
{
|
|
return actionKeyword != Query.GlobalPluginWildcardSign
|
|
&& NonGlobalPlugins.ContainsKey(actionKeyword);
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
}
|
|
}
|
|
} |