patch for UI

This commit is contained in:
2022-06-02 13:48:56 +03:00
parent ab6f14e74c
commit 0d524cdf65
21 changed files with 265 additions and 359 deletions

View File

@@ -1,99 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using PluginManager.Interfaces;
namespace PluginManager.Loaders
{
internal class CommandsLoader
{
private readonly string CMDPath;
private readonly string CMDExtension;
internal delegate void onCommandLoaded(string name, bool success, DBCommand? command = null, Exception? exception = null);
internal delegate void onCommandFileLoaded(string path);
/// <summary>
/// Event fired when a command is loaded
/// </summary>
internal onCommandLoaded? OnCommandLoaded;
/// <summary>
/// Event fired when the file is loaded
/// </summary>
internal onCommandFileLoaded? OnCommandFileLoaded;
/// <summary>
/// Command Loader contructor
/// </summary>
/// <param name="CommandPath">The path to the commands</param>
/// <param name="CommandExtension">The extension to search for in the <paramref name="CommandPath"/></param>
internal CommandsLoader(string CommandPath, string CommandExtension)
{
CMDPath = CommandPath;
CMDExtension = CommandExtension;
}
/// <summary>
/// The method that loads all commands
/// </summary>
/// <returns></returns>
internal List<DBCommand>? LoadCommands()
{
if (!Directory.Exists(CMDPath))
{
Directory.CreateDirectory(CMDPath);
return null;
}
string[] files = Directory.GetFiles(CMDPath, $"*{CMDExtension}", SearchOption.AllDirectories);
foreach (var file in files)
{
Assembly.LoadFile(Path.GetFullPath(file));
if (OnCommandFileLoaded != null)
OnCommandFileLoaded.Invoke(file);
}
List<DBCommand> plugins = new List<DBCommand>();
try
{
Type interfaceType = typeof(DBCommand);
Type[] types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
.ToArray();
foreach (Type type in types)
{
try
{
DBCommand plugin = (DBCommand)Activator.CreateInstance(type)!;
plugins.Add(plugin);
if (OnCommandLoaded != null)
OnCommandLoaded.Invoke(type.FullName!, true, plugin);
}
catch (Exception e)
{
if (OnCommandLoaded != null)
OnCommandLoaded.Invoke(type.FullName!, false, null, e);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
return plugins;
}
}
}

View File

@@ -1,100 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using PluginManager.Interfaces;
namespace PluginManager.Loaders
{
internal class EventsLoader
{
private readonly string EVPath;
private readonly string EVExtension;
internal delegate void onEventLoad(string name, bool success, DBEvent? ev = null, Exception? e = null);
internal delegate void onEventFileLoaded(string path);
/// <summary>
/// An event that is fired whenever a <see cref="DBEvent"/> event is loaded in memory
/// </summary>
internal onEventLoad? EventLoad;
/// <summary>
/// An event that is fired whenever a <see cref="DBEvent"/> event file is loaded
/// </summary>
internal onEventFileLoaded? EventFileLoaded;
/// <summary>
/// The Event Loader constructor
/// </summary>
/// <param name="path">The path to all events</param>
/// <param name="ext">The extension for events</param>
internal EventsLoader(string path, string ext)
{
EVPath = path;
EVExtension = ext;
}
/// <summary>
/// The method that loads all events
/// </summary>
/// <returns></returns>
internal List<DBEvent>? LoadEvents()
{
if (!Directory.Exists(EVPath))
{
Directory.CreateDirectory(EVPath);
return null;
}
string[] files = Directory.GetFiles(EVPath, $"*{EVExtension}", SearchOption.AllDirectories);
foreach (var file in files)
{
Assembly.LoadFile(Path.GetFullPath(file));
if (EventFileLoaded != null)
EventFileLoaded.Invoke(file);
}
List<DBEvent> events = new List<DBEvent>();
try
{
Type interfaceType = typeof(DBEvent);
Type[] types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
.ToArray();
foreach (Type type in types)
{
try
{
DBEvent ev = (DBEvent)Activator.CreateInstance(type)!;
events.Add(ev);
if (EventLoad != null)
EventLoad.Invoke(type.FullName!, true, ev, null);
}
catch (Exception e)
{
if (EventLoad != null)
EventLoad.Invoke(type.FullName!, false, null, e);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
return events;
}
}
}

View File

@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using PluginManager.Interfaces;
using PluginManager.Others;
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 delegate void FileLoadedEventHandler(LoaderArgs args);
internal event FileLoadedEventHandler? FileLoaded;
internal delegate void PluginLoadedEventHandler(LoaderArgs args);
internal event PluginLoadedEventHandler? PluginLoaded;
private string path { get; }
private string extension { get; }
internal Loader(string path, string extension)
{
this.path = path;
this.extension = extension;
}
internal List<T>? Load()
{
List<T> list = new List<T>();
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
return null;
}
string[] files = Directory.GetFiles(path, $"*.{extension}", SearchOption.AllDirectories);
foreach (var file in files)
{
Assembly.LoadFrom(file);
if (FileLoaded != null)
{
LoaderArgs args = new LoaderArgs() { Exception = null, TypeName = nameof(T), IsLoaded = false, PluginName = file, Plugin = null };
FileLoaded.Invoke(args);
}
}
try
{
Type interfaceType = typeof(T);
Type[] types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
.ToArray();
list.Clear();
foreach (Type type in types)
{
try
{
T plugin = (T)(Activator.CreateInstance(type)!);
list.Add(plugin);
if (PluginLoaded != null) { PluginLoaded.Invoke(new() { Exception = null, IsLoaded = true, PluginName = type.FullName, TypeName = nameof(T), Plugin = plugin }); }
}
catch (Exception ex)
{
if (PluginLoaded != null) { PluginLoaded.Invoke(new() { Exception = ex, IsLoaded = false, PluginName = type.FullName, TypeName = nameof(T) }); }
}
}
}
catch (Exception ex) { Functions.WriteErrFile(ex.ToString()); }
return list;
}
}
}

View File

@@ -1,35 +1,32 @@
using Discord.WebSocket;
using PluginManager.Interfaces;
using PluginManager.Others;
using System;
using System.Collections.Generic;
namespace PluginManager.Loaders
{
public class PluginLoader
{
private DiscordSocketClient client;
private readonly DiscordSocketClient _client;
/// <summary>
/// The Plugin Loader constructor
/// </summary>
/// <param name="discordSocketClient">The discord bot client where the plugins will pe attached to</param>
public PluginLoader(DiscordSocketClient discordSocketClient)
{
this.client = discordSocketClient;
}
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";
private const string pluginCMDExtension = "dll";
private const string pluginEVEExtension = "dll";
/// <summary>
/// A list of <see cref="DBCommand"/> commands
/// </summary>
public static List<DBCommand>? Plugins { get; set; }
public static List<DBCommand>? Commands { get; set; }
/// <summary>
/// A list of <see cref="DBEvent"/> commands
@@ -38,6 +35,7 @@ namespace PluginManager.Loaders
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);
/// <summary>
@@ -55,11 +53,10 @@ namespace PluginManager.Loaders
/// </summary>
public void LoadPlugins()
{
Commands = new List<DBCommand>();
Events = new List<DBEvent>();
Plugins = new List<DBCommand>();
Events = new List<DBEvent>();
Functions.WriteLogFile("Starting plugin loader ... Client: " + client.CurrentUser.Username);
Functions.WriteLogFile("Starting plugin loader ... Client: " + _client.CurrentUser.Username);
if (LanguageSystem.Language.ActiveLanguage != null)
Console_Utilities.WriteColorText(
LanguageSystem.Language.ActiveLanguage.FormatText(
@@ -67,45 +64,40 @@ namespace PluginManager.Loaders
)
);
//Load commands
CommandsLoader CMDLoader = new CommandsLoader(pluginCMDFolder, pluginCMDExtension);
CMDLoader.OnCommandLoaded += OnCommandLoaded!;
CMDLoader.OnCommandFileLoaded += OnCommandFileLoaded;
Plugins = CMDLoader.LoadCommands();
Loader<DBCommand> commandsLoader = new Loader<DBCommand>(pluginCMDFolder, pluginCMDExtension);
Loader<DBEvent> eventsLoader = new Loader<DBEvent>(pluginEVEFolder, pluginEVEExtension);
//Load Events
EventsLoader EVLoader = new EventsLoader(pluginEVEFolder, pluginEVEExtension);
EVLoader.EventLoad += OnEventLoaded!;
EVLoader.EventFileLoaded += EventFileLoaded;
Events = EVLoader.LoadEvents();
commandsLoader.FileLoaded += OnCommandFileLoaded;
commandsLoader.PluginLoaded += OnCommandLoaded;
eventsLoader.FileLoaded += EventFileLoaded;
eventsLoader.PluginLoaded += OnEventLoaded;
Commands = commandsLoader.Load();
Events = eventsLoader.Load();
}
private void EventFileLoaded(string path)
private void EventFileLoaded(LoaderArgs e)
{
if (path != null)
Functions.WriteLogFile($"[EVENT] Event from file [{path}] has been successfully created !");
if (e.IsLoaded) Functions.WriteLogFile($"[EVENT] Event from file [{e.PluginName}] has been successfully created !");
}
private void OnCommandFileLoaded(string path)
private void OnCommandFileLoaded(LoaderArgs e)
{
if (path != null)
Functions.WriteLogFile($"[CMD] Command from file [{path}] has been successfully loaded !");
if (e.IsLoaded) Functions.WriteLogFile($"[CMD] Command from file [{e.PluginName}] has been successfully loaded !");
}
private void OnEventLoaded(string typename, bool success, DBEvent eve, Exception exception)
private void OnEventLoaded(LoaderArgs e)
{
if (eve != null && success)
eve.Start(client);
if (onEVELoad != null)
onEVELoad.Invoke(eve!.name, typename, success, exception);
if (e.IsLoaded) { ((DBEvent)e.Plugin!).Start(_client); }
if (onEVELoad != null) onEVELoad.Invoke(((DBEvent)e.Plugin!).name, e.TypeName!, e.IsLoaded, e.Exception);
}
private void OnCommandLoaded(string name, bool success, DBCommand command, Exception exception)
private void OnCommandLoaded(LoaderArgs e)
{
if (onCMDLoad != null)
onCMDLoad.Invoke(command.Command, name, success, exception);
if (onCMDLoad != null) onCMDLoad.Invoke(((DBCommand)e.Plugin!).Command, e.TypeName!, e.IsLoaded, e.Exception);
}
}
}