56 lines
1.7 KiB
C#
56 lines
1.7 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace NeonSprawl.Server.Game.Npc;
|
|
|
|
/// <summary>Adapter over <see cref="NpcBehaviorDefinitionCatalog"/> (NEO-89).</summary>
|
|
public sealed class NpcBehaviorDefinitionRegistry(NpcBehaviorDefinitionCatalog catalog) : INpcBehaviorDefinitionRegistry
|
|
{
|
|
private readonly IReadOnlyList<NpcBehaviorDefRow> _definitionsInIdOrder = BuildDefinitionsInIdOrder(catalog);
|
|
|
|
/// <inheritdoc />
|
|
public bool TryGetDefinition(string? behaviorId, [NotNullWhen(true)] out NpcBehaviorDefRow? definition)
|
|
{
|
|
if (behaviorId is null)
|
|
{
|
|
definition = null;
|
|
return false;
|
|
}
|
|
|
|
return catalog.TryGetBehavior(behaviorId, out definition);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public bool TryNormalizeKnown(string? rawBehaviorId, [NotNullWhen(true)] out string normalized)
|
|
{
|
|
if (rawBehaviorId is null)
|
|
{
|
|
normalized = string.Empty;
|
|
return false;
|
|
}
|
|
|
|
normalized = rawBehaviorId.Trim().ToLowerInvariant();
|
|
if (normalized.Length == 0)
|
|
{
|
|
normalized = string.Empty;
|
|
return false;
|
|
}
|
|
|
|
return catalog.TryGetBehavior(normalized, out _);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IReadOnlyList<NpcBehaviorDefRow> GetDefinitionsInIdOrder() => _definitionsInIdOrder;
|
|
|
|
private static IReadOnlyList<NpcBehaviorDefRow> BuildDefinitionsInIdOrder(NpcBehaviorDefinitionCatalog catalog)
|
|
{
|
|
string[] ids = [.. catalog.ById.Keys.OrderBy(k => k, StringComparer.Ordinal)];
|
|
var list = new List<NpcBehaviorDefRow>(ids.Length);
|
|
foreach (var id in ids)
|
|
{
|
|
list.Add(catalog.ById[id]);
|
|
}
|
|
|
|
return list;
|
|
}
|
|
}
|