Updated Logger and Plugin Loading

This commit is contained in:
2024-06-20 18:23:48 +03:00
parent 86b951f50f
commit 68886fa5f0
11 changed files with 37 additions and 25 deletions

View File

@@ -22,7 +22,7 @@ namespace DiscordBot.Bot.Actions
public string Usage => "add-plugin <path>"; public string Usage => "add-plugin <path>";
public IEnumerable<InternalActionOption> ListOfOptions => [ public IEnumerable<InternalActionOption> ListOfOptions => [
new InternalActionOption("path", "The path to the plugin to add") new InternalActionOption("fileName", "The file name")
]; ];
public InternalActionRunType RunType => InternalActionRunType.ON_CALL; public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
@@ -32,13 +32,14 @@ namespace DiscordBot.Bot.Actions
if(args.Length < 1) if(args.Length < 1)
{ {
Console.WriteLine("Invalid arguments given. Please use the following format:"); Console.WriteLine("Invalid arguments given. Please use the following format:");
Console.WriteLine("add-plugin <path>"); Console.WriteLine("add-plugin <fileName>");
Console.WriteLine("path: The path to the plugin to add"); Console.WriteLine("fileName: The file name");
return; return;
} }
var path = args[0]; string fileName = args[0] + ".dll";
var path = Application.GetPluginFullPath(fileName);
if(!System.IO.File.Exists(path)) if(!System.IO.File.Exists(path))
{ {
@@ -47,7 +48,7 @@ namespace DiscordBot.Bot.Actions
} }
FileInfo fileInfo = new FileInfo(path); FileInfo fileInfo = new FileInfo(path);
PluginInfo pluginInfo = new PluginInfo(fileInfo.Name, new(1, 0, 0), [], false, true); PluginInfo pluginInfo = new PluginInfo(args[0], new(1, 0, 0), [], false, true);
await Application.CurrentApplication.PluginManager.AppendPluginToDatabase(pluginInfo); await Application.CurrentApplication.PluginManager.AppendPluginToDatabase(pluginInfo);
} }
} }

View File

@@ -105,10 +105,9 @@ public class Program
_ => "[white]" _ => "[white]"
}; };
if (logMessage.Message.Contains('[')) if (logMessage.Message.Contains('[') || logMessage.Message.Contains(']'))
{ {
Console.WriteLine(logMessage.Message); logMessage.Message = logMessage.Message.Replace("[", "<").Replace("]", ">");
return;
} }
string messageToPrint = $"{messageColor}{logMessage.Message}[/]"; string messageToPrint = $"{messageColor}{logMessage.Message}[/]";

View File

@@ -111,5 +111,13 @@ namespace DiscordBotCore
} }
public static string GetResourceFullPath() => _ResourcesFolder; public static string GetResourceFullPath() => _ResourcesFolder;
public static string GetPluginFullPath(string path)
{
string result = Path.Combine(_PluginsFolder, path);
return result;
}
public static string GetPluginFullPath() => _PluginsFolder;
} }
} }

View File

@@ -19,6 +19,4 @@ public interface ICommandAction
public InternalActionRunType RunType { get; } public InternalActionRunType RunType { get; }
public Task Execute(string[]? args); public Task Execute(string[]? args);
public void ExecuteStartup() { }
} }

View File

@@ -67,6 +67,9 @@ internal class Loader
_ => PluginType.UNKNOWN _ => PluginType.UNKNOWN
}; };
if (pluginType == PluginType.UNKNOWN)
throw new Exception($"Unknown plugin type for plugin with type {type.FullName} [{type.Assembly}]");
OnPluginLoaded?.Invoke(new PluginLoadResultData(type.FullName, pluginType, true, plugin: plugin)); OnPluginLoaded?.Invoke(new PluginLoadResultData(type.FullName, pluginType, true, plugin: plugin));
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -72,7 +72,7 @@ public class PluginLoader
case PluginType.ACTION: case PluginType.ACTION:
ICommandAction action = (ICommandAction)result.Plugin; ICommandAction action = (ICommandAction)result.Plugin;
if (action.RunType == InternalActionRunType.ON_STARTUP || action.RunType == InternalActionRunType.BOTH) if (action.RunType == InternalActionRunType.ON_STARTUP || action.RunType == InternalActionRunType.BOTH)
action.ExecuteStartup(); action.Execute(null);
if(action.RunType == InternalActionRunType.ON_CALL || action.RunType == InternalActionRunType.BOTH) if(action.RunType == InternalActionRunType.ON_CALL || action.RunType == InternalActionRunType.BOTH)
Actions.Add(action); Actions.Add(action);

View File

@@ -6,6 +6,7 @@ namespace DiscordBotCore.Others.Logger
{ {
internal sealed class LogMessage : ILogMessage internal sealed class LogMessage : ILogMessage
{ {
private static readonly string _DefaultLogMessageSender = "\b";
public string Message { get; set; } public string Message { get; set; }
public DateTime ThrowTime { get; set; } public DateTime ThrowTime { get; set; }
public string SenderName { get; set; } public string SenderName { get; set; }
@@ -22,7 +23,7 @@ namespace DiscordBotCore.Others.Logger
public LogMessage(string message, object sender) public LogMessage(string message, object sender)
{ {
Message = message; Message = message;
SenderName = sender.GetType().FullName ?? sender.GetType().Name; SenderName = sender is string && sender as string == string.Empty ? _DefaultLogMessageSender : sender.GetType().FullName ?? sender.GetType().Name;
ThrowTime = DateTime.Now; ThrowTime = DateTime.Now;
LogMessageType = LogType.INFO; LogMessageType = LogType.INFO;
} }
@@ -30,7 +31,7 @@ namespace DiscordBotCore.Others.Logger
public LogMessage(string message, object sender, DateTime throwTime) public LogMessage(string message, object sender, DateTime throwTime)
{ {
Message = message; Message = message;
SenderName = sender.GetType().FullName ?? sender.GetType().Name; SenderName = sender is string && sender as string == string.Empty ? _DefaultLogMessageSender : sender.GetType().FullName ?? sender.GetType().Name;
ThrowTime = throwTime; ThrowTime = throwTime;
LogMessageType = LogType.INFO; LogMessageType = LogType.INFO;
} }
@@ -38,7 +39,7 @@ namespace DiscordBotCore.Others.Logger
public LogMessage(string message, object sender, LogType logMessageType) public LogMessage(string message, object sender, LogType logMessageType)
{ {
Message = message; Message = message;
SenderName = sender.GetType().FullName ?? sender.GetType().Name; SenderName = sender is string && sender as string == string.Empty ? _DefaultLogMessageSender : sender.GetType().FullName ?? sender.GetType().Name;
ThrowTime = DateTime.Now; ThrowTime = DateTime.Now;
LogMessageType = logMessageType; LogMessageType = logMessageType;
@@ -48,7 +49,7 @@ namespace DiscordBotCore.Others.Logger
{ {
Message = message; Message = message;
ThrowTime = throwTime; ThrowTime = throwTime;
SenderName = sender.GetType().FullName ?? sender.GetType().Name; SenderName = sender is string && sender as string == string.Empty ? _DefaultLogMessageSender : sender.GetType().FullName ?? sender.GetType().Name;
LogMessageType = logMessageType; LogMessageType = logMessageType;
} }

View File

@@ -80,6 +80,7 @@ public sealed class Logger : ILogger
LogToFile(messageAsString); LogToFile(messageAsString);
} }
public void Log(string message) => Log(new LogMessage(message, string.Empty, LogType.INFO));
public void Log(string message, LogType logType, string format) => Log(new LogMessage(message, logType), format); public void Log(string message, LogType logType, string format) => Log(new LogMessage(message, logType), format);
public void Log(string message, LogType logType) => Log(new LogMessage(message, logType)); public void Log(string message, LogType logType) => Log(new LogMessage(message, logType));
public void Log(string message, object Sender) => Log(new LogMessage(message, Sender)); public void Log(string message, object Sender) => Log(new LogMessage(message, Sender));

View File

@@ -23,18 +23,12 @@ namespace DiscordBotUI
public InternalActionRunType RunType => InternalActionRunType.BOTH; public InternalActionRunType RunType => InternalActionRunType.BOTH;
public async void ExecuteStartup()
{
Directory.CreateDirectory(Path.Combine(DiscordBotCore.Application.CurrentApplication.DataFolder, "DiscordBotUI"));
await Execute(["start"]);
}
public async Task Execute(string[]? args) public async Task Execute(string[]? args)
{ {
if (args == null || args.Length == 0) if (args == null || args.Length == 0)
{ {
Console.WriteLine("Please provide an option"); Directory.CreateDirectory(Path.Combine(DiscordBotCore.Application.GetResourceFullPath(), "DiscordBotUI"));
return; await StartUI();
} }
if (args[0] == "theme") if (args[0] == "theme")

View File

@@ -85,7 +85,7 @@ namespace DiscordBotUI_Windows
if (theme == null) if (theme == null)
throw new Exception("Theme not found"); throw new Exception("Theme not found");
string basefolderPath = Path.Combine(DiscordBotCore.Application.CurrentApplication.DataFolder, _ThemesFolder); string basefolderPath = DiscordBotCore.Application.GetResourceFullPath(_ThemesFolder);
Directory.CreateDirectory(basefolderPath); Directory.CreateDirectory(basefolderPath);
string filePath = Path.Combine(basefolderPath, $"{themeName}.json"); string filePath = Path.Combine(basefolderPath, $"{themeName}.json");
await DiscordBotCore.Others.JsonManager.SaveToJsonFile(filePath, theme); await DiscordBotCore.Others.JsonManager.SaveToJsonFile(filePath, theme);
@@ -93,7 +93,7 @@ namespace DiscordBotUI_Windows
internal async Task<int> LoadThemesFromThemesFolder() internal async Task<int> LoadThemesFromThemesFolder()
{ {
string basefolderPath = Path.Combine(DiscordBotCore.Application.CurrentApplication.DataFolder, _ThemesFolder); string basefolderPath = DiscordBotCore.Application.GetResourceFullPath(_ThemesFolder);
Directory.CreateDirectory(basefolderPath); Directory.CreateDirectory(basefolderPath);
var files = Directory.GetFiles(basefolderPath, "*.json"); var files = Directory.GetFiles(basefolderPath, "*.json");
_InstalledThemes.Clear(); _InstalledThemes.Clear();

View File

@@ -14,6 +14,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MusicPlayer", "..\SethPlugi
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LevelingSystem", "..\SethPlugins\LevelingSystem\LevelingSystem.csproj", "{39EF14F8-7E67-4C14-A4F1-E706CFF61192}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LevelingSystem", "..\SethPlugins\LevelingSystem\LevelingSystem.csproj", "{39EF14F8-7E67-4C14-A4F1-E706CFF61192}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CppWrapper", "..\SethPlugins\CppWrapper\CppWrapper.csproj", "{2E526F88-368C-4BE5-9B46-69EE3D311E9B}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -40,6 +42,10 @@ Global
{39EF14F8-7E67-4C14-A4F1-E706CFF61192}.Debug|Any CPU.Build.0 = Debug|Any CPU {39EF14F8-7E67-4C14-A4F1-E706CFF61192}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39EF14F8-7E67-4C14-A4F1-E706CFF61192}.Release|Any CPU.ActiveCfg = Release|Any CPU {39EF14F8-7E67-4C14-A4F1-E706CFF61192}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39EF14F8-7E67-4C14-A4F1-E706CFF61192}.Release|Any CPU.Build.0 = Release|Any CPU {39EF14F8-7E67-4C14-A4F1-E706CFF61192}.Release|Any CPU.Build.0 = Release|Any CPU
{2E526F88-368C-4BE5-9B46-69EE3D311E9B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E526F88-368C-4BE5-9B46-69EE3D311E9B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E526F88-368C-4BE5-9B46-69EE3D311E9B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E526F88-368C-4BE5-9B46-69EE3D311E9B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -47,6 +53,7 @@ Global
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{F094D29D-6F1A-46BC-8B1F-88F3D98FCA84} = {8FAEFF5E-F749-435C-BB7B-D57C0F8B17FC} {F094D29D-6F1A-46BC-8B1F-88F3D98FCA84} = {8FAEFF5E-F749-435C-BB7B-D57C0F8B17FC}
{39EF14F8-7E67-4C14-A4F1-E706CFF61192} = {8FAEFF5E-F749-435C-BB7B-D57C0F8B17FC} {39EF14F8-7E67-4C14-A4F1-E706CFF61192} = {8FAEFF5E-F749-435C-BB7B-D57C0F8B17FC}
{2E526F88-368C-4BE5-9B46-69EE3D311E9B} = {8FAEFF5E-F749-435C-BB7B-D57C0F8B17FC}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3FB3C5DE-ED21-4D2E-ABDD-3A00EE4A2FFF} SolutionGuid = {3FB3C5DE-ED21-4D2E-ABDD-3A00EE4A2FFF}