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

Commit

Permalink
Unload plugin dependants
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Mar 28, 2022
1 parent 80b9a83 commit 2bf6281
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion BrackeysBot.API
20 changes: 18 additions & 2 deletions BrackeysBot/Plugins/SimplePluginManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
Expand Down Expand Up @@ -203,17 +203,19 @@ public IPlugin LoadPlugin(string name)
var descriptionAttribute = pluginType.GetCustomAttribute<PluginDescriptionAttribute>();
string? description = descriptionAttribute?.Description;

IEnumerable<IPlugin> dependencies = EnumeratePluginDependencies(pluginType);
IPlugin[] dependencies = EnumeratePluginDependencies(pluginType).ToArray();
PluginInfo.PluginAuthorInfo? authorInfo = GetPluginAuthorInfo(pluginType);
var pluginInfo = new PluginInfo(name, version, description, authorInfo, dependencies.Select(d => d.PluginInfo).ToArray());
if (Activator.CreateInstance(pluginType) is not MonoPlugin instance)
throw new InvalidPluginException(name, ExceptionMessages.NoDerivationOfPluginClass);

instance.Dependencies = dependencies.ToArray(); // defensive copy
instance.LoadContext = context;
instance.PluginInfo = pluginInfo;
instance.PluginManager = this;
instance.Logger = LogManager.GetLogger(pluginInfo.Name);

UpdatePluginDependants(instance);
SetupPluginDataDirectory(pluginInfo, instance);
SetupPluginConfiguration(instance);
SetupPluginServices(instance, pluginInfo, pluginType);
Expand Down Expand Up @@ -302,6 +304,9 @@ public void UnloadPlugin(IPlugin plugin)

DisablePlugin(plugin);

foreach (IPlugin dependant in plugin.Dependants)
UnloadPlugin(dependant);

try
{
monoPlugin.OnUnload().GetAwaiter().GetResult();
Expand Down Expand Up @@ -512,4 +517,15 @@ private void SetupPluginServices(MonoPlugin instance, PluginInfo pluginInfo, Typ
instance.ConfigureServices(serviceCollection);
instance.ServiceProvider = serviceCollection.BuildServiceProvider();
}

private static void UpdatePluginDependants(IPlugin instance)
{
foreach (MonoPlugin dependency in instance.Dependencies.OfType<MonoPlugin>())
{
if (dependency.Dependants.Contains(instance)) continue;

var dependants = new List<IPlugin>(dependency.Dependants) {instance};
dependency.Dependants = dependants.ToArray();
}
}
}

0 comments on commit 2bf6281

Please sign in to comment.