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

Commit

Permalink
Load third-party libraries before loading plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Mar 11, 2022
1 parent ed45179 commit c81ac32
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions BrackeysBot/BrackeysBotApp.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Reflection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using BrackeysBot.API;
Expand All @@ -14,6 +17,8 @@ namespace BrackeysBot;
/// </summary>
internal sealed class BrackeysBotApp : BackgroundService, IBot
{
private readonly List<Assembly> _libraries = new();

/// <summary>
/// Initializes a new instance of the <see cref="BrackeysBotApp" /> class.
/// </summary>
Expand All @@ -23,6 +28,12 @@ public BrackeysBotApp()
Version = assembly.GetName().Version!.ToString(3);
}

/// <summary>
/// Gets the <c>libraries</c> directory for this bot.
/// </summary>
/// <value>The <c>libraries</c> directory.</value>
public DirectoryInfo LibrariesDirectory { get; } = new("libraries");

/// <inheritdoc />
public ILogger Logger { get; } = LogManager.GetLogger("BrackeysBot");

Expand Down Expand Up @@ -50,17 +61,52 @@ public void EnablePlugins()
PluginManager.EnablePlugin(plugin);
}

/// <summary>
/// Loads third party dependencies as found <see cref="LibrariesDirectory" />.
/// </summary>
public void LoadLibraries()
{
foreach (FileInfo file in LibrariesDirectory.EnumerateFiles("*.dll"))
{
try
{
Assembly assembly = Assembly.LoadFrom(file.FullName);
if (!_libraries.Contains(assembly))
_libraries.Add(assembly);
Logger.Debug($"Loaded {assembly.GetName()}");
}
catch (Exception exception)
{
Logger.Warn(exception, $"Could not load library '{file.Name}' - SOME PLUGINS MAY NOT WORK");
}
}
}

/// <inheritdoc />
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
Logger.Info($"Starting Brackeys Bot version {Version}");

LoadLibraries();
int libraryCount = _libraries.Count;
Logger.Info($"Loaded {libraryCount} {(libraryCount == 1 ? "library" : "libraries")}");

PluginManager.LoadPlugins();
Logger.Info($"Loaded {PluginManager.LoadedPlugins.Count} plugins");
int loadedPluginCount = PluginManager.LoadedPlugins.Count;
Logger.Info($"Loaded {loadedPluginCount} {(loadedPluginCount == 1 ? "plugin" : "plugins")}");

EnablePlugins();
Logger.Info($"Enabled {PluginManager.EnabledPlugins.Count} plugins");
int enabledPluginCount = PluginManager.EnabledPlugins.Count;
Logger.Info($"Enabled {enabledPluginCount} {(enabledPluginCount == 1 ? "plugin" : "plugins")}");

return Task.CompletedTask;
}

public override Task StopAsync(CancellationToken cancellationToken)
{
DisablePlugins();
foreach (Plugin plugin in PluginManager.LoadedPlugins) PluginManager.UnloadPlugin(plugin);

return base.StopAsync(cancellationToken);
}
}

0 comments on commit c81ac32

Please sign in to comment.