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 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;
@@ -32,13 +32,14 @@ namespace DiscordBot.Bot.Actions
if(args.Length < 1)
{
Console.WriteLine("Invalid arguments given. Please use the following format:");
Console.WriteLine("add-plugin <path>");
Console.WriteLine("path: The path to the plugin to add");
Console.WriteLine("add-plugin <fileName>");
Console.WriteLine("fileName: The file name");
return;
}
var path = args[0];
string fileName = args[0] + ".dll";
var path = Application.GetPluginFullPath(fileName);
if(!System.IO.File.Exists(path))
{
@@ -47,7 +48,7 @@ namespace DiscordBot.Bot.Actions
}
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);
}
}

View File

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

View File

@@ -111,5 +111,13 @@ namespace DiscordBotCore
}
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 Task Execute(string[]? args);
public void ExecuteStartup() { }
}

View File

@@ -67,6 +67,9 @@ internal class Loader
_ => 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));
}
catch (Exception ex)

View File

@@ -72,7 +72,7 @@ public class PluginLoader
case PluginType.ACTION:
ICommandAction action = (ICommandAction)result.Plugin;
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)
Actions.Add(action);

View File

@@ -6,6 +6,7 @@ namespace DiscordBotCore.Others.Logger
{
internal sealed class LogMessage : ILogMessage
{
private static readonly string _DefaultLogMessageSender = "\b";
public string Message { get; set; }
public DateTime ThrowTime { get; set; }
public string SenderName { get; set; }
@@ -22,7 +23,7 @@ namespace DiscordBotCore.Others.Logger
public LogMessage(string message, object sender)
{
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;
LogMessageType = LogType.INFO;
}
@@ -30,7 +31,7 @@ namespace DiscordBotCore.Others.Logger
public LogMessage(string message, object sender, DateTime throwTime)
{
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;
LogMessageType = LogType.INFO;
}
@@ -38,7 +39,7 @@ namespace DiscordBotCore.Others.Logger
public LogMessage(string message, object sender, LogType logMessageType)
{
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;
LogMessageType = logMessageType;
@@ -48,7 +49,7 @@ namespace DiscordBotCore.Others.Logger
{
Message = message;
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;
}

View File

@@ -80,6 +80,7 @@ public sealed class Logger : ILogger
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) => Log(new LogMessage(message, logType));
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 async void ExecuteStartup()
{
Directory.CreateDirectory(Path.Combine(DiscordBotCore.Application.CurrentApplication.DataFolder, "DiscordBotUI"));
await Execute(["start"]);
}
public async Task Execute(string[]? args)
{
if (args == null || args.Length == 0)
{
Console.WriteLine("Please provide an option");
return;
Directory.CreateDirectory(Path.Combine(DiscordBotCore.Application.GetResourceFullPath(), "DiscordBotUI"));
await StartUI();
}
if (args[0] == "theme")

View File

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

View File

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