diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-02-13 00:40:33 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-02-13 00:40:33 -0500 |
commit | 0441d0843c65775bc72377e32ed4b3b5ee0b8f75 (patch) | |
tree | ca2f098e04c968ef3bee32ce7900c68b868d17d2 /src/StardewModdingAPI/Framework | |
parent | d1080a8b2b54c777a446f08d9ecd5b76b4b2561a (diff) | |
download | SMAPI-0441d0843c65775bc72377e32ed4b3b5ee0b8f75.tar.gz SMAPI-0441d0843c65775bc72377e32ed4b3b5ee0b8f75.tar.bz2 SMAPI-0441d0843c65775bc72377e32ed4b3b5ee0b8f75.zip |
add new console command API with backward compatibility (#199)
Diffstat (limited to 'src/StardewModdingAPI/Framework')
-rw-r--r-- | src/StardewModdingAPI/Framework/Command.cs | 40 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/CommandHelper.cs | 53 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/CommandManager.cs | 114 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/ModHelper.cs | 8 |
4 files changed, 214 insertions, 1 deletions
diff --git a/src/StardewModdingAPI/Framework/Command.cs b/src/StardewModdingAPI/Framework/Command.cs new file mode 100644 index 00000000..943e018d --- /dev/null +++ b/src/StardewModdingAPI/Framework/Command.cs @@ -0,0 +1,40 @@ +using System; + +namespace StardewModdingAPI.Framework +{ + /// <summary>A command that can be submitted through the SMAPI console to interact with SMAPI.</summary> + internal class Command + { + /********* + ** Accessor + *********/ + /// <summary>The friendly name for the mod that registered the command.</summary> + public string ModName { get; } + + /// <summary>The command name, which the user must type to trigger it.</summary> + public string Name { get; } + + /// <summary>The human-readable documentation shown when the player runs the built-in 'help' command.</summary> + public string Documentation { get; } + + /// <summary>The method to invoke when the command is triggered. This method is passed the command name and arguments submitted by the user.</summary> + public Action<string, string[]> Callback { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="modName">The friendly name for the mod that registered the command.</param> + /// <param name="name">The command name, which the user must type to trigger it.</param> + /// <param name="documentation">The human-readable documentation shown when the player runs the built-in 'help' command.</param> + /// <param name="callback">The method to invoke when the command is triggered. This method is passed the command name and arguments submitted by the user.</param> + public Command(string modName, string name, string documentation, Action<string, string[]> callback) + { + this.ModName = modName; + this.Name = name; + this.Documentation = documentation; + this.Callback = callback; + } + } +} diff --git a/src/StardewModdingAPI/Framework/CommandHelper.cs b/src/StardewModdingAPI/Framework/CommandHelper.cs new file mode 100644 index 00000000..2e9dea8e --- /dev/null +++ b/src/StardewModdingAPI/Framework/CommandHelper.cs @@ -0,0 +1,53 @@ +using System; + +namespace StardewModdingAPI.Framework +{ + /// <summary>Provides an API for managing console commands.</summary> + internal class CommandHelper : ICommandHelper + { + /********* + ** Accessors + *********/ + /// <summary>The friendly mod name for this instance.</summary> + private readonly string ModName; + + /// <summary>Manages console commands.</summary> + private readonly CommandManager CommandManager; + + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="modName">The friendly mod name for this instance.</param> + /// <param name="commandManager">Manages console commands.</param> + public CommandHelper(string modName, CommandManager commandManager) + { + this.ModName = modName; + this.CommandManager = commandManager; + } + + /// <summary>Add a console command.</summary> + /// <param name="name">The command name, which the user must type to trigger it.</param> + /// <param name="documentation">The human-readable documentation shown when the player runs the built-in 'help' command.</param> + /// <param name="callback">The method to invoke when the command is triggered. This method is passed the command name and arguments submitted by the user.</param> + /// <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 ICommandHelper Add(string name, string documentation, Action<string, string[]> callback) + { + this.CommandManager.Add(this.ModName, name, documentation, callback); + return this; + } + + /// <summary>Trigger a command.</summary> + /// <param name="name">The command name.</param> + /// <param name="arguments">The command arguments.</param> + /// <returns>Returns whether a matching command was triggered.</returns> + public bool Trigger(string name, string[] arguments) + { + return this.CommandManager.Trigger(name, arguments); + } + } +}
\ No newline at end of file diff --git a/src/StardewModdingAPI/Framework/CommandManager.cs b/src/StardewModdingAPI/Framework/CommandManager.cs new file mode 100644 index 00000000..3aa4bf97 --- /dev/null +++ b/src/StardewModdingAPI/Framework/CommandManager.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace StardewModdingAPI.Framework +{ + /// <summary>Manages console commands.</summary> + internal class CommandManager + { + /********* + ** Properties + *********/ + /// <summary>The commands registered with SMAPI.</summary> + private readonly IDictionary<string, Command> Commands = new Dictionary<string, Command>(StringComparer.InvariantCultureIgnoreCase); + + + /********* + ** Public methods + *********/ + /// <summary>Add a console command.</summary> + /// <param name="modName">The friendly mod name for this instance.</param> + /// <param name="name">The command name, which the user must type to trigger it.</param> + /// <param name="documentation">The human-readable documentation shown when the player runs the built-in 'help' command.</param> + /// <param name="callback">The method to invoke when the command is triggered. This method is passed the command name and arguments submitted by the user.</param> + /// <param name="allowNullCallback">Whether to allow a null <paramref name="callback"/> argument; this should only used for backwards compatibility.</param> + /// <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(string modName, string name, string documentation, Action<string, string[]> callback, bool allowNullCallback = false) + { + name = this.GetNormalisedName(name); + + // validate format + if (string.IsNullOrWhiteSpace(name)) + throw new ArgumentNullException(nameof(name), "Can't register a command with no name."); + if (name.Any(char.IsWhiteSpace)) + throw new FormatException($"Can't register the '{name}' command because the name can't contain whitespace."); + if (callback == null && !allowNullCallback) + throw new ArgumentNullException(nameof(callback), $"Can't register the '{name}' command because without a callback."); + + // ensure uniqueness + if (this.Commands.ContainsKey(name)) + throw new ArgumentException(nameof(callback), $"Can't register the '{name}' command because there's already a command with that name."); + + // add command + this.Commands.Add(name, new Command(modName, name, documentation, callback)); + } + + /// <summary>Get a command by its unique name.</summary> + /// <param name="name">The command name.</param> + /// <returns>Returns the matching command, or <c>null</c> if not found.</returns> + public Command Get(string name) + { + name = this.GetNormalisedName(name); + Command command; + this.Commands.TryGetValue(name, out command); + return command; + } + + /// <summary>Get all registered commands.</summary> + public IEnumerable<Command> GetAll() + { + return this.Commands + .Values + .OrderBy(p => p.Name); + } + + /// <summary>Trigger a command.</summary> + /// <param name="input">The raw command input.</param> + /// <returns>Returns whether a matching command was triggered.</returns> + public bool Trigger(string input) + { + string[] args = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + string name = args[0]; + args = args.Skip(1).ToArray(); + + return this.Trigger(name, args); + } + + /// <summary>Trigger a command.</summary> + /// <param name="name">The command name.</param> + /// <param name="arguments">The command arguments.</param> + /// <returns>Returns whether a matching command was triggered.</returns> + public bool Trigger(string name, string[] arguments) + { + // get normalised name + name = this.GetNormalisedName(name); + if (name == null) + return false; + + // get command + Command command; + if (this.Commands.TryGetValue(name, out command)) + { + command.Callback.Invoke(name, arguments); + return true; + } + return false; + } + + /********* + ** Private methods + *********/ + /// <summary>Get a normalised command name.</summary> + /// <param name="name">The command name.</param> + private string GetNormalisedName(string name) + { + name = name?.Trim().ToLower(); + return !string.IsNullOrWhiteSpace(name) + ? name + : null; + } + } +}
\ No newline at end of file diff --git a/src/StardewModdingAPI/Framework/ModHelper.cs b/src/StardewModdingAPI/Framework/ModHelper.cs index 4a3d5ed5..04767de5 100644 --- a/src/StardewModdingAPI/Framework/ModHelper.cs +++ b/src/StardewModdingAPI/Framework/ModHelper.cs @@ -27,17 +27,22 @@ namespace StardewModdingAPI.Framework /// <summary>Metadata about loaded mods.</summary> public IModRegistry ModRegistry { get; } + /// <summary>An API for managing console commands.</summary> + public ICommandHelper ConsoleCommands { get; } + /********* ** Public methods *********/ /// <summary>Construct an instance.</summary> + /// <param name="modName">The friendly mod name.</param> /// <param name="modDirectory">The mod directory path.</param> /// <param name="jsonHelper">Encapsulate SMAPI's JSON parsing.</param> /// <param name="modRegistry">Metadata about loaded mods.</param> + /// <param name="commandManager">Manages console commands.</param> /// <exception cref="ArgumentNullException">An argument is null or empty.</exception> /// <exception cref="InvalidOperationException">The <paramref name="modDirectory"/> path does not exist on disk.</exception> - public ModHelper(string modDirectory, JsonHelper jsonHelper, IModRegistry modRegistry) + public ModHelper(string modName, string modDirectory, JsonHelper jsonHelper, IModRegistry modRegistry, CommandManager commandManager) { // validate if (string.IsNullOrWhiteSpace(modDirectory)) @@ -53,6 +58,7 @@ namespace StardewModdingAPI.Framework this.JsonHelper = jsonHelper; this.DirectoryPath = modDirectory; this.ModRegistry = modRegistry; + this.ConsoleCommands = new CommandHelper(modName, commandManager); } /**** |