Plugins - Simplified and streamlined the way plugins provide VMs

Plugins - Added dependency injection to all plugin VMs
Plugin settings - Allow injecting plugin settings into any class defined in the plugin assembly
This commit is contained in:
Robert committed 2020-08-21 11:14:53 +02:00
1 parent aa3c3d6b95
commit c71faba2c9
29 files changed
+349 -165

No files matched your search

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Artemis.Core.Events;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Models;
@@ -75,7 +76,19 @@ namespace Artemis.Core.Services.Interfaces
/// <returns>Returns a list of plug instances of type <see cref="T" /></returns>
List<T> GetPluginsOfType<T>() where T : Plugin;
Plugin GetDevicePlugin(IRGBDevice device);
/// <summary>
/// Gets the plugin that provided the specified assembly
/// </summary>
/// <param name="assembly"></param>
/// <returns></returns>
Plugin GetPluginByAssembly(Assembly assembly);
/// <summary>
/// Gets the plugin that defined the specified device
/// </summary>
/// <param name="device"></param>
/// <returns></returns>
Plugin GetPluginByDevice(IRGBDevice device);
#region Events
+6 -9
View File
@@ -46,10 +46,8 @@ namespace Artemis.Core.Services
Directory.CreateDirectory(Constants.DataFolder + "plugins");
}
/// <inheritdoc />
public bool LoadingPlugins { get; private set; }
/// <inheritdoc />
public void CopyBuiltInPlugins()
{
OnCopyingBuildInPlugins();
@@ -161,7 +159,6 @@ namespace Artemis.Core.Services
}
}
/// <inheritdoc />
public void UnloadPlugins()
{
lock (_plugins)
@@ -181,7 +178,6 @@ namespace Artemis.Core.Services
}
}
/// <inheritdoc />
public void LoadPlugin(PluginInfo pluginInfo)
{
lock (_plugins)
@@ -256,7 +252,6 @@ namespace Artemis.Core.Services
}
}
/// <inheritdoc />
public void UnloadPlugin(PluginInfo pluginInfo)
{
lock (_plugins)
@@ -347,25 +342,27 @@ namespace Artemis.Core.Services
OnPluginDisabled(new PluginEventArgs(plugin.PluginInfo));
}
/// <inheritdoc />
public PluginInfo GetPluginInfo(Plugin plugin)
{
return _plugins.FirstOrDefault(p => p.Instance == plugin);
}
/// <inheritdoc />
public List<PluginInfo> GetAllPluginInfo()
{
return new List<PluginInfo>(_plugins);
}
/// <inheritdoc />
public List<T> GetPluginsOfType<T>() where T : Plugin
{
return _plugins.Where(p => p.Enabled && p.Instance is T).Select(p => (T) p.Instance).ToList();
}
public Plugin GetDevicePlugin(IRGBDevice rgbDevice)
public Plugin GetPluginByAssembly(Assembly assembly)
{
return _plugins.FirstOrDefault(p => p.Assembly == assembly)?.Instance;
}
public Plugin GetPluginByDevice(IRGBDevice rgbDevice)
{
return GetPluginsOfType<DeviceProvider>().First(d => d.RgbDeviceProvider.Devices != null && d.RgbDeviceProvider.Devices.Contains(rgbDevice));
}
@@ -50,7 +50,7 @@ namespace Artemis.Core.Services.Storage
// Add all current devices
foreach (var rgbDevice in _rgbService.LoadedDevices)
{
var plugin = _pluginService.GetDevicePlugin(rgbDevice);
var plugin = _pluginService.GetPluginByDevice(rgbDevice);
configuration.Devices.Add(new ArtemisDevice(rgbDevice, plugin, configuration));
}
@@ -142,7 +142,7 @@ namespace Artemis.Core.Services.Storage
var device = _rgbService.Surface.Devices.FirstOrDefault(d => d.GetDeviceIdentifier() == position.DeviceIdentifier);
if (device != null)
{
var plugin = _pluginService.GetDevicePlugin(device);
var plugin = _pluginService.GetPluginByDevice(device);
surfaceConfiguration.Devices.Add(new ArtemisDevice(device, plugin, surfaceConfiguration, position));
}
}
@@ -184,7 +184,7 @@ namespace Artemis.Core.Services.Storage
var existingDeviceConfig = surface.SurfaceEntity.DeviceEntities.FirstOrDefault(d => d.DeviceIdentifier == deviceIdentifier);
if (existingDeviceConfig != null)
{
var plugin = _pluginService.GetDevicePlugin(rgbDevice);
var plugin = _pluginService.GetPluginByDevice(rgbDevice);
device = new ArtemisDevice(rgbDevice, plugin, surface, existingDeviceConfig);
}
// Fall back on creating a new device
@@ -195,7 +195,7 @@ namespace Artemis.Core.Services.Storage
rgbDevice.DeviceInfo,
deviceIdentifier
);
var plugin = _pluginService.GetDevicePlugin(rgbDevice);
var plugin = _pluginService.GetPluginByDevice(rgbDevice);
device = new ArtemisDevice(rgbDevice, plugin, surface);
}