mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-13 20:24:16 +01:00
Add ICommandManager (#1273)
This commit is contained in:
parent
852d68f326
commit
6792fb4de5
2 changed files with 57 additions and 27 deletions
|
|
@ -8,6 +8,7 @@ using Dalamud.Game.Text;
|
||||||
using Dalamud.Game.Text.SeStringHandling;
|
using Dalamud.Game.Text.SeStringHandling;
|
||||||
using Dalamud.IoC;
|
using Dalamud.IoC;
|
||||||
using Dalamud.IoC.Internal;
|
using Dalamud.IoC.Internal;
|
||||||
|
using Dalamud.Plugin.Services;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
|
||||||
namespace Dalamud.Game.Command;
|
namespace Dalamud.Game.Command;
|
||||||
|
|
@ -18,7 +19,10 @@ namespace Dalamud.Game.Command;
|
||||||
[PluginInterface]
|
[PluginInterface]
|
||||||
[InterfaceVersion("1.0")]
|
[InterfaceVersion("1.0")]
|
||||||
[ServiceManager.BlockingEarlyLoadedService]
|
[ServiceManager.BlockingEarlyLoadedService]
|
||||||
public sealed class CommandManager : IServiceType, IDisposable
|
#pragma warning disable SA1015
|
||||||
|
[ResolveVia<ICommandManager>]
|
||||||
|
#pragma warning restore SA1015
|
||||||
|
public sealed class CommandManager : IServiceType, IDisposable, ICommandManager
|
||||||
{
|
{
|
||||||
private readonly Dictionary<string, CommandInfo> commandMap = new();
|
private readonly Dictionary<string, CommandInfo> commandMap = new();
|
||||||
private readonly Regex commandRegexEn = new(@"^The command (?<command>.+) does not exist\.$", RegexOptions.Compiled);
|
private readonly Regex commandRegexEn = new(@"^The command (?<command>.+) does not exist\.$", RegexOptions.Compiled);
|
||||||
|
|
@ -46,16 +50,10 @@ public sealed class CommandManager : IServiceType, IDisposable
|
||||||
this.chatGui.CheckMessageHandled += this.OnCheckMessageHandled;
|
this.chatGui.CheckMessageHandled += this.OnCheckMessageHandled;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc/>
|
||||||
/// Gets a read-only list of all registered commands.
|
|
||||||
/// </summary>
|
|
||||||
public ReadOnlyDictionary<string, CommandInfo> Commands => new(this.commandMap);
|
public ReadOnlyDictionary<string, CommandInfo> Commands => new(this.commandMap);
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc/>
|
||||||
/// Process a command in full.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="content">The full command string.</param>
|
|
||||||
/// <returns>True if the command was found and dispatched.</returns>
|
|
||||||
public bool ProcessCommand(string content)
|
public bool ProcessCommand(string content)
|
||||||
{
|
{
|
||||||
string command;
|
string command;
|
||||||
|
|
@ -91,19 +89,14 @@ public sealed class CommandManager : IServiceType, IDisposable
|
||||||
argument = content[argStart..];
|
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;
|
return false;
|
||||||
|
|
||||||
this.DispatchCommand(command, argument, handler);
|
this.DispatchCommand(command, argument, handler);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc/>
|
||||||
/// Dispatch the handling of a command.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="command">The command to dispatch.</param>
|
|
||||||
/// <param name="argument">The provided arguments.</param>
|
|
||||||
/// <param name="info">A <see cref="CommandInfo"/> object describing this command.</param>
|
|
||||||
public void DispatchCommand(string command, string argument, CommandInfo info)
|
public void DispatchCommand(string command, string argument, CommandInfo info)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
@ -116,12 +109,7 @@ public sealed class CommandManager : IServiceType, IDisposable
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc/>
|
||||||
/// Add a command handler, which you can use to add your own custom commands to the in-game chat.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="command">The command to register.</param>
|
|
||||||
/// <param name="info">A <see cref="CommandInfo"/> object describing the command.</param>
|
|
||||||
/// <returns>If adding was successful.</returns>
|
|
||||||
public bool AddHandler(string command, CommandInfo info)
|
public bool AddHandler(string command, CommandInfo info)
|
||||||
{
|
{
|
||||||
if (info == null)
|
if (info == null)
|
||||||
|
|
@ -139,11 +127,7 @@ public sealed class CommandManager : IServiceType, IDisposable
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc/>
|
||||||
/// Remove a command from the command handlers.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="command">The command to remove.</param>
|
|
||||||
/// <returns>If the removal was successful.</returns>
|
|
||||||
public bool RemoveHandler(string command)
|
public bool RemoveHandler(string command)
|
||||||
{
|
{
|
||||||
return this.commandMap.Remove(command);
|
return this.commandMap.Remove(command);
|
||||||
|
|
|
||||||
46
Dalamud/Plugin/Services/ICommandManager.cs
Normal file
46
Dalamud/Plugin/Services/ICommandManager.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
|
||||||
|
using Dalamud.Game.Command;
|
||||||
|
|
||||||
|
namespace Dalamud.Plugin.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This class manages registered in-game slash commands.
|
||||||
|
/// </summary>
|
||||||
|
public interface ICommandManager
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a read-only list of all registered commands.
|
||||||
|
/// </summary>
|
||||||
|
public ReadOnlyDictionary<string, CommandInfo> Commands { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Process a command in full.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="content">The full command string.</param>
|
||||||
|
/// <returns>True if the command was found and dispatched.</returns>
|
||||||
|
public bool ProcessCommand(string content);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Dispatch the handling of a command.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command">The command to dispatch.</param>
|
||||||
|
/// <param name="argument">The provided arguments.</param>
|
||||||
|
/// <param name="info">A <see cref="CommandInfo"/> object describing this command.</param>
|
||||||
|
public void DispatchCommand(string command, string argument, CommandInfo info);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add a command handler, which you can use to add your own custom commands to the in-game chat.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command">The command to register.</param>
|
||||||
|
/// <param name="info">A <see cref="CommandInfo"/> object describing the command.</param>
|
||||||
|
/// <returns>If adding was successful.</returns>
|
||||||
|
public bool AddHandler(string command, CommandInfo info);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove a command from the command handlers.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command">The command to remove.</param>
|
||||||
|
/// <returns>If the removal was successful.</returns>
|
||||||
|
public bool RemoveHandler(string command);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue