Skip to content

Commit 20a00ab

Browse files
committed
Added alt commands and ignore case option for commands
1 parent 85c0485 commit 20a00ab

File tree

6 files changed

+84
-26
lines changed

6 files changed

+84
-26
lines changed

src/SampSharp.Entities/SAMP/Commands/CommandInfo.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
using System.Linq;
17+
1618
namespace SampSharp.Entities.SAMP.Commands
1719
{
1820
/// <summary>
@@ -23,18 +25,30 @@ public class CommandInfo
2325
/// <summary>
2426
/// Initializes a new instance of the <see cref="CommandInfo" /> class.
2527
/// </summary>
26-
/// <param name="name">The name of the command.</param>
28+
/// <param name="names">The names of the command.</param>
2729
/// <param name="parameters">The parameters of the command.</param>
28-
public CommandInfo(string name, CommandParameterInfo[] parameters)
30+
/// <param name="ignoreCase">Ignore the names case</param>
31+
public CommandInfo(string[] names, CommandParameterInfo[] parameters, bool ignoreCase)
2932
{
30-
Name = name;
33+
Names = names;
3134
Parameters = parameters;
35+
IgnoreCase = ignoreCase;
3236
}
3337

3438
/// <summary>
35-
/// Gets the name of this command.
39+
/// Gets the names of this command.
40+
/// </summary>
41+
public string[] Names { get; }
42+
43+
/// <summary>
44+
/// Gets whether the command names case are ignored.
45+
/// </summary>
46+
public bool IgnoreCase { get; }
47+
48+
/// <summary>
49+
/// Gets the display name of this command.
3650
/// </summary>
37-
public string Name { get; }
51+
public string DisplayName { get => Names.OrderByDescending(n => n.Length).First(); }
3852

3953
/// <summary>
4054
/// Gets the parameters of this command.

src/SampSharp.Entities/SAMP/Commands/CommandServiceBase.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,22 @@ protected InvokeResult Invoke(IServiceProvider services, object[] prefix, string
8585
inputText = inputText.Substring(name.Length);
8686

8787
// Find commands with the name
88-
if (!_commands.TryGetValue(name, out var commands))
88+
if (!_commands.TryGetValue(name.ToLower(), out var commands))
8989
return InvokeResult.CommandNotFound;
9090

9191
foreach (var command in commands)
9292
{
93+
if (!command.Info.IgnoreCase)
94+
{
95+
for(int i = 0; i < command.Info.Names.Length; i++)
96+
{
97+
if (command.Info.Names[i] == name)
98+
break;
99+
if( i == command.Info.Names.Length - 1)
100+
return InvokeResult.CommandNotFound;
101+
}
102+
}
103+
93104
var cmdInput = inputText;
94105

95106
// Parse the command arguments using the parsers provided by the command
@@ -171,8 +182,8 @@ protected InvokeResult Invoke(IServiceProvider services, object[] prefix, string
171182
private static string CommandText(CommandInfo command)
172183
{
173184
return command.Parameters.Length == 0
174-
? $"Usage: {command.Name}"
175-
: $"Usage: {command.Name} " + string.Join(" ",
185+
? $"Usage: {command.DisplayName}"
186+
: $"Usage: {command.DisplayName} " + string.Join(" ",
176187
command.Parameters.Select(arg => arg.IsRequired ? $"[{arg.Name}]" : $"<{arg.Name}>"));
177188
}
178189

@@ -307,9 +318,9 @@ private void CreateCommandsFromAssemblies(int prefixParameters)
307318

308319
foreach (var (method, commandInfo) in methods)
309320
{
310-
// Determine command name.
311-
var name = commandInfo.Name ?? GetCommandName(method);
312-
if (name == null)
321+
// Determine command names.
322+
var names = commandInfo.Names ?? new[] { GetCommandName(method) };
323+
if (names == null)
313324
continue;
314325

315326
// Validate acceptable return type.
@@ -323,7 +334,7 @@ private void CreateCommandsFromAssemblies(int prefixParameters)
323334
if (!TryCollectParameters(methodParameters, prefixParameters, out var parameters))
324335
continue;
325336

326-
var info = new CommandInfo(name, parameters);
337+
var info = new CommandInfo(names, parameters, commandInfo.IgnoreCase);
327338

328339
var argsPtr = 0; // The current pointer in the event arguments array.
329340
var parameterSources = methodParameters
@@ -362,10 +373,13 @@ private void CreateCommandsFromAssemblies(int prefixParameters)
362373
SystemType = method.DeclaringType
363374
};
364375

365-
if (!_commands.TryGetValue(info.Name, out var lst))
366-
lst = _commands[info.Name] = new List<CommandData>();
367-
368-
lst.Add(data);
376+
foreach(string name in info.Names)
377+
{
378+
if (!_commands.TryGetValue(name.ToLower(), out var lst))
379+
lst = _commands[name.ToLower()] = new List<CommandData>();
380+
381+
lst.Add(data);
382+
}
369383
}
370384
}
371385

src/SampSharp.Entities/SAMP/Commands/ICommandMethodInfo.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ namespace SampSharp.Entities.SAMP.Commands
2121
public interface ICommandMethodInfo
2222
{
2323
/// <summary>
24-
/// Gets the overriden name of the command.
24+
/// Gets the overriden names of the command.
2525
/// </summary>
26-
string Name { get; }
26+
string[] Names { get; }
27+
28+
/// <summary>
29+
/// Gets whether the case is ignored or not.
30+
/// </summary>
31+
bool IgnoreCase { get; }
2732
}
2833
}

src/SampSharp.Entities/SAMP/Commands/PlayerCommandAttribute.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,25 @@ public PlayerCommandAttribute()
2121
/// Initializes a new instance of the <see cref="PlayerCommandAttribute"/> class.
2222
/// </summary>
2323
/// <param name="name">The overridden name of the command.</param>
24-
public PlayerCommandAttribute(string name)
24+
public PlayerCommandAttribute(string name) : this(new[] { name })
2525
{
26-
Name = name;
26+
2727
}
2828

29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="PlayerCommandAttribute"/> class.
31+
/// </summary>
32+
/// <param name="names">The overridden names of the command.</param>
33+
public PlayerCommandAttribute(params string[] names)
34+
{
35+
Names = names;
36+
IgnoreCase = true;
37+
}
38+
39+
/// <inheritdoc />
40+
public string[] Names { get; set; }
41+
2942
/// <inheritdoc />
30-
public string Name { get; set; }
43+
public bool IgnoreCase { get; set; }
3144
}
3245
}

src/SampSharp.Entities/SAMP/Commands/PlayerCommandService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ protected override bool ValidateInputText(ref string input)
8787
private static string CommandText(CommandInfo command)
8888
{
8989
return command.Parameters.Length == 0
90-
? $"Usage: /{command.Name}"
91-
: $"Usage: /{command.Name} " + string.Join(" ",
90+
? $"Usage: /{command.DisplayName}"
91+
: $"Usage: /{command.DisplayName} " + string.Join(" ",
9292
command.Parameters.Select(arg => arg.IsRequired ? $"[{arg.Name}]" : $"<{arg.Name}>"));
9393
}
9494

src/SampSharp.Entities/SAMP/Commands/RconCommandAttribute.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,24 @@ public RconCommandAttribute()
3636
/// Initializes a new instance of the <see cref="RconCommandAttribute"/> class.
3737
/// </summary>
3838
/// <param name="name">The overridden name of the command.</param>
39-
public RconCommandAttribute(string name)
39+
public RconCommandAttribute(string name) : this(new[] { name })
40+
{
41+
}
42+
43+
/// <summary>
44+
/// Initializes a new instance of the <see cref="RconCommandAttribute"/> class.
45+
/// </summary>
46+
/// <param name="names">The overridden name of the command.</param>
47+
public RconCommandAttribute(params string[] names)
4048
{
41-
Name = name;
49+
Names = names;
50+
IgnoreCase = true;
4251
}
4352

4453
/// <inheritdoc />
45-
public string Name { get; set; }
54+
public string[] Names { get; set; }
55+
56+
/// <inheritdoc />
57+
public bool IgnoreCase { get; set; }
4658
}
4759
}

0 commit comments

Comments
 (0)