1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00
Artemis/src/Artemis.Core/Services/PluginService.cs
SpoinkyNL aada338dcc Further expanded plugins
Added navigation between modules
2018-02-25 19:48:22 +01:00

127 lines
4.3 KiB
C#

using System;
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
{
public class PluginService : IPluginService
{
private readonly IKernel _kernel;
private readonly List<PluginInfo> _plugins;
public PluginService(IKernel kernel)
{
_kernel = kernel;
_plugins = new List<PluginInfo>();
if (!Directory.Exists(Constants.DataFolder + "plugins"))
Directory.CreateDirectory(Constants.DataFolder + "plugins");
}
public bool LoadingPlugins { get; private set; }
public ReadOnlyCollection<PluginInfo> Plugins => _plugins.AsReadOnly();
/// <inheritdoc />
public async Task LoadPlugins()
{
if (LoadingPlugins)
throw new ArtemisCoreException("Cannot load plugins while a previous load hasn't been completed yet.");
OnStartedLoadingPlugins();
// Empty the list of plugins
_plugins.Clear();
// Iterate all plugin folders and load each plugin
foreach (var directory in Directory.GetDirectories(Constants.DataFolder + "plugins"))
_plugins.Add(await LoadPluginFromFolder(directory));
OnFinishedLoadedPlugins();
}
public async Task ReloadPlugin(PluginInfo pluginInfo)
{
throw new NotImplementedException();
}
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;
}
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;
public event EventHandler FinishedLoadedPlugins;
private void OnPluginLoaded(PluginEventArgs e)
{
PluginLoaded?.Invoke(this, e);
}
private void OnPluginReloaded(PluginEventArgs e)
{
PluginReloaded?.Invoke(this, e);
}
private void OnStartedLoadingPlugins()
{
LoadingPlugins = true;
StartedLoadingPlugins?.Invoke(this, EventArgs.Empty);
}
private void OnFinishedLoadedPlugins()
{
LoadingPlugins = false;
FinishedLoadedPlugins?.Invoke(this, EventArgs.Empty);
}
#endregion
}
}