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 | #include "fsCloudSaveServiceAndroid.h"
#include <platform/android/jni/JniHelper.h>
#include <mutex>
#include <vector>
#if defined COCOS2D_X
#define ENGINE cocos2d
#elif defined AX
#define ENGINE ax
#elif defined fsFRAMEWORK_MG
#elif defined fsFRAMEWORK_RL
#else
#error Unhandled platform.
#endif
namespace
{
constexpr const char* kCloudSaveBridgeClass = "com/casualarts/uk/fsPlatform/FsPlatformCloudSaveBridge";
// Must match FsPlatformCloudSaveBridge.RESULT_*.
fsCloudSaveResult resultFromJava(jint result)
{
switch (result)
{
case 0: return fsCloudSaveResult::eOK;
case 1: return fsCloudSaveResult::eNOT_FOUND;
case 2: return fsCloudSaveResult::eUNAVAILABLE;
case 3: return fsCloudSaveResult::eQUOTA_EXCEEDED;
default: return fsCloudSaveResult::eERROR;
}
}
struct Completion
{
int requestId;<--- Member variable 'Completion::requestId' has no initializer. [+]Member variable 'Completion::requestId' has no initializer. Member variables of native types, pointers, or references are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior.
fsCloudSaveResult result;<--- Member variable 'Completion::result' has no initializer. [+]Member variable 'Completion::result' has no initializer. Member variables of native types, pointers, or references are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior.
std::string data;
};
std::mutex sCompletionsMutex;
std::vector<Completion> sCompletions;
void completionPush(jint requestId, jint result, std::string data)
{
std::lock_guard<std::mutex> lock(sCompletionsMutex);
sCompletions.push_back({requestId, resultFromJava(result), std::move(data)});
}
bool bridgeMethodGet(ENGINE::JniMethodInfo& methodInfo, const char* methodName, const char* signature)
{
return ENGINE::JniHelper::getStaticMethodInfo(methodInfo, kCloudSaveBridgeClass, methodName, signature);
}
bool bridgeCallRequest(const char* methodName, int requestId, const std::string& name, const std::string* data)
{
ENGINE::JniMethodInfo methodInfo;
const char* signature = data ? "(ILjava/lang/String;[B)V" : "(ILjava/lang/String;)V";
if (!bridgeMethodGet(methodInfo, methodName, signature))
{
return false;
}
jstring nameValue = methodInfo.env->NewStringUTF(name.c_str());
if (data)
{
jbyteArray bytes = methodInfo.env->NewByteArray(static_cast<jsize>(data->size()));
methodInfo.env->SetByteArrayRegion(bytes, 0, static_cast<jsize>(data->size()),
reinterpret_cast<const jbyte*>(data->data()));
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, requestId, nameValue, bytes);
methodInfo.env->DeleteLocalRef(bytes);
}
else
{
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, requestId, nameValue);
}
methodInfo.env->DeleteLocalRef(nameValue);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
return true;
}
bool bridgeCallNoArgs(const char* methodName)
{
ENGINE::JniMethodInfo methodInfo;
if (!bridgeMethodGet(methodInfo, methodName, "()V"))
{
return false;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
return true;
}
}
extern "C"
{
JNIEXPORT void JNICALL Java_com_casualarts_uk_fsPlatform_FsPlatformCloudSaveBridge_nativeOnRead(
JNIEnv* env, jclass, jint requestId, jint result, jbyteArray data)
{
std::string bytes;
if (data)
{
const jsize size = env->GetArrayLength(data);
bytes.resize(static_cast<std::size_t>(size));
if (size > 0)
{
env->GetByteArrayRegion(data, 0, size, reinterpret_cast<jbyte*>(&bytes[0]));
}
}
completionPush(requestId, result, std::move(bytes));
}
JNIEXPORT void JNICALL Java_com_casualarts_uk_fsPlatform_FsPlatformCloudSaveBridge_nativeOnDone(
JNIEnv*, jclass, jint requestId, jint result)
{
completionPush(requestId, result, std::string());
}
}
fsCloudSaveServiceAndroid::~fsCloudSaveServiceAndroid()
{
shutdown();
}
void fsCloudSaveServiceAndroid::initialize(const fsCloudSaveConfig& config)
{
if (!config.providerName.empty())
{
mProviderName = config.providerName;
}
mBridgeFound = bridgeCallNoArgs("initialize");
}
const std::string& fsCloudSaveServiceAndroid::providerNameGet() const
{
return mProviderName;
}
bool fsCloudSaveServiceAndroid::availableGet() const
{
// Sign-in state is only known asynchronously; requests report eUNAVAILABLE when signed out.
return mBridgeFound;
}
void fsCloudSaveServiceAndroid::read(const std::string& name, ReadCallback done)
{
const int requestId = requestIdNext();
mReads[requestId] = done;
if (!bridgeCallRequest("read", requestId, name, nullptr))
{
completionPush(requestId, 4, std::string());
}
}
void fsCloudSaveServiceAndroid::write(const std::string& name, const std::string& data, DoneCallback done)
{
const int requestId = requestIdNext();
mDones[requestId] = done;
if (!bridgeCallRequest("write", requestId, name, &data))
{
completionPush(requestId, 4, std::string());
}
}
void fsCloudSaveServiceAndroid::remove(const std::string& name, DoneCallback done)
{
const int requestId = requestIdNext();
mDones[requestId] = done;
if (!bridgeCallRequest("remove", requestId, name, nullptr))
{
completionPush(requestId, 4, std::string());
}
}
void fsCloudSaveServiceAndroid::update()
{
std::vector<Completion> completions;
{
std::lock_guard<std::mutex> lock(sCompletionsMutex);
completions.swap(sCompletions);
}
for (auto& completion : completions)
{
const auto read = mReads.find(completion.requestId);<--- Shadow local variable
if (read != mReads.end())
{
const ReadCallback done = read->second;
mReads.erase(read);
done(completion.result, completion.data);
continue;
}
const auto other = mDones.find(completion.requestId);
if (other != mDones.end())
{
const DoneCallback done = other->second;
mDones.erase(other);
done(completion.result);
}
}
}
void fsCloudSaveServiceAndroid::shutdown()
{
update();
mReads.clear();
mDones.clear();
}
int fsCloudSaveServiceAndroid::requestIdNext()
{
return mNextRequestId++;
}
|