Skip to content

Commit

Permalink
Fix NREs in DiscordService.
Browse files Browse the repository at this point in the history
Handle the client being null. Previously, a service could be created with a null client. This would leads to NREs when invoking the static Update methods. Now we guard against a null client.
  • Loading branch information
RoosterDragon committed Feb 6, 2024
1 parent 4077f28 commit a4bf635
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions OpenRA.Mods.Common/DiscordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public sealed class DiscordService : IGlobalModData, IDisposable
{
public readonly string ApplicationId = null;
public readonly string Tooltip = "Open Source real-time strategy game engine for early Westwood titles.";
DiscordRpcClient client;
readonly DiscordRpcClient client;
DiscordState currentState;

static DiscordService instance;
Expand Down Expand Up @@ -105,7 +105,7 @@ void SetStatus(DiscordState state, string details = null, string secret = null,
if (currentState == state)
return;

if (instance == null)
if (client == null)
return;

string stateText;
Expand Down Expand Up @@ -192,6 +192,9 @@ void SetStatus(DiscordState state, string details = null, string secret = null,

void UpdateParty(int players, int slots)
{
if (client == null)
return;

if (client.CurrentPresence.Party != null)
{
client.UpdatePartySize(players, slots);
Expand All @@ -206,14 +209,18 @@ void UpdateParty(int players, int slots)
});
}

void SetDetails(string details)
{
if (client == null)
return;

client.UpdateDetails(details);
}

public void Dispose()
{
if (client != null)
{
client.Dispose();
client = null;
instance = null;
}
client?.Dispose();
instance = null;
}

public static void UpdateStatus(DiscordState state, string details = null, string secret = null, int? players = null, int? slots = null)
Expand All @@ -228,7 +235,7 @@ public static void UpdatePlayers(int players, int slots)

public static void UpdateDetails(string details)
{
Service?.client.UpdateDetails(details);
Service?.SetDetails(details);
}
}
}

0 comments on commit a4bf635

Please sign in to comment.