refactor(runtime): acknowledge exact world host projections

This commit is contained in:
Erik 2026-07-26 18:27:41 +02:00
parent 73d0b54e38
commit 18d17d8bb1
17 changed files with 1677 additions and 72 deletions

View file

@ -1,5 +1,6 @@
using System.Reflection;
using AcDream.Runtime.Gameplay;
using AcDream.Runtime.World;
namespace AcDream.Runtime.Tests;
@ -183,7 +184,20 @@ public sealed class GameRuntimeContractTests
InteractionTransactions: default),
default,
default,
default);
new RuntimeWorldEnvironmentOwnershipSnapshot(
IsInitialized: true,
DayGroupDefinitionCount: 4,
ActiveDayGroupCount: 1),
default,
new RuntimeWorldTransitOwnershipSnapshot(
BufferedTeleportDestinationCount: 0,
PendingTeleportStartCount: 0,
ActiveTeleportCount: 1,
AcceptedTeleportDestinationCount: 0,
ActiveRevealCount: 1,
PendingDestinationReadinessCount: 1,
HostProjectionCount: 1,
PendingHostAcknowledgementCount: 2));
recorder.AddCheckpoint(stamp, checkpoint);
@ -191,6 +205,8 @@ public sealed class GameRuntimeContractTests
Assert.Contains("inventory-state=2:4:1:3", entry.Text);
Assert.Contains("character=5:6:7:8:0:0", entry.Text);
Assert.Contains("social=9:10:11", entry.Text);
Assert.Contains("environment-owner=True:4:1", entry.Text);
Assert.Contains("transit-owner=0:0:1:0:1:1:1:2", entry.Text);
Assert.Contains(
"actions=14:50000001:15:4:1:16:1:00000000:0:00000000:" +
"00000000:00000000:00000000:0:0:0:0:00000000:00000000:" +

View file

@ -0,0 +1,204 @@
using System.Numerics;
using AcDream.Core.Physics;
using AcDream.Runtime.World;
namespace AcDream.Runtime.Tests.World;
public sealed class RuntimeWorldTransitDirectHostTests
{
private static readonly string[] ForbiddenHostAssemblyPrefixes =
[
"AcDream.App",
"AcDream.UI.",
"Silk.NET",
"OpenAL",
"Arch",
"ImGui",
];
private const uint OutdoorCell = 0x11340021u;
private const uint IndoorCell = 0x8A020164u;
[Fact]
public void DirectHost_CompletesLoginPortalCancellationAndReset()
{
string[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies()
.Select(static assembly =>
assembly.GetName().Name ?? string.Empty)
.ToArray();
Assert.DoesNotContain(
loadedAssemblies,
name => ForbiddenHostAssemblyPrefixes.Any(
prefix => name.StartsWith(
prefix,
StringComparison.OrdinalIgnoreCase)));
var state = new RuntimeWorldTransitState();
var host = new DirectHost(state);
host.CompleteLogin(OutdoorCell);
Assert.True(state.Snapshot.Completed);
Assert.True(state.Snapshot.WorldViewportObserved);
Assert.Equal(0, state.Ownership.HostProjectionCount);
state.ResetSession();
host.CompletePortal(IndoorCell, sequence: 1);
Assert.True(state.Snapshot.Completed);
Assert.True(state.Snapshot.Materialized);
Assert.Equal(1, state.Snapshot.PortalMaterializationCount);
Assert.Equal(0, state.Ownership.PendingHostAcknowledgementCount);
state.EndTeleport();
state.ResetSession();
host.CancelLogin(OutdoorCell);
Assert.True(state.Snapshot.Cancelled);
Assert.Equal(0, state.Ownership.HostProjectionCount);
state.ResetSession();
Assert.Equal(RuntimePortalSnapshot.Idle, state.Snapshot);
Assert.True(state.Ownership.IsSessionIdle);
}
private sealed class DirectHost(RuntimeWorldTransitState state)
{
public void CompleteLogin(uint cell)
{
long generation = state.BeginLoginReveal(cell);
RuntimeWorldHostProjectionToken host =
Register(generation, cell);
Acknowledge(
host,
RuntimeWorldHostAcknowledgementStage.ProjectionRegistered);
Assert.True(state.AcknowledgeDestinationReadiness(
Ready(generation, cell)));
Assert.True(state.Complete(generation));
DrainPending(host);
Assert.True(state.AcknowledgeWorldViewportVisible(generation));
}
public void CompletePortal(uint cell, ushort sequence)
{
Assert.True(state.TryQueueTeleportStart(sequence));
Assert.True(state.ActivateQueuedTeleport());
Assert.True(state.OfferTeleportDestination(
Destination(cell, sequence),
teleportTimestampAdvanced: true));
Assert.True(state.TryBeginPortalReveal(
sequence,
cell,
out long generation));
RuntimeWorldHostProjectionToken host =
Register(generation, cell);
Acknowledge(
host,
RuntimeWorldHostAcknowledgementStage.ProjectionRegistered);
Assert.True(state.AcknowledgeDestinationReadiness(
Ready(generation, cell)));
Assert.True(state.AcknowledgePortalMaterialized(
generation,
sequence,
cell));
DrainPending(host);
Assert.True(state.RequireDestinationReservationRelease(host));
DrainPending(host);
Assert.True(state.AcknowledgeWorldViewportVisible(generation));
Assert.True(state.Complete(generation));
DrainPending(host);
}
public void CancelLogin(uint cell)
{
long generation = state.BeginLoginReveal(cell);
RuntimeWorldHostProjectionToken host =
Register(generation, cell);
Acknowledge(
host,
RuntimeWorldHostAcknowledgementStage.ProjectionRegistered);
Assert.True(state.Cancel(generation));
DrainPending(host);
}
private RuntimeWorldHostProjectionToken Register(
long generation,
uint cell)
{
Assert.True(state.TryRegisterHostProjection(
generation,
cell,
out RuntimeWorldHostProjectionToken host));
return host;
}
private void DrainPending(RuntimeWorldHostProjectionToken host)
{
while (state.TryGetHostProjection(
host,
out RuntimeWorldHostProjectionSnapshot projection))
{
RuntimeWorldHostAcknowledgementStage pending =
projection.PendingAcknowledgements;
if ((pending & RuntimeWorldHostAcknowledgementStage
.SimulationReleaseProjected) != 0)
{
Acknowledge(
host,
RuntimeWorldHostAcknowledgementStage
.SimulationReleaseProjected);
continue;
}
if ((pending & RuntimeWorldHostAcknowledgementStage
.DestinationReservationReleased) != 0)
{
Acknowledge(
host,
RuntimeWorldHostAcknowledgementStage
.DestinationReservationReleased);
continue;
}
if ((pending & RuntimeWorldHostAcknowledgementStage
.TerminalProjected) != 0)
{
Acknowledge(
host,
RuntimeWorldHostAcknowledgementStage
.TerminalProjected);
continue;
}
break;
}
}
private void Acknowledge(
RuntimeWorldHostProjectionToken host,
RuntimeWorldHostAcknowledgementStage stage) =>
Assert.True(state.AcknowledgeHostProjection(
new RuntimeWorldHostAcknowledgement(host, stage)));
}
private static RuntimeDestinationReadiness Ready(
long generation,
uint cell) =>
new(
generation,
cell,
IsIndoor: (cell & 0xFFFFu) >= 0x0100u,
IsUnhydratable: false,
RequiredRenderRadius: (cell & 0xFFFFu) >= 0x0100u ? 0 : 1,
IsRenderNeighborhoodReady: true,
AreCompositeTexturesReady: true,
IsCollisionReady: true);
private static RuntimeTeleportDestination Destination(
uint cell,
ushort sequence) =>
new(
EntityGuid: 0x50000001u,
InstanceSequence: 1,
PositionSequence: 1,
TeleportSequence: sequence,
ForcePositionSequence: 1,
Position: new Position(
cell,
Vector3.Zero,
Quaternion.Identity));
}

View file

@ -338,6 +338,18 @@ public sealed class RuntimeWorldTransitStateTests
BeginPortal(first, OutdoorCell);
long secondGeneration =
second.BeginLoginReveal(OtherCell);
RuntimeWorldHostProjectionToken firstHost =
RegisterHost(first, firstGeneration, OutdoorCell);
RuntimeWorldHostProjectionToken secondHost =
RegisterHost(second, secondGeneration, OtherCell);
Assert.True(Acknowledge(
first,
firstHost,
RuntimeWorldHostAcknowledgementStage.ProjectionRegistered));
Assert.True(Acknowledge(
second,
secondHost,
RuntimeWorldHostAcknowledgementStage.ProjectionRegistered));
first.AcknowledgeDestinationReadiness(
Ready(firstGeneration, OutdoorCell));
first.AcknowledgePortalMaterialized(
@ -351,6 +363,13 @@ public sealed class RuntimeWorldTransitStateTests
Assert.False(second.IsWorldSimulationAvailable);
Assert.Equal(OtherCell, second.Snapshot.DestinationCell);
Assert.Equal(1, secondGeneration);
Assert.False(Acknowledge(
second,
firstHost,
RuntimeWorldHostAcknowledgementStage
.SimulationReleaseProjected));
Assert.Equal(1, first.Ownership.PendingHostAcknowledgementCount);
Assert.Equal(0, second.Ownership.PendingHostAcknowledgementCount);
}
[Fact]
@ -616,6 +635,195 @@ public sealed class RuntimeWorldTransitStateTests
Assert.True(state.TryQueueTeleportStart(8));
}
[Fact]
public void HostProjection_TracksExactGenerationScopedAcknowledgementSuffix()
{
var state = new RuntimeWorldTransitState();
long generation = BeginPortal(state, OutdoorCell);
RuntimeWorldHostProjectionToken host =
RegisterHost(state, generation, OutdoorCell);
Assert.Equal(
new RuntimeWorldTransitOwnershipSnapshot(
BufferedTeleportDestinationCount: 0,
PendingTeleportStartCount: 0,
ActiveTeleportCount: 1,
AcceptedTeleportDestinationCount: 0,
ActiveRevealCount: 1,
PendingDestinationReadinessCount: 1,
HostProjectionCount: 1,
PendingHostAcknowledgementCount: 1),
state.Ownership);
Assert.False(Acknowledge(
state,
host with { Generation = generation + 1 },
RuntimeWorldHostAcknowledgementStage.ProjectionRegistered));
Assert.False(Acknowledge(
state,
host with { DestinationCell = OtherCell },
RuntimeWorldHostAcknowledgementStage.ProjectionRegistered));
Assert.False(Acknowledge(
state,
host,
RuntimeWorldHostAcknowledgementStage.None));
Assert.False(Acknowledge(
state,
host,
RuntimeWorldHostAcknowledgementStage.ProjectionRegistered
| RuntimeWorldHostAcknowledgementStage
.DestinationReservationReleased));
Assert.True(Acknowledge(
state,
host,
RuntimeWorldHostAcknowledgementStage.ProjectionRegistered));
Assert.True(state.AcknowledgeDestinationReadiness(
Ready(generation, OutdoorCell)));
Assert.True(state.AcknowledgePortalMaterialized(
generation,
1,
OutdoorCell));
Assert.Equal(1, state.Ownership.PendingHostAcknowledgementCount);
Assert.True(Acknowledge(
state,
host,
RuntimeWorldHostAcknowledgementStage
.SimulationReleaseProjected));
Assert.True(state.RequireDestinationReservationRelease(host));
Assert.True(Acknowledge(
state,
host,
RuntimeWorldHostAcknowledgementStage
.DestinationReservationReleased));
Assert.True(state.AcknowledgeWorldViewportVisible(generation));
Assert.True(state.Complete(generation));
Assert.Equal(1, state.Ownership.PendingHostAcknowledgementCount);
Assert.True(Acknowledge(
state,
host,
RuntimeWorldHostAcknowledgementStage.TerminalProjected));
Assert.Equal(0, state.Ownership.HostProjectionCount);
Assert.Equal(0, state.Ownership.PendingHostAcknowledgementCount);
state.EndTeleport();
state.ResetSession();
Assert.True(state.Ownership.IsSessionIdle);
}
[Fact]
public void Cancel_ReplacesIncompleteRegistrationWithRetryableWithdrawal()
{
var state = new RuntimeWorldTransitState();
long generation = state.BeginLoginReveal(OutdoorCell);
RuntimeWorldHostProjectionToken host =
RegisterHost(state, generation, OutdoorCell);
Assert.True(state.Cancel(generation));
Assert.True(state.TryGetHostProjection(
host,
out RuntimeWorldHostProjectionSnapshot projection));
Assert.Equal(
RuntimeWorldHostAcknowledgementStage
.SimulationReleaseProjected
| RuntimeWorldHostAcknowledgementStage
.DestinationReservationReleased
| RuntimeWorldHostAcknowledgementStage.TerminalProjected,
projection.PendingAcknowledgements);
Assert.False(Acknowledge(
state,
host,
RuntimeWorldHostAcknowledgementStage.TerminalProjected));
Assert.True(Acknowledge(
state,
host,
RuntimeWorldHostAcknowledgementStage
.SimulationReleaseProjected));
Assert.True(Acknowledge(
state,
host,
RuntimeWorldHostAcknowledgementStage
.DestinationReservationReleased));
Assert.True(Acknowledge(
state,
host,
RuntimeWorldHostAcknowledgementStage.TerminalProjected));
state.ResetSession();
Assert.True(state.Ownership.IsSessionIdle);
}
[Fact]
public void Supersession_PreservesOldHostWithdrawalByExactGeneration()
{
var state = new RuntimeWorldTransitState();
long first = state.BeginLoginReveal(OutdoorCell);
RuntimeWorldHostProjectionToken firstHost =
RegisterHost(state, first, OutdoorCell);
Assert.True(Acknowledge(
state,
firstHost,
RuntimeWorldHostAcknowledgementStage.ProjectionRegistered));
long second = BeginPortal(state, OtherCell, sequence: 2);
Assert.True(state.TryGetHostProjection(
firstHost,
out RuntimeWorldHostProjectionSnapshot superseded));
Assert.True(superseded.IsSuperseding);
Assert.Equal(
RuntimeWorldHostAcknowledgementStage
.DestinationReservationReleased
| RuntimeWorldHostAcknowledgementStage.TerminalProjected,
superseded.PendingAcknowledgements);
Assert.False(Acknowledge(
state,
firstHost,
RuntimeWorldHostAcknowledgementStage.TerminalProjected));
Assert.True(Acknowledge(
state,
firstHost,
RuntimeWorldHostAcknowledgementStage
.DestinationReservationReleased));
Assert.True(Acknowledge(
state,
firstHost,
RuntimeWorldHostAcknowledgementStage.TerminalProjected));
Assert.Equal(second, state.Snapshot.Generation);
Assert.Equal(0, state.Ownership.HostProjectionCount);
}
[Fact]
public void ResetSession_RejectsUnacknowledgedHostProjection()
{
var state = new RuntimeWorldTransitState();
long generation = state.BeginLoginReveal(OutdoorCell);
RuntimeWorldHostProjectionToken host =
RegisterHost(state, generation, OutdoorCell);
InvalidOperationException error =
Assert.Throws<InvalidOperationException>(state.ResetSession);
Assert.Contains("hosts=1", error.Message);
Assert.Equal(generation, state.Snapshot.Generation);
Assert.True(state.Cancel(generation));
Assert.True(Acknowledge(
state,
host,
RuntimeWorldHostAcknowledgementStage
.SimulationReleaseProjected));
Assert.True(Acknowledge(
state,
host,
RuntimeWorldHostAcknowledgementStage
.DestinationReservationReleased));
Assert.True(Acknowledge(
state,
host,
RuntimeWorldHostAcknowledgementStage.TerminalProjected));
state.ResetSession();
}
private static long BeginPortal(
RuntimeWorldTransitState state,
uint cell,
@ -633,6 +841,25 @@ public sealed class RuntimeWorldTransitStateTests
return generation;
}
private static RuntimeWorldHostProjectionToken RegisterHost(
RuntimeWorldTransitState state,
long generation,
uint cell)
{
Assert.True(state.TryRegisterHostProjection(
generation,
cell,
out RuntimeWorldHostProjectionToken host));
return host;
}
private static bool Acknowledge(
RuntimeWorldTransitState state,
RuntimeWorldHostProjectionToken host,
RuntimeWorldHostAcknowledgementStage stage) =>
state.AcknowledgeHostProjection(
new RuntimeWorldHostAcknowledgement(host, stage));
private static RuntimeTeleportDestination Destination(
uint cell,
ushort sequence,