Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions DiscordRichPresence/Config.rc
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ BEGIN
PUSHBUTTON "Cancel",IDCANCEL,119,72,50,14
CONTROL "Display currently-playing title in Discord status",IDC_CHECK_DISPLAY_TITLE_IN_STATUS,
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,7,163,10
LTEXT "Discord Application ID:",IDC_STATIC,7,47,73,8
EDITTEXT IDC_EDIT_DISCORD_APPLICATION_ID,83,45,101,14,ES_AUTOHSCROLL
CONTROL "Send status when paused or stopped",IDC_SEND_WHEN_PAUSE_OR_STOPPED,
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,37,163,10
LTEXT "Discord Application ID:",IDC_STATIC,7,57,73,8
EDITTEXT IDC_EDIT_DISCORD_APPLICATION_ID,83,55,101,14,ES_AUTOHSCROLL
CONTROL "Show elapsed time",IDC_SHOW_ELAPSED_TIME,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,31,23,115,10
END

Expand Down
61 changes: 47 additions & 14 deletions DiscordRichPresence/DiscordRichPresence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,38 +93,45 @@ int init()

LoadSettingsFile();

g_presenceInfo.InitializeDiscordRPC();
if (g_pluginSettings.SendWhenPausedOrStopped)
{
g_presenceInfo.InitializeDiscordRPC();

ReportIdleStatus();
ReportIdleStatus();
}

return 0;
}

void ReportIdleStatus()
{
if (!g_presenceInfo.HasDiscordModuleLoaded())
{
if (g_pluginSettings.ApplicationID == "0")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird formatting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eesh, indeed.

VS Code isn't formatting and I am doing it by hand. Missed this.

return;

if (g_pluginSettings.ApplicationID == "0")
return;
if (!g_presenceInfo.IsDiscordRPCConnected())
{
g_presenceInfo.InitializeDiscordRPC();
}

g_presenceInfo.CurrentPlaybackState = Stopped;
g_presenceInfo.SetStartTimestamp(0);
g_presenceInfo.SetStateText("(Idle)");
g_presenceInfo.ClearDetails();

g_presenceInfo.PostToDiscord();
g_presenceInfo.PostToDiscord();
}

void ReportCurrentSongStatus(PlaybackState playbackState)
{
if (!g_presenceInfo.HasDiscordModuleLoaded())
return;
assert(playbackState != Stopped);

if (g_pluginSettings.ApplicationID == "0")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment about pre-existing- not a requested action
Been meaning to change this. I hate unnecessary string compares

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you talking about the ApplicationID check?

I'd like to expose a 'validate' function or similar that gate the connection... or even whisk all of the 'get connected' or 'get disconnected' into a wrapper function to encapsulate that logic.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, the ApplicationID check. For some reason it highlighted the wrong line. Not trying to de-rail your PR, this is probably another thing for another day.

return;

if (!g_presenceInfo.IsDiscordRPCConnected())
{
g_presenceInfo.InitializeDiscordRPC();
}

assert(playbackState != Stopped);
g_presenceInfo.CurrentPlaybackState = playbackState;
g_presenceInfo.SetStartTimestamp(0);
g_presenceInfo.SetStateText(playbackState == Playing ? "(Playing)" : "(Paused)");
Expand Down Expand Up @@ -163,12 +170,26 @@ void UpdateRichPresenceDetails()
else if (isPlayingResult == Paused)
{
g_timer.Stop();
ReportCurrentSongStatus(Paused);
if (g_pluginSettings.SendWhenPausedOrStopped)
{
ReportCurrentSongStatus(Paused);
}
else
{
g_presenceInfo.ShutdownDiscordRPC();
}
}
else if (isPlayingResult == Stopped)
{
g_timer.Stop();
ReportIdleStatus();
if (g_pluginSettings.SendWhenPausedOrStopped)
{
ReportIdleStatus();
}
else
{
g_presenceInfo.ShutdownDiscordRPC();
}
}
}

Expand Down Expand Up @@ -198,6 +219,9 @@ void UpdateInMemorySettingsFromDialogState(HWND hWndDlg)
{
g_pluginSettings.ApplicationID = stringData;
}

checkboxHwnd = GetDlgItem(hWndDlg, IDC_SEND_WHEN_PAUSE_OR_STOPPED);
g_pluginSettings.SendWhenPausedOrStopped = Button_GetCheck(checkboxHwnd) == BST_CHECKED;
}

void OnConfirmSettingsDialog(HWND hWndDlg)
Expand All @@ -209,8 +233,9 @@ void OnConfirmSettingsDialog(HWND hWndDlg)
bool applicationIDChanged = previousSettings.ApplicationID != g_pluginSettings.ApplicationID;
bool displayTitleSettingChanged = previousSettings.DisplayTitleInStatus != g_pluginSettings.DisplayTitleInStatus;
bool elapsedTimeChanged = previousSettings.ShowElapsedTime != g_pluginSettings.ShowElapsedTime;
bool sendOnPausedOrStopChanged = previousSettings.SendWhenPausedOrStopped != g_pluginSettings.SendWhenPausedOrStopped;

if (!applicationIDChanged && !displayTitleSettingChanged && !elapsedTimeChanged)
if (!applicationIDChanged && !displayTitleSettingChanged && !elapsedTimeChanged && !sendOnPausedOrStopChanged)
return; // Nothing to do

// Save settings to file
Expand All @@ -233,6 +258,11 @@ void OnConfirmSettingsDialog(HWND hWndDlg)
shouldUpdateRichPresenceDetails = true;
}

if (previousSettings.SendWhenPausedOrStopped != g_pluginSettings.SendWhenPausedOrStopped)
{
shouldUpdateRichPresenceDetails = true;
}

if (shouldUpdateRichPresenceDetails)
{
UpdateRichPresenceDetails();
Expand Down Expand Up @@ -263,6 +293,9 @@ void PopulateSettingsDialogFields(HWND hWndDlg)

HWND editboxHwnd = GetDlgItem(hWndDlg, IDC_EDIT_DISCORD_APPLICATION_ID);
SetWindowTextA(editboxHwnd, g_pluginSettings.ApplicationID.c_str());

checkboxHwnd = GetDlgItem(hWndDlg, IDC_SEND_WHEN_PAUSE_OR_STOPPED);
Button_SetCheck(checkboxHwnd, (g_pluginSettings.SendWhenPausedOrStopped ? BST_CHECKED : BST_UNCHECKED));
}

// Dialogue box callback function
Expand Down
17 changes: 17 additions & 0 deletions DiscordRichPresence/PresenceInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "DiscordRichPresence.h"
#include "SettingsFile.h"

//initialize the static variable
bool PresenceInfo::m_discordConnected = false;

PresenceInfo::PresenceInfo()
: m_initializeFn{}
, m_shutdownFn{}
Expand All @@ -17,6 +20,7 @@ PresenceInfo::PresenceInfo()
m_lowLevelTrackTitleIsUrl = false;
}


void PresenceInfo::SetStateText(char const* str)
{
m_stateBuffer = str;
Expand Down Expand Up @@ -77,14 +81,17 @@ bool PresenceInfo::HasDiscordModuleLoaded() const

static void handleDiscordReady(const DiscordUser* connectedUser)
{
PresenceInfo::SetDiscordRPCConnectStatus(true);
}

static void handleDiscordError(int errcode, const char* message)
{
PresenceInfo::SetDiscordRPCConnectStatus(false);
}

static void handleDiscordDisconnected(int errcode, const char* message)
{
PresenceInfo::SetDiscordRPCConnectStatus(false);
}

static void handleDiscordJoinGame(const char* secret)
Expand All @@ -99,6 +106,15 @@ static void handleDiscordJoinRequest(const DiscordUser* request)
{
}

void PresenceInfo::SetDiscordRPCConnectStatus(bool connected)
{
m_discordConnected = connected;
}
bool PresenceInfo::IsDiscordRPCConnected()
{
return HasDiscordModuleLoaded() && m_discordConnected;
}

void PresenceInfo::InitializeDiscordRPC()
{
if (g_pluginSettings.ApplicationID == "0")
Expand Down Expand Up @@ -137,5 +153,6 @@ void PresenceInfo::ShutdownDiscordRPC()
if (m_hDiscordModule)
{
m_shutdownFn();
PresenceInfo::SetDiscordRPCConnectStatus(false);
}
}
5 changes: 5 additions & 0 deletions DiscordRichPresence/PresenceInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class PresenceInfo
std::string m_streamingTrackTitleBuffer;

HMODULE m_hDiscordModule;
//used to interop with Discord callbacks for connected status
static bool m_discordConnected;

typedef void (DISCORD_EXPORT *Discord_InitializeFn)(const char*, DiscordEventHandlers*, int, const char*);
typedef void (DISCORD_EXPORT *Discord_ShutdownFn)(void);
Expand All @@ -28,6 +30,9 @@ class PresenceInfo
PresenceInfo();
PlaybackState CurrentPlaybackState;

//used to interop with Discord callbacks for connected status
static void SetDiscordRPCConnectStatus(bool);
bool IsDiscordRPCConnected();
void InitializeDiscordRPC();
void ShutdownDiscordRPC();

Expand Down
27 changes: 27 additions & 0 deletions DiscordRichPresence/SettingsFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ std::string GetExecutableFileName()
return std::string(buffer);
}

//used to convert strings to a usable string in MessageBox, example:
//std::wstring stemp = s2ws(executablePath);
//MessageBox(0, stemp.c_str(), L"", MB_OK);
//https://stackoverflow.com/a/27296
std::wstring s2ws(const std::string& s)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I need to put that into a personal dev scratchpad. I used it to show the folder paths as my settings.ini can't be updated by the plugin and I was diagnosing that (and ran into the evil/fun aspects of C++ and strings). Unable to solve my setting file save issues though... :(

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha. No worries

{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}

std::string GetExecutablePath()
{
std::string executableFileName = GetExecutableFileName();
Expand Down Expand Up @@ -51,6 +67,12 @@ void SaveSettingsFile()
settingsFileWrite << "ShowElapsedTime:false" << "\n";

settingsFileWrite << "ApplicationID:" << g_pluginSettings.ApplicationID << "\n";

if (g_pluginSettings.SendWhenPausedOrStopped)
settingsFileWrite << "SendWhenPausedOrStopped:true" << "\n";
else
settingsFileWrite << "SendWhenPausedOrStopped:false" << "\n";

settingsFileWrite.close();
}

Expand Down Expand Up @@ -91,6 +113,7 @@ void LoadSettingsFile()
static const char* displayTileInStatus_label = "DisplayTitleInStatus:";
static const char* showElapsedTime_label = "ShowElapsedTime:";
static const char* applicationID_label = "ApplicationID:";
static const char* sendWhenPausedOrStopped_label = "SendWhenPausedOrStopped:";

if (line.find(displayTileInStatus_label) == 0)
{
Expand All @@ -105,6 +128,10 @@ void LoadSettingsFile()
std::string value = line.substr(strlen(applicationID_label));
g_pluginSettings.ApplicationID = value;
}
else if (line.find(sendWhenPausedOrStopped_label) == 0)
{
g_pluginSettings.SendWhenPausedOrStopped = GetBooleanSettingsFileValue(line, sendWhenPausedOrStopped_label);
}
}
}
else
Expand Down
1 change: 1 addition & 0 deletions DiscordRichPresence/SettingsFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ struct PluginSettings
bool DisplayTitleInStatus;
bool ShowElapsedTime;
std::string ApplicationID;
bool SendWhenPausedOrStopped;
};

std::string GetSettingsFilePath();
Expand Down
1 change: 1 addition & 0 deletions DiscordRichPresence/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define IDC_BUTTON_APPLY 1004
#define IDC_CHECK1 1005
#define IDC_SHOW_ELAPSED_TIME 1005
#define IDC_SEND_WHEN_PAUSE_OR_STOPPED 1008

// Next default values for new objects
//
Expand Down
Binary file added Images/PluginDialog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.