From c06704638738f5162fbf23ac7e15db5e619028c9 Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Fri, 21 Jul 2023 13:08:46 -0600 Subject: [PATCH 1/2] yuzu: Replace httplib with QtNetworkRequest --- src/yuzu/discord_impl.cpp | 80 ++++++++++++++++++++++----------------- src/yuzu/discord_impl.h | 7 ++++ 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/src/yuzu/discord_impl.cpp b/src/yuzu/discord_impl.cpp index ac2fc1bcb6..913c7bb995 100644 --- a/src/yuzu/discord_impl.cpp +++ b/src/yuzu/discord_impl.cpp @@ -3,9 +3,12 @@ #include #include + +#include +#include #include #include -#include + #include "common/common_types.h" #include "common/string_util.h" #include "core/core.h" @@ -31,7 +34,7 @@ void DiscordImpl::Pause() { Discord_ClearPresence(); } -static std::string GetGameString(const std::string& title) { +std::string DiscordImpl::GetGameString(const std::string& title) { // Convert to lowercase std::string icon_name = Common::ToLower(title); @@ -56,51 +59,60 @@ static std::string GetGameString(const std::string& title) { return icon_name; } -void DiscordImpl::Update() { +void DiscordImpl::UpdateGameStatus(bool use_default) { + const std::string default_text = "yuzu is an emulator for the Nintendo Switch"; + const std::string default_image = "yuzu_logo"; + const std::string url = use_default ? default_image : game_url; s64 start_time = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) .count(); - const std::string default_text = "yuzu is an emulator for the Nintendo Switch"; - const std::string default_image = "yuzu_logo"; - std::string game_cover_url = "https://yuzu-emu.org"; - std::string title; - DiscordRichPresence presence{}; + presence.largeImageKey = url.c_str(); + presence.largeImageText = game_title.c_str(); + presence.smallImageKey = default_image.c_str(); + presence.smallImageText = default_text.c_str(); + presence.state = game_title.c_str(); + presence.details = "Currently in game"; + presence.startTimestamp = start_time; + Discord_UpdatePresence(&presence); +} + +void DiscordImpl::Update() { + const std::string default_text = "yuzu is an emulator for the Nintendo Switch"; + const std::string default_image = "yuzu_logo"; + if (system.IsPoweredOn()) { - system.GetAppLoader().ReadTitle(title); + system.GetAppLoader().ReadTitle(game_title); // Used to format Icon URL for yuzu website game compatibility page - std::string icon_name = GetGameString(title); + std::string icon_name = GetGameString(game_title); + game_url = fmt::format("https://yuzu-emu.org/images/game/boxart/{}.png", icon_name); - // New Check for game cover - httplib::Client cli(game_cover_url); - cli.set_connection_timeout(std::chrono::seconds(3)); - cli.set_read_timeout(std::chrono::seconds(3)); + QNetworkAccessManager* manager = new QNetworkAccessManager(); - if (auto res = cli.Head(fmt::format("/images/game/boxart/{}.png", icon_name))) { - if (res->status == 200) { - game_cover_url += fmt::format("/images/game/boxart/{}.png", icon_name); - } else { - game_cover_url = "yuzu_logo"; - } - } else { - game_cover_url = "yuzu_logo"; - } + QNetworkRequest request; + request.setUrl(QUrl(QString::fromStdString(game_url))); + request.setTransferTimeout(3000); + QNetworkReply* rep = manager->get(request); - presence.largeImageKey = game_cover_url.c_str(); - presence.largeImageText = title.c_str(); - - presence.smallImageKey = default_image.c_str(); - presence.smallImageText = default_text.c_str(); - presence.state = title.c_str(); - presence.details = "Currently in game"; - } else { - presence.largeImageKey = default_image.c_str(); - presence.largeImageText = default_text.c_str(); - presence.details = "Currently not in game"; + QObject::connect(manager, &QNetworkAccessManager::finished, + [this](QNetworkReply* reply) { UpdateGameStatus(reply->error()); }); + QObject::connect(manager, &QNetworkAccessManager::finished, manager, + &QNetworkAccessManager::deleteLater); + QObject::connect(manager, &QNetworkAccessManager::finished, rep, + &QNetworkReply::deleteLater); + return; } + s64 start_time = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); + + DiscordRichPresence presence{}; + presence.largeImageKey = default_image.c_str(); + presence.largeImageText = default_text.c_str(); + presence.details = "Currently not in game"; presence.startTimestamp = start_time; Discord_UpdatePresence(&presence); } diff --git a/src/yuzu/discord_impl.h b/src/yuzu/discord_impl.h index 84710b9c67..eb6cf9ae0f 100644 --- a/src/yuzu/discord_impl.h +++ b/src/yuzu/discord_impl.h @@ -19,6 +19,13 @@ public: void Pause() override; void Update() override; +private: + std::string GetGameString(const std::string& title); + void UpdateGameStatus(bool use_default); + + std::string game_url{}; + std::string game_title{}; + Core::System& system; }; From 01b379fe20cd0437815e4a8c329fc21b88fdc345 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Tue, 25 Jul 2023 21:33:46 -0400 Subject: [PATCH 2/2] Address feedback --- src/yuzu/discord_impl.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/yuzu/discord_impl.cpp b/src/yuzu/discord_impl.cpp index 913c7bb995..57b50abd0d 100644 --- a/src/yuzu/discord_impl.cpp +++ b/src/yuzu/discord_impl.cpp @@ -4,8 +4,10 @@ #include #include +#include #include #include + #include #include @@ -89,19 +91,15 @@ void DiscordImpl::Update() { std::string icon_name = GetGameString(game_title); game_url = fmt::format("https://yuzu-emu.org/images/game/boxart/{}.png", icon_name); - QNetworkAccessManager* manager = new QNetworkAccessManager(); - + QNetworkAccessManager manager; QNetworkRequest request; request.setUrl(QUrl(QString::fromStdString(game_url))); request.setTransferTimeout(3000); - QNetworkReply* rep = manager->get(request); - - QObject::connect(manager, &QNetworkAccessManager::finished, - [this](QNetworkReply* reply) { UpdateGameStatus(reply->error()); }); - QObject::connect(manager, &QNetworkAccessManager::finished, manager, - &QNetworkAccessManager::deleteLater); - QObject::connect(manager, &QNetworkAccessManager::finished, rep, - &QNetworkReply::deleteLater); + QNetworkReply* reply = manager.head(request); + QEventLoop request_event_loop; + QObject::connect(reply, &QNetworkReply::finished, &request_event_loop, &QEventLoop::quit); + request_event_loop.exec(); + UpdateGameStatus(reply->error()); return; }