refactor(runtime): publish canonical entity object deltas
Issue stable Runtime identities at canonical registration, publish entity and inventory commits through one generation-stamped synchronous stream, and make graphical adapters borrow the same direct views and events as a no-window host. Preserve exact projection teardown and retail mutation order while removing App-side event reconstruction. Make the hard-recenter ordering fixture independent of the production two-millisecond frame budget so its injected-failure gate is deterministic. Co-authored-by: Codex <codex@openai.com>
This commit is contained in:
parent
d3e96ff912
commit
ce3ac310d9
23 changed files with 2352 additions and 666 deletions
|
|
@ -891,7 +891,7 @@ internal sealed class SessionPlayerCompositionPhase
|
|||
liveSessionCommands,
|
||||
d.PlayerIdentity,
|
||||
live.LiveEntities,
|
||||
d.EntityObjects.Objects,
|
||||
d.EntityObjects,
|
||||
d.Chat,
|
||||
d.PlayerController,
|
||||
worldReveal,
|
||||
|
|
|
|||
|
|
@ -1590,6 +1590,7 @@ public sealed class GameWindow :
|
|||
_streamer,
|
||||
_equippedChildRenderer,
|
||||
_liveEntities,
|
||||
_runtimeEntityObjects,
|
||||
_renderSceneShadow,
|
||||
_livePresentationBindings,
|
||||
_entityEffectAdvance,
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ using AcDream.App.World;
|
|||
using AcDream.Content;
|
||||
using AcDream.Core.Audio;
|
||||
using AcDream.Core.Physics;
|
||||
using AcDream.Runtime.Entities;
|
||||
using AcDream.Runtime.Session;
|
||||
using AcDream.UI.Abstractions.Input;
|
||||
using DatReaderWriter;
|
||||
|
|
@ -81,6 +82,7 @@ internal sealed record LiveShutdownRoots(
|
|||
LandblockStreamer? Streamer,
|
||||
EquippedChildRenderController? EquippedChildren,
|
||||
LiveEntityRuntime? LiveEntities,
|
||||
RuntimeEntityObjectLifetime EntityObjects,
|
||||
RenderSceneShadowRuntime? RenderSceneShadow,
|
||||
LivePresentationRuntimeBindings? PresentationBindings,
|
||||
DeferredEntityEffectAdvanceSource EffectAdvance,
|
||||
|
|
@ -384,6 +386,12 @@ internal static class GameWindowShutdownManifest
|
|||
"shadow render scene",
|
||||
() => live.RenderSceneShadow?.Dispose()),
|
||||
]),
|
||||
new ResourceShutdownStage("runtime entity/object stream",
|
||||
[
|
||||
Hard(
|
||||
"runtime entity/object lifetime",
|
||||
live.EntityObjects.Dispose),
|
||||
]),
|
||||
new ResourceShutdownStage("effect dispatch edges",
|
||||
[
|
||||
Hard("live-presentation bindings", () => live.PresentationBindings?.Dispose()),
|
||||
|
|
|
|||
|
|
@ -5,9 +5,9 @@ using AcDream.App.Net;
|
|||
using AcDream.App.Streaming;
|
||||
using AcDream.App.World;
|
||||
using AcDream.Core.Chat;
|
||||
using AcDream.Core.Items;
|
||||
using AcDream.Core.Selection;
|
||||
using AcDream.Runtime;
|
||||
using AcDream.Runtime.Entities;
|
||||
using AcDream.Runtime.Session;
|
||||
using AcDream.UI.Abstractions;
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ internal sealed class CurrentGameRuntimeAdapter
|
|||
ICommandBus commands,
|
||||
LocalPlayerIdentityState playerIdentity,
|
||||
LiveEntityRuntime entities,
|
||||
ClientObjectTable objects,
|
||||
RuntimeEntityObjectLifetime entityObjects,
|
||||
ChatLog chat,
|
||||
ILocalPlayerControllerSource playerController,
|
||||
WorldRevealCoordinator worldReveal,
|
||||
|
|
@ -49,17 +49,18 @@ internal sealed class CurrentGameRuntimeAdapter
|
|||
session,
|
||||
playerIdentity,
|
||||
entities,
|
||||
objects,
|
||||
entityObjects,
|
||||
chat,
|
||||
playerController,
|
||||
worldReveal,
|
||||
clock);
|
||||
entityObjects.BindEventContext(
|
||||
() => _view.Generation,
|
||||
() => clock.FrameNumber);
|
||||
_events = new CurrentGameRuntimeEventAdapter(
|
||||
_view,
|
||||
entities,
|
||||
objects,
|
||||
chat,
|
||||
clock);
|
||||
entityObjects,
|
||||
chat);
|
||||
_commands = new CurrentGameRuntimeCommandAdapter(
|
||||
session,
|
||||
sessionHost,
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
using AcDream.App.World;
|
||||
using AcDream.Core.Chat;
|
||||
using AcDream.Core.Items;
|
||||
using AcDream.Core.Physics;
|
||||
using AcDream.Runtime;
|
||||
using AcDream.Runtime.Entities;
|
||||
|
||||
namespace AcDream.App.Runtime;
|
||||
|
||||
|
|
@ -26,31 +24,30 @@ internal interface ICurrentGameRuntimeEventSink
|
|||
internal sealed class CurrentGameRuntimeEventAdapter
|
||||
: IRuntimeEventSource,
|
||||
ICurrentGameRuntimeEventSink,
|
||||
IRuntimeEntityObjectObserver,
|
||||
IDisposable
|
||||
{
|
||||
private readonly CurrentGameRuntimeViewAdapter _view;
|
||||
private readonly LiveEntityRuntime _entities;
|
||||
private readonly ClientObjectTable _objects;
|
||||
private readonly RuntimeEntityObjectLifetime _entityObjects;
|
||||
private readonly ChatLog _chat;
|
||||
private readonly IGameRuntimeClock _clock;
|
||||
private readonly RuntimeEventSequencer _events = new();
|
||||
private readonly object _observerGate = new();
|
||||
private IRuntimeEventObserver[] _observers = [];
|
||||
private IDisposable? _entityObjectSubscription;
|
||||
private bool _ownerEventsAttached;
|
||||
private bool _disposed;
|
||||
|
||||
internal long DispatchFailureCount { get; private set; }
|
||||
internal Exception? LastDispatchFailure { get; private set; }
|
||||
|
||||
public CurrentGameRuntimeEventAdapter(
|
||||
CurrentGameRuntimeViewAdapter view,
|
||||
LiveEntityRuntime entities,
|
||||
ClientObjectTable objects,
|
||||
ChatLog chat,
|
||||
IGameRuntimeClock clock)
|
||||
RuntimeEntityObjectLifetime entityObjects,
|
||||
ChatLog chat)
|
||||
{
|
||||
_view = view ?? throw new ArgumentNullException(nameof(view));
|
||||
_entities = entities ?? throw new ArgumentNullException(nameof(entities));
|
||||
_objects = objects ?? throw new ArgumentNullException(nameof(objects));
|
||||
_entityObjects = entityObjects
|
||||
?? throw new ArgumentNullException(nameof(entityObjects));
|
||||
_chat = chat ?? throw new ArgumentNullException(nameof(chat));
|
||||
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
|
||||
}
|
||||
|
||||
public IDisposable Subscribe(IRuntimeEventObserver observer)
|
||||
|
|
@ -106,7 +103,16 @@ internal sealed class CurrentGameRuntimeEventAdapter
|
|||
primaryObjectId,
|
||||
text);
|
||||
foreach (IRuntimeEventObserver observer in observers)
|
||||
observer.OnCommand(in delta);
|
||||
{
|
||||
try
|
||||
{
|
||||
observer.OnCommand(in delta);
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
RecordDispatchFailure(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void EmitLifecycle(RuntimeLifecycleState previous)
|
||||
|
|
@ -123,7 +129,16 @@ internal sealed class CurrentGameRuntimeEventAdapter
|
|||
previous,
|
||||
current);
|
||||
foreach (IRuntimeEventObserver observer in observers)
|
||||
observer.OnLifecycle(in delta);
|
||||
{
|
||||
try
|
||||
{
|
||||
observer.OnLifecycle(in delta);
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
RecordDispatchFailure(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Unsubscribe(IRuntimeEventObserver observer)
|
||||
|
|
@ -159,12 +174,7 @@ internal sealed class CurrentGameRuntimeEventAdapter
|
|||
|
||||
private void AttachOwnerEvents()
|
||||
{
|
||||
_entities.ProjectionVisibilityChanged += OnEntityVisibilityChanged;
|
||||
_objects.ObjectAdded += OnObjectAdded;
|
||||
_objects.ObjectUpdated += OnObjectUpdated;
|
||||
_objects.ObjectMoved += OnObjectMoved;
|
||||
_objects.ObjectRemovalClassified += OnObjectRemoved;
|
||||
_objects.Cleared += OnObjectsCleared;
|
||||
_entityObjectSubscription = _entityObjects.Events.Subscribe(this);
|
||||
_chat.EntryAppended += OnChatEntry;
|
||||
_ownerEventsAttached = true;
|
||||
}
|
||||
|
|
@ -174,79 +184,43 @@ internal sealed class CurrentGameRuntimeEventAdapter
|
|||
if (!_ownerEventsAttached)
|
||||
return;
|
||||
_chat.EntryAppended -= OnChatEntry;
|
||||
_objects.Cleared -= OnObjectsCleared;
|
||||
_objects.ObjectRemovalClassified -= OnObjectRemoved;
|
||||
_objects.ObjectMoved -= OnObjectMoved;
|
||||
_objects.ObjectUpdated -= OnObjectUpdated;
|
||||
_objects.ObjectAdded -= OnObjectAdded;
|
||||
_entities.ProjectionVisibilityChanged -= OnEntityVisibilityChanged;
|
||||
_entityObjectSubscription?.Dispose();
|
||||
_entityObjectSubscription = null;
|
||||
_ownerEventsAttached = false;
|
||||
}
|
||||
|
||||
private void OnEntityVisibilityChanged(LiveEntityRecord record, bool visible)
|
||||
public void OnEntity(in RuntimeEntityDelta delta)
|
||||
{
|
||||
if (!TryObservers(out IRuntimeEventObserver[] observers))
|
||||
return;
|
||||
RuntimeEntityChange change = visible
|
||||
? RuntimeEntityChange.Updated
|
||||
: (record.FinalPhysicsState & PhysicsStateFlags.Hidden) != 0
|
||||
? RuntimeEntityChange.Hidden
|
||||
: RuntimeEntityChange.Withdrawn;
|
||||
var delta = new RuntimeEntityDelta(
|
||||
NextStamp(),
|
||||
change,
|
||||
CurrentGameRuntimeViewAdapter.Snapshot(record));
|
||||
foreach (IRuntimeEventObserver observer in observers)
|
||||
observer.OnEntity(in delta);
|
||||
{
|
||||
try
|
||||
{
|
||||
observer.OnEntity(in delta);
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
RecordDispatchFailure(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnObjectAdded(ClientObject item) =>
|
||||
EmitInventory(RuntimeInventoryChange.Added, item);
|
||||
|
||||
private void OnObjectUpdated(ClientObject item) =>
|
||||
EmitInventory(RuntimeInventoryChange.Updated, item);
|
||||
|
||||
private void OnObjectMoved(ClientObjectMove move)
|
||||
{
|
||||
ClientObject? item = move.Item ?? _objects.Get(move.ItemId);
|
||||
if (item is not null)
|
||||
EmitInventory(RuntimeInventoryChange.Moved, item);
|
||||
}
|
||||
|
||||
private void OnObjectRemoved(ClientObjectRemoval removal) =>
|
||||
EmitInventory(
|
||||
RuntimeInventoryChange.Removed,
|
||||
removal.Object,
|
||||
removal.Generation);
|
||||
|
||||
private void OnObjectsCleared()
|
||||
public void OnInventory(in RuntimeInventoryDelta delta)
|
||||
{
|
||||
if (!TryObservers(out IRuntimeEventObserver[] observers))
|
||||
return;
|
||||
var delta = new RuntimeInventoryDelta(
|
||||
NextStamp(),
|
||||
RuntimeInventoryChange.Cleared,
|
||||
default);
|
||||
foreach (IRuntimeEventObserver observer in observers)
|
||||
observer.OnInventory(in delta);
|
||||
}
|
||||
|
||||
private void EmitInventory(
|
||||
RuntimeInventoryChange change,
|
||||
ClientObject item,
|
||||
ushort? exactGeneration = null)
|
||||
{
|
||||
if (!TryObservers(out IRuntimeEventObserver[] observers))
|
||||
return;
|
||||
var delta = new RuntimeInventoryDelta(
|
||||
NextStamp(),
|
||||
change,
|
||||
CurrentGameRuntimeViewAdapter.Snapshot(
|
||||
item,
|
||||
_entities,
|
||||
exactGeneration));
|
||||
foreach (IRuntimeEventObserver observer in observers)
|
||||
observer.OnInventory(in delta);
|
||||
{
|
||||
try
|
||||
{
|
||||
observer.OnInventory(in delta);
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
RecordDispatchFailure(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnChatEntry(ChatEntry entry)
|
||||
|
|
@ -263,11 +237,20 @@ internal sealed class CurrentGameRuntimeEventAdapter
|
|||
entry.Text,
|
||||
entry.ChannelName));
|
||||
foreach (IRuntimeEventObserver observer in observers)
|
||||
observer.OnChat(in delta);
|
||||
{
|
||||
try
|
||||
{
|
||||
observer.OnChat(in delta);
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
RecordDispatchFailure(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private RuntimeEventStamp NextStamp() =>
|
||||
_events.Next(_view.Generation, _clock.FrameNumber);
|
||||
_entityObjects.Events.NextStamp();
|
||||
|
||||
private bool TryObservers(out IRuntimeEventObserver[] observers)
|
||||
{
|
||||
|
|
@ -275,6 +258,12 @@ internal sealed class CurrentGameRuntimeEventAdapter
|
|||
return observers.Length != 0;
|
||||
}
|
||||
|
||||
private void RecordDispatchFailure(Exception error)
|
||||
{
|
||||
DispatchFailureCount++;
|
||||
LastDispatchFailure = error;
|
||||
}
|
||||
|
||||
private sealed class ObserverSubscription(
|
||||
CurrentGameRuntimeEventAdapter owner,
|
||||
IRuntimeEventObserver observer)
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ using AcDream.App.World;
|
|||
using AcDream.Core.Chat;
|
||||
using AcDream.Core.Items;
|
||||
using AcDream.Runtime;
|
||||
using AcDream.Runtime.Entities;
|
||||
using AcDream.Runtime.Session;
|
||||
|
||||
namespace AcDream.App.Runtime;
|
||||
|
|
@ -21,8 +22,8 @@ internal sealed class CurrentGameRuntimeViewAdapter : IGameRuntimeView
|
|||
private readonly ClientObjectTable _objects;
|
||||
private readonly ChatLog _chat;
|
||||
private readonly IGameRuntimeClock _clock;
|
||||
private readonly EntityView _entityView;
|
||||
private readonly InventoryView _inventoryView;
|
||||
private readonly IRuntimeEntityView _entityView;
|
||||
private readonly IRuntimeInventoryView _inventoryView;
|
||||
private readonly ChatView _chatView;
|
||||
private readonly MovementView _movementView;
|
||||
private readonly PortalView _portalView;
|
||||
|
|
@ -32,7 +33,7 @@ internal sealed class CurrentGameRuntimeViewAdapter : IGameRuntimeView
|
|||
LiveSessionController session,
|
||||
LocalPlayerIdentityState playerIdentity,
|
||||
LiveEntityRuntime entities,
|
||||
ClientObjectTable objects,
|
||||
RuntimeEntityObjectLifetime entityObjects,
|
||||
ChatLog chat,
|
||||
ILocalPlayerControllerSource playerController,
|
||||
WorldRevealCoordinator worldReveal,
|
||||
|
|
@ -42,12 +43,13 @@ internal sealed class CurrentGameRuntimeViewAdapter : IGameRuntimeView
|
|||
_playerIdentity = playerIdentity
|
||||
?? throw new ArgumentNullException(nameof(playerIdentity));
|
||||
_entities = entities ?? throw new ArgumentNullException(nameof(entities));
|
||||
_objects = objects ?? throw new ArgumentNullException(nameof(objects));
|
||||
ArgumentNullException.ThrowIfNull(entityObjects);
|
||||
_objects = entityObjects.Objects;
|
||||
_chat = chat ?? throw new ArgumentNullException(nameof(chat));
|
||||
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
|
||||
|
||||
_entityView = new EntityView(_entities);
|
||||
_inventoryView = new InventoryView(_objects, _entities);
|
||||
_entityView = entityObjects.EntityView;
|
||||
_inventoryView = entityObjects.InventoryView;
|
||||
_chatView = new ChatView(_chat);
|
||||
_movementView = new MovementView(
|
||||
playerController
|
||||
|
|
@ -108,103 +110,6 @@ internal sealed class CurrentGameRuntimeViewAdapter : IGameRuntimeView
|
|||
|
||||
internal void Deactivate() => _active = false;
|
||||
|
||||
internal static RuntimeEntitySnapshot Snapshot(LiveEntityRecord record) =>
|
||||
new(
|
||||
new RuntimeEntityIdentity(
|
||||
record.ServerGuid,
|
||||
record.LocalEntityId ?? 0u,
|
||||
record.Generation),
|
||||
record.FullCellId,
|
||||
(uint)record.FinalPhysicsState,
|
||||
record.PhysicsBody?.CellPosition,
|
||||
record.LocalEntityId.HasValue,
|
||||
record.IsSpatiallyVisible,
|
||||
record.InitialHydrationCompleted);
|
||||
|
||||
internal static RuntimeInventoryItemSnapshot Snapshot(
|
||||
ClientObject item,
|
||||
LiveEntityRuntime entities,
|
||||
ushort? exactGeneration = null)
|
||||
{
|
||||
ushort generation = exactGeneration
|
||||
?? (entities.TryGetRecord(item.ObjectId, out LiveEntityRecord record)
|
||||
? record.Generation
|
||||
: (ushort)0);
|
||||
return new RuntimeInventoryItemSnapshot(
|
||||
item.ObjectId,
|
||||
generation,
|
||||
item.Name,
|
||||
item.ContainerId,
|
||||
item.ContainerSlot,
|
||||
item.WielderId,
|
||||
(uint)item.CurrentlyEquippedLocation,
|
||||
item.StackSize,
|
||||
item.Value);
|
||||
}
|
||||
|
||||
private sealed class EntityView(LiveEntityRuntime owner)
|
||||
: IRuntimeEntityView
|
||||
{
|
||||
public int Count => owner.Count;
|
||||
public int MaterializedCount => owner.MaterializedCount;
|
||||
|
||||
public bool TryGet(uint serverGuid, out RuntimeEntitySnapshot entity)
|
||||
{
|
||||
if (owner.TryGetRecord(serverGuid, out LiveEntityRecord record))
|
||||
{
|
||||
entity = Snapshot(record);
|
||||
return true;
|
||||
}
|
||||
|
||||
entity = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Visit(IRuntimeEntityVisitor visitor)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(visitor);
|
||||
foreach (LiveEntityRecord record in owner.Records)
|
||||
{
|
||||
RuntimeEntitySnapshot entity = Snapshot(record);
|
||||
visitor.Visit(in entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class InventoryView(
|
||||
ClientObjectTable owner,
|
||||
LiveEntityRuntime entities)
|
||||
: IRuntimeInventoryView
|
||||
{
|
||||
public int ObjectCount => owner.ObjectCount;
|
||||
public int ContainerCount => owner.ContainerCount;
|
||||
|
||||
public bool TryGet(
|
||||
uint objectId,
|
||||
out RuntimeInventoryItemSnapshot item)
|
||||
{
|
||||
ClientObject? found = owner.Get(objectId);
|
||||
if (found is not null)
|
||||
{
|
||||
item = Snapshot(found, entities);
|
||||
return true;
|
||||
}
|
||||
|
||||
item = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Visit(IRuntimeInventoryVisitor visitor)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(visitor);
|
||||
foreach (ClientObject item in owner.Objects)
|
||||
{
|
||||
RuntimeInventoryItemSnapshot snapshot = Snapshot(item, entities);
|
||||
visitor.Visit(in snapshot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class ChatView(ChatLog owner) : IRuntimeChatView
|
||||
{
|
||||
public long Revision => owner.Revision;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ using AcDream.Core.Net;
|
|||
using AcDream.Core.Net.Messages;
|
||||
using AcDream.Core.Physics;
|
||||
using AcDream.Core.World;
|
||||
using AcDream.Runtime;
|
||||
using AcDream.Runtime.Entities;
|
||||
using System.Numerics;
|
||||
using System.Runtime.ExceptionServices;
|
||||
|
|
@ -377,29 +378,13 @@ public sealed class LiveEntityRecord
|
|||
internal RetailPhysicsStateTransition ApplyRawPhysicsState(uint rawState) =>
|
||||
_directory.ApplyRawPhysicsState(Canonical, rawState);
|
||||
|
||||
internal void AdvancePositionAuthority() =>
|
||||
_directory.AdvancePositionAuthority(Canonical);
|
||||
|
||||
internal void AdvanceVectorAuthority() =>
|
||||
_directory.AdvanceVectorAuthority(Canonical);
|
||||
|
||||
internal void AdvanceMovementAuthority() =>
|
||||
_directory.AdvanceMovementAuthority(Canonical);
|
||||
|
||||
internal void AdvanceObjDescAuthority()
|
||||
internal void NoteObjDescProjectionSynchronization()
|
||||
{
|
||||
_directory.AdvanceObjDescAuthority(Canonical);
|
||||
AppearanceProjectionSynchronizationPending = true;
|
||||
if (AppearanceHydrationInProgress)
|
||||
AppearanceHydrationRetryRequested = true;
|
||||
}
|
||||
|
||||
internal void AdvanceCreateAuthority() =>
|
||||
_directory.AdvanceCreateAuthority(Canonical);
|
||||
|
||||
internal void SetChildNoDraw(bool noDraw) =>
|
||||
_directory.SetChildNoDraw(Canonical, noDraw);
|
||||
|
||||
/// <summary>
|
||||
/// Cell used when a streamed landblock asks live objects to restore their
|
||||
/// spatial projection. Once materialized, the canonical runtime cell wins;
|
||||
|
|
@ -582,150 +567,26 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
: "A live entity cannot register from inside atomic resource registration.");
|
||||
}
|
||||
|
||||
InboundCreateResult result = _directory.AcceptCreate(incoming);
|
||||
if (result.Disposition is CreateObjectTimestampDisposition.StaleGeneration)
|
||||
return new LiveEntityRegistrationResult(
|
||||
result,
|
||||
Canonical: null,
|
||||
Projection: null,
|
||||
LogicalRegistrationCreated: false,
|
||||
ReplacedExistingGeneration: false);
|
||||
ulong sessionVersion = _directory.SessionLifetimeVersion;
|
||||
ulong operationVersion = AdvanceLifetimeMutation(incoming.Guid);
|
||||
|
||||
if (result.Disposition is CreateObjectTimestampDisposition.ExistingGeneration)
|
||||
RuntimeEntityRegistrationResult registration =
|
||||
_entityObjects.RegisterEntity(
|
||||
incoming,
|
||||
RetirePriorProjection);
|
||||
RuntimeEntityRecord? canonical = registration.Canonical;
|
||||
LiveEntityRecord? projection = canonical is null
|
||||
? null
|
||||
: _projections.GetCurrentOrDefault(canonical.ServerGuid);
|
||||
if (projection is not null
|
||||
&& ReferenceEquals(projection.Canonical, canonical))
|
||||
{
|
||||
if (_directory.TryGetActive(
|
||||
incoming.Guid,
|
||||
out RuntimeEntityRecord retainedCanonical))
|
||||
{
|
||||
_directory.RefreshSnapshot(
|
||||
retainedCanonical,
|
||||
result.Snapshot,
|
||||
refreshPosition: true);
|
||||
_directory.AdvanceCreateAuthority(retainedCanonical);
|
||||
_projections.TryGet(
|
||||
retainedCanonical,
|
||||
out LiveEntityRecord? retainedProjection);
|
||||
if (retainedProjection is not null)
|
||||
RefreshSpatialRuntimeIndexes(retainedProjection);
|
||||
return new LiveEntityRegistrationResult(
|
||||
result,
|
||||
retainedCanonical,
|
||||
retainedProjection,
|
||||
LogicalRegistrationCreated: false,
|
||||
ReplacedExistingGeneration: false);
|
||||
}
|
||||
|
||||
// A failed materialization rollback retains this exact incarnation
|
||||
// as a teardown tombstone. Do not create a second active record
|
||||
// with the same (GUID, INSTANCE_TS) while its partial resources are
|
||||
// still converging; a later retransmit can recover after teardown.
|
||||
if (_directory.TryGetTeardown(
|
||||
incoming.Guid,
|
||||
result.Snapshot.InstanceSequence,
|
||||
out _))
|
||||
{
|
||||
return new LiveEntityRegistrationResult(
|
||||
result,
|
||||
Canonical: null,
|
||||
Projection: null,
|
||||
false,
|
||||
false);
|
||||
}
|
||||
|
||||
// Defensive repair for a caller that cleared only the logical map.
|
||||
// Normal session teardown clears both owners together.
|
||||
RuntimeEntityRecord recoveredCanonical =
|
||||
_directory.AddActive(result.Snapshot);
|
||||
return new LiveEntityRegistrationResult(
|
||||
result,
|
||||
recoveredCanonical,
|
||||
Projection: null,
|
||||
LogicalRegistrationCreated: true,
|
||||
ReplacedExistingGeneration: false);
|
||||
RefreshSpatialRuntimeIndexes(projection);
|
||||
}
|
||||
|
||||
bool replaced = _directory.RemoveActive(
|
||||
incoming.Guid,
|
||||
out RuntimeEntityRecord? oldCanonical);
|
||||
LiveEntityRecord? old = null;
|
||||
if (oldCanonical is not null
|
||||
&& _projections.TryGet(oldCanonical, out LiveEntityRecord? projected))
|
||||
{
|
||||
old = projected;
|
||||
if (!_projections.RemoveActive(old))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Exact App projection for 0x{incoming.Guid:X8}/{old.Generation} could not be retired.");
|
||||
}
|
||||
}
|
||||
if (result.Disposition is CreateObjectTimestampDisposition.NewGeneration)
|
||||
ParentAttachments.EndGeneration(incoming.Guid, result.Snapshot.InstanceSequence);
|
||||
|
||||
Exception? tearDownFailure = null;
|
||||
if (old is not null)
|
||||
{
|
||||
RetainTeardownRecord(old);
|
||||
_logicalTeardownDepth++;
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
TearDownRecord(old);
|
||||
ReleaseTeardownRecord(old);
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
tearDownFailure = error;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_logicalTeardownDepth--;
|
||||
}
|
||||
}
|
||||
else if (oldCanonical is not null)
|
||||
{
|
||||
TearDownCanonicalOnly(oldCanonical);
|
||||
}
|
||||
|
||||
// Resource callbacks are arbitrary App integration code. Any nested
|
||||
// create, accepted delete, or session reset advances this epoch. The
|
||||
// outer packet is then superseded even when the nested action left no
|
||||
// record/snapshot (delete/reset); reinstalling it would resurrect an
|
||||
// incarnation after an accepted terminal event.
|
||||
if (_directory.SessionLifetimeVersion != sessionVersion
|
||||
|| CurrentLifetimeMutation(incoming.Guid) != operationVersion)
|
||||
{
|
||||
if (tearDownFailure is not null)
|
||||
{
|
||||
throw new AggregateException(
|
||||
$"Prior incarnation of live entity 0x{incoming.Guid:X8} failed teardown while its incoming replacement was superseded.",
|
||||
tearDownFailure);
|
||||
}
|
||||
|
||||
return new LiveEntityRegistrationResult(
|
||||
SupersededCreateResult(),
|
||||
_directory.TryGetActive(
|
||||
incoming.Guid,
|
||||
out RuntimeEntityRecord currentCanonical)
|
||||
? currentCanonical
|
||||
: null,
|
||||
_projections.GetCurrentOrDefault(incoming.Guid),
|
||||
false,
|
||||
replaced,
|
||||
tearDownFailure);
|
||||
}
|
||||
|
||||
RuntimeEntityRecord canonical = _directory.AddActive(result.Snapshot);
|
||||
return new LiveEntityRegistrationResult(
|
||||
result,
|
||||
registration.Inbound,
|
||||
canonical,
|
||||
Projection: null,
|
||||
true,
|
||||
replaced,
|
||||
tearDownFailure);
|
||||
projection,
|
||||
registration.LogicalRegistrationCreated,
|
||||
registration.ReplacedExistingGeneration,
|
||||
registration.PriorGenerationCleanupFailure);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -785,7 +646,9 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
bool createdSidecar = false;
|
||||
if (!_projections.TryGet(expectedCanonical, out record))
|
||||
{
|
||||
uint localId = _directory.ClaimLocalId(expectedCanonical);
|
||||
_ = expectedCanonical.LocalEntityId
|
||||
?? throw new InvalidOperationException(
|
||||
"Runtime must issue entity identity before App projection.");
|
||||
try
|
||||
{
|
||||
record = _projections.AddMaterializing(expectedCanonical);
|
||||
|
|
@ -800,7 +663,6 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
record.ProjectionKey = null;
|
||||
record = null;
|
||||
}
|
||||
_directory.ReleaseLocalId(expectedCanonical);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
|
@ -833,7 +695,6 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
{
|
||||
_projections.RemoveActive(record);
|
||||
record.ProjectionKey = null;
|
||||
_directory.ReleaseLocalId(expectedCanonical);
|
||||
record = null;
|
||||
}
|
||||
throw;
|
||||
|
|
@ -870,14 +731,13 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
{
|
||||
_projections.RemoveActive(record);
|
||||
record.ProjectionKey = null;
|
||||
_directory.ReleaseLocalId(expectedCanonical);
|
||||
record.WorldEntity = null;
|
||||
record = null;
|
||||
throw;
|
||||
}
|
||||
|
||||
if (_directory.IsCurrent(expectedCanonical))
|
||||
_directory.RemoveActive(expectedCanonical);
|
||||
_entityObjects.RetireAfterProjectionAcquisitionFailure(
|
||||
expectedCanonical);
|
||||
if (_projections.RemoveActive(record))
|
||||
{
|
||||
RetainTeardownRecord(record);
|
||||
|
|
@ -983,11 +843,24 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
bool visible = _spatial.IsLiveEntityProjectionResident(key);
|
||||
record.IsSpatiallyVisible = visible;
|
||||
RefreshPresentation(record);
|
||||
if ((spatialCellOrLandblockId & 0xFFFFu) != 0xFFFFu)
|
||||
record.FullCellId = spatialCellOrLandblockId;
|
||||
record.CanonicalLandblockId = spatialCellOrLandblockId == 0
|
||||
uint committedFullCell =
|
||||
(spatialCellOrLandblockId & 0xFFFFu) != 0xFFFFu
|
||||
? spatialCellOrLandblockId
|
||||
: record.FullCellId;
|
||||
uint committedLandblock = spatialCellOrLandblockId == 0
|
||||
? 0u
|
||||
: (spatialCellOrLandblockId & 0xFFFF0000u) | 0xFFFFu;
|
||||
if (!_entityObjects.CommitRebucket(
|
||||
record.Canonical,
|
||||
committedFullCell,
|
||||
committedLandblock))
|
||||
{
|
||||
ThrowAfterCommittedProjectionChange(
|
||||
serverGuid,
|
||||
spatialNotificationFailure,
|
||||
runtimeNotificationFailure: null);
|
||||
return false;
|
||||
}
|
||||
bool isOrdinaryRoot = record.ProjectionKind is LiveEntityProjectionKind.World
|
||||
&& record.IsSpatiallyProjected
|
||||
&& record.IsSpatiallyVisible
|
||||
|
|
@ -1182,7 +1055,12 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
return false;
|
||||
}
|
||||
|
||||
ClearWorldCell(serverGuid);
|
||||
if (!_entityObjects.CommitWithdrawal(
|
||||
record.Canonical,
|
||||
AcknowledgeCelllessCanonicalCommit))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
record.WorldEntity.ParentCellId = 0u;
|
||||
return WithdrawLiveEntityProjection(serverGuid);
|
||||
}
|
||||
|
|
@ -1218,10 +1096,9 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
if (!_entityObjects.TryAcceptDelete(
|
||||
delete,
|
||||
isLocalPlayer,
|
||||
removeRetainedObject,
|
||||
out acceptedDelete))
|
||||
return false;
|
||||
AdvanceLifetimeMutation(delete.Guid);
|
||||
ParentAttachments.DeleteGeneration(delete.Guid, delete.InstanceSequence);
|
||||
|
||||
// End active gameplay identity before arbitrary callbacks so a
|
||||
// newer generation can be accepted re-entrantly, but retain the
|
||||
|
|
@ -1229,11 +1106,7 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
// succeeded. A throwing resource callback can then be retried by
|
||||
// the same accepted Delete instead of orphaning its owner.
|
||||
LiveEntityRecord? retainedRegistrationFailure = record;
|
||||
if (_directory.TryGetActive(
|
||||
delete.Guid,
|
||||
out RuntimeEntityRecord activeCanonical)
|
||||
&& activeCanonical.Incarnation == delete.InstanceSequence
|
||||
&& _directory.RemoveActive(activeCanonical))
|
||||
if (acceptedDelete.RetiredCanonical is { } activeCanonical)
|
||||
{
|
||||
if (_projections.TryGet(
|
||||
activeCanonical,
|
||||
|
|
@ -1266,16 +1139,13 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
{
|
||||
if (!retryingAcceptedDelete)
|
||||
{
|
||||
if (removeRetainedObject)
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
_entityObjects.CompleteAcceptedDelete(acceptedDelete!);
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
(failures ??= new List<Exception>()).Add(error);
|
||||
}
|
||||
_entityObjects.CompleteAcceptedDelete(acceptedDelete!);
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
(failures ??= new List<Exception>()).Add(error);
|
||||
}
|
||||
|
||||
try
|
||||
|
|
@ -1872,95 +1742,44 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
_directory.TryGetSnapshot(guid, out spawn);
|
||||
|
||||
public bool TryApplyObjDesc(ObjDescEvent.Parsed update, out WorldSession.EntitySpawn accepted)
|
||||
{
|
||||
bool applied = _directory.TryApplyObjDesc(update, out accepted);
|
||||
if (applied
|
||||
&& _directory.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
_directory.RefreshSnapshot(canonical, accepted);
|
||||
if (_projections.TryGet(canonical, out LiveEntityRecord? record))
|
||||
record.AdvanceObjDescAuthority();
|
||||
else
|
||||
_directory.AdvanceObjDescAuthority(canonical);
|
||||
}
|
||||
return applied;
|
||||
}
|
||||
=> _entityObjects.TryApplyObjDesc(
|
||||
update,
|
||||
canonical =>
|
||||
{
|
||||
if (_projections.TryGet(
|
||||
canonical,
|
||||
out LiveEntityRecord? record))
|
||||
{
|
||||
record.NoteObjDescProjectionSynchronization();
|
||||
}
|
||||
},
|
||||
out accepted);
|
||||
|
||||
public bool TryApplyPickup(PickupEvent.Parsed update, out WorldSession.EntitySpawn accepted)
|
||||
{
|
||||
bool applied = _directory.TryApplyPickup(update, out accepted);
|
||||
if (applied
|
||||
&& _directory.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
_directory.RefreshSnapshot(canonical, accepted);
|
||||
if (_projections.TryGet(canonical, out LiveEntityRecord? record))
|
||||
record.AdvancePositionAuthority();
|
||||
else
|
||||
_directory.AdvancePositionAuthority(canonical);
|
||||
ClearWorldCell(canonical);
|
||||
ParentAttachments.EndChildProjection(update.Guid);
|
||||
}
|
||||
return applied;
|
||||
}
|
||||
=> _entityObjects.TryApplyPickup(
|
||||
update,
|
||||
AcknowledgeCelllessCanonicalCommit,
|
||||
out accepted);
|
||||
|
||||
public bool TryApplyCreateParent(CreateParentUpdate update, out WorldSession.EntitySpawn accepted)
|
||||
{
|
||||
bool applied = _directory.TryApplyCreateParent(update, out accepted);
|
||||
if (applied
|
||||
&& _directory.TryGetActive(
|
||||
update.ChildGuid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
_directory.RefreshSnapshot(canonical, accepted);
|
||||
if (_projections.TryGet(canonical, out LiveEntityRecord? record))
|
||||
record.AdvancePositionAuthority();
|
||||
else
|
||||
_directory.AdvancePositionAuthority(canonical);
|
||||
}
|
||||
return applied;
|
||||
}
|
||||
=> _entityObjects.TryApplyCreateParent(
|
||||
update,
|
||||
acknowledgeProjection: null,
|
||||
out accepted);
|
||||
|
||||
public bool TryApplyParent(ParentEvent.Parsed update, out WorldSession.EntitySpawn accepted)
|
||||
{
|
||||
bool applied = _directory.TryApplyParent(update, out accepted);
|
||||
if (applied
|
||||
&& _directory.TryGetActive(
|
||||
update.ChildGuid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
_directory.RefreshSnapshot(canonical, accepted);
|
||||
if (_projections.TryGet(canonical, out LiveEntityRecord? record))
|
||||
record.AdvancePositionAuthority();
|
||||
else
|
||||
_directory.AdvancePositionAuthority(canonical);
|
||||
}
|
||||
return applied;
|
||||
}
|
||||
=> _entityObjects.TryApplyParent(
|
||||
update,
|
||||
acknowledgeProjection: null,
|
||||
out accepted);
|
||||
|
||||
internal bool CommitStagedParent(
|
||||
ParentAttachmentRelation relation,
|
||||
out WorldSession.EntitySpawn accepted)
|
||||
{
|
||||
bool committed = _directory.TryCommitParent(
|
||||
relation.ChildGuid,
|
||||
relation.ParentGuid,
|
||||
relation.ParentLocation,
|
||||
relation.PlacementId,
|
||||
relation.ChildPositionSequence,
|
||||
=> _entityObjects.TryCommitParent(
|
||||
relation,
|
||||
acknowledgeProjection: null,
|
||||
out accepted);
|
||||
if (committed
|
||||
&& _directory.TryGetActive(
|
||||
relation.ChildGuid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
_directory.RefreshSnapshot(canonical, accepted);
|
||||
}
|
||||
return committed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Commits retail <c>CPhysicsObj::set_parent</c>'s cell-less edge only
|
||||
|
|
@ -1973,25 +1792,20 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
ulong positionAuthorityVersion)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(record);
|
||||
if (!IsCurrentPositionAuthority(record, positionAuthorityVersion))
|
||||
return false;
|
||||
ClearWorldCell(record.ServerGuid);
|
||||
return IsCurrentPositionAuthority(record, positionAuthorityVersion);
|
||||
return _entityObjects.CommitAcceptedParentCellless(
|
||||
record.Canonical,
|
||||
positionAuthorityVersion,
|
||||
AcknowledgeCelllessCanonicalCommit);
|
||||
}
|
||||
|
||||
internal bool CommitAcceptedParentCellless(
|
||||
RuntimeEntityRecord canonical,
|
||||
ulong positionAuthorityVersion)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(canonical);
|
||||
if (!_directory.IsCurrent(canonical)
|
||||
|| canonical.PositionAuthorityVersion != positionAuthorityVersion)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
ClearWorldCell(canonical);
|
||||
return _directory.IsCurrent(canonical)
|
||||
&& canonical.PositionAuthorityVersion == positionAuthorityVersion;
|
||||
return _entityObjects.CommitAcceptedParentCellless(
|
||||
canonical,
|
||||
positionAuthorityVersion,
|
||||
AcknowledgeCelllessCanonicalCommit);
|
||||
}
|
||||
|
||||
public bool TryApplyMotion(
|
||||
|
|
@ -1999,43 +1813,18 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
bool retainPayload,
|
||||
out WorldSession.EntitySpawn accepted,
|
||||
out AcceptedPhysicsTimestamps timestamps)
|
||||
{
|
||||
bool applied = _directory.TryApplyMotion(update, retainPayload, out accepted, out timestamps);
|
||||
if (_directory.TryGetSnapshot(update.Guid, out WorldSession.EntitySpawn snapshot)
|
||||
&& _directory.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
_directory.RefreshSnapshot(canonical, snapshot);
|
||||
}
|
||||
if (applied
|
||||
&& retainPayload
|
||||
&& _directory.TryGetActive(update.Guid, out canonical))
|
||||
{
|
||||
if (_projections.TryGet(canonical, out LiveEntityRecord? record))
|
||||
record.AdvanceMovementAuthority();
|
||||
else
|
||||
_directory.AdvanceMovementAuthority(canonical);
|
||||
}
|
||||
return applied;
|
||||
}
|
||||
=> _entityObjects.TryApplyMotion(
|
||||
update,
|
||||
retainPayload,
|
||||
acknowledgeProjection: null,
|
||||
out accepted,
|
||||
out timestamps);
|
||||
|
||||
public bool TryApplyVector(VectorUpdate.Parsed update, out WorldSession.EntitySpawn accepted)
|
||||
{
|
||||
bool applied = _directory.TryApplyVector(update, out accepted);
|
||||
if (applied
|
||||
&& _directory.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
_directory.RefreshSnapshot(canonical, accepted);
|
||||
if (_projections.TryGet(canonical, out LiveEntityRecord? record))
|
||||
record.AdvanceVectorAuthority();
|
||||
else
|
||||
_directory.AdvanceVectorAuthority(canonical);
|
||||
}
|
||||
return applied;
|
||||
}
|
||||
=> _entityObjects.TryApplyVector(
|
||||
update,
|
||||
acknowledgeProjection: null,
|
||||
out accepted);
|
||||
|
||||
public bool TryApplyState(SetState.Parsed update, out WorldSession.EntitySpawn accepted)
|
||||
=> TryApplyState(update, out accepted, out _);
|
||||
|
|
@ -2044,23 +1833,19 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
SetState.Parsed update,
|
||||
out WorldSession.EntitySpawn accepted,
|
||||
out RetailPhysicsStateTransition transition)
|
||||
{
|
||||
bool applied = _directory.TryApplyState(update, out accepted);
|
||||
transition = default;
|
||||
if (applied
|
||||
&& _directory.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
_directory.RefreshSnapshot(canonical, accepted);
|
||||
transition = _directory.ApplyRawPhysicsState(
|
||||
canonical,
|
||||
update.PhysicsState);
|
||||
if (_projections.TryGet(canonical, out LiveEntityRecord? record))
|
||||
RefreshPresentation(record);
|
||||
}
|
||||
return applied;
|
||||
}
|
||||
=> _entityObjects.TryApplyState(
|
||||
update,
|
||||
(canonical, _) =>
|
||||
{
|
||||
if (_projections.TryGet(
|
||||
canonical,
|
||||
out LiveEntityRecord? record))
|
||||
{
|
||||
RefreshPresentation(record);
|
||||
}
|
||||
},
|
||||
out accepted,
|
||||
out transition);
|
||||
|
||||
/// <summary>
|
||||
/// Retail parent Hidden directly toggles each attached child's NoDraw bit
|
||||
|
|
@ -2070,9 +1855,10 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
{
|
||||
if (!_projections.TryGetCurrent(childServerGuid, out LiveEntityRecord? record))
|
||||
return false;
|
||||
record.SetChildNoDraw(noDraw);
|
||||
RefreshPresentation(record);
|
||||
return true;
|
||||
return _entityObjects.CommitChildNoDraw(
|
||||
record.Canonical,
|
||||
noDraw,
|
||||
_ => RefreshPresentation(record));
|
||||
}
|
||||
|
||||
public bool TryApplyPosition(
|
||||
|
|
@ -2084,62 +1870,26 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
out WorldSession.EntitySpawn accepted,
|
||||
out AcceptedPhysicsTimestamps timestamps)
|
||||
{
|
||||
bool hadCanonical = _directory.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord? beforeCanonical);
|
||||
LiveEntityRecord? beforePosition = null;
|
||||
if (hadCanonical)
|
||||
_projections.TryGet(beforeCanonical!, out beforePosition);
|
||||
bool wasCellLess = hadCanonical
|
||||
&& (beforeCanonical!.FullCellId == 0
|
||||
|| beforePosition is null
|
||||
|| !beforePosition.IsSpatiallyProjected
|
||||
|| !beforePosition.IsSpatiallyVisible);
|
||||
bool known = _directory.TryApplyPosition(
|
||||
bool projectionRequiresTeleportHook =
|
||||
_directory.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord canonical)
|
||||
&& (canonical.FullCellId == 0u
|
||||
|| !_projections.TryGet(
|
||||
canonical,
|
||||
out LiveEntityRecord? projection)
|
||||
|| !projection.IsSpatiallyProjected
|
||||
|| !projection.IsSpatiallyVisible);
|
||||
return _entityObjects.TryApplyPosition(
|
||||
update,
|
||||
isLocalPlayer,
|
||||
forcePositionRotation,
|
||||
currentLocalVelocity,
|
||||
projectionRequiresTeleportHook,
|
||||
acknowledgeProjection: null,
|
||||
out disposition,
|
||||
out accepted,
|
||||
out timestamps);
|
||||
if (known && _directory.TryGetSnapshot(update.Guid, out WorldSession.EntitySpawn snapshot))
|
||||
{
|
||||
bool acceptedPosition = disposition is not PositionTimestampDisposition.Rejected;
|
||||
if (disposition is PositionTimestampDisposition.Apply)
|
||||
{
|
||||
timestamps = timestamps with
|
||||
{
|
||||
TeleportHookRequired = timestamps.TeleportAdvanced || wasCellLess,
|
||||
};
|
||||
}
|
||||
if (_directory.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord acceptedCanonical))
|
||||
{
|
||||
_directory.RefreshSnapshot(
|
||||
acceptedCanonical,
|
||||
snapshot,
|
||||
refreshPosition: acceptedPosition);
|
||||
if (acceptedPosition
|
||||
&& ReferenceEquals(acceptedCanonical, beforeCanonical))
|
||||
{
|
||||
if (_projections.TryGet(
|
||||
acceptedCanonical,
|
||||
out LiveEntityRecord? acceptedRecord)
|
||||
&& ReferenceEquals(acceptedRecord, beforePosition))
|
||||
{
|
||||
acceptedRecord.AdvancePositionAuthority();
|
||||
}
|
||||
else
|
||||
{
|
||||
_directory.AdvancePositionAuthority(acceptedCanonical);
|
||||
}
|
||||
ParentAttachments.EndChildProjection(update.Guid);
|
||||
}
|
||||
}
|
||||
}
|
||||
return known;
|
||||
}
|
||||
|
||||
public bool IsFreshTeleportStart(uint localPlayerGuid, ushort teleportSequence) =>
|
||||
|
|
@ -2669,15 +2419,13 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
|
||||
_isClearing = true;
|
||||
_sessionClearPendingFinalization = true;
|
||||
_directory.BeginSessionClear();
|
||||
IReadOnlyList<RuntimeEntityRecord> retiredCanonicals =
|
||||
_entityObjects.BeginSessionClear();
|
||||
List<Exception>? failures = null;
|
||||
try
|
||||
{
|
||||
foreach (RuntimeEntityRecord canonical in
|
||||
_directory.ActiveRecords.ToArray())
|
||||
foreach (RuntimeEntityRecord canonical in retiredCanonicals)
|
||||
{
|
||||
if (!_directory.RemoveActive(canonical))
|
||||
continue;
|
||||
if (_projections.TryGet(
|
||||
canonical,
|
||||
out LiveEntityRecord? record))
|
||||
|
|
@ -2796,12 +2544,6 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
_directory.RefreshSnapshot(canonical, accepted, refreshPosition);
|
||||
}
|
||||
|
||||
private static InboundCreateResult SupersededCreateResult() => new(
|
||||
CreateObjectTimestampDisposition.StaleGeneration,
|
||||
default,
|
||||
null,
|
||||
default);
|
||||
|
||||
private ulong AdvanceLifetimeMutation(uint serverGuid)
|
||||
=> _directory.AdvanceLifetimeMutation(serverGuid);
|
||||
|
||||
|
|
@ -2841,37 +2583,18 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
ExceptionDispatchInfo.Capture(failure).Throw();
|
||||
}
|
||||
|
||||
private void ClearWorldCell(uint guid)
|
||||
private void AcknowledgeCelllessCanonicalCommit(
|
||||
RuntimeEntityRecord canonical)
|
||||
{
|
||||
if (!_directory.TryGetActive(guid, out RuntimeEntityRecord canonical))
|
||||
if (!_directory.IsCurrent(canonical)
|
||||
|| !_projections.TryGet(
|
||||
canonical,
|
||||
out LiveEntityRecord? record))
|
||||
{
|
||||
return;
|
||||
ClearWorldCell(canonical);
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearWorldCell(RuntimeEntityRecord canonical)
|
||||
{
|
||||
if (!_directory.IsCurrent(canonical))
|
||||
return;
|
||||
if (!_projections.TryGet(canonical, out LiveEntityRecord? record))
|
||||
{
|
||||
_directory.SetFullCell(canonical, 0, 0);
|
||||
return;
|
||||
}
|
||||
bool wasOrdinaryRoot = record.ProjectionKey is { } key
|
||||
&& _spatialRootObjects.TryGetValue(
|
||||
key,
|
||||
out LiveEntityRecord? indexedRoot)
|
||||
&& ReferenceEquals(indexedRoot, record);
|
||||
// Pickup/Parent is retail's leave-world edge. Suspend the canonical
|
||||
// object clock while the record is still indexed as a root; clearing
|
||||
// FullCellId first removes that evidence and makes the later projection
|
||||
// withdrawal look like a duplicate non-root edge.
|
||||
if (wasOrdinaryRoot)
|
||||
{
|
||||
record.SuspendObjectClock();
|
||||
SynchronizePhysicsBodyActiveState(record);
|
||||
}
|
||||
_directory.SetFullCell(canonical, 0, 0);
|
||||
SynchronizePhysicsBodyActiveState(record);
|
||||
RefreshSpatialRuntimeIndexes(record);
|
||||
}
|
||||
|
||||
|
|
@ -3091,27 +2814,50 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
_projections.RetainTeardown(record);
|
||||
}
|
||||
|
||||
private void TearDownCanonicalOnly(RuntimeEntityRecord canonical)
|
||||
private Exception? RetirePriorProjection(
|
||||
RuntimeEntityRecord canonical)
|
||||
{
|
||||
// A canonical-only incarnation has never crossed the App projection
|
||||
// acquisition edge, so it owns no renderer/effect/animation suffix.
|
||||
// Retain it briefly only so Runtime's validated mutators can converge
|
||||
// any presentation-free physics state before the tombstone vanishes.
|
||||
_directory.RetainTeardown(canonical);
|
||||
if (!_projections.TryGet(
|
||||
canonical,
|
||||
out LiveEntityRecord? projection))
|
||||
{
|
||||
return _entityObjects.RetireCanonicalOnly(canonical);
|
||||
}
|
||||
|
||||
if (!_projections.RemoveActive(projection))
|
||||
{
|
||||
return new InvalidOperationException(
|
||||
$"Exact App projection for 0x{canonical.ServerGuid:X8}/{canonical.Incarnation} could not be retired.");
|
||||
}
|
||||
|
||||
RetainTeardownRecord(projection);
|
||||
_logicalTeardownDepth++;
|
||||
try
|
||||
{
|
||||
_directory.SetPhysicsHost(canonical, null);
|
||||
_directory.SetPhysicsBody(canonical, null);
|
||||
_directory.SetPhysicsBodyAcquisitionInProgress(canonical, false);
|
||||
_directory.SetHasPartArray(canonical, false);
|
||||
_directory.ReleaseLocalId(canonical);
|
||||
try
|
||||
{
|
||||
TearDownRecord(projection);
|
||||
ReleaseTeardownRecord(projection);
|
||||
return null;
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_directory.ReleaseTeardown(canonical);
|
||||
_logicalTeardownDepth--;
|
||||
}
|
||||
}
|
||||
|
||||
private void TearDownCanonicalOnly(RuntimeEntityRecord canonical)
|
||||
{
|
||||
Exception? failure = _entityObjects.RetireCanonicalOnly(canonical);
|
||||
if (failure is not null)
|
||||
ExceptionDispatchInfo.Capture(failure).Throw();
|
||||
}
|
||||
|
||||
private void ReleaseTeardownRecord(LiveEntityRecord record)
|
||||
{
|
||||
_projections.ReleaseTeardown(record);
|
||||
|
|
@ -3125,7 +2871,7 @@ public sealed class LiveEntityRuntime : ILiveEntityRadarSource
|
|||
private void CompleteSessionClearIfConverged()
|
||||
{
|
||||
if (!_sessionClearPendingFinalization
|
||||
|| !_directory.CompleteSessionClearIfConverged())
|
||||
|| !_entityObjects.CompleteSessionClearIfConverged())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,7 +80,16 @@ public sealed class RuntimeEntityDirectory
|
|||
|
||||
var record = new RuntimeEntityRecord(snapshot);
|
||||
_activeByGuid.Add(snapshot.Guid, record);
|
||||
return record;
|
||||
try
|
||||
{
|
||||
ClaimLocalId(record);
|
||||
return record;
|
||||
}
|
||||
catch
|
||||
{
|
||||
_activeByGuid.Remove(snapshot.Guid);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public bool RemoveActive(uint guid, out RuntimeEntityRecord? record) =>
|
||||
|
|
|
|||
271
src/AcDream.Runtime/Entities/RuntimeEntityObjectEventStream.cs
Normal file
271
src/AcDream.Runtime/Entities/RuntimeEntityObjectEventStream.cs
Normal file
|
|
@ -0,0 +1,271 @@
|
|||
using AcDream.Core.Items;
|
||||
|
||||
namespace AcDream.Runtime.Entities;
|
||||
|
||||
public interface IRuntimeEntityObjectObserver
|
||||
{
|
||||
void OnEntity(in RuntimeEntityDelta delta);
|
||||
|
||||
void OnInventory(in RuntimeInventoryDelta delta);
|
||||
}
|
||||
|
||||
public interface IRuntimeEntityObjectEventSource
|
||||
{
|
||||
IDisposable Subscribe(IRuntimeEntityObjectObserver observer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One synchronous, generation-stamped commit stream for the canonical entity
|
||||
/// directory and retained object table. It owns no queue and dispatches on the
|
||||
/// caller's update/network-drain thread.
|
||||
/// </summary>
|
||||
public sealed class RuntimeEntityObjectEventStream
|
||||
: IRuntimeEntityObjectEventSource,
|
||||
IDisposable
|
||||
{
|
||||
private readonly RuntimeEntityDirectory _entities;
|
||||
private readonly ClientObjectTable _objects;
|
||||
private readonly RuntimeEventSequencer _sequencer = new();
|
||||
private readonly object _observerGate = new();
|
||||
private IRuntimeEntityObjectObserver[] _observers = [];
|
||||
private Func<RuntimeGenerationToken> _generation = static () => default;
|
||||
private Func<ulong> _frameNumber = static () => 0UL;
|
||||
private bool _contextBound;
|
||||
private bool _disposed;
|
||||
|
||||
internal RuntimeEntityObjectEventStream(
|
||||
RuntimeEntityDirectory entities,
|
||||
ClientObjectTable objects)
|
||||
{
|
||||
_entities = entities
|
||||
?? throw new ArgumentNullException(nameof(entities));
|
||||
_objects = objects ?? throw new ArgumentNullException(nameof(objects));
|
||||
_objects.ObjectAdded += OnObjectAdded;
|
||||
_objects.ObjectUpdated += OnObjectUpdated;
|
||||
_objects.ObjectMoved += OnObjectMoved;
|
||||
_objects.ObjectRemovalClassified += OnObjectRemoved;
|
||||
_objects.Cleared += OnObjectsCleared;
|
||||
}
|
||||
|
||||
public ulong LastSequence => _sequencer.LastSequence;
|
||||
public int SubscriberCount => Volatile.Read(ref _observers).Length;
|
||||
public long DispatchFailureCount { get; private set; }
|
||||
public Exception? LastDispatchFailure { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Binds the session-generation and Runtime-frame sources exactly once.
|
||||
/// Both are queried at commit time so reconnect/reset cannot stamp an event
|
||||
/// with a cached generation.
|
||||
/// </summary>
|
||||
public void BindContext(
|
||||
Func<RuntimeGenerationToken> generation,
|
||||
Func<ulong> frameNumber)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(generation);
|
||||
ArgumentNullException.ThrowIfNull(frameNumber);
|
||||
lock (_observerGate)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
if (_contextBound)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"The Runtime entity/object event context is already bound.");
|
||||
}
|
||||
|
||||
_generation = generation;
|
||||
_frameNumber = frameNumber;
|
||||
_contextBound = true;
|
||||
}
|
||||
}
|
||||
|
||||
public IDisposable Subscribe(IRuntimeEntityObjectObserver observer)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(observer);
|
||||
lock (_observerGate)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
IRuntimeEntityObjectObserver[] current = _observers;
|
||||
if (Array.IndexOf(current, observer) >= 0)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"The Runtime entity/object observer is already subscribed.");
|
||||
}
|
||||
|
||||
var replacement =
|
||||
new IRuntimeEntityObjectObserver[current.Length + 1];
|
||||
Array.Copy(current, replacement, current.Length);
|
||||
replacement[^1] = observer;
|
||||
Volatile.Write(ref _observers, replacement);
|
||||
}
|
||||
|
||||
return new ObserverSubscription(this, observer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates the next stamp for another domain on the same Runtime event
|
||||
/// surface. J4–J6 will move those remaining domain publishers into
|
||||
/// Runtime; until then the App adapter borrows this sequencer.
|
||||
/// </summary>
|
||||
public RuntimeEventStamp NextStamp()
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(
|
||||
Volatile.Read(ref _disposed),
|
||||
this);
|
||||
return _sequencer.Next(_generation(), _frameNumber());
|
||||
}
|
||||
|
||||
internal void PublishEntity(
|
||||
RuntimeEntityChange change,
|
||||
RuntimeEntityRecord record)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(record);
|
||||
var delta = new RuntimeEntityDelta(
|
||||
NextStamp(),
|
||||
change,
|
||||
RuntimeEntityObjectViews.Snapshot(record));
|
||||
IRuntimeEntityObjectObserver[] observers =
|
||||
Volatile.Read(ref _observers);
|
||||
foreach (IRuntimeEntityObjectObserver observer in observers)
|
||||
{
|
||||
try
|
||||
{
|
||||
observer.OnEntity(in delta);
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
RecordDispatchFailure(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
lock (_observerGate)
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
_disposed = true;
|
||||
Volatile.Write(ref _observers, []);
|
||||
_objects.Cleared -= OnObjectsCleared;
|
||||
_objects.ObjectRemovalClassified -= OnObjectRemoved;
|
||||
_objects.ObjectMoved -= OnObjectMoved;
|
||||
_objects.ObjectUpdated -= OnObjectUpdated;
|
||||
_objects.ObjectAdded -= OnObjectAdded;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnObjectAdded(ClientObject item) =>
|
||||
PublishInventory(RuntimeInventoryChange.Added, item);
|
||||
|
||||
private void OnObjectUpdated(ClientObject item) =>
|
||||
PublishInventory(RuntimeInventoryChange.Updated, item);
|
||||
|
||||
private void OnObjectMoved(ClientObjectMove move)
|
||||
{
|
||||
ClientObject? item = move.Item ?? _objects.Get(move.ItemId);
|
||||
RuntimeInventoryItemSnapshot snapshot = item is null
|
||||
? new RuntimeInventoryItemSnapshot(
|
||||
move.ItemId,
|
||||
0,
|
||||
string.Empty,
|
||||
move.Current.ContainerId,
|
||||
move.Current.ContainerSlot,
|
||||
move.Current.WielderId,
|
||||
(uint)move.Current.EquipLocation,
|
||||
0,
|
||||
0)
|
||||
: RuntimeEntityObjectViews.Snapshot(item, _entities);
|
||||
PublishInventory(RuntimeInventoryChange.Moved, snapshot);
|
||||
}
|
||||
|
||||
private void OnObjectRemoved(ClientObjectRemoval removal) =>
|
||||
PublishInventory(
|
||||
RuntimeInventoryChange.Removed,
|
||||
RuntimeEntityObjectViews.Snapshot(
|
||||
removal.Object,
|
||||
_entities,
|
||||
removal.Generation));
|
||||
|
||||
private void OnObjectsCleared() =>
|
||||
PublishInventory(
|
||||
RuntimeInventoryChange.Cleared,
|
||||
default(RuntimeInventoryItemSnapshot));
|
||||
|
||||
private void PublishInventory(
|
||||
RuntimeInventoryChange change,
|
||||
ClientObject item) =>
|
||||
PublishInventory(
|
||||
change,
|
||||
RuntimeEntityObjectViews.Snapshot(item, _entities));
|
||||
|
||||
private void PublishInventory(
|
||||
RuntimeInventoryChange change,
|
||||
RuntimeInventoryItemSnapshot item)
|
||||
{
|
||||
var delta = new RuntimeInventoryDelta(
|
||||
NextStamp(),
|
||||
change,
|
||||
item);
|
||||
IRuntimeEntityObjectObserver[] observers =
|
||||
Volatile.Read(ref _observers);
|
||||
foreach (IRuntimeEntityObjectObserver observer in observers)
|
||||
{
|
||||
try
|
||||
{
|
||||
observer.OnInventory(in delta);
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
RecordDispatchFailure(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void RecordDispatchFailure(Exception error)
|
||||
{
|
||||
DispatchFailureCount++;
|
||||
LastDispatchFailure = error;
|
||||
}
|
||||
|
||||
private void Unsubscribe(IRuntimeEntityObjectObserver observer)
|
||||
{
|
||||
lock (_observerGate)
|
||||
{
|
||||
IRuntimeEntityObjectObserver[] current = _observers;
|
||||
int index = Array.IndexOf(current, observer);
|
||||
if (index < 0)
|
||||
return;
|
||||
if (current.Length == 1)
|
||||
{
|
||||
Volatile.Write(ref _observers, []);
|
||||
return;
|
||||
}
|
||||
|
||||
var replacement =
|
||||
new IRuntimeEntityObjectObserver[current.Length - 1];
|
||||
if (index != 0)
|
||||
Array.Copy(current, 0, replacement, 0, index);
|
||||
if (index != current.Length - 1)
|
||||
{
|
||||
Array.Copy(
|
||||
current,
|
||||
index + 1,
|
||||
replacement,
|
||||
index,
|
||||
current.Length - index - 1);
|
||||
}
|
||||
Volatile.Write(ref _observers, replacement);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class ObserverSubscription(
|
||||
RuntimeEntityObjectEventStream owner,
|
||||
IRuntimeEntityObjectObserver observer)
|
||||
: IDisposable
|
||||
{
|
||||
private RuntimeEntityObjectEventStream? _owner = owner;
|
||||
|
||||
public void Dispose() =>
|
||||
Interlocked.Exchange(ref _owner, null)?.Unsubscribe(observer);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,17 @@
|
|||
using AcDream.Core.Items;
|
||||
using AcDream.Core.Net;
|
||||
using AcDream.Core.Net.Messages;
|
||||
using AcDream.Core.Physics;
|
||||
|
||||
namespace AcDream.Runtime.Entities;
|
||||
|
||||
public readonly record struct RuntimeEntityRegistrationResult(
|
||||
InboundCreateResult Inbound,
|
||||
RuntimeEntityRecord? Canonical,
|
||||
bool LogicalRegistrationCreated,
|
||||
bool ReplacedExistingGeneration,
|
||||
Exception? PriorGenerationCleanupFailure = null);
|
||||
|
||||
/// <summary>
|
||||
/// One exact DeleteObject acceptance issued by a
|
||||
/// <see cref="RuntimeEntityObjectLifetime"/>. The graphical host retires the
|
||||
|
|
@ -14,15 +22,21 @@ public sealed class RuntimeEntityDeleteAcceptance
|
|||
{
|
||||
internal RuntimeEntityDeleteAcceptance(
|
||||
RuntimeEntityObjectLifetime owner,
|
||||
DeleteObject.Parsed delete)
|
||||
DeleteObject.Parsed delete,
|
||||
RuntimeEntityRecord? retiredCanonical,
|
||||
bool removeRetainedObject)
|
||||
{
|
||||
Owner = owner;
|
||||
Delete = delete;
|
||||
RetiredCanonical = retiredCanonical;
|
||||
RemoveRetainedObject = removeRetainedObject;
|
||||
}
|
||||
|
||||
internal RuntimeEntityObjectLifetime Owner { get; }
|
||||
internal bool Completed { get; set; }
|
||||
public DeleteObject.Parsed Delete { get; }
|
||||
public RuntimeEntityRecord? RetiredCanonical { get; }
|
||||
public bool RemoveRetainedObject { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -30,17 +44,253 @@ public sealed class RuntimeEntityDeleteAcceptance
|
|||
/// retained object lifetimes. Graphical and direct hosts borrow these exact
|
||||
/// instances; they never allocate a second directory or object table.
|
||||
/// </summary>
|
||||
public sealed class RuntimeEntityObjectLifetime
|
||||
public sealed class RuntimeEntityObjectLifetime : IDisposable
|
||||
{
|
||||
private bool _sessionClearInProgress;
|
||||
private bool _disposed;
|
||||
|
||||
public RuntimeEntityObjectLifetime(
|
||||
uint firstLocalEntityId = RuntimeEntityDirectory.FirstLocalEntityId)
|
||||
{
|
||||
Entities = new RuntimeEntityDirectory(firstLocalEntityId);
|
||||
Objects = new ClientObjectTable();
|
||||
var views = new RuntimeEntityObjectViews(Entities, Objects);
|
||||
EntityView = views.Entities;
|
||||
InventoryView = views.Inventory;
|
||||
Events = new RuntimeEntityObjectEventStream(Entities, Objects);
|
||||
}
|
||||
|
||||
public RuntimeEntityDirectory Entities { get; }
|
||||
public ClientObjectTable Objects { get; }
|
||||
public IRuntimeEntityView EntityView { get; }
|
||||
public IRuntimeInventoryView InventoryView { get; }
|
||||
public RuntimeEntityObjectEventStream Events { get; }
|
||||
|
||||
public void BindEventContext(
|
||||
Func<RuntimeGenerationToken> generation,
|
||||
Func<ulong> frameNumber)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
Events.BindContext(generation, frameNumber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Owns the presentation-free half of retail's CreateObject lifetime
|
||||
/// transaction. An attached graphical host may synchronously retire the
|
||||
/// displaced projection through <paramref name="retirePriorProjection"/>;
|
||||
/// a direct host omits it and Runtime retires canonical-only state.
|
||||
/// </summary>
|
||||
public RuntimeEntityRegistrationResult RegisterEntity(
|
||||
WorldSession.EntitySpawn incoming,
|
||||
Func<RuntimeEntityRecord, Exception?>? retirePriorProjection = null)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
if (_sessionClearInProgress)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"A Runtime entity cannot register while its session lifetime is clearing.");
|
||||
}
|
||||
|
||||
InboundCreateResult result = Entities.AcceptCreate(incoming);
|
||||
if (result.Disposition
|
||||
is CreateObjectTimestampDisposition.StaleGeneration)
|
||||
{
|
||||
return new RuntimeEntityRegistrationResult(
|
||||
result,
|
||||
Canonical: null,
|
||||
LogicalRegistrationCreated: false,
|
||||
ReplacedExistingGeneration: false);
|
||||
}
|
||||
|
||||
ulong sessionVersion = Entities.SessionLifetimeVersion;
|
||||
ulong operationVersion =
|
||||
Entities.AdvanceLifetimeMutation(incoming.Guid);
|
||||
|
||||
if (result.Disposition
|
||||
is CreateObjectTimestampDisposition.ExistingGeneration)
|
||||
{
|
||||
if (Entities.TryGetActive(
|
||||
incoming.Guid,
|
||||
out RuntimeEntityRecord retained))
|
||||
{
|
||||
Entities.RefreshSnapshot(
|
||||
retained,
|
||||
result.Snapshot,
|
||||
refreshPosition: true);
|
||||
Entities.AdvanceCreateAuthority(retained);
|
||||
PublishEntity(RuntimeEntityChange.Updated, retained);
|
||||
if (!IsCurrentOperation(
|
||||
incoming.Guid,
|
||||
retained,
|
||||
sessionVersion,
|
||||
operationVersion))
|
||||
{
|
||||
return SupersededRegistration(
|
||||
incoming.Guid,
|
||||
replacedExistingGeneration: false);
|
||||
}
|
||||
return new RuntimeEntityRegistrationResult(
|
||||
result,
|
||||
retained,
|
||||
LogicalRegistrationCreated: false,
|
||||
ReplacedExistingGeneration: false);
|
||||
}
|
||||
|
||||
if (Entities.TryGetTeardown(
|
||||
incoming.Guid,
|
||||
result.Snapshot.InstanceSequence,
|
||||
out _))
|
||||
{
|
||||
return new RuntimeEntityRegistrationResult(
|
||||
result,
|
||||
Canonical: null,
|
||||
LogicalRegistrationCreated: false,
|
||||
ReplacedExistingGeneration: false);
|
||||
}
|
||||
|
||||
RuntimeEntityRecord recovered = Entities.AddActive(result.Snapshot);
|
||||
PublishEntity(RuntimeEntityChange.Registered, recovered);
|
||||
if (!IsCurrentOperation(
|
||||
incoming.Guid,
|
||||
recovered,
|
||||
sessionVersion,
|
||||
operationVersion))
|
||||
{
|
||||
return SupersededRegistration(
|
||||
incoming.Guid,
|
||||
replacedExistingGeneration: false);
|
||||
}
|
||||
return new RuntimeEntityRegistrationResult(
|
||||
result,
|
||||
recovered,
|
||||
LogicalRegistrationCreated: true,
|
||||
ReplacedExistingGeneration: false);
|
||||
}
|
||||
|
||||
bool replaced = Entities.RemoveActive(
|
||||
incoming.Guid,
|
||||
out RuntimeEntityRecord? prior);
|
||||
if (result.Disposition
|
||||
is CreateObjectTimestampDisposition.NewGeneration)
|
||||
{
|
||||
Entities.ParentAttachments.EndGeneration(
|
||||
incoming.Guid,
|
||||
result.Snapshot.InstanceSequence);
|
||||
}
|
||||
|
||||
Exception? cleanupFailure = null;
|
||||
if (prior is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
PublishEntity(RuntimeEntityChange.Deleted, prior);
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
cleanupFailure = error;
|
||||
}
|
||||
|
||||
Exception? projectionFailure = retirePriorProjection is null
|
||||
? RetireCanonicalOnly(prior)
|
||||
: retirePriorProjection(prior);
|
||||
cleanupFailure = Combine(cleanupFailure, projectionFailure);
|
||||
}
|
||||
|
||||
if (Entities.SessionLifetimeVersion != sessionVersion
|
||||
|| Entities.CurrentLifetimeMutation(incoming.Guid)
|
||||
!= operationVersion)
|
||||
{
|
||||
if (cleanupFailure is not null)
|
||||
{
|
||||
throw new AggregateException(
|
||||
$"Prior incarnation of live entity 0x{incoming.Guid:X8} failed teardown while its incoming replacement was superseded.",
|
||||
cleanupFailure);
|
||||
}
|
||||
|
||||
return new RuntimeEntityRegistrationResult(
|
||||
SupersededCreateResult(),
|
||||
Entities.TryGetActive(
|
||||
incoming.Guid,
|
||||
out RuntimeEntityRecord current)
|
||||
? current
|
||||
: null,
|
||||
LogicalRegistrationCreated: false,
|
||||
ReplacedExistingGeneration: replaced);
|
||||
}
|
||||
|
||||
RuntimeEntityRecord canonical = Entities.AddActive(result.Snapshot);
|
||||
try
|
||||
{
|
||||
PublishEntity(RuntimeEntityChange.Registered, canonical);
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
if (cleanupFailure is not null)
|
||||
{
|
||||
throw new AggregateException(
|
||||
$"Live entity 0x{incoming.Guid:X8} registered after prior cleanup and commit observers failed.",
|
||||
cleanupFailure,
|
||||
error);
|
||||
}
|
||||
throw;
|
||||
}
|
||||
|
||||
if (!IsCurrentOperation(
|
||||
incoming.Guid,
|
||||
canonical,
|
||||
sessionVersion,
|
||||
operationVersion))
|
||||
{
|
||||
if (cleanupFailure is not null)
|
||||
{
|
||||
throw new AggregateException(
|
||||
$"Prior incarnation of live entity 0x{incoming.Guid:X8} failed teardown while its committed replacement was superseded.",
|
||||
cleanupFailure);
|
||||
}
|
||||
|
||||
return SupersededRegistration(
|
||||
incoming.Guid,
|
||||
replaced);
|
||||
}
|
||||
|
||||
return new RuntimeEntityRegistrationResult(
|
||||
result,
|
||||
canonical,
|
||||
LogicalRegistrationCreated: true,
|
||||
ReplacedExistingGeneration: replaced,
|
||||
cleanupFailure);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Completes a displaced incarnation that never crossed the graphical
|
||||
/// projection acquisition edge.
|
||||
/// </summary>
|
||||
public Exception? RetireCanonicalOnly(RuntimeEntityRecord canonical)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
ArgumentNullException.ThrowIfNull(canonical);
|
||||
try
|
||||
{
|
||||
Entities.RetainTeardown(canonical);
|
||||
try
|
||||
{
|
||||
Entities.SetPhysicsHost(canonical, null);
|
||||
Entities.SetPhysicsBody(canonical, null);
|
||||
Entities.SetPhysicsBodyAcquisitionInProgress(canonical, false);
|
||||
Entities.SetHasPartArray(canonical, false);
|
||||
Entities.ReleaseLocalId(canonical);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Entities.ReleaseTeardown(canonical);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the retained-object half of an accepted CreateObject while the
|
||||
|
|
@ -54,6 +304,7 @@ public sealed class RuntimeEntityObjectLifetime
|
|||
WorldSession.EntitySpawn spawn,
|
||||
bool replaceGeneration)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
ArgumentNullException.ThrowIfNull(canonical);
|
||||
if (canonical.ServerGuid != spawn.Guid
|
||||
|| canonical.Incarnation != spawn.InstanceSequence)
|
||||
|
|
@ -75,24 +326,423 @@ public sealed class RuntimeEntityObjectLifetime
|
|||
&& IsCurrent();
|
||||
}
|
||||
|
||||
public bool TryApplyObjDesc(
|
||||
ObjDescEvent.Parsed update,
|
||||
Action<RuntimeEntityRecord>? acknowledgeProjection,
|
||||
out WorldSession.EntitySpawn accepted)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
bool applied = Entities.TryApplyObjDesc(update, out accepted);
|
||||
if (!applied
|
||||
|| !Entities.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
return applied;
|
||||
}
|
||||
|
||||
Entities.RefreshSnapshot(canonical, accepted);
|
||||
Entities.AdvanceObjDescAuthority(canonical);
|
||||
acknowledgeProjection?.Invoke(canonical);
|
||||
if (!Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
PublishEntity(RuntimeEntityChange.Updated, canonical);
|
||||
return Entities.IsCurrent(canonical);
|
||||
}
|
||||
|
||||
public bool TryApplyPickup(
|
||||
PickupEvent.Parsed update,
|
||||
Action<RuntimeEntityRecord>? acknowledgeProjection,
|
||||
out WorldSession.EntitySpawn accepted)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
bool applied = Entities.TryApplyPickup(update, out accepted);
|
||||
if (!applied
|
||||
|| !Entities.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
return applied;
|
||||
}
|
||||
|
||||
Entities.RefreshSnapshot(canonical, accepted);
|
||||
Entities.AdvancePositionAuthority(canonical);
|
||||
Entities.SuspendObjectClock(canonical);
|
||||
Entities.SetFullCell(canonical, 0u, 0u);
|
||||
Entities.ParentAttachments.EndChildProjection(update.Guid);
|
||||
acknowledgeProjection?.Invoke(canonical);
|
||||
if (!Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
PublishEntity(RuntimeEntityChange.Withdrawn, canonical);
|
||||
return Entities.IsCurrent(canonical);
|
||||
}
|
||||
|
||||
public bool TryApplyCreateParent(
|
||||
CreateParentUpdate update,
|
||||
Action<RuntimeEntityRecord>? acknowledgeProjection,
|
||||
out WorldSession.EntitySpawn accepted)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
bool applied = Entities.TryApplyCreateParent(update, out accepted);
|
||||
return CommitPositionChannelUpdate(
|
||||
applied,
|
||||
update.ChildGuid,
|
||||
accepted,
|
||||
acknowledgeProjection);
|
||||
}
|
||||
|
||||
public bool TryApplyParent(
|
||||
ParentEvent.Parsed update,
|
||||
Action<RuntimeEntityRecord>? acknowledgeProjection,
|
||||
out WorldSession.EntitySpawn accepted)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
bool applied = Entities.TryApplyParent(update, out accepted);
|
||||
return CommitPositionChannelUpdate(
|
||||
applied,
|
||||
update.ChildGuid,
|
||||
accepted,
|
||||
acknowledgeProjection);
|
||||
}
|
||||
|
||||
public bool TryCommitParent(
|
||||
ParentAttachmentRelation relation,
|
||||
Action<RuntimeEntityRecord>? acknowledgeProjection,
|
||||
out WorldSession.EntitySpawn accepted)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
bool committed = Entities.TryCommitParent(
|
||||
relation.ChildGuid,
|
||||
relation.ParentGuid,
|
||||
relation.ParentLocation,
|
||||
relation.PlacementId,
|
||||
relation.ChildPositionSequence,
|
||||
out accepted);
|
||||
if (!committed
|
||||
|| !Entities.TryGetActive(
|
||||
relation.ChildGuid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
return committed;
|
||||
}
|
||||
|
||||
Entities.RefreshSnapshot(canonical, accepted);
|
||||
acknowledgeProjection?.Invoke(canonical);
|
||||
if (!Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
PublishEntity(RuntimeEntityChange.Updated, canonical);
|
||||
return Entities.IsCurrent(canonical);
|
||||
}
|
||||
|
||||
public bool CommitAcceptedParentCellless(
|
||||
RuntimeEntityRecord canonical,
|
||||
ulong positionAuthorityVersion,
|
||||
Action<RuntimeEntityRecord>? acknowledgeProjection)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
ArgumentNullException.ThrowIfNull(canonical);
|
||||
if (!Entities.IsCurrent(canonical)
|
||||
|| canonical.PositionAuthorityVersion != positionAuthorityVersion)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Entities.SuspendObjectClock(canonical);
|
||||
Entities.SetFullCell(canonical, 0u, 0u);
|
||||
acknowledgeProjection?.Invoke(canonical);
|
||||
if (!Entities.IsCurrent(canonical)
|
||||
|| canonical.PositionAuthorityVersion != positionAuthorityVersion)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
PublishEntity(RuntimeEntityChange.Withdrawn, canonical);
|
||||
return Entities.IsCurrent(canonical)
|
||||
&& canonical.PositionAuthorityVersion == positionAuthorityVersion;
|
||||
}
|
||||
|
||||
public bool TryApplyMotion(
|
||||
WorldSession.EntityMotionUpdate update,
|
||||
bool retainPayload,
|
||||
Action<RuntimeEntityRecord>? acknowledgeProjection,
|
||||
out WorldSession.EntitySpawn accepted,
|
||||
out AcceptedPhysicsTimestamps timestamps)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
bool applied = Entities.TryApplyMotion(
|
||||
update,
|
||||
retainPayload,
|
||||
out accepted,
|
||||
out timestamps);
|
||||
if (Entities.TryGetSnapshot(
|
||||
update.Guid,
|
||||
out WorldSession.EntitySpawn snapshot)
|
||||
&& Entities.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
Entities.RefreshSnapshot(canonical, snapshot);
|
||||
if (applied && retainPayload)
|
||||
Entities.AdvanceMovementAuthority(canonical);
|
||||
acknowledgeProjection?.Invoke(canonical);
|
||||
if (applied && Entities.IsCurrent(canonical))
|
||||
PublishEntity(RuntimeEntityChange.Updated, canonical);
|
||||
if (applied && !Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
}
|
||||
|
||||
return applied;
|
||||
}
|
||||
|
||||
public bool TryApplyVector(
|
||||
VectorUpdate.Parsed update,
|
||||
Action<RuntimeEntityRecord>? acknowledgeProjection,
|
||||
out WorldSession.EntitySpawn accepted)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
bool applied = Entities.TryApplyVector(update, out accepted);
|
||||
if (!applied
|
||||
|| !Entities.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
return applied;
|
||||
}
|
||||
|
||||
Entities.RefreshSnapshot(canonical, accepted);
|
||||
Entities.AdvanceVectorAuthority(canonical);
|
||||
acknowledgeProjection?.Invoke(canonical);
|
||||
if (!Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
PublishEntity(RuntimeEntityChange.Updated, canonical);
|
||||
return Entities.IsCurrent(canonical);
|
||||
}
|
||||
|
||||
public bool TryApplyState(
|
||||
SetState.Parsed update,
|
||||
Action<RuntimeEntityRecord, RetailPhysicsStateTransition>?
|
||||
acknowledgeProjection,
|
||||
out WorldSession.EntitySpawn accepted,
|
||||
out RetailPhysicsStateTransition transition)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
bool applied = Entities.TryApplyState(update, out accepted);
|
||||
transition = default;
|
||||
if (!applied
|
||||
|| !Entities.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
return applied;
|
||||
}
|
||||
|
||||
Entities.RefreshSnapshot(canonical, accepted);
|
||||
transition = Entities.ApplyRawPhysicsState(
|
||||
canonical,
|
||||
update.PhysicsState);
|
||||
acknowledgeProjection?.Invoke(canonical, transition);
|
||||
if (!Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
PublishEntity(
|
||||
transition.HiddenTransition
|
||||
is RetailHiddenTransition.BecameHidden
|
||||
? RuntimeEntityChange.Hidden
|
||||
: RuntimeEntityChange.Updated,
|
||||
canonical);
|
||||
return Entities.IsCurrent(canonical);
|
||||
}
|
||||
|
||||
public bool TryApplyPosition(
|
||||
WorldSession.EntityPositionUpdate update,
|
||||
bool isLocalPlayer,
|
||||
System.Numerics.Quaternion? forcePositionRotation,
|
||||
System.Numerics.Vector3? currentLocalVelocity,
|
||||
bool projectionRequiresTeleportHook,
|
||||
Action<RuntimeEntityRecord>? acknowledgeProjection,
|
||||
out PositionTimestampDisposition disposition,
|
||||
out WorldSession.EntitySpawn accepted,
|
||||
out AcceptedPhysicsTimestamps timestamps)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
bool hadCanonical = Entities.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord beforeCanonical);
|
||||
uint beforeCell = beforeCanonical?.FullCellId ?? 0u;
|
||||
bool wasCellless = hadCanonical && beforeCell == 0u;
|
||||
bool known = Entities.TryApplyPosition(
|
||||
update,
|
||||
isLocalPlayer,
|
||||
forcePositionRotation,
|
||||
currentLocalVelocity,
|
||||
out disposition,
|
||||
out accepted,
|
||||
out timestamps);
|
||||
if (!known
|
||||
|| !Entities.TryGetSnapshot(
|
||||
update.Guid,
|
||||
out WorldSession.EntitySpawn snapshot)
|
||||
|| !Entities.TryGetActive(
|
||||
update.Guid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
return known;
|
||||
}
|
||||
|
||||
bool acceptedPosition =
|
||||
disposition is not PositionTimestampDisposition.Rejected;
|
||||
if (disposition is PositionTimestampDisposition.Apply)
|
||||
{
|
||||
timestamps = timestamps with
|
||||
{
|
||||
TeleportHookRequired =
|
||||
timestamps.TeleportAdvanced
|
||||
|| wasCellless
|
||||
|| projectionRequiresTeleportHook,
|
||||
};
|
||||
}
|
||||
|
||||
Entities.RefreshSnapshot(
|
||||
canonical,
|
||||
snapshot,
|
||||
refreshPosition: acceptedPosition);
|
||||
if (acceptedPosition
|
||||
&& ReferenceEquals(canonical, beforeCanonical))
|
||||
{
|
||||
Entities.AdvancePositionAuthority(canonical);
|
||||
Entities.ParentAttachments.EndChildProjection(update.Guid);
|
||||
}
|
||||
|
||||
acknowledgeProjection?.Invoke(canonical);
|
||||
if (!Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
if (acceptedPosition)
|
||||
{
|
||||
PublishEntity(
|
||||
beforeCell != canonical.FullCellId
|
||||
? RuntimeEntityChange.Rebucketed
|
||||
: RuntimeEntityChange.Updated,
|
||||
canonical);
|
||||
}
|
||||
return Entities.IsCurrent(canonical);
|
||||
}
|
||||
|
||||
public bool CommitRebucket(
|
||||
RuntimeEntityRecord canonical,
|
||||
uint fullCellId,
|
||||
uint canonicalLandblockId,
|
||||
Action<RuntimeEntityRecord>? acknowledgeProjection = null)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
ArgumentNullException.ThrowIfNull(canonical);
|
||||
if (!Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
|
||||
uint previous = canonical.FullCellId;
|
||||
Entities.SetFullCell(
|
||||
canonical,
|
||||
fullCellId,
|
||||
canonicalLandblockId);
|
||||
acknowledgeProjection?.Invoke(canonical);
|
||||
if (!Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
if (previous != fullCellId)
|
||||
PublishEntity(RuntimeEntityChange.Rebucketed, canonical);
|
||||
return Entities.IsCurrent(canonical);
|
||||
}
|
||||
|
||||
public bool CommitWithdrawal(
|
||||
RuntimeEntityRecord canonical,
|
||||
Action<RuntimeEntityRecord>? acknowledgeProjection = null)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
ArgumentNullException.ThrowIfNull(canonical);
|
||||
if (!Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
|
||||
Entities.SuspendObjectClock(canonical);
|
||||
Entities.SetFullCell(canonical, 0u, 0u);
|
||||
acknowledgeProjection?.Invoke(canonical);
|
||||
if (!Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
PublishEntity(RuntimeEntityChange.Withdrawn, canonical);
|
||||
return Entities.IsCurrent(canonical);
|
||||
}
|
||||
|
||||
public bool CommitChildNoDraw(
|
||||
RuntimeEntityRecord canonical,
|
||||
bool noDraw,
|
||||
Action<RuntimeEntityRecord>? acknowledgeProjection = null)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
ArgumentNullException.ThrowIfNull(canonical);
|
||||
if (!Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
|
||||
Entities.SetChildNoDraw(canonical, noDraw);
|
||||
acknowledgeProjection?.Invoke(canonical);
|
||||
if (!Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
PublishEntity(RuntimeEntityChange.Updated, canonical);
|
||||
return Entities.IsCurrent(canonical);
|
||||
}
|
||||
|
||||
public bool RetireAfterProjectionAcquisitionFailure(
|
||||
RuntimeEntityRecord canonical)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
ArgumentNullException.ThrowIfNull(canonical);
|
||||
if (!Entities.RemoveActive(canonical))
|
||||
return false;
|
||||
|
||||
Entities.AdvanceLifetimeMutation(canonical.ServerGuid);
|
||||
PublishEntity(RuntimeEntityChange.Deleted, canonical);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Accepts the exact DeleteObject generation without yet publishing the
|
||||
/// retained-object removal. This split lets the graphical host retire the
|
||||
/// active identity first, matching the established reentrant callback
|
||||
/// order, while Runtime remains the only freshness authority.
|
||||
/// Accepts and retires the exact canonical DeleteObject generation without
|
||||
/// yet publishing the retained-object removal. This split lets a graphical
|
||||
/// host tear down the exact retired projection before completing the
|
||||
/// retained-object mutation while Runtime remains the only freshness,
|
||||
/// identity, and canonical-lifetime authority.
|
||||
/// </summary>
|
||||
public bool TryAcceptDelete(
|
||||
DeleteObject.Parsed delete,
|
||||
bool isLocalPlayer,
|
||||
bool removeRetainedObject,
|
||||
out RuntimeEntityDeleteAcceptance acceptance)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
if (!Entities.TryDelete(delete, isLocalPlayer))
|
||||
{
|
||||
acceptance = null!;
|
||||
return false;
|
||||
}
|
||||
|
||||
acceptance = new RuntimeEntityDeleteAcceptance(this, delete);
|
||||
Entities.AdvanceLifetimeMutation(delete.Guid);
|
||||
Entities.ParentAttachments.DeleteGeneration(
|
||||
delete.Guid,
|
||||
delete.InstanceSequence);
|
||||
|
||||
RuntimeEntityRecord? retiredCanonical = null;
|
||||
if (Entities.TryGetActive(
|
||||
delete.Guid,
|
||||
out RuntimeEntityRecord active)
|
||||
&& active.Incarnation == delete.InstanceSequence
|
||||
&& Entities.RemoveActive(active))
|
||||
{
|
||||
retiredCanonical = active;
|
||||
PublishEntity(
|
||||
removeRetainedObject
|
||||
? RuntimeEntityChange.Deleted
|
||||
: RuntimeEntityChange.Withdrawn,
|
||||
active);
|
||||
}
|
||||
|
||||
acceptance = new RuntimeEntityDeleteAcceptance(
|
||||
this,
|
||||
delete,
|
||||
retiredCanonical,
|
||||
removeRetainedObject);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -104,6 +754,7 @@ public sealed class RuntimeEntityObjectLifetime
|
|||
public void CompleteAcceptedDelete(
|
||||
RuntimeEntityDeleteAcceptance acceptance)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
ArgumentNullException.ThrowIfNull(acceptance);
|
||||
if (!ReferenceEquals(acceptance.Owner, this))
|
||||
{
|
||||
|
|
@ -117,19 +768,132 @@ public sealed class RuntimeEntityObjectLifetime
|
|||
}
|
||||
|
||||
acceptance.Completed = true;
|
||||
ObjectTableWiring.ApplyEntityDelete(Objects, acceptance.Delete);
|
||||
if (acceptance.RemoveRetainedObject)
|
||||
ObjectTableWiring.ApplyEntityDelete(Objects, acceptance.Delete);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies a delete accepted by the dormant exact-incarnation owner after
|
||||
/// the active Runtime directory correctly reports no live record.
|
||||
/// </summary>
|
||||
public void ApplyAcceptedDormantDelete(DeleteObject.Parsed delete) =>
|
||||
public void ApplyAcceptedDormantDelete(DeleteObject.Parsed delete)
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
ObjectTableWiring.ApplyEntityDelete(Objects, delete);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears retained object state at the Runtime-owned reset stage. App
|
||||
/// projection teardown remains a later acknowledged stage.
|
||||
/// </summary>
|
||||
public void ClearObjects() => Objects.Clear();
|
||||
public void ClearObjects()
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
Objects.Clear();
|
||||
}
|
||||
|
||||
public IReadOnlyList<RuntimeEntityRecord> BeginSessionClear()
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
if (_sessionClearInProgress)
|
||||
return Array.Empty<RuntimeEntityRecord>();
|
||||
|
||||
_sessionClearInProgress = true;
|
||||
Entities.BeginSessionClear();
|
||||
RuntimeEntityRecord[] retired = Entities.ActiveRecords.ToArray();
|
||||
foreach (RuntimeEntityRecord canonical in retired)
|
||||
{
|
||||
if (!Entities.RemoveActive(canonical))
|
||||
continue;
|
||||
PublishEntity(RuntimeEntityChange.Deleted, canonical);
|
||||
}
|
||||
return retired;
|
||||
}
|
||||
|
||||
public bool CompleteSessionClearIfConverged()
|
||||
{
|
||||
EnsureNotDisposed();
|
||||
if (!_sessionClearInProgress
|
||||
|| !Entities.CompleteSessionClearIfConverged())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_sessionClearInProgress = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
_disposed = true;
|
||||
Events.Dispose();
|
||||
}
|
||||
|
||||
private bool CommitPositionChannelUpdate(
|
||||
bool applied,
|
||||
uint guid,
|
||||
WorldSession.EntitySpawn accepted,
|
||||
Action<RuntimeEntityRecord>? acknowledgeProjection)
|
||||
{
|
||||
if (!applied
|
||||
|| !Entities.TryGetActive(
|
||||
guid,
|
||||
out RuntimeEntityRecord canonical))
|
||||
{
|
||||
return applied;
|
||||
}
|
||||
|
||||
Entities.RefreshSnapshot(canonical, accepted);
|
||||
Entities.AdvancePositionAuthority(canonical);
|
||||
acknowledgeProjection?.Invoke(canonical);
|
||||
if (!Entities.IsCurrent(canonical))
|
||||
return false;
|
||||
PublishEntity(RuntimeEntityChange.Updated, canonical);
|
||||
return Entities.IsCurrent(canonical);
|
||||
}
|
||||
|
||||
private void PublishEntity(
|
||||
RuntimeEntityChange change,
|
||||
RuntimeEntityRecord canonical) =>
|
||||
Events.PublishEntity(change, canonical);
|
||||
|
||||
private void EnsureNotDisposed() =>
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
private static Exception? Combine(
|
||||
Exception? first,
|
||||
Exception? second) =>
|
||||
first is null
|
||||
? second
|
||||
: second is null
|
||||
? first
|
||||
: new AggregateException(first, second);
|
||||
|
||||
private static InboundCreateResult SupersededCreateResult() => new(
|
||||
CreateObjectTimestampDisposition.StaleGeneration,
|
||||
default,
|
||||
null,
|
||||
default);
|
||||
|
||||
private bool IsCurrentOperation(
|
||||
uint guid,
|
||||
RuntimeEntityRecord canonical,
|
||||
ulong sessionVersion,
|
||||
ulong operationVersion) =>
|
||||
Entities.SessionLifetimeVersion == sessionVersion
|
||||
&& Entities.CurrentLifetimeMutation(guid) == operationVersion
|
||||
&& Entities.IsCurrent(canonical);
|
||||
|
||||
private RuntimeEntityRegistrationResult SupersededRegistration(
|
||||
uint guid,
|
||||
bool replacedExistingGeneration) =>
|
||||
new(
|
||||
SupersededCreateResult(),
|
||||
Entities.TryGetActive(guid, out RuntimeEntityRecord current)
|
||||
? current
|
||||
: null,
|
||||
LogicalRegistrationCreated: false,
|
||||
ReplacedExistingGeneration: replacedExistingGeneration);
|
||||
}
|
||||
|
|
|
|||
151
src/AcDream.Runtime/Entities/RuntimeEntityObjectViews.cs
Normal file
151
src/AcDream.Runtime/Entities/RuntimeEntityObjectViews.cs
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
using AcDream.Core.Items;
|
||||
using AcDream.Core.Net.Messages;
|
||||
using AcDream.Core.Physics;
|
||||
using System.Numerics;
|
||||
|
||||
namespace AcDream.Runtime.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Allocation-free borrowed views over the canonical entity/object lifetime.
|
||||
/// These views contain no App projection or backend state.
|
||||
/// </summary>
|
||||
internal sealed class RuntimeEntityObjectViews
|
||||
{
|
||||
public RuntimeEntityObjectViews(
|
||||
RuntimeEntityDirectory entities,
|
||||
ClientObjectTable objects)
|
||||
{
|
||||
Entities = new EntityView(
|
||||
entities ?? throw new ArgumentNullException(nameof(entities)));
|
||||
Inventory = new InventoryView(
|
||||
entities,
|
||||
objects ?? throw new ArgumentNullException(nameof(objects)));
|
||||
}
|
||||
|
||||
public IRuntimeEntityView Entities { get; }
|
||||
public IRuntimeInventoryView Inventory { get; }
|
||||
|
||||
internal static RuntimeEntitySnapshot Snapshot(RuntimeEntityRecord record)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(record);
|
||||
uint localEntityId = record.LocalEntityId
|
||||
?? throw new InvalidOperationException(
|
||||
$"Canonical entity 0x{record.ServerGuid:X8}/{record.Incarnation} has no Runtime identity.");
|
||||
return new RuntimeEntitySnapshot(
|
||||
new RuntimeEntityIdentity(
|
||||
record.ServerGuid,
|
||||
localEntityId,
|
||||
record.Incarnation),
|
||||
record.FullCellId,
|
||||
(uint)record.FinalPhysicsState,
|
||||
ConvertPosition(record.Snapshot.Position));
|
||||
}
|
||||
|
||||
private static Position? ConvertPosition(
|
||||
CreateObject.ServerPosition? position) =>
|
||||
position is not { } value
|
||||
? null
|
||||
: new Position(
|
||||
value.LandblockId,
|
||||
new Vector3(
|
||||
value.PositionX,
|
||||
value.PositionY,
|
||||
value.PositionZ),
|
||||
new Quaternion(
|
||||
value.RotationX,
|
||||
value.RotationY,
|
||||
value.RotationZ,
|
||||
value.RotationW));
|
||||
|
||||
internal static RuntimeInventoryItemSnapshot Snapshot(
|
||||
ClientObject item,
|
||||
RuntimeEntityDirectory entities,
|
||||
ushort? exactGeneration = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(item);
|
||||
ushort incarnation = exactGeneration
|
||||
?? (entities.TryGetActive(
|
||||
item.ObjectId,
|
||||
out RuntimeEntityRecord canonical)
|
||||
? canonical.Incarnation
|
||||
: (ushort)0);
|
||||
return new RuntimeInventoryItemSnapshot(
|
||||
item.ObjectId,
|
||||
incarnation,
|
||||
item.Name,
|
||||
item.ContainerId,
|
||||
item.ContainerSlot,
|
||||
item.WielderId,
|
||||
(uint)item.CurrentlyEquippedLocation,
|
||||
item.StackSize,
|
||||
item.Value);
|
||||
}
|
||||
|
||||
private sealed class EntityView(RuntimeEntityDirectory owner)
|
||||
: IRuntimeEntityView
|
||||
{
|
||||
public int Count => owner.Count;
|
||||
|
||||
public bool TryGet(
|
||||
uint serverGuid,
|
||||
out RuntimeEntitySnapshot entity)
|
||||
{
|
||||
if (owner.TryGetActive(
|
||||
serverGuid,
|
||||
out RuntimeEntityRecord record))
|
||||
{
|
||||
entity = Snapshot(record);
|
||||
return true;
|
||||
}
|
||||
|
||||
entity = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Visit(IRuntimeEntityVisitor visitor)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(visitor);
|
||||
foreach (RuntimeEntityRecord record in owner.ActiveRecords)
|
||||
{
|
||||
RuntimeEntitySnapshot entity = Snapshot(record);
|
||||
visitor.Visit(in entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class InventoryView(
|
||||
RuntimeEntityDirectory entities,
|
||||
ClientObjectTable owner)
|
||||
: IRuntimeInventoryView
|
||||
{
|
||||
public int ObjectCount => owner.ObjectCount;
|
||||
public int ContainerCount => owner.ContainerCount;
|
||||
|
||||
public bool TryGet(
|
||||
uint objectId,
|
||||
out RuntimeInventoryItemSnapshot item)
|
||||
{
|
||||
ClientObject? found = owner.Get(objectId);
|
||||
if (found is not null)
|
||||
{
|
||||
item = Snapshot(found, entities);
|
||||
return true;
|
||||
}
|
||||
|
||||
item = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Visit(IRuntimeInventoryVisitor visitor)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(visitor);
|
||||
foreach (ClientObject item in owner.Objects)
|
||||
{
|
||||
RuntimeInventoryItemSnapshot snapshot = Snapshot(
|
||||
item,
|
||||
entities);
|
||||
visitor.Visit(in snapshot);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -225,12 +225,26 @@ public sealed class RuntimeTraceRecorder : IRuntimeEventObserver
|
|||
/// <summary>Monotonic sequence owner for one runtime instance.</summary>
|
||||
public sealed class RuntimeEventSequencer
|
||||
{
|
||||
private RuntimeGenerationToken _generation;
|
||||
private bool _hasGeneration;
|
||||
private ulong _sequence;
|
||||
|
||||
public RuntimeEventStamp Next(
|
||||
RuntimeGenerationToken generation,
|
||||
ulong frameNumber) =>
|
||||
new(generation, checked(++_sequence), frameNumber);
|
||||
ulong frameNumber)
|
||||
{
|
||||
if (!_hasGeneration || generation != _generation)
|
||||
{
|
||||
_generation = generation;
|
||||
_sequence = 0;
|
||||
_hasGeneration = true;
|
||||
}
|
||||
|
||||
return new RuntimeEventStamp(
|
||||
generation,
|
||||
checked(++_sequence),
|
||||
frameNumber);
|
||||
}
|
||||
|
||||
public ulong LastSequence => _sequence;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,10 +11,7 @@ public readonly record struct RuntimeEntitySnapshot(
|
|||
RuntimeEntityIdentity Identity,
|
||||
uint CellId,
|
||||
uint PhysicsState,
|
||||
Position? Position,
|
||||
bool IsMaterialized,
|
||||
bool IsSpatiallyVisible,
|
||||
bool IsHydrated);
|
||||
Position? Position);
|
||||
|
||||
public interface IRuntimeEntityVisitor
|
||||
{
|
||||
|
|
@ -25,8 +22,6 @@ public interface IRuntimeEntityView
|
|||
{
|
||||
int Count { get; }
|
||||
|
||||
int MaterializedCount { get; }
|
||||
|
||||
bool TryGet(uint serverGuid, out RuntimeEntitySnapshot entity);
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue