1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StardewModdingAPI
{
public class Command
{
internal static List<Command> RegisteredCommands = new List<Command>();
public String CommandName;
public String CommandDesc;
public String[] CommandArgs;
public String[] CalledArgs;
public event EventHandler<EventArgsCommand> CommandFired;
/// <summary>
/// Calls the specified command. (It runs the command)
/// </summary>
/// <param name="input">The command to run</param>
public static void CallCommand(string input)
{
input = input.TrimEnd(new[] {' '});
string[] args = new string[0];
Command fnd;
if (input.Contains(" "))
{
args = input.Split(new[] {" "}, 2, StringSplitOptions.RemoveEmptyEntries);
fnd = FindCommand(args[0]);
args = args[1].Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
}
else
{
fnd = FindCommand(input);
}
if (fnd != null)
{
fnd.CalledArgs = args;
fnd.Fire();
}
else
{
Program.LogError("Unknown Command");
}
}
/// <summary>
/// Registers a command to the list of commands properly
/// </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)
{
Command c = new Command(command, cdesc, args);
if (RegisteredCommands.Contains(c))
{
Program.LogError("Command already registered! [{0}]", c.CommandName);
return RegisteredCommands.Find(x => x.Equals(c));
}
RegisteredCommands.Add(c);
Program.LogColour(ConsoleColor.Cyan, "Registered command: " + command);
return c;
}
/// <summary>
/// Looks up a command in the list of registered commands. Returns null if it doesn't exist (I think)
/// </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>
/// 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;
}
/// <summary>
/// Runs a command. Fires it. Calls it. Any of those.
/// </summary>
public void Fire()
{
if (CommandFired == null)
{
Program.LogError("Command failed to fire because it's fire event is null: " + CommandName);
return;
}
CommandFired.Invoke(this, null);
}
}
}
|