Skip to content

Commit 2904960

Browse files
committed
add log_context console command to replace test_input
This logs more contextual info like menu changes.
1 parent 533e06d commit 2904960

File tree

11 files changed

+66
-102
lines changed

11 files changed

+66
-102
lines changed

docs/release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
## Upcoming release
55
* For players:
66
* Added [`--prefer-terminal-name` command-line argument](technical/smapi.md#command-line-arguments) to override which terminal SMAPI is launched with (thanks to test482!).
7+
* Added `log_context` console command. This replaces `log_input` and logs more information like menu changes.
78
* Fixed some mods compiled for Stardew Valley 1.6.3+ not working in 1.6.0–1.6.2.
89
* Fixed SMAPI's "_Found warnings with X mods_" message counting hidden warnings.
910
* Improved translations. Thanks to RezaHidayatM (added Indonesian)!

src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ConsoleCommand.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ internal abstract class ConsoleCommand : IConsoleCommand
1616
/// <summary>The command description.</summary>
1717
public string Description { get; }
1818

19-
/// <summary>Whether the command may need to perform logic when the player presses a button. This value shouldn't change.</summary>
20-
public bool MayNeedInput { get; }
21-
2219
/// <summary>Whether the command may need to perform logic when the game updates. This value shouldn't change.</summary>
2320
public bool MayNeedUpdate { get; }
2421

@@ -48,13 +45,11 @@ public virtual void OnButtonPressed(IMonitor monitor, SButton button) { }
4845
/// <summary>Construct an instance.</summary>
4946
/// <param name="name">The command name the user must type.</param>
5047
/// <param name="description">The command description.</param>
51-
/// <param name="mayNeedInput">Whether the command may need to perform logic when the player presses a button.</param>
5248
/// <param name="mayNeedUpdate">Whether the command may need to perform logic when the game updates.</param>
53-
protected ConsoleCommand(string name, string description, bool mayNeedInput = false, bool mayNeedUpdate = false)
49+
protected ConsoleCommand(string name, string description, bool mayNeedUpdate = false)
5450
{
5551
this.Name = name;
5652
this.Description = description;
57-
this.MayNeedInput = mayNeedInput;
5853
this.MayNeedUpdate = mayNeedUpdate;
5954
}
6055

src/SMAPI.Mods.ConsoleCommands/Framework/Commands/IConsoleCommand.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ internal interface IConsoleCommand
1515
/// <summary>Whether the command may need to perform logic when the game updates. This value shouldn't change.</summary>
1616
bool MayNeedUpdate { get; }
1717

18-
/// <summary>Whether the command may need to perform logic when the player presses a button. This value shouldn't change.</summary>
19-
bool MayNeedInput { get; }
20-
2118

2219
/*********
2320
** Public methods
@@ -31,10 +28,5 @@ internal interface IConsoleCommand
3128
/// <summary>Perform any logic needed on update tick.</summary>
3229
/// <param name="monitor">Writes messages to the console and log file.</param>
3330
void OnUpdated(IMonitor monitor);
34-
35-
/// <summary>Perform any logic when input is received.</summary>
36-
/// <param name="monitor">Writes messages to the console and log file.</param>
37-
/// <param name="button">The button that was pressed.</param>
38-
void OnButtonPressed(IMonitor monitor, SButton button);
3931
}
4032
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using StardewModdingAPI.Framework;
3+
4+
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other
5+
{
6+
/// <summary>A command which logs contextual info like keys pressed or menus changed until it's disabled.</summary>
7+
[SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")]
8+
internal class LogContextCommand : ConsoleCommand
9+
{
10+
/*********
11+
** Public methods
12+
*********/
13+
/// <summary>Construct an instance.</summary>
14+
public LogContextCommand()
15+
: base("log_context", "Prints contextual info like keys pressed or menus changed until it's disabled.", mayNeedUpdate: true) { }
16+
17+
/// <summary>Handle the command.</summary>
18+
/// <param name="monitor">Writes messages to the console and log file.</param>
19+
/// <param name="command">The command name.</param>
20+
/// <param name="args">The command arguments.</param>
21+
public override void Handle(IMonitor monitor, string command, ArgumentParser args)
22+
{
23+
Monitor.ForceLogContext = true;
24+
25+
monitor.Log(
26+
Monitor.ForceLogContext ? "OK, logging contextual info until you run this command again." : "OK, no longer logging contextual info.",
27+
LogLevel.Info
28+
);
29+
}
30+
}
31+
}

src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/SMAPI.Mods.ConsoleCommands/ModEntry.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using StardewModdingAPI.Events;
54
using StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands;
65

76
namespace StardewModdingAPI.Mods.ConsoleCommands
@@ -18,9 +17,6 @@ public class ModEntry : Mod
1817
/// <summary>The commands which may need to handle update ticks.</summary>
1918
private IConsoleCommand[] UpdateHandlers = null!;
2019

21-
/// <summary>The commands which may need to handle input.</summary>
22-
private IConsoleCommand[] InputHandlers = null!;
23-
2420

2521
/*********
2622
** Public methods
@@ -35,27 +31,16 @@ public override void Entry(IModHelper helper)
3531
helper.ConsoleCommands.Add(command.Name, command.Description, (name, args) => this.HandleCommand(command, name, args));
3632

3733
// cache commands
38-
this.InputHandlers = this.Commands.Where(p => p.MayNeedInput).ToArray();
3934
this.UpdateHandlers = this.Commands.Where(p => p.MayNeedUpdate).ToArray();
4035

4136
// hook events
4237
helper.Events.GameLoop.UpdateTicked += this.OnUpdateTicked;
43-
helper.Events.Input.ButtonPressed += this.OnButtonPressed;
4438
}
4539

4640

4741
/*********
4842
** Private methods
4943
*********/
50-
/// <summary>The method invoked when a button is pressed.</summary>
51-
/// <param name="sender">The event sender.</param>
52-
/// <param name="e">The event arguments.</param>
53-
private void OnButtonPressed(object? sender, ButtonPressedEventArgs e)
54-
{
55-
foreach (IConsoleCommand command in this.InputHandlers)
56-
command.OnButtonPressed(this.Monitor, e.Button);
57-
}
58-
5944
/// <summary>The method invoked when the game updates its state.</summary>
6045
/// <param name="sender">The event sender.</param>
6146
/// <param name="e">The event arguments.</param>

src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,4 @@
2222
<ItemGroup>
2323
<None Update="manifest.json" CopyToOutputDirectory="PreserveNewest" />
2424
</ItemGroup>
25-
26-
<Import Project="..\SMAPI.Internal\SMAPI.Internal.projitems" Label="Shared" />
2725
</Project>

src/SMAPI.sln

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SMAPI.Tests.ModApiConsumer"
102102
EndProject
103103
Global
104104
GlobalSection(SharedMSBuildProjectFiles) = preSolution
105-
SMAPI.Internal\SMAPI.Internal.projitems*{0634ea4c-3b8f-42db-aea6-ca9e4ef6e92f}*SharedItemsImports = 5
106105
SMAPI.Internal\SMAPI.Internal.projitems*{0a9bb24f-15ff-4c26-b1a2-81f7ae316518}*SharedItemsImports = 5
107106
SMAPI.Internal\SMAPI.Internal.projitems*{80efd92f-728f-41e0-8a5b-9f6f49a91899}*SharedItemsImports = 5
108107
SMAPI.Internal\SMAPI.Internal.projitems*{85208f8d-6fd1-4531-be05-7142490f59fe}*SharedItemsImports = 13

src/SMAPI/Framework/Monitor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ internal class Monitor : IMonitor
3838
/*********
3939
** Accessors
4040
*********/
41+
/// <summary>Whether to log basic contextual info (like buttons pressed and menus opened) even if <see cref="IsVerbose"/> is disabled.</summary>
42+
public static bool ForceLogContext { get; set; }
43+
44+
/// <summary>The current log level for contextual info that's relevant to the <see cref="ForceLogContext"/> flag.</summary>
45+
public static LogLevel ContextLogLevel => Monitor.ForceLogContext ? LogLevel.Info : LogLevel.Trace;
46+
4147
/// <inheritdoc />
4248
public bool IsVerbose { get; }
4349

0 commit comments

Comments
 (0)