neon-sprawl/server/NeonSprawl.Server/Game/Crafting/RecipeCatalogPathResolution.cs

79 lines
3.1 KiB
C#

namespace NeonSprawl.Server.Game.Crafting;
/// <summary>Resolves recipe catalog paths for local dev, tests, and container layouts (NEO-66).</summary>
public static class RecipeCatalogPathResolution
{
/// <summary>Walks <paramref name="startDirectory"/> and parents for an existing <c>content/recipes</c> directory.</summary>
public static string? TryDiscoverRecipesDirectory(string startDirectory)
{
for (var dir = new DirectoryInfo(startDirectory); dir is not null; dir = dir.Parent)
{
var candidate = Path.Combine(dir.FullName, "content", "recipes");
if (Directory.Exists(candidate))
return Path.GetFullPath(candidate);
}
return null;
}
/// <summary>
/// Resolves the recipes catalog directory.
/// Empty <paramref name="configuredRecipesDirectory"/> triggers discovery from <see cref="AppContext.BaseDirectory"/>.
/// </summary>
public static string ResolveRecipesDirectory(string? configuredRecipesDirectory, string contentRootPath)
{
if (string.IsNullOrWhiteSpace(configuredRecipesDirectory))
{
var discovered = TryDiscoverRecipesDirectory(AppContext.BaseDirectory);
if (discovered is not null)
return discovered;
throw new InvalidOperationException(
"Content:RecipesDirectory is not set and auto-discovery failed (no ancestor of AppContext.BaseDirectory contains 'content/recipes'). " +
"Set Content:RecipesDirectory in configuration or environment (e.g. Content__RecipesDirectory).");
}
var trimmed = configuredRecipesDirectory.Trim();
if (Path.IsPathRooted(trimmed))
return Path.GetFullPath(trimmed);
return Path.GetFullPath(Path.Combine(contentRootPath, trimmed));
}
/// <summary>Resolves JSON Schema path for a single recipe row (Draft 2020-12).</summary>
public static string ResolveRecipeDefSchemaPath(
string recipesDirectory,
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(recipesDirectory, "..", "schemas", "recipe-def.schema.json"));
}
/// <summary>Resolves JSON Schema path for a recipe I/O row (Draft 2020-12).</summary>
public static string ResolveRecipeIoRowSchemaPath(
string recipesDirectory,
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(recipesDirectory, "..", "schemas", "recipe-io-row.schema.json"));
}
}