using System.Text.Json; using Autodesk.AutoCAD.ApplicationServices; using PRecAnnot.Abstractions; namespace PRecAnnot.Plant3D2026; internal sealed record LineNumberApplyPermit { public required bool AllowApply { get; init; } public required string FixtureMarkerPath { get; init; } public required string ExpectedStableId { get; init; } public required string ExpectedText { get; init; } } internal static class LineNumberApplyPermitReader { private const string MarkerContent = "P_REC_ANNOT_TEST_FIXTURE=LINE_NUMBER_APPLY"; public static void Validate(Document document, LineNumberApplyPermit permit, LineNumberApplyPreflight operation) { if (!permit.AllowApply || !string.Equals(permit.ExpectedStableId, operation.StableId, StringComparison.Ordinal) || !string.Equals(permit.ExpectedText, operation.Text, StringComparison.Ordinal)) throw new InvalidOperationException("LINE_NUMBER_APPLY_PERMIT_PLAN_MISMATCH"); if (!Path.IsPathFullyQualified(permit.FixtureMarkerPath) || permit.FixtureMarkerPath.StartsWith("\\\\", StringComparison.Ordinal) || !File.Exists(permit.FixtureMarkerPath) || !string.Equals(File.ReadAllText(permit.FixtureMarkerPath).Trim(), MarkerContent, StringComparison.Ordinal)) throw new InvalidOperationException("LINE_NUMBER_APPLY_TEST_FIXTURE_REQUIRED"); var fixtureRoot = Path.GetDirectoryName(Path.GetFullPath(permit.FixtureMarkerPath))! + Path.DirectorySeparatorChar; if (!Path.GetFullPath(document.Name).StartsWith(fixtureRoot, StringComparison.OrdinalIgnoreCase)) throw new InvalidOperationException("LINE_NUMBER_APPLY_TEST_FIXTURE_REQUIRED"); } public static LineNumberApplyPermit Read() { var path = Environment.GetEnvironmentVariable("P_REC_ANNOT_LINE_NUMBER_APPLY_PERMIT_PATH"); if (string.IsNullOrWhiteSpace(path) || !Path.IsPathFullyQualified(path) || path.StartsWith("\\\\", StringComparison.Ordinal) || !File.Exists(path)) throw new InvalidOperationException("LINE_NUMBER_APPLY_PERMIT_REQUIRED"); return JsonSerializer.Deserialize(File.ReadAllText(path), new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? throw new InvalidOperationException("LINE_NUMBER_APPLY_PERMIT_INVALID"); } }