refactor(world): extract live entity teardown

This commit is contained in:
Erik 2026-07-21 18:25:59 +02:00
parent 4427cfb2c7
commit f38822c490
8 changed files with 565 additions and 159 deletions

View file

@ -146,6 +146,7 @@ internal sealed class LiveEntityHydrationController
private readonly ILiveEntityReadyPublisher _ready;
private readonly ILiveEntityWorldOriginCoordinator _origin;
private readonly ILiveEntityNetworkUpdateSink _networkUpdates;
private readonly ILiveEntityTeardownCoordinator _teardown;
private readonly Action<uint, AcceptedPhysicsTimestamps> _publishTimestamps;
private readonly Func<uint> _playerGuid;
private readonly Action<string>? _diagnostic;
@ -159,6 +160,7 @@ internal sealed class LiveEntityHydrationController
ILiveEntityReadyPublisher ready,
ILiveEntityWorldOriginCoordinator origin,
ILiveEntityNetworkUpdateSink networkUpdates,
ILiveEntityTeardownCoordinator teardown,
Action<uint, AcceptedPhysicsTimestamps> publishTimestamps,
Func<uint> playerGuid,
Action<string>? diagnostic = null)
@ -171,6 +173,7 @@ internal sealed class LiveEntityHydrationController
_ready = ready ?? throw new ArgumentNullException(nameof(ready));
_origin = origin ?? throw new ArgumentNullException(nameof(origin));
_networkUpdates = networkUpdates ?? throw new ArgumentNullException(nameof(networkUpdates));
_teardown = teardown ?? throw new ArgumentNullException(nameof(teardown));
_publishTimestamps = publishTimestamps
?? throw new ArgumentNullException(nameof(publishTimestamps));
_playerGuid = playerGuid ?? throw new ArgumentNullException(nameof(playerGuid));
@ -339,6 +342,47 @@ internal sealed class LiveEntityHydrationController
return accepted;
}
/// <summary>
/// Retail <c>SmartBox::HandleDeleteObject @ 0x00451EA0</c> rejects the
/// player, requires the exact Instance timestamp, then performs one
/// symmetric logical teardown. An unknown owner only loses queued
/// pre-create effect packets.
/// </summary>
public bool OnDelete(DeleteObject.Parsed delete)
{
// SmartBox::HandleDeleteObject rejects the player before resolving an
// object or touching any queued owner state. A pre-Create player
// Delete must therefore be side-effect-free too.
if (delete.Guid == _playerGuid())
return false;
if (!_runtime.TryGetRecord(delete.Guid, out _))
_teardown.ForgetUnknownOwner(delete.Guid);
bool removed = _runtime.UnregisterLiveEntity(
delete,
isLocalPlayer: false,
beforeTeardown: () =>
ObjectTableWiring.ApplyEntityDelete(_objects, delete));
if (removed)
{
_diagnostic?.Invoke(
$"live: delete guid=0x{delete.Guid:X8} instSeq={delete.InstanceSequence}");
}
return removed;
}
/// <summary>
/// Retail's 25-second visibility expiry converges through the same exact
/// generation gate and teardown transaction as an authoritative F747.
/// </summary>
public bool OnPrune(LiveEntityPruneCandidate candidate) =>
OnDelete(new DeleteObject.Parsed(
candidate.ServerGuid,
candidate.Generation));
public int RetryPendingTeardowns() => _runtime.RetryPendingTeardowns();
/// <summary>
/// Parent events may precede either CreateObject. The relationship owner
/// retains wire order and invokes <see cref="TryAcceptParentForProjection"/>

View file

@ -0,0 +1,167 @@
using AcDream.App.Interaction;
using AcDream.App.Physics;
using AcDream.App.Rendering;
using AcDream.App.Rendering.Vfx;
using AcDream.App.Rendering.Wb;
using AcDream.Core.Physics;
using AcDream.Core.Rendering;
using AcDream.Core.Selection;
namespace AcDream.App.World;
internal interface ILiveEntityTeardownCoordinator
: ILiveEntityRuntimeComponentLifecycle
{
void ForgetUnknownOwner(uint serverGuid);
}
/// <summary>
/// Owns the retryable App-component half of retail
/// <c>CObjectMaint::DeleteObject @ 0x00508460</c>. Each successful cleanup is
/// committed on the exact record tombstone, so a later retry never repeats an
/// ExitWorld notification or removes a same-GUID replacement.
/// </summary>
internal sealed class LiveEntityRuntimeTeardownController
: ILiveEntityTeardownCoordinator
{
private readonly LiveEntityRuntime? _runtime;
private readonly LiveEntityPresentationController? _presentation;
private readonly EntityEffectController? _effects;
private readonly RemoteTeleportController? _remoteTeleport;
private readonly SelectionInteractionController? _interactions;
private readonly SelectionState? _selection;
private readonly LiveEntityAnimationRuntimeView<LiveEntityAnimationState>? _animations;
private readonly RemoteMovementObservationTracker? _remoteMovementObservations;
private readonly TranslucencyFadeManager? _translucencyFades;
private readonly LiveEntityProjectionWithdrawalController? _projectionWithdrawal;
private readonly EquippedChildRenderController? _children;
private readonly ShadowObjectRegistry? _shadows;
private readonly LiveEntityLightController? _lights;
private readonly EntityClassificationCache? _classification;
private readonly Func<uint>? _playerGuid;
private readonly Func<LiveEntityRecord, LiveEntityTeardownPlan> _createPlan;
private readonly Action<uint> _forgetUnknownOwner;
public LiveEntityRuntimeTeardownController(
LiveEntityRuntime runtime,
LiveEntityPresentationController presentation,
EntityEffectController effects,
RemoteTeleportController remoteTeleport,
SelectionInteractionController? interactions,
SelectionState selection,
LiveEntityAnimationRuntimeView<LiveEntityAnimationState> animations,
RemoteMovementObservationTracker remoteMovementObservations,
TranslucencyFadeManager translucencyFades,
LiveEntityProjectionWithdrawalController projectionWithdrawal,
EquippedChildRenderController children,
ShadowObjectRegistry shadows,
LiveEntityLightController lights,
EntityClassificationCache classification,
Func<uint> playerGuid)
{
_runtime = runtime ?? throw new ArgumentNullException(nameof(runtime));
_presentation = presentation ?? throw new ArgumentNullException(nameof(presentation));
_effects = effects ?? throw new ArgumentNullException(nameof(effects));
_remoteTeleport = remoteTeleport ?? throw new ArgumentNullException(nameof(remoteTeleport));
_interactions = interactions;
_selection = selection ?? throw new ArgumentNullException(nameof(selection));
_animations = animations ?? throw new ArgumentNullException(nameof(animations));
_remoteMovementObservations = remoteMovementObservations
?? throw new ArgumentNullException(nameof(remoteMovementObservations));
_translucencyFades = translucencyFades
?? throw new ArgumentNullException(nameof(translucencyFades));
_projectionWithdrawal = projectionWithdrawal
?? throw new ArgumentNullException(nameof(projectionWithdrawal));
_children = children ?? throw new ArgumentNullException(nameof(children));
_shadows = shadows ?? throw new ArgumentNullException(nameof(shadows));
_lights = lights ?? throw new ArgumentNullException(nameof(lights));
_classification = classification
?? throw new ArgumentNullException(nameof(classification));
_playerGuid = playerGuid ?? throw new ArgumentNullException(nameof(playerGuid));
_createPlan = CreatePlan;
_forgetUnknownOwner = effects.ForgetUnknownOwner;
}
internal LiveEntityRuntimeTeardownController(
Func<LiveEntityRecord, LiveEntityTeardownPlan> createPlan,
Action<uint> forgetUnknownOwner)
{
_createPlan = createPlan ?? throw new ArgumentNullException(nameof(createPlan));
_forgetUnknownOwner = forgetUnknownOwner
?? throw new ArgumentNullException(nameof(forgetUnknownOwner));
}
public void TearDown(LiveEntityRecord record)
{
ArgumentNullException.ThrowIfNull(record);
record.RuntimeComponentTeardownPlan ??= _createPlan(record);
record.RuntimeComponentTeardownPlan.Advance();
}
public void ForgetUnknownOwner(uint serverGuid) =>
_forgetUnknownOwner(serverGuid);
private LiveEntityTeardownPlan CreatePlan(LiveEntityRecord record)
{
LiveEntityRuntime runtime = _runtime!;
uint serverGuid = record.ServerGuid;
var incarnation = new LiveEntityIncarnationCleanup(
record,
guid => runtime.TryGetRecord(guid, out LiveEntityRecord current)
? current
: null);
var cleanups = new List<Action>
{
() => _presentation!.Forget(record),
() => _effects!.OnLiveEntityUnregistered(record),
() => _remoteTeleport!.Forget(record),
() =>
{
bool replacementExists =
runtime.TryGetRecord(serverGuid, out LiveEntityRecord current)
&& !ReferenceEquals(current, record);
if (_interactions is { } interactions)
{
interactions.OnEntityRemoved(record, replacementExists);
}
else if (!replacementExists
&& _selection!.SelectedObjectId == serverGuid)
{
_selection.Clear(
SelectionChangeSource.System,
SelectionChangeReason.SelectedObjectRemoved);
}
},
};
if (record.WorldEntity is not { } existingEntity)
return new LiveEntityTeardownPlan(cleanups);
// CPhysicsObj::exit_world @ 0x00514E60 drains the PartArray and
// MovementManager before unsticking/clearing the object's target and
// notifying its voyeurs. Preserve the two independent motion drains.
if (_animations!.TryGetValue(existingEntity.Id, out LiveEntityAnimationState animation))
cleanups.Add(() => animation.Sequencer?.Manager.HandleExitWorld());
if (record.RemoteMotionRuntime is RemoteMotion remoteMotion)
cleanups.Add(remoteMotion.Movement.HandleExitWorld);
if (record.PhysicsHost is EntityPhysicsHost physicsHost)
{
cleanups.Add(physicsHost.PositionManager.UnStick);
cleanups.Add(physicsHost.ClearTarget);
cleanups.Add(physicsHost.NotifyExitWorld);
}
cleanups.Add(() => _animations.Remove(existingEntity.Id));
cleanups.Add(() => _classification!.InvalidateEntity(existingEntity.Id));
cleanups.Add(() => incarnation.RunIfNoReplacement(
() => _remoteMovementObservations!.Remove(serverGuid)));
cleanups.Add(() => _translucencyFades!.ClearEntity(existingEntity.Id));
cleanups.Add(() => _projectionWithdrawal!.LeaveWorld(
record,
_playerGuid!()));
cleanups.Add(() => _children!.OnLogicalTeardown(record));
cleanups.Add(() => _shadows!.Deregister(existingEntity.Id));
cleanups.Add(() => _lights!.Forget(existingEntity.Id));
return new LiveEntityTeardownPlan(cleanups);
}
}