diff --git a/sources/Commands/Command.uc b/sources/Commands/Command.uc index bfb1b94..aefac81 100644 --- a/sources/Commands/Command.uc +++ b/sources/Commands/Command.uc @@ -132,6 +132,12 @@ struct Parameter // (For `CPT_Boolean` type variables only) - preferred boolean format, // used in help pages var PreferredBooleanFormat booleanFormat; + // `CPT_Text` can be attempted to be auto-resolved as an alias from + /// some source during parsing. For command to attempt that, this field must + // be not-`none` and contain the name of the alias source (either "weapon", + // "color", "feature", "entity" or some kind of custom alias source name). + // Only relevant when given value is prefixed with "$" character. + var Text aliasSourceName; }; // Defines a sub-command of a this command (specified as @@ -252,6 +258,7 @@ private final function CleanParameters(array parameters) { _.memory.Free(parameters[i].displayName); _.memory.Free(parameters[i].variableName); + _.memory.Free(parameters[i].aliasSourceName); } } diff --git a/sources/Commands/CommandDataBuilder.uc b/sources/Commands/CommandDataBuilder.uc index b2057c8..2517020 100644 --- a/sources/Commands/CommandDataBuilder.uc +++ b/sources/Commands/CommandDataBuilder.uc @@ -797,17 +797,29 @@ public final function CommandDataBuilder ParamNumberList( * parameter's value in `HashTable` after user's command input * is parsed. Provided value will be copied. * If left `none`, - will coincide with `name` parameter. + * @param aliasSourceName Name of the alias source that must be used to + * auto-resolve this parameter's value. `none` means that parameter will be + * recorded as-is, any other value (either "weapon", "color", "feature", + * "entity" or some kind of custom alias source name) will make values + * prefixed with "$" to be resolved as aliases. * @return Returns the caller `CommandDataBuilder` to allow for * method chaining. */ public final function CommandDataBuilder ParamText( BaseText name, - optional BaseText variableName) + optional BaseText variableName, + optional BaseText aliasSourceName) { + local Command.Parameter newParameterValue; + if (name == none) { return self; } - PushParameter(NewParameter(name, CPT_Text, false, variableName)); + newParameterValue = NewParameter(name, CPT_Text, false, variableName); + if (aliasSourceName != none) { + newParameterValue.aliasSourceName = aliasSourceName.Copy(); + } + PushParameter(newParameterValue); return self; } @@ -824,17 +836,29 @@ public final function CommandDataBuilder ParamText( * parameter's value in `HashTable` after user's command input * is parsed. Provided value will be copied. * If left `none`, - will coincide with `name` parameter. + * @param aliasSource Name of the alias source that must be used to + * auto-resolve this parameter's value. `none` means that parameter will be + * recorded as-is, any other value (either "weapon", "color", "feature", + * "entity" or some kind of custom alias source name) will make values + * prefixed with "$" to be resolved as aliases. * @return Returns the caller `CommandDataBuilder` to allow for * method chaining. */ public final function CommandDataBuilder ParamTextList( BaseText name, - optional BaseText variableName) + optional BaseText variableName, + optional BaseText aliasSourceName) { + local Command.Parameter newParameterValue; + if (name == none) { return self; } - PushParameter(NewParameter(name, CPT_Text, true, variableName)); + newParameterValue = NewParameter(name, CPT_Text, true, variableName); + if (aliasSourceName != none) { + newParameterValue.aliasSourceName = aliasSourceName.Copy(); + } + PushParameter(newParameterValue); return self; } diff --git a/sources/Commands/CommandParser.uc b/sources/Commands/CommandParser.uc index 35b6cb7..e4f18a4 100644 --- a/sources/Commands/CommandParser.uc +++ b/sources/Commands/CommandParser.uc @@ -580,10 +580,43 @@ private final function bool ParseTextValue( commandParser.Fail(); return false; } + AutoResolveAlias(textValue, expectedParameter.aliasSourceName); RecordParameter(parsedParameters, expectedParameter, textValue.IntoText()); return true; } +// Resolves alias with appropriate source, if parameter was specified to be +// auto-resolved. +// Resolved values is returned through first out-parameter. +private final function AutoResolveAlias( + out MutableText textValue, + Text aliasSourceName) +{ + local Text resolvedValue; + + if (textValue == none) return; + if (aliasSourceName == none) return; + if (!textValue.StartsWithS("$")) return; + + if (aliasSourceName.Compare(P("weapon"))) { + resolvedValue = _.alias.ResolveWeapon(textValue, true); + } + else if (aliasSourceName.Compare(P("color"))) { + resolvedValue = _.alias.ResolveColor(textValue, true); + } + else if (aliasSourceName.Compare(P("feature"))) { + resolvedValue = _.alias.ResolveFeature(textValue, true); + } + else if (aliasSourceName.Compare(P("entity"))) { + resolvedValue = _.alias.ResolveEntity(textValue, true); + } + else { + resolvedValue = _.alias.ResolveCustom(aliasSourceName, textValue, true); + } + textValue.FreeSelf(); + textValue = resolvedValue.IntoMutableText(); +} + // Assumes `commandParser` and `parsedParameters` are not `none`. // Parses a single `Text` value into given `parsedParameters` // hash table, consuming all remaining contents.