diff --git a/CHANGELOG.md b/CHANGELOG.md index ee4d870..96c9423 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ All notable changes to the `NwcIO` library, the CLI and the `NwcIO.Scene` adapte - `NwcIO.Internal.*` types are now `internal`; only the documented public surface ships in the package. `NwcJsonExportOptions` remains public. Package validation runs on `dotnet pack` against the 0.12.1 baseline, with `CompatibilitySuppressions.xml` recording the intentional type removals. ### Changed +- The CLI folder and `.nwf` assembly paths now share one `ProcessAssembly(NwcAssembly, …)` pipeline. Default output paths and the `NwcIO Assembly Report` / `NwcIO NWF Assembly Report` labels are unchanged; the folder path only loads the assembly and the `.nwf` path only loads the native file before delegating. - **Breaking:** for files read without the DbCache directory, `MaterialParser` no longer adds the Concrete and Rebar presets, no longer picks a mesh's material by its name or size (bounding box above 2000) or falls back to the first material, and no longer guesses a material colour from its name. A material named in property records keeps its colour from a `revit_Color` record, otherwise a neutral grey. Every mesh without a material shares one `Default Material` (a fresh instance per document, listed in `NwcDocument.Materials`, so JSON `meshes[].material` always resolves). New `NwcMaterial.IsInferred` is `true` for a fallback material or colour; it is now settable. The strict (DbCache) path is unchanged. - `NwcReaderOptions.MaxMeshVertices` (default 200,000) sets the vertex cap of one geometry record on the lenient reader path. Class 95 records and two-vertex records now read as line meshes (`LineIndices`) instead of triangle strips or being dropped. - The `NwcIO` and `NwcIO.Cli` package tags no longer name the frozen viewer and coordination extras (`clash-detection`, `clash-matrix`, `clash-grouping`, `model-diff`, `diff`, `grid-system`, `grids`, `measure`, `measurement`, `bcf`, `bcfzip`, `html-viewer`, `webgl`, `spatial-index`, `bvh`), and the `NwcIO.Scene` package has its own tags (`navisworks`, `nwc`, `ioscene`, `scene`, `bim`). diff --git a/src/NwcIO.Cli/Program.cs b/src/NwcIO.Cli/Program.cs index 9ece2f7..85ec2ce 100644 --- a/src/NwcIO.Cli/Program.cs +++ b/src/NwcIO.Cli/Program.cs @@ -1267,8 +1267,6 @@ private static int ProcessAssemblyDirectory( { var @out = outWriter ?? Console.Out; var @err = errorWriter ?? Console.Error; - TextWriter infoOut = (outputPath == "-") ? @err : @out; - if (outputPath != "-") { outputPath ??= Path.Combine(dirPath, Path.GetFileName(dirPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)) + "_assembly.glb"); @@ -1277,254 +1275,20 @@ private static int ProcessAssemblyDirectory( var patterns = string.IsNullOrWhiteSpace(folderPattern) ? ["*.nwc"] : SplitPatterns(folderPattern); var assembly = NwcAssembly.LoadFromDirectory( dirPath, patterns, recursiveFolders, readerOptions ?? new NwcReaderOptions { BuildSceneTree = includeTree }); - - if (!string.IsNullOrEmpty(explicitTimeLinerXmlPath)) - { - if (File.Exists(explicitTimeLinerXmlPath)) - { - assembly.ImportTimeLinerXml(explicitTimeLinerXmlPath); - if (printReport) @err.WriteLine($"TimeLiner schedule loaded from XML: '{explicitTimeLinerXmlPath}'."); - } - else - { - EnsureParentDirectory(explicitTimeLinerXmlPath); - assembly.ExportTimeLinerXml(explicitTimeLinerXmlPath); - if (printReport) @err.WriteLine($"TimeLiner schedule exported to XML: '{explicitTimeLinerXmlPath}'."); - } - } - - if (!string.IsNullOrEmpty(explicitTimeLinerMsProjectPath)) - { - if (File.Exists(explicitTimeLinerMsProjectPath)) - { - assembly.ImportTimeLinerMsProject(explicitTimeLinerMsProjectPath); - if (printReport) @err.WriteLine($"TimeLiner schedule loaded from MS Project XML: '{explicitTimeLinerMsProjectPath}'."); - } - else - { - EnsureParentDirectory(explicitTimeLinerMsProjectPath); - assembly.ExportTimeLinerMsProject(explicitTimeLinerMsProjectPath); - if (printReport) @err.WriteLine($"TimeLiner schedule exported to MS Project XML: '{explicitTimeLinerMsProjectPath}'."); - } - } - - if (!string.IsNullOrEmpty(explicitTimeLinerPrimaveraPath)) - { - if (File.Exists(explicitTimeLinerPrimaveraPath)) - { - assembly.ImportTimeLinerPrimavera(explicitTimeLinerPrimaveraPath); - if (printReport) @err.WriteLine($"TimeLiner schedule loaded from Primavera P6 XER: '{explicitTimeLinerPrimaveraPath}'."); - } - else - { - EnsureParentDirectory(explicitTimeLinerPrimaveraPath); - assembly.ExportTimeLinerPrimavera(explicitTimeLinerPrimaveraPath); - if (printReport) @err.WriteLine($"TimeLiner schedule exported to Primavera P6 XER: '{explicitTimeLinerPrimaveraPath}'."); - } - } - - if (linkTimeLiner && assembly.TimeLiner is not null) - { - int linked = Model.TimeLiner.NwcTimeLinerLinker.AutoLink(assembly.TimeLiner, assembly.SelectionSets); - infoOut.WriteLine($"Linked {linked} TimeLiner tasks to assembly elements."); - } - - if (!string.IsNullOrEmpty(explicitProfilerPath)) - { - if (!File.Exists(explicitProfilerPath)) - { - @err.WriteLine($"Error: Appearance profiler file not found: {explicitProfilerPath}"); - return 2; - } - var profiler = Model.Appearance.NwcXmlAppearanceProfilerSerializer.Open(explicitProfilerPath); - int overridden = assembly.ApplyAppearanceProfiler(profiler); - if (printReport) - { - @err.WriteLine($"Appearance profiler applied to assembly: {overridden} elements overridden."); - } - } - - IReadOnlyList? clashes = null; - bool needClashes = checkClashes || - !string.IsNullOrEmpty(explicitBcfPath) || - !string.IsNullOrEmpty(explicitClashHtmlPath) || - outputPath.EndsWith(".bcf", StringComparison.OrdinalIgnoreCase) || - outputPath.EndsWith(".bcfzip", StringComparison.OrdinalIgnoreCase) || - outputPath.EndsWith(".html", StringComparison.OrdinalIgnoreCase) || - outputPath.EndsWith(".htm", StringComparison.OrdinalIgnoreCase); - - if (needClashes) - { - clashes = assembly.FindClashes(clashTolerance); - } - - if (outputPath.EndsWith(".bcf", StringComparison.OrdinalIgnoreCase) || outputPath.EndsWith(".bcfzip", StringComparison.OrdinalIgnoreCase)) - { - EnsureParentDirectory(outputPath); - assembly.SaveBcf(outputPath, clashes); - } - else if (outputPath.EndsWith(".html", StringComparison.OrdinalIgnoreCase) || outputPath.EndsWith(".htm", StringComparison.OrdinalIgnoreCase)) - { - EnsureParentDirectory(outputPath); - assembly.SaveClashReportHtml(outputPath, clashes); - } - else if (outputPath.EndsWith(".ifc", StringComparison.OrdinalIgnoreCase)) - { - EnsureParentDirectory(outputPath); - assembly.SaveIfc(outputPath, ifcOptions); - } - else if (outputPath.EndsWith(".glb", StringComparison.OrdinalIgnoreCase)) - { - EnsureParentDirectory(outputPath); - assembly.SaveGlb(outputPath, worldCoords); - } - else if (outputPath.EndsWith(".obj", StringComparison.OrdinalIgnoreCase)) - { - EnsureParentDirectory(outputPath); - assembly.SaveObj(outputPath, worldCoords); - } - else if (outputPath.EndsWith(".nwc", StringComparison.OrdinalIgnoreCase)) - { - EnsureParentDirectory(outputPath); - assembly.SaveNwc(outputPath); - } - else if (outputPath == "-") - { - if (stdoutStream != null) - { - assembly.SaveJson(stdoutStream, new NwcJsonExportOptions { Indented = indent }); - stdoutStream.Flush(); - } - else - { - using var stdout = Console.OpenStandardOutput(); - assembly.SaveJson(stdout, new NwcJsonExportOptions { Indented = indent }); - stdout.Flush(); - } - } - else - { - EnsureParentDirectory(outputPath); - assembly.SaveJson(outputPath, new NwcJsonExportOptions { Indented = indent }); - } - - if (!string.IsNullOrEmpty(explicitIfcPath)) { EnsureParentDirectory(explicitIfcPath); assembly.SaveIfc(explicitIfcPath, ifcOptions); } - if (!string.IsNullOrEmpty(explicitNwcPath)) - { - EnsureParentDirectory(explicitNwcPath); - assembly.SaveNwc(explicitNwcPath); - } - if (!string.IsNullOrEmpty(explicitGlbPath)) { EnsureParentDirectory(explicitGlbPath); assembly.SaveGlb(explicitGlbPath, worldCoords); } - if (!string.IsNullOrEmpty(explicitObjPath)) { EnsureParentDirectory(explicitObjPath); assembly.SaveObj(explicitObjPath, worldCoords); } - if (!string.IsNullOrEmpty(explicitBcfPath)) { EnsureParentDirectory(explicitBcfPath); assembly.SaveBcf(explicitBcfPath, clashes); } - if (!string.IsNullOrEmpty(explicitViewpointsBcfPath)) { EnsureParentDirectory(explicitViewpointsBcfPath); assembly.SaveViewpointsBcf(explicitViewpointsBcfPath); } - if (!string.IsNullOrEmpty(explicitViewpointsXmlPath)) { EnsureParentDirectory(explicitViewpointsXmlPath); assembly.SaveViewpointsXml(explicitViewpointsXmlPath); } - if (!string.IsNullOrEmpty(explicitClashHtmlPath)) { EnsureParentDirectory(explicitClashHtmlPath); assembly.SaveClashReportHtml(explicitClashHtmlPath, clashes); } - if (!string.IsNullOrEmpty(explicitClashXmlPath)) - { - EnsureParentDirectory(explicitClashXmlPath); - var test = assembly.RunClashDetection("Federated Clash Test", clashTolerance); - ApplyClashGrouping(test, clashGroupMode, clashRadius, printReport, @err); - assembly.ExportClashReportXml(explicitClashXmlPath); - } - if (!string.IsNullOrEmpty(explicitClashBcfPath)) - { - EnsureParentDirectory(explicitClashBcfPath); - if (assembly.ClashTests.Count == 0) - { - var test = assembly.RunClashDetection("Federated Clash Test", clashTolerance); - ApplyClashGrouping(test, clashGroupMode, clashRadius, printReport, @err); - } - assembly.ExportClashBcf(explicitClashBcfPath); - } - if (!string.IsNullOrEmpty(explicitHtmlViewerPath)) - { - EnsureParentDirectory(explicitHtmlViewerPath); - WarnIfViewerTruncated(NwcHtmlViewerExporter.SaveWithResult(explicitHtmlViewerPath, assembly), @err); - } - if (!string.IsNullOrEmpty(explicitNwfXmlPath)) - { - EnsureParentDirectory(explicitNwfXmlPath); - assembly.SaveNwf(explicitNwfXmlPath); - } - - bool needMatrix = !string.IsNullOrEmpty(explicitClashMatrixJsonPath) || - !string.IsNullOrEmpty(explicitClashMatrixMdPath) || - !string.IsNullOrEmpty(explicitClashMatrixBcfPath); - - if (needMatrix) - { - assembly.ClashMatrix ??= Model.Clash.NwcClashMatrix.CreateFromAssembly(assembly); - assembly.ClashMatrix.Execute(assembly); - - if (!string.IsNullOrEmpty(explicitClashMatrixJsonPath)) - { - EnsureParentDirectory(explicitClashMatrixJsonPath); - assembly.ExportClashMatrixJson(explicitClashMatrixJsonPath, indent); - if (printReport) @err.WriteLine($"Clash matrix exported to JSON: '{explicitClashMatrixJsonPath}'."); - } - if (!string.IsNullOrEmpty(explicitClashMatrixMdPath)) - { - EnsureParentDirectory(explicitClashMatrixMdPath); - assembly.ExportClashMatrixMarkdown(explicitClashMatrixMdPath); - if (printReport) @err.WriteLine($"Clash matrix summary exported to Markdown: '{explicitClashMatrixMdPath}'."); - } - if (!string.IsNullOrEmpty(explicitClashMatrixBcfPath)) - { - EnsureParentDirectory(explicitClashMatrixBcfPath); - assembly.ExportClashMatrixBcf(explicitClashMatrixBcfPath); - if (printReport) @err.WriteLine($"Clash matrix conflicts exported to BCF 2.1: '{explicitClashMatrixBcfPath}'."); - } - - infoOut.WriteLine(assembly.ClashMatrix.ToMarkdownTable()); - } - - if (!string.IsNullOrEmpty(raycastArg)) - { - ExecuteRaycastTest(assembly.SpatialIndex, raycastArg, infoOut, @err, assembly); - } - - if (listViewpoints) - { - PrintViewpoints(assembly.SavedViewpoints, infoOut); - } - - sw.Stop(); - - if (printReport) - { - @err.WriteLine("=== NwcIO Assembly Report ==="); - PrintDiagnostics(assembly.Diagnostics, @err); - @err.WriteLine($"Assembly: {assembly.Name}"); - @err.WriteLine($"Models: {assembly.Models.Count}"); - @err.WriteLine($"Total Meshes: {assembly.AllMeshes.Count}"); - @err.WriteLine($"Total Materials: {assembly.AllMaterials.Count}"); - @err.WriteLine($"Saved viewpoints: {assembly.SavedViewpoints.Count}"); - @err.WriteLine($"Bounding Box: {assembly.BoundingBox}"); - if (bvhStats) - { - var idx = assembly.SpatialIndex; - @err.WriteLine($"BVH Nodes: {idx.NodeCount} ({idx.LeafCount} leaves, max depth {idx.MaxDepth})"); - } - if (clashes is not null) - { - @err.WriteLine($"Clashes Detected: {clashes.Count}"); - } - @err.WriteLine($"Elapsed: {sw.ElapsedMilliseconds} ms"); - @err.WriteLine($"Output: {outputPath}"); - } - else - { - infoOut.WriteLine(FormattableString.Invariant( - $"Assembly '{assembly.Name}': {assembly.Models.Count} models, {assembly.AllMeshes.Count} meshes, {assembly.AllMaterials.Count} materials exported to '{outputPath}' in {sw.ElapsedMilliseconds} ms.")); - } - - return 0; + return ProcessAssembly( + assembly, dirPath, outputPath!, explicitGlbPath, explicitObjPath, explicitIfcPath, explicitNwcPath, + explicitBcfPath, explicitViewpointsBcfPath, explicitViewpointsXmlPath, explicitClashHtmlPath, explicitProfilerPath, + worldCoords, checkClashes, listViewpoints, bvhStats, raycastArg, printReport, indent, ifcOptions, sw, + explicitClashXmlPath, explicitClashBcfPath, explicitNwfXmlPath, clashTolerance, explicitHtmlViewerPath, clashGroupMode, clashRadius, + explicitTimeLinerXmlPath, linkTimeLiner, explicitClashMatrixJsonPath, explicitClashMatrixMdPath, explicitClashMatrixBcfPath, + explicitTimeLinerMsProjectPath, explicitTimeLinerPrimaveraPath, + stdoutStream, @out, @err, isNwf: false); } - private static int ProcessNwfAssembly( - string nwfPath, - string? outputPath, + private static int ProcessAssembly( + NwcAssembly assembly, + string sourceLabel, + string outputPath, string? explicitGlbPath, string? explicitObjPath, string? explicitIfcPath, @@ -1557,23 +1321,15 @@ private static int ProcessNwfAssembly( string? explicitClashMatrixBcfPath = null, string? explicitTimeLinerMsProjectPath = null, string? explicitTimeLinerPrimaveraPath = null, - NwcReaderOptions? readerOptions = null, - bool includeTree = true, Stream? stdoutStream = null, TextWriter? outWriter = null, TextWriter? errorWriter = null, - NwcAssembly? preloadedAssembly = null) + bool isNwf = true) { var @out = outWriter ?? Console.Out; var @err = errorWriter ?? Console.Error; TextWriter infoOut = (outputPath == "-") ? @err : @out; - if (outputPath != "-") - { - outputPath ??= Path.ChangeExtension(nwfPath, ".glb"); - } - var assembly = preloadedAssembly ?? NwcAssembly.OpenNwf(nwfPath, readerOptions ?? new NwcReaderOptions { BuildSceneTree = includeTree }); - if (!string.IsNullOrEmpty(explicitTimeLinerXmlPath)) { if (File.Exists(explicitTimeLinerXmlPath)) @@ -1789,20 +1545,19 @@ private static int ProcessNwfAssembly( if (printReport) { - if (preloadedAssembly is null) + @err.WriteLine(isNwf ? "=== NwcIO NWF Assembly Report ===" : "=== NwcIO Assembly Report ==="); + PrintDiagnostics(assembly.Diagnostics, @err); + if (isNwf) { - @err.WriteLine("=== NwcIO NWF Assembly Report ==="); - @err.WriteLine($"NWF File: {nwfPath}"); + @err.WriteLine($"NWF File: {sourceLabel}"); // Models[0] is the .nwf document itself; report the component files separately. @err.WriteLine($"Loaded Models: {assembly.Models.Count} (component files: {assembly.Models.Count - 1})"); } else { - @err.WriteLine("=== NwcIO Assembly Report ==="); @err.WriteLine($"Assembly: {assembly.Name}"); @err.WriteLine($"Models: {assembly.Models.Count}"); } - PrintDiagnostics(assembly.Diagnostics, @err); @err.WriteLine($"Total Meshes: {assembly.AllMeshes.Count}"); @err.WriteLine($"Total Materials: {assembly.AllMaterials.Count}"); @err.WriteLine($"Saved viewpoints: {assembly.SavedViewpoints.Count}"); @@ -1830,12 +1585,72 @@ private static int ProcessNwfAssembly( else { infoOut.WriteLine(FormattableString.Invariant( - $"NWF Assembly '{assembly.Name}': {assembly.Models.Count} models, {assembly.AllMeshes.Count} meshes, {assembly.AllMaterials.Count} materials exported to '{outputPath}' in {sw.ElapsedMilliseconds} ms.")); + $"{(isNwf ? "NWF Assembly" : "Assembly")} '{assembly.Name}': {assembly.Models.Count} models, {assembly.AllMeshes.Count} meshes, {assembly.AllMaterials.Count} materials exported to '{outputPath}' in {sw.ElapsedMilliseconds} ms.")); } return 0; } + private static int ProcessNwfAssembly( + string nwfPath, + string? outputPath, + string? explicitGlbPath, + string? explicitObjPath, + string? explicitIfcPath, + string? explicitNwcPath, + string? explicitBcfPath, + string? explicitViewpointsBcfPath, + string? explicitViewpointsXmlPath, + string? explicitClashHtmlPath, + string? explicitProfilerPath, + bool worldCoords, + bool checkClashes, + bool listViewpoints, + bool bvhStats, + string? raycastArg, + bool printReport, + bool indent, + NwcIfcExportOptions ifcOptions, + Stopwatch sw, + string? explicitClashXmlPath = null, + string? explicitClashBcfPath = null, + string? explicitNwfXmlPath = null, + double clashTolerance = 0.001, + string? explicitHtmlViewerPath = null, + string? clashGroupMode = null, + double clashRadius = 1.0, + string? explicitTimeLinerXmlPath = null, + bool linkTimeLiner = false, + string? explicitClashMatrixJsonPath = null, + string? explicitClashMatrixMdPath = null, + string? explicitClashMatrixBcfPath = null, + string? explicitTimeLinerMsProjectPath = null, + string? explicitTimeLinerPrimaveraPath = null, + NwcReaderOptions? readerOptions = null, + bool includeTree = true, + Stream? stdoutStream = null, + TextWriter? outWriter = null, + TextWriter? errorWriter = null, + NwcAssembly? preloadedAssembly = null) + { + var @out = outWriter ?? Console.Out; + var @err = errorWriter ?? Console.Error; + if (outputPath != "-") + { + outputPath ??= Path.ChangeExtension(nwfPath, ".glb"); + } + + var assembly = preloadedAssembly ?? NwcAssembly.OpenNwf(nwfPath, readerOptions ?? new NwcReaderOptions { BuildSceneTree = includeTree }); + return ProcessAssembly( + assembly, nwfPath, outputPath!, explicitGlbPath, explicitObjPath, explicitIfcPath, explicitNwcPath, + explicitBcfPath, explicitViewpointsBcfPath, explicitViewpointsXmlPath, explicitClashHtmlPath, explicitProfilerPath, + worldCoords, checkClashes, listViewpoints, bvhStats, raycastArg, printReport, indent, ifcOptions, sw, + explicitClashXmlPath, explicitClashBcfPath, explicitNwfXmlPath, clashTolerance, explicitHtmlViewerPath, clashGroupMode, clashRadius, + explicitTimeLinerXmlPath, linkTimeLiner, explicitClashMatrixJsonPath, explicitClashMatrixMdPath, explicitClashMatrixBcfPath, + explicitTimeLinerMsProjectPath, explicitTimeLinerPrimaveraPath, + stdoutStream, @out, @err, isNwf: preloadedAssembly is null); + } + private static string[] SplitPatterns(string patterns) => patterns.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); diff --git a/tests/NwcIO.Tests/AssemblyPipelineTests.cs b/tests/NwcIO.Tests/AssemblyPipelineTests.cs new file mode 100644 index 0000000..03191ea --- /dev/null +++ b/tests/NwcIO.Tests/AssemblyPipelineTests.cs @@ -0,0 +1,95 @@ +using NwcIO.Internal.Container; +using NwcIO.Model; +using Xunit; + +namespace NwcIO.Tests; + +public sealed class AssemblyPipelineTests +{ + [Fact] + public void Folder_ReportsFolderLabelsAndWritesDefaultOutput() + { + using var dir = new TempDir(); + NwcDocument.Create().Save(Path.Combine(dir.Path, "a.nwc")); + + var report = Run([dir.Path, Path.Combine(dir.Path, "folder.json"), "--report", "--bvh-stats"]); + Assert.Equal(0, report.ExitCode); + Assert.Contains("=== NwcIO Assembly Report ===", report.StdErr); + Assert.DoesNotContain("NWF", report.StdErr); + Assert.Contains("Models: 1", report.StdErr); + Assert.Contains("Total Materials:", report.StdErr); + Assert.Contains("BVH Nodes:", report.StdErr); + + var summary = Run([dir.Path]); + Assert.Equal(0, summary.ExitCode); + Assert.StartsWith("Assembly '", summary.StdOut); + string expected = Path.Combine(dir.Path, Path.GetFileName(dir.Path) + "_assembly.glb"); + Assert.Contains($"exported to '{expected}'", summary.StdOut); + Assert.True(File.Exists(expected)); + } + + [Fact] + public void Nwf_ReportsNwfLabelsResolvesComponentsAndWritesDefaultOutput() + { + using var dir = new TempDir(); + string model = Path.Combine(dir.Path, "a.nwc"); + NwcDocument.Create().Save(model); + string nwf = Path.Combine(dir.Path, "fed.nwf"); + WriteNwfWithXRefs(nwf, model); + + var report = Run([nwf, Path.Combine(dir.Path, "nwf.json"), "--report", "--bvh-stats"]); + Assert.Equal(0, report.ExitCode); + Assert.Contains("=== NwcIO NWF Assembly Report ===", report.StdErr); + Assert.Contains("Loaded Models: 2 (component files: 1)", report.StdErr); + Assert.DoesNotContain("Warning: unresolved reference", report.StdErr); + Assert.Contains("BVH Nodes:", report.StdErr); + + var summary = Run([nwf]); + Assert.Equal(0, summary.ExitCode); + Assert.StartsWith("NWF Assembly '", summary.StdOut); + string expected = Path.ChangeExtension(nwf, ".glb"); + Assert.Contains($"exported to '{expected}'", summary.StdOut); + Assert.True(File.Exists(expected)); + } + + [Fact] + public void Nwf_WithMissingComponent_WarnsInReport() + { + using var dir = new TempDir(); + string missing = Path.Combine(dir.Path, "gone.nwc"); + string nwf = Path.Combine(dir.Path, "fed.nwf"); + WriteNwfWithXRefs(nwf, missing); + + var report = Run([nwf, Path.Combine(dir.Path, "nwf.json"), "--report"]); + Assert.Equal(0, report.ExitCode); + Assert.Contains("Loaded Models: 1 (component files: 0)", report.StdErr); + Assert.Contains("Warning: unresolved reference, component model missing: '" + missing.Replace('\\', '/') + "'", report.StdErr); + } + + private sealed class TempDir : IDisposable + { + public string Path { get; } = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "nwcio-pipeline-" + Guid.NewGuid().ToString("N")); + + public TempDir() => Directory.CreateDirectory(Path); + + public void Dispose() => Directory.Delete(Path, recursive: true); + } + + private static (int ExitCode, string StdOut, string StdErr) Run(params string[] args) + { + using var stdout = new StringWriter(); + using var stderr = new StringWriter(); + int exit = NwcIO.Cli.Program.Run(args, stdoutStream: null, outWriter: stdout, errorWriter: stderr); + return (exit, stdout.ToString(), stderr.ToString()); + } + + private static void WriteNwfWithXRefs(string nwfPath, params string[] effectivePaths) + { + string table = string.Join(",", effectivePaths.Select(p => + "{\"OriginalPath\":\"" + p.Replace('\\', '/') + "\",\"RemappedPath\":\"\"}")); + string json = "{\"Type\":\"XRefTable\",\"Version\":1,\"SavedFilename\":\"fed.nwf\",\"Table\":[" + table + "]}"; + byte[] xrefStream = System.Text.Encoding.UTF8.GetBytes(json); + using var fs = File.Create(nwfPath); + NwcContainerWriter.Write(fs, [xrefStream]); + } +}