From 163eebd92e21075698986a843850f0850514e778 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 22 May 2020 19:57:22 -0400 Subject: move internal commands out of SCore --- src/SMAPI/Framework/Commands/HelpCommand.cs | 64 +++++++++++++++++++++++ src/SMAPI/Framework/Commands/IInternalCommand.cs | 24 +++++++++ src/SMAPI/Framework/Commands/ReloadI18nCommand.cs | 44 ++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 src/SMAPI/Framework/Commands/HelpCommand.cs create mode 100644 src/SMAPI/Framework/Commands/IInternalCommand.cs create mode 100644 src/SMAPI/Framework/Commands/ReloadI18nCommand.cs (limited to 'src/SMAPI/Framework/Commands') diff --git a/src/SMAPI/Framework/Commands/HelpCommand.cs b/src/SMAPI/Framework/Commands/HelpCommand.cs new file mode 100644 index 00000000..b8730a00 --- /dev/null +++ b/src/SMAPI/Framework/Commands/HelpCommand.cs @@ -0,0 +1,64 @@ +using System.Linq; + +namespace StardewModdingAPI.Framework.Commands +{ + /// The 'help' SMAPI console command. + internal class HelpCommand : IInternalCommand + { + /********* + ** Fields + *********/ + /// Manages console commands. + private readonly CommandManager CommandManager; + + + /********* + ** Accessors + *********/ + /// The command name, which the user must type to trigger it. + public string Name { get; } = "help"; + + /// The human-readable documentation shown when the player runs the built-in 'help' command. + public string Description { get; } = "Lists command documentation.\n\nUsage: help\nLists all available commands.\n\nUsage: help \n- cmd: The name of a command whose documentation to display."; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// Manages console commands. + public HelpCommand(CommandManager commandManager) + { + this.CommandManager = commandManager; + } + + /// Handle the console command when it's entered by the user. + /// The command arguments. + /// Writes messages to the console. + public void HandleCommand(string[] args, IMonitor monitor) + { + if (args.Any()) + { + Command result = this.CommandManager.Get(args[0]); + if (result == null) + monitor.Log("There's no command with that name.", LogLevel.Error); + else + monitor.Log($"{result.Name}: {result.Documentation}{(result.Mod != null ? $"\n(Added by {result.Mod.DisplayName}.)" : "")}", LogLevel.Info); + } + else + { + string message = "The following commands are registered:\n"; + IGrouping[] groups = (from command in this.CommandManager.GetAll() orderby command.Mod?.DisplayName, command.Name group command.Name by command.Mod?.DisplayName).ToArray(); + foreach (var group in groups) + { + string modName = group.Key ?? "SMAPI"; + string[] commandNames = group.ToArray(); + message += $"{modName}:\n {string.Join("\n ", commandNames)}\n\n"; + } + message += "For more information about a command, type 'help command_name'."; + + monitor.Log(message, LogLevel.Info); + } + } + } +} diff --git a/src/SMAPI/Framework/Commands/IInternalCommand.cs b/src/SMAPI/Framework/Commands/IInternalCommand.cs new file mode 100644 index 00000000..abf105b6 --- /dev/null +++ b/src/SMAPI/Framework/Commands/IInternalCommand.cs @@ -0,0 +1,24 @@ +namespace StardewModdingAPI.Framework.Commands +{ + /// A core SMAPI console command. + interface IInternalCommand + { + /********* + ** Accessors + *********/ + /// The command name, which the user must type to trigger it. + string Name { get; } + + /// The human-readable documentation shown when the player runs the built-in 'help' command. + string Description { get; } + + + /********* + ** Methods + *********/ + /// Handle the console command when it's entered by the user. + /// The command arguments. + /// Writes messages to the console. + void HandleCommand(string[] args, IMonitor monitor); + } +} diff --git a/src/SMAPI/Framework/Commands/ReloadI18nCommand.cs b/src/SMAPI/Framework/Commands/ReloadI18nCommand.cs new file mode 100644 index 00000000..12328bb6 --- /dev/null +++ b/src/SMAPI/Framework/Commands/ReloadI18nCommand.cs @@ -0,0 +1,44 @@ +using System; + +namespace StardewModdingAPI.Framework.Commands +{ + /// The 'reload_i18n' SMAPI console command. + internal class ReloadI18nCommand : IInternalCommand + { + /********* + ** Fields + *********/ + /// Reload translations for all mods. + private readonly Action ReloadTranslations; + + + /********* + ** Accessors + *********/ + /// The command name, which the user must type to trigger it. + public string Name { get; } = "reload_i18n"; + + /// The human-readable documentation shown when the player runs the built-in 'help' command. + public string Description { get; } = "Reloads translation files for all mods.\n\nUsage: reload_i18n"; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// Reload translations for all mods.. + public ReloadI18nCommand(Action reloadTranslations) + { + this.ReloadTranslations = reloadTranslations; + } + + /// Handle the console command when it's entered by the user. + /// The command arguments. + /// Writes messages to the console. + public void HandleCommand(string[] args, IMonitor monitor) + { + this.ReloadTranslations(); + monitor.Log("Reloaded translation files for all mods. This only affects new translations the mods fetch; if they cached some text, it may not be updated.", LogLevel.Info); + } + } +} -- cgit