SMAPI/StardewModdingAPI/Command.cs

109 lines
3.7 KiB
C#
Raw Normal View History

2016-03-23 08:36:04 +08:00
using System;
using System.Collections.Generic;
using StardewModdingAPI.Events;
namespace StardewModdingAPI
{
public class Command
{
internal static List<Command> RegisteredCommands = new List<Command>();
public string[] CalledArgs;
public string[] CommandArgs;
public string CommandDesc;
public string CommandName;
/// <summary>
/// Creates a Command from a Name, Description, and Arguments
/// </summary>
/// <param name="cname">Name</param>
/// <param name="cdesc">Description</param>
/// <param name="args">Arguments</param>
public Command(string cname, string cdesc, string[] args = null)
{
CommandName = cname;
CommandDesc = cdesc;
if (args == null)
args = new string[0];
CommandArgs = args;
}
2016-03-23 08:36:04 +08:00
public event EventHandler<EventArgsCommand> CommandFired;
/// <summary>
/// Calls the specified command. (It runs the command)
2016-03-23 08:36:04 +08:00
/// </summary>
/// <param name="input">The command to run</param>
public static void CallCommand(string input)
{
input = input.TrimEnd(' ');
var args = new string[0];
2016-03-23 08:36:04 +08:00
Command fnd;
if (input.Contains(" "))
{
args = input.Split(new[] {" "}, 2, StringSplitOptions.RemoveEmptyEntries);
fnd = FindCommand(args[0]);
args = args[1].Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries);
2016-03-23 08:36:04 +08:00
}
else
{
fnd = FindCommand(input);
}
if (fnd != null)
{
fnd.CalledArgs = args;
fnd.Fire();
}
else
{
Log.AsyncR("Unknown Command");
2016-03-23 08:36:04 +08:00
}
}
/// <summary>
/// Registers a command to the list of commands properly
2016-03-23 08:36:04 +08:00
/// </summary>
/// <param name="command">Name of the command to register</param>
/// <param name="cdesc">Description</param>
/// <param name="args">Arguments (these are purely for viewing so that a user can see what an argument needs to be)</param>
/// <returns></returns>
public static Command RegisterCommand(string command, string cdesc, string[] args = null)
{
var c = new Command(command, cdesc, args);
2016-03-23 08:36:04 +08:00
if (RegisteredCommands.Contains(c))
{
Log.AsyncR($"Command already registered! [{c.CommandName}]");
2016-03-23 08:36:04 +08:00
return RegisteredCommands.Find(x => x.Equals(c));
}
RegisteredCommands.Add(c);
2016-03-27 13:32:15 +08:00
Log.Async("Registered command: " + command);
2016-03-23 08:36:04 +08:00
return c;
}
/// <summary>
/// Looks up a command in the list of registered commands. Returns null if it doesn't exist (I think)
2016-03-23 08:36:04 +08:00
/// </summary>
/// <param name="name">Name of command to find</param>
/// <returns></returns>
public static Command FindCommand(string name)
{
return RegisteredCommands.Find(x => x.CommandName.Equals(name));
}
/// <summary>
/// Runs a command. Fires it. Calls it. Any of those.
2016-03-23 08:36:04 +08:00
/// </summary>
public void Fire()
{
if (CommandFired == null)
{
Log.AsyncR("Command failed to fire because it's fire event is null: " + CommandName);
2016-03-23 08:36:04 +08:00
return;
}
CommandFired.Invoke(this, new EventArgsCommand(this));
}
}
}