Fix NREs in DiscordService.

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.
This commit is contained in:
RoosterDragon
2024-02-06 19:23:32 +00:00
committed by Gustas
parent 4077f28285
commit 0c22499534

View File

@@ -33,7 +33,7 @@ namespace OpenRA.Mods.Common
{ {
public readonly string ApplicationId = null; public readonly string ApplicationId = null;
public readonly string Tooltip = "Open Source real-time strategy game engine for early Westwood titles."; public readonly string Tooltip = "Open Source real-time strategy game engine for early Westwood titles.";
DiscordRpcClient client; readonly DiscordRpcClient client;
DiscordState currentState; DiscordState currentState;
static DiscordService instance; static DiscordService instance;
@@ -105,7 +105,7 @@ namespace OpenRA.Mods.Common
if (currentState == state) if (currentState == state)
return; return;
if (instance == null) if (client == null)
return; return;
string stateText; string stateText;
@@ -192,6 +192,9 @@ namespace OpenRA.Mods.Common
void UpdateParty(int players, int slots) void UpdateParty(int players, int slots)
{ {
if (client == null)
return;
if (client.CurrentPresence.Party != null) if (client.CurrentPresence.Party != null)
{ {
client.UpdatePartySize(players, slots); client.UpdatePartySize(players, slots);
@@ -206,14 +209,18 @@ namespace OpenRA.Mods.Common
}); });
} }
void SetDetails(string details)
{
if (client == null)
return;
client.UpdateDetails(details);
}
public void Dispose() public void Dispose()
{ {
if (client != null) client?.Dispose();
{ instance = null;
client.Dispose();
client = null;
instance = null;
}
} }
public static void UpdateStatus(DiscordState state, string details = null, string secret = null, int? players = null, int? slots = null) public static void UpdateStatus(DiscordState state, string details = null, string secret = null, int? players = null, int? slots = null)
@@ -228,7 +235,7 @@ namespace OpenRA.Mods.Common
public static void UpdateDetails(string details) public static void UpdateDetails(string details)
{ {
Service?.client.UpdateDetails(details); Service?.SetDetails(details);
} }
} }
} }