Skip to content

Commit c16ef67

Browse files
committed
[App/Discord] Rework how rich presence is managed.
Updating rich presence state every time the window title changes is a bad idea(tm).
1 parent 7632746 commit c16ef67

File tree

7 files changed

+38
-22
lines changed

7 files changed

+38
-22
lines changed

assets/discord/app.png

54.7 KB
Loading

src/xenia/app/discord/discord_presence.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void HandleDiscordJoinGame(const char* joinSecret) {}
2020
void HandleDiscordJoinRequest(const DiscordUser* request) {}
2121
void HandleDiscordSpectateGame(const char* spectateSecret) {}
2222

23-
void DiscordPresence::InitializeDiscord() {
23+
void DiscordPresence::Initialize() {
2424
DiscordEventHandlers handlers = {};
2525
handlers.ready = &HandleDiscordReady;
2626
handlers.errored = &HandleDiscordError;
@@ -39,7 +39,7 @@ void DiscordPresence::NotPlaying() {
3939
Discord_UpdatePresence(&discordPresence);
4040
}
4141

42-
void DiscordPresence::PlayingTitle(std::wstring game_title) {
42+
void DiscordPresence::PlayingTitle(const std::wstring& game_title) {
4343
auto discord_game_title = xe::to_string(game_title);
4444
DiscordRichPresence discordPresence = {};
4545
discordPresence.state = "In Game";
@@ -50,7 +50,7 @@ void DiscordPresence::PlayingTitle(std::wstring game_title) {
5050
Discord_UpdatePresence(&discordPresence);
5151
}
5252

53-
void DiscordPresence::ShutdownDiscord() { Discord_Shutdown(); }
53+
void DiscordPresence::Shutdown() { Discord_Shutdown(); }
5454

5555
} // namespace discord
5656
} // namespace xe

src/xenia/app/discord/discord_presence.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ namespace discord {
1717

1818
class DiscordPresence {
1919
public:
20-
static void InitializeDiscord();
20+
static void Initialize();
2121
static void NotPlaying();
22-
static void PlayingTitle(std::wstring game_title);
23-
static void ShutdownDiscord();
22+
static void PlayingTitle(const std::wstring& game_title);
23+
static void Shutdown();
2424
};
2525

2626
} // namespace discord

src/xenia/app/emulator_window.cc

-13
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "build/version.h"
1414

1515
#include "third_party/imgui/imgui.h"
16-
#include "xenia/app/discord/discord_presence.h"
1716
#include "xenia/base/clock.h"
1817
#include "xenia/base/cvar.h"
1918
#include "xenia/base/debugging.h"
@@ -29,7 +28,6 @@
2928
#include "xenia/ui/imgui_drawer.h"
3029

3130
DECLARE_bool(debug);
32-
DEFINE_bool(discord, false, "Enable Discord rich presence", "General");
3331

3432
namespace xe {
3533
namespace app {
@@ -85,11 +83,6 @@ bool EmulatorWindow::Initialize() {
8583
return false;
8684
}
8785

88-
if (cvars::discord) {
89-
discord::DiscordPresence::InitializeDiscord();
90-
discord::DiscordPresence::NotPlaying();
91-
}
92-
9386
UpdateTitle();
9487

9588
window_->on_closed.AddListener([this](UIEvent* e) { loop_->Quit(); });
@@ -344,9 +337,6 @@ void EmulatorWindow::FileOpen() {
344337
void EmulatorWindow::FileClose() {
345338
if (emulator_->is_title_open()) {
346339
emulator_->TerminateTitle();
347-
if (cvars::discord) {
348-
discord::DiscordPresence::NotPlaying();
349-
}
350340
}
351341
}
352342

@@ -444,9 +434,6 @@ void EmulatorWindow::UpdateTitle() {
444434
auto game_title = emulator()->game_title();
445435
title += xe::format_string(L" | [%.8X] %s", emulator()->title_id(),
446436
game_title.c_str());
447-
if (cvars::discord) {
448-
discord::DiscordPresence::PlayingTitle(game_title);
449-
}
450437
}
451438

452439
auto graphics_system = emulator()->graphics_system();

src/xenia/app/xenia_main.cc

+27-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
******************************************************************************
88
*/
99

10+
#include "xenia/app/discord/discord_presence.h"
1011
#include "xenia/app/emulator_window.h"
1112
#include "xenia/base/cvar.h"
1213
#include "xenia/base/debugging.h"
@@ -56,6 +57,8 @@ DEFINE_bool(mount_cache, false, "Enable cache mount", "General");
5657
CmdVar(target, "", "Specifies the target .xex or .iso to execute.");
5758
DECLARE_bool(debug);
5859

60+
DEFINE_bool(discord, true, "Enable Discord rich presence", "General");
61+
5962
namespace xe {
6063
namespace app {
6164

@@ -145,6 +148,11 @@ int xenia_main(const std::vector<std::wstring>& args) {
145148
Profiler::Initialize();
146149
Profiler::ThreadEnter("main");
147150

151+
if (cvars::discord) {
152+
discord::DiscordPresence::Initialize();
153+
discord::DiscordPresence::NotPlaying();
154+
}
155+
148156
// Figure out where content should go.
149157
std::wstring content_root = xe::to_wstring(cvars::content_root);
150158
std::wstring config_folder;
@@ -256,11 +264,21 @@ int xenia_main(const std::vector<std::wstring>& args) {
256264
}
257265

258266
auto evt = xe::threading::Event::CreateAutoResetEvent(false);
259-
emulator->on_launch.AddListener([&]() {
267+
emulator->on_launch.AddListener([&](auto title_id, const auto& game_title) {
268+
if (cvars::discord) {
269+
discord::DiscordPresence::PlayingTitle(
270+
game_title.empty() ? L"Unknown Title" : game_title);
271+
}
260272
emulator_window->UpdateTitle();
261273
evt->Set();
262274
});
263275

276+
emulator->on_terminate.AddListener([&]() {
277+
if (cvars::discord) {
278+
discord::DiscordPresence::NotPlaying();
279+
}
280+
});
281+
264282
emulator_window->window()->on_closing.AddListener([&](ui::UIEvent* e) {
265283
// This needs to shut down before the graphics context.
266284
Profiler::Shutdown();
@@ -271,6 +289,10 @@ int xenia_main(const std::vector<std::wstring>& args) {
271289
exiting = true;
272290
evt->Set();
273291

292+
if (cvars::discord) {
293+
discord::DiscordPresence::Shutdown();
294+
}
295+
274296
// TODO(DrChat): Remove this code and do a proper exit.
275297
XELOGI("Cheap-skate exit!");
276298
exit(0);
@@ -318,6 +340,10 @@ int xenia_main(const std::vector<std::wstring>& args) {
318340
debug_window.reset();
319341
emulator.reset();
320342

343+
if (cvars::discord) {
344+
discord::DiscordPresence::Shutdown();
345+
}
346+
321347
Profiler::Dump();
322348
Profiler::Shutdown();
323349
emulator_window.reset();

src/xenia/emulator.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ namespace xe {
5353
Emulator::Emulator(const std::wstring& command_line,
5454
const std::wstring& content_root)
5555
: on_launch(),
56+
on_terminate(),
5657
on_exit(),
5758
command_line_(command_line),
5859
content_root_(content_root),
@@ -233,6 +234,7 @@ X_STATUS Emulator::TerminateTitle() {
233234
kernel_state_->TerminateTitle();
234235
title_id_ = 0;
235236
game_title_ = L"";
237+
on_terminate();
236238
return X_STATUS_SUCCESS;
237239
}
238240

@@ -671,7 +673,7 @@ X_STATUS Emulator::CompleteLaunch(const std::wstring& path,
671673
}
672674

673675
main_thread_ = main_thread;
674-
on_launch();
676+
on_launch(title_id_, game_title_);
675677

676678
return X_STATUS_SUCCESS;
677679
}

src/xenia/emulator.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ class Emulator {
145145
void WaitUntilExit();
146146

147147
public:
148-
xe::Delegate<> on_launch;
148+
xe::Delegate<uint32_t, const std::wstring&> on_launch;
149+
xe::Delegate<> on_terminate;
149150
xe::Delegate<> on_exit;
150151

151152
private:

0 commit comments

Comments
 (0)