82 lines
3.7 KiB
C#
82 lines
3.7 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Options;
|
|
using NeonSprawl.Server.Game.Items;
|
|
using NeonSprawl.Server.Game.Skills;
|
|
|
|
namespace NeonSprawl.Server.Game.Encounters;
|
|
|
|
/// <summary>DI registration for the fail-fast encounter + reward-table catalogs and registries (NEO-101, NEO-102).</summary>
|
|
public static class EncounterCatalogServiceCollectionExtensions
|
|
{
|
|
/// <summary>Binds <see cref="ContentPathsOptions"/> and registers encounter + reward-table catalog singletons and registries.</summary>
|
|
public static IServiceCollection AddEncounterAndRewardCatalogs(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
services.AddOptions<ContentPathsOptions>()
|
|
.Bind(configuration.GetSection(ContentPathsOptions.SectionName));
|
|
|
|
services.AddSingleton<RewardTableDefinitionCatalog>(sp =>
|
|
{
|
|
var hostEnv = sp.GetRequiredService<IHostEnvironment>();
|
|
var opts = sp.GetRequiredService<IOptions<ContentPathsOptions>>().Value;
|
|
var itemCatalog = sp.GetRequiredService<ItemDefinitionCatalog>();
|
|
var logger = sp.GetRequiredService<ILoggerFactory>()
|
|
.CreateLogger("NeonSprawl.Server.Game.Encounters.RewardTableCatalog");
|
|
|
|
var rewardTablesDir = RewardTableCatalogPathResolution.ResolveRewardTablesDirectory(
|
|
opts.RewardTablesDirectory,
|
|
hostEnv.ContentRootPath);
|
|
var rewardTableSchemaPath = RewardTableCatalogPathResolution.ResolveRewardTableDefSchemaPath(
|
|
rewardTablesDir,
|
|
opts.RewardTableDefSchemaPath,
|
|
hostEnv.ContentRootPath);
|
|
var rewardGrantRowSchemaPath = RewardTableCatalogPathResolution.ResolveRewardGrantRowSchemaPath(
|
|
rewardTablesDir,
|
|
opts.RewardGrantRowSchemaPath,
|
|
hostEnv.ContentRootPath);
|
|
|
|
var knownItemIds = itemCatalog.ById.Keys.ToHashSet(StringComparer.Ordinal);
|
|
|
|
return RewardTableDefinitionCatalogLoader.Load(
|
|
rewardTablesDir,
|
|
rewardTableSchemaPath,
|
|
rewardGrantRowSchemaPath,
|
|
knownItemIds,
|
|
logger);
|
|
});
|
|
|
|
services.AddSingleton<EncounterDefinitionCatalog>(sp =>
|
|
{
|
|
var hostEnv = sp.GetRequiredService<IHostEnvironment>();
|
|
var opts = sp.GetRequiredService<IOptions<ContentPathsOptions>>().Value;
|
|
var rewardTableCatalog = sp.GetRequiredService<RewardTableDefinitionCatalog>();
|
|
var logger = sp.GetRequiredService<ILoggerFactory>()
|
|
.CreateLogger("NeonSprawl.Server.Game.Encounters.EncounterCatalog");
|
|
|
|
var encountersDir = EncounterCatalogPathResolution.ResolveEncountersDirectory(
|
|
opts.EncountersDirectory,
|
|
hostEnv.ContentRootPath);
|
|
var encounterDefSchemaPath = EncounterCatalogPathResolution.ResolveEncounterDefSchemaPath(
|
|
encountersDir,
|
|
opts.EncounterDefSchemaPath,
|
|
hostEnv.ContentRootPath);
|
|
|
|
var knownRewardTableIds = rewardTableCatalog.ById.Keys.ToHashSet(StringComparer.Ordinal);
|
|
|
|
return EncounterDefinitionCatalogLoader.Load(
|
|
encountersDir,
|
|
encounterDefSchemaPath,
|
|
knownRewardTableIds,
|
|
logger);
|
|
});
|
|
|
|
services.AddSingleton<IEncounterDefinitionRegistry>(sp =>
|
|
new EncounterDefinitionRegistry(sp.GetRequiredService<EncounterDefinitionCatalog>()));
|
|
|
|
services.AddSingleton<IRewardTableDefinitionRegistry>(sp =>
|
|
new RewardTableDefinitionRegistry(sp.GetRequiredService<RewardTableDefinitionCatalog>()));
|
|
|
|
return services;
|
|
}
|
|
}
|