Updated performance in plugin loading
This commit is contained in:
@@ -1,25 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.WebSocket;
|
||||
using DiscordBotCore.Interfaces;
|
||||
using DiscordBotCore.Others;
|
||||
using DiscordBotCore.Others.Exceptions;
|
||||
|
||||
|
||||
namespace DiscordBotCore.Loaders;
|
||||
|
||||
public class PluginLoader
|
||||
public sealed class PluginLoader
|
||||
{
|
||||
internal readonly DiscordSocketClient _Client;
|
||||
|
||||
public delegate void CommandLoaded(PluginLoadResultData resultData);
|
||||
|
||||
public delegate void EventLoaded(PluginLoadResultData resultData);
|
||||
|
||||
public delegate void SlashCommandLoaded(PluginLoadResultData resultData);
|
||||
|
||||
public delegate void ActionLoaded(PluginLoadResultData resultData);
|
||||
private readonly DiscordSocketClient _Client;
|
||||
public delegate void CommandLoaded(IDbCommand eCommand);
|
||||
public delegate void EventLoaded(IDbEvent eEvent);
|
||||
public delegate void SlashCommandLoaded(IDbSlashCommand eSlashCommand);
|
||||
public delegate void ActionLoaded(ICommandAction eAction);
|
||||
|
||||
public CommandLoaded? OnCommandLoaded;
|
||||
public EventLoaded? OnEventLoaded;
|
||||
@@ -38,13 +34,6 @@ public class PluginLoader
|
||||
|
||||
public async Task LoadPlugins()
|
||||
{
|
||||
|
||||
if (_Client == null)
|
||||
{
|
||||
Application.Logger.Log("Discord client is null", this, LogType.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
Commands.Clear();
|
||||
Events.Clear();
|
||||
SlashCommands.Clear();
|
||||
@@ -65,48 +54,62 @@ public class PluginLoader
|
||||
Application.Logger.Log(result.ErrorMessage, this, LogType.Error);
|
||||
}
|
||||
|
||||
private async void OnPluginLoaded(PluginLoadResultData result)
|
||||
private async void InitializeCommand(ICommandAction action)
|
||||
{
|
||||
switch (result.PluginType)
|
||||
if (action.RunType == InternalActionRunType.OnStartup || action.RunType == InternalActionRunType.OnStartupAndCall)
|
||||
await Application.CurrentApplication.InternalActionManager.StartAction(action, null);
|
||||
|
||||
if(action.RunType == InternalActionRunType.OnCall || action.RunType == InternalActionRunType.OnStartupAndCall)
|
||||
Actions.Add(action);
|
||||
|
||||
OnActionLoaded?.Invoke(action);
|
||||
}
|
||||
|
||||
private void InitializeDbCommand(IDbCommand command)
|
||||
{
|
||||
Commands.Add(command);
|
||||
OnCommandLoaded?.Invoke(command);
|
||||
}
|
||||
|
||||
private void InitializeEvent(IDbEvent eEvent)
|
||||
{
|
||||
if (!eEvent.TryStartEvent())
|
||||
{
|
||||
case PluginType.ACTION:
|
||||
ICommandAction action = (ICommandAction)result.Plugin;
|
||||
if (action.RunType == InternalActionRunType.OnStartup || action.RunType == InternalActionRunType.OnStartupAndCall)
|
||||
await Application.CurrentApplication.InternalActionManager.StartAction(action, null);
|
||||
|
||||
if(action.RunType == InternalActionRunType.OnCall || action.RunType == InternalActionRunType.OnStartupAndCall)
|
||||
Actions.Add(action);
|
||||
|
||||
OnActionLoaded?.Invoke(result);
|
||||
|
||||
break;
|
||||
case PluginType.COMMAND:
|
||||
Commands.Add((IDbCommand)result.Plugin);
|
||||
OnCommandLoaded?.Invoke(result);
|
||||
break;
|
||||
case PluginType.EVENT:
|
||||
if (this.TryStartEvent((IDbEvent)result.Plugin))
|
||||
{
|
||||
Events.Add((IDbEvent)result.Plugin);
|
||||
OnEventLoaded?.Invoke(result);
|
||||
}
|
||||
|
||||
break;
|
||||
case PluginType.SLASH_COMMAND:
|
||||
if (await this.TryStartSlashCommand((IDbSlashCommand)result.Plugin))
|
||||
{
|
||||
if(((IDbSlashCommand)result.Plugin).HasInteraction)
|
||||
_Client.InteractionCreated += ((IDbSlashCommand)result.Plugin).ExecuteInteraction;
|
||||
SlashCommands.Add((IDbSlashCommand)result.Plugin);
|
||||
OnSlashCommandLoaded?.Invoke(result);
|
||||
}
|
||||
else
|
||||
Application.Logger.Log($"Failed to start slash command {result.PluginName}", this, LogType.Error);
|
||||
break;
|
||||
case PluginType.UNKNOWN:
|
||||
default:
|
||||
Application.Logger.Log("Unknown plugin type", this, LogType.Error);
|
||||
break;
|
||||
return;
|
||||
}
|
||||
|
||||
Events.Add(eEvent);
|
||||
OnEventLoaded?.Invoke(eEvent);
|
||||
}
|
||||
|
||||
private async void InitializeSlashCommand(IDbSlashCommand slashCommand)
|
||||
{
|
||||
Result result = await slashCommand.TryStartSlashCommand();
|
||||
result.Match(
|
||||
() =>
|
||||
{
|
||||
if (slashCommand.HasInteraction)
|
||||
_Client.InteractionCreated += slashCommand.ExecuteInteraction;
|
||||
SlashCommands.Add(slashCommand);
|
||||
OnSlashCommandLoaded?.Invoke(slashCommand);
|
||||
},
|
||||
HandleError
|
||||
);
|
||||
}
|
||||
|
||||
private void HandleError(Exception exception)
|
||||
{
|
||||
Application.Logger.Log(exception.Message, this, LogType.Error);
|
||||
}
|
||||
|
||||
private void OnPluginLoaded(PluginLoaderResult result)
|
||||
{
|
||||
result.Match(
|
||||
InitializeDbCommand,
|
||||
InitializeEvent,
|
||||
InitializeSlashCommand,
|
||||
InitializeCommand,
|
||||
HandleError
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user