Removed the WebUI. Removed the Modules

This commit is contained in:
2024-10-30 23:10:04 +02:00
parent f8df0f0254
commit 9e8bfbbe16
2133 changed files with 120 additions and 15581 deletions

View File

@@ -22,7 +22,7 @@ namespace CppCompatibilityModule.Extern
return Result.Success();
}
Application.Logger.Log($"Loading library {LibraryPath}");
Application.CurrentApplication.Logger.Log($"Loading library {LibraryPath}");
if(!NativeLibrary.TryLoad(LibraryPath, out IntPtr hModule))
@@ -30,7 +30,7 @@ namespace CppCompatibilityModule.Extern
return Result.Failure(new DllNotFoundException($"Unable to load library {LibraryPath}"));
}
Application.Logger.Log($"Library {LibraryPath} loaded successfully [{hModule}]");
Application.CurrentApplication.Logger.Log($"Library {LibraryPath} loaded successfully [{hModule}]");
LibraryHandle = hModule;
@@ -47,7 +47,7 @@ namespace CppCompatibilityModule.Extern
NativeLibrary.Free(LibraryHandle);
LibraryHandle = IntPtr.Zero;
Application.Logger.Log($"Library {LibraryPath} freed successfully");
Application.CurrentApplication.Logger.Log($"Library {LibraryPath} freed successfully");
}
private IntPtr GetFunctionPointer(string functionName)
@@ -69,11 +69,11 @@ namespace CppCompatibilityModule.Extern
{
IntPtr functionPointer = GetFunctionPointer(methodName);
Application.Logger.Log($"Function pointer for {methodName} obtained successfully [address: {functionPointer}]");
Application.CurrentApplication.Logger.Log($"Function pointer for {methodName} obtained successfully [address: {functionPointer}]");
T result = (T)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(T));
Application.Logger.Log($"Delegate for {methodName} created successfully");
Application.CurrentApplication.Logger.Log($"Delegate for {methodName} created successfully");
return result;
}
@@ -82,7 +82,7 @@ namespace CppCompatibilityModule.Extern
{
IntPtr functionPointer = Marshal.GetFunctionPointerForDelegate(functionDelegate);
Application.Logger.Log($"Function pointer for delegate {functionDelegate.Method.Name} obtained successfully [address: {functionPointer}]");
Application.CurrentApplication.Logger.Log($"Function pointer for delegate {functionDelegate.Method.Name} obtained successfully [address: {functionPointer}]");
return functionPointer;
}
@@ -107,7 +107,7 @@ namespace CppCompatibilityModule.Extern
var result = setterDelegate.DynamicInvoke(executableFunctionPtr);
Application.Logger.Log($"Function {setterExternFunctionName} bound to local action successfully");
Application.CurrentApplication.Logger.Log($"Function {setterExternFunctionName} bound to local action successfully");
return result;
}
@@ -118,7 +118,7 @@ namespace CppCompatibilityModule.Extern
functionDelegate(ref parameter);
Application.Logger.Log($"Function {methodName} called successfully with parameter");
Application.CurrentApplication.Logger.Log($"Function {methodName} called successfully with parameter");
}
public void CallFunction(string methodName)
@@ -127,7 +127,7 @@ namespace CppCompatibilityModule.Extern
functionDelegate();
Application.Logger.Log($"Function {methodName} called successfully");
Application.CurrentApplication.Logger.Log($"Function {methodName} called successfully");
}
}
}

View File

@@ -47,7 +47,7 @@ public class ExternalApplication
return result.Match<ExternalApplication?>(
() => new ExternalApplication(Guid.NewGuid(), library),
(ex) => {
Application.Logger.Log(ex.Message, LogType.Error);
Application.CurrentApplication.Logger.Log(ex.Message, LogType.Error);
library.FreeLibrary();
return null;
});

View File

@@ -1,27 +1,15 @@
using DiscordBotCore;
using DiscordBotCore.Interfaces.Modules;
using DiscordBotCore.Others;
namespace CppCompatibilityModule;
public class Entry : IModule
public class ExternalApplicationHandler
{
public ModuleType ModuleType => ModuleType.Compatibility;
public string Name => "CppCompatibility";
public IDictionary<string, string> MethodMapping => new Dictionary<string, string>()
{
{"create_application", "CreateApplication"},
{"stop_application", "StopApplication"},
{"execute_function_with_parameter", "CallFunctionWithParameter"},
{"execute_function_without_parameter", "CallFunctionWithoutParameter"}
};
private ExternalApplicationManager? _ExternalApplicationManager;
public Task Initialize()
public ExternalApplicationHandler()
{
_ExternalApplicationManager = new ExternalApplicationManager();
return Task.CompletedTask;
}
public Guid CreateApplication(string dllFilePath)
@@ -29,7 +17,7 @@ public class Entry : IModule
if (_ExternalApplicationManager is null)
{
Application.Logger.Log("Failed to create application because the manager is not initialized. This should have never happened in the first place !!!", this, LogType.Critical);
Application.CurrentApplication.Logger.Log("Failed to create application because the manager is not initialized. This should have never happened in the first place !!!", this, LogType.Critical);
return Guid.Empty;
}
@@ -45,7 +33,7 @@ public class Entry : IModule
{
if (_ExternalApplicationManager is null)
{
Application.Logger.Log("Failed to stop application because the manager is not initialized. This should have never happened in the first place!!!", this, LogType.Critical);
Application.CurrentApplication.Logger.Log("Failed to stop application because the manager is not initialized. This should have never happened in the first place!!!", this, LogType.Critical);
return;
}
@@ -56,7 +44,7 @@ public class Entry : IModule
{
if (_ExternalApplicationManager is null)
{
Application.Logger.Log("Failed to call function because the manager is not initialized. This should have never happened in the first place!!!", this, LogType.Critical);
Application.CurrentApplication.Logger.Log("Failed to call function because the manager is not initialized. This should have never happened in the first place!!!", this, LogType.Critical);
return;
}
@@ -67,7 +55,7 @@ public class Entry : IModule
{
if (_ExternalApplicationManager is null)
{
Application.Logger.Log("Failed to call function because the manager is not initialized. This should have never happened in the first place!!!", this, LogType.Critical);
Application.CurrentApplication.Logger.Log("Failed to call function because the manager is not initialized. This should have never happened in the first place!!!", this, LogType.Critical);
return;
}

View File

@@ -3,7 +3,7 @@ using DiscordBotCore;
namespace CppCompatibilityModule;
public class ExternalApplicationManager
internal class ExternalApplicationManager
{
private List<ExternalApplication> _ExternalApplications;
@@ -33,14 +33,14 @@ public class ExternalApplicationManager
var application = _ExternalApplications.FirstOrDefault(app => app.ApplicationId == applicationId, null);
if(application is null)
{
Application.Logger.Log($"Couldn't find application with id {applicationId}");
Application.CurrentApplication.Logger.Log($"Couldn't find application with id {applicationId}");
return;
}
application.FreeLibrary();
_ExternalApplications.Remove(application);
Application.Logger.Log($"Application with id {applicationId} freed successfully");
Application.CurrentApplication.Logger.Log($"Application with id {applicationId} freed successfully");
}
public void ExecuteApplicationFunctionWithParameter(Guid appId, string functionName, ref object parameter)
@@ -48,7 +48,7 @@ public class ExternalApplicationManager
var application = _ExternalApplications.FirstOrDefault(app => app.ApplicationId == appId);
if(application is null)
{
Application.Logger.Log($"Couldn't find application with id {appId}");
Application.CurrentApplication.Logger.Log($"Couldn't find application with id {appId}");
return;
}
@@ -60,7 +60,7 @@ public class ExternalApplicationManager
var application = _ExternalApplications.FirstOrDefault(app => app.ApplicationId == appId);
if(application is null)
{
Application.Logger.Log($"Couldn't find application with id {appId}");
Application.CurrentApplication.Logger.Log($"Couldn't find application with id {appId}");
return;
}

View File

@@ -1,49 +0,0 @@
using DiscordBotCore.Interfaces.Modules;
using DiscordBotCore.Others;
namespace LoggerModule
{
public class Entry : IModule
{
public string Name => "LoggerModule";
public ModuleType ModuleType => ModuleType.Logger;
public IDictionary<string, string> MethodMapping => new Dictionary<string, string>
{
{"BaseLog", "LogMessage"},
{"LogWithTypeAndFormat", "LogMessageWithTypeAndFormat"},
{"LogWithType", "LogMessageWithType"},
{"LogWithSender", "LogMessageWithSender"},
{"LogWithTypeAndSender", "LogMessageWithTypeAndSender"},
{"BaseLogException", "LogExceptionWithSenderAndFullStack"},
{"SetPrintFunction", "SetOutFunction"},
};
const string _LogFolder = "./Data/Logs/";
const string _LogFormat = "{ThrowTime} {SenderName} {Message}";
public ILogger Module { get; private set; } = null!;
public Task Initialize()
{
ILogger logger = new Logger(_LogFolder, _LogFormat);
Module = logger;
return Task.CompletedTask;
}
public void SetOutFunction(Action<string, LogType> outFunction)
{
Module.SetOutFunction(outFunction);
}
public void LogMessage(string message) => Module.Log(message);
public void LogMessageWithTypeAndFormat(string message, LogType logType, string format) => Module.Log(message, logType, format);
public void LogMessageWithType(string message, LogType logType) => Module.Log(message, logType);
public void LogMessageWithSender(string message, object Sender) => Module.Log(message, Sender);
public void LogMessageWithTypeAndSender(string message, object Sender, LogType type) => Module.Log(message, Sender, type);
public void LogExceptionWithSenderAndFullStack(Exception exception, object Sender, bool logFullStack = false) => Module.LogException(exception, Sender, logFullStack);
}
}

View File

@@ -1,12 +0,0 @@
using DiscordBotCore.Others;
namespace LoggerModule;
public interface ILogMessage
{
public string Message { get; protected set; }
public DateTime ThrowTime { get; protected set; }
public string SenderName { get; protected set; }
public LogType LogMessageType { get; protected set; }
}

View File

@@ -1,22 +0,0 @@
using DiscordBotCore.Others;
namespace LoggerModule;
public interface ILogger
{
public struct FormattedMessage {
public string Message;
public LogType Type;
}
string LogMessageFormat { get; set; }
void Log(string message);
void Log(string message, LogType logType);
void Log(string message, LogType logType, string format);
void Log(string message, object Sender);
void Log(string message, object Sender, LogType type);
void LogException(Exception exception, object Sender, bool logFullStack = false);
void SetOutFunction(Action<string,LogType> outFunction);
}

View File

@@ -1,78 +0,0 @@
using DiscordBotCore.Others;
namespace LoggerModule
{
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; }
public LogType LogMessageType { get; set; }
public LogMessage(string message, LogType logMessageType)
{
Message = message;
LogMessageType = logMessageType;
ThrowTime = DateTime.Now;
SenderName = string.Empty;
}
public LogMessage(string message, object sender)
{
Message = message;
SenderName = sender is string && sender as string == string.Empty ? _DefaultLogMessageSender : sender.GetType().FullName ?? sender.GetType().Name;
ThrowTime = DateTime.Now;
LogMessageType = LogType.Info;
}
public LogMessage(string message, object sender, DateTime throwTime)
{
Message = message;
SenderName = sender is string && sender as string == string.Empty ? _DefaultLogMessageSender : sender.GetType().FullName ?? sender.GetType().Name;
ThrowTime = throwTime;
LogMessageType = LogType.Info;
}
public LogMessage(string message, object sender, LogType logMessageType)
{
Message = message;
SenderName = sender is string && sender as string == string.Empty ? _DefaultLogMessageSender : sender.GetType().FullName ?? sender.GetType().Name;
ThrowTime = DateTime.Now;
LogMessageType = logMessageType;
}
public LogMessage(string message, DateTime throwTime, object sender, LogType logMessageType)
{
Message = message;
ThrowTime = throwTime;
SenderName = sender is string && sender as string == string.Empty ? _DefaultLogMessageSender : sender.GetType().FullName ?? sender.GetType().Name;
LogMessageType = logMessageType;
}
public LogMessage WithMessage(string message)
{
this.Message = message;
return this;
}
public LogMessage WithCurrentThrowTime()
{
this.ThrowTime = DateTime.Now;
return this;
}
public LogMessage WithMessageType(LogType logType)
{
this.LogMessageType = logType;
return this;
}
public static LogMessage CreateFromException(Exception exception, object Sender, bool logFullStack)
{
LogMessage message = new LogMessage(logFullStack? exception.ToString() : exception.Message, Sender, LogType.Error);
return message;
}
}
}

View File

@@ -1,87 +0,0 @@
using DiscordBotCore.Others;
namespace LoggerModule;
public sealed class Logger : ILogger
{
private readonly FileStream _LogFileStream;
public List<string> LogMessageProperties = typeof(ILogMessage).GetProperties().Select(p => p.Name).ToList();
private Action<string, LogType>? _OutFunction;
public string LogMessageFormat { get ; set; }
public Logger(string logFolder, string logMessageFormat, Action<string,LogType>? outFunction = null)
{
this.LogMessageFormat = logMessageFormat;
this._OutFunction = outFunction;
var logFile = logFolder + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
_LogFileStream = File.Open(logFile, FileMode.Append, FileAccess.Write, FileShare.Read);
}
/// <summary>
/// Generate a formatted string based on the default parameters of the ILogMessage and a string defined as model
/// </summary>
/// <param name="message">The message</param>
/// <returns>A formatted string with the message values</returns>
private string GenerateLogMessage(ILogMessage message)
{
string messageAsString = new string(LogMessageFormat);
foreach (var prop in LogMessageProperties)
{
Type messageType = typeof(ILogMessage);
messageAsString = messageAsString.Replace("{" + prop + "}", messageType?.GetProperty(prop)?.GetValue(message)?.ToString());
}
return messageAsString;
}
private async void LogToFile(string message)
{
byte[] messageAsBytes = System.Text.Encoding.ASCII.GetBytes(message);
await _LogFileStream.WriteAsync(messageAsBytes, 0, messageAsBytes.Length);
byte[] newLine = System.Text.Encoding.ASCII.GetBytes(Environment.NewLine);
await _LogFileStream.WriteAsync(newLine, 0, newLine.Length);
await _LogFileStream.FlushAsync();
}
private string GenerateLogMessage(ILogMessage message, string customFormat)
{
string messageAsString = customFormat;
foreach (var prop in LogMessageProperties)
{
Type messageType = typeof(ILogMessage);
messageAsString = messageAsString.Replace("{" + prop + "}", messageType?.GetProperty(prop)?.GetValue(message)?.ToString());
}
return messageAsString;
}
public void Log(ILogMessage message, string format)
{
string messageAsString = GenerateLogMessage(message, format);
_OutFunction?.Invoke(messageAsString, message.LogMessageType);
LogToFile(messageAsString);
}
public void Log(ILogMessage message)
{
string messageAsString = GenerateLogMessage(message);
_OutFunction?.Invoke(messageAsString, message.LogMessageType);
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));
public void Log(string message, object Sender, LogType type) => Log(new LogMessage(message, Sender, type));
public void LogException(Exception exception, object Sender, bool logFullStack = false) => Log(LogMessage.CreateFromException(exception, Sender, logFullStack));
public void SetOutFunction(Action<string, LogType> outFunction)
{
this._OutFunction = outFunction;
}
}

View File

@@ -1,13 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\DiscordBotCore\DiscordBotCore.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,3 +0,0 @@
# Modules
This directory contains the modules that are used in the project. Each module is a separate directory that contains the following files:
1. [LoggerModule](./LoggerModule) - a module that provides logging functionality for the bot.