From 532540b74ff836e9f9f4e9c577dd4eb46b0cb70c Mon Sep 17 00:00:00 2001 From: Tudor Andrei Date: Fri, 24 Feb 2023 11:36:43 +0200 Subject: [PATCH] --- .gitignore | 3 +- PluginManager/Items/ConsoleCommandsHandler.cs | 2 +- PluginManager/Loaders/Loader.cs | 114 ------------------ PluginManager/Loaders/LoaderV2.cs | 12 +- PluginManager/Loaders/PluginLoader.cs | 2 +- PluginManager/PluginManager.csproj | 43 +++---- README.md | 7 +- 7 files changed, 39 insertions(+), 144 deletions(-) delete mode 100644 PluginManager/Loaders/Loader.cs diff --git a/.gitignore b/.gitignore index 36496ba..edb5abd 100644 --- a/.gitignore +++ b/.gitignore @@ -370,4 +370,5 @@ FodyWeavers.xsd /Plugins/ /DiscordBot.rar /DiscordBot/Data/ -/DiscordBot/Updater/ \ No newline at end of file +/DiscordBot/Updater/ +.vscode/ \ No newline at end of file diff --git a/PluginManager/Items/ConsoleCommandsHandler.cs b/PluginManager/Items/ConsoleCommandsHandler.cs index 7cba8b0..266f6b6 100644 --- a/PluginManager/Items/ConsoleCommandsHandler.cs +++ b/PluginManager/Items/ConsoleCommandsHandler.cs @@ -258,7 +258,7 @@ public class ConsoleCommandsHandler isDownloading = false; - await ExecuteCommad("localload " + name); + //await ExecuteCommad("localload " + name); } ); diff --git a/PluginManager/Loaders/Loader.cs b/PluginManager/Loaders/Loader.cs deleted file mode 100644 index 2c61f8d..0000000 --- a/PluginManager/Loaders/Loader.cs +++ /dev/null @@ -1,114 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Reflection; - -namespace PluginManager.Loaders; - -internal class LoaderArgs : EventArgs -{ - internal string? PluginName { get; init; } - internal string? TypeName { get; init; } - internal bool IsLoaded { get; init; } - internal Exception? Exception { get; init; } - internal object? Plugin { get; init; } -} - -internal class Loader -{ - internal Loader(string path, string extension) - { - this.path = path; - this.extension = extension; - } - - - private string path { get; } - private string extension { get; } - - internal event FileLoadedEventHandler? FileLoaded; - - internal event PluginLoadedEventHandler? PluginLoaded; - - internal List? Load() - { - var list = new List(); - if (!Directory.Exists(path)) - { - Directory.CreateDirectory(path); - return null; - } - - var files = Directory.GetFiles(path, $"*.{extension}", SearchOption.AllDirectories); - foreach (var file in files) - { - Assembly.LoadFrom(file); - if (FileLoaded != null) - { - var args = new LoaderArgs - { - Exception = null, - TypeName = nameof(T), - IsLoaded = false, - PluginName = new FileInfo(file).Name.Split('.')[0], - Plugin = null - }; - FileLoaded.Invoke(args); - } - } - - try - { - var interfaceType = typeof(T); - var types = AppDomain.CurrentDomain.GetAssemblies() - .SelectMany(a => a.GetTypes()) - .Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass) - .ToArray(); - - - list.Clear(); - foreach (var type in types) - try - { - var plugin = (T)Activator.CreateInstance(type)!; - list.Add(plugin); - - - if (PluginLoaded != null) - PluginLoaded.Invoke(new LoaderArgs - { - Exception = null, - IsLoaded = true, - PluginName = type.FullName, - TypeName = nameof(T), - Plugin = plugin - } - ); - } - catch (Exception ex) - { - if (PluginLoaded != null) - PluginLoaded.Invoke(new LoaderArgs - { - Exception = ex, - IsLoaded = false, - PluginName = type.FullName, - TypeName = nameof(T) - }); - } - } - catch (Exception ex) - { - Logger.WriteErrFile(ex.ToString()); - } - - - return list; - } - - - internal delegate void FileLoadedEventHandler(LoaderArgs args); - - internal delegate void PluginLoadedEventHandler(LoaderArgs args); -} \ No newline at end of file diff --git a/PluginManager/Loaders/LoaderV2.cs b/PluginManager/Loaders/LoaderV2.cs index 9cbfd84..c16b26e 100644 --- a/PluginManager/Loaders/LoaderV2.cs +++ b/PluginManager/Loaders/LoaderV2.cs @@ -8,9 +8,17 @@ using PluginManager.Interfaces; namespace PluginManager.Loaders { - internal class LoaderV2 + internal class LoaderArgs : EventArgs { - internal LoaderV2(string path, string extension) + internal string? PluginName { get; init; } + internal string? TypeName { get; init; } + internal bool IsLoaded { get; init; } + internal Exception? Exception { get; init; } + internal object? Plugin { get; init; } + } + internal class Loader + { + internal Loader(string path, string extension) { this.path = path; this.extension = extension; diff --git a/PluginManager/Loaders/PluginLoader.cs b/PluginManager/Loaders/PluginLoader.cs index 2b9beba..f04b715 100644 --- a/PluginManager/Loaders/PluginLoader.cs +++ b/PluginManager/Loaders/PluginLoader.cs @@ -97,7 +97,7 @@ public class PluginLoader Logger.WriteLogFile("Starting plugin loader ... Client: " + _client.CurrentUser.Username); Logger.WriteLine("Loading plugins"); - var loader = new LoaderV2("./Data/Plugins", "dll"); + var loader = new Loader("./Data/Plugins", "dll"); loader.FileLoaded += (args) => Logger.WriteLogFile($"{args.PluginName} file Loaded"); loader.PluginLoaded += Loader_PluginLoaded; var res = loader.Load(); diff --git a/PluginManager/PluginManager.csproj b/PluginManager/PluginManager.csproj index 0154859..da82f6f 100644 --- a/PluginManager/PluginManager.csproj +++ b/PluginManager/PluginManager.csproj @@ -1,24 +1,19 @@ - - - - net6.0 - enable - - - - 512 - none - false - - - - - - - - - - - - - + + + net6.0 + enable + + + 512 + none + false + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 029a402..5583792 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ internal class LevelCommand : DBCommand public bool requireAdmin => false; - public async void ExecuteServer(SocketCommandContext context) + public async void ExecuteServer(CmdArgs context) { object[] user = await Variables.database.ReadDataArrayAsync($"SELECT * FROM Levels WHERE UserID='{context.Message.Author.Id}'"); if (user is null) @@ -69,6 +69,11 @@ internal class LevelCommand : DBCommand builder.WithAuthor(context.Message.Author.Mention); await context.Channel.SendMessageAsync(embed: builder.Build()); } + + //Optional method (tell the bot what should it do if the command is executed from a DM channel) + //public async void ExecuteDM(CmdArgs context) { + // + //} }