1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Plugins - Added utility to get the calling plugin

Plugins - Removed IParameter overloads to avoid Ninject dependency in plugins
This commit is contained in:
SpoinkyNL 2020-11-04 23:10:26 +01:00
parent d59b36c3d1
commit c7f3a8283a
4 changed files with 62 additions and 51 deletions

View File

@ -70,10 +70,10 @@ namespace Artemis.Core.Services
List<PluginInfo> GetAllPluginInfo();
/// <summary>
/// Finds all enabled <see cref="Plugin" /> instances of <typeparamref name="T"/>
/// Finds all enabled <see cref="Plugin" /> instances of <typeparamref name="T" />
/// </summary>
/// <typeparam name="T">Either <see cref="Plugin" /> or a plugin type implementing <see cref="Plugin" /></typeparam>
/// <returns>Returns a list of plugin instances of <typeparamref name="T"/></returns>
/// <returns>Returns a list of plugin instances of <typeparamref name="T" /></returns>
List<T> GetPluginsOfType<T>() where T : Plugin;
/// <summary>
@ -90,6 +90,12 @@ namespace Artemis.Core.Services
/// <returns></returns>
Plugin GetPluginByDevice(IRGBDevice device);
/// <summary>
/// Returns the plugin info of the current call stack
/// </summary>
/// <returns>If the current call stack contains a plugin, the plugin. Otherwise null</returns>
Plugin GetCallingPlugin();
#region Events
/// <summary>

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
@ -360,6 +361,22 @@ namespace Artemis.Core.Services
return GetPluginsOfType<DeviceProvider>().First(d => d.RgbDeviceProvider.Devices != null && d.RgbDeviceProvider.Devices.Contains(rgbDevice));
}
public Plugin GetCallingPlugin()
{
StackTrace stackTrace = new StackTrace(); // get call stack
StackFrame[] stackFrames = stackTrace.GetFrames(); // get method calls (frames)
foreach (StackFrame stackFrame in stackFrames)
{
Assembly assembly = stackFrame.GetMethod().DeclaringType.Assembly;
Plugin plugin = GetPluginByAssembly(assembly);
if (plugin != null)
return plugin;
}
return null;
}
public void Dispose()
{
UnloadPlugins();

View File

@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using Artemis.Core;
using Artemis.Core.Services;
using Artemis.UI.Shared.Screens.Dialogs;
using Artemis.UI.Shared.Screens.Exceptions;
using MaterialDesignThemes.Wpf;
@ -18,17 +22,42 @@ namespace Artemis.UI.Shared.Services
private readonly IKernel _kernel;
private readonly IViewManager _viewManager;
private readonly IWindowManager _windowManager;
private readonly IPluginService _pluginService;
public DialogService(IKernel kernel, IViewManager viewManager, IWindowManager windowManager)
public DialogService(IKernel kernel, IViewManager viewManager, IWindowManager windowManager, IPluginService pluginService)
{
_kernel = kernel;
_viewManager = viewManager;
_windowManager = windowManager;
_pluginService = pluginService;
}
private async Task<object> ShowDialog<T>(IParameter[] parameters) where T : DialogViewModelBase
{
if (parameters == null) throw new ArgumentNullException(nameof(parameters));
return await ShowDialog("RootDialog", _kernel.Get<T>(parameters));
}
private async Task<object> ShowDialog(string identifier, DialogViewModelBase viewModel)
{
Task<object> result = null;
await Execute.OnUIThreadAsync(() =>
{
UIElement view = _viewManager.CreateViewForModel(viewModel);
_viewManager.BindViewToModel(view, viewModel);
if (identifier == null)
result = DialogHost.Show(view, viewModel.OnDialogOpened, viewModel.OnDialogClosed);
else
result = DialogHost.Show(view, identifier, viewModel.OnDialogOpened, viewModel.OnDialogClosed);
});
return await result;
}
public async Task<bool> ShowConfirmDialog(string header, string text, string confirmText = "Confirm", string cancelText = "Cancel")
{
IParameter[] arguments = new IParameter[]
IParameter[] arguments =
{
new ConstructorArgument("header", header),
new ConstructorArgument("text", text),
@ -41,7 +70,7 @@ namespace Artemis.UI.Shared.Services
public async Task<bool> ShowConfirmDialogAt(string identifier, string header, string text, string confirmText = "Confirm", string cancelText = "Cancel")
{
IParameter[] arguments = new IParameter[]
IParameter[] arguments =
{
new ConstructorArgument("header", header),
new ConstructorArgument("text", text),
@ -66,12 +95,6 @@ namespace Artemis.UI.Shared.Services
return ShowDialog<T>(paramsArray);
}
public async Task<object> ShowDialog<T>(IParameter[] parameters) where T : DialogViewModelBase
{
if (parameters == null) throw new ArgumentNullException(nameof(parameters));
return await ShowDialog("RootDialog", _kernel.Get<T>(parameters));
}
public async Task<object> ShowDialogAt<T>(string identifier) where T : DialogViewModelBase
{
return await ShowDialog(identifier, _kernel.Get<T>());
@ -86,9 +109,13 @@ namespace Artemis.UI.Shared.Services
return await ShowDialogAt<T>(identifier, paramsArray);
}
public async Task<object> ShowDialogAt<T>(string identifier, IParameter[] parameters) where T : DialogViewModelBase
private async Task<object> ShowDialogAt<T>(string identifier, IParameter[] parameters) where T : DialogViewModelBase
{
Plugin callingPlugin = _pluginService.GetCallingPlugin();
if (parameters == null) throw new ArgumentNullException(nameof(parameters));
if (callingPlugin != null)
return await ShowDialog(identifier, callingPlugin.PluginInfo.Kernel.Get<T>(parameters));
return await ShowDialog(identifier, _kernel.Get<T>(parameters));
}
@ -96,22 +123,5 @@ namespace Artemis.UI.Shared.Services
{
_windowManager.ShowDialog(new ExceptionViewModel(message, exception));
}
private async Task<object> ShowDialog(string identifier, DialogViewModelBase viewModel)
{
Task<object> result = null;
await Execute.OnUIThreadAsync(() =>
{
UIElement view = _viewManager.CreateViewForModel(viewModel);
_viewManager.BindViewToModel(view, viewModel);
if (identifier == null)
result = DialogHost.Show(view, viewModel.OnDialogOpened, viewModel.OnDialogClosed);
else
result = DialogHost.Show(view, identifier, viewModel.OnDialogOpened, viewModel.OnDialogClosed);
});
return await result;
}
}
}

View File

@ -51,15 +51,6 @@ namespace Artemis.UI.Shared.Services
/// <returns>A task resolving to the result of the dialog's <see cref="DialogSession" /></returns>
Task<object> ShowDialog<T>(Dictionary<string, object> parameters) where T : DialogViewModelBase;
/// <summary>
/// Shows a dialog by initializing a view model implementing <see cref="DialogViewModelBase" /> using an array of
/// Ninject <see cref="IParameter" />, requires you to reference Ninject
/// </summary>
/// <typeparam name="T">The type of the view model</typeparam>
/// <param name="parameters">An array of Ninject <see cref="IParameter" /> to pass to the view model during activation</param>
/// <returns>A task resolving to the result of the dialog's <see cref="DialogSession" /></returns>
Task<object> ShowDialog<T>(IParameter[] parameters) where T : DialogViewModelBase;
/// <summary>
/// Shows a dialog by initializing a view model implementing <see cref="DialogViewModelBase" />
/// </summary>
@ -84,19 +75,6 @@ namespace Artemis.UI.Shared.Services
/// <returns>A task resolving to the result of the dialog's <see cref="DialogSession" /></returns>
Task<object> ShowDialogAt<T>(string identifier, Dictionary<string, object> parameters) where T : DialogViewModelBase;
/// <summary>
/// Shows a dialog by initializing a view model implementing <see cref="DialogViewModelBase" /> using an array of
/// Ninject <see cref="IParameter" />, requires you to reference Ninject
/// </summary>
/// <typeparam name="T">The type of the view model</typeparam>
/// <param name="identifier">
/// The identifier of the <see cref="DialogHost" /> to use eg.
/// <code>&lt;materialDesign:DialogHost Identifier="MyDialogHost"&gt;</code>
/// </param>
/// <param name="parameters">An array of Ninject <see cref="IParameter" /> to pass to the view model during activation</param>
/// <returns>A task resolving to the result of the dialog's <see cref="DialogSession" /></returns>
Task<object> ShowDialogAt<T>(string identifier, IParameter[] parameters) where T : DialogViewModelBase;
/// <summary>
/// Shows a dialog displaying the provided message and exception. Does not handle, log or throw the exception.
/// </summary>