Skip to content

Commit

Permalink
Do not load plugin when it is disabled (#10515)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykhailopylyp committed Apr 5, 2021
1 parent ed21dba commit 1c8b7a5
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 147 deletions.
7 changes: 5 additions & 2 deletions src/modules/launcher/PowerLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public partial class App : IDisposable, ISingleInstanceApp
private ThemeManager _themeManager;
private SettingWindowViewModel _settingsVM;
private StringMatcher _stringMatcher;
private SettingsWatcher _settingsWatcher;
private SettingsReader _settingsReader;

[STAThread]
public static void Main()
Expand Down Expand Up @@ -103,6 +103,9 @@ private void OnStartup(object sender, StartupEventArgs e)
_mainVM = new MainViewModel(_settings);
_mainWindow = new MainWindow(_settings, _mainVM);
API = new PublicAPIInstance(_settingsVM, _mainVM, _themeManager);
_settingsReader = new SettingsReader(_settings, _themeManager);
_settingsReader.ReadSettings();
PluginManager.InitializePlugins(API);
Current.MainWindow = _mainWindow;
Expand All @@ -113,7 +116,7 @@ private void OnStartup(object sender, StartupEventArgs e)
RegisterExitEvents();
_settingsWatcher = new SettingsWatcher(_settings, _themeManager);
_settingsReader.ReadSettingsOnChange();
_mainVM.MainWindowVisibility = Visibility.Visible;
_mainVM.ColdStartFix();
Expand Down
34 changes: 18 additions & 16 deletions src/modules/launcher/PowerLauncher/Plugin/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using PowerLauncher.Properties;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
Expand Down Expand Up @@ -49,7 +50,10 @@ public static List<PluginPair> AllPlugins
{
if (_allPlugins == null)
{
_allPlugins = PluginsLoader.Plugins(PluginConfig.Parse(Directories));
_allPlugins = PluginConfig.Parse(Directories)
.Where(x => x.Language.ToUpperInvariant() == AllowedLanguage.CSharp)
.Select(x => new PluginPair(x))
.ToList();
}
}
}
Expand Down Expand Up @@ -119,23 +123,15 @@ public static void InitializePlugins(IPublicAPI api)
var failedPlugins = new ConcurrentQueue<PluginPair>();
Parallel.ForEach(AllPlugins, pair =>
{
try
if (pair.Metadata.Disabled)
{
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($"Total init cost for <{pair.Metadata.Name}> is <{pair.Metadata.InitTime}ms>", MethodBase.GetCurrentMethod().DeclaringType);
return;
}
catch (Exception e)
pair.LoadPlugin(API);
if (!pair.IsPluginLoaded)
{
Log.Exception($"Fail to Init plugin: {pair.Metadata.Name}", e, MethodBase.GetCurrentMethod().DeclaringType);
pair.Metadata.Disabled = true;
failedPlugins.Enqueue(pair);
}
});
Expand All @@ -145,7 +141,8 @@ public static void InitializePlugins(IPublicAPI api)
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", string.Empty, false);
var description = string.Format(CultureInfo.CurrentCulture, Resources.FailedToInitializePluginsDescription, failed);
API.ShowMsg(Resources.FailedToInitializePluginsTitle, description, string.Empty, false);
}
}

Expand All @@ -162,6 +159,11 @@ public static List<Result> QueryForPlugin(PluginPair pair, Query query, bool del
throw new ArgumentNullException(nameof(pair));
}

if (!pair.IsPluginLoaded)
{
return new List<Result>();
}

try
{
List<Result> results = null;
Expand Down
91 changes: 0 additions & 91 deletions src/modules/launcher/PowerLauncher/Plugin/PluginsLoader.cs

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/modules/launcher/PowerLauncher/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,10 @@
<data name="deseralization_error_message" xml:space="preserve">
<value>Settings will be reset to default and program will continue to function.</value>
</data>
<data name="FailedToInitializePluginsDescription" xml:space="preserve">
<value>Plugins: {0} - fail to load and would be disabled, please contact plugins creator for help</value>
</data>
<data name="FailedToInitializePluginsTitle" xml:space="preserve">
<value>Fail to initialize plugins</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,23 @@
namespace PowerLauncher
{
// Watch for /Local/Microsoft/PowerToys/Launcher/Settings.json changes
public class SettingsWatcher : BaseModel
public class SettingsReader : BaseModel
{
private readonly ISettingsUtils _settingsUtils;

private const int MaxRetries = 10;
private static readonly object _watcherSyncObject = new object();
private readonly IFileSystemWatcher _watcher;
private static readonly object _readSyncObject = new object();
private readonly PowerToysRunSettings _settings;

private readonly ThemeManager _themeManager;

public SettingsWatcher(PowerToysRunSettings settings, ThemeManager themeManager)
private IFileSystemWatcher _watcher;

public SettingsReader(PowerToysRunSettings settings, ThemeManager themeManager)
{
_settingsUtils = new SettingsUtils();
_settings = settings;
_themeManager = themeManager;

// Set up watcher
_watcher = Microsoft.PowerToys.Settings.UI.Library.Utilities.Helper.GetFileWatcher(PowerLauncherSettings.ModuleName, "settings.json", OverloadSettings);

// Load initial settings file
OverloadSettings();

// Apply theme at startup
_themeManager.ChangeTheme(_settings.Theme, true);
}
Expand All @@ -61,9 +55,14 @@ public void CreateSettingsIfNotExists()
}
}

public void OverloadSettings()
public void ReadSettingsOnChange()
{
_watcher = Microsoft.PowerToys.Settings.UI.Library.Utilities.Helper.GetFileWatcher(PowerLauncherSettings.ModuleName, "settings.json", ReadSettings);
}

public void ReadSettings()
{
Monitor.Enter(_watcherSyncObject);
Monitor.Enter(_readSyncObject);
var retry = true;
var retryCount = 0;
while (retry)
Expand All @@ -86,16 +85,7 @@ public void OverloadSettings()
foreach (var setting in overloadSettings.Plugins)
{
var plugin = PluginManager.AllPlugins.FirstOrDefault(x => x.Metadata.ID == setting.Id);
if (plugin != null)
{
plugin.Metadata.Disabled = setting.Disabled;
plugin.Metadata.ActionKeyword = setting.ActionKeyword;
plugin.Metadata.IsGlobal = setting.IsGlobal;
if (plugin.Plugin is ISettingProvider)
{
(plugin.Plugin as ISettingProvider).UpdateSettings(setting);
}
}
plugin?.Update(setting, App.API);
}
}

Expand Down Expand Up @@ -168,7 +158,7 @@ public void OverloadSettings()
}
}

Monitor.Exit(_watcherSyncObject);
Monitor.Exit(_readSyncObject);
}

private static string ConvertHotkey(HotkeySettings hotkey)
Expand Down

0 comments on commit 1c8b7a5

Please sign in to comment.