Add AnyCommandExecuted callback to IConsoleHost.

Intended use case here is for content to listen to ConCmds for AFK detection.
This commit is contained in:
Pieter-Jan Briers
2021-07-21 21:32:16 +02:00
parent 7124d86f94
commit be57b5d20b
4 changed files with 23 additions and 6 deletions

View File

@@ -83,6 +83,8 @@ namespace Robust.Client.Console
OutputText(text, true, true);
}
public override event ConAnyCommandCallback? AnyCommandExecuted;
/// <inheritdoc />
public override void ExecuteCommand(ICommonSession? session, string command)
{
@@ -103,7 +105,11 @@ namespace Robust.Client.Console
{
var command1 = AvailableCommands[commandName];
args.RemoveAt(0);
command1.Execute(new ConsoleShell(this, null), command, args.ToArray());
var shell = new ConsoleShell(this, null);
var cmdArgs = args.ToArray();
AnyCommandExecuted?.Invoke(shell, commandName, command, cmdArgs);
command1.Execute(shell, command, cmdArgs);
}
else
WriteError(null, "Unknown command: " + commandName);

View File

@@ -17,6 +17,8 @@ namespace Robust.Server.Console
[Dependency] private readonly IPlayerManager _players = default!;
[Dependency] private readonly ISystemConsoleManager _systemConsole = default!;
public override event ConAnyCommandCallback? AnyCommandExecuted;
/// <inheritdoc />
public override void ExecuteCommand(ICommonSession? session, string command)
{
@@ -87,20 +89,22 @@ namespace Robust.Server.Console
if (AvailableCommands.TryGetValue(cmdName, out var conCmd)) // command registered
{
args.RemoveAt(0);
var cmdArgs = args.ToArray();
if (shell.Player != null) // remote client
{
if (_groupController.CanCommand((IPlayerSession) shell.Player, cmdName)) // client has permission
{
args.RemoveAt(0);
conCmd.Execute(shell, command, args.ToArray());
AnyCommandExecuted?.Invoke(shell, cmdName, command, cmdArgs);
conCmd.Execute(shell, command, cmdArgs);
}
else
shell.WriteError($"Unknown command: '{cmdName}'");
}
else // system console
{
args.RemoveAt(0);
conCmd.Execute(shell, command, args.ToArray());
AnyCommandExecuted?.Invoke(shell, cmdName, command, cmdArgs);
conCmd.Execute(shell, command, cmdArgs);
}
}
else

View File

@@ -30,6 +30,8 @@ namespace Robust.Shared.Console
/// <inheritdoc />
public IReadOnlyDictionary<string, IConsoleCommand> RegisteredCommands => AvailableCommands;
public abstract event ConAnyCommandCallback? AnyCommandExecuted;
protected ConsoleHost()
{
LocalShell = new ConsoleShell(this, null);

View File

@@ -12,6 +12,8 @@ namespace Robust.Shared.Console
/// <param name="args">An array of all the parsed arguments.</param>
public delegate void ConCommandCallback(IConsoleShell shell, string argStr, string[] args);
public delegate void ConAnyCommandCallback(IConsoleShell shell, string commandName, string argStr, string[] args);
/// <summary>
/// The console host exists as a singleton subsystem that provides all of the features of the console API.
/// It will register console commands, spawn console shells and execute command strings.
@@ -38,7 +40,10 @@ namespace Robust.Shared.Console
/// </summary>
IReadOnlyDictionary<string, IConsoleCommand> RegisteredCommands { get; }
/// <summary>
/// Invoked before any console command is executed.
/// </summary>
event ConAnyCommandCallback AnyCommandExecuted;
event EventHandler ClearText;
/// <summary>