diff options
| author | 2023-07-26 11:04:57 -0400 | |
|---|---|---|
| committer | 2023-07-26 11:04:57 -0400 | |
| commit | a1c355051e030d48b24044023c3fb35d2640386b (patch) | |
| tree | 3648e6d1d4389d8453c31941a08843d956c241d8 | |
| parent | Merge pull request #10990 from comex/ubsan (diff) | |
| parent | Address feedback (diff) | |
| download | yuzu-a1c355051e030d48b24044023c3fb35d2640386b.tar.gz yuzu-a1c355051e030d48b24044023c3fb35d2640386b.tar.xz yuzu-a1c355051e030d48b24044023c3fb35d2640386b.zip | |
Merge pull request #11128 from german77/discord
yuzu: Replace httplib with QtNetworkRequest
Diffstat (limited to '')
| -rw-r--r-- | src/yuzu/discord_impl.cpp | 82 | ||||
| -rw-r--r-- | src/yuzu/discord_impl.h | 7 |
2 files changed, 53 insertions, 36 deletions
diff --git a/src/yuzu/discord_impl.cpp b/src/yuzu/discord_impl.cpp index ac2fc1bcb..57b50abd0 100644 --- a/src/yuzu/discord_impl.cpp +++ b/src/yuzu/discord_impl.cpp | |||
| @@ -3,9 +3,14 @@ | |||
| 3 | 3 | ||
| 4 | #include <chrono> | 4 | #include <chrono> |
| 5 | #include <string> | 5 | #include <string> |
| 6 | |||
| 7 | #include <QEventLoop> | ||
| 8 | #include <QNetworkAccessManager> | ||
| 9 | #include <QNetworkReply> | ||
| 10 | |||
| 6 | #include <discord_rpc.h> | 11 | #include <discord_rpc.h> |
| 7 | #include <fmt/format.h> | 12 | #include <fmt/format.h> |
| 8 | #include <httplib.h> | 13 | |
| 9 | #include "common/common_types.h" | 14 | #include "common/common_types.h" |
| 10 | #include "common/string_util.h" | 15 | #include "common/string_util.h" |
| 11 | #include "core/core.h" | 16 | #include "core/core.h" |
| @@ -31,7 +36,7 @@ void DiscordImpl::Pause() { | |||
| 31 | Discord_ClearPresence(); | 36 | Discord_ClearPresence(); |
| 32 | } | 37 | } |
| 33 | 38 | ||
| 34 | static std::string GetGameString(const std::string& title) { | 39 | std::string DiscordImpl::GetGameString(const std::string& title) { |
| 35 | // Convert to lowercase | 40 | // Convert to lowercase |
| 36 | std::string icon_name = Common::ToLower(title); | 41 | std::string icon_name = Common::ToLower(title); |
| 37 | 42 | ||
| @@ -56,51 +61,56 @@ static std::string GetGameString(const std::string& title) { | |||
| 56 | return icon_name; | 61 | return icon_name; |
| 57 | } | 62 | } |
| 58 | 63 | ||
| 59 | void DiscordImpl::Update() { | 64 | void DiscordImpl::UpdateGameStatus(bool use_default) { |
| 65 | const std::string default_text = "yuzu is an emulator for the Nintendo Switch"; | ||
| 66 | const std::string default_image = "yuzu_logo"; | ||
| 67 | const std::string url = use_default ? default_image : game_url; | ||
| 60 | s64 start_time = std::chrono::duration_cast<std::chrono::seconds>( | 68 | s64 start_time = std::chrono::duration_cast<std::chrono::seconds>( |
| 61 | std::chrono::system_clock::now().time_since_epoch()) | 69 | std::chrono::system_clock::now().time_since_epoch()) |
| 62 | .count(); | 70 | .count(); |
| 71 | DiscordRichPresence presence{}; | ||
| 72 | |||
| 73 | presence.largeImageKey = url.c_str(); | ||
| 74 | presence.largeImageText = game_title.c_str(); | ||
| 75 | presence.smallImageKey = default_image.c_str(); | ||
| 76 | presence.smallImageText = default_text.c_str(); | ||
| 77 | presence.state = game_title.c_str(); | ||
| 78 | presence.details = "Currently in game"; | ||
| 79 | presence.startTimestamp = start_time; | ||
| 80 | Discord_UpdatePresence(&presence); | ||
| 81 | } | ||
| 82 | |||
| 83 | void DiscordImpl::Update() { | ||
| 63 | const std::string default_text = "yuzu is an emulator for the Nintendo Switch"; | 84 | const std::string default_text = "yuzu is an emulator for the Nintendo Switch"; |
| 64 | const std::string default_image = "yuzu_logo"; | 85 | const std::string default_image = "yuzu_logo"; |
| 65 | std::string game_cover_url = "https://yuzu-emu.org"; | ||
| 66 | std::string title; | ||
| 67 | |||
| 68 | DiscordRichPresence presence{}; | ||
| 69 | 86 | ||
| 70 | if (system.IsPoweredOn()) { | 87 | if (system.IsPoweredOn()) { |
| 71 | system.GetAppLoader().ReadTitle(title); | 88 | system.GetAppLoader().ReadTitle(game_title); |
| 72 | 89 | ||
| 73 | // Used to format Icon URL for yuzu website game compatibility page | 90 | // Used to format Icon URL for yuzu website game compatibility page |
| 74 | std::string icon_name = GetGameString(title); | 91 | std::string icon_name = GetGameString(game_title); |
| 75 | 92 | game_url = fmt::format("https://yuzu-emu.org/images/game/boxart/{}.png", icon_name); | |
| 76 | // New Check for game cover | 93 | |
| 77 | httplib::Client cli(game_cover_url); | 94 | QNetworkAccessManager manager; |
| 78 | cli.set_connection_timeout(std::chrono::seconds(3)); | 95 | QNetworkRequest request; |
| 79 | cli.set_read_timeout(std::chrono::seconds(3)); | 96 | request.setUrl(QUrl(QString::fromStdString(game_url))); |
| 80 | 97 | request.setTransferTimeout(3000); | |
| 81 | if (auto res = cli.Head(fmt::format("/images/game/boxart/{}.png", icon_name))) { | 98 | QNetworkReply* reply = manager.head(request); |
| 82 | if (res->status == 200) { | 99 | QEventLoop request_event_loop; |
| 83 | game_cover_url += fmt::format("/images/game/boxart/{}.png", icon_name); | 100 | QObject::connect(reply, &QNetworkReply::finished, &request_event_loop, &QEventLoop::quit); |
| 84 | } else { | 101 | request_event_loop.exec(); |
| 85 | game_cover_url = "yuzu_logo"; | 102 | UpdateGameStatus(reply->error()); |
| 86 | } | 103 | return; |
| 87 | } else { | ||
| 88 | game_cover_url = "yuzu_logo"; | ||
| 89 | } | ||
| 90 | |||
| 91 | presence.largeImageKey = game_cover_url.c_str(); | ||
| 92 | presence.largeImageText = title.c_str(); | ||
| 93 | |||
| 94 | presence.smallImageKey = default_image.c_str(); | ||
| 95 | presence.smallImageText = default_text.c_str(); | ||
| 96 | presence.state = title.c_str(); | ||
| 97 | presence.details = "Currently in game"; | ||
| 98 | } else { | ||
| 99 | presence.largeImageKey = default_image.c_str(); | ||
| 100 | presence.largeImageText = default_text.c_str(); | ||
| 101 | presence.details = "Currently not in game"; | ||
| 102 | } | 104 | } |
| 103 | 105 | ||
| 106 | s64 start_time = std::chrono::duration_cast<std::chrono::seconds>( | ||
| 107 | std::chrono::system_clock::now().time_since_epoch()) | ||
| 108 | .count(); | ||
| 109 | |||
| 110 | DiscordRichPresence presence{}; | ||
| 111 | presence.largeImageKey = default_image.c_str(); | ||
| 112 | presence.largeImageText = default_text.c_str(); | ||
| 113 | presence.details = "Currently not in game"; | ||
| 104 | presence.startTimestamp = start_time; | 114 | presence.startTimestamp = start_time; |
| 105 | Discord_UpdatePresence(&presence); | 115 | Discord_UpdatePresence(&presence); |
| 106 | } | 116 | } |
diff --git a/src/yuzu/discord_impl.h b/src/yuzu/discord_impl.h index 84710b9c6..eb6cf9ae0 100644 --- a/src/yuzu/discord_impl.h +++ b/src/yuzu/discord_impl.h | |||
| @@ -19,6 +19,13 @@ public: | |||
| 19 | void Pause() override; | 19 | void Pause() override; |
| 20 | void Update() override; | 20 | void Update() override; |
| 21 | 21 | ||
| 22 | private: | ||
| 23 | std::string GetGameString(const std::string& title); | ||
| 24 | void UpdateGameStatus(bool use_default); | ||
| 25 | |||
| 26 | std::string game_url{}; | ||
| 27 | std::string game_title{}; | ||
| 28 | |||
| 22 | Core::System& system; | 29 | Core::System& system; |
| 23 | }; | 30 | }; |
| 24 | 31 | ||