using Discord.WebSocket; using PluginManager.Interfaces; using PluginManager.Others; using System; using System.Collections.Generic; namespace PluginManager.Loaders { public class PluginLoader { private 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? Plugins { 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() { Plugins = new List(); Events = new List(); Functions.WriteLogFile("Starting plugin loader..."); if (LanguageSystem.Language.ActiveLanguage != null) Console_Utilities.WriteColorText( LanguageSystem.Language.ActiveLanguage.FormatText( LanguageSystem.Language.ActiveLanguage.LanguageWords["PLUGIN_LOADING_START"] ) ); //Load commands CommandsLoader CMDLoader = new CommandsLoader(pluginCMDFolder, pluginCMDExtension); CMDLoader.OnCommandLoaded += OnCommandLoaded!; CMDLoader.OnCommandFileLoaded += OnCommandFileLoaded; Plugins = CMDLoader.LoadCommands(); //Load Events EventsLoader EVLoader = new EventsLoader(pluginEVEFolder, pluginEVEExtension); EVLoader.EventLoad += OnEventLoaded!; EVLoader.EventFileLoaded += EventFileLoaded; Events = EVLoader.LoadEvents(); } private void EventFileLoaded(string path) { if (path != null) Functions.WriteLogFile($"[EVENT] Event from file [{path}] has been successfully created !"); } private void OnCommandFileLoaded(string path) { if (path != null) Functions.WriteLogFile($"[CMD] Command from file [{path}] has been successfully loaded !"); } private void OnEventLoaded(string typename, bool success, DBEvent eve, Exception exception) { if (eve != null && success) eve.Start(client); if (onEVELoad != null) onEVELoad.Invoke(eve!.name, typename, success, exception); } private void OnCommandLoaded(string name, bool success, DBCommand command, Exception exception) { if (onCMDLoad != null) onCMDLoad.Invoke(command.Command, name, success, exception); } } }