acdream/src/AcDream.App/Rendering/EquippedChildRenderController.cs
Erik 392c1e22c1 fix(physics): bind a parented child to the parent's live incarnation (#319)
A player-parented child never received a canonical cell. Its FullCellId stayed
0 for its whole attached lifetime, so it could not follow the player across a
boundary. Scope was wider than the local player: every REMOTE player's
equipment too.

ROOT CAUSE. EquippedChildRenderController hardcoded ParentInstanceSequence: 0
for a parented CreateObject. Correct for creatures and statics, which really
are sequence 0; wrong for players, whose ObjectInstance is Character.TotalLogins
(ACE Player_Networking.cs:37). The relation filed under (playerGuid, 0) while
the record carried TotalLogins, so both route-7 write sites — D1's attach
re-cell and D2's propagation lookup — keyed on an incarnation that never
matched. TryCommitParent did not validate the sequence, so the attach
succeeded and printed normally. Silent.

A ROUTE 7 REGRESSION (cd3129e9) that un-masked a latent bug: the TickChild call
route 7 deleted was keyed on the child guid alone and was structurally immune
to a wrong parent key.

THE FIX IS TO STOP TREATING PLAYERS DIFFERENTLY, not to special-case them.
Retail's attach path is guid-only end to end — PhysicsDesc::get_parent_id
@0x00558a18 -> CObjectMaint::GetObjectA @0x00558a2d -> set_parent @0x00558a3e,
with SetChildren @0x00509370 hash-walking by guid — and neither set_parent
overload (@0x00515A90, @0x00515B50) nor enter_cell @0x00510ED0 contains any
player test or instance-sequence read. Our player/non-player split was purely
an artifact of keying relations by (guid, incarnation) against a wire message
that carries no parent incarnation. Late-binding to whoever currently holds the
guid is retail's own semantics. Fixed at BOTH producers: OnSpawn and
OnCreateParentAccepted, the second carrying the byte-identical defect and not
named in the contract's scope line.

THE INVARIANT IS EQUALITY, NOT FRESHNESS. The contract rejected both framings I
offered: every one of the 45 FullCellId liveness predicates excludes a
committed child on a NON-cell clause first, so the child inherits only the
parent record's existing staleness, which is already present today with no
symptom. The key fix alone restores child-equals-parent for every parent class.

TWO SITES GATED, inert only because the cell was zero and would have woken
wrongly: the hydration candidate loop (a nonzero-cell child would take the
legacy RebucketLiveEntity -> CommitRebucket, a second canonical writer — route
7's exact defect class) and RestoreShadow (would install a broadphase row for
the weapon, the #184 shape, contradicting route 7's P4). Retail anchor:
update_object's parent != 0 early-out @0x00515D40 — children are never
independently re-placed.

THREE MAJORS WERE FIXED BY DELETION. The first pass added a deferral queue for
an unaddressable parent, carrying a missing child-freshness gate (A2), a
sentinel-0 collision with the generation filters (A3), and unbounded
accumulation (A5). Both reviewers then proved the deferred branch unreachable
for BOTH producers — RegisterEntityCore defers the entire CreateObject one
layer above, reading the same ?? chain, and CreateParentUpdate is produced only
inside AcceptCreateCore, after that gate passes. The machinery was deleted
rather than repaired, and the diff SHRANK to 76 added / 13 removed from 91/24
while gaining the A1 fix. Retail confirmed the deletion does not diverge:
acdream's real port of retail's per-guid replay (QueueBlobForObject) is a
different, untouched layer, and the deleted queue was a third redundant one
downstream of it.

THE GUARD MUST NOT TEAR WHAT IT PROTECTS. The first pass threw
InvalidOperationException AFTER the canonical half had committed, so the one
time it fired it left the child parented with no committed relation and a
staged one blocking Resolve — a torn transaction, the exact outcome the
contract pinned against. Now a pure CanCommitIncarnation precondition checked
BEFORE the commit at both sites, with a logged refusal instead of a throw.
Route 3's N3 principle (do not make a transient fatal on a host that must
survive 30 sessions x 2 hours) reinforces it, but the tearing argument stands
alone.

TEST QUALITY, the recurring lesson in its most refined form. The A1 test
initially passed sabotage FOR THE WRONG REASON: a mismatched ChildPositionSequence
meant TryCommitParent's own gate refused in either ordering, so the three
assertions carrying A1's meaning passed both ways and only an incidental
staging assertion failed. It failed on stranding, not tearing. Corrected, the
sabotage now names line 925 — Assert.Null(snapshot.ParentGuid), with the
parent's guid in it — proving the canonical mutation happened before the catch.
"Fails under sabotage" is necessary, not sufficient; WHICH assertion fails is
the real question.

The dual parent-class matrix (player 0x5… incarnation > 1 vs creature 0x8…
incarnation 0, identical outcomes, sabotage-verified in both directions) is the
structural fix for how this survived a full dual review and two connected
sessions: every prior test and both captured gate logs used sequence-0 parents.

Register: AP-142 clause (f); AP-132 amended to distinguish the two producers;
new row AP-146 for the local player's coarse canonical cell (retail writes it
per tick at SetPositionInternal @0x00515330 — which, per the retail review, ALSO
walks this->children writing each child's objcell_id @0x005153AE-@0x005153D8,
so retail's per-tick child propagation lives in the same function). That
divergence had no row at all, a standing rule-1 violation now corrected.
Follow-up #320 filed for making the player's cell track ordinary movement —
deliberately excluded here: it touches the landblock-preserve contract, the
Rebucketed cadence, route-2/4b-3 classification inputs AP-136/AP-138 spent four
review rounds pinning, and the portal-space frozen-source-cell race.

Two dual review rounds; 6 architecture MAJORs and 2 retail MAJORs closed.
Diagnostic refusals are latched per child guid and the latch clears on
Clear()/RemoveChild, so a recycled guid's next incarnation still logs rather
than being silently suppressed.

Complete Release suite MEASURED at 11,112 passed / 4 skipped / 0 failed
(baseline 11,090 at 52175aa1, +22). Neither known flake fired.

STILL OWED: the connected gate, with the CORRECTED positive criterion — assert
the equipped child's FullCellId EQUALS the parent's after a crossing (a zero is
a failure, not a silence), run with BOTH a player and a creature parent, plus
the new step carrying an armed creature across a landblock unload/reload.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-05 11:56:31 +02:00

1864 lines
74 KiB
C#

using System.Numerics;
using AcDream.App.World;
using AcDream.App.Rendering.Vfx;
using AcDream.Core.Items;
using AcDream.Core.Meshing;
using AcDream.Core.Net;
using AcDream.Core.Net.Messages;
using AcDream.Core.Physics;
using AcDream.Core.World;
using DatReaderWriter;
using AcDream.Content;
using DatReaderWriter.DBObjs;
using DatReaderWriter.Enums;
using DatReaderWriter.Types;
using AcDream.Runtime.Entities;
namespace AcDream.App.Rendering;
/// <summary>
/// Render-side owner of retail parented physics objects (held weapons,
/// shields, and visible ammunition). Network/state parsing remains in Core;
/// this controller projects an accepted parent relation into a dynamic child
/// <see cref="WorldEntity"/> and recomposes it after the parent's animation
/// advances each frame.
/// </summary>
public sealed class EquippedChildRenderController : IDisposable
{
private readonly IDatReaderWriter _dats;
private readonly object _datLock;
private readonly ClientObjectTable _objects;
private readonly LiveEntityRuntime _liveEntities;
private readonly Func<ParentEvent.Parsed, bool> _acceptParent;
private readonly Func<LiveEntityRecord, ulong, ulong, ExactProjectionWithdrawalOutcome>
_withdrawProjection;
private readonly EntityEffectPoseRegistry _poses;
private ParentAttachmentState Relations => _liveEntities.ParentAttachments;
/// <summary>Raised after the attached projection is fully registered.</summary>
internal event Action<LiveEntityReadyCandidate>? EntityReady;
/// <summary>Raised after an attached projection has its composed pose.</summary>
public event Action<uint>? ProjectionPoseReady;
/// <summary>Raised when an attached projection leaves its cell presentation.</summary>
public event Action<LiveEntityRecord>? ProjectionRemoved;
private readonly Dictionary<RuntimeEntityKey, AttachedChild> _attachedByChild = [];
private readonly Dictionary<RuntimeEntityKey, PendingUnparentTransition>
_pendingUnparentByChild = [];
private readonly Dictionary<RuntimeEntityKey, PendingOrdinaryRemoval>
_pendingOrdinaryRemovalByRoot = [];
private readonly Dictionary<RuntimeEntityKey, PendingProjectionSubtree>
_pendingDetachedRemovalByChild = [];
private readonly Dictionary<RuntimeEntityKey, PendingProjectionSubtree>
_pendingReparentRemovalByChild = [];
private readonly Dictionary<RuntimeEntityKey, PendingProjectionSubtree>
_pendingPoseLossRemovalByChild = [];
private readonly Dictionary<RuntimeEntityKey, PendingProjectionSubtree>
_pendingOrphanRemovalByChild = [];
private readonly List<uint> _pendingProjectionChildrenScratch = new();
private readonly List<KeyValuePair<RuntimeEntityKey, PendingOrdinaryRemoval>>
_pendingOrdinaryRemovalScratch = new();
private readonly List<KeyValuePair<RuntimeEntityKey, PendingProjectionSubtree>>
_pendingProjectionSubtreeScratch = new();
private readonly List<KeyValuePair<RuntimeEntityKey, PendingUnparentTransition>>
_pendingUnparentScratch = new();
private readonly AttachmentUpdateOrder<RuntimeEntityKey, AttachedChild>
_updateOrder = new();
private readonly AttachmentUpdateOrder<uint, uint> _relationRecoveryOrder =
new();
private readonly Func<AttachedChild, RuntimeEntityKey?> _parentOfAttached;
private readonly Func<RuntimeEntityKey, bool> _tickAttached;
private readonly Func<RuntimeEntityKey, bool> _reconcileAttached;
private int _activePoseCompositionVisits;
/// <summary>
/// #319 D3/B6 (retail + architecture review round 2, 2026-08-05):
/// per-child log-once latch for <see cref="AcceptLateBoundCreateObjectRelation"/>'s
/// unaddressable-parent refusal - see the identical rationale on
/// <c>ParentAttachmentState._loggedIncarnationRefusals</c>.
/// </summary>
private readonly HashSet<uint> _loggedUnaddressableParentRefusals = [];
internal int LastFullPoseCompositionVisits { get; private set; }
internal int LastReconcilePoseCompositionVisits { get; private set; }
public IEnumerable<uint> AttachedEntityIds
{
get
{
foreach (AttachedChild child in _attachedByChild.Values)
yield return child.Entity.Id;
}
}
public EquippedChildRenderController(
IDatReaderWriter dats,
object datLock,
ClientObjectTable objects,
LiveEntityRuntime liveEntities,
EntityEffectPoseRegistry poses,
Func<ParentEvent.Parsed, bool> acceptParent,
Func<LiveEntityRecord, ulong, ulong, ExactProjectionWithdrawalOutcome>
withdrawProjection)
{
_dats = dats ?? throw new ArgumentNullException(nameof(dats));
_datLock = datLock ?? throw new ArgumentNullException(nameof(datLock));
_objects = objects ?? throw new ArgumentNullException(nameof(objects));
_liveEntities = liveEntities ?? throw new ArgumentNullException(nameof(liveEntities));
_poses = poses ?? throw new ArgumentNullException(nameof(poses));
_acceptParent = acceptParent ?? throw new ArgumentNullException(nameof(acceptParent));
_withdrawProjection = withdrawProjection
?? throw new ArgumentNullException(nameof(withdrawProjection));
_parentOfAttached = static child => child.ParentRecord.ProjectionKey;
_tickAttached = TickChild;
_reconcileAttached = ReconcileChild;
_objects.ObjectMoved += OnObjectMoved;
_objects.MoveRolledBack += OnMoveRolledBack;
_objects.ObjectRemovalClassified += OnObjectRemovalClassified;
}
/// <summary>
/// Seed/refresh an object from CreateObject. Equipped child CreateObjects
/// carry Placement + Parent directly; this is retail's login/first-observe
/// bootstrap and does not wait for a separate ParentEvent.
/// </summary>
public void OnSpawn(WorldSession.EntitySpawn spawn)
{
lock (_datLock)
{
if (spawn.ParentGuid is { } parentGuid and not 0
&& spawn.ParentLocation is { } parentLocation)
{
// PhysicsDesc::PhysicsDesc / UnPack (0x0051D4D0 /
// 0x0051DDD0) default an absent AnimationFrame to zero.
// Parent remains a complete relation in that case.
uint placementId = spawn.PlacementId ?? 0u;
AcceptLateBoundCreateObjectRelation(
parentGuid,
spawn.Guid,
parentLocation,
placementId,
spawn.PositionSequence);
}
ResolveAndTryRealize(spawn.Guid);
// ParentEvent can precede the parent's CreateObject. Revisit the
// complete descendant chain rooted at the object that arrived.
RetryWaitingDescendants(spawn.Guid);
}
}
/// <summary>
/// Retail <c>SmartBox::UpdateVisualDesc @ 0x00451F40</c> replaces an
/// attached child's visual description without requiring a world
/// Position. Re-realize the last accepted relation from the newest
/// canonical snapshot while retaining the logical entity.
/// </summary>
internal bool TryApplyAttachedAppearance(
LiveEntityRecord expectedRecord,
ulong expectedObjDescAuthorityVersion)
{
ArgumentNullException.ThrowIfNull(expectedRecord);
lock (_datLock)
{
if (!_liveEntities.IsCurrentObjDescAuthority(
expectedRecord,
expectedObjDescAuthorityVersion)
|| expectedRecord.ProjectionKind is not
LiveEntityProjectionKind.Attached
|| !expectedRecord.IsSpatiallyProjected
|| expectedRecord.WorldEntity is not { } expectedEntity
|| !Relations.RestoreLastAccepted(expectedRecord.ServerGuid))
{
return false;
}
bool projected = ResolveAndTryRealize(expectedRecord.ServerGuid);
if (projected)
{
// WithdrawPriorProjection removes the complete attached
// subtree and retains each descendant relation for recovery.
// The ObjDesc transaction is not published until that same
// subtree is synchronously whole again.
RetryWaitingDescendants(expectedRecord.ServerGuid);
}
return projected
&& _liveEntities.IsCurrentObjDescAuthority(
expectedRecord,
expectedObjDescAuthorityVersion)
&& expectedRecord.ProjectionKind is
LiveEntityProjectionKind.Attached
&& expectedRecord.IsSpatiallyProjected
&& ReferenceEquals(expectedRecord.WorldEntity, expectedEntity);
}
}
/// <summary>
/// Completes attachment projection after a root world entity has been
/// registered. Relation acceptance intentionally happens before root
/// projection selection; this notification only satisfies render-pose
/// dependencies for children waiting on that root.
/// </summary>
public void OnWorldEntityRegistered(uint guid)
{
lock (_datLock)
{
RetryWaitingDescendants(guid);
}
}
/// <summary>
/// Retries pose-withdrawn descendants only after their parent's new root
/// and indexed parts have been published.
/// </summary>
public void OnPosePublished(uint guid)
{
lock (_datLock)
RetryWaitingDescendants(guid);
}
/// <summary>
/// Apply/queue a live ParentEvent after the live-entity owner has exact-
/// gated the parent's INSTANCE_TS and advanced the child's shared
/// POSITION_TS. This controller owns only the render relationship.
/// </summary>
public void OnParentEvent(ParentEvent.Parsed update)
{
lock (_datLock)
{
Relations.Enqueue(update);
if (ResolveAndTryRealize(update.ChildGuid))
RetryWaitingDescendants(update.ChildGuid);
}
}
/// <summary>
/// Removes projections owned by one retired incarnation. The runtime has
/// already removed that record from its active GUID table, so the root's
/// attached-map entry is committed by exact record identity without a
/// GUID lookup. Still-live descendants use the normal exact withdrawal
/// path and remain eligible to replay their retained parent relation.
/// </summary>
public void OnLogicalTeardown(LiveEntityRecord record)
{
ArgumentNullException.ThrowIfNull(record);
lock (_datLock)
TearDownRecordProjections(record);
}
private void OnObjectRemovalClassified(ClientObjectRemoval removal)
{
if (removal.Reason is not ClientObjectRemovalReason.Ordinary)
return;
uint guid = removal.Object.ObjectId;
TearDownCurrentObjectProjections(guid);
// InventoryRemoveObject removes the item from the UI view, not the
// live physics generation. Preserve fresher pending ParentEvents so a
// same-generation world/parent update can still replay them. Logical
// delete/replacement are driven directly by the accepted inbound
// lifecycle and never depend on this table.
}
/// <summary>
/// A fresh Position or Pickup unparented this child. Remove the accepted
/// attachment/rollback projection while retaining fresher unresolved
/// ParentEvents; unlike logical deletion, do not disturb child-addressed
/// pending state or children that may themselves reference it.
/// </summary>
public ChildUnparentDisposition OnChildBecameUnparented(
uint childGuid,
Action? continuation = null)
{
if (!_liveEntities.TryGetRecord(childGuid, out LiveEntityRecord record))
{
Relations.EndChildProjection(childGuid);
return ChildUnparentDisposition.NotAttached;
}
RuntimeEntityKey key = RequireProjectionKey(record);
var pending = new PendingUnparentTransition(
record,
record.PositionAuthorityVersion,
CaptureProjectionSubtreeParentFirst(
record,
restoreRootRelation: false,
restoreDescendantRelations: true),
continuation);
return AdvanceUnparentTransition(key, pending);
}
/// <summary>Recompose every child after the parent's animation tick.</summary>
public void Tick()
{
RetryPendingProjectionTransitions();
_activePoseCompositionVisits = 0;
IReadOnlyList<RuntimeEntityKey> failed = _updateOrder.ForEachParentFirst(
_attachedByChild,
_parentOfAttached,
_tickAttached);
LastFullPoseCompositionVisits = _activePoseCompositionVisits;
for (int i = 0; i < failed.Count; i++)
WithdrawForPoseLoss(failed[i]);
}
/// <summary>
/// Preserve the post-network reconciliation boundary while recomposing
/// only attachment branches whose published parent pose/presentation
/// changed after the full animation-order pass.
/// </summary>
public void ReconcileSpatialMutations()
{
RetryPendingProjectionTransitions();
_activePoseCompositionVisits = 0;
IReadOnlyList<RuntimeEntityKey> failed = _updateOrder.ForEachParentFirst(
_attachedByChild,
_parentOfAttached,
_reconcileAttached);
LastReconcilePoseCompositionVisits = _activePoseCompositionVisits;
for (int i = 0; i < failed.Count; i++)
WithdrawForPoseLoss(failed[i]);
}
private void RetryPendingProjectionTransitions()
{
CopyEntries(
_pendingOrdinaryRemovalByRoot,
_pendingOrdinaryRemovalScratch);
for (int i = 0; i < _pendingOrdinaryRemovalScratch.Count; i++)
{
(RuntimeEntityKey rootKey, PendingOrdinaryRemoval pending) =
_pendingOrdinaryRemovalScratch[i];
AdvanceOrdinaryRemoval(rootKey, pending);
}
CopyEntries(
_pendingDetachedRemovalByChild,
_pendingProjectionSubtreeScratch);
for (int i = 0; i < _pendingProjectionSubtreeScratch.Count; i++)
{
(RuntimeEntityKey childKey, PendingProjectionSubtree pending) =
_pendingProjectionSubtreeScratch[i];
AdvanceDetachedRemoval(childKey, pending);
}
RetryProjectionSubtrees(_pendingReparentRemovalByChild);
RetryProjectionSubtrees(_pendingPoseLossRemovalByChild);
RetryProjectionSubtrees(_pendingOrphanRemovalByChild);
CopyEntries(_pendingUnparentByChild, _pendingUnparentScratch);
for (int i = 0; i < _pendingUnparentScratch.Count; i++)
{
(RuntimeEntityKey childKey, PendingUnparentTransition pending) =
_pendingUnparentScratch[i];
if (!_liveEntities.IsCurrentRecord(pending.Record)
|| pending.Record.PositionAuthorityVersion
!= pending.PositionAuthorityVersion)
{
_pendingUnparentByChild.Remove(childKey);
continue;
}
AdvanceUnparentTransition(childKey, pending);
}
Relations.CopyPendingProjectionChildrenTo(_pendingProjectionChildrenScratch);
for (int i = 0; i < _pendingProjectionChildrenScratch.Count; i++)
ResolveAndTryRealize(_pendingProjectionChildrenScratch[i]);
}
private static void CopyEntries<T>(
Dictionary<RuntimeEntityKey, T> source,
List<KeyValuePair<RuntimeEntityKey, T>> destination)
{
destination.Clear();
foreach (KeyValuePair<RuntimeEntityKey, T> entry in source)
destination.Add(entry);
}
private bool TickChild(RuntimeEntityKey childKey)
{
if (!_attachedByChild.TryGetValue(childKey, out AttachedChild? child))
return false;
_activePoseCompositionVisits++;
if (TryResolveExactAttachment(child, out WorldEntity parent)
&& _poses.TryGetRootPose(parent.Id, out Matrix4x4 parentWorld)
&& _poses.TryGetPartPoseSnapshot(
parent.Id,
out var parentPartPoses,
out var parentPartAvailability)
&& EquippedChildAttachment.TryComposePoseInto(
child.ParentSetup,
parentPartPoses,
parentPartAvailability,
child.ChildSetup,
child.ParentLocation,
child.Placement,
child.PartTemplate,
child.Scale,
child.PartPoseBuffer,
child.AttachedPartBuffer,
out EquippedChildPose pose))
{
child.Entity.MeshRefs = pose.AttachedParts;
child.Entity.SetIndexedPartPoses(pose.PartLocal, child.PartAvailability);
if (!ApplyParentWorldPose(child.Entity, parentWorld))
return false;
ApplyParentDrawVisibility(child.Entity, parent);
child.Entity.ParentCellId = parent.ParentCellId;
CaptureParentPresentation(child, parent);
PublishChildPose(child.Entity, parentWorld, parent.ParentCellId, pose);
// C4 route 7 D4: the canonical cell is Runtime's D1/D2
// propagation write, not this render tick's job any more — move
// only the graphical draw bucket to match. The disposition is
// consumed explicitly (review A2/R3, route 5's A1 class): a
// silently-discarded bool must not advance presentation on a
// write-nothing outcome.
if (TryResolveExactAttachment(child, out parent)
&& parent.ParentCellId is { } parentCellId)
{
EquippedChildPresentationRebucketDisposition disposition =
_liveEntities.RebucketEquippedChildPresentation(
child.ChildGuid,
parentCellId);
if (disposition is
EquippedChildPresentationRebucketDisposition.NoProjection)
{
// Defensive: as this call site is structured today,
// this branch cannot actually fire. TryResolveExactAttachment
// (just above, and again at the top of this method) requires
// _liveEntities.IsCurrentRecord(child.ChildRecord), which is
// the exact same _projections.TryGetCurrent(guid) lookup
// RebucketEquippedChildPresentation's own NoProjection guard
// performs for the same guid one call later — if the
// projection were gone, TryResolveExactAttachment would
// already have failed and this method would already have
// returned false above, never reaching here (round-3 review
// N6; verified by sabotage — see
// RebucketEquippedChildPresentation_D4_NoLiveProjection_ReturnsNoProjectionDisposition
// in EquippedChildProjectionWithdrawalTests.cs, which tests
// the disposition value directly rather than through this
// unreachable path). Kept as a fail-safe in case a future
// refactor reorders or removes the TryResolveExactAttachment
// gate above.
return false;
}
// Moved: normal case. NotAttached: the known-benign
// in-flight unparent/pending-residence window —
// OnChildBecameUnparented (or the residence conductor) owns
// the child's fate. Displaced (B6, round-3 review): a newer
// operation already superseded this exact rebucket attempt
// mid-flight, so the record is current and healthy, just not
// via THIS call — not evidence of a problem. Both leave the
// bucket wherever the more current operation put it, and
// this tick still reports success because the pose itself
// composed and published correctly.
}
ProjectionPoseReady?.Invoke(child.ChildGuid);
return true;
}
return false;
}
private bool ReconcileChild(RuntimeEntityKey childKey)
{
if (!_attachedByChild.TryGetValue(childKey, out AttachedChild? child)
|| !TryResolveExactAttachment(child, out WorldEntity parent))
{
return false;
}
return ParentPresentationMatches(child, parent) || TickChild(childKey);
}
private bool ParentPresentationMatches(
AttachedChild child,
WorldEntity parent)
=> ReferenceEquals(child.LastParentEntity, parent)
&& child.LastParentPoseVersion
== _poses.GetPoseChangeVersion(parent.Id)
&& child.LastParentDrawVisible == parent.IsDrawVisible
&& child.LastParentAncestorDrawVisible
== parent.IsAncestorDrawVisible
&& child.LastParentCellId == parent.ParentCellId;
private void CaptureParentPresentation(
AttachedChild child,
WorldEntity parent)
{
child.LastParentEntity = parent;
child.LastParentPoseVersion = _poses.GetPoseChangeVersion(parent.Id);
child.LastParentDrawVisible = parent.IsDrawVisible;
child.LastParentAncestorDrawVisible = parent.IsAncestorDrawVisible;
child.LastParentCellId = parent.ParentCellId;
}
private bool TryRealize(
ParentAttachmentRelation pending,
ParentProjectionCandidateKind candidateKind)
{
uint childGuid = pending.ChildGuid;
if (!_liveEntities.TryGetRecord(pending.ParentGuid, out LiveEntityRecord parentRecord)
|| parentRecord.WorldEntity is not { } parentEntity
|| !parentRecord.IsSpatiallyProjected
|| !_liveEntities.TryGetCanonical(
childGuid,
out RuntimeEntityRecord childCanonical)
|| !_liveEntities.TryGetSnapshot(pending.ParentGuid, out WorldSession.EntitySpawn parentSpawn)
|| !_liveEntities.TryGetSnapshot(childGuid, out WorldSession.EntitySpawn childSpawn))
return false;
if (parentSpawn.SetupTableId is not { } parentSetupId
|| childSpawn.SetupTableId is not { } childSetupId
|| parentEntity.ParentCellId is not { } parentCellId)
return false;
Setup? parentSetup = _dats.Get<Setup>(parentSetupId);
Setup? childSetup = _dats.Get<Setup>(childSetupId);
if (parentSetup is null || childSetup is null)
return false;
var parentLocation = (ParentLocation)pending.ParentLocation;
var placement = (Placement)pending.PlacementId;
IReadOnlyList<MeshRef> template = BuildPartTemplate(childSetup, childSpawn);
bool[] childPartAvailability = BuildPartAvailability(template);
float scale = childSpawn.ObjScale is { } objScale && objScale > 0f
? objScale
: 1.0f;
if (!_poses.TryGetPartPoseSnapshot(
parentEntity.Id,
out var parentPartPoses,
out var parentPartAvailability))
return false;
if (!_poses.TryGetRootPose(parentEntity.Id, out Matrix4x4 parentWorld)
|| !TryDecomposeWorldPose(parentWorld, out Vector3 parentPosition, out Quaternion parentRotation))
{
return false;
}
if (!EquippedChildAttachment.TryComposePoseInto(
parentSetup,
parentPartPoses,
parentPartAvailability,
childSetup,
parentLocation,
placement,
template,
scale,
partPoseBuffer: null,
attachedPartBuffer: null,
out EquippedChildPose pose))
return false;
// A parented object may materialize here before the ordinary world
// hydration path sees it. Install its logical effect profile before
// MaterializeLiveEntity registers runtime resources, exactly as for a
// top-level projection; rebucketing/reattachment must never recreate it.
var effectProfile = childSpawn.Physics is { } physics
? Vfx.EntityEffectProfile.CreateLive(childSetup, physics)
: Vfx.EntityEffectProfile.CreateDatStatic(childSetup);
if (_liveEntities.TryGetProjection(
childCanonical,
out LiveEntityRecord? retainedChild)
&& !_liveEntities.TryGetEffectProfile(childGuid, out _))
{
_liveEntities.SetEffectProfile(childGuid, effectProfile);
}
if (!_liveEntities.IsCurrentRecord(parentRecord)
|| !_liveEntities.IsCurrentCanonical(childCanonical)
|| !Relations.IsPending(pending, candidateKind))
{
return false;
}
// C3c: a world-created (residence-managed) child converting to an
// attached projection exits the residence-managed presentation path
// at this same-incarnation kind transition. Attached children have
// no Runtime placement, and the flip's sticky-residence rule expects
// equipped children to carry LegacyImmediate so a later drop back to
// world stays on the legacy path by construction
// (DatLiveEntityProjectionMaterializer.MaterializeProjection's
// retained-residence comment). C3c-R1 review F1: the conversion is
// the owner's explicit API, which asserts no initial-create
// residence is still active, rather than a direct field write here.
if (retainedChild is not null
&& _liveEntities.IsCurrentRecord(retainedChild))
{
_liveEntities.ConvertMaterializationResidenceToLegacyImmediate(
retainedChild);
}
WorldEntity? entity = _liveEntities.MaterializeLiveEntity(
childCanonical,
parentCellId,
localId =>
{
var created = new WorldEntity
{
Id = localId,
ServerGuid = childGuid,
SourceGfxObjOrSetupId = childSetupId,
Position = parentPosition,
Rotation = parentRotation,
MeshRefs = pose.AttachedParts,
PaletteOverride = BuildPaletteOverride(childSpawn),
ParentCellId = parentCellId,
};
created.SetIndexedPartPoses(pose.PartLocal, childPartAvailability);
return created;
},
LiveEntityProjectionKind.Attached,
initializeProjection: record => record.EffectProfile = effectProfile,
out LiveEntityRecord? childRecord);
if (entity is null || childRecord is null)
return false;
if (!_liveEntities.IsCurrentRecord(parentRecord)
|| !_liveEntities.IsCurrentRecord(childRecord)
|| !ReferenceEquals(childRecord.WorldEntity, entity)
|| !Relations.IsPending(pending, candidateKind))
{
if (_liveEntities.IsCurrentRecord(childRecord)
&& ReferenceEquals(childRecord.WorldEntity, entity)
&& childRecord.IsSpatiallyProjected)
{
BeginProjectionSubtreeWithdrawal(
_pendingOrphanRemovalByChild,
childRecord,
restoreRootRelation: false,
restoreDescendantRelations: false);
}
return false;
}
childRecord.HasPartArray = true;
ApplyParentWorldPose(entity, parentWorld);
ApplyParentDrawVisibility(entity, parentEntity);
entity.ParentCellId = parentCellId;
entity.ApplyAppearance(
pose.AttachedParts,
BuildPaletteOverride(childSpawn),
childSpawn.AnimPartChanges is { Count: > 0 } changes
? changes.Select(change => new PartOverride(change.PartIndex, change.NewModelId)).ToArray()
: Array.Empty<PartOverride>());
entity.SetIndexedPartPoses(pose.PartLocal, childPartAvailability);
var attached = new AttachedChild(
parentRecord,
childRecord,
pending.ParentGuid,
childGuid,
parentLocation,
placement,
parentSetup,
childSetup,
template,
childPartAvailability,
pose.PartLocal,
pose.AttachedParts,
scale,
entity);
CaptureParentPresentation(attached, parentEntity);
RuntimeEntityKey childKey = RequireProjectionKey(childRecord);
_attachedByChild[childKey] = attached;
_pendingUnparentByChild.Remove(childKey);
if ((parentRecord.FinalPhysicsState & PhysicsStateFlags.Hidden) != 0)
{
// A child attached while its parent is already Hidden inherits
// the same direct child NoDraw mutation retail applies from
// CPhysicsObj::set_hidden (0x00514C60).
_liveEntities.SetAttachedChildNoDraw(childGuid, noDraw: true);
}
PublishChildPose(entity, parentWorld, parentEntity.ParentCellId, pose);
Console.WriteLine(
$"equipment: attached child=0x{childGuid:X8} parent=0x{pending.ParentGuid:X8} " +
$"location={parentLocation} placement={placement}");
Relations.MarkProjected(pending, candidateKind);
LiveEntityReadyCandidate readyCandidate =
LiveEntityReadyCandidate.Capture(childRecord);
ProjectionPoseReady?.Invoke(childGuid);
return PublishEntityReadyExact(
_liveEntities,
readyCandidate,
EntityReady);
}
internal static bool PublishEntityReadyExact(
LiveEntityRuntime runtime,
LiveEntityReadyCandidate candidate,
Action<LiveEntityReadyCandidate>? publish)
{
ArgumentNullException.ThrowIfNull(runtime);
if (!candidate.IsCurrent(runtime))
return false;
publish?.Invoke(candidate);
return candidate.IsCurrent(runtime);
}
private void PublishChildPose(
WorldEntity child,
Matrix4x4 parentWorld,
uint? parentCellId,
EquippedChildPose pose)
{
_poses.Publish(
child.Id,
pose.RootLocal * parentWorld,
pose.PartLocal,
parentCellId ?? 0u,
child.IndexedPartAvailable);
}
/// <summary>
/// Installs the immediate parent's composed world root on the child
/// projection. The child's MeshRefs already contain its holding and
/// placement transforms relative to that direct parent root.
/// </summary>
internal static bool ApplyParentWorldPose(WorldEntity child, Matrix4x4 parentWorld)
{
if (!TryDecomposeWorldPose(parentWorld, out Vector3 position, out Quaternion rotation))
return false;
child.SetPosition(position);
child.Rotation = rotation;
return true;
}
/// <summary>
/// Preserve retail attachment-tree draw inheritance after adapting every
/// child CPhysicsObj into an independent retained draw entry. This does not
/// alter the child's own NoDraw state; parent-first ticking propagates an
/// ancestor's suppression through arbitrarily deep attachment chains.
/// </summary>
internal static void ApplyParentDrawVisibility(WorldEntity child, WorldEntity parent)
{
child.IsAncestorDrawVisible =
parent.IsDrawVisible && parent.IsAncestorDrawVisible;
}
private static bool TryDecomposeWorldPose(
Matrix4x4 world,
out Vector3 position,
out Quaternion rotation)
{
position = world.Translation;
if (!Matrix4x4.Decompose(world, out Vector3 scale, out rotation, out _)
|| Vector3.DistanceSquared(scale, Vector3.One) > 1e-6f)
return false;
rotation = Quaternion.Normalize(rotation);
return true;
}
/// <summary>
/// Resolves retail's parent Setup part index to the currently attached
/// child local ID for DefaultScriptPartHook.
/// </summary>
public uint? FindChildLocalIdAtPart(uint parentLocalId, uint partIndex)
{
foreach (AttachedChild child in _attachedByChild.Values)
{
if (!TryResolveExactAttachment(child, out WorldEntity parent)
|| parent.Id != parentLocalId
|| !child.ParentSetup.HoldingLocations.TryGetValue(
child.ParentLocation,
out LocationType? holding)
|| holding.PartId != partIndex)
{
continue;
}
return child.Entity.Id;
}
return null;
}
/// <summary>Returns the live parent whose UpdateChild path owns this child.</summary>
public uint? FindParentLocalId(uint childLocalId)
{
foreach (AttachedChild child in _attachedByChild.Values)
{
if (child.Entity.Id != childLocalId)
continue;
if (TryResolveExactAttachment(child, out WorldEntity parent))
return parent.Id;
return null;
}
return null;
}
/// <summary>
/// Retail <c>CPhysicsObj::set_hidden</c> walks the direct CHILDLIST and
/// sets/clears each child's NoDraw bit before changing root collision/cell
/// presentation. Descendants are not recursively rewritten here.
/// </summary>
public void SetDirectChildrenNoDraw(uint parentGuid, bool noDraw)
{
foreach (AttachedChild child in _attachedByChild.Values)
{
if (child.ParentGuid == parentGuid
&& TryResolveExactAttachment(child, out _))
_liveEntities.SetAttachedChildNoDraw(child.ChildGuid, noDraw);
}
}
public void OnCreateParentAccepted(CreateParentUpdate update)
{
lock (_datLock)
{
// CreateParentUpdate is the same-generation ObjDesc-refresh
// shape of a CreateObject-carried relation (BuildSameGenerationEvents,
// InboundPhysicsStateController.cs) - it carries the parent's
// GUID and location only, never an instance sequence, for the
// identical structural reason OnSpawn's raw CreateObject path
// does not (#319 F1).
AcceptLateBoundCreateObjectRelation(
update.ParentGuid,
update.ChildGuid,
update.ParentLocation,
update.PlacementId,
update.ChildPositionSequence);
ResolveAndTryRealize(update.ChildGuid);
}
}
/// <summary>
/// #319 F1: late-binds a CreateObject-carried parent relation (raw
/// CreateObject's <c>Physics.Parent</c> via <see cref="OnSpawn"/>, or
/// the same-generation <see cref="CreateParentUpdate"/> envelope via
/// <see cref="OnCreateParentAccepted"/>) to the parent's LIVE
/// incarnation, rather than assuming sequence zero. Neither wire shape
/// carries a parent instance sequence - retail's own attach is
/// GUID-only (<c>PhysicsDesc::get_parent_id</c> @0x00558a18 -&gt;
/// <c>GetObjectA</c> @0x00558a2d -&gt; <c>set_parent</c> @0x00558a3e),
/// so "the current holder of the guid" is the faithful mapping. The
/// parent's snapshot lookup here mirrors <see cref="ResolveRelations"/>'s
/// own lookup (<see cref="ResolveLiveParentInstance"/>).
/// </summary>
/// <remarks>
/// #319 A6 (architecture review, 2026-08-05): a queued/late-bind
/// deferral for an unaddressable parent was tried here and REMOVED.
/// <c>RuntimeEntityObjectLifetime.RegisterEntityCore</c>'s
/// <c>EnqueueDeferredCreate</c> gate (`:797-812`) defers the ENTIRE
/// CreateObject - both this raw shape (`incoming.ParentGuid`) and the
/// same-generation <see cref="CreateParentUpdate"/> envelope
/// (`incoming.Physics.Parent.Guid`, same `??` chain) - whenever its
/// parent is not yet addressable, and does so BEFORE either wire shape
/// is ever produced (`AcceptCreateCore`/`BuildSameGenerationEvents` are
/// reached only after that gate passes). So this method never observes
/// an unaddressable parent in production for EITHER producer - not an
/// empirical absence, a structural one. A deferral queue here carried
/// three independent defects (a missing child POSITION_TS gate, a
/// placeholder-incarnation collision with the generation filters,
/// unbounded mid-session accumulation) while being exercised by nothing
/// but a test that called this class's methods directly, bypassing the
/// production routing that makes the branch unreachable. If the else
/// branch below is ever reached, that upstream invariant has broken;
/// log loudly rather than reconstructing unreachable machinery to
/// paper over it. (B2, architecture review round 2: the upstream gate
/// tests <c>Entities.TryGetActive</c> - the active-record table - while
/// this method's own lookup below tests <c>_liveEntities.TryGetSnapshot</c>
/// - the inbound snapshot table. Active implies a snapshot exists,
/// because <c>AddActive</c> is fed from the same snapshot
/// <c>AcceptCreate</c> just wrote; the only known inversion window is
/// inside <c>TryDeleteEntity</c>, where the snapshot is removed several
/// statements before the active record, and reaching this method
/// through that window would require a re-entrant child CreateObject
/// inside it - not reachable from a single-threaded pump.) (B3: this
/// unreachability argument is scoped to the GRAPHICAL host - the
/// content-less direct/headless host has no
/// <c>EquippedChildRenderController</c> at all, so it has no
/// CreateObject-carried relation producer to which this argument would
/// even apply.)
/// </remarks>
private void AcceptLateBoundCreateObjectRelation(
uint parentGuid,
uint childGuid,
uint parentLocation,
uint placementId,
ushort childPositionSequence)
{
if (_liveEntities.TryGetSnapshot(parentGuid, out WorldSession.EntitySpawn parentSpawn))
{
Relations.AcceptCreateObjectRelation(new ParentAttachmentRelation(
parentGuid,
childGuid,
parentLocation,
placementId,
parentSpawn.InstanceSequence,
childPositionSequence));
}
else if (_loggedUnaddressableParentRefusals.Add(childGuid))
{
Console.Error.WriteLine(
$"equipment: parent 0x{parentGuid:X8} unaddressable for child " +
$"0x{childGuid:X8} at CreateObject-carried relation accept - " +
"refusing (#319 A6; should be structurally unreachable - see " +
"RuntimeEntityObjectLifetime.RegisterEntityCore's " +
"EnqueueDeferredCreate gate). Logged once for this child; " +
"further refusals for the same child are suppressed.");
}
}
private bool TryResolveExactAttachment(
AttachedChild child,
out WorldEntity parent)
{
if (_liveEntities.IsCurrentRecord(child.ParentRecord)
&& _liveEntities.IsCurrentRecord(child.ChildRecord)
&& child.ParentRecord.IsSpatiallyProjected
&& child.ChildRecord.IsSpatiallyProjected
&& child.ParentRecord.WorldEntity is { } exactParent
&& child.ChildRecord.WorldEntity is { } exactChild
&& ReferenceEquals(exactChild, child.Entity))
{
parent = exactParent;
return true;
}
parent = null!;
return false;
}
private void ResolveRelations(uint childGuid)
{
Relations.Resolve(
childGuid,
guid => _liveEntities.TryGetSnapshot(guid, out _),
ResolveLiveParentInstance,
_acceptParent);
}
/// <summary>
/// The parent's LIVE incarnation, or null if not currently addressable.
/// Shared by <see cref="ResolveRelations"/>'s late-bind lookup and
/// <see cref="PrepareAndTryRealize"/>'s #319 F1 commit-time tripwire.
/// </summary>
private ushort? ResolveLiveParentInstance(uint parentGuid) =>
_liveEntities.TryGetSnapshot(parentGuid, out WorldSession.EntitySpawn spawn)
? spawn.InstanceSequence
: null;
private bool ResolveAndTryRealize(uint childGuid)
{
bool projected = false;
while (true)
{
ResolveRelations(childGuid);
if (!Relations.TryGetStagedProjection(
childGuid,
out ParentAttachmentRelation staged))
{
break;
}
ParentProjectionValidationDisposition validation =
ValidateParentProjection(staged);
if (validation is ParentProjectionValidationDisposition.Waiting)
break;
if (validation is ParentProjectionValidationDisposition.Rejected)
{
Relations.RejectProjection(staged);
continue;
}
ProjectionPreparationResult result = PrepareAndTryRealize(
staged,
ParentProjectionCandidateKind.Staged);
projected |= result.Projected;
if (!result.CanAdvanceWireQueue)
return projected;
}
if (Relations.TryGetRecoveryProjection(
childGuid,
out ParentAttachmentRelation recovery)
&& ValidateParentProjection(recovery)
is ParentProjectionValidationDisposition.Ready)
{
projected |= PrepareAndTryRealize(
recovery,
ParentProjectionCandidateKind.Recovery).Projected;
}
return projected;
}
private ProjectionPreparationResult PrepareAndTryRealize(
ParentAttachmentRelation relation,
ParentProjectionCandidateKind candidateKind)
{
if (!_liveEntities.TryGetCanonical(
relation.ChildGuid,
out RuntimeEntityRecord childCanonical))
{
return default;
}
ulong positionAuthorityVersion =
childCanonical.PositionAuthorityVersion;
if (candidateKind is ParentProjectionCandidateKind.Staged)
{
// #319 A1 (architecture review, 2026-08-05): the incarnation
// tripwire MUST be evaluated before either half of the commit
// runs. The original shape called CommitStagedParent (the
// canonical half) first and let CommitProjection's internal
// check throw second - by then the canonical layer had already
// been rewritten as parented, so a mismatch tore the
// transaction (canonically parented, no committed relation, a
// staged relation blocking Resolve forever) instead of
// refusing cleanly. This read-only pre-check runs before any
// mutation on either side.
if (!Relations.CanCommitIncarnation(relation, ResolveLiveParentInstance))
{
Relations.RejectProjection(relation);
return new ProjectionPreparationResult(
CanAdvanceWireQueue: true,
Projected: false);
}
if (!_liveEntities.CommitStagedParent(relation, out _)
|| !Relations.CommitProjection(relation, ResolveLiveParentInstance))
{
return default;
}
candidateKind = ParentProjectionCandidateKind.Recovery;
}
else if (!Relations.IsCommitted(relation))
{
return default;
}
if (!Relations.IsPending(relation, candidateKind)
|| !_liveEntities.CommitAcceptedParentCellless(
childCanonical,
positionAuthorityVersion))
{
return default;
}
if (_liveEntities.TryGetProjection(
childCanonical,
out LiveEntityRecord? childRecord)
&& !WithdrawPriorProjection(childRecord))
{
return default;
}
return new(
CanAdvanceWireQueue: true,
Projected: TryRealize(relation, candidateKind));
}
internal ParentProjectionValidationDisposition ValidateParentProjection(
ParentAttachmentRelation relation)
{
if (relation.ParentGuid == relation.ChildGuid)
return ParentProjectionValidationDisposition.Rejected;
if (!_liveEntities.TryGetRecord(relation.ParentGuid, out LiveEntityRecord parent)
|| !_liveEntities.TryGetCanonical(relation.ChildGuid, out _)
|| parent.WorldEntity is null
|| !parent.HasPartArray)
{
return ParentProjectionValidationDisposition.Waiting;
}
if (!_liveEntities.TryGetSnapshot(
relation.ParentGuid,
out WorldSession.EntitySpawn parentSpawn)
|| parentSpawn.SetupTableId is not { } parentSetupId)
{
return ParentProjectionValidationDisposition.Rejected;
}
Setup? parentSetup = _dats.Get<Setup>(parentSetupId);
return parentSetup is not null
&& parentSetup.HoldingLocations.ContainsKey(
(ParentLocation)relation.ParentLocation)
? ParentProjectionValidationDisposition.Ready
: ParentProjectionValidationDisposition.Rejected;
}
private readonly record struct ProjectionPreparationResult(
bool CanAdvanceWireQueue,
bool Projected);
private void RetryWaitingDescendants(uint parentGuid)
{
_relationRecoveryOrder.RealizeDescendants(
parentGuid,
Relations.ChildrenWaitingForParent,
ResolveAndTryRealize);
}
private IReadOnlyList<MeshRef> BuildPartTemplate(
Setup setup,
WorldSession.EntitySpawn spawn)
{
var result = new MeshRef[setup.Parts.Count];
for (int i = 0; i < result.Length; i++)
result[i] = new MeshRef((uint)setup.Parts[i], Matrix4x4.Identity);
IReadOnlyList<CreateObject.AnimPartChange> partChanges =
spawn.AnimPartChanges ?? Array.Empty<CreateObject.AnimPartChange>();
for (int i = 0; i < partChanges.Count; i++)
{
CreateObject.AnimPartChange change = partChanges[i];
if (change.PartIndex < result.Length)
result[change.PartIndex] = new MeshRef(change.NewModelId, Matrix4x4.Identity);
}
IReadOnlyList<CreateObject.TextureChange> textureChanges =
spawn.TextureChanges ?? Array.Empty<CreateObject.TextureChange>();
for (int partIndex = 0; partIndex < result.Length; partIndex++)
{
Dictionary<uint, uint>? oldToNew = null;
for (int t = 0; t < textureChanges.Count; t++)
{
CreateObject.TextureChange change = textureChanges[t];
if (change.PartIndex != partIndex) continue;
oldToNew ??= new Dictionary<uint, uint>();
oldToNew[change.OldTexture] = change.NewTexture;
}
if (oldToNew is null) continue;
GfxObj? gfx = _dats.Get<GfxObj>(result[partIndex].GfxObjId);
if (gfx is null) continue;
Dictionary<uint, uint>? surfaceOverrides = null;
foreach (var surfaceQid in gfx.Surfaces)
{
uint surfaceId = (uint)surfaceQid;
Surface? surface = _dats.Get<Surface>(surfaceId);
if (surface is null) continue;
uint oldTexture = (uint)surface.OrigTextureId;
if (!oldToNew.TryGetValue(oldTexture, out uint replacement)) continue;
surfaceOverrides ??= new Dictionary<uint, uint>();
surfaceOverrides[surfaceId] = replacement;
}
if (surfaceOverrides is not null)
{
result[partIndex] = new MeshRef(
result[partIndex].GfxObjId,
Matrix4x4.Identity)
{
SurfaceOverrides = surfaceOverrides,
};
}
}
return result;
}
private bool[] BuildPartAvailability(IReadOnlyList<MeshRef> template)
{
var available = new bool[template.Count];
for (int i = 0; i < template.Count; i++)
available[i] = _dats.Get<GfxObj>(template[i].GfxObjId) is not null;
return available;
}
private static PaletteOverride? BuildPaletteOverride(WorldSession.EntitySpawn spawn)
{
if (spawn.SubPalettes is not { Count: > 0 } subPalettes)
return null;
var ranges = new PaletteOverride.SubPaletteRange[subPalettes.Count];
for (int i = 0; i < subPalettes.Count; i++)
{
CreateObject.SubPaletteSwap swap = subPalettes[i];
ranges[i] = new PaletteOverride.SubPaletteRange(
swap.SubPaletteId,
swap.Offset,
swap.Length);
}
return new PaletteOverride(spawn.BasePaletteId ?? 0, ranges);
}
private void OnObjectMoved(ClientObjectMove move)
{
// ServerSaysMoveItem publishes both complete placements. Only an
// equipped -> unequipped transition retires an attached paperdoll
// projection. InventoryPutObjectIn3D also has EquipLocation=None, but
// it is a world-entry confirmation and must never withdraw a Position
// projection that arrived just before the F019A event.
if (move.Previous.EquipLocation != EquipMask.None
&& move.Current.EquipLocation == EquipMask.None)
BeginDetachedRemoval(move.ItemId);
}
private void BeginDetachedRemoval(uint childGuid)
{
if (!_liveEntities.TryGetRecord(childGuid, out LiveEntityRecord record))
return;
BeginProjectionSubtreeWithdrawal(
_pendingDetachedRemovalByChild,
record,
restoreRootRelation: false,
restoreDescendantRelations: true);
}
private void AdvanceDetachedRemoval(
RuntimeEntityKey childKey,
PendingProjectionSubtree pending)
{
AdvanceProjectionSubtree(
_pendingDetachedRemovalByChild,
childKey,
pending);
}
private void OnMoveRolledBack(ClientObject item)
{
if (item.CurrentlyEquippedLocation == EquipMask.None)
return;
if (!_liveEntities.TryGetRecord(
item.ObjectId,
out LiveEntityRecord record)
|| record.ProjectionKey is not { } key)
{
return;
}
if (_attachedByChild.TryGetValue(key, out AttachedChild? attached)
&& _liveEntities.IsCurrentRecord(attached.ChildRecord)
&& attached.ChildRecord.IsSpatiallyProjected)
{
// A component-stage failure left the exact equipped projection
// untouched. The authoritative rollback therefore has nothing to
// reconstruct and merely cancels its retained leave-world retry.
_pendingDetachedRemovalByChild.Remove(key);
return;
}
if (_pendingDetachedRemovalByChild.TryGetValue(
key,
out PendingProjectionSubtree pending))
{
AdvanceDetachedRemoval(key, pending);
if (_pendingDetachedRemovalByChild.ContainsKey(key))
return;
}
// A rejected unwield restores the equipped location without a fresh
// wire ParentEvent; reinstall the last accepted relationship only for
// this explicit rollback signal.
if (Relations.RestoreLastAccepted(item.ObjectId))
{
lock (_datLock)
{
if (ResolveAndTryRealize(item.ObjectId))
RetryWaitingDescendants(item.ObjectId);
}
}
}
private bool Remove(uint childGuid)
{
if (!_liveEntities.TryGetRecord(
childGuid,
out LiveEntityRecord record)
|| record.ProjectionKey is not { } key
|| !_attachedByChild.TryGetValue(key, out AttachedChild? child))
return false;
if (!_liveEntities.IsCurrentRecord(child.ChildRecord))
return CommitProjectionRemoval(child);
return Remove(
child,
child.ChildRecord.PositionAuthorityVersion,
child.ChildRecord.ProjectionMutationVersion);
}
private bool Remove(
AttachedChild child,
ulong positionAuthorityVersion,
ulong projectionMutationVersion)
{
if (!_liveEntities.IsCurrentRecord(child.ChildRecord))
return CommitProjectionRemoval(child);
ExactProjectionWithdrawalOutcome outcome = WithdrawAttachedProjection(
child.ChildRecord,
positionAuthorityVersion,
projectionMutationVersion,
_withdrawProjection,
() => CommitProjectionRemoval(child));
if (outcome.Failure is not null)
throw outcome.Failure;
return outcome.Disposition is ExactProjectionWithdrawalDisposition.Completed;
}
private bool WithdrawPriorProjection(LiveEntityRecord childRecord)
{
RuntimeEntityKey key = RequireProjectionKey(childRecord);
if (_pendingReparentRemovalByChild.TryGetValue(
key,
out PendingProjectionSubtree pending))
{
return AdvanceProjectionSubtree(
_pendingReparentRemovalByChild,
key,
pending);
}
return BeginProjectionSubtreeWithdrawal(
_pendingReparentRemovalByChild,
childRecord,
restoreRootRelation: false,
restoreDescendantRelations: true);
}
private ChildUnparentDisposition AdvanceUnparentTransition(
RuntimeEntityKey childKey,
PendingUnparentTransition pending)
{
ExactProjectionWithdrawalOutcome outcome = AdvanceProjectionSubtree(
pending.Subtree,
out PendingProjectionSubtree next);
if (outcome.Disposition is ExactProjectionWithdrawalDisposition.Pending)
{
_pendingUnparentByChild[childKey] = pending with { Subtree = next };
if (outcome.Failure is not null)
throw outcome.Failure;
return ChildUnparentDisposition.Pending;
}
if (outcome.Disposition is ExactProjectionWithdrawalDisposition.Superseded)
{
_pendingUnparentByChild.Remove(childKey);
if (outcome.Failure is not null)
throw outcome.Failure;
return ChildUnparentDisposition.Superseded;
}
PendingUnparentTransition awaitingContinuation = pending with { Subtree = next };
_pendingUnparentByChild[childKey] = awaitingContinuation;
if (outcome.Failure is not null)
throw outcome.Failure;
Relations.EndChildProjection(pending.Record.ServerGuid);
try
{
awaitingContinuation.Continuation?.Invoke();
_pendingUnparentByChild.Remove(childKey);
return ChildUnparentDisposition.Completed;
}
catch
{
bool continuationCommitted =
_liveEntities.IsCurrentRecord(awaitingContinuation.Record)
&& awaitingContinuation.Record.PositionAuthorityVersion
== awaitingContinuation.PositionAuthorityVersion
&& awaitingContinuation.Record.IsSpatiallyProjected
&& awaitingContinuation.Record.ProjectionKind
is LiveEntityProjectionKind.World;
if (continuationCommitted)
_pendingUnparentByChild.Remove(childKey);
else
_pendingUnparentByChild[childKey] = awaitingContinuation;
throw;
}
}
internal static ExactProjectionWithdrawalOutcome WithdrawAttachedProjection(
LiveEntityRecord childRecord,
ulong positionAuthorityVersion,
ulong projectionMutationVersion,
Func<LiveEntityRecord, ulong, ulong, ExactProjectionWithdrawalOutcome>
withdrawProjection,
Func<bool> commitRemoval)
{
ArgumentNullException.ThrowIfNull(childRecord);
ArgumentNullException.ThrowIfNull(withdrawProjection);
ArgumentNullException.ThrowIfNull(commitRemoval);
ExactProjectionWithdrawalOutcome outcome = withdrawProjection(
childRecord,
positionAuthorityVersion,
projectionMutationVersion);
if (outcome.Disposition is ExactProjectionWithdrawalDisposition.Pending)
return outcome;
try
{
commitRemoval();
return outcome;
}
catch (Exception error)
{
return new ExactProjectionWithdrawalOutcome(
outcome.Disposition,
outcome.Failure is null
? error
: new AggregateException(outcome.Failure, error));
}
}
private bool CommitProjectionRemoval(AttachedChild child)
{
RuntimeEntityKey key = RequireProjectionKey(child.ChildRecord);
if (!_attachedByChild.TryGetValue(key, out AttachedChild? current)
|| !ReferenceEquals(current, child))
{
return false;
}
_attachedByChild.Remove(key);
ProjectionRemoved?.Invoke(child.ChildRecord);
return true;
}
private void WithdrawForPoseLoss(RuntimeEntityKey childKey)
{
if (!_liveEntities.TryGetRecord(
childKey,
out LiveEntityRecord record))
return;
BeginProjectionSubtreeWithdrawal(
_pendingPoseLossRemovalByChild,
record,
restoreRootRelation: true,
restoreDescendantRelations: true);
}
private void TearDownCurrentObjectProjections(uint guid)
{
if (_liveEntities.TryGetRecord(guid, out LiveEntityRecord record))
{
RuntimeEntityKey key = RequireProjectionKey(record);
List<AttachedRemovalCapture> captures = CaptureRecordSubtreeParentFirst(record);
AdvanceOrdinaryRemoval(
key,
new PendingOrdinaryRemoval(record, captures, NextIndex: 0));
return;
}
AttachedChild? stale = _attachedByChild.Values.FirstOrDefault(
candidate => candidate.ChildGuid == guid);
if (stale is not null)
CommitProjectionRemoval(stale);
Relations.EndChildProjection(guid);
}
private void AdvanceOrdinaryRemoval(
RuntimeEntityKey rootKey,
PendingOrdinaryRemoval pending)
{
if (!_liveEntities.IsCurrentRecord(pending.RootRecord))
{
_pendingOrdinaryRemovalByRoot.Remove(rootKey);
return;
}
for (int i = pending.NextIndex; i < pending.Captures.Count; i++)
{
AttachedRemovalCapture captured = pending.Captures[i];
ExactProjectionWithdrawalOutcome outcome = WithdrawCaptured(captured);
if (outcome.Disposition is ExactProjectionWithdrawalDisposition.Pending)
{
_pendingOrdinaryRemovalByRoot[rootKey] = pending with { NextIndex = i };
if (outcome.Failure is not null)
throw outcome.Failure;
return;
}
if (!ReferenceEquals(captured.Attached.ChildRecord, pending.RootRecord)
&& outcome.Disposition is ExactProjectionWithdrawalDisposition.Completed)
{
Relations.RestoreLastAccepted(captured.Attached.ChildGuid);
}
if (outcome.Failure is not null)
{
_pendingOrdinaryRemovalByRoot[rootKey] = pending with
{
NextIndex = i + 1,
};
throw outcome.Failure;
}
}
_pendingOrdinaryRemovalByRoot.Remove(rootKey);
Relations.EndChildProjection(pending.RootRecord.ServerGuid);
}
private void TearDownRecordProjections(LiveEntityRecord record)
{
RuntimeEntityKey key = RequireProjectionKey(record);
if (_pendingUnparentByChild.TryGetValue(key, out var pending)
&& ReferenceEquals(pending.Record, record))
{
_pendingUnparentByChild.Remove(key);
}
if (_pendingOrdinaryRemovalByRoot.TryGetValue(
key,
out PendingOrdinaryRemoval ordinary)
&& ReferenceEquals(ordinary.RootRecord, record))
{
_pendingOrdinaryRemovalByRoot.Remove(key);
}
RemovePendingProjectionSubtree(_pendingDetachedRemovalByChild, record);
RemovePendingProjectionSubtree(_pendingReparentRemovalByChild, record);
RemovePendingProjectionSubtree(_pendingPoseLossRemovalByChild, record);
RemovePendingProjectionSubtree(_pendingOrphanRemovalByChild, record);
List<AttachedRemovalCapture> subtree = CaptureRecordSubtreeParentFirst(record);
for (int i = 0; i < subtree.Count; i++)
{
AttachedRemovalCapture captured = subtree[i];
AttachedChild child = captured.Attached;
if (ReferenceEquals(child.ChildRecord, record))
{
CommitProjectionRemoval(child);
continue;
}
ExactProjectionWithdrawalOutcome outcome = WithdrawCaptured(captured);
if (outcome.Disposition is ExactProjectionWithdrawalDisposition.Pending)
{
if (outcome.Failure is not null)
throw outcome.Failure;
throw new InvalidOperationException(
$"Attached projection 0x{child.ChildGuid:X8} remains pending exact teardown of 0x{record.ServerGuid:X8}.");
}
if (outcome.Disposition is ExactProjectionWithdrawalDisposition.Completed)
Relations.RestoreLastAccepted(child.ChildGuid);
if (outcome.Failure is not null)
throw outcome.Failure;
}
}
private List<AttachedRemovalCapture> CaptureRecordSubtreeParentFirst(
LiveEntityRecord record)
{
var result = new List<AttachedRemovalCapture>();
var visited = new HashSet<LiveEntityRecord>(ReferenceEqualityComparer.Instance);
if (record.ProjectionKey is { } key
&& _attachedByChild.TryGetValue(key, out AttachedChild? root)
&& ReferenceEquals(root.ChildRecord, record))
{
result.Add(Capture(root));
}
CollectDescendantsParentFirst(record, result, visited);
return result;
}
private void CollectDescendantsParentFirst(
LiveEntityRecord parent,
List<AttachedRemovalCapture> destination,
HashSet<LiveEntityRecord> visited)
{
if (!visited.Add(parent))
return;
AttachedChild[] children = _attachedByChild.Values
.Where(child => ReferenceEquals(child.ParentRecord, parent))
.ToArray();
for (int i = 0; i < children.Length; i++)
{
destination.Add(Capture(children[i]));
CollectDescendantsParentFirst(children[i].ChildRecord, destination, visited);
}
}
private static AttachedRemovalCapture Capture(AttachedChild child) => new(
child,
child.ChildRecord.PositionAuthorityVersion,
child.ChildRecord.ProjectionMutationVersion);
private ExactProjectionWithdrawalOutcome WithdrawCaptured(
AttachedRemovalCapture captured)
{
RuntimeEntityKey key = RequireProjectionKey(
captured.Attached.ChildRecord);
if (!_attachedByChild.TryGetValue(
key,
out AttachedChild? current)
|| !ReferenceEquals(current, captured.Attached))
{
return new ExactProjectionWithdrawalOutcome(
ExactProjectionWithdrawalDisposition.Superseded,
Failure: null);
}
return WithdrawAttachedProjection(
captured.Attached.ChildRecord,
captured.PositionAuthorityVersion,
captured.ProjectionMutationVersion,
_withdrawProjection,
() => CommitProjectionRemoval(captured.Attached));
}
private PendingProjectionSubtree CaptureProjectionSubtreeParentFirst(
LiveEntityRecord root,
bool restoreRootRelation,
bool restoreDescendantRelations)
{
var captures = new List<ProjectionRemovalCapture>();
var visited = new HashSet<LiveEntityRecord>(ReferenceEqualityComparer.Instance);
CaptureProjectionNode(root, isRoot: true, captures, visited);
return new PendingProjectionSubtree(
root,
root.PositionAuthorityVersion,
captures,
NextIndex: 0,
restoreRootRelation,
restoreDescendantRelations);
}
private void CaptureProjectionNode(
LiveEntityRecord record,
bool isRoot,
List<ProjectionRemovalCapture> destination,
HashSet<LiveEntityRecord> visited)
{
if (!visited.Add(record))
return;
AttachedChild? attached = null;
if (record.ProjectionKey is { } key)
_attachedByChild.TryGetValue(key, out attached);
if (attached is not null && !ReferenceEquals(attached.ChildRecord, record))
attached = null;
if (record.IsSpatiallyProjected || attached is not null)
{
destination.Add(new ProjectionRemovalCapture(
record,
record.PositionAuthorityVersion,
record.ProjectionMutationVersion,
attached,
isRoot));
}
AttachedChild[] children = _attachedByChild.Values
.Where(child => ReferenceEquals(child.ParentRecord, record))
.ToArray();
for (int i = 0; i < children.Length; i++)
{
CaptureProjectionNode(
children[i].ChildRecord,
isRoot: false,
destination,
visited);
}
}
private bool BeginProjectionSubtreeWithdrawal(
Dictionary<RuntimeEntityKey, PendingProjectionSubtree> pendingByRoot,
LiveEntityRecord root,
bool restoreRootRelation,
bool restoreDescendantRelations)
{
PendingProjectionSubtree pending = CaptureProjectionSubtreeParentFirst(
root,
restoreRootRelation,
restoreDescendantRelations);
return AdvanceProjectionSubtree(
pendingByRoot,
RequireProjectionKey(root),
pending);
}
private bool AdvanceProjectionSubtree(
Dictionary<RuntimeEntityKey, PendingProjectionSubtree> pendingByRoot,
RuntimeEntityKey rootKey,
PendingProjectionSubtree pending)
{
ExactProjectionWithdrawalOutcome outcome = AdvanceProjectionSubtree(
pending,
out PendingProjectionSubtree next);
bool retry = outcome.Disposition is ExactProjectionWithdrawalDisposition.Pending
|| (outcome.Failure is not null && next.NextIndex < next.Captures.Count);
if (retry)
pendingByRoot[rootKey] = next;
else
pendingByRoot.Remove(rootKey);
if (outcome.Failure is not null)
throw outcome.Failure;
return outcome.Disposition is ExactProjectionWithdrawalDisposition.Completed
&& next.NextIndex >= next.Captures.Count;
}
private ExactProjectionWithdrawalOutcome AdvanceProjectionSubtree(
PendingProjectionSubtree pending,
out PendingProjectionSubtree next)
{
next = pending;
if (!_liveEntities.IsCurrentRecord(pending.RootRecord)
|| pending.RootRecord.PositionAuthorityVersion
!= pending.RootPositionAuthorityVersion)
{
return new(
ExactProjectionWithdrawalDisposition.Superseded,
Failure: null);
}
for (int i = pending.NextIndex; i < pending.Captures.Count; i++)
{
ProjectionRemovalCapture captured = pending.Captures[i];
ExactProjectionWithdrawalOutcome outcome = WithdrawProjectionCapture(captured);
if (outcome.Disposition is ExactProjectionWithdrawalDisposition.Pending)
{
next = pending with { NextIndex = i };
return outcome;
}
if (outcome.Disposition is ExactProjectionWithdrawalDisposition.Superseded
&& captured.IsRoot)
{
next = pending with { NextIndex = i + 1 };
return outcome;
}
if (outcome.Disposition is ExactProjectionWithdrawalDisposition.Completed
&& captured.Attached is not null
&& (captured.IsRoot
? pending.RestoreRootRelation
: pending.RestoreDescendantRelations))
{
Relations.RestoreLastAccepted(captured.Record.ServerGuid);
}
next = pending with { NextIndex = i + 1 };
if (outcome.Failure is not null)
return outcome;
}
return new(
ExactProjectionWithdrawalDisposition.Completed,
Failure: null);
}
private ExactProjectionWithdrawalOutcome WithdrawProjectionCapture(
ProjectionRemovalCapture captured)
{
if (captured.Attached is { } attached)
{
return WithdrawCaptured(new AttachedRemovalCapture(
attached,
captured.PositionAuthorityVersion,
captured.ProjectionMutationVersion));
}
if (!_liveEntities.IsCurrentRecord(captured.Record)
|| captured.Record.PositionAuthorityVersion
!= captured.PositionAuthorityVersion
|| captured.Record.ProjectionMutationVersion
!= captured.ProjectionMutationVersion)
{
return new(
ExactProjectionWithdrawalDisposition.Superseded,
Failure: null);
}
if (!captured.Record.IsSpatiallyProjected)
{
return new(
ExactProjectionWithdrawalDisposition.Completed,
Failure: null);
}
return _withdrawProjection(
captured.Record,
captured.PositionAuthorityVersion,
captured.ProjectionMutationVersion);
}
private void RetryProjectionSubtrees(
Dictionary<RuntimeEntityKey, PendingProjectionSubtree> pendingByRoot)
{
CopyEntries(pendingByRoot, _pendingProjectionSubtreeScratch);
for (int i = 0; i < _pendingProjectionSubtreeScratch.Count; i++)
{
(RuntimeEntityKey rootKey, PendingProjectionSubtree pending) =
_pendingProjectionSubtreeScratch[i];
AdvanceProjectionSubtree(pendingByRoot, rootKey, pending);
}
}
private static void RemovePendingProjectionSubtree(
Dictionary<RuntimeEntityKey, PendingProjectionSubtree> pendingByRoot,
LiveEntityRecord record)
{
if (record.ProjectionKey is { } key
&& pendingByRoot.TryGetValue(
key,
out PendingProjectionSubtree pending)
&& ReferenceEquals(pending.RootRecord, record))
{
pendingByRoot.Remove(key);
}
}
public void Clear()
{
AttachedChild[] attached = _attachedByChild.Values.ToArray();
for (int i = 0; i < attached.Length; i++)
CommitProjectionRemoval(attached[i]);
_pendingUnparentByChild.Clear();
_pendingOrdinaryRemovalByRoot.Clear();
_pendingDetachedRemovalByChild.Clear();
_pendingReparentRemovalByChild.Clear();
_pendingPoseLossRemovalByChild.Clear();
_pendingOrphanRemovalByChild.Clear();
_loggedUnaddressableParentRefusals.Clear();
Relations.Clear();
}
public void Dispose()
{
Clear();
_objects.ObjectMoved -= OnObjectMoved;
_objects.MoveRolledBack -= OnMoveRolledBack;
_objects.ObjectRemovalClassified -= OnObjectRemovalClassified;
}
private sealed record AttachedChild(
LiveEntityRecord ParentRecord,
LiveEntityRecord ChildRecord,
uint ParentGuid,
uint ChildGuid,
ParentLocation ParentLocation,
Placement Placement,
Setup ParentSetup,
Setup ChildSetup,
IReadOnlyList<MeshRef> PartTemplate,
IReadOnlyList<bool> PartAvailability,
Matrix4x4[] PartPoseBuffer,
MeshRef[] AttachedPartBuffer,
float Scale,
WorldEntity Entity)
{
public WorldEntity? LastParentEntity { get; set; }
public ulong LastParentPoseVersion { get; set; }
public bool LastParentDrawVisible { get; set; }
public bool LastParentAncestorDrawVisible { get; set; }
public uint? LastParentCellId { get; set; }
}
private readonly record struct PendingUnparentTransition(
LiveEntityRecord Record,
ulong PositionAuthorityVersion,
PendingProjectionSubtree Subtree,
Action? Continuation);
private readonly record struct PendingProjectionSubtree(
LiveEntityRecord RootRecord,
ulong RootPositionAuthorityVersion,
IReadOnlyList<ProjectionRemovalCapture> Captures,
int NextIndex,
bool RestoreRootRelation,
bool RestoreDescendantRelations);
private readonly record struct ProjectionRemovalCapture(
LiveEntityRecord Record,
ulong PositionAuthorityVersion,
ulong ProjectionMutationVersion,
AttachedChild? Attached,
bool IsRoot);
private readonly record struct AttachedRemovalCapture(
AttachedChild Attached,
ulong PositionAuthorityVersion,
ulong ProjectionMutationVersion);
private readonly record struct PendingOrdinaryRemoval(
LiveEntityRecord RootRecord,
IReadOnlyList<AttachedRemovalCapture> Captures,
int NextIndex);
private static RuntimeEntityKey RequireProjectionKey(
LiveEntityRecord record) =>
record.ProjectionKey
?? throw new InvalidOperationException(
$"Live entity 0x{record.ServerGuid:X8}/{record.Generation} " +
"has no exact projection key.");
}
public enum ChildUnparentDisposition
{
NotAttached,
Completed,
Pending,
Superseded,
}
internal enum ParentProjectionValidationDisposition
{
Ready,
Waiting,
Rejected,
}