neon-sprawl/server/NeonSprawl.Server/Game/Encounters/EncounterCatalogPathResolut...

61 lines
2.5 KiB
C#

namespace NeonSprawl.Server.Game.Encounters;
/// <summary>Resolves encounter catalog paths for local dev, tests, and container layouts (NEO-101).</summary>
public static class EncounterCatalogPathResolution
{
/// <summary>Walks <paramref name="startDirectory"/> and parents for an existing <c>content/encounters</c> directory.</summary>
public static string? TryDiscoverEncountersDirectory(string startDirectory)
{
for (var dir = new DirectoryInfo(startDirectory); dir is not null; dir = dir.Parent)
{
var candidate = Path.Combine(dir.FullName, "content", "encounters");
if (Directory.Exists(candidate))
return Path.GetFullPath(candidate);
}
return null;
}
/// <summary>
/// Resolves the encounters catalog directory.
/// Empty <paramref name="configuredEncountersDirectory"/> triggers discovery from <see cref="AppContext.BaseDirectory"/>.
/// </summary>
public static string ResolveEncountersDirectory(string? configuredEncountersDirectory, string contentRootPath)
{
if (string.IsNullOrWhiteSpace(configuredEncountersDirectory))
{
var discovered = TryDiscoverEncountersDirectory(AppContext.BaseDirectory);
if (discovered is not null)
return discovered;
throw new InvalidOperationException(
"Content:EncountersDirectory is not set and auto-discovery failed (no ancestor of AppContext.BaseDirectory contains 'content/encounters'). " +
"Set Content:EncountersDirectory in configuration or environment (e.g. Content__EncountersDirectory).");
}
var trimmed = configuredEncountersDirectory.Trim();
if (Path.IsPathRooted(trimmed))
return Path.GetFullPath(trimmed);
return Path.GetFullPath(Path.Combine(contentRootPath, trimmed));
}
/// <summary>Resolves JSON Schema path for a single encounter row (Draft 2020-12).</summary>
public static string ResolveEncounterDefSchemaPath(
string encountersDirectory,
string? configuredSchemaPath,
string contentRootPath)
{
if (!string.IsNullOrWhiteSpace(configuredSchemaPath))
{
var trimmed = configuredSchemaPath.Trim();
if (Path.IsPathRooted(trimmed))
return Path.GetFullPath(trimmed);
return Path.GetFullPath(Path.Combine(contentRootPath, trimmed));
}
return Path.GetFullPath(Path.Combine(encountersDirectory, "..", "schemas", "encounter-def.schema.json"));
}
}