From 6792fb4de58d9ce18b0d51bc47981f76ef53973b Mon Sep 17 00:00:00 2001 From: MidoriKami <9083275+MidoriKami@users.noreply.github.com> Date: Sat, 24 Jun 2023 21:02:45 -0700 Subject: [PATCH] Add ICommandManager (#1273) --- Dalamud/Game/Command/CommandManager.cs | 38 ++++++------------ Dalamud/Plugin/Services/ICommandManager.cs | 46 ++++++++++++++++++++++ 2 files changed, 57 insertions(+), 27 deletions(-) create mode 100644 Dalamud/Plugin/Services/ICommandManager.cs diff --git a/Dalamud/Game/Command/CommandManager.cs b/Dalamud/Game/Command/CommandManager.cs index 7bb429063..11cbbffbd 100644 --- a/Dalamud/Game/Command/CommandManager.cs +++ b/Dalamud/Game/Command/CommandManager.cs @@ -8,6 +8,7 @@ using Dalamud.Game.Text; using Dalamud.Game.Text.SeStringHandling; using Dalamud.IoC; using Dalamud.IoC.Internal; +using Dalamud.Plugin.Services; using Serilog; namespace Dalamud.Game.Command; @@ -18,7 +19,10 @@ namespace Dalamud.Game.Command; [PluginInterface] [InterfaceVersion("1.0")] [ServiceManager.BlockingEarlyLoadedService] -public sealed class CommandManager : IServiceType, IDisposable +#pragma warning disable SA1015 +[ResolveVia] +#pragma warning restore SA1015 +public sealed class CommandManager : IServiceType, IDisposable, ICommandManager { private readonly Dictionary commandMap = new(); private readonly Regex commandRegexEn = new(@"^The command (?.+) does not exist\.$", RegexOptions.Compiled); @@ -46,16 +50,10 @@ public sealed class CommandManager : IServiceType, IDisposable this.chatGui.CheckMessageHandled += this.OnCheckMessageHandled; } - /// - /// Gets a read-only list of all registered commands. - /// + /// public ReadOnlyDictionary Commands => new(this.commandMap); - /// - /// Process a command in full. - /// - /// The full command string. - /// True if the command was found and dispatched. + /// public bool ProcessCommand(string content) { string command; @@ -91,19 +89,14 @@ public sealed class CommandManager : IServiceType, IDisposable argument = content[argStart..]; } - if (!this.commandMap.TryGetValue(command, out var handler)) // Commad was not found. + if (!this.commandMap.TryGetValue(command, out var handler)) // Command was not found. return false; this.DispatchCommand(command, argument, handler); return true; } - /// - /// Dispatch the handling of a command. - /// - /// The command to dispatch. - /// The provided arguments. - /// A object describing this command. + /// public void DispatchCommand(string command, string argument, CommandInfo info) { try @@ -116,12 +109,7 @@ public sealed class CommandManager : IServiceType, IDisposable } } - /// - /// Add a command handler, which you can use to add your own custom commands to the in-game chat. - /// - /// The command to register. - /// A object describing the command. - /// If adding was successful. + /// public bool AddHandler(string command, CommandInfo info) { if (info == null) @@ -139,11 +127,7 @@ public sealed class CommandManager : IServiceType, IDisposable } } - /// - /// Remove a command from the command handlers. - /// - /// The command to remove. - /// If the removal was successful. + /// public bool RemoveHandler(string command) { return this.commandMap.Remove(command); diff --git a/Dalamud/Plugin/Services/ICommandManager.cs b/Dalamud/Plugin/Services/ICommandManager.cs new file mode 100644 index 000000000..ead7723eb --- /dev/null +++ b/Dalamud/Plugin/Services/ICommandManager.cs @@ -0,0 +1,46 @@ +using System.Collections.ObjectModel; + +using Dalamud.Game.Command; + +namespace Dalamud.Plugin.Services; + +/// +/// This class manages registered in-game slash commands. +/// +public interface ICommandManager +{ + /// + /// Gets a read-only list of all registered commands. + /// + public ReadOnlyDictionary Commands { get; } + + /// + /// Process a command in full. + /// + /// The full command string. + /// True if the command was found and dispatched. + public bool ProcessCommand(string content); + + /// + /// Dispatch the handling of a command. + /// + /// The command to dispatch. + /// The provided arguments. + /// A object describing this command. + public void DispatchCommand(string command, string argument, CommandInfo info); + + /// + /// Add a command handler, which you can use to add your own custom commands to the in-game chat. + /// + /// The command to register. + /// A object describing the command. + /// If adding was successful. + public bool AddHandler(string command, CommandInfo info); + + /// + /// Remove a command from the command handlers. + /// + /// The command to remove. + /// If the removal was successful. + public bool RemoveHandler(string command); +}