refactor(world): extract live entity teardown
This commit is contained in:
parent
4427cfb2c7
commit
f38822c490
8 changed files with 565 additions and 159 deletions
|
|
@ -372,8 +372,8 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
// ACE Player_Tick.cs line 368: the client never sends "released forward"
|
||||
// MoveToState, so the server never broadcasts an explicit stop. Observer
|
||||
// must infer it from position deltas.
|
||||
private readonly Dictionary<uint, (System.Numerics.Vector3 Pos, System.DateTime Time)>
|
||||
_remoteLastMove = new();
|
||||
private readonly AcDream.App.Physics.RemoteMovementObservationTracker
|
||||
_remoteMovementObservations = new();
|
||||
|
||||
/// <summary>
|
||||
/// Per-remote-entity movement state for smoothing between server
|
||||
|
|
@ -397,8 +397,8 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
/// keyed by server guid. Retail keeps these stamps on CPhysicsObj
|
||||
/// (update_times); seeded from CreateObject's PhysicsDesc timestamp
|
||||
/// block during <see cref="LiveEntityHydrationController.OnCreate"/>, consulted at the
|
||||
/// top of <see cref="OnLiveMotionUpdated"/>, dropped with the entity in
|
||||
/// <see cref="OnLiveEntityDeleted"/>.
|
||||
/// top of <see cref="OnLiveMotionUpdated"/>, and dropped by the live
|
||||
/// entity teardown owner.
|
||||
/// </summary>
|
||||
private AcDream.App.World.LiveEntityRuntime? _liveEntities;
|
||||
private AcDream.App.World.LiveEntityLivenessController? _liveEntityLiveness;
|
||||
|
|
@ -2064,6 +2064,8 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
// N.5 mandatory path: spawn adapters + dispatcher always construct.
|
||||
// _wbMeshAdapter, _meshShader, _textureCache, and _bindlessSupport are
|
||||
// all guaranteed non-null here (startup throws above if any are missing).
|
||||
var liveEntityComponentLifecycle =
|
||||
new AcDream.App.World.DeferredLiveEntityRuntimeComponentLifecycle();
|
||||
{
|
||||
var wbSpawnAdapter = new AcDream.App.Rendering.Wb.LandblockSpawnAdapter(_wbMeshAdapter!);
|
||||
// Sequencer factory: look up Setup + MotionTable from dats and build
|
||||
|
|
@ -2212,8 +2214,6 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
_worldState,
|
||||
AdvanceLandblockPresentationRetirement);
|
||||
|
||||
var liveEntityComponentLifecycle =
|
||||
new AcDream.App.World.DeferredLiveEntityRuntimeComponentLifecycle();
|
||||
_liveEntities = new AcDream.App.World.LiveEntityRuntime(
|
||||
_worldState,
|
||||
new AcDream.App.World.CompositeLiveEntityResourceLifecycle(
|
||||
|
|
@ -2224,9 +2224,6 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
entityScriptActivator.OnCreate,
|
||||
entityScriptActivator.OnRemove)),
|
||||
liveEntityComponentLifecycle);
|
||||
liveEntityComponentLifecycle.Bind(
|
||||
new AcDream.App.World.DelegateLiveEntityRuntimeComponentLifecycle(
|
||||
TearDownLiveEntityRuntimeComponents));
|
||||
_liveEntities.ProjectionVisibilityChanged += (record, visible) =>
|
||||
{
|
||||
if (record.WorldEntity is { } entity)
|
||||
|
|
@ -2237,10 +2234,6 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
if (record.WorldEntity is { } entity)
|
||||
_particleSink!.SetEntityPresentationVisible(entity.Id, visible);
|
||||
};
|
||||
_liveEntityLiveness = new AcDream.App.World.LiveEntityLivenessController(
|
||||
_liveEntities,
|
||||
() => _playerServerGuid,
|
||||
OnLiveEntityPruned);
|
||||
_projectileController = new AcDream.App.Physics.ProjectileController(
|
||||
_liveEntities,
|
||||
_physicsEngine,
|
||||
|
|
@ -2566,6 +2559,24 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
() => _playerServerGuid,
|
||||
IsSealedDungeonCell,
|
||||
Console.WriteLine);
|
||||
var liveEntityTeardown =
|
||||
new AcDream.App.World.LiveEntityRuntimeTeardownController(
|
||||
_liveEntities,
|
||||
_liveEntityPresentation!,
|
||||
_entityEffects!,
|
||||
_remoteTeleportController!,
|
||||
_selectionInteractions,
|
||||
_selection,
|
||||
_animatedEntities,
|
||||
_remoteMovementObservations,
|
||||
_translucencyFades,
|
||||
_liveEntityProjectionWithdrawal!,
|
||||
_equippedChildRenderer!,
|
||||
_physicsEngine.ShadowObjects,
|
||||
_liveEntityLights!,
|
||||
_classificationCache,
|
||||
() => _playerServerGuid);
|
||||
liveEntityComponentLifecycle.Bind(liveEntityTeardown);
|
||||
_liveEntityHydration = new AcDream.App.World.LiveEntityHydrationController(
|
||||
_liveEntities,
|
||||
Objects,
|
||||
|
|
@ -2579,9 +2590,14 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
_liveEntityPresentation!),
|
||||
originCoordinator,
|
||||
networkUpdateBridge,
|
||||
liveEntityTeardown,
|
||||
PublishLocalPhysicsTimestamps,
|
||||
() => _playerServerGuid,
|
||||
_options.DumpLiveSpawns ? Console.WriteLine : null);
|
||||
_liveEntityLiveness = new AcDream.App.World.LiveEntityLivenessController(
|
||||
_liveEntities,
|
||||
() => _playerServerGuid,
|
||||
candidate => _liveEntityHydration.OnPrune(candidate));
|
||||
parentAcceptance!.Bind(_liveEntityHydration.TryAcceptParentForProjection);
|
||||
networkUpdateBridge.Bind(
|
||||
new AcDream.App.World.DelegateLiveEntityNetworkUpdateSink(
|
||||
|
|
@ -2691,7 +2707,7 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
NetworkEffects = () => _entityEffects?.ClearNetworkState(),
|
||||
AnimationHookFrames = () => _animationHookFrames?.Clear(),
|
||||
LivePresentation = () => _liveEntityPresentation?.Clear(),
|
||||
RemoteMovementDiagnostics = _remoteLastMove.Clear,
|
||||
RemoteMovementDiagnostics = _remoteMovementObservations.Clear,
|
||||
});
|
||||
|
||||
private void ResetSessionMouseCapture()
|
||||
|
|
@ -2831,7 +2847,8 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
spawn,
|
||||
static (hydration, value) => hydration.OnCreate(value)),
|
||||
Deleted: deletion => _inboundEntityEvents.Run(
|
||||
this, deletion, static (window, value) => window.OnLiveEntityDeleted(value)),
|
||||
_liveEntityHydration!, deletion,
|
||||
static (hydration, value) => hydration.OnDelete(value)),
|
||||
PickedUp: pickup => _inboundEntityEvents.Run(
|
||||
_liveEntityHydration!, pickup,
|
||||
static (hydration, value) => hydration.OnPickup(value)),
|
||||
|
|
@ -3015,41 +3032,6 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
block.Height, heightTable, landblockX, landblockY, localX, localY);
|
||||
}
|
||||
|
||||
private void OnLiveEntityDeleted(AcDream.Core.Net.Messages.DeleteObject.Parsed delete)
|
||||
{
|
||||
// A Delete can arrive before CreateObject, in which case no instance
|
||||
// timestamp owner exists and its mixed pending effect FIFO must be
|
||||
// discarded directly. For a known record, cleanup belongs after the
|
||||
// generation gate: a stale Delete must not cancel the current owner.
|
||||
if (_liveEntities?.TryGetRecord(delete.Guid, out _) != true)
|
||||
_entityEffects?.ForgetUnknownOwner(delete.Guid);
|
||||
bool removed = _liveEntities!.UnregisterLiveEntity(
|
||||
delete,
|
||||
isLocalPlayer: delete.Guid == _playerServerGuid,
|
||||
beforeTeardown: () =>
|
||||
{
|
||||
AcDream.Core.Net.ObjectTableWiring.ApplyEntityDelete(Objects, delete);
|
||||
});
|
||||
|
||||
if (removed
|
||||
&& Environment.GetEnvironmentVariable("ACDREAM_DUMP_MOTION") == "1")
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"live: delete guid=0x{delete.Guid:X8} instSeq={delete.InstanceSequence}");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnLiveEntityPruned(AcDream.App.World.LiveEntityPruneCandidate candidate)
|
||||
{
|
||||
// ACE retains KnownObjects across normal teleports and consequently
|
||||
// does not issue an F747 when an old region leaves client visibility.
|
||||
// Route the 25-second client liveness expiry through the exact same
|
||||
// generation gate and symmetric teardown as a wire ObjectDelete.
|
||||
OnLiveEntityDeleted(new AcDream.Core.Net.Messages.DeleteObject.Parsed(
|
||||
candidate.ServerGuid,
|
||||
candidate.Generation));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rebuilds the paperdoll doll from the live player entity (retail makeObject(player) +
|
||||
/// DoObjDescChangesFromDefault). Clones the player's already-resolved Setup / MeshRefs / palette /
|
||||
|
|
@ -3556,105 +3538,6 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
hiddenEntityHost.NotifyHidden();
|
||||
}
|
||||
|
||||
private void TearDownLiveEntityRuntimeComponents(LiveEntityRecord record)
|
||||
{
|
||||
record.RuntimeComponentTeardownPlan ??= CreateLiveEntityRuntimeTeardownPlan(record);
|
||||
record.RuntimeComponentTeardownPlan.Advance();
|
||||
}
|
||||
|
||||
private AcDream.App.World.LiveEntityTeardownPlan CreateLiveEntityRuntimeTeardownPlan(
|
||||
LiveEntityRecord record)
|
||||
{
|
||||
uint serverGuid = record.ServerGuid;
|
||||
var incarnation = new AcDream.App.World.LiveEntityIncarnationCleanup(
|
||||
record,
|
||||
guid => _liveEntities?.TryGetRecord(guid, out LiveEntityRecord current) == true
|
||||
? current
|
||||
: null);
|
||||
var cleanups = new List<Action>
|
||||
{
|
||||
() => _liveEntityPresentation?.Forget(record),
|
||||
() => _entityEffects?.OnLiveEntityUnregistered(record),
|
||||
() => _remoteTeleportController?.Forget(record),
|
||||
() =>
|
||||
{
|
||||
bool replacementExists =
|
||||
_liveEntities?.TryGetRecord(serverGuid, out LiveEntityRecord current) == true
|
||||
&& !ReferenceEquals(current, record);
|
||||
if (_selectionInteractions is { } interactions)
|
||||
{
|
||||
interactions.OnEntityRemoved(record, replacementExists);
|
||||
}
|
||||
else if (!replacementExists && _selection.SelectedObjectId == serverGuid)
|
||||
{
|
||||
_selection.Clear(
|
||||
AcDream.Core.Selection.SelectionChangeSource.System,
|
||||
AcDream.Core.Selection.SelectionChangeReason.SelectedObjectRemoved);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
if (record.WorldEntity is not { } existingEntity)
|
||||
{
|
||||
return new AcDream.App.World.LiveEntityTeardownPlan(cleanups);
|
||||
}
|
||||
|
||||
// R2-Q5 + R3-W2: retail runs BOTH layers' exit-world drains
|
||||
// independently (r3-port-plan §4): the manager's (each pending
|
||||
// animation fires MotionDone(success:false) → the bound interp pops
|
||||
// in step) THEN the interp's own (flushes any remainder —
|
||||
// MovementManager::HandleExitWorld 0x00524350, the R5-V5 facade
|
||||
// relay: interp ONLY → CMotionInterp::HandleExitWorld 0x00527f30).
|
||||
if (_animatedEntities.TryGetValue(existingEntity.Id, out var aeGone))
|
||||
cleanups.Add(() => aeGone.Sequencer?.Manager.HandleExitWorld());
|
||||
RemoteMotion? rmGone = record.RemoteMotionRuntime as RemoteMotion;
|
||||
if (rmGone is not null)
|
||||
cleanups.Add(rmGone.Movement.HandleExitWorld);
|
||||
// R5-V2: retail CPhysicsObj::exit_world tells every voyeur of this
|
||||
// entity that it left the world (TargetManager::NotifyVoyeurOfEvent
|
||||
// ExitWorld) — a watcher moving-to/stuck-to this entity drops the
|
||||
// moveto/stick. This is the ONLY clean-up for a watcher whose target
|
||||
// already sent an Ok (once status != Undefined the 10 s staleness
|
||||
// timeout never fires), so it must run before the host is pruned.
|
||||
EntityPhysicsHost? ephGone = record.PhysicsHost as EntityPhysicsHost;
|
||||
if (ephGone is not null)
|
||||
{
|
||||
// R5-V3 (#171): retail exit_world (0x00514e60) order — the
|
||||
// departing entity first tears down its OWN stick
|
||||
// (PositionManager::UnStick @0x00514e88 — drops its voyeur
|
||||
// subscription on whatever it was stuck to) and its own target
|
||||
// subscription (TargetManager::ClearTarget @0x00514e97 — same
|
||||
// for a plain mid-chase moveto), THEN NotifyVoyeurOfEvent
|
||||
// (ExitWorld) tells the entities watching IT. Without the first
|
||||
// two, a despawning attacker leaves a dead voyeur entry on its
|
||||
// target until send-failure pruning.
|
||||
cleanups.Add(ephGone.PositionManager.UnStick);
|
||||
cleanups.Add(ephGone.ClearTarget);
|
||||
cleanups.Add(ephGone.NotifyExitWorld);
|
||||
}
|
||||
cleanups.Add(() => _animatedEntities.Remove(existingEntity.Id));
|
||||
cleanups.Add(() => _classificationCache.InvalidateEntity(existingEntity.Id));
|
||||
|
||||
// Dead-reckon state is keyed by SERVER guid (not local id) so we
|
||||
// clear using the same guid the next spawn/update would use.
|
||||
// LiveEntityRuntime removes the canonical remote-motion index by
|
||||
// captured reference after this App teardown plan converges. Never use
|
||||
// the GUID-only dictionary view here: it may already expose a newer
|
||||
// incarnation.
|
||||
cleanups.Add(() => incarnation.RunIfNoReplacement(
|
||||
() => _remoteLastMove.Remove(serverGuid)));
|
||||
cleanups.Add(() => _translucencyFades.ClearEntity(existingEntity.Id)); // #188
|
||||
cleanups.Add(() => _liveEntityProjectionWithdrawal!.LeaveWorld(
|
||||
record,
|
||||
_playerServerGuid));
|
||||
cleanups.Add(() => _equippedChildRenderer?.OnLogicalTeardown(record));
|
||||
// Logical teardown ends the retained shadow payload too. The weaker
|
||||
// pickup/parent path stops after Suspend so re-entry can restore it.
|
||||
cleanups.Add(() => _physicsEngine.ShadowObjects.Deregister(existingEntity.Id));
|
||||
cleanups.Add(() => _liveEntityLights?.Forget(existingEntity.Id));
|
||||
return new AcDream.App.World.LiveEntityTeardownPlan(cleanups);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// R4-V5: shared retail <c>unpack_movement</c> type-6..9 routing
|
||||
/// (<c>0x00524440</c> cases 6/7/8/9, decomp §2f) — one body for remotes
|
||||
|
|
@ -4149,9 +4032,9 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
}
|
||||
|
||||
// CRITICAL: when we enter a locomotion cycle (Walk/Run/etc),
|
||||
// stamp the _remoteLastMove timestamp to "now". Without this,
|
||||
// stamp the remote observation timestamp to "now". Without this,
|
||||
// the stop-detection loop in TickAnimations sees the previous
|
||||
// _remoteLastMove timestamp (set by the last UpdatePosition,
|
||||
// observation timestamp (set by the last UpdatePosition,
|
||||
// often >300ms ago during idle) and fires the stop signal
|
||||
// IMMEDIATELY — flipping the sequencer straight back to Ready.
|
||||
// The visible symptom was "remote char never animates; just
|
||||
|
|
@ -4172,8 +4055,8 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
// window from this transition. Without this, the entity
|
||||
// starts its run animation and is instantly interrupted.
|
||||
var refreshedTime = System.DateTime.UtcNow;
|
||||
if (_remoteLastMove.TryGetValue(update.Guid, out var prev))
|
||||
_remoteLastMove[update.Guid] = (prev.Pos, refreshedTime);
|
||||
if (_remoteMovementObservations.TryGetValue(update.Guid, out var prev))
|
||||
_remoteMovementObservations[update.Guid] = (prev.Pos, refreshedTime);
|
||||
if (_remoteDeadReckon.TryGetValue(update.Guid, out var dr))
|
||||
dr.LastServerPosTime = (refreshedTime - System.DateTime.UnixEpoch).TotalSeconds;
|
||||
}
|
||||
|
|
@ -4936,16 +4819,16 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
if (update.Guid != _playerServerGuid)
|
||||
{
|
||||
var now = System.DateTime.UtcNow;
|
||||
if (_remoteLastMove.TryGetValue(update.Guid, out var prev))
|
||||
if (_remoteMovementObservations.TryGetValue(update.Guid, out var prev))
|
||||
{
|
||||
float moveDist = System.Numerics.Vector3.Distance(prev.Pos, worldPos);
|
||||
if (moveDist > 0.05f)
|
||||
_remoteLastMove[update.Guid] = (worldPos, now);
|
||||
_remoteMovementObservations[update.Guid] = (worldPos, now);
|
||||
// else: leave old entry so "Time" = last real movement time
|
||||
}
|
||||
else
|
||||
{
|
||||
_remoteLastMove[update.Guid] = (worldPos, now);
|
||||
_remoteMovementObservations[update.Guid] = (worldPos, now);
|
||||
}
|
||||
|
||||
// Retail-faithful hard-snap on UpdatePosition.
|
||||
|
|
@ -7519,7 +7402,7 @@ public sealed class GameWindow : IDisposable, ILiveAnimationPresentationContext
|
|||
// after that callback stack has unwound on the same update thread.
|
||||
try
|
||||
{
|
||||
_liveEntities?.RetryPendingTeardowns();
|
||||
_liveEntityHydration?.RetryPendingTeardowns();
|
||||
}
|
||||
catch (AggregateException error)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue