Class ConfigManager<T>
- Namespace
- AetherUtils.Core.Configuration
- Assembly
- AetherUtils.Core.dll
Provides methods for saving, loading, and querying a generic YAML configuration.
public class ConfigManager<T> where T : class, new()
Type Parameters
T- The DTO class that represents the configuration.
- Inheritance
-
ConfigManager<T>
- Derived
- Inherited Members
- Extension Methods
Remarks
The custom class should have its properties related to configuration marked with a ConfigAttribute.
These properties are the only ones which will be serialized and de-serialized from disk.
Constructors
ConfigManager(string)
Create a configuration manager with the specified config file.
public ConfigManager(string configFilePath)
Parameters
configFilePathstring- The full path to a configuration file to save and/or load.
Properties
ConfigExists
Get a value indicating whether the configuration file exists on disk.
public bool ConfigExists { get; }
Property Value
ConfigFilePath
Get or set the path to the configuration file on disk.
public string? ConfigFilePath { get; set; }
Property Value
CurrentConfig
Get or set the current configuration.
[UsedImplicitly]
public T? CurrentConfig { get; set; }
Property Value
- T
IsInitialized
Get a value indicating whether the configuration has been initialized.
public bool IsInitialized { get; }
Property Value
Methods
CreateDefaultConfig()
Create the default configuration.
[UsedImplicitly]
public virtual bool CreateDefaultConfig()
Returns
Get(ConfigOption)
Get a configuration value specified by the configuration
option.public object? Get(ConfigOption option)
Parameters
optionConfigOption- The ConfigOption containing information about the value to get.
Returns
- object
- The configuration value or
nullif the value did not exist.
Get(string)
Get a configuration value specified by
configName.public object? Get(string configName)
Parameters
configNamestring- The name of the configuration to get.
Returns
- object
- The configuration value or
nullif the value did not exist.
GetConfig()
Get the current configuration as an object.
[UsedImplicitly]
public T? GetConfig()
Returns
- T
- The current configuration or
nullif not initialized.
Load(bool)
Load a configuration file from disk based on the ConfigFilePath.
public bool Load(bool applyDefaults = false)
Parameters
applyDefaultsbool- Indicates whether the default values should be applied to the loaded configuration.
Returns
- bool
trueif the config loaded successfully;falseotherwise.
Exceptions
- ArgumentException
- If ConfigFilePath is
nullor empty. - FileNotFoundException
- If the configuration file specified by ConfigFilePath was not found on disk.
LoadAsync(bool)
Asynchronously load a configuration file from disk based on the ConfigFilePath.
public Task<bool> LoadAsync(bool applyDefaults = false)
Parameters
applyDefaultsbool- Indicates whether the default values should be applied to the loaded configuration.
Returns
Exceptions
- ArgumentException
- If ConfigFilePath is
nullor empty. - FileNotFoundException
- If the configuration file specified by ConfigFilePath was not found on disk.
Save()
Save a configuration file to disk based on the CurrentConfig and the ConfigFilePath.
public bool Save()
Returns
- bool
trueif the config saved successfully;falseotherwise.
Exceptions
- ArgumentException
- If ConfigFilePath is
nullor empty.
SaveAsync()
Asynchronously save a configuration file to disk based on the CurrentConfig and the
ConfigFilePath.
public Task<bool> SaveAsync()
Returns
Exceptions
- ArgumentException
- If ConfigFilePath is
nullor empty.
Set(ConfigOption)
Set a configuration value specified by the configuration
option.public bool Set(ConfigOption option)
Parameters
optionConfigOption- The ConfigOption containing information about the value to set.
Returns
- bool
trueif the value was set successfully;falseotherwise.
Set(string, object?)
Set a configuration
value specified by configName.[UsedImplicitly]
public bool Set(string configName, object? value)
Parameters
Returns
- bool
trueif the value was set successfully;falseotherwise.
SetConfig(T?)
Set the current configuration object. This is more convenient than setting each property manually using
Set(ConfigOption).
[UsedImplicitly]
public void SetConfig(T? config)
Parameters
configT- The configuration object to set the internal config to.