From 0c224995342ab39bc77296d3c8e09e53e603838b Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Tue, 6 Feb 2024 19:23:32 +0000 Subject: [PATCH] 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. --- OpenRA.Mods.Common/DiscordService.cs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/OpenRA.Mods.Common/DiscordService.cs b/OpenRA.Mods.Common/DiscordService.cs index 6df53c0333..324d983512 100644 --- a/OpenRA.Mods.Common/DiscordService.cs +++ b/OpenRA.Mods.Common/DiscordService.cs @@ -33,7 +33,7 @@ namespace OpenRA.Mods.Common { 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; @@ -105,7 +105,7 @@ namespace OpenRA.Mods.Common if (currentState == state) return; - if (instance == null) + if (client == null) return; string stateText; @@ -192,6 +192,9 @@ namespace OpenRA.Mods.Common void UpdateParty(int players, int slots) { + if (client == null) + return; + if (client.CurrentPresence.Party != null) { 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() { - 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) @@ -228,7 +235,7 @@ namespace OpenRA.Mods.Common public static void UpdateDetails(string details) { - Service?.client.UpdateDetails(details); + Service?.SetDetails(details); } } }