Plugins - Reworked bootstrapper

This commit is contained in:
Robert committed 2021-04-30 17:39:58 +02:00
1 parent 5cae14efd3
commit 21700aaad5
13 files changed
+288 -135

No files matched your search

@@ -1,26 +0,0 @@
namespace Artemis.Core
{
/// <summary>
/// An optional entry point for your plugin
/// </summary>
public interface IPluginBootstrapper
{
/// <summary>
/// Called when the plugin is loaded
/// </summary>
/// <param name="plugin"></param>
void OnPluginLoaded(Plugin plugin);
/// <summary>
/// Called when the plugin is activated
/// </summary>
/// <param name="plugin">The plugin instance of your plugin</param>
void OnPluginEnabled(Plugin plugin);
/// <summary>
/// Called when the plugin is deactivated or when Artemis shuts down
/// </summary>
/// <param name="plugin">The plugin instance of your plugin</param>
void OnPluginDisabled(Plugin plugin);
}
}
+5 -4
View File
@@ -71,7 +71,7 @@ namespace Artemis.Core
/// <summary>
/// Gets the plugin bootstrapper
/// </summary>
public IPluginBootstrapper? Bootstrapper { get; internal set; }
public PluginBootstrapper? Bootstrapper { get; internal set; }
/// <summary>
/// The Ninject kernel of the plugin
@@ -118,10 +118,11 @@ namespace Artemis.Core
/// Looks up the feature info the feature of type <typeparamref name="T" />
/// </summary>
/// <typeparam name="T">The type of feature to find</typeparam>
/// <returns>If found, feature info of the feature</returns>
public PluginFeatureInfo? GetFeatureInfo<T>() where T : PluginFeature
/// <returns>Feature info of the feature</returns>
public PluginFeatureInfo GetFeatureInfo<T>() where T : PluginFeature
{
return _features.FirstOrDefault(i => i.FeatureType == typeof(T));
// This should be a safe assumption because any type of PluginFeature is registered and added
return _features.First(i => i.FeatureType == typeof(T));
}
/// <inheritdoc />
@@ -0,0 +1,100 @@
namespace Artemis.Core
{
/// <summary>
/// An optional entry point for your plugin
/// </summary>
public abstract class PluginBootstrapper
{
private Plugin? _plugin;
/// <summary>
/// Called when the plugin is loaded
/// </summary>
/// <param name="plugin"></param>
public virtual void OnPluginLoaded(Plugin plugin)
{
}
/// <summary>
/// Called when the plugin is activated
/// </summary>
/// <param name="plugin">The plugin instance of your plugin</param>
public virtual void OnPluginEnabled(Plugin plugin)
{
}
/// <summary>
/// Called when the plugin is deactivated or when Artemis shuts down
/// </summary>
/// <param name="plugin">The plugin instance of your plugin</param>
public virtual void OnPluginDisabled(Plugin plugin)
{
}
/// <summary>
/// Adds the provided prerequisite to the plugin.
/// </summary>
/// <param name="prerequisite">The prerequisite to add</param>
public void AddPluginPrerequisite(PluginPrerequisite prerequisite)
{
// TODO: We can keep track of them and add them after load, same goes for the others
if (_plugin == null)
throw new ArtemisPluginException("Cannot add plugin prerequisites before the plugin is loaded");
if (!_plugin.Info.Prerequisites.Contains(prerequisite))
_plugin.Info.Prerequisites.Add(prerequisite);
}
/// <summary>
/// Removes the provided prerequisite from the plugin.
/// </summary>
/// <param name="prerequisite">The prerequisite to remove</param>
/// <returns>
/// <see langword="true" /> is successfully removed; otherwise <see langword="false" />. This method also returns
/// <see langword="false" /> if the prerequisite was not found.
/// </returns>
public bool RemovePluginPrerequisite(PluginPrerequisite prerequisite)
{
if (_plugin == null)
throw new ArtemisPluginException("Cannot add plugin prerequisites before the plugin is loaded");
return _plugin.Info.Prerequisites.Remove(prerequisite);
}
/// <summary>
/// Adds the provided prerequisite to the feature of type <typeparamref name="T"/>.
/// </summary>
/// <param name="prerequisite">The prerequisite to add</param>
public void AddFeaturePrerequisite<T>(PluginPrerequisite prerequisite) where T : PluginFeature
{
if (_plugin == null)
throw new ArtemisPluginException("Cannot add feature prerequisites before the plugin is loaded");
PluginFeatureInfo info = _plugin.GetFeatureInfo<T>();
if (!info.Prerequisites.Contains(prerequisite))
info.Prerequisites.Add(prerequisite);
}
/// <summary>
/// Removes the provided prerequisite from the feature of type <typeparamref name="T"/>.
/// </summary>
/// <param name="prerequisite">The prerequisite to remove</param>
/// <returns>
/// <see langword="true" /> is successfully removed; otherwise <see langword="false" />. This method also returns
/// <see langword="false" /> if the prerequisite was not found.
/// </returns>
public bool RemoveFeaturePrerequisite<T>(PluginPrerequisite prerequisite) where T : PluginFeature
{
if (_plugin == null)
throw new ArtemisPluginException("Cannot add feature prerequisites before the plugin is loaded");
return _plugin.GetFeatureInfo<T>().Prerequisites.Remove(prerequisite);
}
internal void InternalOnPluginLoaded(Plugin plugin)
{
_plugin = plugin;
OnPluginLoaded(plugin);
}
}
}
+5 -12
View File
@@ -15,7 +15,7 @@ namespace Artemis.Core
/// Represents basic info about a plugin feature and contains a reference to the instance of said feature
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public class PluginFeatureInfo : CorePropertyChanged
public class PluginFeatureInfo : CorePropertyChanged, IPrerequisitesSubject
{
private string? _description;
private string? _icon;
@@ -31,7 +31,7 @@ namespace Artemis.Core
Description = attribute?.Description;
Icon = attribute?.Icon;
AlwaysEnabled = attribute?.AlwaysEnabled ?? false;
if (Icon != null) return;
if (typeof(BaseDataModelExpansion).IsAssignableFrom(featureType))
Icon = "TableAdd";
@@ -130,18 +130,11 @@ namespace Artemis.Core
internal set => SetAndNotify(ref _instance, value);
}
/// <summary>
/// Gets a list of prerequisites for this plugin feature
/// </summary>
/// <inheritdoc />
public List<PluginPrerequisite> Prerequisites { get; } = new();
/// <summary>
/// Determines whether the prerequisites of this feature are met
/// </summary>
public bool ArePrerequisitesMet()
{
return Prerequisites.All(p => p.IsMet());
}
/// <inheritdoc />
public bool ArePrerequisitesMet() => Prerequisites.All(p => p.IsMet());
/// <inheritdoc />
public override string ToString()
+4 -12
View File
@@ -10,7 +10,7 @@ namespace Artemis.Core
/// Represents basic info about a plugin and contains a reference to the instance of said plugin
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public class PluginInfo : CorePropertyChanged
public class PluginInfo : CorePropertyChanged, IPrerequisitesSubject
{
private bool _autoEnableFeatures = true;
private string? _description;
@@ -119,19 +119,11 @@ namespace Artemis.Core
internal set => SetAndNotify(ref _plugin, value);
}
/// <summary>
/// Gets a list of prerequisites for this plugin
/// </summary>
/// <inheritdoc />
public List<PluginPrerequisite> Prerequisites { get; } = new();
/// <summary>
/// Determines whether the prerequisites of this plugin are met
/// </summary>
public bool ArePrerequisitesMet()
{
return Prerequisites.All(p => p.IsMet());
}
/// <inheritdoc />
public bool ArePrerequisitesMet() => Prerequisites.All(p => p.IsMet());
/// <inheritdoc />
public override string ToString()
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Linq;
namespace Artemis.Core
{
/// <summary>
/// Represents a type that has prerequisites
/// </summary>
public interface IPrerequisitesSubject
{
/// <summary>
/// Gets a list of prerequisites for this plugin
/// </summary>
List<PluginPrerequisite> Prerequisites { get; }
/// <summary>
/// Determines whether the prerequisites of this plugin are met
/// </summary>
bool ArePrerequisitesMet();
}
}
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@@ -11,24 +12,6 @@ namespace Artemis.Core
{
private PluginPrerequisiteAction? _currentAction;
/// <summary>
/// Creates a new instance of the <see cref="PluginPrerequisite" /> class
/// </summary>
/// <param name="plugin">The plugin this is a prerequisite for</param>
protected PluginPrerequisite(Plugin plugin)
{
Plugin = plugin;
}
/// <summary>
/// Creates a new instance of the <see cref="PluginPrerequisite" /> class
/// </summary>
/// <param name="pluginFeature">The plugin feature this is a prerequisite for</param>
protected PluginPrerequisite(PluginFeature pluginFeature)
{
PluginFeature = pluginFeature;
}
/// <summary>
/// Gets the name of the prerequisite
/// </summary>
@@ -63,18 +46,6 @@ namespace Artemis.Core
private set => SetAndNotify(ref _currentAction, value);
}
/// <summary>
/// Gets or sets the plugin this prerequisite is for
/// <para>Note: Only one plugin or a plugin feature can be set at once</para>
/// </summary>
public Plugin? Plugin { get; }
/// <summary>
/// Gets or sets the feature this prerequisite is for
/// <para>Note: Only one plugin or a plugin feature can be set at once</para>
/// </summary>
public PluginFeature? PluginFeature { get; }
/// <summary>
/// Execute all install actions
/// </summary>