neon-sprawl/server/NeonSprawl.Server/Game/Quests/QuestCatalogServiceCollecti...

79 lines
3.9 KiB
C#

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using NeonSprawl.Server.Game.Crafting;
using NeonSprawl.Server.Game.Encounters;
using NeonSprawl.Server.Game.Factions;
using NeonSprawl.Server.Game.Items;
using NeonSprawl.Server.Game.Skills;
namespace NeonSprawl.Server.Game.Quests;
/// <summary>DI registration for the fail-fast quest catalog and registry (NEO-113, NEO-114).</summary>
public static class QuestCatalogServiceCollectionExtensions
{
/// <summary>Binds <see cref="ContentPathsOptions"/> and registers <see cref="QuestDefinitionCatalog"/> and <see cref="IQuestDefinitionRegistry"/> as singletons.</summary>
public static IServiceCollection AddQuestDefinitionCatalog(this IServiceCollection services, IConfiguration configuration)
{
services.AddOptions<ContentPathsOptions>()
.Bind(configuration.GetSection(ContentPathsOptions.SectionName));
services.AddSingleton<QuestDefinitionCatalog>(sp =>
{
var hostEnv = sp.GetRequiredService<IHostEnvironment>();
var opts = sp.GetRequiredService<IOptions<ContentPathsOptions>>().Value;
var itemCatalog = sp.GetRequiredService<ItemDefinitionCatalog>();
var recipeCatalog = sp.GetRequiredService<RecipeDefinitionCatalog>();
var encounterCatalog = sp.GetRequiredService<EncounterDefinitionCatalog>();
var skillCatalog = sp.GetRequiredService<SkillDefinitionCatalog>();
var factionCatalog = sp.GetRequiredService<FactionDefinitionCatalog>();
var logger = sp.GetRequiredService<ILoggerFactory>()
.CreateLogger("NeonSprawl.Server.Game.Quests.QuestCatalog");
var questsDir = QuestCatalogPathResolution.ResolveQuestsDirectory(
opts.QuestsDirectory,
hostEnv.ContentRootPath);
var questDefSchemaPath = QuestCatalogPathResolution.ResolveQuestDefSchemaPath(
questsDir,
opts.QuestDefSchemaPath,
hostEnv.ContentRootPath);
var questStepDefSchemaPath = QuestCatalogPathResolution.ResolveQuestStepDefSchemaPath(
questsDir,
opts.QuestStepDefSchemaPath,
hostEnv.ContentRootPath);
var questObjectiveDefSchemaPath = QuestCatalogPathResolution.ResolveQuestObjectiveDefSchemaPath(
questsDir,
opts.QuestObjectiveDefSchemaPath,
hostEnv.ContentRootPath);
var rewardGrantRowSchemaPath = QuestCatalogPathResolution.ResolveRewardGrantRowSchemaPath(questsDir);
var questSkillXpGrantSchemaPath = QuestCatalogPathResolution.ResolveQuestSkillXpGrantSchemaPath(questsDir);
var questRewardBundleSchemaPath = QuestCatalogPathResolution.ResolveQuestRewardBundleSchemaPath(questsDir);
var knownItemIds = itemCatalog.ById.Keys.ToHashSet(StringComparer.Ordinal);
var knownRecipeIds = recipeCatalog.ById.Keys.ToHashSet(StringComparer.Ordinal);
var knownEncounterIds = encounterCatalog.ById.Keys.ToHashSet(StringComparer.Ordinal);
var knownFactionIds = factionCatalog.ById.Keys.ToHashSet(StringComparer.Ordinal);
return QuestDefinitionCatalogLoader.Load(
questsDir,
questDefSchemaPath,
questStepDefSchemaPath,
questObjectiveDefSchemaPath,
rewardGrantRowSchemaPath,
questSkillXpGrantSchemaPath,
questRewardBundleSchemaPath,
knownItemIds,
knownRecipeIds,
knownEncounterIds,
knownFactionIds,
skillCatalog.ById,
logger);
});
services.AddSingleton<IQuestDefinitionRegistry>(sp =>
new QuestDefinitionRegistry(sp.GetRequiredService<QuestDefinitionCatalog>()));
return services;
}
}