2020-03-16 03:47:21 +03:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "common/achievements.h"
|
|
|
|
#include "common/debug.h"
|
2021-06-04 04:34:59 +03:00
|
|
|
#include "common/stream.h"
|
2020-03-16 03:47:21 +03:00
|
|
|
#include "common/system.h"
|
|
|
|
#include "common/translation.h"
|
2021-06-04 04:34:59 +03:00
|
|
|
#include "common/unzip.h"
|
2020-03-16 03:47:21 +03:00
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
DECLARE_SINGLETON(AchievementsManager);
|
|
|
|
|
|
|
|
|
|
|
|
AchievementsManager::AchievementsManager() {
|
2020-04-29 01:56:54 +03:00
|
|
|
_iniFile = nullptr;
|
2020-03-16 03:47:21 +03:00
|
|
|
unsetActiveDomain();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AchievementsManager::~AchievementsManager() {
|
|
|
|
}
|
|
|
|
|
2021-05-28 02:12:59 +03:00
|
|
|
bool AchievementsManager::setActiveDomain(const AchievementsInfo &info) {
|
|
|
|
if (info.appId.empty()) {
|
|
|
|
unsetActiveDomain();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-30 04:12:09 +03:00
|
|
|
const char* platform = info.platform == STEAM_ACHIEVEMENTS ? "steam" :
|
2021-05-28 03:10:16 +03:00
|
|
|
info.platform == GALAXY_ACHIEVEMENTS ? "galaxy" :
|
2021-05-28 02:13:49 +03:00
|
|
|
"achman";
|
2020-03-16 03:47:21 +03:00
|
|
|
|
2021-05-30 04:12:09 +03:00
|
|
|
String iniFileName = String::format("%s-%s.dat", platform, info.appId.c_str());
|
2020-03-16 03:47:21 +03:00
|
|
|
|
2020-04-29 01:56:54 +03:00
|
|
|
if (_iniFileName == iniFileName) {
|
2020-03-16 03:47:21 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isReady()) {
|
|
|
|
unsetActiveDomain();
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:38:14 +03:00
|
|
|
debug("AchievementsManager::setActiveDomain(): '%s'", iniFileName.c_str());
|
|
|
|
|
2020-04-29 01:56:54 +03:00
|
|
|
_iniFileName = iniFileName;
|
|
|
|
|
|
|
|
_iniFile = new Common::INIFile();
|
|
|
|
_iniFile->loadFromSaveFile(_iniFileName); // missing file is OK
|
2020-03-16 03:47:21 +03:00
|
|
|
|
2021-06-04 04:34:59 +03:00
|
|
|
loadAchievementsData(platform, info.appId.c_str());
|
2021-05-28 03:10:16 +03:00
|
|
|
|
2021-06-04 04:34:59 +03:00
|
|
|
for (uint32 i = 0; i < _stats.size(); i++) {
|
|
|
|
if (!(_iniFile->hasKey(_stats[i].id, "statistics"))) {
|
|
|
|
_iniFile->setKey(_stats[i].id, "statistics", _stats[i].start);
|
2021-05-31 04:23:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-30 04:12:09 +03:00
|
|
|
setSpecialString("platform", platform);
|
|
|
|
setSpecialString("gameId", info.appId);
|
|
|
|
|
2020-03-16 03:47:21 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-04 04:34:59 +03:00
|
|
|
bool AchievementsManager::loadAchievementsData(const char *platform, const char *appId) {
|
|
|
|
Archive *cfgZip = Common::makeZipArchive("achievements.dat");
|
|
|
|
if (!cfgZip) {
|
|
|
|
warning("achievements.dat is not found. Achievements messages are unavailable");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String cfgFileName = String::format("%s-%s.ini", platform, appId);
|
|
|
|
SeekableReadStream *stream = cfgZip->createReadStreamForMember(cfgFileName);
|
|
|
|
if (!stream) {
|
|
|
|
delete cfgZip;
|
|
|
|
warning("%s is not found in achievements.dat. Achievements messages are unavailable", cfgFileName.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
INIFile cfgFile;
|
|
|
|
if (!cfgFile.loadFromStream(*stream)) {
|
|
|
|
delete stream;
|
|
|
|
delete cfgZip;
|
|
|
|
warning("%s is corrupted in achievements.dat. Achievements messages are unavailable", cfgFileName.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_descriptions.clear();
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
String prefix = String::format("item_%d", i);
|
|
|
|
|
|
|
|
String id, title, comment, hidden;
|
|
|
|
cfgFile.getKey(prefix + "_id", "achievements:en", id);
|
|
|
|
cfgFile.getKey(prefix + "_title", "achievements:en", title);
|
|
|
|
cfgFile.getKey(prefix + "_comment", "achievements:en", comment);
|
|
|
|
cfgFile.getKey(prefix + "_hidden", "achievements:en", hidden);
|
|
|
|
|
|
|
|
if (id.empty()) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
_descriptions.push_back({id, title, comment, !hidden.empty()});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_stats.clear();
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
String prefix = String::format("item_%d", i);
|
|
|
|
|
|
|
|
String id, comment, start;
|
|
|
|
cfgFile.getKey(prefix + "_id", "stats:en", id);
|
|
|
|
cfgFile.getKey(prefix + "_comment", "stats:en", comment);
|
|
|
|
cfgFile.getKey(prefix + "_start", "stats:en", start);
|
|
|
|
|
|
|
|
if (id.empty()) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
_stats.push_back({id, comment, start});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete stream;
|
|
|
|
delete cfgZip;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-16 03:47:21 +03:00
|
|
|
bool AchievementsManager::unsetActiveDomain() {
|
2021-05-31 04:38:14 +03:00
|
|
|
debug("AchievementsManager::unsetActiveDomain()");
|
|
|
|
|
2020-04-29 01:56:54 +03:00
|
|
|
_iniFileName = "";
|
2020-03-16 03:47:21 +03:00
|
|
|
|
2020-04-29 01:56:54 +03:00
|
|
|
delete _iniFile;
|
|
|
|
_iniFile = nullptr;
|
2020-03-16 03:47:21 +03:00
|
|
|
|
2021-05-28 02:12:59 +03:00
|
|
|
_descriptions.clear();
|
2021-06-04 04:34:59 +03:00
|
|
|
_stats.clear();
|
2021-05-28 02:12:59 +03:00
|
|
|
|
2020-03-16 03:47:21 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-28 02:12:59 +03:00
|
|
|
bool AchievementsManager::setAchievement(const String &id) {
|
|
|
|
if (!isReady()) {
|
2021-05-31 04:38:14 +03:00
|
|
|
warning("AchievementsManager::setAchievement('%s'): AchMan not ready, did you forget to call setActiveDomain()?", id.c_str());
|
2021-05-28 02:12:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isAchieved(id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
String displayedMessage = id;
|
|
|
|
for (uint32 i = 0; i < _descriptions.size(); i++) {
|
2021-06-04 04:34:59 +03:00
|
|
|
if (_descriptions[i].id == id) {
|
2021-05-28 02:12:59 +03:00
|
|
|
displayedMessage = _descriptions[i].title;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:38:14 +03:00
|
|
|
debug("AchievementsManager::setAchievement('%s'): '%s'", id.c_str(), displayedMessage.c_str());
|
2020-03-16 03:47:21 +03:00
|
|
|
|
2020-04-29 01:56:54 +03:00
|
|
|
_iniFile->setKey(id, "achievements", "true");
|
|
|
|
_iniFile->saveToSaveFile(_iniFileName);
|
2020-03-16 03:47:21 +03:00
|
|
|
|
|
|
|
if (!displayedMessage.empty() && g_system) {
|
2020-06-13 22:12:25 +05:30
|
|
|
U32String msg;
|
2020-09-08 21:23:47 +01:00
|
|
|
msg = Common::U32String::format("%S\n%S",
|
2020-08-20 00:26:11 +05:30
|
|
|
_("Achievement unlocked!").c_str(),
|
|
|
|
Common::U32String(displayedMessage).c_str()
|
|
|
|
);
|
2020-06-13 22:12:25 +05:30
|
|
|
g_system->displayMessageOnOSD(msg);
|
2020-03-16 03:47:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-13 13:23:47 +01:00
|
|
|
bool AchievementsManager::isAchieved(const String &id) const {
|
2020-03-16 03:47:21 +03:00
|
|
|
if (!isReady()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-29 01:56:54 +03:00
|
|
|
return _iniFile->hasKey(id, "achievements");
|
2020-03-16 03:47:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool AchievementsManager::clearAchievement(const String &id) {
|
|
|
|
if (!isReady()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-29 01:56:54 +03:00
|
|
|
_iniFile->removeKey(id, "achievements");
|
|
|
|
_iniFile->saveToSaveFile(_iniFileName);
|
2020-03-16 03:47:21 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-01 23:59:40 +03:00
|
|
|
bool AchievementsManager::setStatFloatEx(const String &id, float value, const String §ion) const {
|
2020-03-16 03:47:21 +03:00
|
|
|
if (!isReady()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String tmp = Common::String::format("%8.8f", value);
|
2021-05-29 21:30:13 +03:00
|
|
|
_iniFile->setKey(id, section, tmp);
|
2020-04-29 01:56:54 +03:00
|
|
|
_iniFile->saveToSaveFile(_iniFileName);
|
2020-03-16 03:47:21 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-01 23:59:40 +03:00
|
|
|
float AchievementsManager::getStatFloatEx(const String &id, const String §ion) const {
|
2020-03-16 03:47:21 +03:00
|
|
|
if (!isReady()) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2020-04-29 01:56:54 +03:00
|
|
|
String tmp;
|
2021-05-29 21:30:13 +03:00
|
|
|
_iniFile->getKey(id, section, tmp);
|
2021-03-13 13:24:52 +01:00
|
|
|
return (float)atof(tmp.c_str());
|
2020-03-16 03:47:21 +03:00
|
|
|
}
|
|
|
|
|
2021-05-29 21:30:13 +03:00
|
|
|
|
|
|
|
bool AchievementsManager::setStatFloat(const String &id, float value) {
|
|
|
|
return setStatFloatEx(id, value, "statistics");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-01 23:59:40 +03:00
|
|
|
float AchievementsManager::getStatFloat(const String &id) const {
|
2021-05-29 21:30:13 +03:00
|
|
|
return getStatFloatEx(id, "statistics");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool AchievementsManager::updateAverageRateStatFloat(const String &id, float count, float times) {
|
|
|
|
if (!isReady()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float old_count = getStatFloatEx(id + "_count", "rates");
|
|
|
|
float old_times = getStatFloatEx(id + "_times", "rates");
|
|
|
|
|
|
|
|
setStatFloatEx(id + "_count", old_count + count, "rates");
|
|
|
|
setStatFloatEx(id + "_times", old_times + times, "rates");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-01 23:59:40 +03:00
|
|
|
float AchievementsManager::getAverageRateStatFloat(const String &id) const {
|
2021-05-29 21:30:13 +03:00
|
|
|
if (!isReady()) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float count = getStatFloatEx(id + "_count", "rates");
|
|
|
|
float times = getStatFloatEx(id + "_times", "rates");
|
|
|
|
|
|
|
|
return (times != 0) ? (count / times) : 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-16 03:47:21 +03:00
|
|
|
bool AchievementsManager::setStatInt(String const &id, int value) {
|
|
|
|
if (!isReady()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-29 01:56:54 +03:00
|
|
|
String tmp = Common::String::format("%d", value);
|
|
|
|
_iniFile->setKey(id, "statistics", tmp);
|
|
|
|
_iniFile->saveToSaveFile(_iniFileName);
|
2020-03-16 03:47:21 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-01 23:59:40 +03:00
|
|
|
int AchievementsManager::getStatInt(String const &id) const {
|
2020-03-16 03:47:21 +03:00
|
|
|
if (!isReady()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-29 01:56:54 +03:00
|
|
|
String tmp;
|
|
|
|
_iniFile->getKey(id, "statistics", tmp);
|
2021-03-13 13:24:52 +01:00
|
|
|
return (int)atol(tmp.c_str());
|
2020-03-16 03:47:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-01 23:59:40 +03:00
|
|
|
const String AchievementsManager::getStatRaw(String const &id) const {
|
2021-05-31 04:29:30 +03:00
|
|
|
if (!isReady()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
String tmp;
|
|
|
|
_iniFile->getKey(id, "statistics", tmp);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-30 04:12:09 +03:00
|
|
|
bool AchievementsManager::setSpecialString(String const &id, String const &value) {
|
|
|
|
if (!isReady()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_iniFile->setKey(id, "special", value);
|
|
|
|
_iniFile->saveToSaveFile(_iniFileName);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-16 03:47:21 +03:00
|
|
|
bool AchievementsManager::resetAllAchievements() {
|
|
|
|
if (!isReady()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-29 01:56:54 +03:00
|
|
|
_iniFile->removeSection("achievements");
|
|
|
|
_iniFile->saveToSaveFile(_iniFileName);
|
2020-03-16 03:47:21 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool AchievementsManager::resetAllStats() {
|
|
|
|
if (!isReady()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-29 01:56:54 +03:00
|
|
|
_iniFile->removeSection("statistics");
|
2021-05-29 21:30:13 +03:00
|
|
|
_iniFile->removeSection("rates");
|
2020-04-29 01:56:54 +03:00
|
|
|
_iniFile->saveToSaveFile(_iniFileName);
|
2020-03-16 03:47:21 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // End of namespace Common
|