| | | 1 | | using System.Net.Http.Json; |
| | | 2 | | using Chronicis.Client.Models; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.Services; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Loads render definitions from wwwroot/render-definitions/ as static assets. |
| | | 8 | | /// Caches definitions in memory after first load. |
| | | 9 | | /// Resolution walks from most-specific category to least-specific, then falls back to default. |
| | | 10 | | /// </summary> |
| | | 11 | | public class RenderDefinitionService : IRenderDefinitionService |
| | | 12 | | { |
| | | 13 | | private readonly HttpClient _http; |
| | | 14 | | private readonly ILogger<RenderDefinitionService> _logger; |
| | 0 | 15 | | private readonly Dictionary<string, RenderDefinition?> _cache = new(StringComparer.OrdinalIgnoreCase); |
| | | 16 | | |
| | 0 | 17 | | private static readonly RenderDefinition DefaultDefinition = new() |
| | 0 | 18 | | { |
| | 0 | 19 | | Version = 1, |
| | 0 | 20 | | DisplayName = null, |
| | 0 | 21 | | TitleField = "name", |
| | 0 | 22 | | Sections = new List<RenderSection>(), |
| | 0 | 23 | | Hidden = new List<string> { "pk", "model" }, |
| | 0 | 24 | | CatchAll = true |
| | 0 | 25 | | }; |
| | | 26 | | |
| | 0 | 27 | | public RenderDefinitionService(HttpClient http, ILogger<RenderDefinitionService> logger) |
| | | 28 | | { |
| | 0 | 29 | | _http = http; |
| | 0 | 30 | | _logger = logger; |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | public async Task<RenderDefinition> ResolveAsync(string source, string? categoryPath) |
| | | 34 | | { |
| | | 35 | | // Build candidate paths from most-specific to least-specific |
| | | 36 | | // e.g., for source="ros", categoryPath="bestiary/Cultural-Being": |
| | | 37 | | // 1. render-definitions/ros/bestiary/Cultural-Being.json |
| | | 38 | | // 2. render-definitions/ros/bestiary.json |
| | | 39 | | // 3. render-definitions/ros.json |
| | | 40 | | // 4. render-definitions/_default.json |
| | | 41 | | // 5. Built-in default (hardcoded) |
| | | 42 | | |
| | 0 | 43 | | var candidates = BuildCandidatePaths(source, categoryPath); |
| | | 44 | | |
| | 0 | 45 | | foreach (var candidate in candidates) |
| | | 46 | | { |
| | 0 | 47 | | var definition = await TryLoadAsync(candidate); |
| | 0 | 48 | | if (definition != null) |
| | | 49 | | { |
| | 0 | 50 | | _logger.LogDebug("Resolved render definition: {Path}", candidate); |
| | 0 | 51 | | return definition; |
| | | 52 | | } |
| | 0 | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | _logger.LogDebug( |
| | 0 | 56 | | "No render definition found for source={Source}, category={Category}. Using built-in default.", |
| | 0 | 57 | | source, categoryPath); |
| | 0 | 58 | | return DefaultDefinition; |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | private static List<string> BuildCandidatePaths(string source, string? categoryPath) |
| | | 62 | | { |
| | 0 | 63 | | var candidates = new List<string>(); |
| | | 64 | | |
| | 0 | 65 | | if (!string.IsNullOrWhiteSpace(categoryPath)) |
| | | 66 | | { |
| | | 67 | | // Walk up the category path |
| | | 68 | | // "bestiary/Cultural-Being" → ["bestiary/Cultural-Being", "bestiary"] |
| | 0 | 69 | | var segments = categoryPath.Split('/', StringSplitOptions.RemoveEmptyEntries); |
| | | 70 | | |
| | 0 | 71 | | for (var i = segments.Length; i > 0; i--) |
| | | 72 | | { |
| | 0 | 73 | | var partialPath = string.Join("/", segments.Take(i)); |
| | 0 | 74 | | candidates.Add($"render-definitions/{source}/{partialPath}.json"); |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | // Source-level fallback |
| | 0 | 79 | | candidates.Add($"render-definitions/{source}.json"); |
| | | 80 | | |
| | | 81 | | // Global default |
| | 0 | 82 | | candidates.Add("render-definitions/_default.json"); |
| | | 83 | | |
| | 0 | 84 | | return candidates; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | private async Task<RenderDefinition?> TryLoadAsync(string path) |
| | | 88 | | { |
| | 0 | 89 | | if (_cache.TryGetValue(path, out var cached)) |
| | 0 | 90 | | return cached; |
| | | 91 | | |
| | | 92 | | try |
| | | 93 | | { |
| | 0 | 94 | | var response = await _http.GetAsync(path); |
| | 0 | 95 | | if (!response.IsSuccessStatusCode) |
| | | 96 | | { |
| | 0 | 97 | | _cache[path] = null; |
| | 0 | 98 | | return null; |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | var definition = await response.Content.ReadFromJsonAsync<RenderDefinition>(); |
| | 0 | 102 | | _cache[path] = definition; |
| | 0 | 103 | | return definition; |
| | | 104 | | } |
| | 0 | 105 | | catch (HttpRequestException) |
| | | 106 | | { |
| | 0 | 107 | | _cache[path] = null; |
| | 0 | 108 | | return null; |
| | | 109 | | } |
| | 0 | 110 | | catch (Exception ex) |
| | | 111 | | { |
| | 0 | 112 | | _logger.LogWarning(ex, "Failed to parse render definition at {Path}", path); |
| | 0 | 113 | | _cache[path] = null; |
| | 0 | 114 | | return null; |
| | | 115 | | } |
| | 0 | 116 | | } |
| | | 117 | | } |