rewrite command parsing, fix null reference exceptions in some cases

This commit is contained in:
Jesse Plamondon-Willard 2016-11-10 10:11:02 -05:00
parent f9983a4bca
commit 179df57ce8
2 changed files with 22 additions and 19 deletions

View File

@ -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;
if (command != null)
// tokenise input
string[] args = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string commandName = args[0];
args = args.Skip(1).ToArray();
// 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));
}
}
}
}

View File

@ -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";