move internal commands out of SCore
This commit is contained in:
parent
33da29b3e5
commit
163eebd92e
|
@ -2,6 +2,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using StardewModdingAPI.Framework.Commands;
|
||||
|
||||
namespace StardewModdingAPI.Framework
|
||||
{
|
||||
|
@ -27,7 +28,7 @@ namespace StardewModdingAPI.Framework
|
|||
/// <exception cref="ArgumentNullException">The <paramref name="name"/> or <paramref name="callback"/> is null or empty.</exception>
|
||||
/// <exception cref="FormatException">The <paramref name="name"/> is not a valid format.</exception>
|
||||
/// <exception cref="ArgumentException">There's already a command with that name.</exception>
|
||||
public void Add(IModMetadata mod, string name, string documentation, Action<string, string[]> callback, bool allowNullCallback = false)
|
||||
public CommandManager Add(IModMetadata mod, string name, string documentation, Action<string, string[]> callback, bool allowNullCallback = false)
|
||||
{
|
||||
name = this.GetNormalizedName(name);
|
||||
|
||||
|
@ -45,6 +46,16 @@ namespace StardewModdingAPI.Framework
|
|||
|
||||
// add command
|
||||
this.Commands.Add(name, new Command(mod, name, documentation, callback));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Add a console command.</summary>
|
||||
/// <param name="command">the SMAPI console command to add.</param>
|
||||
/// <param name="monitor">Writes messages to the console.</param>
|
||||
/// <exception cref="ArgumentException">There's already a command with that name.</exception>
|
||||
public CommandManager Add(IInternalCommand command, IMonitor monitor)
|
||||
{
|
||||
return this.Add(null, command.Name, command.Description, (name, args) => command.HandleCommand(args, monitor));
|
||||
}
|
||||
|
||||
/// <summary>Get a command by its unique name.</summary>
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
using System.Linq;
|
||||
|
||||
namespace StardewModdingAPI.Framework.Commands
|
||||
{
|
||||
/// <summary>The 'help' SMAPI console command.</summary>
|
||||
internal class HelpCommand : IInternalCommand
|
||||
{
|
||||
/*********
|
||||
** Fields
|
||||
*********/
|
||||
/// <summary>Manages console commands.</summary>
|
||||
private readonly CommandManager CommandManager;
|
||||
|
||||
|
||||
/*********
|
||||
** Accessors
|
||||
*********/
|
||||
/// <summary>The command name, which the user must type to trigger it.</summary>
|
||||
public string Name { get; } = "help";
|
||||
|
||||
/// <summary>The human-readable documentation shown when the player runs the built-in 'help' command.</summary>
|
||||
public string Description { get; } = "Lists command documentation.\n\nUsage: help\nLists all available commands.\n\nUsage: help <cmd>\n- cmd: The name of a command whose documentation to display.";
|
||||
|
||||
|
||||
/*********
|
||||
** Public methods
|
||||
*********/
|
||||
/// <summary>Construct an instance.</summary>
|
||||
/// <param name="commandManager">Manages console commands.</param>
|
||||
public HelpCommand(CommandManager commandManager)
|
||||
{
|
||||
this.CommandManager = commandManager;
|
||||
}
|
||||
|
||||
/// <summary>Handle the console command when it's entered by the user.</summary>
|
||||
/// <param name="args">The command arguments.</param>
|
||||
/// <param name="monitor">Writes messages to the console.</param>
|
||||
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<string, string>[] 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
namespace StardewModdingAPI.Framework.Commands
|
||||
{
|
||||
/// <summary>A core SMAPI console command.</summary>
|
||||
interface IInternalCommand
|
||||
{
|
||||
/*********
|
||||
** Accessors
|
||||
*********/
|
||||
/// <summary>The command name, which the user must type to trigger it.</summary>
|
||||
string Name { get; }
|
||||
|
||||
/// <summary>The human-readable documentation shown when the player runs the built-in 'help' command.</summary>
|
||||
string Description { get; }
|
||||
|
||||
|
||||
/*********
|
||||
** Methods
|
||||
*********/
|
||||
/// <summary>Handle the console command when it's entered by the user.</summary>
|
||||
/// <param name="args">The command arguments.</param>
|
||||
/// <param name="monitor">Writes messages to the console.</param>
|
||||
void HandleCommand(string[] args, IMonitor monitor);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
using System;
|
||||
|
||||
namespace StardewModdingAPI.Framework.Commands
|
||||
{
|
||||
/// <summary>The 'reload_i18n' SMAPI console command.</summary>
|
||||
internal class ReloadI18nCommand : IInternalCommand
|
||||
{
|
||||
/*********
|
||||
** Fields
|
||||
*********/
|
||||
/// <summary>Reload translations for all mods.</summary>
|
||||
private readonly Action ReloadTranslations;
|
||||
|
||||
|
||||
/*********
|
||||
** Accessors
|
||||
*********/
|
||||
/// <summary>The command name, which the user must type to trigger it.</summary>
|
||||
public string Name { get; } = "reload_i18n";
|
||||
|
||||
/// <summary>The human-readable documentation shown when the player runs the built-in 'help' command.</summary>
|
||||
public string Description { get; } = "Reloads translation files for all mods.\n\nUsage: reload_i18n";
|
||||
|
||||
|
||||
/*********
|
||||
** Public methods
|
||||
*********/
|
||||
/// <summary>Construct an instance.</summary>
|
||||
/// <param name="reloadTranslations">Reload translations for all mods..</param>
|
||||
public ReloadI18nCommand(Action reloadTranslations)
|
||||
{
|
||||
this.ReloadTranslations = reloadTranslations;
|
||||
}
|
||||
|
||||
/// <summary>Handle the console command when it's entered by the user.</summary>
|
||||
/// <param name="args">The command arguments.</param>
|
||||
/// <param name="monitor">Writes messages to the console.</param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,6 +16,7 @@ using System.Windows.Forms;
|
|||
#endif
|
||||
using Newtonsoft.Json;
|
||||
using StardewModdingAPI.Events;
|
||||
using StardewModdingAPI.Framework.Commands;
|
||||
using StardewModdingAPI.Framework.Events;
|
||||
using StardewModdingAPI.Framework.Exceptions;
|
||||
using StardewModdingAPI.Framework.Logging;
|
||||
|
@ -508,8 +509,9 @@ namespace StardewModdingAPI.Framework
|
|||
{
|
||||
// prepare console
|
||||
this.Monitor.Log("Type 'help' for help, or 'help <cmd>' for a command's usage", LogLevel.Info);
|
||||
this.GameInstance.CommandManager.Add(null, "help", "Lists command documentation.\n\nUsage: help\nLists all available commands.\n\nUsage: help <cmd>\n- cmd: The name of a command whose documentation to display.", this.HandleCommand);
|
||||
this.GameInstance.CommandManager.Add(null, "reload_i18n", "Reloads translation files for all mods.\n\nUsage: reload_i18n", this.HandleCommand);
|
||||
this.GameInstance.CommandManager
|
||||
.Add(new HelpCommand(this.GameInstance.CommandManager), this.Monitor)
|
||||
.Add(new ReloadI18nCommand(this.ReloadTranslations), this.Monitor);
|
||||
|
||||
// start handling command line input
|
||||
Thread inputThread = new Thread(() =>
|
||||
|
@ -1273,6 +1275,12 @@ namespace StardewModdingAPI.Framework
|
|||
}
|
||||
|
||||
/// <summary>Reload translations for all mods.</summary>
|
||||
private void ReloadTranslations()
|
||||
{
|
||||
this.ReloadTranslations(this.ModRegistry.GetAll(contentPacks: false));
|
||||
}
|
||||
|
||||
/// <summary>Reload translations for the given mods.</summary>
|
||||
/// <param name="mods">The mods for which to reload translations.</param>
|
||||
private void ReloadTranslations(IEnumerable<IModMetadata> mods)
|
||||
{
|
||||
|
@ -1357,48 +1365,6 @@ namespace StardewModdingAPI.Framework
|
|||
return translations;
|
||||
}
|
||||
|
||||
/// <summary>The method called when the user submits a core SMAPI command in the console.</summary>
|
||||
/// <param name="name">The command name.</param>
|
||||
/// <param name="arguments">The command arguments.</param>
|
||||
private void HandleCommand(string name, string[] arguments)
|
||||
{
|
||||
switch (name)
|
||||
{
|
||||
case "help":
|
||||
if (arguments.Any())
|
||||
{
|
||||
Command result = this.GameInstance.CommandManager.Get(arguments[0]);
|
||||
if (result == null)
|
||||
this.Monitor.Log("There's no command with that name.", LogLevel.Error);
|
||||
else
|
||||
this.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<string, string>[] groups = (from command in this.GameInstance.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'.";
|
||||
|
||||
this.Monitor.Log(message, LogLevel.Info);
|
||||
}
|
||||
break;
|
||||
|
||||
case "reload_i18n":
|
||||
this.ReloadTranslations(this.ModRegistry.GetAll(contentPacks: false));
|
||||
this.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);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotSupportedException($"Unrecognized core SMAPI command '{name}'.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Redirect messages logged directly to the console to the given monitor.</summary>
|
||||
/// <param name="gameMonitor">The monitor with which to log messages as the game.</param>
|
||||
/// <param name="message">The message to log.</param>
|
||||
|
|
Loading…
Reference in New Issue