From 2627b56b95b4a13bdd219da1b416a7c51284558a Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Sun, 7 Mar 2021 20:39:03 +0700 Subject: [PATCH] Change how `Command` data is filled and displayed --- .../Commands/BuiltInCommands/ACommandDosh.uc | 2 +- .../Commands/BuiltInCommands/ACommandHelp.uc | 261 +++++++++++++-- .../Commands/BuiltInCommands/ACommandNick.uc | 2 +- sources/Commands/Command.uc | 314 ++---------------- sources/Commands/CommandDataBuilder.uc | 55 ++- sources/Commands/Commands.uc | 22 +- 6 files changed, 339 insertions(+), 317 deletions(-) diff --git a/sources/Commands/BuiltInCommands/ACommandDosh.uc b/sources/Commands/BuiltInCommands/ACommandDosh.uc index 3b77e3e..e97f692 100644 --- a/sources/Commands/BuiltInCommands/ACommandDosh.uc +++ b/sources/Commands/BuiltInCommands/ACommandDosh.uc @@ -22,6 +22,7 @@ class ACommandDosh extends Command; //'dosh' for giving dosh (subcommand for setting it, options for min/max resulting value, silent) protected function BuildData(CommandDataBuilder builder) { + builder.Name(P("dosh")).Summary(P("Changes how much money player has.")); builder.RequireTarget(); builder.ParamInteger(P("amount")) .Describe(P("Gives (takes if negative) players a specified " @@ -99,5 +100,4 @@ protected function ExecutedFor(APlayer player, CommandCall result) defaultproperties { - commandName = "dosh" } \ No newline at end of file diff --git a/sources/Commands/BuiltInCommands/ACommandHelp.uc b/sources/Commands/BuiltInCommands/ACommandHelp.uc index ec1b877..bd0c0f9 100644 --- a/sources/Commands/BuiltInCommands/ACommandHelp.uc +++ b/sources/Commands/BuiltInCommands/ACommandHelp.uc @@ -22,13 +22,22 @@ class ACommandHelp extends Command var LoggerAPI.Definition testMsg; +var public const int TSPACE, TCOMMAND_NAME_FALLBACK, TPLUS; +var public const int TOPEN_BRACKET, TCLOSE_BRACKET, TCOLUMN_SPACE; +var public const int TKEY, TDOUBLE_KEY, TCOMMA_SPACE, TBOOLEAN, TINDENT; +var public const int TBOOLEAN_TRUE_FALSE, TBOOLEAN_ENABLE_DISABLE; +var public const int TBOOLEAN_ON_OFF, TBOOLEAN_YES_NO; +var public const int TOPTIONS, TCMD_WITH_TARGET, TCMD_WITHOUT_TARGET; + protected function BuildData(CommandDataBuilder builder) { + builder.Name(P("help")) + .Summary(P("Detailed information about available commands.")); builder.OptionalParams() .ParamTextList(P("commands")) - .Describe(P("Displays information about all specified commands.")); + .Describe(P("Display information about all specified commands.")); builder.Option(P("list")) - .Describe(P("Displays list of all available commands.")); + .Describe(P("Display list of all available commands.")); } protected function Executed(CommandCall callInfo) @@ -37,22 +46,34 @@ protected function Executed(CommandCall callInfo) local DynamicArray commandsToDisplay; local APlayer callerPlayer; callerPlayer = callInfo.GetCallerPlayer(); - if (callerPlayer == none) return; + if (callerPlayer == none) { + return; + } - // Command list + // Print command list if "--list" option was specified if (callInfo.GetOptions().HasKey(P("list"))) { DisplayCommandList(callerPlayer); } - // Help pages - parameters = callInfo.GetParameters(); - commandsToDisplay = DynamicArray(parameters.GetItem(P("commands"))); - DisplayCommandHelpPages(callerPlayer, commandsToDisplay); + // Help pages. + // Only need to print them if: + // 1. Any commands are specified as parameters; + // 2. No commands or "--list" option was specified, then we want to + // print a help page for this command. + if ( !callInfo.GetOptions().HasKey(P("list")) + || callInfo.GetParameters().HasKey(P("commands"))) + { + parameters = callInfo.GetParameters(); + commandsToDisplay = DynamicArray(parameters.GetItem(P("commands"))); + DisplayCommandHelpPages(callerPlayer, commandsToDisplay); + } } private final function DisplayCommandList(APlayer player) { local int i; local ConsoleWriter console; + local Command nextCommand; + local Command.Data nextData; local array commandNames; local Commands commandsFeature; if (player == none) return; @@ -61,8 +82,17 @@ private final function DisplayCommandList(APlayer player) console = player.Console(); commandNames = commandsFeature.GetCommandNames(); - for (i = 0; i < commandNames.length; i += 1) { - console.WriteLine(commandNames[i]); + for (i = 0; i < commandNames.length; i += 1) + { + nextCommand = commandsFeature.GetCommand(commandNames[i]); + if (nextCommand == none) continue; + + nextData = nextCommand.GetData(); + console.UseColor(_.color.textEmphasis) + .Write(nextData.name) + .ResetColor() + .Write(T(TCOLUMN_SPACE)) + .WriteLine(nextData.summary); } _.memory.FreeMany(commandNames); } @@ -72,7 +102,6 @@ private final function DisplayCommandHelpPages( DynamicArray commandList) { local int i; - local Text nextHelpPage; local Command nextCommand; local Commands commandsFeature; if (player == none) return; @@ -80,25 +109,217 @@ private final function DisplayCommandHelpPages( if (commandsFeature == none) return; // If arguments were empty - at least display our own help page - if (commandList.GetLength() == 1 && Text(commandList.GetItem(0)).IsEmpty()) + if (commandList == none) { - nextHelpPage = PrintHelp(); - player.Console().WriteLine(nextHelpPage).Flush(); - nextHelpPage.FreeSelf(); + PrintHelpPage(player.Console(), GetData()); return; } + // Otherwise - print help for specified commands for (i = 0; i < commandList.GetLength(); i += 1) { nextCommand = commandsFeature.GetCommand(Text(commandList.GetItem(i))); if (nextCommand == none) continue; - nextHelpPage = nextCommand.PrintHelp(); - player.Console().WriteLine(nextHelpPage); - nextHelpPage.FreeSelf(); + PrintHelpPage(player.Console(), nextCommand.GetData()); + } +} + +// Following methods are mostly self-explanatory, +// all assume that passed `cout != none` +private final function PrintHelpPage(ConsoleWriter cout, Command.Data data) +{ + local Text commandNameUpperCase; + // Get capitalized command name + commandNameUpperCase = data.name.UpperCopy(); + // Print header: name + basic info + cout.UseColor(_.color.textHeader) + .Write(commandNameUpperCase) + .UseColor(_.color.textDefault); + commandNameUpperCase.FreeSelf(); + if (data.requiresTarget) { + cout.WriteLine(T(TCMD_WITH_TARGET)); + } + else { + cout.WriteLine(T(TCMD_WITHOUT_TARGET)); + } + // Print commands and options + PrintCommands(cout, data); + PrintOptions(cout, data); + // Clean up + cout.ResetColor().Flush(); +} + +private final function PrintCommands(ConsoleWriter cout, Command.Data data) +{ + local int i; + local array subCommands; + subCommands = data.subCommands; + for (i = 0; i < subCommands.length; i += 1) { + PrintSubCommand(cout, subCommands[i], data.name); + } +} + +private final function PrintSubCommand( + ConsoleWriter cout, + SubCommand subCommand, + Text commandName) +{ + // Command + parameters + // Command name + sub command name + cout.UseColor(_.color.textEmphasis) + .Write(commandName) + .Write(T(TSPACE)); + if (subCommand.name != none && !subCommand.name.IsEmpty()) { + cout.Write(subCommand.name).Write(T(TSPACE)); + } + cout.UseColor(_.color.textDefault); + // Parameters + PrintParameters(cout, subCommand.required, subCommand.optional); + cout.Flush(); + // Description + if (subCommand.description != none && !subCommand.description.IsEmpty()) { + cout.WriteBlock(subCommand.description); + } +} + +private final function PrintOptions(ConsoleWriter cout, Command.Data data) +{ + local int i; + local array