summaryrefslogtreecommitdiff
path: root/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs
blob: 11aa10c33393d77e191e921e64f28a0d0b8d3f1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;

namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other
{
    /// <summary>A command which logs the keys being pressed for 30 seconds once enabled.</summary>
    internal class TestInputCommand : TrainerCommand
    {
        /*********
        ** Fields
        *********/
        /// <summary>The number of seconds for which to log input.</summary>
        private readonly int LogSeconds = 30;

        /// <summary>When the command should stop printing input, or <c>null</c> if currently disabled.</summary>
        private long? ExpiryTicks;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        public TestInputCommand()
            : base("test_input", "Prints all input to the console for 30 seconds.", mayNeedUpdate: true, mayNeedInput: true) { }

        /// <summary>Handle the command.</summary>
        /// <param name="monitor">Writes messages to the console and log file.</param>
        /// <param name="command">The command name.</param>
        /// <param name="args">The command arguments.</param>
        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);
        }

        /// <summary>Perform any logic needed on update tick.</summary>
        /// <param name="monitor">Writes messages to the console and log file.</param>
        public override void OnUpdated(IMonitor monitor)
        {
            // handle expiry
            if (this.ExpiryTicks == null)
                return;
            if (this.ExpiryTicks <= DateTime.UtcNow.Ticks)
            {
                monitor.Log("No longer logging input.", LogLevel.Info);
                this.ExpiryTicks = null;
                return;
            }
        }

        /// <summary>Perform any logic when input is received.</summary>
        /// <param name="monitor">Writes messages to the console and log file.</param>
        /// <param name="button">The button that was pressed.</param>
        public override void OnButtonPressed(IMonitor monitor, SButton button)
        {
            if (this.ExpiryTicks != null)
                monitor.Log($"Pressed {button}", LogLevel.Info);
        }
    }
}