Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit 9855e63

Browse files
committed
Extract Plugin members to IPlugin interface
Plugin renamed to MonoPlugin, whereas IPluginManager now exposes methods which accept and return IPlugin. This opens up the door for other possible plugin implementation systems, like Java or Lua. It's unlikely these will ever actually exist... but portability is good. Yes, I like to over-engineer code. Don't @ me
1 parent f45c3bc commit 9855e63

10 files changed

Lines changed: 121 additions & 83 deletions

BrackeysBot.API/Exceptions/PluginNotLoadedException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace BrackeysBot.API.Exceptions;
88
/// </summary>
99
public sealed class PluginNotLoadedException : Exception
1010
{
11-
internal PluginNotLoadedException(Plugin plugin)
11+
internal PluginNotLoadedException(IPlugin plugin)
1212
: base($"{plugin} is not loaded.")
1313
{
1414
Plugin = plugin;
@@ -18,5 +18,5 @@ internal PluginNotLoadedException(Plugin plugin)
1818
/// Gets the plugin.
1919
/// </summary>
2020
/// <value>The plugin.</value>
21-
public Plugin Plugin { get; }
21+
public IPlugin Plugin { get; }
2222
}

BrackeysBot.API/Plugins/IPlugin.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.IO;
3+
using BrackeysBot.API.Configuration;
4+
using NLog;
5+
6+
namespace BrackeysBot.API.Plugins;
7+
8+
/// <summary>
9+
/// Represents a bot plugin.
10+
/// </summary>
11+
public interface IPlugin : IDisposable, IConfigurationHolder
12+
{
13+
/// <summary>
14+
/// Gets the data directory for this plugin.
15+
/// </summary>
16+
/// <value>The data directory.</value>
17+
DirectoryInfo DataDirectory { get; }
18+
19+
/// <summary>
20+
/// Gets the logger for this plugin.
21+
/// </summary>
22+
/// <value>The plugin's logger.</value>
23+
ILogger Logger { get; }
24+
25+
/// <summary>
26+
/// Gets the information about this plugin.
27+
/// </summary>
28+
/// <value>A <see cref="BrackeysBot.API.Plugins.PluginInfo" /> object containing</value>
29+
PluginInfo PluginInfo { get; }
30+
31+
/// <summary>
32+
/// Gets the manager which owns this plugin.
33+
/// </summary>
34+
/// <value>The plugin manager.</value>
35+
IPluginManager PluginManager { get; }
36+
37+
/// <summary>
38+
/// Gets the service provider for this plugin.
39+
/// </summary>
40+
/// <value>The service provider.</value>
41+
public IServiceProvider ServiceProvider { get; }
42+
}

BrackeysBot.API/Plugins/IPluginManager.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ namespace BrackeysBot.API.Plugins;
1111
public interface IPluginManager
1212
{
1313
/// <summary>
14-
/// Gets a read-only view of the plugins enabled by this bot.
14+
/// Gets a read-only view of the plugins enabled by this manager.
1515
/// </summary>
16-
/// <value>A read-only view of <see cref="Plugin" /> instances.</value>
17-
IReadOnlyList<Plugin> EnabledPlugins { get; }
16+
/// <value>A read-only view of <see cref="IPlugin" /> instances which are currently enabled.</value>
17+
IReadOnlyList<IPlugin> EnabledPlugins { get; }
1818

1919
/// <summary>
20-
/// Gets a read-only view of the plugins loaded by this bot.
20+
/// Gets a read-only view of the plugins loaded by this manager.
2121
/// </summary>
22-
/// <value>A read-only view of <see cref="Plugin" /> instances.</value>
23-
IReadOnlyList<Plugin> LoadedPlugins { get; }
22+
/// <value>A read-only view of <see cref="IPlugin" /> instances which are currently loaded.</value>
23+
IReadOnlyList<IPlugin> LoadedPlugins { get; }
2424

2525
/// <summary>
2626
/// Gets the logger for this plugin manager.
@@ -34,24 +34,24 @@ public interface IPluginManager
3434
/// <param name="plugin">The plugin to disable.</param>
3535
/// <exception cref="ArgumentNullException"><paramref name="plugin" /> is <see langword="null" />.</exception>
3636
/// <exception cref="PluginNotLoadedException"><paramref name="plugin" /> refers to a plugin that is not loaded.</exception>
37-
void DisablePlugin(Plugin plugin);
37+
void DisablePlugin(IPlugin plugin);
3838

3939
/// <summary>
4040
/// Enables a plugin.
4141
/// </summary>
4242
/// <param name="plugin">The plugin to enable.</param>
4343
/// <exception cref="ArgumentNullException"><paramref name="plugin" /> is <see langword="null" />.</exception>
4444
/// <exception cref="PluginNotLoadedException"><paramref name="plugin" /> refers to a plugin that is not loaded.</exception>
45-
void EnablePlugin(Plugin plugin);
45+
void EnablePlugin(IPlugin plugin);
4646

4747
/// <summary>
4848
/// Attempts to find a plugin by its type.
4949
/// </summary>
5050
/// <typeparam name="T">The plugin type.</typeparam>
5151
/// <returns>
52-
/// The plugin, or <see langword="null" /> if the plugin with the specified type was not found, or is not loaded.
52+
/// The plugin, or <see langword="default" /> if the plugin with the specified type was not found, or is not loaded.
5353
/// </returns>
54-
T? GetPlugin<T>() where T : Plugin;
54+
T? GetPlugin<T>() where T : IPlugin;
5555

5656
/// <summary>
5757
/// Attempts to find a plugin by its name.
@@ -60,33 +60,33 @@ public interface IPluginManager
6060
/// <returns>
6161
/// The plugin, or <see langword="null" /> if the plugin with the specified name was not found, or is not loaded.
6262
/// </returns>
63-
Plugin? GetPlugin(string name);
63+
IPlugin? GetPlugin(string name);
6464

6565
/// <summary>
6666
/// Loads a plugin with a specified name.
6767
/// </summary>
6868
/// <param name="name">The name of the plugin to load, sans the <c>.dll</c> extension.</param>
69-
/// <returns>The newly loaded <see cref="Plugin" />.</returns>
69+
/// <returns>The newly loaded plugin.</returns>
7070
/// <exception cref="ArgumentNullException">
7171
/// <paramref name="name" /> is <see langword="null" />, empty, or consists of only whitespace characters.
7272
/// </exception>
7373
/// <exception cref="PluginNotFoundException">No plugin by the name <paramref name="name" /> could be found.</exception>
7474
/// <exception cref="InvalidPluginException">
7575
/// The plugin does not contain an embedded resource named <c>plugin.json</c>.
7676
/// </exception>
77-
Plugin LoadPlugin(string name);
77+
IPlugin LoadPlugin(string name);
7878

7979
/// <summary>
8080
/// Loads all plugins that this plugin manager can detect.
8181
/// </summary>
82-
/// <returns>The read-only view of the loaded <see cref="Plugin" /> instances.</returns>
83-
IReadOnlyList<Plugin> LoadPlugins();
82+
/// <returns>The read-only view of the loaded <see cref="MonoPlugin" /> instances.</returns>
83+
IReadOnlyList<IPlugin> LoadPlugins();
8484

8585
/// <summary>
8686
/// Unloads a plugin.
8787
/// </summary>
8888
/// <param name="plugin">The plugin to unload.</param>
8989
/// <exception cref="ArgumentNullException"><paramref name="plugin" /> is <see langword="null" />.</exception>
9090
/// <exception cref="PluginNotLoadedException"><paramref name="plugin" /> refers to a plugin that is not loaded.</exception>
91-
void UnloadPlugin(Plugin plugin);
91+
void UnloadPlugin(IPlugin plugin);
9292
}
Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,32 @@ namespace BrackeysBot.API.Plugins;
1111
/// <summary>
1212
/// Represents a bot plugin.
1313
/// </summary>
14-
public abstract class Plugin : IDisposable, IConfigurationHolder
14+
public abstract class MonoPlugin : IPlugin
1515
{
16-
/// <summary>
17-
/// Gets the data directory for this plugin.
18-
/// </summary>
19-
/// <value>The data directory.</value>
16+
/// <inheritdoc />
17+
public IConfiguration Configuration { get; internal set; } = null!;
18+
19+
/// <inheritdoc />
2020
public DirectoryInfo DataDirectory { get; internal set; } = null!;
2121

22-
/// <summary>
23-
/// Gets the logger for this plugin.
24-
/// </summary>
25-
/// <value>The plugin's logger.</value>
22+
/// <inheritdoc />
2623
public ILogger Logger { get; internal set; } = null!;
2724

28-
/// <summary>
29-
/// Gets the information about this plugin.
30-
/// </summary>
31-
/// <value>A <see cref="BrackeysBot.API.Plugins.PluginInfo" /> object containing</value>
25+
/// <inheritdoc />
3226
public PluginInfo PluginInfo { get; internal set; } = null!;
3327

34-
/// <summary>
35-
/// Gets the manager which owns this plugin.
36-
/// </summary>
37-
/// <value>The plugin manager.</value>
28+
/// <inheritdoc />
3829
public IPluginManager PluginManager { get; internal set; } = null!;
3930

31+
/// <inheritdoc />
32+
public IServiceProvider ServiceProvider { get; internal set; } = null!;
33+
4034
/// <summary>
4135
/// Gets the underlying <see cref="DisCatSharp.DiscordClient" /> instance.
4236
/// </summary>
4337
/// <value>The underlying <see cref="DisCatSharp.DiscordClient" />.</value>
4438
protected internal DiscordClient? DiscordClient { get; internal set; }
4539

46-
/// <summary>
47-
/// Gets the service provider for this plugin.
48-
/// </summary>
49-
/// <value>The service provider.</value>
50-
public IServiceProvider ServiceProvider { get; internal set; } = null!;
51-
52-
/// <inheritdoc />
53-
public IConfiguration Configuration { get; internal set; } = null!;
54-
5540
/// <inheritdoc />
5641
public virtual void Dispose()
5742
{

BrackeysBot.API/Plugins/PluginAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace BrackeysBot.API.Plugins;
44

55
/// <summary>
6-
/// Specifies <see cref="Plugin" /> information such as the plugin's name and version.
6+
/// Specifies <see cref="MonoPlugin" /> information such as the plugin's name and version.
77
/// </summary>
88
[AttributeUsage(AttributeTargets.Class)]
99
public sealed class PluginAttribute : Attribute

BrackeysBot.API/Plugins/PluginAuthorAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace BrackeysBot.API.Plugins;
44

55
/// <summary>
6-
/// Specifies the author of a <see cref="Plugin" />.
6+
/// Specifies the author of a <see cref="MonoPlugin" />.
77
/// </summary>
88
[AttributeUsage(AttributeTargets.Class)]
99
public sealed class PluginAuthorAttribute : Attribute

BrackeysBot.API/Plugins/PluginDependenciesAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace BrackeysBot.API.Plugins;
44

55
/// <summary>
6-
/// Specifies the dependencies that should be loaded prior to this <see cref="Plugin" />, so that this plugin functions
6+
/// Specifies the dependencies that should be loaded prior to this <see cref="MonoPlugin" />, so that this plugin functions
77
/// correctly.
88
/// </summary>
99
[AttributeUsage(AttributeTargets.Class)]

BrackeysBot.API/Plugins/PluginDescriptionAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace BrackeysBot.API.Plugins;
44

55
/// <summary>
6-
/// Specifies the description of a <see cref="Plugin" />.
6+
/// Specifies the description of a <see cref="MonoPlugin" />.
77
/// </summary>
88
[AttributeUsage(AttributeTargets.Class)]
99
public sealed class PluginDescriptionAttribute : Attribute

BrackeysBot/BrackeysBotApp.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public BrackeysBotApp()
4848
/// </summary>
4949
public void DisablePlugins()
5050
{
51-
foreach (Plugin plugin in PluginManager.EnabledPlugins)
51+
foreach (IPlugin plugin in PluginManager.EnabledPlugins)
5252
PluginManager.DisablePlugin(plugin);
5353
}
5454

@@ -57,7 +57,7 @@ public void DisablePlugins()
5757
/// </summary>
5858
public void EnablePlugins()
5959
{
60-
foreach (Plugin plugin in PluginManager.LoadedPlugins)
60+
foreach (IPlugin plugin in PluginManager.LoadedPlugins)
6161
PluginManager.EnablePlugin(plugin);
6262
}
6363

@@ -107,7 +107,9 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)
107107
public override Task StopAsync(CancellationToken cancellationToken)
108108
{
109109
DisablePlugins();
110-
foreach (Plugin plugin in PluginManager.LoadedPlugins) PluginManager.UnloadPlugin(plugin);
110+
111+
foreach (IPlugin plugin in PluginManager.LoadedPlugins)
112+
PluginManager.UnloadPlugin(plugin);
111113

112114
return base.StopAsync(cancellationToken);
113115
}

0 commit comments

Comments
 (0)