From 6ed025dd894e0f635f7227e20dfcca993e2160b2 Mon Sep 17 00:00:00 2001 From: lb_ii Date: Fri, 28 May 2021 02:12:59 +0300 Subject: [PATCH] ACHIEVEMENTS: Add API methods that are easy to use --- common/achievements.cpp | 32 ++++++++++++++++++++++++++++++++ common/achievements.h | 18 ++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/common/achievements.cpp b/common/achievements.cpp index e4b3bda5dec..3be95c9726d 100644 --- a/common/achievements.cpp +++ b/common/achievements.cpp @@ -40,6 +40,17 @@ AchievementsManager::AchievementsManager() { AchievementsManager::~AchievementsManager() { } +bool AchievementsManager::setActiveDomain(const AchievementsInfo &info) { + if (info.appId.empty()) { + unsetActiveDomain(); + return false; + } + + _descriptions = info.descriptions; + + return setActiveDomain(info.platform, info.appId); +} + bool AchievementsManager::setActiveDomain(AchievementsPlatform platform, const String &appId) { String prefix = platform == STEAM_ACHIEVEMENTS ? "steam-" + appId : platform == GALAXY_ACHIEVEMENTS ? "galaxy-" + appId : @@ -70,10 +81,31 @@ bool AchievementsManager::unsetActiveDomain() { delete _iniFile; _iniFile = nullptr; + _descriptions.clear(); + return true; } +bool AchievementsManager::setAchievement(const String &id) { + if (!isReady()) { + return false; + } + if (isAchieved(id)) { + return true; + } + + String displayedMessage = id; + for (uint32 i = 0; i < _descriptions.size(); i++) { + if (strcmp(_descriptions[i].id, id.c_str()) == 0) { + displayedMessage = _descriptions[i].title; + break; + } + } + + return setAchievement(id, displayedMessage); +} + bool AchievementsManager::setAchievement(const String &id, const String &displayedMessage) { if (!isReady()) { return false; diff --git a/common/achievements.h b/common/achievements.h index 1a0f689c164..a94e6cbc9df 100644 --- a/common/achievements.h +++ b/common/achievements.h @@ -81,12 +81,19 @@ public: ~AchievementsManager(); /** - * Set a platform and application ID as active domain. + * (DEPRECATED) Set a platform and application ID as active domain. * * @param[in] platform Achievements platform. * @param[in] appId Achievements application ID of the given platform. */ bool setActiveDomain(AchievementsPlatform platform, const String &appId); + + /** + * Set a platform and application ID as active domain, store messages texts. + * + * @param[in] info Achievements platform, application ID and messages information. + */ + bool setActiveDomain(const AchievementsInfo &info); bool unsetActiveDomain(); //!< Unset the current active domain. bool isReady() const { return _iniFile != nullptr; } //!< Check whether the domain is ready. @@ -95,13 +102,19 @@ public: * @{ */ - /** Set an achievement. + /** (DEPRECATED) Set an achievement. * * @param[in] id Internal ID of the achievement. * @param[in] displayedMessage Message displayed when the achievement is achieved. */ bool setAchievement(const String &id, const String &displayedMessage); + /** Set an achievement. Message is automatically displayed with text from active domain. + * + * @param[in] id Internal ID of the achievement. + */ + bool setAchievement(const String &id); + /** * Set an achievement as achieved. * @@ -167,6 +180,7 @@ public: private: INIFile *_iniFile; String _iniFileName; + Common::Array _descriptions; }; /** Shortcut for accessing the Achievements Manager. */