diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-10 10:11:02 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-10 10:11:02 -0500 |
commit | 179df57ce8f067dc14e4697b1260bec630f52058 (patch) | |
tree | af7aed685b3d03e2f65926ca263633f68da02bc5 /src | |
parent | f9983a4bca32160613d06ceb5bcc9326c9c3283d (diff) | |
download | SMAPI-179df57ce8f067dc14e4697b1260bec630f52058.tar.gz SMAPI-179df57ce8f067dc14e4697b1260bec630f52058.tar.bz2 SMAPI-179df57ce8f067dc14e4697b1260bec630f52058.zip |
rewrite command parsing, fix null reference exceptions in some cases
Diffstat (limited to 'src')
-rw-r--r-- | src/StardewModdingAPI/Command.cs | 39 | ||||
-rw-r--r-- | src/StardewModdingAPI/Constants.cs | 2 |
2 files changed, 22 insertions, 19 deletions
diff --git a/src/StardewModdingAPI/Command.cs b/src/StardewModdingAPI/Command.cs index 17924c08..65207f0e 100644 --- a/src/StardewModdingAPI/Command.cs +++ b/src/StardewModdingAPI/Command.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using StardewModdingAPI.Events; namespace StardewModdingAPI @@ -68,29 +69,31 @@ namespace StardewModdingAPI /**** ** SMAPI ****/ - /// <summary>Invoke the specified command.</summary> + /// <summary>Parse a command string and invoke it if valid.</summary> /// <param name="input">The command to run, including the command name and any arguments.</param> public static void CallCommand(string input) { - input = input.TrimEnd(' '); - string[] args = new string[0]; - Command command; - if (input.Contains(" ")) - { - args = input.Split(new[] { " " }, 2, StringSplitOptions.RemoveEmptyEntries); - command = Command.FindCommand(args[0]); - args = args[1].Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); - } - else - command = Command.FindCommand(input); + // normalise input + input = input?.Trim(); + if (string.IsNullOrWhiteSpace(input)) + return; + + // tokenise input + string[] args = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + string commandName = args[0]; + args = args.Skip(1).ToArray(); - if (command != null) + // get command + Command command = Command.FindCommand(commandName); + if (command == null) { - command.CalledArgs = args; - command.Fire(); - } - else Log.AsyncR("Unknown command"); + return; + } + + // fire command + command.CalledArgs = args; + command.Fire(); } /// <summary>Register a command with SMAPI.</summary> @@ -118,4 +121,4 @@ namespace StardewModdingAPI return Command.RegisteredCommands.Find(x => x.CommandName.Equals(name)); } } -}
\ No newline at end of file +} diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index 12496cbf..6c6d8868 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -23,7 +23,7 @@ namespace StardewModdingAPI ** Accessors *********/ /// <summary>SMAPI's current semantic version.</summary> - public static readonly Version Version = new Version(1, 0, 0, "beta2"); + public static readonly Version Version = new Version(1, 0, 0, "beta3"); /// <summary>The minimum supported version of Stardew Valley.</summary> public const string MinimumGameVersion = "1.1"; |