Removed seperate plugins project since you're always referencing both..

Added more loading logic
This commit is contained in:
SpoinkyNL committed 2018-04-06 19:54:25 +02:00
1 parent b80e1ea528
commit 9a2ea0c2b3
57 files changed
+959 -650

No files matched your search

@@ -0,0 +1,11 @@
using Artemis.Core.Plugins.Interfaces;
using Artemis.Core.Plugins.Models;
using Stylet;
namespace Artemis.Core.Plugins.Abstract
{
public abstract class ModuleViewModel : Screen, IModuleViewModel
{
public PluginInfo PluginInfo { get; set; }
}
}
@@ -0,0 +1,37 @@
using System;
using Artemis.Core.Plugins.Interfaces;
namespace Artemis.Core.Plugins.Abstract
{
public abstract class ProfileModule : IModule
{
/// <inheritdoc />
public abstract Type ViewModelType { get; }
/// <inheritdoc />
public abstract bool ExpandsMainDataModel { get; }
/// <inheritdoc />
public void LoadPlugin()
{
// Load and activate the last active profile
}
/// <inheritdoc />
public virtual void Update(double deltaTime)
{
// Update the profile
}
/// <inheritdoc />
public virtual void Render(double deltaTime)
{
// Render the profile
}
/// <inheritdoc />
public void Dispose()
{
}
}
}
@@ -0,0 +1,25 @@
using System;
using Artemis.Core.Plugins.Models;
namespace Artemis.Core.Plugins.Exceptions
{
public class ArtemisPluginException : Exception
{
public ArtemisPluginException(PluginInfo pluginInfo)
{
PluginInfo = pluginInfo;
}
public ArtemisPluginException(PluginInfo pluginInfo, string message) : base(message)
{
PluginInfo = pluginInfo;
}
public ArtemisPluginException(PluginInfo pluginInfo, string message, Exception inner) : base(message, inner)
{
PluginInfo = pluginInfo;
}
public PluginInfo PluginInfo { get; }
}
}
@@ -0,0 +1,11 @@
namespace Artemis.Core.Plugins.Interfaces
{
/// <inheritdoc />
/// <summary>
/// Allows you to expand the application-wide datamodel
/// </summary>
public interface IDataModelExpansion : IPlugin
{
void Update(double deltaTime);
}
}
@@ -0,0 +1,10 @@
namespace Artemis.Core.Plugins.Interfaces
{
/// <inheritdoc />
/// <summary>
/// Allows you to implement your own RGB device
/// </summary>
public interface IDevice : IPlugin
{
}
}
@@ -0,0 +1,25 @@
using Artemis.Core.ProfileElements;
using RGB.NET.Core;
namespace Artemis.Core.Plugins.Interfaces
{
/// <inheritdoc />
/// <summary>
/// Allows you to create your own layer type
/// </summary>
public interface ILayerType : IPlugin
{
/// <summary>
/// Updates the layer type
/// </summary>
/// <param name="layer"></param>
void Update(Layer layer);
/// <summary>
/// Renders the layer type
/// </summary>
void Render(Layer device, IRGBDevice rgbDevice);
ILayerType ApplyToLayer(Layer layer);
}
}
@@ -0,0 +1,34 @@
using System;
namespace Artemis.Core.Plugins.Interfaces
{
/// <inheritdoc />
/// <summary>
/// Allows you to add support for new games/applications
/// </summary>
public interface IModule : IPlugin
{
/// <summary>
/// The type of this module's view model
/// </summary>
Type ViewModelType { get; }
/// <summary>
/// Wether or not this module expands upon the main data model. If set to true any data in main data model can be
/// accessed by profiles in this module
/// </summary>
bool ExpandsMainDataModel { get; }
/// <summary>
/// Called each frame when the module must update
/// </summary>
/// <param name="deltaTime">Time since the last update</param>
void Update(double deltaTime);
/// <summary>
/// Called each frame when the module must render
/// </summary>
/// <param name="deltaTime">Time since the last render</param>
void Render(double deltaTime);
}
}
@@ -0,0 +1,6 @@
namespace Artemis.Core.Plugins.Interfaces
{
public interface IModuleDataModel
{
}
}
@@ -0,0 +1,14 @@
using Artemis.Core.Plugins.Models;
using Stylet;
namespace Artemis.Core.Plugins.Interfaces
{
/// <inheritdoc />
/// <summary>
/// Allows you to create a view model for a module
/// </summary>
public interface IModuleViewModel : IScreen
{
PluginInfo PluginInfo { get; set; }
}
}
@@ -0,0 +1,15 @@
using System;
namespace Artemis.Core.Plugins.Interfaces
{
/// <summary>
/// This is the base plugin type, use the other interfaces such as IModule to create plugins
/// </summary>
public interface IPlugin : IDisposable
{
/// <summary>
/// Called when the plugin is loaded
/// </summary>
void LoadPlugin();
}
}
@@ -0,0 +1,127 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Plugins.Interfaces;
using CSScriptLibrary;
using Newtonsoft.Json;
using Ninject;
namespace Artemis.Core.Plugins.Models
{
public class PluginInfo
{
private static Assembly _assembly;
/// <summary>
/// The plugin's GUID
/// </summary>
public Guid Guid { get; set; }
/// <summary>
/// The name of the plugin
/// </summary>
public string Name { get; set; }
/// <summary>
/// The version of the plugin
/// </summary>
public string Version { get; set; }
/// <summary>
/// The file implementing IPlugin, loaded on startup
/// </summary>
public string Main { get; set; }
/// <summary>
/// The instantiated plugin, available after successful load
/// </summary>
[JsonIgnore]
public IPlugin Plugin { get; set; }
/// <summary>
/// Full path to the plugin's current folder
/// </summary>
[JsonIgnore]
public string Folder { get; set; }
/// <summary>
/// Unloads the plugin and clears the plugin info's internal data
/// </summary>
public void UnloadPlugin()
{
Plugin.Dispose();
_assembly = null;
}
/// <summary>
/// Load a plugin from a folder
/// </summary>
/// <param name="kernel">The Ninject kernel to use for DI</param>
/// <param name="folder">The folder in which plugin.json is located</param>
/// <returns></returns>
public static async Task<PluginInfo> FromFolder(IKernel kernel, string folder)
{
// Make sure the right engine is used
CSScript.EvaluatorConfig.Engine = EvaluatorEngine.CodeDom;
CSScript.EvaluatorConfig.DebugBuild = true;
CSScript.GlobalSettings.SearchDirs = 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;
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
_assembly = await CSScript.Evaluator.CompileCodeAsync(File.ReadAllText(Folder + Main));
var pluginType = _assembly.GetTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t)).ToList();
if (!pluginType.Any())
throw new ArtemisPluginException(this, "Failed to load plugin, no type found that implements IPlugin");
if (pluginType.Count > 1)
throw new ArtemisPluginException(this, "Failed to load plugin, more than one type found that implements IPlugin");
// Instantiate the plugin with Ninject
Plugin = (IPlugin) kernel.Get(pluginType.First());
Plugin.LoadPlugin();
}
/// <summary>
/// Gets the view model of the module accompanying the provided plugin info
/// </summary>
/// <param name="kernel">The Ninject kernel to use for DI</param>
/// <returns></returns>
public IModuleViewModel GetModuleViewModel(IKernel kernel)
{
// Don't attempt to locave VMs for something other than a module
if (Plugin == null)
throw new ArtemisPluginException(this, "Cannot locate a view model for this plugin because it's not compiled.");
if (!(Plugin is IModule module))
throw new ArtemisPluginException(this, "Cannot locate a view model for this plugin as it's not a module.");
// Get the type from the module
var vmType = module.ViewModelType;
if (!typeof(IModuleViewModel).IsAssignableFrom(vmType))
throw new ArtemisPluginException(this, "ViewModel must implement IModuleViewModel.");
// Instantiate the ViewModel with Ninject
var vm = (IModuleViewModel) kernel.Get(vmType);
vm.PluginInfo = this;
return vm;
}
}
}