neon-sprawl/server/NeonSprawl.Server/Game/Contracts/ContractCatalogPathResoluti...

81 lines
4.0 KiB
C#

namespace NeonSprawl.Server.Game.Contracts;
/// <summary>Resolves contract catalog paths for local dev, tests, and container layouts (NEO-145).</summary>
public static class ContractCatalogPathResolution
{
/// <summary>Walks <paramref name="startDirectory"/> and parents for an existing <c>content/contracts</c> directory.</summary>
public static string? TryDiscoverContractsDirectory(string startDirectory)
{
for (var dir = new DirectoryInfo(startDirectory); dir is not null; dir = dir.Parent)
{
var candidate = Path.Combine(dir.FullName, "content", "contracts");
if (Directory.Exists(candidate))
return Path.GetFullPath(candidate);
}
return null;
}
/// <summary>
/// Resolves the contracts catalog directory.
/// Empty <paramref name="configuredContractsDirectory"/> triggers discovery from <see cref="AppContext.BaseDirectory"/>.
/// </summary>
public static string ResolveContractsDirectory(string? configuredContractsDirectory, string contentRootPath)
{
if (string.IsNullOrWhiteSpace(configuredContractsDirectory))
{
var discovered = TryDiscoverContractsDirectory(AppContext.BaseDirectory);
if (discovered is not null)
return discovered;
throw new InvalidOperationException(
"Content:ContractsDirectory is not set and auto-discovery failed (no ancestor of AppContext.BaseDirectory contains 'content/contracts'). " +
"Set Content:ContractsDirectory in configuration or environment (e.g. Content__ContractsDirectory).");
}
var trimmed = configuredContractsDirectory.Trim();
if (Path.IsPathRooted(trimmed))
return Path.GetFullPath(trimmed);
return Path.GetFullPath(Path.Combine(contentRootPath, trimmed));
}
/// <summary>Resolves JSON Schema path for a single contract template row (Draft 2020-12).</summary>
public static string ResolveContractTemplateSchemaPath(
string contractsDirectory,
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(contractsDirectory, "..", "schemas", "contract-template.schema.json"));
}
/// <summary>Resolves JSON Schema path for a reward grant row (shared by quest bundles and contract templates).</summary>
public static string ResolveRewardGrantRowSchemaPath(string contractsDirectory) =>
Path.GetFullPath(Path.Combine(contractsDirectory, "..", "schemas", "reward-grant-row.schema.json"));
/// <summary>Resolves JSON Schema path for a quest skill XP grant row.</summary>
public static string ResolveQuestSkillXpGrantSchemaPath(string contractsDirectory) =>
Path.GetFullPath(Path.Combine(contractsDirectory, "..", "schemas", "quest-skill-xp-grant.schema.json"));
/// <summary>Resolves JSON Schema path for a completion reward bundle.</summary>
public static string ResolveQuestRewardBundleSchemaPath(string contractsDirectory) =>
Path.GetFullPath(Path.Combine(contractsDirectory, "..", "schemas", "quest-reward-bundle.schema.json"));
/// <summary>Resolves JSON Schema path for a faction gate rule row.</summary>
public static string ResolveFactionGateRuleSchemaPath(string contractsDirectory) =>
Path.GetFullPath(Path.Combine(contractsDirectory, "..", "schemas", "faction-gate-rule.schema.json"));
/// <summary>Resolves JSON Schema path for a reputation grant row.</summary>
public static string ResolveReputationGrantRowSchemaPath(string contractsDirectory) =>
Path.GetFullPath(Path.Combine(contractsDirectory, "..", "schemas", "reputation-grant-row.schema.json"));
}