From 9b563cc0f6b2a3c302b85a6cc2630320b825888c Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Mon, 1 Jul 2024 13:07:34 +0300 Subject: [PATCH] Updated Logger. Local plugin database has now only executable dependencies stored in the database --- DiscordBot/Bot/Actions/Extra/PluginMethods.cs | 13 ++++++++++--- DiscordBot/Program.cs | 6 ++---- DiscordBot/Utilities/Console Utilities.cs | 7 +++---- DiscordBotCore/Interfaces/Logger/ILogger.cs | 2 +- DiscordBotCore/Online/PluginManager.cs | 7 ++++--- DiscordBotCore/Others/Logger/LogMessage.cs | 4 ++-- DiscordBotCore/Others/Logger/Logger.cs | 2 +- DiscordBotCore/Plugin/OnlineDependencyInfo.cs | 4 +++- DiscordBotCore/Plugin/PluginInfo.cs | 8 ++++++-- DiscordBotCore/Updater/Plugins/PluginUpdater.cs | 2 +- 10 files changed, 33 insertions(+), 22 deletions(-) diff --git a/DiscordBot/Bot/Actions/Extra/PluginMethods.cs b/DiscordBot/Bot/Actions/Extra/PluginMethods.cs index ab22e96..3cbf43c 100644 --- a/DiscordBot/Bot/Actions/Extra/PluginMethods.cs +++ b/DiscordBot/Bot/Actions/Extra/PluginMethods.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using System.Threading.Tasks; using DiscordBot.Utilities; @@ -40,9 +41,15 @@ internal static class PluginMethods internal static async Task RefreshPlugins(bool quiet) { + try + { + await LoadPlugins(quiet ? ["-q"] : null); + await Application.CurrentApplication.InternalActionManager.Initialize(); + }catch(Exception ex) + { + Application.CurrentApplication.Logger.LogException(ex, typeof(PluginMethods), false); + } - await LoadPlugins(quiet ? ["-q"] : null); - await Application.CurrentApplication.InternalActionManager.Initialize(); } internal static async Task DownloadPlugin(PluginManager manager, string pluginName) @@ -108,7 +115,7 @@ internal static class PluginMethods foreach (var dependency in pluginData.Dependencies) { - var task = ctx.AddTask($"Downloading {dependency.DownloadLocation}: "); + var task = ctx.AddTask($"Downloading {dependency.DownloadLocation} -> Executable: {dependency.IsExecutable}: "); IProgress progress = new Progress(p => { task.Value = p; diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 2e11d54..811168d 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; @@ -22,10 +21,10 @@ public class Program /// public static async Task Startup(string[] args) { + await LoadComponents(args); await PrepareConsole(); - await PluginMethods.LoadPlugins(null); - await Application.CurrentApplication.InternalActionManager.Initialize(); + await PluginMethods.RefreshPlugins(false); await ConsoleInputHandler(); } @@ -54,7 +53,6 @@ public class Program /// Returns the bootloader for the Discord Bot private static async Task PrepareConsole() { - AnsiConsole.MarkupLine($"[yellow]Running on version: {Assembly.GetExecutingAssembly().GetName().Version}[/]"); AnsiConsole.MarkupLine("[yellow]Git SethBot: https://github.com/andreitdr/SethDiscordBot [/]"); AnsiConsole.MarkupLine("[yellow]Git Plugins: https://github.com/andreitdr/SethPlugins [/]"); diff --git a/DiscordBot/Utilities/Console Utilities.cs b/DiscordBot/Utilities/Console Utilities.cs index 45b0eed..0246826 100644 --- a/DiscordBot/Utilities/Console Utilities.cs +++ b/DiscordBot/Utilities/Console Utilities.cs @@ -1,14 +1,12 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; using System.Threading.Tasks; using Spectre.Console; namespace DiscordBot.Utilities; -public static class ConsoleUtilities +internal static class ConsoleUtilities { + public static async Task ExecuteWithProgressBar(Task function, string message) { T result = default; @@ -28,4 +26,5 @@ public static class ConsoleUtilities return result; } + } \ No newline at end of file diff --git a/DiscordBotCore/Interfaces/Logger/ILogger.cs b/DiscordBotCore/Interfaces/Logger/ILogger.cs index 4df4e41..6c3e463 100644 --- a/DiscordBotCore/Interfaces/Logger/ILogger.cs +++ b/DiscordBotCore/Interfaces/Logger/ILogger.cs @@ -11,7 +11,7 @@ namespace DiscordBotCore.Interfaces.Logger public string LogMessageFormat { get; set; } public void Log(ILogMessage message); - public void LogException(Exception exception, object Sender); + public void LogException(Exception exception, object Sender, bool logFullStack = false); public event EventHandler OnFormattedLog; public event EventHandler OnRawLog; diff --git a/DiscordBotCore/Online/PluginManager.cs b/DiscordBotCore/Online/PluginManager.cs index 7d211ce..31167b1 100644 --- a/DiscordBotCore/Online/PluginManager.cs +++ b/DiscordBotCore/Online/PluginManager.cs @@ -1,9 +1,7 @@ using System; -using System.Collections.Frozen; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; using System.Threading.Tasks; using DiscordBotCore.Others; using DiscordBotCore.Plugin; @@ -165,6 +163,9 @@ public class PluginManager File.Delete(dependency.Value); await RemovePluginFromDatabase(pluginInfo.PluginName); + + if (Directory.Exists($"Libraries/{pluginInfo.PluginName}")) + Directory.Delete($"Libraries/{pluginInfo.PluginName}", true); } public async Task GetDependencyLocation(string dependencyName) @@ -177,7 +178,7 @@ public class PluginManager return plugin.ListOfDependancies[dependencyName]; } - throw new Exception("Dependency not found"); + return string.Empty; } public string GenerateDependencyLocation(string pluginName, string dependencyName) diff --git a/DiscordBotCore/Others/Logger/LogMessage.cs b/DiscordBotCore/Others/Logger/LogMessage.cs index b74ff92..a59e811 100644 --- a/DiscordBotCore/Others/Logger/LogMessage.cs +++ b/DiscordBotCore/Others/Logger/LogMessage.cs @@ -71,9 +71,9 @@ namespace DiscordBotCore.Others.Logger return this; } - public static LogMessage CreateFromException(Exception exception, object Sender) + public static LogMessage CreateFromException(Exception exception, object Sender, bool logFullStack) { - LogMessage message = new LogMessage(exception.ToString(), Sender, LogType.ERROR); + LogMessage message = new LogMessage(logFullStack? exception.ToString() : exception.Message, Sender, LogType.ERROR); return message; } } diff --git a/DiscordBotCore/Others/Logger/Logger.cs b/DiscordBotCore/Others/Logger/Logger.cs index 79eb79f..56e74b1 100644 --- a/DiscordBotCore/Others/Logger/Logger.cs +++ b/DiscordBotCore/Others/Logger/Logger.cs @@ -85,5 +85,5 @@ public sealed class Logger : ILogger public void Log(string message, LogType logType) => Log(new LogMessage(message, logType)); public void Log(string message, object Sender) => Log(new LogMessage(message, Sender)); public void Log(string message, object Sender, LogType type) => Log(new LogMessage(message, Sender, type)); - public void LogException(Exception exception, object Sender) => Log(LogMessage.CreateFromException(exception, Sender)); + public void LogException(Exception exception, object Sender, bool logFullStack = false) => Log(LogMessage.CreateFromException(exception, Sender, logFullStack)); } diff --git a/DiscordBotCore/Plugin/OnlineDependencyInfo.cs b/DiscordBotCore/Plugin/OnlineDependencyInfo.cs index 36afdd5..b1e678d 100644 --- a/DiscordBotCore/Plugin/OnlineDependencyInfo.cs +++ b/DiscordBotCore/Plugin/OnlineDependencyInfo.cs @@ -8,12 +8,14 @@ public class OnlineDependencyInfo public string DependencyName { get; private set; } public string DownloadLink { get; private set; } public string DownloadLocation { get; private set; } + public bool IsExecutable { get; private set; } [JsonConstructor] - public OnlineDependencyInfo(string dependencyName, string downloadLink, string downloadLocation) + public OnlineDependencyInfo(string dependencyName, string downloadLink, string downloadLocation, bool isExecutable) { DependencyName = dependencyName; DownloadLink = downloadLink; DownloadLocation = downloadLocation; + IsExecutable = isExecutable; } } diff --git a/DiscordBotCore/Plugin/PluginInfo.cs b/DiscordBotCore/Plugin/PluginInfo.cs index a789b4e..fac7b4f 100644 --- a/DiscordBotCore/Plugin/PluginInfo.cs +++ b/DiscordBotCore/Plugin/PluginInfo.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Linq; using System.Text.Json.Serialization; @@ -37,6 +36,11 @@ public class PluginInfo public static PluginInfo FromOnlineInfo(PluginOnlineInfo onlineInfo) { - return new PluginInfo(onlineInfo.Name, onlineInfo.Version, onlineInfo.Dependencies.Select(dep => new KeyValuePair(dep.DependencyName, dep.DownloadLocation)).ToDictionary()); + return new PluginInfo(onlineInfo.Name, + onlineInfo.Version, + onlineInfo.Dependencies + .Where(dep => dep.IsExecutable) + .Select(dep => new KeyValuePair(dep.DependencyName, dep.DownloadLocation)) + .ToDictionary()); } } diff --git a/DiscordBotCore/Updater/Plugins/PluginUpdater.cs b/DiscordBotCore/Updater/Plugins/PluginUpdater.cs index 3a70346..8187845 100644 --- a/DiscordBotCore/Updater/Plugins/PluginUpdater.cs +++ b/DiscordBotCore/Updater/Plugins/PluginUpdater.cs @@ -43,7 +43,7 @@ public class PluginUpdater await ServerCom.DownloadFileAsync(pluginInfo.DownLoadLink, $"{DiscordBotCore.Application.CurrentApplication.ApplicationEnvironmentVariables["PluginFolder"]}/{pluginName}.dll", progressMeter); foreach(OnlineDependencyInfo dependency in pluginInfo.Dependencies) - await ServerCom.DownloadFileAsync(dependency.DownloadLocation, dependency.DownloadLocation, progressMeter); + await ServerCom.DownloadFileAsync(dependency.DownloadLink, dependency.DownloadLocation, progressMeter); await _PluginsManager.RemovePluginFromDatabase(pluginName); await _PluginsManager.AppendPluginToDatabase(PluginInfo.FromOnlineInfo(pluginInfo));