Formatted code and rebuilt PluginLoader
This commit is contained in:
@@ -106,7 +106,7 @@ public class Boot
|
||||
if (arg.Message.Contains("401"))
|
||||
{
|
||||
Config.AppSettings.Remove("token");
|
||||
Config.Logger.Log("The token is invalid. Please restart the bot and enter a valid token.", source: typeof(Boot), type: LogType.CRITICAL);
|
||||
Config.Logger.Log("The token is invalid. Please restart the bot and enter a valid token.", typeof(Boot), LogType.CRITICAL);
|
||||
await Config.AppSettings.SaveToFile();
|
||||
await Task.Delay(4000);
|
||||
Environment.Exit(0);
|
||||
@@ -115,7 +115,7 @@ public class Boot
|
||||
|
||||
private async Task Client_LoggedOut()
|
||||
{
|
||||
Config.Logger.Log("Successfully Logged Out", source: typeof(Boot));
|
||||
Config.Logger.Log("Successfully Logged Out", typeof(Boot));
|
||||
await Log(new LogMessage(LogSeverity.Info, "Boot", "Successfully logged out from discord !"));
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ public class Boot
|
||||
|
||||
private Task LoggedIn()
|
||||
{
|
||||
Config.Logger.Log("Successfully Logged In", source: typeof(Boot));
|
||||
Config.Logger.Log("Successfully Logged In", typeof(Boot));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -138,12 +138,12 @@ public class Boot
|
||||
{
|
||||
case LogSeverity.Error:
|
||||
case LogSeverity.Critical:
|
||||
Config.Logger.Log(message.Message, source: typeof(Boot), type: LogType.ERROR);
|
||||
Config.Logger.Log(message.Message, typeof(Boot), LogType.ERROR);
|
||||
break;
|
||||
|
||||
case LogSeverity.Info:
|
||||
case LogSeverity.Debug:
|
||||
Config.Logger.Log(message.Message, source: typeof(Boot), type: LogType.INFO);
|
||||
Config.Logger.Log(message.Message, typeof(Boot), LogType.INFO);
|
||||
|
||||
|
||||
break;
|
||||
|
||||
@@ -25,9 +25,9 @@ internal class CommandHandler
|
||||
/// <param name="botPrefix">The prefix to watch for</param>
|
||||
public CommandHandler(DiscordSocketClient client, CommandService commandService, string botPrefix)
|
||||
{
|
||||
this._client = client;
|
||||
this._commandService = commandService;
|
||||
this._botPrefix = botPrefix;
|
||||
_client = client;
|
||||
_commandService = commandService;
|
||||
_botPrefix = botPrefix;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -103,12 +103,10 @@ internal class CommandHandler
|
||||
.FirstOrDefault(plug => plug.Command ==
|
||||
message.Content.Substring(mentionPrefix.Length + 1)
|
||||
.Split(' ')[0] ||
|
||||
(
|
||||
plug.Aliases is not null &&
|
||||
plug.Aliases.Contains(message.CleanContent
|
||||
.Substring(mentionPrefix.Length + 1)
|
||||
.Split(' ')[0]
|
||||
)
|
||||
plug.Aliases is not null &&
|
||||
plug.Aliases.Contains(message.CleanContent
|
||||
.Substring(mentionPrefix.Length + 1)
|
||||
.Split(' ')[0]
|
||||
)
|
||||
);
|
||||
|
||||
@@ -120,11 +118,11 @@ internal class CommandHandler
|
||||
plugin = PluginLoader.Commands!
|
||||
.FirstOrDefault(p => p.Command ==
|
||||
message.Content.Split(' ')[0].Substring(_botPrefix.Length) ||
|
||||
(p.Aliases is not null &&
|
||||
p.Aliases.Contains(
|
||||
message.Content.Split(' ')[0]
|
||||
.Substring(_botPrefix.Length)
|
||||
))
|
||||
p.Aliases is not null &&
|
||||
p.Aliases.Contains(
|
||||
message.Content.Split(' ')[0]
|
||||
.Substring(_botPrefix.Length)
|
||||
)
|
||||
);
|
||||
cleanMessage = message.Content.Substring(_botPrefix.Length);
|
||||
}
|
||||
@@ -144,9 +142,9 @@ internal class CommandHandler
|
||||
DbCommandExecutingArguments cmd = new(context, cleanMessage, split[0], argsClean);
|
||||
|
||||
Config.Logger.Log(
|
||||
message: $"User ({context.User.Username}) from Guild \"{context.Guild.Name}\" executed command \"{cmd.cleanContent}\"",
|
||||
source: typeof(CommandHandler),
|
||||
type: LogType.INFO
|
||||
$"User ({context.User.Username}) from Guild \"{context.Guild.Name}\" executed command \"{cmd.cleanContent}\"",
|
||||
typeof(CommandHandler),
|
||||
LogType.INFO
|
||||
);
|
||||
|
||||
if (context.Channel is SocketDMChannel)
|
||||
|
||||
@@ -29,8 +29,8 @@ public class Config
|
||||
|
||||
AppSettings = new SettingsDictionary<string, string>("./Data/Resources/config.json");
|
||||
|
||||
AppSettings["LogFolder"] = "./Data/Logs";
|
||||
AppSettings["PluginFolder"] = "./Data/Plugins";
|
||||
AppSettings["LogFolder"] = "./Data/Logs";
|
||||
AppSettings["PluginFolder"] = "./Data/Plugins";
|
||||
AppSettings["ArchiveFolder"] = "./Data/Archives";
|
||||
|
||||
if (OperatingSystem.IsLinux())
|
||||
@@ -42,21 +42,22 @@ public class Config
|
||||
"GNOME" => "GNOME",
|
||||
_ => "CONSOLE"
|
||||
};
|
||||
} else AppSettings["UI"] = "CONSOLE";
|
||||
}
|
||||
else AppSettings["UI"] = "CONSOLE";
|
||||
|
||||
Logger = new Logger(false, true,
|
||||
AppSettings["LogFolder"] + $"/{DateTime.Today.ToShortDateString().Replace("/", "")}.log"
|
||||
);
|
||||
|
||||
ArchiveManager.Initialize();
|
||||
|
||||
|
||||
UX.UxHandler.Init();
|
||||
_isLoaded = true;
|
||||
|
||||
|
||||
Logger.Log(message: "Config initialized", source: typeof(Config));
|
||||
|
||||
|
||||
|
||||
|
||||
Logger.Log("Config initialized", typeof(Config));
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -544,8 +544,10 @@ public class SqlDatabase
|
||||
/// </summary>
|
||||
/// <param name="parameterValues">The parameter raw inputs. The Key is name and the Value is the value of the parameter</param>
|
||||
/// <returns>The SQLiteParameter that has the name, value and DBType set according to your inputs</returns>
|
||||
private SQLiteParameter? CreateParameter(KeyValuePair<string, object> parameterValues) =>
|
||||
CreateParameter(parameterValues.Key, parameterValues.Value);
|
||||
private SQLiteParameter? CreateParameter(KeyValuePair<string, object> parameterValues)
|
||||
{
|
||||
return CreateParameter(parameterValues.Key, parameterValues.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute a query with parameters
|
||||
|
||||
@@ -5,10 +5,12 @@ public interface IVersion
|
||||
public int Major { get; }
|
||||
public int Minor { get; }
|
||||
public int Patch { get; }
|
||||
|
||||
|
||||
public bool IsNewerThan(IVersion version);
|
||||
|
||||
public bool IsOlderThan(IVersion version);
|
||||
|
||||
public bool IsEqualTo(IVersion version);
|
||||
|
||||
|
||||
public string ToShortString();
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ using System;
|
||||
|
||||
namespace PluginManager.Interfaces.Updater;
|
||||
|
||||
public abstract class Version : IVersion
|
||||
public abstract class Version: IVersion
|
||||
{
|
||||
public int Major { get; }
|
||||
public int Minor { get; }
|
||||
@@ -12,34 +12,34 @@ public abstract class Version : IVersion
|
||||
|
||||
protected Version(int major, int minor, int patch)
|
||||
{
|
||||
this.Major = major;
|
||||
this.Minor = minor;
|
||||
this.Patch = patch;
|
||||
Major = major;
|
||||
Minor = minor;
|
||||
Patch = patch;
|
||||
}
|
||||
|
||||
protected Version(string versionAsString)
|
||||
{
|
||||
string[] versionParts = versionAsString.Split(_Separator);
|
||||
|
||||
|
||||
if (versionParts.Length != 3)
|
||||
{
|
||||
throw new ArgumentException("Invalid version string");
|
||||
}
|
||||
|
||||
this.Major = int.Parse(versionParts[0]);
|
||||
this.Minor = int.Parse(versionParts[1]);
|
||||
this.Patch = int.Parse(versionParts[2]);
|
||||
|
||||
Major = int.Parse(versionParts[0]);
|
||||
Minor = int.Parse(versionParts[1]);
|
||||
Patch = int.Parse(versionParts[2]);
|
||||
}
|
||||
|
||||
|
||||
public bool IsNewerThan(IVersion version)
|
||||
{
|
||||
if (this.Major > version.Major)
|
||||
if (Major > version.Major)
|
||||
return true;
|
||||
|
||||
if (this.Major == version.Major && this.Minor > version.Minor)
|
||||
if (Major == version.Major && Minor > version.Minor)
|
||||
return true;
|
||||
|
||||
if (this.Major == version.Major && this.Minor == version.Minor && this.Patch > version.Patch)
|
||||
if (Major == version.Major && Minor == version.Minor && Patch > version.Patch)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
@@ -47,13 +47,13 @@ public abstract class Version : IVersion
|
||||
|
||||
public bool IsOlderThan(IVersion version)
|
||||
{
|
||||
if (this.Major < version.Major)
|
||||
if (Major < version.Major)
|
||||
return true;
|
||||
|
||||
if (this.Major == version.Major && this.Minor < version.Minor)
|
||||
if (Major == version.Major && Minor < version.Minor)
|
||||
return true;
|
||||
|
||||
if (this.Major == version.Major && this.Minor == version.Minor && this.Patch < version.Patch)
|
||||
if (Major == version.Major && Minor == version.Minor && Patch < version.Patch)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
@@ -61,9 +61,9 @@ public abstract class Version : IVersion
|
||||
|
||||
public bool IsEqualTo(IVersion version)
|
||||
{
|
||||
return this.Major == version.Major && this.Minor == version.Minor && this.Patch == version.Patch;
|
||||
return Major == version.Major && Minor == version.Minor && Patch == version.Patch;
|
||||
}
|
||||
|
||||
|
||||
public string ToShortString()
|
||||
{
|
||||
return $"{Major}.{Minor}.{Patch}";
|
||||
|
||||
20
PluginManager/Loaders/FileLoaderResult.cs
Normal file
20
PluginManager/Loaders/FileLoaderResult.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace PluginManager.Loaders;
|
||||
|
||||
public class FileLoaderResult
|
||||
{
|
||||
public string PluginName { get; init; }
|
||||
|
||||
public string? ErrorMessage { get; init; }
|
||||
|
||||
|
||||
public FileLoaderResult(string pluginName, string errorMessage)
|
||||
{
|
||||
PluginName = pluginName;
|
||||
ErrorMessage = errorMessage;
|
||||
}
|
||||
|
||||
public FileLoaderResult(string pluginName)
|
||||
{
|
||||
PluginName = pluginName;
|
||||
}
|
||||
}
|
||||
@@ -1,52 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
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
|
||||
{
|
||||
internal Loader(string path, string extension)
|
||||
private readonly string _SearchPath;
|
||||
private readonly string _FileExtension;
|
||||
|
||||
internal delegate void FileLoadedHandler(FileLoaderResult result);
|
||||
|
||||
internal delegate void PluginLoadedHandler(PluginLoadResultData result);
|
||||
|
||||
internal event FileLoadedHandler? OnFileLoadedException;
|
||||
internal event PluginLoadedHandler? OnPluginLoaded;
|
||||
|
||||
internal Loader(string searchPath, string fileExtension)
|
||||
{
|
||||
this.Path = path;
|
||||
this.Extension = extension;
|
||||
_SearchPath = searchPath;
|
||||
_FileExtension = fileExtension;
|
||||
}
|
||||
|
||||
|
||||
private string Path { get; }
|
||||
private string Extension { get; }
|
||||
|
||||
internal event FileLoadedEventHandler? FileLoaded;
|
||||
|
||||
internal event PluginLoadedEventHandler? PluginLoaded;
|
||||
|
||||
|
||||
internal (List<DBEvent>?, List<DBCommand>?, List<DBSlashCommand>?) Load()
|
||||
internal async Task Load()
|
||||
{
|
||||
List<DBEvent> events = new();
|
||||
List<DBSlashCommand> slashCommands = new();
|
||||
List<DBCommand> commands = new();
|
||||
|
||||
if (!Directory.Exists(Path))
|
||||
if (!Directory.Exists(_SearchPath))
|
||||
{
|
||||
Directory.CreateDirectory(Path);
|
||||
return (null, null, null);
|
||||
Directory.CreateDirectory(_SearchPath);
|
||||
return;
|
||||
}
|
||||
|
||||
var files = Directory.GetFiles(Path, $"*.{Extension}", SearchOption.AllDirectories);
|
||||
var files = Directory.GetFiles(_SearchPath, $"*.{_FileExtension}", SearchOption.TopDirectoryOnly);
|
||||
foreach (var file in files)
|
||||
{
|
||||
try
|
||||
@@ -55,91 +43,40 @@ internal class Loader
|
||||
}
|
||||
catch
|
||||
{
|
||||
Config.Logger.Log("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded", source: typeof(Loader), type: LogType.ERROR);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (FileLoaded != null)
|
||||
{
|
||||
var args = new LoaderArgs
|
||||
{
|
||||
Exception = null,
|
||||
TypeName = null,
|
||||
IsLoaded = false,
|
||||
PluginName = new FileInfo(file).Name.Split('.')[0],
|
||||
Plugin = null
|
||||
};
|
||||
FileLoaded.Invoke(args);
|
||||
OnFileLoadedException?.Invoke(new FileLoaderResult(file, "Failed to load file"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return (LoadItems<DBEvent>(), LoadItems<DBCommand>(), LoadItems<DBSlashCommand>());
|
||||
await LoadEverythingOfType<DBEvent>();
|
||||
await LoadEverythingOfType<DBCommand>();
|
||||
await LoadEverythingOfType<DBSlashCommand>();
|
||||
}
|
||||
|
||||
internal List<T> LoadItems<T>()
|
||||
private async Task LoadEverythingOfType<T>()
|
||||
{
|
||||
List<T> list = new();
|
||||
var types = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(s => s.GetTypes())
|
||||
.Where(p => typeof(T).IsAssignableFrom(p) && !p.IsInterface);
|
||||
|
||||
|
||||
try
|
||||
foreach (var type in types)
|
||||
{
|
||||
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
|
||||
try
|
||||
{
|
||||
var plugin = (T)Activator.CreateInstance(type);
|
||||
var pluginType = plugin switch
|
||||
{
|
||||
var plugin = (T)Activator.CreateInstance(type)!;
|
||||
list.Add(plugin);
|
||||
|
||||
|
||||
if (PluginLoaded != null)
|
||||
PluginLoaded.Invoke(new LoaderArgs
|
||||
{
|
||||
Exception = null,
|
||||
IsLoaded = true,
|
||||
PluginName = type.FullName,
|
||||
TypeName = typeof(T) == typeof(DBCommand) ? "DBCommand" :
|
||||
typeof(T) == typeof(DBEvent) ? "DBEvent" :
|
||||
typeof(T) == typeof(DBSlashCommand) ? "DBSlashCommand" :
|
||||
null,
|
||||
Plugin = plugin
|
||||
}
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (PluginLoaded != null)
|
||||
PluginLoaded.Invoke(new LoaderArgs
|
||||
{
|
||||
Exception = ex,
|
||||
IsLoaded = false,
|
||||
PluginName = type.FullName,
|
||||
TypeName = nameof(T)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return list;
|
||||
DBEvent => PluginType.EVENT,
|
||||
DBCommand => PluginType.COMMAND,
|
||||
DBSlashCommand => PluginType.SLASH_COMMAND,
|
||||
_ => PluginType.UNKNOWN
|
||||
};
|
||||
OnPluginLoaded?.Invoke(new PluginLoadResultData(type.FullName, pluginType, true, plugin: plugin));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
OnPluginLoaded?.Invoke(new PluginLoadResultData(type.FullName, PluginType.UNKNOWN, false, ex.Message));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log(ex.Message, source: typeof(Loader), type: LogType.ERROR);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
internal delegate void FileLoadedEventHandler(LoaderArgs args);
|
||||
|
||||
internal delegate void PluginLoadedEventHandler(LoaderArgs args);
|
||||
}
|
||||
|
||||
6
PluginManager/Loaders/PluginHandler.cs
Normal file
6
PluginManager/Loaders/PluginHandler.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace PluginManager.Loaders;
|
||||
|
||||
public class PluginHandler
|
||||
{
|
||||
|
||||
}
|
||||
22
PluginManager/Loaders/PluginLoadResultData.cs
Normal file
22
PluginManager/Loaders/PluginLoadResultData.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using PluginManager.Others;
|
||||
|
||||
namespace PluginManager.Loaders;
|
||||
|
||||
public class PluginLoadResultData
|
||||
{
|
||||
public string PluginName { get; init; }
|
||||
public PluginType PluginType { get; init; }
|
||||
public string? ErrorMessage { get; init; }
|
||||
public bool IsSuccess { get; init; }
|
||||
|
||||
public object? Plugin { get; init; }
|
||||
|
||||
public PluginLoadResultData(string pluginName, PluginType pluginType, bool isSuccess, string? errorMessage = null, object plugin = null)
|
||||
{
|
||||
PluginName = pluginName;
|
||||
PluginType = pluginType;
|
||||
IsSuccess = isSuccess;
|
||||
ErrorMessage = errorMessage;
|
||||
Plugin = plugin;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
using PluginManager.Interfaces;
|
||||
using PluginManager.Others;
|
||||
@@ -11,173 +8,78 @@ namespace PluginManager.Loaders;
|
||||
|
||||
public class PluginLoader
|
||||
{
|
||||
public delegate void CMDLoaded(string name, string typeName, bool success, Exception? e = null);
|
||||
internal readonly DiscordSocketClient _Client;
|
||||
|
||||
public delegate void EVELoaded(string name, string typeName, bool success, Exception? e = null);
|
||||
public delegate void CommandLoaded(PluginLoadResultData resultData);
|
||||
|
||||
public delegate void SLSHLoaded(string name, string tyypename, bool success, Exception? e = null);
|
||||
public delegate void EventLoaded(PluginLoadResultData resultData);
|
||||
|
||||
private const string pluginFolder = @"./Data/Plugins/";
|
||||
public delegate void SlashCommandLoaded(PluginLoadResultData resultData);
|
||||
|
||||
internal const string pluginExtension = "dll";
|
||||
private readonly DiscordSocketClient _client;
|
||||
public CommandLoaded? OnCommandLoaded;
|
||||
public EventLoaded? OnEventLoaded;
|
||||
public SlashCommandLoaded? OnSlashCommandLoaded;
|
||||
|
||||
/// <summary>
|
||||
/// Event that is fired when a <see cref="DBCommand" /> is successfully loaded into commands list
|
||||
/// </summary>
|
||||
public CMDLoaded? onCMDLoad;
|
||||
public static List<DBCommand>? Commands;
|
||||
public static List<DBEvent>? Events;
|
||||
public static List<DBSlashCommand>? SlashCommands;
|
||||
|
||||
/// <summary>
|
||||
/// Event that is fired when a <see cref="DBEvent" /> is successfully loaded into events list
|
||||
/// </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>
|
||||
/// <param name="discordSocketClient">The discord bot client where the plugins will pe attached to</param>
|
||||
public PluginLoader(DiscordSocketClient discordSocketClient)
|
||||
{
|
||||
_client = discordSocketClient;
|
||||
_Client = discordSocketClient;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A list of <see cref="DBCommand" /> commands
|
||||
/// </summary>
|
||||
public static List<DBCommand>? Commands { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A list of <see cref="DBEvent" /> commands
|
||||
/// </summary>
|
||||
public static List<DBEvent>? Events { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A list of <see cref="DBSlashCommand" /> commands
|
||||
/// </summary>
|
||||
public static List<DBSlashCommand>? SlashCommands { get; set; }
|
||||
|
||||
public static int PluginsLoaded
|
||||
public async Task LoadPlugins()
|
||||
{
|
||||
get
|
||||
{
|
||||
var count = 0;
|
||||
if (Commands is not null)
|
||||
count += Commands.Count;
|
||||
if (Events is not null)
|
||||
count += Events.Count;
|
||||
if (SlashCommands is not null)
|
||||
count += SlashCommands.Count;
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The main mathod that is called to load all events
|
||||
/// </summary>
|
||||
public async void LoadPlugins()
|
||||
{
|
||||
//Load all plugins
|
||||
|
||||
Commands = new List<DBCommand>();
|
||||
Events = new List<DBEvent>();
|
||||
SlashCommands = new List<DBSlashCommand>();
|
||||
|
||||
Config.Logger.Log("Starting plugin loader ... Client: " + _client.CurrentUser.Username, source: typeof(PluginLoader), type: LogType.INFO);
|
||||
Config.Logger.Log("Loading plugins...", typeof(PluginLoader));
|
||||
|
||||
var loader = new Loader("./Data/Plugins", "dll");
|
||||
loader.FileLoaded += args => Config.Logger.Log($"{args.PluginName} file Loaded", source: typeof(PluginLoader), type: LogType.INFO);
|
||||
loader.PluginLoaded += Loader_PluginLoaded;
|
||||
var res = loader.Load();
|
||||
Events = res.Item1;
|
||||
Commands = res.Item2;
|
||||
SlashCommands = res.Item3;
|
||||
var loader = new Loader(Config.AppSettings["PluginFolder"], "dll");
|
||||
|
||||
loader.OnFileLoadedException += FileLoadedException;
|
||||
loader.OnPluginLoaded += OnPluginLoaded;
|
||||
|
||||
await loader.Load();
|
||||
}
|
||||
|
||||
private async void Loader_PluginLoaded(LoaderArgs args)
|
||||
private void FileLoadedException(FileLoaderResult result)
|
||||
{
|
||||
switch (args.TypeName)
|
||||
Config.Logger.Log(result.ErrorMessage, typeof(PluginLoader), LogType.ERROR);
|
||||
}
|
||||
|
||||
private void OnPluginLoaded(PluginLoadResultData result)
|
||||
{
|
||||
switch (result.PluginType)
|
||||
{
|
||||
case "DBCommand":
|
||||
onCMDLoad?.Invoke(((DBCommand)args.Plugin!).Command, args.TypeName!, args.IsLoaded, args.Exception);
|
||||
case PluginType.COMMAND:
|
||||
Commands.Add((DBCommand)result.Plugin);
|
||||
OnCommandLoaded?.Invoke(result);
|
||||
break;
|
||||
case "DBEvent":
|
||||
try
|
||||
case PluginType.EVENT:
|
||||
if (this.TryStartEvent((DBEvent)result.Plugin))
|
||||
{
|
||||
if (args.IsLoaded)
|
||||
((DBEvent)args.Plugin!).Start(_client);
|
||||
|
||||
onEVELoad?.Invoke(((DBEvent)args.Plugin!).Name, args.TypeName!, args.IsLoaded, args.Exception);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log(ex.Message, source: typeof(PluginLoader), type: LogType.ERROR);
|
||||
Events.Add((DBEvent)result.Plugin);
|
||||
OnEventLoaded?.Invoke(result);
|
||||
}
|
||||
|
||||
break;
|
||||
case "DBSlashCommand":
|
||||
if (args.IsLoaded)
|
||||
case PluginType.SLASH_COMMAND:
|
||||
if (this.TryStartSlashCommand((DBSlashCommand)result.Plugin))
|
||||
{
|
||||
var slash = (DBSlashCommand)args.Plugin;
|
||||
var builder = new SlashCommandBuilder();
|
||||
builder.WithName(slash.Name);
|
||||
builder.WithDescription(slash.Description);
|
||||
builder.WithDMPermission(slash.canUseDM);
|
||||
builder.Options = slash.Options;
|
||||
|
||||
onSLSHLoad?.Invoke(((DBSlashCommand)args.Plugin!).Name, args.TypeName, args.IsLoaded,
|
||||
args.Exception
|
||||
);
|
||||
await _client.CreateGlobalApplicationCommandAsync(builder.Build());
|
||||
SlashCommands.Add((DBSlashCommand)result.Plugin);
|
||||
OnSlashCommandLoaded?.Invoke(result);
|
||||
}
|
||||
|
||||
break;
|
||||
case PluginType.UNKNOWN:
|
||||
default:
|
||||
Config.Logger.Log("Unknown plugin type", typeof(PluginLoader), LogType.ERROR);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task LoadPluginFromAssembly(Assembly asmb, DiscordSocketClient client)
|
||||
{
|
||||
var types = asmb.GetTypes();
|
||||
foreach (var type in types)
|
||||
try
|
||||
{
|
||||
if (type.IsClass && typeof(DBEvent).IsAssignableFrom(type))
|
||||
{
|
||||
var instance = (DBEvent)Activator.CreateInstance(type);
|
||||
instance.Start(client);
|
||||
Events.Add(instance);
|
||||
Config.Logger.Log($"[EVENT] Loaded external {type.FullName}!", source: typeof(PluginLoader));
|
||||
}
|
||||
else if (type.IsClass && typeof(DBCommand).IsAssignableFrom(type))
|
||||
{
|
||||
var instance = (DBCommand)Activator.CreateInstance(type);
|
||||
Commands.Add(instance);
|
||||
Config.Logger.Log($"[CMD] Instance: {type.FullName} loaded !", source: typeof(PluginLoader));
|
||||
}
|
||||
else if (type.IsClass && typeof(DBSlashCommand).IsAssignableFrom(type))
|
||||
{
|
||||
var instance = (DBSlashCommand)Activator.CreateInstance(type);
|
||||
var builder = new SlashCommandBuilder();
|
||||
builder.WithName(instance.Name);
|
||||
builder.WithDescription(instance.Description);
|
||||
builder.WithDMPermission(instance.canUseDM);
|
||||
builder.Options = instance.Options;
|
||||
|
||||
await client.CreateGlobalApplicationCommandAsync(builder.Build());
|
||||
SlashCommands.Add(instance);
|
||||
Config.Logger.Log($"[SLASH] Instance: {type.FullName} loaded !", source: typeof(PluginLoader));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//Console.WriteLine(ex.Message);
|
||||
Config.Logger.Log(ex.Message, source: typeof(PluginLoader), type: LogType.ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
54
PluginManager/Loaders/PluginLoaderExtensions.cs
Normal file
54
PluginManager/Loaders/PluginLoaderExtensions.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using Discord;
|
||||
using PluginManager.Interfaces;
|
||||
using PluginManager.Others;
|
||||
|
||||
namespace PluginManager.Loaders;
|
||||
|
||||
internal static class PluginLoaderExtensions
|
||||
{
|
||||
internal static bool TryStartEvent(this PluginLoader pluginLoader, DBEvent? dbEvent)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (dbEvent is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(dbEvent));
|
||||
}
|
||||
|
||||
|
||||
dbEvent.Start(pluginLoader._Client);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Config.Logger.Log($"Error starting event {dbEvent.Name}: {e.Message}", typeof(PluginLoader), LogType.ERROR);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool TryStartSlashCommand(this PluginLoader pluginLoader, DBSlashCommand? dbSlashCommand)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (dbSlashCommand is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(dbSlashCommand));
|
||||
}
|
||||
|
||||
var builder = new SlashCommandBuilder();
|
||||
builder.WithName(dbSlashCommand.Name);
|
||||
builder.WithDescription(dbSlashCommand.Description);
|
||||
builder.WithDMPermission(dbSlashCommand.canUseDM);
|
||||
builder.Options = dbSlashCommand.Options;
|
||||
|
||||
pluginLoader._Client.CreateGlobalApplicationCommandAsync(builder.Build());
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Config.Logger.Log($"Error starting slash command {dbSlashCommand.Name}: {e.Message}", typeof(PluginLoader), LogType.ERROR);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ using PluginManager.Interfaces.Updater;
|
||||
|
||||
namespace PluginManager.Online.Helpers;
|
||||
|
||||
public class ApplicationVersion : Version
|
||||
public class ApplicationVersion: Version
|
||||
{
|
||||
|
||||
public ApplicationVersion(int major, int minor, int patch): base(major, minor, patch)
|
||||
@@ -11,6 +11,6 @@ public class ApplicationVersion : Version
|
||||
public ApplicationVersion(string versionAsString): base(versionAsString)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ using PluginManager.Interfaces.Updater;
|
||||
|
||||
namespace PluginManager.Online.Helpers;
|
||||
|
||||
public class PluginVersion : Version
|
||||
public class PluginVersion: Version
|
||||
{
|
||||
[JsonConstructor]
|
||||
public PluginVersion(int major, int minor, int patch): base(major, minor, patch)
|
||||
|
||||
@@ -9,53 +9,54 @@ namespace PluginManager.Online;
|
||||
|
||||
public class PluginsManager
|
||||
{
|
||||
private static readonly string _DefaultBranch = "releases";
|
||||
private static readonly string _DefaultBranch = "releases";
|
||||
private static readonly string _DefaultBaseUrl = "https://raw.githubusercontent.com/andreitdr/SethPlugins";
|
||||
|
||||
|
||||
private static readonly string _DefaultPluginsLink = "PluginsList.json";
|
||||
|
||||
|
||||
|
||||
|
||||
public string Branch { get; init; }
|
||||
public string BaseUrl { get; init; }
|
||||
|
||||
|
||||
|
||||
|
||||
private string PluginsLink => $"{BaseUrl}/{Branch}/{_DefaultPluginsLink}";
|
||||
|
||||
|
||||
public PluginsManager(Uri baseUrl, string branch)
|
||||
{
|
||||
this.BaseUrl = baseUrl.ToString();
|
||||
this.Branch = branch;
|
||||
BaseUrl = baseUrl.ToString();
|
||||
Branch = branch;
|
||||
}
|
||||
|
||||
|
||||
public PluginsManager(string branch)
|
||||
{
|
||||
this.BaseUrl = _DefaultBaseUrl;
|
||||
this.Branch = branch;
|
||||
BaseUrl = _DefaultBaseUrl;
|
||||
Branch = branch;
|
||||
}
|
||||
|
||||
|
||||
public PluginsManager()
|
||||
{
|
||||
this.BaseUrl = _DefaultBaseUrl;
|
||||
this.Branch = _DefaultBranch;
|
||||
BaseUrl = _DefaultBaseUrl;
|
||||
Branch = _DefaultBranch;
|
||||
}
|
||||
|
||||
public async Task<List<PluginOnlineInfo?>> GetPluginsList()
|
||||
{
|
||||
string jsonText = await ServerCom.GetAllTextFromUrl(PluginsLink);
|
||||
List<PluginOnlineInfo?> result = await JsonManager.ConvertFromJson<List<PluginOnlineInfo?>>(jsonText);
|
||||
|
||||
OSType currentOS = OperatingSystem.IsWindows() ? OSType.WINDOWS : OperatingSystem.IsLinux() ? OSType.LINUX : OSType.MACOSX;
|
||||
var jsonText = await ServerCom.GetAllTextFromUrl(PluginsLink);
|
||||
List<PluginOnlineInfo?> result = await JsonManager.ConvertFromJson<List<PluginOnlineInfo?>>(jsonText);
|
||||
|
||||
var currentOS = OperatingSystem.IsWindows() ? OSType.WINDOWS :
|
||||
OperatingSystem.IsLinux() ? OSType.LINUX : OSType.MACOSX;
|
||||
|
||||
return result.FindAll(pl => (pl.SupportedOS & currentOS) != 0);
|
||||
}
|
||||
|
||||
public async Task<PluginOnlineInfo?> GetPluginDataByName(string pluginName)
|
||||
{
|
||||
List<PluginOnlineInfo?> plugins = await GetPluginsList();
|
||||
PluginOnlineInfo? result = plugins.Find(p => p.Name == pluginName);
|
||||
List<PluginOnlineInfo?> plugins = await GetPluginsList();
|
||||
var result = plugins.Find(p => p.Name == pluginName);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ public static class ServerCom
|
||||
var lines = response.Split('\n');
|
||||
return lines.ToList();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get all text from a file async
|
||||
/// </summary>
|
||||
|
||||
@@ -13,7 +13,7 @@ public static class ArchiveManager
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
if (IsInitialized)
|
||||
if (IsInitialized)
|
||||
throw new Exception("ArchiveManager is already initialized");
|
||||
|
||||
if (!Config.AppSettings.ContainsKey("ArchiveFolder"))
|
||||
@@ -89,7 +89,7 @@ public static class ArchiveManager
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log(message: ex.Message, source: typeof(ArchiveManager), type: LogType.ERROR); // Write the error to a file
|
||||
Config.Logger.Log(ex.Message, typeof(ArchiveManager), LogType.ERROR); // Write the error to a file
|
||||
await Task.Delay(100);
|
||||
return await ReadFromPakAsync(fileName, archFile);
|
||||
}
|
||||
@@ -126,7 +126,7 @@ public static class ArchiveManager
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log(ex.Message, source: typeof(ArchiveManager), type: LogType.ERROR);
|
||||
Config.Logger.Log(ex.Message, typeof(ArchiveManager), LogType.ERROR);
|
||||
}
|
||||
|
||||
currentZipFile++;
|
||||
@@ -158,7 +158,7 @@ public static class ArchiveManager
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log(ex.Message, source: typeof(ArchiveManager), type: LogType.ERROR);
|
||||
Config.Logger.Log(ex.Message, typeof(ArchiveManager), LogType.ERROR);
|
||||
}
|
||||
|
||||
await Task.Delay(10);
|
||||
|
||||
@@ -17,26 +17,26 @@ public class DbCommandExecutingArguments
|
||||
|
||||
public DbCommandExecutingArguments(SocketUserMessage? message, DiscordSocketClient client)
|
||||
{
|
||||
this.context = new SocketCommandContext(client, message);
|
||||
int pos = 0;
|
||||
context = new SocketCommandContext(client, message);
|
||||
var pos = 0;
|
||||
if (message.HasMentionPrefix(client.CurrentUser, ref pos))
|
||||
{
|
||||
var mentionPrefix = "<@" + client.CurrentUser.Id + ">";
|
||||
this.cleanContent = message.Content.Substring(mentionPrefix.Length + 1);
|
||||
cleanContent = message.Content.Substring(mentionPrefix.Length + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.cleanContent = message.Content.Substring(Config.DiscordBot.botPrefix.Length);
|
||||
cleanContent = message.Content.Substring(Config.DiscordBot.botPrefix.Length);
|
||||
}
|
||||
|
||||
var split = this.cleanContent.Split(' ');
|
||||
var split = cleanContent.Split(' ');
|
||||
|
||||
string[]? argsClean = null;
|
||||
if (split.Length > 1)
|
||||
argsClean = string.Join(' ', split, 1, split.Length - 1).Split(' ');
|
||||
|
||||
this.commandUsed = split[0];
|
||||
this.arguments = argsClean;
|
||||
commandUsed = split[0];
|
||||
arguments = argsClean;
|
||||
}
|
||||
|
||||
public SocketCommandContext context { get; init; }
|
||||
|
||||
@@ -32,10 +32,18 @@ public enum InternalActionRunType
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum OSType : byte
|
||||
public enum OSType: byte
|
||||
{
|
||||
NONE = 0,
|
||||
NONE = 0,
|
||||
WINDOWS = 1 << 0,
|
||||
LINUX = 2 << 1,
|
||||
MACOSX = 3 << 2,
|
||||
}
|
||||
LINUX = 2 << 1,
|
||||
MACOSX = 3 << 2
|
||||
}
|
||||
|
||||
public enum PluginType
|
||||
{
|
||||
UNKNOWN,
|
||||
COMMAND,
|
||||
EVENT,
|
||||
SLASH_COMMAND
|
||||
}
|
||||
|
||||
@@ -18,7 +18,11 @@ public class JsonManager
|
||||
public static async Task SaveToJsonFile<T>(string file, T Data)
|
||||
{
|
||||
var str = new MemoryStream();
|
||||
await JsonSerializer.SerializeAsync(str, Data, typeof(T), new JsonSerializerOptions { WriteIndented = true });
|
||||
await JsonSerializer.SerializeAsync(str, Data, typeof(T), new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true
|
||||
}
|
||||
);
|
||||
await File.WriteAllBytesAsync(file, str.ToArray());
|
||||
await str.FlushAsync();
|
||||
str.Close();
|
||||
|
||||
@@ -43,9 +43,15 @@ public class Log: ILog
|
||||
ThrowTime = DateTime.Now;
|
||||
}
|
||||
|
||||
public static implicit operator Log(string message) => new(message);
|
||||
public static implicit operator Log(string message)
|
||||
{
|
||||
return new Log(message);
|
||||
}
|
||||
|
||||
public static implicit operator string(Log log) => $"[{log.ThrowTime}] {log.Message}";
|
||||
public static implicit operator string(Log log)
|
||||
{
|
||||
return $"[{log.ThrowTime}] {log.Message}";
|
||||
}
|
||||
|
||||
public string AsLongString()
|
||||
{
|
||||
|
||||
@@ -33,7 +33,7 @@ public class SettingsDictionary<TKey, TValue>: IDictionary<TKey, TValue>
|
||||
{
|
||||
if (File.Exists(_file))
|
||||
{
|
||||
string FileContent = File.ReadAllText(_file);
|
||||
var FileContent = File.ReadAllText(_file);
|
||||
if (string.IsNullOrEmpty(FileContent))
|
||||
File.WriteAllText(_file, "{}");
|
||||
|
||||
@@ -66,62 +66,62 @@ public class SettingsDictionary<TKey, TValue>: IDictionary<TKey, TValue>
|
||||
|
||||
public void Add(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
this._dictionary!.Add(item);
|
||||
_dictionary!.Add(item);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
this._dictionary!.Clear();
|
||||
_dictionary!.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
return this._dictionary!.Contains(item);
|
||||
return _dictionary!.Contains(item);
|
||||
}
|
||||
|
||||
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||
{
|
||||
this._dictionary!.CopyTo(array, arrayIndex);
|
||||
_dictionary!.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
return this._dictionary!.Remove(item);
|
||||
return _dictionary!.Remove(item);
|
||||
}
|
||||
|
||||
public int Count => _dictionary!.Count;
|
||||
public bool IsReadOnly => _dictionary!.IsReadOnly;
|
||||
public void Add(TKey key, TValue value)
|
||||
{
|
||||
this._dictionary!.Add(key, value);
|
||||
_dictionary!.Add(key, value);
|
||||
}
|
||||
|
||||
public bool ContainsKey(TKey key)
|
||||
{
|
||||
return this._dictionary!.ContainsKey(key);
|
||||
return _dictionary!.ContainsKey(key);
|
||||
}
|
||||
|
||||
public bool Remove(TKey key)
|
||||
{
|
||||
return this._dictionary!.Remove(key);
|
||||
return _dictionary!.Remove(key);
|
||||
}
|
||||
|
||||
public bool TryGetValue(TKey key, out TValue value)
|
||||
{
|
||||
return this._dictionary!.TryGetValue(key, out value);
|
||||
return _dictionary!.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
public TValue this[TKey key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._dictionary!.ContainsKey(key))
|
||||
if (this._dictionary[key] is string s && !string.IsNullOrEmpty(s) && !string.IsNullOrWhiteSpace(s))
|
||||
return this._dictionary[key];
|
||||
if (_dictionary!.ContainsKey(key))
|
||||
if (_dictionary[key] is string s && !string.IsNullOrEmpty(s) && !string.IsNullOrWhiteSpace(s))
|
||||
return _dictionary[key];
|
||||
|
||||
return default!;
|
||||
}
|
||||
set => this._dictionary![key] = value;
|
||||
set => _dictionary![key] = value;
|
||||
}
|
||||
|
||||
public ICollection<TKey> Keys => _dictionary!.Keys;
|
||||
|
||||
@@ -4,10 +4,10 @@ public class OnlineDependencyInfo
|
||||
{
|
||||
public string DownloadLink { get; private set; }
|
||||
public string DownloadLocation { get; private set; }
|
||||
|
||||
|
||||
public OnlineDependencyInfo(string downloadLink, string downloadLocation)
|
||||
{
|
||||
DownloadLink = downloadLink;
|
||||
DownloadLink = downloadLink;
|
||||
DownloadLocation = downloadLocation;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,17 +6,17 @@ namespace PluginManager.Plugin;
|
||||
public class PluginInfo
|
||||
{
|
||||
public string PluginName { get; private set; }
|
||||
public IVersion PluginVersion { get; private set; }
|
||||
public IVersion PluginVersion { get; private set; }
|
||||
public FileInfo FileData { get; private set; }
|
||||
|
||||
|
||||
public PluginInfo(string pluginName, IVersion pluginVersion)
|
||||
{
|
||||
PluginName = pluginName;
|
||||
PluginName = pluginName;
|
||||
PluginVersion = pluginVersion;
|
||||
|
||||
|
||||
FileData = new FileInfo($"{Config.AppSettings["PluginFolder"]}/{pluginName}.dll");
|
||||
}
|
||||
|
||||
|
||||
public static PluginInfo FromOnlineInfo(PluginOnlineInfo onlineInfo)
|
||||
{
|
||||
return new PluginInfo(onlineInfo.Name, onlineInfo.Version);
|
||||
|
||||
@@ -15,35 +15,35 @@ public class PluginOnlineInfo
|
||||
public List<OnlineDependencyInfo> Dependencies { get; private set; }
|
||||
public OSType SupportedOS { get; private set; }
|
||||
public bool HasDependencies { get; init; }
|
||||
|
||||
|
||||
[JsonConstructor]
|
||||
public PluginOnlineInfo(string name, PluginVersion version, string description, string downLoadLink, OSType supportedOS, List<OnlineDependencyInfo> dependencies)
|
||||
{
|
||||
Name = name;
|
||||
Version = version;
|
||||
Description = description;
|
||||
DownLoadLink = downLoadLink;
|
||||
SupportedOS = supportedOS;
|
||||
Dependencies = dependencies;
|
||||
Name = name;
|
||||
Version = version;
|
||||
Description = description;
|
||||
DownLoadLink = downLoadLink;
|
||||
SupportedOS = supportedOS;
|
||||
Dependencies = dependencies;
|
||||
HasDependencies = dependencies.Count > 0;
|
||||
}
|
||||
|
||||
|
||||
public PluginOnlineInfo(string name, PluginVersion version, string description, string downLoadLink, OSType supportedOS)
|
||||
{
|
||||
Name = name;
|
||||
Version = version;
|
||||
Description = description;
|
||||
DownLoadLink = downLoadLink;
|
||||
SupportedOS = supportedOS;
|
||||
Dependencies = new List<OnlineDependencyInfo>();
|
||||
Name = name;
|
||||
Version = version;
|
||||
Description = description;
|
||||
DownLoadLink = downLoadLink;
|
||||
SupportedOS = supportedOS;
|
||||
Dependencies = new List<OnlineDependencyInfo>();
|
||||
HasDependencies = false;
|
||||
}
|
||||
|
||||
|
||||
public static async Task<PluginOnlineInfo> FromRawData(string jsonText)
|
||||
{
|
||||
return await JsonManager.ConvertFromJson<PluginOnlineInfo>(jsonText);
|
||||
}
|
||||
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Name} - {Version} ({Description})";
|
||||
|
||||
@@ -12,17 +12,17 @@
|
||||
<None Remove="BlankWindow1.xaml"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia" Version="11.0.6" />
|
||||
<PackageReference Include="Avalonia" Version="11.0.6"/>
|
||||
<PackageReference Include="Discord.Net" Version="3.11.0"/>
|
||||
<PackageReference Include="Spectre.Console" Version="0.47.0" />
|
||||
<PackageReference Include="Spectre.Console" Version="0.47.0"/>
|
||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.118"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<UpToDateCheckInput Remove="UI\Controls\MessageBox.axaml" />
|
||||
<UpToDateCheckInput Remove="UI\Controls\MessageBox.axaml"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Spectre.Console">
|
||||
<HintPath>..\..\..\.nuget\packages\spectre.console\0.47.0\lib\net7.0\Spectre.Console.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Spectre.Console">
|
||||
<HintPath>..\..\..\.nuget\packages\spectre.console\0.47.0\lib\net7.0\Spectre.Console.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -13,17 +13,19 @@ public enum MessageBoxButtons
|
||||
{
|
||||
YesNo,
|
||||
YesNoCancel,
|
||||
ContinueCancel,
|
||||
ContinueCancel
|
||||
}
|
||||
|
||||
internal interface IOutputModel
|
||||
{
|
||||
|
||||
|
||||
internal Task ShowMessageBox(string title, string message, MessageBoxType type);
|
||||
|
||||
internal Task<string> ShowInputBox(string title, string message);
|
||||
|
||||
internal Task ShowMessageBox(string message);
|
||||
|
||||
internal Task<int> ShowMessageBox(string title, string message, MessageBoxButtons buttons, bool isWarning);
|
||||
|
||||
|
||||
internal Task ShowNotification(string title, string message, int timeout_seconds = 5);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.UX.Linux;
|
||||
|
||||
internal class KDE : IOutputModel
|
||||
internal class KDE: IOutputModel
|
||||
{
|
||||
internal string KdeDialogApplication { get; } = "kdialog";
|
||||
|
||||
@@ -11,15 +11,15 @@ internal class KDE : IOutputModel
|
||||
{
|
||||
var process = new Process();
|
||||
process.StartInfo.FileName = KdeDialogApplication;
|
||||
|
||||
string typeStr = type switch
|
||||
|
||||
var typeStr = type switch
|
||||
{
|
||||
MessageBoxType.Info => "msgbox",
|
||||
MessageBoxType.Info => "msgbox",
|
||||
MessageBoxType.Warning => "sorry",
|
||||
MessageBoxType.Error => "error",
|
||||
_ => "info"
|
||||
MessageBoxType.Error => "error",
|
||||
_ => "info"
|
||||
};
|
||||
|
||||
|
||||
process.StartInfo.Arguments = $"--title \"{title}\" --{typeStr} \"{message}\"";
|
||||
process.Start();
|
||||
await process.WaitForExitAsync();
|
||||
@@ -28,48 +28,48 @@ internal class KDE : IOutputModel
|
||||
public async Task<string> ShowInputBox(string title, string message)
|
||||
{
|
||||
var process = new Process();
|
||||
process.StartInfo.FileName = KdeDialogApplication;
|
||||
process.StartInfo.Arguments = $"--title \"{title}\" --inputbox \"{message}\"";
|
||||
process.StartInfo.FileName = KdeDialogApplication;
|
||||
process.StartInfo.Arguments = $"--title \"{title}\" --inputbox \"{message}\"";
|
||||
process.StartInfo.RedirectStandardOutput = true;
|
||||
process.StartInfo.RedirectStandardInput = true;
|
||||
process.StartInfo.RedirectStandardInput = true;
|
||||
process.Start();
|
||||
|
||||
|
||||
await process.WaitForExitAsync();
|
||||
return await process.StandardOutput.ReadToEndAsync();
|
||||
}
|
||||
|
||||
|
||||
public async Task ShowMessageBox(string message)
|
||||
{
|
||||
var process = new Process();
|
||||
process.StartInfo.FileName = KdeDialogApplication;
|
||||
process.StartInfo.FileName = KdeDialogApplication;
|
||||
process.StartInfo.Arguments = $"--msgbox \"{message}\"";
|
||||
process.Start();
|
||||
await process.WaitForExitAsync();
|
||||
}
|
||||
|
||||
|
||||
public async Task<int> ShowMessageBox(string title, string message, MessageBoxButtons buttons, bool isWarning)
|
||||
{
|
||||
var process = new Process();
|
||||
process.StartInfo.FileName = KdeDialogApplication;
|
||||
|
||||
string buttonsStr = buttons switch
|
||||
|
||||
var buttonsStr = buttons switch
|
||||
{
|
||||
MessageBoxButtons.YesNo => "yesno",
|
||||
MessageBoxButtons.YesNoCancel => "yesnocancel",
|
||||
MessageBoxButtons.YesNo => "yesno",
|
||||
MessageBoxButtons.YesNoCancel => "yesnocancel",
|
||||
MessageBoxButtons.ContinueCancel => "continuecancel",
|
||||
_ => "yesno"
|
||||
_ => "yesno"
|
||||
};
|
||||
string typeStr = isWarning ? "warning" : "";
|
||||
var typeStr = isWarning ? "warning" : "";
|
||||
process.StartInfo.Arguments = $"--title \"{title}\" --{typeStr}{buttonsStr} \"{message}\"";
|
||||
process.Start();
|
||||
await process.WaitForExitAsync();
|
||||
return process.ExitCode;
|
||||
}
|
||||
|
||||
|
||||
public async Task ShowNotification(string title, string message, int timeout_seconds = 5)
|
||||
{
|
||||
var process = new Process();
|
||||
process.StartInfo.FileName = KdeDialogApplication;
|
||||
process.StartInfo.FileName = KdeDialogApplication;
|
||||
process.StartInfo.Arguments = $"--title \"{title}\" --passivepopup \"{message}\" {timeout_seconds}";
|
||||
process.Start();
|
||||
await process.WaitForExitAsync();
|
||||
|
||||
@@ -1,45 +1,43 @@
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console;
|
||||
|
||||
namespace PluginManager.UX.Other;
|
||||
|
||||
|
||||
internal class Console : IOutputModel
|
||||
internal class Console: IOutputModel
|
||||
{
|
||||
|
||||
public Task ShowMessageBox(string title, string message, MessageBoxType type)
|
||||
{
|
||||
AnsiConsole.Markup(title);
|
||||
AnsiConsole.Markup(message);
|
||||
|
||||
|
||||
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<string> ShowInputBox(string title, string message)
|
||||
{
|
||||
AnsiConsole.MarkupLine(title);
|
||||
|
||||
string input = AnsiConsole.Ask<string>(message);
|
||||
|
||||
|
||||
var input = AnsiConsole.Ask<string>(message);
|
||||
|
||||
return Task.FromResult(input);
|
||||
}
|
||||
|
||||
|
||||
public Task ShowMessageBox(string message)
|
||||
{
|
||||
AnsiConsole.MarkupLine(message);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<int> ShowMessageBox(string title, string message,MessageBoxButtons buttons, bool isWarning)
|
||||
|
||||
public Task<int> ShowMessageBox(string title, string message, MessageBoxButtons buttons, bool isWarning)
|
||||
{
|
||||
AnsiConsole.MarkupLine(title);
|
||||
AnsiConsole.MarkupLine(message);
|
||||
|
||||
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
|
||||
public Task ShowNotification(string title, string message, int timeout_seconds = 5)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
|
||||
@@ -2,10 +2,10 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.UX;
|
||||
|
||||
public static class UxHandler
|
||||
public static class UxHandler
|
||||
{
|
||||
private static IOutputModel? _model;
|
||||
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
_model = Config.AppSettings["UI"] switch
|
||||
@@ -14,32 +14,32 @@ public static class UxHandler
|
||||
"CONSOLE" => new Other.Console(),
|
||||
_ => null
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static async Task ShowMessageBox(string title, string message, MessageBoxType type = MessageBoxType.Info)
|
||||
{
|
||||
await _model.ShowMessageBox(title, message, type);
|
||||
}
|
||||
|
||||
|
||||
public static async Task<string> ShowInputBox(string title, string message)
|
||||
{
|
||||
return await _model.ShowInputBox(title, message);
|
||||
}
|
||||
|
||||
|
||||
public static async Task ShowMessageBox(string message)
|
||||
{
|
||||
await _model.ShowMessageBox(message);
|
||||
}
|
||||
|
||||
|
||||
public static async Task<int> ShowMessageBox(string title, string message, MessageBoxButtons buttons, bool isWarning)
|
||||
{
|
||||
return await _model.ShowMessageBox(title, message, buttons, isWarning);
|
||||
}
|
||||
|
||||
|
||||
public static async Task ShowNotification(string title, string message, int timeout_seconds = 5)
|
||||
{
|
||||
await _model.ShowNotification(title, message, timeout_seconds);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user