Plugin info - Implemented property changed

Plugins - Improved enable failure detection
Plugins UI - Show an indicator on plugins that failed to enable
Plugins UI - Show a progress indicator on plugins that are enabling
UI - Added reusable Snackbar (not the Dutch kind with kroketten)
This commit is contained in:
Robert committed 2020-06-25 19:25:58 +02:00
1 parent 28bcfcc95a
commit a47eedf1c2
18 files changed
+399 -179

No files matched your search

+18 -1
View File
@@ -52,13 +52,30 @@ namespace Artemis.Core.Plugins.Abstract
if (enable && !Enabled)
{
Enabled = true;
EnablePlugin();
PluginInfo.Enabled = true;
// If enable failed, put it back in a disabled state
try
{
EnablePlugin();
}
catch
{
Enabled = false;
PluginInfo.Enabled = false;
throw;
}
OnPluginEnabled();
}
else if (!enable && Enabled)
{
Enabled = false;
PluginInfo.Enabled = false;
// Even if disable failed, still leave it in a disabled state to avoid more issues
DisablePlugin();
OnPluginDisabled();
}
}
+78 -17
View File
@@ -5,11 +5,24 @@ using Artemis.Core.Plugins.Abstract;
using Artemis.Storage.Entities.Plugins;
using McMaster.NETCore.Plugins;
using Newtonsoft.Json;
using Stylet;
namespace Artemis.Core.Plugins.Models
{
public class PluginInfo
[JsonObject(MemberSerialization.OptIn)]
public class PluginInfo : PropertyChangedBase
{
private Guid _guid;
private string _name;
private string _description;
private string _icon;
private Version _version;
private string _main;
private DirectoryInfo _directory;
private Plugin _instance;
private bool _enabled;
private bool _lastEnableSuccessful;
internal PluginInfo()
{
}
@@ -18,77 +31,125 @@ namespace Artemis.Core.Plugins.Models
/// The plugins GUID
/// </summary>
[JsonProperty(Required = Required.Always)]
public Guid Guid { get; internal set; }
public Guid Guid
{
get => _guid;
internal set => SetAndNotify(ref _guid, value);
}
/// <summary>
/// The name of the plugin
/// </summary>
[JsonProperty(Required = Required.Always)]
public string Name { get; internal set; }
public string Name
{
get => _name;
internal set => SetAndNotify(ref _name, value);
}
/// <summary>
/// A short description of the plugin
/// </summary>
public string Description { get; set; }
[JsonProperty]
public string Description
{
get => _description;
set => SetAndNotify(ref _description, value);
}
/// <summary>
/// The plugins display icon that's shown in the settings see <see href="https://materialdesignicons.com" /> for
/// available
/// icons
/// </summary>
public string Icon { get; set; }
[JsonProperty]
public string Icon
{
get => _icon;
set => SetAndNotify(ref _icon, value);
}
/// <summary>
/// The version of the plugin
/// </summary>
[JsonProperty(Required = Required.Always)]
public Version Version { get; internal set; }
public Version Version
{
get => _version;
internal set => SetAndNotify(ref _version, value);
}
/// <summary>
/// The main entry DLL, should contain a class implementing Plugin
/// </summary>
[JsonProperty(Required = Required.Always)]
public string Main { get; internal set; }
public string Main
{
get => _main;
internal set => SetAndNotify(ref _main, value);
}
/// <summary>
/// The plugins root directory
/// </summary>
[JsonIgnore]
public DirectoryInfo Directory { get; internal set; }
public DirectoryInfo Directory
{
get => _directory;
internal set => SetAndNotify(ref _directory, value);
}
/// <summary>
/// A reference to the type implementing Plugin, available after successful load
/// </summary>
[JsonIgnore]
public Plugin Instance { get; internal set; }
public Plugin Instance
{
get => _instance;
internal set => SetAndNotify(ref _instance, value);
}
/// <summary>
/// Indicates whether the user enabled the plugin or not
/// </summary>
[JsonIgnore]
public bool Enabled { get; internal set; }
public bool Enabled
{
get => _enabled;
internal set => SetAndNotify(ref _enabled, value);
}
/// <summary>
/// Indicates whether the last time the plugin loaded, it loaded correctly
/// </summary>
public bool LastEnableSuccessful
{
get => _lastEnableSuccessful;
internal set => SetAndNotify(ref _lastEnableSuccessful, value);
}
/// <summary>
/// The PluginLoader backing this plugin
/// </summary>
[JsonIgnore]
internal PluginLoader PluginLoader { get; set; }
/// <summary>
/// The assembly the plugin code lives in
/// </summary>
[JsonIgnore]
internal Assembly Assembly { get; set; }
/// <summary>
/// The entity representing the plugin
/// </summary>
[JsonIgnore]
internal PluginEntity PluginEntity { get; set; }
public override string ToString()
{
return $"{nameof(Guid)}: {Guid}, {nameof(Name)}: {Name}, {nameof(Version)}: {Version}";
return $"{Name} v{Version} - {Guid}";
}
internal void ApplyToEntity()
{
PluginEntity.Id = Guid;
PluginEntity.IsEnabled = Enabled;
PluginEntity.LastEnableSuccessful = LastEnableSuccessful;
}
}
}
+59 -53
View File
@@ -137,8 +137,6 @@ namespace Artemis.Core.Services
var pluginInfo = JsonConvert.DeserializeObject<PluginInfo>(File.ReadAllText(metadataFile));
pluginInfo.Directory = subDirectory;
_logger.Debug("Loading plugin {pluginInfo}", pluginInfo);
OnPluginLoading(new PluginEventArgs(pluginInfo));
LoadPlugin(pluginInfo);
}
catch (Exception e)
@@ -150,38 +148,22 @@ namespace Artemis.Core.Services
// Activate plugins after they are all loaded
foreach (var pluginInfo in _plugins.Where(p => p.Enabled))
{
if (!pluginInfo.PluginEntity.LastEnableSuccessful)
if (!pluginInfo.LastEnableSuccessful)
{
pluginInfo.Enabled = false;
_logger.Warning("Plugin failed to load last time, disabling it now to avoid instability. Plugin info: {pluginInfo}", pluginInfo);
continue;
}
// Mark this as false until the plugin enabled successfully and save it in case the plugin drags us down into a crash
pluginInfo.PluginEntity.LastEnableSuccessful = false;
_pluginRepository.SavePlugin(pluginInfo.PluginEntity);
var threwException = false;
try
{
_logger.Debug("Enabling plugin {pluginInfo}", pluginInfo);
pluginInfo.Instance.SetEnabled(true);
EnablePlugin(pluginInfo.Instance);
}
catch (Exception e)
catch (Exception)
{
_logger.Warning(new ArtemisPluginException(pluginInfo, "Failed to enable plugin", e), "Plugin exception");
pluginInfo.Enabled = false;
threwException = true;
// ignored, logged in EnablePlugin
}
// We got this far so the plugin enabled and we didn't crash horribly, yay
if (!threwException)
{
pluginInfo.PluginEntity.LastEnableSuccessful = true;
_pluginRepository.SavePlugin(pluginInfo.PluginEntity);
}
OnPluginEnabled(new PluginEventArgs(pluginInfo));
}
LoadingPlugins = false;
@@ -213,16 +195,20 @@ namespace Artemis.Core.Services
{
lock (_plugins)
{
_logger.Debug("Loading plugin {pluginInfo}", pluginInfo);
OnPluginLoading(new PluginEventArgs(pluginInfo));
// Unload the plugin first if it is already loaded
if (_plugins.Contains(pluginInfo))
UnloadPlugin(pluginInfo);
var pluginEntity = _pluginRepository.GetPluginByGuid(pluginInfo.Guid);
if (pluginEntity == null)
pluginEntity = new PluginEntity {PluginGuid = pluginInfo.Guid, IsEnabled = true, LastEnableSuccessful = true};
pluginEntity = new PluginEntity {Id = pluginInfo.Guid, IsEnabled = true, LastEnableSuccessful = true};
pluginInfo.PluginEntity = pluginEntity;
pluginInfo.Enabled = pluginEntity.IsEnabled;
pluginInfo.LastEnableSuccessful = pluginEntity.LastEnableSuccessful;
var mainFile = Path.Combine(pluginInfo.Directory.FullName, pluginInfo.Main);
if (!File.Exists(mainFile))
@@ -310,28 +296,37 @@ namespace Artemis.Core.Services
public void EnablePlugin(Plugin plugin)
{
plugin.PluginInfo.Enabled = true;
plugin.PluginInfo.PluginEntity.IsEnabled = true;
plugin.PluginInfo.PluginEntity.LastEnableSuccessful = false;
_pluginRepository.SavePlugin(plugin.PluginInfo.PluginEntity);
lock (_plugins)
{
_logger.Debug("Enabling plugin {pluginInfo}", plugin.PluginInfo);
var threwException = false;
try
{
plugin.SetEnabled(true);
}
catch (Exception e)
{
_logger.Warning(new ArtemisPluginException(plugin.PluginInfo, "Failed to enable plugin", e), "Plugin exception");
plugin.PluginInfo.Enabled = false;
threwException = true;
}
plugin.PluginInfo.LastEnableSuccessful = false;
plugin.PluginInfo.ApplyToEntity();
// We got this far so the plugin enabled and we didn't crash horribly, yay
if (!threwException)
{
plugin.PluginInfo.PluginEntity.LastEnableSuccessful = true;
_pluginRepository.SavePlugin(plugin.PluginInfo.PluginEntity);
try
{
plugin.SetEnabled(true);
}
catch (Exception e)
{
_logger.Warning(new ArtemisPluginException(plugin.PluginInfo, "Exception during SetEnabled(true)", e), "Failed to enable plugin");
throw;
}
finally
{
// We got this far so the plugin enabled and we didn't crash horribly, yay
if (plugin.PluginInfo.Enabled)
{
plugin.PluginInfo.LastEnableSuccessful = true;
plugin.PluginInfo.ApplyToEntity();
_pluginRepository.SavePlugin(plugin.PluginInfo.PluginEntity);
_logger.Debug("Successfully enabled plugin {pluginInfo}", plugin.PluginInfo);
}
}
}
OnPluginEnabled(new PluginEventArgs(plugin.PluginInfo));
@@ -339,18 +334,29 @@ namespace Artemis.Core.Services
public void DisablePlugin(Plugin plugin)
{
plugin.PluginInfo.Enabled = false;
plugin.PluginInfo.PluginEntity.IsEnabled = false;
_pluginRepository.SavePlugin(plugin.PluginInfo.PluginEntity);
// Device providers cannot be disabled at runtime, restart the application
if (plugin is DeviceProvider)
lock (_plugins)
{
CurrentProcessUtilities.Shutdown(2, true);
return;
}
_logger.Debug("Disabling plugin {pluginInfo}", plugin.PluginInfo);
plugin.SetEnabled(false);
// Device providers cannot be disabled at runtime, restart the application
if (plugin is DeviceProvider)
{
// Don't call SetEnabled(false) but simply update enabled state and save it
plugin.PluginInfo.Enabled = false;
plugin.PluginInfo.ApplyToEntity();
_pluginRepository.SavePlugin(plugin.PluginInfo.PluginEntity);
_logger.Debug("Shutting down for device provider disable {pluginInfo}", plugin.PluginInfo);
CurrentProcessUtilities.Shutdown(2, true);
return;
}
plugin.SetEnabled(false);
plugin.PluginInfo.ApplyToEntity();
_pluginRepository.SavePlugin(plugin.PluginInfo.PluginEntity);
_logger.Debug("Successfully disabled plugin {pluginInfo}", plugin.PluginInfo);
}
OnPluginDisabled(new PluginEventArgs(plugin.PluginInfo));
}