Пример #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 <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));
        }