Exemple #1
0
        public string[] GetSuggestions(string text, int index)
        {
            var args         = text.Substring(0, index).Split(' ').ToArray();
            var commandStart = index >= text.Length ? string.Empty : text.Substring(index, text.Length - index);

            var commands = m_CommandStore.Commands;
            var context  = m_CommandContextBuilder.CreateContext(m_ConsoleActorAccessor.Actor, args, string.Empty, commands);

            IEnumerable <ICommandRegistration> matchingCommands;

            if (context.CommandRegistration == null)
            {
                matchingCommands = commands.Where(d =>
                                                  d.ParentId == null &&
                                                  d.Name.StartsWith(commandStart, StringComparison.OrdinalIgnoreCase));
            }
            else
            {
                matchingCommands = commands.Where(d =>
                                                  !string.IsNullOrEmpty(d.ParentId) &&
                                                  d.ParentId.Equals(context.CommandRegistration.Id, StringComparison.OrdinalIgnoreCase) &&
                                                  d.Name.StartsWith(commandStart, StringComparison.OrdinalIgnoreCase));
            }

            AsyncHelper.RunSync(async() => await context.DisposeAsync());
            return(matchingCommands.Select(d => d.Name).ToArray());
        }
Exemple #2
0
        protected override async Task OnExecuteAsync()
        {
            var commands = await m_CommandStore.GetCommandsAsync();

            var totalCount = commands.Count;

            const int itemsPerPage = 10;

            int currentPage = 1;

            if (Context.Parameters.Length == 0 || Context.Parameters.TryGet(0, out currentPage))
            {
                if (currentPage < 1)
                {
                    throw new CommandWrongUsageException(Context);
                }

                var pageCommands = commands
                                   .Where(d => d.ParentId == null)
                                   .Skip(itemsPerPage * (currentPage - 1))
                                   .Take(itemsPerPage)
                                   .ToList();

                await PrintPageAsync(currentPage, (int)Math.Ceiling((double)totalCount / itemsPerPage), pageCommands);
            }
            else if (Context.Parameters.Length > 0)
            {
                var context    = m_CommandContextBuilder.CreateContext(Context.Actor, Context.Parameters.ToArray(), Context.CommandPrefix, commands);
                var permission = GetPermission(context.CommandRegistration, commands);

                if (context.CommandRegistration == null)
                {
                    await Context.Actor.PrintMessageAsync(m_StringLocalizer["commands:errors:not_found", new { CommandName = context.GetCommandLine(false) }], Color.Red);

                    return;
                }

                if (!string.IsNullOrEmpty(permission) && await m_PermissionChecker.CheckPermissionAsync(Context.Actor, permission) != PermissionGrantResult.Grant)
                {
                    throw new NotEnoughPermissionException(m_StringLocalizer, permission);
                }

                await PrintCommandHelpAsync(context, permission, commands);

                await context.DisposeAsync();
            }
        }
Exemple #3
0
        public async Task WriteHelpFileAsync()
        {
            StringBuilder markdownBuilder = new StringBuilder();

            markdownBuilder.Append("# ").AppendLine(m_Plugin.DisplayName);
            markdownBuilder.Append("Id: ").AppendLine(m_Plugin.OpenModComponentId);
            markdownBuilder.Append("Version: ").AppendLine(m_Plugin.Version.ToString());
            if (!string.IsNullOrEmpty(m_Plugin.Author))
            {
                markdownBuilder.Append("Author: ").AppendLine(m_Plugin.Author);
            }
            if (!string.IsNullOrEmpty(m_Plugin.Website))
            {
                markdownBuilder.Append("Website: ").AppendLine(m_Plugin.Website);
            }

            var commands = (await m_CommandStore.GetCommandsAsync())
                           .Where(d => d.Component == m_Plugin)
                           .ToList();

            var rootCommands = commands
                               .Where(d => string.IsNullOrEmpty(d.ParentId));

            if (rootCommands.Any())
            {
                markdownBuilder.AppendLine();
                markdownBuilder.AppendLine("## Commands");
                foreach (var currentCommand in rootCommands)
                {
                    var ctx = m_CommandContextBuilder.CreateContext(null, new string[] { currentCommand.Name }, string.Empty, commands);
                    AppendCommand(markdownBuilder, currentCommand, commands, ctx);
                }
            }

            var permissions = m_PermissionRegistry.GetPermissions(m_Plugin)
                              .Where(d => !m_PrintedCommandPermissions.Any(e =>
                                                                           e.Permission.Equals(d.Permission, StringComparison.OrdinalIgnoreCase)));

            if (permissions.Any())
            {
                markdownBuilder.AppendLine();
                markdownBuilder.AppendLine("## Permissions");
                foreach (var permission in permissions)
                {
                    markdownBuilder.Append("- ").Append(permission.Owner.OpenModComponentId).Append(':').Append(permission.Permission);
                    if (!string.IsNullOrEmpty(permission.Description))
                    {
                        markdownBuilder.Append(": ").Append(permission.Description);
                    }

                    markdownBuilder.AppendLine();
                }
            }

            var directory = m_Plugin.WorkingDirectory;

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            var filePath = Path.Combine(directory, "help.md");

            File.WriteAllText(filePath, markdownBuilder.ToString());
        }
Exemple #4
0
        private void AppendCommand(
            StringBuilder markdownBuilder,
            ICommandRegistration command,
            List <ICommandRegistration> commands,
            List <string> args)
        {
            var ctx = m_CommandContextBuilder.CreateContext(null, args.ToArray(), string.Empty, commands);

            try
            {
                markdownBuilder.Append($"- {ctx.CommandPrefix}{ctx.CommandAlias}");
                if (!string.IsNullOrEmpty(command.Syntax))
                {
                    markdownBuilder.Append($" {command.Syntax}");
                }

                if (!string.IsNullOrEmpty(command.Description))
                {
                    markdownBuilder.Append($": {command.Description}");
                }

                markdownBuilder.AppendLine("  ");
                markdownBuilder.AppendLine($"  id: {command.Id}  ");
                var permissionRegistrations = new List <IPermissionRegistration>();

                var commandPermission = m_PermissionBuilder.GetPermission(command, commands);

                var commandPermissionRegistration = m_PermissionRegistry.FindPermission(m_Plugin, commandPermission);
                if (commandPermissionRegistration != null)
                {
                    permissionRegistrations.Add(commandPermissionRegistration);
                }

                if (command.PermissionRegistrations != null)
                {
                    var prefixedPermissions = new List <IPermissionRegistration>();

                    foreach (var permission in command.PermissionRegistrations)
                    {
                        prefixedPermissions.Add(new PermissionRegistration
                        {
                            Permission   = commandPermission + "." + permission.Permission,
                            Description  = permission.Description,
                            DefaultGrant = permission.DefaultGrant,
                            Owner        = permission.Owner
                        });
                    }

                    permissionRegistrations.AddRange(prefixedPermissions);
                }

                if (permissionRegistrations.Count > 0)
                {
                    markdownBuilder.AppendLine($"  permissions: ");

                    foreach (var permissionRegistration in permissionRegistrations)
                    {
                        markdownBuilder.Append($"  - {permissionRegistration.Owner.OpenModComponentId}:{permissionRegistration.Permission}");
                        if (!string.IsNullOrEmpty(permissionRegistration.Description))
                        {
                            markdownBuilder.Append($": {permissionRegistration.Description}");
                        }

                        markdownBuilder.AppendLine("  ");
                    }

                    m_PrintedCommandPermissions.AddRange(permissionRegistrations);
                }

                var childCommands = commands
                                    .Where(d => string.Equals(d.ParentId, command.Id, StringComparison.OrdinalIgnoreCase));
                foreach (var child in childCommands)
                {
                    args.Add(child.Name);
                    AppendCommand(markdownBuilder, child, commands, args);
                }
            }
            finally
            {
                ctx.DisposeAsync().GetAwaiter().GetResult();
            }
        }