File: achievements.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (454 lines) | stat: -rw-r--r-- 10,777 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */


#include "common/config-manager.h"
#include "common/debug.h"
#include "common/stream.h"
#include "common/system.h"
#include "common/translation.h"
#include "common/compression/unzip.h"

#include "engines/achievements.h"

namespace Common {

DECLARE_SINGLETON(AchievementsManager);


AchievementsManager::AchievementsManager() {
	_iniFile = nullptr;
	unsetActiveDomain();
}


AchievementsManager::~AchievementsManager() {
}

bool AchievementsManager::setActiveDomain(const AchievementsInfo &info) {
	if (info.appId.empty()) {
		unsetActiveDomain();
		return false;
	}

	const char* platform = info.platform == STEAM_ACHIEVEMENTS ? "steam" :
					info.platform == GALAXY_ACHIEVEMENTS ? "galaxy" :
					"achman";

	String iniFileName = String::format("%s-%s.dat", platform, info.appId.c_str());

	if (_iniFileName == iniFileName) {
		return true;
	}

	if (isReady()) {
		unsetActiveDomain();
	}

	debug(2, "AchievementsManager::setActiveDomain(): '%s'", iniFileName.c_str());

	_iniFileName = iniFileName;

	_iniFile = new Common::INIFile();
	_iniFile->loadFromSaveFile(_iniFileName); // missing file is OK

	loadAchievementsData(platform, info.appId.c_str());

	for (uint32 i = 0; i < _stats.size(); i++) {
		if (!(_iniFile->hasKey(_stats[i].id, "statistics"))) {
			_iniFile->setKey(_stats[i].id, "statistics", _stats[i].start);
		}
	}

	setSpecialString("platform", platform);
	setSpecialString("gameId", info.appId);

	return true;
}


String AchievementsManager::getCurrentLang() const {
#ifdef USE_TRANSLATION
	String uiLang = TransMan.getCurrentLanguage().c_str();
	if (_achievements.contains(uiLang)) {
		return uiLang;
	}
#endif

	return "en";
}


bool AchievementsManager::loadAchievementsData(const char *platform, const char *appId) {
	Archive *cfgZip = nullptr;

	if (!cfgZip && ConfMan.hasKey("extrapath")) {
		Common::FSDirectory extrapath(ConfMan.getPath("extrapath"));
		cfgZip = Common::makeZipArchive(extrapath.createReadStreamForMember("achievements.dat"));
	}

	if (!cfgZip) {
		cfgZip = Common::makeZipArchive("achievements.dat");
	}

	if (!cfgZip) {
		warning("achievements.dat is not found. Achievements messages are unavailable");
		return false;
	}

	SeekableReadStream *verStream = cfgZip->createReadStreamForMember("VERSION");
	if (!verStream) {
		delete cfgZip;
		warning("VERSION file is not found in achievements.dat. Achievements messages are unavailable");
		return false;
	}

	String version = verStream->readLine();
	delete verStream;

	if (version != "1") {
		delete cfgZip;
		warning("Incompatible VERSION file in achievements.dat. Achievements messages are unavailable");
		return false;
	}

	String cfgFileName = String::format("%s-%s.ini", platform, appId);
	SeekableReadStream *stream = cfgZip->createReadStreamForMember(Common::Path(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;
	}

	_achievements.clear();
	INIFile::SectionList sections = cfgFile.getSections();
	for (Common::INIFile::SectionList::const_iterator section = sections.begin(); section != sections.end(); ++section) {
		if (!(section->name.hasPrefix("achievements:"))) {
			continue;
		}

		String lang = section->name.substr(13); //strlen("achievements:")

		for (int i = 0; i < 256; i++) {
			String prefix = String::format("item_%d", i);

			String id      = section->getKey(prefix + "_id")      ? section->getKey(prefix + "_id")->value      : "";
			String title   = section->getKey(prefix + "_title")   ? section->getKey(prefix + "_title")->value   : "";
			String comment = section->getKey(prefix + "_comment") ? section->getKey(prefix + "_comment")->value : "";
			String hidden  = section->getKey(prefix + "_hidden")  ? section->getKey(prefix + "_hidden")->value  : "";

			if (id.empty()) {
				break;
			} else {
				AchievementDescription desc = { id, title, comment, !hidden.empty() };
				_achievements[lang].push_back(desc);
			}
		}
	}

	_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 {
			StatDescription desc = {id, comment, start};
			_stats.push_back(desc);
		}
	}

	delete stream;
	delete cfgZip;
	return true;
}


bool AchievementsManager::unsetActiveDomain() {
	debug(2, "AchievementsManager::unsetActiveDomain()");

	_iniFileName = "";

	delete _iniFile;
	_iniFile = nullptr;

	_achievements.clear();
	_stats.clear();

	return true;
}


bool AchievementsManager::setAchievement(const String &id) {
	if (!isReady()) {
		warning("AchievementsManager::setAchievement('%s'): AchMan not ready, did you forget to call setActiveDomain()?", id.c_str());
		return false;
	}
	if (isAchieved(id)) {
		return true;
	}

	const String &lang = getCurrentLang();

	String displayedMessage = id;
	if (_achievements.contains(lang)) {
		for (uint32 i = 0; i < _achievements[lang].size(); i++) {
			if (_achievements[lang][i].id == id) {
				displayedMessage = _achievements[lang][i].title;
				break;
			}
		}
	}

	debug(2, "AchievementsManager::setAchievement('%s'): '%s'", id.c_str(), displayedMessage.c_str());

	_iniFile->setKey(id, "achievements", "true");
	_iniFile->saveToSaveFile(_iniFileName);

	if (!ConfMan.getBool("disable_achievement_unlocked_osd") && !displayedMessage.empty() && g_system) {
		U32String msg;
		msg = Common::U32String::format("%S\n%S",
			_("Achievement unlocked!").c_str(),
			Common::U32String(displayedMessage).c_str()
		);
		g_system->displayMessageOnOSD(msg);
	}

	return true;
}


bool AchievementsManager::isAchieved(const String &id) const {
	if (!isReady()) {
		return false;
	}

	return _iniFile->hasKey(id, "achievements");
}


bool AchievementsManager::clearAchievement(const String &id) {
	if (!isReady()) {
		return false;
	}

	_iniFile->removeKey(id, "achievements");
	_iniFile->saveToSaveFile(_iniFileName);
	return true;
}


bool AchievementsManager::setStatFloatEx(const String &id, float value, const String &section) const {
	if (!isReady()) {
		return false;
	}

	String tmp = Common::String::format("%8.8f", value);
	_iniFile->setKey(id, section, tmp);
	_iniFile->saveToSaveFile(_iniFileName);
	return 0;
}


float AchievementsManager::getStatFloatEx(const String &id, const String &section) const {
	if (!isReady()) {
		return 0.0;
	}

	String tmp;
	_iniFile->getKey(id, section, tmp);
	return (float)atof(tmp.c_str());
}


bool AchievementsManager::setStatFloat(const String &id, float value) {
	return setStatFloatEx(id, value, "statistics");
}


float AchievementsManager::getStatFloat(const String &id) const {
	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;
}


float AchievementsManager::getAverageRateStatFloat(const String &id) const {
	if (!isReady()) {
		return 0.0;
	}

	float count = getStatFloatEx(id + "_count", "rates");
	float times = getStatFloatEx(id + "_times", "rates");

	return (times != 0) ? (count / times) : 0.0;
}


bool AchievementsManager::setStatInt(String const &id, int value) {
	if (!isReady()) {
		return false;
	}

	String tmp = Common::String::format("%d", value);
	_iniFile->setKey(id, "statistics", tmp);
	_iniFile->saveToSaveFile(_iniFileName);
	return 0;
}


int AchievementsManager::getStatInt(String const &id) const {
	if (!isReady()) {
		return 0;
	}

	String tmp;
	_iniFile->getKey(id, "statistics", tmp);
	return (int)atol(tmp.c_str());
}


const String AchievementsManager::getStatRaw(String const &id) const {
	if (!isReady()) {
		return "";
	}

	String tmp;
	_iniFile->getKey(id, "statistics", tmp);
	return tmp;
}


bool AchievementsManager::setSpecialString(String const &id, String const &value) {
	if (!isReady()) {
		return false;
	}

	_iniFile->setKey(id, "special", value);
	_iniFile->saveToSaveFile(_iniFileName);
	return 0;
}


bool AchievementsManager::resetAllAchievements() {
	if (!isReady()) {
		return false;
	}

	_iniFile->removeSection("achievements");
	_iniFile->saveToSaveFile(_iniFileName);
	return 0;
}


bool AchievementsManager::resetAllStats() {
	if (!isReady()) {
		return false;
	}

	_iniFile->removeSection("statistics");
	_iniFile->removeSection("rates");
	_iniFile->saveToSaveFile(_iniFileName);
	return 0;
}


uint16 AchievementsManager::getAchievementCount() const {
	if (!isReady()) {
		return 0;
	}

	const String &lang = getCurrentLang();
	if (!_achievements.contains(lang)) {
		return 0;
	}

	return _achievements[lang].size();
}


const AchievementDescription *AchievementsManager::getAchievementDescription(uint16 index) const {
	if (!isReady()) {
		return nullptr;
	}

	const String &lang = getCurrentLang();
	if (!_achievements.contains(lang)) {
		return nullptr;
	}

	if (index >= _achievements[lang].size()) {
		return nullptr;
	}

	return &(_achievements[lang][index]);
}


uint16 AchievementsManager::getStatCount() const {
	if (!isReady()) {
		return 0;
	}

	return _stats.size();
}


const StatDescription *AchievementsManager::getStatDescription(uint16 index) const {
	if (!isReady()) {
		return nullptr;
	}

	if (index >= _stats.size()) {
		return nullptr;
	}

	return &(_stats[index]);
}


} // End of namespace Common