Skip to content

Commit

Permalink
Simplify access to current Mount
Browse files Browse the repository at this point in the history
  • Loading branch information
sliekens committed Sep 16, 2023
1 parent 417d80a commit e4eb11e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
5 changes: 5 additions & 0 deletions GW2SDK.Tests/Features/Mumble/GameLinkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public void The_link_provides_context()
var server = gameTick.Context.ServerAddress;
Assert.NotEmpty(server.ToString());

if (gameTick.Context.IsMounted)
{
Assert.True(Enum.IsDefined(typeof(MountName), gameTick.Context.Mount));
}

// Port is not specified
Assert.Equal(0, server.Port);
}
Expand Down
8 changes: 5 additions & 3 deletions GW2SDK/Features/Mumble/Context.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Runtime.InteropServices;
using GuildWars2.Win32;
Expand Down Expand Up @@ -61,15 +62,16 @@ public record struct Context
[FieldOffset(84)]
internal readonly byte MountIndex;

public bool IsMounted => MountIndex != 0;
[MemberNotNullWhen(true, nameof(Mount))]
public readonly bool IsMounted => MountIndex != 0;

public readonly IPEndPoint ServerAddress =>
new(serverAddress.sin_addr.s_un.s_addr, serverAddress.sin_port);

[Pure]
public MountName GetMount() =>
public readonly MountName? Mount =>
MountIndex switch
{
0 => null,
1 => MountName.Jackal,
2 => MountName.Griffon,
3 => MountName.Springer,
Expand Down
7 changes: 1 addition & 6 deletions samples/Mumble/GameReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
else
{
var transport = "foot";
if (tick.Context.IsMounted)
{
transport = tick.Context.GetMount().ToString();
}

var transport = tick.Context.IsMounted ? tick.Context.Mount.ToString() : "foot";
logger.LogInformation(
"[{UiTick}] {Name}, {Title} ({Specialization}) is on {Transport} in {Map}, Position: {{ Latitude = {X}, Longitude = {Z}, Elevation = {Y} }}",
tick.UiTick,
Expand Down

0 comments on commit e4eb11e

Please sign in to comment.