Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DiscordHandler cleanup. #236

Merged
merged 1 commit into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DTAConfig/OptionPanels/CnCNetOptionsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ public override bool Save()

if (!String.IsNullOrEmpty(ClientConfiguration.Instance.DiscordAppId))
{
restartRequired = IniSettings.DiscordIntegration != chkDiscordIntegration.Checked;
IniSettings.DiscordIntegration.Value = chkDiscordIntegration.Checked;
}

Expand Down
2 changes: 1 addition & 1 deletion DXMainClient/DXGUI/Generic/LoadingScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions DXMainClient/DXGUI/Generic/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/// <summary>
Expand Down
125 changes: 90 additions & 35 deletions DXMainClient/Domain/DiscordHandler.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A class for handling Discord integration.
/// </summary>
public class DiscordHandler : GameComponent
{
public DiscordRpcClient client;
private DiscordRpcClient client;

private RichPresence currentPresence;
private RichPresence _currentPresence;

/// <summary>
/// RichPresence instance that is currently being displayed.
/// </summary>
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);
}
}
}
Expand All @@ -43,22 +41,54 @@ public RichPresence CurrentPresence
/// RichPresence instance that was last displayed before the current one.
/// </summary>
public RichPresence PreviousPresence { get; private set; }
public DiscordHandler(WindowManager wm) : base(wm.Game)

/// <summary>
/// Creates a new instance of Discord handler.
/// </summary>
/// <param name="windowManager">The window manager.</param>
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

/// <summary>
/// Initializes or reinitializes Discord RPC client object & event handlers.
/// </summary>
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;
Expand All @@ -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)
/// <summary>
/// Connects to Discord.
/// Does not do anything if the Discord RPC client has not been initialized or is already connected.
/// </summary>
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)
/// <summary>
/// Disconnects from Discord.
/// Does not do anything if the Discord RPC client has not been initialized or is not connected.
/// </summary>
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();
Metadorius marked this conversation as resolved.
Show resolved Hide resolved

// Methods
Logger.Log("DiscordHandler: Disconnected Discord RPC client.");
}

/// <summary>
/// Updates Discord Rich Presence with default info.
Expand Down Expand Up @@ -232,7 +283,9 @@ public void UpdatePresence(string save, bool resetTimer = false)
};
}

// Event handlers
#endregion

#region eventhandlers

private void OnReady(object sender, ReadyMessage args)
{
Expand Down Expand Up @@ -274,5 +327,7 @@ private void OnUnsubscribe(object sender, UnsubscribeMessage args)
{
Logger.Log($"Discord: Unsubscribed: {args.Event}");
}

#endregion
}
}