MystBin
/2c965b9e95b80075f2 Created 6 months ago...
Raw
Command.cs Hide Copy Raw
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
111
public abstract class Command { private readonly string _name; private Argument[] _arguments; private readonly string? _doc; public string Name { get { return _name; } } public Argument[] Arguments { get { return _arguments; } set { _arguments = value; } } public string? Docstring { get { return _doc; } } public static List<Command> CreatedCommands { get { List<Command> names = []; foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) { foreach (var type in asm.GetTypes()) { if (type.BaseType == typeof(Command)) { var inst = Activator.CreateInstance(type); if (inst == null) throw new ArgumentNullException("Null error when searching for commands."); names.Add((Command)inst); } } } return names; } } public Command(string name) { _name = name; _arguments = [name]; _doc = null; } public Command(string name, string docstring) { _name = name; _arguments = []; _doc = docstring.Trim(); } public Command(string name, Argument[] arguments) { _name = name; _arguments = [.. arguments]; _doc = null; } public Command(string name, Argument[] arguments, string docstring) { _name = name; _arguments = [.. arguments]; _doc = docstring.Trim(); } public virtual void Callback() { throw new NotImplementedException("this method must be overrided in a subclass."); } public override bool Equals(object? obj) { if (obj is null) return false; Command cmd = (Command)obj; return cmd.Name == Name; } public override int GetHashCode() => Name.GetHashCode(); public static bool operator ==(Command? cmd1, Command? cmd2) { if (cmd1 is null) return cmd2 is null; if (cmd2 is null) return cmd1 is null; return cmd1.Equals(cmd2); } public static bool operator !=(Command? cmd1, Command? cmd2) { if (cmd1 is null) return cmd2 is not null; if (cmd2 is null) return cmd1 is not null; return !cmd1.Equals(cmd2); } public static bool operator ==(string str, Command cmd) => cmd.Name == str; public static bool operator !=(string str, Command cmd) => cmd.Name != str; // public static bool operator ==(Command? cmd, string str) => cmd.Name == str; // public static bool operator !=(Command? cmd, string str) => cmd.Name != str; public static bool operator ==(Command? cmd, string str) { if (cmd is null) return false; return cmd.Name == str; } public static bool operator !=(Command? cmd, string str) { if (cmd is null) return false; return cmd.Name != str; } }