Implemented command type readers, parser and service.
This commit is contained in:
@@ -1,35 +1,121 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public class Command
|
||||
{
|
||||
private Action<IMessage> _action;
|
||||
private readonly object _instance;
|
||||
private readonly Func<IMessage, IReadOnlyList<object>, Task> _action;
|
||||
|
||||
public string Name { get; }
|
||||
public string Description { get; }
|
||||
public string Text { get; }
|
||||
public Module Module { get; }
|
||||
public IReadOnlyList<CommandParameter> Parameters { get; }
|
||||
|
||||
internal Command(CommandAttribute attribute, MethodInfo methodInfo)
|
||||
internal Command(Module module, object instance, CommandAttribute attribute, MethodInfo methodInfo)
|
||||
{
|
||||
Module = module;
|
||||
_instance = instance;
|
||||
|
||||
Name = methodInfo.Name;
|
||||
Text = attribute.Text;
|
||||
|
||||
var description = methodInfo.GetCustomAttribute<DescriptionAttribute>();
|
||||
if (description != null)
|
||||
Description = description.Text;
|
||||
|
||||
Name = attribute.Name;
|
||||
Text = attribute.Text;
|
||||
Parameters = BuildParameters(methodInfo);
|
||||
_action = BuildAction(methodInfo);
|
||||
}
|
||||
|
||||
public void Invoke(IMessage msg)
|
||||
public async Task<ParseResult> Parse(IMessage msg, SearchResult searchResult)
|
||||
{
|
||||
_action.Invoke(msg);
|
||||
if (!searchResult.IsSuccess)
|
||||
return ParseResult.FromError(searchResult);
|
||||
|
||||
return await CommandParser.ParseArgs(this, msg, searchResult.ArgText, 0).ConfigureAwait(false);
|
||||
}
|
||||
public async Task<ExecuteResult> Execute(IMessage msg, ParseResult parseResult)
|
||||
{
|
||||
if (!parseResult.IsSuccess)
|
||||
return ExecuteResult.FromError(parseResult);
|
||||
|
||||
try
|
||||
{
|
||||
await _action.Invoke(msg, parseResult.Values);//Note: This code may need context
|
||||
return ExecuteResult.FromSuccess();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ExecuteResult.FromError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void BuildAction()
|
||||
private IReadOnlyList<CommandParameter> BuildParameters(MethodInfo methodInfo)
|
||||
{
|
||||
_action = null;
|
||||
//TODO: Implement
|
||||
var parameters = methodInfo.GetParameters();
|
||||
var paramBuilder = ImmutableArray.CreateBuilder<CommandParameter>(parameters.Length - 1);
|
||||
for (int i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
var parameter = parameters[i];
|
||||
var type = parameter.ParameterType;
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
if (type != typeof(IMessage))
|
||||
throw new InvalidOperationException("The first parameter of a command must be IMessage.");
|
||||
else
|
||||
continue;
|
||||
}
|
||||
|
||||
var typeInfo = type.GetTypeInfo();
|
||||
if (typeInfo.IsEnum)
|
||||
type = Enum.GetUnderlyingType(type);
|
||||
|
||||
var reader = Module.Service.GetTypeReader(type);
|
||||
if (reader == null)
|
||||
throw new InvalidOperationException($"This type ({type.FullName}) is not supported.");
|
||||
|
||||
bool isUnparsed = parameter.GetCustomAttribute<UnparsedAttribute>() != null;
|
||||
if (isUnparsed)
|
||||
{
|
||||
if (type != typeof(string))
|
||||
throw new InvalidOperationException("Unparsed parameters only support the string type.");
|
||||
else if (i != parameters.Length - 1)
|
||||
throw new InvalidOperationException("Unparsed parameters must be the last parameter in a command.");
|
||||
}
|
||||
|
||||
string name = parameter.Name;
|
||||
string description = typeInfo.GetCustomAttribute<DescriptionAttribute>()?.Text;
|
||||
bool isOptional = parameter.IsOptional;
|
||||
object defaultValue = parameter.HasDefaultValue ? parameter.DefaultValue : null;
|
||||
|
||||
paramBuilder.Add(new CommandParameter(name, description, reader, isOptional, isUnparsed, defaultValue));
|
||||
}
|
||||
return paramBuilder.ToImmutable();
|
||||
}
|
||||
private Func<IMessage, IReadOnlyList<object>, Task> BuildAction(MethodInfo methodInfo)
|
||||
{
|
||||
//TODO: Temporary reflection hack. Lets build an actual expression tree here.
|
||||
return (msg, args) =>
|
||||
{
|
||||
object[] newArgs = new object[args.Count + 1];
|
||||
newArgs[0] = msg;
|
||||
for (int i = 0; i < args.Count; i++)
|
||||
newArgs[i + 1] = args[i];
|
||||
var result = methodInfo.Invoke(_instance, newArgs);
|
||||
return result as Task ?? Task.CompletedTask;
|
||||
};
|
||||
}
|
||||
|
||||
public override string ToString() => Name;
|
||||
private string DebuggerDisplay => $"{Module.Name}.{Name} ({Text})";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user