mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 19:36:28 +00:00
Plugins - Load features by their info before enabling plugins
General - Added missing XML comments
This commit is contained in:
1 parent
fa26e6b7da
commit
d5d1211649
24 files changed
+241
-180
No files matched your search
@@ -17,4 +17,20 @@ namespace Artemis.Core
|
||||
/// </summary>
|
||||
public PluginFeature PluginFeature { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides data about plugin feature info related events
|
||||
/// </summary>
|
||||
public class PluginFeatureInfoEventArgs : EventArgs
|
||||
{
|
||||
internal PluginFeatureInfoEventArgs(PluginFeatureInfo pluginFeatureInfo)
|
||||
{
|
||||
PluginFeatureInfo = pluginFeatureInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin feature this event is related to
|
||||
/// </summary>
|
||||
public PluginFeatureInfo PluginFeatureInfo { get; }
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,9 @@ namespace Artemis.Core
|
||||
/// </summary>
|
||||
public DataModelPath? EventPath { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the last time the event this condition is applied to was triggered
|
||||
/// </summary>
|
||||
public DateTime LastTrigger { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
@@ -53,7 +56,7 @@ namespace Artemis.Core
|
||||
if (_disposed)
|
||||
throw new ObjectDisposedException("DataModelConditionEvent");
|
||||
|
||||
if (EventPath?.GetValue() is not IDataModelEvent dataModelEvent)
|
||||
if (EventPath?.GetValue() is not IDataModelEvent dataModelEvent)
|
||||
return false;
|
||||
// Only evaluate to true once every time the event has been triggered since the last evaluation
|
||||
if (dataModelEvent.LastTrigger <= LastTrigger)
|
||||
@@ -67,7 +70,6 @@ namespace Artemis.Core
|
||||
|
||||
// If there are no children, we always evaluate to true whenever the event triggered
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -131,7 +133,7 @@ namespace Artemis.Core
|
||||
// Target list
|
||||
EventPath?.Save();
|
||||
Entity.EventPath = EventPath?.Entity;
|
||||
|
||||
|
||||
// Children
|
||||
Entity.Children.Clear();
|
||||
Entity.Children.AddRange(Children.Select(c => c.GetEntity()));
|
||||
|
||||
@@ -225,7 +225,7 @@ namespace Artemis.Core
|
||||
{
|
||||
LedEntity ledEntity = new()
|
||||
{
|
||||
DeviceIdentifier = artemisLed.Device.RgbDevice.GetDeviceIdentifier(),
|
||||
DeviceIdentifier = artemisLed.Device.Identifier,
|
||||
LedName = artemisLed.RgbLed.Id.ToString()
|
||||
};
|
||||
LayerEntity.Leds.Add(ledEntity);
|
||||
@@ -578,7 +578,7 @@ namespace Artemis.Core
|
||||
List<ArtemisLed> availableLeds = devices.SelectMany(d => d.Leds).ToList();
|
||||
foreach (LedEntity ledEntity in LayerEntity.Leds)
|
||||
{
|
||||
ArtemisLed? match = availableLeds.FirstOrDefault(a => a.Device.RgbDevice.GetDeviceIdentifier() == ledEntity.DeviceIdentifier &&
|
||||
ArtemisLed? match = availableLeds.FirstOrDefault(a => a.Device.Identifier == ledEntity.DeviceIdentifier &&
|
||||
a.RgbLed.Id.ToString() == ledEntity.LedName);
|
||||
if (match != null)
|
||||
leds.Add(match);
|
||||
|
||||
@@ -21,6 +21,7 @@ namespace Artemis.Core
|
||||
|
||||
internal ArtemisDevice(IRGBDevice rgbDevice, DeviceProvider deviceProvider)
|
||||
{
|
||||
Identifier = rgbDevice.GetDeviceIdentifier();
|
||||
DeviceEntity = new DeviceEntity();
|
||||
RgbDevice = rgbDevice;
|
||||
DeviceProvider = deviceProvider;
|
||||
@@ -45,6 +46,7 @@ namespace Artemis.Core
|
||||
|
||||
internal ArtemisDevice(IRGBDevice rgbDevice, DeviceProvider deviceProvider, DeviceEntity deviceEntity)
|
||||
{
|
||||
Identifier = rgbDevice.GetDeviceIdentifier();
|
||||
DeviceEntity = deviceEntity;
|
||||
RgbDevice = rgbDevice;
|
||||
DeviceProvider = deviceProvider;
|
||||
@@ -59,6 +61,11 @@ namespace Artemis.Core
|
||||
ApplyKeyboardLayout();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the (hopefully unique and persistent) ID of this device
|
||||
/// </summary>
|
||||
public string Identifier { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the rectangle covering the device
|
||||
/// </summary>
|
||||
@@ -336,7 +343,7 @@ namespace Artemis.Core
|
||||
internal void ApplyToEntity()
|
||||
{
|
||||
// Other properties are computed
|
||||
DeviceEntity.Id = RgbDevice.GetDeviceIdentifier();
|
||||
DeviceEntity.Id = Identifier;
|
||||
|
||||
DeviceEntity.InputIdentifiers.Clear();
|
||||
foreach (ArtemisDeviceInputIdentifier identifier in InputIdentifiers)
|
||||
|
||||
@@ -46,6 +46,9 @@ namespace Artemis.Core
|
||||
private set => SetAndNotify(ref _absoluteRectangle, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the layout applied to this LED
|
||||
/// </summary>
|
||||
public ArtemisLedLayout? Layout { get; internal set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -53,7 +56,7 @@ namespace Artemis.Core
|
||||
{
|
||||
return RgbLed.ToString();
|
||||
}
|
||||
|
||||
|
||||
internal void CalculateRectangles()
|
||||
{
|
||||
Rectangle = RgbLed.Boundary.ToSKRect();
|
||||
|
||||
@@ -5,6 +5,9 @@ using RGB.NET.Layout;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a LED layout decorated with extra Artemis-specific data
|
||||
/// </summary>
|
||||
public class ArtemisLedLayout
|
||||
{
|
||||
internal ArtemisLedLayout(ArtemisLayout deviceLayout, ILedLayout led)
|
||||
|
||||
@@ -29,6 +29,9 @@ namespace Artemis.Core.LayerBrushes
|
||||
/// </summary>
|
||||
public ListLedGroup? LedGroup { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// For internal use only, is public for dependency injection but ignore pl0x
|
||||
/// </summary>
|
||||
[Inject]
|
||||
public IRgbService? RgbService { get; set; }
|
||||
|
||||
@@ -38,35 +41,6 @@ namespace Artemis.Core.LayerBrushes
|
||||
/// <returns>Your RGB.NET effect</returns>
|
||||
public abstract IBrush GetBrush();
|
||||
|
||||
internal void UpdateLedGroup()
|
||||
{
|
||||
if (LedGroup == null)
|
||||
return;
|
||||
|
||||
if (Layer.Parent != null)
|
||||
LedGroup.ZIndex = Layer.Parent.Children.Count - Layer.Parent.Children.IndexOf(Layer);
|
||||
else
|
||||
LedGroup.ZIndex = 1;
|
||||
|
||||
List<Led> missingLeds = Layer.Leds.Where(l => !LedGroup.ContainsLed(l.RgbLed)).Select(l => l.RgbLed).ToList();
|
||||
List<Led> extraLeds = LedGroup.Where(l => Layer.Leds.All(layerLed => layerLed.RgbLed != l)).ToList();
|
||||
LedGroup.AddLeds(missingLeds);
|
||||
LedGroup.RemoveLeds(extraLeds);
|
||||
LedGroup.Brush = GetBrush();
|
||||
}
|
||||
|
||||
internal override void Initialize()
|
||||
{
|
||||
if (RgbService == null)
|
||||
throw new ArtemisCoreException("Cannot initialize RGB.NET layer brush because RgbService is not set");
|
||||
|
||||
LedGroup = new ListLedGroup(RgbService.Surface);
|
||||
Layer.RenderPropertiesUpdated += LayerOnRenderPropertiesUpdated;
|
||||
|
||||
InitializeProperties();
|
||||
UpdateLedGroup();
|
||||
}
|
||||
|
||||
#region IDisposable
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -87,6 +61,35 @@ namespace Artemis.Core.LayerBrushes
|
||||
|
||||
#endregion
|
||||
|
||||
internal void UpdateLedGroup()
|
||||
{
|
||||
if (LedGroup == null)
|
||||
return;
|
||||
|
||||
if (Layer.Parent != null)
|
||||
LedGroup.ZIndex = Layer.Parent.Children.Count - Layer.Parent.Children.IndexOf(Layer);
|
||||
else
|
||||
LedGroup.ZIndex = 1;
|
||||
|
||||
List<Led> missingLeds = Layer.Leds.Where(l => !LedGroup.ContainsLed(l.RgbLed)).Select(l => l.RgbLed).ToList();
|
||||
List<Led> extraLeds = LedGroup.Where(l => Layer.Leds.All(layerLed => layerLed.RgbLed != l)).ToList();
|
||||
LedGroup.AddLeds(missingLeds);
|
||||
LedGroup.RemoveLeds(extraLeds);
|
||||
LedGroup.Brush = GetBrush();
|
||||
}
|
||||
|
||||
internal override void Initialize()
|
||||
{
|
||||
if (RgbService == null)
|
||||
throw new ArtemisCoreException("Cannot initialize RGB.NET layer brush because RgbService is not set");
|
||||
|
||||
LedGroup = new ListLedGroup(RgbService.Surface);
|
||||
Layer.RenderPropertiesUpdated += LayerOnRenderPropertiesUpdated;
|
||||
|
||||
InitializeProperties();
|
||||
UpdateLedGroup();
|
||||
}
|
||||
|
||||
// Not used in this effect type
|
||||
internal override void InternalRender(SKCanvas canvas, SKRect bounds, SKPaint paint)
|
||||
{
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Artemis.Core
|
||||
/// </summary>
|
||||
public class Plugin : CorePropertyChanged, IDisposable
|
||||
{
|
||||
private readonly List<PluginFeature> _features;
|
||||
private readonly List<PluginFeatureInfo> _features;
|
||||
|
||||
private bool _isEnabled;
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace Artemis.Core
|
||||
Directory = directory;
|
||||
Entity = pluginEntity ?? new PluginEntity {Id = Guid, IsEnabled = true};
|
||||
|
||||
_features = new List<PluginFeature>();
|
||||
_features = new List<PluginFeatureInfo>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -61,7 +61,7 @@ namespace Artemis.Core
|
||||
/// <summary>
|
||||
/// Gets a read-only collection of all features this plugin provides
|
||||
/// </summary>
|
||||
public ReadOnlyCollection<PluginFeature> Features => _features.AsReadOnly();
|
||||
public ReadOnlyCollection<PluginFeatureInfo> Features => _features.AsReadOnly();
|
||||
|
||||
/// <summary>
|
||||
/// The assembly the plugin code lives in
|
||||
@@ -107,7 +107,7 @@ namespace Artemis.Core
|
||||
/// <returns>If found, the instance of the feature</returns>
|
||||
public T? GetFeature<T>() where T : PluginFeature
|
||||
{
|
||||
return _features.FirstOrDefault(i => i is T) as T;
|
||||
return _features.FirstOrDefault(i => i.Instance is T) as T;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -122,31 +122,32 @@ namespace Artemis.Core
|
||||
Entity.IsEnabled = IsEnabled;
|
||||
}
|
||||
|
||||
internal void AddFeature(PluginFeature feature)
|
||||
internal void AddFeature(PluginFeatureInfo featureInfo)
|
||||
{
|
||||
feature.Plugin = this;
|
||||
_features.Add(feature);
|
||||
if (featureInfo.Plugin != this)
|
||||
throw new ArtemisCoreException("Feature is not associated with this plugin");
|
||||
_features.Add(featureInfo);
|
||||
|
||||
OnFeatureAdded(new PluginFeatureEventArgs(feature));
|
||||
OnFeatureAdded(new PluginFeatureInfoEventArgs(featureInfo));
|
||||
}
|
||||
|
||||
internal void RemoveFeature(PluginFeature feature)
|
||||
internal void RemoveFeature(PluginFeatureInfo featureInfo)
|
||||
{
|
||||
if (feature.IsEnabled)
|
||||
if (featureInfo.Instance != null && featureInfo.Instance.IsEnabled)
|
||||
throw new ArtemisCoreException("Cannot remove an enabled feature from a plugin");
|
||||
|
||||
_features.Remove(feature);
|
||||
feature.Dispose();
|
||||
|
||||
OnFeatureRemoved(new PluginFeatureEventArgs(feature));
|
||||
_features.Remove(featureInfo);
|
||||
featureInfo.Instance?.Dispose();
|
||||
|
||||
OnFeatureRemoved(new PluginFeatureInfoEventArgs(featureInfo));
|
||||
}
|
||||
|
||||
|
||||
internal void SetEnabled(bool enable)
|
||||
{
|
||||
if (IsEnabled == enable)
|
||||
return;
|
||||
|
||||
if (!enable && Features.Any(e => e.IsEnabled))
|
||||
if (!enable && Features.Any(e => e.Instance != null && e.Instance.IsEnabled))
|
||||
throw new ArtemisCoreException("Cannot disable this plugin because it still has enabled features");
|
||||
|
||||
IsEnabled = enable;
|
||||
@@ -176,8 +177,8 @@ namespace Artemis.Core
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
foreach (PluginFeature feature in Features)
|
||||
feature.Dispose();
|
||||
foreach (PluginFeatureInfo feature in Features)
|
||||
feature.Instance?.Dispose();
|
||||
SetEnabled(false);
|
||||
|
||||
Kernel?.Dispose();
|
||||
@@ -189,7 +190,7 @@ namespace Artemis.Core
|
||||
_features.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
@@ -198,7 +199,7 @@ namespace Artemis.Core
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
@@ -214,12 +215,12 @@ namespace Artemis.Core
|
||||
/// <summary>
|
||||
/// Occurs when an feature is loaded and added to the plugin
|
||||
/// </summary>
|
||||
public event EventHandler<PluginFeatureEventArgs>? FeatureAdded;
|
||||
public event EventHandler<PluginFeatureInfoEventArgs>? FeatureAdded;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when an feature is disabled and removed from the plugin
|
||||
/// </summary>
|
||||
public event EventHandler<PluginFeatureEventArgs>? FeatureRemoved;
|
||||
public event EventHandler<PluginFeatureInfoEventArgs>? FeatureRemoved;
|
||||
|
||||
/// <summary>
|
||||
/// Invokes the Enabled event
|
||||
@@ -240,7 +241,7 @@ namespace Artemis.Core
|
||||
/// <summary>
|
||||
/// Invokes the FeatureAdded event
|
||||
/// </summary>
|
||||
protected virtual void OnFeatureAdded(PluginFeatureEventArgs e)
|
||||
protected virtual void OnFeatureAdded(PluginFeatureInfoEventArgs e)
|
||||
{
|
||||
FeatureAdded?.Invoke(this, e);
|
||||
}
|
||||
@@ -248,7 +249,7 @@ namespace Artemis.Core
|
||||
/// <summary>
|
||||
/// Invokes the FeatureRemoved event
|
||||
/// </summary>
|
||||
protected virtual void OnFeatureRemoved(PluginFeatureEventArgs e)
|
||||
protected virtual void OnFeatureRemoved(PluginFeatureInfoEventArgs e)
|
||||
{
|
||||
FeatureRemoved?.Invoke(this, e);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Artemis.Core.DataModelExpansions;
|
||||
using System;
|
||||
using Artemis.Core.DataModelExpansions;
|
||||
using Artemis.Core.DeviceProviders;
|
||||
using Artemis.Core.LayerBrushes;
|
||||
using Artemis.Core.LayerEffects;
|
||||
@@ -16,22 +17,48 @@ namespace Artemis.Core
|
||||
{
|
||||
private string? _description;
|
||||
private string? _icon;
|
||||
private PluginFeature? _instance;
|
||||
private string _name = null!;
|
||||
private PluginFeature _pluginFeature = null!;
|
||||
|
||||
internal PluginFeatureInfo()
|
||||
internal PluginFeatureInfo(Plugin plugin, Type featureType, PluginFeatureAttribute? attribute)
|
||||
{
|
||||
Plugin = plugin ?? throw new ArgumentNullException(nameof(plugin));
|
||||
FeatureType = featureType ?? throw new ArgumentNullException(nameof(featureType));
|
||||
|
||||
Name = attribute?.Name ?? featureType.Name.Humanize(LetterCasing.Title);
|
||||
Description = attribute?.Description;
|
||||
Icon = attribute?.Icon;
|
||||
|
||||
if (Icon != null) return;
|
||||
if (typeof(BaseDataModelExpansion).IsAssignableFrom(featureType))
|
||||
Icon = "TableAdd";
|
||||
else if (typeof(DeviceProvider).IsAssignableFrom(featureType))
|
||||
Icon = "Devices";
|
||||
else if (typeof(ProfileModule).IsAssignableFrom(featureType))
|
||||
Icon = "VectorRectangle";
|
||||
else if (typeof(Module).IsAssignableFrom(featureType))
|
||||
Icon = "GearBox";
|
||||
else if (typeof(LayerBrushProvider).IsAssignableFrom(featureType))
|
||||
Icon = "Brush";
|
||||
else if (typeof(LayerEffectProvider).IsAssignableFrom(featureType))
|
||||
Icon = "AutoAwesome";
|
||||
else
|
||||
Icon = "Plugin";
|
||||
}
|
||||
|
||||
internal PluginFeatureInfo(PluginFeature instance, PluginFeatureAttribute? attribute)
|
||||
internal PluginFeatureInfo(Plugin plugin, PluginFeatureAttribute? attribute, PluginFeature instance)
|
||||
{
|
||||
if (instance == null) throw new ArgumentNullException(nameof(instance));
|
||||
Plugin = plugin ?? throw new ArgumentNullException(nameof(plugin));
|
||||
FeatureType = instance.GetType();
|
||||
|
||||
Name = attribute?.Name ?? instance.GetType().Name.Humanize(LetterCasing.Title);
|
||||
Description = attribute?.Description;
|
||||
Icon = attribute?.Icon;
|
||||
PluginFeature = instance;
|
||||
Instance = instance;
|
||||
|
||||
if (Icon != null) return;
|
||||
Icon = PluginFeature switch
|
||||
Icon = Instance switch
|
||||
{
|
||||
BaseDataModelExpansion => "TableAdd",
|
||||
DeviceProvider => "Devices",
|
||||
@@ -43,6 +70,16 @@ namespace Artemis.Core
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin this feature info is associated with
|
||||
/// </summary>
|
||||
public Plugin Plugin { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of the feature
|
||||
/// </summary>
|
||||
public Type FeatureType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The name of the plugin
|
||||
/// </summary>
|
||||
@@ -75,18 +112,18 @@ namespace Artemis.Core
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin this info is associated with
|
||||
/// Gets the feature this info is associated with
|
||||
/// </summary>
|
||||
public PluginFeature PluginFeature
|
||||
public PluginFeature? Instance
|
||||
{
|
||||
get => _pluginFeature;
|
||||
internal set => SetAndNotify(ref _pluginFeature, value);
|
||||
get => _instance;
|
||||
internal set => SetAndNotify(ref _instance, value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return PluginFeature.Id;
|
||||
return Instance?.Id ?? "Uninitialized feature";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,7 @@ namespace Artemis.Core.Services
|
||||
bool IsRenderPaused { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Recreates the Texture to use the given <see cref="SKBitmap"/>
|
||||
/// Recreates the Texture to use the given <see cref="SKBitmap" />
|
||||
/// </summary>
|
||||
/// <param name="bitmap"></param>
|
||||
void UpdateTexture(SKBitmap bitmap);
|
||||
@@ -119,7 +119,16 @@ namespace Artemis.Core.Services
|
||||
/// </summary>
|
||||
void SaveDevices();
|
||||
|
||||
/// <summary>
|
||||
/// Enables the provided device
|
||||
/// </summary>
|
||||
/// <param name="device">The device to enable</param>
|
||||
void EnableDevice(ArtemisDevice device);
|
||||
|
||||
/// <summary>
|
||||
/// Disables the provided device
|
||||
/// </summary>
|
||||
/// <param name="device">The device to disable</param>
|
||||
void DisableDevice(ArtemisDevice device);
|
||||
|
||||
/// <summary>
|
||||
@@ -133,7 +142,7 @@ namespace Artemis.Core.Services
|
||||
event EventHandler<DeviceEventArgs> DeviceRemoved;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when the surface has had modifications to its LED collection
|
||||
/// Occurs when the surface has had modifications to its LED collection
|
||||
/// </summary>
|
||||
event EventHandler LedsChanged;
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ namespace Artemis.Core.Services
|
||||
// If this is a profile module, animate profile disable
|
||||
// module.Deactivate would do the same but without animation
|
||||
if (module.IsActivated && module is ProfileModule profileModule)
|
||||
await profileModule.ChangeActiveProfileAnimated(null, null);
|
||||
await profileModule.ChangeActiveProfileAnimated(null, Enumerable.Empty<ArtemisDevice>());
|
||||
|
||||
module.Deactivate(false);
|
||||
}
|
||||
@@ -210,6 +210,7 @@ namespace Artemis.Core.Services
|
||||
List<Module> modules = _pluginManagementService.GetFeaturesOfType<Module>().ToList();
|
||||
List<Task> tasks = new();
|
||||
foreach (Module module in modules)
|
||||
{
|
||||
lock (module)
|
||||
{
|
||||
bool shouldBeActivated = module.EvaluateActivationRequirements() && module.IsEnabled;
|
||||
@@ -218,6 +219,7 @@ namespace Artemis.Core.Services
|
||||
else if (!shouldBeActivated && module.IsActivated)
|
||||
tasks.Add(DeactivateModule(module));
|
||||
}
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using Artemis.Core.DeviceProviders;
|
||||
using Artemis.Core.Ninject;
|
||||
using Artemis.Storage.Entities.Plugins;
|
||||
@@ -130,7 +129,11 @@ namespace Artemis.Core.Services
|
||||
{
|
||||
lock (_plugins)
|
||||
{
|
||||
return _plugins.Where(p => p.IsEnabled).SelectMany(p => p.Features.Where(i => i.IsEnabled && i is T)).Cast<T>().ToList();
|
||||
return _plugins.Where(p => p.IsEnabled)
|
||||
.SelectMany(p => p.Features.Where(f => f.Instance != null && f.Instance.IsEnabled && f.Instance is T))
|
||||
.Select(f => f.Instance)
|
||||
.Cast<T>()
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,6 +189,7 @@ namespace Artemis.Core.Services
|
||||
// Load the plugin assemblies into the plugin context
|
||||
DirectoryInfo pluginDirectory = new(Path.Combine(Constants.DataFolder, "plugins"));
|
||||
foreach (DirectoryInfo subDirectory in pluginDirectory.EnumerateDirectories())
|
||||
{
|
||||
try
|
||||
{
|
||||
LoadPlugin(subDirectory);
|
||||
@@ -194,6 +198,7 @@ namespace Artemis.Core.Services
|
||||
{
|
||||
_logger.Warning(new ArtemisPluginException("Failed to load plugin", e), "Plugin exception");
|
||||
}
|
||||
}
|
||||
|
||||
// ReSharper disable InconsistentlySynchronizedField - It's read-only, idc
|
||||
_logger.Debug("Loaded {count} plugin(s)", _plugins.Count);
|
||||
@@ -218,7 +223,7 @@ namespace Artemis.Core.Services
|
||||
foreach (Plugin plugin in _plugins.Where(p => p.Entity.IsEnabled))
|
||||
EnablePlugin(plugin, false, ignorePluginLock);
|
||||
|
||||
_logger.Debug("Enabled {count} plugin(s)", _plugins.Where(p => p.IsEnabled).Sum(p => p.Features.Count(f => f.IsEnabled)));
|
||||
_logger.Debug("Enabled {count} plugin(s)", _plugins.Count(p => p.IsEnabled));
|
||||
// ReSharper restore InconsistentlySynchronizedField
|
||||
|
||||
LoadingPlugins = false;
|
||||
@@ -289,6 +294,28 @@ namespace Artemis.Core.Services
|
||||
throw new ArtemisPluginException(plugin, "Failed to load the plugins assembly", e);
|
||||
}
|
||||
|
||||
// Get the Plugin feature from the main assembly and if there is only one, instantiate it
|
||||
List<Type> featureTypes;
|
||||
try
|
||||
{
|
||||
featureTypes = plugin.Assembly.GetTypes().Where(t => typeof(PluginFeature).IsAssignableFrom(t)).ToList();
|
||||
}
|
||||
catch (ReflectionTypeLoadException e)
|
||||
{
|
||||
throw new ArtemisPluginException(
|
||||
plugin,
|
||||
"Failed to initialize the plugin assembly",
|
||||
// ReSharper disable once RedundantEnumerableCastCall - Casting from nullable to non-nullable here
|
||||
new AggregateException(e.LoaderExceptions.Where(le => le != null).Cast<Exception>().ToArray())
|
||||
);
|
||||
}
|
||||
|
||||
foreach (Type featureType in featureTypes)
|
||||
plugin.AddFeature(new PluginFeatureInfo(plugin, featureType, (PluginFeatureAttribute?) Attribute.GetCustomAttribute(featureType, typeof(PluginFeatureAttribute))));
|
||||
|
||||
if (!featureTypes.Any())
|
||||
_logger.Warning("Plugin {plugin} contains no features", plugin);
|
||||
|
||||
List<Type> bootstrappers = plugin.Assembly.GetTypes().Where(t => typeof(IPluginBootstrapper).IsAssignableFrom(t)).ToList();
|
||||
if (bootstrappers.Count > 1)
|
||||
_logger.Warning($"{plugin} has more than one bootstrapper, only initializing {bootstrappers.First().FullName}");
|
||||
@@ -315,60 +342,45 @@ namespace Artemis.Core.Services
|
||||
|
||||
plugin.SetEnabled(true);
|
||||
|
||||
// Get the Plugin feature from the main assembly and if there is only one, instantiate it
|
||||
List<Type> featureTypes;
|
||||
try
|
||||
{
|
||||
featureTypes = plugin.Assembly.GetTypes().Where(t => typeof(PluginFeature).IsAssignableFrom(t)).ToList();
|
||||
}
|
||||
catch (ReflectionTypeLoadException e)
|
||||
{
|
||||
throw new ArtemisPluginException(
|
||||
plugin,
|
||||
"Failed to initialize the plugin assembly",
|
||||
// ReSharper disable once RedundantEnumerableCastCall - Casting from nullable to non-nullable here
|
||||
new AggregateException(e.LoaderExceptions.Where(le => le != null).Cast<Exception>().ToArray())
|
||||
);
|
||||
}
|
||||
|
||||
if (!featureTypes.Any())
|
||||
_logger.Warning("Plugin {plugin} contains no features", plugin);
|
||||
|
||||
// Create instances of each feature and add them to the plugin
|
||||
// Create instances of each feature
|
||||
// Construction should be simple and not contain any logic so failure at this point means the entire plugin fails
|
||||
foreach (Type featureType in featureTypes)
|
||||
foreach (PluginFeatureInfo featureInfo in plugin.Features)
|
||||
{
|
||||
try
|
||||
{
|
||||
plugin.Kernel.Bind(featureType).ToSelf().InSingletonScope();
|
||||
plugin.Kernel.Bind(featureInfo.FeatureType).ToSelf().InSingletonScope();
|
||||
|
||||
// Include Plugin as a parameter for the PluginSettingsProvider
|
||||
IParameter[] parameters = {new Parameter("Plugin", plugin, false)};
|
||||
PluginFeature instance = (PluginFeature) plugin.Kernel.Get(featureType, parameters);
|
||||
PluginFeature instance = (PluginFeature) plugin.Kernel.Get(featureInfo.FeatureType, parameters);
|
||||
|
||||
// Get the PluginFeature attribute which contains extra info on the feature
|
||||
PluginFeatureAttribute? pluginFeatureAttribute = (PluginFeatureAttribute?) Attribute.GetCustomAttribute(featureType, typeof(PluginFeatureAttribute));
|
||||
instance.Info = new PluginFeatureInfo(instance, pluginFeatureAttribute);
|
||||
plugin.AddFeature(instance);
|
||||
featureInfo.Instance = instance;
|
||||
instance.Info = featureInfo;
|
||||
instance.Plugin = plugin;
|
||||
|
||||
// Load the enabled state and if not found, default to true
|
||||
instance.Entity = plugin.Entity.Features.FirstOrDefault(i => i.Type == featureType.FullName) ??
|
||||
new PluginFeatureEntity {IsEnabled = plugin.Info.AutoEnableFeatures, Type = featureType.FullName!};
|
||||
instance.Entity = plugin.Entity.Features.FirstOrDefault(i => i.Type == featureInfo.FeatureType.FullName) ??
|
||||
new PluginFeatureEntity {IsEnabled = plugin.Info.AutoEnableFeatures, Type = featureInfo.FeatureType.FullName!};
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Warning(new ArtemisPluginException(plugin, "Failed to instantiate feature", e), "Failed to instantiate feature", plugin);
|
||||
}
|
||||
}
|
||||
|
||||
// Activate plugins after they are all loaded
|
||||
foreach (PluginFeature pluginFeature in plugin.Features.Where(i => i.Entity.IsEnabled))
|
||||
// Activate features after they are all loaded
|
||||
foreach (PluginFeatureInfo pluginFeature in plugin.Features.Where(f => f.Instance != null && f.Instance.Entity.IsEnabled))
|
||||
{
|
||||
try
|
||||
{
|
||||
EnablePluginFeature(pluginFeature, false, !ignorePluginLock);
|
||||
EnablePluginFeature(pluginFeature.Instance!, false, !ignorePluginLock);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// ignored, logged in EnablePluginFeature
|
||||
}
|
||||
}
|
||||
|
||||
if (saveState)
|
||||
{
|
||||
@@ -406,12 +418,10 @@ namespace Artemis.Core.Services
|
||||
if (!plugin.IsEnabled)
|
||||
return;
|
||||
|
||||
while (plugin.Features.Any())
|
||||
foreach (PluginFeatureInfo pluginFeatureInfo in plugin.Features)
|
||||
{
|
||||
PluginFeature feature = plugin.Features[0];
|
||||
if (feature.IsEnabled)
|
||||
DisablePluginFeature(feature, false);
|
||||
plugin.RemoveFeature(feature);
|
||||
if (pluginFeatureInfo.Instance != null && pluginFeatureInfo.Instance.IsEnabled)
|
||||
DisablePluginFeature(pluginFeatureInfo.Instance, false);
|
||||
}
|
||||
|
||||
plugin.SetEnabled(false);
|
||||
@@ -450,7 +460,6 @@ namespace Artemis.Core.Services
|
||||
|
||||
Plugin? existing = _plugins.FirstOrDefault(p => p.Guid == pluginInfo.Guid);
|
||||
if (existing != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
RemovePlugin(existing);
|
||||
@@ -459,7 +468,6 @@ namespace Artemis.Core.Services
|
||||
{
|
||||
throw new ArtemisPluginException("A plugin with the same GUID is already loaded, failed to remove old version", e);
|
||||
}
|
||||
}
|
||||
|
||||
string targetDirectory = pluginInfo.Main.Split(".dll")[0].Replace("/", "").Replace("\\", "");
|
||||
string uniqueTargetDirectory = targetDirectory;
|
||||
@@ -582,9 +590,11 @@ namespace Artemis.Core.Services
|
||||
|
||||
private void SavePlugin(Plugin plugin)
|
||||
{
|
||||
foreach (PluginFeature pluginFeature in plugin.Features)
|
||||
if (plugin.Entity.Features.All(i => i.Type != pluginFeature.GetType().FullName))
|
||||
plugin.Entity.Features.Add(pluginFeature.Entity);
|
||||
foreach (PluginFeatureInfo featureInfo in plugin.Features.Where(i => i.Instance != null))
|
||||
{
|
||||
if (plugin.Entity.Features.All(i => i.Type != featureInfo.FeatureType.FullName))
|
||||
plugin.Entity.Features.Add(featureInfo.Instance!.Entity);
|
||||
}
|
||||
|
||||
_pluginRepository.SavePlugin(plugin.Entity);
|
||||
}
|
||||
|
||||
@@ -146,7 +146,8 @@ namespace Artemis.Core.Services
|
||||
|
||||
void ActivatingProfilePluginToggle(object? sender, PluginEventArgs e)
|
||||
{
|
||||
InstantiateProfile(profile);
|
||||
if (!profile.Disposed)
|
||||
InstantiateProfile(profile);
|
||||
}
|
||||
|
||||
// This could happen during activation so subscribe to it
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Artemis.Core
|
||||
{
|
||||
public CorePluginFeature()
|
||||
{
|
||||
Constants.CorePlugin.AddFeature(this);
|
||||
Constants.CorePlugin.AddFeature(new PluginFeatureInfo(Constants.CorePlugin, null, this));
|
||||
IsEnabled = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ namespace Artemis.Core
|
||||
foreach (LayerEntity profileEntityLayer in profileEntity.Layers)
|
||||
profileEntityLayer.Leds.AddRange(_devices.SelectMany(d => d.Leds).Select(l => new LedEntity
|
||||
{
|
||||
DeviceIdentifier = l.Device.RgbDevice.GetDeviceIdentifier(),
|
||||
DeviceIdentifier = l.Device.Identifier,
|
||||
LedName = l.RgbLed.Id.ToString()
|
||||
}));
|
||||
|
||||
|
||||
Reference in new issue
Block a user