diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/yuzu/discord_impl.cpp | 84 | ||||
| -rw-r--r-- | src/yuzu/discord_impl.h | 7 |
2 files changed, 55 insertions, 36 deletions
diff --git a/src/yuzu/discord_impl.cpp b/src/yuzu/discord_impl.cpp index ac2fc1bcb..913c7bb99 100644 --- a/src/yuzu/discord_impl.cpp +++ b/src/yuzu/discord_impl.cpp | |||
| @@ -3,9 +3,12 @@ | |||
| 3 | 3 | ||
| 4 | #include <chrono> | 4 | #include <chrono> |
| 5 | #include <string> | 5 | #include <string> |
| 6 | |||
| 7 | #include <QNetworkAccessManager> | ||
| 8 | #include <QNetworkReply> | ||
| 6 | #include <discord_rpc.h> | 9 | #include <discord_rpc.h> |
| 7 | #include <fmt/format.h> | 10 | #include <fmt/format.h> |
| 8 | #include <httplib.h> | 11 | |
| 9 | #include "common/common_types.h" | 12 | #include "common/common_types.h" |
| 10 | #include "common/string_util.h" | 13 | #include "common/string_util.h" |
| 11 | #include "core/core.h" | 14 | #include "core/core.h" |
| @@ -31,7 +34,7 @@ void DiscordImpl::Pause() { | |||
| 31 | Discord_ClearPresence(); | 34 | Discord_ClearPresence(); |
| 32 | } | 35 | } |
| 33 | 36 | ||
| 34 | static std::string GetGameString(const std::string& title) { | 37 | std::string DiscordImpl::GetGameString(const std::string& title) { |
| 35 | // Convert to lowercase | 38 | // Convert to lowercase |
| 36 | std::string icon_name = Common::ToLower(title); | 39 | std::string icon_name = Common::ToLower(title); |
| 37 | 40 | ||
| @@ -56,51 +59,60 @@ static std::string GetGameString(const std::string& title) { | |||
| 56 | return icon_name; | 59 | return icon_name; |
| 57 | } | 60 | } |
| 58 | 61 | ||
| 59 | void DiscordImpl::Update() { | 62 | void DiscordImpl::UpdateGameStatus(bool use_default) { |
| 63 | const std::string default_text = "yuzu is an emulator for the Nintendo Switch"; | ||
| 64 | const std::string default_image = "yuzu_logo"; | ||
| 65 | const std::string url = use_default ? default_image : game_url; | ||
| 60 | s64 start_time = std::chrono::duration_cast<std::chrono::seconds>( | 66 | s64 start_time = std::chrono::duration_cast<std::chrono::seconds>( |
| 61 | std::chrono::system_clock::now().time_since_epoch()) | 67 | std::chrono::system_clock::now().time_since_epoch()) |
| 62 | .count(); | 68 | .count(); |
| 69 | DiscordRichPresence presence{}; | ||
| 70 | |||
| 71 | presence.largeImageKey = url.c_str(); | ||
| 72 | presence.largeImageText = game_title.c_str(); | ||
| 73 | presence.smallImageKey = default_image.c_str(); | ||
| 74 | presence.smallImageText = default_text.c_str(); | ||
| 75 | presence.state = game_title.c_str(); | ||
| 76 | presence.details = "Currently in game"; | ||
| 77 | presence.startTimestamp = start_time; | ||
| 78 | Discord_UpdatePresence(&presence); | ||
| 79 | } | ||
| 80 | |||
| 81 | void DiscordImpl::Update() { | ||
| 63 | const std::string default_text = "yuzu is an emulator for the Nintendo Switch"; | 82 | const std::string default_text = "yuzu is an emulator for the Nintendo Switch"; |
| 64 | const std::string default_image = "yuzu_logo"; | 83 | 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 | 84 | ||
| 70 | if (system.IsPoweredOn()) { | 85 | if (system.IsPoweredOn()) { |
| 71 | system.GetAppLoader().ReadTitle(title); | 86 | system.GetAppLoader().ReadTitle(game_title); |
| 72 | 87 | ||
| 73 | // Used to format Icon URL for yuzu website game compatibility page | 88 | // Used to format Icon URL for yuzu website game compatibility page |
| 74 | std::string icon_name = GetGameString(title); | 89 | std::string icon_name = GetGameString(game_title); |
| 75 | 90 | game_url = fmt::format("https://yuzu-emu.org/images/game/boxart/{}.png", icon_name); | |
| 76 | // New Check for game cover | 91 | |
| 77 | httplib::Client cli(game_cover_url); | 92 | QNetworkAccessManager* manager = new QNetworkAccessManager(); |
| 78 | cli.set_connection_timeout(std::chrono::seconds(3)); | 93 | |
| 79 | cli.set_read_timeout(std::chrono::seconds(3)); | 94 | QNetworkRequest request; |
| 80 | 95 | request.setUrl(QUrl(QString::fromStdString(game_url))); | |
| 81 | if (auto res = cli.Head(fmt::format("/images/game/boxart/{}.png", icon_name))) { | 96 | request.setTransferTimeout(3000); |
| 82 | if (res->status == 200) { | 97 | QNetworkReply* rep = manager->get(request); |
| 83 | game_cover_url += fmt::format("/images/game/boxart/{}.png", icon_name); | 98 | |
| 84 | } else { | 99 | QObject::connect(manager, &QNetworkAccessManager::finished, |
| 85 | game_cover_url = "yuzu_logo"; | 100 | [this](QNetworkReply* reply) { UpdateGameStatus(reply->error()); }); |
| 86 | } | 101 | QObject::connect(manager, &QNetworkAccessManager::finished, manager, |
| 87 | } else { | 102 | &QNetworkAccessManager::deleteLater); |
| 88 | game_cover_url = "yuzu_logo"; | 103 | QObject::connect(manager, &QNetworkAccessManager::finished, rep, |
| 89 | } | 104 | &QNetworkReply::deleteLater); |
| 90 | 105 | return; | |
| 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 | } | 106 | } |
| 103 | 107 | ||
| 108 | s64 start_time = std::chrono::duration_cast<std::chrono::seconds>( | ||
| 109 | std::chrono::system_clock::now().time_since_epoch()) | ||
| 110 | .count(); | ||
| 111 | |||
| 112 | DiscordRichPresence presence{}; | ||
| 113 | presence.largeImageKey = default_image.c_str(); | ||
| 114 | presence.largeImageText = default_text.c_str(); | ||
| 115 | presence.details = "Currently not in game"; | ||
| 104 | presence.startTimestamp = start_time; | 116 | presence.startTimestamp = start_time; |
| 105 | Discord_UpdatePresence(&presence); | 117 | Discord_UpdatePresence(&presence); |
| 106 | } | 118 | } |
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 | ||