From 27a94308a7407e464a92a722d43dd8ccbc103f67 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Sat, 16 Jun 2018 22:56:21 +0200 Subject: [PATCH] Discord fixes --- rpcs3/rpcs3.vcxproj | 2 ++ rpcs3/rpcs3.vcxproj.filters | 6 ++++ rpcs3/rpcs3_app.cpp | 6 ++-- rpcs3/rpcs3qt/_discord_utils.cpp | 33 ++++++++++++++++++++ rpcs3/rpcs3qt/_discord_utils.h | 15 ++++++++++ rpcs3/rpcs3qt/main_window.cpp | 35 +++++++--------------- rpcs3/rpcs3qt/settings_dialog.cpp | 50 ++++++++++++++----------------- rpcs3/rpcs3qt/settings_dialog.h | 4 +++ 8 files changed, 96 insertions(+), 55 deletions(-) create mode 100644 rpcs3/rpcs3qt/_discord_utils.cpp create mode 100644 rpcs3/rpcs3qt/_discord_utils.h diff --git a/rpcs3/rpcs3.vcxproj b/rpcs3/rpcs3.vcxproj index bb843931b6..01a4d1f1dd 100644 --- a/rpcs3/rpcs3.vcxproj +++ b/rpcs3/rpcs3.vcxproj @@ -985,6 +985,7 @@ + @@ -1424,6 +1425,7 @@ .\QTGeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp "$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\QTGeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp" -D_WINDOWS -DUNICODE -DWIN32 -DWIN64 -DQT_OPENGL_LIB -DQT_WIDGETS_LIB -DQT_QUICK_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_WINEXTRAS_LIB -DLLVM_AVAILABLE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE "-I$(VULKAN_SDK)\Include" "-I.\.." "-I.\..\3rdparty\minidx12\Include" "-I$(QTDIR)\include" "-I$(QTDIR)\include\QtOpenGL" "-I$(QTDIR)\include\QtWidgets" "-I$(QTDIR)\include\QtQuick" "-I$(QTDIR)\include\QtGui" "-I$(QTDIR)\include\QtANGLE" "-I$(QTDIR)\include\QtQml" "-I$(QTDIR)\include\QtNetwork" "-I$(QTDIR)\include\QtCore" "-I.\debug" "-I$(QTDIR)\mkspecs\win32-msvc2015" "-I.\QTGeneratedFiles\$(ConfigurationName)\." "-I.\QTGeneratedFiles" "-I$(QTDIR)\include\QtWinExtras" + diff --git a/rpcs3/rpcs3.vcxproj.filters b/rpcs3/rpcs3.vcxproj.filters index 797257d46b..3f5393619c 100644 --- a/rpcs3/rpcs3.vcxproj.filters +++ b/rpcs3/rpcs3.vcxproj.filters @@ -638,6 +638,9 @@ Generated Files + + Gui\utils + @@ -721,6 +724,9 @@ Gui + + Gui\utils + diff --git a/rpcs3/rpcs3_app.cpp b/rpcs3/rpcs3_app.cpp index fe9e45d9a6..661226805b 100644 --- a/rpcs3/rpcs3_app.cpp +++ b/rpcs3/rpcs3_app.cpp @@ -5,8 +5,7 @@ #include "rpcs3qt/welcome_dialog.h" #ifdef WITH_DISCORD_RPC -#include "discord_rpc.h" -#include "discord_register.h" +#include "rpcs3qt/_discord_utils.h" #endif #include "Emu/System.h" @@ -98,8 +97,7 @@ void rpcs3_app::Init() // Discord Rich Presence Integration if (guiSettings->GetValue(gui::m_richPresence).toBool()) { - DiscordEventHandlers handlers = {}; - Discord_Initialize("424004941485572097", &handlers, 1, NULL); + discord::initialize(); } #endif diff --git a/rpcs3/rpcs3qt/_discord_utils.cpp b/rpcs3/rpcs3qt/_discord_utils.cpp new file mode 100644 index 0000000000..9d26900cf3 --- /dev/null +++ b/rpcs3/rpcs3qt/_discord_utils.cpp @@ -0,0 +1,33 @@ +#include "_discord_utils.h" +#include "discord_rpc.h" +#include "discord_register.h" + +namespace discord +{ + void initialize(const std::string& application_id) + { + DiscordEventHandlers handlers = {}; + Discord_Initialize(application_id.c_str(), &handlers, 1, NULL); + } + + void shutdown() + { + Discord_Shutdown(); + } + + void update_presence(const std::string& state, const std::string& details, bool reset_timer) + { + DiscordRichPresence discordPresence = {}; + discordPresence.details = details.c_str(); + discordPresence.state = state.c_str(); + discordPresence.largeImageKey = "rpcs3_logo"; + discordPresence.largeImageText = "RPCS3 is the world's first PlayStation 3 emulator."; + + if (reset_timer) + { + discordPresence.startTimestamp = time(0); + } + + Discord_UpdatePresence(&discordPresence); + } +} diff --git a/rpcs3/rpcs3qt/_discord_utils.h b/rpcs3/rpcs3qt/_discord_utils.h new file mode 100644 index 0000000000..d6cbbe268a --- /dev/null +++ b/rpcs3/rpcs3qt/_discord_utils.h @@ -0,0 +1,15 @@ +#pragma once + +#include "stdafx.h" + +namespace discord +{ + // Convenience function for initialization + void initialize(const std::string& application_id = "424004941485572097"); + + // Convenience function for shutdown + void shutdown(); + + // Convenience function for status updates. The default is set to idle. + void update_presence(const std::string& state = "", const std::string& details = "Idle", bool reset_timer = true); +} diff --git a/rpcs3/rpcs3qt/main_window.cpp b/rpcs3/rpcs3qt/main_window.cpp index e05c5f9e34..37dd726642 100644 --- a/rpcs3/rpcs3qt/main_window.cpp +++ b/rpcs3/rpcs3qt/main_window.cpp @@ -9,8 +9,7 @@ #include #ifdef WITH_DISCORD_RPC -#include "discord_rpc.h" -#include "discord_register.h" +#include "_discord_utils.h" #endif #include "qt_utils.h" @@ -65,7 +64,7 @@ main_window::~main_window() { delete ui; #ifdef WITH_DISCORD_RPC - Discord_Shutdown(); + discord::shutdown(); #endif } @@ -277,19 +276,6 @@ void main_window::Boot(const std::string& path, bool direct, bool add_only) LOG_SUCCESS(LOADER, "Boot successful."); const std::string serial = Emu.GetTitleID().empty() ? "" : "[" + Emu.GetTitleID() + "] "; AddRecentAction(gui::Recent_Game(qstr(Emu.GetBoot()), qstr(serial + Emu.GetTitle()))); -#ifdef WITH_DISCORD_RPC - // Discord Rich Presence Integration - if (guiSettings->GetValue(gui::m_richPresence).toBool()) - { - DiscordRichPresence discordPresence = {}; - discordPresence.state = Emu.GetTitleID().c_str(); - discordPresence.details = Emu.GetTitle().c_str(); - discordPresence.largeImageKey = "rpcs3_logo"; - discordPresence.largeImageText = "RPCS3 is the world's first PlayStation 3 emulator."; - discordPresence.startTimestamp = time(0); - Discord_UpdatePresence(&discordPresence); - } -#endif } else { @@ -775,6 +761,14 @@ void main_window::OnEmuRun() ui->toolbar_start->setIcon(m_icon_pause); ui->toolbar_start->setToolTip(tr("Pause emulation")); EnableMenus(true); + +#ifdef WITH_DISCORD_RPC + // Discord Rich Presence Integration + if (guiSettings->GetValue(gui::m_richPresence).toBool()) + { + discord::update_presence(Emu.GetTitleID(), Emu.GetTitle()); + } +#endif } void main_window::OnEmuResume() @@ -832,14 +826,7 @@ void main_window::OnEmuStop() // Discord Rich Presence Integration if (guiSettings->GetValue(gui::m_richPresence).toBool()) { - QString discord_state = guiSettings->GetValue(gui::m_discordState).toString(); - DiscordRichPresence discordPresence = {}; - discordPresence.details = "Idle"; - discordPresence.state = sstr(discord_state).c_str(); - discordPresence.largeImageKey = "rpcs3_logo"; - discordPresence.largeImageText = "RPCS3 is the world's first PlayStation 3 emulator."; - discordPresence.startTimestamp = time(0); - Discord_UpdatePresence(&discordPresence); + discord::update_presence(sstr(guiSettings->GetValue(gui::m_discordState).toString())); } #endif } diff --git a/rpcs3/rpcs3qt/settings_dialog.cpp b/rpcs3/rpcs3qt/settings_dialog.cpp index 1a1e2e5df0..8f6cbda5c6 100644 --- a/rpcs3/rpcs3qt/settings_dialog.cpp +++ b/rpcs3/rpcs3qt/settings_dialog.cpp @@ -26,8 +26,7 @@ #include #ifdef WITH_DISCORD_RPC -#include "discord_rpc.h" -#include "discord_register.h" +#include "_discord_utils.h" #endif inline std::string sstr(const QString& _in) { return _in.toStdString(); } @@ -109,11 +108,11 @@ settings_dialog::settings_dialog(std::shared_ptr guiSettings, std: } // Discord variables - bool use_discord = xgui_settings->GetValue(gui::m_richPresence).toBool(); - QString discord_state = xgui_settings->GetValue(gui::m_discordState).toString(); + m_use_discord = xgui_settings->GetValue(gui::m_richPresence).toBool(); + m_discord_state = xgui_settings->GetValue(gui::m_discordState).toString(); // Various connects - connect(ui->okButton, &QAbstractButton::clicked, [=] + connect(ui->okButton, &QAbstractButton::clicked, [=, use_discord_old = m_use_discord, discord_state_old = m_discord_state] { std::set selectedlle; for (int i = 0; illeList->count(); ++i) @@ -129,30 +128,27 @@ settings_dialog::settings_dialog(std::shared_ptr guiSettings, std: xemu_settings->SaveSettings(); accept(); + // Discord Settings can be saved regardless of WITH_DISCORD_RPC + xgui_settings->SetValue(gui::m_richPresence, m_use_discord); + xgui_settings->SetValue(gui::m_discordState, m_discord_state); + #ifdef WITH_DISCORD_RPC - bool use_discord_new = xgui_settings->GetValue(gui::m_richPresence).toBool(); - QString discord_state_new = xgui_settings->GetValue(gui::m_discordState).toString(); - - if (use_discord != use_discord_new || discord_state != discord_state_new) + if (m_use_discord != use_discord_old) { - if (use_discord_new) + if (m_use_discord) { - DiscordEventHandlers handlers = {}; - Discord_Initialize("424004941485572097", &handlers, 1, NULL); - - DiscordRichPresence discordPresence = {}; - discordPresence.details = "Idle"; - discordPresence.state = sstr(discord_state_new).c_str(); - discordPresence.largeImageKey = "rpcs3_logo"; - discordPresence.largeImageText = "RPCS3 is the world's first PlayStation 3 emulator."; - discordPresence.startTimestamp = time(0); - Discord_UpdatePresence(&discordPresence); + discord::initialize(); + discord::update_presence(sstr(m_discord_state)); } else { - Discord_ClearPresence(); + discord::shutdown(); } } + else if (m_discord_state != discord_state_old && Emu.IsStopped()) + { + discord::update_presence(sstr(m_discord_state), "Idle", false); + } #endif }); @@ -868,21 +864,21 @@ settings_dialog::settings_dialog(std::shared_ptr guiSettings, std: if (!game) { // Discord: - ui->useRichPresence->setChecked(use_discord); - ui->label_discordState->setEnabled(use_discord); - ui->discordState->setEnabled(use_discord); - ui->discordState->setText(discord_state); + ui->useRichPresence->setChecked(m_use_discord); + ui->label_discordState->setEnabled(m_use_discord); + ui->discordState->setEnabled(m_use_discord); + ui->discordState->setText(m_discord_state); connect(ui->useRichPresence, &QCheckBox::clicked, [this](bool checked) { ui->discordState->setEnabled(checked); ui->label_discordState->setEnabled(checked); - xgui_settings->SetValue(gui::m_richPresence, checked); + m_use_discord = checked; }); connect(ui->discordState, &QLineEdit::editingFinished, [this]() { - xgui_settings->SetValue(gui::m_discordState, ui->discordState->text()); + m_discord_state = ui->discordState->text(); }); // colorize preview icons diff --git a/rpcs3/rpcs3qt/settings_dialog.h b/rpcs3/rpcs3qt/settings_dialog.h index 9b74b567f3..33d9b68b79 100644 --- a/rpcs3/rpcs3qt/settings_dialog.h +++ b/rpcs3/rpcs3qt/settings_dialog.h @@ -47,6 +47,10 @@ private: std::shared_ptr xgui_settings; std::shared_ptr xemu_settings; + // Discord + bool m_use_discord; + QString m_discord_state; + // descriptions QList> m_description_labels; QHash m_descriptions;