diff --git a/src/RvtIO/Parsing/TfBlockDeserializer.GElementGeometryGraph.cs b/src/RvtIO/Parsing/TfBlockDeserializer.GElementGeometryGraph.cs index 58ca754..653cba1 100644 --- a/src/RvtIO/Parsing/TfBlockDeserializer.GElementGeometryGraph.cs +++ b/src/RvtIO/Parsing/TfBlockDeserializer.GElementGeometryGraph.cs @@ -14,6 +14,8 @@ internal sealed record GElementFaceSurfaceLink(int FaceQueueIndex, int SurfaceQu internal sealed record OwnGElementGeometryGraph(int OwnerId, uint OpaqueHeader, IReadOnlyList Nodes, IReadOnlyList FaceSurfaces, int End); + private enum GraphReadStep { Root, Queue, Tail, FaceSurface, Field } + /// Complete, footer-exact FIFO for the physical node kinds of the r22 layout. Owned Face -> Surface links /// use queue indices, never handles, order or shape matching. Non-owning references and flags remain raw: this does /// not reconstruct runtime pointers, document lookup, active faces or extrusion validity. @@ -34,20 +36,23 @@ internal sealed record OwnGElementGeometryGraph(int OwnerId, uint OpaqueHeader, int position = start + 14, end = start + 12 + (int)size, itemCount = 0; var queue = new List<(ushort ClassId, uint Handle)>(); var nodes = new List(); - string context = "root"; + var step = GraphReadStep.Root; + int contextIndex = 0, contextValue = 0; + string? fieldName = null; try { nodes.Add(Node(-1, 2007, (uint)owner)); for (int index = 0; index < queue.Count; index++) { var entry = queue[index]; - context = $"fifo:{index}:{entry.ClassId}"; + step = GraphReadStep.Queue; + (contextIndex, contextValue) = (index, entry.ClassId); if (entry.ClassId is not (2007 or 2015 or 2009 or 2092 or 1996 or 1589 or 1243 or 1254 or 2014 or 522 or 1850 or 1848 or 1736 or 1999 or 1257 or 1974 or 983 or 1991 or 2238)) throw Fail; nodes.Add(Node(index, entry.ClassId, entry.Handle)); } - context = "tail"; + step = GraphReadStep.Tail; if (position != end) throw Fail; var surfaces = new List(); // A missing, external or hand pointer stays as stored in its field. @@ -57,14 +62,26 @@ internal sealed record OwnGElementGeometryGraph(int OwnerId, uint OpaqueHeader, var pointer = node.Fields.Single(f => f.Declaring == 1589 && f.Name == "m_pSurf") .Items.Single().Pointer!; if (pointer.QueueIndex is not int surface) continue; - context = $"face-surface:{node.QueueIndex}:{surface}"; + step = GraphReadStep.FaceSurface; + (contextIndex, contextValue) = (node.QueueIndex, surface); if (nodes[surface + 1].ClassId is not (522 or 983)) throw Fail; surfaces.Add(new(node.QueueIndex, surface)); } failure = null; return new(owner, BitConverter.ToUInt32(data, start + 4), nodes, surfaces, position - start); } - catch (DecodeFail) { failure = context; return null; } + catch (DecodeFail) + { + failure = step switch + { + GraphReadStep.Queue => $"fifo:{contextIndex}:{contextValue}", + GraphReadStep.Tail => "tail", + GraphReadStep.FaceSurface => $"face-surface:{contextIndex}:{contextValue}", + GraphReadStep.Field => $"field:{contextIndex}:{fieldName}:{contextValue}", + _ => "root", + }; + return null; + } GElementGraphNode Node(int index, ushort cls, uint handle) { @@ -75,16 +92,17 @@ GElementGraphNode Node(int index, ushort cls, uint handle) IReadOnlyList ReadFields(ushort cls, int depth) { - if (depth >= 32 || !GElementGraphSchemas.ContainsKey(cls)) throw Fail; - var result = new List(); - foreach (var field in GElementGraphFields(cls)) result.Add(Field(field, depth)); + if (depth >= 32 || !GElementGraphFieldsByClass.TryGetValue(cls, out var fields)) throw Fail; + var result = new List(fields.Length); + foreach (var field in fields) result.Add(Field(field, depth)); return result; } GElementGraphField Field(TfField field, int depth) { - context = $"field:{field.Declaring}:{field.Name}:{position - start}"; int first = position - start; + step = GraphReadStep.Field; + (contextIndex, contextValue, fieldName) = (field.Declaring, first, field.Name); int high = field.Mods >> 4; uint? count = high == 5 ? (uint)Raw(4, false) : high == 1 ? (uint)field.Cnt!.Value : null; uint length = count ?? 1; diff --git a/src/RvtIO/Parsing/TfBlockDeserializer.GElementGraphSchemas.cs b/src/RvtIO/Parsing/TfBlockDeserializer.GElementGraphSchemas.cs index e7bf4af..11278bf 100644 --- a/src/RvtIO/Parsing/TfBlockDeserializer.GElementGraphSchemas.cs +++ b/src/RvtIO/Parsing/TfBlockDeserializer.GElementGraphSchemas.cs @@ -93,6 +93,9 @@ internal static partial class TfBlockDeserializer [new("m_cda", 14, 0, 413, null)]), }; + private static readonly Dictionary GElementGraphFieldsByClass = + GElementGraphSchemas.Keys.ToDictionary(id => id, id => GElementGraphFields(id).ToArray()); + private static bool HasGElementGraphSchemas() => GElementGraphSchemas.All(pair => Schemas.TryGetValue(pair.Key, out var actual) && actual.Name == pair.Value.Name && actual.Base == pair.Value.Base && actual.Fields.SequenceEqual(pair.Value.Fields)); diff --git a/tests/RvtIO.Tests/GElementGeometryGraphTests.cs b/tests/RvtIO.Tests/GElementGeometryGraphTests.cs index 89a1b34..a12677e 100644 --- a/tests/RvtIO.Tests/GElementGeometryGraphTests.cs +++ b/tests/RvtIO.Tests/GElementGeometryGraphTests.cs @@ -117,13 +117,16 @@ public void EveryByteTruncationIncludingLaterPayloadRejectsAtomicallyWithReframe } [Theory] - [InlineData("faces")] [InlineData("edges")] [InlineData("interior")] - public void CountLimitAndRemainingBytesAreBothEnforced(string key) + [InlineData("faces", 1978, "m_pFaces")] + [InlineData("edges", 2092, "m_pEdges")] + [InlineData("interior", 1244, "m_interiorEdgePnts")] + public void CountLimitAndRemainingBytesAreBothEnforced(string key, int declaring, string field) { foreach(uint count in new[] { 100001u,100000u }) { var (bytes,offsets) = Block(); BitConverter.GetBytes(count).CopyTo(bytes,offsets[key]); - Assert.Null(Read(bytes,out var failure)); Assert.NotNull(failure); + Assert.Null(Read(bytes, out var failure)); + Assert.Equal($"field:{declaring}:{field}:{offsets[key]}", failure); } }