Skip to content

Alt commands #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/SampSharp.Entities/SAMP/Commands/CommandInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Linq;

namespace SampSharp.Entities.SAMP.Commands
{
/// <summary>
Expand All @@ -23,18 +25,30 @@ public class CommandInfo
/// <summary>
/// Initializes a new instance of the <see cref="CommandInfo" /> class.
/// </summary>
/// <param name="name">The name of the command.</param>
/// <param name="names">The names of the command.</param>
/// <param name="parameters">The parameters of the command.</param>
public CommandInfo(string name, CommandParameterInfo[] parameters)
/// <param name="ignoreCase">Ignore the names case</param>
public CommandInfo(string[] names, CommandParameterInfo[] parameters, bool ignoreCase)
{
Name = name;
Names = names;
Parameters = parameters;
IgnoreCase = ignoreCase;
}

/// <summary>
/// Gets the name of this command.
/// Gets the names of this command.
/// </summary>
public string[] Names { get; }

/// <summary>
/// Gets whether the command names case are ignored.
/// </summary>
public bool IgnoreCase { get; }

/// <summary>
/// Gets the display name of this command.
/// </summary>
public string Name { get; }
public string DisplayName { get => Names.OrderByDescending(n => n.Length).First(); }

/// <summary>
/// Gets the parameters of this command.
Expand Down
36 changes: 25 additions & 11 deletions src/SampSharp.Entities/SAMP/Commands/CommandServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,22 @@ protected InvokeResult Invoke(IServiceProvider services, object[] prefix, string
inputText = inputText.Substring(name.Length);

// Find commands with the name
if (!_commands.TryGetValue(name, out var commands))
if (!_commands.TryGetValue(name.ToLower(), out var commands))
return InvokeResult.CommandNotFound;

foreach (var command in commands)
{
if (!command.Info.IgnoreCase)
{
for(int i = 0; i < command.Info.Names.Length; i++)
{
if (command.Info.Names[i] == name)
break;
if( i == command.Info.Names.Length - 1)
return InvokeResult.CommandNotFound;
}
}

var cmdInput = inputText;

// Parse the command arguments using the parsers provided by the command
Expand Down Expand Up @@ -171,8 +182,8 @@ protected InvokeResult Invoke(IServiceProvider services, object[] prefix, string
private static string CommandText(CommandInfo command)
{
return command.Parameters.Length == 0
? $"Usage: {command.Name}"
: $"Usage: {command.Name} " + string.Join(" ",
? $"Usage: {command.DisplayName}"
: $"Usage: {command.DisplayName} " + string.Join(" ",
command.Parameters.Select(arg => arg.IsRequired ? $"[{arg.Name}]" : $"<{arg.Name}>"));
}

Expand Down Expand Up @@ -307,9 +318,9 @@ private void CreateCommandsFromAssemblies(int prefixParameters)

foreach (var (method, commandInfo) in methods)
{
// Determine command name.
var name = commandInfo.Name ?? GetCommandName(method);
if (name == null)
// Determine command names.
var names = commandInfo.Names ?? new[] { GetCommandName(method) };
if (names == null)
continue;

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

var info = new CommandInfo(name, parameters);
var info = new CommandInfo(names, parameters, commandInfo.IgnoreCase);

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

if (!_commands.TryGetValue(info.Name, out var lst))
lst = _commands[info.Name] = new List<CommandData>();

lst.Add(data);
foreach(string name in info.Names)
{
if (!_commands.TryGetValue(name.ToLower(), out var lst))
lst = _commands[name.ToLower()] = new List<CommandData>();

lst.Add(data);
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/SampSharp.Entities/SAMP/Commands/ICommandMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ namespace SampSharp.Entities.SAMP.Commands
public interface ICommandMethodInfo
{
/// <summary>
/// Gets the overriden name of the command.
/// Gets the overriden names of the command.
/// </summary>
string Name { get; }
string[] Names { get; }

/// <summary>
/// Gets whether the case is ignored or not.
/// </summary>
bool IgnoreCase { get; }
}
}
19 changes: 16 additions & 3 deletions src/SampSharp.Entities/SAMP/Commands/PlayerCommandAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,25 @@ public PlayerCommandAttribute()
/// Initializes a new instance of the <see cref="PlayerCommandAttribute"/> class.
/// </summary>
/// <param name="name">The overridden name of the command.</param>
public PlayerCommandAttribute(string name)
public PlayerCommandAttribute(string name) : this(new[] { name })
{
Name = name;

}

/// <summary>
/// Initializes a new instance of the <see cref="PlayerCommandAttribute"/> class.
/// </summary>
/// <param name="names">The overridden names of the command.</param>
public PlayerCommandAttribute(params string[] names)
{
Names = names;
IgnoreCase = true;
}

/// <inheritdoc />
public string[] Names { get; set; }

/// <inheritdoc />
public string Name { get; set; }
public bool IgnoreCase { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/SampSharp.Entities/SAMP/Commands/PlayerCommandService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ protected override bool ValidateInputText(ref string input)
private static string CommandText(CommandInfo command)
{
return command.Parameters.Length == 0
? $"Usage: /{command.Name}"
: $"Usage: /{command.Name} " + string.Join(" ",
? $"Usage: /{command.DisplayName}"
: $"Usage: /{command.DisplayName} " + string.Join(" ",
command.Parameters.Select(arg => arg.IsRequired ? $"[{arg.Name}]" : $"<{arg.Name}>"));
}

Expand Down
18 changes: 15 additions & 3 deletions src/SampSharp.Entities/SAMP/Commands/RconCommandAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,24 @@ public RconCommandAttribute()
/// Initializes a new instance of the <see cref="RconCommandAttribute"/> class.
/// </summary>
/// <param name="name">The overridden name of the command.</param>
public RconCommandAttribute(string name)
public RconCommandAttribute(string name) : this(new[] { name })
{
}

/// <summary>
/// Initializes a new instance of the <see cref="RconCommandAttribute"/> class.
/// </summary>
/// <param name="names">The overridden name of the command.</param>
public RconCommandAttribute(params string[] names)
{
Name = name;
Names = names;
IgnoreCase = true;
}

/// <inheritdoc />
public string Name { get; set; }
public string[] Names { get; set; }

/// <inheritdoc />
public bool IgnoreCase { get; set; }
}
}