This commit is contained in:
2022-06-03 22:28:22 +03:00
parent 3e01e75de3
commit ab6f14e74c
21 changed files with 358 additions and 264 deletions

View File

@@ -1,32 +1,35 @@
using Discord.WebSocket;
using PluginManager.Interfaces;
using PluginManager.Others;
using System;
using System.Collections.Generic;
namespace PluginManager.Loaders
{
public class PluginLoader
{
private readonly DiscordSocketClient _client;
private DiscordSocketClient client;
/// <summary>
/// The Plugin Loader constructor
/// </summary>
/// <param name="discordSocketClient">The discord bot client where the plugins will pe attached to</param>
public PluginLoader(DiscordSocketClient discordSocketClient) { this._client = discordSocketClient; }
public PluginLoader(DiscordSocketClient discordSocketClient)
{
this.client = discordSocketClient;
}
private const string pluginCMDFolder = @"./Data/Plugins/Commands/";
private const string pluginEVEFolder = @"./Data/Plugins/Events/";
private const string pluginCMDExtension = "dll";
private const string pluginEVEExtension = "dll";
private const string pluginCMDExtension = ".dll";
private const string pluginEVEExtension = ".dll";
/// <summary>
/// A list of <see cref="DBCommand"/> commands
/// </summary>
public static List<DBCommand>? Commands { get; set; }
public static List<DBCommand>? Plugins { get; set; }
/// <summary>
/// A list of <see cref="DBEvent"/> commands
@@ -35,7 +38,6 @@ namespace PluginManager.Loaders
public delegate void CMDLoaded(string name, string typeName, bool success, Exception? e = null);
public delegate void EVELoaded(string name, string typeName, bool success, Exception? e = null);
/// <summary>
@@ -53,10 +55,11 @@ namespace PluginManager.Loaders
/// </summary>
public void LoadPlugins()
{
Commands = new List<DBCommand>();
Events = new List<DBEvent>();
Functions.WriteLogFile("Starting plugin loader ... Client: " + _client.CurrentUser.Username);
Plugins = new List<DBCommand>();
Events = new List<DBEvent>();
Functions.WriteLogFile("Starting plugin loader ... Client: " + client.CurrentUser.Username);
if (LanguageSystem.Language.ActiveLanguage != null)
Console_Utilities.WriteColorText(
LanguageSystem.Language.ActiveLanguage.FormatText(
@@ -64,40 +67,45 @@ namespace PluginManager.Loaders
)
);
//Load commands
CommandsLoader CMDLoader = new CommandsLoader(pluginCMDFolder, pluginCMDExtension);
CMDLoader.OnCommandLoaded += OnCommandLoaded!;
CMDLoader.OnCommandFileLoaded += OnCommandFileLoaded;
Plugins = CMDLoader.LoadCommands();
Loader<DBCommand> commandsLoader = new Loader<DBCommand>(pluginCMDFolder, pluginCMDExtension);
Loader<DBEvent> eventsLoader = new Loader<DBEvent>(pluginEVEFolder, pluginEVEExtension);
commandsLoader.FileLoaded += OnCommandFileLoaded;
commandsLoader.PluginLoaded += OnCommandLoaded;
//Load Events
EventsLoader EVLoader = new EventsLoader(pluginEVEFolder, pluginEVEExtension);
EVLoader.EventLoad += OnEventLoaded!;
EVLoader.EventFileLoaded += EventFileLoaded;
Events = EVLoader.LoadEvents();
eventsLoader.FileLoaded += EventFileLoaded;
eventsLoader.PluginLoaded += OnEventLoaded;
Commands = commandsLoader.Load();
Events = eventsLoader.Load();
}
private void EventFileLoaded(LoaderArgs e)
private void EventFileLoaded(string path)
{
if (e.IsLoaded) Functions.WriteLogFile($"[EVENT] Event from file [{e.PluginName}] has been successfully created !");
if (path != null)
Functions.WriteLogFile($"[EVENT] Event from file [{path}] has been successfully created !");
}
private void OnCommandFileLoaded(LoaderArgs e)
private void OnCommandFileLoaded(string path)
{
if (e.IsLoaded) Functions.WriteLogFile($"[CMD] Command from file [{e.PluginName}] has been successfully loaded !");
if (path != null)
Functions.WriteLogFile($"[CMD] Command from file [{path}] has been successfully loaded !");
}
private void OnEventLoaded(LoaderArgs e)
private void OnEventLoaded(string typename, bool success, DBEvent eve, Exception exception)
{
if (e.IsLoaded) { ((DBEvent)e.Plugin!).Start(_client); }
if (onEVELoad != null) onEVELoad.Invoke(((DBEvent)e.Plugin!).name, e.TypeName!, e.IsLoaded, e.Exception);
if (eve != null && success)
eve.Start(client);
if (onEVELoad != null)
onEVELoad.Invoke(eve!.name, typename, success, exception);
}
private void OnCommandLoaded(LoaderArgs e)
private void OnCommandLoaded(string name, bool success, DBCommand command, Exception exception)
{
if (onCMDLoad != null) onCMDLoad.Invoke(((DBCommand)e.Plugin!).Command, e.TypeName!, e.IsLoaded, e.Exception);
if (onCMDLoad != null)
onCMDLoad.Invoke(command.Command, name, success, exception);
}
}
}