From 5b486ca6b8f4df169d118005e1ed49189d4a00b9 Mon Sep 17 00:00:00 2001 From: Silent Date: Wed, 26 Jul 2023 23:41:08 +0200 Subject: [PATCH] Achievements: Log-in explicitly on startup to refresh the token --- pcsx2/Achievements.cpp | 56 ++++++++++++++++++++++++++++++------ pcsx2/Achievements.h | 2 ++ pcsx2/ImGui/FullscreenUI.cpp | 2 +- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/pcsx2/Achievements.cpp b/pcsx2/Achievements.cpp index f8731ff2b6..48a6d85bd7 100644 --- a/pcsx2/Achievements.cpp +++ b/pcsx2/Achievements.cpp @@ -106,7 +106,8 @@ namespace Achievements static void EndLoadingScreen(bool was_running_idle); static void LoginCallback(s32 status_code, const std::string& content_type, Common::HTTPDownloader::Request::Data data); static void LoginASyncCallback(s32 status_code, const std::string& content_type, Common::HTTPDownloader::Request::Data data); - static void SendLogin(const char* username, const char* password, Common::HTTPDownloader* http_downloader, + static void LoginWithTokenCallback(s32 status_code, const std::string& content_type, Common::HTTPDownloader::Request::Data data); + static void SendLogin(const char* username, const char* password, const char* api_token, Common::HTTPDownloader* http_downloader, Common::HTTPDownloader::Request::Callback callback); static void DownloadImage(std::string url, std::string cache_filename); static void DisplayAchievementSummary(); @@ -445,6 +446,11 @@ bool Achievements::IsLoggedIn() return s_logged_in; } +bool Achievements::HasSavedCredentials() +{ + return !s_username.empty() && !s_api_token.empty(); +} + bool Achievements::ChallengeModeActive() { #ifdef ENABLE_RAINTEGRATION @@ -513,7 +519,7 @@ void Achievements::Initialize() s_last_ping_time.Reset(); s_username = Host::GetBaseStringSettingValue("Achievements", "Username"); s_api_token = Host::GetBaseStringSettingValue("Achievements", "Token"); - s_logged_in = (!s_username.empty() && !s_api_token.empty()); + s_logged_in = false; // We are not logged in until we confirm those credentials if (VMManager::HasValidVM()) GameChanged(VMManager::GetDiscCRC(), VMManager::GetCurrentCRC()); @@ -953,13 +959,34 @@ void Achievements::LoginASyncCallback(s32 status_code, const std::string& conten LoginCallback(status_code, std::move(content_type), std::move(data)); } +void Achievements::LoginWithTokenCallback(s32 status_code, const std::string& content_type, Common::HTTPDownloader::Request::Data data) +{ + std::unique_lock lock(s_achievements_mutex); + + RAPIResponse response(status_code, data); + if (!response) + { + FormattedError("Login failed. Please check your user name and password, and try again."); + return; + } + + if (s_active) + { + s_logged_in = true; + + // If we have a game running, set it up. + if (!s_game_hash.empty()) + SendGetGameId(); + } +} + void Achievements::SendLogin( - const char* username, const char* password, Common::HTTPDownloader* http_downloader, Common::HTTPDownloader::Request::Callback callback) + const char* username, const char* password, const char* api_token, Common::HTTPDownloader* http_downloader, Common::HTTPDownloader::Request::Callback callback) { RAPIRequest request; request.username = username; request.password = password; - request.api_token = nullptr; + request.api_token = api_token; request.Send(http_downloader, std::move(callback)); } @@ -975,7 +1002,7 @@ bool Achievements::LoginAsync(const char* username, const char* password) ImGuiFullscreen::OpenBackgroundProgressDialog("cheevos_async_login", "Logging in to RetroAchievements...", 0, 0, 0); } - SendLogin(username, password, s_http_downloader.get(), LoginASyncCallback); + SendLogin(username, password, nullptr, s_http_downloader.get(), LoginASyncCallback); return true; } @@ -989,7 +1016,7 @@ bool Achievements::Login(const char* username, const char* password) if (s_active) { - SendLogin(username, password, s_http_downloader.get(), LoginCallback); + SendLogin(username, password, nullptr, s_http_downloader.get(), LoginCallback); s_http_downloader->WaitForAllRequests(); return IsLoggedIn(); } @@ -1000,12 +1027,21 @@ bool Achievements::Login(const char* username, const char* password) if (!http_downloader) return false; - SendLogin(username, password, http_downloader.get(), LoginCallback); + SendLogin(username, password, nullptr, http_downloader.get(), LoginCallback); http_downloader->WaitForAllRequests(); return !Host::GetBaseStringSettingValue("Achievements", "Token").empty(); } +bool Achievements::LoginWithTokenAsync(const char* username, const char* api_token) +{ + if (s_logged_in || std::strlen(username) == 0 || std::strlen(api_token) == 0 || IsUsingRAIntegration()) + return false; + + SendLogin(username, nullptr, api_token, s_http_downloader.get(), LoginWithTokenCallback); + return true; +} + void Achievements::Logout() { if (s_active) @@ -1015,11 +1051,11 @@ void Achievements::Logout() if (s_logged_in) { ClearGameInfo(); - std::string().swap(s_username); - std::string().swap(s_api_token); s_logged_in = false; Host::OnAchievementsRefreshed(); } + std::string().swap(s_username); + std::string().swap(s_api_token); } // remove from config @@ -1477,6 +1513,8 @@ void Achievements::GameChanged(u32 disc_crc, u32 crc) if (IsLoggedIn()) SendGetGameId(); + else if (HasSavedCredentials()) + LoginWithTokenAsync(s_username.c_str(), s_api_token.c_str()); } void Achievements::SendGetGameId() diff --git a/pcsx2/Achievements.h b/pcsx2/Achievements.h index 7c60fa8804..c404e63338 100644 --- a/pcsx2/Achievements.h +++ b/pcsx2/Achievements.h @@ -88,6 +88,7 @@ namespace Achievements bool IsActive(); bool IsLoggedIn(); + bool HasSavedCredentials(); bool ChallengeModeActive(); bool LeaderboardsActive(); bool IsTestModeActive(); @@ -131,6 +132,7 @@ namespace Achievements bool LoginAsync(const char* username, const char* password); bool Login(const char* username, const char* password); + bool LoginWithTokenAsync(const char* username, const char* api_token); void Logout(); void GameChanged(u32 disc_crc, u32 crc); diff --git a/pcsx2/ImGui/FullscreenUI.cpp b/pcsx2/ImGui/FullscreenUI.cpp index e7a5889089..87319da865 100644 --- a/pcsx2/ImGui/FullscreenUI.cpp +++ b/pcsx2/ImGui/FullscreenUI.cpp @@ -6990,7 +6990,7 @@ void FullscreenUI::DrawAchievementsSettingsPage(std::unique_lock& se Achievements::ProcessPendingHTTPRequestsFromGSThread(); MenuHeading("Account"); - if (Achievements::IsLoggedIn()) + if (Achievements::HasSavedCredentials()) { ImGui::PushStyleColor(ImGuiCol_TextDisabled, ImGui::GetStyle().Colors[ImGuiCol_Text]); ActiveButton(fmt::format(ICON_FA_USER " Username: {}", Achievements::GetUsername()).c_str(), false, false,