using System.Collections.Generic; using System.Linq; using NitroxModel.Core; using NitroxModel.DataStructures.GameLogic; using NitroxServer.ConsoleCommands.Abstract; using NitroxServer.ConsoleCommands.Abstract.Type; namespace NitroxServer.ConsoleCommands { internal class HelpCommand : Command { public override IEnumerable Aliases { get; } = new[] { "?" }; public HelpCommand() : base("help", Perms.PLAYER, "Displays this") { AddParameter(new TypeString("command", false, "Command to see help information for")); } protected override void Execute(CallArgs args) { List cmdsText; if (args.IsConsole) { cmdsText = GetHelpText(Perms.CONSOLE, false, args.IsValid(0) ? args.Get(0) : null); foreach (string cmdText in cmdsText) { Log.Info(cmdText); } } else { cmdsText = GetHelpText(args.Sender.Value.Permissions, true, args.IsValid(0) ? args.Get(0) : null); foreach (string cmdText in cmdsText) { SendMessageToPlayer(args.Sender, cmdText); } } } private List GetHelpText(Perms permThreshold, bool cropText, string singleCommand) { static bool CanExecuteAndProcess(Command cmd, Perms perms) { return cmd.CanExecute(perms) && !(perms == Perms.CONSOLE && cmd.Flags.HasFlag(PermsFlag.NO_CONSOLE)); } //Runtime query to avoid circular dependencies IEnumerable commands = NitroxServiceLocator.LocateService>(); if (singleCommand != null && !commands.Any(cmd => cmd.Name.Equals(singleCommand))) { return new List { "Command does not exist" }; } List cmdsText = new(); cmdsText.Add(singleCommand != null ? $"=== Showing help for {singleCommand} ===" : "=== Showing command list ==="); cmdsText.AddRange(commands.Where(cmd => CanExecuteAndProcess(cmd, permThreshold) && (singleCommand == null || cmd.Name.Equals(singleCommand))) .OrderByDescending(cmd => cmd.Name) .Select(cmd => cmd.ToHelpText(singleCommand != null, cropText))); return cmdsText; } } }