Browse Source

Change how Acedia's config swapping works

pull/8/head
Anton Tarasenko 3 years ago
parent
commit
11e0ef809f
  1. 4
      sources/Avarice/Avarice.uc
  2. 46
      sources/Avarice/Avarice_Feature.uc
  3. 6
      sources/Commands/Commands.uc
  4. 10
      sources/Commands/Commands_Feature.uc
  5. 44
      sources/Features/Feature.uc
  6. 38
      sources/Features/FeatureConfig.uc

4
sources/Avarice/Avarice.uc

@ -32,7 +32,7 @@ struct AvariceLinkRecord
// can be used in other configs to point at each link. // can be used in other configs to point at each link.
// `address` must have form "host:port", where "host" is either ip or // `address` must have form "host:port", where "host" is either ip or
// domain name and "port" is a numerical port value. // domain name and "port" is a numerical port value.
var private config array<AvariceLinkRecord> link; var public config array<AvariceLinkRecord> link;
// In case Avarice utility is launched after link started trying open // In case Avarice utility is launched after link started trying open
// the connection - that connection attempt will fail. To fix that link must // the connection - that connection attempt will fail. To fix that link must
@ -41,7 +41,7 @@ var private config array<AvariceLinkRecord> link;
// re-attempt opening connection. Setting value too low can prevent any // re-attempt opening connection. Setting value too low can prevent any
// connection from opening, setting it too high might make you wait for // connection from opening, setting it too high might make you wait for
// connection too long. // connection too long.
var private config float reconnectTime; var public config float reconnectTime;
protected function AssociativeArray ToData() protected function AssociativeArray ToData()
{ {

46
sources/Avarice/Avarice_Feature.uc

@ -90,49 +90,17 @@ protected function OnDisabled()
_.memory.FreeMany(createdLinks); _.memory.FreeMany(createdLinks);
} }
protected function SwapConfig( protected function SwapConfig(FeatureConfig config)
AssociativeArray previousConfigData,
AssociativeArray newConfigData)
{ {
local int i; local Avarice newConfig;
local Text nextText; newConfig = Avarice(config);
local DynamicArray linksArray; if (newConfig == none) {
local AssociativeArray nextLink;
local Avarice.AvariceLinkRecord nextRecord;
// Simply restart all the links
for (i = 0; i < createdLinks.length; i += 1)
{
if (createdLinks[i] != none) {
createdLinks[i].FreeSelf();
}
}
createdLinks.length = 0;
link.length = 0;
if (newConfigData == none) {
return; return;
} }
reconnectTime = newConfigData.GetFloat(P("reconnectTime")); link = newConfig.link;
reconnectTime = newConfig.reconnectTime;
// For static `GetReconnectTime()` method
default.reconnectTime = reconnectTime; default.reconnectTime = reconnectTime;
linksArray = newConfigData.GetDynamicArray(P("link"));
if (linksArray == none) {
return;
}
for (i = 0; i < linksArray.GetLength(); i += 1)
{
nextLink = linksArray.GetAssociativeArray(i);
if (nextLink == none) {
continue;
}
nextText = nextLink.GetText(P("name"));
if (nextText != none) {
nextRecord.name = nextText.ToPlainString();
}
nextText = nextLink.GetText(P("address"));
if (nextText != none) {
nextRecord.address = nextText.ToPlainString();
}
link[i] = nextRecord;
}
} }
// Reply back any messages from "echo" service // Reply back any messages from "echo" service

6
sources/Commands/Commands.uc

@ -21,13 +21,13 @@ class Commands extends FeatureConfig
perobjectconfig perobjectconfig
config(AcediaSystem); config(AcediaSystem);
var private config bool useChatInput; var public config bool useChatInput;
protected function AssociativeArray ToData() protected function AssociativeArray ToData()
{ {
local AssociativeArray data; local AssociativeArray data;
data = __().collections.EmptyAssociativeArray(); data = __().collections.EmptyAssociativeArray();
data.SetBool(__().text.FromString("useChatInput"), useChatInput, true); data.SetBool(P("useChatInput"), useChatInput, true);
return data; return data;
} }
@ -38,7 +38,7 @@ protected function FromData(AssociativeArray source)
} }
} }
protected function Reset() protected function DefaultIt()
{ {
useChatInput = true; useChatInput = true;
} }

10
sources/Commands/Commands_Feature.uc

@ -54,14 +54,14 @@ protected function OnDisabled()
commandDelimiters.length = 0; commandDelimiters.length = 0;
} }
protected function SwapConfig( protected function SwapConfig(FeatureConfig config)
AssociativeArray previousConfigData,
AssociativeArray newConfigData)
{ {
if (newConfigData == none) { local Commands newConfig;
newConfig = Commands(config);
if (newConfig == none) {
return; return;
} }
useChatInput = newConfigData.GetBool(P("useChatInput")); useChatInput = newConfig.useChatInput;
} }
/** /**

44
sources/Features/Feature.uc

@ -137,28 +137,31 @@ protected function Finalizer()
* (allocated). To set initial config on this `Feature`'s start - specify it * (allocated). To set initial config on this `Feature`'s start - specify it
* as a parameter to `EnableMe()` method. * as a parameter to `EnableMe()` method.
* *
* Method will do nothing if `newConfigName` parameter is set to `none`.
*
* @param newConfigName Name of the config to apply to the caller `Feature`. * @param newConfigName Name of the config to apply to the caller `Feature`.
*/ */
public final function ApplyConfig(Text newConfigName) public final function ApplyConfig(Text newConfigName)
{ {
local AssociativeArray newConfigData; local FeatureConfig newConfig;
newConfigData = configClass.static.LoadData(newConfigName); if (newConfigName == none) {
if (newConfigData == none) return;
}
newConfig = configClass.static.GetConfigInstance(newConfigName);
if (newConfig == none)
{ {
_.logger.Auto(errorBadConfigData).ArgClass(class); _.logger.Auto(errorBadConfigData).ArgClass(class);
// Fallback to "default" config // Fallback to "default" config
newConfigName = _.text.FromString(defaultConfigName); newConfigName = _.text.FromString(defaultConfigName);
configClass.static.NewConfig(newConfigName); configClass.static.NewConfig(newConfigName);
newConfigData = configClass.static.LoadData(newConfigName); newConfig = configClass.static.GetConfigInstance(newConfigName);
} }
SwapConfig(currentConfig, newConfigData); else {
if (currentConfig != none) { newConfigName = newConfigName.Copy();
currentConfig.Empty(true);
} }
SwapConfig(newConfig);
_.memory.Free(currentConfigName); _.memory.Free(currentConfigName);
_.memory.Free(currentConfig); currentConfigName = newConfigName;
currentConfigName = newConfigName.Copy();
currentConfig = newConfigData;
} }
/** /**
@ -275,7 +278,7 @@ public static final function bool DisableMe()
* *
* AVOID MANUALLY CALLING IT. * AVOID MANUALLY CALLING IT.
*/ */
protected function OnEnabled() { } protected function OnEnabled(){}
/** /**
* When using proper methods for enabling a `Feature`, * When using proper methods for enabling a `Feature`,
@ -283,24 +286,21 @@ protected function OnEnabled() { }
* *
* AVOID MANUALLY CALLING IT. * AVOID MANUALLY CALLING IT.
*/ */
protected function OnDisabled() { } protected function OnDisabled(){}
/** /**
* Will be called whenever caller `Feature` class must change it's config * Will be called whenever caller `Feature` class must change it's config
* parameters. This can be done both when the `Feature` is enabled or disabled. * parameters. This can be done both when the `Feature` is enabled or disabled.
* *
* @param previousConfigData Config data that was previously set for this * @param newConfigData New config that caller `Feature`'s class must use.
* `Feature` class. `none` is passed iff config is set for the first time. * We pass `FeatureConfig` value for performance and simplicity reasons,
* @param newConfigData New config data that caller `Feature`'s class * but to keep Acedia working correctly and in an expected way you
* must use. Guaranteed to not be `none`. * MUST AVOID MODIFYING THIS VALUE IN ANY WAY WHATSOEVER.
* Guaranteed to not be `none`.
* *
* AVOID MANUALLY CALLING IT. * AVOID MANUALLY CALLING THIS METHOD.
* DO NOT DEALLOCATE PASSED VALUES.
* DO NOT USE PASSED VALUES OUTSIDE OF THIS METHOD CALL.
*/ */
protected function SwapConfig( protected function SwapConfig(FeatureConfig newConfig){}
AssociativeArray previousConfigData,
AssociativeArray newConfigData) { }
private static function SetListenersActiveStatus(bool newStatus) private static function SetListenersActiveStatus(bool newStatus)
{ {

38
sources/Features/FeatureConfig.uc

@ -17,9 +17,11 @@
* } * }
* ``` * ```
* *
* You should only define a new child class, along with implementing it's * For each `Feature` you need to define a new child class, along with
* `FromData()`, `ToData()` and `DefaultIt()` methods and otherwise avoid * implementing it's `FromData()`, `ToData()` and `DefaultIt()` methods.
* directly using objects of this class. * You will also need to implement `Feature`'s `SwapConfig()` method that take
* an instance of `FeatureConfig` as a parameter. Other than that you should
* avoid directly using objects of this class.
* Copyright 2021 Anton Tarasenko * Copyright 2021 Anton Tarasenko
*------------------------------------------------------------------------------ *------------------------------------------------------------------------------
* This file is part of Acedia. * This file is part of Acedia.
@ -205,6 +207,27 @@ public static function array<Text> AvailableConfigs()
return emptyResult; return emptyResult;
} }
/**
* Returns `FeatureConfig` of caller class with name `name`.
*
* @param name Name of the config object, whos settings data is to
* be loaded. Case-insensitive.
* @return `FeatureConfig` of caller class with name `name`.
*/
public final static function FeatureConfig GetConfigInstance(Text name)
{
local FeatureConfig requiredConfig;
if (default.existingConfigs == none) {
return none;
}
if (name != none) {
name = name.LowerCopy();
}
requiredConfig = FeatureConfig(default.existingConfigs.GetItem(name));
__().memory.Free(name);
return requiredConfig;
}
/** /**
* Loads Acedia's representation of settings data of a particular config * Loads Acedia's representation of settings data of a particular config
* object, given by the `name`. * object, given by the `name`.
@ -221,17 +244,10 @@ public final static function AssociativeArray LoadData(Text name)
{ {
local AssociativeArray result; local AssociativeArray result;
local FeatureConfig requiredConfig; local FeatureConfig requiredConfig;
if (default.existingConfigs == none) { requiredConfig = GetConfigInstance(name);
return none;
}
if (name != none) {
name = name.LowerCopy();
}
requiredConfig = FeatureConfig(default.existingConfigs.GetItem(name));
if (requiredConfig != none) { if (requiredConfig != none) {
result = requiredConfig.ToData(); result = requiredConfig.ToData();
} }
__().memory.Free(name);
return result; return result;
} }

Loading…
Cancel
Save