|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using StardewModdingAPI.Framework; |
| 5 | +using StardewValley.Extensions; |
| 6 | + |
| 7 | +namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other; |
| 8 | + |
| 9 | +/// <summary>A command which toggles verbose mode in the SMAPI log.</summary> |
| 10 | +internal class SetVerboseCommand : ConsoleCommand |
| 11 | +{ |
| 12 | + /********* |
| 13 | + ** Fields |
| 14 | + *********/ |
| 15 | + /// <summary>The mod register with which to validate mod IDs.</summary> |
| 16 | + private readonly IModRegistry ModRegistry; |
| 17 | + |
| 18 | + |
| 19 | + /********* |
| 20 | + ** Public methods |
| 21 | + *********/ |
| 22 | + /// <summary>Construct an instance.</summary> |
| 23 | + /// <param name="modRegistry">The mod register with which to validate mod IDs.</param> |
| 24 | + public SetVerboseCommand(IModRegistry modRegistry) |
| 25 | + : base( |
| 26 | + name: "set_verbose", |
| 27 | + description: |
| 28 | + """ |
| 29 | + Toggles whether more detailed information is written to the SMAPI log file (and console in developer mode). This may impact performance. This doesn't affect mods manually set to verbose in the config file. |
| 30 | +
|
| 31 | + Usage: set_verbose |
| 32 | + Toggles verbose logging for SMAPI and all mods. |
| 33 | +
|
| 34 | + Usage: set_verbose [true|false] |
| 35 | + Sets whether verbose logging is enabled (true) or disabled (false) for SMAPI and all mods. |
| 36 | +
|
| 37 | + Usage: set_verbose [true|false] [modId]+ |
| 38 | + Sets whether verbose logging is enabled (true) or disabled (false) for the specified mod IDs. You can specify 'SMAPI' to set it for SMAPI itself. |
| 39 | + """, |
| 40 | + mayNeedUpdate: true |
| 41 | + ) |
| 42 | + { |
| 43 | + this.ModRegistry = modRegistry; |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary>Handle the command.</summary> |
| 47 | + /// <param name="monitor">Writes messages to the console and log file.</param> |
| 48 | + /// <param name="command">The command name.</param> |
| 49 | + /// <param name="args">The command arguments.</param> |
| 50 | + public override void Handle(IMonitor monitor, string command, ArgumentParser args) |
| 51 | + { |
| 52 | + // parse mode |
| 53 | + bool setTo = !(Monitor.ForceVerboseLoggingForAll || Monitor.ForceVerboseLogging.Count > 0); |
| 54 | + if (args.Count > 0) |
| 55 | + { |
| 56 | + if (int.TryParse(args[0], out int numeric) && numeric is 0 or 1) |
| 57 | + setTo = numeric is 1; |
| 58 | + else if (!bool.TryParse(args[0], out setTo)) |
| 59 | + { |
| 60 | + monitor.Log("Invalid argument: if specified, the first argument should be 'true' or 'false' to indicate whether to enable or disable verbose logging."); |
| 61 | + return; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // apply |
| 66 | + if (args.Count is 0 or 1) |
| 67 | + { |
| 68 | + if (setTo) |
| 69 | + { |
| 70 | + Monitor.ForceVerboseLoggingForAll = true; |
| 71 | + Monitor.ForceVerboseLogging.Clear(); |
| 72 | + monitor.Log("Enabled verbose logs for SMAPI and all mods.", LogLevel.Info); |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + Monitor.ForceVerboseLoggingForAll = false; |
| 77 | + Monitor.ForceVerboseLogging.Clear(); |
| 78 | + monitor.Log("Reset to normal.", LogLevel.Info); |
| 79 | + } |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + List<string> toggled = []; |
| 84 | + List<string> unknown = []; |
| 85 | + |
| 86 | + for (int i = 1; i < args.Count; i++) |
| 87 | + { |
| 88 | + // get mod |
| 89 | + string modId = args[i]; |
| 90 | + IModInfo? mod; |
| 91 | + if (modId.EqualsIgnoreCase("SMAPI")) |
| 92 | + { |
| 93 | + modId = "SMAPI"; |
| 94 | + mod = null; |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + mod = this.ModRegistry.Get(modId); |
| 99 | + if (mod is null) |
| 100 | + { |
| 101 | + unknown.Add(modId); |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + modId = mod.Manifest.UniqueID; |
| 106 | + } |
| 107 | + |
| 108 | + // toggle |
| 109 | + Monitor.ForceVerboseLogging.Toggle(modId, setTo); |
| 110 | + toggled.Add(mod is null ? "SMAPI" : mod.Manifest.Name); |
| 111 | + } |
| 112 | + |
| 113 | + if (toggled.Count > 0) |
| 114 | + monitor.Log($"{(setTo ? "Enabled" : "Disabled")} verbose logging for mod{(toggled.Count > 1 ? "s" : "")} '{string.Join("', '", toggled.OrderBy(p => p, StringComparer.OrdinalIgnoreCase))}'.", LogLevel.Info); |
| 115 | + |
| 116 | + if (unknown.Count > 0) |
| 117 | + { |
| 118 | + bool plural = unknown.Count > 1; |
| 119 | + monitor.Log($"No mod{(plural ? "s" : "")} found with ID{(plural ? "s" : "")} '{string.Join("', '", unknown)}'.", LogLevel.Warn); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments