Files
eden/src/yuzu/discord_impl.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

122 lines
4.4 KiB
C++
Raw Normal View History

2022-05-15 02:06:02 +02:00
// SPDX-FileCopyrightText: 2018 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2018-09-16 20:05:51 +02:00
#include <chrono>
#include <string>
2023-07-21 13:08:46 -06:00
2023-07-25 21:33:46 -04:00
#include <QEventLoop>
2023-07-21 13:08:46 -06:00
#include <QNetworkAccessManager>
#include <QNetworkReply>
2023-07-25 21:33:46 -04:00
2018-09-16 20:05:51 +02:00
#include <discord_rpc.h>
2023-02-03 15:49:29 +11:00
#include <fmt/format.h>
2023-07-21 13:08:46 -06:00
2018-09-16 20:05:51 +02:00
#include "common/common_types.h"
2023-02-03 15:49:29 +11:00
#include "common/string_util.h"
2018-09-16 20:05:51 +02:00
#include "core/core.h"
#include "core/loader/loader.h"
#include "yuzu/discord_impl.h"
namespace DiscordRPC {
DiscordImpl::DiscordImpl(Core::System& system_) : system{system_} {
2018-09-16 20:05:51 +02:00
DiscordEventHandlers handlers{};
// The number is the client ID for yuzu, it's used for images and the
// application name
2025-07-22 14:42:33 -04:00
Discord_Initialize("1397286652128264252", &handlers, 1, nullptr);
2018-09-16 20:05:51 +02:00
}
DiscordImpl::~DiscordImpl() {
Discord_ClearPresence();
Discord_Shutdown();
}
void DiscordImpl::Pause() {
Discord_ClearPresence();
}
2023-07-21 13:08:46 -06:00
std::string DiscordImpl::GetGameString(const std::string& title) {
2023-02-03 15:49:29 +11:00
// Convert to lowercase
std::string icon_name = Common::ToLower(title);
// Replace spaces with dashes
std::replace(icon_name.begin(), icon_name.end(), ' ', '-');
// Remove non-alphanumeric characters but keep dashes
std::erase_if(icon_name, [](char c) { return !std::isalnum(c) && c != '-'; });
// Remove dashes from the start and end of the string
icon_name.erase(icon_name.begin(), std::find_if(icon_name.begin(), icon_name.end(),
[](int ch) { return ch != '-'; }));
icon_name.erase(
std::find_if(icon_name.rbegin(), icon_name.rend(), [](int ch) { return ch != '-'; }).base(),
icon_name.end());
// Remove double dashes
icon_name.erase(std::unique(icon_name.begin(), icon_name.end(),
[](char a, char b) { return a == '-' && b == '-'; }),
icon_name.end());
return icon_name;
}
2023-07-21 13:08:46 -06:00
void DiscordImpl::UpdateGameStatus(bool use_default) {
2025-05-11 23:58:25 +00:00
const std::string default_text = "eden is an emulator for the Nintendo Switch";
2025-07-22 14:42:33 -04:00
const std::string default_image = "https://git.eden-emu.dev/eden-emu/eden/raw/branch/master/"
2025-05-11 23:58:25 +00:00
"dist/qt_themes/default/icons/256x256/eden_named.png";
2023-07-21 13:08:46 -06:00
const std::string url = use_default ? default_image : game_url;
2018-09-16 20:05:51 +02:00
s64 start_time = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
2023-07-21 13:08:46 -06:00
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() {
2025-05-11 23:58:25 +00:00
const std::string default_text = "eden is an emulator for the Nintendo Switch";
const std::string default_image = "https://git.eden-emu.dev/eden-emu/eden/raw/branch/master/"
"dist/qt_themes/default/icons/256x256/eden_named.png";
2023-02-03 15:49:29 +11:00
if (system.IsPoweredOn()) {
2023-07-21 13:08:46 -06:00
system.GetAppLoader().ReadTitle(game_title);
2023-02-03 15:49:29 +11:00
// Used to format Icon URL for yuzu website game compatibility page
2023-07-21 13:08:46 -06:00
std::string icon_name = GetGameString(game_title);
2025-05-11 23:58:25 +00:00
game_url = fmt::format(
"https://raw.githubusercontent.com/eden-emulator/boxart/refs/heads/master/img/{}.png",
icon_name);
2023-07-21 13:08:46 -06:00
2023-07-25 21:33:46 -04:00
QNetworkAccessManager manager;
2023-07-21 13:08:46 -06:00
QNetworkRequest request;
request.setUrl(QUrl(QString::fromStdString(game_url)));
request.setTransferTimeout(3000);
2023-07-25 21:33:46 -04:00
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());
2025-05-11 23:58:25 +00:00
2023-07-21 13:08:46 -06:00
return;
2018-09-16 20:05:51 +02:00
}
2023-02-03 15:49:29 +11:00
2023-07-21 13:08:46 -06:00
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";
2018-09-16 20:05:51 +02:00
presence.startTimestamp = start_time;
Discord_UpdatePresence(&presence);
}
} // namespace DiscordRPC