Redesigned the DiscordBotCore by splitting it into multiple projects. Created a WebUI and preparing to remove the DiscordBot application
This commit is contained in:
@@ -1,18 +1,23 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Net.Mime;
|
||||
using System.Runtime.InteropServices;
|
||||
using DiscordBotCore;
|
||||
using DiscordBotCore.Logging;
|
||||
using DiscordBotCore.Others;
|
||||
using DiscordBotCore.Utilities;
|
||||
|
||||
namespace CppCompatibilityModule.Extern
|
||||
{
|
||||
public sealed class ExternLibrary
|
||||
{
|
||||
private readonly ILogger _Logger;
|
||||
public string LibraryPath { get; init; }
|
||||
public IntPtr LibraryHandle { get; private set; }
|
||||
|
||||
public ExternLibrary(string libraryPath)
|
||||
public ExternLibrary(ILogger logger, string libraryPath)
|
||||
{
|
||||
LibraryPath = libraryPath;
|
||||
LibraryHandle = IntPtr.Zero;
|
||||
_Logger = logger;
|
||||
}
|
||||
|
||||
public Result InitializeLibrary()
|
||||
@@ -22,7 +27,7 @@ namespace CppCompatibilityModule.Extern
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
Application.CurrentApplication.Logger.Log($"Loading library {LibraryPath}");
|
||||
_Logger.Log($"Loading library {LibraryPath}");
|
||||
|
||||
|
||||
if(!NativeLibrary.TryLoad(LibraryPath, out IntPtr hModule))
|
||||
@@ -30,7 +35,7 @@ namespace CppCompatibilityModule.Extern
|
||||
return Result.Failure(new DllNotFoundException($"Unable to load library {LibraryPath}"));
|
||||
}
|
||||
|
||||
Application.CurrentApplication.Logger.Log($"Library {LibraryPath} loaded successfully [{hModule}]");
|
||||
_Logger.Log($"Library {LibraryPath} loaded successfully [{hModule}]");
|
||||
|
||||
LibraryHandle = hModule;
|
||||
|
||||
@@ -47,7 +52,7 @@ namespace CppCompatibilityModule.Extern
|
||||
NativeLibrary.Free(LibraryHandle);
|
||||
LibraryHandle = IntPtr.Zero;
|
||||
|
||||
Application.CurrentApplication.Logger.Log($"Library {LibraryPath} freed successfully");
|
||||
_Logger.Log($"Library {LibraryPath} freed successfully");
|
||||
}
|
||||
|
||||
private IntPtr GetFunctionPointer(string functionName)
|
||||
@@ -69,11 +74,11 @@ namespace CppCompatibilityModule.Extern
|
||||
{
|
||||
IntPtr functionPointer = GetFunctionPointer(methodName);
|
||||
|
||||
Application.CurrentApplication.Logger.Log($"Function pointer for {methodName} obtained successfully [address: {functionPointer}]");
|
||||
_Logger.Log($"Function pointer for {methodName} obtained successfully [address: {functionPointer}]");
|
||||
|
||||
T result = (T)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(T));
|
||||
|
||||
Application.CurrentApplication.Logger.Log($"Delegate for {methodName} created successfully");
|
||||
_Logger.Log($"Delegate for {methodName} created successfully");
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -82,7 +87,7 @@ namespace CppCompatibilityModule.Extern
|
||||
{
|
||||
IntPtr functionPointer = Marshal.GetFunctionPointerForDelegate(functionDelegate);
|
||||
|
||||
Application.CurrentApplication.Logger.Log($"Function pointer for delegate {functionDelegate.Method.Name} obtained successfully [address: {functionPointer}]");
|
||||
_Logger.Log($"Function pointer for delegate {functionDelegate.Method.Name} obtained successfully [address: {functionPointer}]");
|
||||
|
||||
return functionPointer;
|
||||
}
|
||||
@@ -107,7 +112,7 @@ namespace CppCompatibilityModule.Extern
|
||||
|
||||
var result = setterDelegate.DynamicInvoke(executableFunctionPtr);
|
||||
|
||||
Application.CurrentApplication.Logger.Log($"Function {setterExternFunctionName} bound to local action successfully");
|
||||
_Logger.Log($"Function {setterExternFunctionName} bound to local action successfully");
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -118,7 +123,7 @@ namespace CppCompatibilityModule.Extern
|
||||
|
||||
functionDelegate(ref parameter);
|
||||
|
||||
Application.CurrentApplication.Logger.Log($"Function {methodName} called successfully with parameter");
|
||||
_Logger.Log($"Function {methodName} called successfully with parameter");
|
||||
}
|
||||
|
||||
public void CallFunction(string methodName)
|
||||
@@ -127,7 +132,7 @@ namespace CppCompatibilityModule.Extern
|
||||
|
||||
functionDelegate();
|
||||
|
||||
Application.CurrentApplication.Logger.Log($"Function {methodName} called successfully");
|
||||
_Logger.Log($"Function {methodName} called successfully");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using DiscordBotCore;
|
||||
using DiscordBotCore.Logging;
|
||||
using DiscordBotCore.Others;
|
||||
|
||||
namespace CppCompatibilityModule.Extern;
|
||||
@@ -7,11 +8,13 @@ public class ExternalApplication
|
||||
{
|
||||
public Guid ApplicationId { get; private set; }
|
||||
private readonly ExternLibrary _ExternLibrary;
|
||||
private readonly ILogger _Logger;
|
||||
|
||||
private ExternalApplication(Guid applicationGuid, ExternLibrary library)
|
||||
private ExternalApplication(ILogger logger, Guid applicationGuid, ExternLibrary library)
|
||||
{
|
||||
this.ApplicationId = applicationGuid;
|
||||
this._ExternLibrary = library;
|
||||
this._Logger = logger;
|
||||
}
|
||||
|
||||
internal void CallFunction(string methodName, ref object parameter)
|
||||
@@ -39,15 +42,15 @@ public class ExternalApplication
|
||||
_ExternLibrary.FreeLibrary();
|
||||
}
|
||||
|
||||
public static ExternalApplication? CreateFromDllFile(string dllFilePath)
|
||||
public static ExternalApplication? CreateFromDllFile(ILogger logger, string dllFilePath)
|
||||
{
|
||||
ExternLibrary library = new ExternLibrary(dllFilePath);
|
||||
ExternLibrary library = new ExternLibrary(logger, dllFilePath);
|
||||
var result = library.InitializeLibrary();
|
||||
|
||||
return result.Match<ExternalApplication?>(
|
||||
() => new ExternalApplication(Guid.NewGuid(), library),
|
||||
() => new ExternalApplication(logger, Guid.NewGuid(), library),
|
||||
(ex) => {
|
||||
Application.CurrentApplication.Logger.Log(ex.Message, LogType.Error);
|
||||
logger.Log(ex.Message, LogType.Error);
|
||||
library.FreeLibrary();
|
||||
return null;
|
||||
});
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
using DiscordBotCore;
|
||||
using DiscordBotCore.Logging;
|
||||
using DiscordBotCore.Others;
|
||||
|
||||
namespace CppCompatibilityModule;
|
||||
|
||||
public class ExternalApplicationHandler
|
||||
{
|
||||
private readonly ILogger _Logger;
|
||||
private ExternalApplicationManager? _ExternalApplicationManager;
|
||||
|
||||
public ExternalApplicationHandler()
|
||||
public ExternalApplicationHandler(ILogger logger)
|
||||
{
|
||||
_ExternalApplicationManager = new ExternalApplicationManager();
|
||||
_Logger = logger;
|
||||
_ExternalApplicationManager = new ExternalApplicationManager(logger);
|
||||
}
|
||||
|
||||
public Guid CreateApplication(string dllFilePath)
|
||||
@@ -17,7 +20,7 @@ public class ExternalApplicationHandler
|
||||
|
||||
if (_ExternalApplicationManager is null)
|
||||
{
|
||||
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);
|
||||
_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;
|
||||
}
|
||||
|
||||
@@ -33,7 +36,7 @@ public class ExternalApplicationHandler
|
||||
{
|
||||
if (_ExternalApplicationManager is null)
|
||||
{
|
||||
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);
|
||||
_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;
|
||||
}
|
||||
|
||||
@@ -44,7 +47,7 @@ public class ExternalApplicationHandler
|
||||
{
|
||||
if (_ExternalApplicationManager is null)
|
||||
{
|
||||
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);
|
||||
_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;
|
||||
}
|
||||
|
||||
@@ -55,7 +58,7 @@ public class ExternalApplicationHandler
|
||||
{
|
||||
if (_ExternalApplicationManager is null)
|
||||
{
|
||||
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);
|
||||
_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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
using CppCompatibilityModule.Extern;
|
||||
using DiscordBotCore;
|
||||
using DiscordBotCore.Logging;
|
||||
|
||||
namespace CppCompatibilityModule;
|
||||
|
||||
internal class ExternalApplicationManager
|
||||
{
|
||||
private readonly ILogger _Logger;
|
||||
private List<ExternalApplication> _ExternalApplications;
|
||||
|
||||
public ExternalApplicationManager()
|
||||
public ExternalApplicationManager(ILogger logger)
|
||||
{
|
||||
_Logger = logger;
|
||||
_ExternalApplications = new List<ExternalApplication>();
|
||||
}
|
||||
|
||||
public bool TryCreateApplication(string applicationFileName, out Guid applicationId)
|
||||
{
|
||||
ExternalApplication? externalApplication = ExternalApplication.CreateFromDllFile(applicationFileName);
|
||||
ExternalApplication? externalApplication = ExternalApplication.CreateFromDllFile(_Logger, applicationFileName);
|
||||
|
||||
if(externalApplication is null)
|
||||
{
|
||||
@@ -33,14 +36,14 @@ internal class ExternalApplicationManager
|
||||
var application = _ExternalApplications.FirstOrDefault(app => app.ApplicationId == applicationId, null);
|
||||
if(application is null)
|
||||
{
|
||||
Application.CurrentApplication.Logger.Log($"Couldn't find application with id {applicationId}");
|
||||
_Logger.Log($"Couldn't find application with id {applicationId}");
|
||||
return;
|
||||
}
|
||||
|
||||
application.FreeLibrary();
|
||||
_ExternalApplications.Remove(application);
|
||||
|
||||
Application.CurrentApplication.Logger.Log($"Application with id {applicationId} freed successfully");
|
||||
_Logger.Log($"Application with id {applicationId} freed successfully");
|
||||
}
|
||||
|
||||
public void ExecuteApplicationFunctionWithParameter(Guid appId, string functionName, ref object parameter)
|
||||
@@ -48,7 +51,7 @@ internal class ExternalApplicationManager
|
||||
var application = _ExternalApplications.FirstOrDefault(app => app.ApplicationId == appId);
|
||||
if(application is null)
|
||||
{
|
||||
Application.CurrentApplication.Logger.Log($"Couldn't find application with id {appId}");
|
||||
_Logger.Log($"Couldn't find application with id {appId}");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -60,7 +63,7 @@ internal class ExternalApplicationManager
|
||||
var application = _ExternalApplications.FirstOrDefault(app => app.ApplicationId == appId);
|
||||
if(application is null)
|
||||
{
|
||||
Application.CurrentApplication.Logger.Log($"Couldn't find application with id {appId}");
|
||||
_Logger.Log($"Couldn't find application with id {appId}");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user