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 | #include "gfCAchievementManager.h"
#include <fsCore/src/fsNCoreUtils.h>
#include <fsCore/src/fsCUserSavedData.h>
#include <fsCore/src/debug/fsAssert.h>
#include <fsAppCore/src/fsCProfileManager.h>
#if defined(fsPlatformAchievements)
#include <fsPlatform/achievements/fsAchievementFactory.h>
#endif
#include "gfCAchievement.h"
gfCAchievementManager* gfCAchievementManager::mInstance = nullptr;
fsBool gfCAchievementManager::allAchievementsUnlocked(fsStr const&pAchievementKey, fsStr const& pExceptFor) const
{
auto f =std::find_if(mAchievements.begin(), mAchievements.end(),
[&](gfCAchievement* const achievement)
{
return pExceptFor == achievement->nameGet() ? false :
!achievement->unlocked(pAchievementKey);
});
return mAchievements.end() == f && 0 != mAchievements.size();
}
gfCAchievement* gfCAchievementManager::achievementFind(const fsStr& pName) const
{
auto achievement = std::find_if(mAchievements.begin(), mAchievements.end(),
[pName](gfCAchievement* const achievement)
{
return achievement->nameGet() == pName;
});
if (achievement != mAchievements.end())
{
return *achievement;
}
return nullptr;
}
fsBool gfCAchievementManager::achievementCriteriaMet(const fsStr& pName) const
{
fsStr pathTrimmedName = pName;
pathTrimmedName.erase(0, pathTrimmedName.rfind('/') + 1);
if (gfCAchievement* const achievement = achievementFind(pathTrimmedName))<--- Variable 'achievement' can be declared as pointer to const
{
return achievement->criteriaMet();
}
return false;
}
fsBool gfCAchievementManager::achievementUnlocked(const fsStr& pName, const fsStr& pSaveKey) const
{
if (gfCAchievement* const achievement = achievementFind(pName))<--- Variable 'achievement' can be declared as pointer to const
{
return achievement->unlocked(pSaveKey);
}
fsStr unlockedStr = fsCProfileManager::instanceGet()->userSavedDataGet()->strGet(pSaveKey);
return fsStr::npos != unlockedStr.find(pName);
}
void gfCAchievementManager::achievementRegister(gfCAchievement* const pAchievement)
{
mAchievements.push_back(pAchievement);
}
fsStr gfCAchievementManager::achievementKeyDecode(const fsStr& pKey) const
{
return mDictionary->strGet(pKey);
}
void gfCAchievementManager::achievementDictionaryRegister(const fsStr& pFilename)
{
fsCVariableTable dictionary;
dictionary.load(fsCResourceName(pFilename, fsCResourceName::eRES_LOCATION_ASSETS));
mDictionary->merge(&dictionary);
}
#if defined(fsPlatformAchievements)
void gfCAchievementManager::externalAchievementDictionaryRegister(const fsStr& pFilename)
{
fsCVariableTable dictionary;
dictionary.load(fsCResourceName(pFilename, fsCResourceName::eRES_LOCATION_ASSETS));
mExternalDictionary->merge(&dictionary);
}
#endif
void gfCAchievementManager::achievementCallbackRegister(TAchievementCallback pCallback)
{
mCallback = pCallback;
}
fsBool gfCAchievementManager::FncMatch::operator()(const gfCAchievement& pAchievement) const
{
return mMatch == pAchievement.nameGet();
}
fsStr gfCAchievementManager::savedAchievementGet(const fsStr& pSetKey, const fsStr& pName) const
{
fsStr achievementStr = fsCProfileManager::instanceGet()->userSavedDataGet()->strGet(pSetKey);
if (fsStr::npos != achievementStr.find(pName))
{
fsStr achievement = achievementStr;
achievement.erase(0, achievement.find(pName));
return achievement;
}
return "";
}
fsStr gfCAchievementManager::savedMultipleAchievementGet(const fsStr& pSetKey, const fsStr& pName)
{
const fsStr achievementStr = fsCProfileManager::instanceGet()->userSavedDataGet()->strGet(pSetKey);
if (fsStr::npos != achievementStr.find(pName))
{
fsStr achievement = achievementStr;
achievement.erase(0, achievement.find(pName));
fsStr::sizeT numeralIndex = achievement.find_first_of("0123456789");
if (numeralIndex != fsStr::npos)
{
while (fsStr::npos != fsStr("0123456789").find((achievement[numeralIndex])))
{
++numeralIndex;
}
}
else
{
numeralIndex = pName.dataSizeGet();
}
if (numeralIndex != fsStr::npos)
{
achievement.erase(achievement.find(pName) + numeralIndex);
}
return achievement;
}
return "";
}
fsS32 gfCAchievementManager::savedMultipleAchievementCountGet(const fsStr& pSetKey, const fsStr& pName)
{
fsStr achievementStr = savedMultipleAchievementGet(pSetKey, pName);
if (!achievementStr.emptyGet())
{
if (fsStr::npos != achievementStr.find_first_of("0123456789"))
{
achievementStr.erase(0, achievementStr.find_first_of("0123456789"));
return achievementStr.toS32();
}
return 1;
}
return 0;
}
void gfCAchievementManager::externalAchievementUnlock(const fsStr& pName)
{
#if defined(fsPlatformAchievements)
if (!mExternalDictionary || !mAchievementService)
{
return;
}
const fsStr platformName(mAchievementService->platformNameGet().c_str());
fsStr platformId = mExternalDictionary->strGetWithDefault(pName + fsStr(platformName) + fsStr("Id"), "");
if (platformId.emptyGet())
{
platformId = mExternalDictionary->strGetWithDefault(pName + fsStr("PlatformId"), "");
}
if (!platformId.emptyGet())
{
mAchievementService->unlock(platformId.utf8Get());
}
#else
(void)pName;
#endif
}
void gfCAchievementManager::achievementUnlock(const fsStr& pSetKey, const fsStr& pName, const fsPoint& pPos,
fsBool pGrouping)
{
if (gfCAchievement* const achievement = achievementFind(pName))<--- Variable 'achievement' can be declared as pointer to const
{
if (achievement->criteriaMet() && !achievement->unlocked(pSetKey))
{
fsCProfileManager::instanceGet()->userSavedDataGet()->strSet(pSetKey,
fsCProfileManager::instanceGet()->
userSavedDataGet()->strGet(pSetKey) + pName);
fsAssert(mCallback != nullptr,
"Callback needs to be registered so we know how to show off the achievement");
mCallback(pName, pPos);
externalAchievementUnlock(pName);
}
}
else
{
fsStr achievementStr = savedAchievementGet(pSetKey, pName);
if (!achievementStr.emptyGet())
{
if (pGrouping)
{
achievementStr = savedMultipleAchievementGet(pSetKey, pName);
const fsS32 count = savedMultipleAchievementCountGet(pSetKey, pName) + 1;
fsStr saveStr = fsCProfileManager::instanceGet()->userSavedDataGet()->strGet(pSetKey);
saveStr.erase(saveStr.find(achievementStr), achievementStr.dataSizeGet());
fsCProfileManager::instanceGet()->userSavedDataGet()->strSet(pSetKey, saveStr + pName + fsStr(count));
}
else
{
const fsStr saveStr = fsCProfileManager::instanceGet()->userSavedDataGet()->strGet(pSetKey);
fsCProfileManager::instanceGet()->userSavedDataGet()->strSet(pSetKey, saveStr + pName);
}
}
else
{
fsCProfileManager::instanceGet()->userSavedDataGet()->strSet(pSetKey,
fsCProfileManager::instanceGet()->
userSavedDataGet()->strGet(pSetKey) + pName +
(pGrouping ? fsStr("1") : fsStr("")));
}
fsAssert(mCallback != nullptr, "Callback needs to be registered so we know how to show off the achievement");
mCallback(pName, pPos);
externalAchievementUnlock(pName);
}
}
void gfCAchievementManager::destroy()
{
delete mInstance;
mInstance = nullptr;
}
void gfCAchievementManager::create()
{
mInstance = new gfCAchievementManager();
mInstance->mDictionary = new fsCVariableTable;
#if defined(fsPlatformAchievements)
mInstance->mExternalDictionary = new fsCVariableTable;
#endif
}
gfCAchievementManager* gfCAchievementManager::instanceGet()
{
return mInstance;
}
gfCAchievementManager::gfCAchievementManager() :
mCallback(nullptr),
mDictionary(nullptr)
#if defined(fsPlatformAchievements)
,
mExternalDictionary(nullptr)
#endif
{
#if defined(fsPlatformAchievements)
mAchievementService = fsAchievementServiceCreate();<--- Variable 'mAchievementService' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'mAchievementService' a value by passing the value to the constructor in the initialization list.
mAchievementService->initialize(fsAchievementConfig());
#endif
}
gfCAchievementManager::~gfCAchievementManager()
{
fsNCoreUtils::clearSequenceContainerOfPtrs(mAchievements);
#if defined(fsPlatformAchievements)
if (mAchievementService)
{
mAchievementService->shutdown();
}
delete mExternalDictionary;
#endif
delete mDictionary;<--- Class 'gfCAchievementManager' does not have a copy constructor which is recommended since it has dynamic memory/resource management.<--- Class 'gfCAchievementManager' does not have a operator= which is recommended since it has dynamic memory/resource management.
}
|