Skip to content

Commit

Permalink
Simplify access to Identity
Browse files Browse the repository at this point in the history
  • Loading branch information
sliekens committed Sep 16, 2023
1 parent fdeb177 commit 064c2d9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 36 deletions.
7 changes: 4 additions & 3 deletions GW2SDK.Tests/Features/Mumble/GameLinkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ public void The_link_provides_identity()
{
using var sut = GameLink.Open();

var snapshot = sut.GetSnapshot();
Assert.True(snapshot.TryGetIdentity(out var actual, MissingMemberBehavior.Error));
Assert.NotNull(actual);
var actual = sut.GetSnapshot().GetIdentity(MissingMemberBehavior.Error);

Assert.NotEmpty(actual.Name);
Assert.True(Enum.IsDefined(typeof(UiSize), actual.UiSize));
}
}
33 changes: 4 additions & 29 deletions GW2SDK/Features/Mumble/GameTick.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using System.Text.Json;
using JetBrains.Annotations;

Expand Down Expand Up @@ -44,33 +43,9 @@ public readonly record struct GameTick
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2048)]
public readonly string Description;

public bool TryGetIdentity(
[NotNullWhen(true)] out Identity? identity,
MissingMemberBehavior missingMemberBehavior
)
public Identity GetIdentity(MissingMemberBehavior missingMemberBehavior = MissingMemberBehavior.Error)
{
identity = default;
if (Name != "Guild Wars 2")
{
return false;
}

try
{
using var json = JsonDocument.Parse(Identity);
identity = json.RootElement.GetIdentity(missingMemberBehavior);
return true;
}
catch (JsonException)
{
// There is no synchronization between the game and Mumble, so we occassionally get a dirty read
// The result of dirty reads is that we can get partially modified JSON that is potentially invalid
//
// Example: toggling between commander tag on/off might give a partial snapshot where 'true' and 'false' leak into each other
// "commander": frue,
//
//
return false;
}
using var json = JsonDocument.Parse(Identity);
return json.RootElement.GetIdentity(missingMemberBehavior);
}
}
5 changes: 1 addition & 4 deletions samples/Mumble/GameReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
tick =>
{
// This callback is executed whenever the game client updates the shared memory
if (!tick.TryGetIdentity(out var identity, MissingMemberBehavior.Error))
{
return;
}
var identity = tick.GetIdentity();

var specialization = "no specialization";
if (specializationsDictionary.TryGetValue(identity.SpecializationId, out var found))
Expand Down

0 comments on commit 064c2d9

Please sign in to comment.