2021-08-27 15:03:47 +00:00
|
|
|
using Autofac;
|
2020-12-19 10:55:35 +00:00
|
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Core;
|
|
|
|
|
|
|
|
public class ConfigModule<T>: Module where T : new()
|
2020-12-19 10:55:35 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly string _submodule;
|
2020-12-19 10:55:35 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public ConfigModule(string submodule = null)
|
|
|
|
{
|
|
|
|
_submodule = submodule;
|
|
|
|
}
|
2020-12-19 10:55:35 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
protected override void Load(ContainerBuilder builder)
|
|
|
|
{
|
|
|
|
// We're assuming IConfiguration is already available somehow - it comes from various places (auto-injected in ASP, etc)
|
2020-12-19 10:55:35 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Register the CoreConfig and where to find it
|
|
|
|
builder.Register(c =>
|
|
|
|
c.Resolve<IConfiguration>().GetSection("PluralKit").Get<CoreConfig>() ?? new CoreConfig())
|
|
|
|
.SingleInstance();
|
2020-12-19 10:55:35 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Register the submodule config (BotConfig, etc) if specified
|
|
|
|
if (_submodule != null)
|
|
|
|
builder.Register(c =>
|
|
|
|
c.Resolve<IConfiguration>().GetSection("PluralKit").GetSection(_submodule).Get<T>() ?? new T())
|
|
|
|
.SingleInstance();
|
2020-12-19 10:55:35 +00:00
|
|
|
}
|
|
|
|
}
|