mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 11:56:30 +00:00
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:
1 parent
aa3c3d6b95
commit
c71faba2c9
29 files changed
+349
-165
No files matched your search
@@ -2,6 +2,7 @@
|
||||
using Artemis.Core.Exceptions;
|
||||
using Artemis.Core.Plugins.Abstract;
|
||||
using Artemis.Core.Plugins.Models;
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
using Artemis.Storage.Repositories.Interfaces;
|
||||
using Ninject.Activation;
|
||||
|
||||
@@ -10,20 +11,28 @@ namespace Artemis.Core.Ninject
|
||||
internal class PluginSettingsProvider : Provider<PluginSettings>
|
||||
{
|
||||
private readonly IPluginRepository _pluginRepository;
|
||||
private readonly IPluginService _pluginService;
|
||||
|
||||
internal PluginSettingsProvider(IPluginRepository pluginRepository)
|
||||
internal PluginSettingsProvider(IPluginRepository pluginRepository, IPluginService pluginService)
|
||||
{
|
||||
_pluginRepository = pluginRepository;
|
||||
_pluginService = pluginService;
|
||||
}
|
||||
|
||||
protected override PluginSettings CreateInstance(IContext context)
|
||||
{
|
||||
var parentRequest = context.Request.ParentRequest;
|
||||
if (parentRequest == null || !typeof(Plugin).IsAssignableFrom(parentRequest.Service))
|
||||
throw new ArtemisCoreException("PluginSettings can only be injected into a plugin");
|
||||
if (parentRequest == null)
|
||||
throw new ArtemisCoreException("PluginSettings couldn't be injected, failed to get the injection parent request");
|
||||
|
||||
// First try by PluginInfo parameter
|
||||
var pluginInfo = parentRequest.Parameters.FirstOrDefault(p => p.Name == "PluginInfo")?.GetValue(context, null) as PluginInfo;
|
||||
if (pluginInfo == null)
|
||||
throw new ArtemisCoreException("A plugin needs to be initialized with PluginInfo as a parameter");
|
||||
pluginInfo = _pluginService.GetPluginByAssembly(parentRequest.Service.Assembly)?.PluginInfo;
|
||||
// Fall back to assembly based detection
|
||||
if (pluginInfo == null)
|
||||
throw new ArtemisCoreException("PluginSettings can only be injected with the PluginInfo parameter provided " +
|
||||
"or into a class defined in a plugin assembly");
|
||||
|
||||
return new PluginSettings(pluginInfo, _pluginRepository);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user