Browse Source

Change `Feature` base class to `AcediaObject`

pull/8/head
Anton Tarasenko 4 years ago
parent
commit
5c0bdb173b
  1. 139
      sources/Feature.uc

139
sources/Feature.uc

@ -3,9 +3,12 @@
* can be enabled or disabled, according to server owner's wishes. * can be enabled or disabled, according to server owner's wishes.
* In the current version of Acedia enabling or disabling a feature requires * In the current version of Acedia enabling or disabling a feature requires
* manually editing configuration file and restarting a server. * manually editing configuration file and restarting a server.
* Factually feature is just a collection of settings with one universal * Creating a `Feature` instance should be done by using
* 'isActive' setting that tells Acedia whether or not to load a feature. * `EnableMe()` / `DisableMe()` methods; instead of regular `Constructor()`
* Copyright 2019 Anton Tarasenko * and `Finalizer()` one should use `OnEnabled() and `OnDisabled()` methods.
* Any instances created through other means will be automatically deallocated,
* enforcing `Singleton`-like behavior for the `Feature` class.
* Copyright 2019 - 2021 Anton Tarasenko
*------------------------------------------------------------------------------ *------------------------------------------------------------------------------
* This file is part of Acedia. * This file is part of Acedia.
* *
@ -22,9 +25,21 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with Acedia. If not, see <https://www.gnu.org/licenses/>. * along with Acedia. If not, see <https://www.gnu.org/licenses/>.
*/ */
class Feature extends Singleton class Feature extends AcediaObject
abstract; abstract;
// Default value of this variable will store one and only existing version
// of `Feature` of this class.
var private Feature activeInstance;
var private int activeInstanceLifeVersion;
// Setting default value of this variable to 'true' prevents creation of
// a `Feature`, even if no instances of it exist. This is used to ensure active
// `Feature`s can only be created through the proper means and behave like
// singletons.
// Only a default value is ever used.
var protected bool blockSpawning;
// Setting that tells Acedia whether or not to enable this feature // Setting that tells Acedia whether or not to enable this feature
// during initialization. // during initialization.
// Only it's default value is ever used. // Only it's default value is ever used.
@ -33,56 +48,127 @@ var private config bool autoEnable;
// Listeners listed here will be automatically activated. // Listeners listed here will be automatically activated.
var public const array< class<Listener> > requiredListeners; var public const array< class<Listener> > requiredListeners;
// Sets whether to enable this feature by default. protected function Contructor()
public static final function SetAutoEnable(bool doEnable)
{ {
default.autoEnable = doEnable; if (default.blockSpawning)
StaticSaveConfig(); {
FreeSelf();
return;
}
SetListenersActiveSatus(true);
OnEnabled();
} }
protected function Finalizer()
{
if (GetInstance() == self)
{
SetListenersActiveSatus(false);
OnDisabled();
default.activeInstance = none;
}
}
/**
* Returns an instance of the `Feature` of the class used to call
* this method.
*
* @return Active `Feature` instance of the class used to call
* this method (i.e. `class'MyFunFeature'.static.GetInstance()`).
* `none` if particular `Feature` in question is not currently active.
*/
public final static function Feature GetInstance()
{
if (default.activeInstance == none) {
return none;
}
if ( default.activeInstance.GetLifeVersion()
!= default.activeInstanceLifeVersion)
{
default.activeInstance = none;
return none;
}
return default.activeInstance;
}
/**
* Checks if caller `Feature` should be auto-enabled on game starting.
*
* @return `true` if caller `Feature` should be auto-enabled and
* `false` otherwise.
*/
public static final function bool IsAutoEnabled() public static final function bool IsAutoEnabled()
{ {
return default.autoEnable; return default.autoEnable;
} }
// Whether feature is enabled is determined by /**
* Checks whether caller `Feature` is currently enabled.
*
* @return `true` if caller `Feature` is currently enabled and
* `false` otherwise.
*/
public static final function bool IsEnabled() public static final function bool IsEnabled()
{ {
return (GetInstance() != none); return (GetInstance() != none);
} }
// Enables feature of given class. /**
* Enables the feature and returns it's active instance.
*
* Cannot fail. Any checks on whether it's appropriate to enable `Feature`
* must be done separately, before calling this method.
*
* @return Active instance of the caller `Feature` class.
*/
public static final function Feature EnableMe() public static final function Feature EnableMe()
{ {
local Feature newInstance; local Feature newInstance;
if (IsEnabled()) if (IsEnabled()) {
{ return GetInstance();
return Feature(GetInstance());
} }
default.blockSpawning = false; default.blockSpawning = false;
// TODO: code duplication with `Service`?
newInstance = Feature(__().memory.Allocate(default.class)); newInstance = Feature(__().memory.Allocate(default.class));
default.activeInstance = newInstance;
default.activeInstanceLifeVersion = newInstance.GetLifeVersion();
default.blockSpawning = true; default.blockSpawning = true;
return newInstance; return newInstance;
} }
/**
* Disables this feature in case it is enabled. Does nothing otherwise.
*
* @return `true` if `Feature` in question was enabled at th moment of
* the call and `false` otherwise.
*/
public static final function bool DisableMe() public static final function bool DisableMe()
{ {
local Feature myself; local Feature myself;
myself = Feature(GetInstance()); myself = GetInstance();
if (myself != none) if (myself != none)
{ {
myself.Destroy(); myself.FreeSelf();
return true; return true;
} }
return false; return false;
} }
// Event functions that are called when /**
* When using proper methods for enabling a `Feature`,
* this method is guaranteed to be called right after it is enabled.
*
* AVOID MANUALLY CALLING IT.
*/
protected function OnEnabled(){} protected function OnEnabled(){}
/**
* When using proper methods for enabling a `Feature`,
* this method is guaranteed to be called right after it is disabled.
*
* AVOID MANUALLY CALLING IT.
*/
protected function OnDisabled(){} protected function OnDisabled(){}
// Set listeners' status
private static function SetListenersActiveSatus(bool newStatus) private static function SetListenersActiveSatus(bool newStatus)
{ {
local int i; local int i;
@ -93,25 +179,8 @@ private static function SetListenersActiveSatus(bool newStatus)
} }
} }
protected function OnCreated()
{
default.blockSpawning = true;
SetListenersActiveSatus(true);
OnEnabled();
}
protected function OnDestroyed()
{
SetListenersActiveSatus(false);
OnDisabled();
}
defaultproperties defaultproperties
{ {
autoEnable = false autoEnable = false
DrawType = DT_None
// Prevent spawning this feature by any other means than 'EnableMe()'.
blockSpawning = true blockSpawning = true
// Features are server-only actors
remoteRole = ROLE_None
} }
Loading…
Cancel
Save