Added the ability to prevent cancellation.

This commit is contained in:
That_One_Nerd 2023-04-01 08:50:08 -04:00
parent 1e5f0dba4e
commit a7ff0a7ab4
3 changed files with 18 additions and 1 deletions

View File

@ -0,0 +1,12 @@
namespace SrcMod.Shell.Modules.ObjectModels;
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class CanCancelAttribute : Attribute
{
public readonly bool CanCancel;
public CanCancelAttribute(bool canCancel)
{
CanCancel = canCancel;
}
}

View File

@ -2,6 +2,7 @@
public class CommandInfo public class CommandInfo
{ {
public bool CanBeCancelled { get; private set; }
public required ModuleInfo Module { get; init; } public required ModuleInfo Module { get; init; }
public required MethodInfo Method { get; init; } public required MethodInfo Method { get; init; }
public string Name { get; private set; } public string Name { get; private set; }
@ -11,6 +12,7 @@ public class CommandInfo
private CommandInfo() private CommandInfo()
{ {
CanBeCancelled = false;
Name = string.Empty; Name = string.Empty;
NameId = string.Empty; NameId = string.Empty;
Parameters = Array.Empty<ParameterInfo>(); Parameters = Array.Empty<ParameterInfo>();
@ -34,10 +36,13 @@ public class CommandInfo
CommandAttribute[] attributes = info.GetCustomAttributes<CommandAttribute>().ToArray(); CommandAttribute[] attributes = info.GetCustomAttributes<CommandAttribute>().ToArray();
if (attributes.Length <= 0) return Array.Empty<CommandInfo>(); if (attributes.Length <= 0) return Array.Empty<CommandInfo>();
CanCancelAttribute? cancel = info.GetCustomAttribute<CanCancelAttribute>();
foreach (CommandAttribute attribute in attributes) foreach (CommandAttribute attribute in attributes)
{ {
commands.Add(new() commands.Add(new()
{ {
CanBeCancelled = cancel is null || cancel.CanCancel,
Method = info, Method = info,
Module = parentModule, Module = parentModule,
Name = info.Name, Name = info.Name,

View File

@ -241,7 +241,7 @@ public class Shell
activeCommand.DoWork += runCommand; activeCommand.DoWork += runCommand;
activeCommand.RunWorkerAsync(); activeCommand.RunWorkerAsync();
activeCommand.WorkerSupportsCancellation = true; activeCommand.WorkerSupportsCancellation = command.CanBeCancelled;
while (activeCommand is not null && activeCommand.IsBusy) Thread.Yield(); while (activeCommand is not null && activeCommand.IsBusy) Thread.Yield();