Compare commits

...

2 Commits

  1. 217
      sources/Commands/ACommandFeature.uc
  2. 96
      sources/Commands/ACommandFeature_Announcer.uc
  3. 271
      sources/Tools/PendingConfigsTool.uc

217
sources/Commands/ACommandFeature.uc

@ -17,16 +17,26 @@
* You should have received a copy of the GNU General Public License
* along with Acedia. If not, see <https://www.gnu.org/licenses/>.
*/
class ACommandFeature extends Command;
class ACommandFeature extends Command
dependson(PendingConfigsTool);
// TODO: autoconf, newconf, deleteconf, setconf
// TODO: when displaying features - display which one is enabled
var private class<Feature> selectedFeatureClass;
var private Text selectedConfigName;
var private PendingConfigsTool pendingConfigs;
var private ACommandFeature_Announcer announcer;
protected function Constructor()
{
pendingConfigs =
PendingConfigsTool(_.memory.Allocate(class'PendingConfigsTool'));
super.Constructor();
}
protected function Finalizer()
{
_.memory.Free(announcer);
_.memory.Free(pendingConfigs);
super.Finalizer();
}
@ -43,114 +53,169 @@ protected function BuildData(CommandDataBuilder builder)
builder.SubCommand(P("disable"))
.ParamText(P("feature"))
.Describe(P("Disables specified <feature>."));
builder.SubCommand(P("showconf"))
.ParamText(P("feature"))
.ParamText(P("config"));
builder.SubCommand(P("editconf"))
.ParamText(P("feature"))
.ParamText(P("config"))
.ParamText(P("variable path"))
.ParamRemainder(P("value"));
announcer = ACommandFeature_Announcer(
_.memory.Allocate(class'ACommandFeature_Announcer'));
}
protected function Executed(CallData arguments, EPlayer instigator)
{
local Text userGivenFeatureName, userGivenConfigName;
announcer.Setup(none, instigator, othersConsole);
userGivenFeatureName = arguments.parameters.GetText(P("feature"));
selectedFeatureClass = LoadFeatureClass(userGivenFeatureName);
_.memory.Free(userGivenFeatureName);
userGivenConfigName = arguments.parameters.GetText(P("config"));
if (userGivenConfigName != none)
{
selectedConfigName = userGivenConfigName.LowerCopy();
userGivenConfigName.FreeSelf();
}
pendingConfigs.SelectConfig(selectedFeatureClass, selectedConfigName);
if (arguments.subCommandName.IsEmpty()) {
ShowAllFeatures();
}
else if (arguments.subCommandName.Compare(P("enable")))
{
EnableFeature(
arguments.parameters.GetText(P("feature")),
arguments.parameters.GetText(P("config")));
else if (arguments.subCommandName.Compare(P("enable"))) {
EnableFeature();
}
else if (arguments.subCommandName.Compare(P("disable"))) {
DisableFeature(arguments.parameters.GetText(P("feature")));
DisableFeature();
}
else if (arguments.subCommandName.Compare(P("showconf"))) {
ShowFeatureConfig();
}
else if (arguments.subCommandName.Compare(P("editconf")))
{
EditFeatureConfig(
arguments.parameters.GetText(P("variable path")),
arguments.parameters.GetText(P("value")));
}
_.memory.Free(selectedConfigName);
selectedConfigName = none;
}
protected function EnableFeature(BaseText featureName, BaseText configParameter)
protected function EnableFeature()
{
local bool wasEnabled;
local Text oldConfig, newConfig;
local Feature instance;
local class<Feature> featureClass;
featureClass = LoadFeatureClass(featureName);
if (featureClass == none) {
return;
}
wasEnabled = featureClass.static.IsEnabled();
oldConfig = featureClass.static.GetCurrentConfig();
newConfig = GetConfigFromParameter(configParameter, featureClass);
wasEnabled = selectedFeatureClass.static.IsEnabled();
oldConfig = selectedFeatureClass.static.GetCurrentConfig();
newConfig = PickConfigBasedOnParameter();
// Already enabled with the same config!
if (oldConfig != none && oldConfig.Compare(newConfig, SCASE_INSENSITIVE))
{
announcer.AnnounceFailedAlreadyEnabled(featureClass, newConfig);
announcer.AnnounceFailedAlreadyEnabled(selectedFeatureClass, newConfig);
_.memory.Free(newConfig);
_.memory.Free(oldConfig);
return;
}
// Try enabling and report the result
instance = featureClass.static.EnableMe(newConfig);
if (instance == none) {
announcer.AnnounceFailedCannotEnableFeature(featureClass, newConfig);
instance = selectedFeatureClass.static.EnableMe(newConfig);
if (instance == none)
{
announcer.AnnounceFailedCannotEnableFeature(
selectedFeatureClass,
newConfig);
}
else if (wasEnabled) {
announcer.AnnounceSwappedConfig(featureClass, oldConfig, newConfig);
else if (wasEnabled)
{
announcer.AnnounceSwappedConfig(
selectedFeatureClass,
oldConfig,
newConfig);
}
else {
announcer.AnnounceEnabledFeature(featureClass, newConfig);
announcer.AnnounceEnabledFeature(selectedFeatureClass, newConfig);
}
_.memory.Free(newConfig);
_.memory.Free(oldConfig);
}
protected function DisableFeature(Text featureName)
protected function DisableFeature()
{
local class<Feature> featureClass;
featureClass = LoadFeatureClass(featureName);
if (featureClass == none) {
return;
}
if (!featureClass.static.IsEnabled())
if (!selectedFeatureClass.static.IsEnabled())
{
announcer.AnnounceFailedAlreadyDisabled(featureClass);
announcer.AnnounceFailedAlreadyDisabled(selectedFeatureClass);
return;
}
featureClass.static.DisableMe();
selectedFeatureClass.static.DisableMe();
// It is possible that this command itself is destroyed after above command
// so do the check just in case
if (IsAllocated()) {
announcer.AnnounceDisabledFeature(featureClass);
announcer.AnnounceDisabledFeature(selectedFeatureClass);
}
}
protected function Text GetConfigFromParameter(
BaseText configParameter,
class<Feature> featureClass)
protected function ShowFeatureConfig()
{
local MutableText dataAsJSON;
local HashTable currentData, pendingData;
if (selectedConfigName == none) {
return;
}
currentData = GetCurrentConfigData();
if (currentData == none)
{
announcer.AnnounceFailedNoDataForConfig(
selectedFeatureClass,
selectedConfigName);
return;
}
// Display current data
dataAsJSON = _.json.PrettyPrint(currentData);
announcer.AnnounceCurrentConfig(selectedFeatureClass, selectedConfigName);
callerConsole.Flush().WriteLine(dataAsJSON);
_.memory.Free(dataAsJSON);
// Display pending data
pendingData = pendingConfigs.GetPendingConfigData();
if (pendingData != none)
{
dataAsJSON = _.json.PrettyPrint(pendingData);
announcer.AnnouncePendingConfig(
selectedFeatureClass,
selectedConfigName);
callerConsole.Flush().WriteLine(dataAsJSON);
_.memory.Free(dataAsJSON);
}
_.memory.Free(pendingData);
_.memory.Free(currentData);
}
protected function Text PickConfigBasedOnParameter()
{
local Text resolvedConfig;
local class<FeatureConfig> configClass;
if (featureClass == none) {
return none;
}
configClass = featureClass.default.configClass;
configClass = selectedFeatureClass.default.configClass;
if (configClass == none)
{
announcer.AnnounceFailedNoConfigClass(featureClass);
announcer.AnnounceFailedNoConfigClass(selectedFeatureClass);
return none;
}
// If config was specified - simply check that it exists
if (configParameter != none)
if (selectedConfigName != none)
{
if (configClass.static.Exists(configParameter)) {
return configParameter.Copy();
if (configClass.static.Exists(selectedConfigName)) {
return selectedConfigName.Copy();
}
announcer.AnnounceFailedConfigMissing(configParameter);
announcer.AnnounceFailedConfigMissing(selectedConfigName);
return none;
}
// If it wasn't specified - try auto config instead
resolvedConfig = configClass.static.GetAutoEnabledConfig();
if (resolvedConfig == none) {
announcer.AnnounceFailedNoConfigProvided(featureClass);
announcer.AnnounceFailedNoConfigProvided(selectedFeatureClass);
}
return resolvedConfig;
}
@ -190,7 +255,7 @@ protected function ShowFeature(class<Feature> feature)
{
local int i;
local Text autoConfig;
local MutableText featureName, builder;
local MutableText featureName, builder, nextConfig;
local ListBuilder configList;
local array<Text> availableConfigs;
local class<FeatureConfig> configClass;
@ -215,10 +280,10 @@ protected function ShowFeature(class<Feature> feature)
builder.Append(featureName);
_.memory.Free(featureName);
if (availableConfigs.length == 1) {
builder.Append(P(" with config:"));
builder.Append(P(" with config: "));
}
else if (availableConfigs.length > 1) {
builder.Append(P(" with configs:"));
builder.Append(P(" with configs: "));
}
callerConsole.Write(builder);
_.memory.Free(builder);
@ -226,7 +291,12 @@ protected function ShowFeature(class<Feature> feature)
autoConfig = configClass.static.GetAutoEnabledConfig();
for (i = 0; i < availableConfigs.length; i += 1)
{
configList.Item(availableConfigs[i]);
nextConfig = availableConfigs[i].MutableCopy();
if (pendingConfigs.HasPendingConfigFor(feature, nextConfig)) {
nextConfig.Append(P("*"));
}
configList.Item(nextConfig);
_.memory.Free(nextConfig);
if ( autoConfig != none
&& autoConfig.Compare(availableConfigs[i], SCASE_INSENSITIVE))
{
@ -241,6 +311,47 @@ protected function ShowFeature(class<Feature> feature)
_.memory.Free(builder);
}
protected function EditFeatureConfig(BaseText pathToValue, BaseText newValue)
{
local PendingConfigsTool.PendingConfigToolError error;
error = pendingConfigs.ChangeConfig(pathToValue, newValue);
if (error == PCTE_ConfigMissing) {
announcer.AnnounceFailedConfigMissing(selectedConfigName);
}
else if (error == PCTE_ExpectedObject) {
announcer.AnnounceFailedExpectedObject();
}
else if (error == PCTE_BadPointer)
{
announcer.AnnounceFailedBadPointer(
selectedFeatureClass,
selectedConfigName,
pathToValue);
}
}
private function HashTable GetCurrentConfigData()
{
local class<FeatureConfig> configClass;
if (selectedConfigName == none) {
return none;
}
configClass = selectedFeatureClass.default.configClass;
if (configClass == none)
{
announcer.AnnounceFailedNoConfigClass(selectedFeatureClass);
return none;
}
return configClass.static.LoadData(selectedConfigName);
}
// 5. Add `saveconf` and `--save` flag
// 6. Add `removeconf`
// 7. Add `newconf`
// 8. Add `autoconf` for setting auto config
defaultproperties
{
}

96
sources/Commands/ACommandFeature_Announcer.uc

@ -21,17 +21,22 @@ class ACommandFeature_Announcer extends CommandAnnouncer;
var private AnnouncementVariations enabledFeature, disabledFeature;
var private AnnouncementVariations swappedConfig;
var private AnnouncementVariations showCurrentConfig, showPendingConfig;
var private AnnouncementVariations failedToLoadFeatureClass;
var private AnnouncementVariations failedNoConfigProvided, failedConfigMissing;
var private AnnouncementVariations failedCannotEnableFeature;
var private AnnouncementVariations failedNoConfigClass;
var private AnnouncementVariations failedAlreadyEnabled, failedAlreadyDisabled;
var private AnnouncementVariations failedNoDataForConfig, failedExpectedObject;
var private AnnouncementVariations failedBadPointer;
protected function Finalizer()
{
FreeVariations(enabledFeature);
FreeVariations(disabledFeature);
FreeVariations(swappedConfig);
FreeVariations(showCurrentConfig);
FreeVariations(showPendingConfig);
FreeVariations(failedToLoadFeatureClass);
FreeVariations(failedNoConfigProvided);
FreeVariations(failedConfigMissing);
@ -39,6 +44,9 @@ protected function Finalizer()
FreeVariations(failedNoConfigClass);
FreeVariations(failedAlreadyEnabled);
FreeVariations(failedAlreadyDisabled);
FreeVariations(failedNoDataForConfig);
FreeVariations(failedExpectedObject);
FreeVariations(failedBadPointer);
super.Finalizer();
}
@ -117,6 +125,40 @@ public final function AnnounceSwappedConfig(
MakeAnnouncement(swappedConfig);
}
public final function AnnounceCurrentConfig(
class<Feature> featureClass,
BaseText config)
{
if (!showCurrentConfig.initialized)
{
showCurrentConfig.initialized = true;
showCurrentConfig.toSelfReport = _.text.MakeTemplate_S(
"Current config \"%2\" for feature {$TextEmphasis `%1`}:");
}
showCurrentConfig.toSelfReport
.Reset()
.ArgClass(featureClass)
.Arg(config);
MakeAnnouncement(showCurrentConfig);
}
public final function AnnouncePendingConfig(
class<Feature> featureClass,
BaseText config)
{
if (!showPendingConfig.initialized)
{
showPendingConfig.initialized = true;
showPendingConfig.toSelfReport = _.text.MakeTemplate_S(
"Pending config \"%2\" for feature {$TextEmphasis `%1`}:");
}
showPendingConfig.toSelfReport
.Reset()
.ArgClass(featureClass)
.Arg(config);
MakeAnnouncement(showPendingConfig);
}
public final function AnnounceFailedToLoadFeatureClass(BaseText failedClassName)
{
if (!failedToLoadFeatureClass.initialized)
@ -136,7 +178,7 @@ public final function AnnounceFailedNoConfigProvided(
{
failedNoConfigProvided.initialized = true;
failedNoConfigProvided.toSelfReport = _.text.MakeTemplate_S(
"{$TextFailue No config specified} and {$TextFailure no"
"{$TextFailure No config specified} and {$TextFailure no"
@ "auto-enabled config} exists for feature {$TextEmphasis `%1`}");
}
failedNoConfigProvided.toSelfReport.Reset().ArgClass(featureClass);
@ -149,7 +191,7 @@ public final function AnnounceFailedConfigMissing(BaseText config)
{
failedConfigMissing.initialized = true;
failedConfigMissing.toSelfReport = _.text.MakeTemplate_S(
"Specified config \"%1\" {$TextFailue doesn't exist}");
"Specified config \"%1\" {$TextFailure doesn't exist}");
}
failedConfigMissing.toSelfReport.Reset().Arg(config);
MakeAnnouncement(failedConfigMissing);
@ -219,6 +261,56 @@ public final function AnnounceFailedAlreadyEnabled(
MakeAnnouncement(failedAlreadyEnabled);
}
public final function AnnounceFailedNoDataForConfig(
class<Feature> featureClass,
BaseText config)
{
if (!failedNoDataForConfig.initialized)
{
failedNoDataForConfig.initialized = true;
failedNoDataForConfig.toSelfReport = _.text.MakeTemplate_S(
"Feature {$TextEmphasis `%1`} is {$TextFailure missing data} for"
@ "config \"%2\"");
}
failedNoDataForConfig.toSelfReport
.Reset()
.ArgClass(featureClass)
.Arg(config);
MakeAnnouncement(failedNoDataForConfig);
}
public final function AnnounceFailedExpectedObject()
{
if (!failedExpectedObject.initialized)
{
failedExpectedObject.initialized = true;
failedExpectedObject.toSelfReport = _.text.MakeTemplate_S(
"Value change {$TextFailure failed}, because when changing"
@ "the value of the whole config, a JSON object must be provided");
}
MakeAnnouncement(failedExpectedObject);
}
public final function AnnounceFailedBadPointer(
class<Feature> featureClass,
BaseText config,
BaseText pointer)
{
if (!failedBadPointer.initialized)
{
failedBadPointer.initialized = true;
failedBadPointer.toSelfReport = _.text.MakeTemplate_S(
"Provided JSON pointer \"%3\" is {$TextFailure invalid} for config"
@ "\"%2\" of feature {$TextEmphasis `%1`}");
}
failedBadPointer.toSelfReport
.Reset()
.ArgClass(featureClass)
.Arg(config)
.Arg(pointer);
MakeAnnouncement(failedBadPointer);
}
defaultproperties
{
}

271
sources/Tools/PendingConfigsTool.uc

@ -0,0 +1,271 @@
/**
* Auxiliary object for `ACommandFeature` to help with managing pending
* configs for `Feature`s. Pending configs are `HashTable`s with config data
* that are yet to be applied to configs and `Feature`s. They allow users to
* make several changes to the data before actually making changes to
* the gameplay code.
* 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 <https://www.gnu.org/licenses/>.
*/
class PendingConfigsTool extends AcediaObject;
var private class<Feature> selectedFeatureClass;
var private Text selectedConfigName;
enum PendingConfigToolError
{
PCTE_None,
PCTE_ConfigMissing,
PCTE_ExpectedObject,
PCTE_BadPointer
};
struct PendingConfigs
{
var class<Feature> featureClass;
var HashTable pendingSaves;
};
var private array<PendingConfigs> featurePendingEdits;
protected function Finalizer()
{
local int i;
for (i = 0; i < featurePendingEdits.length; i ++) {
_.memory.Free(featurePendingEdits[i].pendingSaves);
}
featurePendingEdits.length = 0;
}
public final function SelectConfig(
class<Feature> featureClass,
BaseText configName)
{
_.memory.Free(selectedConfigName);
selectedFeatureClass = featureClass;
selectedConfigName = none;
if (configName != none) {
selectedConfigName = configName.LowerCopy();
}
}
public function bool HasPendingConfigFor(
class<Feature> featureClass,
BaseText configName)
{
local int i;
local bool result;
local Text lowerCaseConfigName;
if (featureClass == none) return false;
if (configName == none) return false;
for (i = 0; i < featurePendingEdits.length; i ++)
{
if (featurePendingEdits[i].featureClass == featureClass)
{
lowerCaseConfigName = configName.LowerCopy();
result = featurePendingEdits[i].pendingSaves
.HasKey(lowerCaseConfigName);
lowerCaseConfigName.FreeSelf();
return result;
}
}
return false;
}
public function HashTable GetPendingConfigData(optional bool createIfMissing)
{
local int editsIndex;
local HashTable result;
local PendingConfigs newRecord;
if (selectedConfigName == none) {
return none;
}
editsIndex = GetPendingConfigDataIndex();
if (editsIndex >= 0)
{
result = featurePendingEdits[editsIndex]
.pendingSaves
.GetHashTable(selectedConfigName);
if (result != none) {
return result;
}
}
if (createIfMissing)
{
if (editsIndex < 0)
{
editsIndex = featurePendingEdits.length;
newRecord.featureClass = selectedFeatureClass;
newRecord.pendingSaves = _.collections.EmptyHashTable();
featurePendingEdits[editsIndex] = newRecord;
}
result = GetCurrentConfigData();
if (result != none)
{
featurePendingEdits[editsIndex]
.pendingSaves
.SetItem(selectedConfigName, result);
}
}
return result;
}
public function PendingConfigToolError ChangeConfig(
BaseText pathToValue,
BaseText newValue)
{
local HashTable pendingData;
local JSONPointer pointer;
local Parser parser;
local AcediaObject newValueAsJSON;
local PendingConfigToolError result;
if (pathToValue == none) {
return PCTE_BadPointer;
}
pendingData = GetPendingConfigData(true);
if (pendingData == none) {
return PCTE_ConfigMissing;
}
// Get guaranteed not-`none` JSON value, treating it as JSON string
// if necessary
parser = _.text.Parse(newValue);
newValueAsJSON = _.json.ParseWith(parser);
parser.FreeSelf();
if (newValueAsJSON == none && newValue != none) {
newValueAsJSON = newValue.Copy();
}
// Set new data
pointer = _.json.Pointer(pathToValue);
result = SetItemByJSON(pendingData, pointer, newValueAsJSON);
pointer.FreeSelf();
pendingData.FreeSelf();
_.memory.Free(newValueAsJSON);
return result;
}
private function PendingConfigToolError SetItemByJSON(
HashTable data,
JSONPointer pointer,
AcediaObject jsonValue)
{
local Text containerIndex;
local AcediaObject container;
local PendingConfigToolError result;
if (pointer.IsEmpty())
{
if (HashTable(jsonValue) != none)
{
result = ChangePendingConfigData(HashTable(jsonValue));
_.memory.Free(jsonValue);
return result;
}
_.memory.Free(jsonValue);
return PCTE_ExpectedObject;
}
// Since `!pointer.IsEmpty()`, we are guaranteed to pop a valid value
containerIndex = pointer.Pop();
container = data.GetItemByJSON(pointer);
if (container == none)
{
containerIndex.FreeSelf();
return PCTE_BadPointer;
}
result = SetContainerItemByText(container, containerIndex, jsonValue);
containerIndex.FreeSelf();
container.FreeSelf();
return result;
}
private function PendingConfigToolError SetContainerItemByText(
AcediaObject container,
BaseText containerIndex,
AcediaObject jsonValue)
{
local int arrayIndex;
local Parser parser;
local ArrayList arrayListContainer;
local HashTable hashTableContainer;
hashTableContainer = HashTable(container);
arrayListContainer = ArrayList(container);
if (hashTableContainer != none) {
hashTableContainer.SetItem(containerIndex, jsonValue);
}
if (arrayListContainer != none)
{
parser = containerIndex.Parse();
if (parser.MInteger(arrayIndex, 10).Ok())
{
arrayListContainer.SetItem(arrayIndex, jsonValue);
parser.FreeSelf();
return PCTE_None;
}
parser.FreeSelf();
if (containerIndex.Compare(P("-"))) {
arrayListContainer.AddItem(jsonValue);
}
else {
return PCTE_BadPointer;
}
}
return PCTE_None;
}
private function int GetPendingConfigDataIndex()
{
local int i;
for (i = 0; i < featurePendingEdits.length; i ++)
{
if (featurePendingEdits[i].featureClass == selectedFeatureClass) {
return i;
}
}
return -1;
}
private function PendingConfigToolError ChangePendingConfigData(
HashTable newData)
{
local int editsIndex;
if (selectedConfigName == none) {
return PCTE_None;
}
editsIndex = GetPendingConfigDataIndex();
if (editsIndex < 0) {
return PCTE_ConfigMissing;
}
featurePendingEdits[editsIndex].pendingSaves
.SetItem(selectedConfigName, newData);
return PCTE_None;
}
private function HashTable GetCurrentConfigData()
{
return selectedFeatureClass.default.configClass.static
.LoadData(selectedConfigName);
}
defaultproperties
{
}
Loading…
Cancel
Save