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;
///
/// The Plugin Loader constructor
///
/// The discord bot client where the plugins will pe attached to
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";
///
/// A list of commands
///
public static List? Commands { get; set; }
///
/// A list of commands
///
public static List? Events { get; set; }
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);
///
/// Event that is fired when a is successfully loaded into commands list
///
public CMDLoaded? onCMDLoad;
///
/// Event that is fired when a is successfully loaded into events list
///
public EVELoaded? onEVELoad;
///
/// The main mathod that is called to load all events
///
public void LoadPlugins()
{
Commands = new List();
Events = new List();
Functions.WriteLogFile("Starting plugin loader ... Client: " + _client.CurrentUser.Username);
if (LanguageSystem.Language.ActiveLanguage != null)
Console_Utilities.WriteColorText(
LanguageSystem.Language.ActiveLanguage.FormatText(
LanguageSystem.Language.ActiveLanguage.LanguageWords["PLUGIN_LOADING_START"]
)
);
Loader commandsLoader = new Loader(pluginCMDFolder, pluginCMDExtension);
Loader eventsLoader = new Loader(pluginEVEFolder, pluginEVEExtension);
commandsLoader.FileLoaded += OnCommandFileLoaded;
commandsLoader.PluginLoaded += OnCommandLoaded;
eventsLoader.FileLoaded += EventFileLoaded;
eventsLoader.PluginLoaded += OnEventLoaded;
Commands = commandsLoader.Load();
Events = eventsLoader.Load();
}
private void EventFileLoaded(LoaderArgs e)
{
if (e.IsLoaded) Functions.WriteLogFile($"[EVENT] Event from file [{e.PluginName}] has been successfully created !");
}
private void OnCommandFileLoaded(LoaderArgs e)
{
if (e.IsLoaded) Functions.WriteLogFile($"[CMD] Command from file [{e.PluginName}] has been successfully loaded !");
}
private void OnEventLoaded(LoaderArgs e)
{
if (e.IsLoaded) { ((DBEvent)e.Plugin!).Start(_client); }
if (onEVELoad != null) onEVELoad.Invoke(((DBEvent)e.Plugin!).name, e.TypeName!, e.IsLoaded, e.Exception);
}
private void OnCommandLoaded(LoaderArgs e)
{
if (onCMDLoad != null) onCMDLoad.Invoke(((DBCommand)e.Plugin!).Command, e.TypeName!, e.IsLoaded, e.Exception);
}
}
}