From 7cae40704987d444b867db10050e337d8908be8d Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 10 Oct 2022 02:43:35 +0700 Subject: [PATCH] Add new alias source for commands' aliases --- config/AcediaAliases_Commands.ini | 0 sources/Aliases/AliasesAPI.uc | 63 ++++++++++++++++++- sources/Aliases/Aliases_Feature.uc | 16 +++++ .../BuiltInSources/CommandAliasSource.uc | 26 ++++++++ .../Aliases/BuiltInSources/CommandAliases.uc | 28 +++++++++ 5 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 config/AcediaAliases_Commands.ini create mode 100644 sources/Aliases/BuiltInSources/CommandAliasSource.uc create mode 100644 sources/Aliases/BuiltInSources/CommandAliases.uc diff --git a/config/AcediaAliases_Commands.ini b/config/AcediaAliases_Commands.ini new file mode 100644 index 0000000..e69de29 diff --git a/sources/Aliases/AliasesAPI.uc b/sources/Aliases/AliasesAPI.uc index dbbb6b3..997617a 100644 --- a/sources/Aliases/AliasesAPI.uc +++ b/sources/Aliases/AliasesAPI.uc @@ -28,6 +28,7 @@ var private BaseAliasSource weaponAliasSource; var private BaseAliasSource colorAliasSource; var private BaseAliasSource featureAliasSource; var private BaseAliasSource entityAliasSource; +var private BaseAliasSource commandAliasSource; public function _reloadSources() { @@ -37,10 +38,12 @@ public function _reloadSources() _.memory.Free(colorAliasSource); _.memory.Free(featureAliasSource); _.memory.Free(entityAliasSource); + _.memory.Free(commandAliasSource); weaponAliasSource = none; colorAliasSource = none; featureAliasSource = none; entityAliasSource = none; + commandAliasSource = none; feature = Aliases_Feature( class'Aliases_Feature'.static.GetEnabledInstance()); if (feature == none) { @@ -50,6 +53,7 @@ public function _reloadSources() colorAliasSource = feature.GetColorSource(); featureAliasSource = feature.GetFeatureSource(); entityAliasSource = feature.GetEntitySource(); + commandAliasSource = feature.GetCommandSource(); _.memory.Free(feature); } @@ -101,7 +105,6 @@ public final function BaseAliasSource GetWeaponSource() * Returns `BaseAliasSource` that is designated in configuration files as * a source for color aliases. * - * * @return Reference to the `BaseAliasSource` that contains color aliases. * Can return `none` if no source for colors was configured or * the configured source is incorrectly defined. @@ -118,7 +121,6 @@ public final function BaseAliasSource GetColorSource() * Returns `BaseAliasSource` that is designated in configuration files as * a source for feature aliases. * - * * @return Reference to the `BaseAliasSource` that contains feature aliases. * Can return `none` if no source for features was configured or * the configured source is incorrectly defined. @@ -135,7 +137,6 @@ public final function BaseAliasSource GetFeatureSource() * Returns `BaseAliasSource` that is designated in configuration files as * a source for entity aliases. * - * * @return Reference to the `BaseAliasSource` that contains entity aliases. * Can return `none` if no source for entities was configured or * the configured source is incorrectly defined. @@ -148,6 +149,25 @@ public final function BaseAliasSource GetEntitySource() return entityAliasSource; } +/** + * Returns `BaseAliasSource` that is designated in configuration files as + * a source for command aliases. + * + * Command aliases values can have the format of "" or + * " ". + * + * @return Reference to the `BaseAliasSource` that contains command aliases. + * Can return `none` if no source for command was configured or + * the configured source is incorrectly defined. + */ +public final function BaseAliasSource GetCommandSource() +{ + if (commandAliasSource != none) { + commandAliasSource.NewRef(); + } + return commandAliasSource; +} + private final function Text ResolveWithSource( BaseText alias, BaseAliasSource source, @@ -311,6 +331,43 @@ public final function Text ResolveEntity( return ResolveWithSource(alias, entityAliasSource, copyOnFailure); } +/** + * Tries to look up a value stored for given alias in an `BaseAliasSource` + * configured to store command aliases. + * + * Command aliases values can have the format of "" or + * " ". + * + * In Acedia aliases are typically prefixed with '$' to indicate that user + * means to enter alias. This method is able to handle both aliases with and + * without that prefix. This does not lead to conflicts, because '$' cannot + * be a valid part of any alias. + * + * Lookup of alias can fail if either alias does not exist in command alias + * source or command alias source itself does not exist + * (due to either faulty configuration or incorrect definition). + * To determine if command alias source exists you can check + * `_.alias.GetCommandSource()` value. + * + * @param alias Alias, for which method will attempt to + * look up a value. Case-insensitive. + * @param copyOnFailure Whether method should return copy of original + * `alias` value in case caller source did not have any records + * corresponding to `alias`. If `alias` was specified with '$' prefix - + * it will be discarded. + * @return If look up was successful - value, associated with the given + * alias `alias`. If lookup was unsuccessful, it depends on `copyOnFailure` + * flag: `copyOnFailure == false` means method will return `none` + * and `copyOnFailure == true` means method will return `alias.Copy()`. + * If `alias == none` method always returns `none`. + */ +public final function Text ResolveCommand( + BaseText alias, + optional bool copyOnFailure) +{ + return ResolveWithSource(alias, commandAliasSource, copyOnFailure); +} + /** * Tries to look up a value stored for given alias in a custom alias source * with a given name `sourceName`. diff --git a/sources/Aliases/Aliases_Feature.uc b/sources/Aliases/Aliases_Feature.uc index f1e8223..8402ff6 100644 --- a/sources/Aliases/Aliases_Feature.uc +++ b/sources/Aliases/Aliases_Feature.uc @@ -38,6 +38,7 @@ var private AliasSource weaponAliasSource; var private AliasSource colorAliasSource; var private AliasSource featureAliasSource; var private AliasSource entityAliasSource; +var private AliasSource commandAliasSource; // Everything else var private HashTable customSources; @@ -65,6 +66,8 @@ private function DropSources() featureAliasSource = none; _.memory.Free(entityAliasSource); entityAliasSource = none; + _.memory.Free(commandAliasSource); + commandAliasSource = none; _.memory.Free(customSources); customSources = none; } @@ -210,6 +213,19 @@ public function AliasSource GetEntitySource() return entityAliasSource; } +/** + * Returns `AliasSource` for command aliases. + * + * @return `AliasSource`, configured to store command aliases. + */ +public function AliasSource GetCommandSource() +{ + if (commandAliasSource != none) { + commandAliasSource.Newref(); + } + return commandAliasSource; +} + /** * Returns custom `AliasSource` with a given name. * diff --git a/sources/Aliases/BuiltInSources/CommandAliasSource.uc b/sources/Aliases/BuiltInSources/CommandAliasSource.uc new file mode 100644 index 0000000..6102bf8 --- /dev/null +++ b/sources/Aliases/BuiltInSources/CommandAliasSource.uc @@ -0,0 +1,26 @@ +/** + * Source intended for command aliases. + * Copyright 2022 Anton Tarasenko + *------------------------------------------------------------------------------ + * This file is part of Acedia. + * + * Acedia is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License, or + * (at your option) any later version. + * + * Acedia is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Acedia. If not, see . + */ +class ColorAliasSource extends AliasSource + config(AcediaAliases_Commands); + +defaultproperties +{ + aliasesClass = class'CommandAliases' +} \ No newline at end of file diff --git a/sources/Aliases/BuiltInSources/CommandAliases.uc b/sources/Aliases/BuiltInSources/CommandAliases.uc new file mode 100644 index 0000000..c6ad7c8 --- /dev/null +++ b/sources/Aliases/BuiltInSources/CommandAliases.uc @@ -0,0 +1,28 @@ +/** + * Per-object-configuration intended for command aliases. + * Copyright 2022 Anton Tarasenko + *------------------------------------------------------------------------------ + * This file is part of Acedia. + * + * Acedia is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License, or + * (at your option) any later version. + * + * Acedia is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Acedia. If not, see . + */ +class CommandAliases extends AliasesStorage + perObjectConfig + config(AcediaAliases_Commands); + +defaultproperties +{ + configName = "AcediaAliases_Commands" + sourceClass = class'CommandAliasSource' +} \ No newline at end of file