feat(headless): complete portable single-session host
This commit is contained in:
parent
fbebb91848
commit
f8cb840fb1
30 changed files with 3571 additions and 32 deletions
|
|
@ -0,0 +1,291 @@
|
|||
using System.Numerics;
|
||||
using AcDream.Core.Net;
|
||||
using AcDream.Core.Net.Messages;
|
||||
using AcDream.Core.Physics;
|
||||
using AcDream.Runtime.Entities;
|
||||
using AcDream.Runtime.World;
|
||||
|
||||
namespace AcDream.Runtime.Session;
|
||||
|
||||
/// <summary>
|
||||
/// Presentation-free inbound entity route for a direct Runtime host. It
|
||||
/// applies the same canonical identity, timestamp, object-table, and transit
|
||||
/// owners used by the graphical route without constructing App hydration,
|
||||
/// rendering, animation, or effect projections.
|
||||
/// </summary>
|
||||
public sealed class RuntimeLiveEntitySessionController
|
||||
{
|
||||
private readonly GameRuntime _runtime;
|
||||
private readonly WorldSession _session;
|
||||
private readonly Action<string> _log;
|
||||
|
||||
public RuntimeLiveEntitySessionController(
|
||||
GameRuntime runtime,
|
||||
WorldSession session,
|
||||
Action<string>? log = null)
|
||||
{
|
||||
_runtime = runtime ?? throw new ArgumentNullException(nameof(runtime));
|
||||
_session = session ?? throw new ArgumentNullException(nameof(session));
|
||||
_log = log ?? (_ => { });
|
||||
}
|
||||
|
||||
public LiveEntitySessionSink CreateSink() => new(
|
||||
OnSpawned,
|
||||
OnDeleted,
|
||||
OnPickedUp,
|
||||
OnMotionUpdated,
|
||||
OnPositionUpdated,
|
||||
OnVectorUpdated,
|
||||
OnStateUpdated,
|
||||
OnParentUpdated,
|
||||
OnTeleportStarted,
|
||||
OnAppearanceUpdated,
|
||||
_ => { },
|
||||
_ => { });
|
||||
|
||||
private RuntimeEntityObjectLifetime Entities =>
|
||||
_runtime.EntityObjects;
|
||||
|
||||
private void OnSpawned(WorldSession.EntitySpawn spawn)
|
||||
{
|
||||
RuntimeEntityRegistrationResult registration =
|
||||
Entities.RegisterEntity(spawn);
|
||||
if (registration.Canonical is not { } canonical)
|
||||
return;
|
||||
|
||||
ulong integrationVersion = canonical.CreateIntegrationVersion;
|
||||
_ = Entities.ApplyAcceptedSpawn(
|
||||
canonical,
|
||||
integrationVersion,
|
||||
canonical.Snapshot,
|
||||
replaceGeneration:
|
||||
registration.Inbound.Disposition
|
||||
is CreateObjectTimestampDisposition.NewGeneration);
|
||||
}
|
||||
|
||||
private void OnDeleted(DeleteObject.Parsed delete)
|
||||
{
|
||||
if (delete.Guid == _runtime.PlayerIdentity.ServerGuid
|
||||
|| !Entities.TryAcceptDelete(
|
||||
delete,
|
||||
isLocalPlayer: false,
|
||||
removeRetainedObject: true,
|
||||
out RuntimeEntityDeleteAcceptance acceptance))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Entities.CompleteAcceptedDelete(acceptance);
|
||||
if (acceptance.RetiredCanonical is { } retired)
|
||||
{
|
||||
Exception? failure = Entities.RetireCanonicalOnly(retired);
|
||||
if (failure is not null)
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPickedUp(PickupEvent.Parsed pickup) =>
|
||||
_ = Entities.TryApplyPickup(
|
||||
pickup,
|
||||
acknowledgeProjection: null,
|
||||
out _);
|
||||
|
||||
private void OnMotionUpdated(
|
||||
WorldSession.EntityMotionUpdate update)
|
||||
{
|
||||
bool isLocal =
|
||||
update.Guid == _runtime.PlayerIdentity.ServerGuid;
|
||||
_ = Entities.TryApplyMotion(
|
||||
update,
|
||||
retainPayload: !isLocal || !update.IsAutonomous,
|
||||
acknowledgeProjection: null,
|
||||
out _,
|
||||
out _);
|
||||
}
|
||||
|
||||
private void OnPositionUpdated(
|
||||
WorldSession.EntityPositionUpdate update)
|
||||
{
|
||||
bool isLocal =
|
||||
update.Guid == _runtime.PlayerIdentity.ServerGuid;
|
||||
bool known = Entities.TryApplyPosition(
|
||||
update,
|
||||
isLocal,
|
||||
forcePositionRotation: null,
|
||||
currentLocalVelocity: null,
|
||||
projectionRequiresTeleportHook: false,
|
||||
acknowledgeProjection: null,
|
||||
out PositionTimestampDisposition disposition,
|
||||
out _,
|
||||
out AcceptedPhysicsTimestamps timestamps);
|
||||
if (!known
|
||||
|| !isLocal
|
||||
|| disposition is PositionTimestampDisposition.Rejected)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var position = update.Position;
|
||||
var destination = new RuntimeTeleportDestination(
|
||||
update.Guid,
|
||||
update.InstanceSequence,
|
||||
update.PositionSequence,
|
||||
update.TeleportSequence,
|
||||
update.ForcePositionSequence,
|
||||
new Position(
|
||||
position.LandblockId,
|
||||
new Vector3(
|
||||
position.PositionX,
|
||||
position.PositionY,
|
||||
position.PositionZ),
|
||||
new Quaternion(
|
||||
position.RotationX,
|
||||
position.RotationY,
|
||||
position.RotationZ,
|
||||
position.RotationW)));
|
||||
_runtime.TransitOwner.OfferTeleportDestination(
|
||||
destination,
|
||||
timestamps.TeleportAdvanced);
|
||||
TryCompletePortal();
|
||||
}
|
||||
|
||||
private void OnVectorUpdated(VectorUpdate.Parsed update) =>
|
||||
_ = Entities.TryApplyVector(
|
||||
update,
|
||||
acknowledgeProjection: null,
|
||||
out _);
|
||||
|
||||
private void OnStateUpdated(SetState.Parsed update) =>
|
||||
_ = Entities.TryApplyState(
|
||||
update,
|
||||
acknowledgeProjection: null,
|
||||
out _,
|
||||
out _);
|
||||
|
||||
private void OnParentUpdated(ParentEvent.Parsed update) =>
|
||||
_ = Entities.TryApplyParent(
|
||||
update,
|
||||
acknowledgeProjection: null,
|
||||
out _);
|
||||
|
||||
private void OnTeleportStarted(uint rawSequence)
|
||||
{
|
||||
ushort sequence = unchecked((ushort)rawSequence);
|
||||
RuntimeWorldTransitState transit = _runtime.TransitOwner;
|
||||
if (!transit.TryQueueTeleportStart(sequence))
|
||||
return;
|
||||
if (!transit.ActivateQueuedTeleport())
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Runtime rejected its queued headless teleport activation.");
|
||||
}
|
||||
TryCompletePortal();
|
||||
}
|
||||
|
||||
private void OnAppearanceUpdated(ObjDescEvent.Parsed update) =>
|
||||
_ = Entities.TryApplyObjDesc(
|
||||
update,
|
||||
acknowledgeProjection: null,
|
||||
out _);
|
||||
|
||||
private void TryCompletePortal()
|
||||
{
|
||||
RuntimeWorldTransitState transit = _runtime.TransitOwner;
|
||||
if (!transit.TryGetAcceptedTeleportDestination(
|
||||
out RuntimeTeleportDestination destination)
|
||||
|| !transit.TryBeginPortalReveal(
|
||||
destination.TeleportSequence,
|
||||
destination.CellId,
|
||||
out long generation))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!transit.TryRegisterHostProjection(
|
||||
generation,
|
||||
destination.CellId,
|
||||
out RuntimeWorldHostProjectionToken projection))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Runtime rejected the headless portal projection.");
|
||||
}
|
||||
|
||||
Acknowledge(
|
||||
transit,
|
||||
projection,
|
||||
RuntimeWorldHostAcknowledgementStage.ProjectionRegistered);
|
||||
|
||||
bool indoor = (destination.CellId & 0xFFFFu) >= 0x0100u;
|
||||
if (!transit.AcknowledgeDestinationReadiness(
|
||||
new RuntimeDestinationReadiness(
|
||||
generation,
|
||||
destination.CellId,
|
||||
indoor,
|
||||
IsUnhydratable: false,
|
||||
RequiredRenderRadius: indoor ? 0 : 1,
|
||||
IsRenderNeighborhoodReady: true,
|
||||
AreCompositeTexturesReady: true,
|
||||
IsCollisionReady: true)))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Runtime rejected headless destination readiness.");
|
||||
}
|
||||
|
||||
if (!transit.AcknowledgePortalMaterialized(
|
||||
generation,
|
||||
destination.TeleportSequence,
|
||||
destination.CellId))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Runtime rejected headless portal materialization.");
|
||||
}
|
||||
Acknowledge(
|
||||
transit,
|
||||
projection,
|
||||
RuntimeWorldHostAcknowledgementStage
|
||||
.SimulationReleaseProjected);
|
||||
|
||||
if (!transit.RequireDestinationReservationRelease(projection))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Runtime rejected headless destination release.");
|
||||
}
|
||||
Acknowledge(
|
||||
transit,
|
||||
projection,
|
||||
RuntimeWorldHostAcknowledgementStage
|
||||
.DestinationReservationReleased);
|
||||
|
||||
if (!transit.AcknowledgeWorldViewportVisible(generation)
|
||||
|| !transit.Complete(generation))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Runtime rejected headless portal completion.");
|
||||
}
|
||||
Acknowledge(
|
||||
transit,
|
||||
projection,
|
||||
RuntimeWorldHostAcknowledgementStage.TerminalProjected);
|
||||
|
||||
_session.SendGameAction(GameActionLoginComplete.Build());
|
||||
transit.EndTeleport();
|
||||
_log(
|
||||
$"headless: portal complete generation={generation} "
|
||||
+ $"cell=0x{destination.CellId:X8}");
|
||||
}
|
||||
|
||||
private static void Acknowledge(
|
||||
RuntimeWorldTransitState transit,
|
||||
RuntimeWorldHostProjectionToken projection,
|
||||
RuntimeWorldHostAcknowledgementStage stage)
|
||||
{
|
||||
if (!transit.AcknowledgeHostProjection(
|
||||
new RuntimeWorldHostAcknowledgement(
|
||||
projection,
|
||||
stage)))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Runtime rejected headless host acknowledgement {stage}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue