1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2026-01-01 02:03:32 +00:00

Implemented plugin reloading

This commit is contained in:
SpoinkyNL 2018-03-05 23:44:39 +01:00
parent 31affd01b3
commit b80e1ea528
3 changed files with 37 additions and 18 deletions

View File

@ -1,5 +1,5 @@
using System; using System;
using Artemis.Plugins.Interfaces; using Artemis.Plugins.Models;
namespace Artemis.Core.Events namespace Artemis.Core.Events
{ {
@ -9,11 +9,11 @@ namespace Artemis.Core.Events
{ {
} }
public PluginEventArgs(IPlugin plugin) public PluginEventArgs(PluginInfo pluginInfo)
{ {
Plugin = plugin; PluginInfo = pluginInfo;
} }
public IPlugin Plugin { get; } public PluginInfo PluginInfo { get; }
} }
} }

View File

@ -39,7 +39,7 @@ namespace Artemis.Core.Services
// Empty the list of plugins // Empty the list of plugins
foreach (var pluginInfo in _plugins) foreach (var pluginInfo in _plugins)
pluginInfo.Dispose(); pluginInfo.UnloadPlugin();
_plugins.Clear(); _plugins.Clear();
// Iterate all plugin folders and load each plugin // Iterate all plugin folders and load each plugin
@ -52,17 +52,24 @@ namespace Artemis.Core.Services
/// <inheritdoc /> /// <inheritdoc />
public async Task ReloadPlugin(PluginInfo pluginInfo) public async Task ReloadPlugin(PluginInfo pluginInfo)
{ {
throw new NotImplementedException(); pluginInfo.UnloadPlugin();
await pluginInfo.CompilePlugin(_kernel);
OnPluginReloaded(new PluginEventArgs(pluginInfo));
} }
/// <inheritdoc /> /// <inheritdoc />
public async Task<IModuleViewModel> GetModuleViewModel(PluginInfo pluginInfo) public async Task<IModuleViewModel> GetModuleViewModel(PluginInfo pluginInfo)
{ {
return pluginInfo.GetModuleViewModel(_kernel); return await Task.Run(() => pluginInfo.GetModuleViewModel(_kernel));
} }
public void Dispose() public void Dispose()
{ {
// Empty the list of plugins
foreach (var pluginInfo in _plugins)
pluginInfo.UnloadPlugin();
_plugins.Clear();
} }
#region Events #region Events

View File

@ -1,5 +1,4 @@
using System; using System.IO;
using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -11,7 +10,7 @@ using Ninject;
namespace Artemis.Plugins.Models namespace Artemis.Plugins.Models
{ {
public class PluginInfo : IDisposable public class PluginInfo
{ {
private static Assembly _assembly; private static Assembly _assembly;
@ -42,9 +41,13 @@ namespace Artemis.Plugins.Models
[JsonIgnore] [JsonIgnore]
public string Folder { get; set; } public string Folder { get; set; }
public void Dispose() /// <summary>
/// Unloads the plugin and clears the plugin info's internal data
/// </summary>
public void UnloadPlugin()
{ {
Plugin.UnloadPlugin(); Plugin.UnloadPlugin();
_assembly = null;
} }
/// <summary> /// <summary>
@ -67,19 +70,28 @@ namespace Artemis.Plugins.Models
var pluginInfo = JsonConvert.DeserializeObject<PluginInfo>(File.ReadAllText(folder + "plugin.json")); var pluginInfo = JsonConvert.DeserializeObject<PluginInfo>(File.ReadAllText(folder + "plugin.json"));
pluginInfo.Folder = folder; pluginInfo.Folder = folder;
await pluginInfo.CompilePlugin(kernel);
return pluginInfo;
}
/// <summary>
/// Compiles the plugin's main CS file and any of it's includes and instantiates it.
/// </summary>
/// <param name="kernel">The Ninject kernel to use for DI</param>
public async Task CompilePlugin(IKernel kernel)
{
// Load the main script and get the type // Load the main script and get the type
_assembly = await CSScript.Evaluator.CompileCodeAsync(File.ReadAllText(folder + pluginInfo.Main)); _assembly = await CSScript.Evaluator.CompileCodeAsync(File.ReadAllText(Folder + Main));
var pluginType = _assembly.GetTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t)).ToList(); var pluginType = _assembly.GetTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t)).ToList();
if (!pluginType.Any()) if (!pluginType.Any())
throw new ArtemisPluginException(pluginInfo, "Failed to load plugin, no type found that implements IPlugin"); throw new ArtemisPluginException(this, "Failed to load plugin, no type found that implements IPlugin");
if (pluginType.Count > 1) if (pluginType.Count > 1)
throw new ArtemisPluginException(pluginInfo, "Failed to load plugin, more than one type found that implements IPlugin"); throw new ArtemisPluginException(this, "Failed to load plugin, more than one type found that implements IPlugin");
// Instantiate the plugin with Ninject // Instantiate the plugin with Ninject
pluginInfo.Plugin = (IPlugin) kernel.Get(pluginType.First()); Plugin = (IPlugin) kernel.Get(pluginType.First());
pluginInfo.Plugin.LoadPlugin(); Plugin.LoadPlugin();
return pluginInfo;
} }
/// <summary> /// <summary>