#nullable disable
using System;
using System.Diagnostics.CodeAnalysis;
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other
{
/// A command which logs the keys being pressed for 30 seconds once enabled.
[SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")]
internal class TestInputCommand : ConsoleCommand
{
/*********
** Fields
*********/
/// The number of seconds for which to log input.
private readonly int LogSeconds = 30;
/// When the command should stop printing input, or null if currently disabled.
private long? ExpiryTicks;
/*********
** Public methods
*********/
/// Construct an instance.
public TestInputCommand()
: base("test_input", "Prints all input to the console for 30 seconds.", mayNeedUpdate: true, mayNeedInput: true) { }
/// Handle the command.
/// Writes messages to the console and log file.
/// The command name.
/// The command arguments.
public override void Handle(IMonitor monitor, string command, ArgumentParser args)
{
this.ExpiryTicks = DateTime.UtcNow.Add(TimeSpan.FromSeconds(this.LogSeconds)).Ticks;
monitor.Log($"OK, logging all player input for {this.LogSeconds} seconds.", LogLevel.Info);
}
/// Perform any logic needed on update tick.
/// Writes messages to the console and log file.
public override void OnUpdated(IMonitor monitor)
{
// handle expiry
if (this.ExpiryTicks != null && this.ExpiryTicks <= DateTime.UtcNow.Ticks)
{
monitor.Log("No longer logging input.", LogLevel.Info);
this.ExpiryTicks = null;
return;
}
}
/// Perform any logic when input is received.
/// Writes messages to the console and log file.
/// The button that was pressed.
public override void OnButtonPressed(IMonitor monitor, SButton button)
{
if (this.ExpiryTicks != null)
monitor.Log($"Pressed {button}", LogLevel.Info);
}
}
}