Updated ICommandAction.cs and DBEvent.cs. Removed thread request from DBEvent and added special thread request to ICommandAction.cs

This commit is contained in:
2024-07-22 19:20:17 +03:00
parent 08c5febd66
commit 8c338820c5
42 changed files with 691 additions and 26 deletions

View File

@@ -47,7 +47,10 @@ namespace DiscordBotCore
{
get
{
try { return _ModuleManager.GetModule<ILogger>(); }
try
{
return _ModuleManager.GetModule<ILogger>();
}
catch (ModuleNotFoundException<ILogger> ex)
{
Console.WriteLine("No logger found");

View File

@@ -15,7 +15,7 @@
<None Remove="BlankWindow1.xaml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Discord.Net" Version="3.15.2" />
<PackageReference Include="Discord.Net" Version="3.15.3" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.118" />
</ItemGroup>
<ItemGroup>

View File

@@ -14,11 +14,6 @@ public interface DBEvent
/// </summary>
string Description { get; }
/// <summary>
/// If the event requires another thread to run
/// </summary>
bool RequireOtherThread { get; }
/// <summary>
/// The method that is invoked when the event is loaded into memory
/// </summary>

View File

@@ -8,15 +8,39 @@ namespace DiscordBotCore.Interfaces;
public interface ICommandAction
{
/// <summary>
/// The name of the action. It is also used to call the action
/// </summary>
public string ActionName { get; }
/// <summary>
/// The description of the action
/// </summary>
public string? Description { get; }
/// <summary>
/// An example or a format of how to use the action
/// </summary>
public string? Usage { get; }
/// <summary>
/// Some parameter descriptions. The key is the parameter name and the value is the description. Supports nesting. Only for the Help command
/// </summary>
public IEnumerable<InternalActionOption> ListOfOptions { get; }
/// <summary>
/// The type of the action. It can be a startup action, a normal action (invoked) or both
/// </summary>
public InternalActionRunType RunType { get; }
/// <summary>
/// Specifies if the action requires another thread to run. This is useful for actions that are blocking the main thread.
/// </summary>
public bool RequireOtherThread { get; }
/// <summary>
/// The method that is invoked when the action is called.
/// </summary>
/// <param name="args">The parameters. Its best practice to reflect the parameters described in <see cref="ListOfOptions"/></param>
public Task Execute(string[]? args);
}

View File

@@ -72,7 +72,7 @@ public class PluginLoader
case PluginType.ACTION:
ICommandAction action = (ICommandAction)result.Plugin;
if (action.RunType == InternalActionRunType.OnStartup || action.RunType == InternalActionRunType.OnStartupAndCall)
action.Execute(null);
await Application.CurrentApplication.InternalActionManager.StartAction(action, null);
if(action.RunType == InternalActionRunType.OnCall || action.RunType == InternalActionRunType.OnStartupAndCall)
Actions.Add(action);

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using DiscordBotCore.Interfaces;
using DiscordBotCore.Loaders;
@@ -59,9 +60,9 @@ public class InternalActionManager
{
Application.CurrentApplication.Logger.Log($"Action {actionName} is not executable", this, LogType.Error);
return false;
}
}
await Actions[actionName].Execute(args);
await StartAction(Actions[actionName], args);
return true;
}
catch (Exception e)
@@ -70,4 +71,19 @@ public class InternalActionManager
return false;
}
}
public async Task StartAction(ICommandAction action, params string[]? args)
{
if (action.RequireOtherThread)
{
async void Start() => await action.Execute(args);
Thread thread = new(Start);
thread.Start();
}
else
{
await action.Execute(args);
}
}
}

View File

@@ -30,10 +30,10 @@ public static class ArchiveManager
/// <param name="fileName">The file name in the archive</param>
/// <param name="archName">The archive location on the disk</param>
/// <returns>An array of bytes that represents the Stream value from the file that was read inside the archive</returns>
public static async Task<byte[]?> ReadStreamFromPakAsync(string fileName, string archName)
public static async Task<byte[]?> ReadAllBytes(string fileName, string archName)
{
archName = Application.CurrentApplication.ApplicationEnvironmentVariables["ArchiveFolder"] + archName;
archName = Path.Combine(Application.CurrentApplication.ApplicationEnvironmentVariables["ArchiveFolder"], archName);
if (!File.Exists(archName))
throw new Exception("Failed to load file !");
@@ -49,6 +49,9 @@ public static class ArchiveManager
stream.Close();
memoryStream.Close();
Console.WriteLine("Read file from archive: " + fileName);
Console.WriteLine("Size: " + data.Length);
return data;
}