using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using NeonSprawl.Server.Game.Combat; using NeonSprawl.Server.Game.Skills; namespace NeonSprawl.Server.Game.Npc; /// DI registration for the fail-fast NPC behavior catalog (NEO-88). public static class NpcBehaviorCatalogServiceCollectionExtensions { /// Binds and registers and as singletons. public static IServiceCollection AddNpcBehaviorDefinitionCatalog(this IServiceCollection services, IConfiguration configuration) { services.AddOptions() .Bind(configuration.GetSection(ContentPathsOptions.SectionName)); services.AddSingleton(sp => { var hostEnv = sp.GetRequiredService(); var opts = sp.GetRequiredService>().Value; var logger = sp.GetRequiredService() .CreateLogger("NeonSprawl.Server.Game.Npc.NpcBehaviorCatalog"); var npcBehaviorsDir = NpcBehaviorCatalogPathResolution.ResolveNpcBehaviorsDirectory( opts.NpcBehaviorsDirectory, hostEnv.ContentRootPath); var schemaPath = NpcBehaviorCatalogPathResolution.ResolveNpcBehaviorDefSchemaPath( npcBehaviorsDir, opts.NpcBehaviorDefSchemaPath, hostEnv.ContentRootPath); var catalog = NpcBehaviorDefinitionCatalogLoader.Load(npcBehaviorsDir, schemaPath, logger); var abilityRegistry = sp.GetRequiredService(); var crossRefErr = PrototypeE5M2NpcBehaviorCatalogRules.TryGetAttackAbilityCrossRefError( catalog.ById, abilityRegistry); if (crossRefErr is not null) { throw new InvalidOperationException(crossRefErr); } return catalog; }); services.AddSingleton(sp => new NpcBehaviorDefinitionRegistry(sp.GetRequiredService())); return services; } }