neon-sprawl/server/NeonSprawl.Server/Game/Skills/SkillCatalogServiceCollecti...

38 lines
1.7 KiB
C#

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
namespace NeonSprawl.Server.Game.Skills;
/// <summary>DI registration for the fail-fast skill catalog (NEO-34).</summary>
public static class SkillCatalogServiceCollectionExtensions
{
/// <summary>Binds <see cref="ContentPathsOptions"/> and registers <see cref="SkillDefinitionCatalog"/> and <see cref="ISkillDefinitionRegistry"/> as singletons.</summary>
public static IServiceCollection AddSkillDefinitionCatalog(this IServiceCollection services, IConfiguration configuration)
{
services.AddOptions<ContentPathsOptions>()
.Bind(configuration.GetSection(ContentPathsOptions.SectionName));
services.AddSingleton<SkillDefinitionCatalog>(sp =>
{
var hostEnv = sp.GetRequiredService<IHostEnvironment>();
var opts = sp.GetRequiredService<IOptions<ContentPathsOptions>>().Value;
var logger = sp.GetRequiredService<ILoggerFactory>()
.CreateLogger("NeonSprawl.Server.Game.Skills.SkillCatalog");
var skillsDir = SkillCatalogPathResolution.ResolveSkillsDirectory(opts.SkillsDirectory, hostEnv.ContentRootPath);
var schemaPath = SkillCatalogPathResolution.ResolveSkillDefSchemaPath(
skillsDir,
opts.SkillDefSchemaPath,
hostEnv.ContentRootPath);
return SkillDefinitionCatalogLoader.Load(skillsDir, schemaPath, logger);
});
services.AddSingleton<ISkillDefinitionRegistry>(sp =>
new SkillDefinitionRegistry(sp.GetRequiredService<SkillDefinitionCatalog>()));
return services;
}
}