diff --git a/DTAConfig/OptionPanels/CnCNetOptionsPanel.cs b/DTAConfig/OptionPanels/CnCNetOptionsPanel.cs index 4955069b2..3c487c4d9 100644 --- a/DTAConfig/OptionPanels/CnCNetOptionsPanel.cs +++ b/DTAConfig/OptionPanels/CnCNetOptionsPanel.cs @@ -254,7 +254,6 @@ public override bool Save() if (!String.IsNullOrEmpty(ClientConfiguration.Instance.DiscordAppId)) { - restartRequired = IniSettings.DiscordIntegration != chkDiscordIntegration.Checked; IniSettings.DiscordIntegration.Value = chkDiscordIntegration.Checked; } diff --git a/DXMainClient/DXGUI/Generic/LoadingScreen.cs b/DXMainClient/DXGUI/Generic/LoadingScreen.cs index 47fb35ba0..0b7fbd06b 100644 --- a/DXMainClient/DXGUI/Generic/LoadingScreen.cs +++ b/DXMainClient/DXGUI/Generic/LoadingScreen.cs @@ -81,7 +81,7 @@ private void Finish() "N/A" : CUpdater.GameVersion; DiscordHandler discordHandler = null; - if (!string.IsNullOrEmpty(ClientConfiguration.Instance.DiscordAppId) && UserINISettings.Instance.DiscordIntegration) + if (!string.IsNullOrEmpty(ClientConfiguration.Instance.DiscordAppId)) discordHandler = new DiscordHandler(WindowManager); var gameCollection = new GameCollection(); diff --git a/DXMainClient/DXGUI/Generic/MainMenu.cs b/DXMainClient/DXGUI/Generic/MainMenu.cs index 94879680d..34dc5f7ec 100644 --- a/DXMainClient/DXGUI/Generic/MainMenu.cs +++ b/DXMainClient/DXGUI/Generic/MainMenu.cs @@ -381,6 +381,11 @@ private void SettingsSaved(object sender, EventArgs e) if (!connectionManager.IsConnected) ProgramConstants.PLAYERNAME = UserINISettings.Instance.PlayerName; + + if (UserINISettings.Instance.DiscordIntegration) + discordHandler?.Connect(); + else + discordHandler?.Disconnect(); } /// diff --git a/DXMainClient/Domain/DiscordHandler.cs b/DXMainClient/Domain/DiscordHandler.cs index 22432bcc4..0656b4322 100644 --- a/DXMainClient/Domain/DiscordHandler.cs +++ b/DXMainClient/Domain/DiscordHandler.cs @@ -1,40 +1,38 @@ using ClientCore; using DiscordRPC; using DiscordRPC.Message; -using DTAClient.Online; using Microsoft.Xna.Framework; using Rampastring.Tools; using Rampastring.XNAUI; -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Net; -using System.Net.NetworkInformation; -using System.Text; using System.Text.RegularExpressions; -using System.Threading; namespace DTAClient.Domain { + /// + /// A class for handling Discord integration. + /// public class DiscordHandler : GameComponent { - public DiscordRpcClient client; + private DiscordRpcClient client; - private RichPresence currentPresence; + private RichPresence _currentPresence; + + /// + /// RichPresence instance that is currently being displayed. + /// public RichPresence CurrentPresence { get { - return currentPresence; + return _currentPresence; } set { - if (currentPresence == null || !currentPresence.Equals(PreviousPresence)) + if (_currentPresence == null || !_currentPresence.Equals(PreviousPresence)) { - PreviousPresence = CurrentPresence; - currentPresence = value; - client.SetPresence(currentPresence); + PreviousPresence = _currentPresence; + _currentPresence = value; + client.SetPresence(_currentPresence); } } } @@ -43,22 +41,54 @@ public RichPresence CurrentPresence /// RichPresence instance that was last displayed before the current one. /// public RichPresence PreviousPresence { get; private set; } - public DiscordHandler(WindowManager wm) : base(wm.Game) + + /// + /// Creates a new instance of Discord handler. + /// + /// The window manager. + public DiscordHandler(WindowManager windowManager) : base(windowManager.Game) + { + windowManager.Game.Components.Add(this); + } + + #region overrides + + public override void Initialize() { - this.wm = wm; + InitializeClient(); + UpdatePresence(); + + if (UserINISettings.Instance.DiscordIntegration) + Connect(); - wm.Game.Components.Add(this); + base.Initialize(); } - private WindowManager wm; + protected override void Dispose(bool disposing) + { + if (client.IsInitialized) + client.ClearPresence(); - // Overrides + client.Dispose(); + base.Dispose(disposing); + } - public override void Initialize() + #endregion + + #region methods + + /// + /// Initializes or reinitializes Discord RPC client object & event handlers. + /// + private void InitializeClient() { - client = new DiscordRpcClient(ClientConfiguration.Instance.DiscordAppId); + if (client != null && client.IsInitialized) + { + client.ClearPresence(); + client.Dispose(); + } - UpdatePresence(); + client = new DiscordRpcClient(ClientConfiguration.Instance.DiscordAppId); client.OnReady += OnReady; client.OnClose += OnClose; client.OnError += OnError; @@ -68,23 +98,44 @@ public override void Initialize() client.OnSubscribe += OnSubscribe; client.OnUnsubscribe += OnUnsubscribe; - client.Initialize(); - base.Initialize(); + if (CurrentPresence != null) + client.SetPresence(CurrentPresence); } - public override void Update(GameTime gameTime) + /// + /// Connects to Discord. + /// Does not do anything if the Discord RPC client has not been initialized or is already connected. + /// + public void Connect() { - client.Invoke(); - base.Update(gameTime); + if (client == null || client != null && client.IsInitialized) + return; + + bool success = client.Initialize(); + + if (success) + Logger.Log("DiscordHandler: Connected Discord RPC client."); + else + Logger.Log("DiscordHandler: Failed to connect Discord RPC client."); } - protected override void Dispose(bool disposing) + /// + /// Disconnects from Discord. + /// Does not do anything if the Discord RPC client has not been initialized or is not connected. + /// + public void Disconnect() { - client.Dispose(); - base.Dispose(disposing); - } + if (client == null || !client.IsInitialized) + return; + + // HACK warning + // Currently DiscordRpcClient does not appear to have any way to reliably disconnect and reconnect using same client object. + // Deinitialize does not appear to completely reset connection state & resources and any attempts to call Initialize afterwards will fail. + // A hacky solution is to dispose current client object and create and initialize a new one. + InitializeClient(); //client.Deinitialize(); - // Methods + Logger.Log("DiscordHandler: Disconnected Discord RPC client."); + } /// /// Updates Discord Rich Presence with default info. @@ -232,7 +283,9 @@ public void UpdatePresence(string save, bool resetTimer = false) }; } - // Event handlers + #endregion + + #region eventhandlers private void OnReady(object sender, ReadyMessage args) { @@ -274,5 +327,7 @@ private void OnUnsubscribe(object sender, UnsubscribeMessage args) { Logger.Log($"Discord: Unsubscribed: {args.Event}"); } + + #endregion } }