You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
209 lines
6.6 KiB
209 lines
6.6 KiB
/** |
|
* Container for the information about available resources from other packages. |
|
* 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 AcediaEnvironment extends AcediaObject; |
|
|
|
/** |
|
* # `AcediaEnvironment` |
|
* |
|
* Instance of this class will be used by Acedia to manage resources available |
|
* from different packages like `Feature`s, aliases, etc.. |
|
* This is mostly necessary to implement Acedia loader (and, possibly, |
|
* its alternatives) that would load available packages and enable `Feature`s |
|
* admin wants to be enabled. |
|
* |
|
* ## Packages |
|
* |
|
* Any package to be used in Acedia should first be *registered* with |
|
* `RegisterPackage()` method. Then a manifest class from it will be read and |
|
* Acedia will become aware of all the resources that package contains. |
|
* Once any of those resources is used, package gets marked as *loaded* and its |
|
* *entry object* (if specified) will be created. |
|
*/ |
|
|
|
var private array< class<_manifest> > availablePackages; |
|
var private array< class<_manifest> > loadedPackages; |
|
|
|
var private array< class<Feature> > availableFeatures; |
|
var private array<Feature> enabledFeatures; |
|
|
|
var private string manifestSuffix; |
|
|
|
var private LoggerAPI.Definition infoRegisteringPackage, infoAlreadyRegistered; |
|
var private LoggerAPI.Definition errNotRegistered; |
|
|
|
protected function Constructor() |
|
{ |
|
// Always register our core package |
|
RegisterPackage_S("AcediaCore.Manifest"); |
|
} |
|
|
|
/** |
|
* Registers an Acedia package with name given by `packageName`. |
|
* |
|
* @param packageName Name of the package to register. Must not be `none`. |
|
* This package must exist and not have yet been registered in this |
|
* environment. |
|
* @return `true` if package was successfully registered, `false` if it |
|
* either does not exist, was already registered or `packageName` is |
|
* `none`. |
|
*/ |
|
public final function bool RegisterPackage(BaseText packageName) |
|
{ |
|
local class<_manifest> manifestClass; |
|
|
|
if (packageName == none) { |
|
return false; |
|
} |
|
_.logger.Auto(infoRegisteringPackage).Arg(packageName.Copy()); |
|
manifestClass = class<_manifest>(DynamicLoadObject( |
|
packageName $ manifestSuffix, class'Class', true)); |
|
if (manifestClass == none) |
|
{ |
|
_.logger.Auto(errNotRegistered).Arg(packageName.Copy()); |
|
return false; |
|
} |
|
if (IsManifestRegistered(manifestClass)) |
|
{ |
|
_.logger.Auto(infoAlreadyRegistered).Arg(packageName.Copy()); |
|
return false; |
|
} |
|
availablePackages[availablePackages.length] = manifestClass; |
|
ReadManifest(manifestClass); |
|
return true; |
|
} |
|
|
|
/** |
|
* Registers an Acedia package with name given by `packageName`. |
|
* |
|
* @param packageName Name of the package to register. |
|
* This package must exist and not have yet been registered in this |
|
* environment. |
|
* @return `true` if package was successfully registered, `false` if it |
|
* either does not exist or was already registered. |
|
*/ |
|
public final function RegisterPackage_S(string packageName) |
|
{ |
|
local Text wrapper; |
|
|
|
wrapper = _.text.FromString(packageName); |
|
RegisterPackage(wrapper); |
|
_.memory.Free(wrapper); |
|
} |
|
|
|
private final function bool IsManifestRegistered(class<_manifest> manifestClass) |
|
{ |
|
local int i; |
|
|
|
for (i = 0; i < availablePackages.length; i += 1) |
|
{ |
|
if (manifestClass == availablePackages[i]) { |
|
return true; |
|
} |
|
} |
|
return false; |
|
} |
|
|
|
private final function ReadManifest(class<_manifest> manifestClass) |
|
{ |
|
local int i; |
|
|
|
|
|
for (i = 0; i < manifestClass.default.aliasSources.length; i += 1) |
|
{ |
|
if (manifestClass.default.aliasSources[i] == none) { |
|
continue; |
|
} |
|
_.memory.Allocate(manifestClass.default.aliasSources[i]); |
|
} |
|
for (i = 0; i < manifestClass.default.features.length; i += 1) |
|
{ |
|
if (manifestClass.default.features[i] == none) { |
|
continue; |
|
} |
|
manifestClass.default.features[i].static.LoadConfigs(); |
|
availableFeatures[availableFeatures.length] = |
|
manifestClass.default.features[i]; |
|
} |
|
for (i = 0; i < manifestClass.default.testCases.length; i += 1) |
|
{ |
|
class'TestingService'.static |
|
.RegisterTestCase(manifestClass.default.testCases[i]); |
|
} |
|
} |
|
|
|
/** |
|
* Returns all packages registered in the caller `AcediaEnvironment`. |
|
* |
|
* NOTE: package being registered doesn't mean it's actually loaded. |
|
* Package must either be explicitly loaded or automatically when one of its |
|
* resources is being used. |
|
* |
|
* @return All packages registered in caller `AcediaEnvironment`. |
|
*/ |
|
public final function array< class<_manifest> > GetAvailablePackages() |
|
{ |
|
return availablePackages; |
|
} |
|
|
|
/** |
|
* Returns all packages loaded in the caller `AcediaEnvironment`. |
|
* |
|
* NOTE: package being registered doesn't mean it's actually loaded. |
|
* Package must either be explicitly loaded or automatically when one of its |
|
* resources is being used. |
|
* |
|
* @return All packages loaded in caller `AcediaEnvironment`. |
|
*/ |
|
public final function array< class<_manifest> > GetLoadedPackages() |
|
{ |
|
return loadedPackages; |
|
} |
|
|
|
/** |
|
* Returns all `Feature`s available in the caller `AcediaEnvironment`. |
|
* |
|
* @return All `Feature`s available in the caller `AcediaEnvironment`. |
|
*/ |
|
public final function array< class<Feature> > GetAvailableFeatures() |
|
{ |
|
return availableFeatures; |
|
} |
|
|
|
/** |
|
* Returns all `Feature` instances enabled in the caller `AcediaEnvironment`. |
|
* |
|
* @return All `Feature`s enabled in the caller `AcediaEnvironment`. |
|
*/ |
|
public final function array<Feature> GetEnabledFeatures() |
|
{ |
|
local int i; |
|
for (i = 0; i < enabledFeatures.length; i += 1) { |
|
enabledFeatures[i].NewRef(); |
|
} |
|
return enabledFeatures; |
|
} |
|
|
|
defaultproperties |
|
{ |
|
manifestSuffix = ".Manifest" |
|
infoRegisteringPackage = (l=LOG_Info,m="Registering package \"%1\".") |
|
infoAlreadyRegistered = (l=LOG_Info,m="Package \"%1\" is already registered.") |
|
errNotRegistered = (l=LOG_Error,m="Package \"%2\" has failed to be registered.") |
|
} |