Skip to content

Commit

Permalink
Catch JsonException in Mumble Link and expose RawIdentity
Browse files Browse the repository at this point in the history
  • Loading branch information
Archomeda committed Mar 29, 2020
1 parent c6f953f commit 90b3f81
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 34 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# Gw2Sharp History

## 0.9.3
### Services
- Exposed the raw JSON Mumble Link identity as `IGw2MumbleClient.RawIdentity`

### Fixes
- When the Mumble Link identity contains an invalid JSON, it will no longer cause Gw2Sharp to throw a JsonException (this can happen when running multiple instances of Guild Wars 2 at the same time)

## 0.9.2
### Fixes
- Setting a custom user agent now properly sets a space inbetween the that and the one set by Gw2Sharp
- Setting a custom user agent now properly sets a space inbetween that and the one set by Gw2Sharp

## 0.9.1
### Endpoints
Expand Down
1 change: 1 addition & 0 deletions Gw2Sharp.Tests/Mumble/Gw2MumbleClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void ReadStructCorrectlyTest()
Assert.Equal(0.8136908, client.CameraFront.X, 7);
Assert.Equal(-0.3139677, client.CameraFront.Y, 7);
Assert.Equal(-0.4892152, client.CameraFront.Z, 7);
Assert.Equal(@"{""name"":""Reiga Fiercecrusher"",""profession"":2,""spec"":18,""race"":1,""map_id"":1206,""world_id"":268435460,""team_color_id"":0,""commander"":false,""map"":1206,""fov"":0.960,""uisz"":1}", client.RawIdentity);
Assert.Equal("Reiga Fiercecrusher", client.CharacterName);
Assert.Equal(ProfessionType.Warrior, client.Profession);
Assert.Equal(RaceType.Charr, client.Race);
Expand Down
67 changes: 46 additions & 21 deletions Gw2Sharp/Mumble/Gw2MumbleClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Gw2MumbleClient : BaseClient, IGw2MumbleClient
internal static readonly char[] mumbleLinkGameName = new[] { 'G', 'u', 'i', 'l', 'd', ' ', 'W', 'a', 'r', 's', ' ', '2', '\0' };
private const string EMPTY_IDENTITY = "{}";

private string rawIdentity = EMPTY_IDENTITY;
private readonly object identityLock = new object();
private readonly object serverAddressLock = new object();

Expand Down Expand Up @@ -54,37 +55,48 @@ protected internal Gw2MumbleClient(IConnection connection, IGw2Client gw2Client)
() => this.memoryMappedFile.Value.CreateViewAccessor(), true);
}

private string identityCache = EMPTY_IDENTITY;
private CharacterIdentity? identity;
private unsafe CharacterIdentity? Identity
private unsafe void UpdateIdentityIfNeeded()
{
get
{
// Check if we're in the same frame update
if (this.linkedMem.identity[0] == '\0')
return this.identity;
// Check if we're in the same frame update
if (this.linkedMem.identity[0] == '\0')
return;

// Thread-safety
lock (this.identityLock)
// Thread-safety
lock (this.identityLock)
{
// Check again
if (this.linkedMem.identity[0] != '\0')
{
// Check again
if (this.linkedMem.identity[0] != '\0')
var cacheSpan = this.rawIdentity.AsSpan();
fixed (char* ptr = this.linkedMem.identity)
{
var cacheSpan = this.identityCache.AsSpan();
fixed (char* ptr = this.linkedMem.identity)
// Check if the identity is different from last update
var span = new ReadOnlySpan<char>(ptr, this.rawIdentity.Length);
if (!span.SequenceEqual(cacheSpan))
{
// Check if the identity is different from last update
var span = new ReadOnlySpan<char>(ptr, this.identityCache.Length);
if (!span.SequenceEqual(cacheSpan))
// Update cache
this.rawIdentity = new string(ptr);
try
{
this.identity = JsonSerializer.Deserialize<CharacterIdentity>(this.rawIdentity, deserializerSettings);
}
catch (JsonException)
{
// Update and parse JSON
this.identityCache = new string(ptr);
this.identity = JsonSerializer.Deserialize<CharacterIdentity>(this.identityCache, deserializerSettings);
this.identity = null;
}
}
this.linkedMem.identity[0] = '\0';
}
this.linkedMem.identity[0] = '\0';
}
}
}

private CharacterIdentity? identity;
private unsafe CharacterIdentity? Identity
{
get
{
this.UpdateIdentityIfNeeded();
return this.identity;
}
}
Expand Down Expand Up @@ -242,6 +254,19 @@ public unsafe string ServerAddress
public uint Instance =>
this.IsAvailable ? this.linkedMem.context.instance : default;

/// <inheritdoc />
public unsafe string RawIdentity
{
get
{
if (!this.IsAvailable)
return string.Empty;

this.UpdateIdentityIfNeeded();
return this.rawIdentity;
}
}

/// <inheritdoc />
public string CharacterName =>
this.IsAvailable && this.Identity != null ? this.Identity.Name : string.Empty;
Expand Down
5 changes: 5 additions & 0 deletions Gw2Sharp/Mumble/IGw2MumbleClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ public interface IGw2MumbleClient : IClient, IDisposable
/// </summary>
uint Instance { get; }

/// <summary>
/// The raw identity in JSON.
/// </summary>
string RawIdentity { get; }

/// <summary>
/// The character name.
/// </summary>
Expand Down
44 changes: 34 additions & 10 deletions MumbleLinkReader/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions MumbleLinkReader/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ private void MumbleLoop()
this.textBoxCameraFront2.Text = m.CameraFront.Y.ToString();
this.textBoxCameraFront3.Text = m.CameraFront.Z.ToString();

this.textBoxRawIdentity.Text = m.RawIdentity;
this.textBoxCharacterName.Text = m.CharacterName;
this.textBoxRace.Text = m.Race.ToString();
this.textBoxSpecialization.Text = m.Specialization.ToString();
Expand Down
3 changes: 1 addition & 2 deletions MumbleLinkReader/Form1.resx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
Expand Down

0 comments on commit 90b3f81

Please sign in to comment.