Updated performance in plugin loading

This commit is contained in:
2024-08-18 14:32:13 +03:00
parent 95e8d95c92
commit c080074292
16 changed files with 463 additions and 244 deletions

View File

@@ -1,10 +1,9 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using DiscordBotCore.Interfaces;
using DiscordBotCore.Others;
using DiscordBotCore.Others.Exceptions;
namespace DiscordBotCore.Loaders;
@@ -13,7 +12,7 @@ internal class Loader
internal delegate void FileLoadedHandler(FileLoaderResult result);
internal delegate void PluginLoadedHandler(PluginLoadResultData result);
internal delegate void PluginLoadedHandler(PluginLoaderResult result);
internal event FileLoadedHandler? OnFileLoadedException;
internal event PluginLoadedHandler? OnPluginLoaded;
@@ -21,8 +20,8 @@ internal class Loader
internal async Task Load()
{
var installedPlugins = await Application.CurrentApplication.PluginManager.GetInstalledPlugins();
var files = installedPlugins.Where(plugin=>plugin.IsEnabled).Select(plugin => plugin.FilePath).ToArray();
var files = installedPlugins.Where(plugin => plugin.IsEnabled).Select(plugin => plugin.FilePath).ToArray();
foreach (var file in files)
{
try
@@ -41,7 +40,7 @@ internal class Loader
await LoadEverythingOfType<ICommandAction>();
}
private async Task LoadEverythingOfType<T>()
private Task LoadEverythingOfType<T>()
{
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
@@ -58,25 +57,24 @@ internal class Loader
throw new Exception($"Failed to create instance of plugin with type {type.FullName} [{type.Assembly}]");
}
var pluginType = plugin switch
PluginLoaderResult result = plugin switch
{
IDbEvent => PluginType.EVENT,
IDbCommand => PluginType.COMMAND,
IDbSlashCommand => PluginType.SLASH_COMMAND,
ICommandAction => PluginType.ACTION,
_ => PluginType.UNKNOWN
IDbEvent @event => PluginLoaderResult.FromIDbEvent(@event),
IDbCommand command => PluginLoaderResult.FromIDbCommand(command),
IDbSlashCommand command => PluginLoaderResult.FromIDbSlashCommand(command),
ICommandAction action => PluginLoaderResult.FromICommandAction(action),
_ => PluginLoaderResult.FromException(new PluginNotFoundException($"Unknown plugin type {plugin.GetType().FullName}"))
};
if (pluginType == PluginType.UNKNOWN)
throw new Exception($"Unknown plugin type for plugin with type {type.FullName} [{type.Assembly}]");
OnPluginLoaded?.Invoke(new PluginLoadResultData(type.FullName, pluginType, true, plugin: plugin));
OnPluginLoaded?.Invoke(result);
}
catch (Exception ex)
{
OnPluginLoaded?.Invoke(new PluginLoadResultData(type.FullName, PluginType.UNKNOWN, false, ex.Message));
OnPluginLoaded?.Invoke(PluginLoaderResult.FromException(ex));
}
}
return Task.CompletedTask;
}
}