예제 #1
0
        /// <summary>
        /// Invoke help for <see cref="ICommandHandler"/> with the supplied <see cref="ICommandContext"/> and <see cref="ParameterQueue"/>.
        /// </summary>
        /// <remarks>
        /// This will display help text if the <see cref="ICommandContext"/> has permission to invoke this <see cref="ICommandHandler"/>.
        /// </remarks>
        public CommandResult InvokeHelp(ICommandContext context, ParameterQueue queue)
        {
            CommandResult result = CanInvoke(context);

            if (result != CommandResult.Ok)
            {
                return(result);
            }

            string command = queue.Front;

            if (command == null ||
                !handlers.TryGetValue(command, out ICommandHandler handler))
            {
                // no additional commands are present show help for category
                var builder = new StringBuilder();
                builder.AppendLine("-----------------------------------------------");
                builder.AppendLine($"Showing help for: {queue.BreadcrumbTrail}");
                GetHelp(builder, context, true);

                context.SendMessage(builder.ToString());

                return(CommandResult.Ok);
            }

            queue.Dequeue();
            return(handler.InvokeHelp(context, queue));
        }
예제 #2
0
        /// <summary>
        /// Invoke help for <see cref="ICommandHandler"/> with the supplied <see cref="ICommandContext"/> and <see cref="ParameterQueue"/>.
        /// </summary>
        /// <remarks>
        /// This will display help text if the <see cref="ICommandContext"/> has permission to invoke this <see cref="ICommandHandler"/>.
        /// </remarks>
        public CommandResult InvokeHelp(ICommandContext context, ParameterQueue queue)
        {
            CommandResult result = CanInvoke(context);

            if (result != CommandResult.Ok)
            {
                return(result);
            }

            var builder = new StringBuilder();

            builder.AppendLine("-----------------------------------------------");
            builder.AppendLine($"Showing help for: {queue.BreadcrumbTrail}");
            builder.AppendLine(helpText);

            context.SendMessage(builder.ToString());

            return(CommandResult.Ok);
        }
예제 #3
0
        /// <summary>
        /// Invoke <see cref="CommandCategory"/> with the supplied <see cref="ICommandContext"/> and <see cref="ParameterQueue"/>.
        /// </summary>
        public virtual CommandResult Invoke(ICommandContext context, ParameterQueue queue)
        {
            CommandResult result = CanInvoke(context);

            if (result != CommandResult.Ok)
            {
                return(result);
            }

            if (queue.Count == 0)
            {
                // no additional commands are present show help for category
                var builder = new StringBuilder();
                builder.AppendLine($"Showing help for: {queue.BreadcrumbTrail}");
                GetHelp(builder, context, true);

                context.SendMessage(builder.ToString());
                return(CommandResult.Ok);
            }

            if (targetType != null)
            {
                if (context.Target != null && !targetType.IsInstanceOfType(context.Target))
                {
                    return(CommandResult.InvalidTarget);
                }

                // invoker will always exist
                if (!targetType.IsInstanceOfType(context.Invoker))
                {
                    return(CommandResult.InvalidTarget);
                }
            }

            string command = queue.Dequeue();

            if (!handlers.TryGetValue(command, out ICommandHandler handler))
            {
                return(CommandResult.NoCommand);
            }

            return(handler.Invoke(context, queue));
        }
예제 #4
0
        /// <summary>
        /// Invoke <see cref="CommandHandler"/>, convert <see cref="ParameterQueue"/> to object parameters before invoking the underlying <see cref="MethodInfo"/>.
        /// </summary>
        public CommandResult Invoke(ICommandContext context, ParameterQueue queue)
        {
            CommandResult result = CanInvoke(context);

            if (result != CommandResult.Ok)
            {
                return(result);
            }

            if (targetType != null)
            {
                if (context.Target != null && !targetType.IsInstanceOfType(context.Target))
                {
                    return(CommandResult.InvalidTarget);
                }

                // invoker will always exist
                if (!targetType.IsInstanceOfType(context.Invoker))
                {
                    return(CommandResult.InvalidTarget);
                }
            }

            try
            {
                // convert ParameterQueue to objects via IParameterConvert interface
                var convertedParameters = new List <object> {
                    context
                };
                foreach (CommandParameter parameter in parameters)
                {
                    object parameterValue = null;
                    if (queue.Count != 0)
                    {
                        parameterValue = parameter.Converter.Convert(context, queue);
                    }
                    else
                    {
                        // if we have no commands left in queue and the parameter isn't optional we don't have enough data
                        if (!parameter.IsOptional)
                        {
                            return(CommandResult.InvalidParameters);
                        }

                        // cache this?
                        if (parameter.Type.IsValueType)
                        {
                            parameterValue = Activator.CreateInstance(parameter.Type);
                        }
                    }

                    convertedParameters.Add(parameterValue);
                }

                methodContainer.Invoke(convertedParameters);
            }
            catch (Exception exception)
            {
                context.SendError(exception.ToString());
            }

            return(CommandResult.Ok);
        }