This commit is contained in:
2023-02-24 11:36:43 +02:00
parent 9ba4ca43e2
commit 532540b74f
7 changed files with 39 additions and 144 deletions

1
.gitignore vendored
View File

@@ -371,3 +371,4 @@ FodyWeavers.xsd
/DiscordBot.rar /DiscordBot.rar
/DiscordBot/Data/ /DiscordBot/Data/
/DiscordBot/Updater/ /DiscordBot/Updater/
.vscode/

View File

@@ -258,7 +258,7 @@ public class ConsoleCommandsHandler
isDownloading = false; isDownloading = false;
await ExecuteCommad("localload " + name); //await ExecuteCommad("localload " + name);
} }
); );

View File

@@ -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<T>
{
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<T>? Load()
{
var list = new List<T>();
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);
}

View File

@@ -8,9 +8,17 @@ using PluginManager.Interfaces;
namespace PluginManager.Loaders 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.path = path;
this.extension = extension; this.extension = extension;

View File

@@ -97,7 +97,7 @@ public class PluginLoader
Logger.WriteLogFile("Starting plugin loader ... Client: " + _client.CurrentUser.Username); Logger.WriteLogFile("Starting plugin loader ... Client: " + _client.CurrentUser.Username);
Logger.WriteLine("Loading plugins"); 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.FileLoaded += (args) => Logger.WriteLogFile($"{args.PluginName} file Loaded");
loader.PluginLoaded += Loader_PluginLoaded; loader.PluginLoaded += Loader_PluginLoaded;
var res = loader.Load(); var res = loader.Load();

View File

@@ -1,24 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<DebugType>none</DebugType> <DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols> <DebugSymbols>false</DebugSymbols>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Remove="BlankWindow1.xaml" /> <None Remove="BlankWindow1.xaml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Discord.Net" Version="3.8.1" /> <PackageReference Include="Discord.Net" Version="3.8.1" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.117" /> <PackageReference Include="System.Data.SQLite.Core" Version="1.0.117" />
<PackageReference Include="Terminal.Gui" Version="1.8.2" /> <PackageReference Include="Terminal.Gui" Version="1.8.2" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -47,7 +47,7 @@ internal class LevelCommand : DBCommand
public bool requireAdmin => false; 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}'"); object[] user = await Variables.database.ReadDataArrayAsync($"SELECT * FROM Levels WHERE UserID='{context.Message.Author.Id}'");
if (user is null) if (user is null)
@@ -69,6 +69,11 @@ internal class LevelCommand : DBCommand
builder.WithAuthor(context.Message.Author.Mention); builder.WithAuthor(context.Message.Author.Mention);
await context.Channel.SendMessageAsync(embed: builder.Build()); 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) {
//
//}
} }