diff --git a/assets/discord/app.png b/assets/discord/app.png new file mode 100644 index 000000000..310bc0478 Binary files /dev/null and b/assets/discord/app.png differ diff --git a/src/xenia/app/discord/discord_presence.cc b/src/xenia/app/discord/discord_presence.cc index 1bf93c177..b3d10af1d 100644 --- a/src/xenia/app/discord/discord_presence.cc +++ b/src/xenia/app/discord/discord_presence.cc @@ -20,7 +20,7 @@ void HandleDiscordJoinGame(const char* joinSecret) {} void HandleDiscordJoinRequest(const DiscordUser* request) {} void HandleDiscordSpectateGame(const char* spectateSecret) {} -void DiscordPresence::InitializeDiscord() { +void DiscordPresence::Initialize() { DiscordEventHandlers handlers = {}; handlers.ready = &HandleDiscordReady; handlers.errored = &HandleDiscordError; @@ -39,7 +39,7 @@ void DiscordPresence::NotPlaying() { Discord_UpdatePresence(&discordPresence); } -void DiscordPresence::PlayingTitle(std::wstring game_title) { +void DiscordPresence::PlayingTitle(const std::wstring& game_title) { auto discord_game_title = xe::to_string(game_title); DiscordRichPresence discordPresence = {}; discordPresence.state = "In Game"; @@ -50,7 +50,7 @@ void DiscordPresence::PlayingTitle(std::wstring game_title) { Discord_UpdatePresence(&discordPresence); } -void DiscordPresence::ShutdownDiscord() { Discord_Shutdown(); } +void DiscordPresence::Shutdown() { Discord_Shutdown(); } } // namespace discord } // namespace xe diff --git a/src/xenia/app/discord/discord_presence.h b/src/xenia/app/discord/discord_presence.h index 9e7d67f59..d63ce70c1 100644 --- a/src/xenia/app/discord/discord_presence.h +++ b/src/xenia/app/discord/discord_presence.h @@ -17,10 +17,10 @@ namespace discord { class DiscordPresence { public: - static void InitializeDiscord(); + static void Initialize(); static void NotPlaying(); - static void PlayingTitle(std::wstring game_title); - static void ShutdownDiscord(); + static void PlayingTitle(const std::wstring& game_title); + static void Shutdown(); }; } // namespace discord diff --git a/src/xenia/app/emulator_window.cc b/src/xenia/app/emulator_window.cc index daf973fb6..6cb2cd452 100644 --- a/src/xenia/app/emulator_window.cc +++ b/src/xenia/app/emulator_window.cc @@ -13,7 +13,6 @@ #include "build/version.h" #include "third_party/imgui/imgui.h" -#include "xenia/app/discord/discord_presence.h" #include "xenia/base/clock.h" #include "xenia/base/cvar.h" #include "xenia/base/debugging.h" @@ -29,7 +28,6 @@ #include "xenia/ui/imgui_drawer.h" DECLARE_bool(debug); -DEFINE_bool(discord, false, "Enable Discord rich presence", "General"); namespace xe { namespace app { @@ -85,11 +83,6 @@ bool EmulatorWindow::Initialize() { return false; } - if (cvars::discord) { - discord::DiscordPresence::InitializeDiscord(); - discord::DiscordPresence::NotPlaying(); - } - UpdateTitle(); window_->on_closed.AddListener([this](UIEvent* e) { loop_->Quit(); }); @@ -344,9 +337,6 @@ void EmulatorWindow::FileOpen() { void EmulatorWindow::FileClose() { if (emulator_->is_title_open()) { emulator_->TerminateTitle(); - if (cvars::discord) { - discord::DiscordPresence::NotPlaying(); - } } } @@ -444,9 +434,6 @@ void EmulatorWindow::UpdateTitle() { auto game_title = emulator()->game_title(); title += xe::format_string(L" | [%.8X] %s", emulator()->title_id(), game_title.c_str()); - if (cvars::discord) { - discord::DiscordPresence::PlayingTitle(game_title); - } } auto graphics_system = emulator()->graphics_system(); diff --git a/src/xenia/app/xenia_main.cc b/src/xenia/app/xenia_main.cc index 5308526bf..73e27cca9 100644 --- a/src/xenia/app/xenia_main.cc +++ b/src/xenia/app/xenia_main.cc @@ -7,6 +7,7 @@ ****************************************************************************** */ +#include "xenia/app/discord/discord_presence.h" #include "xenia/app/emulator_window.h" #include "xenia/base/cvar.h" #include "xenia/base/debugging.h" @@ -56,6 +57,8 @@ DEFINE_bool(mount_cache, false, "Enable cache mount", "General"); CmdVar(target, "", "Specifies the target .xex or .iso to execute."); DECLARE_bool(debug); +DEFINE_bool(discord, true, "Enable Discord rich presence", "General"); + namespace xe { namespace app { @@ -145,6 +148,11 @@ int xenia_main(const std::vector& args) { Profiler::Initialize(); Profiler::ThreadEnter("main"); + if (cvars::discord) { + discord::DiscordPresence::Initialize(); + discord::DiscordPresence::NotPlaying(); + } + // Figure out where content should go. std::wstring content_root = xe::to_wstring(cvars::content_root); std::wstring config_folder; @@ -256,11 +264,21 @@ int xenia_main(const std::vector& args) { } auto evt = xe::threading::Event::CreateAutoResetEvent(false); - emulator->on_launch.AddListener([&]() { + emulator->on_launch.AddListener([&](auto title_id, const auto& game_title) { + if (cvars::discord) { + discord::DiscordPresence::PlayingTitle( + game_title.empty() ? L"Unknown Title" : game_title); + } emulator_window->UpdateTitle(); evt->Set(); }); + emulator->on_terminate.AddListener([&]() { + if (cvars::discord) { + discord::DiscordPresence::NotPlaying(); + } + }); + emulator_window->window()->on_closing.AddListener([&](ui::UIEvent* e) { // This needs to shut down before the graphics context. Profiler::Shutdown(); @@ -271,6 +289,10 @@ int xenia_main(const std::vector& args) { exiting = true; evt->Set(); + if (cvars::discord) { + discord::DiscordPresence::Shutdown(); + } + // TODO(DrChat): Remove this code and do a proper exit. XELOGI("Cheap-skate exit!"); exit(0); @@ -318,6 +340,10 @@ int xenia_main(const std::vector& args) { debug_window.reset(); emulator.reset(); + if (cvars::discord) { + discord::DiscordPresence::Shutdown(); + } + Profiler::Dump(); Profiler::Shutdown(); emulator_window.reset(); diff --git a/src/xenia/emulator.cc b/src/xenia/emulator.cc index 3a92ae99d..df48ac89c 100644 --- a/src/xenia/emulator.cc +++ b/src/xenia/emulator.cc @@ -53,6 +53,7 @@ namespace xe { Emulator::Emulator(const std::wstring& command_line, const std::wstring& content_root) : on_launch(), + on_terminate(), on_exit(), command_line_(command_line), content_root_(content_root), @@ -233,6 +234,7 @@ X_STATUS Emulator::TerminateTitle() { kernel_state_->TerminateTitle(); title_id_ = 0; game_title_ = L""; + on_terminate(); return X_STATUS_SUCCESS; } @@ -671,7 +673,7 @@ X_STATUS Emulator::CompleteLaunch(const std::wstring& path, } main_thread_ = main_thread; - on_launch(); + on_launch(title_id_, game_title_); return X_STATUS_SUCCESS; } diff --git a/src/xenia/emulator.h b/src/xenia/emulator.h index efc51a8ad..b9db4a31a 100644 --- a/src/xenia/emulator.h +++ b/src/xenia/emulator.h @@ -145,7 +145,8 @@ class Emulator { void WaitUntilExit(); public: - xe::Delegate<> on_launch; + xe::Delegate on_launch; + xe::Delegate<> on_terminate; xe::Delegate<> on_exit; private: