mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 19:36:28 +00:00
Added support for built-in plugins
Added dependency injection to IPlugin construction
This commit is contained in:
1 parent
f5749ccb26
commit
a51ae92fea
27 files changed
+557
-1141
No files matched your search
@@ -2,15 +2,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.Core.Events;
|
||||
using Artemis.Core.Exceptions;
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
using Artemis.Plugins.Interfaces;
|
||||
using Artemis.Plugins.Models;
|
||||
using CSScriptLibrary;
|
||||
using Newtonsoft.Json;
|
||||
using Ninject;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
@@ -24,7 +21,7 @@ namespace Artemis.Core.Services
|
||||
{
|
||||
_kernel = kernel;
|
||||
_plugins = new List<PluginInfo>();
|
||||
|
||||
|
||||
if (!Directory.Exists(Constants.DataFolder + "plugins"))
|
||||
Directory.CreateDirectory(Constants.DataFolder + "plugins");
|
||||
}
|
||||
@@ -41,12 +38,18 @@ namespace Artemis.Core.Services
|
||||
OnStartedLoadingPlugins();
|
||||
|
||||
// Empty the list of plugins
|
||||
foreach (var pluginInfo in _plugins)
|
||||
pluginInfo.Dispose();
|
||||
_plugins.Clear();
|
||||
|
||||
// Load all built-in plugins
|
||||
foreach (var builtInPlugin in _kernel.GetAll<IPlugin>())
|
||||
_plugins.Add(PluginInfo.FromBuiltInPlugin(_kernel, builtInPlugin));
|
||||
|
||||
// Iterate all plugin folders and load each plugin
|
||||
foreach (var directory in Directory.GetDirectories(Constants.DataFolder + "plugins"))
|
||||
_plugins.Add(await LoadPluginFromFolder(directory));
|
||||
|
||||
_plugins.Add(await PluginInfo.FromFolder(_kernel, directory));
|
||||
|
||||
OnFinishedLoadedPlugins();
|
||||
}
|
||||
|
||||
@@ -57,44 +60,15 @@ namespace Artemis.Core.Services
|
||||
|
||||
public async Task<IModuleViewModel> GetModuleViewModel(PluginInfo pluginInfo)
|
||||
{
|
||||
// Don't attempt to locave VMs for something other than a module
|
||||
if (pluginInfo.Plugin is IModule)
|
||||
throw new ArtemisPluginException(pluginInfo, "Cannot locate a view model for this plugin as it's not a module.");
|
||||
// Compile the ViewModel and get the type
|
||||
var compile = await Task.Run(() => CSScript.LoadFile(pluginInfo.Folder + pluginInfo.ViewModel));
|
||||
var vmType = compile.ExportedTypes.FirstOrDefault(t => typeof(IModuleViewModel).IsAssignableFrom(t));
|
||||
if (vmType == null)
|
||||
throw new ArtemisPluginException(pluginInfo, "Cannot locate a view model for this module.");
|
||||
|
||||
// Instantiate the ViewModel with Ninject
|
||||
var vm = (IModuleViewModel) _kernel.Get(vmType);
|
||||
vm.PluginInfo = pluginInfo;
|
||||
return vm;
|
||||
return await pluginInfo.GetModuleViewModel(_kernel);
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
private async Task<PluginInfo> LoadPluginFromFolder(string folder)
|
||||
{
|
||||
if (!folder.EndsWith("\\"))
|
||||
folder += "\\";
|
||||
if (!File.Exists(folder + "plugin.json"))
|
||||
throw new ArtemisPluginException(null, "Failed to load plugin, no plugin.json found in " + folder);
|
||||
|
||||
var pluginInfo = JsonConvert.DeserializeObject<PluginInfo>(File.ReadAllText(folder + "plugin.json"));
|
||||
pluginInfo.Folder = folder;
|
||||
|
||||
// Load the main plugin which will contain a class implementing IPlugin
|
||||
var plugin = await CSScript.Evaluator.LoadFileAsync<IPlugin>(folder + pluginInfo.Main);
|
||||
pluginInfo.Plugin = plugin;
|
||||
|
||||
return pluginInfo;
|
||||
}
|
||||
|
||||
#region Events
|
||||
|
||||
|
||||
public event EventHandler<PluginEventArgs> PluginLoaded;
|
||||
public event EventHandler<PluginEventArgs> PluginReloaded;
|
||||
public event EventHandler StartedLoadingPlugins;
|
||||
|
||||
@@ -5,9 +5,14 @@ using Artemis.Core.Events;
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
using RGB.NET.Brushes;
|
||||
using RGB.NET.Core;
|
||||
using RGB.NET.Devices.Asus;
|
||||
using RGB.NET.Devices.CoolerMaster;
|
||||
using RGB.NET.Devices.Corsair;
|
||||
using RGB.NET.Devices.DMX;
|
||||
using RGB.NET.Devices.Logitech;
|
||||
using RGB.NET.Devices.Msi;
|
||||
using RGB.NET.Devices.Novation;
|
||||
using RGB.NET.Devices.Razer;
|
||||
using RGB.NET.Groups;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
@@ -38,24 +43,18 @@ namespace Artemis.Core.Services
|
||||
await Task.Run(() =>
|
||||
{
|
||||
// TODO SpoinkyNL 8-1-18: Keep settings into account
|
||||
// Surface.LoadDevices(AsusDeviceProvider.Instance);
|
||||
Surface.LoadDevices(CorsairDeviceProvider.Instance);
|
||||
Surface.LoadDevices(LogitechDeviceProvider.Instance);
|
||||
Surface.LoadDevices(AsusDeviceProvider.Instance);
|
||||
Surface.LoadDevices(CoolerMasterDeviceProvider.Instance);
|
||||
// Surface.LoadDevices(NovationDeviceProvider.Instance);
|
||||
Surface.LoadDevices(CorsairDeviceProvider.Instance);
|
||||
Surface.LoadDevices(DMXDeviceProvider.Instance);
|
||||
Surface.LoadDevices(LogitechDeviceProvider.Instance);
|
||||
Surface.LoadDevices(NovationDeviceProvider.Instance);
|
||||
Surface.LoadDevices(MsiDeviceProvider.Instance);
|
||||
Surface.LoadDevices(RazerDeviceProvider.Instance);
|
||||
|
||||
// TODO SpoinkyNL 8-1-18: Load alignment
|
||||
Surface.AlignDevices();
|
||||
|
||||
// Do some testing, why does this work, how does it know I want to target the surface? Check source!
|
||||
var mouse1 = Surface.Leds.First(l => l.Id == LedId.Mouse1);
|
||||
mouse1.Color = new Color(255, 0, 0);
|
||||
var mouse2 = Surface.Leds.First(l => l.Id == LedId.Mouse2);
|
||||
mouse2.Color = new Color(255, 255, 0);
|
||||
var mouse3 = Surface.Leds.First(l => l.Id == LedId.Mouse3);
|
||||
mouse3.Color = new Color(255, 255, 255);
|
||||
var mouse4 = Surface.Leds.First(l => l.Id == LedId.Mouse4);
|
||||
mouse4.Color = new Color(255, 0, 255);
|
||||
Surface.UpdateMode = UpdateMode.Continuous;
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user