2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

Merge pull request #11128 from german77/discord

yuzu: Replace httplib with QtNetworkRequest
This commit is contained in:
liamwhite 2023-07-26 11:04:57 -04:00 committed by GitHub
commit a1c355051e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 36 deletions

View file

@ -3,9 +3,14 @@
#include <chrono> #include <chrono>
#include <string> #include <string>
#include <QEventLoop>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <discord_rpc.h> #include <discord_rpc.h>
#include <fmt/format.h> #include <fmt/format.h>
#include <httplib.h>
#include "common/common_types.h" #include "common/common_types.h"
#include "common/string_util.h" #include "common/string_util.h"
#include "core/core.h" #include "core/core.h"
@ -31,7 +36,7 @@ void DiscordImpl::Pause() {
Discord_ClearPresence(); Discord_ClearPresence();
} }
static std::string GetGameString(const std::string& title) { std::string DiscordImpl::GetGameString(const std::string& title) {
// Convert to lowercase // Convert to lowercase
std::string icon_name = Common::ToLower(title); std::string icon_name = Common::ToLower(title);
@ -56,51 +61,56 @@ static std::string GetGameString(const std::string& title) {
return icon_name; 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::seconds>( s64 start_time = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()) std::chrono::system_clock::now().time_since_epoch())
.count(); .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{}; 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()) { if (system.IsPoweredOn()) {
system.GetAppLoader().ReadTitle(title); system.GetAppLoader().ReadTitle(game_title);
// Used to format Icon URL for yuzu website game compatibility page // 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 QNetworkAccessManager manager;
httplib::Client cli(game_cover_url); QNetworkRequest request;
cli.set_connection_timeout(std::chrono::seconds(3)); request.setUrl(QUrl(QString::fromStdString(game_url)));
cli.set_read_timeout(std::chrono::seconds(3)); request.setTransferTimeout(3000);
QNetworkReply* reply = manager.head(request);
if (auto res = cli.Head(fmt::format("/images/game/boxart/{}.png", icon_name))) { QEventLoop request_event_loop;
if (res->status == 200) { QObject::connect(reply, &QNetworkReply::finished, &request_event_loop, &QEventLoop::quit);
game_cover_url += fmt::format("/images/game/boxart/{}.png", icon_name); request_event_loop.exec();
} else { UpdateGameStatus(reply->error());
game_cover_url = "yuzu_logo"; return;
}
} else {
game_cover_url = "yuzu_logo";
}
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";
} }
s64 start_time = std::chrono::duration_cast<std::chrono::seconds>(
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; presence.startTimestamp = start_time;
Discord_UpdatePresence(&presence); Discord_UpdatePresence(&presence);
} }

View file

@ -19,6 +19,13 @@ public:
void Pause() override; void Pause() override;
void Update() 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; Core::System& system;
}; };