mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 03:48:56 +00:00
Added support for built-in plugins
Added dependency injection to IPlugin construction
This commit is contained in:
1 parent
d076cf0656
commit
a465c4cd8b
27 files changed
+557
-1141
No files matched your search
@@ -1,9 +1,18 @@
|
||||
using Artemis.Plugins.Interfaces;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.Plugins.Exceptions;
|
||||
using Artemis.Plugins.Interfaces;
|
||||
using CSScriptLibrary;
|
||||
using Newtonsoft.Json;
|
||||
using Ninject;
|
||||
|
||||
namespace Artemis.Plugins.Models
|
||||
{
|
||||
public class PluginInfo
|
||||
public class PluginInfo : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the plugin
|
||||
@@ -36,5 +45,53 @@ namespace Artemis.Plugins.Models
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public string Folder { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Indicates wether this is a built-in plugin. Built-in plugins are precompiled and have no files
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public bool IsBuiltIn { get; private set; }
|
||||
|
||||
public static async Task<PluginInfo> FromFolder(IKernel kernel, 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 assembly = await CSScript.Evaluator.CompileCodeAsync(File.ReadAllText(folder + pluginInfo.Main));
|
||||
var pluginType = assembly.GetTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t)).ToList();
|
||||
if (!pluginType.Any())
|
||||
throw new ArtemisPluginException(pluginInfo, "Failed to load plugin, no type found that implements IPlugin");
|
||||
if (pluginType.Count > 1)
|
||||
throw new ArtemisPluginException(pluginInfo, "Failed to load plugin, more than one type found that implements IPlugin");
|
||||
|
||||
pluginInfo.Plugin = (IPlugin) kernel.Get(pluginType.First());
|
||||
pluginInfo.Plugin.LoadPlugin();
|
||||
|
||||
return pluginInfo;
|
||||
}
|
||||
|
||||
public static PluginInfo FromBuiltInPlugin(IKernel kernel, IPlugin builtInPlugin)
|
||||
{
|
||||
var pluginInfo = new PluginInfo
|
||||
{
|
||||
Name = builtInPlugin.GetType().Name,
|
||||
Version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion,
|
||||
Plugin = builtInPlugin,
|
||||
IsBuiltIn = true
|
||||
};
|
||||
pluginInfo.Plugin.LoadPlugin();
|
||||
|
||||
return pluginInfo;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user