diff --git a/Common/GPU/Vulkan/VulkanMemory.h b/Common/GPU/Vulkan/VulkanMemory.h index 9f51990691..f218de3605 100644 --- a/Common/GPU/Vulkan/VulkanMemory.h +++ b/Common/GPU/Vulkan/VulkanMemory.h @@ -5,6 +5,7 @@ #include #include +#include "Common/Data/Collections/FastVec.h" #include "Common/GPU/Vulkan/VulkanContext.h" #include "Common/GPU/GPUBackendCommon.h" @@ -205,4 +206,3 @@ private: uint32_t usage_ = 0; bool grow_; }; - diff --git a/Common/Math/geom2d.h b/Common/Math/geom2d.h index 3d2223c36e..f319d7c79d 100644 --- a/Common/Math/geom2d.h +++ b/Common/Math/geom2d.h @@ -69,6 +69,9 @@ struct Bounds { Bounds Expand(float xAmount, float yAmount) const { return Bounds(x - xAmount, y - yAmount, w + xAmount * 2, h + yAmount * 2); } + Bounds Expand(float left, float top, float right, float bottom) const { + return Bounds(x - left, y - top, w + left + right, h + top + bottom); + } Bounds Offset(float xAmount, float yAmount) const { return Bounds(x + xAmount, y + yAmount, w, h); } diff --git a/Common/Serialize/Serializer.h b/Common/Serialize/Serializer.h index a7fa964a0c..808967b518 100644 --- a/Common/Serialize/Serializer.h +++ b/Common/Serialize/Serializer.h @@ -147,6 +147,11 @@ public: void DoMarker(const char *prevName, u32 arbitraryNumber = 0x42); + void SkipBytes(size_t bytes) { + // Should work in all modes. + *ptr += bytes; + } + size_t Offset() const { return *ptr - ptrStart_; } private: diff --git a/Common/System/OSD.cpp b/Common/System/OSD.cpp index fe7c8b7380..8013463e95 100644 --- a/Common/System/OSD.cpp +++ b/Common/System/OSD.cpp @@ -2,6 +2,7 @@ #include "Common/System/OSD.h" #include "Common/TimeUtil.h" +#include "Common/Log.h" OnScreenDisplay g_OSD; @@ -17,6 +18,14 @@ void OnScreenDisplay::Update() { } } + for (auto iter = sideEntries_.begin(); iter != sideEntries_.end(); ) { + if (now >= iter->endTime) { + iter = sideEntries_.erase(iter); + } else { + iter++; + } + } + for (auto iter = bars_.begin(); iter != bars_.end(); ) { if (now >= iter->endTime) { iter = bars_.erase(iter); @@ -31,6 +40,11 @@ std::vector OnScreenDisplay::Entries() { return entries_; // makes a copy. } +std::vector OnScreenDisplay::SideEntries() { + std::lock_guard guard(mutex_); + return sideEntries_; // makes a copy. +} + std::vector OnScreenDisplay::ProgressBars() { std::lock_guard guard(mutex_); return bars_; // makes a copy. @@ -99,6 +113,88 @@ void OnScreenDisplay::ShowAchievementUnlocked(int achievementID) { entries_.insert(entries_.begin(), msg); } +void OnScreenDisplay::ShowAchievementProgress(int achievementID, float duration_s) { + double now = time_now_d(); + + for (auto &entry : sideEntries_) { + if (entry.numericID == achievementID && entry.type == OSDType::ACHIEVEMENT_PROGRESS) { + // Duplicate, let's just bump the timer. + entry.startTime = now; + entry.endTime = now + (double)duration_s; + // We're done. + return; + } + } + + // OK, let's make a new side-entry. + Entry entry; + entry.numericID = achievementID; + entry.type = OSDType::ACHIEVEMENT_PROGRESS; + entry.startTime = now; + entry.endTime = now + (double)duration_s; + sideEntries_.insert(sideEntries_.begin(), entry); +} + +void OnScreenDisplay::ShowChallengeIndicator(int achievementID, bool show) { + double now = time_now_d(); + + for (auto &entry : sideEntries_) { + if (entry.numericID == achievementID && entry.type == OSDType::ACHIEVEMENT_CHALLENGE_INDICATOR && !show) { + // Hide and eventually delete it. + entry.endTime = now + (double)FadeoutTime(); + // Found it, we're done. + return; + } + } + + if (!show) { + // Sanity check + return; + } + + // OK, let's make a new side-entry. + Entry entry; + entry.numericID = achievementID; + entry.type = OSDType::ACHIEVEMENT_CHALLENGE_INDICATOR; + entry.startTime = now; + entry.endTime = now + 10000000.0; // Don't auto-fadeout. + sideEntries_.insert(sideEntries_.begin(), entry); +} + +void OnScreenDisplay::ShowLeaderboardTracker(int leaderboardTrackerID, const char *trackerText, bool show) { // show=true is used both for create and update. + double now = time_now_d(); + + for (auto &entry : sideEntries_) { + if (entry.numericID == leaderboardTrackerID && entry.type == OSDType::LEADERBOARD_TRACKER) { + if (show) { + // Just an update. + entry.text = trackerText; + } else { + // Keep the current text, hide and eventually delete it. + entry.endTime = now + (double)FadeoutTime(); + } + // Found it, we're done. + return; + } + } + + if (!show) { + // Sanity check + return; + } + + // OK, let's make a new side-entry. + Entry entry; + entry.numericID = leaderboardTrackerID; + entry.type = OSDType::LEADERBOARD_TRACKER; + entry.startTime = now; + entry.endTime = now + 10000000.0; // Don't auto-fadeout + if (trackerText) { + entry.text = trackerText; + } + sideEntries_.insert(sideEntries_.begin(), entry); +} + void OnScreenDisplay::ShowOnOff(const std::string &message, bool on, float duration_s) { // TODO: translate "on" and "off"? Or just get rid of this whole thing? Show(OSDType::MESSAGE_INFO, message + ": " + (on ? "on" : "off"), duration_s); @@ -129,12 +225,12 @@ void OnScreenDisplay::SetProgressBar(std::string id, std::string &&message, int bars_.push_back(bar); } -void OnScreenDisplay::RemoveProgressBar(std::string id, float fadeout_s) { +void OnScreenDisplay::RemoveProgressBar(std::string id) { std::lock_guard guard(mutex_); for (auto iter = bars_.begin(); iter != bars_.end(); iter++) { if (iter->id == id) { iter->progress = iter->maxValue; - iter->endTime = time_now_d() + (double)fadeout_s; + iter->endTime = time_now_d() + FadeoutTime(); break; } } diff --git a/Common/System/OSD.h b/Common/System/OSD.h index 6816e4425e..20819da5d4 100644 --- a/Common/System/OSD.h +++ b/Common/System/OSD.h @@ -16,8 +16,10 @@ enum class OSDType { ACHIEVEMENT_UNLOCKED, - // PROGRESS_BAR, - // PROGRESS_INDETERMINATE, + // Side entries + ACHIEVEMENT_PROGRESS, // Achievement icon + "measured_progress" text, auto-hide after 2s + ACHIEVEMENT_CHALLENGE_INDICATOR, // Achievement icon ONLY, no auto-hide + LEADERBOARD_TRACKER, }; // Data holder for on-screen messages. @@ -31,7 +33,6 @@ public: Show(type, text, text2, "", duration_s, id); } void Show(OSDType type, const std::string &text, const std::string &text2, const std::string &icon, float duration_s = 0.0f, const char *id = nullptr); - void ShowAchievementUnlocked(int achievementID); void ShowOnOff(const std::string &message, bool on, float duration_s = 0.0f); @@ -40,10 +41,17 @@ public: // Call this every frame, cleans up old entries. void Update(); + // Specialized achievement-related types. These go to the side notifications, not the top-middle. + void ShowAchievementUnlocked(int achievementID); + void ShowAchievementProgress(int achievementID, float duration_s); + void ShowChallengeIndicator(int achievementID, bool show); // call with show=false to hide. + void ShowLeaderboardTracker(int leaderboardTrackerID, const char *trackerText, bool show); // show=true is used both for create and update. + // Progress bar controls - // Set is both create and update. + // Set is both create and update. If you set maxValue <= minValue, you'll create an "indeterminate" progress + // bar that doesn't show a specific amount of progress. void SetProgressBar(std::string id, std::string &&message, int minValue, int maxValue, int progress); - void RemoveProgressBar(std::string id, float fadeout_s); + void RemoveProgressBar(std::string id); struct Entry { OSDType type; @@ -66,10 +74,14 @@ public: }; std::vector Entries(); + std::vector SideEntries(); std::vector ProgressBars(); + static float FadeoutTime() { return 0.25f; } + private: std::vector entries_; + std::vector sideEntries_; std::vector bars_; std::mutex mutex_; }; diff --git a/Common/UI/Context.cpp b/Common/UI/Context.cpp index cb62571bf0..6e4e277f07 100644 --- a/Common/UI/Context.cpp +++ b/Common/UI/Context.cpp @@ -313,6 +313,17 @@ void UIContext::FillRect(const UI::Drawable &drawable, const Bounds &bounds) { } } +void UIContext::DrawRectDropShadow(const Bounds &bounds, float radius, float alpha, uint32_t color) { + if (alpha <= 0.0f) + return; + + color = colorAlpha(color, alpha); + + // Bias the shadow downwards a bit. + Bounds shadowBounds = bounds.Expand(radius, 0.5 * radius, radius, 1.5 * radius); + Draw()->DrawImage4Grid(theme->dropShadow4Grid, shadowBounds.x, shadowBounds.y, shadowBounds.x2(), shadowBounds.y2(), color, radius * (1.0f / 24.0f) * 2.0f); +} + void UIContext::DrawImageVGradient(ImageID image, uint32_t color1, uint32_t color2, const Bounds &bounds) { uidrawbuffer_->DrawImageStretchVGradient(image, bounds.x, bounds.y, bounds.x2(), bounds.y2(), color1, color2); } diff --git a/Common/UI/Context.h b/Common/UI/Context.h index 1e2767d181..f8c409c287 100644 --- a/Common/UI/Context.h +++ b/Common/UI/Context.h @@ -77,6 +77,8 @@ public: void SetTintSaturation(float tint, float sat); + // High level drawing functions. They generally assume the default texture to be bounds. + void SetFontStyle(const UI::FontStyle &style); const UI::FontStyle &GetFontStyle() { return *fontStyle_; } void SetFontScale(float scaleX, float scaleY); @@ -88,6 +90,7 @@ public: void DrawTextRect(const char *str, const Bounds &bounds, uint32_t color, int align = 0); void DrawTextShadowRect(const char *str, const Bounds &bounds, uint32_t color, int align = 0); void FillRect(const UI::Drawable &drawable, const Bounds &bounds); + void DrawRectDropShadow(const Bounds &bounds, float radius, float alpha, uint32_t color = 0); void DrawImageVGradient(ImageID image, uint32_t color1, uint32_t color2, const Bounds &bounds); // in dps, like dp_xres and dp_yres diff --git a/Core/Config.cpp b/Core/Config.cpp index a19c162677..d2b6835d11 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -266,20 +266,17 @@ static bool DefaultSasThread() { static const ConfigSetting achievementSettings[] = { ConfigSetting("AchievementsEnable", &g_Config.bAchievementsEnable, true, CfgFlag::DEFAULT), - ConfigSetting("AchievementsLeaderboards", &g_Config.bAchievementsLeaderboards, false, CfgFlag::DEFAULT), - ConfigSetting("AchievementsTestMode", &g_Config.bAchievementsTestMode, false, CfgFlag::DEFAULT), - ConfigSetting("AchievementsUnofficialTestMode", &g_Config.bAchievementsUnofficialTestMode, false, CfgFlag::DEFAULT), - ConfigSetting("AchievementsRichPresence", &g_Config.bAchievementsRichPresence, true, CfgFlag::DEFAULT), ConfigSetting("AchievementsChallengeMode", &g_Config.bAchievementsChallengeMode, false, CfgFlag::DEFAULT), + ConfigSetting("AchievementsEncoreMode", &g_Config.bAchievementsEncoreMode, false, CfgFlag::DEFAULT), + ConfigSetting("AchievementsUnofficial", &g_Config.bAchievementsUnofficial, false, CfgFlag::DEFAULT), ConfigSetting("AchievementsSoundEffects", &g_Config.bAchievementsSoundEffects, true, CfgFlag::DEFAULT), - ConfigSetting("AchievementsNotifications", &g_Config.bAchievementsNotifications, true, CfgFlag::DEFAULT), ConfigSetting("AchievementsLogBadMemReads", &g_Config.bAchievementsLogBadMemReads, false, CfgFlag::DEFAULT), // Achievements login info. Note that password is NOT stored, only a login token. - // And that login token is stored separately from the ini, see NativeSaveSecret. - ConfigSetting("AchievementsUserName", &g_Config.sAchievementsUserName, "", CfgFlag::DEFAULT), + // And that login token is stored separately from the ini, see NativeSaveSecret, but it can also be loaded + // from the ini if manually entered (useful when testing various builds on Android). ConfigSetting("AchievementsToken", &g_Config.sAchievementsToken, "", CfgFlag::DONT_SAVE), - ConfigSetting("AchievementsLoginTimestamp", &g_Config.sAchievementsLoginTimestamp, "", CfgFlag::DEFAULT), + ConfigSetting("AchievementsUserName", &g_Config.sAchievementsUserName, "", CfgFlag::DEFAULT), }; static const ConfigSetting cpuSettings[] = { diff --git a/Core/Config.h b/Core/Config.h index a1b17aaa6c..3f01bb68c5 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -488,20 +488,16 @@ public: // Retro Achievement settings // Copied from Duckstation, we might want to remove some. bool bAchievementsEnable; - bool bAchievementsLeaderboards; - bool bAchievementsTestMode; - bool bAchievementsUnofficialTestMode; - bool bAchievementsRichPresence; bool bAchievementsChallengeMode; + bool bAchievementsEncoreMode; + bool bAchievementsUnofficial; bool bAchievementsSoundEffects; - bool bAchievementsNotifications; bool bAchievementsLogBadMemReads; // Achivements login info. Note that password is NOT stored, only a login token. // Still, we may wanna store it more securely than in PPSSPP.ini, especially on Android. std::string sAchievementsUserName; std::string sAchievementsToken; // Not saved, to be used if you want to manually make your RA login persistent. See Native_SaveSecret for the normal case. - std::string sAchievementsLoginTimestamp; // Various directories. Autoconfigured, not read from ini. Path currentDirectory; // The directory selected in the game browsing window. diff --git a/Core/CwCheat.cpp b/Core/CwCheat.cpp index 59d0143d8a..857f50cc69 100644 --- a/Core/CwCheat.cpp +++ b/Core/CwCheat.cpp @@ -19,6 +19,7 @@ #include "Core/System.h" #include "Core/HLE/sceCtrl.h" #include "Core/MIPS/JitCommon/JitCommon.h" +#include "Core/RetroAchievements.h" #include "GPU/Common/PostShader.h" #ifdef _WIN32 @@ -319,9 +320,11 @@ void __CheatDoState(PointerWrap &p) { } void hleCheat(u64 userdata, int cyclesLate) { - if (cheatsEnabled != g_Config.bEnableCheats) { + bool shouldBeEnabled = !Achievements::ChallengeModeActive() && g_Config.bEnableCheats; + + if (cheatsEnabled != shouldBeEnabled) { // Okay, let's move to the desired state, then. - if (g_Config.bEnableCheats) { + if (shouldBeEnabled) { __CheatStart(); } else { __CheatStop(); @@ -1193,6 +1196,10 @@ void CWCheatEngine::ExecuteOp(const CheatOperation &op, const CheatCode &cheat, } void CWCheatEngine::Run() { + if (Achievements::ChallengeModeActive()) { + return; + } + for (CheatCode cheat : cheats_) { // InterpretNextOp and ExecuteOp move i. for (size_t i = 0; i < cheat.lines.size(); ) { @@ -1207,8 +1214,7 @@ bool CWCheatEngine::HasCheats() { } bool CheatsInEffect() { - if (!cheatEngine || !cheatsEnabled) + if (!cheatEngine || !cheatsEnabled || Achievements::ChallengeModeActive()) return false; return cheatEngine->HasCheats(); } - diff --git a/Core/HLE/sceDisplay.cpp b/Core/HLE/sceDisplay.cpp index b5039cabd3..13e2c9310b 100644 --- a/Core/HLE/sceDisplay.cpp +++ b/Core/HLE/sceDisplay.cpp @@ -49,6 +49,7 @@ #include "Core/HLE/sceKernelInterrupt.h" #include "Core/HW/Display.h" #include "Core/Util/PPGeDraw.h" +#include "Core/RetroAchievements.h" #include "GPU/GPU.h" #include "GPU/GPUState.h" @@ -348,12 +349,15 @@ void __DisplaySetWasPaused() { } static int FrameTimingLimit() { - if (PSP_CoreParameter().fpsLimit == FPSLimit::CUSTOM1) - return g_Config.iFpsLimit1; - if (PSP_CoreParameter().fpsLimit == FPSLimit::CUSTOM2) - return g_Config.iFpsLimit2; - if (PSP_CoreParameter().fpsLimit == FPSLimit::ANALOG) - return PSP_CoreParameter().analogFpsLimit; + if (!Achievements::ChallengeModeActive()) { + if (PSP_CoreParameter().fpsLimit == FPSLimit::CUSTOM1) + return g_Config.iFpsLimit1; + if (PSP_CoreParameter().fpsLimit == FPSLimit::CUSTOM2) + return g_Config.iFpsLimit2; + if (PSP_CoreParameter().fpsLimit == FPSLimit::ANALOG) + return PSP_CoreParameter().analogFpsLimit; + } + // Note: Fast-forward is OK in challenge mode. if (PSP_CoreParameter().fastForward) return 0; return framerate; diff --git a/Core/HLE/sceKernel.cpp b/Core/HLE/sceKernel.cpp index 5ff26c0461..60317157d9 100644 --- a/Core/HLE/sceKernel.cpp +++ b/Core/HLE/sceKernel.cpp @@ -85,8 +85,7 @@ #include "sceDmac.h" #include "sceMp4.h" #include "sceOpenPSID.h" - -#include "../Util/PPGeDraw.h" +#include "Core/Util/PPGeDraw.h" /* 17: [MIPS32 R4K 00000000 ]: Loader: Type: 1 Vaddr: 00000000 Filesz: 2856816 Memsz: 2856816 diff --git a/Core/HLE/sceUmd.cpp b/Core/HLE/sceUmd.cpp index b85f962a03..5f0d8c964d 100644 --- a/Core/HLE/sceUmd.cpp +++ b/Core/HLE/sceUmd.cpp @@ -34,6 +34,7 @@ #include "Core/HLE/sceKernelInterrupt.h" #include "Core/HLE/sceKernelMemory.h" #include "Core/HLE/KernelWaitHelpers.h" +#include "Core/RetroAchievements.h" #include "Core/FileSystems/BlockDevices.h" #include "Core/FileSystems/MetaFileSystem.h" @@ -483,13 +484,15 @@ static u32 sceUmdGetErrorStat() return umdErrorStat; } -void __UmdReplace(Path filepath) { +void __UmdReplace(const Path &filepath) { std::string error = ""; if (!UmdReplace(filepath, error)) { ERROR_LOG(SCEIO, "UMD Replace failed: %s", error.c_str()); return; } + Achievements::ChangeUMD(filepath); + UMDInserted = false; // Wake any threads waiting for the disc to be removed. UmdWakeThreads(); diff --git a/Core/HLE/sceUmd.h b/Core/HLE/sceUmd.h index 91e9bcafd6..e33bdaae96 100644 --- a/Core/HLE/sceUmd.h +++ b/Core/HLE/sceUmd.h @@ -43,7 +43,7 @@ enum pspUmdType { void __UmdInit(); void __UmdDoState(PointerWrap &p); -void __UmdReplace(Path filepath); +void __UmdReplace(const Path &filepath); bool getUMDReplacePermit(); void Register_sceUmdUser(); diff --git a/Core/RetroAchievements.cpp b/Core/RetroAchievements.cpp index 492fbc7b6c..0d3a8aeed3 100644 --- a/Core/RetroAchievements.cpp +++ b/Core/RetroAchievements.cpp @@ -15,6 +15,7 @@ #include #include "ext/rcheevos/include/rcheevos.h" +#include "ext/rcheevos/include/rc_client.h" #include "ext/rcheevos/include/rc_api_user.h" #include "ext/rcheevos/include/rc_api_info.h" #include "ext/rcheevos/include/rc_api_request.h" @@ -36,6 +37,7 @@ #include "Common/TimeUtil.h" #include "Common/Data/Text/I18n.h" #include "Common/Serialize/Serializer.h" +#include "Common/Serialize/SerializeFuncs.h" #include "Common/StringUtils.h" #include "Common/Crypto/md5.h" #include "Common/UI/IconCache.h" @@ -48,68 +50,6 @@ #include "Core/FileSystems/MetaFileSystem.h" #include "Core/RetroAchievements.h" -// Simply wrap our current HTTP backend to fit the DuckStation-derived code. -namespace Common { - class HTTPDownloader { - public: - static std::unique_ptr Create() { - return std::unique_ptr(new HTTPDownloader()); - } - class Request { - public: - typedef std::string Data; - typedef std::function Callback; - }; - - void PollRequests() { - downloader_.Update(); - } - void WaitForAllRequests() { - downloader_.WaitForAll(); - } - bool HasAnyRequests() const { - return downloader_.GetActiveCount() > 0; - } - void CreateRequest(std::string &&url, Request::Callback &&callback) { - Request::Callback movedCallback = std::move(callback); - downloader_.StartDownloadWithCallback(url, Path(), [=](http::Download &download) { - std::string data; - download.buffer().TakeAll(&data); - movedCallback(download.ResultCode(), "", data); - }); - } - void CreatePostRequest(std::string &&url, const char *post_data, Request::Callback &&callback) { - Request::Callback movedCallback = std::move(callback); - std::string post_data_str(post_data); - - INFO_LOG(ACHIEVEMENTS, "Request: post_data=%s", post_data); - - downloader_.AsyncPostWithCallback(url, post_data_str, "application/x-www-form-urlencoded", [=](http::Download &download) { - std::string data; - download.buffer().TakeAll(&data); - movedCallback(download.ResultCode(), "", data); - }); - } - - private: - HTTPDownloader() {} - - http::Downloader downloader_; - }; -} // namespace - -void OSDAddToast(float duration_s, const std::string &text) { - g_OSD.Show(OSDType::MESSAGE_INFO, text); -} - -void OSDAddNotification(float duration_s, const std::string &title, const std::string &summary, const std::string &iconImageData) { - g_OSD.Show(OSDType::MESSAGE_INFO, title, summary, iconImageData, 5.0f); -} - -void OSDAddAchievementUnlockedNotification(unsigned int achievementId) { - g_OSD.ShowAchievementUnlocked(achievementId); -} - void OSDOpenBackgroundProgressDialog(const char *str_id, std::string message, s32 min, s32 max, s32 value) { NOTICE_LOG(ACHIEVEMENTS, "Progress dialog opened: %s %s", str_id, message.c_str()); g_OSD.SetProgressBar(str_id, std::move(message), min, max, value); @@ -122,1819 +62,100 @@ void OSDUpdateBackgroundProgressDialog(const char *str_id, std::string message, void OSDCloseBackgroundProgressDialog(const char *str_id) { NOTICE_LOG(ACHIEVEMENTS, "Progress dialog closed: %s", str_id); - g_OSD.RemoveProgressBar(str_id, 0.25f); + g_OSD.RemoveProgressBar(str_id); } -void OSDAddErrorMessage(const char *str_id, std::string message, float duration) { - g_OSD.Show(OSDType::MESSAGE_ERROR, message); - NOTICE_LOG(ACHIEVEMENTS, "Keyed message: %s %s (%0.1f s)", str_id, message.c_str(), duration); -} - -namespace Host { -void OnAchievementsRefreshed() { - System_PostUIMessage("achievements_refreshed", ""); -} void OnAchievementsLoginStateChange() { System_PostUIMessage("achievements_loginstatechange", ""); } -} namespace Achievements { -enum : s32 -{ - HTTP_OK = 200, - - // Number of seconds between rich presence pings. RAIntegration uses 2 minutes. - RICH_PRESENCE_PING_FREQUENCY = 2 * 60, - NO_RICH_PRESENCE_PING_FREQUENCY = RICH_PRESENCE_PING_FREQUENCY * 2, -}; - // It's the name of the secret, not a secret name - the value is not secret :) static const char *RA_TOKEN_SECRET_NAME = "retroachievements"; -static void FormattedError(const char *format, ...); -static void LogFailedResponseJSON(const Common::HTTPDownloader::Request::Data &data); -static void CheevosEventHandler(const rc_runtime_event_t *runtime_event); -static unsigned PeekMemory(unsigned address, unsigned num_bytes, void *ud); -static bool IsMastered(); -static void ActivateLockedAchievements(); -static bool ActivateAchievement(Achievement *achievement); -static void DeactivateAchievement(Achievement *achievement); -static void UnlockAchievement(u32 achievement_id, bool add_notification = true); -static void AchievementPrimed(u32 achievement_id); -static void AchievementUnprimed(u32 achievement_id); -static void AchievementDisabled(u32 achievement_id); -static void SubmitLeaderboard(u32 leaderboard_id, int value); -static void SendPing(); -static void SendPlaying(); -static void UpdateRichPresence(); -static Achievement *GetMutableAchievementByID(u32 id); -static void ClearGameInfo(bool clear_achievements = true, bool clear_leaderboards = true); -static void ClearGameHash(); -static std::string GetUserAgent(); -static void LoginCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data); -static void LoginASyncCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data); -static void SendLogin(const char *username, const char *password, Common::HTTPDownloader *http_downloader, - Common::HTTPDownloader::Request::Callback callback); -static void DownloadImage(std::string url, std::string cache_filename); -static void DisplayAchievementSummary(); -static void DisplayMasteredNotification(); -static void GetUserUnlocksCallback(s32 status_code, std::string content_type, - Common::HTTPDownloader::Request::Data data); -static void GetUserUnlocks(); -static void GetPatchesCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data); -static void GetLbInfoCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data); -static void GetPatches(u32 game_id); -static std::string GetGameHash(const Path &path); -static void SetChallengeMode(bool enabled); -static void SendGetGameId(); -static void GetGameIdCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data); -static void SendPlayingCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data); -static void UpdateRichPresence(); -static void SendPingCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data); -static void UnlockAchievementCallback(s32 status_code, std::string content_type, - Common::HTTPDownloader::Request::Data data); -static void SubmitLeaderboardCallback(s32 status_code, std::string content_type, - Common::HTTPDownloader::Request::Data data); -static void ResetRuntime(); - -static bool s_active = false; -static bool s_logged_in = false; -static bool s_challenge_mode = false; -static u32 s_game_id = 0; - -static std::recursive_mutex s_achievements_mutex; -static rc_runtime_t s_rcheevos_runtime; -static std::unique_ptr s_http_downloader; - -static std::string s_username; -static std::string s_api_token; - -static Path s_game_path; -static std::string s_game_hash; -static std::string s_game_title; -static std::string s_game_icon; -static std::vector s_achievements; -static std::vector s_leaderboards; -static std::atomic s_primed_achievement_count{0}; - -static bool s_has_rich_presence = false; -static std::string s_rich_presence_string; -static double s_last_ping_time; - -static u32 s_last_queried_lboard = 0; -static u32 s_submitting_lboard_id = 0; -static std::optional> s_lboard_entries; - static Achievements::Statistics g_stats; const std::string g_gameIconCachePrefix = "game:"; const std::string g_iconCachePrefix = "badge:"; +Path s_game_path; +std::string s_game_hash; +bool g_isIdentifying = false; + +// rc_client implementation +static rc_client_t *g_rcClient; #define PSP_MEMORY_OFFSET 0x08000000 -// TODO: Add an icon cache as a string map. We won't cache achievement icons across sessions, let's just -// download them as we go. - -template -static const char *RAPIStructName(); - -#define RAPI_STRUCT_NAME(x) \ - template<> \ - const char* RAPIStructName() \ - { \ - return #x; \ - } - -RAPI_STRUCT_NAME(rc_api_login_request_t); -RAPI_STRUCT_NAME(rc_api_fetch_image_request_t); -RAPI_STRUCT_NAME(rc_api_resolve_hash_request_t); -RAPI_STRUCT_NAME(rc_api_fetch_game_data_request_t); -RAPI_STRUCT_NAME(rc_api_fetch_user_unlocks_request_t); -RAPI_STRUCT_NAME(rc_api_start_session_request_t); -RAPI_STRUCT_NAME(rc_api_ping_request_t); -RAPI_STRUCT_NAME(rc_api_award_achievement_request_t); -RAPI_STRUCT_NAME(rc_api_submit_lboard_entry_request_t); -RAPI_STRUCT_NAME(rc_api_fetch_leaderboard_info_request_t); - -RAPI_STRUCT_NAME(rc_api_login_response_t); -RAPI_STRUCT_NAME(rc_api_resolve_hash_response_t); -RAPI_STRUCT_NAME(rc_api_fetch_game_data_response_t); -RAPI_STRUCT_NAME(rc_api_ping_response_t); -RAPI_STRUCT_NAME(rc_api_award_achievement_response_t); -RAPI_STRUCT_NAME(rc_api_submit_lboard_entry_response_t); -RAPI_STRUCT_NAME(rc_api_start_session_response_t); -RAPI_STRUCT_NAME(rc_api_fetch_user_unlocks_response_t); -RAPI_STRUCT_NAME(rc_api_fetch_leaderboard_info_response_t); - -// Unused for now. -// RAPI_STRUCT_NAME(rc_api_fetch_achievement_info_response_t); -// RAPI_STRUCT_NAME(rc_api_fetch_games_list_response_t); - -#undef RAPI_STRUCT_NAME - -template -struct RAPIRequest : public T -{ -private: - rc_api_request_t api_request; - -public: - RAPIRequest() { std::memset(this, 0, sizeof(*this)); } - - ~RAPIRequest() { rc_api_destroy_request(&api_request); } - - void Send(Common::HTTPDownloader::Request::Callback callback) { Send(s_http_downloader.get(), std::move(callback)); } - - void Send(Common::HTTPDownloader *http_downloader, Common::HTTPDownloader::Request::Callback callback) - { - const int error = InitFunc(&api_request, this); - if (error != RC_OK) - { - FormattedError("%s failed: error %d (%s)", RAPIStructName(), error, rc_error_str(error)); - callback(-1, std::string(), Common::HTTPDownloader::Request::Data()); - return; - } - - if (api_request.post_data) - { - // needs to be a post - http_downloader->CreatePostRequest(api_request.url, api_request.post_data, std::move(callback)); - } else - { - // get is fine - http_downloader->CreateRequest(api_request.url, std::move(callback)); - } - } - - bool DownloadImage(std::string cache_filename) - { - const int error = InitFunc(&api_request, this); - if (error != RC_OK) - { - FormattedError("%s failed: error %d (%s)", RAPIStructName(), error, rc_error_str(error)); - return false; - } - - _dbg_assert_msg_(!api_request.post_data, "Download request does not have POST data"); - - Achievements::DownloadImage(api_request.url, std::move(cache_filename)); - return true; - } - - std::string GetURL() - { - const int error = InitFunc(&api_request, this); - if (error != RC_OK) - { - FormattedError("%s failed: error %d (%s)", RAPIStructName(), error, rc_error_str(error)); - return std::string(); - } - - return api_request.url; - } -}; - -template -struct RAPIResponse : public T -{ -private: - bool initialized = false; - -public: - RAPIResponse(s32 status_code, Common::HTTPDownloader::Request::Data &data) - { - if (status_code != 200 || data.empty()) - { - FormattedError("%s failed: empty response and/or status code %d", RAPIStructName(), status_code); - LogFailedResponseJSON(data); - return; - } - - // ensure null termination, rapi needs it - data.push_back(0); - - const int error = ParseFunc(this, reinterpret_cast(data.data())); - initialized = (error == RC_OK); - - const rc_api_response_t &response = static_cast(this)->response; - if (error != RC_OK) - { - FormattedError("%s failed: parse function returned %d (%s)", RAPIStructName(), error, rc_error_str(error)); - LogFailedResponseJSON(data); - } else if (!response.succeeded) - { - FormattedError("%s failed: %s", RAPIStructName(), - response.error_message ? response.error_message : ""); - LogFailedResponseJSON(data); - } - } - - ~RAPIResponse() - { - if (initialized) - DestroyFunc(this); - } - - operator bool() const { return initialized && static_cast(this)->response.succeeded; } -}; - -} // namespace Achievements - -void Achievements::FormattedError(const char *format, ...) -{ - std::va_list ap; - va_start(ap, format); - char buffer[1024]; - vsnprintf(buffer, sizeof(buffer), format, ap); - va_end(ap); - - ERROR_LOG(ACHIEVEMENTS, "%s", buffer); - // Host::AddOSDMessage(std::move(error), 10.0f); +rc_client_t *GetClient() { + return g_rcClient; } -void Achievements::LogFailedResponseJSON(const Common::HTTPDownloader::Request::Data &data) -{ - const std::string str_data(reinterpret_cast(data.data()), data.size()); - ERROR_LOG(ACHIEVEMENTS, "API call failed. Response JSON was:\n%s", str_data.c_str()); +bool IsLoggedIn() { + return rc_client_get_user_info(g_rcClient) != nullptr; } -const Achievements::Achievement *Achievements::GetAchievementByID(u32 id) -{ - for (const Achievement &ach : s_achievements) - { - if (ach.id == id) - return &ach; - } - - return nullptr; -} - -Achievements::Achievement *Achievements::GetMutableAchievementByID(u32 id) -{ - for (Achievement &ach : s_achievements) - { - if (ach.id == id) - return &ach; - } - - return nullptr; -} - -void Achievements::ClearGameInfo(bool clear_achievements, bool clear_leaderboards) -{ - const bool had_game = (s_game_id != 0); - - if (clear_achievements) - { - while (!s_achievements.empty()) - { - Achievement &ach = s_achievements.back(); - DeactivateAchievement(&ach); - s_achievements.pop_back(); - } - s_primed_achievement_count.store(0, std::memory_order_release); - } - if (clear_leaderboards) - { - while (!s_leaderboards.empty()) - { - Leaderboard &lb = s_leaderboards.back(); - rc_runtime_deactivate_lboard(&s_rcheevos_runtime, lb.id); - s_leaderboards.pop_back(); - } - - s_last_queried_lboard = 0; - s_submitting_lboard_id = 0; - s_lboard_entries.reset(); - } - - if (s_achievements.empty() && s_leaderboards.empty()) - { - // Ready to tear down cheevos completely - s_game_title = {}; - s_game_icon = {}; - s_rich_presence_string = {}; - s_has_rich_presence = false; - s_game_id = 0; - } - - // Reset statistics - g_stats = {}; - - if (had_game) - Host::OnAchievementsRefreshed(); -} - -void Achievements::ClearGameHash() -{ - s_game_path.clear(); - s_game_hash.clear(); -} - -bool Achievements::IsActive() -{ - return s_active; -} - -bool Achievements::IsLoggedIn() -{ - return s_logged_in; -} - -bool Achievements::ChallengeModeActive() -{ - return s_challenge_mode; -} - -bool Achievements::LeaderboardsActive() -{ - return ChallengeModeActive() && g_Config.bAchievementsLeaderboards; -} - -bool Achievements::IsTestModeActive() -{ - return g_Config.bAchievementsTestMode; -} - -bool Achievements::IsUnofficialTestModeActive() -{ - return g_Config.bAchievementsUnofficialTestMode; -} - -bool Achievements::IsRichPresenceEnabled() -{ - return g_Config.bAchievementsRichPresence; -} - -bool Achievements::HasActiveGame() -{ - return s_game_id != 0; -} - -u32 Achievements::GetGameID() -{ - return s_game_id; -} - -std::unique_lock Achievements::GetLock() -{ - return std::unique_lock(s_achievements_mutex); -} - -void Achievements::Initialize() -{ - std::unique_lock lock(s_achievements_mutex); - _assert_msg_(g_Config.bAchievementsEnable, "Achievements are enabled"); - - s_http_downloader = Common::HTTPDownloader::Create(); - if (!s_http_downloader) - { - // TODO: Also report to user - ERROR_LOG(ACHIEVEMENTS, "Failed to create HTTPDownloader, cannot use achievements"); - return; - } - - s_active = true; - s_challenge_mode = false; - rc_runtime_init(&s_rcheevos_runtime); - - s_last_ping_time = time_now_d(); - s_username = g_Config.sAchievementsUserName; - s_api_token = NativeLoadSecret(RA_TOKEN_SECRET_NAME); - if (s_api_token.empty()) { - s_api_token = g_Config.sAchievementsToken; - } - s_logged_in = (!s_username.empty() && !s_api_token.empty()); - - // this is just the non-SSL path. - rc_api_set_host("http://retroachievements.org"); - - // if (System::IsValid()) - // GameChanged(); -} - -void Achievements::UpdateSettings() -{ - if (!g_Config.bAchievementsEnable) - { - // we're done here - Shutdown(); - return; - } - - if (!s_active) - { - // we just got enabled - Initialize(); - return; - } - - /* - // TODO: We don't have an "old" config state. But we can probably maintain one right here - // in this file. - - if (g_settings.achievements_challenge_mode != old_config.achievements_challenge_mode) - { - // Hardcore mode can only be enabled through reset (ResetChallengeMode()). - if (s_challenge_mode && !g_settings.achievements_challenge_mode) - { - ResetChallengeMode(); - } else if (!s_challenge_mode && g_settings.achievements_challenge_mode) - { - ImGuiFullscreen::ShowToast( - std::string(), Host::TranslateStdString("Achievements", "Hardcore mode will be enabled on system reset."), - 10.0f); - } - } - */ - - // FIXME: Handle changes to various settings individually - /* - if (g_settings.achievements_test_mode != old_config.achievements_test_mode || - g_settings.achievements_unofficial_test_mode != old_config.achievements_unofficial_test_mode || - g_settings.achievements_use_first_disc_from_playlist != old_config.achievements_use_first_disc_from_playlist || - g_settings.achievements_rich_presence != old_config.achievements_rich_presence) - { - return; - } - */ - - Shutdown(); - Initialize(); -} - -bool Achievements::ConfirmChallengeModeDisable(const char *trigger) -{ - // I really hope this doesn't deadlock :/ - /* - const bool confirmed = Host::ConfirmMessage( - Host::TranslateString("Achievements", "Confirm Hardcore Mode"), - fmt::format(Host::TranslateString("Achievements", - "{0} cannot be performed while hardcore mode is active. Do you " - "want to disable hardcore mode? {0} will be cancelled if you select No.") - .GetCharArray(), - trigger)); - if (!confirmed) +bool EncoreModeActive() { + if (!g_rcClient) { return false; - */ - - DisableChallengeMode(); - return true; + } + return rc_client_get_encore_mode_enabled(g_rcClient); } -void Achievements::DisableChallengeMode() -{ - if (!s_active) - return; - - if (s_challenge_mode) - SetChallengeMode(false); -} - -bool Achievements::ResetChallengeMode() -{ - if (!s_active || s_challenge_mode == g_Config.bAchievementsChallengeMode) +bool UnofficialEnabled() { + if (!g_rcClient) { return false; - - SetChallengeMode(g_Config.bAchievementsChallengeMode); - return true; + } + return rc_client_get_unofficial_enabled(g_rcClient); } -void Achievements::SetChallengeMode(bool enabled) -{ - if (enabled == s_challenge_mode) - return; +bool ChallengeModeActive() { + if (!g_rcClient) { + return false; + } + return rc_client_get_hardcore_enabled(g_rcClient); +} - // new mode - s_challenge_mode = enabled; +bool WarnUserIfChallengeModeActive(const char *message) { + if (!ChallengeModeActive()) { + return false; + } - if (HasActiveGame()) - { + const char *showMessage = message; + if (!message) { auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); - auto di = GetI18NCategory(I18NCat::DIALOG); - - OSDAddNotification(5.0f, std::string(ac->T("Challenge Mode")) + ": " + di->T(enabled ? "Enabled" : "Disabled"), "", ""); + showMessage = ac->T("This feature is not available in Challenge Mode"); } - if (HasActiveGame() && !IsTestModeActive()) - { - // deactivate, but don't clear all achievements (getting unlocks will reactivate them) - std::unique_lock lock(s_achievements_mutex); - for (Achievement &achievement : s_achievements) - { - DeactivateAchievement(&achievement); - achievement.locked = true; - } - for (Leaderboard &leaderboard : s_leaderboards) - rc_runtime_deactivate_lboard(&s_rcheevos_runtime, leaderboard.id); - } - - // re-grab unlocks, this will reactivate what's locked in non-hardcore mode later on - if (!s_achievements.empty()) - GetUserUnlocks(); -} - -bool Achievements::Shutdown() -{ - if (!s_active) - return true; - - std::unique_lock lock(s_achievements_mutex); - s_http_downloader->WaitForAllRequests(); - - ClearGameInfo(); - ClearGameHash(); - std::string().swap(s_username); - std::string().swap(s_api_token); - s_logged_in = false; - Host::OnAchievementsRefreshed(); - - s_active = false; - s_challenge_mode = false; - rc_runtime_destroy(&s_rcheevos_runtime); - - s_http_downloader.reset(); + g_OSD.Show(OSDType::MESSAGE_WARNING, showMessage, 3.0f); return true; } -bool Achievements::ConfirmSystemReset() -{ - return true; +bool IsBlockingExecution() { + return g_isIdentifying; } -void Achievements::ResetRuntime() -{ - if (!s_active) - return; - - std::unique_lock lock(s_achievements_mutex); - INFO_LOG(ACHIEVEMENTS, "Resetting rcheevos state..."); - rc_runtime_reset(&s_rcheevos_runtime); -} - -void Achievements::FrameUpdate() -{ - if (!IsActive()) - return; - - s_http_downloader->PollRequests(); - - if (HasActiveGame()) - { - std::unique_lock lock(s_achievements_mutex); - rc_runtime_do_frame(&s_rcheevos_runtime, &CheevosEventHandler, &PeekMemory, nullptr, nullptr); - UpdateRichPresence(); - - if (!IsTestModeActive()) - { - const s32 ping_frequency = - g_Config.bAchievementsRichPresence ? RICH_PRESENCE_PING_FREQUENCY : NO_RICH_PRESENCE_PING_FREQUENCY; - if (static_cast(time_now_d() - s_last_ping_time) >= ping_frequency) - SendPing(); - } - } -} - -void Achievements::ProcessPendingHTTPRequests() -{ - s_http_downloader->PollRequests(); -} - -/* -bool Achievements::DoState(PointerWrap &pw) -{ - auto sw = pw.Section("Achievements", 1); - if (!sw) { - // Save state is missing the section. - // Reset the runtime. - rc_runtime_reset(&s_rcheevos_runtime); - return; +static u32 GetGameID() { + if (!g_rcClient) { + return 0; } - // if we're inactive, we still need to skip the data (if any) - if (!s_active) - { - u32 data_size = 0; - sw.Do(&data_size); - if (data_size > 0) - sw.SkipBytes(data_size); - - return !sw.HasError(); + const rc_client_game_t *info = rc_client_get_game_info(g_rcClient); + if (!info) { + return 0; } - - std::unique_lock lock(s_achievements_mutex); - - if (sw.IsReading()) - { - // if we're active, make sure we've downloaded and activated all the achievements - // before deserializing, otherwise that state's going to get lost. - if (s_http_downloader->HasAnyRequests()) - { - Host::DisplayLoadingScreen("Downloading achievements data..."); - s_http_downloader->WaitForAllRequests(); - } - - u32 data_size = 0; - sw.Do(&data_size); - if (data_size == 0) - { - // reset runtime, no data (state might've been created without cheevos) - DEBUG_LOG(ACHIEVEMENTS, "State is missing cheevos data, resetting runtime"); - rc_runtime_reset(&s_rcheevos_runtime); - return !sw.HasError(); - } - - const std::unique_ptr data(new u8[data_size]); - sw.DoBytes(data.get(), data_size); - if (sw.HasError()) - return false; - - return true; - } else - { - u32 data_size; - std::unique_ptr data; - - { - // internally this happens twice.. not great. - const int size = rc_runtime_progress_size(&s_rcheevos_runtime, nullptr); - - data_size = (size >= 0) ? static_cast(size) : 0; - data = std::unique_ptr(new u8[data_size]); - - const int result = rc_runtime_serialize_progress(data.get(), &s_rcheevos_runtime, nullptr); - if (result != RC_OK) - { - // set data to zero, effectively serializing nothing - WARN_LOG(ACHIEVEMENTS, "Failed to serialize cheevos state (%d)", result); - data_size = 0; - } - } - - sw.Do(&data_size); - if (data_size > 0) - sw.DoBytes(data.get(), data_size); - - return !sw.HasError(); - } -} -*/ - -bool Achievements::SafeHasAchievementsOrLeaderboards() -{ - std::unique_lock lock(s_achievements_mutex); - return !s_achievements.empty() || !s_leaderboards.empty(); + return info->id; // 0 if not identified } -const std::string &Achievements::GetUsername() -{ - return s_username; +bool IsActive() { + return GetGameID() != 0; } -const std::string &Achievements::GetRichPresenceString() -{ - return s_rich_presence_string; -} - -void Achievements::LoginCallback(s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data) -{ - std::unique_lock lock(s_achievements_mutex); - - RAPIResponse response( - status_code, data); - if (!response || !response.username || !response.api_token) - { - FormattedError("Login failed. Please check your user name and password, and try again."); - return; - } - - std::string username(response.username); - std::string api_token(response.api_token); - - // save to config - g_Config.sAchievementsUserName = username; - g_Config.sAchievementsLoginTimestamp = StringFromFormat("%llu", (unsigned long long)std::time(nullptr)); - NativeSaveSecret(RA_TOKEN_SECRET_NAME, api_token); - - g_Config.Save("AchievementsLogin"); - - if (s_active) - { - s_username = std::move(username); - s_api_token = std::move(api_token); - s_logged_in = true; - - // If we have a game running, set it up. - if (!s_game_hash.empty()) - SendGetGameId(); - } - - Host::OnAchievementsLoginStateChange(); -} - -void Achievements::LoginASyncCallback(s32 status_code, std::string content_type, - Common::HTTPDownloader::Request::Data data) -{ - OSDCloseBackgroundProgressDialog("cheevos_async_login"); - - LoginCallback(status_code, std::move(content_type), std::move(data)); -} - -void Achievements::SendLogin(const char *username, const char *password, Common::HTTPDownloader *http_downloader, - Common::HTTPDownloader::Request::Callback callback) -{ - RAPIRequest request; - request.username = username; - request.password = password; - request.api_token = nullptr; - request.Send(http_downloader, std::move(callback)); -} - -bool Achievements::LoginAsync(const char *username, const char *password) -{ - s_http_downloader->WaitForAllRequests(); - - if (s_logged_in || std::strlen(username) == 0 || std::strlen(password) == 0) - return false; - - OSDOpenBackgroundProgressDialog("cheevos_async_login", "Logging in to RetroAchivements...", 0, 0, 0); - - SendLogin(username, password, s_http_downloader.get(), LoginASyncCallback); - return true; -} - -void Achievements::Logout() -{ - if (s_active) - { - std::unique_lock lock(s_achievements_mutex); - s_http_downloader->WaitForAllRequests(); - if (s_logged_in) - { - ClearGameInfo(); - std::string().swap(s_username); - std::string().swap(s_api_token); - s_logged_in = false; - Host::OnAchievementsLoginStateChange(); - } - } - - // remove from config - g_Config.sAchievementsUserName.clear(); - NativeSaveSecret(RA_TOKEN_SECRET_NAME, ""); - g_Config.sAchievementsLoginTimestamp.clear(); - g_Config.Save("Achievements logout"); -} - -void Achievements::DownloadImage(std::string url, std::string cache_filename) -{ - auto callback = [cache_filename](s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data) { - if (status_code != HTTP_OK) - return; - g_iconCache.InsertIcon(cache_filename, IconFormat::PNG, std::move(data)); - }; - - if (g_iconCache.MarkPending(cache_filename)) { - INFO_LOG(ACHIEVEMENTS, "Downloading image: %s (%s)", url.c_str(), cache_filename.c_str()); - s_http_downloader->CreateRequest(std::move(url), std::move(callback)); - } -} - -Achievements::Statistics Achievements::GetStatistics() { - return g_stats; -} - -std::string Achievements::GetGameAchievementSummary() { - auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); - - std::string summary; - summary = StringFromFormat(ac->T("Earned", "You have unlocked %d of %d achievements, earning %d of %d points"), - GetUnlockedAchiementCount(), GetAchievementCount(), GetCurrentPointsForGame(), GetMaximumPointsForGame()); - if (GetLeaderboardCount() > 0 && LeaderboardsActive()) { - summary.append("\n"); - summary.append(ac->T("Leaderboard submission is enabled")); - } - return summary; -} - -void Achievements::DisplayAchievementSummary() -{ - auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); - - std::string title; - if (ChallengeModeActive()) - title = s_game_title + " (" + ac->T("Challenge Mode") + ")"; - else - title = s_game_title; - - std::string summary = GetGameAchievementSummary(); - - OSDAddNotification(10.0f, title, summary, s_game_icon); -} - -void Achievements::DisplayMasteredNotification() -{ - auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); - - if (!g_Config.bAchievementsNotifications) - return; - - // TODO: Translation? - std::string title = ReplaceAll(ac->T("Mastered %1"), "%1", s_game_title.c_str()); - std::string message = StringFromFormat(ac->T("%d achievements, %d points"), GetAchievementCount(), GetCurrentPointsForGame()); - - OSDAddNotification(20.0f, title, message, s_game_icon); - NOTICE_LOG(ACHIEVEMENTS, "%s", message.c_str()); -} - -void Achievements::GetUserUnlocksCallback(s32 status_code, std::string content_type, - Common::HTTPDownloader::Request::Data data) -{ - // if (!System::IsValid()) - // return; - - RAPIResponse - response(status_code, data); - - std::unique_lock lock(s_achievements_mutex); - if (!response) - { - ClearGameInfo(true, false); - return; - } - - // flag achievements as unlocked - for (u32 i = 0; i < response.num_achievement_ids; i++) - { - Achievement *cheevo = GetMutableAchievementByID(response.achievement_ids[i]); - if (!cheevo) - { - ERROR_LOG(ACHIEVEMENTS, "Server returned unknown achievement %u", response.achievement_ids[i]); - continue; - } - - cheevo->locked = false; - } - - // start scanning for locked achievements - ActivateLockedAchievements(); - DisplayAchievementSummary(); - SendPlaying(); - UpdateRichPresence(); - SendPing(); - Host::OnAchievementsRefreshed(); -} - -void Achievements::GetUserUnlocks() -{ - RAPIRequest request; - request.username = s_username.c_str(); - request.api_token = s_api_token.c_str(); - request.game_id = s_game_id; - request.hardcore = static_cast(ChallengeModeActive()); - request.Send(GetUserUnlocksCallback); -} - -static int ValidateAddress(unsigned address) { - return Memory::IsValidAddress(address + PSP_MEMORY_OFFSET) ? 1 : 0; -} - -void Achievements::GetPatchesCallback(s32 status_code, std::string content_type, - Common::HTTPDownloader::Request::Data data) -{ - // if (!System::IsValid()) - // return; - - RAPIResponse - response(status_code, data); - - std::unique_lock lock(s_achievements_mutex); - ClearGameInfo(); - if (!response || !response.title) - { - DisableChallengeMode(); - return; - } - - // ensure fullscreen UI is ready - // Host::RunOnCPUThread(FullscreenUI::Initialize); - - s_game_id = response.id; - s_game_title = response.title; - - // try for a icon. not that we really need one, PSP games have their own icons... - if (response.image_name && std::strlen(response.image_name) > 0) - { - s_game_icon = g_gameIconCachePrefix + StringFromFormat("%d", s_game_id); - if (!g_iconCache.Contains(s_game_icon)) - { - RAPIRequest request; - request.image_name = response.image_name; - request.image_type = RC_IMAGE_TYPE_GAME; - request.DownloadImage(s_game_icon); - } - } - - // parse achievements - for (u32 i = 0; i < response.num_achievements; i++) - { - const rc_api_achievement_definition_t &defn = response.achievements[i]; - - // Skip local and unofficial achievements for now, unless "Test Unofficial Achievements" is enabled - if (defn.category == RC_ACHIEVEMENT_CATEGORY_UNOFFICIAL) - { - if (!IsUnofficialTestModeActive()) - { - WARN_LOG(ACHIEVEMENTS, "Skipping unofficial achievement %u (%s)", defn.id, defn.title); - continue; - } - } - // local achievements shouldn't be in this list, but just in case? - else if (defn.category != RC_ACHIEVEMENT_CATEGORY_CORE) - { - continue; - } - - if (GetMutableAchievementByID(defn.id)) - { - ERROR_LOG(ACHIEVEMENTS, "Achievement %u already exists", defn.id); - continue; - } - - if (!defn.definition || !defn.title || !defn.description || !defn.badge_name) - { - ERROR_LOG(ACHIEVEMENTS, "Incomplete achievement %u", defn.id); - continue; - } - - Achievement cheevo; - cheevo.id = defn.id; - cheevo.memaddr = defn.definition; - cheevo.title = defn.title; - cheevo.description = defn.description; - cheevo.badge_name = defn.badge_name; - cheevo.locked = true; - cheevo.active = false; - cheevo.primed = false; - cheevo.points = defn.points; - cheevo.category = static_cast(defn.category); - s_achievements.push_back(std::move(cheevo)); - } - - for (u32 i = 0; i < response.num_leaderboards; i++) - { - const rc_api_leaderboard_definition_t &defn = response.leaderboards[i]; - if (!defn.title || !defn.description || !defn.definition) - { - ERROR_LOG(ACHIEVEMENTS, "Incomplete achievement %u", defn.id); - continue; - } - - Leaderboard lboard; - lboard.id = defn.id; - lboard.title = defn.title; - lboard.description = defn.description; - lboard.format = defn.format; - lboard.hidden = defn.hidden; - s_leaderboards.push_back(std::move(lboard)); - - const int err = rc_runtime_activate_lboard(&s_rcheevos_runtime, defn.id, defn.definition, nullptr, 0); - if (err != RC_OK) - { - ERROR_LOG(ACHIEVEMENTS, "Leaderboard %u memaddr parse error: %s", defn.id, rc_error_str(err)); - } else - { - DEBUG_LOG(ACHIEVEMENTS, "Activated leaderboard %s (%u)", defn.title, defn.id); - } - } - - // parse rich presence - if (response.rich_presence_script && std::strlen(response.rich_presence_script) > 0) - { - const int res = rc_runtime_activate_richpresence(&s_rcheevos_runtime, response.rich_presence_script, nullptr, 0); - if (res == RC_OK) - s_has_rich_presence = true; - else - WARN_LOG(ACHIEVEMENTS, "Failed to activate rich presence: %s", rc_error_str(res)); - } - - INFO_LOG(ACHIEVEMENTS, "Game Title: %s", s_game_title.c_str()); - INFO_LOG(ACHIEVEMENTS, "Achievements: %zu", s_achievements.size()); - INFO_LOG(ACHIEVEMENTS, "Leaderboards: %zu", s_leaderboards.size()); - - // We don't want to block saving/loading states when there's no achievements. - if (s_achievements.empty() && s_leaderboards.empty()) - DisableChallengeMode(); - - if (!s_achievements.empty() || s_has_rich_presence) - { - if (!IsTestModeActive()) - { - GetUserUnlocks(); - } else - { - ActivateLockedAchievements(); - DisplayAchievementSummary(); - Host::OnAchievementsRefreshed(); - } - } else - { - DisplayAchievementSummary(); - } - - if (s_achievements.empty() && s_leaderboards.empty() && !s_has_rich_presence) - { - ClearGameInfo(); - } - - // Hook up memory validation (doesn't seem to do much though?) - rc_runtime_validate_addresses(&s_rcheevos_runtime, &Achievements::CheevosEventHandler, &ValidateAddress); - - // Stop the progress bar. - OSDCloseBackgroundProgressDialog("get_patches"); -} - -void Achievements::GetLbInfoCallback(s32 status_code, std::string content_type, - Common::HTTPDownloader::Request::Data data) -{ - // if (!System::IsValid()) - // return; - - RAPIResponse - response(status_code, data); - if (!response) - return; - - std::unique_lock lock(s_achievements_mutex); - if (response.id != s_last_queried_lboard) - { - // User has already requested another leaderboard, drop this data - return; - } - - const Leaderboard *leaderboard = GetLeaderboardByID(response.id); - if (!leaderboard) - { - ERROR_LOG(ACHIEVEMENTS, "Attempting to list unknown leaderboard %u", response.id); - return; - } - - s_lboard_entries = std::vector(); - for (u32 i = 0; i < response.num_entries; i++) - { - const rc_api_lboard_info_entry_t &entry = response.entries[i]; - if (!entry.username) - continue; - - char score[128]; - rc_runtime_format_lboard_value(score, sizeof(score), entry.score, leaderboard->format); - - LeaderboardEntry lbe; - lbe.user = entry.username; - lbe.rank = entry.rank; - lbe.submitted = entry.submitted; - lbe.formatted_score = score; - lbe.is_self = lbe.user == s_username; - - s_lboard_entries->push_back(std::move(lbe)); - } -} - -void Achievements::GetPatches(u32 game_id) -{ - auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); - - OSDOpenBackgroundProgressDialog("get_patches", ac->T("Syncing achievements data..."), 0, 0, 0); - - RAPIRequest request; - request.username = s_username.c_str(); - request.api_token = s_api_token.c_str(); - request.game_id = game_id; - request.Send(GetPatchesCallback); -} - -// File reader that can handle Path (and thus Android storage and stuff). -static void *ac_open(const char *utf8Path) { - Path path(utf8Path); - FILE *f = File::OpenCFile(path, "rb"); - return (void *)f; -} - -static void ac_seek(void *file_handle, int64_t offset, int origin) { - fseek((FILE *)file_handle, offset, origin); -} - -static int64_t ac_tell(void *file_handle) { - return ftell((FILE *)file_handle); -} - -static size_t ac_read(void *file_handle, void *buffer, size_t requested_bytes) { - return fread(buffer, 1, requested_bytes, (FILE *)file_handle); -} - -static void ac_close(void *file_handle) { - fclose((FILE *)file_handle); -} - -std::string Achievements::GetGameHash(const Path &path) -{ - // According to https://docs.retroachievements.org/Game-Identification/, we should simply - // concatenate param.sfo and eboot.bin, and hash the result, to obtain the game hash. - - // UNFORTUNATELY, it's borked. Turns out that retroarch's rc_hash_cd_file is broken and will read - // outside the last sector in every case. Doubly unfortunately, all the hashes on retroachievements - // are generated like that. Oh well. - - // We will need to reimplement it properly (hash some zeroes I guess, below) to handle file types - // that the cdreader can't handle (or we make a custom cdreader) but for now we just return orig_hash_str. - - rc_hash_filereader rc_filereader; - rc_filereader.open = &ac_open; - rc_filereader.seek = &ac_seek; - rc_filereader.tell = &ac_tell; - rc_filereader.read = &ac_read; - rc_filereader.close = &ac_close; - - rc_hash_init_custom_filereader(&rc_filereader); - rc_hash_init_default_cdreader(); - - char orig_hash_str[33]{}; - std::string ppath = path.ToString(); - - if (0 == rc_hash_generate_from_file(orig_hash_str, RC_CONSOLE_PSP, ppath.c_str())) { - ERROR_LOG(ACHIEVEMENTS, "Failed to generate hash from file: %s", ppath.c_str()); - return ""; - } - - return std::string(orig_hash_str); -} - -void Achievements::GetGameIdCallback(s32 status_code, std::string content_type, - Common::HTTPDownloader::Request::Data data) -{ - // if (!System::IsValid()) - // return; - - RAPIResponse - response(status_code, data); - if (!response) - return; - - const u32 game_id = response.game_id; - NOTICE_LOG(ACHIEVEMENTS, "Server returned GameID %u", game_id); - if (game_id == 0) - { - auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); - // We don't want to block saving/loading states when there's no achievements. - OSDAddNotification(4.0f, ac->T("RetroAchievements are not available for this game"), "", ""); - DisableChallengeMode(); - return; - } - - GetPatches(game_id); -} - -void Achievements::LeftGame() { - // Should just uninitialize -} - -void Achievements::GameChanged(const Path &path) -{ - if (!IsActive() || s_game_path == path) - return; - - std::string game_hash; - - game_hash = GetGameHash(path); - if (s_game_hash == game_hash) - { - // only the path has changed - different format/save state/etc. - INFO_LOG(ACHIEVEMENTS, "Detected path change from '%s' to '%s'", s_game_path.c_str(), path.c_str()); - s_game_path = path; - return; - } - - if (s_http_downloader->HasAnyRequests()) - { - s_http_downloader->WaitForAllRequests(); - } - - std::unique_lock lock(s_achievements_mutex); - - ClearGameInfo(); - ClearGameHash(); - s_game_path = path; - s_game_hash = std::move(game_hash); - - if (s_game_hash.empty()) - { - // when we're booting the bios, this will fail - if (!s_game_path.empty()) - { - OSDAddErrorMessage("retroachievements_disc_read_failed", - "Failed to read executable from disc. Achievements disabled.", 10.0f); - } - - DisableChallengeMode(); - return; - } - - if (IsLoggedIn()) - SendGetGameId(); -} - -void Achievements::SendGetGameId() -{ - RAPIRequest request; - request.username = s_username.c_str(); - request.api_token = s_api_token.c_str(); - request.game_hash = s_game_hash.c_str(); - request.Send(GetGameIdCallback); -} - -void Achievements::SendPlayingCallback(s32 status_code, std::string content_type, - Common::HTTPDownloader::Request::Data data) -{ - // if (!System::IsValid()) - // return; - - RAPIResponse - response(status_code, data); - if (!response) - return; - - INFO_LOG(ACHIEVEMENTS, "Playing game updated to %u (%s)", s_game_id, s_game_title.c_str()); -} - -void Achievements::SendPlaying() -{ - if (!HasActiveGame()) - return; - - RAPIRequest request; - request.username = s_username.c_str(); - request.api_token = s_api_token.c_str(); - request.game_id = s_game_id; - request.Send(SendPlayingCallback); -} - -void Achievements::UpdateRichPresence() -{ - if (!s_has_rich_presence) - return; - - char buffer[512]; - const int res = - rc_runtime_get_richpresence(&s_rcheevos_runtime, buffer, sizeof(buffer), PeekMemory, nullptr, nullptr); - if (res <= 0) - { - const bool had_rich_presence = !s_rich_presence_string.empty(); - s_rich_presence_string.clear(); - if (had_rich_presence) - Host::OnAchievementsRefreshed(); - - return; - } - - std::unique_lock lock(s_achievements_mutex); - if (s_rich_presence_string == buffer) - return; - - s_rich_presence_string.assign(buffer); - Host::OnAchievementsRefreshed(); -} - -void Achievements::SendPingCallback(s32 status_code, std::string content_type, - Common::HTTPDownloader::Request::Data data) -{ - // if (!System::IsValid()) - // return; - - RAPIResponse response(status_code, - data); -} - -void Achievements::SendPing() -{ - if (!HasActiveGame()) - return; - - s_last_ping_time = time_now_d(); - - RAPIRequest request; - request.api_token = s_api_token.c_str(); - request.username = s_username.c_str(); - request.game_id = s_game_id; - request.rich_presence = s_rich_presence_string.c_str(); - request.Send(SendPingCallback); -} - -const std::string &Achievements::GetGameTitle() -{ - return s_game_title; -} - -const std::string &Achievements::GetGameIcon() -{ - return s_game_icon; -} - -bool Achievements::EnumerateAchievements(std::function callback) -{ - for (const Achievement &cheevo : s_achievements) - { - if (!callback(cheevo)) - return false; - } - - return true; -} - -u32 Achievements::GetUnlockedAchiementCount() -{ - u32 count = 0; - for (const Achievement &cheevo : s_achievements) - { - if (!cheevo.locked) - count++; - } - - return count; -} - -u32 Achievements::GetAchievementCount() -{ - return static_cast(s_achievements.size()); -} - -u32 Achievements::GetMaximumPointsForGame() -{ - u32 points = 0; - for (const Achievement &cheevo : s_achievements) - points += cheevo.points; - - return points; -} - -u32 Achievements::GetCurrentPointsForGame() -{ - u32 points = 0; - for (const Achievement &cheevo : s_achievements) - { - if (!cheevo.locked) - points += cheevo.points; - } - - return points; -} - -bool Achievements::EnumerateLeaderboards(std::function callback) -{ - for (const Leaderboard &lboard : s_leaderboards) - { - if (!callback(lboard)) - return false; - } - - return true; -} - -std::optional Achievements::TryEnumerateLeaderboardEntries(u32 id, - std::function callback) -{ - if (id == s_last_queried_lboard) - { - if (s_lboard_entries) - { - for (const LeaderboardEntry &entry : *s_lboard_entries) - { - if (!callback(entry)) - return false; - } - return true; - } - } else - { - s_last_queried_lboard = id; - s_lboard_entries.reset(); - - // TODO: Add paging? For now, stick to defaults - RAPIRequest request; - request.username = s_username.c_str(); - request.leaderboard_id = id; - request.first_entry = 0; - - // Just over what a single page can store, should be a reasonable amount for now - request.count = 15; - - request.Send(GetLbInfoCallback); - } - - return std::nullopt; -} - -const Achievements::Leaderboard *Achievements::GetLeaderboardByID(u32 id) -{ - for (const Leaderboard &lb : s_leaderboards) - { - if (lb.id == id) - return &lb; - } - - return nullptr; -} - -u32 Achievements::GetLeaderboardCount() -{ - return static_cast(s_leaderboards.size()); -} - -bool Achievements::IsLeaderboardTimeType(const Leaderboard &leaderboard) -{ - return leaderboard.format != RC_FORMAT_SCORE && leaderboard.format != RC_FORMAT_VALUE; -} - -bool Achievements::IsMastered() -{ - for (const Achievement &cheevo : s_achievements) - { - if (cheevo.locked) - return false; - } - - return true; -} - -void Achievements::ActivateLockedAchievements() -{ - for (Achievement &cheevo : s_achievements) - { - if (cheevo.locked) - ActivateAchievement(&cheevo); - } -} - -bool Achievements::ActivateAchievement(Achievement *achievement) -{ - if (achievement->active) - return true; - - const int err = - rc_runtime_activate_achievement(&s_rcheevos_runtime, achievement->id, achievement->memaddr.c_str(), nullptr, 0); - if (err != RC_OK) - { - ERROR_LOG(ACHIEVEMENTS, "Achievement %u memaddr parse error: %s", achievement->id, rc_error_str(err)); - return false; - } - - achievement->active = true; - - DEBUG_LOG(ACHIEVEMENTS, "Activated achievement %s (%u)", achievement->title.c_str(), achievement->id); - return true; -} - -void Achievements::DeactivateAchievement(Achievement *achievement) -{ - if (!achievement->active) - return; - - rc_runtime_deactivate_achievement(&s_rcheevos_runtime, achievement->id); - achievement->active = false; - - if (achievement->primed) - { - achievement->primed = false; - s_primed_achievement_count.fetch_sub(std::memory_order_acq_rel); - } - - DEBUG_LOG(ACHIEVEMENTS, "Deactivated achievement %s (%u)", achievement->title.c_str(), achievement->id); -} - -void Achievements::UnlockAchievementCallback(s32 status_code, std::string content_type, - Common::HTTPDownloader::Request::Data data) -{ - // if (!System::IsValid()) - // return; - - RAPIResponse - response(status_code, data); - if (!response) - return; - - INFO_LOG(ACHIEVEMENTS, "Successfully unlocked achievement %u, new score %u", response.awarded_achievement_id, - response.new_player_score); -} - -void Achievements::SubmitLeaderboardCallback(s32 status_code, std::string content_type, - Common::HTTPDownloader::Request::Data data) -{ - // if (!System::IsValid()) - // return; - - RAPIResponse - response(status_code, data); - if (!response) - return; - - // Force the next leaderboard query to repopulate everything, just in case the user wants to see their new score - s_last_queried_lboard = 0; - - // RA API doesn't send us the leaderboard ID back.. hopefully we don't submit two at once :/ - if (s_submitting_lboard_id == 0) - return; - - const Leaderboard *lb = GetLeaderboardByID(std::exchange(s_submitting_lboard_id, 0u)); - if (!lb || !g_Config.bAchievementsNotifications) - return; - - char submitted_score[128]; - char best_score[128]; - rc_runtime_format_lboard_value(submitted_score, sizeof(submitted_score), response.submitted_score, lb->format); - rc_runtime_format_lboard_value(best_score, sizeof(best_score), response.best_score, lb->format); - - auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); - const char *formatString = ac->T("Submitted Score"); - std::string summary = StringFromFormat(formatString, - submitted_score, best_score, response.new_rank, response.num_entries); - - OSDAddNotification(10.0f, lb->title, std::move(summary), s_game_icon); -} - -void Achievements::UnlockAchievement(u32 achievement_id, bool add_notification /* = true*/) -{ - auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); - - std::unique_lock lock(s_achievements_mutex); - - Achievement *achievement = GetMutableAchievementByID(achievement_id); - if (!achievement) - { - ERROR_LOG(ACHIEVEMENTS, "Attempting to unlock unknown achievement %u", achievement_id); - return; - } else if (!achievement->locked) - { - WARN_LOG(ACHIEVEMENTS, "Achievement %u for game %u is already unlocked", achievement_id, s_game_id); - return; - } - - achievement->locked = false; - DeactivateAchievement(achievement); - - INFO_LOG(ACHIEVEMENTS, "Achievement %s (%u) for game %u unlocked", achievement->title.c_str(), achievement_id, s_game_id); - - if (g_Config.bAchievementsNotifications) - { - std::string title; - switch (achievement->category) - { - case AchievementCategory::Local: - title = achievement->title + " (" + ac->T("Local") + ")"; - break; - case AchievementCategory::Unofficial: - title = achievement->title + " (" + ac->T("Unofficial") + ")"; - break; - case AchievementCategory::Core: - default: - title = achievement->title; - break; - } - - OSDAddNotification(15.0f, std::move(title), achievement->description, - GetAchievementBadgePath(*achievement)); - } - - if (IsMastered()) - DisplayMasteredNotification(); - - if (IsTestModeActive()) - { - WARN_LOG(ACHIEVEMENTS, "Skipping sending achievement %u unlock to server because of test mode.", achievement_id); - return; - } - - if (achievement->category != AchievementCategory::Core) - { - WARN_LOG(ACHIEVEMENTS, "Skipping sending achievement %u unlock to server because it's not from the core set.", - achievement_id); - return; - } - - RAPIRequest request; - request.username = s_username.c_str(); - request.api_token = s_api_token.c_str(); - request.game_hash = s_game_hash.c_str(); - request.achievement_id = achievement_id; - request.hardcore = static_cast(ChallengeModeActive()); - request.Send(UnlockAchievementCallback); -} - -void Achievements::SubmitLeaderboard(u32 leaderboard_id, int value) -{ - if (IsTestModeActive()) - { - WARN_LOG(ACHIEVEMENTS, "Skipping sending leaderboard %u result to server because of test mode.", leaderboard_id); - return; - } - - if (!ChallengeModeActive()) - { - WARN_LOG(ACHIEVEMENTS, "Skipping sending leaderboard %u result to server because Challenge Mode is off.", leaderboard_id); - return; - } - - if (!LeaderboardsActive()) - { - WARN_LOG(ACHIEVEMENTS, "Skipping sending leaderboard %u result to server because leaderboards are disabled.", leaderboard_id); - return; - } - - std::unique_lock lock(s_achievements_mutex); - - s_submitting_lboard_id = leaderboard_id; - - RAPIRequest request; - request.username = s_username.c_str(); - request.api_token = s_api_token.c_str(); - request.game_hash = s_game_hash.c_str(); - request.leaderboard_id = leaderboard_id; - request.score = value; - request.Send(SubmitLeaderboardCallback); -} - -void Achievements::AchievementPrimed(u32 achievement_id) -{ - std::unique_lock lock(s_achievements_mutex); - Achievement *achievement = GetMutableAchievementByID(achievement_id); - if (!achievement || achievement->primed) - return; - - achievement->primed = true; - s_primed_achievement_count.fetch_add(std::memory_order_acq_rel); -} - -void Achievements::AchievementUnprimed(u32 achievement_id) -{ - std::unique_lock lock(s_achievements_mutex); - Achievement *achievement = GetMutableAchievementByID(achievement_id); - if (!achievement || !achievement->primed) - return; - - achievement->primed = false; - s_primed_achievement_count.fetch_sub(std::memory_order_acq_rel); -} - -void Achievements::AchievementDisabled(u32 achievement_id) -{ - std::unique_lock lock(s_achievements_mutex); - Achievement *achievement = GetMutableAchievementByID(achievement_id); - if (!achievement) - return; - - // Have not seen this trigger yet, despite games doing bad memory accesses. - INFO_LOG(ACHIEVEMENTS, "Achievement disabled due to invalid memory access: %s", achievement->title.c_str()); - achievement->disabled = true; -} - -std::pair Achievements::GetAchievementProgress(const Achievement &achievement) -{ - std::pair result; - rc_runtime_get_achievement_measured(&s_rcheevos_runtime, achievement.id, &result.first, &result.second); - return result; -} - -std::string Achievements::GetAchievementProgressText(const Achievement &achievement) -{ - char buf[256]; - rc_runtime_format_achievement_measured(&s_rcheevos_runtime, achievement.id, buf, std::size(buf)); - return buf; -} - -// Note that this returns an g_iconCache key, rather than an actual filename. So look up your image there. -std::string Achievements::GetAchievementBadgePath(const Achievement &achievement, bool download_if_missing, bool force_unlocked_icon) -{ - const bool use_locked = (achievement.locked && !force_unlocked_icon); - - std::string badge_path = g_iconCachePrefix + achievement.badge_name + std::string(use_locked ? "_lock" : ""); - if (g_iconCache.Contains(badge_path)) { - return badge_path; - } - - // need to download it - if (download_if_missing) { - RAPIRequest request; - request.image_name = achievement.badge_name.c_str(); - request.image_type = use_locked ? RC_IMAGE_TYPE_ACHIEVEMENT_LOCKED : RC_IMAGE_TYPE_ACHIEVEMENT; - request.DownloadImage(badge_path); - } - - return badge_path; -} - -std::string Achievements::GetAchievementBadgeURL(const Achievement &achievement) -{ - RAPIRequest request; - request.image_name = achievement.badge_name.c_str(); - request.image_type = achievement.locked ? RC_IMAGE_TYPE_ACHIEVEMENT_LOCKED : RC_IMAGE_TYPE_ACHIEVEMENT; - return request.GetURL(); -} - -u32 Achievements::GetPrimedAchievementCount() -{ - // Relaxed is fine here, worst that happens is we draw the triggers one frame late. - return s_primed_achievement_count.load(std::memory_order_relaxed); -} - -void Achievements::CheevosEventHandler(const rc_runtime_event_t *runtime_event) -{ - static const char *events[] = { "RC_RUNTIME_EVENT_ACHIEVEMENT_ACTIVATED", "RC_RUNTIME_EVENT_ACHIEVEMENT_PAUSED", - "RC_RUNTIME_EVENT_ACHIEVEMENT_RESET", "RC_RUNTIME_EVENT_ACHIEVEMENT_TRIGGERED", - "RC_RUNTIME_EVENT_ACHIEVEMENT_PRIMED", "RC_RUNTIME_EVENT_LBOARD_STARTED", - "RC_RUNTIME_EVENT_LBOARD_CANCELED", "RC_RUNTIME_EVENT_LBOARD_UPDATED", - "RC_RUNTIME_EVENT_LBOARD_TRIGGERED", "RC_RUNTIME_EVENT_ACHIEVEMENT_DISABLED", - "RC_RUNTIME_EVENT_LBOARD_DISABLED" }; - const char *event_text = - ((unsigned)runtime_event->type >= ARRAY_SIZE(events)) ? "unknown" : events[(unsigned)runtime_event->type]; - DEBUG_LOG(ACHIEVEMENTS, "Cheevos Event %s for %u", event_text, runtime_event->id); - - switch (runtime_event->type) - { - case RC_RUNTIME_EVENT_ACHIEVEMENT_TRIGGERED: - UnlockAchievement(runtime_event->id); - break; - - case RC_RUNTIME_EVENT_ACHIEVEMENT_PRIMED: - AchievementPrimed(runtime_event->id); - break; - - case RC_RUNTIME_EVENT_ACHIEVEMENT_UNPRIMED: - AchievementUnprimed(runtime_event->id); - break; - - case RC_RUNTIME_EVENT_ACHIEVEMENT_DISABLED: - AchievementDisabled(runtime_event->id); - break; - - case RC_RUNTIME_EVENT_LBOARD_TRIGGERED: - SubmitLeaderboard(runtime_event->id, runtime_event->value); - break; - - default: - break; - } -} - -unsigned Achievements::PeekMemory(unsigned address, unsigned num_bytes, void *ud) { +// This is the function the rc_client will use to read memory for the emulator. we don't need it yet, +// so just provide a dummy function that returns "no memory read". +static uint32_t read_memory_callback(uint32_t address, uint8_t *buffer, uint32_t num_bytes, rc_client_t *client) { // Achievements are traditionally defined relative to the base of main memory of the emulated console. // This is some kind of RetroArch-related legacy. In the PSP's case, this is simply a straight offset of 0x08000000. address += PSP_MEMORY_OFFSET; @@ -1942,8 +163,7 @@ unsigned Achievements::PeekMemory(unsigned address, unsigned num_bytes, void *ud if (!Memory::IsValidAddress(address)) { // Some achievement packs are really, really spammy. // So we'll just count the bad accesses. - g_stats.badMemoryAccessCount++; - + Achievements::g_stats.badMemoryAccessCount++; if (g_Config.bAchievementsLogBadMemReads) { WARN_LOG(G3D, "RetroAchievements PeekMemory: Bad address %08x (%d bytes)", address, num_bytes); } @@ -1951,10 +171,468 @@ unsigned Achievements::PeekMemory(unsigned address, unsigned num_bytes, void *ud } switch (num_bytes) { - case 1: return Memory::ReadUnchecked_U8(address); - case 2: return Memory::ReadUnchecked_U16(address); - case 4: return Memory::ReadUnchecked_U32(address); + case 1: + *buffer = Memory::ReadUnchecked_U8(address); + return 1; + case 2: { + uint16_t temp = Memory::ReadUnchecked_U16(address); + memcpy(buffer, &temp, 2); + return 2; + } + case 4: { + uint32_t temp = Memory::ReadUnchecked_U32(address); + memcpy(buffer, &temp, 4); + return 4; + } default: return 0; } } + +// This is the HTTP request dispatcher that is provided to the rc_client. Whenever the client +// needs to talk to the server, it will call this function. +static void server_call_callback(const rc_api_request_t *request, + rc_client_server_callback_t callback, void *callback_data, rc_client_t *client) +{ + // If post data is provided, we need to make a POST request, otherwise, a GET request will suffice. + if (request->post_data) { + g_DownloadManager.AsyncPostWithCallback(std::string(request->url), std::string(request->post_data), "application/x-www-form-urlencoded", [=](http::Download &download) { + std::string buffer; + download.buffer().TakeAll(&buffer); + rc_api_server_response_t response{}; + response.body = buffer.c_str(); + response.body_length = buffer.size(); + response.http_status_code = download.ResultCode(); + callback(&response, callback_data); + }); + } else { + g_DownloadManager.StartDownloadWithCallback(std::string(request->url), Path(), [=](http::Download &download) { + std::string buffer; + download.buffer().TakeAll(&buffer); + rc_api_server_response_t response{}; + response.body = buffer.c_str(); + response.body_length = buffer.size(); + response.http_status_code = download.ResultCode(); + callback(&response, callback_data); + }); + } +} + +// Write log messages to the console +static void log_message_callback(const char *message, const rc_client_t *client) { + INFO_LOG(ACHIEVEMENTS, "RetroAchievements log: %s", message); +} + +static void login_token_callback(int result, const char *error_message, rc_client_t *client, void *userdata) { + switch (result) { + case RC_OK: + OnAchievementsLoginStateChange(); + break; + case RC_INVALID_STATE: + case RC_API_FAILURE: + case RC_MISSING_VALUE: + case RC_INVALID_JSON: + ERROR_LOG(ACHIEVEMENTS, "Failure logging in via token: %d, %s", result, error_message); + OnAchievementsLoginStateChange(); + break; + } +} + +// For detailed documentation, see https://github.com/RetroAchievements/rcheevos/wiki/rc_client_set_event_handler. +static void event_handler_callback(const rc_client_event_t *event, rc_client_t *client) { + auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); + + switch (event->type) { + case RC_CLIENT_EVENT_ACHIEVEMENT_TRIGGERED: + // An achievement was earned by the player. The handler should notify the player that the achievement was earned. + g_OSD.ShowAchievementUnlocked(event->achievement->id); + INFO_LOG(ACHIEVEMENTS, "Achievement unlocked: '%s' (%d)", event->achievement->title, event->achievement->id); + break; + case RC_CLIENT_EVENT_GAME_COMPLETED: + { + // All achievements for the game have been earned. The handler should notify the player that the game was completed or mastered, depending on challenge mode. + auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); + + const rc_client_game_t *gameInfo = rc_client_get_game_info(g_rcClient); + + // TODO: Translation? + std::string title = ReplaceAll(ac->T("Mastered %1"), "%1", gameInfo->title); + rc_client_user_game_summary_t summary; + rc_client_get_user_game_summary(g_rcClient, &summary); + + std::string message = StringFromFormat(ac->T("%d achievements"), summary.num_unlocked_achievements); + + g_OSD.Show(OSDType::MESSAGE_INFO, title, message, gameInfo->badge_name, 10.0f); + + INFO_LOG(ACHIEVEMENTS, "%s", message.c_str()); + break; + } + case RC_CLIENT_EVENT_LEADERBOARD_STARTED: + // A leaderboard attempt has started. The handler may show a message with the leaderboard title and /or description indicating the attempt started. + INFO_LOG(ACHIEVEMENTS, "Leaderboard attempt started: %s", event->leaderboard->title); + g_OSD.Show(OSDType::MESSAGE_INFO, ReplaceAll(ac->T("%1: Leaderboard attempt started"), "%1", event->leaderboard->title), event->leaderboard->description, 3.0f); + break; + case RC_CLIENT_EVENT_LEADERBOARD_FAILED: + NOTICE_LOG(ACHIEVEMENTS, "Leaderboard attempt failed: %s", event->leaderboard->title); + g_OSD.Show(OSDType::MESSAGE_INFO, ReplaceAll(ac->T("%1: Leaderboard attempt failed"), "%1", event->leaderboard->title), 3.0f); + // A leaderboard attempt has failed. + break; + case RC_CLIENT_EVENT_LEADERBOARD_SUBMITTED: + NOTICE_LOG(ACHIEVEMENTS, "Leaderboard result submitted: %s", event->leaderboard->title); + g_OSD.Show(OSDType::MESSAGE_SUCCESS, ReplaceAll(ReplaceAll(ac->T("%1: Submitting leaderboard score: %2!"), "%1", event->leaderboard->title), "%2", event->leaderboard->tracker_value), event->leaderboard->description, 3.0f); + // A leaderboard attempt was completed.The handler may show a message with the leaderboard title and /or description indicating the final value being submitted to the server. + break; + case RC_CLIENT_EVENT_ACHIEVEMENT_CHALLENGE_INDICATOR_SHOW: + NOTICE_LOG(ACHIEVEMENTS, "Challenge indicator show: %s", event->achievement->title); + g_OSD.ShowChallengeIndicator(event->achievement->id, true); + // A challenge achievement has become active. The handler should show a small version of the achievement icon + // to indicate the challenge is active. + break; + case RC_CLIENT_EVENT_ACHIEVEMENT_CHALLENGE_INDICATOR_HIDE: + NOTICE_LOG(ACHIEVEMENTS, "Challenge indicator hide: %s", event->achievement->title); + g_OSD.ShowChallengeIndicator(event->achievement->id, false); + // The handler should hide the small version of the achievement icon that was shown by the corresponding RC_CLIENT_EVENT_ACHIEVEMENT_CHALLENGE_INDICATOR_SHOW event. + break; + case RC_CLIENT_EVENT_ACHIEVEMENT_PROGRESS_INDICATOR_SHOW: + NOTICE_LOG(ACHIEVEMENTS, "Progress indicator show: %s, progress: '%s' (%f)", event->achievement->title, event->achievement->measured_progress, event->achievement->measured_percent); + // An achievement that tracks progress has changed the amount of progress that has been made. + // The handler should show a small version of the achievement icon along with the achievement->measured_progress text (for two seconds). + // Only one progress indicator should be shown at a time. + // If a progress indicator is already visible, it should be updated with the new icon and text, and the two second timer should be restarted. + g_OSD.ShowAchievementProgress(event->achievement->id, 2.0f); + break; + case RC_CLIENT_EVENT_LEADERBOARD_TRACKER_SHOW: + NOTICE_LOG(ACHIEVEMENTS, "Leaderboard tracker show: '%s' (id %d)", event->leaderboard_tracker->display, event->leaderboard_tracker->id); + // A leaderboard_tracker has become active. The handler should show the tracker text on screen. + // Multiple active leaderboards may share a single tracker if they have the same definition and value. + // As such, the leaderboard tracker IDs are unique amongst the leaderboard trackers, and have no correlation to the active leaderboard(s). + // Use event->leaderboard_tracker->id for uniqueness checks, and display event->leaderboard_tracker->display (string) + g_OSD.ShowLeaderboardTracker(event->leaderboard_tracker->id, event->leaderboard_tracker->display, true); + break; + case RC_CLIENT_EVENT_LEADERBOARD_TRACKER_HIDE: + // A leaderboard_tracker has become inactive. The handler should hide the tracker text from the screen. + NOTICE_LOG(ACHIEVEMENTS, "Leaderboard tracker hide: '%s' (id %d)", event->leaderboard_tracker->display, event->leaderboard_tracker->id); + g_OSD.ShowLeaderboardTracker(event->leaderboard_tracker->id, nullptr, false); + break; + case RC_CLIENT_EVENT_LEADERBOARD_TRACKER_UPDATE: + // A leaderboard_tracker value has been updated. The handler should update the tracker text on the screen. + NOTICE_LOG(ACHIEVEMENTS, "Leaderboard tracker update: '%s' (id %d)", event->leaderboard_tracker->display, event->leaderboard_tracker->id); + g_OSD.ShowLeaderboardTracker(event->leaderboard_tracker->id, event->leaderboard_tracker->display, true); + break; + case RC_CLIENT_EVENT_RESET: + WARN_LOG(ACHIEVEMENTS, "Resetting game due to achievement setting change!"); + // Challenge mode was enabled, or something else that forces a game reset. + System_PostUIMessage("reset", ""); + break; + case RC_CLIENT_EVENT_SERVER_ERROR: + ERROR_LOG(ACHIEVEMENTS, "Server error: %s: %s", event->server_error->api, event->server_error->error_message); + g_OSD.Show(OSDType::MESSAGE_ERROR, "Server error"); + break; + default: + WARN_LOG(ACHIEVEMENTS, "Unhandled rc_client event %d, ignoring", event->type); + break; + } +} + +void Initialize() { + if (!g_Config.bAchievementsEnable) { + _dbg_assert_(!g_rcClient); + INFO_LOG(ACHIEVEMENTS, "Achievements are disabled, not initializing."); + return; + } + _assert_msg_(g_Config.bAchievementsEnable, "Achievements are enabled"); + + g_rcClient = rc_client_create(read_memory_callback, server_call_callback); + // Provide a logging function to simplify debugging + rc_client_enable_logging(g_rcClient, RC_CLIENT_LOG_LEVEL_VERBOSE, log_message_callback); + + // Disable SSL for now. + rc_client_set_host(g_rcClient, "http://retroachievements.org"); + + rc_client_set_event_handler(g_rcClient, event_handler_callback); + + std::string api_token = NativeLoadSecret(RA_TOKEN_SECRET_NAME); + if (!api_token.empty()) { + rc_client_begin_login_with_token(g_rcClient, g_Config.sAchievementsUserName.c_str(), api_token.c_str(), &login_token_callback, nullptr); + } +} + +static void login_password_callback(int result, const char *error_message, rc_client_t *client, void *userdata) { + switch (result) { + case RC_OK: + { + // Get the token and store it. + const rc_client_user_t *user = rc_client_get_user_info(client); + g_Config.sAchievementsUserName = user->username; + NativeSaveSecret(RA_TOKEN_SECRET_NAME, std::string(user->token)); + OnAchievementsLoginStateChange(); + break; + } + case RC_INVALID_STATE: + case RC_API_FAILURE: + case RC_MISSING_VALUE: + case RC_INVALID_JSON: + ERROR_LOG(ACHIEVEMENTS, "Failure logging in via token: %d, %s", result, error_message); + OnAchievementsLoginStateChange(); + break; + } + + OSDCloseBackgroundProgressDialog("cheevos_async_login"); +} + +bool LoginAsync(const char *username, const char *password) { + if (IsLoggedIn() || std::strlen(username) == 0 || std::strlen(password) == 0 || IsUsingRAIntegration()) + return false; + + OSDOpenBackgroundProgressDialog("cheevos_async_login", "Logging in to RetroAchivements...", 0, 0, 0); + rc_client_begin_login_with_password(g_rcClient, username, password, &login_password_callback, nullptr); + return true; +} + +void Logout() { + rc_client_logout(g_rcClient); + // remove from config + g_Config.sAchievementsUserName.clear(); + NativeSaveSecret(RA_TOKEN_SECRET_NAME, ""); + g_Config.Save("Achievements logout"); +} + +void UpdateSettings() { + if (!g_Config.bAchievementsEnable) { + // we're done here + Shutdown(); + return; + } + + if (!g_rcClient) { + // we just got enabled. + Initialize(); + } +} + +bool Shutdown() { + rc_client_destroy(g_rcClient); + g_rcClient = nullptr; + return true; +} + +void ResetRuntime() { + INFO_LOG(ACHIEVEMENTS, "Resetting rcheevos state..."); + rc_client_reset(g_rcClient); +} + +void FrameUpdate() { + if (!g_rcClient) + return; + rc_client_do_frame(g_rcClient); +} + +void Idle() { + rc_client_idle(g_rcClient); +} + +void DoState(PointerWrap &p) { + auto sw = p.Section("Achievements", 0, 1); + if (!sw) { + // Save state is missing the section. + // Reset the runtime. + if (HasAchievementsOrLeaderboards()) { + auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); + g_OSD.Show(OSDType::MESSAGE_WARNING, ac->T("Save state loaded without achievement data"), 5.0f); + } + rc_client_reset(g_rcClient); + return; + } + + uint32_t data_size = 0; + + if (!IsActive()) { + Do(p, data_size); + if (p.mode == PointerWrap::MODE_READ) { + WARN_LOG(ACHIEVEMENTS, "Save state contained achievement data, but achievements are not active. Ignore."); + } + p.SkipBytes(data_size); + return; + } + + if (p.mode == PointerWrap::MODE_MEASURE || p.mode == PointerWrap::MODE_WRITE || p.mode == PointerWrap::MODE_VERIFY || p.mode == PointerWrap::MODE_NOOP) { + data_size = (uint32_t)(g_rcClient ? rc_client_progress_size(g_rcClient) : 0); + } + Do(p, data_size); + + if (data_size > 0) { + uint8_t *buffer = new uint8_t[data_size]; + switch (p.mode) { + case PointerWrap::MODE_NOOP: + case PointerWrap::MODE_MEASURE: + case PointerWrap::MODE_WRITE: + case PointerWrap::MODE_VERIFY: + { + int retval = rc_client_serialize_progress(g_rcClient, buffer); + if (retval != RC_OK) { + ERROR_LOG(ACHIEVEMENTS, "Error %d serializing achievement data. Ignoring.", retval); + } + break; + } + } + + DoArray(p, buffer, data_size); + + switch (p.mode) { + case PointerWrap::MODE_READ: + { + int retval = rc_client_deserialize_progress(g_rcClient, buffer); + if (retval != RC_OK) { + // TODO: What should we really do here? + ERROR_LOG(ACHIEVEMENTS, "Error %d deserializing achievement data. Ignoring.", retval); + } + break; + } + } + delete[] buffer; + } else { + if (IsActive()) { + auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); + g_OSD.Show(OSDType::MESSAGE_WARNING, ac->T("Save state loaded without achievement data"), 5.0f); + } + rc_client_reset(g_rcClient); + } +} + +bool HasAchievementsOrLeaderboards() { + if (!g_rcClient) { + return false; + } + return IsActive(); +} + +void DownloadImageIfMissing(const std::string &cache_key, std::string &&url) { + if (g_iconCache.MarkPending(cache_key)) { + INFO_LOG(ACHIEVEMENTS, "Downloading image: %s (%s)", url.c_str(), cache_key.c_str()); + g_DownloadManager.StartDownloadWithCallback(url, Path(), [cache_key](http::Download &download) { + if (download.ResultCode() != 200) + return; + std::string data; + download.buffer().TakeAll(&data); + g_iconCache.InsertIcon(cache_key, IconFormat::PNG, std::move(data)); + }); + } +} + +Statistics GetStatistics() { + return g_stats; +} + +std::string GetGameAchievementSummary() { + auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); + + rc_client_user_game_summary_t summary; + rc_client_get_user_game_summary(g_rcClient, &summary); + + std::string summaryString = StringFromFormat(ac->T("Earned", "You have unlocked %d of %d achievements, earning %d of %d points"), + summary.num_unlocked_achievements, summary.num_core_achievements + summary.num_unofficial_achievements, + summary.points_unlocked, summary.points_core); + if (ChallengeModeActive()) { + summaryString.append("\n"); + summaryString.append(ac->T("Challenge Mode")); + } + if (EncoreModeActive()) { + summaryString.append("\n"); + summaryString.append(ac->T("Encore Mode")); + } + return summaryString; +} + +void identify_and_load_callback(int result, const char *error_message, rc_client_t *client, void *userdata) { + auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); + + NOTICE_LOG(ACHIEVEMENTS, "Load callback: %d (%s)", result, error_message); + + switch (result) { + case RC_OK: + { + // Successful! Show a message that we're active. + const rc_client_game_t *gameInfo = rc_client_get_game_info(client); + + char cacheId[128]; + snprintf(cacheId, sizeof(cacheId), "gi:%s", gameInfo->badge_name); + + char temp[512]; + if (RC_OK == rc_client_game_get_image_url(gameInfo, temp, sizeof(temp))) { + Achievements::DownloadImageIfMissing(cacheId, std::move(std::string(temp))); + } + g_OSD.Show(OSDType::MESSAGE_INFO, std::string(gameInfo->title), GetGameAchievementSummary(), cacheId, 5.0f); + break; + } + case RC_NO_GAME_LOADED: + // The current game does not support achievements. + g_OSD.Show(OSDType::MESSAGE_INFO, ac->T("This game has no achievements"), 3.0f); + break; + default: + // Other various errors. + ERROR_LOG(ACHIEVEMENTS, "Failed to identify/load game: %d (%s)", result, error_message); + break; + } + + g_isIdentifying = false; +} + +void SetGame(const Path &path) { + if (!g_rcClient || !IsLoggedIn()) { + // Nothing to do. + return; + } + + rc_hash_filereader rc_filereader; + rc_filereader.open = [](const char *utf8Path) { + Path path(utf8Path); + FILE *f = File::OpenCFile(path, "rb"); + return (void *)f; + }; + rc_filereader.seek = [](void *file_handle, int64_t offset, int origin) { fseek((FILE *)file_handle, offset, origin); }; + rc_filereader.tell = [](void *file_handle) -> int64_t { return (int64_t)ftell((FILE *)file_handle); }; + rc_filereader.read = [](void *file_handle, void *buffer, size_t requested_bytes) -> size_t { return fread(buffer, 1, requested_bytes, (FILE *)file_handle); }; + rc_filereader.close = [](void *file_handle) { fclose((FILE *)file_handle); }; + + // The caller should hold off on executing game code until this turns false, checking with IsBlockingExecution() + g_isIdentifying = true; + + // Apply pre-load settings. + rc_client_set_hardcore_enabled(g_rcClient, g_Config.bAchievementsChallengeMode ? 1 : 0); + rc_client_set_encore_mode_enabled(g_rcClient, g_Config.bAchievementsEncoreMode ? 1 : 0); + rc_client_set_unofficial_enabled(g_rcClient, g_Config.bAchievementsUnofficial ? 1 : 0); + + rc_hash_init_custom_filereader(&rc_filereader); + rc_hash_init_default_cdreader(); + rc_client_begin_identify_and_load_game(g_rcClient, RC_CONSOLE_PSP, path.c_str(), nullptr, 0, &identify_and_load_callback, nullptr); +} + +void UnloadGame() { + if (g_rcClient) { + rc_client_unload_game(g_rcClient); + } +} + +void change_media_callback(int result, const char *error_message, rc_client_t *client, void *userdata) { + NOTICE_LOG(ACHIEVEMENTS, "Change media callback: %d (%s)", result, error_message); + g_isIdentifying = false; +} + +void ChangeUMD(const Path &path) { + if (!IsActive()) { + // Nothing to do. + return; + } + + rc_client_begin_change_media(g_rcClient, + path.c_str(), + nullptr, + 0, + &change_media_callback, + nullptr + ); + + g_isIdentifying = true; +} + +} // namespace Achievements diff --git a/Core/RetroAchievements.h b/Core/RetroAchievements.h index 5a701b5482..10e219dfe7 100644 --- a/Core/RetroAchievements.h +++ b/Core/RetroAchievements.h @@ -9,9 +9,6 @@ #pragma once -#include "Common/StringUtils.h" -#include "Common/CommonTypes.h" - #include #include #include @@ -19,54 +16,17 @@ #include #include +#include "Common/StringUtils.h" +#include "Common/CommonTypes.h" + +#include "ext/rcheevos/include/rc_client.h" + class Path; class PointerWrap; namespace Achievements { -enum class AchievementCategory : u8 -{ - Local = 0, - Core = 3, - Unofficial = 5 -}; - -struct Achievement -{ - u32 id; - std::string title; - std::string description; - std::string memaddr; - std::string badge_name; - - u32 points; - AchievementCategory category; - bool locked; - bool active; - bool primed; - bool disabled; // due to bad memory access, presumably -}; - -struct Leaderboard -{ - u32 id; - std::string title; - std::string description; - int format; - bool hidden; -}; - -struct LeaderboardEntry -{ - std::string user; - std::string formatted_score; - time_t submitted; - u32 rank; - bool is_self; -}; - -struct Statistics -{ +struct Statistics { // Debug stats int badMemoryAccessCount; }; @@ -85,101 +45,64 @@ static inline bool IsUsingRAIntegration() #endif -bool IsActive(); +// Returns true if the user is logged in properly, and everything is set up for playing games with achievements. bool IsLoggedIn(); + +// Returns true if in a game, and achievements are active in the current game. +bool IsActive(); + +// Returns true if unofficial achievements are enabled. +bool UnofficialEnabled(); + +// Returns true if the emulator should hold off on executing game code, such as during game identification. +bool IsBlockingExecution(); + +// Returns true if features such as save states should be disabled. +// This should only be used for controlling functionality of the following things, which are banned in Challenge/Hardcore mode: +// +// * Savestates +// * Slowdown time (though hard to fully prevent, could use crazy post shaders or software rendering...) bool ChallengeModeActive(); -bool LeaderboardsActive(); -bool IsTestModeActive(); -bool IsUnofficialTestModeActive(); -bool IsRichPresenceEnabled(); -bool HasActiveGame(); -u32 GetGameID(); +// Same as ChallengeModeActive but comes with a convenient user message. Don't use for every-frame checks or UI enablement, +// only for shortcut keys and commands. You should look up the message in I18NCat::ACHIEVEMENTS. +// If no message is specified, a standard "This feature is not available in Challenge Mode" message will be shown. +// Also returns true if challenge mode is active. +bool WarnUserIfChallengeModeActive(const char *message = nullptr); -/// Acquires the achievements lock. Must be held when accessing any achievement state from another thread. -std::unique_lock GetLock(); +// The new API is so much nicer that we can use it directly instead of wrapping it. So let's expose the client. +// Will of course return nullptr if not active. +rc_client_t *GetClient(); void Initialize(); void UpdateSettings(); -/// Called when the system is being reset. If it returns false, the reset should be aborted. -bool ConfirmSystemReset(); - /// Called when the system is being shut down. If Shutdown() returns false, the shutdown should be aborted if possible. bool Shutdown(); -/// Called once a frame at vsync time on the CPU thread. +void DownloadImageIfMissing(const std::string &cache_key, std::string &&url); + +/// Called once a frame at vsync time on the CPU thread, during gameplay. void FrameUpdate(); -/// Called when the system is paused, because FrameUpdate() won't be getting called. -void ProcessPendingHTTPRequests(); +/// Called every frame to let background processing happen. +void Idle(); /// Saves/loads state. -bool DoState(PointerWrap &sw); +void DoState(PointerWrap &p); /// Returns true if the current game has any achievements or leaderboards. -/// Does not need to have the lock held. -bool SafeHasAchievementsOrLeaderboards(); - -const std::string &GetUsername(); -const std::string &GetRichPresenceString(); +bool HasAchievementsOrLeaderboards(); bool LoginAsync(const char *username, const char *password); void Logout(); -void GameChanged(const Path &path); -void LeftGame(); - -/// Re-enables hardcode mode if it is enabled in the settings. -bool ResetChallengeMode(); - -/// Forces hardcore mode off until next reset. -void DisableChallengeMode(); - -/// Prompts the user to disable hardcore mode, if they agree, returns true. -bool ConfirmChallengeModeDisable(const char *trigger); - -/// Returns true if features such as save states should be disabled. -bool ChallengeModeActive(); - -const std::string &GetGameTitle(); -const std::string &GetGameIcon(); - -bool EnumerateAchievements(std::function callback); - -// TODO: Make these support multiple games, not just the current games, with cached info. -u32 GetUnlockedAchiementCount(); -u32 GetAchievementCount(); -u32 GetMaximumPointsForGame(); -u32 GetCurrentPointsForGame(); +void SetGame(const Path &path); +void ChangeUMD(const Path &path); // for in-game UMD change +void UnloadGame(); // Call when leaving a game. Statistics GetStatistics(); -bool EnumerateLeaderboards(std::function callback); - -// Unlike most other functions here, this you're supposed to poll until you get a valid std::optional. -std::optional TryEnumerateLeaderboardEntries(u32 id, std::function callback); -const Leaderboard *GetLeaderboardByID(u32 id); -u32 GetLeaderboardCount(); -bool IsLeaderboardTimeType(const Leaderboard &leaderboard); -u32 GetPrimedAchievementCount(); - -const Achievement *GetAchievementByID(u32 id); -std::pair GetAchievementProgress(const Achievement &achievement); std::string GetGameAchievementSummary(); -std::string GetAchievementProgressText(const Achievement &achievement); -std::string GetAchievementBadgePath(const Achievement &achievement, bool download_if_missing = true, - bool force_unlocked_icon = false); -std::string GetAchievementBadgeURL(const Achievement &achievement); -#ifdef WITH_RAINTEGRATION -void SwitchToRAIntegration(); - -namespace RAIntegration { -void MainWindowChanged(void *new_handle); -void GameChanged(); -std::vector> GetMenuItems(); -void ActivateMenuItem(int item); -} // namespace RAIntegration -#endif } // namespace Achievements diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index bc68cebd40..99ba2e3e06 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -47,6 +47,7 @@ #include "Core/MemMap.h" #include "Core/MIPS/MIPS.h" #include "Core/MIPS/JitCommon/JitBlockCache.h" +#include "Core/RetroAchievements.h" #include "HW/MemoryStick.h" #include "GPU/GPUState.h" @@ -391,12 +392,18 @@ namespace SaveState currentMIPS->DoState(p); HLEDoState(p); __KernelDoState(p); + Achievements::DoState(p); // Kernel object destructors might close open files, so do the filesystem last. pspFileSystem.DoState(p); } void Enqueue(SaveState::Operation op) { + if (Achievements::ChallengeModeActive()) { + // No savestate operations are permitted, let's just ignore it. + return; + } + std::lock_guard guard(mutex); pending.push_back(op); diff --git a/UI/DevScreens.cpp b/UI/DevScreens.cpp index 2c056115a4..9d912015af 100644 --- a/UI/DevScreens.cpp +++ b/UI/DevScreens.cpp @@ -830,7 +830,20 @@ void SystemInfoScreen::CreateTabs() { return UI::EVENT_DONE; }); internals->Add(new Choice(si->T("Clear")))->OnClick.Add([&](UI::EventParams &) { - g_OSD.RemoveProgressBar("testprogress", 0.25f); + g_OSD.RemoveProgressBar("testprogress"); + return UI::EVENT_DONE; + }); + internals->Add(new ItemHeader(si->T("Achievement tests"))); + internals->Add(new Choice(si->T("Leaderboard tracker: Show")))->OnClick.Add([=](UI::EventParams &) { + g_OSD.ShowLeaderboardTracker(1, "My leaderboard tracker", true); + return UI::EVENT_DONE; + }); + internals->Add(new Choice(si->T("Leaderboard tracker: Update")))->OnClick.Add([=](UI::EventParams &) { + g_OSD.ShowLeaderboardTracker(1, "Updated tracker", true); + return UI::EVENT_DONE; + }); + internals->Add(new Choice(si->T("Leaderboard tracker: Hide")))->OnClick.Add([=](UI::EventParams &) { + g_OSD.ShowLeaderboardTracker(1, nullptr, false); return UI::EVENT_DONE; }); #if PPSSPP_PLATFORM(ANDROID) diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index 058dead585..efb114cda5 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -348,7 +348,7 @@ void EmuScreen::bootGame(const Path &filename) { g_OSD.Show(OSDType::MESSAGE_WARNING, gr->T("DefaultCPUClockRequired", "Warning: This game requires the CPU clock to be set to default."), 10.0f); } - Achievements::GameChanged(filename); + Achievements::SetGame(filename); loadingViewColor_->Divert(0xFFFFFFFF, 0.75f); loadingViewVisible_->Divert(UI::V_VISIBLE, 0.75f); @@ -405,6 +405,7 @@ void EmuScreen::bootComplete() { EmuScreen::~EmuScreen() { if (!invalid_ || bootPending_) { // If we were invalid, it would already be shutdown. + Achievements::UnloadGame(); PSP_Shutdown(); } @@ -581,7 +582,7 @@ void EmuScreen::onVKey(int virtualKeyCode, bool down) { break; case VIRTKEY_SPEED_TOGGLE: - if (down) { + if (down && !Achievements::WarnUserIfChallengeModeActive()) { // Cycle through enabled speeds. if (PSP_CoreParameter().fpsLimit == FPSLimit::NORMAL && g_Config.iFpsLimit1 >= 0) { PSP_CoreParameter().fpsLimit = FPSLimit::CUSTOM1; @@ -597,28 +598,32 @@ void EmuScreen::onVKey(int virtualKeyCode, bool down) { break; case VIRTKEY_SPEED_CUSTOM1: - if (down) { - if (PSP_CoreParameter().fpsLimit == FPSLimit::NORMAL) { - PSP_CoreParameter().fpsLimit = FPSLimit::CUSTOM1; - g_OSD.Show(OSDType::MESSAGE_INFO, sc->T("fixed", "Speed: alternate"), 1.0); - } - } else { - if (PSP_CoreParameter().fpsLimit == FPSLimit::CUSTOM1) { - PSP_CoreParameter().fpsLimit = FPSLimit::NORMAL; - g_OSD.Show(OSDType::MESSAGE_INFO, sc->T("standard", "Speed: standard"), 1.0); + if (!Achievements::WarnUserIfChallengeModeActive()) { + if (down) { + if (PSP_CoreParameter().fpsLimit == FPSLimit::NORMAL) { + PSP_CoreParameter().fpsLimit = FPSLimit::CUSTOM1; + g_OSD.Show(OSDType::MESSAGE_INFO, sc->T("fixed", "Speed: alternate"), 1.0); + } + } else { + if (PSP_CoreParameter().fpsLimit == FPSLimit::CUSTOM1) { + PSP_CoreParameter().fpsLimit = FPSLimit::NORMAL; + g_OSD.Show(OSDType::MESSAGE_INFO, sc->T("standard", "Speed: standard"), 1.0); + } } } break; case VIRTKEY_SPEED_CUSTOM2: - if (down) { - if (PSP_CoreParameter().fpsLimit == FPSLimit::NORMAL) { - PSP_CoreParameter().fpsLimit = FPSLimit::CUSTOM2; - g_OSD.Show(OSDType::MESSAGE_INFO, sc->T("SpeedCustom2", "Speed: alternate 2"), 1.0); - } - } else { - if (PSP_CoreParameter().fpsLimit == FPSLimit::CUSTOM2) { - PSP_CoreParameter().fpsLimit = FPSLimit::NORMAL; - g_OSD.Show(OSDType::MESSAGE_INFO, sc->T("standard", "Speed: standard"), 1.0); + if (!Achievements::WarnUserIfChallengeModeActive()) { + if (down) { + if (PSP_CoreParameter().fpsLimit == FPSLimit::NORMAL) { + PSP_CoreParameter().fpsLimit = FPSLimit::CUSTOM2; + g_OSD.Show(OSDType::MESSAGE_INFO, sc->T("SpeedCustom2", "Speed: alternate 2"), 1.0); + } + } else { + if (PSP_CoreParameter().fpsLimit == FPSLimit::CUSTOM2) { + PSP_CoreParameter().fpsLimit = FPSLimit::NORMAL; + g_OSD.Show(OSDType::MESSAGE_INFO, sc->T("standard", "Speed: standard"), 1.0); + } } } break; @@ -634,13 +639,15 @@ void EmuScreen::onVKey(int virtualKeyCode, bool down) { break; case VIRTKEY_FRAME_ADVANCE: - if (down) { - // If game is running, pause emulation immediately. Otherwise, advance a single frame. - if (Core_IsStepping()) { - frameStep_ = true; - Core_EnableStepping(false); - } else if (!frameStep_) { - Core_EnableStepping(true, "ui.frameAdvance", 0); + if (!Achievements::WarnUserIfChallengeModeActive()) { + if (down) { + // If game is running, pause emulation immediately. Otherwise, advance a single frame. + if (Core_IsStepping()) { + frameStep_ = true; + Core_EnableStepping(false); + } else if (!frameStep_) { + Core_EnableStepping(true, "ui.frameAdvance", 0); + } } } break; @@ -690,7 +697,7 @@ void EmuScreen::onVKey(int virtualKeyCode, bool down) { #endif case VIRTKEY_REWIND: - if (down) { + if (down && !Achievements::WarnUserIfChallengeModeActive()) { if (SaveState::CanRewind()) { SaveState::Rewind(&AfterSaveStateAction); } else { @@ -699,21 +706,21 @@ void EmuScreen::onVKey(int virtualKeyCode, bool down) { } break; case VIRTKEY_SAVE_STATE: - if (down) + if (down && !Achievements::WarnUserIfChallengeModeActive()) SaveState::SaveSlot(gamePath_, g_Config.iCurrentStateSlot, &AfterSaveStateAction); break; case VIRTKEY_LOAD_STATE: - if (down) + if (down && !Achievements::WarnUserIfChallengeModeActive()) SaveState::LoadSlot(gamePath_, g_Config.iCurrentStateSlot, &AfterSaveStateAction); break; case VIRTKEY_PREVIOUS_SLOT: - if (down) { + if (down && !Achievements::WarnUserIfChallengeModeActive()) { SaveState::PrevSlot(); System_PostUIMessage("savestate_displayslot", ""); } break; case VIRTKEY_NEXT_SLOT: - if (down) { + if (down && !Achievements::WarnUserIfChallengeModeActive()) { SaveState::NextSlot(); System_PostUIMessage("savestate_displayslot", ""); } @@ -1494,50 +1501,52 @@ void EmuScreen::render() { Core_UpdateDebugStats(g_Config.bShowDebugStats || g_Config.bLogFrameDrops); - PSP_BeginHostFrame(); + bool blockedExecution = Achievements::IsBlockingExecution(); + if (!blockedExecution) { + PSP_BeginHostFrame(); + PSP_RunLoopWhileState(); - PSP_RunLoopWhileState(); - - // Hopefully coreState is now CORE_NEXTFRAME - switch (coreState) { - case CORE_NEXTFRAME: - // Reached the end of the frame, all good. Set back to running for the next frame - coreState = CORE_RUNNING; - break; - case CORE_STEPPING: - case CORE_RUNTIME_ERROR: - { - // If there's an exception, display information. - const MIPSExceptionInfo &info = Core_GetExceptionInfo(); - if (info.type != MIPSExceptionType::NONE) { - // Clear to blue background screen - bool dangerousSettings = !Reporting::IsSupported(); - uint32_t color = dangerousSettings ? 0xFF900050 : 0xFF900000; - thin3d->BindFramebufferAsRenderTarget(nullptr, { RPAction::CLEAR, RPAction::DONT_CARE, RPAction::DONT_CARE, color }, "EmuScreen_RuntimeError"); - // The info is drawn later in renderUI - } else { - // If we're stepping, it's convenient not to clear the screen entirely, so we copy display to output. - // This won't work in non-buffered, but that's fine. - thin3d->BindFramebufferAsRenderTarget(nullptr, { RPAction::CLEAR, RPAction::DONT_CARE, RPAction::DONT_CARE }, "EmuScreen_Stepping"); - // Just to make sure. - if (PSP_IsInited()) { - gpu->CopyDisplayToOutput(true); + // Hopefully coreState is now CORE_NEXTFRAME + switch (coreState) { + case CORE_NEXTFRAME: + // Reached the end of the frame, all good. Set back to running for the next frame + coreState = CORE_RUNNING; + break; + case CORE_STEPPING: + case CORE_RUNTIME_ERROR: + { + // If there's an exception, display information. + const MIPSExceptionInfo &info = Core_GetExceptionInfo(); + if (info.type != MIPSExceptionType::NONE) { + // Clear to blue background screen + bool dangerousSettings = !Reporting::IsSupported(); + uint32_t color = dangerousSettings ? 0xFF900050 : 0xFF900000; + thin3d->BindFramebufferAsRenderTarget(nullptr, { RPAction::CLEAR, RPAction::DONT_CARE, RPAction::DONT_CARE, color }, "EmuScreen_RuntimeError"); + // The info is drawn later in renderUI + } else { + // If we're stepping, it's convenient not to clear the screen entirely, so we copy display to output. + // This won't work in non-buffered, but that's fine. + thin3d->BindFramebufferAsRenderTarget(nullptr, { RPAction::CLEAR, RPAction::DONT_CARE, RPAction::DONT_CARE }, "EmuScreen_Stepping"); + // Just to make sure. + if (PSP_IsInited()) { + gpu->CopyDisplayToOutput(true); + } } + break; + } + default: + // Didn't actually reach the end of the frame, ran out of the blockTicks cycles. + // In this case we need to bind and wipe the backbuffer, at least. + // It's possible we never ended up outputted anything - make sure we have the backbuffer cleared + thin3d->BindFramebufferAsRenderTarget(nullptr, { RPAction::CLEAR, RPAction::CLEAR, RPAction::CLEAR }, "EmuScreen_NoFrame"); + break; } - break; - } - default: - // Didn't actually reach the end of the frame, ran out of the blockTicks cycles. - // In this case we need to bind and wipe the backbuffer, at least. - // It's possible we never ended up outputted anything - make sure we have the backbuffer cleared - thin3d->BindFramebufferAsRenderTarget(nullptr, { RPAction::CLEAR, RPAction::CLEAR, RPAction::CLEAR }, "EmuScreen_NoFrame"); - break; - } - PSP_EndHostFrame(); + PSP_EndHostFrame(); - // This must happen after PSP_EndHostFrame so that things like push buffers are end-frame'd before we start destroying stuff. - checkPowerDown(); + // This must happen after PSP_EndHostFrame so that things like push buffers are end-frame'd before we start destroying stuff. + checkPowerDown(); + } if (invalid_) return; diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index a5010fffa6..8e23a3ad93 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -1206,7 +1206,8 @@ void NativeUpdate() { g_requestManager.ProcessRequests(); - Achievements::ProcessPendingHTTPRequests(); + // it's ok to call this redundantly with DoFrame from EmuScreen + Achievements::Idle(); g_DownloadManager.Update(); g_screenManager->update(); diff --git a/UI/OnScreenDisplay.cpp b/UI/OnScreenDisplay.cpp index 9d921b666f..2d6f3bac65 100644 --- a/UI/OnScreenDisplay.cpp +++ b/UI/OnScreenDisplay.cpp @@ -53,8 +53,10 @@ static NoticeLevel GetNoticeLevel(OSDType type) { } } +// Align only matters here for the ASCII-only flag. static void MeasureNotice(const UIContext &dc, NoticeLevel level, const std::string &text, const std::string &details, const std::string &iconName, int align, float *width, float *height, float *height1) { dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text.c_str(), width, height, align); + *height1 = *height; float width2 = 0.0f, height2 = 0.0f; @@ -85,8 +87,8 @@ static void MeasureNotice(const UIContext &dc, NoticeLevel level, const std::str // Align only matters here for the ASCII-only flag. static void MeasureOSDEntry(const UIContext &dc, const OnScreenDisplay::Entry &entry, int align, float *width, float *height, float *height1) { if (entry.type == OSDType::ACHIEVEMENT_UNLOCKED) { - const Achievements::Achievement *achievement = Achievements::GetAchievementByID(entry.numericID); - MeasureAchievement(dc, *achievement, width, height); + const rc_client_achievement_t *achievement = rc_client_get_achievement_info(Achievements::GetClient(), entry.numericID); + MeasureAchievement(dc, achievement, AchievementRenderStyle::UNLOCKED, width, height); *width = 550.0f; *height1 = *height; } else { @@ -99,10 +101,7 @@ static void RenderNotice(UIContext &dc, Bounds bounds, float height1, NoticeLeve uint32_t foreGround = whiteAlpha(alpha); - Bounds shadowBounds = bounds.Expand(10.0f); - - dc.Draw()->DrawImage4Grid(dc.theme->dropShadow4Grid, shadowBounds.x, shadowBounds.y + 4.0f, shadowBounds.x2(), shadowBounds.y2(), alphaMul(0xFF000000, 0.9f * alpha), 1.0f); - + dc.DrawRectDropShadow(bounds, 12.0f, 0.7f * alpha); dc.FillRect(background, bounds); ImageID iconID = GetOSDIcon(level); @@ -143,12 +142,14 @@ static void RenderNotice(UIContext &dc, Bounds bounds, float height1, NoticeLeve static void RenderOSDEntry(UIContext &dc, const OnScreenDisplay::Entry &entry, Bounds bounds, float height1, int align, float alpha) { if (entry.type == OSDType::ACHIEVEMENT_UNLOCKED) { - const Achievements::Achievement *achievement = Achievements::GetAchievementByID(entry.numericID); - RenderAchievement(dc, *achievement, AchievementRenderStyle::UNLOCKED, bounds, alpha, entry.startTime, time_now_d()); + const rc_client_achievement_t * achievement = rc_client_get_achievement_info(Achievements::GetClient(), entry.numericID); + if (achievement) { + RenderAchievement(dc, achievement, AchievementRenderStyle::UNLOCKED, bounds, alpha, entry.startTime, time_now_d()); + } return; + } else { + RenderNotice(dc, bounds, height1, GetNoticeLevel(entry.type), entry.text, entry.text2, entry.iconName, align, alpha); } - - RenderNotice(dc, bounds, height1, GetNoticeLevel(entry.type), entry.text, entry.text2, entry.iconName, align, alpha); } static void MeasureOSDProgressBar(const UIContext &dc, const OnScreenDisplay::ProgressBar &bar, float *width, float *height) { @@ -159,9 +160,7 @@ static void MeasureOSDProgressBar(const UIContext &dc, const OnScreenDisplay::Pr static void RenderOSDProgressBar(UIContext &dc, const OnScreenDisplay::ProgressBar &entry, Bounds bounds, int align, float alpha) { uint32_t foreGround = whiteAlpha(alpha); - Bounds shadowBounds = bounds.Expand(10.0f); - - dc.Draw()->DrawImage4Grid(dc.theme->dropShadow4Grid, shadowBounds.x, shadowBounds.y + 4.0f, shadowBounds.x2(), shadowBounds.y2(), alphaMul(0xFF000000, 0.9f * alpha), 1.0f); + dc.DrawRectDropShadow(bounds, 12.0f, 0.7f * alpha); uint32_t backgroundColor = colorAlpha(0x806050, alpha); uint32_t progressBackgroundColor = colorAlpha(0xa08070, alpha); @@ -197,6 +196,23 @@ static void RenderOSDProgressBar(UIContext &dc, const OnScreenDisplay::ProgressB dc.DrawTextShadowRect(entry.message.c_str(), bounds, colorAlpha(0xFFFFFFFF, alpha), (align & FLAG_DYNAMIC_ASCII) | ALIGN_CENTER); } +static void MeasureLeaderboardTracker(UIContext &dc, const std::string &text, float *width, float *height) { + dc.MeasureText(dc.GetFontStyle(), 1.0f, 1.0f, text.c_str(), width, height); + *width += 10.0f; + *height += 10.0f; +} + +static void RenderLeaderboardTracker(UIContext &dc, const Bounds &bounds, const std::string &text, float alpha) { + // TODO: Awful color. + uint32_t backgroundColor = colorAlpha(0x806050, alpha); + UI::Drawable background = UI::Drawable(backgroundColor); + dc.DrawRectDropShadow(bounds, 12.0f, 0.7f * alpha); + dc.FillRect(background, bounds); + dc.SetFontStyle(dc.theme->uiFont); + dc.SetFontScale(1.0f, 1.0f); + dc.DrawTextShadowRect(text.c_str(), bounds.Inset(5.0f, 5.0f), colorAlpha(0xFFFFFFFF, alpha), ALIGN_VCENTER | ALIGN_LEFT); +} + void OnScreenMessagesView::Draw(UIContext &dc) { if (!g_Config.bShowOnScreenMessages) { return; @@ -206,11 +222,73 @@ void OnScreenMessagesView::Draw(UIContext &dc) { double now = time_now_d(); + float y = 10.0f; + + const float fadeoutCoef = 1.0f / OnScreenDisplay::FadeoutTime(); + + // Draw side entries. Top entries should apply on top of them if there's a collision, so drawing + // these first makes sense. + const std::vector sideEntries = g_OSD.SideEntries(); + for (auto &entry : sideEntries) { + float tw, th; + + const rc_client_achievement_t *achievement = nullptr; + AchievementRenderStyle style; + + switch (entry.type) { + case OSDType::ACHIEVEMENT_PROGRESS: + { + achievement = rc_client_get_achievement_info(Achievements::GetClient(), entry.numericID); + if (!achievement) + continue; + style = AchievementRenderStyle::PROGRESS_INDICATOR; + MeasureAchievement(dc, achievement, style, &tw, &th); + break; + } + case OSDType::ACHIEVEMENT_CHALLENGE_INDICATOR: + { + achievement = rc_client_get_achievement_info(Achievements::GetClient(), entry.numericID); + if (!achievement) + continue; + style = AchievementRenderStyle::CHALLENGE_INDICATOR; + MeasureAchievement(dc, achievement, style, &tw, &th); + break; + } + case OSDType::LEADERBOARD_TRACKER: + { + MeasureLeaderboardTracker(dc, entry.text, &tw, &th); + break; + } + default: + continue; + } + Bounds b(10.0f, y, tw, th); + float alpha = Clamp((float)(entry.endTime - now) * fadeoutCoef, 0.0f, 1.0f); + // OK, render the thing. + + switch (entry.type) { + case OSDType::ACHIEVEMENT_PROGRESS: + case OSDType::ACHIEVEMENT_CHALLENGE_INDICATOR: + { + RenderAchievement(dc, achievement, style, b, alpha, entry.startTime, now); + break; + } + case OSDType::LEADERBOARD_TRACKER: + RenderLeaderboardTracker(dc, b, entry.text, alpha); + break; + default: + continue; + } + + y += (b.h + 4.0f) * alpha; // including alpha here gets us smooth animations. + } + // Get height float w, h; dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, "Wg", &w, &h); - float y = 10.0f; + y = 10.0f; + // Then draw them all. const std::vector bars = g_OSD.ProgressBars(); for (auto &bar : bars) { @@ -225,19 +303,34 @@ void OnScreenMessagesView::Draw(UIContext &dc) { } const std::vector entries = g_OSD.Entries(); - for (auto iter = entries.begin(); iter != entries.end(); ++iter) { + for (const auto &entry : entries) { dc.SetFontScale(1.0f, 1.0f); // Messages that are wider than the screen are left-aligned instead of centered. int align = 0; // If we have newlines, we may be looking at ASCII debug output. But let's verify. - if (iter->text.find('\n') != 0) { - if (!UTF8StringHasNonASCII(iter->text.c_str())) + if (entry.text.find('\n') != 0) { + if (!UTF8StringHasNonASCII(entry.text.c_str())) align |= FLAG_DYNAMIC_ASCII; } - float tw, th, h1; - MeasureOSDEntry(dc, *iter, align, &tw, &th, &h1); + float tw, th = 0.0f, h1 = 0.0f; + + switch (entry.type) { + case OSDType::ACHIEVEMENT_UNLOCKED: + { + const rc_client_achievement_t *achievement = rc_client_get_achievement_info(Achievements::GetClient(), entry.numericID); + if (achievement) { + MeasureAchievement(dc, achievement, AchievementRenderStyle::UNLOCKED, &tw, &th); + h1 = th; + } + tw = 550.0f; + break; + } + default: + MeasureOSDEntry(dc, entry, align, &tw, &th, &h1); + break; + } Bounds b(0.0f, y, tw, th); @@ -259,8 +352,8 @@ void OnScreenMessagesView::Draw(UIContext &dc) { b.h *= scale; } - float alpha = Clamp((float)(iter->endTime - now) * 4.0f, 0.0f, 1.0f); - RenderOSDEntry(dc, *iter, b, h1, align, alpha); + float alpha = Clamp((float)(entry.endTime - now) * 4.0f, 0.0f, 1.0f); + RenderOSDEntry(dc, entry, b, h1, align, alpha); y += (b.h * scale + 4.0f) * alpha; // including alpha here gets us smooth animations. } diff --git a/UI/PauseScreen.cpp b/UI/PauseScreen.cpp index 84f3c749f4..f92242b133 100644 --- a/UI/PauseScreen.cpp +++ b/UI/PauseScreen.cpp @@ -265,27 +265,13 @@ GamePauseScreen::~GamePauseScreen() { __DisplaySetWasPaused(); } -void GamePauseScreen::CreateViews() { +void GamePauseScreen::CreateSavestateControls(UI::LinearLayout *leftColumnItems, bool vertical) { + auto pa = GetI18NCategory(I18NCat::PAUSE); + static const int NUM_SAVESLOTS = 5; using namespace UI; - bool vertical = UseVerticalLayout(); - - Margins scrollMargins(0, 20, 0, 0); - Margins actionMenuMargins(0, 20, 15, 0); - auto gr = GetI18NCategory(I18NCat::GRAPHICS); - auto pa = GetI18NCategory(I18NCat::PAUSE); - - root_ = new LinearLayout(ORIENT_HORIZONTAL); - - ViewGroup *leftColumn = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(1.0, scrollMargins)); - root_->Add(leftColumn); - - LinearLayout *leftColumnItems = new LinearLayoutList(ORIENT_VERTICAL, new LayoutParams(FILL_PARENT, WRAP_CONTENT)); - leftColumn->Add(leftColumnItems); - - leftColumnItems->Add(new Spacer(0.0)); leftColumnItems->SetSpacing(10.0); for (int i = 0; i < NUM_SAVESLOTS; i++) { SaveSlotView *slot = leftColumnItems->Add(new SaveSlotView(gamePath_, i, vertical, new LayoutParams(FILL_PARENT, WRAP_CONTENT))); @@ -311,6 +297,36 @@ void GamePauseScreen::CreateViews() { rewindButton->SetEnabled(SaveState::CanRewind()); rewindButton->OnClick.Handle(this, &GamePauseScreen::OnRewind); } +} + +void GamePauseScreen::CreateViews() { + using namespace UI; + + bool vertical = UseVerticalLayout(); + + Margins scrollMargins(0, 10, 0, 0); + Margins actionMenuMargins(0, 10, 15, 0); + auto gr = GetI18NCategory(I18NCat::GRAPHICS); + auto pa = GetI18NCategory(I18NCat::PAUSE); + auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); + + root_ = new LinearLayout(ORIENT_HORIZONTAL); + + ViewGroup *leftColumn = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(1.0, scrollMargins)); + root_->Add(leftColumn); + + LinearLayout *leftColumnItems = new LinearLayoutList(ORIENT_VERTICAL, new LayoutParams(FILL_PARENT, WRAP_CONTENT)); + leftColumn->Add(leftColumnItems); + + leftColumnItems->Add(new Spacer(0.0)); + if (Achievements::IsActive()) { + leftColumnItems->Add(new GameAchievementSummaryView()); + leftColumnItems->Add(new Spacer(5.0)); + } + + if (!Achievements::ChallengeModeActive()) { + CreateSavestateControls(leftColumnItems, vertical); + } ViewGroup *rightColumn = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(vertical ? 200 : 300, FILL_PARENT, actionMenuMargins)); root_->Add(rightColumn); @@ -347,7 +363,7 @@ void GamePauseScreen::CreateViews() { return UI::EVENT_DONE; }); } - if (g_Config.bAchievementsEnable && Achievements::SafeHasAchievementsOrLeaderboards()) { + if (g_Config.bAchievementsEnable && Achievements::HasAchievementsOrLeaderboards()) { rightColumnItems->Add(new Choice(pa->T("Achievements")))->OnClick.Add([&](UI::EventParams &e) { screenManager()->push(new RetroAchievementsListScreen(gamePath_)); return UI::EVENT_DONE; diff --git a/UI/PauseScreen.h b/UI/PauseScreen.h index ed0dfced8d..830e7d0581 100644 --- a/UI/PauseScreen.h +++ b/UI/PauseScreen.h @@ -45,6 +45,8 @@ protected: void CallbackDeleteConfig(bool yes); private: + void CreateSavestateControls(UI::LinearLayout *viewGroup, bool vertical); + UI::EventReturn OnGameSettings(UI::EventParams &e); UI::EventReturn OnExitToMenu(UI::EventParams &e); UI::EventReturn OnReportFeedback(UI::EventParams &e); diff --git a/UI/RetroAchievementScreens.cpp b/UI/RetroAchievementScreens.cpp index 6535f35daa..dea09feac4 100644 --- a/UI/RetroAchievementScreens.cpp +++ b/UI/RetroAchievementScreens.cpp @@ -17,11 +17,9 @@ void RetroAchievementsListScreen::CreateTabs() { achievements->SetSpacing(5.0f); CreateAchievementsTab(achievements); - if (Achievements::GetLeaderboardCount() > 0) { - UI::LinearLayout *leaderboards = AddTab("Leaderboards", ac->T("Leaderboards")); - leaderboards->SetSpacing(5.0f); - CreateLeaderboardsTab(leaderboards); - } + UI::LinearLayout *leaderboards = AddTab("Leaderboards", ac->T("Leaderboards")); + leaderboards->SetSpacing(5.0f); + CreateLeaderboardsTab(leaderboards); #ifdef _DEBUG CreateStatisticsTab(AddTab("AchievementsStatistics", ac->T("Statistics"))); @@ -34,29 +32,50 @@ void RetroAchievementsListScreen::CreateAchievementsTab(UI::ViewGroup *achieveme using namespace UI; - std::vector unlockedAchievements; - std::vector lockedAchievements; + int filter = RC_CLIENT_ACHIEVEMENT_CATEGORY_CORE; + if (Achievements::UnofficialEnabled()) { + filter = RC_CLIENT_ACHIEVEMENT_CATEGORY_CORE_AND_UNOFFICIAL; + } + + rc_client_achievement_list_t *list = rc_client_create_achievement_list(Achievements::GetClient(), + filter, RC_CLIENT_ACHIEVEMENT_LIST_GROUPING_LOCK_STATE); + + std::vector unlockedAchievements; + std::vector lockedAchievements; + std::vector otherAchievements; + + for (uint32_t i = 0; i < list->num_buckets; i++) { + const rc_client_achievement_bucket_t &bucket = list->buckets[i]; + for (uint32_t j = 0; j < bucket.num_achievements; j++) { + switch (bucket.bucket_type) { + case RC_CLIENT_ACHIEVEMENT_BUCKET_LOCKED: + lockedAchievements.push_back(bucket.achievements[j]); + break; + case RC_CLIENT_ACHIEVEMENT_BUCKET_UNLOCKED: + unlockedAchievements.push_back(bucket.achievements[j]); + break; + default: + otherAchievements.push_back(bucket.achievements[j]); + break; + } + } + } achievements->Add(new ItemHeader(ac->T("Achievements"))); - achievements->Add(new GameAchievementSummaryView(Achievements::GetGameID())); - - Achievements::EnumerateAchievements([&](const Achievements::Achievement &achievement) { - if (achievement.locked) { - lockedAchievements.push_back(achievement); - } else { - unlockedAchievements.push_back(achievement); - } - return true; - }); + achievements->Add(new GameAchievementSummaryView()); achievements->Add(new ItemHeader(ac->T("Unlocked achievements"))); for (auto &achievement : unlockedAchievements) { - achievements->Add(new AchievementView(std::move(achievement))); + achievements->Add(new AchievementView(achievement)); } achievements->Add(new ItemHeader(ac->T("Locked achievements"))); for (auto &achievement : lockedAchievements) { - achievements->Add(new AchievementView(std::move(achievement))); + achievements->Add(new AchievementView(achievement)); + } + achievements->Add(new ItemHeader(ac->T("Other achievements"))); + for (auto &achievement : otherAchievements) { + achievements->Add(new AchievementView(achievement)); } } @@ -66,25 +85,25 @@ void RetroAchievementsListScreen::CreateLeaderboardsTab(UI::ViewGroup *viewGroup using namespace UI; - viewGroup->Add(new GameAchievementSummaryView(Achievements::GetGameID())); + viewGroup->Add(new GameAchievementSummaryView()); viewGroup->Add(new ItemHeader(ac->T("Leaderboards"))); - std::vector leaderboards; - - Achievements::EnumerateLeaderboards([&](const Achievements::Leaderboard &leaderboard) { - leaderboards.push_back(leaderboard); - return true; - }); + std::vector leaderboards; + rc_client_leaderboard_list_t *list = rc_client_create_leaderboard_list(Achievements::GetClient(), RC_CLIENT_LEADERBOARD_LIST_GROUPING_NONE); + for (uint32_t i = 0; i < list->num_buckets; i++) { + const rc_client_leaderboard_bucket_t &bucket = list->buckets[i]; + for (uint32_t j = 0; j < bucket.num_leaderboards; j++) { + leaderboards.push_back(bucket.leaderboards[j]); + } + } for (auto &leaderboard : leaderboards) { - if (!leaderboard.hidden) { - int leaderboardID = leaderboard.id; - viewGroup->Add(new LeaderboardSummaryView(std::move(leaderboard)))->OnClick.Add([=](UI::EventParams &e) -> UI::EventReturn { - screenManager()->push(new RetroAchievementsLeaderboardScreen(gamePath_, leaderboardID)); - return UI::EVENT_DONE; - }); - } + int leaderboardID = leaderboard->id; + viewGroup->Add(new LeaderboardSummaryView(leaderboard))->OnClick.Add([=](UI::EventParams &e) -> UI::EventReturn { + screenManager()->push(new RetroAchievementsLeaderboardScreen(gamePath_, leaderboardID)); + return UI::EVENT_DONE; + }); } } @@ -98,35 +117,60 @@ void RetroAchievementsListScreen::CreateStatisticsTab(UI::ViewGroup *viewGroup) viewGroup->Add(new InfoItem(ac->T("Bad memory accesses"), StringFromFormat("%d", stats.badMemoryAccessCount))); } +RetroAchievementsLeaderboardScreen::~RetroAchievementsLeaderboardScreen() { + if (pendingAsyncCall_) { + rc_client_abort_async(Achievements::GetClient(), pendingAsyncCall_); + } + Poll(); // Gets rid of pendingEntryList_. + if (entryList_) { + rc_client_destroy_leaderboard_entry_list(entryList_); + } +} + +RetroAchievementsLeaderboardScreen::RetroAchievementsLeaderboardScreen(const Path &gamePath, int leaderboardID) + : TabbedUIDialogScreenWithGameBackground(gamePath), leaderboardID_(leaderboardID) { + rc_client_begin_fetch_leaderboard_entries(Achievements::GetClient(), leaderboardID_, 0, 20, [](int result, const char *error_message, rc_client_leaderboard_entry_list_t *list, rc_client_t *client, void *userdata) { + if (result != RC_OK) { + g_OSD.Show(OSDType::MESSAGE_ERROR, error_message, 10.0f); + return; + } + + RetroAchievementsLeaderboardScreen *thiz = (RetroAchievementsLeaderboardScreen *)userdata; + thiz->pendingEntryList_ = list; + thiz->pendingAsyncCall_ = nullptr; + }, this); +} + void RetroAchievementsLeaderboardScreen::CreateTabs() { auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); - const Achievements::Leaderboard *leaderboard = Achievements::GetLeaderboardByID(leaderboardID_); + const rc_client_leaderboard_t *leaderboard = rc_client_get_leaderboard_info(Achievements::GetClient(), leaderboardID_); + using namespace UI; - - UI::LinearLayout *layout = AddTab("AchievementsLeaderboard", leaderboard->title.c_str()); + UI::LinearLayout *layout = AddTab("AchievementsLeaderboard", leaderboard->title); layout->Add(new TextView(leaderboard->description)); - layout->Add(new ItemHeader(ac->T("Leaderboard"))); - Poll(); + if (entryList_) { + for (uint32_t i = 0; i < entryList_->num_entries; i++) { + bool is_self = (i == entryList_->user_index); + // Should highlight somehow. + const rc_client_leaderboard_entry_t &entry = entryList_->entries[i]; - // TODO: Make it pretty. - for (auto &entry : entries_) { - layout->Add(new TextView(StringFromFormat(" %d: %s: %s%s", entry.rank, entry.user.c_str(), entry.formatted_score.c_str(), entry.is_self ? " <<<<< " : ""))); + char buffer[512]; + rc_client_leaderboard_entry_get_user_image_url(&entryList_->entries[i], buffer, sizeof(buffer)); + // Can also show entry.submitted, which is a time_t. And maybe highlight recent ones? + layout->Add(new LeaderboardEntryView(&entryList_->entries[i], is_self)); + } } } void RetroAchievementsLeaderboardScreen::Poll() { - if (done_) - return; - - std::optional result = Achievements::TryEnumerateLeaderboardEntries(leaderboardID_, [&](const Achievements::LeaderboardEntry &entry) -> bool { - entries_.push_back(entry); - return true; - }); - - if (result.has_value()) { - done_ = true; + if (pendingEntryList_) { + if (entryList_) { + rc_client_destroy_leaderboard_entry_list(entryList_); + } + entryList_ = pendingEntryList_; + pendingEntryList_ = nullptr; RecreateViews(); } } @@ -136,6 +180,8 @@ void RetroAchievementsLeaderboardScreen::update() { Poll(); } +RetroAchievementsSettingsScreen::~RetroAchievementsSettingsScreen() {} + void RetroAchievementsSettingsScreen::CreateTabs() { auto ac = GetI18NCategory(I18NCat::ACHIEVEMENTS); auto di = GetI18NCategory(I18NCat::DIALOG); @@ -160,8 +206,17 @@ void RetroAchievementsSettingsScreen::CreateAccountTab(UI::ViewGroup *viewGroup) using namespace UI; - if (Achievements::IsLoggedIn()) { - viewGroup->Add(new InfoItem(ac->T("Username"), Achievements::GetUsername())); + if (!g_Config.bAchievementsEnable) { + viewGroup->Add(new TextView(ac->T("Achievements are disabled"))); + } else if (Achievements::IsLoggedIn()) { + const rc_client_user_t *info = rc_client_get_user_info(Achievements::GetClient()); + + // In the future, RetroAchievements will support display names. Prepare for that. + if (strcmp(info->display_name, info->username) != 0) { + viewGroup->Add(new InfoItem(ac->T("Name"), info->display_name)); + } + viewGroup->Add(new InfoItem(ac->T("Username"), info->username)); + // viewGroup->Add(new InfoItem(ac->T("Unread messages"), info.numUnreadMessages)); viewGroup->Add(new Choice(di->T("Log out")))->OnClick.Add([=](UI::EventParams &) -> UI::EventReturn { Achievements::Logout(); return UI::EVENT_DONE; @@ -212,85 +267,165 @@ void RetroAchievementsSettingsScreen::CreateSettingsTab(UI::ViewGroup *viewGroup using namespace UI; viewGroup->Add(new ItemHeader(ac->T("Settings"))); - viewGroup->Add(new CheckBox(&g_Config.bAchievementsRichPresence, ac->T("Rich Presence"))); - viewGroup->Add(new CheckBox(&g_Config.bAchievementsSoundEffects, ac->T("Sound Effects"))); // not yet implemented - viewGroup->Add(new CheckBox(&g_Config.bAchievementsLogBadMemReads, ac->T("Log bad memory accesses"))); - - // Not yet fully implemented - // viewGroup->Add(new CheckBox(&g_Config.bAchievementsChallengeMode, ac->T("Challenge Mode (no savestates)"))); + viewGroup->Add(new CheckBox(&g_Config.bAchievementsEnable, ac->T("Achievements enabled")))->OnClick.Add([&](UI::EventParams &e) -> UI::EventReturn { + Achievements::UpdateSettings(); + RecreateViews(); + return UI::EVENT_DONE; + }); + viewGroup->Add(new CheckBox(&g_Config.bAchievementsChallengeMode, ac->T("Challenge Mode (no savestates)"))); + viewGroup->Add(new CheckBox(&g_Config.bAchievementsEncoreMode, ac->T("Encore Mode"))); + viewGroup->Add(new CheckBox(&g_Config.bAchievementsUnofficial, ac->T("Unofficial achievements"))); + // viewGroup->Add(new CheckBox(&g_Config.bAchievementsSoundEffects, ac->T("Sound Effects")))->SetEnabledPtr(&g_Config.bAchievementsEnable); // not yet implemented + viewGroup->Add(new CheckBox(&g_Config.bAchievementsLogBadMemReads, ac->T("Log bad memory accesses")))->SetEnabledPtr(&g_Config.bAchievementsEnable); // TODO: What are these for? // viewGroup->Add(new CheckBox(&g_Config.bAchievementsTestMode, ac->T("Test Mode"))); // viewGroup->Add(new CheckBox(&g_Config.bAchievementsUnofficialTestMode, ac->T("Unofficial Test Mode"))); } -void MeasureAchievement(const UIContext &dc, const Achievements::Achievement &achievement, float *w, float *h) { +void MeasureAchievement(const UIContext &dc, const rc_client_achievement_t *achievement, AchievementRenderStyle style, float *w, float *h) { + *w = 0.0f; + switch (style) { + case AchievementRenderStyle::PROGRESS_INDICATOR: + dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, achievement->measured_progress, w, h); + *w += 44.0f + 4.0f * 3.0f; + *h = 44.0f; + break; + case AchievementRenderStyle::CHALLENGE_INDICATOR: + // ONLY the icon. + *w = 60.0f; + *h = 60.0f; + break; + default: + *h = 72.0f; + break; + } +} + +void MeasureGameAchievementSummary(const UIContext &dc, float *w, float *h) { + std::string description = Achievements::GetGameAchievementSummary(); + + float tw, th; + dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, "Wg", &tw, &th); + + dc.MeasureText(dc.theme->uiFont, 0.66f, 0.66f, description.c_str(), w, h); + *h += 12.0f + th; + *w += 8.0f; +} + +void MeasureLeaderboardSummary(const UIContext &dc, const rc_client_leaderboard_t *leaderboard, float *w, float *h) { *w = 0.0f; *h = 72.0f; } -void MeasureGameAchievementSummary(const UIContext &dc, int gameID, float *w, float *h) { - *w = 0.0f; - *h = 72.0f; -} - -void MeasureLeaderboardSummary(const UIContext &dc, const Achievements::Leaderboard &achievement, float *w, float *h) { +void MeasureLeaderboardEntry(const UIContext &dc, const rc_client_leaderboard_entry_t *entry, float *w, float *h) { *w = 0.0f; *h = 72.0f; } // Graphical -void RenderAchievement(UIContext &dc, const Achievements::Achievement &achievement, AchievementRenderStyle style, const Bounds &bounds, float alpha, float startTime, float time_s) { +void RenderAchievement(UIContext &dc, const rc_client_achievement_t *achievement, AchievementRenderStyle style, const Bounds &bounds, float alpha, float startTime, float time_s) { using namespace UI; UI::Drawable background = UI::Drawable(dc.theme->backgroundColor); - if (achievement.locked) { - background.color = 0x706060; + + // Set some alpha, if displayed in list. + if (style == AchievementRenderStyle::LISTED) { + background.color = colorAlpha(background.color, 0.6f); } - background.color = colorAlpha(background.color, alpha); - uint32_t fgColor = colorAlpha(dc.theme->itemStyle.fgColor, alpha); + + if (!achievement->unlocked) { + // Make the background color gray. + // TODO: Different colors in challenge mode, or even in the "re-take achievements" mode when we add that? + background.color = (background.color & 0xFF000000) | 0x706060; + } + + int iconState = achievement->state; + + background.color = alphaMul(background.color, alpha); + uint32_t fgColor = alphaMul(dc.theme->itemStyle.fgColor, alpha); if (style == AchievementRenderStyle::UNLOCKED) { float mixWhite = pow(Clamp((float)(1.0f - (time_s - startTime)), 0.0f, 1.0f), 3.0f); background.color = colorBlend(0xFFE0FFFF, background.color, mixWhite); } - float iconSpace = 64.0f; + float padding = 4.0f; + + float iconSpace = bounds.h - padding * 2.0f; dc.Flush(); + dc.RebindTexture(); dc.Begin(); + + if (style != AchievementRenderStyle::LISTED) { + dc.DrawRectDropShadow(bounds, 12.0f, 0.7f * alpha); + } + dc.FillRect(background, bounds); + dc.Flush(); + dc.Begin(); + dc.SetFontStyle(dc.theme->uiFont); - dc.SetFontScale(1.0f, 1.0f); - dc.DrawTextRect(achievement.title.c_str(), bounds.Inset(iconSpace + 12.0f, 2.0f, 5.0f, 5.0f), fgColor, ALIGN_TOPLEFT); + char temp[512]; - dc.SetFontScale(0.66f, 0.66f); - dc.DrawTextRect(achievement.description.c_str(), bounds.Inset(iconSpace + 12.0f, 39.0f, 5.0f, 5.0f), fgColor, ALIGN_TOPLEFT); + switch (style) { + case AchievementRenderStyle::LISTED: + case AchievementRenderStyle::UNLOCKED: + { + dc.SetFontScale(1.0f, 1.0f); + dc.DrawTextRect(achievement->title, bounds.Inset(iconSpace + 12.0f, 2.0f, padding, padding), fgColor, ALIGN_TOPLEFT); - char temp[64]; - snprintf(temp, sizeof(temp), "%d", achievement.points); + dc.SetFontScale(0.66f, 0.66f); + dc.DrawTextRect(achievement->description, bounds.Inset(iconSpace + 12.0f, 39.0f, padding, padding), fgColor, ALIGN_TOPLEFT); - dc.SetFontScale(1.5f, 1.5f); - dc.DrawTextRect(temp, bounds.Expand(-5.0f, -5.0f), fgColor, ALIGN_RIGHT | ALIGN_VCENTER); + if (style == AchievementRenderStyle::LISTED && strlen(achievement->measured_progress) > 0) { + dc.SetFontScale(1.0f, 1.0f); + dc.DrawTextRect(achievement->measured_progress, bounds.Inset(iconSpace + 12.0f, padding, padding + 100.0f, padding), fgColor, ALIGN_VCENTER | ALIGN_RIGHT); + } - dc.SetFontScale(1.0f, 1.0f); - dc.Flush(); + // TODO: Draw measured_progress / measured_percent in a cute way + snprintf(temp, sizeof(temp), "%d", achievement->points); - std::string name = Achievements::GetAchievementBadgePath(achievement); - if (g_iconCache.BindIconTexture(&dc, name)) { - dc.Draw()->DrawTexRect(Bounds(bounds.x + 4.0f, bounds.y + 4.0f, iconSpace, iconSpace), 0.0f, 0.0f, 1.0f, 1.0f, whiteAlpha(alpha)); + dc.SetFontScale(1.5f, 1.5f); + dc.DrawTextRect(temp, bounds.Expand(-5.0f, -5.0f), fgColor, ALIGN_RIGHT | ALIGN_VCENTER); + + dc.SetFontScale(1.0f, 1.0f); + dc.Flush(); + break; + } + case AchievementRenderStyle::PROGRESS_INDICATOR: + // TODO: Also render a progress bar. + dc.SetFontScale(1.0f, 1.0f); + dc.DrawTextRect(achievement->measured_progress, bounds.Inset(iconSpace + padding * 2.0f, padding, padding, padding), fgColor, ALIGN_LEFT | ALIGN_VCENTER); + // Show the unlocked icon. + iconState = RC_CLIENT_ACHIEVEMENT_STATE_UNLOCKED; + break; + case AchievementRenderStyle::CHALLENGE_INDICATOR: + // Nothing but the icon, unlocked. + iconState = RC_CLIENT_ACHIEVEMENT_STATE_UNLOCKED; + break; } - dc.Flush(); - dc.RebindTexture(); + // Download and display the image. + char cacheKey[256]; + snprintf(cacheKey, sizeof(cacheKey), "ai:%s:%s", achievement->badge_name, iconState == RC_CLIENT_ACHIEVEMENT_STATE_UNLOCKED ? "unlocked" : "locked"); + if (RC_OK == rc_client_achievement_get_image_url(achievement, iconState, temp, sizeof(temp))) { + Achievements::DownloadImageIfMissing(cacheKey, std::move(std::string(temp))); + if (g_iconCache.BindIconTexture(&dc, cacheKey)) { + dc.Draw()->DrawTexRect(Bounds(bounds.x + padding, bounds.y + padding, iconSpace, iconSpace), 0.0f, 0.0f, 1.0f, 1.0f, whiteAlpha(alpha)); + } + dc.Flush(); + dc.RebindTexture(); + } } -void RenderGameAchievementSummary(UIContext &dc, int gameID, const Bounds &bounds, float alpha) { +void RenderGameAchievementSummary(UIContext &dc, const Bounds &bounds, float alpha) { using namespace UI; UI::Drawable background = dc.theme->itemStyle.background; - background.color = colorAlpha(background.color, alpha); + background.color = alphaMul(background.color, alpha); uint32_t fgColor = colorAlpha(dc.theme->itemStyle.fgColor, alpha); float iconSpace = 64.0f; @@ -301,11 +436,12 @@ void RenderGameAchievementSummary(UIContext &dc, int gameID, const Bounds &bound dc.SetFontStyle(dc.theme->uiFont); + const rc_client_game_t *gameInfo = rc_client_get_game_info(Achievements::GetClient()); + dc.SetFontScale(1.0f, 1.0f); - dc.DrawTextRect(Achievements::GetGameTitle().c_str(), bounds.Inset(iconSpace + 5.0f, 2.0f, 5.0f, 5.0f), fgColor, ALIGN_TOPLEFT); + dc.DrawTextRect(gameInfo->title, bounds.Inset(iconSpace + 5.0f, 2.0f, 5.0f, 5.0f), fgColor, ALIGN_TOPLEFT); std::string description = Achievements::GetGameAchievementSummary(); - std::string icon = Achievements::GetGameIcon(); dc.SetFontScale(0.66f, 0.66f); dc.DrawTextRect(description.c_str(), bounds.Inset(iconSpace + 5.0f, 38.0f, 5.0f, 5.0f), fgColor, ALIGN_TOPLEFT); @@ -313,16 +449,21 @@ void RenderGameAchievementSummary(UIContext &dc, int gameID, const Bounds &bound dc.SetFontScale(1.0f, 1.0f); dc.Flush(); - std::string name = icon; - if (g_iconCache.BindIconTexture(&dc, name)) { - dc.Draw()->DrawTexRect(Bounds(bounds.x, bounds.y, iconSpace, iconSpace), 0.0f, 0.0f, 1.0f, 1.0f, whiteAlpha(alpha)); + char url[512]; + char cacheKey[256]; + snprintf(cacheKey, sizeof(cacheKey), "gi:%s", gameInfo->badge_name); + if (RC_OK == rc_client_game_get_image_url(gameInfo, url, sizeof(url))) { + Achievements::DownloadImageIfMissing(cacheKey, std::move(std::string(url))); + if (g_iconCache.BindIconTexture(&dc, cacheKey)) { + dc.Draw()->DrawTexRect(Bounds(bounds.x, bounds.y, iconSpace, iconSpace), 0.0f, 0.0f, 1.0f, 1.0f, whiteAlpha(alpha)); + } } dc.Flush(); dc.RebindTexture(); } -void RenderLeaderboardSummary(UIContext &dc, const Achievements::Leaderboard &leaderboard, AchievementRenderStyle style, const Bounds &bounds, float alpha, float startTime, float time_s) { +void RenderLeaderboardSummary(UIContext &dc, const rc_client_leaderboard_t *leaderboard, AchievementRenderStyle style, const Bounds &bounds, float alpha, float startTime, float time_s) { using namespace UI; UI::Drawable background = UI::Drawable(dc.theme->backgroundColor); background.color = colorAlpha(background.color, alpha); @@ -333,7 +474,6 @@ void RenderLeaderboardSummary(UIContext &dc, const Achievements::Leaderboard &le background.color = colorBlend(0xFFE0FFFF, background.color, mixWhite); } - float iconSpace = 64.0f; dc.Flush(); dc.Begin(); @@ -342,10 +482,10 @@ void RenderLeaderboardSummary(UIContext &dc, const Achievements::Leaderboard &le dc.SetFontStyle(dc.theme->uiFont); dc.SetFontScale(1.0f, 1.0f); - dc.DrawTextRect(leaderboard.title.c_str(), bounds.Inset(iconSpace + 12.0f, 2.0f, 5.0f, 5.0f), fgColor, ALIGN_TOPLEFT); + dc.DrawTextRect(leaderboard->title, bounds.Inset(12.0f, 2.0f, 5.0f, 5.0f), fgColor, ALIGN_TOPLEFT); dc.SetFontScale(0.66f, 0.66f); - dc.DrawTextRect(leaderboard.description.c_str(), bounds.Inset(iconSpace + 12.0f, 39.0f, 5.0f, 5.0f), fgColor, ALIGN_TOPLEFT); + dc.DrawTextRect(leaderboard->description, bounds.Inset(12.0f, 39.0f, 5.0f, 5.0f), fgColor, ALIGN_TOPLEFT); /* char temp[64]; @@ -362,21 +502,84 @@ void RenderLeaderboardSummary(UIContext &dc, const Achievements::Leaderboard &le dc.RebindTexture(); } +void RenderLeaderboardEntry(UIContext &dc, const rc_client_leaderboard_entry_t *entry, const Bounds &bounds, float alpha) { + using namespace UI; + UI::Drawable background = dc.theme->itemStyle.background; + + background.color = alphaMul(background.color, alpha); + uint32_t fgColor = alphaMul(dc.theme->itemStyle.fgColor, alpha); + + float iconSize = 64.0f; + float numberSpace = 128.0f; + float iconLeft = numberSpace + 5.0f; + float iconSpace = numberSpace + 5.0f + iconSize; + + dc.Flush(); + + dc.Begin(); + dc.FillRect(background, bounds); + + dc.SetFontStyle(dc.theme->uiFont); + + dc.SetFontScale(1.5f, 1.5f); + dc.DrawTextRect(StringFromFormat("%d", entry->rank).c_str(), Bounds(bounds.x + 4.0f, bounds.y + 4.0f, numberSpace - 10.0f, bounds.h - 4.0f * 2.0f), fgColor, ALIGN_TOPRIGHT); + + dc.SetFontScale(1.0f, 1.0f); + dc.DrawTextRect(entry->user, bounds.Inset(iconSpace + 5.0f, 2.0f, 5.0f, 5.0f), fgColor, ALIGN_TOPLEFT); + + dc.SetFontScale(0.66f, 0.66f); + dc.DrawTextRect(entry->display, bounds.Inset(iconSpace + 5.0f, 38.0f, 5.0f, 5.0f), fgColor, ALIGN_TOPLEFT); + + dc.SetFontScale(1.0f, 1.0f); + dc.Flush(); + + // Come up with a unique name for the icon entry. + char cacheKey[256]; + snprintf(cacheKey, sizeof(cacheKey), "lbe:%s", entry->user); + char temp[512]; + if (RC_OK == rc_client_leaderboard_entry_get_user_image_url(entry, temp, sizeof(temp))) { + Achievements::DownloadImageIfMissing(cacheKey, std::move(std::string(temp))); + if (g_iconCache.BindIconTexture(&dc, cacheKey)) { + dc.Draw()->DrawTexRect(Bounds(bounds.x + iconLeft, bounds.y + 4.0f, 64.0f, 64.0f), 0.0f, 0.0f, 1.0f, 1.0f, whiteAlpha(alpha)); + } + } + + dc.Flush(); + dc.RebindTexture(); +} + void AchievementView::Draw(UIContext &dc) { RenderAchievement(dc, achievement_, AchievementRenderStyle::LISTED, bounds_, 1.0f, 0.0f, 0.0f); } void AchievementView::GetContentDimensions(const UIContext &dc, float &w, float &h) const { - MeasureAchievement(dc, achievement_, &w, &h); + MeasureAchievement(dc, achievement_, AchievementRenderStyle::LISTED, &w, &h); } void AchievementView::Click() { // In debug builds, clicking achievements will show them being unlocked (which may be a lie). #ifdef _DEBUG - g_OSD.ShowAchievementUnlocked(achievement_.id); + static int type = 0; + type++; + type = type % 4; + switch (type) { + case 0: g_OSD.ShowAchievementUnlocked(achievement_->id); break; + case 1: g_OSD.ShowAchievementProgress(achievement_->id, 2.0f); break; + case 2: g_OSD.ShowChallengeIndicator(achievement_->id, true); break; + case 3: g_OSD.ShowChallengeIndicator(achievement_->id, false); break; + } + #endif } +void GameAchievementSummaryView::Draw(UIContext &dc) { + RenderGameAchievementSummary(dc, bounds_, 1.0f); +} + +void GameAchievementSummaryView::GetContentDimensions(const UIContext &dc, float &w, float &h) const { + MeasureGameAchievementSummary(dc, &w, &h); +} + void LeaderboardSummaryView::Draw(UIContext &dc) { RenderLeaderboardSummary(dc, leaderboard_, AchievementRenderStyle::LISTED, bounds_, 1.0f, 0.0f, 0.0f); } @@ -385,10 +588,11 @@ void LeaderboardSummaryView::GetContentDimensions(const UIContext &dc, float &w, MeasureLeaderboardSummary(dc, leaderboard_, &w, &h); } -void GameAchievementSummaryView::Draw(UIContext &dc) { - RenderGameAchievementSummary(dc, gameID_, bounds_, 1.0f); +void LeaderboardEntryView::Draw(UIContext &dc) { + RenderLeaderboardEntry(dc, entry_, bounds_, 1.0f); } -void GameAchievementSummaryView::GetContentDimensions(const UIContext &dc, float &w, float &h) const { - MeasureGameAchievementSummary(dc, gameID_, &w, &h); +void LeaderboardEntryView::GetContentDimensions(const UIContext &dc, float &w, float &h) const { + MeasureLeaderboardEntry(dc, entry_, &w, &h); } + diff --git a/UI/RetroAchievementScreens.h b/UI/RetroAchievementScreens.h index e7a2fe19ee..cf83084e44 100644 --- a/UI/RetroAchievementScreens.h +++ b/UI/RetroAchievementScreens.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "Common/File/Path.h" #include "Common/UI/View.h" #include "Common/UI/UIScreen.h" @@ -29,6 +31,7 @@ private: class RetroAchievementsSettingsScreen : public TabbedUIDialogScreenWithGameBackground { public: RetroAchievementsSettingsScreen(const Path &gamePath) : TabbedUIDialogScreenWithGameBackground(gamePath) {} + ~RetroAchievementsSettingsScreen(); const char *tag() const override { return "RetroAchievementsSettingsScreen"; } void CreateTabs() override; @@ -47,20 +50,29 @@ private: class RetroAchievementsLeaderboardScreen : public TabbedUIDialogScreenWithGameBackground { public: - RetroAchievementsLeaderboardScreen(const Path &gamePath, int leaderboardID) : TabbedUIDialogScreenWithGameBackground(gamePath), leaderboardID_(leaderboardID) {} + RetroAchievementsLeaderboardScreen(const Path &gamePath, int leaderboardID); + ~RetroAchievementsLeaderboardScreen(); + const char *tag() const override { return "RetroAchievementsLeaderboardScreen"; } void CreateTabs() override; void update() override; + protected: bool ShowSearchControls() const override { return false; } + private: void Poll(); int leaderboardID_; - bool done_ = false; - std::vector entries_; + + // Keep the fetched list alive and destroy in destructor. + rc_client_leaderboard_entry_list_t *entryList_ = nullptr; + + rc_client_leaderboard_entry_list_t *pendingEntryList_ = nullptr; + + rc_client_async_handle_t *pendingAsyncCall_ = nullptr; }; class UIContext; @@ -68,41 +80,66 @@ class UIContext; enum class AchievementRenderStyle { LISTED, UNLOCKED, + PROGRESS_INDICATOR, + CHALLENGE_INDICATOR, }; -void MeasureAchievement(const UIContext &dc, const Achievements::Achievement &achievement, float *w, float *h); -void RenderAchievement(UIContext &dc, const Achievements::Achievement &achievement, AchievementRenderStyle style, const Bounds &bounds, float alpha, float startTime, float time_s); -void MeasureGameAchievementSummary(const UIContext &dc, int gameID, float *w, float *h); -void RenderGameAchievementSummary(UIContext &dc, int gameID, const Bounds &bounds, float alpha); +void MeasureAchievement(const UIContext &dc, const rc_client_achievement_t *achievement, AchievementRenderStyle style, float *w, float *h); +void RenderAchievement(UIContext &dc, const rc_client_achievement_t *achievement, AchievementRenderStyle style, const Bounds &bounds, float alpha, float startTime, float time_s); + +void MeasureGameAchievementSummary(const UIContext &dc, float *w, float *h); +void RenderGameAchievementSummary(UIContext &dc, const Bounds &bounds, float alpha); + +void MeasureLeaderboardEntry(const UIContext &dc, const rc_client_leaderboard_entry_t *entry, float *w, float *h); +void RenderLeaderboardEntry(UIContext &dc, const rc_client_leaderboard_entry_t *entry, const Bounds &bounds, float alpha); class AchievementView : public UI::ClickableItem { public: - AchievementView(const Achievements::Achievement &&achievement, UI::LayoutParams *layoutParams = nullptr) : UI::ClickableItem(layoutParams), achievement_(achievement) {} + AchievementView(const rc_client_achievement_t *achievement, UI::LayoutParams *layoutParams = nullptr) : UI::ClickableItem(layoutParams), achievement_(achievement) { + layoutParams_->height = UI::WRAP_CONTENT; // Override the standard Item fixed height. + } void Click() override; void Draw(UIContext &dc) override; void GetContentDimensions(const UIContext &dc, float &w, float &h) const override; private: - Achievements::Achievement achievement_; -}; - -class LeaderboardSummaryView : public UI::ClickableItem { -public: - LeaderboardSummaryView(const Achievements::Leaderboard &&leaderboard, UI::LayoutParams *layoutParams = nullptr) : UI::ClickableItem(layoutParams), leaderboard_(leaderboard) {} - - void Draw(UIContext &dc) override; - void GetContentDimensions(const UIContext &dc, float &w, float &h) const override; - -private: - Achievements::Leaderboard leaderboard_; + const rc_client_achievement_t *achievement_; }; class GameAchievementSummaryView : public UI::Item { public: - GameAchievementSummaryView(int gameID, UI::LayoutParams *layoutParams = nullptr) : UI::Item(layoutParams), gameID_(gameID) {} + GameAchievementSummaryView(UI::LayoutParams *layoutParams = nullptr) : UI::Item(layoutParams) { + layoutParams_->height = UI::WRAP_CONTENT; // Override the standard Item fixed height. + } void Draw(UIContext &dc) override; void GetContentDimensions(const UIContext &dc, float &w, float &h) const override; +}; + +class LeaderboardSummaryView : public UI::ClickableItem { +public: + LeaderboardSummaryView(const rc_client_leaderboard_t *leaderboard, UI::LayoutParams *layoutParams = nullptr) : UI::ClickableItem(layoutParams), leaderboard_(leaderboard) { + layoutParams_->height = UI::WRAP_CONTENT; // Override the standard Item fixed height. + } + + void Draw(UIContext &dc) override; + void GetContentDimensions(const UIContext &dc, float &w, float &h) const override; + private: - int gameID_; + const rc_client_leaderboard_t *leaderboard_; +}; + +class LeaderboardEntryView : public UI::Item { +public: + LeaderboardEntryView(const rc_client_leaderboard_entry_t *entry, bool isCurrentUser, UI::LayoutParams *layoutParams = nullptr) + : UI::Item(layoutParams), entry_(entry), isCurrentUser_(isCurrentUser) { + layoutParams_->height = UI::WRAP_CONTENT; // Override the standard Item fixed height. + } + + void Draw(UIContext &dc) override; + void GetContentDimensions(const UIContext &dc, float &w, float &h) const override; + +private: + const rc_client_leaderboard_entry_t *entry_; + bool isCurrentUser_; }; diff --git a/UI/UI.vcxproj b/UI/UI.vcxproj index 7cac1f3c79..68cf5bac73 100644 --- a/UI/UI.vcxproj +++ b/UI/UI.vcxproj @@ -106,6 +106,9 @@ + + {533f1d30-d04d-47cc-ad71-20f658907e36} + {beb0a821-3c7f-410f-a525-63afbc69bf8f} diff --git a/UWP/rcheevos_UWP/rcheevos_UWP.vcxproj b/UWP/rcheevos_UWP/rcheevos_UWP.vcxproj index eb1a060dc8..8d4c4b7d09 100644 --- a/UWP/rcheevos_UWP/rcheevos_UWP.vcxproj +++ b/UWP/rcheevos_UWP/rcheevos_UWP.vcxproj @@ -41,6 +41,7 @@ + @@ -48,6 +49,7 @@ + @@ -69,6 +71,7 @@ + @@ -103,8 +106,10 @@ true - - + + + + diff --git a/UWP/rcheevos_UWP/rcheevos_UWP.vcxproj.filters b/UWP/rcheevos_UWP/rcheevos_UWP.vcxproj.filters index d0fa50830b..5986d29ae0 100644 --- a/UWP/rcheevos_UWP/rcheevos_UWP.vcxproj.filters +++ b/UWP/rcheevos_UWP/rcheevos_UWP.vcxproj.filters @@ -55,6 +55,12 @@ rhash + + rcheevos + + + include + @@ -140,5 +146,8 @@ rhash + + rcheevos + \ No newline at end of file diff --git a/Windows/MainWindowMenu.cpp b/Windows/MainWindowMenu.cpp index 43c6c7bc99..2e7101f76f 100644 --- a/Windows/MainWindowMenu.cpp +++ b/Windows/MainWindowMenu.cpp @@ -51,6 +51,7 @@ #include "Core/HLE/sceUmd.h" #include "Core/SaveState.h" #include "Core/Core.h" +#include "Core/RetroAchievements.h" extern bool g_TakeScreenshot; @@ -69,14 +70,23 @@ namespace MainWindow { LRESULT CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM); void SetIngameMenuItemStates(HMENU menu, const GlobalUIState state) { - UINT menuEnable = state == UISTATE_INGAME || state == UISTATE_EXCEPTION ? MF_ENABLED : MF_GRAYED; + bool menuEnableBool = state == UISTATE_INGAME || state == UISTATE_EXCEPTION; + + bool saveStateEnableBool = menuEnableBool; + if (Achievements::ChallengeModeActive()) { + saveStateEnableBool = false; + } + + UINT menuEnable = menuEnableBool ? MF_ENABLED : MF_GRAYED; + UINT saveStateEnable = saveStateEnableBool ? MF_ENABLED : MF_GRAYED; UINT menuInGameEnable = state == UISTATE_INGAME ? MF_ENABLED : MF_GRAYED; UINT umdSwitchEnable = state == UISTATE_INGAME && getUMDReplacePermit() ? MF_ENABLED : MF_GRAYED; - EnableMenuItem(menu, ID_FILE_SAVESTATEFILE, menuEnable); - EnableMenuItem(menu, ID_FILE_LOADSTATEFILE, menuEnable); - EnableMenuItem(menu, ID_FILE_QUICKSAVESTATE, menuEnable); - EnableMenuItem(menu, ID_FILE_QUICKLOADSTATE, menuEnable); + EnableMenuItem(menu, ID_FILE_SAVESTATE_SLOT_MENU, saveStateEnable); + EnableMenuItem(menu, ID_FILE_SAVESTATEFILE, saveStateEnable); + EnableMenuItem(menu, ID_FILE_LOADSTATEFILE, saveStateEnable); + EnableMenuItem(menu, ID_FILE_QUICKSAVESTATE, saveStateEnable); + EnableMenuItem(menu, ID_FILE_QUICKLOADSTATE, saveStateEnable); EnableMenuItem(menu, ID_EMULATION_PAUSE, menuEnable); EnableMenuItem(menu, ID_EMULATION_STOP, menuEnable); EnableMenuItem(menu, ID_EMULATION_RESET, menuEnable); @@ -517,70 +527,87 @@ namespace MainWindow { } break; case ID_FILE_LOADSTATEFILE: - if (W32Util::BrowseForFileName(true, hWnd, L"Load state", 0, L"Save States (*.ppst)\0*.ppst\0All files\0*.*\0\0", L"ppst", fn)) { - SetCursor(LoadCursor(0, IDC_WAIT)); - SaveState::Load(Path(fn), -1, SaveStateActionFinished); + if (!Achievements::WarnUserIfChallengeModeActive()) { + if (W32Util::BrowseForFileName(true, hWnd, L"Load state", 0, L"Save States (*.ppst)\0*.ppst\0All files\0*.*\0\0", L"ppst", fn)) { + SetCursor(LoadCursor(0, IDC_WAIT)); + SaveState::Load(Path(fn), -1, SaveStateActionFinished); + } } break; case ID_FILE_SAVESTATEFILE: - if (W32Util::BrowseForFileName(false, hWnd, L"Save state", 0, L"Save States (*.ppst)\0*.ppst\0All files\0*.*\0\0", L"ppst", fn)) { - SetCursor(LoadCursor(0, IDC_WAIT)); - SaveState::Save(Path(fn), -1, SaveStateActionFinished); + if (!Achievements::WarnUserIfChallengeModeActive()) { + if (W32Util::BrowseForFileName(false, hWnd, L"Save state", 0, L"Save States (*.ppst)\0*.ppst\0All files\0*.*\0\0", L"ppst", fn)) { + SetCursor(LoadCursor(0, IDC_WAIT)); + SaveState::Save(Path(fn), -1, SaveStateActionFinished); + } } break; case ID_FILE_SAVESTATE_NEXT_SLOT: { - SaveState::NextSlot(); - System_PostUIMessage("savestate_displayslot", ""); - break; - } - - case ID_FILE_SAVESTATE_NEXT_SLOT_HC: - { - if (!KeyMap::PspButtonHasMappings(VIRTKEY_NEXT_SLOT)) { + if (!Achievements::WarnUserIfChallengeModeActive()) { SaveState::NextSlot(); System_PostUIMessage("savestate_displayslot", ""); } break; } - case ID_FILE_SAVESTATE_SLOT_1: g_Config.iCurrentStateSlot = 0; break; - case ID_FILE_SAVESTATE_SLOT_2: g_Config.iCurrentStateSlot = 1; break; - case ID_FILE_SAVESTATE_SLOT_3: g_Config.iCurrentStateSlot = 2; break; - case ID_FILE_SAVESTATE_SLOT_4: g_Config.iCurrentStateSlot = 3; break; - case ID_FILE_SAVESTATE_SLOT_5: g_Config.iCurrentStateSlot = 4; break; - - case ID_FILE_QUICKLOADSTATE: + case ID_FILE_SAVESTATE_NEXT_SLOT_HC: { - SetCursor(LoadCursor(0, IDC_WAIT)); - SaveState::LoadSlot(PSP_CoreParameter().fileToStart, g_Config.iCurrentStateSlot, SaveStateActionFinished); + if (!Achievements::WarnUserIfChallengeModeActive()) { + if (!KeyMap::PspButtonHasMappings(VIRTKEY_NEXT_SLOT)) { + SaveState::NextSlot(); + System_PostUIMessage("savestate_displayslot", ""); + } + } break; } - case ID_FILE_QUICKLOADSTATE_HC: - { - if (!KeyMap::PspButtonHasMappings(VIRTKEY_LOAD_STATE)) - { + case ID_FILE_SAVESTATE_SLOT_1: + case ID_FILE_SAVESTATE_SLOT_2: + case ID_FILE_SAVESTATE_SLOT_3: + case ID_FILE_SAVESTATE_SLOT_4: + case ID_FILE_SAVESTATE_SLOT_5: + if (!Achievements::WarnUserIfChallengeModeActive()) { + g_Config.iCurrentStateSlot = wmId - ID_FILE_SAVESTATE_SLOT_1; + } + break; + + case ID_FILE_QUICKLOADSTATE: + if (!Achievements::WarnUserIfChallengeModeActive()) { SetCursor(LoadCursor(0, IDC_WAIT)); SaveState::LoadSlot(PSP_CoreParameter().fileToStart, g_Config.iCurrentStateSlot, SaveStateActionFinished); } break; + + case ID_FILE_QUICKLOADSTATE_HC: + { + if (!Achievements::WarnUserIfChallengeModeActive()) { + if (!KeyMap::PspButtonHasMappings(VIRTKEY_LOAD_STATE)) { + SetCursor(LoadCursor(0, IDC_WAIT)); + SaveState::LoadSlot(PSP_CoreParameter().fileToStart, g_Config.iCurrentStateSlot, SaveStateActionFinished); + } + } + break; } case ID_FILE_QUICKSAVESTATE: { - SetCursor(LoadCursor(0, IDC_WAIT)); - SaveState::SaveSlot(PSP_CoreParameter().fileToStart, g_Config.iCurrentStateSlot, SaveStateActionFinished); + if (!Achievements::WarnUserIfChallengeModeActive()) { + SetCursor(LoadCursor(0, IDC_WAIT)); + SaveState::SaveSlot(PSP_CoreParameter().fileToStart, g_Config.iCurrentStateSlot, SaveStateActionFinished); + } break; } case ID_FILE_QUICKSAVESTATE_HC: { - if (!KeyMap::PspButtonHasMappings(VIRTKEY_SAVE_STATE)) - { - SetCursor(LoadCursor(0, IDC_WAIT)); - SaveState::SaveSlot(PSP_CoreParameter().fileToStart, g_Config.iCurrentStateSlot, SaveStateActionFinished); - break; + if (!Achievements::WarnUserIfChallengeModeActive()) { + if (!KeyMap::PspButtonHasMappings(VIRTKEY_SAVE_STATE)) + { + SetCursor(LoadCursor(0, IDC_WAIT)); + SaveState::SaveSlot(PSP_CoreParameter().fileToStart, g_Config.iCurrentStateSlot, SaveStateActionFinished); + break; + } } break; } diff --git a/android/jni/Android.mk b/android/jni/Android.mk index 1c29b20489..bfb6874e63 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -95,6 +95,7 @@ RCHEEVOS_FILES := \ ${SRC}/ext/rcheevos/src/rcheevos/lboard.c \ ${SRC}/ext/rcheevos/src/rcheevos/memref.c \ ${SRC}/ext/rcheevos/src/rcheevos/operand.c \ + ${SRC}/ext/rcheevos/src/rcheevos/rc_client.c \ ${SRC}/ext/rcheevos/src/rcheevos/rc_validate.c \ ${SRC}/ext/rcheevos/src/rcheevos/richpresence.c \ ${SRC}/ext/rcheevos/src/rcheevos/runtime.c \ @@ -103,8 +104,7 @@ RCHEEVOS_FILES := \ ${SRC}/ext/rcheevos/src/rcheevos/value.c \ ${SRC}/ext/rcheevos/src/rhash/cdreader.c \ ${SRC}/ext/rcheevos/src/rhash/hash.c \ - ${SRC}/ext/rcheevos/src/rhash/md5.c \ - ${SRC}/ext/rcheevos/src/rurl/url.c + ${SRC}/ext/rcheevos/src/rhash/md5.c VR_FILES := \ $(SRC)/Common/VR/OpenXRLoader.cpp \ diff --git a/assets/lang/en_US.ini b/assets/lang/en_US.ini index 1800175eb9..fe31752454 100644 --- a/assets/lang/en_US.ini +++ b/assets/lang/en_US.ini @@ -24,23 +24,30 @@ [Achievements] %d achievements, %d points = %d achievements, %d points +%1: Leaderboard attempt started = %1: Leaderboard attempt started +%1: Leaderboard attempt failed = %1: Leaderboard attempt failed +%1: Submitting leaderboard score: %2! = %1: Submitting leaderboard score: %2! Account = Account Achievements = Achievements +Achievements are disabled = Achievements are disabled Challenge Mode = Challenge Mode Challenge Mode (no savestates) = Challenge Mode (no savestates) Earned = You have earned %d of %d achievements, and %d of %d points +Encore Mode = Encore Mode How to use RetroAchievements = How to use RetroAchievements Leaderboard submission is enabled = Leaderboard submission is enabled Leaderboards = Leaderboards Links = Links -Local = Local Locked achievements = Locked achievements Log bad memory accesses = Log bad memory accesses Mastered %1 = Mastered %1 +This feature is not available in Challenge Mode = This feature is not available in Challenge Mode +Save states not available in Challenge Mode = Save states not available in Challenge Mode Register on www.retroachievements.org = Register on www.retroachievements.org RetroAchievements are not available for this game = RetroAchievements are not available for this game RetroAchievements website = RetroAchievements website Rich Presence = Rich Presence +Save state loaded without achievement data = Save state loaded without achievement data Sound Effects = Sound Effects Statistics = Statistics Submitted Score = Submitted Score @@ -49,7 +56,7 @@ Syncing achievements data... = Syncing achievements data... Test Mode = Test Mode This game has no achievements = This game has no achievements Unlocked achievements = Unlocked achievements -Unofficial = Unofficial +Unofficial achievements = Unofficial achievements [Audio] Alternate speed volume = Alternate speed volume diff --git a/ext/rcheevos b/ext/rcheevos index d176b4bcb4..223df07613 160000 --- a/ext/rcheevos +++ b/ext/rcheevos @@ -1 +1 @@ -Subproject commit d176b4bcb42488da84f55991fd10f8ff36183384 +Subproject commit 223df0761313c5c9d9f4d4dc4b5897cf41e7e036 diff --git a/ext/rcheevos-build/CMakeLists.txt b/ext/rcheevos-build/CMakeLists.txt index 6cb442f8d4..d39f008c7a 100644 --- a/ext/rcheevos-build/CMakeLists.txt +++ b/ext/rcheevos-build/CMakeLists.txt @@ -23,6 +23,8 @@ set(ALL_SOURCE_FILES ${SRC_DIR}/rcheevos/memref.c ${SRC_DIR}/rcheevos/operand.c ${SRC_DIR}/rcheevos/rc_compat.h + ${SRC_DIR}/rcheevos/rc_client.c + ${SRC_DIR}/rcheevos/rc_client_internal.h ${SRC_DIR}/rcheevos/rc_internal.h ${SRC_DIR}/rcheevos/rc_validate.h ${SRC_DIR}/rcheevos/rc_validate.c @@ -37,8 +39,6 @@ set(ALL_SOURCE_FILES ${SRC_DIR}/rhash/hash.c ${SRC_DIR}/rhash/md5.c ${SRC_DIR}/rhash/md5.h - # rurl - ${SRC_DIR}/rurl/url.c ) add_library(rcheevos STATIC ${ALL_SOURCE_FILES}) diff --git a/ext/rcheevos-build/rcheevos.vcxproj b/ext/rcheevos-build/rcheevos.vcxproj index 7a549fb390..ac67e81de7 100644 --- a/ext/rcheevos-build/rcheevos.vcxproj +++ b/ext/rcheevos-build/rcheevos.vcxproj @@ -49,6 +49,7 @@ + @@ -58,7 +59,6 @@ - @@ -67,6 +67,7 @@ + @@ -74,6 +75,7 @@ + diff --git a/ext/rcheevos-build/rcheevos.vcxproj.filters b/ext/rcheevos-build/rcheevos.vcxproj.filters index d4fb9e4ffb..6bc1b11314 100644 --- a/ext/rcheevos-build/rcheevos.vcxproj.filters +++ b/ext/rcheevos-build/rcheevos.vcxproj.filters @@ -10,9 +10,6 @@ {55ec06aa-6d34-4edf-8b0b-f93f93a064b4} - - {990047ac-f0d4-44f5-8463-54f898557d02} - {9e049d1f-4b83-4aa5-89f3-01a42e1773e2} @@ -87,8 +84,8 @@ rhash - - rurl + + rcheevos @@ -146,5 +143,11 @@ rhash + + include + + + rcheevos + \ No newline at end of file diff --git a/headless/Headless.cpp b/headless/Headless.cpp index a59ebafc45..4a79bad5d8 100644 --- a/headless/Headless.cpp +++ b/headless/Headless.cpp @@ -143,6 +143,12 @@ void System_AudioGetDebugStats(char *buf, size_t bufSize) { if (buf) buf[0] = '\ void System_AudioClear() {} void System_AudioPushSamples(const s32 *audio, int numSamples) {} +// TODO: To avoid having to define these here, these should probably be turned into system "requests". +void NativeSaveSecret(const char *nameOfSecret, const std::string &data) {} +std::string NativeLoadSecret(const char *nameOfSecret) { + return ""; +} + int printUsage(const char *progname, const char *reason) { if (reason != NULL) diff --git a/headless/Headless.vcxproj b/headless/Headless.vcxproj index 7329860ba5..695f20dbe4 100644 --- a/headless/Headless.vcxproj +++ b/headless/Headless.vcxproj @@ -508,6 +508,9 @@ {8bfd8150-94d5-4bf9-8a50-7bd9929a0850} + + {31694510-a8c0-40f6-b09b-e8df825adefa} + {457f45d2-556f-47bc-a31d-aff0d15beaed} diff --git a/libretro/Makefile.common b/libretro/Makefile.common index da68a6f6fc..29bc7de7e4 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -212,6 +212,7 @@ SOURCES_C += \ $(EXTDIR)/rcheevos/src/rcheevos/lboard.c \ $(EXTDIR)/rcheevos/src/rcheevos/memref.c \ $(EXTDIR)/rcheevos/src/rcheevos/operand.c \ + $(EXTDIR)/rcheevos/src/rcheevos/rc_client.c \ $(EXTDIR)/rcheevos/src/rcheevos/rc_validate.c \ $(EXTDIR)/rcheevos/src/rcheevos/richpresence.c \ $(EXTDIR)/rcheevos/src/rcheevos/runtime.c \ diff --git a/unittest/UnitTest.cpp b/unittest/UnitTest.cpp index ca588ea6c4..66e7fe5a15 100644 --- a/unittest/UnitTest.cpp +++ b/unittest/UnitTest.cpp @@ -103,6 +103,12 @@ void System_AudioGetDebugStats(char *buf, size_t bufSize) { if (buf) buf[0] = '\ void System_AudioClear() {} void System_AudioPushSamples(const s32 *audio, int numSamples) {} +// TODO: To avoid having to define these here, these should probably be turned into system "requests". +void NativeSaveSecret(const char *nameOfSecret, const std::string &data) {} +std::string NativeLoadSecret(const char *nameOfSecret) { + return ""; +} + #if PPSSPP_PLATFORM(ANDROID) JNIEnv *getEnv() { return nullptr; diff --git a/unittest/UnitTests.vcxproj b/unittest/UnitTests.vcxproj index 687314e43d..e96bfd7892 100644 --- a/unittest/UnitTests.vcxproj +++ b/unittest/UnitTests.vcxproj @@ -426,15 +426,15 @@ {8bfd8150-94d5-4bf9-8a50-7bd9929a0850} + + {31694510-a8c0-40f6-b09b-e8df825adefa} + {f761046e-6c38-4428-a5f1-38391a37bb34} {457f45d2-556f-47bc-a31d-aff0d15beaed} - - {004b8d11-2be3-4bd9-ab40-2be04cf2096f} -