feat(runtime): define borrowed views commands and ordered events
Establish the J1 presentation-independent contract with instance-scoped clocks and generations, immutable borrowed views, typed generation-gated commands, normalized ordered diagnostics, and teardown acknowledgements. Route graphical startup plus press-time selection, movement, and combat through focused App adapters over the exact existing owners without adding a queue or mirrored world. Validated by the Release solution build, 13 Runtime tests, 3,838 App tests with three existing skips, and the complete 8,424-test Release suite with five existing skips. Co-authored-by: Codex <codex@openai.com>
This commit is contained in:
parent
afebbe3eca
commit
854d9e9cd1
21 changed files with 2594 additions and 44 deletions
106
src/AcDream.App/Runtime/CurrentGameRuntimeAdapter.cs
Normal file
106
src/AcDream.App/Runtime/CurrentGameRuntimeAdapter.cs
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
using AcDream.App.Combat;
|
||||
using AcDream.App.Input;
|
||||
using AcDream.App.Interaction;
|
||||
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;
|
||||
|
||||
namespace AcDream.App.Runtime;
|
||||
|
||||
/// <summary>
|
||||
/// App composition root for Slice J's borrowed runtime boundary. Focused
|
||||
/// adapters project the existing canonical owners; this root owns only those
|
||||
/// adapters and never owns or copies gameplay state.
|
||||
/// </summary>
|
||||
internal sealed class CurrentGameRuntimeAdapter
|
||||
: IGameRuntimeView,
|
||||
IGameRuntimeCommands,
|
||||
IRuntimeEventSource,
|
||||
IDisposable
|
||||
{
|
||||
private readonly CurrentGameRuntimeViewAdapter _view;
|
||||
private readonly CurrentGameRuntimeEventAdapter _events;
|
||||
private readonly CurrentGameRuntimeCommandAdapter _commands;
|
||||
private bool _disposed;
|
||||
|
||||
public CurrentGameRuntimeAdapter(
|
||||
RuntimeOptions options,
|
||||
LiveSessionController session,
|
||||
LiveSessionHost sessionHost,
|
||||
LocalPlayerIdentityState playerIdentity,
|
||||
LiveEntityRuntime entities,
|
||||
ClientObjectTable objects,
|
||||
ChatLog chat,
|
||||
ILocalPlayerControllerSource playerController,
|
||||
WorldRevealCoordinator worldReveal,
|
||||
IGameRuntimeClock clock,
|
||||
SelectionState selectionState,
|
||||
SelectionInteractionController selection,
|
||||
GameplayInputFrameController gameplayInput,
|
||||
ILiveCombatModeCommand combat)
|
||||
{
|
||||
_view = new CurrentGameRuntimeViewAdapter(
|
||||
session,
|
||||
playerIdentity,
|
||||
entities,
|
||||
objects,
|
||||
chat,
|
||||
playerController,
|
||||
worldReveal,
|
||||
clock);
|
||||
_events = new CurrentGameRuntimeEventAdapter(
|
||||
_view,
|
||||
entities,
|
||||
objects,
|
||||
chat,
|
||||
clock);
|
||||
_commands = new CurrentGameRuntimeCommandAdapter(
|
||||
options,
|
||||
session,
|
||||
sessionHost,
|
||||
_view,
|
||||
selectionState,
|
||||
selection,
|
||||
gameplayInput,
|
||||
combat,
|
||||
_events);
|
||||
}
|
||||
|
||||
public RuntimeGenerationToken Generation => _view.Generation;
|
||||
public RuntimeLifecycleSnapshot Lifecycle => _view.Lifecycle;
|
||||
public IGameRuntimeClock Clock => _view.Clock;
|
||||
public IRuntimeEntityView Entities => _view.Entities;
|
||||
public IRuntimeInventoryView Inventory => _view.Inventory;
|
||||
public IRuntimeChatView Chat => _view.Chat;
|
||||
public IRuntimeMovementView Movement => _view.Movement;
|
||||
public IRuntimePortalView Portal => _view.Portal;
|
||||
|
||||
public IRuntimeSessionCommands Session => _commands;
|
||||
public IRuntimeSelectionCommands Selection => _commands;
|
||||
public IRuntimeCombatCommands Combat => _commands;
|
||||
public IRuntimeMovementCommands MovementCommands => _commands;
|
||||
IRuntimeMovementCommands IGameRuntimeCommands.Movement => _commands;
|
||||
public IRuntimeChatCommands ChatCommands => _commands;
|
||||
IRuntimeChatCommands IGameRuntimeCommands.Chat => _commands;
|
||||
public IRuntimePortalCommands PortalCommands => _commands;
|
||||
IRuntimePortalCommands IGameRuntimeCommands.Portal => _commands;
|
||||
|
||||
public RuntimeStateCheckpoint CaptureCheckpoint() =>
|
||||
_view.CaptureCheckpoint();
|
||||
|
||||
public IDisposable Subscribe(IRuntimeEventObserver observer) =>
|
||||
_events.Subscribe(observer);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
_disposed = true;
|
||||
_view.Deactivate();
|
||||
_events.Dispose();
|
||||
}
|
||||
}
|
||||
394
src/AcDream.App/Runtime/CurrentGameRuntimeCommandAdapter.cs
Normal file
394
src/AcDream.App/Runtime/CurrentGameRuntimeCommandAdapter.cs
Normal file
|
|
@ -0,0 +1,394 @@
|
|||
using AcDream.App.Combat;
|
||||
using AcDream.App.Input;
|
||||
using AcDream.App.Interaction;
|
||||
using AcDream.App.Net;
|
||||
using AcDream.Core.Selection;
|
||||
using AcDream.Runtime;
|
||||
using AcDream.UI.Abstractions;
|
||||
using AcDream.UI.Abstractions.Input;
|
||||
|
||||
namespace AcDream.App.Runtime;
|
||||
|
||||
/// <summary>
|
||||
/// Generation-gated synchronous command projection onto the same current
|
||||
/// owners used by graphical input. It introduces no queue or frame boundary.
|
||||
/// </summary>
|
||||
internal sealed class CurrentGameRuntimeCommandAdapter
|
||||
: IRuntimeSessionCommands,
|
||||
IRuntimeSelectionCommands,
|
||||
IRuntimeCombatCommands,
|
||||
IRuntimeMovementCommands,
|
||||
IRuntimeChatCommands,
|
||||
IRuntimePortalCommands
|
||||
{
|
||||
private readonly RuntimeOptions _options;
|
||||
private readonly LiveSessionController _session;
|
||||
private readonly LiveSessionHost _sessionHost;
|
||||
private readonly CurrentGameRuntimeViewAdapter _view;
|
||||
private readonly SelectionState _selectionState;
|
||||
private readonly SelectionInteractionController _selection;
|
||||
private readonly GameplayInputFrameController _gameplayInput;
|
||||
private readonly ILiveCombatModeCommand _combat;
|
||||
private readonly ICurrentGameRuntimeEventSink _events;
|
||||
|
||||
public CurrentGameRuntimeCommandAdapter(
|
||||
RuntimeOptions options,
|
||||
LiveSessionController session,
|
||||
LiveSessionHost sessionHost,
|
||||
CurrentGameRuntimeViewAdapter view,
|
||||
SelectionState selectionState,
|
||||
SelectionInteractionController selection,
|
||||
GameplayInputFrameController gameplayInput,
|
||||
ILiveCombatModeCommand combat,
|
||||
ICurrentGameRuntimeEventSink events)
|
||||
{
|
||||
_options = options ?? throw new ArgumentNullException(nameof(options));
|
||||
_session = session ?? throw new ArgumentNullException(nameof(session));
|
||||
_sessionHost = sessionHost ?? throw new ArgumentNullException(nameof(sessionHost));
|
||||
_view = view ?? throw new ArgumentNullException(nameof(view));
|
||||
_selectionState = selectionState
|
||||
?? throw new ArgumentNullException(nameof(selectionState));
|
||||
_selection = selection ?? throw new ArgumentNullException(nameof(selection));
|
||||
_gameplayInput = gameplayInput
|
||||
?? throw new ArgumentNullException(nameof(gameplayInput));
|
||||
_combat = combat ?? throw new ArgumentNullException(nameof(combat));
|
||||
_events = events ?? throw new ArgumentNullException(nameof(events));
|
||||
}
|
||||
|
||||
public RuntimeSessionStartResult Start(
|
||||
RuntimeGenerationToken expectedGeneration)
|
||||
{
|
||||
RuntimeLifecycleState previous = _view.Lifecycle.State;
|
||||
RuntimeCommandStatus gate = Validate(expectedGeneration, requireWorld: false);
|
||||
if (gate != RuntimeCommandStatus.Accepted)
|
||||
return RejectedStart(gate);
|
||||
|
||||
LiveSessionStartResult direct = _sessionHost.Start(_options);
|
||||
RuntimeSessionStartResult result = Convert(direct);
|
||||
_events.EmitCommand(
|
||||
RuntimeCommandDomain.Session,
|
||||
operation: 0,
|
||||
ToCommandStatus(result.Status),
|
||||
result.CharacterId,
|
||||
result.CharacterName);
|
||||
_events.EmitLifecycle(previous);
|
||||
return result;
|
||||
}
|
||||
|
||||
public RuntimeSessionStartResult Reconnect(
|
||||
RuntimeGenerationToken expectedGeneration)
|
||||
{
|
||||
RuntimeLifecycleState previous = _view.Lifecycle.State;
|
||||
RuntimeCommandStatus gate = Validate(expectedGeneration, requireWorld: false);
|
||||
if (gate != RuntimeCommandStatus.Accepted)
|
||||
return RejectedStart(gate);
|
||||
|
||||
LiveSessionStartResult direct = _sessionHost.Reconnect(_options);
|
||||
RuntimeSessionStartResult result = Convert(direct);
|
||||
_events.EmitCommand(
|
||||
RuntimeCommandDomain.Session,
|
||||
operation: 1,
|
||||
ToCommandStatus(result.Status),
|
||||
result.CharacterId,
|
||||
result.CharacterName);
|
||||
_events.EmitLifecycle(previous);
|
||||
return result;
|
||||
}
|
||||
|
||||
public RuntimeTeardownAcknowledgement Stop(
|
||||
RuntimeGenerationToken expectedGeneration)
|
||||
{
|
||||
RuntimeLifecycleState previous = _view.Lifecycle.State;
|
||||
RuntimeCommandStatus gate = Validate(expectedGeneration, requireWorld: false);
|
||||
if (gate != RuntimeCommandStatus.Accepted)
|
||||
{
|
||||
return new RuntimeTeardownAcknowledgement(
|
||||
expectedGeneration,
|
||||
_view.Generation,
|
||||
gate,
|
||||
RuntimeTeardownStage.None);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_session.Stop();
|
||||
RuntimeGenerationToken current = _view.Generation;
|
||||
RuntimeTeardownStage completed =
|
||||
_session.CurrentSession is null && !_session.IsInWorld
|
||||
? RuntimeTeardownStage.Complete
|
||||
: RuntimeTeardownStage.CommandsInert
|
||||
| RuntimeTeardownStage.InboundDetached;
|
||||
var acknowledgement = new RuntimeTeardownAcknowledgement(
|
||||
expectedGeneration,
|
||||
current,
|
||||
RuntimeCommandStatus.Accepted,
|
||||
completed);
|
||||
_events.EmitCommand(
|
||||
RuntimeCommandDomain.Session,
|
||||
operation: 2,
|
||||
RuntimeCommandStatus.Accepted);
|
||||
_events.EmitLifecycle(previous);
|
||||
return acknowledgement;
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
var acknowledgement = new RuntimeTeardownAcknowledgement(
|
||||
expectedGeneration,
|
||||
_view.Generation,
|
||||
RuntimeCommandStatus.Rejected,
|
||||
RuntimeTeardownStage.None,
|
||||
error);
|
||||
_events.EmitCommand(
|
||||
RuntimeCommandDomain.Session,
|
||||
operation: 2,
|
||||
RuntimeCommandStatus.Rejected,
|
||||
text: error.GetType().Name);
|
||||
_events.EmitLifecycle(previous);
|
||||
return acknowledgement;
|
||||
}
|
||||
}
|
||||
|
||||
public RuntimeCommandResult Execute(
|
||||
RuntimeGenerationToken expectedGeneration,
|
||||
RuntimeSelectionCommand command)
|
||||
{
|
||||
RuntimeCommandStatus gate = Validate(expectedGeneration, requireWorld: true);
|
||||
if (gate != RuntimeCommandStatus.Accepted)
|
||||
return Result(gate);
|
||||
|
||||
InputAction action = command switch
|
||||
{
|
||||
RuntimeSelectionCommand.SelectClosestHostile =>
|
||||
InputAction.SelectionClosestMonster,
|
||||
RuntimeSelectionCommand.SelectPrevious =>
|
||||
InputAction.SelectionPreviousSelection,
|
||||
RuntimeSelectionCommand.ExamineSelected =>
|
||||
InputAction.SelectionExamine,
|
||||
RuntimeSelectionCommand.UseSelected =>
|
||||
InputAction.UseSelected,
|
||||
RuntimeSelectionCommand.PickUpSelected =>
|
||||
InputAction.SelectionPickUp,
|
||||
_ => InputAction.None,
|
||||
};
|
||||
RuntimeCommandStatus status = action != InputAction.None
|
||||
&& _selection.HandleInputAction(action)
|
||||
? RuntimeCommandStatus.Accepted
|
||||
: RuntimeCommandStatus.Unsupported;
|
||||
uint selected = _selectionState.SelectedObjectId ?? 0u;
|
||||
_events.EmitCommand(
|
||||
RuntimeCommandDomain.Selection,
|
||||
(int)command,
|
||||
status,
|
||||
selected);
|
||||
return Result(status, selected);
|
||||
}
|
||||
|
||||
public RuntimeCommandResult Execute(
|
||||
RuntimeGenerationToken expectedGeneration,
|
||||
RuntimeCombatCommand command)
|
||||
{
|
||||
RuntimeCommandStatus gate = Validate(expectedGeneration, requireWorld: true);
|
||||
if (gate != RuntimeCommandStatus.Accepted)
|
||||
return Result(gate);
|
||||
|
||||
RuntimeCommandStatus status;
|
||||
if (command == RuntimeCombatCommand.ToggleMode)
|
||||
{
|
||||
_combat.Toggle();
|
||||
status = RuntimeCommandStatus.Accepted;
|
||||
}
|
||||
else
|
||||
{
|
||||
status = RuntimeCommandStatus.Unsupported;
|
||||
}
|
||||
|
||||
_events.EmitCommand(RuntimeCommandDomain.Combat, (int)command, status);
|
||||
return Result(status);
|
||||
}
|
||||
|
||||
public RuntimeCommandResult Execute(
|
||||
RuntimeGenerationToken expectedGeneration,
|
||||
RuntimeMovementCommand command)
|
||||
{
|
||||
RuntimeCommandStatus gate = Validate(expectedGeneration, requireWorld: true);
|
||||
if (gate != RuntimeCommandStatus.Accepted)
|
||||
return Result(gate);
|
||||
|
||||
InputAction action = command switch
|
||||
{
|
||||
RuntimeMovementCommand.ToggleRunLock => InputAction.MovementRunLock,
|
||||
RuntimeMovementCommand.Stop => InputAction.MovementStop,
|
||||
_ => InputAction.None,
|
||||
};
|
||||
RuntimeCommandStatus status;
|
||||
if (action == InputAction.None)
|
||||
{
|
||||
status = RuntimeCommandStatus.Unsupported;
|
||||
}
|
||||
else
|
||||
{
|
||||
_gameplayInput.HandlePressedMovementAction(action);
|
||||
status = RuntimeCommandStatus.Accepted;
|
||||
}
|
||||
|
||||
_events.EmitCommand(RuntimeCommandDomain.Movement, (int)command, status);
|
||||
return Result(status);
|
||||
}
|
||||
|
||||
public RuntimeCommandResult Execute(
|
||||
RuntimeGenerationToken expectedGeneration,
|
||||
in RuntimeChatCommand command)
|
||||
{
|
||||
RuntimeCommandStatus gate = Validate(expectedGeneration, requireWorld: true);
|
||||
if (gate != RuntimeCommandStatus.Accepted)
|
||||
return Result(gate);
|
||||
|
||||
if (!TryMap(command.Channel, out ChatChannelKind channel))
|
||||
{
|
||||
_events.EmitCommand(
|
||||
RuntimeCommandDomain.Chat,
|
||||
(int)command.Channel,
|
||||
RuntimeCommandStatus.Unsupported);
|
||||
return Result(RuntimeCommandStatus.Unsupported);
|
||||
}
|
||||
|
||||
_session.Commands.Publish(new SendChatCmd(
|
||||
channel,
|
||||
command.TargetName,
|
||||
command.Text));
|
||||
_events.EmitCommand(
|
||||
RuntimeCommandDomain.Chat,
|
||||
(int)command.Channel,
|
||||
RuntimeCommandStatus.Accepted,
|
||||
text: command.Text);
|
||||
return Result(RuntimeCommandStatus.Accepted);
|
||||
}
|
||||
|
||||
public RuntimeCommandResult Execute(
|
||||
RuntimeGenerationToken expectedGeneration,
|
||||
RuntimePortalCommand command)
|
||||
{
|
||||
RuntimeCommandStatus gate = Validate(expectedGeneration, requireWorld: true);
|
||||
if (gate != RuntimeCommandStatus.Accepted)
|
||||
return Result(gate);
|
||||
|
||||
ClientCommandId? commandId = command switch
|
||||
{
|
||||
RuntimePortalCommand.RecallLifestone =>
|
||||
ClientCommandId.LifestoneRecall,
|
||||
RuntimePortalCommand.RecallMarketplace =>
|
||||
ClientCommandId.MarketplaceRecall,
|
||||
RuntimePortalCommand.RecallHouse =>
|
||||
ClientCommandId.HouseRecall,
|
||||
RuntimePortalCommand.RecallMansion =>
|
||||
ClientCommandId.MansionRecall,
|
||||
_ => null,
|
||||
};
|
||||
if (commandId is null)
|
||||
{
|
||||
_events.EmitCommand(
|
||||
RuntimeCommandDomain.Portal,
|
||||
(int)command,
|
||||
RuntimeCommandStatus.Unsupported);
|
||||
return Result(RuntimeCommandStatus.Unsupported);
|
||||
}
|
||||
|
||||
_session.Commands.Publish(new ExecuteClientCommandCmd(
|
||||
commandId.Value,
|
||||
string.Empty));
|
||||
_events.EmitCommand(
|
||||
RuntimeCommandDomain.Portal,
|
||||
(int)command,
|
||||
RuntimeCommandStatus.Accepted);
|
||||
return Result(RuntimeCommandStatus.Accepted);
|
||||
}
|
||||
|
||||
private RuntimeCommandStatus Validate(
|
||||
RuntimeGenerationToken expectedGeneration,
|
||||
bool requireWorld)
|
||||
{
|
||||
if (!_view.IsActive)
|
||||
return RuntimeCommandStatus.Inactive;
|
||||
if (expectedGeneration != _view.Generation)
|
||||
return RuntimeCommandStatus.StaleGeneration;
|
||||
if (requireWorld && !_session.IsInWorld)
|
||||
return RuntimeCommandStatus.Inactive;
|
||||
return RuntimeCommandStatus.Accepted;
|
||||
}
|
||||
|
||||
private RuntimeCommandResult Result(
|
||||
RuntimeCommandStatus status,
|
||||
uint objectId = 0u) =>
|
||||
new(status, _view.Generation, objectId);
|
||||
|
||||
private RuntimeSessionStartResult RejectedStart(RuntimeCommandStatus status) =>
|
||||
new(
|
||||
status == RuntimeCommandStatus.StaleGeneration
|
||||
? RuntimeSessionStartStatus.StaleGeneration
|
||||
: RuntimeSessionStartStatus.Inactive,
|
||||
_view.Generation);
|
||||
|
||||
private RuntimeSessionStartResult Convert(LiveSessionStartResult result)
|
||||
{
|
||||
RuntimeSessionStartStatus status = result.Status switch
|
||||
{
|
||||
LiveSessionStartStatus.Disabled => RuntimeSessionStartStatus.Disabled,
|
||||
LiveSessionStartStatus.MissingCredentials =>
|
||||
RuntimeSessionStartStatus.MissingCredentials,
|
||||
LiveSessionStartStatus.NoCharacters =>
|
||||
RuntimeSessionStartStatus.NoCharacters,
|
||||
LiveSessionStartStatus.Connected =>
|
||||
RuntimeSessionStartStatus.Connected,
|
||||
LiveSessionStartStatus.Deferred =>
|
||||
RuntimeSessionStartStatus.Deferred,
|
||||
LiveSessionStartStatus.Failed =>
|
||||
RuntimeSessionStartStatus.Failed,
|
||||
_ => throw new ArgumentOutOfRangeException(
|
||||
nameof(result),
|
||||
result.Status,
|
||||
"Unknown live-session start result."),
|
||||
};
|
||||
return new RuntimeSessionStartResult(
|
||||
status,
|
||||
_view.Generation,
|
||||
result.Selection?.CharacterId ?? 0u,
|
||||
result.Selection?.CharacterName ?? string.Empty,
|
||||
result.Error);
|
||||
}
|
||||
|
||||
private static RuntimeCommandStatus ToCommandStatus(
|
||||
RuntimeSessionStartStatus status) =>
|
||||
status switch
|
||||
{
|
||||
RuntimeSessionStartStatus.Failed => RuntimeCommandStatus.Rejected,
|
||||
RuntimeSessionStartStatus.Inactive => RuntimeCommandStatus.Inactive,
|
||||
RuntimeSessionStartStatus.StaleGeneration =>
|
||||
RuntimeCommandStatus.StaleGeneration,
|
||||
_ => RuntimeCommandStatus.Accepted,
|
||||
};
|
||||
|
||||
private static bool TryMap(
|
||||
RuntimeChatChannel channel,
|
||||
out ChatChannelKind result)
|
||||
{
|
||||
result = channel switch
|
||||
{
|
||||
RuntimeChatChannel.Say => ChatChannelKind.Say,
|
||||
RuntimeChatChannel.Tell => ChatChannelKind.Tell,
|
||||
RuntimeChatChannel.Fellowship => ChatChannelKind.Fellowship,
|
||||
RuntimeChatChannel.Allegiance => ChatChannelKind.Allegiance,
|
||||
RuntimeChatChannel.Vassals => ChatChannelKind.Vassals,
|
||||
RuntimeChatChannel.Patron => ChatChannelKind.Patron,
|
||||
RuntimeChatChannel.Monarch => ChatChannelKind.Monarch,
|
||||
RuntimeChatChannel.CoVassals => ChatChannelKind.CoVassals,
|
||||
RuntimeChatChannel.General => ChatChannelKind.General,
|
||||
RuntimeChatChannel.Trade => ChatChannelKind.Trade,
|
||||
RuntimeChatChannel.LookingForGroup => ChatChannelKind.Lfg,
|
||||
RuntimeChatChannel.Roleplay => ChatChannelKind.Roleplay,
|
||||
RuntimeChatChannel.Society => ChatChannelKind.Society,
|
||||
RuntimeChatChannel.Olthoi => ChatChannelKind.Olthoi,
|
||||
_ => ChatChannelKind.Unknown,
|
||||
};
|
||||
return result != ChatChannelKind.Unknown;
|
||||
}
|
||||
}
|
||||
288
src/AcDream.App/Runtime/CurrentGameRuntimeEventAdapter.cs
Normal file
288
src/AcDream.App/Runtime/CurrentGameRuntimeEventAdapter.cs
Normal file
|
|
@ -0,0 +1,288 @@
|
|||
using AcDream.App.World;
|
||||
using AcDream.Core.Chat;
|
||||
using AcDream.Core.Items;
|
||||
using AcDream.Core.Physics;
|
||||
using AcDream.Runtime;
|
||||
|
||||
namespace AcDream.App.Runtime;
|
||||
|
||||
internal interface ICurrentGameRuntimeEventSink
|
||||
{
|
||||
void EmitCommand(
|
||||
RuntimeCommandDomain domain,
|
||||
int operation,
|
||||
RuntimeCommandStatus status,
|
||||
uint primaryObjectId = 0u,
|
||||
string? text = null);
|
||||
|
||||
void EmitLifecycle(RuntimeLifecycleState previous);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lazily projects existing owner notifications into one ordered diagnostic
|
||||
/// stream. With no subscribers it attaches no callbacks and adds no per-event
|
||||
/// production work.
|
||||
/// </summary>
|
||||
internal sealed class CurrentGameRuntimeEventAdapter
|
||||
: IRuntimeEventSource,
|
||||
ICurrentGameRuntimeEventSink,
|
||||
IDisposable
|
||||
{
|
||||
private readonly CurrentGameRuntimeViewAdapter _view;
|
||||
private readonly LiveEntityRuntime _entities;
|
||||
private readonly ClientObjectTable _objects;
|
||||
private readonly ChatLog _chat;
|
||||
private readonly IGameRuntimeClock _clock;
|
||||
private readonly RuntimeEventSequencer _events = new();
|
||||
private readonly object _observerGate = new();
|
||||
private IRuntimeEventObserver[] _observers = [];
|
||||
private bool _ownerEventsAttached;
|
||||
private bool _disposed;
|
||||
|
||||
public CurrentGameRuntimeEventAdapter(
|
||||
CurrentGameRuntimeViewAdapter view,
|
||||
LiveEntityRuntime entities,
|
||||
ClientObjectTable objects,
|
||||
ChatLog chat,
|
||||
IGameRuntimeClock clock)
|
||||
{
|
||||
_view = view ?? throw new ArgumentNullException(nameof(view));
|
||||
_entities = entities ?? throw new ArgumentNullException(nameof(entities));
|
||||
_objects = objects ?? throw new ArgumentNullException(nameof(objects));
|
||||
_chat = chat ?? throw new ArgumentNullException(nameof(chat));
|
||||
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
|
||||
}
|
||||
|
||||
public IDisposable Subscribe(IRuntimeEventObserver observer)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(observer);
|
||||
lock (_observerGate)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
IRuntimeEventObserver[] current = _observers;
|
||||
if (Array.IndexOf(current, observer) >= 0)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"The runtime observer is already subscribed.");
|
||||
}
|
||||
|
||||
var replacement = new IRuntimeEventObserver[current.Length + 1];
|
||||
Array.Copy(current, replacement, current.Length);
|
||||
replacement[^1] = observer;
|
||||
Volatile.Write(ref _observers, replacement);
|
||||
if (!_ownerEventsAttached)
|
||||
AttachOwnerEvents();
|
||||
}
|
||||
|
||||
return new ObserverSubscription(this, observer);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
lock (_observerGate)
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
_disposed = true;
|
||||
Volatile.Write(ref _observers, []);
|
||||
DetachOwnerEvents();
|
||||
}
|
||||
}
|
||||
|
||||
public void EmitCommand(
|
||||
RuntimeCommandDomain domain,
|
||||
int operation,
|
||||
RuntimeCommandStatus status,
|
||||
uint primaryObjectId = 0u,
|
||||
string? text = null)
|
||||
{
|
||||
if (!TryObservers(out IRuntimeEventObserver[] observers))
|
||||
return;
|
||||
var delta = new RuntimeCommandDelta(
|
||||
NextStamp(),
|
||||
domain,
|
||||
operation,
|
||||
status,
|
||||
primaryObjectId,
|
||||
text);
|
||||
foreach (IRuntimeEventObserver observer in observers)
|
||||
observer.OnCommand(in delta);
|
||||
}
|
||||
|
||||
public void EmitLifecycle(RuntimeLifecycleState previous)
|
||||
{
|
||||
RuntimeLifecycleState current = _view.Lifecycle.State;
|
||||
if (current == previous
|
||||
|| !TryObservers(out IRuntimeEventObserver[] observers))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var delta = new RuntimeLifecycleDelta(
|
||||
NextStamp(),
|
||||
previous,
|
||||
current);
|
||||
foreach (IRuntimeEventObserver observer in observers)
|
||||
observer.OnLifecycle(in delta);
|
||||
}
|
||||
|
||||
private void Unsubscribe(IRuntimeEventObserver observer)
|
||||
{
|
||||
lock (_observerGate)
|
||||
{
|
||||
IRuntimeEventObserver[] current = _observers;
|
||||
int index = Array.IndexOf(current, observer);
|
||||
if (index < 0)
|
||||
return;
|
||||
if (current.Length == 1)
|
||||
{
|
||||
Volatile.Write(ref _observers, []);
|
||||
DetachOwnerEvents();
|
||||
return;
|
||||
}
|
||||
|
||||
var replacement = new IRuntimeEventObserver[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 void AttachOwnerEvents()
|
||||
{
|
||||
_entities.ProjectionVisibilityChanged += OnEntityVisibilityChanged;
|
||||
_objects.ObjectAdded += OnObjectAdded;
|
||||
_objects.ObjectUpdated += OnObjectUpdated;
|
||||
_objects.ObjectMoved += OnObjectMoved;
|
||||
_objects.ObjectRemovalClassified += OnObjectRemoved;
|
||||
_objects.Cleared += OnObjectsCleared;
|
||||
_chat.EntryAppended += OnChatEntry;
|
||||
_ownerEventsAttached = true;
|
||||
}
|
||||
|
||||
private void DetachOwnerEvents()
|
||||
{
|
||||
if (!_ownerEventsAttached)
|
||||
return;
|
||||
_chat.EntryAppended -= OnChatEntry;
|
||||
_objects.Cleared -= OnObjectsCleared;
|
||||
_objects.ObjectRemovalClassified -= OnObjectRemoved;
|
||||
_objects.ObjectMoved -= OnObjectMoved;
|
||||
_objects.ObjectUpdated -= OnObjectUpdated;
|
||||
_objects.ObjectAdded -= OnObjectAdded;
|
||||
_entities.ProjectionVisibilityChanged -= OnEntityVisibilityChanged;
|
||||
_ownerEventsAttached = false;
|
||||
}
|
||||
|
||||
private void OnEntityVisibilityChanged(LiveEntityRecord record, bool visible)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
private void OnChatEntry(ChatEntry entry)
|
||||
{
|
||||
if (!TryObservers(out IRuntimeEventObserver[] observers))
|
||||
return;
|
||||
var delta = new RuntimeChatDelta(
|
||||
NextStamp(),
|
||||
new RuntimeChatEntry(
|
||||
_chat.Revision,
|
||||
entry.SenderGuid,
|
||||
(int)entry.Kind,
|
||||
entry.Sender,
|
||||
entry.Text,
|
||||
entry.ChannelName));
|
||||
foreach (IRuntimeEventObserver observer in observers)
|
||||
observer.OnChat(in delta);
|
||||
}
|
||||
|
||||
private RuntimeEventStamp NextStamp() =>
|
||||
_events.Next(_view.Generation, _clock.FrameNumber);
|
||||
|
||||
private bool TryObservers(out IRuntimeEventObserver[] observers)
|
||||
{
|
||||
observers = Volatile.Read(ref _observers);
|
||||
return observers.Length != 0;
|
||||
}
|
||||
|
||||
private sealed class ObserverSubscription(
|
||||
CurrentGameRuntimeEventAdapter owner,
|
||||
IRuntimeEventObserver observer)
|
||||
: IDisposable
|
||||
{
|
||||
private CurrentGameRuntimeEventAdapter? _owner = owner;
|
||||
|
||||
public void Dispose() =>
|
||||
Interlocked.Exchange(ref _owner, null)?.Unsubscribe(observer);
|
||||
}
|
||||
}
|
||||
271
src/AcDream.App/Runtime/CurrentGameRuntimeViewAdapter.cs
Normal file
271
src/AcDream.App/Runtime/CurrentGameRuntimeViewAdapter.cs
Normal file
|
|
@ -0,0 +1,271 @@
|
|||
using AcDream.App.Input;
|
||||
using AcDream.App.Net;
|
||||
using AcDream.App.Streaming;
|
||||
using AcDream.App.World;
|
||||
using AcDream.Core.Chat;
|
||||
using AcDream.Core.Items;
|
||||
using AcDream.Runtime;
|
||||
|
||||
namespace AcDream.App.Runtime;
|
||||
|
||||
/// <summary>
|
||||
/// Read-only borrowed projections over the current App owners. Enumeration is
|
||||
/// synchronous and allocation-free; no mutable collection is copied or owned.
|
||||
/// </summary>
|
||||
internal sealed class CurrentGameRuntimeViewAdapter : IGameRuntimeView
|
||||
{
|
||||
private readonly LiveSessionController _session;
|
||||
private readonly LocalPlayerIdentityState _playerIdentity;
|
||||
private readonly LiveEntityRuntime _entities;
|
||||
private readonly ClientObjectTable _objects;
|
||||
private readonly ChatLog _chat;
|
||||
private readonly IGameRuntimeClock _clock;
|
||||
private readonly EntityView _entityView;
|
||||
private readonly InventoryView _inventoryView;
|
||||
private readonly ChatView _chatView;
|
||||
private readonly MovementView _movementView;
|
||||
private readonly PortalView _portalView;
|
||||
private bool _active = true;
|
||||
|
||||
public CurrentGameRuntimeViewAdapter(
|
||||
LiveSessionController session,
|
||||
LocalPlayerIdentityState playerIdentity,
|
||||
LiveEntityRuntime entities,
|
||||
ClientObjectTable objects,
|
||||
ChatLog chat,
|
||||
ILocalPlayerControllerSource playerController,
|
||||
WorldRevealCoordinator worldReveal,
|
||||
IGameRuntimeClock clock)
|
||||
{
|
||||
_session = session ?? throw new ArgumentNullException(nameof(session));
|
||||
_playerIdentity = playerIdentity
|
||||
?? throw new ArgumentNullException(nameof(playerIdentity));
|
||||
_entities = entities ?? throw new ArgumentNullException(nameof(entities));
|
||||
_objects = objects ?? throw new ArgumentNullException(nameof(objects));
|
||||
_chat = chat ?? throw new ArgumentNullException(nameof(chat));
|
||||
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
|
||||
|
||||
_entityView = new EntityView(_entities);
|
||||
_inventoryView = new InventoryView(_objects, _entities);
|
||||
_chatView = new ChatView(_chat);
|
||||
_movementView = new MovementView(
|
||||
playerController
|
||||
?? throw new ArgumentNullException(nameof(playerController)),
|
||||
_clock);
|
||||
_portalView = new PortalView(
|
||||
worldReveal ?? throw new ArgumentNullException(nameof(worldReveal)));
|
||||
}
|
||||
|
||||
internal bool IsActive => _active && !_session.IsDisposalComplete;
|
||||
|
||||
public RuntimeGenerationToken Generation =>
|
||||
new(_session.SessionGeneration);
|
||||
|
||||
public RuntimeLifecycleSnapshot Lifecycle
|
||||
{
|
||||
get
|
||||
{
|
||||
RuntimeLifecycleState state;
|
||||
if (!IsActive)
|
||||
state = RuntimeLifecycleState.Disposed;
|
||||
else if (_session.IsInWorld)
|
||||
state = RuntimeLifecycleState.InWorld;
|
||||
else if (_session.CurrentSession is not null)
|
||||
state = RuntimeLifecycleState.Starting;
|
||||
else if (_session.SessionGeneration == 0UL)
|
||||
state = RuntimeLifecycleState.Constructed;
|
||||
else
|
||||
state = RuntimeLifecycleState.Stopped;
|
||||
|
||||
return new RuntimeLifecycleSnapshot(
|
||||
Generation,
|
||||
state,
|
||||
_playerIdentity.ServerGuid,
|
||||
_session.CurrentSession is not null);
|
||||
}
|
||||
}
|
||||
|
||||
public IGameRuntimeClock Clock => _clock;
|
||||
public IRuntimeEntityView Entities => _entityView;
|
||||
public IRuntimeInventoryView Inventory => _inventoryView;
|
||||
public IRuntimeChatView Chat => _chatView;
|
||||
public IRuntimeMovementView Movement => _movementView;
|
||||
public IRuntimePortalView Portal => _portalView;
|
||||
|
||||
public RuntimeStateCheckpoint CaptureCheckpoint() =>
|
||||
new(
|
||||
Generation,
|
||||
Lifecycle.State,
|
||||
_clock.FrameNumber,
|
||||
_entities.Count,
|
||||
_entities.MaterializedCount,
|
||||
_objects.ObjectCount,
|
||||
_objects.ContainerCount,
|
||||
_chat.Revision,
|
||||
_chat.Count,
|
||||
_movementView.Snapshot,
|
||||
_portalView.Snapshot);
|
||||
|
||||
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;
|
||||
public int Count => owner.Count;
|
||||
}
|
||||
|
||||
private sealed class MovementView(
|
||||
ILocalPlayerControllerSource owner,
|
||||
IGameRuntimeClock clock)
|
||||
: IRuntimeMovementView
|
||||
{
|
||||
public RuntimeMovementSnapshot Snapshot
|
||||
{
|
||||
get
|
||||
{
|
||||
PlayerMovementController? controller = owner.Controller;
|
||||
return controller is null
|
||||
? new RuntimeMovementSnapshot(
|
||||
false,
|
||||
0u,
|
||||
default,
|
||||
default,
|
||||
false,
|
||||
clock.SimulationTimeSeconds)
|
||||
: new RuntimeMovementSnapshot(
|
||||
true,
|
||||
controller.LocalEntityId,
|
||||
controller.CellPosition,
|
||||
controller.BodyVelocity,
|
||||
controller.IsAirborne,
|
||||
clock.SimulationTimeSeconds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class PortalView(WorldRevealCoordinator owner)
|
||||
: IRuntimePortalView
|
||||
{
|
||||
public RuntimePortalSnapshot Snapshot
|
||||
{
|
||||
get
|
||||
{
|
||||
WorldRevealLifecycleSnapshot snapshot = owner.Snapshot;
|
||||
RuntimePortalKind kind = snapshot.Generation == 0
|
||||
? RuntimePortalKind.None
|
||||
: snapshot.Kind switch
|
||||
{
|
||||
WorldRevealKind.Login => RuntimePortalKind.Login,
|
||||
WorldRevealKind.Portal => RuntimePortalKind.Portal,
|
||||
_ => RuntimePortalKind.None,
|
||||
};
|
||||
return new RuntimePortalSnapshot(
|
||||
snapshot.Generation,
|
||||
kind,
|
||||
snapshot.Readiness.DestinationCell,
|
||||
snapshot.IsReady,
|
||||
snapshot.Materialized,
|
||||
snapshot.Completed,
|
||||
snapshot.Cancelled,
|
||||
snapshot.WorldViewportObserved);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue