Skip to content

Commit 20351b0

Browse files
authored
Merge pull request #49 from defisym/EOS_FIX
Callbacks protect & log state over frames
2 parents b5ba2df + e056380 commit 20351b0

File tree

22 files changed

+216
-99
lines changed

22 files changed

+216
-99
lines changed

Extensions/AndroidUtilities/ToInstall/Files/Data/Runtime/Unicode/.keep

Whitespace-only changes.

Extensions/AndroidUtilities/ToInstall/Files/Extensions/Unicode/.keep

Whitespace-only changes.

Extensions/EpicOnlineServices/Definition.h

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,17 @@ inline auto AuthTypeComboListEnumToLoginCredentialType(AuthTypeComboListEnum com
4949
return EOS_ELoginCredentialType::EOS_LCT_ExchangeCode;
5050
}
5151

52+
struct LogOpt {
53+
bool bAutoLogin = false;
54+
bool bAutoLogout = false;
55+
56+
bool bLoginCalled = false;
57+
bool bUserLogin = false;
58+
};
59+
5260
struct GlobalData {
5361
LPRDATA rdPtr = nullptr;
62+
LogOpt logOpt;
5463

5564
EOSUtilities* pEOSUtilities = nullptr;
5665
EOSAchievement* pEOSAchievement = nullptr;
@@ -84,7 +93,7 @@ struct GlobalData {
8493
}
8594

8695
inline void EOSUpdate() const {
87-
//EOSUpdatePlatform();
96+
EOSUpdatePlatform();
8897
pEOSUtilities->Update();
8998
}
9099

@@ -128,7 +137,8 @@ struct GlobalData {
128137

129138
inline bool EOSInit(LPEDATA edPtr);
130139

131-
inline void EOSInitPlatform() {
140+
// same as platform functions, but do them together
141+
inline void EOSAllocPlatform() {
132142
pEOSAchievement = new EOSAchievement(pEOSUtilities);
133143
pEOSStat = new EOSStat(pEOSUtilities);
134144
pEOSPresence = new EOSPresence(pEOSUtilities);
@@ -139,21 +149,57 @@ struct GlobalData {
139149
delete pEOSStat;
140150
delete pEOSPresence;
141151
}
142-
152+
153+
inline void EOSInitPlatform() const {
154+
pEOSAchievement->PlatformInit();
155+
pEOSStat->PlatformInit();
156+
pEOSPresence->PlatformInit();
157+
}
158+
159+
inline void EOSQueryPlatform() const {
160+
pEOSAchievement->PlatformQuery();
161+
pEOSStat->PlatformQuery();
162+
pEOSPresence->PlatformQuery();
163+
}
164+
143165
inline void EOSUpdatePlatform() const {
144166
pEOSAchievement->PlatformUpdate();
145167
pEOSStat->PlatformUpdate();
146168
pEOSPresence->PlatformUpdate();
147169
}
148170

149-
// init platform here
150-
inline void EOSLoginSuccess() const {
151-
pEOSAchievement->PlatformInit();
152-
pEOSStat->PlatformInit();
153-
pEOSPresence->PlatformInit();
171+
inline bool EOSCallbackComplete()const {
172+
bool bFinish = true;
173+
174+
auto checkCallback = [] (auto* pEOS) {
175+
return pEOS != nullptr
176+
? pEOS->AllCallbackComplete()
177+
: true;
178+
};
179+
180+
bFinish = bFinish && checkCallback(pEOSAchievement);
181+
bFinish = bFinish && checkCallback(pEOSStat);
182+
bFinish = bFinish && checkCallback(pEOSPresence);
183+
bFinish = bFinish && checkCallback(pEOSUtilities);
184+
185+
return bFinish;
186+
}
187+
188+
inline void EOSWaitForCallbackComplete() const {
189+
while (!EOSCallbackComplete()) {
190+
//OutputDebugStringA("Wait...\n");
191+
EOSUpdate();
192+
}
193+
194+
//OutputDebugStringA("Complete!\n");
154195
}
155196

156-
inline void EOSLogin(const std::function<void(bool)>& callback) const {
197+
private:
198+
using LogResultCallback = std::function<void(bool)>;
199+
inline const static LogResultCallback defaultCb = [] (bool) {};
200+
201+
public:
202+
inline void EOSLogin(const LogResultCallback& callback = defaultCb) const {
157203
if (!pEOSUtilities || !pEOSUtilities->Init()) {
158204
callback(false);
159205

@@ -177,15 +223,23 @@ struct GlobalData {
177223
}
178224

179225
if (state == EOSState::ConnectSuccess) {
180-
EOSLoginSuccess();
226+
EOSInitPlatform();
181227
callback(true);
182228
}
183229
});
184230
}
185231
});
186232
}
187233

188-
inline void EOSLogout(const std::function<void(bool)>& callback) const {
234+
inline void EOSAutoLogin(const LogResultCallback& callback = defaultCb) {
235+
if (logOpt.bAutoLogin && !logOpt.bLoginCalled) {
236+
logOpt.bLoginCalled = true;
237+
// login will add callback count internally
238+
EOSLogin(callback);
239+
}
240+
}
241+
242+
inline void EOSLogout(const LogResultCallback& callback = defaultCb) const {
189243
if (!pEOSUtilities && pEOSUtilities->State() < EOSState::AuthSuccess) {
190244
callback(false);
191245

@@ -197,4 +251,11 @@ struct GlobalData {
197251
callback(pEU->State() == EOSState::InitSuccess);
198252
});
199253
}
254+
255+
inline void EOSAutoLogout(const LogResultCallback& callback = defaultCb) const {
256+
if (logOpt.bAutoLogout && logOpt.bUserLogin) {
257+
// logout will add callback count internally
258+
EOSLogout(callback);
259+
}
260+
}
200261
};

Extensions/EpicOnlineServices/EOSAchievement.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// https://dev.epicgames.com/docs/zh-Hans/game-services/achievements
99
// https://dev.epicgames.com/zh-CN/news/adding-achievements-to-your-game
1010

11-
class EOSAchievement :private PlatformBase {
11+
class EOSAchievement :public PlatformBase {
1212
private:
1313
using CallbackType = std::function<void(EOSAchievement*)>;
1414
inline const static CallbackType defaultCb = [] (EOSAchievement*) {};
@@ -21,9 +21,9 @@ class EOSAchievement :private PlatformBase {
2121
explicit EOSAchievement(EOSUtilities* pEU) : PlatformBase(pEU) {}
2222
~EOSAchievement() override = default;
2323
inline void PlatformInit() override {
24-
QueryAchievementDefinitions();
24+
PlatformQuery();
2525
}
26-
inline void PlatformUpdate() override {
26+
inline void PlatformQuery() override {
2727
QueryAchievementDefinitions();
2828
}
2929

@@ -41,8 +41,10 @@ class EOSAchievement :private PlatformBase {
4141
opt.ApiVersion = EOS_ACHIEVEMENTS_QUERYDEFINITIONS_API_LATEST;
4242
opt.LocalUserId = pEU->productUserId;
4343

44+
callbackCounter.CallCallback();
4445
EOS_Achievements_QueryDefinitions(achHandle, &opt, this, [] (const EOS_Achievements_OnQueryDefinitionsCompleteCallbackInfo* Data) {
4546
const auto pEA = static_cast<decltype(this)>(Data->ClientData);
47+
CallbackCounterHelper callbackCounterHelper(pEA->callbackCounter);
4648

4749
if (!EOSUtilities::EOSOK(Data->ResultCode)) {
4850
pEA->pEU->SetLastError("Achievement", "Failed to query definition", Data->ResultCode);
@@ -75,9 +77,11 @@ class EOSAchievement :private PlatformBase {
7577
unlockAchievementsOptions.AchievementIds = pArray;
7678
unlockAchievementsOptions.AchievementsCount = sz;
7779

80+
callbackCounter.CallCallback();
7881
EOS_Achievements_UnlockAchievements(achHandle, &unlockAchievementsOptions, this,
7982
[] (const EOS_Achievements_OnUnlockAchievementsCompleteCallbackInfo* Data) {
8083
const auto pEA = static_cast<decltype(this)>(Data->ClientData);
84+
CallbackCounterHelper callbackCounterHelper(pEA->callbackCounter);
8185

8286
if (!EOSUtilities::EOSOK(Data->ResultCode)) {
8387
pEA->pEU->SetLastError("Achievement", "Failed to unlock achievement", Data->ResultCode);
@@ -164,7 +168,7 @@ class EOSAchievement :private PlatformBase {
164168
auto count = GetAchievementCount();
165169

166170
for (decltype(count) i = 0; i < count; i++) {
167-
GetAchievementByIndex(i, cb);
171+
auto bRet = GetAchievementByIndex(i, cb);
168172
}
169173
}
170174
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include <atomic>
4+
5+
class CallbackCounter {
6+
private:
7+
std::atomic<size_t> callbackCount = 0;
8+
9+
public:
10+
inline void CallCallback() { ++callbackCount; }
11+
inline void FinishCallback() { --callbackCount; }
12+
inline bool AllCallbackComplete() const { return callbackCount == 0; }
13+
};
14+
15+
class CallbackCounterHelper {
16+
public:
17+
CallbackCounter& callbackCounter;
18+
CallbackCounterHelper(CallbackCounter& cc) : callbackCounter(cc) {}
19+
~CallbackCounterHelper() {
20+
callbackCounter.FinishCallback();
21+
}
22+
};

Extensions/EpicOnlineServices/EOSCommandLine.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
#include "GeneralDefinition.h"
55

66
// Example:
7-
// -AUTH_LOGIN=unused -AUTH_PASSWORD=<password> -AUTH_TYPE=exchangecode -epicapp=<appid> -epicenv=Prod -EpicPortal -epicusername=<username> -epicuserid=<userid> -epiclocale=en-US -epicsandboxid=<sandboxid>
8-
// -AUTH_LOGIN=unused -AUTH_PASSWORD=<password> -AUTH_TYPE=exchangecode -epicapp=<appid> -epicenv=Prod -EpicPortal -epicusername=<username>-epicuserid<userid> -epiclocale=en-US
7+
// -AUTH_LOGIN=unused -AUTH_PASSWORD=<password> -AUTH_TYPE=exchangecode -epicapp=<appid> -epicenv=Prod -EpicPortal -epicusername=<username> -epicuserid=<userid> -epiclocale=en-US -epicsandboxid=<sandboxid> -epicdeploymentid=<deploymentid>
98

109
// https://eoshelp.epicgames.com/s/case/5004z00001rezlvAAA/login-with-exchange-code?language=zh_CN
1110
// the Launcher will use the equals sign, so any custom parsing that you do should factor this in, for example:
@@ -24,6 +23,7 @@ class EOSCommandLine {
2423
static constexpr const char* EOSCommandLine_EpicUserID = "-epicUserID";
2524
static constexpr const char* EOSCommandLine_EpicLocal = "-epicLocal";
2625
static constexpr const char* EOSCommandLine_EpicSandboxID = "-epicSandboxID";
26+
static constexpr const char* EOSCommandLine_EpicDeploymentID = "-epicDeploymentid";
2727

2828
static constexpr char EOSCommandLine_Dash = '-';
2929
static constexpr char EOSCommandLine_Equal = '=';
@@ -101,6 +101,12 @@ class EOSCommandLine {
101101
break;
102102
}
103103

104+
105+
if (StrIEqu(item.c_str(), EOSCommandLine_EpicDeploymentID)) {
106+
epicDeploymentID = cur.content;
107+
108+
break;
109+
}
104110
} while (false);
105111
}
106112

@@ -117,6 +123,7 @@ class EOSCommandLine {
117123
std::string epicUserID;
118124
std::string epicLocal;
119125
std::string epicSandboxID;
126+
std::string epicDeploymentID;
120127

121128
EOSCommandLine() {
122129
// get command line

Extensions/EpicOnlineServices/EOSPlatformBase.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
#pragma once
22

3+
#include "EOSCallbackCounter.h"
34
#include "EOSUtilities.h"
45

56
class PlatformBase {
67
protected:
78
EOSUtilities* pEU = nullptr;
9+
CallbackCounter callbackCounter;
810

911
public:
1012
explicit PlatformBase(EOSUtilities* pEU) {
1113
this->pEU = pEU;
1214
}
1315
virtual ~PlatformBase() = default;
1416

17+
// init platform, usually need to query data first
1518
virtual inline void PlatformInit() = 0;
16-
virtual inline void PlatformUpdate() = 0;
19+
// query data
20+
virtual inline void PlatformQuery() = 0;
21+
// handle update task if needed
22+
virtual inline void PlatformUpdate() {}
1723

1824
inline bool PlatformOK() const {
1925
return pEU->PlatformOK();
2026
}
27+
28+
inline bool AllCallbackComplete() const {
29+
return callbackCounter.AllCallbackComplete();
30+
}
2131
};
2232

2333
// ------------
@@ -41,5 +51,5 @@ class PlatformBase {
4151
// explicit EOSStat(EOSUtilities* pEU) : PlatformBase(pEU) {}
4252
// ~EOSStat() override = default;
4353
// inline void PlatformInit() override {}
44-
// inline void PlatformUpdate() override {}
54+
// inline void PlatformQuery() override {}
4555
//};

0 commit comments

Comments
 (0)