Generate critical source image artifacts

This commit is contained in:
2026-03-17 22:28:17 +01:00
parent 4979cf87f7
commit 2936d7146f
6 changed files with 363 additions and 2 deletions

View File

@@ -1,35 +1,100 @@
using System.Text;
namespace RolemasterDb.ImportTool;
public sealed class ImportArtifactPaths
{
private ImportArtifactPaths(
string artifactsRootPath,
string tableSlug,
string directoryPath,
string xmlPath,
string fragmentsJsonPath,
string parsedCellsJsonPath,
string validationReportPath)
string validationReportPath,
string pagesDirectoryPath,
string cellsDirectoryPath)
{
ArtifactsRootPath = artifactsRootPath;
TableSlug = tableSlug;
DirectoryPath = directoryPath;
XmlPath = xmlPath;
FragmentsJsonPath = fragmentsJsonPath;
ParsedCellsJsonPath = parsedCellsJsonPath;
ValidationReportPath = validationReportPath;
PagesDirectoryPath = pagesDirectoryPath;
CellsDirectoryPath = cellsDirectoryPath;
}
public string ArtifactsRootPath { get; }
public string TableSlug { get; }
public string DirectoryPath { get; }
public string XmlPath { get; }
public string FragmentsJsonPath { get; }
public string ParsedCellsJsonPath { get; }
public string ValidationReportPath { get; }
public string PagesDirectoryPath { get; }
public string CellsDirectoryPath { get; }
public static ImportArtifactPaths Create(string artifactsRootPath, string tableSlug)
{
var directoryPath = Path.Combine(artifactsRootPath, tableSlug);
var pagesDirectoryPath = Path.Combine(directoryPath, "pages");
var cellsDirectoryPath = Path.Combine(directoryPath, "cells");
return new ImportArtifactPaths(
artifactsRootPath,
tableSlug,
directoryPath,
Path.Combine(directoryPath, "source.xml"),
Path.Combine(directoryPath, "fragments.json"),
Path.Combine(directoryPath, "parsed-cells.json"),
Path.Combine(directoryPath, "validation-report.json"));
Path.Combine(directoryPath, "validation-report.json"),
pagesDirectoryPath,
cellsDirectoryPath);
}
public string GetPageImagePath(int pageNumber) =>
Path.Combine(PagesDirectoryPath, $"page-{pageNumber:000}.png");
public string GetRelativeCellImagePath(string? groupKey, string columnKey, string rollBandLabel) =>
Path.Combine(
TableSlug,
"cells",
$"{NormalizeFileSegment(groupKey ?? "none")}__{NormalizeFileSegment(columnKey)}__{NormalizeFileSegment(rollBandLabel)}.png")
.Replace('\\', '/');
public string ResolveRelativePath(string relativePath) =>
Path.GetFullPath(Path.Combine(ArtifactsRootPath, relativePath.Replace('/', Path.DirectorySeparatorChar)));
private static string NormalizeFileSegment(string value)
{
var builder = new StringBuilder();
foreach (var character in value.Trim().ToLowerInvariant())
{
if (char.IsLetterOrDigit(character))
{
builder.Append(character);
continue;
}
if (character is '-' or '_')
{
builder.Append(character);
continue;
}
if (character == '+')
{
builder.Append("plus");
continue;
}
builder.Append('_');
}
var normalized = builder.ToString().Trim('_');
return string.IsNullOrWhiteSpace(normalized) ? "empty" : normalized;
}
}