From 0b2f1e6ab6074fe9fb10f69c8a6f68a590a783d8 Mon Sep 17 00:00:00 2001 From: Wizzy69 Date: Wed, 7 Jun 2023 20:36:11 +0300 Subject: [PATCH] Reimplemented Command Actions. --- DiscordBot/Discord/Actions/Help.cs | 62 +++ DiscordBot/Discord/Actions/ListPlugins.cs | 49 ++ DiscordBot/Discord/Actions/LoadPlugins.cs | 94 ++++ DiscordBot/Program.cs | 143 +----- DiscordBot/Utilities/Command.cs | 14 - .../Utilities/ConsoleCommandsHandler.cs | 439 ------------------ .../Utilities/ConsoleCommandsHandler_OLD.cs | 435 +++++++++++++++++ PluginManager/Bot/Boot.cs | 3 + PluginManager/Config.cs | 11 +- PluginManager/Interfaces/IInternalAction.cs | 23 + PluginManager/Loaders/ActionsLoader.cs | 76 +++ PluginManager/Loaders/Loader.cs | 5 +- PluginManager/Loaders/PluginLoader.cs | 1 + PluginManager/Online/Helpers/VersionString.cs | 37 +- PluginManager/Others/Actions/ActionManager.cs | 51 -- .../Others/Actions/InternalAction.cs | 40 -- .../Others/Actions/InternalActionsManager.cs | 57 +++ PluginManager/Others/Enums.cs | 5 + PluginManager/Others/Logger/DBLogger.cs | 3 +- 19 files changed, 867 insertions(+), 681 deletions(-) create mode 100644 DiscordBot/Discord/Actions/Help.cs create mode 100644 DiscordBot/Discord/Actions/ListPlugins.cs create mode 100644 DiscordBot/Discord/Actions/LoadPlugins.cs delete mode 100644 DiscordBot/Utilities/Command.cs delete mode 100644 DiscordBot/Utilities/ConsoleCommandsHandler.cs create mode 100644 DiscordBot/Utilities/ConsoleCommandsHandler_OLD.cs create mode 100644 PluginManager/Interfaces/IInternalAction.cs create mode 100644 PluginManager/Loaders/ActionsLoader.cs delete mode 100644 PluginManager/Others/Actions/ActionManager.cs delete mode 100644 PluginManager/Others/Actions/InternalAction.cs create mode 100644 PluginManager/Others/Actions/InternalActionsManager.cs diff --git a/DiscordBot/Discord/Actions/Help.cs b/DiscordBot/Discord/Actions/Help.cs new file mode 100644 index 0000000..4420722 --- /dev/null +++ b/DiscordBot/Discord/Actions/Help.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using PluginManager.Others; +using PluginManager.Interfaces; + +namespace DiscordBot.Discord.Actions +{ + public class Help : ICommandAction + { + public string ActionName => "help"; + + public string Description => "Shows the list of commands and their usage"; + + public string Usage => "help [command]"; + + public InternalActionRunType RunType => InternalActionRunType.ON_CALL; + + public async Task Execute(string[] args) + { + if (args == null || args.Length == 0) + { + var items = new List + { + new[] { "-", "-", "-" }, + new[] { "Command", "Usage", "Description" }, + new[] { "-", "-", "-" } + }; + + foreach (var a in Program.internalActionManager.Actions) + { + items.Add(new[] { a.Key, a.Value.Usage, a.Value.Description }); + } + + items.Add(new[] { "-", "-", "-" }); + + DiscordBot.Utilities.Utilities.FormatAndAlignTable(items, Utilities.TableFormat.CENTER_EACH_COLUMN_BASED); + return; + } + + if (!Program.internalActionManager.Actions.ContainsKey(args[0])) + { + Console.WriteLine("Command not found"); + return; + } + + var action = Program.internalActionManager.Actions[args[0]]; + var actionData = new List + { + new[] { "-", "-", "-" }, + new[] { "Command", "Usage", "Description" }, + new[] { "-", "-", "-"}, + new[] { action.ActionName, action.Usage, action.Description }, + new[] { "-", "-", "-" } + }; + + DiscordBot.Utilities.Utilities.FormatAndAlignTable(actionData, Utilities.TableFormat.CENTER_EACH_COLUMN_BASED); + } + } +} \ No newline at end of file diff --git a/DiscordBot/Discord/Actions/ListPlugins.cs b/DiscordBot/Discord/Actions/ListPlugins.cs new file mode 100644 index 0000000..86c8484 --- /dev/null +++ b/DiscordBot/Discord/Actions/ListPlugins.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +using PluginManager.Interfaces; +using PluginManager.Others; + +namespace DiscordBot.Discord.Actions +{ + public class ListPlugins : ICommandAction + { + public string ActionName => "listplugs"; + + public string Description => "Lists all plugins"; + + public string Usage => "listplugs"; + + public InternalActionRunType RunType => InternalActionRunType.ON_CALL; + + public async Task Execute(string[] args) + { + + var manager = new PluginManager.Online.PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]); + if (manager == null) + { + Console.WriteLine("Plugin manager is null"); + return; + } + + + var data = await manager.GetAvailablePlugins(); + var items = new List + { + new[] { "-", "-", "-", "-" }, + new[] { "Name", "Type", "Description", "Required" }, + new[] { "-", "-", "-", "-" } + }; + + foreach (var plugin in data) + { + items.Add(new[] { plugin[0], plugin[1], plugin[2], plugin[3] }); + } + + items.Add(new[] { "-", "-", "-", "-" }); + + DiscordBot.Utilities.Utilities.FormatAndAlignTable(items, Utilities.TableFormat.DEFAULT); + } + } +} \ No newline at end of file diff --git a/DiscordBot/Discord/Actions/LoadPlugins.cs b/DiscordBot/Discord/Actions/LoadPlugins.cs new file mode 100644 index 0000000..f91aefe --- /dev/null +++ b/DiscordBot/Discord/Actions/LoadPlugins.cs @@ -0,0 +1,94 @@ +using System.Runtime.CompilerServices; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using PluginManager.Interfaces; +using PluginManager.Loaders; +using PluginManager.Others; + +namespace DiscordBot.Discord.Actions +{ + public class LoadPlugins : ICommandAction + { + public string ActionName => "loadplugs"; + + public string Description => "Loads all plugins"; + + public string Usage => "loadplugs"; + + private bool pluginsLoaded = false; + + public InternalActionRunType RunType => InternalActionRunType.ON_STARTUP; + + public async Task Execute(string[] args) + { + if (pluginsLoaded) + return; + var loader = new PluginLoader(PluginManager.Config.DiscordBot.client); + var cc = Console.ForegroundColor; + loader.onCMDLoad += (name, typeName, success, exception) => + { + if (name == null || name.Length < 2) + name = typeName; + if (success) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("[CMD] Successfully loaded command : " + name); + } + + else + { + Console.ForegroundColor = ConsoleColor.Red; + if (exception is null) + Console.WriteLine("An error occured while loading: " + name); + else + Console.WriteLine("[CMD] Failed to load command : " + name + " because " + exception!.Message); + } + + Console.ForegroundColor = cc; + }; + loader.onEVELoad += (name, typeName, success, exception) => + { + if (name == null || name.Length < 2) + name = typeName; + + if (success) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("[EVENT] Successfully loaded event : " + name); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("[EVENT] Failed to load event : " + name + " because " + exception!.Message); + } + + Console.ForegroundColor = cc; + }; + + loader.onSLSHLoad += (name, typeName, success, exception) => + { + if (name == null || name.Length < 2) + name = typeName; + + if (success) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("[SLASH] Successfully loaded command : " + name); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("[SLASH] Failed to load command : " + name + " because " + exception!.Message); + } + + Console.ForegroundColor = cc; + }; + + loader.LoadPlugins(); + Console.ForegroundColor = cc; + pluginsLoaded = true; + } + } +} \ No newline at end of file diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 6e6a48b..482fa80 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -1,9 +1,7 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Reflection; -using System.Threading; using System.Threading.Tasks; using System.Linq; @@ -14,17 +12,15 @@ using PluginManager.Online.Helpers; using PluginManager.Others; using DiscordBot.Utilities; -using Microsoft.VisualBasic.CompilerServices; -using OperatingSystem = PluginManager.Others.OperatingSystem; using static PluginManager.Config; +using PluginManager.Interfaces; namespace DiscordBot; public class Program { public static Json URLs; - private static bool loadPluginsOnStartup = false; - private static ConsoleCommandsHandler consoleCommandsHandler; + public static PluginManager.Others.Actions.InternalActionManager internalActionManager; /// /// The main entry point for the application. @@ -56,19 +52,17 @@ public class Program Console.WriteLine("Debug mode enabled"); #endif - if (loadPluginsOnStartup) - consoleCommandsHandler.HandleCommand("lp"); while (true) { var cmd = Console.ReadLine(); - if (!consoleCommandsHandler.HandleCommand(cmd! -#if DEBUG - , false -#endif + string[] args = cmd.Split(' '); + string command = args[0]; + args = args.Skip(1).ToArray(); + if(args.Length == 0) + args = null; - ) && cmd.Length > 0) - Console.WriteLine("Failed to run command " + cmd); + internalActionManager.Execute(command, args).Wait(); // Execute the command } } @@ -136,7 +130,6 @@ public class Program Console.WriteLine("Loading Core ..."); var b = await StartNoGui(); - consoleCommandsHandler = new ConsoleCommandsHandler(b.client); try { if(args.Contains("--gui")) @@ -145,6 +138,10 @@ public class Program Console.WriteLine("GUI not implemented yet"); return; } + + internalActionManager = new PluginManager.Others.Actions.InternalActionManager("./Data/Actions", "*.dll"); + await internalActionManager.Initialize(); + NoGUI(); } catch (IOException ex) @@ -217,7 +214,7 @@ public class Program case "CurrentVersion": var currentVersion = Config.Data["Version"]; var newVersion = s[1]; - if(newVersion != currentVersion) + if(new VersionString(newVersion) != new VersionString(newVersion)) { Console.WriteLine("A new updated was found. Check the changelog for more information."); List changeLog = await ServerCom.ReadTextFromURL(URLs["Changelog"]); @@ -232,122 +229,10 @@ public class Program Console.ReadKey(); } break; - // case "CurrentVersion": - // var newVersion = s[1]; - // var currentVersion = Config.Data["Version"]; - - // if (!newVersion.Equals(currentVersion)) - // { - - // var nVer = new VersionString(newVersion.Substring(2)); - // var cVer = new VersionString((Config.Data["Version"]).Substring(2)); - // if (cVer > nVer) - // { - // Config.Data["Version"] = "1." + cVer.ToShortString() + " (Beta)"; - // break; - // } - - // if (OperatingSystem.WINDOWS == Functions.GetOperatingSystem()) - // { - // Console.Clear(); - // Console.WriteLine("A new update was found !"); - // Console.WriteLine("Run the launcher to update"); - // Console.WriteLine("Current version: " + currentVersion); - // Console.WriteLine("Latest version: " + s[1]); - - // File.WriteAllText("version.txt", currentVersion); - - // await Task.Delay(3000); - - // break; - // } - - // if(OperatingSystem.LINUX == Functions.GetOperatingSystem()) - // { - // Console.WriteLine("A new update was found !"); - // Console.WriteLine("Run the launcher to update"); - // } - // Console.Clear(); - // Console.ForegroundColor = ConsoleColor.Red; - // Console.WriteLine("A new version of the bot is available !"); - // Console.ForegroundColor = ConsoleColor.Yellow; - // Console.WriteLine("Current version : " + - // Assembly.GetExecutingAssembly().GetName().Version.ToString()); - // Console.ForegroundColor = ConsoleColor.Green; - // Console.WriteLine("New version : " + newVersion); - // Console.ForegroundColor = ConsoleColor.White; - - // File.WriteAllText("version.txt", newVersion); - - // Console.WriteLine("Changelog :"); - - // List changeLog = await ServerCom.ReadTextFromURL(URLs["Changelog"]); - // foreach (var item in changeLog) - // Utilities.Utilities.WriteColorText(item); - // Console.WriteLine("Do you want to update the bot ? (y/n)"); - // if (Console.ReadKey().Key == ConsoleKey.Y) - // { - // var url = URLs["LinuxBot"].Replace("{0}", newVersion); - // Config.Logger.Log($"executing: download_file {url}"); - - // await ServerCom.DownloadFileAsync(url, "./update.zip", new Progress(percent => { Console.WriteLine($"\rProgress: {percent}% "); })); - // await File.WriteAllTextAsync("Install.sh", - // "#!/bin/bash\nunzip -qq -o update.zip \nrm update.zip\nchmod a+x DiscordBot"); - - // try - // { - // Console.WriteLine("executing: chmod a+x Install.sh"); - // Process.Start("chmod", "a+x Install.sh").WaitForExit(); - // Process.Start("Install.sh").WaitForExit(); - - // Console.WriteLine("executing: rm Install.sh"); - // Process.Start("rm", "Install.sh").WaitForExit(); - - // Config.Logger.Log("The new version of the bot has been installed."); - // Console.WriteLine("Please restart the bot."); - // Environment.Exit(0); - // } - // catch (Exception ex) - // { - // Config.Logger.Log(ex.Message, "Updater", LogLevel.ERROR); - // if (ex.Message.Contains("Access de")) - // Config.Logger.Log("Please run the bot as sudo."); - // } - - - // } - // } - - // break; - // case "LauncherVersion": - // var updaternewversion = s[1]; - // //File.WriteAllText(updaternewversion + ".txt", updaternewversion); - // if (Functions.GetOperatingSystem() == OperatingSystem.LINUX) - // break; - - // Directory.CreateDirectory(Functions.dataFolder + "Applications"); - // if (!Config.Data.ContainsKey("LauncherVersion")) - // Config.Data["LauncherVersion"] = "0.0.0.0"; - // if (Config.Data["LauncherVersion"] != updaternewversion || - // !File.Exists("./Launcher.exe")) - // { - // Console.Clear(); - // Console.WriteLine("Installing a new Launcher ...\nDo NOT close the bot during update !"); - // var bar = new Utilities.Utilities.ProgressBar(ProgressBarType.NO_END); - // bar.Start(); - // await ServerCom.DownloadFileAsync(URLs["WindowsLauncher"], $"./Launcher.exe", null); - // //await ArchiveManager.ExtractArchive("./Updater.zip", "./", null, - // // UnzipProgressType.PercentageFromTotalSize); - // Config.Data["LauncherVersion"] = updaternewversion; - // // File.Delete("Updater.zip"); - // bar.Stop("The launcher has been updated !"); - // Console.Clear(); - // } - - // break; } } Console.Clear(); + } } \ No newline at end of file diff --git a/DiscordBot/Utilities/Command.cs b/DiscordBot/Utilities/Command.cs deleted file mode 100644 index a52fca5..0000000 --- a/DiscordBot/Utilities/Command.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using Discord.Commands; -using Discord.WebSocket; - -namespace DiscordBot.Utilities; - -public class ConsoleCommand -{ - public string? CommandName { get; init; } - public string? Description { get; init; } - public string? Usage { get; init; } - public Action? Action { get; init; } -} \ No newline at end of file diff --git a/DiscordBot/Utilities/ConsoleCommandsHandler.cs b/DiscordBot/Utilities/ConsoleCommandsHandler.cs deleted file mode 100644 index a0043af..0000000 --- a/DiscordBot/Utilities/ConsoleCommandsHandler.cs +++ /dev/null @@ -1,439 +0,0 @@ -using System.Threading; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Net.Http; -using System.Reflection; -using System.Threading.Tasks; - -using Discord.WebSocket; - -using PluginManager; -using PluginManager.Loaders; -using PluginManager.Online; -using PluginManager.Others; - -using OperatingSystem = PluginManager.Others.OperatingSystem; - -namespace DiscordBot.Utilities; - -public class ConsoleCommandsHandler -{ - - public static ConsoleCommandsHandler handler; - private readonly PluginsManager manager; - - private readonly List commandList = new(); - - - private bool isDownloading; - private bool pluginsLoaded; - private DiscordSocketClient client; - - public ConsoleCommandsHandler(DiscordSocketClient client) - { - this.client = client; - - manager = new PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]); - InitializeBasicCommands(); - Console.WriteLine("Done"); - - if (handler == null) - handler = this; - else - throw new Exception("ConsoleCommandsHandler already initialized"); - } - - private void InitializeBasicCommands() - { - commandList.Clear(); - - AddCommand("help", "Show help", "help ", args => - { - if (args.Length <= 1) - { - Console.WriteLine("Available commands:"); - var items = new List - { - new[] { "-", "-", "-" }, - new[] { "Command", "Description", "Usage" }, - new[] { " ", " ", "Argument type: [required]" }, - new[] { "-", "-", "-" } - }; - - foreach (var command in commandList) - { - if (!command.CommandName.StartsWith("_")) - items.Add(new[] { command.CommandName, command.Description, command.Usage }); - } - - items.Add(new[] { "-", "-", "-" }); - Utilities.FormatAndAlignTable(items, TableFormat.DEFAULT); - } - else - { - foreach (var command in commandList) - if (command.CommandName == args[1]) - { - Console.WriteLine("Command description: " + command.Description); - Console.WriteLine("Command execution format:" + command.Usage); - return; - } - - Console.WriteLine("Command not found"); - } - } - ); - - AddCommand("lp", "Load plugins", () => - { - if (pluginsLoaded) - return; - var loader = new PluginLoader(client!); - var cc = Console.ForegroundColor; - loader.onCMDLoad += (name, typeName, success, exception) => - { - if (name == null || name.Length < 2) - name = typeName; - if (success) - { - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine("[CMD] Successfully loaded command : " + name); - } - - else - { - Console.ForegroundColor = ConsoleColor.Red; - if (exception is null) - Console.WriteLine("An error occured while loading: " + name); - else - Console.WriteLine("[CMD] Failed to load command : " + name + " because " + exception!.Message); - } - - Console.ForegroundColor = cc; - }; - loader.onEVELoad += (name, typeName, success, exception) => - { - if (name == null || name.Length < 2) - name = typeName; - - if (success) - { - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine("[EVENT] Successfully loaded event : " + name); - } - else - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine("[EVENT] Failed to load event : " + name + " because " + exception!.Message); - } - - Console.ForegroundColor = cc; - }; - - loader.onSLSHLoad += (name, typeName, success, exception) => - { - if (name == null || name.Length < 2) - name = typeName; - - if (success) - { - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine("[SLASH] Successfully loaded command : " + name); - } - else - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine("[SLASH] Failed to load command : " + name + " because " + exception!.Message); - } - - Console.ForegroundColor = cc; - }; - - loader.LoadPlugins(); - Console.ForegroundColor = cc; - pluginsLoaded = true; - } - ); - - AddCommand("listplugs", "list available plugins", async () => { - if(manager == null) - { - Console.WriteLine("Plugin manager is null"); - return; - } - var data = await manager.GetAvailablePlugins(); - var items = new List - { - new[] { "-", "-", "-", "-" }, - new[] { "Name", "Type", "Description", "Required" }, - new[] { "-", "-", "-", "-" } - }; - - foreach (var plugin in data) - { - items.Add(new[] { plugin[0], plugin[1], plugin[2], plugin[3] }); - } - - items.Add(new[] { "-", "-", "-", "-" }); - Utilities.FormatAndAlignTable(items, TableFormat.DEFAULT); - }); - - AddCommand("dwplug", "download plugin", "dwplug [name]", async args => - { - isDownloading = true; - Utilities.Spinner spinner = new Utilities.Spinner(); - if (args.Length == 1) - { - isDownloading = false; - Console.WriteLine("Please specify plugin name"); - return; - } - - var name = string.Join(' ', args, 1, args.Length - 1); - // info[0] = plugin type - // info[1] = plugin link - // info[2] = if others are required, or string.Empty if none - var info = await manager.GetPluginLinkByName(name); - if (info[1] == null) // link is null - { - if (name == "") - { - isDownloading = false; - Utilities.WriteColorText("Name is invalid"); - return; - } - - isDownloading = false; - Utilities.WriteColorText($"Failed to find plugin &b{name} &c!" + - " Use &glistplugs &ccommand to display all available plugins !"); - return; - } - - spinner.Start(); - - string path; - if (info[0] == "Plugin") - path = "./Data/Plugins/" + name + ".dll"; - else - path = $"./{info[1].Split('/')[info[1].Split('/').Length - 1]}"; - - - Console.WriteLine($"Downloading plugin {name} from {info[1]} to {path}"); - await ServerCom.DownloadFileAsync(info[1], path, null); - - Console.WriteLine("\n"); - - // check requirements if any - - if (info.Length == 3 && info[2] != string.Empty && info[2] != null) - { - Console.WriteLine($"Downloading requirements for plugin : {name}"); - - var lines = await ServerCom.ReadTextFromURL(info[2]); - - foreach (var line in lines) - { - if (!(line.Length > 0 && line.Contains(","))) - continue; - var split = line.Split(','); - Console.WriteLine($"\nDownloading item: {split[1]}"); - if (File.Exists("./" + split[1])) File.Delete("./" + split[1]); - await ServerCom.DownloadFileAsync(split[0], "./" + split[1], null); - - Console.WriteLine("Item " + split[1] + " downloaded !"); - - Console.WriteLine(); - if (split[0].EndsWith(".pak")) - { - File.Move("./" + split[1], "./Data/PAKS/" + split[1], true); - } - else if (split[0].EndsWith(".zip") || split[0].EndsWith(".pkg")) - { - Console.WriteLine($"Extracting {split[1]} ..."); - await ArchiveManager.ExtractArchive("./" + split[1], "./", null, - UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE); - Console.WriteLine("\n"); - File.Delete("./" + split[1]); - } - } - - Console.WriteLine(); - } - spinner.Stop(); - - var ver = await manager.GetVersionOfPackageFromWeb(name); - if (ver is null) throw new Exception("Incorrect version"); - Config.Plugins[name] = ver.ToShortString(); - - isDownloading = false; - - - Config.Logger.Log("Plugin installed !", this, LogLevel.INFO); - - //await ExecuteCommad("localload " + name); - } - ); - - - AddCommand("value", "read value from VariableStack", "value [key]", args => - { - if (args.Length != 2) - return; - if (!Config.Data.ContainsKey(args[1])) - return; - - var data = Config.Data[args[1]]; - Console.WriteLine($"{args[1]} => {data}"); - } - ); - - AddCommand("add", "add variable to the system variables", "add [key] [value]", args => - { - if (args.Length < 4) - return; - var key = args[1]; - var value = args[2]; - var isReadOnly = args[3].Equals("true", StringComparison.CurrentCultureIgnoreCase); - - try - { - Config.Data[key] = value; - Console.WriteLine($"Updated config file with the following command: {args[1]} => {value}"); - } - catch (Exception ex) - { - Console.WriteLine(ex.ToString()); - } - } - ); - - AddCommand("remv", "remove variable from system variables", "remv [key]", args => - { - if (args.Length < 2) - return; - Config.Data.Remove(args[1]); - } - ); - - AddCommand("sd", "Shuts down the discord bot", async () => - { - if (client is null) - return; - - await Functions.SaveToJsonFile(Functions.dataFolder + "config.json", Config.Data); - await Functions.SaveToJsonFile(Functions.dataFolder + "Plugins.json", Config.Plugins); - await client.StopAsync(); - await client.DisposeAsync(); - - Config.Logger.SaveToFile(); - await Task.Delay(1000); - Environment.Exit(0); - } - ); - - AddCommand("import", "Load an external command", "import [pluginName]", async args => - { - if (args.Length <= 1) return; - try - { - var pName = string.Join(' ', args, 1, args.Length - 1); - using (var client = new HttpClient()) - { - var url = (await manager.GetPluginLinkByName(pName))[1]; - if (url is null) throw new Exception($"Invalid plugin name {pName}."); - var s = await client.GetStreamAsync(url); - var str = new MemoryStream(); - await s.CopyToAsync(str); - var asmb = Assembly.Load(str.ToArray()); - - await PluginLoader.LoadPluginFromAssembly(asmb, this.client); - } - - - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - }); - - AddCommand("localload", "Load a local command", "local [pluginName]", async args => - { - if (args.Length <= 1) return; - try - { - var pName = string.Join(' ', args, 1, args.Length - 1); - var asmb = Assembly.LoadFile(Path.GetFullPath("./Data/Plugins/" + pName + ".dll")); - - await PluginLoader.LoadPluginFromAssembly(asmb, this.client); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - Config.Logger.Log(ex.Message, this, LogLevel.ERROR); - } - }); - - - commandList.Sort((x, y) => x.CommandName.CompareTo(y.CommandName)); - } - - public void AddCommand(string command, string description, string usage, Action action) - { - Console.WriteLine($"Adding command {command} ..."); - commandList.Add(new ConsoleCommand - { CommandName = command, Description = description, Action = action, Usage = usage }); - Console.ForegroundColor = ConsoleColor.White; - Utilities.WriteColorText($"Command &r{command} &cadded to the list of commands"); - } - - public void AddCommand(string command, string description, Action action) - { - AddCommand(command, description, command, args => action()); - } - - public void RemoveCommand(string command) - { - commandList.RemoveAll(x => x.CommandName == command); - } - - public bool CommandExists(string command) - { - return GetCommand(command) is not null; - } - - public ConsoleCommand? GetCommand(string command) - { - return commandList.FirstOrDefault(t => t.CommandName == command); - } - - public bool HandleCommand(string command, bool removeCommandExecution = true) - { - Console.ForegroundColor = ConsoleColor.White; - var args = command.Split(' '); - foreach (var item in commandList.ToList()) - if (item.CommandName == args[0]) - { - if (args[0].StartsWith("_")) - throw new Exception("This command is reserved for internal worker and can not be executed manually !"); - - if (removeCommandExecution) - { - Console.SetCursorPosition(0, Console.CursorTop - 1); - for (var i = 0; i < command.Length + 30; i++) - Console.Write(" "); - Console.SetCursorPosition(0, Console.CursorTop); - } - - Console.WriteLine(); - item.Action(args); - - return true; - } - - return false; - } -} \ No newline at end of file diff --git a/DiscordBot/Utilities/ConsoleCommandsHandler_OLD.cs b/DiscordBot/Utilities/ConsoleCommandsHandler_OLD.cs new file mode 100644 index 0000000..17bf751 --- /dev/null +++ b/DiscordBot/Utilities/ConsoleCommandsHandler_OLD.cs @@ -0,0 +1,435 @@ +// using System; +// using System.Collections.Generic; +// using System.IO; +// using System.Linq; +// using System.Net.Http; +// using System.Reflection; +// using System.Threading.Tasks; + +// using Discord.WebSocket; + +// using PluginManager; +// using PluginManager.Loaders; +// using PluginManager.Online; +// using PluginManager.Others; + +// namespace DiscordBot.Utilities; + +// public class ConsoleCommandsHandlerOLD +// { + +// public static ConsoleCommandsHandler handler; +// private readonly PluginsManager manager; + +// private readonly List commandList = new(); + + +// private bool isDownloading; +// private bool pluginsLoaded; +// private DiscordSocketClient client; + +// public ConsoleCommandsHandler(DiscordSocketClient client) +// { +// this.client = client; + +// manager = new PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]); +// InitializeBasicCommands(); +// Console.WriteLine("Done"); + +// if (handler == null) +// handler = this; +// else +// throw new Exception("ConsoleCommandsHandler already initialized"); +// } + +// private void InitializeBasicCommands() +// { +// commandList.Clear(); + +// AddCommand("help", "Show help", "help ", args => +// { +// if (args.Length <= 1) +// { +// Console.WriteLine("Available commands:"); +// var items = new List +// { +// new[] { "-", "-", "-" }, +// new[] { "Command", "Description", "Usage" }, +// new[] { " ", " ", "Argument type: [required]" }, +// new[] { "-", "-", "-" } +// }; + +// foreach (var command in commandList) +// { +// if (!command.CommandName.StartsWith("_")) +// items.Add(new[] { command.CommandName, command.Description, command.Usage }); +// } + +// items.Add(new[] { "-", "-", "-" }); +// Utilities.FormatAndAlignTable(items, TableFormat.DEFAULT); +// } +// else +// { +// foreach (var command in commandList) +// if (command.CommandName == args[1]) +// { +// Console.WriteLine("Command description: " + command.Description); +// Console.WriteLine("Command execution format:" + command.Usage); +// return; +// } + +// Console.WriteLine("Command not found"); +// } +// } +// ); + +// AddCommand("lp", "Load plugins", () => +// { +// if (pluginsLoaded) +// return; +// var loader = new PluginLoader(client!); +// var cc = Console.ForegroundColor; +// loader.onCMDLoad += (name, typeName, success, exception) => +// { +// if (name == null || name.Length < 2) +// name = typeName; +// if (success) +// { +// Console.ForegroundColor = ConsoleColor.Green; +// Console.WriteLine("[CMD] Successfully loaded command : " + name); +// } + +// else +// { +// Console.ForegroundColor = ConsoleColor.Red; +// if (exception is null) +// Console.WriteLine("An error occured while loading: " + name); +// else +// Console.WriteLine("[CMD] Failed to load command : " + name + " because " + exception!.Message); +// } + +// Console.ForegroundColor = cc; +// }; +// loader.onEVELoad += (name, typeName, success, exception) => +// { +// if (name == null || name.Length < 2) +// name = typeName; + +// if (success) +// { +// Console.ForegroundColor = ConsoleColor.Green; +// Console.WriteLine("[EVENT] Successfully loaded event : " + name); +// } +// else +// { +// Console.ForegroundColor = ConsoleColor.Red; +// Console.WriteLine("[EVENT] Failed to load event : " + name + " because " + exception!.Message); +// } + +// Console.ForegroundColor = cc; +// }; + +// loader.onSLSHLoad += (name, typeName, success, exception) => +// { +// if (name == null || name.Length < 2) +// name = typeName; + +// if (success) +// { +// Console.ForegroundColor = ConsoleColor.Green; +// Console.WriteLine("[SLASH] Successfully loaded command : " + name); +// } +// else +// { +// Console.ForegroundColor = ConsoleColor.Red; +// Console.WriteLine("[SLASH] Failed to load command : " + name + " because " + exception!.Message); +// } + +// Console.ForegroundColor = cc; +// }; + +// loader.LoadPlugins(); +// Console.ForegroundColor = cc; +// pluginsLoaded = true; +// } +// ); + +// AddCommand("listplugs", "list available plugins", async () => { +// if(manager == null) +// { +// Console.WriteLine("Plugin manager is null"); +// return; +// } +// var data = await manager.GetAvailablePlugins(); +// var items = new List +// { +// new[] { "-", "-", "-", "-" }, +// new[] { "Name", "Type", "Description", "Required" }, +// new[] { "-", "-", "-", "-" } +// }; + +// foreach (var plugin in data) +// { +// items.Add(new[] { plugin[0], plugin[1], plugin[2], plugin[3] }); +// } + +// items.Add(new[] { "-", "-", "-", "-" }); +// Utilities.FormatAndAlignTable(items, TableFormat.DEFAULT); +// }); + +// AddCommand("dwplug", "download plugin", "dwplug [name]", async args => +// { +// isDownloading = true; +// Utilities.Spinner spinner = new Utilities.Spinner(); +// if (args.Length == 1) +// { +// isDownloading = false; +// Console.WriteLine("Please specify plugin name"); +// return; +// } + +// var name = string.Join(' ', args, 1, args.Length - 1); +// // info[0] = plugin type +// // info[1] = plugin link +// // info[2] = if others are required, or string.Empty if none +// var info = await manager.GetPluginLinkByName(name); +// if (info[1] == null) // link is null +// { +// if (name == "") +// { +// isDownloading = false; +// Utilities.WriteColorText("Name is invalid"); +// return; +// } + +// isDownloading = false; +// Utilities.WriteColorText($"Failed to find plugin &b{name} &c!" + +// " Use &glistplugs &ccommand to display all available plugins !"); +// return; +// } + +// spinner.Start(); + +// string path; +// if (info[0] == "Plugin") +// path = "./Data/Plugins/" + name + ".dll"; +// else +// path = $"./{info[1].Split('/')[info[1].Split('/').Length - 1]}"; + + +// Console.WriteLine($"Downloading plugin {name} from {info[1]} to {path}"); +// await ServerCom.DownloadFileAsync(info[1], path, null); + +// Console.WriteLine("\n"); + +// // check requirements if any + +// if (info.Length == 3 && info[2] != string.Empty && info[2] != null) +// { +// Console.WriteLine($"Downloading requirements for plugin : {name}"); + +// var lines = await ServerCom.ReadTextFromURL(info[2]); + +// foreach (var line in lines) +// { +// if (!(line.Length > 0 && line.Contains(","))) +// continue; +// var split = line.Split(','); +// Console.WriteLine($"\nDownloading item: {split[1]}"); +// if (File.Exists("./" + split[1])) File.Delete("./" + split[1]); +// await ServerCom.DownloadFileAsync(split[0], "./" + split[1], null); + +// Console.WriteLine("Item " + split[1] + " downloaded !"); + +// Console.WriteLine(); +// if (split[0].EndsWith(".pak")) +// { +// File.Move("./" + split[1], "./Data/PAKS/" + split[1], true); +// } +// else if (split[0].EndsWith(".zip") || split[0].EndsWith(".pkg")) +// { +// Console.WriteLine($"Extracting {split[1]} ..."); +// await ArchiveManager.ExtractArchive("./" + split[1], "./", null, +// UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE); +// Console.WriteLine("\n"); +// File.Delete("./" + split[1]); +// } +// } + +// Console.WriteLine(); +// } +// spinner.Stop(); + +// var ver = await manager.GetVersionOfPackageFromWeb(name); +// if (ver is null) throw new Exception("Incorrect version"); +// Config.Plugins[name] = ver.ToShortString(); + +// isDownloading = false; + + +// Config.Logger.Log("Plugin installed !", this, LogLevel.INFO); + +// //await ExecuteCommad("localload " + name); +// } +// ); + + +// AddCommand("value", "read value from VariableStack", "value [key]", args => +// { +// if (args.Length != 2) +// return; +// if (!Config.Data.ContainsKey(args[1])) +// return; + +// var data = Config.Data[args[1]]; +// Console.WriteLine($"{args[1]} => {data}"); +// } +// ); + +// AddCommand("add", "add variable to the system variables", "add [key] [value]", args => +// { +// if (args.Length < 4) +// return; +// var key = args[1]; +// var value = args[2]; +// var isReadOnly = args[3].Equals("true", StringComparison.CurrentCultureIgnoreCase); + +// try +// { +// Config.Data[key] = value; +// Console.WriteLine($"Updated config file with the following command: {args[1]} => {value}"); +// } +// catch (Exception ex) +// { +// Console.WriteLine(ex.ToString()); +// } +// } +// ); + +// AddCommand("remv", "remove variable from system variables", "remv [key]", args => +// { +// if (args.Length < 2) +// return; +// Config.Data.Remove(args[1]); +// } +// ); + +// AddCommand("sd", "Shuts down the discord bot", async () => +// { +// if (client is null) +// return; + +// await Functions.SaveToJsonFile(Functions.dataFolder + "config.json", Config.Data); +// await Functions.SaveToJsonFile(Functions.dataFolder + "Plugins.json", Config.Plugins); +// await client.StopAsync(); +// await client.DisposeAsync(); + +// Config.Logger.SaveToFile(); +// await Task.Delay(1000); +// Environment.Exit(0); +// } +// ); + +// AddCommand("import", "Load an external command", "import [pluginName]", async args => +// { +// if (args.Length <= 1) return; +// try +// { +// var pName = string.Join(' ', args, 1, args.Length - 1); +// using (var client = new HttpClient()) +// { +// var url = (await manager.GetPluginLinkByName(pName))[1]; +// if (url is null) throw new Exception($"Invalid plugin name {pName}."); +// var s = await client.GetStreamAsync(url); +// var str = new MemoryStream(); +// await s.CopyToAsync(str); +// var asmb = Assembly.Load(str.ToArray()); + +// await PluginLoader.LoadPluginFromAssembly(asmb, this.client); +// } + + +// } +// catch (Exception ex) +// { +// Console.WriteLine(ex.Message); +// } +// }); + +// AddCommand("localload", "Load a local command", "local [pluginName]", async args => +// { +// if (args.Length <= 1) return; +// try +// { +// var pName = string.Join(' ', args, 1, args.Length - 1); +// var asmb = Assembly.LoadFile(Path.GetFullPath("./Data/Plugins/" + pName + ".dll")); + +// await PluginLoader.LoadPluginFromAssembly(asmb, this.client); +// } +// catch (Exception ex) +// { +// Console.WriteLine(ex.Message); +// Config.Logger.Log(ex.Message, this, LogLevel.ERROR); +// } +// }); + + +// commandList.Sort((x, y) => x.CommandName.CompareTo(y.CommandName)); +// } + +// public void AddCommand(string command, string description, string usage, Action action) +// { +// Console.WriteLine($"Adding command {command} ..."); +// commandList.Add(new ConsoleCommand +// { CommandName = command, Description = description, Action = action, Usage = usage }); +// Console.ForegroundColor = ConsoleColor.White; +// Utilities.WriteColorText($"Command &r{command} &cadded to the list of commands"); +// } + +// public void AddCommand(string command, string description, Action action) +// { +// AddCommand(command, description, command, args => action()); +// } + +// public void RemoveCommand(string command) +// { +// commandList.RemoveAll(x => x.CommandName == command); +// } + +// public bool CommandExists(string command) +// { +// return GetCommand(command) is not null; +// } + +// public ConsoleCommand? GetCommand(string command) +// { +// return commandList.FirstOrDefault(t => t.CommandName == command); +// } + +// public bool HandleCommand(string command, bool removeCommandExecution = true) +// { +// Console.ForegroundColor = ConsoleColor.White; +// var args = command.Split(' '); +// foreach (var item in commandList.ToList()) +// if (item.CommandName == args[0]) +// { +// if (args[0].StartsWith("_")) +// throw new Exception("This command is reserved for internal worker and can not be executed manually !"); + +// if (removeCommandExecution) +// { +// Console.SetCursorPosition(0, Console.CursorTop - 1); +// for (var i = 0; i < command.Length + 30; i++) +// Console.Write(" "); +// Console.SetCursorPosition(0, Console.CursorTop); +// } + +// Console.WriteLine(); +// item.Action(args); + +// return true; +// } + +// return false; +// } +// } \ No newline at end of file diff --git a/PluginManager/Bot/Boot.cs b/PluginManager/Bot/Boot.cs index 6fc54b2..676601c 100644 --- a/PluginManager/Bot/Boot.cs +++ b/PluginManager/Bot/Boot.cs @@ -88,6 +88,9 @@ public class Boot await Task.Delay(2000); + + Config._DiscordBotClient = this; + while (!isReady) ; } diff --git a/PluginManager/Config.cs b/PluginManager/Config.cs index d9b5886..6fe794c 100644 --- a/PluginManager/Config.cs +++ b/PluginManager/Config.cs @@ -5,19 +5,24 @@ using System.IO; using System.Collections.Generic; using PluginManager.Others; using System.Collections; -using PluginManager.Online.Helpers; using PluginManager.Others.Logger; namespace PluginManager; -public static class Config +public class Config { private static bool IsLoaded = false; - public static DBLogger Logger; public static Json Data; public static Json Plugins; + internal static Bot.Boot? _DiscordBotClient; + + public static Bot.Boot? DiscordBot + { + get => _DiscordBotClient; + } + public static async Task Initialize() { if (IsLoaded) diff --git a/PluginManager/Interfaces/IInternalAction.cs b/PluginManager/Interfaces/IInternalAction.cs new file mode 100644 index 0000000..83f98c7 --- /dev/null +++ b/PluginManager/Interfaces/IInternalAction.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using PluginManager.Others; + +namespace PluginManager.Interfaces +{ + public interface ICommandAction + { + public string ActionName { get; } + + public string? Description { get; } + + public string? Usage { get; } + + public InternalActionRunType RunType { get; } + + public Task Execute(string[]? args); + + } +} \ No newline at end of file diff --git a/PluginManager/Loaders/ActionsLoader.cs b/PluginManager/Loaders/ActionsLoader.cs new file mode 100644 index 0000000..c938627 --- /dev/null +++ b/PluginManager/Loaders/ActionsLoader.cs @@ -0,0 +1,76 @@ +using System.IO; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Reflection; +using PluginManager.Others.Actions; +using PluginManager.Interfaces; +using System.Collections; + +namespace PluginManager.Loaders +{ + public class ActionsLoader + { + public delegate void ActionLoaded(string name, string typeName, bool success, Exception? e = null); + public event ActionLoaded? ActionLoadedEvent; + + private string actionFolder = @"./Data/Actions/"; + private string actionExtension = "dll"; + + public ActionsLoader(string path, string extension) + { + actionFolder = path; + actionExtension = extension; + } + + public async Task?> Load() + { + Directory.CreateDirectory(actionFolder); + var files = Directory.GetFiles(actionFolder, $"*.{actionExtension}", SearchOption.AllDirectories); + + List actions = new List(); + + foreach (var file in files) + { + try + { + Assembly.LoadFrom(file); + } + catch (Exception e) + { + ActionLoadedEvent?.Invoke(file, "", false, e); + } + } + + var types = AppDomain.CurrentDomain.GetAssemblies() + .SelectMany(s => s.GetTypes()) + .Where(p => typeof(ICommandAction).IsAssignableFrom(p) && !p.IsInterface); + + foreach (var type in types) + { + try + { + var action = (ICommandAction) Activator.CreateInstance(type); + if (action.ActionName == null) + { + ActionLoadedEvent?.Invoke(action.ActionName, type.Name, false); + continue; + } + + if(action.RunType == PluginManager.Others.InternalActionRunType.ON_STARTUP) + await action.Execute(null); + + ActionLoadedEvent?.Invoke(action.ActionName, type.Name, true); + actions.Add(action); + } + catch (Exception e) + { + ActionLoadedEvent?.Invoke(type.Name, type.Name, false, e); + } + } + + return actions; + } + } +} \ No newline at end of file diff --git a/PluginManager/Loaders/Loader.cs b/PluginManager/Loaders/Loader.cs index ecf4582..5196f70 100644 --- a/PluginManager/Loaders/Loader.cs +++ b/PluginManager/Loaders/Loader.cs @@ -110,7 +110,10 @@ namespace PluginManager.Loaders Exception = null, IsLoaded = true, PluginName = type.FullName, - TypeName = typeof(T) == typeof(DBCommand) ? "DBCommand" : typeof(T) == typeof(DBEvent) ? "DBEvent" : "DBSlashCommand", + TypeName = typeof(T) == typeof(DBCommand) ? "DBCommand" : + typeof(T) == typeof(DBEvent) ? "DBEvent" : + typeof(T) == typeof(DBSlashCommand) ? "DBSlashCommand" : + null, Plugin = plugin } ); diff --git a/PluginManager/Loaders/PluginLoader.cs b/PluginManager/Loaders/PluginLoader.cs index f9f326c..6b0cad0 100644 --- a/PluginManager/Loaders/PluginLoader.cs +++ b/PluginManager/Loaders/PluginLoader.cs @@ -9,6 +9,7 @@ using Discord.WebSocket; using PluginManager.Interfaces; using PluginManager.Online; +using PluginManager.Others; namespace PluginManager.Loaders; diff --git a/PluginManager/Online/Helpers/VersionString.cs b/PluginManager/Online/Helpers/VersionString.cs index e06bc87..760b962 100644 --- a/PluginManager/Online/Helpers/VersionString.cs +++ b/PluginManager/Online/Helpers/VersionString.cs @@ -4,6 +4,24 @@ namespace PluginManager.Online.Helpers; public class VersionString { + private bool Equals(VersionString other) + { + return PackageCheckVersion == other.PackageCheckVersion && PackageMainVersion == other.PackageMainVersion && PackageVersionID == other.PackageVersionID; + } + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((VersionString)obj); + } + + public override int GetHashCode() + { + return HashCode.Combine(PackageCheckVersion, PackageMainVersion, PackageVersionID); + } + public int PackageCheckVersion; public int PackageMainVersion; public int PackageVersionID; @@ -13,9 +31,22 @@ public class VersionString var data = version.Split('.'); try { - PackageVersionID = int.Parse(data[0]); - PackageMainVersion = int.Parse(data[1]); - PackageCheckVersion = int.Parse(data[2]); + if (data.Length == 3) + { + PackageVersionID = int.Parse(data[0]); + PackageMainVersion = int.Parse(data[1]); + PackageCheckVersion = int.Parse(data[2]); + } + else if (data.Length == 4) + { + // ignore the first item data[0] + PackageVersionID = int.Parse(data[1]); + PackageMainVersion = int.Parse(data[2]); + PackageCheckVersion = int.Parse(data[3]); + } + else + throw new Exception("Invalid version string"); + } catch (Exception ex) { diff --git a/PluginManager/Others/Actions/ActionManager.cs b/PluginManager/Others/Actions/ActionManager.cs deleted file mode 100644 index b2bcae6..0000000 --- a/PluginManager/Others/Actions/ActionManager.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace PluginManager.Others.Actions -{ - public class ActionManager - { - public List Actions { get; private set; } - - private bool _isInitialized = false; - - public ActionManager() - { - if(_isInitialized) return; - - Actions = new List(); - - _isInitialized = true; - } - - public bool ActionExists(string name) - { - if(!_isInitialized) throw new Exception("ActionManager is not initialized"); - return Actions.Any(x => x.Name == name); - } - - public void AddAction(InternalAction action) - { - if(!_isInitialized) throw new Exception("ActionManager is not initialized"); - Actions.Add(action); - } - - public void ExecuteAction(string name, string[] args) - { - if(!_isInitialized) throw new Exception("ActionManager is not initialized"); - var action = Actions.FirstOrDefault(x => x.Name == name); - if(action == null) throw new Exception($"Action {name} not found"); - action.Invoke(args); - } - - public async Task ExecuteActionAsync(string name, string[] args) - { - if(!_isInitialized) throw new Exception("ActionManager is not initialized"); - var action = Actions.FirstOrDefault(x => x.Name == name); - if(action == null) throw new Exception($"Action {name} not found"); - await action.InvokeAsync(args); - } - } -} diff --git a/PluginManager/Others/Actions/InternalAction.cs b/PluginManager/Others/Actions/InternalAction.cs deleted file mode 100644 index c43d684..0000000 --- a/PluginManager/Others/Actions/InternalAction.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PluginManager.Others.Actions -{ - public class InternalAction - { - public string? Name { get; init; } - public Action Action { get; init; } - - public InternalAction(string name, Action action) - { - Name = name; - Action = action; - } - - public InternalAction(string name, Action action) - { - Name = name; - Action = (o) => - { - action(); - return; - }; - } - - public void Invoke(string[] args) - { - Action(args); - } - - public async Task InvokeAsync(string[] args) - { - await Task.Run(() => Action(args)); - } - } -} diff --git a/PluginManager/Others/Actions/InternalActionsManager.cs b/PluginManager/Others/Actions/InternalActionsManager.cs new file mode 100644 index 0000000..aaa7e06 --- /dev/null +++ b/PluginManager/Others/Actions/InternalActionsManager.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using PluginManager.Interfaces; +using PluginManager.Loaders; + +namespace PluginManager.Others.Actions +{ + public class InternalActionManager + { + public ActionsLoader loader; + public Dictionary Actions = new Dictionary(); + + public InternalActionManager(string path, string extension) + { + loader = new ActionsLoader(path, extension); + } + + public async Task Initialize() + { + loader.ActionLoadedEvent += OnActionLoaded; + var m_actions = await loader.Load(); + if(m_actions == null) return; + foreach(var action in m_actions) + Actions.Add(action.ActionName, action); + } + + private void OnActionLoaded(string name, string typeName, bool success, Exception? e) + { + if (!success) + { + Config.Logger.Error(e); + return; + } + + Config.Logger.Log($"Action {name} loaded successfully", typeName, LogLevel.INFO); + } + + public async Task Execute(string actionName, string[]? args) + { + if (!Actions.ContainsKey(actionName)) + { + Config.Logger.Log($"Action {actionName} not found", "InternalActionManager", LogLevel.WARNING); + return "Action not found"; + } + + try{ + await Actions[actionName].Execute(args); + return "Action executed"; + }catch(Exception e){ + Config.Logger.Log(e.Message, "InternalActionManager", LogLevel.ERROR); + return e.Message; + } + } + } +} diff --git a/PluginManager/Others/Enums.cs b/PluginManager/Others/Enums.cs index cee40be..bace71c 100644 --- a/PluginManager/Others/Enums.cs +++ b/PluginManager/Others/Enums.cs @@ -35,3 +35,8 @@ public enum SaveType BACKUP } +public enum InternalActionRunType +{ + ON_STARTUP, + ON_CALL +} \ No newline at end of file diff --git a/PluginManager/Others/Logger/DBLogger.cs b/PluginManager/Others/Logger/DBLogger.cs index 1d12b9c..cc7d768 100644 --- a/PluginManager/Others/Logger/DBLogger.cs +++ b/PluginManager/Others/Logger/DBLogger.cs @@ -29,13 +29,14 @@ namespace PluginManager.Others.Logger } public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO) => Log(new LogMessage(message, type, sender)); + public void Error(Exception? e) => Log(e.Message, e.Source, LogLevel.ERROR); public void Log(LogMessage message) { if(LogEvent is not null) LogEvent?.Invoke(message.Message, message.Type); - if (message.Type != LogLevel.NONE) + if (message.Type != LogLevel.ERROR && message.Type != LogLevel.CRITICAL) LogHistory.Add(message); else ErrorHistory.Add(message);