From 179df57ce8f067dc14e4697b1260bec630f52058 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 10 Nov 2016 10:11:02 -0500 Subject: rewrite command parsing, fix null reference exceptions in some cases --- src/StardewModdingAPI/Command.cs | 39 ++++++++++++++++++++------------------ src/StardewModdingAPI/Constants.cs | 2 +- 2 files changed, 22 insertions(+), 19 deletions(-) (limited to 'src') 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 ****/ - /// Invoke the specified command. + /// Parse a command string and invoke it if valid. /// The command to run, including the command name and any arguments. 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(); } /// Register a command with SMAPI. @@ -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 *********/ /// SMAPI's current semantic version. - public static readonly Version Version = new Version(1, 0, 0, "beta2"); + public static readonly Version Version = new Version(1, 0, 0, "beta3"); /// The minimum supported version of Stardew Valley. public const string MinimumGameVersion = "1.1"; -- cgit