discord: optimize RPC game image loading

- Remove blocking network timeout check that was causing 5-second delays
- Simplify logic to always attempt using Tinfoil game images
- Let Discord handle image loading and fallback gracefully
- Clean up URL formatting for better consistency
- Remove unnecessary network validation that caused false negatives

This fixes the issue where Discord RPC would fall back to default
Citron logo due to network timeouts, allowing game-specific artwork
to display properly from Tinfoil's CDN.

Signed-off-by: Zephyron <zephyron@citron-emu.org>
This commit is contained in:
Zephyron
2025-10-17 17:31:20 +10:00
parent 3f71089f6d
commit 6d53fec8db

View File

@@ -20,119 +20,112 @@
#include "citron/uisettings.h" #include "citron/uisettings.h"
static void OnDiscordReady(const DiscordUser* request) { static void OnDiscordReady(const DiscordUser* request) {
fmt::print("\n[DISCORD CALLBACK] SUCCESS: Connected to Discord as {}#{}\n\n", request->username, request->discriminator); fmt::print("\n[DISCORD CALLBACK] SUCCESS: Connected to Discord as {}#{}\n\n", request->username, request->discriminator);
} }
static void OnDiscordDisconnected(int errcode, const char* message) { static void OnDiscordDisconnected(int errcode, const char* message) {
fmt::print("\n[DISCORD CALLBACK] ERROR: Disconnected from Discord. Code: {}, Message: {}\n\n", errcode, message); fmt::print("\n[DISCORD CALLBACK] ERROR: Disconnected from Discord. Code: {}, Message: {}\n\n", errcode, message);
} }
static void OnDiscordError(int errcode, const char* message) { static void OnDiscordError(int errcode, const char* message) {
fmt::print("\n[DISCORD CALLBACK] ERROR: An error occurred. Code: {}, Message: {}\n\n", errcode, message); fmt::print("\n[DISCORD CALLBACK] ERROR: An error occurred. Code: {}, Message: {}\n\n", errcode, message);
} }
namespace DiscordRPC { namespace DiscordRPC {
DiscordImpl::DiscordImpl(Core::System& system_) : system{system_} { DiscordImpl::DiscordImpl(Core::System& system_) : system{system_} {
DiscordEventHandlers handlers{}; DiscordEventHandlers handlers{};
handlers.ready = OnDiscordReady; handlers.ready = OnDiscordReady;
handlers.disconnected = OnDiscordDisconnected; handlers.disconnected = OnDiscordDisconnected;
handlers.errored = OnDiscordError; handlers.errored = OnDiscordError;
Discord_Initialize("1361252452329848892", &handlers, 1, nullptr); Discord_Initialize("1361252452329848892", &handlers, 1, nullptr);
// Initialize the timer for the first state (being in the menu). // Initialize the timer for the first state (being in the menu).
current_state_start_time = std::chrono::duration_cast<std::chrono::seconds>( current_state_start_time = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count(); std::chrono::system_clock::now().time_since_epoch()).count();
was_powered_on = false; // Start in the "off" state. was_powered_on = false; // Start in the "off" state.
discord_thread_running = true; discord_thread_running = true;
discord_thread = std::thread(&DiscordImpl::ThreadRun, this); discord_thread = std::thread(&DiscordImpl::ThreadRun, this);
} }
DiscordImpl::~DiscordImpl() { DiscordImpl::~DiscordImpl() {
if (discord_thread_running) { if (discord_thread_running) {
discord_thread_running = false; discord_thread_running = false;
if (discord_thread.joinable()) { if (discord_thread.joinable()) {
discord_thread.join(); discord_thread.join();
} }
} }
Discord_ClearPresence(); Discord_ClearPresence();
Discord_Shutdown(); Discord_Shutdown();
} }
void DiscordImpl::Pause() { void DiscordImpl::Pause() {
Discord_ClearPresence(); Discord_ClearPresence();
} }
void DiscordImpl::UpdateGameStatus(bool use_default) { void DiscordImpl::UpdateGameStatus(bool use_default) {
const std::string default_text = "Citron Is A Homebrew Emulator For The Nintendo Switch"; const std::string default_text = "Citron Is A Homebrew Emulator For The Nintendo Switch";
const std::string default_image = "citron_logo"; const std::string default_image = "citron_logo";
DiscordRichPresence presence{}; DiscordRichPresence presence{};
if (!use_default && !game_title_id.empty()) { if (!use_default && !game_title_id.empty()) {
game_url = fmt::format("{}{}/256/256", "https://tinfoil.media/ti/", game_title_id); // Discord external image format
cached_url = game_url; // Note: Discord may require app permissions for external URLs
presence.largeImageKey = cached_url.c_str(); game_url = fmt::format("https://tinfoil.media/ti/{}/256/256", game_title_id);
} else { cached_url = game_url;
presence.largeImageKey = default_image.c_str(); presence.largeImageKey = cached_url.c_str();
} } else {
presence.largeImageKey = default_image.c_str();
}
presence.largeImageText = game_title.c_str(); presence.largeImageText = game_title.c_str();
presence.smallImageKey = default_image.c_str(); presence.smallImageKey = default_image.c_str();
presence.smallImageText = default_text.c_str(); presence.smallImageText = default_text.c_str();
presence.details = game_title.c_str(); presence.details = game_title.c_str();
presence.state = "Currently in game"; presence.state = "Currently in game";
presence.startTimestamp = current_state_start_time; // Use the state-based timer presence.startTimestamp = current_state_start_time;
Discord_UpdatePresence(&presence); Discord_UpdatePresence(&presence);
} }
void DiscordImpl::Update() { void DiscordImpl::Update() {
const bool is_powered_on = system.IsPoweredOn(); const bool is_powered_on = system.IsPoweredOn();
if (is_powered_on != was_powered_on) { if (is_powered_on != was_powered_on) {
// State changed! Reset the timer // State changed! Reset the timer
current_state_start_time = std::chrono::duration_cast<std::chrono::seconds>( current_state_start_time = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count(); std::chrono::system_clock::now().time_since_epoch()).count();
// Update the state tracker. // Update the state tracker.
was_powered_on = is_powered_on; was_powered_on = is_powered_on;
} }
if (is_powered_on) { if (is_powered_on) {
// Game is running. // Game is running.
system.GetAppLoader().ReadTitle(game_title); system.GetAppLoader().ReadTitle(game_title);
system.GetAppLoader().ReadProgramId(program_id); system.GetAppLoader().ReadProgramId(program_id);
game_title_id = fmt::format("{:016X}", program_id); game_title_id = fmt::format("{:016X}", program_id);
QNetworkAccessManager manager; // Always try to use the Tinfoil image - Discord will handle fallback
QNetworkRequest request; // Network check removed as it was causing unnecessary delays and false negatives
request.setUrl(QUrl(QString::fromStdString( UpdateGameStatus(false);
fmt::format("https://tinfoil.media/ti/{}/256/256", game_title_id)))); } else {
request.setTransferTimeout(5000); // Game is NOT running (in menus).
QNetworkReply* reply = manager.head(request); const std::string default_text = "Citron Is A Homebrew Emulator For The Nintendo Switch";
QEventLoop request_event_loop; const std::string default_image = "citron_logo";
QObject::connect(reply, &QNetworkReply::finished, &request_event_loop, &QEventLoop::quit); DiscordRichPresence presence{};
request_event_loop.exec(); presence.largeImageKey = default_image.c_str();
presence.largeImageText = default_text.c_str();
presence.details = "In the Menus";
presence.startTimestamp = current_state_start_time; // Use the state-based timer
Discord_UpdatePresence(&presence);
}
}
UpdateGameStatus(reply->error() != QNetworkReply::NoError); void DiscordImpl::ThreadRun() {
reply->deleteLater(); while (discord_thread_running) {
} else { Update();
// Game is NOT running (in menus). Discord_RunCallbacks();
const std::string default_text = "Citron Is A Homebrew Emulator For The Nintendo Switch"; std::this_thread::sleep_for(std::chrono::seconds(15));
const std::string default_image = "citron_logo"; }
DiscordRichPresence presence{}; }
presence.largeImageKey = default_image.c_str();
presence.largeImageText = default_text.c_str();
presence.details = "In the Menus";
presence.startTimestamp = current_state_start_time; // Use the state-based timer
Discord_UpdatePresence(&presence);
}
}
void DiscordImpl::ThreadRun() {
while (discord_thread_running) {
Update();
Discord_RunCallbacks();
std::this_thread::sleep_for(std::chrono::seconds(15));
}
}
} // namespace DiscordRPC } // namespace DiscordRPC