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 _plugins; public PluginService(IKernel kernel) { _kernel = kernel; _plugins = new List(); if (!Directory.Exists(Constants.DataFolder + "plugins")) Directory.CreateDirectory(Constants.DataFolder + "plugins"); } public bool LoadingPlugins { get; private set; } public ReadOnlyCollection Plugins => _plugins.AsReadOnly(); /// /// Loads all installed plugins. If plugins already loaded this will reload them all /// /// 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) { } public async Task GetPluginViewModel(PluginInfo pluginInfo) { // 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(IPluginViewModel).IsAssignableFrom(t)); if (vmType == null) throw new ArtemisPluginException(pluginInfo, "Cannot locate a view model for this plugin."); // Instantiate the ViewModel with Ninject var vm = (IPluginViewModel) _kernel.Get(vmType); vm.PluginInfo = pluginInfo; return vm; } public void Dispose() { } private async Task 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(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(folder + pluginInfo.Main); pluginInfo.Plugin = plugin; return pluginInfo; } #region Events /// /// Occurs when a single plugin has loaded /// public event EventHandler PluginLoaded; /// /// Occurs when a single plugin has reloaded /// public event EventHandler PluginReloaded; /// /// Occurs when loading all plugins has started /// public event EventHandler StartedLoadingPlugins; /// /// Occurs when loading all plugins has finished /// 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 } }