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,296 @@
|
|||
using System.Net;
|
||||
using AcDream.Core.Combat;
|
||||
using AcDream.Core.Items;
|
||||
using AcDream.Core.Net;
|
||||
using AcDream.Core.Net.Messages;
|
||||
using AcDream.Core.Spells;
|
||||
using AcDream.Runtime.Entities;
|
||||
using AcDream.Runtime.Gameplay;
|
||||
using AcDream.Runtime.Session;
|
||||
|
||||
namespace AcDream.Runtime.Tests.Session;
|
||||
|
||||
public sealed class DirectGameRuntimeCommandAdapterTests
|
||||
{
|
||||
[Fact]
|
||||
public void DirectRouteSendsTypedChatAndPortalAndRejectsOldGeneration()
|
||||
{
|
||||
var operations = new FixtureSessionOperations();
|
||||
var gameplay = new FixtureGameplayOperations();
|
||||
using var runtime = new GameRuntime(new GameRuntimeDependencies(
|
||||
gameplay,
|
||||
gameplay,
|
||||
gameplay,
|
||||
gameplay,
|
||||
SessionOperations: operations));
|
||||
gameplay.Bind(runtime);
|
||||
var resetHost = new FixtureResetHost();
|
||||
DirectGameRuntimeCommandAdapter? adapter = null;
|
||||
LiveSessionConnectOptions options = new(
|
||||
true,
|
||||
"127.0.0.1",
|
||||
9000,
|
||||
"account",
|
||||
"password");
|
||||
var live = new LiveSessionHost(
|
||||
runtime.Session,
|
||||
new LiveSessionHostBindings(
|
||||
new LiveSessionRoutingFactories(
|
||||
_ => new FixtureEventRoute(),
|
||||
session => adapter!.CreateRoute(session)),
|
||||
generation => runtime.ResetGeneration(
|
||||
generation,
|
||||
resetHost),
|
||||
new LiveSessionSelectionBindings(
|
||||
id => runtime.PlayerIdentity.ServerGuid = id,
|
||||
_ => { },
|
||||
runtime.CommunicationOwner.Chat.SetLocalPlayerGuid,
|
||||
_ => { },
|
||||
_ => { },
|
||||
runtime.ActionOwner.Combat.Clear),
|
||||
new LiveSessionEnteredWorldBindings(
|
||||
_ => { },
|
||||
() => { },
|
||||
() => { },
|
||||
_ => { },
|
||||
() => { }),
|
||||
(_, _, _) => { },
|
||||
() => { }),
|
||||
options);
|
||||
adapter = new DirectGameRuntimeCommandAdapter(runtime, live);
|
||||
var trace = new RuntimeTraceRecorder();
|
||||
using IDisposable subscription = runtime.Subscribe(trace);
|
||||
|
||||
RuntimeSessionStartResult started =
|
||||
adapter.Session.Start(runtime.Generation);
|
||||
RuntimeGenerationToken firstGeneration = runtime.Generation;
|
||||
var gameActions = new List<byte[]>();
|
||||
operations.Sessions[^1].GameActionCapture =
|
||||
body => gameActions.Add(body);
|
||||
|
||||
RuntimeCommandResult chat = adapter.Chat.Execute(
|
||||
runtime.Generation,
|
||||
new RuntimeChatCommand(
|
||||
RuntimeChatChannel.Say,
|
||||
"hello"));
|
||||
RuntimeCommandResult portal = adapter.Portal.Execute(
|
||||
runtime.Generation,
|
||||
RuntimePortalCommand.RecallLifestone);
|
||||
RuntimeCommandResult unsupported = adapter.Movement.Execute(
|
||||
runtime.Generation,
|
||||
RuntimeMovementCommand.Stop);
|
||||
|
||||
RuntimeSessionStartResult reconnected =
|
||||
adapter.Session.Reconnect(runtime.Generation);
|
||||
RuntimeCommandResult stale = adapter.Chat.Execute(
|
||||
firstGeneration,
|
||||
new RuntimeChatCommand(
|
||||
RuntimeChatChannel.Say,
|
||||
"stale"));
|
||||
|
||||
Assert.Equal(RuntimeSessionStartStatus.Connected, started.Status);
|
||||
Assert.Equal(
|
||||
RuntimeSessionStartStatus.Connected,
|
||||
reconnected.Status);
|
||||
Assert.True(chat.Accepted);
|
||||
Assert.True(portal.Accepted);
|
||||
Assert.Equal(
|
||||
RuntimeCommandStatus.Unsupported,
|
||||
unsupported.Status);
|
||||
Assert.Equal(
|
||||
RuntimeCommandStatus.StaleGeneration,
|
||||
stale.Status);
|
||||
Assert.Equal(2, gameActions.Count);
|
||||
Assert.Contains(
|
||||
trace.Entries,
|
||||
entry => entry.Kind == RuntimeTraceKind.Command
|
||||
&& (entry.Code >> 16)
|
||||
== (int)RuntimeCommandDomain.Chat
|
||||
&& entry.Text == "hello");
|
||||
Assert.Contains(
|
||||
trace.Entries,
|
||||
entry => entry.Kind == RuntimeTraceKind.Command
|
||||
&& (entry.Code >> 16)
|
||||
== (int)RuntimeCommandDomain.Portal);
|
||||
|
||||
RuntimeTeardownAcknowledgement stopped =
|
||||
adapter.Session.Stop(runtime.Generation);
|
||||
Assert.True(stopped.IsComplete);
|
||||
Assert.False(runtime.Session.IsInWorld);
|
||||
}
|
||||
|
||||
private sealed class FixtureSessionOperations : ILiveSessionOperations
|
||||
{
|
||||
public List<WorldSession> Sessions { get; } = [];
|
||||
|
||||
public IPEndPoint ResolveEndpoint(string host, int port) =>
|
||||
new(IPAddress.Loopback, port);
|
||||
|
||||
public WorldSession CreateSession(IPEndPoint endpoint)
|
||||
{
|
||||
var session = new WorldSession(
|
||||
endpoint,
|
||||
new FixtureTransport());
|
||||
Sessions.Add(session);
|
||||
return session;
|
||||
}
|
||||
|
||||
public void Connect(
|
||||
WorldSession session,
|
||||
string user,
|
||||
string password)
|
||||
{
|
||||
}
|
||||
|
||||
public CharacterList.Parsed GetCharacters(WorldSession session) =>
|
||||
new(
|
||||
0u,
|
||||
[
|
||||
new CharacterList.Character(
|
||||
0x50000001u,
|
||||
"Direct",
|
||||
0u),
|
||||
],
|
||||
[],
|
||||
11,
|
||||
"account",
|
||||
true,
|
||||
true);
|
||||
|
||||
public void EnterWorld(
|
||||
WorldSession session,
|
||||
int activeCharacterIndex)
|
||||
{
|
||||
}
|
||||
|
||||
public void Tick(WorldSession session)
|
||||
{
|
||||
}
|
||||
|
||||
public void DisposeSession(WorldSession session) =>
|
||||
session.Dispose();
|
||||
}
|
||||
|
||||
private sealed class FixtureEventRoute : ILiveSessionEventRouting
|
||||
{
|
||||
public void Attach()
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FixtureResetHost : IRuntimeGenerationResetHost
|
||||
{
|
||||
public void RetireEntityProjection(RuntimeEntityRecord entity)
|
||||
{
|
||||
}
|
||||
|
||||
public void DrainEntityProjectionBoundary()
|
||||
{
|
||||
}
|
||||
|
||||
public void CompleteEntityProjectionRetirement()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FixtureTransport : IWorldSessionTransport
|
||||
{
|
||||
public void Send(ReadOnlySpan<byte> datagram)
|
||||
{
|
||||
}
|
||||
|
||||
public void Send(
|
||||
IPEndPoint remote,
|
||||
ReadOnlySpan<byte> datagram)
|
||||
{
|
||||
}
|
||||
|
||||
public int Receive(
|
||||
Span<byte> destination,
|
||||
TimeSpan timeout,
|
||||
out IPEndPoint? from)
|
||||
{
|
||||
from = null;
|
||||
return -1;
|
||||
}
|
||||
|
||||
public ValueTask<NetReceiveResult> ReceiveAsync(
|
||||
Memory<byte> destination,
|
||||
CancellationToken cancellationToken) =>
|
||||
ValueTask.FromException<NetReceiveResult>(
|
||||
new OperationCanceledException(cancellationToken));
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FixtureGameplayOperations
|
||||
: IRuntimeCombatAttackOperations,
|
||||
IRuntimeCombatTargetOperations,
|
||||
IRuntimeCombatModeOperations,
|
||||
IRuntimeSpellCastOperations
|
||||
{
|
||||
private GameRuntime? _runtime;
|
||||
|
||||
public void Bind(GameRuntime runtime) => _runtime = runtime;
|
||||
public bool CanStartAttack() => false;
|
||||
public void PrepareAttackRequest()
|
||||
{
|
||||
}
|
||||
|
||||
public bool SendAttack(AttackHeight height, float power) => false;
|
||||
public void SendCancelAttack()
|
||||
{
|
||||
}
|
||||
|
||||
public bool IsDualWield => false;
|
||||
public bool PlayerReadyForAttack => false;
|
||||
public bool AutoRepeatAttack => false;
|
||||
public bool AutoTarget => false;
|
||||
public uint? SelectClosestTarget() => null;
|
||||
public bool IsInWorld => _runtime?.Session.IsInWorld == true;
|
||||
public IReadOnlyList<ClientObject> GetOrderedEquipment() => [];
|
||||
public void NotifyExplicitCombatModeRequest()
|
||||
{
|
||||
}
|
||||
|
||||
public void SendChangeCombatMode(CombatMode mode)
|
||||
{
|
||||
}
|
||||
|
||||
public uint LocalPlayerId =>
|
||||
_runtime?.PlayerIdentity.ServerGuid ?? 0u;
|
||||
public bool CanSend => false;
|
||||
public bool HasRequiredComponents(uint spellId) => false;
|
||||
|
||||
public bool IsTargetCompatible(
|
||||
uint targetId,
|
||||
SpellMetadata spell,
|
||||
bool showMessage) => false;
|
||||
|
||||
public void StopCompletely()
|
||||
{
|
||||
}
|
||||
|
||||
public void SendUntargeted(uint spellId)
|
||||
{
|
||||
}
|
||||
|
||||
public void SendTargeted(uint targetId, uint spellId)
|
||||
{
|
||||
}
|
||||
|
||||
public void DisplayMessage(string message)
|
||||
{
|
||||
}
|
||||
|
||||
public void IncrementBusy()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -351,6 +351,53 @@ public sealed class LiveSessionControllerTests
|
|||
Assert.Equal(1, operations.DisposeCounts[operations.Sessions[0]]);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("index")]
|
||||
[InlineData("id")]
|
||||
[InlineData("name")]
|
||||
public void Start_SelectsConfiguredAvailableCharacter(string selectorKind)
|
||||
{
|
||||
var calls = new List<string>();
|
||||
var operations = new TestOperations(calls);
|
||||
var host = new TestHost(calls);
|
||||
var controller = new LiveSessionController(operations);
|
||||
LiveSessionCharacterSelector selector = selectorKind switch
|
||||
{
|
||||
"index" => new(ActiveIndex: 1),
|
||||
"id" => new(CharacterId: 0x50000002u),
|
||||
"name" => new(CharacterName: "ready"),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(selectorKind)),
|
||||
};
|
||||
|
||||
LiveSessionStartResult result = controller.Start(
|
||||
LiveOptions(selector: selector),
|
||||
host);
|
||||
|
||||
Assert.Equal(LiveSessionStartStatus.Connected, result.Status);
|
||||
Assert.Equal(0x50000002u, result.Selection!.CharacterId);
|
||||
Assert.Contains("enter:1", calls);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Start_UnavailableConfiguredCharacterConvergesOffline()
|
||||
{
|
||||
var calls = new List<string>();
|
||||
var operations = new TestOperations(calls);
|
||||
var host = new TestHost(calls);
|
||||
var controller = new LiveSessionController(operations);
|
||||
|
||||
LiveSessionStartResult result = controller.Start(
|
||||
LiveOptions(
|
||||
selector: new LiveSessionCharacterSelector(
|
||||
CharacterId: 0x50000001u)),
|
||||
host);
|
||||
|
||||
Assert.Equal(LiveSessionStartStatus.NoCharacters, result.Status);
|
||||
Assert.False(controller.IsInWorld);
|
||||
Assert.Null(controller.CurrentSession);
|
||||
Assert.Equal(1, operations.DisposeCounts[operations.Sessions[0]]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Start_ConnectFailureConvergesOffline()
|
||||
{
|
||||
|
|
@ -926,13 +973,15 @@ public sealed class LiveSessionControllerTests
|
|||
|
||||
private static LiveSessionConnectOptions LiveOptions(
|
||||
bool live = true,
|
||||
string? user = "user") =>
|
||||
string? user = "user",
|
||||
LiveSessionCharacterSelector? selector = null) =>
|
||||
new(
|
||||
live,
|
||||
"127.0.0.1",
|
||||
9000,
|
||||
user ?? string.Empty,
|
||||
"password");
|
||||
"password",
|
||||
selector);
|
||||
|
||||
private static CharacterList.Parsed AvailableCharacters() => new(
|
||||
0u,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,278 @@
|
|||
using System.Net;
|
||||
using AcDream.Core.Combat;
|
||||
using AcDream.Core.Items;
|
||||
using AcDream.Core.Net;
|
||||
using AcDream.Core.Net.Messages;
|
||||
using AcDream.Core.Physics;
|
||||
using AcDream.Core.Spells;
|
||||
using AcDream.Runtime.Entities;
|
||||
using AcDream.Runtime.Gameplay;
|
||||
using AcDream.Runtime.Session;
|
||||
|
||||
namespace AcDream.Runtime.Tests.Session;
|
||||
|
||||
public sealed class RuntimeLiveEntitySessionControllerTests
|
||||
{
|
||||
[Fact]
|
||||
public void DirectSinkOwnsCanonicalCreateUpdateDeleteWithoutProjection()
|
||||
{
|
||||
using GameRuntime runtime = CreateRuntime();
|
||||
using var session = new WorldSession(
|
||||
new IPEndPoint(IPAddress.Loopback, 9000),
|
||||
new FixtureTransport());
|
||||
var controller = new RuntimeLiveEntitySessionController(
|
||||
runtime,
|
||||
session);
|
||||
LiveEntitySessionSink sink = controller.CreateSink();
|
||||
WorldSession.EntitySpawn spawn =
|
||||
Spawn(0x70000001u, incarnation: 1);
|
||||
|
||||
sink.Spawned(spawn);
|
||||
sink.PositionUpdated(new WorldSession.EntityPositionUpdate(
|
||||
spawn.Guid,
|
||||
spawn.Position!.Value with
|
||||
{
|
||||
PositionX = 20f,
|
||||
},
|
||||
Velocity: null,
|
||||
PlacementId: null,
|
||||
IsGrounded: true,
|
||||
InstanceSequence: 1,
|
||||
PositionSequence: 2,
|
||||
TeleportSequence: 0,
|
||||
ForcePositionSequence: 0));
|
||||
|
||||
Assert.Equal(1, runtime.Entities.Count);
|
||||
Assert.Equal(1, runtime.Inventory.ObjectCount);
|
||||
Assert.True(
|
||||
runtime.EntityObjects.Entities.TryGetActive(
|
||||
spawn.Guid,
|
||||
out RuntimeEntityRecord canonical));
|
||||
Assert.Equal(
|
||||
20f,
|
||||
canonical.Snapshot.Position!.Value.PositionX);
|
||||
|
||||
sink.Deleted(new DeleteObject.Parsed(spawn.Guid, 1));
|
||||
|
||||
Assert.Equal(0, runtime.Entities.Count);
|
||||
Assert.Equal(0, runtime.Inventory.ObjectCount);
|
||||
Assert.Equal(
|
||||
0,
|
||||
runtime.EntityObjects.Entities.PendingTeardownCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DirectSinkCompletesExactPortalAndSendsLoginComplete()
|
||||
{
|
||||
using GameRuntime runtime = CreateRuntime();
|
||||
const uint playerGuid = 0x50000001u;
|
||||
runtime.PlayerIdentity.ServerGuid = playerGuid;
|
||||
using var session = new WorldSession(
|
||||
new IPEndPoint(IPAddress.Loopback, 9000),
|
||||
new FixtureTransport());
|
||||
var gameActions = new List<byte[]>();
|
||||
session.GameActionCapture = body => gameActions.Add(body);
|
||||
var controller = new RuntimeLiveEntitySessionController(
|
||||
runtime,
|
||||
session);
|
||||
LiveEntitySessionSink sink = controller.CreateSink();
|
||||
WorldSession.EntitySpawn spawn =
|
||||
Spawn(playerGuid, incarnation: 1);
|
||||
sink.Spawned(spawn);
|
||||
|
||||
sink.TeleportStarted(1u);
|
||||
sink.PositionUpdated(new WorldSession.EntityPositionUpdate(
|
||||
playerGuid,
|
||||
spawn.Position!.Value with
|
||||
{
|
||||
LandblockId = 0x01020001u,
|
||||
PositionX = 30f,
|
||||
},
|
||||
Velocity: null,
|
||||
PlacementId: null,
|
||||
IsGrounded: true,
|
||||
InstanceSequence: 1,
|
||||
PositionSequence: 2,
|
||||
TeleportSequence: 1,
|
||||
ForcePositionSequence: 0));
|
||||
|
||||
Assert.Single(gameActions);
|
||||
Assert.True(runtime.TransitOwner.CaptureOwnership().IsSessionIdle);
|
||||
Assert.True(runtime.Portal.Snapshot.Completed);
|
||||
Assert.Equal(0x01020001u, runtime.Portal.Snapshot.DestinationCell);
|
||||
}
|
||||
|
||||
private static GameRuntime CreateRuntime()
|
||||
{
|
||||
var operations = new FixtureGameplayOperations();
|
||||
var runtime = new GameRuntime(new GameRuntimeDependencies(
|
||||
operations,
|
||||
operations,
|
||||
operations,
|
||||
operations));
|
||||
operations.Bind(runtime);
|
||||
return runtime;
|
||||
}
|
||||
|
||||
private static WorldSession.EntitySpawn Spawn(
|
||||
uint guid,
|
||||
ushort incarnation)
|
||||
{
|
||||
var position = new CreateObject.ServerPosition(
|
||||
0x01010001u,
|
||||
10f,
|
||||
10f,
|
||||
5f,
|
||||
1f,
|
||||
0f,
|
||||
0f,
|
||||
0f);
|
||||
var timestamps = new PhysicsTimestamps(
|
||||
Position: 1,
|
||||
Movement: 1,
|
||||
State: 1,
|
||||
Vector: 1,
|
||||
Teleport: 0,
|
||||
ServerControlledMove: 1,
|
||||
ForcePosition: 0,
|
||||
ObjDesc: 1,
|
||||
Instance: incarnation);
|
||||
var physics = new PhysicsSpawnData(
|
||||
RawState: (uint)PhysicsStateFlags.ReportCollisions,
|
||||
Position: position,
|
||||
Movement: null,
|
||||
AnimationFrame: null,
|
||||
SetupTableId: 0x02000001u,
|
||||
MotionTableId: null,
|
||||
SoundTableId: null,
|
||||
PhysicsScriptTableId: null,
|
||||
Parent: null,
|
||||
Children: null,
|
||||
Scale: null,
|
||||
Friction: null,
|
||||
Elasticity: null,
|
||||
Translucency: null,
|
||||
Velocity: null,
|
||||
Acceleration: null,
|
||||
AngularVelocity: null,
|
||||
DefaultScriptType: null,
|
||||
DefaultScriptIntensity: null,
|
||||
Timestamps: timestamps);
|
||||
return new WorldSession.EntitySpawn(
|
||||
guid,
|
||||
position,
|
||||
0x02000001u,
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
null,
|
||||
null,
|
||||
"direct entity",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
PhysicsState: physics.RawState,
|
||||
InstanceSequence: incarnation,
|
||||
MovementSequence: 1,
|
||||
ServerControlSequence: 1,
|
||||
PositionSequence: 1,
|
||||
Physics: physics);
|
||||
}
|
||||
|
||||
private sealed class FixtureTransport : IWorldSessionTransport
|
||||
{
|
||||
public void Send(ReadOnlySpan<byte> datagram)
|
||||
{
|
||||
}
|
||||
|
||||
public void Send(
|
||||
IPEndPoint remote,
|
||||
ReadOnlySpan<byte> datagram)
|
||||
{
|
||||
}
|
||||
|
||||
public int Receive(
|
||||
Span<byte> destination,
|
||||
TimeSpan timeout,
|
||||
out IPEndPoint? from)
|
||||
{
|
||||
from = null;
|
||||
return -1;
|
||||
}
|
||||
|
||||
public ValueTask<NetReceiveResult> ReceiveAsync(
|
||||
Memory<byte> destination,
|
||||
CancellationToken cancellationToken) =>
|
||||
ValueTask.FromException<NetReceiveResult>(
|
||||
new OperationCanceledException(cancellationToken));
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FixtureGameplayOperations
|
||||
: IRuntimeCombatAttackOperations,
|
||||
IRuntimeCombatTargetOperations,
|
||||
IRuntimeCombatModeOperations,
|
||||
IRuntimeSpellCastOperations
|
||||
{
|
||||
private GameRuntime? _runtime;
|
||||
|
||||
public void Bind(GameRuntime runtime) => _runtime = runtime;
|
||||
public bool CanStartAttack() => false;
|
||||
public void PrepareAttackRequest()
|
||||
{
|
||||
}
|
||||
|
||||
public bool SendAttack(AttackHeight height, float power) => false;
|
||||
public void SendCancelAttack()
|
||||
{
|
||||
}
|
||||
|
||||
public bool IsDualWield => false;
|
||||
public bool PlayerReadyForAttack => false;
|
||||
public bool AutoRepeatAttack => false;
|
||||
public bool AutoTarget => false;
|
||||
public uint? SelectClosestTarget() => null;
|
||||
public bool IsInWorld => _runtime?.Session.IsInWorld == true;
|
||||
public IReadOnlyList<ClientObject> GetOrderedEquipment() => [];
|
||||
public void NotifyExplicitCombatModeRequest()
|
||||
{
|
||||
}
|
||||
|
||||
public void SendChangeCombatMode(CombatMode mode)
|
||||
{
|
||||
}
|
||||
|
||||
public uint LocalPlayerId =>
|
||||
_runtime?.PlayerIdentity.ServerGuid ?? 0u;
|
||||
public bool CanSend => false;
|
||||
public bool HasRequiredComponents(uint spellId) => false;
|
||||
|
||||
public bool IsTargetCompatible(
|
||||
uint targetId,
|
||||
SpellMetadata spell,
|
||||
bool showMessage) => false;
|
||||
|
||||
public void StopCompletely()
|
||||
{
|
||||
}
|
||||
|
||||
public void SendUntargeted(uint spellId)
|
||||
{
|
||||
}
|
||||
|
||||
public void SendTargeted(uint targetId, uint spellId)
|
||||
{
|
||||
}
|
||||
|
||||
public void DisplayMessage(string message)
|
||||
{
|
||||
}
|
||||
|
||||
public void IncrementBusy()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue