This commit is contained in:
2022-10-23 20:11:27 +03:00
parent cbaf552e7a
commit 47f88f167f
10 changed files with 337 additions and 241 deletions

View File

@@ -19,11 +19,11 @@ public class PluginLoader
public delegate void EVELoaded(string name, string typeName, bool success, Exception? e = null);
private const string pluginCMDFolder = @"./Data/Plugins/Commands/";
private const string pluginEVEFolder = @"./Data/Plugins/Events/";
public delegate void SLSHLoaded(string name, string tyypename, bool success, Exception? e = null);
internal const string pluginCMDExtension = "dll";
internal const string pluginEVEExtension = "dll";
private const string pluginFolder = @"./Data/Plugins/";
internal const string pluginExtension = "dll";
private readonly DiscordSocketClient _client;
/// <summary>
@@ -36,6 +36,11 @@ public class PluginLoader
/// </summary>
public EVELoaded? onEVELoad;
/// <summary>
/// Event that is fired when a <see cref="DBEvent" /> is successfully loaded into events list
/// </summary>
public SLSHLoaded? onSLSHLoad;
/// <summary>
/// The Plugin Loader constructor
/// </summary>
@@ -67,7 +72,7 @@ public class PluginLoader
public async void LoadPlugins()
{
//Check for updates in commands
foreach (var file in Directory.GetFiles("./Data/Plugins/Commands", $"*.{pluginCMDExtension}",
foreach (var file in Directory.GetFiles("./Data/Plugins/", $"*.{pluginExtension}",
SearchOption.AllDirectories))
await Task.Run(async () =>
{
@@ -83,22 +88,6 @@ public class PluginLoader
await PluginUpdater.Download(name);
});
//Check for updates in events
foreach (var file in Directory.GetFiles("./Data/Plugins/Events", $"*.{pluginEVEExtension}",
SearchOption.AllDirectories))
await Task.Run(async () =>
{
var name = new FileInfo(file).Name.Split('.')[0];
var version = await ServerCom.GetVersionOfPackageFromWeb(name);
if (version is null)
return;
if (!Config.PluginVersionsContainsKey(name))
Config.SetPluginVersion(
name, (version.PackageVersionID + ".0.0"));
if (await PluginUpdater.CheckForUpdates(name))
await PluginUpdater.Download(name);
});
//Save the new config file (after the updates)
@@ -114,81 +103,132 @@ public class PluginLoader
Functions.WriteLogFile("Starting plugin loader ... Client: " + _client.CurrentUser.Username);
Console.WriteLine("Loading plugins");
var commandsLoader = new Loader<DBCommand>(pluginCMDFolder, pluginCMDExtension);
var eventsLoader = new Loader<DBEvent>(pluginEVEFolder, pluginEVEExtension);
var slashLoader = new Loader<DBSlashCommand>("./Data/Plugins/SlashCommands/", "dll");
/* var commandsLoader = new Loader<DBCommand>(pluginCMDFolder, pluginCMDExtension);
var eventsLoader = new Loader<DBEvent>(pluginEVEFolder, pluginEVEExtension);
var slashLoader = new Loader<DBSlashCommand>("./Data/Plugins/SlashCommands/", "dll");
commandsLoader.FileLoaded += OnCommandFileLoaded;
commandsLoader.PluginLoaded += OnCommandLoaded;
commandsLoader.FileLoaded += OnCommandFileLoaded;
commandsLoader.PluginLoaded += OnCommandLoaded;
eventsLoader.FileLoaded += EventFileLoaded;
eventsLoader.PluginLoaded += OnEventLoaded;
eventsLoader.FileLoaded += EventFileLoaded;
eventsLoader.PluginLoaded += OnEventLoaded;
slashLoader.FileLoaded += SlashLoader_FileLoaded;
slashLoader.PluginLoaded += SlashLoader_PluginLoaded;
slashLoader.FileLoaded += SlashLoader_FileLoaded;
slashLoader.PluginLoaded += SlashLoader_PluginLoaded;
Commands = commandsLoader.Load();
Events = eventsLoader.Load();
SlashCommands = slashLoader.Load();
Commands = commandsLoader.Load();
Events = eventsLoader.Load();
SlashCommands = slashLoader.Load();*/
var loader = new LoaderV2("./Data/Plugins", "dll");
loader.FileLoaded += (args) => Functions.WriteLogFile($"{args.PluginName} file Loaded");
loader.PluginLoaded += Loader_PluginLoaded;
var res = loader.Load();
Events = res.Item1;
Commands = res.Item2;
SlashCommands = res.Item3;
}
private async void SlashLoader_PluginLoaded(LoaderArgs args)
private async void Loader_PluginLoaded(LoaderArgs args)
{
if (args.IsLoaded)
// Console.WriteLine(args.TypeName);
switch (args.TypeName)
{
var slash = (DBSlashCommand)args.Plugin;
SlashCommandBuilder builder = new SlashCommandBuilder();
builder.WithName(slash.Name);
builder.WithDescription(slash.Description);
builder.WithDMPermission(slash.canUseDM);
builder.Options = slash.Options;
Console.WriteLine("Loaded " + slash.Name);
await _client.CreateGlobalApplicationCommandAsync(builder.Build());
case "DBCommand":
onCMDLoad?.Invoke(((DBCommand)args.Plugin!).Command, args.TypeName!, args.IsLoaded, args.Exception);
break;
case "DBEvent":
try
{
if (args.IsLoaded)
((DBEvent)args.Plugin!).Start(_client);
onEVELoad?.Invoke(((DBEvent)args.Plugin!).Name, args.TypeName!, args.IsLoaded, args.Exception);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine("Plugin: " + args.PluginName);
Console.WriteLine("Type: " + args.TypeName);
Console.WriteLine("IsLoaded: " + args.IsLoaded);
}
break;
case "DBSlashCommand":
if (args.IsLoaded)
{
var slash = (DBSlashCommand)args.Plugin;
SlashCommandBuilder builder = new SlashCommandBuilder();
builder.WithName(slash.Name);
builder.WithDescription(slash.Description);
builder.WithDMPermission(slash.canUseDM);
builder.Options = slash.Options;
//Console.WriteLine("Loaded " + slash.Name);
onSLSHLoad?.Invoke(((DBSlashCommand)args.Plugin!).Name, args.TypeName, args.IsLoaded, args.Exception);
await _client.CreateGlobalApplicationCommandAsync(builder.Build());
}
else Console.WriteLine("Failed to load " + args.PluginName + "\nException: " + args.Exception.Message);
}
private void SlashLoader_FileLoaded(LoaderArgs args)
{
if (!args.IsLoaded)
Functions.WriteLogFile($"[SLASH] Event from file [{args.PluginName}] has been successfully created !");
}
private void EventFileLoaded(LoaderArgs e)
{
if (!e.IsLoaded)
Functions.WriteLogFile($"[EVENT] Event from file [{e.PluginName}] has been successfully created !");
}
private void OnCommandFileLoaded(LoaderArgs e)
{
if (!e.IsLoaded)
Functions.WriteLogFile($"[CMD] Command from file [{e.PluginName}] has been successfully loaded !");
}
private void OnEventLoaded(LoaderArgs e)
{
try
{
if (e.IsLoaded)
((DBEvent)e.Plugin!).Start(_client);
onEVELoad?.Invoke(((DBEvent)e.Plugin!).Name, e.TypeName!, e.IsLoaded, e.Exception);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine("Plugin: " + e.PluginName);
Console.WriteLine("Type: " + e.TypeName);
Console.WriteLine("IsLoaded: " + e.IsLoaded);
}
//else Console.WriteLine("Failed to load " + args.PluginName + "\nException: " + args.Exception.Message);
break;
}
}
/*
private async void SlashLoader_PluginLoaded(LoaderArgs args)
{
if (args.IsLoaded)
{
var slash = (DBSlashCommand)args.Plugin;
SlashCommandBuilder builder = new SlashCommandBuilder();
builder.WithName(slash.Name);
builder.WithDescription(slash.Description);
builder.WithDMPermission(slash.canUseDM);
builder.Options = slash.Options;
Console.WriteLine("Loaded " + slash.Name);
await _client.CreateGlobalApplicationCommandAsync(builder.Build());
private void OnCommandLoaded(LoaderArgs e)
{
onCMDLoad?.Invoke(((DBCommand)e.Plugin!).Command, e.TypeName!, e.IsLoaded, e.Exception);
}
}
else Console.WriteLine("Failed to load " + args.PluginName + "\nException: " + args.Exception.Message);
}
private void SlashLoader_FileLoaded(LoaderArgs args)
{
if (!args.IsLoaded)
Functions.WriteLogFile($"[SLASH] SlashCommand from file [{args.PluginName}] has been successfully created !");
}
private void EventFileLoaded(LoaderArgs e)
{
if (!e.IsLoaded)
Functions.WriteLogFile($"[EVENT] Event from file [{e.PluginName}] has been successfully created !");
}
private void OnCommandFileLoaded(LoaderArgs e)
{
if (!e.IsLoaded)
Functions.WriteLogFile($"[CMD] Command from file [{e.PluginName}] has been successfully loaded !");
}
private void OnEventLoaded(LoaderArgs e)
{
try
{
if (e.IsLoaded)
((DBEvent)e.Plugin!).Start(_client);
onEVELoad?.Invoke(((DBEvent)e.Plugin!).Name, e.TypeName!, e.IsLoaded, e.Exception);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine("Plugin: " + e.PluginName);
Console.WriteLine("Type: " + e.TypeName);
Console.WriteLine("IsLoaded: " + e.IsLoaded);
}
}
private void OnCommandLoaded(LoaderArgs e)
{
onCMDLoad?.Invoke(((DBCommand)e.Plugin!).Command, e.TypeName!, e.IsLoaded, e.Exception);
}*/
}